/
db.go
253 lines (212 loc) · 4.48 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
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
package db
import (
"crypto/sha1"
"fmt"
"sync"
"github.com/ability-sh/abi-db/source"
"github.com/ability-sh/abi-lib/dynamic"
"github.com/ability-sh/abi-lib/json"
"github.com/dop251/goja"
)
const wCount = 0x100
type exec func(s source.Source, vm *goja.Runtime)
type context struct {
C chan error
}
func (c *context) Recycle() {
close(c.C)
}
type collection struct {
key string
s source.Source
ss chan exec
}
type collectionCursor struct {
n int
s source.Cursor
}
func (rs *collectionCursor) Next() (string, error) {
r, e := rs.s.Next()
if e != nil {
return "", e
}
return r[rs.n:], nil
}
func (rs *collectionCursor) Close() {
rs.s.Close()
}
type database struct {
s source.Source
ss []chan exec
w sync.WaitGroup
}
func Open(s source.Source) Database {
v := &database{s: s}
v.ss = make([]chan exec, wCount)
for i := 0; i < wCount; i++ {
v.ss[i] = make(chan exec, 64)
v.w.Add(1)
go func(C chan exec) {
defer v.w.Done()
vm := goja.New()
vm.Set("get", func(key string) string {
b, err := s.Get(key)
if err != nil {
if err == source.ErrNoSuchKey {
return ""
}
panic(vm.ToValue(err.Error()))
}
return string(b)
})
vm.Set("put", func(key string, data string) {
err := s.Put(key, []byte(data))
if err != nil {
panic(vm.ToValue(err.Error()))
}
})
vm.Set("del", func(key string) {
err := s.Del(key)
if err != nil {
panic(vm.ToValue(err.Error()))
}
})
for {
cmd, ok := <-C
if !ok {
break
}
if cmd == nil {
break
}
cmd(s, vm)
}
}(v.ss[i])
}
return v
}
func keyIndex(key string) int {
m := sha1.New()
m.Write([]byte(key))
b := m.Sum(nil)
return int(uint8(b[0]))
}
func (db *database) Collection(key string) Collection {
return &collection{key: key, ss: db.ss[keyIndex(key)], s: db.s}
}
func (ct *collection) Key() string {
return ct.key
}
func (ct *collection) Put(c Context, key string, data []byte) error {
k := fmt.Sprintf("%s%s", ct.key, key)
cc := c.(*context)
ct.ss <- func(s source.Source, vm *goja.Runtime) {
cc.C <- s.Put(k, data)
}
return <-cc.C
}
func (ct *collection) Del(c Context, key string) error {
k := fmt.Sprintf("%s%s", ct.key, key)
cc := c.(*context)
ct.ss <- func(s source.Source, vm *goja.Runtime) {
cc.C <- s.Del(k)
}
return <-cc.C
}
func (ct *collection) Get(c Context, key string) ([]byte, error) {
k := fmt.Sprintf("%s%s", ct.key, key)
return ct.s.Get(k)
}
func (ct *collection) GetObject(c Context, key string) (interface{}, error) {
k := fmt.Sprintf("%s%s", ct.key, key)
b, err := ct.s.Get(k)
if err != nil {
return nil, err
}
var r interface{} = nil
err = json.Unmarshal(b, &r)
return r, err
}
func (ct *collection) PutObject(c Context, key string, object interface{}) error {
b, err := json.Marshal(object)
if err != nil {
return err
}
return ct.Put(c, key, b)
}
func (ct *collection) MergeObject(c Context, key string, object interface{}) error {
k := fmt.Sprintf("%s%s", ct.key, key)
cc := c.(*context)
ct.ss <- func(s source.Source, vm *goja.Runtime) {
var v interface{} = nil
b, err := s.Get(k)
if err != nil {
if err != source.ErrNoSuchKey {
cc.C <- err
return
}
} else {
json.Unmarshal(b, &v)
}
if v == nil {
v = object
} else {
dynamic.Each(object, func(key interface{}, value interface{}) bool {
dynamic.Set(v, dynamic.StringValue(key, ""), value)
return true
})
}
b, err = json.Marshal(v)
if err != nil {
cc.C <- err
return
}
cc.C <- s.Put(k, b)
}
return <-cc.C
}
func (ct *collection) Query(c Context, prefix string) (source.Cursor, error) {
k := fmt.Sprintf("%s%s", ct.key, prefix)
rs, err := ct.s.Query(k, "")
if err != nil {
return nil, err
}
return &collectionCursor{n: len(ct.key), s: rs}, nil
}
type execResult struct {
data string
}
func (e *execResult) Error() string {
return e.data
}
func (ct *collection) Exec(c Context, code string) (string, error) {
k := ct.key
cc := c.(*context)
ct.ss <- func(s source.Source, vm *goja.Runtime) {
vm.Set("collection", k)
rs, err := vm.RunString(code)
if err != nil {
cc.C <- err
return
}
cc.C <- &execResult{data: rs.String()}
}
err := <-cc.C
r, ok := err.(*execResult)
if ok {
return r.data, nil
}
return "", err
}
func (db *database) NewContext() Context {
return &context{C: make(chan error)}
}
func (db *database) Recycle() {
for i := 0; i < wCount; i++ {
db.ss[i] <- nil
}
db.w.Wait()
for i := 0; i < wCount; i++ {
close(db.ss[i])
}
}