Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dag2file.go #5

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,33 @@ package merkledag
import "hash"

func Add(store KVStore, node Node, h hash.Hash) []byte {

// TODO 将分片写入到KVStore中,并返回Merkle Root
return nil
var stack []Node
stack = append(stack, node)
for {
node = stack[len(stack)-1]
stack = stack[:len(stack)-1]
// 检查节点是否为文件
if fileNode, ok := node.(File); ok {
// 如果是文件节点,将其字节放入 KVStore
err := store.Put([]byte("fileData"), fileNode.Bytes())
if err != nil {
// 处理错误
return nil
}
//计算当前 Merkle Root
h.Sum(fileNode.Bytes())
} else if dirNode, ok := node.(Dir); ok {
// 如果是目录节点,则遍历文件并入栈
dirIterator := dirNode.It()
for dirIterator.Next() {
fileOrDirNode := dirIterator.Node()
stack = append(stack, fileOrDirNode)
}
}
}
// 计算并返回 Merkle Root
return h.Sum(nil)
}

78 changes: 78 additions & 0 deletions dag2file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package merkledag

import (
"encoding/json"
"strings"
)

// Hash to file

// example a path : /doc/tmp/temp.txt
func Hash2File(store KVStore, hash []byte, path string, hp HashPool) []byte {
// 根据hash和path, 返回对应的文件, hash对应的类型是tree
flag, _ := store.Has(hash)
if flag {
objBinary, _ := store.Get(hash)
var obj Object
json.Unmarshal(objBinary, &obj)
pathArr := strings.Split(path, "/")
cur := 1
return getFileByDir(obj, pathArr, cur, store)
}
return nil
}

func getFileByDir(obj Object, pathArr []string, cur int, store KVStore) []byte {
if cur >= len(pathArr) {
return nil
}
index := 0
for i := range obj.Links {
objType := string(obj.Data[index : index+4])
index += 4
objInfo := obj.Links[i]
if objInfo.Name != pathArr[cur] {
continue
}
switch objType {
case TREE:
objDirBinary, _ := store.Get(objInfo.Hash)
var objDir Object
json.Unmarshal(objDirBinary, &objDir)
ans := getFileByDir(objDir, pathArr, cur+1, store)
if ans != nil {
return ans
}
case BLOB:
ans, _ := store.Get(objInfo.Hash)
return ans
case LIST:
objLinkBinary, _ := store.Get(objInfo.Hash)
var objLink Object
json.Unmarshal(objLinkBinary, &objLink)
ans := getFileByList(objLink, store)
return ans
}
}
return nil
}

func getFileByList(obj Object, store KVStore) []byte {
ans := make([]byte, 0)
index := 0
for i := range obj.Links {
curObjType := string(obj.Data[index : index+4])
index += 4
curObjLink := obj.Links[i]
curObjBinary, _ := store.Get(curObjLink.Hash)
var curObj Object
json.Unmarshal(curObjBinary, &curObj)
if curObjType == BLOB {
ans = append(ans, curObjBinary...)
} else { //List
tmp := getFileByList(curObj, store)
ans = append(ans, tmp...)
}
}
return ans
}
7 changes: 7 additions & 0 deletions hash_pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package merkledag

import "hash"

type HashPool interface {
Get() hash.Hash
}