-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.go
314 lines (255 loc) · 8.27 KB
/
example.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
package main
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"os"
blocks "github.com/ipfs/go-block-format"
bsrv "github.com/ipfs/go-blockservice"
car "github.com/ipfs/go-car"
cid "github.com/ipfs/go-cid"
datastore "github.com/ipfs/go-datastore"
blockstore "github.com/ipfs/go-ipfs-blockstore"
offline "github.com/ipfs/go-ipfs-exchange-offline"
dag "github.com/ipfs/go-merkledag"
// leveldbds "github.com/ipfs/go-ds-leveldb"
flatfsds "github.com/ipfs/go-ds-flatfs"
cbor "github.com/ipfs/go-ipld-cbor"
multihash "github.com/multiformats/go-multihash"
ipld "github.com/ipld/go-ipld-prime"
dagcbor "github.com/ipld/go-ipld-prime/encoding/dagcbor"
dagjson "github.com/ipld/go-ipld-prime/encoding/dagjson"
ipldfluent "github.com/ipld/go-ipld-prime/fluent"
ipldfree "github.com/ipld/go-ipld-prime/impl/free"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
num2words "github.com/divan/num2words"
)
func main() {
{
// ---------------------- //
// Create a new .car file //
// ---------------------- //
// Datastore does raw Put(key, data) where the keys are in Datastore-specific form, not very friendly
dataStoreIn, err := createDataStore("datastore.in")
if err != nil {
panic(err)
}
defer dataStoreIn.Close()
// Blockstore will do a nice `Put(block.Cid, block.RawData())` for us as a wraper around the Datastore
blockStoreIn := blockstore.NewBlockstore(dataStoreIn)
// Create some data using ipld-cbor
classicRoot, err := createIpldClassicData(blockStoreIn)
if err != nil {
panic(err)
}
// Create some example IPLD data with ipld-prime
primeRoot, err := createIpldPrimeData(blockStoreIn)
if err != nil {
panic(err)
}
// Make a .car file with two roots, one for each of our sets of data, it will slurp out the merkle dag from the
// Datastore for those two roots
if err = writeCar("example.car", blockStoreIn, []cid.Cid{*classicRoot, *primeRoot}); err != nil {
panic(err)
}
if err = dataStoreIn.Close(); err != nil {
panic(err)
}
}
// ----------------------------- //
// Read in an existing .car file //
// ----------------------------- //
{
// Create a fresh Datastore that we can load .car data into
dataStoreOut, err := createDataStore("datastore.out")
if err != nil {
panic(err)
}
defer dataStoreOut.Close()
blockStoreOut := blockstore.NewBlockstore(dataStoreOut)
// Load from the .car file, it will extract each block and insert it into the
// Datastore/Blockstore/Blockservice we supply
roots, err := readCar("example.car", blockStoreOut)
if err != nil {
panic(err)
}
// Demonstrate we have the data we stored
for idx, root := range roots {
fmt.Printf("example.car header root %v: %v\n", idx+1, root)
}
// Print out the blocks(s) we created with ipld-cbor
if err = dumpIpldClassic(blockStoreOut, roots[0]); err != nil {
panic(err)
}
// Print out the blocks(s) we created with ipld-prime
if err = dumpIpldPrime(blockStoreOut, roots[1]); err != nil {
panic(err)
}
}
}
// Given a file path/name, a Blockstore and an array of root CIDs, extract the merkle DAGs
// from that Blockstore from those roots and store them in a .car file by the name given
func writeCar(path string, blockStore blockstore.Blockstore, roots []cid.Cid) error {
// go-car uses DAGService for traversing from the roots; in turn it needs a BlockService
// which we can instantiate in offline mode
blockService := bsrv.New(blockStore, offline.Exchange(blockStore))
dagService := dag.NewDAGService(blockService)
outFile, err := os.Create(path)
if err != nil {
return err
}
defer outFile.Close()
outWriter := bufio.NewWriter(outFile)
if err = car.WriteCar(context.Background(), dagService, roots, outWriter); err != nil {
return err
}
outWriter.Flush()
return nil
}
// Given a file pathname and a Blockstore, extract the blocks in the .car file into that
// Blockstore and return the root CIDs listed in the .car file header.
func readCar(path string, blockStore blockstore.Blockstore) ([]cid.Cid, error) {
inFile, err := os.Open("example.car")
if err != nil {
return nil, err
}
defer inFile.Close()
inReader := bufio.NewReader(inFile)
header, err := car.LoadCar(blockStore, inReader)
if err != nil {
return nil, err
}
return header.Roots, nil
}
func createDataStore(path string) (datastore.Batching, error) {
return flatfsds.CreateOrOpen(path, flatfsds.Suffix(2), true)
// could also use leveldb:
// dataStoreOut, err := leveldbds.NewDatastore(path), &leveldbds.Options{})
}
//---------------------------------------------
// Classic ipld-cbor data manipulation
//---------------------------------------------
type cborTest struct {
S string
I int
B bool
}
func createIpldClassicData(blockStore blockstore.Blockstore) (*cid.Cid, error) {
cbor.RegisterCborType(cborTest{})
cnd1, err := cbor.WrapObject(cborTest{"foo", 100, false}, multihash.SHA2_256, -1)
if err != nil {
return nil, err
}
if err = blockStore.Put(cnd1); err != nil {
return nil, err
}
cid := cnd1.Cid()
return &cid, nil
}
func dumpIpldClassic(blockStore blockstore.Blockstore, root cid.Cid) error {
blkOut, err := blockStore.Get(root)
if err != nil {
return err
}
var out = cborTest{}
err = cbor.DecodeInto(blkOut.RawData(), &out)
if err != nil {
return err
}
fmt.Printf("Decoded classic block [%v]:\n\t%v\n", blkOut.Cid(), out)
return nil
}
//---------------------------------------------
// New ipld-prime hotness data manipulation
//---------------------------------------------
func createIpldPrimeData(blockStore blockstore.Blockstore) (*cid.Cid, error) {
var err error
var lnk ipld.Link
var fnb = ipldfluent.WrapNodeBuilder(ipldfree.NodeBuilder())
linkBuilder := cidlink.LinkBuilder{cid.Prefix{
Version: 1,
Codec: cid.DagCBOR,
MhType: multihash.SHA2_256,
MhLength: -1,
}}
var blockBuilder = func(node ipld.Node) (ipld.Link, []byte, error) {
buf := bytes.Buffer{}
lnk, err = linkBuilder.Build(context.Background(), ipld.LinkContext{}, node,
func(ipld.LinkContext) (io.Writer, ipld.StoreCommitter, error) {
return &buf, func(lnk ipld.Link) error { return nil }, nil
},
)
if err != nil {
return nil, nil, err
}
return lnk, buf.Bytes(), nil
}
// a linked list of blocks each referring to the previous
for i := 0; i < 10; i++ {
// create a map with the shape: { number: "one", previous: CID }
node := fnb.CreateMap(func(mb ipldfluent.MapBuilder, knb ipldfluent.NodeBuilder, vnb ipldfluent.NodeBuilder) {
mb.Insert(knb.CreateString("number"), vnb.CreateString(num2words.Convert(i)))
if lnk != nil {
mb.Insert(knb.CreateString("previous"), vnb.CreateLink(lnk))
}
})
lnk, buf, err := blockBuilder(node)
cidLink, _ := lnk.(cidlink.Link)
blk, err := blocks.NewBlockWithCid(buf, cidLink.Cid)
if err != nil {
return nil, err
}
blockStore.Put(blk)
}
cidLink, _ := lnk.(cidlink.Link)
return &cidLink.Cid, nil
}
// Note that we could use this same technique to print out the block created with ipld-cbor
func dumpIpldPrime(blockStore blockstore.Blockstore, root cid.Cid) error {
var lnk ipld.Link
lnk = cidlink.Link{root}
linkLoader := func(lnk ipld.Link, _ ipld.LinkContext) (io.Reader, error) {
cidLink, _ := lnk.(cidlink.Link)
blk, err := blockStore.Get(cidLink.Cid)
if err != nil {
return nil, err
}
return bytes.NewBuffer(blk.RawData()), nil
}
for {
loadedNode, err := lnk.Load(context.Background(), ipld.LinkContext{}, ipldfree.NodeBuilder(), linkLoader)
if err != nil {
panic(err)
}
number, err := loadedNode.TraverseField("number")
if err != nil {
panic(err)
}
numberString, err := number.AsString()
if err != nil {
panic(err)
}
cidLink, _ := lnk.(cidlink.Link)
fmt.Printf("ipld-prime block [%v]\n\tnumber: %v\n", cidLink.Cid, numberString)
loadedNode, err = loadedNode.TraverseField("previous")
if err != nil {
break
}
if loadedNode.ReprKind() != ipld.ReprKind_Link {
panic("`previous` property is not a link")
}
lnk, err = loadedNode.AsLink()
if err != nil {
panic(err)
}
}
return nil
}
func init() {
// the default prefix of "blocks" makes a directory that flatfs datastore doesn't handle properly
blockstore.BlockPrefix = datastore.NewKey("")
_ = dagcbor.Encoder // force registration of dagcbor with the cidlink encoder
_ = dagjson.Encoder // force registration of dagjson with the cidlink encoder
}