-
Notifications
You must be signed in to change notification settings - Fork 40
/
builder.go
149 lines (130 loc) · 2.98 KB
/
builder.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
package merkle
import (
"container/list"
"github.com/icon-project/goloop/common/crypto"
"github.com/icon-project/goloop/common/db"
"github.com/icon-project/goloop/common/errors"
)
type DataRequester interface {
OnData(value []byte, builder Builder) error
}
type RequestIterator interface {
Next() bool
Key() []byte
BucketIDs() []db.BucketID
}
type Builder interface {
OnData(value []byte) error
UnresolvedCount() int
Requests() RequestIterator
RequestData(id db.BucketID, key []byte, requester DataRequester)
Database() db.Database
Flush(write bool) error
}
type request struct {
key []byte
bucketIDs []db.BucketID
requesters []DataRequester
}
type merkleBuilder struct {
store db.Database
requests *list.List
requestMap map[string]*list.Element
}
type requestIterator struct {
element *list.Element
request *request
}
func (i *requestIterator) Next() bool {
if i.element == nil {
return false
} else {
i.request = i.element.Value.(*request)
i.element = i.element.Next()
return true
}
}
func (i *requestIterator) BucketIDs() []db.BucketID {
if i.request != nil {
return i.request.bucketIDs
}
return nil
}
func (i *requestIterator) Key() []byte {
if i.request != nil {
return i.request.key
}
return nil
}
func (b *merkleBuilder) Requests() RequestIterator {
return &requestIterator{
element: b.requests.Front(),
}
}
func (b *merkleBuilder) OnData(value []byte) error {
key := crypto.SHA3Sum256(value)
reqID := string(key)
if e, ok := b.requestMap[reqID]; ok {
req := e.Value.(*request)
for i, requester := range req.requesters {
bkID := req.bucketIDs[i]
bk, err := b.store.GetBucket(bkID)
if err != nil {
return err
}
if err := bk.Set(key, value); err != nil {
return err
}
if err := requester.OnData(value, b); err != nil {
return err
}
}
b.requests.Remove(e)
return nil
} else {
return errors.New("IllegalArguments")
}
}
func (b *merkleBuilder) RequestData(id db.BucketID, key []byte, requester DataRequester) {
if key == nil {
return
}
reqID := string(key)
if e, ok := b.requestMap[reqID]; ok {
req := e.Value.(*request)
req.bucketIDs = append(req.bucketIDs, id)
req.requesters = append(req.requesters, requester)
} else {
req := &request{
key: key,
bucketIDs: []db.BucketID{id},
requesters: []DataRequester{requester},
}
e := b.requests.PushBack(req)
b.requestMap[reqID] = e
}
}
func (b *merkleBuilder) UnresolvedCount() int {
return b.requests.Len()
}
func (b *merkleBuilder) Flush(write bool) error {
if ldb, ok := b.store.(db.LayerDB); ok {
return ldb.Flush(write)
}
return nil
}
func (b *merkleBuilder) Database() db.Database {
return b.store
}
func NewBuilder(dbase db.Database) Builder {
ldb := db.NewLayerDB(dbase)
return NewBuilderWithRawDatabase(ldb)
}
func NewBuilderWithRawDatabase(dbase db.Database) Builder {
builder := &merkleBuilder{
store: dbase,
requests: list.New(),
requestMap: make(map[string]*list.Element),
}
return builder
}