From 8787863112d0265d56d497a4711eefccb119975e Mon Sep 17 00:00:00 2001 From: asabya Date: Mon, 4 Nov 2024 22:35:43 +0530 Subject: [PATCH] refactor: nil ref instead of all zeros --- pkg/manifest/mantaray/marshal.go | 4 +++- pkg/manifest/mantaray/node.go | 21 ++++----------------- pkg/manifest/mantaray/persist.go | 3 +-- pkg/manifest/mantaray/persist_test.go | 2 -- 4 files changed, 8 insertions(+), 22 deletions(-) diff --git a/pkg/manifest/mantaray/marshal.go b/pkg/manifest/mantaray/marshal.go index fcd19c55411..4e0324c35dd 100644 --- a/pkg/manifest/mantaray/marshal.go +++ b/pkg/manifest/mantaray/marshal.go @@ -287,6 +287,9 @@ func (n *Node) UnmarshalBinary(data []byte) error { bb.fromBytes(data[offset:]) offset += 32 // skip forks return bb.iter(func(b byte) error { + if refBytesSize == 0 { + return nil + } f := &fork{} if len(data) < offset+nodeForkTypeBytesSize { @@ -296,7 +299,6 @@ func (n *Node) UnmarshalBinary(data []byte) error { nodeType := data[offset] nodeForkSize := nodeForkPreReferenceSize + refBytesSize - if nodeTypeIsWithMetadataType(nodeType) { if len(data) < offset+nodeForkPreReferenceSize+refBytesSize+nodeForkMetadataBytesSize { return fmt.Errorf("not enough bytes for node fork: %d (%d) on byte '%x': %w", (len(data) - offset), (nodeForkPreReferenceSize + refBytesSize + nodeForkMetadataBytesSize), []byte{b}, ErrInvalidManifest) diff --git a/pkg/manifest/mantaray/node.go b/pkg/manifest/mantaray/node.go index 437eeb460da..2e2754f200b 100644 --- a/pkg/manifest/mantaray/node.go +++ b/pkg/manifest/mantaray/node.go @@ -311,28 +311,15 @@ func (n *Node) Remove(ctx context.Context, path []byte, ls LoadSaver) error { return ErrNotFound } rest := path[len(f.prefix):] + defer func() { + n.ref = nil + }() if len(rest) == 0 { - // 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 } - 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 + return f.Node.Remove(ctx, rest, ls) } func common(a, b []byte) (c []byte) { diff --git a/pkg/manifest/mantaray/persist.go b/pkg/manifest/mantaray/persist.go index 8e4cfda6c1d..8b18896fb61 100644 --- a/pkg/manifest/mantaray/persist.go +++ b/pkg/manifest/mantaray/persist.go @@ -5,7 +5,6 @@ package mantaray import ( - "bytes" "context" "errors" "golang.org/x/sync/errgroup" @@ -61,7 +60,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 && !bytes.Equal(n.ref, zero32) { + if n != nil && n.ref != nil { return nil } select { diff --git a/pkg/manifest/mantaray/persist_test.go b/pkg/manifest/mantaray/persist_test.go index 33538e59e8d..301237eff89 100644 --- a/pkg/manifest/mantaray/persist_test.go +++ b/pkg/manifest/mantaray/persist_test.go @@ -145,7 +145,6 @@ func TestPersistRemove(t *testing.T) { if err != nil { t.Fatalf("expected no error, got %v", err) } - ref := n.Reference() // reload and remove nn := mantaray.NewNodeRef(ref) @@ -163,7 +162,6 @@ func TestPersistRemove(t *testing.T) { } ref = nn.Reference() - // reload and lookup removed node nnn := mantaray.NewNodeRef(ref) for i := 0; i < len(tc.toRemove); i++ {