Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions internal/storage/fake/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"path/filepath"
"sort"
"strings"
"time"
"unicode/utf8"

"github.com/googlecloudplatform/gcsfuse/v3/internal/storage/caching"
Expand Down Expand Up @@ -272,7 +271,7 @@ func (b *bucket) mintObject(
func (b *bucket) mintFolder(folderName string) (f gcs.Folder) {
f = gcs.Folder{
Name: folderName,
UpdateTime: b.clock.Now(),
UpdateTime: b.clock.Now().UnixNano(),
}

return
Expand Down Expand Up @@ -1212,6 +1211,9 @@ func (b *bucket) CreateFolder(ctx context.Context, folderName string) (*gcs.Fold
}

func (b *bucket) RenameFolder(ctx context.Context, folderName string, destinationFolderId string) (*gcs.Folder, error) {
b.mu.Lock()
defer b.mu.Unlock()

// Check that the destination name is legal.
err := checkName(destinationFolderId)
if err != nil {
Expand All @@ -1231,7 +1233,7 @@ func (b *bucket) RenameFolder(ctx context.Context, folderName string, destinatio
for i := range b.folders {
if strings.HasPrefix(b.folders[i].Name, folderName) {
b.folders[i].Name = strings.Replace(b.folders[i].Name, folderName, destinationFolderId, 1)
b.folders[i].UpdateTime = time.Now()
b.folders[i].UpdateTime = b.clock.Now().UnixNano()
}
}

Expand All @@ -1242,7 +1244,7 @@ func (b *bucket) RenameFolder(ctx context.Context, folderName string, destinatio
for i := range b.objects {
if strings.HasPrefix(b.objects[i].metadata.Name, folderName) {
b.objects[i].metadata.Name = strings.Replace(b.objects[i].metadata.Name, folderName, destinationFolderId, 1)
b.objects[i].metadata.Updated = time.Now()
b.objects[i].metadata.Updated = b.clock.Now()
}
}

Expand All @@ -1252,7 +1254,7 @@ func (b *bucket) RenameFolder(ctx context.Context, folderName string, destinatio
// Return the updated folder.
folder := &gcs.Folder{
Name: destinationFolderId,
UpdateTime: time.Now(),
UpdateTime: b.clock.Now().UnixNano(),
}

return folder, nil
Expand Down
10 changes: 7 additions & 3 deletions internal/storage/gcs/folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,25 @@ package gcs

import (
"strings"
"time"

"cloud.google.com/go/storage/control/apiv2/controlpb"
)

type Folder struct {
Name string
UpdateTime time.Time
UpdateTime int64
}

func GCSFolder(bucketName string, attrs *controlpb.Folder) *Folder {
// Setting the parameters in Folder and doing conversions as necessary.
var updateTime int64
if ts := attrs.GetUpdateTime(); ts != nil {
updateTime = ts.GetSeconds()*1e9 + int64(ts.GetNanos())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using 1e9 (which is an untyped float constant) in integer arithmetic can be less clear and potentially confusing. It is more idiomatic in Go to use an untyped integer constant like 1_000_000_000 or 1000000000 for nanosecond conversions.

Suggested change
updateTime = ts.GetSeconds()*1e9 + int64(ts.GetNanos())
updateTime = ts.GetSeconds()*1000000000 + int64(ts.GetNanos())

}

return &Folder{
Name: getFolderName(bucketName, attrs.Name),
UpdateTime: attrs.GetUpdateTime().AsTime(),
UpdateTime: updateTime,
}
}

Expand Down
34 changes: 29 additions & 5 deletions internal/storage/gcs/folder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package gcs

import (
"testing"
"time"

"cloud.google.com/go/storage/control/apiv2/controlpb"
"github.com/stretchr/testify/assert"
Expand All @@ -36,8 +35,8 @@ func TestGetFolderName(t *testing.T) {

func TestGCSFolder(t *testing.T) {
timestamp := &timestamppb.Timestamp{
Seconds: time.Now().Unix(), // Number of seconds since Unix epoch (1970-01-01T00:00:00Z)
Nanos: int32(time.Now().Nanosecond()), // Nanoseconds (0 to 999,999,999)
Seconds: 123456789,
Nanos: 987654321,
}
attrs := controlpb.Folder{
Name: TestFolderName,
Expand All @@ -47,6 +46,31 @@ func TestGCSFolder(t *testing.T) {

gcsFolder := GCSFolder(TestBucketName, &attrs)

assert.Equal(t, attrs.Name, gcsFolder.Name)
assert.Equal(t, attrs.UpdateTime.AsTime(), gcsFolder.UpdateTime)
assert.Equal(t, TestFolderName, gcsFolder.Name)
assert.Equal(t, int64(123456789987654321), gcsFolder.UpdateTime)
}

func TestGCSFolder_UninitializedTime(t *testing.T) {
attrs := controlpb.Folder{
Name: TestFolderName,
Metageneration: 10,
}

gcsFolder := GCSFolder(TestBucketName, &attrs)

assert.EqualValues(t, int64(0), gcsFolder.UpdateTime)
}

func BenchmarkGCSFolder(b *testing.B) {
timestamp := &timestamppb.Timestamp{
Seconds: 1234567890,
}
attrs := controlpb.Folder{
Name: "projects/_/buckets/testBucket/folders/testFolder",
UpdateTime: timestamp,
}

for b.Loop() {
_ = GCSFolder("testBucket", &attrs)
}
Comment thread
kislaykishore marked this conversation as resolved.
}
Loading