forked from haadcode/go-ipfs-log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
436 lines (334 loc) · 7.24 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
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
package colog
import (
"encoding/json"
"io"
"sync"
"github.com/keks/go-ipfs-colog/immutabledb"
)
// CoLog is a concurrent log
type CoLog struct {
mutex sync.Mutex
db immutabledb.ImmutableDB
next, prev Index
heads HashSet
chans *entryChanSet
}
// New returns a concurrent log
func New(db immutabledb.ImmutableDB) *CoLog {
// TODO: make index persistent
return &CoLog{
db: db,
next: make(Index),
prev: make(Index),
heads: NewHashSet(),
chans: newEntryChanSet(),
}
}
// Add adds data to the colog and returns the resulting entry
func (l *CoLog) Add(data interface{}) (*Entry, error) {
l.mutex.Lock()
defer l.mutex.Unlock()
// prepare entry
e := &Entry{
Prev: l.heads.Copy(),
}
// set value
err := e.set(data)
if err != nil {
return nil, err
}
// use empty string to mark root entry
if e.Prev.Count() == 0 {
e.Prev.Add("")
}
eBytes, err := json.Marshal(e)
if err != nil {
return nil, err
}
// store entry
hStr, err := l.db.Put(eBytes)
if err != nil {
return nil, err
}
// set hash
e.Hash = Hash(hStr)
// update index
for h := range e.Prev {
l.next.Add(h, e.Hash)
l.prev.Add(e.Hash, h)
}
// update local heads
l.heads = NewHashSet()
l.heads.Add(e.Hash)
// send it to watchers
l.chans.Send(e)
return e, err
}
// Get returns an entry from the db.
func (l *CoLog) Get(h Hash) (*Entry, error) {
e := NewEntry()
eBytes, err := l.db.Get(string(h))
if err != nil {
return nil, err
}
err = json.Unmarshal(eBytes, e)
if err != nil {
return nil, err
}
e.Hash = h
return e, nil
}
// Contains returns whether an Entry with Hash h is stored.
func (l *CoLog) Contains(h Hash) bool {
l.mutex.Lock()
defer l.mutex.Unlock()
return l.contains(h)
}
// Nexts returns the set of hashes that reference hash h.
func (l *CoLog) Nexts(h Hash) HashSet {
l.mutex.Lock()
defer l.mutex.Unlock()
return l.next[h]
}
// Prevs returns the set of hashes that are referenced by hash h.
func (l *CoLog) Prevs(h Hash) HashSet {
l.mutex.Lock()
defer l.mutex.Unlock()
return l.prev[h]
}
func (l *CoLog) contains(h Hash) bool {
hs, ok := l.prev[h]
delete(hs, "")
return ok && len(h) > 0
}
// Join merges colog `other' into `l'
func (l *CoLog) Join(other *CoLog) error {
l.mutex.Lock()
defer l.mutex.Unlock()
newHeads := make(HashSet)
for h := range other.prev {
// skip known hashes
if l.contains(h) {
continue
}
e, err := other.Get(h)
if err != nil {
return err
}
eBytes, err := json.Marshal(e)
if err != nil {
return err
}
// add to db
_, err = l.db.Put(eBytes)
if err != nil {
return err
}
// send it to watchers
l.chans.Send(e)
// fix heads
for head := range l.heads {
// case 1: hash is head in both logs => remains head
if other.heads.Contains(head) {
newHeads.Add(head)
continue
}
// case 2: hash is head in l1, but not in l2 and is not part of l2
// => remains head
if !other.contains(head) {
newHeads.Add(head)
continue
}
// case 3: hash is head in l1, but not in l2 and is part of l2
// => not head anymore
// do nothing
}
for head := range other.heads {
// we had those already
if l.heads.Contains(head) {
continue
}
// case 4: hash is head in l2, but not in l1 and is not part of l1
// => remains head
if !l.contains(head) {
newHeads.Add(head)
continue
}
// case 5: hash is head in l2, but not in l1 and is part of l1
// => not head anymore
// do nothing
}
// fix up index
for hPrev := range e.Prev {
l.next.Add(hPrev, e.Hash)
l.prev.Add(e.Hash, hPrev)
}
}
l.heads = newHeads
return nil
}
// QueryBound defines a bound for a query.
type QueryBound struct {
Hash
Closed bool // as in closed interval
}
// Query defines which part of the colog should be queried
type Query struct {
Before, After QueryBound
}
// Result is the result of a Query
type Result func() (*Entry, error)
// Query traverses the part of the colog specified by Query
func (l *CoLog) Query(qry Query) Result {
l.mutex.Lock()
defer l.mutex.Unlock()
// keeps track how many of the revious pointers have been added to out already
addedPrevs := map[Hash]int{}
// set up hash stack to track concurrentness
var stack = []Hash{}
// push to stack
push := func(hs ...Hash) {
stack = append(stack, hs...)
}
//pop from stack
pop := func() Hash {
h := stack[len(stack)-1]
stack = stack[:len(stack)-1]
return h
}
if qry.After.Hash == "" {
// if empty Hash add all root nodes
push(l.next[""].Sorted()...)
} else if qry.After.Closed {
// if inclusive add hash directly
push(qry.After.Hash)
} else {
// otherwise add children
push(l.next[qry.After.Hash].Sorted()...)
}
return func() (*Entry, error) {
if len(stack) == 0 {
return nil, io.EOF
}
// pop hash from stack
h := pop()
// ignore root
if h == "" {
if len(stack) == 0 {
return nil, io.EOF
}
h = pop()
}
// get Entry
e, err := l.Get(h)
if err != nil {
return e, err
}
// did we reach the end?
if h == qry.Before.Hash {
stack = []Hash{}
if !qry.Before.Closed {
return nil, io.EOF
}
return e, nil
}
// mark that an Entry was added in all next hashes
for hNext := range l.next[h] {
addedPrevs[hNext]++
}
// push next hashes, but only if all past hashes have been added
for _, hNext := range l.next[h].Sorted() {
if addedPrevs[hNext] == len(l.prev[hNext]) {
push(hNext)
}
}
return e, nil
}
}
// Items returns the Entries in canonical order
func (l *CoLog) Items() []*Entry {
// output Entry slice
out := make([]*Entry, 0, len(l.prev))
// query everything
res := l.Query(Query{})
var (
e *Entry
err error
)
for err == nil {
e, err = res()
if err == nil {
out = append(out, e)
}
}
if err != io.EOF {
panic(err)
}
return out
}
// Heads returns the Hashes of the dangling heads.
func (l *CoLog) Heads() []Hash {
return l.heads.Sorted()
}
// FetchFromHead takes a Hash `head' and recursively adds the entries behind the head
func (l *CoLog) FetchFromHead(head Hash) error {
l.mutex.Lock()
defer l.mutex.Unlock()
if _, ok := l.prev[head]; ok {
return nil
}
// set up hash stack to track concurrentness
var stack = []Hash{}
// push to stack
push := func(hs ...Hash) {
stack = append(stack, hs...)
}
//pop from stack
pop := func() Hash {
h := stack[len(stack)-1]
stack = stack[:len(stack)-1]
return h
}
// walk colog from head
push(head)
for len(stack) > 0 {
// pop hash from stack
h := pop()
//ignore root
if h == "" {
continue
}
// check if already known
if l.contains(h) {
continue
}
// set as head if no followups known
if nexts := l.next[h]; len(nexts) == 0 {
l.heads.Add(h)
}
// get Entry
e, err := l.Get(h)
if err != nil {
continue
}
for hPrev := range e.Prev {
// remove from heads in case it was there
l.heads.Drop(hPrev)
// add to index
l.prev.Add(h, hPrev)
l.next.Add(hPrev, h)
// mark for recursion
push(hPrev)
}
l.chans.Send(e)
}
return nil
}
// Watch returns an Entry chan, notifying you of additions to the log
func (l *CoLog) Watch() <-chan *Entry {
return l.chans.New()
}
// Unwatch drops channel ch from the list of channels that are being written to
func (l *CoLog) Unwatch(ch <-chan *Entry) {
l.chans.Drop(ch)
}