Skip to content

Commit

Permalink
Collapse when we completely delete
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Aug 25, 2020
1 parent ea81644 commit e6282fa
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
17 changes: 17 additions & 0 deletions amt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,23 @@ func TestBasicSetGet(t *testing.T) {

}

func TestRoundTrip(t *testing.T) {
bs := cbor.NewCborStore(newMockBlocks())
ctx := context.Background()
a := NewAMT(bs)
emptyCid, err := a.Flush(ctx)
require.NoError(t, err)

k := uint64(100000)
assertSet(t, a, k, "foo")
assertDelete(t, a, k)

c, err := a.Flush(ctx)
require.NoError(t, err)

require.Equal(t, emptyCid, c)
}

func TestOutOfRange(t *testing.T) {
ctx := context.Background()
bs := cbor.NewCborStore(newMockBlocks())
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/filecoin-project/go-amt-ipld v1.0.0 h1:jJteqdKcwFYoN9Wgs7MzDWVWvRWUfhhFo4bltJ7wrus=
github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls=
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
Expand Down
10 changes: 7 additions & 3 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,19 @@ func newNode(nd internal.Node, allowEmpty, expectLeaf bool) (*node, error) {
}

func (nd *node) collapse(ctx context.Context, bs cbor.IpldStore, height int) (int, error) {
if nd.links[0] == nil {
return height, nil
}
// If we have any links going "to the right", we can't collapse any
// more.
for _, l := range nd.links[1:] {
if l != nil {
return height, nil
}
}

// If we have _no_ links, we've collapsed everything.
if nd.links[0] == nil {
return 0, nil
}

// only one child, collapse it.

subn, err := nd.links[0].load(ctx, bs, height-1)
Expand Down

0 comments on commit e6282fa

Please sign in to comment.