forked from EdwinWalela/clover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem.go
254 lines (206 loc) · 4.68 KB
/
mem.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package clover
import (
"sort"
"sync"
)
type collection map[string]*Document
type memEngine struct {
sync.RWMutex
collections map[string]collection
}
func newMemStorageEngine() StorageEngine {
return &memEngine{
RWMutex: sync.RWMutex{},
collections: make(map[string]collection),
}
}
// Close implements StorageEngine
func (*memEngine) Close() error {
return nil
}
// CreateCollection implements StorageEngine
func (e *memEngine) CreateCollection(name string) error {
e.Lock()
defer e.Unlock()
if e.hasCollection(name) {
return ErrCollectionExist
}
e.collections[name] = make(collection)
return nil
}
// Delete implements StorageEngine
func (e *memEngine) Delete(q *Query) error {
return e.replaceDocs(q, func(_ *Document) *Document {
return nil
})
}
// DeleteById implements StorageEngine
func (e *memEngine) DeleteById(collectionName string, id string) error {
e.Lock()
defer e.Unlock()
c, ok := e.collections[collectionName]
if !ok {
return ErrCollectionNotExist
}
delete(c, id)
return nil
}
func (e *memEngine) hasCollection(collectionName string) bool {
_, ok := e.collections[collectionName]
return ok
}
// DropCollection implements StorageEngine
func (e *memEngine) DropCollection(name string) error {
e.Lock()
defer e.Unlock()
if e.hasCollection(name) {
delete(e.collections, name)
} else {
return ErrCollectionNotExist
}
return nil
}
// FindAll implements StorageEngine
func (e *memEngine) FindAll(q *Query) ([]*Document, error) {
docs := []*Document{}
err := e.IterateDocs(q, func(doc *Document) error {
if q.satisfy(doc) {
docs = append(docs, doc)
}
return nil
})
return docs, err
}
// FindById implements StorageEngine
func (e *memEngine) FindById(collectionName string, id string) (*Document, error) {
e.RLock()
defer e.RUnlock()
c, ok := e.collections[collectionName]
if !ok {
return nil, ErrCollectionNotExist
}
return c[id], nil
}
// HasCollection implements StorageEngine
func (e *memEngine) HasCollection(name string) (bool, error) {
e.RLock()
defer e.RUnlock()
_, ok := e.collections[name]
return ok, nil
}
// Insert implements StorageEngine
func (e *memEngine) Insert(collection string, docs ...*Document) error {
e.Lock()
defer e.Unlock()
c, ok := e.collections[collection]
if !ok {
return ErrCollectionNotExist
}
for _, d := range docs {
if c[d.ObjectId()] != nil {
return ErrDuplicateKey
}
}
for _, d := range docs {
c[d.ObjectId()] = d
}
return nil
}
func (e *memEngine) iterateDocs(q *Query, consumer docConsumer) error {
c, ok := e.collections[q.collection]
if !ok {
return ErrCollectionNotExist
}
sortDocs := len(q.sortOpts) > 0
allDocs := []*Document{}
for _, d := range c {
if q.satisfy(d) {
allDocs = append(allDocs, d)
}
}
if sortDocs {
sort.Slice(allDocs, func(i, j int) bool {
return compareDocuments(allDocs[i], allDocs[j], q.sortOpts) < 0
})
}
docsToSkip := q.skip
if len(allDocs) < q.skip {
docsToSkip = len(allDocs)
}
allDocs = allDocs[docsToSkip:]
if q.limit >= 0 && len(allDocs) > q.limit {
allDocs = allDocs[:q.limit]
}
for _, doc := range allDocs {
err := consumer(doc)
if err == errStopIteration {
return nil
}
if err != nil {
return err
}
}
return nil
}
// IterateDocs implements StorageEngine
func (e *memEngine) IterateDocs(q *Query, consumer docConsumer) error {
e.RLock()
defer e.RUnlock()
return e.iterateDocs(q, consumer)
}
// Open implements StorageEngine
func (e *memEngine) Open(path string) error {
return nil
}
// Update implements StorageEngine
func (e *memEngine) Update(q *Query, updater func(doc *Document) *Document) error {
return e.replaceDocs(q, updater)
}
func (e *memEngine) UpdateById(collectionName string, docId string, updater func(doc *Document) *Document) error {
e.Lock()
defer e.Unlock()
c, ok := e.collections[collectionName]
if !ok {
return ErrCollectionNotExist
}
doc, has := c[docId]
if !has {
return ErrDocumentNotExist
}
c[docId] = updater(doc)
return nil
}
func (e *memEngine) replaceDocs(q *Query, updater docUpdater) error {
e.Lock()
defer e.Unlock()
c, ok := e.collections[q.collection]
if !ok {
return ErrCollectionNotExist
}
docs := make([]*Document, 0)
e.iterateDocs(q, func(doc *Document) error {
if q.satisfy(doc) {
docs = append(docs, doc)
}
return nil
})
for _, doc := range docs {
key := doc.ObjectId()
if q.satisfy(doc) {
newDoc := updater(doc)
if newDoc == nil {
delete(c, key)
} else {
c[key].fields = newDoc.fields
}
}
}
return nil
}
func (e *memEngine) ListCollections() ([]string, error) {
collections := make([]string, 0)
for name := range e.collections {
collections = append(collections, name)
}
return collections, nil
}