-
Notifications
You must be signed in to change notification settings - Fork 4
/
antsdb.go
186 lines (169 loc) · 3.8 KB
/
antsdb.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
package antsdb
import (
"context"
dsImpl "github.com/SWRMLabs/ss-ds-store"
"github.com/SWRMLabs/ss-store"
ds "github.com/ipfs/go-datastore"
crdt "github.com/ipfs/go-ds-crdt"
logging "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p-core/peer"
pubsub "github.com/libp2p/go-libp2p-pubsub"
multihash "github.com/multiformats/go-multihash"
"time"
)
var (
defaultRootNs = "/ant"
defaultTopic = "antWorker"
log = logging.Logger("antsdb")
)
type Option func(a *AntsDB)
func WithChannel(topic string) Option {
return func(a *AntsDB) {
a.topicName = topic
}
}
func WithPeerValidator(validator func(context.Context, peer.ID) bool) Option {
return func(a *AntsDB) {
a.validator = validator
}
}
func WithNamespace(ns string) Option {
return func(a *AntsDB) {
a.namespace = ds.NewKey(ns)
}
}
func WithRebroadcastDuration(d time.Duration) Option {
return func(a *AntsDB) {
a.rebcastInterval = d
}
}
func WithOnCloseHook(hook func()) Option {
return func(a *AntsDB) {
a.addOnClose(hook)
}
}
func defaultOpts(a *AntsDB) {
if len(a.namespace.String()) == 0 {
a.namespace = ds.NewKey(defaultRootNs)
}
if a.validator == nil {
a.validator = func(_ context.Context, p peer.ID) bool {
log.Info("Got pubsub msg from ", p.Pretty())
return true
}
}
if len(a.topicName) == 0 {
a.topicName = defaultTopic
}
if a.rebcastInterval == 0 {
a.rebcastInterval = time.Second
}
}
type AntsDB struct {
ctx context.Context
cancel context.CancelFunc
syncer crdt.SessionDAGSyncer
pubsub *pubsub.PubSub
storage ds.Batching
namespace ds.Key
topicName string
rebcastInterval time.Duration
validator func(context.Context, peer.ID) bool
closers []func()
store.Store
}
func New(
syncer crdt.SessionDAGSyncer,
pubsub *pubsub.PubSub,
store ds.Batching,
opts ...Option,
) (*AntsDB, error) {
ctx, cancel := context.WithCancel(context.Background())
adb := &AntsDB{
ctx: ctx,
cancel: cancel,
syncer: syncer,
pubsub: pubsub,
storage: store,
}
for _, opt := range opts {
opt(adb)
}
defaultOpts(adb)
return adb, adb.setup()
}
func (a *AntsDB) setup() error {
topicHash, err := multihash.Sum([]byte(a.topicName), multihash.MD5, -1)
if err == nil {
log.Infof("Updating topic name with hash %s", topicHash)
a.topicName = topicHash.B58String()
}
err = a.pubsub.RegisterTopicValidator(
a.topicName,
func(ctx context.Context, p peer.ID, msg *pubsub.Message) bool {
return a.validator(ctx, p)
},
)
if err != nil {
log.Errorf("Failed registering pubsub topic Err:%s", err.Error())
return err
}
broadcaster, err := crdt.NewPubSubBroadcaster(
a.ctx,
a.pubsub,
a.topicName,
)
if err != nil {
log.Errorf("Failed creating broadcaster Err:%s", err.Error())
return err
}
opts := crdt.DefaultOptions()
opts.RebroadcastInterval = a.rebcastInterval
opts.DAGSyncerTimeout = 2 * time.Minute
opts.Logger = log
opts.PutHook = func(k ds.Key, v []byte) {
log.Infof("AntsDB PUT %s", k)
}
opts.DeleteHook = func(k ds.Key) {
log.Infof("AntsDB DELETE %s", k)
}
crdt, err := crdt.New(
a.storage,
a.namespace,
a.syncer,
broadcaster,
opts,
)
if err != nil {
log.Errorf("Failed creating crdt datastore Err:%s", err.Error())
return err
}
a.Store, err = dsImpl.NewDataStore(&dsImpl.DSConfig{
DS: crdt,
})
if err != nil {
log.Errorf("Failed creating new Store Err:%s", err.Error())
return err
}
a.addOnClose(func() {
log.Info("Stopping AntsDB")
a.cancel()
log.Info("Closing CRDT datastore")
crdt.Close()
})
return nil
}
func (a *AntsDB) addOnClose(hook func()) {
if a.closers == nil {
a.closers = []func(){hook}
return
}
a.closers = append(a.closers, hook)
}
func (a *AntsDB) Close() error {
log.Info("Closing AntsDB")
for _, stop := range a.closers {
stop()
}
return nil
}