This repository has been archived by the owner on Oct 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
fdata.go
565 lines (494 loc) · 12.8 KB
/
fdata.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
package factoid
import (
"bytes"
"database/sql"
"fmt"
"time"
"github.com/lib/pq"
"github.com/pkg/errors"
"github.com/riking/marvin/lualib"
"github.com/riking/marvin/util"
"github.com/yuin/gopher-lua"
)
const (
sqlMigrate3 = `CREATE TABLE module_factoid_data (
id SERIAL PRIMARY KEY,
map text NOT NULL,
key text NOT NULL,
data json,
UNIQUE(map, key)
)`
// $1 = map $2 = key
sqlFDataGetOne = `SELECT data FROM module_factoid_data WHERE map = $1 AND key = $2`
// $1 = map
sqlFDataGetAll = `SELECT key, data FROM module_factoid_data WHERE map = $1`
_sqlFDataTempTableName = `module_factoid_data_load`
sqlFDataMakeTempTable = `CREATE TEMPORARY TABLE module_factoid_data_load
(map text NOT NULL, key text NOT NULL, data json)
ON COMMIT DROP`
// (!!) No syntax checks
sqlFDataSetFromTemp = `INSERT INTO module_factoid_data (map, key, data)
SELECT map, key, data FROM module_factoid_data_load
ON CONFLICT (map, key) DO UPDATE SET data = EXCLUDED.data`
// TODO use this when volume low
sqlFDataSet = `INSERT INTO module_factoid_data (map, key, data)
VALUES ($1, $2, $3)
ON CONFLICT (map, key) DO UPDATE SET data = EXCLUDED.data`
)
const (
fdataKeyMaxLen = 500
fdataValMaxLen = 1024 * 40
)
type fdataKey struct {
Map string
Key string
}
type fdataVal struct {
JSON []byte
// DBSync value meanings:
// - TriYes when the value is fresh with the database.
// - TriNo when the value is dirty and needs to be saved.
// - TriDefault when the entry is missing
DBSync util.TriValue
}
type fdataReqFunc func(*FactoidModule) interface{}
type fdataReq struct {
C chan interface{}
F fdataReqFunc
}
func (mod *FactoidModule) workerFDataChan() {
for req := range mod.fdataReqChan {
req.C <- req.F(mod)
}
}
func (mod *FactoidModule) workerFDataSync() {
var (
responseChan = make(chan interface{})
timerChan <-chan time.Time
updateData map[fdataKey][]byte
doneWaiting bool
)
for {
// Wait for dirty value write
q := <-mod.fdataSyncSignal
// Grace period (skipped if q was true)
timerChan = time.After(30 * time.Second)
doneWaiting = q
for !doneWaiting {
select {
case q = <-mod.fdataSyncSignal:
if q == true {
doneWaiting = true
}
case <-timerChan:
doneWaiting = true
}
}
timerChan = nil
fmt.Println("[fdata] Saving factoid data...")
// Gather all dirty data
updateData = make(map[fdataKey][]byte)
mod.fdataReqChan <- fdataReq{C: responseChan,
F: func(mod *FactoidModule) interface{} {
var key fdataKey
for mapName, mapContent := range mod.fdataMap {
key.Map = mapName
for keyName, keyContent := range mapContent {
if keyContent.DBSync == util.TriYes {
continue
}
key.Key = keyName
updateData[key] = keyContent.JSON
}
}
return nil
},
}
<-responseChan
// Write to database
err := mod.fdataSaveToDBBulk(updateData)
if err != nil {
util.LogError(errors.Wrap(err, "Failed saving factoid persistent data"))
}
fmt.Println("[fdata] Marking factoid data as saved")
// Flag data in the cache as saved
mod.fdataReqChan <- fdataReq{C: responseChan,
F: func(mod *FactoidModule) interface{} {
if err != nil {
return nil
}
for key, val := range updateData {
m := mod.fdataMap[key.Map]
if m == nil {
continue
}
q := m[key.Key]
if bytes.Equal(m[key.Key].JSON, val) {
q.DBSync = util.TriYes
m[key.Key] = q
} else {
q.DBSync = util.TriNo
m[key.Key] = q
}
}
return nil
},
}
<-responseChan
fmt.Println("[fdata] mark done")
}
}
func (mod *FactoidModule) fdataSaveToDBBulk(data map[fdataKey][]byte) (err error) {
tx, err := mod.team.DB().Begin()
if err != nil {
return errors.Wrap(err, "begin transaction")
}
txOK := false
defer func() {
if !txOK {
tx.Rollback()
}
}()
_, err = tx.Exec(sqlFDataMakeTempTable) // CREATE TEMPORARY TABLE ... ON COMMIT DROP
if err != nil {
return errors.Wrap(err, "create temporary table")
}
stmt, err := tx.Prepare(pq.CopyIn(_sqlFDataTempTableName, "map", "key", "data"))
for key, val := range data {
if val == nil {
_, err = stmt.Exec(string(key.Map), string(key.Key), nil)
} else {
_, err = stmt.Exec(string(key.Map), string(key.Key), string(val))
}
if err != nil {
return errors.Wrap(err, "loading COPY data")
}
}
_, err = stmt.Exec()
if err != nil {
return errors.Wrap(err, "flush COPY data")
}
err = stmt.Close()
if err != nil {
return errors.Wrap(err, "close COPY stmt")
}
_, err = tx.Exec(sqlFDataSetFromTemp) // INSERT INTO ... SELECT ... ON CONFLICT DO UPDATE ...
if err != nil {
return errors.Wrap(err, "move from temporary to real table")
}
err = tx.Commit()
if err != nil {
return errors.Wrap(err, "commit transaction")
}
txOK = true
return nil
}
func (mod *FactoidModule) fdataTriggerSync() {
select {
case mod.fdataSyncSignal <- false:
default:
// Already working on it ¯\_(ツ)_/¯
}
}
func (mod *FactoidModule) GetFDataAll(mapName string) (map[string][]byte, error) {
stmt, err := mod.team.DB().Prepare(sqlFDataGetAll)
if err != nil {
return nil, errors.Wrap(err, "prepare stmt")
}
rows, err := stmt.Query(mapName)
if err != nil {
return nil, errors.Wrapf(err, "fdata query map=%s", mapName)
}
result := make(map[string][]byte)
var b []byte
var key string
for rows.Next() {
err = rows.Scan(&key, &b)
if err != nil {
return nil, errors.Wrapf(err, "fdata scan map=%s", mapName)
}
if len(b) == 0 {
result[key] = nil
} else {
result[key] = b
}
}
if rows.Err() != nil {
return nil, errors.Wrapf(err, "fdata query map=%s", mapName)
}
// Fill in dirty values that haven't hit the DB
mod.fdataFreshenMap(mapName, result)
return result, nil
}
func (mod *FactoidModule) fdataDBGetOne(mapName, keyName string) ([]byte, error) {
stmt, err := mod.team.DB().Prepare(sqlFDataGetOne)
if err != nil {
return nil, errors.Wrap(err, "prepare stmt")
}
var data []byte
row := stmt.QueryRow(mapName, keyName)
err = row.Scan(&data)
if err == sql.ErrNoRows {
return nil, nil
} else if err != nil {
return nil, errors.Wrap(err, "query")
}
return data, nil
}
// fdataVal states:
// - DBSync is 0, JSON is nil - unknown status, check the DB
// - DBSync is yes/no, JSON is nil - no value in DB
// - DBSync is yes/no, JSON not nil - value
func (mod *FactoidModule) fdataGetCachedEntry(mapName, keyName string) fdataVal {
ch := make(chan interface{})
mod.fdataReqChan <- fdataReq{C: ch,
F: func(mod *FactoidModule) interface{} {
m := mod.fdataMap[mapName]
if m == nil {
return fdataVal{JSON: nil, DBSync: util.TriDefault}
}
return m[keyName]
},
}
return (<-ch).(fdataVal)
}
func (mod *FactoidModule) fdataFreshenMap(mapName string, content map[string][]byte) {
ch := make(chan interface{})
mod.fdataReqChan <- fdataReq{C: ch,
F: func(mod *FactoidModule) interface{} {
m := mod.fdataMap[mapName]
if m == nil {
return nil
}
for k, v := range m {
content[k] = v.JSON
}
return nil
},
}
<-ch
return
}
func fdataFuncSetEntry(mapName, keyName string, data []byte) fdataReqFunc {
return func(mod *FactoidModule) interface{} {
m := mod.fdataMap[mapName]
if m == nil {
m = make(map[string]fdataVal)
mod.fdataMap[mapName] = m
}
m[keyName] = fdataVal{
JSON: data,
DBSync: util.TriNo,
}
mod.fdataTriggerSync()
return nil
}
}
func fdataFuncStoreCache(mapName, keyName string, data []byte) fdataReqFunc {
return func(mod *FactoidModule) interface{} {
m := mod.fdataMap[mapName]
if m == nil {
m = make(map[string]fdataVal)
mod.fdataMap[mapName] = m
}
m[keyName] = fdataVal{
JSON: data,
DBSync: util.TriYes,
}
return nil
}
}
func (mod *FactoidModule) GetFDataValue(mapName, keyName string) ([]byte, error) {
fval := mod.fdataGetCachedEntry(mapName, keyName)
if fval.DBSync != util.TriDefault {
return fval.JSON, nil
}
b, err := mod.fdataDBGetOne(mapName, keyName)
if err != nil {
return nil, err
}
ch := make(chan interface{}, 1)
mod.fdataReqChan <- fdataReq{C: ch, F: fdataFuncStoreCache(mapName, keyName, b)}
return b, nil
}
func (mod *FactoidModule) SetFDataValue(mapName, keyName string, val []byte) {
if mod.team.TeamConfig().IsReadOnly {
return
}
ch := make(chan interface{}, 1)
mod.fdataReqChan <- fdataReq{C: ch, F: fdataFuncSetEntry(mapName, keyName, val)}
return
}
type LFDataMap struct {
mod *FactoidModule
MapName string
lcache *lua.LTable
fullContent map[string][]byte
}
const metatableFDataMap = "_metatable_FDataMap"
func (*LFDataMap) SetupMetatable(L *lua.LState) {
tab := L.NewTypeMetatable(metatableFDataMap)
tab.RawSetString("__index", L.NewFunction(luaFData_get))
tab.RawSetString("__newindex", L.NewFunction(luaFData_set))
tab.RawSetString("__len", L.NewFunction(luaFData_count))
tab.RawSetString("__preload", L.NewFunction(luaFData_preload))
L.SetGlobal("iterfdata", L.NewFunction(luaFData_iter))
L.SetGlobal("iterfmap", L.NewFunction(luaFData_iter))
}
func LNewFDataMap(g *lualib.G, mod *FactoidModule, mapName string) lua.LValue {
v := &LFDataMap{mod: mod, MapName: mapName}
v.lcache = g.L.NewTable()
u := g.L.NewUserData()
u.Value = v
u.Metatable = g.L.GetTypeMetatable(metatableFDataMap)
return u
}
func luaFData_get(L *lua.LState) int {
u := L.CheckUserData(1)
kv := L.Get(2)
if kv.Type() != lua.LTString {
return 0
}
key := L.CheckString(2)
fdm, ok := u.Value.(*LFDataMap)
if !ok {
L.RaiseError("Wrong self for FDataMap.__index, got %T", u.Value)
}
if len(key) > fdataKeyMaxLen {
L.RaiseError("fdata keys cannot be longer than %d", fdataKeyMaxLen)
}
lv := fdm.lcache.RawGetString(key)
if lv != lua.LNil {
L.Push(lv)
return 1
}
var jsonBytes []byte
if fdm.fullContent != nil {
b, ok := fdm.fullContent[key]
if !ok {
L.Push(lua.LNil)
return 1
}
jsonBytes = b
} else {
b, err := fdm.mod.GetFDataValue(fdm.MapName, key)
if err != nil {
L.RaiseError("Factoid map error: %s", err)
}
jsonBytes = b
}
top := L.GetTop()
L.Push(L.GetTable(L.GetGlobal("json"), lua.LString("load")))
L.Push(lua.LString(jsonBytes))
L.Call(1, 1)
fdm.lcache.RawSetString(key, L.Get(top+1))
return 1
}
func luaFData_set(L *lua.LState) int {
u := L.CheckUserData(1)
kv := L.Get(2)
if kv.Type() != lua.LTString {
L.RaiseError("FactoidData only allows string-typed keys. To store an array, choose a name for it.")
return 0
}
key := L.CheckString(2)
val := L.CheckAny(3)
fdm, ok := u.Value.(*LFDataMap)
if !ok {
L.RaiseError("Wrong self for FDataMap.__newindex, got %T", u.Value)
}
if len(key) > fdataKeyMaxLen {
L.RaiseError("fdata keys cannot be longer than %d", fdataKeyMaxLen)
}
fdm.lcache.RawSetString(key, val)
if val == lua.LNil {
if fdm.fullContent != nil {
delete(fdm.fullContent, key)
}
fdm.mod.SetFDataValue(fdm.MapName, key, nil)
return 0
}
top := L.GetTop()
L.Push(L.GetTable(L.GetGlobal("json"), lua.LString("dump")))
L.Push(val)
L.Call(1, 1)
jsonLV := L.Get(top + 1)
if jsonLV.Type() != lua.LTString {
L.RaiseError("bad return from json.dump (expected lua string, got %s)", jsonLV.Type())
}
jsonData := []byte(jsonLV.(lua.LString))
if len(jsonData) > fdataValMaxLen {
// Need additional checks to have an actual quota
L.RaiseError("fdata values cannot be larger than %d bytes (JSON)", fdataValMaxLen)
}
if fdm.fullContent != nil {
fdm.fullContent[key] = jsonData
}
fdm.mod.SetFDataValue(fdm.MapName, key, jsonData)
return 0
}
func luaFData_preload(L *lua.LState) int {
u := L.CheckUserData(1)
fdm, ok := u.Value.(*LFDataMap)
if !ok {
L.RaiseError("Wrong self for FDataMap.__index, got %T", u.Value)
}
if fdm.fullContent != nil {
return 0
}
m, err := fdm.mod.GetFDataAll(fdm.MapName)
if err != nil {
L.RaiseError("preload(): database error: %s", err)
}
fdm.lcache = L.NewTable()
fdm.fullContent = m
return 0
}
func luaFData_count(L *lua.LState) int {
u := L.CheckUserData(1)
fdm, ok := u.Value.(*LFDataMap)
if !ok {
L.RaiseError("fmap.__len requires a fmap as argument #1, got %T", u.Value)
}
// preload full table
if fdm.fullContent == nil {
L.Push(L.GetTable(u.Metatable, lua.LString("__preload")))
L.Push(u)
L.Call(1, 0)
}
L.Push(lua.LNumber(len(fdm.fullContent)))
return 1
}
func luaFData_iter(L *lua.LState) int {
u := L.CheckUserData(1)
fdm, ok := u.Value.(*LFDataMap)
if !ok {
L.RaiseError("iterfdata() requires a fmap as argument #1, got %T", u.Value)
}
// preload full table
if fdm.fullContent == nil {
L.Push(L.GetTable(u.Metatable, lua.LString("__preload")))
L.Push(u)
L.Call(1, 0)
}
keys := make([]string, 0, len(fdm.fullContent))
for k := range fdm.fullContent {
keys = append(keys, k)
}
var idx int = 0
cl := L.NewFunction(func(L *lua.LState) int {
if idx >= len(keys) {
return 0
}
k := keys[idx]
idx++
val := L.GetField(u, k)
L.Push(lua.LString(k))
L.Push(val)
return 2
})
L.Push(cl)
L.Push(u)
L.Push(lua.LNumber(0))
return 3
}