Skip to content

Commit

Permalink
Stat cache bucket changes (#2138)
Browse files Browse the repository at this point in the history
* adds get folder implementation for fast stat cache

* adds unit test for fast stat cache get folder implementation
  • Loading branch information
ankitaluthra1 authored Jul 10, 2024
1 parent 664be83 commit ad33dcd
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 10 deletions.
32 changes: 23 additions & 9 deletions internal/storage/caching/fast_stat_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ func (b *fastStatBucket) lookUp(name string) (hit bool, m *gcs.MinObject) {
return
}

func (b *fastStatBucket) lookUpFolder(name string) (bool, *gcs.Folder) {
b.mu.Lock()
defer b.mu.Unlock()

hit, f := b.cache.LookUpFolder(name, b.clock.Now())
return hit, f
}

////////////////////////////////////////////////////////////////////////
// Bucket interface
////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -298,17 +306,23 @@ func (b *fastStatBucket) StatObjectFromGcs(ctx context.Context,

func (b *fastStatBucket) GetFolder(
ctx context.Context,
prefix string) (folder *gcs.Folder, err error) {
// Fetch the listing.
folder, err = b.wrapped.GetFolder(ctx, prefix)
if err != nil {
return
}
prefix string) (*gcs.Folder, error) {

// TODO: add folder metadata in stat cache
// b.insertFolder(folder)
if hit, entry := b.lookUpFolder(prefix); hit {
// Negative entries result in NotFoundError.
if entry == nil {
err := &gcs.NotFoundError{
Err: fmt.Errorf("negative cache entry for folder %v", prefix),
}

return
return nil, err
}

return entry, nil
}

// Fetch the Folder
return b.wrapped.GetFolder(ctx, prefix)
}

func (b *fastStatBucket) RenameFolder(ctx context.Context, folderName string, destinationFolderId string) (o *control.RenameFolderOperation, err error) {
Expand Down
43 changes: 43 additions & 0 deletions internal/storage/caching/fast_stat_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,3 +725,46 @@ func (t *DeleteObjectTest) WrappedSucceeds() {
err = t.deleteObject(name)
AssertEq(nil, err)
}

func (t *StatObjectTest) TestShouldReturnFromCacheWhenEntryIsPresent() {
const name = "some-name"
folder := &gcs.Folder{
Name: name,
}
ExpectCall(t.cache, "LookUpFolder")(name, Any()).
WillOnce(Return(true, folder))

result, err := t.bucket.GetFolder(context.TODO(), name)

AssertEq(nil, err)
ExpectThat(result, Pointee(DeepEquals(*folder)))
}

func (t *StatObjectTest) TestShouldReturnNotFoundErrorWhenNilEntryIsReturned() {
const name = "some-name"

ExpectCall(t.cache, "LookUpFolder")(name, Any()).
WillOnce(Return(true, nil))

result, err := t.bucket.GetFolder(context.TODO(), name)

ExpectThat(err, HasSameTypeAs(&gcs.NotFoundError{}))
AssertEq(nil, result)
}

func (t *StatObjectTest) TestShouldCallGetFolderWhenEntryIsNotPresent() {
const name = "some-name"
folder := &gcs.Folder{
Name: name,
}

ExpectCall(t.cache, "LookUpFolder")(name, Any()).
WillOnce(Return(false, nil))
ExpectCall(t.wrapped, "GetFolder")(Any(), name).
WillOnce(Return(folder, nil))

result, err := t.bucket.GetFolder(context.TODO(), name)

AssertEq(nil, err)
ExpectThat(result, Pointee(DeepEquals(*folder)))
}
2 changes: 1 addition & 1 deletion internal/storage/mock_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func (m *mockBucket) GetFolder(
"GetFolder",
file,
line,
[]interface{}{})
[]interface{}{ctx, prefix})

if len(retVals) != 2 {
panic(fmt.Sprintf("mockBucket.GetFolder: invalid return values: %v", retVals))
Expand Down

0 comments on commit ad33dcd

Please sign in to comment.