forked from textileio/go-threads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
record.go
130 lines (108 loc) · 3.16 KB
/
record.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
package net
import (
"fmt"
"sync"
"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p-core/peer"
core "github.com/singyiu/go-threads/core/net"
)
type linkedRecord interface {
Cid() cid.Cid
PrevID() cid.Cid
}
// Collector maintains an ordered list of records from multiple sources (thread-safe)
type recordCollector struct {
rs map[peer.ID]*recordSequence
lock sync.Mutex
}
func newRecordCollector() *recordCollector {
return &recordCollector{rs: make(map[peer.ID]*recordSequence)}
}
// Store the record of the log.
func (r *recordCollector) Store(lid peer.ID, rec core.Record) {
r.lock.Lock()
defer r.lock.Unlock()
seq, found := r.rs[lid]
if !found {
seq = newRecordSequence()
r.rs[lid] = seq
}
seq.Store(rec)
}
// List all previously stored records in a proper order if the latter exists.
func (r *recordCollector) List() (map[peer.ID][]core.Record, error) {
r.lock.Lock()
defer r.lock.Unlock()
logSeqs := make(map[peer.ID][]core.Record, len(r.rs))
for id, seq := range r.rs {
ordered, ok := seq.List()
if !ok {
return nil, fmt.Errorf("disjoint record sequence in log %s", id)
}
casted := make([]core.Record, len(ordered))
for i := 0; i < len(ordered); i++ {
casted[i] = ordered[i].(core.Record)
}
logSeqs[id] = casted
}
return logSeqs, nil
}
// Not a thread-safe structure
type recordSequence struct {
fragments [][]linkedRecord
set map[cid.Cid]struct{}
}
func newRecordSequence() *recordSequence {
return &recordSequence{set: make(map[cid.Cid]struct{})}
}
func (s *recordSequence) Store(rec linkedRecord) {
// verify if record is already contained in some fragment
if _, found := s.set[rec.Cid()]; found {
return
}
s.set[rec.Cid()] = struct{}{}
// now try to find a sequence to be attached to
for i, fragment := range s.fragments {
if fragment[len(fragment)-1].Cid() == rec.PrevID() {
s.fragments[i] = append(fragment, rec)
return
} else if fragment[0].PrevID() == rec.Cid() {
s.fragments[i] = append([]linkedRecord{rec}, fragment...)
return
}
}
// start a new fragment
s.fragments = append(s.fragments, []linkedRecord{rec})
}
// return reconstructed sequence and success flag
func (s *recordSequence) List() ([]linkedRecord, bool) {
LOOP:
// avoid recursion as sequences could be pretty large
for {
if len(s.fragments) == 1 {
return s.fragments[0], true
}
// take a fragment ...
fragment := s.fragments[0]
fHead, fTail := fragment[len(fragment)-1], fragment[0]
// ... and try to compose it with another one
for i, candidate := range s.fragments[1:] {
cHead, cTail := candidate[len(candidate)-1], candidate[0]
// index shifted by slicing
i += 1
if fHead.Cid() == cTail.PrevID() {
// composition: (tail) <- fragment <- candidate <- (head)
s.fragments[0] = append(fragment, candidate...)
s.fragments = append(s.fragments[:i], s.fragments[i+1:]...)
continue LOOP
} else if fTail.PrevID() == cHead.Cid() {
// composition: (tail) <- candidate <- fragment <- (head)
s.fragments[i] = append(candidate, fragment...)
s.fragments = s.fragments[1:]
continue LOOP
}
}
// no composition found, hence there are at least two disjoint fragments
return nil, false
}
}