Skip to content

Commit

Permalink
Adressing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ola-rozenfeld committed Feb 1, 2024
1 parent 1046709 commit c22848c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 41 deletions.
6 changes: 3 additions & 3 deletions go/pkg/diskcache/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "diskcache",
srcs = [
"atim_darwin.go",
"atim_linux.go",
"atim_windows.go",
"diskcache.go",
"sys_darwin.go",
"sys_linux.go",
"sys_windows.go",
],
importpath = "github.com/bazelbuild/remote-apis-sdks/go/pkg/diskcache",
visibility = ["//visibility:public"],
Expand Down
77 changes: 42 additions & 35 deletions go/pkg/diskcache/diskcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,43 +115,50 @@ func New(ctx context.Context, root string, maxCapacityBytes uint64) *DiskCache {
_ = os.MkdirAll(root, os.ModePerm)
// We use Git's directory/file naming structure as inspiration:
// https://git-scm.com/book/en/v2/Git-Internals-Git-Objects#:~:text=The%20subdirectory%20is%20named%20with%20the%20first%202%20characters%20of%20the%20SHA%2D1%2C%20and%20the%20filename%20is%20the%20remaining%2038%20characters.
var wg sync.WaitGroup
wg.Add(256)
for i := 0; i < 256; i++ {
_ = os.MkdirAll(filepath.Join(root, fmt.Sprintf("%02x", i)), os.ModePerm)
prefixDir := filepath.Join(root, fmt.Sprintf("%02x", i))
go func() {
defer wg.Done()
_ = os.MkdirAll(prefixDir, os.ModePerm)
_ = filepath.WalkDir(prefixDir, func(path string, d fs.DirEntry, err error) error {
// We log and continue on all errors, because cache read errors are not critical.
if err != nil {
log.Errorf("Error reading cache directory: %v", err)
return nil
}
if d.IsDir() {
return nil
}
subdir := filepath.Base(filepath.Dir(path))
k, err := res.getKeyFromFileName(subdir + d.Name())
if err != nil {
log.Errorf("Error parsing cached file name %s: %v", path, err)
return nil
}
atime, err := GetLastAccessTime(path)
if err != nil {
log.Errorf("Error getting last accessed time of %s: %v", path, err)
return nil
}
it := &qitem{
key: k,
lat: atime,
}
size, err := res.getItemSize(k)
if err != nil {
log.Errorf("Error getting file size of %s: %v", path, err)
return nil
}
res.store.Store(k, it)
atomic.AddInt64(&res.sizeBytes, size)
heap.Push(res.queue, it)
return nil
})
}()
}
_ = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
// We log and continue on all errors, because cache read errors are not critical.
if err != nil {
log.Errorf("Error reading cache directory: %v", err)
return nil
}
if d.IsDir() {
return nil
}
subdir := filepath.Base(filepath.Dir(path))
k, err := res.getKeyFromFileName(subdir + d.Name())
if err != nil {
log.Errorf("Error parsing cached file name %s: %v", path, err)
return nil
}
atime, err := GetLastAccessTime(path)
if err != nil {
log.Errorf("Error getting last accessed time of %s: %v", path, err)
return nil
}
it := &qitem{
key: k,
lat: atime,
}
size, err := res.getItemSize(k)
if err != nil {
log.Errorf("Error getting file size of %s: %v", path, err)
return nil
}
res.store.Store(k, it)
atomic.AddInt64(&res.sizeBytes, size)
heap.Push(res.queue, it)
return nil
})
wg.Wait()
go res.gc()
return res
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Utility to get the last accessed time on Darwin.
// System utilities that differ between OS implementations.
package diskcache

import (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Utility to get the last accessed time on Linux.
// System utilities that differ between OS implementations.
package diskcache

import (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Utility to get the last accessed time on Windows.
// System utilities that differ between OS implementations.
package diskcache

import (
Expand Down

0 comments on commit c22848c

Please sign in to comment.