-
Notifications
You must be signed in to change notification settings - Fork 1
/
log_sync_ctx.go
73 lines (58 loc) · 1.46 KB
/
log_sync_ctx.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
package main
/* This file maintains package level context for log syncing */
// #include "gdp_types.h"
import "C"
import (
"database/sql"
"errors"
"math/rand"
"github.com/tonyyanga/gdp-replicate/logserver"
"github.com/tonyyanga/gdp-replicate/policy"
"go.uber.org/zap"
)
type HandleTicket = uint32
type LogSyncCtx struct {
Policy policy.Policy
logServer logserver.LogServer
}
// Global map from handleTicket in LogSyncHandle to Go context
var logCtxMap = make(map[HandleTicket]LogSyncCtx)
func newLogSyncCtx(sqlFile string) (HandleTicket, error) {
db, err := sql.Open("sqlite3", sqlFile)
if err != nil {
zap.S().Errorw(
"Failed to open sqlite database",
"sqlite-file", sqlFile,
"error", err,
)
return 0, err
}
logServer := logserver.NewSqliteServer(db)
// TODO: support alternate policies
policy := policy.NewExternalGraphDiffPolicy(logServer)
ticket := generateHandleTicket()
logCtxMap[ticket] = LogSyncCtx{
logServer: logServer,
Policy: policy,
}
return ticket, nil
}
// Helper func to get log sync ctx from map
func getLogSyncCtx(handle C.LogSyncHandle) (*LogSyncCtx, error) {
ticket := uint32(handle.handleTicket)
result, ok := logCtxMap[ticket]
if !ok {
return nil, errors.New("Undefined log sync handle")
} else {
return &result, nil
}
}
// Generate random ticket not in the map
func generateHandleTicket() HandleTicket {
for {
ticket := rand.Uint32()
if _, ok := logCtxMap[ticket]; !ok {
return ticket
}
}
}