forked from krstak/memorydb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
205 lines (167 loc) · 5.3 KB
/
db.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
package memorydb
import (
"errors"
"fmt"
"reflect"
"strconv"
"sync"
)
var ErrItemNotFound = errors.New("item not found")
type Manager struct {
identifier *string
// Add adds new item in the collection. It return new id as interface{} value
Add func(item interface{}, collection string) interface{}
// FindAll finds all items in the collection. It return slice of interface{}
FindAll func(collection string) []interface{}
// FindById finds an item in the collection by given id. It returns item as interface{} and error if item can't be found
FindById func(id interface{}, collection string) (interface{}, error)
// FindBy finds an item in the collection by given field and value. It returns item as interface{} and error if item can't be found
FindBy func(field, value, collection string) (interface{}, error)
// Update updates an item with given id in the collection. It returns error if item can't be updated
Update func(id interface{}, item interface{}, collection string) error
// Remove removes an item from the collection. It returns error if item can't be removed
Remove func(id interface{}, collection string) error
}
// Identifier sets identifier for an entity
// Example: Identifier("ID")
// Default identifier is "Id"
func (m *Manager) Identifier(key string) {
*m.identifier = key
}
type col struct {
lastId int64
items []interface{}
}
func Create() Manager {
db := make(map[string]col)
var syn sync.RWMutex
ident := "Id"
return Manager{
identifier: &ident,
Add: add(&ident, db, &syn),
FindAll: getAll(db, &syn),
FindById: findById(&ident, db, &syn),
FindBy: findBy(db, &syn),
Remove: remove(&ident, db, &syn),
Update: update(&ident, db, &syn),
}
}
func getAll(db map[string]col, syn *sync.RWMutex) func(string) []interface{} {
return func(collection string) []interface{} {
syn.RLock()
defer syn.RUnlock()
c, ok := db[collection]
if !ok {
return make([]interface{}, 0)
}
return c.items
}
}
func add(identifierName *string, db map[string]col, syn *sync.RWMutex) func(interface{}, string) interface{} {
return func(item interface{}, collection string) interface{} {
t := checkType(item, *identifierName)
v := reflect.ValueOf(item).Elem().FieldByName(*identifierName)
syn.Lock()
defer syn.Unlock()
lastId := db[collection].lastId
newId := lastId + 1
var id interface{}
switch t.Kind() {
case reflect.Int:
id = int(newId)
case reflect.Int8:
id = int8(newId)
case reflect.Int16:
id = int16(newId)
case reflect.Int32:
id = int32(newId)
case reflect.Int64:
id = int64(newId)
case reflect.Uint:
id = uint(newId)
case reflect.Uint8:
id = uint8(newId)
case reflect.Uint16:
id = uint16(newId)
case reflect.Uint32:
id = uint32(newId)
case reflect.Uint64:
id = uint64(newId)
case reflect.String:
strId := strconv.FormatInt(newId, 10)
id = strId
default:
panic(fmt.Sprintf("type %s is not supported as identifier" + t.Kind().String()))
}
idValue := reflect.ValueOf(id)
v.Set(idValue)
items := append(db[collection].items, item)
c := col{lastId: newId, items: items}
db[collection] = c
return id
}
}
func remove(identifierName *string, db map[string]col, syn *sync.RWMutex) func(interface{}, string) error {
return func(id interface{}, collection string) error {
_, index, err := find(db, *identifierName, id, collection, syn)
if err != nil {
return err
}
syn.Lock()
defer syn.Unlock()
items := append(db[collection].items[:index], db[collection].items[index+1:]...)
c := col{lastId: db[collection].lastId, items: items}
db[collection] = c
return nil
}
}
func update(identifierName *string, db map[string]col, syn *sync.RWMutex) func(interface{}, interface{}, string) error {
return func(id interface{}, item interface{}, collection string) error {
checkType(item, *identifierName)
_, index, err := find(db, *identifierName, id, collection, syn)
if err != nil {
return err
}
syn.Lock()
defer syn.Unlock()
idValue := reflect.ValueOf(id)
v := reflect.ValueOf(item).Elem().FieldByName(*identifierName)
v.Set(idValue)
db[collection].items[index] = item
return nil
}
}
func findById(identifierName *string, db map[string]col, syn *sync.RWMutex) func(interface{}, string) (interface{}, error) {
return func(id interface{}, collection string) (interface{}, error) {
item, _, err := find(db, *identifierName, id, collection, syn)
return item, err
}
}
func findBy(db map[string]col, syn *sync.RWMutex) func(string, string, string) (interface{}, error) {
return func(field, value, collection string) (interface{}, error) {
item, _, err := find(db, field, value, collection, syn)
return item, err
}
}
func find(db map[string]col, field string, value interface{}, collection string, syn *sync.RWMutex) (interface{}, int, error) {
items := getAll(db, syn)(collection)
for i, v := range items {
val := valueOf(v, field)
if val == value {
return v, i, nil
}
}
return nil, 0, ErrItemNotFound
}
func valueOf(item interface{}, field string) interface{} {
checkType(item, field)
v := reflect.ValueOf(item).Elem().FieldByName(field)
return v.Interface()
}
func checkType(item interface{}, field string) reflect.Type {
t, ok := reflect.TypeOf(item).Elem().FieldByName(field)
if !ok {
panic(fmt.Sprintf("field '%s' doesn't exist", field))
}
return t.Type
}