Repository: buchgr/bazel-remote
Pull request #251 (2020-04-20 05:26:49)

cache/disk: disk.Cache is easier to read than disk.DiskCache


Patch:
diff --git a/cache/disk/disk.go b/cache/disk/disk.go
index f5ba072..4f67c24 100644
--- a/cache/disk/disk.go
+++ b/cache/disk/disk.go
@@ -44,8 +44,8 @@ func (i *lruItem) Size() int64 {
 	return i.size
 }
 
-// DiskCache is filesystem-based cache, with an optional backend proxy.
-type DiskCache struct {
+// Cache is a filesystem-based LRU cache, with an optional backend proxy.
+type Cache struct {
 	dir   string
 	proxy cache.CacheProxy
 
@@ -62,8 +62,8 @@ const sha256HashStrSize = sha256.Size * 2 // Two hex characters per byte.
 
 // New returns a new instance of a filesystem-based cache rooted at `dir`,
 // with a maximum size of `maxSizeBytes` bytes and an optional backend `proxy`.
-// DiskCache is safe for concurrent use.
-func New(dir string, maxSizeBytes int64, proxy cache.CacheProxy) *DiskCache {
+// Cache is safe for concurrent use.
+func New(dir string, maxSizeBytes int64, proxy cache.CacheProxy) *Cache {
 	// Create the directory structure.
 	hexLetters := []byte("0123456789abcdef")
 	for _, c1 := range hexLetters {
@@ -144,7 +144,7 @@ func New(dir string, maxSizeBytes int64, proxy cache.CacheProxy) *DiskCache {
 		}
 	}
 
-	c := &DiskCache{
+	c := &Cache{
 		dir:   filepath.Clean(dir),
 		proxy: proxy,
 		mu:    &sync.Mutex{},
@@ -164,7 +164,7 @@ func New(dir string, maxSizeBytes int64, proxy cache.CacheProxy) *DiskCache {
 	return c
 }
 
-func (c *DiskCache) migrateDirectories() error {
+func (c *Cache) migrateDirectories() error {
 	err := migrateDirectory(filepath.Join(c.dir, cache.AC.String()))
 	if err != nil {
 		return err
@@ -200,7 +200,7 @@ func migrateDirectory(dir string) error {
 // loadExistingFiles lists all files in the cache directory, and adds them to the
 // LRU index so that they can be served. Files are sorted by access time first,
 // so that the eviction behavior is preserved across server restarts.
-func (c *DiskCache) loadExistingFiles() error {
+func (c *Cache) loadExistingFiles() error {
 	log.Printf("Loading existing files in %s.\n", c.dir)
 
 	// Walk the directory tree
@@ -248,7 +248,7 @@ func (c *DiskCache) loadExistingFiles() error {
 // Put stores a stream of `expectedSize` bytes from `r` into the cache.
 // If `hash` is not the empty string, and the contents don't match it,
 // a non-nil error is returned.
-func (c *DiskCache) Put(kind cache.EntryKind, hash string, expectedSize int64, r io.Reader) error {
+func (c *Cache) Put(kind cache.EntryKind, hash string, expectedSize int64, r io.Reader) error {
 
 	// The hash format is checked properly in the http/grpc code.
 	// Just perform a simple/fast check here, to catch bad tests.
@@ -373,7 +373,7 @@ func (c *DiskCache) Put(kind cache.EntryKind, hash string, expectedSize int64, r
 // be requested from the proxy, in which case, a placeholder entry
 // has been added to the index and the caller must either replace
 // the entry with the actual size, or remove it from the LRU.
-func (c *DiskCache) availableOrTryProxy(key string) (available bool, tryProxy bool) {
+func (c *Cache) availableOrTryProxy(key string) (available bool, tryProxy bool) {
 	inProgress := false
 	tryProxy = false
 
@@ -404,7 +404,7 @@ func (c *DiskCache) availableOrTryProxy(key string) (available bool, tryProxy bo
 // and the number of bytes that can be read from it. If the item is not found, the
 // io.ReadCloser will be nil. If some error occurred when processing the request, then
 // it is returned.
-func (c *DiskCache) Get(kind cache.EntryKind, hash string) (io.ReadCloser, int64, error) {
+func (c *Cache) Get(kind cache.EntryKind, hash string) (io.ReadCloser, int64, error) {
 
 	// The hash format is checked properly in the http/grpc code.
 	// Just perform a simple/fast check here, to catch bad tests.
@@ -529,7 +529,7 @@ func (c *DiskCache) Get(kind cache.EntryKind, hash string) (io.ReadCloser, int64
 //
 // If there is a local cache miss, the proxy backend (if there is
 // one) will be checked.
-func (c *DiskCache) Contains(kind cache.EntryKind, hash string) (bool, int64) {
+func (c *Cache) Contains(kind cache.EntryKind, hash string) (bool, int64) {
 
 	// The hash format is checked properly in the http/grpc code.
 	// Just perform a simple/fast check here, to catch bad tests.
@@ -562,14 +562,14 @@ func (c *DiskCache) Contains(kind cache.EntryKind, hash string) (bool, int64) {
 }
 
 // MaxSize returns the maximum cache size in bytes.
-func (c *DiskCache) MaxSize() int64 {
+func (c *Cache) MaxSize() int64 {
 	// The underlying value is never modified, no need to lock.
 	return c.lru.MaxSize()
 }
 
 // Stats returns the current size of the cache in bytes, and the number of
 // items stored in the cache.
-func (c *DiskCache) Stats() (currentSize int64, numItems int) {
+func (c *Cache) Stats() (currentSize int64, numItems int) {
 	c.mu.Lock()
 	defer c.mu.Unlock()
 
@@ -597,7 +597,7 @@ func cacheFilePath(kind cache.EntryKind, cacheDir string, hash string) string {
 // value from the CAS if it and all its dependencies are also available. If
 // not, nil values are returned. If something unexpected went wrong, return
 // an error.
-func (c *DiskCache) GetValidatedActionResult(hash string) (*pb.ActionResult, []byte, error) {
+func (c *Cache) GetValidatedActionResult(hash string) (*pb.ActionResult, []byte, error) {
 	rdr, sizeBytes, err := c.Get(cache.AC, hash)
 	if err != nil {
 		return nil, nil, err
diff --git a/server/grpc.go b/server/grpc.go
index ce726f1..0b0b137 100644
--- a/server/grpc.go
+++ b/server/grpc.go
@@ -30,7 +30,7 @@ var (
 )
 
 type grpcServer struct {
-	cache        *disk.DiskCache
+	cache        *disk.Cache
 	accessLogger cache.Logger
 	errorLogger  cache.Logger
 	depsCheck    bool
@@ -41,7 +41,7 @@ type grpcServer struct {
 // blocking call to https://godoc.org/google.golang.org/grpc#Server.Serve
 func ListenAndServeGRPC(addr string, opts []grpc.ServerOption,
 	validateACDeps bool,
-	c *disk.DiskCache, a cache.Logger, e cache.Logger) error {
+	c *disk.Cache, a cache.Logger, e cache.Logger) error {
 
 	listener, err := net.Listen("tcp", addr)
 	if err != nil {
@@ -53,7 +53,7 @@ func ListenAndServeGRPC(addr string, opts []grpc.ServerOption,
 
 func serveGRPC(l net.Listener, opts []grpc.ServerOption,
 	validateACDepsCheck bool,
-	c *disk.DiskCache, a cache.Logger, e cache.Logger) error {
+	c *disk.Cache, a cache.Logger, e cache.Logger) error {
 
 	srv := grpc.NewServer(opts...)
 	s := &grpcServer{
diff --git a/server/http.go b/server/http.go
index 2aca461..5d9a821 100644
--- a/server/http.go
+++ b/server/http.go
@@ -29,7 +29,7 @@ type HTTPCache interface {
 }
 
 type httpCache struct {
-	cache        *disk.DiskCache
+	cache        *disk.Cache
 	accessLogger cache.Logger
 	errorLogger  cache.Logger
 	validateAC   bool
@@ -48,7 +48,7 @@ type statusPageData struct {
 // accessLogger will print one line for each HTTP request to stdout.
 // errorLogger will print unexpected server errors. Inexistent files and malformed URLs will not
 // be reported.
-func NewHTTPCache(cache *disk.DiskCache, accessLogger cache.Logger, errorLogger cache.Logger, validateAC bool, commit string) HTTPCache {
+func NewHTTPCache(cache *disk.Cache, accessLogger cache.Logger, errorLogger cache.Logger, validateAC bool, commit string) HTTPCache {
 
 	_, numItems := cache.Stats()
 


Test patch:
diff --git a/cache/disk/disk_test.go b/cache/disk/disk_test.go
index 73022a5..566050a 100644
--- a/cache/disk/disk_test.go
+++ b/cache/disk/disk_test.go
@@ -33,7 +33,7 @@ func tempDir(t *testing.T) string {
 	return dir
 }
 
-func checkItems(cache *DiskCache, expSize int64, expNum int) error {
+func checkItems(cache *Cache, expSize int64, expNum int) error {
 	if cache.lru.Len() != expNum {
 		return fmt.Errorf("expected %d files in the cache, found %d", expNum, cache.lru.Len())
 	}
@@ -203,11 +203,11 @@ func expectContentEquals(rdr io.ReadCloser, sizeBytes int64, expectedContent []b
 	return nil
 }
 
-func putGetCompare(kind cache.EntryKind, hash string, content string, testCache *DiskCache) error {
+func putGetCompare(kind cache.EntryKind, hash string, content string, testCache *Cache) error {
 	return putGetCompareBytes(kind, hash, []byte(content), testCache)
 }
 
-func putGetCompareBytes(kind cache.EntryKind, hash string, data []byte, testCache *DiskCache) error {
+func putGetCompareBytes(kind cache.EntryKind, hash string, data []byte, testCache *Cache) error {
 
 	r := bytes.NewReader(data)
 
diff --git a/server/grpc_test.go b/server/grpc_test.go
index 803000c..0201b44 100644
--- a/server/grpc_test.go
+++ b/server/grpc_test.go
@@ -42,7 +42,7 @@ var (
 	casClient pb.ContentAddressableStorageClient
 	bsClient  bytestream.ByteStreamClient
 	ctx       = context.Background()
-	diskCache *disk.DiskCache
+	diskCache *disk.Cache
 
 	badDigestTestCases = []badDigest{
 		{digest: pb.Digest{Hash: ""}, reason: "empty hash"},