Commit 5e786b5d authored by Steven Allen's avatar Steven Allen

fix(arc): return the correct size when only "has" is cached

parent 87bfe426
...@@ -86,10 +86,15 @@ func (b *arccache) Has(k cid.Cid) (bool, error) { ...@@ -86,10 +86,15 @@ func (b *arccache) Has(k cid.Cid) (bool, error) {
func (b *arccache) GetSize(k cid.Cid) (int, error) { func (b *arccache) GetSize(k cid.Cid) (int, error) {
if has, blockSize, ok := b.hasCached(k); ok { 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 blockSize, nil
} }
return -1, ErrNotFound // we have it but don't know the size, ask the datastore.
} }
blockSize, err := b.blockstore.GetSize(k) blockSize, err := b.blockstore.GetSize(k)
if err == ErrNotFound { if err == ErrNotFound {
......
...@@ -123,7 +123,7 @@ func TestGetFillsCache(t *testing.T) { ...@@ -123,7 +123,7 @@ func TestGetFillsCache(t *testing.T) {
t.Fatal("has returned invalid result") t.Fatal("has returned invalid result")
} }
if blockSize, err := arc.GetSize(exampleBlock.Cid()); blockSize == -1 || err != nil { 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)
} }
} }
...@@ -185,6 +185,25 @@ func TestGetSizeAfterSucessfulGetIsCached(t *testing.T) { ...@@ -185,6 +185,25 @@ func TestGetSizeAfterSucessfulGetIsCached(t *testing.T) {
arc.GetSize(exampleBlock.Cid()) 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) { func TestGetSizeMissingZeroSizeBlock(t *testing.T) {
arc, bs, cd := createStores(t) arc, bs, cd := createStores(t)
emptyBlock := blocks.NewBlock([]byte{}) emptyBlock := blocks.NewBlock([]byte{})
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment