-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
198 lines (169 loc) · 5.22 KB
/
options.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
package db
import (
core "github.com/dcnetio/gothreads-lib/core/db"
"github.com/dcnetio/gothreads-lib/core/thread"
"github.com/dcnetio/gothreads-lib/jsonpatcher"
"github.com/libp2p/go-libp2p/core/crypto"
)
func newDefaultEventCodec() core.EventCodec {
return jsonpatcher.New()
}
// NewOptions defines options for creating a new db.
type NewOptions struct {
Name string
Key thread.Key
LogKey crypto.Key
Collections []CollectionConfig
Block bool
EventCodec core.EventCodec
Token thread.Token
Debug bool
}
// NewOption specifies a new db option.
type NewOption func(*NewOptions)
// WithNewKey provides control over thread keys to use with a db.
func WithNewKey(key thread.Key) NewOption {
return func(o *NewOptions) {
o.Key = key
}
}
// WithNewLogKey is the public or private key used to write log records.
// If this is just a public key, the service itself won't be able to create records.
// In other words, all records must be pre-created and added with AddRecord.
// If no log key is provided, one will be created internally.
func WithNewLogKey(key crypto.Key) NewOption {
return func(o *NewOptions) {
o.LogKey = key
}
}
// WithNewName sets the db name.
func WithNewName(name string) NewOption {
return func(o *NewOptions) {
o.Name = name
}
}
// WithNewCollections is used to specify collections that
// will be created.
func WithNewCollections(cs ...CollectionConfig) NewOption {
return func(o *NewOptions) {
o.Collections = cs
}
}
// WithNewBackfillBlock makes the caller of NewDBFromAddr block until the
// underlying thread is completely backfilled.
// Without this, NewDBFromAddr returns immediately and thread backfilling
// happens in the background.
func WithNewBackfillBlock(block bool) NewOption {
return func(o *NewOptions) {
o.Block = block
}
}
// WithNewEventCodec configure to use ec as the EventCodec
// for transforming actions in events, and viceversa.
func WithNewEventCodec(ec core.EventCodec) NewOption {
return func(o *NewOptions) {
o.EventCodec = ec
}
}
// WithNewToken provides authorization for interacting with a db.
func WithNewToken(t thread.Token) NewOption {
return func(o *NewOptions) {
o.Token = t
}
}
// WithNewDebug indicate to output debug information.
func WithNewDebug(enable bool) NewOption {
return func(o *NewOptions) {
o.Debug = enable
}
}
// Options defines options for interacting with a db.
type Options struct {
Token thread.Token
}
// Option specifies a db option.
type Option func(*Options)
// WithToken provides authorization for interacting with a db.
func WithToken(t thread.Token) Option {
return func(o *Options) {
o.Token = t
}
}
// TxnOptions defines options for a transaction.
type TxnOptions struct {
Token thread.Token
}
// TxnOption specifies a transaction option.
type TxnOption func(*TxnOptions)
// WithTxnToken provides authorization for the transaction.
func WithTxnToken(t thread.Token) TxnOption {
return func(o *TxnOptions) {
o.Token = t
}
}
// NewManagedOptions defines options for creating a new managed db.
type NewManagedOptions struct {
Name string
Key thread.Key
LogKey crypto.Key
Token thread.Token
Collections []CollectionConfig
Block bool
}
// NewManagedOption specifies a new managed db option.
type NewManagedOption func(*NewManagedOptions)
// WithNewManagedName assigns a name to a new managed db.
func WithNewManagedName(name string) NewManagedOption {
return func(o *NewManagedOptions) {
o.Name = name
}
}
// WithNewManagedToken provides authorization for creating a new managed db.
func WithNewManagedToken(t thread.Token) NewManagedOption {
return func(o *NewManagedOptions) {
o.Token = t
}
}
// WithNewManagedKey provides control over thread keys to use with a managed db.
func WithNewManagedKey(key thread.Key) NewManagedOption {
return func(o *NewManagedOptions) {
o.Key = key
}
}
// WithNewManagedLogKey is the public or private key used to write log records.
// If this is just a public key, the service itself won't be able to create records.
// In other words, all records must be pre-created and added with AddRecord.
// If no log key is provided, one will be created internally.
func WithNewManagedLogKey(key crypto.Key) NewManagedOption {
return func(o *NewManagedOptions) {
o.LogKey = key
}
}
// WithNewManagedCollections is used to specify collections that
// will be created in a managed db.
func WithNewManagedCollections(cs ...CollectionConfig) NewManagedOption {
return func(o *NewManagedOptions) {
o.Collections = cs
}
}
// WithNewBackfillBlock makes the caller of NewDBFromAddr block until the
// underlying thread is completely backfilled.
// Without this, NewDBFromAddr returns immediately and thread backfilling
// happens in the background.
func WithNewManagedBackfillBlock(block bool) NewManagedOption {
return func(o *NewManagedOptions) {
o.Block = block
}
}
// ManagedOptions defines options for interacting with a managed db.
type ManagedOptions struct {
Token thread.Token
}
// ManagedOption specifies a managed db option.
type ManagedOption func(*ManagedOptions)
// WithManagedToken provides authorization for interacting with a managed db.
func WithManagedToken(t thread.Token) ManagedOption {
return func(o *ManagedOptions) {
o.Token = t
}
}