Skip to content

Commit

Permalink
refactor: nil ref instead of all zeros
Browse files Browse the repository at this point in the history
  • Loading branch information
asabya committed Nov 4, 2024
1 parent 0069db3 commit 8787863
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 22 deletions.
4 changes: 3 additions & 1 deletion pkg/manifest/mantaray/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
21 changes: 4 additions & 17 deletions pkg/manifest/mantaray/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions pkg/manifest/mantaray/persist.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package mantaray

import (
"bytes"
"context"
"errors"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions pkg/manifest/mantaray/persist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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++ {
Expand Down

0 comments on commit 8787863

Please sign in to comment.