Skip to content

Commit

Permalink
Added differences for root leaves
Browse files Browse the repository at this point in the history
  • Loading branch information
f7f376a1fcd0d0e11a10ed1b6577c9 committed Aug 19, 2023
1 parent acd630e commit a549705
Show file tree
Hide file tree
Showing 5 changed files with 384 additions and 117 deletions.
208 changes: 174 additions & 34 deletions dag/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,39 +80,43 @@ func CreateDag(path string, encoding ...multibase.Encoding) (*Dag, error) {
return nil, err
}

leaf, err := CheckMetaFile(path, dag, encoder)
metaLeaf, err := CheckMetaFile(path, dag, encoder)
if err != nil {
log.Println("Failed to check meta file")
} else {
builder.AddLink("0", leaf.Hash)
leaf.SetLabel("0")
dag.AddLeaf(leaf, encoder, nil)
builder.AddLink("0", metaLeaf.Hash)
metaLeaf.SetLabel("0")
dag.AddLeaf(metaLeaf, encoder, nil)
}

dag.AddLeaf(metaLeaf, encoder, nil)

for _, entry := range entries {
if entry.Name() != ".meta" {
leaf, err := processEntry(entry, &path, dag, encoder)
if err != nil {
return nil, err
}

label := builder.GetNextAvailableLabel()
label := dag.GetNextAvailableLabel()
builder.AddLink(label, leaf.Hash)
leaf.SetLabel(label)
dag.AddLeaf(leaf, encoder, nil)
}
}

leaf, err = builder.BuildLeaf(encoder)

leaf, err := builder.BuildRootLeaf(encoder, len(dag.Leafs))
if err != nil {
return nil, err
}

dag.AddLeaf(leaf, encoder, nil)

rootHash := leaf.Hash
return dag.BuildDag(rootHash), nil
builtDag := dag.BuildDag(leaf.Hash)

builtDag.GenerateLabelMap()

return builtDag, nil
}

