mirror of
https://github.com/minio/minio.git
synced 2025-03-29 08:43:40 -04:00
Merge pull request #103 from harshavardhana/pr_out_add_database_query_function
This commit is contained in:
commit
9e5b689dc5
@ -1 +0,0 @@
|
|||||||
package database
|
|
@ -1,20 +1,27 @@
|
|||||||
package database
|
package tiedot
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/HouzuoGuo/tiedot/db"
|
"github.com/HouzuoGuo/tiedot/db"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewDatabase() *Database {
|
func NewDatabase(dirname string) (*Database, error) {
|
||||||
return &Database{}
|
data := Database{}
|
||||||
|
data.setdbdir(dirname)
|
||||||
|
if err := data.setDBhandle(); err != nil {
|
||||||
|
return &Database{}, err
|
||||||
|
}
|
||||||
|
return &data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (data *Database) setdbdir(dirname string) {
|
func (data *Database) setdbdir(dirname string) {
|
||||||
data.DBdir = dirname
|
data.DBdir = dirname
|
||||||
}
|
}
|
||||||
|
|
||||||
func (data *Database) GetDBHandle(dirname string) error {
|
func (data *Database) setDBhandle() error {
|
||||||
var err error
|
var err error
|
||||||
data.setdbdir(dirname)
|
|
||||||
data.DBhandle, err = db.OpenDB(data.DBdir)
|
data.DBhandle, err = db.OpenDB(data.DBdir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -28,7 +35,7 @@ func (data *Database) InitCollection(colls ...string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (data *Database) GetCollections() []string {
|
func (data *Database) GetAllCollections() []string {
|
||||||
var colls []string
|
var colls []string
|
||||||
for _, name := range data.DBhandle.AllCols() {
|
for _, name := range data.DBhandle.AllCols() {
|
||||||
colls = append(colls, name)
|
colls = append(colls, name)
|
||||||
@ -40,12 +47,28 @@ func (data *Database) getCollectionHandle(coll string) *db.Col {
|
|||||||
return data.DBhandle.Use(coll)
|
return data.DBhandle.Use(coll)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (data *Database) GetCollectionData(coll string, docid int) (map[string]interface{}, error) {
|
|
||||||
collHandle := data.getCollectionHandle(coll)
|
|
||||||
return collHandle.Read(docid)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (data *Database) InsertToCollection(coll string, model map[string]interface{}) (docid int, err error) {
|
func (data *Database) InsertToCollection(coll string, model map[string]interface{}) (docid int, err error) {
|
||||||
collHandle := data.getCollectionHandle(coll)
|
collHandle := data.getCollectionHandle(coll)
|
||||||
return collHandle.Insert(model)
|
return collHandle.Insert(model)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (data *Database) InsertIndexToCollection(coll string, indexes []string) error {
|
||||||
|
collHandle := data.getCollectionHandle(coll)
|
||||||
|
return collHandle.Index(indexes)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (data *Database) QueryDB(coll string, queryByte []byte) (map[int]struct{}, error) {
|
||||||
|
if len(queryByte) <= 0 {
|
||||||
|
return nil, fmt.Errorf("Invalid query string")
|
||||||
|
}
|
||||||
|
|
||||||
|
var query interface{}
|
||||||
|
json.Unmarshal(queryByte, &query)
|
||||||
|
|
||||||
|
queryResult := make(map[int]struct{}) // query result (document IDs) goes into map keys
|
||||||
|
err := db.EvalQuery(query, data.getCollectionHandle(coll), &queryResult)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return queryResult, nil
|
||||||
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package database
|
package tiedot
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
@ -14,9 +14,9 @@ type MySuite struct{}
|
|||||||
var _ = Suite(&MySuite{})
|
var _ = Suite(&MySuite{})
|
||||||
|
|
||||||
func (s *MySuite) Testing(c *C) {
|
func (s *MySuite) Testing(c *C) {
|
||||||
d := NewDatabase()
|
d, err := NewDatabase("/tmp/testdata")
|
||||||
d.GetDBHandle("/tmp/testdata")
|
|
||||||
defer os.RemoveAll("/tmp/testdata")
|
defer os.RemoveAll("/tmp/testdata")
|
||||||
|
c.Assert(err, IsNil)
|
||||||
|
|
||||||
d.InitCollection("Matrix")
|
d.InitCollection("Matrix")
|
||||||
|
|
||||||
@ -26,11 +26,11 @@ func (s *MySuite) Testing(c *C) {
|
|||||||
"language": "Go",
|
"language": "Go",
|
||||||
}
|
}
|
||||||
|
|
||||||
docId, err1 := d.InsertToCollection("Matrix", data)
|
_, err1 := d.InsertToCollection("Matrix", data)
|
||||||
c.Assert(err1, IsNil)
|
c.Assert(err1, IsNil)
|
||||||
|
|
||||||
retdata, err2 := d.GetCollectionData("Matrix", docId)
|
var indexes []string
|
||||||
|
indexes = []string{"version", "url", "language"}
|
||||||
|
err2 := d.InsertIndexToCollection("Matrix", indexes)
|
||||||
c.Assert(err2, IsNil)
|
c.Assert(err2, IsNil)
|
||||||
c.Assert(data, DeepEquals, retdata)
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package database
|
package tiedot
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/HouzuoGuo/tiedot/db"
|
"github.com/HouzuoGuo/tiedot/db"
|
Loading…
x
Reference in New Issue
Block a user