-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdoclite.go
160 lines (137 loc) · 3.52 KB
/
doclite.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package doclite
import (
"encoding/json"
"errors"
"github.com/haibeey/doclite/doclite"
)
var (
//ErrNotFound error for document not found
//export ErrNotFound
ErrNotFound = errors.New("not Found")
)
// Doclite holds an intance of the Database object
type Doclite struct {
db *doclite.DB
}
// GetDB returns the doclite databse
func (d *Doclite) GetDB() *doclite.DB {
return d.db
}
// Commit saves all current changes on the database
func (d *Doclite) Commit() {
d.db.Save()
}
// Collection holds a collection
type Collection struct {
tree *doclite.Btree
}
// GetCol returns the collection tree
func (c *Collection) GetCol() *doclite.Btree {
return c.tree
}
// Connect returns an instance of Doclite object database
func Connect(filename string) *Doclite {
db := doclite.OpenDB(filename)
return &Doclite{db: db}
}
/*
Close closes the Database
Close must be called at exit of the process interacting with doclite.
*/
func (d *Doclite) Close() error {
return d.db.Close()
}
// Base returns the root Collection of the database
func (d *Doclite) Base() *Collection {
return &Collection{tree: d.db.Connect()}
}
// Collection returns a sub-collection in a collection
func (c *Collection) Collection(collectionName string) *Collection {
return &Collection{tree: c.tree.Get(collectionName)}
}
// Name returns the name of the sub collection
func (c *Collection) Name() string {
return c.tree.Name
}
/*
Insert add a new document to the collection and returns the id of the inserted document or an error.
The id return are not binding i.e when the document is deleted another new document would take up the id.
Doc is a go struct object holding some data example
type Employer struct {
Name string
Address string
}
e:=&Employer{name:"joe",address:"doe"}
collection.Insert(e)
*/
func (c *Collection) Insert(doc interface{}) (int64, error) {
buf, err := json.Marshal(doc)
if err != nil {
return -1, err
}
defer c.Commit()
return c.tree.Insert(buf), nil
}
// DeleteOne deletes a document from the database
// When document is deleted a new document take up it space and id
func (c *Collection) DeleteOne(id int64) {
c.tree.Delete(id)
}
// Delete remove all document matching filter from the database
func (c *Collection) Delete(filter, doc interface{}) {
c.tree.DeleteAll(filter, doc)
}
/*
FindOne find a document by id matching the doc struct. Example below.
type Employer struct {
Name string
Address string
}
e:=&Employer{}
collection.FindOne(e)
fmt.Println(e.name,e.address)
*/
func (c *Collection) FindOne(id int64, doc interface{}) error {
n, err := c.tree.Find(id)
if err != nil {
return err
}
if n == nil {
return ErrNotFound
}
return json.Unmarshal(n.Doc().Data(), doc)
}
/*
Find returns a cursor object containing all matching document of type doc.
filter is of type struct or map. it use to select matching argument of type docs.
type Employer struct {
Name string
Address string
}
e=&Employer{}
joe:=&Employer{Address:"doe"}
cur:=baseCollection.Find(joe,e)
for {
emp:=cur.Next()
if emp==nil{
fmt.Println(emp,"is nil at",i)
break
}
fmt.Println(emp)
}
*/
func (c *Collection) Find(filter interface{}) *doclite.Cursor {
return c.tree.FindAll(filter)
}
// Commit saves all current changes
func (c *Collection) Commit() {
c.tree.Save()
}
// UpdateOneDoc is used update an existing document by id in the database with a new document.
func (c *Collection) UpdateOneDoc(id int64, doc interface{}) error {
buf, err := json.Marshal(doc)
if err != nil {
return err
}
return c.tree.Update(id, buf)
}