@@ -6,50 +6,16 @@ import (
6
6
"fmt"
7
7
"time"
8
8
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
-
15
9
block "github.com/ipfs/go-block-format"
16
10
cbor "github.com/ipfs/go-ipld-cbor"
17
11
recbor "github.com/polydawn/refmt/cbor"
18
12
atlas "github.com/polydawn/refmt/obj/atlas"
19
13
20
- //ds "gx/ipfs/QmdHG8MAuARdGHxx4rPQASLcvhz24fzjSQq7AJRAQEorq5/go-datastore"
21
14
cid "github.com/ipfs/go-cid"
22
15
mh "github.com/multiformats/go-multihash"
23
16
cbg "github.com/whyrusleeping/cbor-gen"
24
17
)
25
18
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
-
53
19
func NewSerializationError (err error ) error {
54
20
return SerializationError {err }
55
21
}
@@ -71,6 +37,20 @@ func (se SerializationError) Is(o error) bool {
71
37
return ok
72
38
}
73
39
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
+
74
54
type blocks interface {
75
55
GetBlock (context.Context , cid.Cid ) (block.Block , error )
76
56
AddBlock (block.Block ) error
@@ -93,8 +73,8 @@ func (bs *bswrapper) AddBlock(blk block.Block) error {
93
73
return bs .bs .Put (blk )
94
74
}
95
75
96
- func CSTFromBstore (bs Blockstore ) * CborIpldStore {
97
- return & CborIpldStore {
76
+ func CSTFromBstore (bs Blockstore ) * BasicCborIpldStore {
77
+ return & BasicCborIpldStore {
98
78
Blocks : & bswrapper {bs },
99
79
}
100
80
}
@@ -120,11 +100,11 @@ func (mb *mockBlocks) AddBlock(b block.Block) error {
120
100
return nil
121
101
}
122
102
123
- func NewCborStore () * CborIpldStore {
124
- return & CborIpldStore {Blocks : newMockBlocks ()}
103
+ func NewCborStore () * BasicCborIpldStore {
104
+ return & BasicCborIpldStore {Blocks : newMockBlocks ()}
125
105
}
126
106
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 {
128
108
ctx , cancel := context .WithTimeout (ctx , time .Second * 10 )
129
109
defer cancel ()
130
110
@@ -138,7 +118,6 @@ func (s *CborIpldStore) Get(ctx context.Context, c cid.Cid, out interface{}) err
138
118
if err := cu .UnmarshalCBOR (bytes .NewReader (blk .RawData ())); err != nil {
139
119
return NewSerializationError (err )
140
120
}
141
-
142
121
return nil
143
122
}
144
123
@@ -153,7 +132,7 @@ type cidProvider interface {
153
132
Cid () cid.Cid
154
133
}
155
134
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 ) {
157
136
mhType := uint64 (mh .BLAKE2B_MIN + 31 )
158
137
mhLen := - 1
159
138
codec := uint64 (cid .DagCBOR )
@@ -171,7 +150,7 @@ func (s *CborIpldStore) Put(ctx context.Context, v interface{}) (cid.Cid, error)
171
150
if ok {
172
151
buf := new (bytes.Buffer )
173
152
if err := cm .MarshalCBOR (buf ); err != nil {
174
- return cid .Undef , NewSerializationError ( err )
153
+ return cid .Undef , err
175
154
}
176
155
177
156
pref := cid.Prefix {
@@ -212,3 +191,7 @@ func (s *CborIpldStore) Put(ctx context.Context, v interface{}) (cid.Cid, error)
212
191
213
192
return nd .Cid (), nil
214
193
}
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