-
Notifications
You must be signed in to change notification settings - Fork 0
/
postlist_blockstore.go
71 lines (58 loc) · 1.52 KB
/
postlist_blockstore.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
package main
import (
"github.com/ipfs/go-cid"
"github.com/ipfs/go-ipfs-blockstore"
cbor "github.com/ipfs/go-ipld-cbor"
mh "github.com/multiformats/go-multihash"
)
// Get, Put operations
func putPost(bs blockstore.Blockstore, p *Smor) (*cid.Cid, error) {
// convert it to cbor ipld
nd, err := cbor.WrapObject(p, mh.SHA2_256, -1)
if err != nil {
return nil, err
}
// write it to the blockstore (a content addressing layer over any storage backend)
if err := bs.Put(nd); err != nil {
return nil, err
}
// return its content identifier
return nd.Cid(), nil
}
func getPost(bs blockstore.Blockstore, c *cid.Cid) (*Smor, error) {
// read the data from the datastore
blk, err := bs.Get(c)
if err != nil {
return nil, err
}
// unmarshal it into a smor object
var smor Smor
if err := cbor.DecodeInto(blk.RawData(), &smor); err != nil {
return nil, err
}
return &smor, nil
}
func (ml *MerkleListNode) getPostByIndex(bs blockstore.Blockstore, i int) (*Smor, error) {
return getPost(bs, ml.Posts[i])
}
func putNode(bs blockstore.Blockstore, mln *MerkleListNode) (*cid.Cid, error) {
node, err := cbor.WrapObject(mln, mh.SHA2_256, -1)
if err != nil {
return nil, err
}
if err := bs.Put(node); err != nil {
return nil, err
}
return node.Cid(), nil
}
func getNode(bs blockstore.Blockstore, c *cid.Cid) (*MerkleListNode, error) {
blk, err := bs.Get(c)
if err != nil {
return nil, err
}
var mln MerkleListNode
if err := cbor.DecodeInto(blk.RawData(), &mln); err != nil {
return nil, err
}
return &mln, nil
}