forked from filecoin-project/go-hamt-ipld
/
hamt.go
350 lines (286 loc) · 6.63 KB
/
hamt.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
package hamt
import (
"context"
"fmt"
"math/big"
cid "github.com/ipfs/go-cid"
murmur3 "github.com/spaolacci/murmur3"
)
const arrayWidth = 3
type Node struct {
Bitfield *big.Int `refmt:"bf"`
Pointers []*Pointer `refmt:"p"`
// for fetching and storing children
store *CborIpldStore
}
func NewNode(cs *CborIpldStore) *Node {
return &Node{
Bitfield: big.NewInt(0),
Pointers: make([]*Pointer, 0),
store: cs,
}
}
type KV struct {
Key string
Value interface{}
}
type Pointer struct {
KVs []*KV `refmt:"v,omitempty"`
Link *cid.Cid `refmt:"l,omitempty"`
// cached node to avoid too many serialization operations
cache *Node
}
var hash = func(k string) []byte {
h := murmur3.New128()
h.Write([]byte(k))
return h.Sum(nil)
}
func (n *Node) Find(ctx context.Context, k string) (interface{}, error) {
var out interface{}
err := n.getValue(ctx, hash(k), 0, k, func(kv *KV) error {
out = kv.Value
return nil
})
if err != nil {
return nil, err
}
return out, nil
}
func (n *Node) Delete(ctx context.Context, k string) error {
return n.modifyValue(ctx, hash(k), 0, k, nil)
}
var ErrNotFound = fmt.Errorf("not found")
func (n *Node) getValue(ctx context.Context, hv []byte, depth int, k string, cb func(*KV) error) error {
idx := hv[depth]
if n.Bitfield.Bit(int(idx)) == 0 {
return ErrNotFound
}
cindex := byte(n.indexForBitPos(int(idx)))
c := n.getChild(cindex)
if c.isShard() {
chnd, err := c.loadChild(ctx, n.store)
if err != nil {
return err
}
return chnd.getValue(ctx, hv, depth+1, k, cb)
}
for _, kv := range c.KVs {
if kv.Key == k {
return cb(kv)
}
}
return ErrNotFound
}
func (p *Pointer) loadChild(ctx context.Context, ns *CborIpldStore) (*Node, error) {
if p.cache != nil {
return p.cache, nil
}
out, err := LoadNode(ctx, ns, p.Link)
if err != nil {
return nil, err
}
p.cache = out
return out, nil
}
func LoadNode(ctx context.Context, cs *CborIpldStore, c *cid.Cid) (*Node, error) {
var out Node
if err := cs.Get(ctx, c, &out); err != nil {
return nil, err
}
out.store = cs
return &out, nil
}
func (n *Node) checkSize(ctx context.Context) (uint64, error) {
c, err := n.store.Put(ctx, n)
if err != nil {
return 0, err
}
blk, err := n.store.Blocks.GetBlock(ctx, c)
if err != nil {
return 0, err
}
totsize := uint64(len(blk.RawData()))
for _, ch := range n.Pointers {
if ch.isShard() {
chnd, err := ch.loadChild(ctx, n.store)
if err != nil {
return 0, err
}
chsize, err := chnd.checkSize(ctx)
if err != nil {
return 0, err
}
totsize += chsize
}
}
return totsize, nil
}
func (n *Node) Flush(ctx context.Context) error {
for _, p := range n.Pointers {
if p.cache != nil {
if err := p.cache.Flush(ctx); err != nil {
return err
}
c, err := n.store.Put(ctx, p.cache)
if err != nil {
return err
}
p.cache = nil
p.Link = c
}
}
return nil
}
func (n *Node) Set(ctx context.Context, k string, v interface{}) error {
return n.modifyValue(ctx, hash(k), 0, k, v)
}
func (n *Node) cleanChild(chnd *Node, cindex byte) error {
l := len(chnd.Pointers)
switch {
case l == 0:
return fmt.Errorf("incorrectly formed HAMT")
case l == 1:
// TODO: only do this if its a value, cant do this for shards unless pairs requirements are met.
ps := chnd.Pointers[0]
if ps.isShard() {
return nil
}
return n.setChild(cindex, ps)
case l <= arrayWidth:
var chvals []*KV
for _, p := range chnd.Pointers {
if p.isShard() {
return nil
}
for _, sp := range p.KVs {
if len(chvals) == arrayWidth {
return nil
}
chvals = append(chvals, sp)
}
}
return n.setChild(cindex, &Pointer{KVs: chvals})
default:
return nil
}
}
func (n *Node) modifyValue(ctx context.Context, hv []byte, depth int, k string, v interface{}) error {
idx := int(hv[depth])
if n.Bitfield.Bit(idx) != 1 {
return n.insertChild(idx, k, v)
}
cindex := byte(n.indexForBitPos(idx))
child := n.getChild(cindex)
if child.isShard() {
chnd, err := child.loadChild(ctx, n.store)
if err != nil {
return err
}
if err := chnd.modifyValue(ctx, hv, depth+1, k, v); err != nil {
return err
}
// CHAMP optimization, ensure trees look correct after deletions
if v == nil {
if err := n.cleanChild(chnd, cindex); err != nil {
return err
}
}
return nil
}
if v == nil {
for i, p := range child.KVs {
if p.Key == k {
if len(child.KVs) == 1 {
return n.rmChild(cindex, idx)
}
copy(child.KVs[i:], child.KVs[i+1:])
child.KVs = child.KVs[:len(child.KVs)-1]
return nil
}
}
return ErrNotFound
}
// check if key already exists
for _, p := range child.KVs {
if p.Key == k {
p.Value = v
return nil
}
}
// If the array is full, create a subshard and insert everything into it
if len(child.KVs) >= arrayWidth {
sub := NewNode(n.store)
if err := sub.modifyValue(ctx, hv, depth+1, k, v); err != nil {
return err
}
for _, p := range child.KVs {
if err := sub.modifyValue(ctx, hash(p.Key), depth+1, p.Key, p.Value); err != nil {
return err
}
}
c, err := n.store.Put(ctx, sub)
if err != nil {
return err
}
return n.setChild(cindex, &Pointer{Link: c})
}
// otherwise insert the new element into the array in order
np := &KV{Key: k, Value: v}
for i := 0; i < len(child.KVs); i++ {
if k < child.KVs[i].Key {
child.KVs = append(child.KVs[:i], append([]*KV{np}, child.KVs[i:]...)...)
return nil
}
}
child.KVs = append(child.KVs, np)
return nil
}
func (n *Node) insertChild(idx int, k string, v interface{}) error {
if v == nil {
return ErrNotFound
}
i := n.indexForBitPos(idx)
n.Bitfield.SetBit(n.Bitfield, idx, 1)
p := &Pointer{KVs: []*KV{{Key: k, Value: v}}}
n.Pointers = append(n.Pointers[:i], append([]*Pointer{p}, n.Pointers[i:]...)...)
return nil
}
func (n *Node) setChild(i byte, p *Pointer) error {
n.Pointers[i] = p
return nil
}
func (n *Node) rmChild(i byte, idx int) error {
copy(n.Pointers[i:], n.Pointers[i+1:])
n.Pointers = n.Pointers[:len(n.Pointers)-1]
n.Bitfield.SetBit(n.Bitfield, idx, 0)
return nil
}
func (n *Node) getChild(i byte) *Pointer {
if int(i) >= len(n.Pointers) || i < 0 {
return nil
}
return n.Pointers[i]
}
func (n *Node) Copy() *Node {
nn := NewNode(n.store)
nn.Bitfield.Set(n.Bitfield)
nn.Pointers = make([]*Pointer, len(n.Pointers))
for i, p := range n.Pointers {
pp := &Pointer{}
if p.cache != nil {
pp.cache = p.cache.Copy()
}
pp.Link = p.Link
if p.KVs != nil {
pp.KVs = make([]*KV, len(p.KVs))
for j, kv := range p.KVs {
pp.KVs[j] = &KV{Key: kv.Key, Value: kv.Value}
}
}
nn.Pointers[i] = pp
}
return nn
}
func (p *Pointer) isShard() bool {
return p.Link != nil
}