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

Mantaray manifest remove fork #4819

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
18 changes: 17 additions & 1 deletion pkg/manifest/mantaray/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,26 @@ func (n *Node) Remove(ctx context.Context, path []byte, ls LoadSaver) error {
rest := path[len(f.prefix):]
if len(rest) == 0 {
asabya marked this conversation as resolved.
Show resolved Hide resolved
// full path matched

// Make the ref all zeros to indicate that this node needs to re-uploaded
n.ref = zero32
// Set the refBytesSize to 32 so that unmarshall works properly
n.refBytesSize = len(n.ref)

// remove the fork
delete(n.forks, path[0])
return nil
}
return f.Node.Remove(ctx, rest, ls)
err := f.Node.Remove(ctx, rest, ls)
if err != nil {
return err
}
// Make the ref all zeros to indicate that this node needs to re-uploaded
n.ref = zero32
// Set the refBytesSize to 32 so that unmarshall works properly
n.refBytesSize = len(n.ref)

return nil
}

func common(a, b []byte) (c []byte) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/manifest/mantaray/persist.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
package mantaray

import (
"bytes"
"context"
"errors"

"golang.org/x/sync/errgroup"
)

Expand Down Expand Up @@ -61,7 +61,7 @@ func (n *Node) Save(ctx context.Context, s Saver) error {
}

func (n *Node) save(ctx context.Context, s Saver) error {
if n != nil && n.ref != nil {
if n != nil && n.ref != nil && !bytes.Equal(n.ref, zero32) {
asabya marked this conversation as resolved.
Show resolved Hide resolved
return nil
}
select {
Expand Down
115 changes: 115 additions & 0 deletions pkg/manifest/mantaray/persist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bytes"
"context"
"crypto/sha256"
"errors"
"sync"
"testing"

Expand Down Expand Up @@ -62,6 +63,120 @@ func TestPersistIdempotence(t *testing.T) {
}
}

func TestPersistRemove(t *testing.T) {
t.Parallel()

for _, tc := range []struct {
name string
toAdd []mantaray.NodeEntry
toRemove [][]byte
}{
{
name: "simple",
toAdd: []mantaray.NodeEntry{
{
Path: []byte("/"),
Metadata: map[string]string{
"index-document": "index.html",
},
},
{
Path: []byte("index.html"),
},
{
Path: []byte("img/1.png"),
},
{
Path: []byte("img/2.png"),
},
{
Path: []byte("robots.txt"),
},
},
toRemove: [][]byte{
[]byte("img/2.png"),
},
},
{
name: "nested-prefix-is-not-collapsed",
toAdd: []mantaray.NodeEntry{
{
Path: []byte("index.html"),
},
{
Path: []byte("img/1.png"),
},
{
Path: []byte("img/2/test1.png"),
},
{
Path: []byte("img/2/test2.png"),
},
{
Path: []byte("robots.txt"),
},
},
toRemove: [][]byte{
[]byte("img/2/test1.png"),
},
},
} {
ctx := context.Background()
var ls mantaray.LoadSaver = newMockLoadSaver()
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

// add and persist
n := mantaray.New()
for i := 0; i < len(tc.toAdd); i++ {
c := tc.toAdd[i].Path
e := tc.toAdd[i].Entry
if len(e) == 0 {
e = append(make([]byte, 32-len(c)), c...)
}
m := tc.toAdd[i].Metadata
err := n.Add(ctx, c, e, m, ls)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
}
err := n.Save(ctx, ls)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}

ref := n.Reference()
// reload and remove
nn := mantaray.NewNodeRef(ref)
for i := 0; i < len(tc.toRemove); i++ {
c := tc.toRemove[i]
err := nn.Remove(ctx, c, ls)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
}

err = nn.Save(ctx, ls)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}

ref = nn.Reference()

// reload and lookup removed node
nnn := mantaray.NewNodeRef(ref)
for i := 0; i < len(tc.toRemove); i++ {
c := tc.toRemove[i]
_, err = nnn.LookupNode(ctx, c, ls)
if !errors.Is(err, mantaray.ErrNotFound) {
t.Fatalf("expected not found error, got %v", err)
}
}
})
}
}

type addr [32]byte
type mockLoadSaver struct {
mtx sync.Mutex
Expand Down