Skip to content

Commit

Permalink
Merge pull request ipfs/go-ipfs-blockstore#36 from ipfs/fix/getsize
Browse files Browse the repository at this point in the history
return the correct size when only "has" is cached

This commit was moved from ipfs/go-ipfs-blockstore@5c0c8bd
  • Loading branch information
Stebalien authored Dec 9, 2019
2 parents bc81914 + 674921b commit 0849598
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
9 changes: 7 additions & 2 deletions blockstore/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 blockstore/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 0849598

Please sign in to comment.