forked from berty/go-orbit-db
/
interface.go
441 lines (344 loc) · 14 KB
/
interface.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
package iface
import (
"context"
"time"
cid "github.com/ipfs/go-cid"
datastore "github.com/ipfs/go-datastore"
coreiface "github.com/ipfs/kubo/core/coreiface"
"github.com/libp2p/go-libp2p/core/event"
peer "github.com/libp2p/go-libp2p/core/peer"
ipfslog "github.com/thusharprakash/go-ipfs-log"
"github.com/thusharprakash/go-ipfs-log/entry"
"github.com/thusharprakash/go-ipfs-log/identityprovider"
"github.com/thusharprakash/go-ipfs-log/iface"
"github.com/thusharprakash/go-ipfs-log/keystore"
"github.com/thusharprakash/go-orbit-db/accesscontroller"
"github.com/thusharprakash/go-orbit-db/address"
"github.com/thusharprakash/go-orbit-db/events"
"github.com/thusharprakash/go-orbit-db/stores/operation"
"github.com/thusharprakash/go-orbit-db/stores/replicator"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
)
// Message Marshaler
type MessageExchangeHeads struct {
Address string `json:"address"`
Heads []*entry.Entry `json:"heads"`
}
type MessageMarshaler interface {
Marshal(msg *MessageExchangeHeads) ([]byte, error)
Unmarshal(data []byte, msg *MessageExchangeHeads) error
}
// CreateDBOptions lists the arguments to create a store
type CreateDBOptions struct {
EventBus event.Bus
Directory *string
Overwrite *bool
LocalOnly *bool
Create *bool
StoreType *string
AccessControllerAddress string
AccessController accesscontroller.ManifestParams
Replicate *bool
Keystore keystore.Interface
Cache datastore.Datastore
Identity *identityprovider.Identity
SortFn ipfslog.SortFn
IO ipfslog.IO
Timeout time.Duration
MessageMarshaler MessageMarshaler
Logger *zap.Logger
CloseFunc func()
StoreSpecificOpts interface{}
}
type CreateDocumentDBOptions struct {
KeyExtractor func(interface{}) (string, error)
Marshal func(interface{}) ([]byte, error)
Unmarshal func(data []byte, v interface{}) error
ItemFactory func() interface{}
}
// DetermineAddressOptions Lists the arguments used to determine a store address
type DetermineAddressOptions struct {
OnlyHash *bool
Replicate *bool
AccessController accesscontroller.ManifestParams
}
// BaseOrbitDB Provides the main OrbitDB interface used to open and create stores
type BaseOrbitDB interface {
// IPFS Returns the instance of the IPFS API used by the current DB
IPFS() coreiface.CoreAPI
// Identity Returns the identity used by the current DB
Identity() *identityprovider.Identity
// Open Opens an existing data store
Open(ctx context.Context, dbAddress string, options *CreateDBOptions) (Store, error)
// Create Creates a new store
Create(ctx context.Context, name string, storeType string, options *CreateDBOptions) (Store, error)
// Close Closes the current DB and all the related stores
Close() error
// DetermineAddress Returns the store address for the given parameters
DetermineAddress(ctx context.Context, name string, storeType string, options *DetermineAddressOptions) (address.Address, error)
// RegisterStoreType Registers a new store type
RegisterStoreType(storeType string, constructor StoreConstructor)
// UnregisterStoreType Removes a store type
UnregisterStoreType(storeType string)
// RegisterAccessControllerType Registers a new access controller type
RegisterAccessControllerType(AccessControllerConstructor) error
// UnregisterAccessControllerType Unregisters an access controller type
UnregisterAccessControllerType(string)
// GetAccessControllerType Retrieves an access controller type constructor if it exists
GetAccessControllerType(string) (AccessControllerConstructor, bool)
// EventBus Returns the eventsBus
EventBus() event.Bus
// Logger Returns the logger
Logger() *zap.Logger
// Tracer Returns the tracer
Tracer() trace.Tracer
}
// OrbitDBDocumentStore An OrbitDB instance providing a Document store
type OrbitDBDocumentStore interface {
BaseOrbitDB
OrbitDBDocumentStoreProvider
}
// OrbitDBDocumentStoreProvider Exposes a method providing a document store
type OrbitDBDocumentStoreProvider interface {
// Docs Creates or opens an DocumentStore
Docs(ctx context.Context, address string, options *CreateDBOptions) (DocumentStore, error)
}
// OrbitDBKVStore An OrbitDB instance providing a KeyValue store
type OrbitDBKVStore interface {
BaseOrbitDB
OrbitDBKVStoreProvider
}
// OrbitDBKVStoreProvider Exposes a method providing a key value store
type OrbitDBKVStoreProvider interface {
// KeyValue Creates or opens an KeyValueStore
KeyValue(ctx context.Context, address string, options *CreateDBOptions) (KeyValueStore, error)
}
// OrbitDBLogStore An OrbitDB instance providing an Event Log store
type OrbitDBLogStore interface {
BaseOrbitDB
OrbitDBLogStoreProvider
}
// OrbitDBLogStoreProvider Exposes a method providing an event log store
type OrbitDBLogStoreProvider interface {
// Log Creates or opens an EventLogStore
Log(ctx context.Context, address string, options *CreateDBOptions) (EventLogStore, error)
}
// OrbitDB Provides an OrbitDB interface with the default access controllers and store types
type OrbitDB interface {
BaseOrbitDB
OrbitDBKVStoreProvider
OrbitDBLogStoreProvider
OrbitDBDocumentStoreProvider
}
// StreamOptions Defines the parameters that can be given to the Stream function of an EventLogStore
type StreamOptions struct {
GT *cid.Cid
GTE *cid.Cid
LT *cid.Cid
LTE *cid.Cid
Amount *int
}
type StoreEvents interface {
Subscribe()
}
// Store Defines the operations common to all stores types
type Store interface {
// Deprecated: use EventBus() instead
events.EmitterInterface
// Close Closes the store
Close() error
// Address Returns the address for the current store
Address() address.Address
// Index Returns the StoreIndex struct for the current store instance
Index() StoreIndex
// Type Returns the current store type as a string
Type() string
// ReplicationStatus Returns the current ReplicationInfo status
ReplicationStatus() replicator.ReplicationInfo
// Replicator Returns the Replicator object
Replicator() replicator.Replicator
// Cache Returns the Cache object
Cache() datastore.Datastore
// Drop Removes all the local store content
Drop() error
// Load Fetches entries on the network
Load(ctx context.Context, amount int) error
// Sync Merges stores with the given heads
Sync(ctx context.Context, heads []ipfslog.Entry) error
// LoadMoreFrom Loads more entries from the given CIDs
LoadMoreFrom(ctx context.Context, amount uint, entries []ipfslog.Entry)
// LoadFromSnapshot Loads store content from a snapshot
LoadFromSnapshot(ctx context.Context) error
// OpLog Returns the underlying IPFS Log instance for the store
OpLog() ipfslog.Log
// IPFS Returns the IPFS instance for the store
IPFS() coreiface.CoreAPI
// DBName Returns the store name as a string
DBName() string
// Identity Returns the identity used for the current store
Identity() *identityprovider.Identity
// AccessController Returns the access controller for this store
AccessController() accesscontroller.Interface
// AddOperation Adds an operation to this store
AddOperation(ctx context.Context, op operation.Operation, onProgressCallback chan<- ipfslog.Entry) (ipfslog.Entry, error)
// Logger Returns the logger
Logger() *zap.Logger
// Tracer Returns the tracer
Tracer() trace.Tracer
IO() iface.IO
// subscribe to events on this store
EventBus() event.Bus
}
// EventLogStore A type of store that provides an append only log
type EventLogStore interface {
Store
// Add Appends data to the log
Add(ctx context.Context, data []byte) (operation.Operation, error)
// Get Fetches an entry of the log
Get(ctx context.Context, cid cid.Cid) (operation.Operation, error)
// Stream Populates a chan of entries from this store
Stream(ctx context.Context, resultChan chan operation.Operation, options *StreamOptions) error
// List Fetches a list of operation that occurred on this store
List(ctx context.Context, options *StreamOptions) ([]operation.Operation, error)
}
// KeyValueStore A type of store that provides a key value store
type KeyValueStore interface {
Store
// All Returns a consolidated key value map from the entries of this store
All() map[string][]byte
// Put Sets the value for a key of the map
Put(ctx context.Context, key string, value []byte) (operation.Operation, error)
// Delete Clears the value for a key of the map
Delete(ctx context.Context, key string) (operation.Operation, error)
// Get Retrieves the value for a key of the map
Get(ctx context.Context, key string) ([]byte, error)
}
type DocumentStoreGetOptions struct {
CaseInsensitive bool
PartialMatches bool
}
// DocumentStore A type of store that provides a document store
type DocumentStore interface {
Store
// Put Stores the document
Put(ctx context.Context, document interface{}) (operation.Operation, error)
// Delete Clears the document for a key
Delete(ctx context.Context, key string) (operation.Operation, error)
// PutBatch Add values as multiple operations and returns the latest
PutBatch(ctx context.Context, values []interface{}) (operation.Operation, error)
// PutAll Add values as a single operation and returns it
PutAll(ctx context.Context, values []interface{}) (operation.Operation, error)
// Get Retrieves the document for a key
Get(ctx context.Context, key string, opts *DocumentStoreGetOptions) ([]interface{}, error)
// Query Finds documents using a filter function
Query(ctx context.Context, filter func(doc interface{}) (bool, error)) ([]interface{}, error)
}
// StoreIndex Index contains the state of a datastore,
// ie. what data we currently have.
//
// Index receives a call from a Store when the operations log for the Store
// was updated, ie. new operations were added. In updateIndex, the Index
// implements its CRDT logic: add, remove or update items in the data
// structure. Each new operation received from the operations log is applied
// in order onto the current state, ie. each new operation changes the data
// and the state changes.
//
// Implementing each CRDT as an Index, we can implement both operation-based
// and state-based CRDTs with the same higher level abstractions.
// To read the current state of the database, Index provides a single public
// function: `get()`. It is up to the Store to decide what kind of query
// capabilities it provides to the consumer.
type StoreIndex interface {
// Get Returns the state of the datastore, ie. most up-to-date data
Get(key string) interface{}
// UpdateIndex Applies operations to the Index and updates the state
UpdateIndex(log ipfslog.Log, entries []ipfslog.Entry) error
}
// NewStoreOptions Lists the options to create a new store
type NewStoreOptions struct {
EventBus event.Bus
Index IndexConstructor
AccessController accesscontroller.Interface
Cache datastore.Datastore
CacheDestroy func() error
ReplicationConcurrency uint
ReferenceCount *int
Replicate *bool
MaxHistory *int
Directory string
SortFn ipfslog.SortFn
Logger *zap.Logger
Tracer trace.Tracer
IO ipfslog.IO
PubSub PubSubInterface
MessageMarshaler MessageMarshaler
PeerID peer.ID
DirectChannel DirectChannel
CloseFunc func()
StoreSpecificOpts interface{}
}
type DirectChannelOptions struct {
Logger *zap.Logger
}
type DirectChannel interface {
// Connect Waits for the other peer to be connected
Connect(context.Context, peer.ID) error
// Send Sends a message to the other peer
Send(ctx context.Context, peer peer.ID, data []byte) error
// Close Closes the connection
Close() error
}
type DirectChannelEmitter interface {
Emit(*EventPubSubPayload) error
Close() error
}
type DirectChannelFactory func(ctx context.Context, emitter DirectChannelEmitter, opts *DirectChannelOptions) (DirectChannel, error)
// StoreConstructor Defines the expected constructor for a custom store
type StoreConstructor func(coreiface.CoreAPI, *identityprovider.Identity, address.Address, *NewStoreOptions) (Store, error)
// IndexConstructor Defines the expected constructor for a custom index
type IndexConstructor func(publicKey []byte) StoreIndex
// OnWritePrototype Defines the callback function prototype which is triggered on a write
type OnWritePrototype func(ctx context.Context, addr cid.Cid, entry ipfslog.Entry, heads []cid.Cid) error
// AccessControllerConstructor Required prototype for custom controllers constructors
type AccessControllerConstructor func(context.Context, BaseOrbitDB, accesscontroller.ManifestParams, ...accesscontroller.Option) (accesscontroller.Interface, error)
// PubSubTopic is a pub sub subscription to a topic
type PubSubTopic interface {
// Publish Posts a new message on a topic
Publish(ctx context.Context, message []byte) error
// Peers Lists peers connected to the topic
Peers(ctx context.Context) ([]peer.ID, error)
// WatchPeers subscribes to peers joining or leaving the topic
WatchPeers(ctx context.Context) (<-chan events.Event, error)
// WatchMessages Subscribes to new messages
WatchMessages(ctx context.Context) (<-chan *EventPubSubMessage, error)
// Topic Returns the topic name
Topic() string
}
type PubSubInterface interface {
// TopicSubscribe Subscribes to a topic
TopicSubscribe(ctx context.Context, topic string) (PubSubTopic, error)
}
type PubSubSubscriptionOptions struct {
Logger *zap.Logger
Tracer trace.Tracer
}
// EventPubSubMessage Indicates a new message posted on a pubsub topic
type EventPubSubMessage struct {
Content []byte
}
// EventPubSubPayload An event received on new messages
type EventPubSubPayload struct {
Payload []byte
Peer peer.ID
}
// EventPubSubJoin Is an event triggered when a peer joins the channel
type EventPubSubJoin struct {
Topic string
Peer peer.ID
}
// EventPubSubLeave Is an event triggered when a peer leave the channel
type EventPubSubLeave struct {
Topic string
Peer peer.ID
}