func processMetaFile(path *string, basePath *string, dag *DagBuilder, encoder multibase.Encoder) (*DagLeaf, error) {
Expand Down Expand Up @@ -147,7 +151,7 @@ func processMetaFile(path *string, basePath *string, dag *DagBuilder, encoder mu
return nil, err
}

label := builder.GetNextAvailableLabel()
label := dag.GetNextAvailableLabel()
builder.AddLink(label, chunkLeaf.Hash)
chunkLeaf.SetLabel(label)
dag.AddLeaf(chunkLeaf, encoder, nil)
Expand Down Expand Up @@ -180,23 +184,14 @@ func processEntry(entry fs.FileInfo, path *string, dag *DagBuilder, encoder mult
return nil, err
}

leaf, err := CheckMetaFile(entryPath, dag, encoder)
if err != nil {
log.Println("Failed to check meta file")
} else {
builder.AddLink("0", leaf.Hash)
leaf.SetLabel("0")
dag.AddLeaf(leaf, encoder, nil)
}

for _, entry := range entries {
if entry.Name() != ".meta" {
leaf, err := processEntry(entry, &entryPath, dag, encoder)
if err != nil {
return nil, err
}

label := builder.GetNextAvailableLabel()
label := dag.GetNextAvailableLabel()
builder.AddLink(label, leaf.Hash)
leaf.SetLabel(label)
dag.AddLeaf(leaf, encoder, nil)
Expand Down Expand Up @@ -227,7 +222,7 @@ func processEntry(entry fs.FileInfo, path *string, dag *DagBuilder, encoder mult
return nil, err
}

label := builder.GetNextAvailableLabel()
label := dag.GetNextAvailableLabel()
builder.AddLink(label, chunkLeaf.Hash)
chunkLeaf.SetLabel(label)
dag.AddLeaf(chunkLeaf, encoder, nil)
Expand Down Expand Up @@ -260,7 +255,8 @@ func chunkFile(fileData []byte, chunkSize int) [][]byte {

func CreateDagBuilder() *DagBuilder {
return &DagBuilder{
Leafs: map[string]*DagLeaf{},
Labels: map[string]string{},
Leafs: map[string]*DagLeaf{},
}
}

Expand All @@ -271,6 +267,8 @@ func (b *DagBuilder) AddLeaf(leaf *DagLeaf, encoder multibase.Encoder, parentLea
if !exists {
parentLeaf.AddLink(leaf.Hash)
}

b.Labels[label] = leaf.Hash
}

b.Leafs[leaf.Hash] = leaf
Expand All @@ -280,22 +278,34 @@ func (b *DagBuilder) AddLeaf(leaf *DagLeaf, encoder multibase.Encoder, parentLea

func (b *DagBuilder) BuildDag(root string) *Dag {
return &Dag{
Leafs: b.Leafs,
Root: root,
Leafs: b.Leafs,
Labels: b.Labels,
Root: root,
}
}

func (dag *Dag) Verify(encoder multibase.Encoder) (bool, error) {
result := true

for _, leaf := range dag.Leafs {
leafResult, err := leaf.VerifyLeaf(encoder)
if err != nil {
return false, err
}
for hash, leaf := range dag.Leafs {
if hash == dag.Root {
leafResult, err := leaf.VerifyRootLeaf(encoder)
if err != nil {
return false, err
}

if !leafResult {
return false, nil
}
} else {
leafResult, err := leaf.VerifyLeaf(encoder)
if err != nil {
return false, err
}

if !leafResult {
result = false
if !leafResult {
return false, nil
}
}
}

Expand Down Expand Up @@ -332,7 +342,8 @@ func (dag *Dag) CreateDirectory(path string, encoder multibase.Encoder) error {

/*
if runtime.GOOS == "windows" {
p, err := syscall.UTF16PtrFromString(fileName)
p, err := syscal
l.UTF16PtrFromString(fileName)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -363,9 +374,40 @@ func ReadDag(path string) (*Dag, error) {
return nil, fmt.Errorf("could not decode Dag: %w", err)
}

result.GenerateLabelMap()

return &result, nil
}

func (dag *Dag) GenerateLabelMap() {
dag.Labels = map[string]string{}

for hash, _ := range dag.Leafs {
label := GetLabel(hash)

dag.Labels[label] = hash
}
}

func (dag *Dag) FindLeafByLabel(label string) *DagLeaf {
hash, exists := dag.Labels[label]

fmt.Println("Find leaf: " + label)

if exists {
fmt.Println("Found leaf: " + hash)

leaf, result := dag.Leafs[hash]
if result {
return leaf
} else {
return nil
}
} else {
return nil
}
}

func (dag *Dag) FindLeafByHash(hash string) *DagLeaf {
leaf, result := dag.Leafs[hash]
if result {
Expand Down Expand Up @@ -398,7 +440,13 @@ func (dag *Dag) DeleteLeaf(leaf *DagLeaf, encoder multibase.Encoder) error {
return fmt.Errorf("Parent leaf does not exist")
}

err := parentLeaf.RemoveLink(GetLabel(leaf.Hash))
label := GetLabel(leaf.Hash)

if label == "0" {
return fmt.Errorf("Unable to delete leaf 0")
}

err := parentLeaf.RemoveLink(label)
if err != nil {
return err
}
Expand All @@ -407,11 +455,18 @@ func (dag *Dag) DeleteLeaf(leaf *DagLeaf, encoder multibase.Encoder) error {

delete(dag.Leafs, leaf.Hash)

err = dag.WriteToMetaLeaf(label, encoder)
if err != nil {
return err
}

root, err := dag.RegenerateDag(parentLeaf, encoder)
if err != nil {
return err
}

dag.GenerateLabelMap()

dag.Root = *root

return nil
Expand Down Expand Up @@ -503,7 +558,92 @@ func (dag *Dag) RemoveChildren(leaf *DagLeaf, encoder multibase.Encoder) {
}
}

// There must be a better way, but this is all I could think of doing at the time
func (dag *Dag) AddLeaf(leaf *DagLeaf) {
dag.Leafs[leaf.Hash] = leaf
}

func (dag *Dag) GetDataFromLeaf(leaf *DagLeaf) ([]byte, error) {
if len(leaf.Data) <= 0 {
return []byte{}, nil
}

var content []byte

if len(leaf.Links) > 0 {
for _, link := range leaf.Links {
childLeaf := dag.Leafs[link]
if childLeaf == nil {
return nil, fmt.Errorf("invalid link: %s", link)
}

content = append(content, childLeaf.Data...)
}
} else {
content = leaf.Data
}

return content, nil
}

func (dag *Dag) WriteToMetaLeaf(label string, encoder multibase.Encoder) error {
leaf := dag.FindLeafByLabel("0")

if leaf == nil {
return fmt.Errorf("Meta leaf does not exist")
}

content, err := dag.GetDataFromLeaf(leaf)
if err != nil {
return err
}

builder := CreateDagLeafBuilder(".meta")

builder.SetType(FileLeafType)

fileChunks := chunkFile(content, ChunkSize)

if len(fileChunks) == 1 {
builder.SetData(fileChunks[0])
} else {
for i, chunk := range fileChunks {
chunkEntryPath := filepath.Join(".meta", strconv.Itoa(i))
chunkBuilder := CreateDagLeafBuilder(chunkEntryPath)

chunkBuilder.SetType(ChunkLeafType)
chunkBuilder.SetData(chunk)

chunkLeaf, err := chunkBuilder.BuildLeaf(encoder)
if err != nil {
return err
}

label := dag.GetNextAvailableLabel()
builder.AddLink(label, chunkLeaf.Hash)
chunkLeaf.SetLabel(label)
dag.AddLeaf(chunkLeaf)
}
}

metaLeaf, err := builder.BuildLeaf(encoder)
if err != nil {
return err
}

rootLeaf := dag.Leafs[dag.Root]

if err != nil {
log.Println("Failed to update meta leaf")
} else {
metaLeaf.SetLabel("0")
rootLeaf.AddLink(metaLeaf.Hash)

dag.AddLeaf(metaLeaf)
}

return nil
}

func (dag *Dag) DoesExistMoreThanOnce(hash string) bool {
count := 0

Expand Down
Loading

0 comments on commit a549705

Please sign in to comment.