Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

Commit

Permalink
fix(arc): return the correct size when only "has" is cached
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Dec 9, 2019
1 parent 87bfe42 commit 5e786b5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
9 changes: 7 additions & 2 deletions arc_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,15 @@ func (b *arccache) Has(k cid.Cid) (bool, error) {

func (b *arccache) GetSize(k cid.Cid) (int, error) {
if has, blockSize, ok := b.hasCached(k); ok {
if has {
if !has {
// don't have it, return
return -1, ErrNotFound
}
if blockSize >= 0 {
// have it and we know the size
return blockSize, nil
}
return -1, ErrNotFound
// we have it but don't know the size, ask the datastore.
}
blockSize, err := b.blockstore.GetSize(k)
if err == ErrNotFound {
Expand Down
21 changes: 20 additions & 1 deletion arc_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestGetFillsCache(t *testing.T) {
t.Fatal("has returned invalid result")
}
if blockSize, err := arc.GetSize(exampleBlock.Cid()); blockSize == -1 || err != nil {
t.Fatal("getsize returned invalid result")
t.Fatal("getsize returned invalid result", blockSize, err)
}
}

Expand Down Expand Up @@ -185,6 +185,25 @@ func TestGetSizeAfterSucessfulGetIsCached(t *testing.T) {
arc.GetSize(exampleBlock.Cid())
}

func TestGetSizeAfterSucessfulHas(t *testing.T) {
arc, bs, _ := createStores(t)

bs.Put(exampleBlock)
has, err := arc.Has(exampleBlock.Cid())
if err != nil {
t.Fatal(err)
}
if !has {
t.Fatal("expected to have block")
}

if size, err := arc.GetSize(exampleBlock.Cid()); err != nil {
t.Fatal(err)
} else if size != len(exampleBlock.RawData()) {
t.Fatalf("expected size %d, got %d", len(exampleBlock.RawData()), size)
}
}

func TestGetSizeMissingZeroSizeBlock(t *testing.T) {
arc, bs, cd := createStores(t)
emptyBlock := blocks.NewBlock([]byte{})
Expand Down

0 comments on commit 5e786b5

Please sign in to comment.