-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.go
204 lines (187 loc) · 5.18 KB
/
block.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
package block
import (
"fmt"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-unixfsnode/data"
"github.com/ipld/go-fixtureplate/unixfs"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/linking"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/node/basicnode"
_ "github.com/ipld/go-ipld-prime/codec/raw"
)
type Block struct {
ls linking.LinkSystem
Cid cid.Cid
IpldPath datamodel.Path
UnixfsPath datamodel.Path
DataType int64 // go-unixfsnode/data.Data_*
Children []Child
ByteOffset int64
ByteSize int64
Arity int64 // for sharded nodes
FieldData []byte // bitfield data for sharded nodes
BlockSizes []int64 // for sharded files
ShardIndex string
}
type Child struct {
ls linking.LinkSystem
block *Block
Cid cid.Cid
IpldPath datamodel.Path
UnixfsPath datamodel.Path
ByteOffset int64
ShardIndex string
}
func (c *Child) Block() (Block, error) {
if c.block != nil {
return *c.block, nil
}
block, err := NewBlockWith(c.ls, c.Cid, c.IpldPath, c.UnixfsPath, c.ByteOffset, c.ShardIndex)
if err != nil {
return Block{}, err
}
c.block = &block
return block, nil
}
func NewBlock(ls linking.LinkSystem, c cid.Cid) (Block, error) {
return NewBlockWith(ls, c, datamodel.Path{}, datamodel.Path{}, 0, "")
}
func NewBlockWith(
ls linking.LinkSystem,
c cid.Cid,
ipldPath,
unixfsPath datamodel.Path,
byteOffset int64,
shardIndex string,
) (Block, error) {
node, err := ls.Load(linking.LinkContext{}, cidlink.Link{Cid: c}, basicnode.Prototype.Any)
if err != nil {
return Block{}, err
}
var dt int64 = -1
var byteSize int64
var children []Child
var fieldData []byte
var arity int64
var blockSizes []int64
if node.Kind() == datamodel.Kind_Bytes {
byt, err := node.AsBytes()
if err != nil {
return Block{}, err
}
byteSize = int64(len(byt))
} else {
pbNode, err := unixfs.ToPbnode(node)
if err != nil {
return Block{}, err
}
ufsData, err := unixfs.ToData(pbNode)
if err != nil {
return Block{}, err
}
dt = ufsData.DataType.Int()
switch dt {
case data.Data_Raw:
if !pbNode.Data.Exists() {
return Block{}, fmt.Errorf("raw block has no data")
}
byteSize = int64(len(pbNode.Data.Must().Bytes()))
case data.Data_Directory:
children = make([]Child, pbNode.Links.Length())
for itr := pbNode.Links.Iterator(); !itr.Done(); {
ii, v := itr.Next()
if !v.Name.Exists() {
return Block{}, fmt.Errorf("directory link has no name")
}
children[ii] = Child{
ls: ls,
Cid: v.Hash.Link().(cidlink.Link).Cid,
IpldPath: ipldPath.AppendSegmentString("Links").AppendSegmentInt(ii).AppendSegmentString("Hash"),
UnixfsPath: unixfsPath.AppendSegmentString(v.Name.Must().String()),
}
}
case data.Data_File:
children = make([]Child, pbNode.Links.Length())
blockSizes = make([]int64, ufsData.BlockSizes.Length())
li := ufsData.BlockSizes.Iterator()
for !li.Done() {
ii, v := li.Next()
blockSizes[ii] = v.Int()
}
var offset int64
for itr := pbNode.Links.Iterator(); !itr.Done(); {
ii, v := itr.Next()
if !v.Name.Exists() {
return Block{}, fmt.Errorf("directory link has no name")
}
children[ii] = Child{
ls: ls,
Cid: v.Hash.Link().(cidlink.Link).Cid,
IpldPath: ipldPath.AppendSegmentString("Links").AppendSegmentInt(ii).AppendSegmentString("Hash"),
UnixfsPath: unixfsPath,
ByteOffset: byteOffset + offset, // TODO: is this correct for nested file structures?
}
offset += blockSizes[ii]
byteSize += blockSizes[ii]
}
case data.Data_HAMTShard:
arity = ufsData.FieldFanout().Must().Int()
fieldData = ufsData.FieldData().Must().Bytes()
pfxLen := len(fmt.Sprintf("%X", arity-1))
children = make([]Child, pbNode.Links.Length())
for itr := pbNode.Links.Iterator(); !itr.Done(); {
ii, v := itr.Next()
if !v.Name.Exists() {
return Block{}, fmt.Errorf("directory link has no name")
}
pfx := v.Name.Must().String()[:pfxLen]
name := v.Name.Must().String()[pfxLen:]
unixfsPath := unixfsPath
if name != "" {
unixfsPath = unixfsPath.AppendSegmentString(name)
}
children[ii] = Child{
ls: ls,
Cid: v.Hash.Link().(cidlink.Link).Cid,
IpldPath: ipldPath.AppendSegmentString("Links").AppendSegmentInt(ii).AppendSegmentString("Hash"),
UnixfsPath: unixfsPath,
ShardIndex: pfx,
}
}
case data.Data_Metadata:
return Block{}, fmt.Errorf("metadata block not supported")
case data.Data_Symlink:
return Block{}, fmt.Errorf("metadata block not supported")
default:
return Block{}, fmt.Errorf("unknown data type: %d", ufsData.Type())
}
}
return Block{
ls: ls,
Cid: c,
DataType: dt,
IpldPath: ipldPath,
UnixfsPath: unixfsPath,
ByteOffset: byteOffset,
ByteSize: byteSize,
Children: children,
Arity: arity,
FieldData: fieldData,
BlockSizes: blockSizes,
ShardIndex: shardIndex,
}, nil
}
func (b Block) DataTypeString() string {
if dtn, has := data.DataTypeNames[b.DataType]; has {
return dtn
}
return "RawLeaf"
}
func (b Block) Length() int64 {
var l int64
for _, c := range b.BlockSizes {
l += c
}
return l
}