Skip to content

Commit 4e60a82

Browse files
Stebalienrvagg
authored andcommitted
Custom bitfield type for custom serialization/deserialization
Without touching the generated code. This is a slightly breaking change, but it shouldn't affect anyone.
1 parent 8399499 commit 4e60a82

File tree

5 files changed

+76
-52
lines changed

5 files changed

+76
-52
lines changed

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ test:
88
go test ./...
99
.PHONY: test
1010

11+
gen:
12+
go run ./gen
13+
.PHONY: gen
14+
1115
coverage:
1216
go test -coverprofile=coverage.out ./...
1317
go tool cover -html=coverage.out

bitfield.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package hamt
2+
3+
import (
4+
"io"
5+
"math/big"
6+
7+
cbg "github.com/whyrusleeping/cbor-gen"
8+
)
9+
10+
type Bitfield struct {
11+
big.Int
12+
}
13+
14+
func (b *Bitfield) MarshalCBOR(w io.Writer) error {
15+
return cbg.WriteByteArray(w, b.Bytes())
16+
}
17+
18+
func (b *Bitfield) UnmarshalCBOR(r io.Reader) error {
19+
bytes, err := cbg.ReadByteArray(r, cbg.ByteArrayMaxLen)
20+
if err != nil {
21+
return err
22+
}
23+
b.SetBytes(bytes)
24+
25+
return nil
26+
}

cbor_gen.go

+41-45
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hamt.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7-
"math/big"
87
"sort"
98

109
cid "github.com/ipfs/go-cid"
@@ -81,7 +80,7 @@ type HashFunc func([]byte) []byte
8180
// pointers [Pointer]
8281
// } representation tuple
8382
type Node struct {
84-
Bitfield *big.Int
83+
Bitfield Bitfield
8584
Pointers []*Pointer
8685

8786
bitWidth int
@@ -237,7 +236,6 @@ func (n *Node) Delete(ctx context.Context, k string) (bool, error) {
237236
// Constructs a new node value.
238237
func newNode(cs cbor.IpldStore, hashFn HashFunc, bitWidth int) *Node {
239238
nd := &Node{
240-
Bitfield: big.NewInt(0),
241239
Pointers: make([]*Pointer, 0),
242240
bitWidth: bitWidth,
243241
hash: hashFn,
@@ -776,7 +774,7 @@ func (n *Node) modifyValue(ctx context.Context, hv *hashBits, k []byte, v *cbg.D
776774
// bucket containing the single key/value pair at that position.
777775
func (n *Node) insertKV(idx int, k []byte, v *cbg.Deferred) error {
778776
i := n.indexForBitPos(idx)
779-
n.Bitfield.SetBit(n.Bitfield, idx, 1)
777+
n.Bitfield.SetBit(&n.Bitfield.Int, idx, 1)
780778

781779
p := &Pointer{KVs: []*KV{{Key: k, Value: v}}}
782780

@@ -798,7 +796,7 @@ func (n *Node) setPointer(i byte, p *Pointer) error {
798796
func (n *Node) rmPointer(i byte, idx int) error {
799797
copy(n.Pointers[i:], n.Pointers[i+1:])
800798
n.Pointers = n.Pointers[:len(n.Pointers)-1]
801-
n.Bitfield.SetBit(n.Bitfield, idx, 0)
799+
n.Bitfield.SetBit(&n.Bitfield.Int, idx, 0)
802800

803801
return nil
804802
}
@@ -824,7 +822,7 @@ func (n *Node) getPointer(i byte) *Pointer {
824822
func (n *Node) Copy() *Node {
825823
// TODO(rvagg): clarify what situations this method is actually useful for.
826824
nn := newNode(n.store, n.hash, n.bitWidth)
827-
nn.Bitfield.Set(n.Bitfield)
825+
nn.Bitfield.Set(&n.Bitfield.Int)
828826
nn.Pointers = make([]*Pointer, len(n.Pointers))
829827

830828
for i, p := range n.Pointers {

uhamt.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
// associated array. Indexes `[1]` and `[2]` are not present, but index `[3]`
1414
// is at the second position of our Pointers array.
1515
func (n *Node) indexForBitPos(bp int) int {
16-
return indexForBitPos(bp, n.Bitfield)
16+
return indexForBitPos(bp, &n.Bitfield.Int)
1717
}
1818

1919
func indexForBitPos(bp int, bitfield *big.Int) int {

0 commit comments

Comments
 (0)