Skip to content

Commit

Permalink
Adding Delete and Reset as public Cache methods (bazelbuild#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ola Rozenfeld authored Mar 25, 2020
1 parent d3ce9bb commit 24a03e3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions go/pkg/filemetadata/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ func (c *fmCache) Delete(filename string) error {
return c.Backend.Delete(namespace, abs)
}

// Reset clears the cache.
func (c *fmCache) Reset() {
c.Backend.Reset()
}

func (c *fmCache) check() error {
if c.Backend == nil {
return fmt.Errorf("no backend found for store")
Expand Down
10 changes: 10 additions & 0 deletions go/pkg/filemetadata/filemetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ func Compute(filename string) *Metadata {
// Cache is a cache for file contents->Metadata.
type Cache interface {
Get(path string) *Metadata
Delete(filename string) error
Reset()
}

type noopCache struct{}
Expand All @@ -66,6 +68,14 @@ func (c *noopCache) Get(path string) *Metadata {
return Compute(path)
}

// Delete removes an entry from the cache. It is a noop for the Noop cache.
func (c *noopCache) Delete(string) error {
return nil
}

// Reset clears the cache. It is a noop for the Noop cache.
func (c *noopCache) Reset() {}

// NewNoopCache returns a cache that doesn't cache (evaluates on every Get).
func NewNoopCache() Cache {
return &noopCache{}
Expand Down
15 changes: 15 additions & 0 deletions go/pkg/tree/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,21 @@ func (c *callCountingMetadataCache) Get(path string) *filemetadata.Metadata {
return c.cache.Get(path)
}

func (c *callCountingMetadataCache) Delete(path string) error {
c.t.Helper()
p, err := filepath.Rel(c.execRoot, path)
if err != nil {
c.t.Errorf("expected %v to be under %v", path, c.execRoot)
}
c.calls[p]++
return c.cache.Delete(path)
}

func (c *callCountingMetadataCache) Reset() {
c.t.Helper()
c.cache.Reset()
}

func TestComputeMerkleTreeEmptySubdirs(t *testing.T) {
fileBlob := []byte("bla")
fileDg := digest.NewFromBlob(fileBlob)
Expand Down

0 comments on commit 24a03e3

Please sign in to comment.