forked from berty/go-orbit-db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
orbitdb.go
125 lines (94 loc) · 3.54 KB
/
orbitdb.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
package orbitdb
import (
"berty.tech/go-orbit-db/accesscontroller/ipfs"
"berty.tech/go-orbit-db/accesscontroller/orbitdb"
"berty.tech/go-orbit-db/accesscontroller/simple"
"berty.tech/go-orbit-db/baseorbitdb"
"berty.tech/go-orbit-db/iface"
"berty.tech/go-orbit-db/stores/eventlogstore"
"berty.tech/go-orbit-db/stores/kvstore"
"context"
coreapi "github.com/ipfs/interface-go-ipfs-core"
"github.com/pkg/errors"
)
type orbitDB struct {
baseorbitdb.BaseOrbitDB
}
// OrbitDB An alias of the type defined in the iface package
type OrbitDB = iface.OrbitDB
// Store An alias of the type defined in the iface package
type Store = iface.Store
// EventLogStore An alias of the type defined in the iface package
type EventLogStore = iface.EventLogStore
// KeyValueStore An alias of the type defined in the iface package
type KeyValueStore = iface.KeyValueStore
// StoreIndex An alias of the type defined in the iface package
type StoreIndex = iface.StoreIndex
// StoreConstructor An alias of the type defined in the iface package
type StoreConstructor = iface.StoreConstructor
// IndexConstructor An alias of the type defined in the iface package
type IndexConstructor = iface.IndexConstructor
// OnWritePrototype An alias of the type defined in the iface package
type OnWritePrototype = iface.OnWritePrototype
// StreamOptions An alias of the type defined in the iface package
type StreamOptions = iface.StreamOptions
// CreateDBOptions An alias of the type defined in the iface package
type CreateDBOptions = iface.CreateDBOptions
// DetermineAddressOptions An alias of the type defined in the iface package
type DetermineAddressOptions = iface.DetermineAddressOptions
// NewOrbitDBOptions Options for a new OrbitDB instance
type NewOrbitDBOptions = baseorbitdb.NewOrbitDBOptions
// NewOrbitDB Creates a new OrbitDB instance with default access controllers and store types
func NewOrbitDB(ctx context.Context, i coreapi.CoreAPI, options *NewOrbitDBOptions) (iface.OrbitDB, error) {
odb, err := baseorbitdb.NewOrbitDB(ctx, i, options)
if err != nil {
return nil, err
}
odb.RegisterStoreType("eventlog", eventlogstore.NewOrbitDBEventLogStore)
odb.RegisterStoreType("keyvalue", kvstore.NewOrbitDBKeyValue)
_ = odb.RegisterAccessControllerType(ipfs.NewIPFSAccessController)
_ = odb.RegisterAccessControllerType(orbitdb.NewOrbitDBAccessController)
_ = odb.RegisterAccessControllerType(simple.NewSimpleAccessController)
return &orbitDB{
BaseOrbitDB: odb,
}, nil
}
func (o *orbitDB) Log(ctx context.Context, address string, options *CreateDBOptions) (EventLogStore, error) {
if options == nil {
options = &CreateDBOptions{}
}
options.Create = boolPtr(true)
options.StoreType = stringPtr("eventlog")
store, err := o.Open(ctx, address, options)
if err != nil {
return nil, errors.Wrap(err, "unable to open database")
}
logStore, ok := store.(EventLogStore)
if !ok {
return nil, errors.New("unable to cast store to log")
}
return logStore, nil
}
func stringPtr(s string) *string {
return &s
}
func boolPtr(b bool) *bool {
return &b
}
func (o *orbitDB) KeyValue(ctx context.Context, address string, options *CreateDBOptions) (KeyValueStore, error) {
if options == nil {
options = &CreateDBOptions{}
}
options.Create = boolPtr(true)
options.StoreType = stringPtr("keyvalue")
store, err := o.Open(ctx, address, options)
if err != nil {
return nil, errors.Wrap(err, "unable to open database")
}
kvStore, ok := store.(KeyValueStore)
if !ok {
return nil, errors.New("unable to cast store to keyvalue")
}
return kvStore, nil
}
var _ OrbitDB = (*orbitDB)(nil)