forked from CUIT-CBI/merkle-dag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdag.go
209 lines (200 loc) · 4.48 KB
/
dag.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
package merkledag
import (
"encoding/json"
"hash"
)
const (
LIST_LIMIT = 2048
BLOCK_LIMIT = 256 * 1024
)
const (
BLOB = "blob"
LIST = "list"
TREE = "tree"
)
type Link struct {
Name string
Hash []byte
Size int
}
type Object struct {
Links []Link
Data []byte
}
func dfsForSlice(hight int, node File, store KVStore, seedId int, h hash.Hash) (*Object, int) {
if hight == 1 {
if (len(node.Bytes()) - seedId) <= 256*1024 {
data := node.Bytes()[seedId:]
blob := Object{
Links: nil,
Data: data,
}
jsonData, _ := json.Marshal(blob)
h.Reset()
h.Write(jsonData)
exists, _ := store.Has(h.Sum(nil))
if !exists {
store.Put(h.Sum(nil), data)
}
return &blob, len(data)
}
links := &Object{}
totalLen := 0
for i := 1; i <= 4096; i++ {
end := seedId + 256*1024
if len(node.Bytes()) < end {
end = len(node.Bytes())
}
data := node.Bytes()[seedId:end]
blob := Object{
Links: nil,
Data: data,
}
totalLen += len(data)
jsonData, _ := json.Marshal(blob)
h.Reset()
h.Write(jsonData)
exists, _ := store.Has(h.Sum(nil))
if !exists {
store.Put(h.Sum(nil), data)
}
links.Links = append(links.Links, Link{
Hash: h.Sum(nil),
Size: len(data),
})
links.Data = append(links.Data, []byte("data")...)
seedId += 256 * 1024
if seedId >= len(node.Bytes()) {
break
}
}
jsonData, _ := json.Marshal(links)
h.Reset()
h.Write(jsonData)
exists, _ := store.Has(h.Sum(nil))
if !exists {
store.Put(h.Sum(nil), jsonData)
}
return links, totalLen
} else {
links := &Object{}
totalLen := 0
for i := 1; i <= 4096; i++ {
if seedId >= len(node.Bytes()) {
break
}
child, childLen := dfsForSlice(hight-1, node, store, seedId, h)
totalLen += childLen
jsonData, _ := json.Marshal(child)
h.Reset()
h.Write(jsonData)
links.Links = append(links.Links, Link{
Hash: h.Sum(nil),
Size: childLen,
})
typeName := "link"
if child.Links == nil {
typeName = "data"
}
links.Data = append(links.Data, []byte(typeName)...)
}
jsonData, _ := json.Marshal(links)
h.Reset()
h.Write(jsonData)
exists, _ := store.Has(h.Sum(nil))
if !exists {
store.Put(h.Sum(nil), jsonData)
}
return links, totalLen
}
}
func sliceFile(node File, store KVStore, h hash.Hash) *Object {
if len(node.Bytes()) <= 256*1024 {
data := node.Bytes()
blob := Object{
Links: nil,
Data: data,
}
jsonData, _ := json.Marshal(blob)
h.Reset()
h.Write(jsonData)
exists, _ := store.Has(h.Sum(nil))
if !exists {
store.Put(h.Sum(nil), data)
}
return &blob
}
linkLen := (len(node.Bytes()) + (256*1024 - 1)) / (256 * 1024)
hight := 0
tmp := linkLen
for {
hight++
tmp /= 4096
if tmp == 0 {
break
}
}
res, _ := dfsForSlice(hight, node, store, 0, h)
return res
}
func sliceDirectory(node Dir, store KVStore, h hash.Hash) *Object {
iter := node.It()
tree := &Object{}
for iter.Next() {
elem := iter.Node()
if elem.Type() == FILE {
file := elem.(File)
fileSlice := sliceFile(file, store, h)
jsonData, _ := json.Marshal(fileSlice)
h.Reset()
h.Write(jsonData)
tree.Links = append(tree.Links, Link{
Hash: h.Sum(nil),
Size: int(file.Size()),
Name: file.Name(),
})
elemType := "link"
if fileSlice.Links == nil {
elemType = "data"
}
tree.Data = append(tree.Data, []byte(elemType)...)
} else {
dir := elem.(Dir)
dirSlice := sliceDirectory(dir, store, h)
jsonData, _ := json.Marshal(dirSlice)
h.Reset()
h.Write(jsonData)
tree.Links = append(tree.Links, Link{
Hash: h.Sum(nil),
Size: int(dir.Size()),
Name: dir.Name(),
})
elemType := "tree"
tree.Data = append(tree.Data, []byte(elemType)...)
}
}
jsonData, _ := json.Marshal(tree)
h.Reset()
h.Write(jsonData)
exists, _ := store.Has(h.Sum(nil))
if !exists {
store.Put(h.Sum(nil), jsonData)
}
return tree
}
func Add(store KVStore, node Node, h hash.Hash) []byte {
// 将分片写入KVStore,并返回Merkle Root
if node.Type() == FILE {
file := node.(File)
fileSlice := sliceFile(file, store, h)
jsonData, _ := json.Marshal(fileSlice)
h.Write(jsonData)
return h.Sum(nil)
} else {
dir := node.(Dir)
dirSlice := sliceDirectory(dir, store, h)
jsonData, _ := json.Marshal(dirSlice)
h.Write(jsonData)
return h.Sum(nil)
}
}