/
sql.go
141 lines (125 loc) · 3.84 KB
/
sql.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
package node
import (
"context"
"fmt"
"reflect"
"berty.tech/core/api/node"
"berty.tech/core/entity"
"berty.tech/core/sql"
"github.com/jinzhu/gorm"
opentracing "github.com/opentracing/opentracing-go"
"go.uber.org/zap"
)
// WithSQL registers a gorm connection as the node database
func WithSQL(sql *gorm.DB) NewNodeOption {
return func(n *Node) {
n.sqlDriver = sql.Set("gorm:auto_preload", true).Unscoped()
sql.Callback().Create().Register("berty:after_create", func(scope *gorm.Scope) { n.handleCommitLog("create", scope) })
sql.Callback().Update().Register("berty:after_update", func(scope *gorm.Scope) { n.handleCommitLog("update", scope) })
sql.Callback().Delete().Register("berty:after_delete", func(scope *gorm.Scope) { n.handleCommitLog("delete", scope) })
}
}
// sql returns a gorm.DB object with opentracing context
func (n *Node) sql(ctx context.Context) *gorm.DB {
if ctx == nil {
return n.sqlDriver.Set("rootSpan", n.rootSpan)
}
if span := opentracing.SpanFromContext(ctx); span != nil {
return n.sqlDriver.Set("rootSpan", span)
}
return n.sqlDriver
}
func (n *Node) handleCommitLog(operation string, scope *gorm.Scope) {
// same usage as https://github.com/jinzhu/gorm/blob/master/scope.go#L241
if scope.HasError() {
return
}
if scope.Value == nil {
return
}
if indirectScopeValue := scope.IndirectValue(); indirectScopeValue.Kind() == reflect.Slice {
for i := 0; i < indirectScopeValue.Len(); i++ {
n.sendCommitLog(n.createCommitLog(scope, operation, indirectScopeValue.Index(i)))
}
} else {
n.sendCommitLog(n.createCommitLog(scope, operation, indirectScopeValue))
}
}
func (n *Node) sendCommitLog(commitLog *node.CommitLog) {
if commitLog == nil {
return
}
logger().Debug("commit log", zap.Stringer("commit log", commitLog))
n.clientCommitLogsMutex.Lock()
defer n.clientCommitLogsMutex.Unlock()
for _, sub := range n.clientCommitLogsSubscribers {
sub.queue <- commitLog
}
}
func (n *Node) createCommitLog(scope *gorm.Scope, operation string, reflectValue reflect.Value) *node.CommitLog {
var err error
// Only get address from non-pointer
if reflectValue.CanAddr() && reflectValue.Kind() != reflect.Ptr {
reflectValue = reflectValue.Addr()
}
log := &node.CommitLog{}
switch operation {
case "create":
log.Operation = node.CommitLog_Create
case "update":
log.Operation = node.CommitLog_Update
case "delete":
log.Operation = node.CommitLog_Delete
default:
logger().Warn(fmt.Sprintf("undefined operation %+v", operation))
return nil
}
switch data := reflectValue.Interface().(type) {
case *entity.Contact:
if operation != "delete" {
data, err = sql.ContactByID(scope.DB(), data.ID)
if err != nil {
return nil
}
}
log.Entity = &node.CommitLog_Entity{Contact: data}
case *entity.Device:
if operation != "delete" {
data, err = sql.DeviceByID(scope.DB(), data.ID)
if err != nil {
return nil
}
}
log.Entity = &node.CommitLog_Entity{Device: data}
case *entity.Conversation:
if operation != "delete" {
data, err = sql.ConversationByID(scope.DB(), data.ID)
if err != nil {
return nil
}
}
log.Entity = &node.CommitLog_Entity{Conversation: data}
case *entity.ConversationMember:
data, err = sql.ConversationMemberByID(scope.DB(), data.ID)
if err != nil {
return nil
}
log.Entity = &node.CommitLog_Entity{ConversationMember: data}
case *entity.Config:
log.Entity = &node.CommitLog_Entity{Config: data}
case *entity.Event:
data, err = sql.EventByID(scope.DB(), data.ID)
if err != nil {
return nil
}
log.Entity = &node.CommitLog_Entity{Event: data}
case *entity.DevicePushConfig:
log.Entity = &node.CommitLog_Entity{DevicePushConfig: data}
case *entity.DevicePushIdentifier:
log.Entity = &node.CommitLog_Entity{DevicePushIdentifier: data}
default:
logger().Warn(fmt.Sprintf("unhandled entity %+v", data))
return nil
}
return log
}