-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtable.go
397 lines (328 loc) · 8.7 KB
/
table.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package bingodb
import (
"encoding/json"
"errors"
"fmt"
"github.com/zoyi/skiplist/lib"
"reflect"
"strconv"
"sync"
)
type FieldSchema struct {
Name string
Type string
}
type KeySchema struct {
hashKey *FieldSchema
sortKey *FieldSchema
}
type TableSchema struct {
fields map[string]*FieldSchema
primaryKey *KeySchema
subKeys map[string]*KeySchema
expireField *FieldSchema
}
type Table struct {
*TableSchema
bingo *Bingo
name string
primaryIndex *PrimaryIndex
subIndices map[string]*SubIndex
mutex *sync.Mutex
rowLocks *sync.Map
metricsConfig *MetricsConfig
expireKeyRequired bool
}
type TableInfo struct {
Name string `json:"name"`
Size int `json:"size"`
SubIndices map[string]int64 `json:"subIndices,omitempty"`
ExpireKeyRequired bool `json:"expireKeyRequired"`
}
func (table *Table) Info() *TableInfo {
subIndices := make(map[string]int64)
for key, index := range table.subIndices {
subIndices[key] = index.size
}
return &TableInfo{
Name: table.name,
Size: int(table.primaryIndex.size),
SubIndices: subIndices,
ExpireKeyRequired: table.expireKeyRequired}
}
type KeyTuple struct {
hash interface{}
sort interface{}
}
type SubSortKey struct {
sort interface{}
primaryHash interface{}
primarySort interface{}
}
func (key SubSortKey) Array() interface{} {
return []interface{}{key.sort, key.primaryHash, key.primarySort}
}
func newTable(
bingo *Bingo,
tableName string,
schema *TableSchema,
primaryIndex *PrimaryIndex,
subIndices map[string]*SubIndex,
metricsConfig *MetricsConfig,
expireKeyRequired bool,
) *Table {
return &Table{
TableSchema: schema,
bingo: bingo,
name: tableName,
primaryIndex: primaryIndex,
subIndices: subIndices,
mutex: new(sync.Mutex),
rowLocks: new(sync.Map),
metricsConfig: metricsConfig,
expireKeyRequired: expireKeyRequired,
}
}
func (table *Table) makeMetricsTable() *Table {
if table.metricsConfig == nil {
return nil
}
metricsConfig := table.metricsConfig
tableName := fmt.Sprintf("_%s", table.name)
fields := make(map[string]*FieldSchema)
fields[table.HashKey().Name] = table.HashKey()
if len(metricsConfig.Time) == 0 {
metricsConfig.Time = "time"
}
if len(metricsConfig.Count) == 0 {
metricsConfig.Count = "count"
}
if len(metricsConfig.ExpireKey) == 0 {
metricsConfig.ExpireKey = "expireAt"
}
fields[metricsConfig.Time] = &FieldSchema{Name: metricsConfig.Time, Type: "integer"}
fields[metricsConfig.Count] = &FieldSchema{Name: metricsConfig.Count, Type: "integer"}
fields[metricsConfig.ExpireKey] = &FieldSchema{Name: metricsConfig.ExpireKey, Type: "integer"}
primaryKey := &KeySchema{
hashKey: fields[table.HashKey().Name],
sortKey: fields[metricsConfig.Time],
}
schema := &TableSchema{
fields: fields,
primaryKey: primaryKey,
expireField: fields[metricsConfig.ExpireKey]}
primaryIndex := &PrimaryIndex{index: newIndex(primaryKey)}
subIndices := make(map[string]*SubIndex)
metricsTable := newTable(table.bingo, tableName, schema, primaryIndex, subIndices, nil, true)
table.bingo.tables[tableName] = metricsTable
return metricsTable
}
func (table *Table) HashKey() *FieldSchema {
return table.primaryKey.hashKey
}
func (table *Table) SortKey() *FieldSchema {
return table.primaryKey.sortKey
}
func ParseField(field *FieldSchema, raw interface{}) interface{} {
if field == nil {
return nil
} else {
val, err := field.Parse(raw)
if err != nil {
return nil
}
return val
}
}
func (field *FieldSchema) Parse(raw interface{}) (interface{}, error) {
switch field.Type {
case "integer":
switch raw.(type) {
case json.Number:
value, err := raw.(json.Number).Int64()
return value, err
case string:
value, err := strconv.ParseInt(raw.(string), 10, 64)
return value, err
case []byte:
value, err := strconv.ParseInt(string(raw.([]byte)), 10, 64)
return value, err
case int:
return int64(raw.(int)), nil
case int64:
return raw.(int64), nil
case float64:
return int64(raw.(float64)), nil
case []interface{}:
return field.Parse(raw.([]interface{})[0])
}
case "string":
switch raw.(type) {
case []byte:
if bytes := raw.([]byte); len(bytes) > 0 {
return string(raw.([]byte)), nil
} else {
return nil, nil
}
case []interface{}:
return field.Parse(raw.([]interface{})[0])
case string:
if len(raw.(string)) > 0 {
return raw, nil
}
}
}
return raw, fmt.Errorf(FieldError, field.Name, field.Type, raw)
}
func NumberComparator(a, b interface{}) int {
aAsserted := a.(int64)
bAsserted := b.(int64)
switch {
case aAsserted > bAsserted:
return 1
case aAsserted < bAsserted:
return -1
default:
return 0
}
}
func GeneralCompare(a, b interface{}) int {
if a == nil || b == nil {
if a == b {
return 0
} else if a == nil {
return -1
} else {
return 1
}
}
if reflect.TypeOf(a).Kind() == reflect.Int64 {
return NumberComparator(a, b)
}
return lib.StringComparator(a, b)
}
func (schema *TableSchema) Compare(a, b *Document) int {
if a == nil || b == nil {
if a == b {
return 0
} else if a == nil {
return -1
} else {
return 1
}
}
if diff := GeneralCompare(a.Get(schema.primaryKey.hashKey), b.Get(schema.primaryKey.hashKey)); diff != 0 {
return diff
}
return GeneralCompare(a.Get(schema.primaryKey.sortKey), b.Get(schema.primaryKey.sortKey))
}
func (table *Table) Index(name string) IndexInterface {
if len(name) == 0 {
return table.primaryIndex
} else if index, ok := table.subIndices[name]; ok {
return index
}
return nil
}
func (table *Table) PrimaryIndex() *PrimaryIndex {
return table.primaryIndex
}
func (table *Table) Put(setData *Data, setOnInsertData *Data) (*Document, *Document, bool, error) {
set, setErr := ParseDoc(setData, table.TableSchema)
setOnInsert, setOnInsertErr := ParseDoc(setOnInsertData, table.TableSchema)
if setErr != nil {
return nil, nil, false, setErr
}
if setOnInsertErr != nil {
return nil, nil, false, setOnInsertErr
}
if set == nil && setOnInsert == nil {
return nil, nil, false, errors.New(SetOrInsertMissing)
}
merged := Merge(set, setOnInsert)
if merged.Fetch(table.HashKey().Name) == nil {
return nil, nil, false, errors.New(HashKeyMissing)
}
if table.SortKey() != nil && merged.Fetch(table.SortKey().Name) == nil {
return nil, nil, false, errors.New(SortKeyMissing)
}
if table.expireKeyRequired && merged.Fetch(table.expireField.Name) == nil {
return nil, nil, false, errors.New(ExpireKeyMissing)
}
onUpdate := func(oldRaw interface{}) interface{} {
old := oldRaw.(*Document)
return old.Merge(set)
}
table.mutex.Lock()
defer table.mutex.Unlock()
//keyTuple := merged.NewKeyTuple(table.primaryKey)
//mutex := table.lockForRead(keyTuple)
//defer mutex.Unlock()
// Insert doc into primary index
old, newbie, replaced := table.primaryIndex.put(merged, onUpdate)
// Update for sub index
for _, index := range table.subIndices {
if replaced {
index.remove(old)
}
index.put(newbie)
}
// Update for event list
keeper := table.bingo.keeper
if replaced {
keeper.remove(table, old)
}
keeper.put(table, newbie)
return old, newbie, replaced, nil
}
func (table *Table) Remove(hash interface{}, sort interface{}) (*Document, error) {
//keyTuple, err := table.parseKey(hash, sort)
//if err != nil {
// return nil, err
//}
table.mutex.Lock()
defer table.mutex.Unlock()
//value, ok := table.rowLocks.Load(*keyTuple)
//if ok {
// mutex := value.(*sync.Mutex)
// mutex.Lock()
// defer mutex.Unlock()
// defer table.rowLocks.Delete(*keyTuple)
//}
doc, err := table.primaryIndex.remove(hash, sort)
if err != nil {
return nil, err
}
for _, index := range table.subIndices {
index.remove(doc)
}
table.bingo.keeper.remove(table, doc)
return doc, nil
}
func (table *Table) RemoveByDocument(doc *Document) (*Document, error) {
hashValue := doc.Get(table.primaryKey.hashKey)
sortValue := doc.Get(table.primaryKey.sortKey)
return table.Remove(hashValue, sortValue)
}
//
func (table *Table) parseKey(hashRaw, sortRaw interface{}) (*KeyTuple, error) {
hash := ParseField(table.primaryKey.hashKey, hashRaw)
sort := ParseField(table.primaryKey.sortKey, sortRaw)
if hash == nil {
return nil, errors.New(HashKeyMissing)
}
if sort == nil {
return nil, errors.New(SortKeyMissing)
}
return &KeyTuple{hash: hash, sort: sort}, nil
}
//func (table *Table) lockForRead(keyTuple *KeyTuple) *sync.Mutex {
// table.mutex.RLock()
// defer table.mutex.RUnlock()
//
// value, _ := table.rowLocks.LoadOrStore(*keyTuple, new(sync.Mutex))
// mutex := value.(*sync.Mutex)
// mutex.Lock()
//
// return mutex
//}