Skip to content

Commit 786e2a5

Browse files
authored
refactor: interfaceify CborIpldStore (#40)
* build: add gitignore * refactor: interfaceify CborIpldStore - mostly a port from https://github.com/ipfs/go-hamt-ipld/pull/36, thanks @ZenGround0 for doing the hard part
1 parent 7b434bb commit 786e2a5

File tree

6 files changed

+74
-59
lines changed

6 files changed

+74
-59
lines changed

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binary, built with `go test -c`
9+
*.test
10+
11+
# Output of the go coverage tool, specifically when used with LiteIDE
12+
*.out
13+
14+
# Dependency directories (remove the comment below to include it)
15+
# vendor/
16+
17+
.idea

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ require (
44
github.com/ipfs/go-block-format v0.0.2
55
github.com/ipfs/go-cid v0.0.3
66
github.com/ipfs/go-ipld-cbor v0.0.3
7-
github.com/multiformats/go-multihash v0.0.1
7+
github.com/multiformats/go-multihash v0.0.8
88
github.com/polydawn/refmt v0.0.0-20190221155625-df39d6c2d992
9-
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72
9+
github.com/spaolacci/murmur3 v1.1.0
1010
github.com/whyrusleeping/cbor-gen v0.0.0-20190910031516-c1cbffdb01bb
1111
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
1212
)

go.sum

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
12
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
23
github.com/gxed/hashland/keccakpg v0.0.1 h1:wrk3uMNaMxbXiHibbPO4S0ymqJMm41WiudyFSs7UnsU=
34
github.com/gxed/hashland/keccakpg v0.0.1/go.mod h1:kRzw3HkwxFU1mpmPP8v1WyQzwdGfmKFJ6tItnhQ67kU=
@@ -15,35 +16,49 @@ github.com/ipfs/go-ipld-cbor v0.0.3 h1:ENsxvybwkmke7Z/QJOmeJfoguj6GH3Y0YOaGrfy9Q
1516
github.com/ipfs/go-ipld-cbor v0.0.3/go.mod h1:wTBtrQZA3SoFKMVkp6cn6HMRteIB1VsmHA0AQFOn7Nc=
1617
github.com/ipfs/go-ipld-format v0.0.1 h1:HCu4eB/Gh+KD/Q0M8u888RFkorTWNIL3da4oc5dwc80=
1718
github.com/ipfs/go-ipld-format v0.0.1/go.mod h1:kyJtbkDALmFHv3QR6et67i35QzO3S0dCDnkOJhcZkms=
19+
github.com/jtolds/gls v4.2.1+incompatible h1:fSuqC+Gmlu6l/ZYAoZzx2pyucC8Xza35fpRVWLVmUEE=
1820
github.com/jtolds/gls v4.2.1+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
1921
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g=
2022
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ=
2123
github.com/minio/sha256-simd v0.0.0-20190131020904-2d45a736cd16 h1:5W7KhL8HVF3XCFOweFD3BNESdnO8ewyYTFT2R+/b8FQ=
2224
github.com/minio/sha256-simd v0.0.0-20190131020904-2d45a736cd16/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U=
25+
github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771 h1:MHkK1uRtFbVqvAgvWxafZe54+5uBxLluGylDiKgdhwo=
26+
github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM=
2327
github.com/mr-tron/base58 v1.1.0 h1:Y51FGVJ91WBqCEabAi5OPUz38eAx8DakuAm5svLcsfQ=
2428
github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8=
29+
github.com/mr-tron/base58 v1.1.2 h1:ZEw4I2EgPKDJ2iEw0cNmLB3ROrEmkOtXIkaG7wZg+78=
30+
github.com/mr-tron/base58 v1.1.2/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
2531
github.com/multiformats/go-base32 v0.0.3 h1:tw5+NhuwaOjJCC5Pp82QuXbrmLzWg7uxlMFp8Nq/kkI=
2632
github.com/multiformats/go-base32 v0.0.3/go.mod h1:pLiuGC8y0QR3Ue4Zug5UzK9LjgbkL8NSQj0zQ5Nz/AA=
2733
github.com/multiformats/go-multibase v0.0.1 h1:PN9/v21eLywrFWdFNsFKaU04kLJzuYzmrJR+ubhT9qA=
2834
github.com/multiformats/go-multibase v0.0.1/go.mod h1:bja2MqRZ3ggyXtZSEDKpl0uO/gviWFaSteVbWT51qgs=
2935
github.com/multiformats/go-multihash v0.0.1 h1:HHwN1K12I+XllBCrqKnhX949Orn4oawPkegHMu2vDqQ=
3036
github.com/multiformats/go-multihash v0.0.1/go.mod h1:w/5tugSrLEbWqlcgJabL3oHFKTwfvkofsjW2Qa1ct4U=
37+
github.com/multiformats/go-multihash v0.0.8 h1:wrYcW5yxSi3dU07n5jnuS5PrNwyHy0zRHGVoUugWvXg=
38+
github.com/multiformats/go-multihash v0.0.8/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew=
3139
github.com/polydawn/refmt v0.0.0-20190221155625-df39d6c2d992 h1:bzMe+2coZJYHnhGgVlcQKuRy4FSny4ds8dLQjw5P1XE=
3240
github.com/polydawn/refmt v0.0.0-20190221155625-df39d6c2d992/go.mod h1:uIp+gprXxxrWSjjklXD+mN4wed/tMfjMMmN/9+JsA9o=
41+
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
3342
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
43+
github.com/smartystreets/goconvey v0.0.0-20190222223459-a17d461953aa h1:E+gaaifzi2xF65PbDmuKI3PhLWY6G5opMLniFq8vmXA=
3444
github.com/smartystreets/goconvey v0.0.0-20190222223459-a17d461953aa/go.mod h1:2RVY1rIf+2J2o/IM9+vPq9RzmHDSseB7FoXiSNIUsoU=
35-
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 h1:qLC7fQah7D6K1B0ujays3HV9gkFtllcxhzImRR7ArPQ=
36-
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
45+
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
46+
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
47+
github.com/warpfork/go-wish v0.0.0-20180510122957-5ad1f5abf436 h1:qOpVTI+BrstcjTZLm2Yz/3sOnqkzj3FQoh0g+E5s3Gc=
3748
github.com/warpfork/go-wish v0.0.0-20180510122957-5ad1f5abf436/go.mod h1:x6AKhvSSexNrVSrViXSHUEbICjmGXhtgABaHIySUSGw=
38-
github.com/whyrusleeping/cbor-gen v0.0.0-20190822002707-4e02357de5c1 h1:wDIXmhgPVpV1gWj68IIj1zQNyp7QzBvr5d5UOvMqRNw=
39-
github.com/whyrusleeping/cbor-gen v0.0.0-20190822002707-4e02357de5c1/go.mod h1:xdlJQaiqipF0HW+Mzpg7XRM3fWbGvfgFlcppuvlkIvY=
40-
github.com/whyrusleeping/cbor-gen v0.0.0-20190822231004-8db835b09a5a h1:9oEQR9eq2H2JDmglMcrCa+TxUEYy3HKSiNoLIkPNy/U=
41-
github.com/whyrusleeping/cbor-gen v0.0.0-20190822231004-8db835b09a5a/go.mod h1:xdlJQaiqipF0HW+Mzpg7XRM3fWbGvfgFlcppuvlkIvY=
4249
github.com/whyrusleeping/cbor-gen v0.0.0-20190910031516-c1cbffdb01bb h1:8yBVx6dgk1GfkiWOQ+RbeDDBLCOZxOtmZ949O2uj5H4=
4350
github.com/whyrusleeping/cbor-gen v0.0.0-20190910031516-c1cbffdb01bb/go.mod h1:xdlJQaiqipF0HW+Mzpg7XRM3fWbGvfgFlcppuvlkIvY=
4451
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67 h1:ng3VDlRp5/DHpSWl02R4rM9I+8M2rhmsuLwAMmkLQWE=
4552
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
53+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
54+
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8 h1:1wopBVtVdWnn03fZelqdXTqk7U7zPQCb+T4rbU9ZEoU=
55+
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
56+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
57+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
4658
golang.org/x/sys v0.0.0-20190219092855-153ac476189d h1:Z0Ahzd7HltpJtjAHHxX8QFP3j1yYgiuvjbjRzDj/KH0=
4759
golang.org/x/sys v0.0.0-20190219092855-153ac476189d/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
60+
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
61+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
62+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
4863
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
4964
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

hamt.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type Node struct {
2020
Pointers []*Pointer `refmt:"p"`
2121

2222
// for fetching and storing children
23-
store *CborIpldStore
23+
store CborIpldStore
2424
bitWidth int
2525
}
2626

@@ -39,7 +39,7 @@ func UseTreeBitWidth(bitWidth int) Option {
3939

4040
// NewNode creates a new IPLD HAMT Node with the given store and given
4141
// options
42-
func NewNode(cs *CborIpldStore, options ...Option) *Node {
42+
func NewNode(cs CborIpldStore, options ...Option) *Node {
4343
nd := &Node{
4444
Bitfield: big.NewInt(0),
4545
Pointers: make([]*Pointer, 0),
@@ -123,7 +123,7 @@ func (n *Node) getValue(ctx context.Context, hv *hashBits, k string, cb func(*KV
123123
return ErrNotFound
124124
}
125125

126-
func (p *Pointer) loadChild(ctx context.Context, ns *CborIpldStore, bitWidth int) (*Node, error) {
126+
func (p *Pointer) loadChild(ctx context.Context, ns CborIpldStore, bitWidth int) (*Node, error) {
127127
if p.cache != nil {
128128
return p.cache, nil
129129
}
@@ -138,7 +138,7 @@ func (p *Pointer) loadChild(ctx context.Context, ns *CborIpldStore, bitWidth int
138138
return out, nil
139139
}
140140

141-
func LoadNode(ctx context.Context, cs *CborIpldStore, c cid.Cid, options ...Option) (*Node, error) {
141+
func LoadNode(ctx context.Context, cs CborIpldStore, c cid.Cid, options ...Option) (*Node, error) {
142142
var out Node
143143
if err := cs.Get(ctx, c, &out); err != nil {
144144
return nil, err
@@ -160,12 +160,12 @@ func (n *Node) checkSize(ctx context.Context) (uint64, error) {
160160
return 0, err
161161
}
162162

163-
blk, err := n.store.Blocks.GetBlock(ctx, c)
164-
if err != nil {
165-
return 0, err
163+
var def cbg.Deferred
164+
if err := n.store.Get(ctx, c, &def); err != nil {
165+
return 0, nil
166166
}
167167

168-
totsize := uint64(len(blk.RawData()))
168+
totsize := uint64(len(def.Raw))
169169
for _, ch := range n.Pointers {
170170
if ch.isShard() {
171171
chnd, err := ch.loadChild(ctx, n.store, n.bitWidth)

hamt_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ func TestSetGet(t *testing.T) {
413413
}
414414
}
415415

416-
func nodesEqual(t *testing.T, store *CborIpldStore, n1, n2 *Node) bool {
416+
func nodesEqual(t *testing.T, store CborIpldStore, n1, n2 *Node) bool {
417417
ctx := context.Background()
418418
err := n1.Flush(ctx)
419419
if err != nil {

ipld.go

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,16 @@ import (
66
"fmt"
77
"time"
88

9-
/*
10-
bstore "github.com/ipfs/go-ipfs/blocks/blockstore"
11-
bserv "github.com/ipfs/go-ipfs/blockservice"
12-
offline "github.com/ipfs/go-ipfs/exchange/offline"
13-
*/
14-
159
block "github.com/ipfs/go-block-format"
1610
cbor "github.com/ipfs/go-ipld-cbor"
1711
recbor "github.com/polydawn/refmt/cbor"
1812
atlas "github.com/polydawn/refmt/obj/atlas"
1913

20-
//ds "gx/ipfs/QmdHG8MAuARdGHxx4rPQASLcvhz24fzjSQq7AJRAQEorq5/go-datastore"
2114
cid "github.com/ipfs/go-cid"
2215
mh "github.com/multiformats/go-multihash"
2316
cbg "github.com/whyrusleeping/cbor-gen"
2417
)
2518

26-
// THIS IS ALL TEMPORARY CODE
27-
28-
/*
29-
func init() {
30-
cbor.RegisterCborType(cbor.BigIntAtlasEntry)
31-
cbor.RegisterCborType(Node{})
32-
cbor.RegisterCborType(Pointer{})
33-
34-
kvAtlasEntry := atlas.BuildEntry(KV{}).Transform().TransformMarshal(
35-
atlas.MakeMarshalTransformFunc(func(kv KV) ([]interface{}, error) {
36-
return []interface{}{kv.Key, kv.Value}, nil
37-
})).TransformUnmarshal(
38-
atlas.MakeUnmarshalTransformFunc(func(v []interface{}) (KV, error) {
39-
return KV{
40-
Key: v[0].(string),
41-
Value: v[1],
42-
}, nil
43-
})).Complete()
44-
cbor.RegisterCborType(kvAtlasEntry)
45-
}
46-
*/
47-
48-
type CborIpldStore struct {
49-
Blocks blocks
50-
Atlas *atlas.Atlas
51-
}
52-
5319
func NewSerializationError(err error) error {
5420
return SerializationError{err}
5521
}
@@ -71,6 +37,20 @@ func (se SerializationError) Is(o error) bool {
7137
return ok
7238
}
7339

40+
// CborIpldStore is the ipld store interface required by the hamt.
41+
type CborIpldStore interface {
42+
Get(ctx context.Context, c cid.Cid, out interface{}) error
43+
Put(ctx context.Context, v interface{}) (cid.Cid, error)
44+
}
45+
46+
// BasicCborIpldStore is a simple CborIpldStore made from wrapping a blockstore.
47+
type BasicCborIpldStore struct {
48+
Blocks blocks
49+
Atlas *atlas.Atlas
50+
}
51+
52+
var _ CborIpldStore = &BasicCborIpldStore{}
53+
7454
type blocks interface {
7555
GetBlock(context.Context, cid.Cid) (block.Block, error)
7656
AddBlock(block.Block) error
@@ -93,8 +73,8 @@ func (bs *bswrapper) AddBlock(blk block.Block) error {
9373
return bs.bs.Put(blk)
9474
}
9575

96-
func CSTFromBstore(bs Blockstore) *CborIpldStore {
97-
return &CborIpldStore{
76+
func CSTFromBstore(bs Blockstore) *BasicCborIpldStore {
77+
return &BasicCborIpldStore{
9878
Blocks: &bswrapper{bs},
9979
}
10080
}
@@ -120,11 +100,11 @@ func (mb *mockBlocks) AddBlock(b block.Block) error {
120100
return nil
121101
}
122102

123-
func NewCborStore() *CborIpldStore {
124-
return &CborIpldStore{Blocks: newMockBlocks()}
103+
func NewCborStore() *BasicCborIpldStore {
104+
return &BasicCborIpldStore{Blocks: newMockBlocks()}
125105
}
126106

127-
func (s *CborIpldStore) Get(ctx context.Context, c cid.Cid, out interface{}) error {
107+
func (s *BasicCborIpldStore) Get(ctx context.Context, c cid.Cid, out interface{}) error {
128108
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
129109
defer cancel()
130110

@@ -138,7 +118,6 @@ func (s *CborIpldStore) Get(ctx context.Context, c cid.Cid, out interface{}) err
138118
if err := cu.UnmarshalCBOR(bytes.NewReader(blk.RawData())); err != nil {
139119
return NewSerializationError(err)
140120
}
141-
142121
return nil
143122
}
144123

@@ -153,7 +132,7 @@ type cidProvider interface {
153132
Cid() cid.Cid
154133
}
155134

156-
func (s *CborIpldStore) Put(ctx context.Context, v interface{}) (cid.Cid, error) {
135+
func (s *BasicCborIpldStore) Put(ctx context.Context, v interface{}) (cid.Cid, error) {
157136
mhType := uint64(mh.BLAKE2B_MIN + 31)
158137
mhLen := -1
159138
codec := uint64(cid.DagCBOR)
@@ -171,7 +150,7 @@ func (s *CborIpldStore) Put(ctx context.Context, v interface{}) (cid.Cid, error)
171150
if ok {
172151
buf := new(bytes.Buffer)
173152
if err := cm.MarshalCBOR(buf); err != nil {
174-
return cid.Undef, NewSerializationError(err)
153+
return cid.Undef, err
175154
}
176155

177156
pref := cid.Prefix{
@@ -212,3 +191,7 @@ func (s *CborIpldStore) Put(ctx context.Context, v interface{}) (cid.Cid, error)
212191

213192
return nd.Cid(), nil
214193
}
194+
195+
func (s *BasicCborIpldStore) GetBlock(ctx context.Context, cid cid.Cid) (block.Block, error) {
196+
return s.Blocks.GetBlock(ctx, cid)
197+
}

0 commit comments

Comments
 (0)