-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmapDriver.go
220 lines (207 loc) · 5.5 KB
/
mapDriver.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
package storageDriver
import (
"fmt"
"io"
"reflect"
"sync"
)
type mapDriver struct {
database string
collection string
sync.Mutex
store map[string]map[string][]Document
}
func (d *mapDriver) Driver() (StorageDriver, error) {
if d.database == "" || d.collection == "" {
return nil, fmt.Errorf("database or collection is not set")
}
return d, nil
}
func (d *mapDriver) AggregateMongo(doc []Document) ([]Document, error) {
return nil, fmt.Errorf("not implemented")
}
func (d *mapDriver) Gt(Doc Document) Document { return nil }
func (d *mapDriver) Gte(Doc Document) Document { return nil }
func (d *mapDriver) Lt(Doc Document) Document { return nil }
func (d *mapDriver) Lte(Doc Document) Document { return nil }
func (d *mapDriver) In(key string, values []interface{}) Document { return nil }
func (d *mapDriver) Between(key string, values [2]interface{}) Document { return nil }
func (d *mapDriver) Not(Doc Document) Document { return nil }
func (d *mapDriver) Regex(key string, value string) Document { return nil }
func (d *mapDriver) Cursor() Cursor { return DummyCursor{} }
func (m *mapDriver) DB(name string) error {
if name == "" {
return fmt.Errorf("empty name")
}
m.database = name
return nil
}
func (m *mapDriver) Table(name string) error {
if name == "" {
return fmt.Errorf("empty name")
}
m.collection = name
return nil
}
func (m *mapDriver) Clone() Meta {
var cpy = *m
return &cpy
}
func (d *mapDriver) Get(Query Document) ([]Document, error) {
if _, ok := d.store[d.database]; !ok {
d.store[d.database] = make(map[string][]Document)
}
var docs = make([]Document, 0)
var match bool = true
d.Lock()
defer d.Unlock()
for _, DBDoc := range d.store[d.database][d.collection] {
for k, v := range Query {
if val, ok := DBDoc[k]; !ok || !reflect.DeepEqual(val, v) {
match = false
goto next
}
}
if match {
docs = append(docs, DBDoc)
}
next:
match = true
}
var err error
if len(docs) == 0 {
err = fmt.Errorf("no documents found")
}
return docs, err
}
func (d *mapDriver) Insert(doc Document) error {
if _, ok := d.store[d.database]; !ok {
d.store[d.database] = make(map[string][]Document)
}
d.Lock()
defer d.Unlock()
d.store[d.database][d.collection] = append(d.store[d.database][d.collection], doc)
return nil
}
func (d *mapDriver) GetOne(Query Document) (Document, error) {
if _, ok := d.store[d.database]; !ok {
d.store[d.database] = make(map[string][]Document)
}
var match bool = true
d.Lock()
defer d.Unlock()
for _, DBDoc := range d.store[d.database][d.collection] {
for k, v := range Query {
if val, ok := DBDoc[k]; !ok || !reflect.DeepEqual(val, v) {
match = false
goto next
}
}
if match {
return DBDoc, nil
}
next:
match = true
}
return nil, fmt.Errorf("no documents found")
}
func (d *mapDriver) Custom(_ interface{}) ([]Document, error) {
if _, ok := d.store[d.database]; !ok {
d.store[d.database] = make(map[string][]Document)
}
return nil, fmt.Errorf("not implemented yet")
}
func (d *mapDriver) InsertMulti(docs []Document) error {
if _, ok := d.store[d.database]; !ok {
d.store[d.database] = make(map[string][]Document)
}
for _, doc := range docs {
d.Insert(doc)
}
return nil
}
func (d *mapDriver) InsertMultiNoFail(docs []Document, _ ...io.Writer) []error {
if _, ok := d.store[d.database]; !ok {
d.store[d.database] = make(map[string][]Document)
}
d.InsertMulti(docs)
return nil
}
func (d *mapDriver) Update(Query Document, UpdatedFields Document) error {
if _, ok := d.store[d.database]; !ok {
d.store[d.database] = make(map[string][]Document)
}
doc, err := d.GetOne(Query)
if nil != err {
return err
}
d.Lock()
defer d.Unlock()
for k, v := range UpdatedFields {
doc[k] = v
}
return nil
}
func (d *mapDriver) UpdateMulti(Query, UpdatedFields Document) (int, error) {
if _, ok := d.store[d.database]; !ok {
d.store[d.database] = make(map[string][]Document)
}
docs, err := d.Get(Query)
if nil != err {
return 0, err
}
d.Lock()
defer d.Unlock()
for _, doc := range docs {
for k, v := range UpdatedFields {
doc[k] = v
}
}
return len(docs), nil
}
func (d *mapDriver) Save(Query, Doc Document) error {
if _, ok := d.store[d.database]; !ok {
d.store[d.database] = make(map[string][]Document)
}
doc, err := d.GetOne(Query)
if nil != err {
dd := make(Document)
for k, v := range Query {
dd[k] = v
}
for k, v := range Doc {
dd[k] = v
}
return d.Insert(dd)
}
return d.Update(doc, Doc)
}
func (d *mapDriver) Remove(Query Document) error {
if _, ok := d.store[d.database]; !ok {
d.store[d.database] = make(map[string][]Document)
}
var match bool = true
d.Lock()
defer d.Unlock()
for docIndex, DBDoc := range d.store[d.database][d.collection] {
for k, v := range Query {
if val, ok := DBDoc[k]; !ok || !reflect.DeepEqual(val, v) {
match = false
goto next
}
}
if match {
d.store[d.database][d.collection] = append(d.store[d.database][d.collection][:docIndex], d.store[d.database][d.collection][docIndex+1:]...)
return nil
}
next:
match = true
}
return fmt.Errorf("no document removed")
}
func NewMapDriver() Meta {
fmt.Println("!MapDriver has been deprecated and will be removed in the future releases of storageDriver please use other in memory driver alternatives like ql driver")
var driver = new(mapDriver)
driver.store = make(map[string]map[string][]Document)
return driver
}