Smarter caches #46

Merged
lyoshenka merged 13 commits from smarter_caches into master 2020-11-04 22:04:22 +01:00
2 changed files with 26 additions and 1 deletions
Showing only changes of commit 7a3225434e - Show all commits

View file

@ -117,8 +117,13 @@ func (d *DiskStore) Delete(hash string) error {
return errors.Err(err)
}
// list returns a slice of blobs that already exist in the blobDir
// list returns the hashes of blobs that already exist in the blobDir
func (d *DiskStore) list() ([]string, error) {
err := d.initOnce()
if err != nil {
return nil, err
}
dirs, err := afero.ReadDir(d.fs, d.blobDir)
if err != nil {
return nil, err

View file

@ -119,3 +119,23 @@ func TestLRUStore_UnderlyingBlobMissing(t *testing.T) {
// lru.Get() removes hash if underlying store doesn't have it
assert.False(t, lru.lru.Contains(hash))
}
func TestLRUStore_loadExisting(t *testing.T) {
d := NewDiskStore("/", 2)
d.fs = afero.NewMemMapFs()
hash := "hash"
b := []byte("this is a blob of stuff")
err := d.Put(hash, b)
require.NoError(t, err)
existing, err := d.list()
require.NoError(t, err)
require.Equal(t, 1, len(existing), "blob should exist in cache")
assert.Equal(t, hash, existing[0])
lru := NewLRUStore(d, 3) // lru should load existing blobs when it's created
has, err := lru.Has(hash)
require.NoError(t, err)
assert.True(t, has, "hash should be loaded from disk store but it's not")
}