forked from TRON-US/go-unixfs
-
Notifications
You must be signed in to change notification settings - Fork 3
/
metadagbuilder.go
136 lines (109 loc) · 3.38 KB
/
metadagbuilder.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
package helpers
import (
"encoding/json"
ft "github.com/bittorrent/go-unixfs"
pb "github.com/bittorrent/go-unixfs/pb"
chunker "github.com/bittorrent/go-btfs-chunker"
ipld "github.com/ipfs/go-ipld-format"
)
// SuperMeta contains the common metadata fields for a BTFS object
// to be used for metadata readers and modifiers
type SuperMeta struct {
ChunkSize uint64
MaxLinks uint64
TrickleFormat bool
}
type MetaDagBuilderHelper struct {
db DagBuilderHelper
metaSpl chunker.Splitter
metaDagRoot ipld.Node // Metadata Dag root node
}
func NewMetaDagBuilderHelper(idb DagBuilderHelper, spl chunker.Splitter, mroot ipld.Node) *MetaDagBuilderHelper {
return &MetaDagBuilderHelper{
db: idb,
metaSpl: spl,
metaDagRoot: mroot,
}
}
func GetSuperMeta(chunkSize uint64, maxLinks uint64, trickleFormat bool) *SuperMeta {
return &SuperMeta{
ChunkSize: chunkSize,
MaxLinks: maxLinks,
TrickleFormat: trickleFormat,
}
}
func GetOrDefaultSuperMeta(b []byte) (*SuperMeta, error) {
var sm SuperMeta
if b != nil {
err := json.Unmarshal(b, &sm)
if err != nil {
return nil, err
}
}
if b == nil || sm.ChunkSize == 0 {
sm.ChunkSize = uint64(chunker.DefaultBlockSize)
}
if b == nil || sm.MaxLinks == 0 {
sm.MaxLinks = uint64(DefaultLinksPerBlock)
}
return &sm, nil
}
func (mdb *MetaDagBuilderHelper) SetSpl() {
mdb.db.spl = mdb.metaSpl
}
func (mdb *MetaDagBuilderHelper) Done() bool {
return mdb.db.Done()
}
func (mdb *MetaDagBuilderHelper) Next() ([]byte, error) {
return mdb.db.Next()
}
func (mdb *MetaDagBuilderHelper) NewLeafNode(data []byte, fsNodeType pb.Data_DataType) (ipld.Node, error) {
return mdb.db.NewLeafNode(data, fsNodeType)
}
func (mdb *MetaDagBuilderHelper) FillNodeLayer(node *FSNodeOverDag, fsNodeType pb.Data_DataType) error {
return mdb.db.FillNodeLayer(node, fsNodeType)
}
func (mdb *MetaDagBuilderHelper) AttachMetadataDag(root ipld.Node, fileSize uint64, mRoot ipld.Node) (ipld.Node, error) {
return mdb.db.AttachMetadataDag(root, fileSize, mRoot)
}
// NewMetaLeafDataNode builds a metadata `node` with the meta data
// obtained from the Metadata Splitter.
// It returns `ipld.Node` with the `dataSize`.
func (mdb *MetaDagBuilderHelper) NewLeafDataNode(fsNodeType pb.Data_DataType) (node ipld.Node, dataSize uint64, err error) {
metaDataChunk, err := mdb.Next()
if err != nil {
return nil, 0, err
}
dataSize = uint64(len(metaDataChunk))
// Create a new leaf node with token metadata.
node, err = mdb.NewLeafNode(metaDataChunk, ft.TTokenMeta)
if err != nil {
return nil, 0, err
}
return node, dataSize, nil
}
func (mdb *MetaDagBuilderHelper) Add(node ipld.Node) error {
return mdb.db.Add(node)
}
func (mdb *MetaDagBuilderHelper) Maxlinks() int {
return mdb.db.Maxlinks()
}
func (mdb *MetaDagBuilderHelper) NewFSNodeOverDag(dataType pb.Data_DataType) *FSNodeOverDag {
return mdb.db.NewFSNodeOverDag(dataType)
}
// SetMetaDagRoot sets metadata DAG root
func (mdb *MetaDagBuilderHelper) SetMetaDagRoot(root ipld.Node) {
mdb.metaDagRoot = root
}
// GetMetaDagRoot returns the root of the token metadata DAG
func (mdb *MetaDagBuilderHelper) GetMetaDagRoot() ipld.Node {
return mdb.metaDagRoot
}
// SetDb sets metadata DAG root
func (mdb *MetaDagBuilderHelper) SetDb(db *DagBuilderHelper) {
mdb.db = *db
}
// GetDb returns the DagbuilderHelper
func (mdb *MetaDagBuilderHelper) GetDb() DagBuilderHelper {
return mdb.db
}