-
Notifications
You must be signed in to change notification settings - Fork 55
/
log.go
216 lines (172 loc) · 4.89 KB
/
log.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
package eventlogstore
import (
"context"
ipfslog "berty.tech/go-ipfs-log"
"berty.tech/go-ipfs-log/identityprovider"
"berty.tech/go-orbit-db/address"
"berty.tech/go-orbit-db/iface"
"berty.tech/go-orbit-db/stores/basestore"
"berty.tech/go-orbit-db/stores/operation"
cid "github.com/ipfs/go-cid"
coreapi "github.com/ipfs/interface-go-ipfs-core"
"github.com/pkg/errors"
)
type orbitDBEventLogStore struct {
basestore.BaseStore
}
func (o *orbitDBEventLogStore) List(ctx context.Context, options *iface.StreamOptions) ([]operation.Operation, error) {
var operations []operation.Operation
c := make(chan operation.Operation)
go func() {
_ = o.Stream(ctx, c, options)
}()
for op := range c {
operations = append(operations, op)
}
return operations, nil
}
func (o *orbitDBEventLogStore) Add(ctx context.Context, value []byte) (operation.Operation, error) {
op := operation.NewOperation(nil, "ADD", value)
e, err := o.AddOperation(ctx, op, nil)
if err != nil {
return nil, errors.Wrap(err, "error while deleting value")
}
op, err = operation.ParseOperation(e)
if err != nil {
return nil, errors.Wrap(err, "unable to parse newly created entry")
}
return op, nil
}
func (o *orbitDBEventLogStore) Get(ctx context.Context, cid cid.Cid) (operation.Operation, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
errChan := make(chan error)
stream := make(chan operation.Operation)
one := 1
go func() {
if err := o.Stream(ctx, stream, &iface.StreamOptions{GTE: &cid, Amount: &one}); err != nil {
errChan <- errors.Wrap(err, "unable to open stream")
cancel()
return
}
}()
select {
case value, ok := <-stream:
cancel()
if ok {
return value, nil
}
return nil, errors.New("channel read failed")
case err := <-errChan:
return nil, err
case <-ctx.Done():
return nil, errors.New("context deadline exceeded")
}
}
func (o *orbitDBEventLogStore) Stream(ctx context.Context, resultChan chan operation.Operation, options *iface.StreamOptions) error {
defer close(resultChan)
messages, err := o.query(options)
if err != nil {
return errors.Wrap(err, "unable to fetch query results")
}
for _, message := range messages {
op, err := operation.ParseOperation(message)
if err != nil {
return errors.Wrap(err, "unable to parse operation")
}
resultChan <- op
}
return nil
}
func (o *orbitDBEventLogStore) query(options *iface.StreamOptions) ([]ipfslog.Entry, error) {
if options == nil {
options = &iface.StreamOptions{}
}
uncastedEvents := o.Index().Get("")
if uncastedEvents == nil {
return nil, nil
}
events, ok := o.Index().Get("").([]ipfslog.Entry)
if !ok {
return nil, errors.New("unable to cast index to entries")
}
amount := 1
if options.Amount != nil {
if *options.Amount == 0 {
amount = 1
} else if *options.Amount > -1 {
amount = *options.Amount
} else {
amount = len(events)
}
}
var c cid.Cid
if options.GT != nil || options.GTE != nil {
// Greater than case
if options.GT != nil {
c = *options.GT
} else {
c = *options.GTE
}
return o.read(events, c, amount, options.GTE != nil), nil
}
if options.LT != nil {
c = *options.LT
} else if options.LTE != nil {
c = *options.LTE
}
// Reversing events
for i := len(events)/2 - 1; i >= 0; i-- {
opp := len(events) - 1 - i
events[i], events[opp] = events[opp], events[i]
}
// Lower than and lastN case, search latest first by reversing the sequence
result := o.read(events, c, amount, options.LTE != nil || options.LT == nil)
// Reversing result
for i := len(result)/2 - 1; i >= 0; i-- {
opp := len(result) - 1 - i
result[i], result[opp] = result[opp], result[i]
}
return result, nil
}
func (o *orbitDBEventLogStore) read(ops []ipfslog.Entry, hash cid.Cid, amount int, inclusive bool) []ipfslog.Entry {
// Find the index of the gt/lt hash, or start from the beginning of the array if not found
startIndex := 0
for i, e := range ops {
if e.GetHash().String() == hash.String() {
startIndex = i
break
}
}
// If gte/lte is set, we include the given hash, if not, start from the next element
if !inclusive {
startIndex++
}
var result []ipfslog.Entry
// Slice the array to its requested size
for i, e := range ops {
if i < startIndex {
continue
}
if amount == 0 {
break
}
result = append(result, e)
amount--
}
return result
}
func (o *orbitDBEventLogStore) Type() string {
return "eventlog"
}
// NewOrbitDBEventLogStore Instantiates a new EventLogStore
func NewOrbitDBEventLogStore(ctx context.Context, ipfs coreapi.CoreAPI, identity *identityprovider.Identity, addr address.Address, options *iface.NewStoreOptions) (i iface.Store, e error) {
store := &orbitDBEventLogStore{}
options.Index = NewEventIndex
err := store.InitBaseStore(ctx, ipfs, identity, addr, options)
if err != nil {
return nil, errors.Wrap(err, "unable to initialize base store")
}
return store, nil
}
var _ iface.EventLogStore = &orbitDBEventLogStore{}