Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(storage): add ComponentCount as part of ObjectAttrs #7230

Merged
merged 9 commits into from
Jan 11, 2023
6 changes: 6 additions & 0 deletions storage/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,9 @@ func TestIntegration_Compose(t *testing.T) {
if _, err := c.Run(ctx); err != nil {
t.Fatalf("ComposeFrom error: %v", err)
}
if c.ComponentCount != int64(len(objects)) {
t.Fatalf("ComponentCount of dst Object: %v is incorrect", c.ComponentCount)
Tulsishah marked this conversation as resolved.
Show resolved Hide resolved
}
checkCompose(compDst, "application/octet-stream")

// It should also work if we do.
Expand All @@ -1671,6 +1674,9 @@ func TestIntegration_Compose(t *testing.T) {
if _, err := c.Run(ctx); err != nil {
t.Fatalf("ComposeFrom error: %v", err)
}
if c.ComponentCount != int64(len(objects)) {
t.Fatalf("ComponentCount of dst Object: %v is incorrect", c.ComponentCount)
}
checkCompose(compDst, "text/json")
})
}
Expand Down
23 changes: 23 additions & 0 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,27 @@ type ObjectAttrs struct {
// later value but not to an earlier one. For more information see
// https://cloud.google.com/storage/docs/metadata#custom-time .
CustomTime time.Time

// As of 2015-06-03, the official GCS documentation for this
Tulsishah marked this conversation as resolved.
Show resolved Hide resolved
// property (https://goo.gl/GwD5Dq) says this:
//
// Newly uploaded objects have a component count of 1, and composing a
// sequence of objects creates an object whose component count is equal
// to the sum of component counts in the sequence.
//
// However, in Google-internal bug 21572928 it was clarified that this
// doesn't match the actual implementation, which can be documented as:
//
// Newly uploaded objects do not have a component count. Composing a
// sequence of objects creates an object whose component count is equal
// to the sum of the component counts of the objects in the sequence,
// where objects that do not have a component count are treated as having
// a component count of 1.
//
// This is a much less elegant and convenient rule, so this package emulates
// the officially documented behavior above. That is, it synthesizes a
// component count of 1 for objects that do not have a component count.
ComponentCount int64
}

// convertTime converts a time in RFC3339 format to time.Time.
Expand Down Expand Up @@ -1385,6 +1406,7 @@ func newObject(o *raw.Object) *ObjectAttrs {
Updated: convertTime(o.Updated),
Etag: o.Etag,
CustomTime: convertTime(o.CustomTime),
ComponentCount: o.ComponentCount,
}
}

Expand Down Expand Up @@ -1419,6 +1441,7 @@ func newObjectFromProto(o *storagepb.Object) *ObjectAttrs {
Deleted: convertProtoTime(o.GetDeleteTime()),
Updated: convertProtoTime(o.GetUpdateTime()),
CustomTime: convertProtoTime(o.GetCustomTime()),
ComponentCount: int64(o.ComponentCount),
}
}

Expand Down
4 changes: 4 additions & 0 deletions storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1747,6 +1747,7 @@ func TestRawObjectToObjectAttrs(t *testing.T) {
TimeCreated: "2019-03-31T19:32:10Z",
TimeDeleted: "2019-03-31T19:33:39Z",
TemporaryHold: true,
ComponentCount: 2,
},
want: &ObjectAttrs{
Bucket: "Test",
Expand All @@ -1763,6 +1764,7 @@ func TestRawObjectToObjectAttrs(t *testing.T) {
RetentionExpirationTime: time.Date(2019, 3, 31, 19, 33, 36, 0, time.UTC),
Size: 1 << 20,
TemporaryHold: true,
ComponentCount: 2,
},
},
}
Expand Down Expand Up @@ -1833,6 +1835,7 @@ func TestProtoObjectToObjectAttrs(t *testing.T) {
CreateTime: timestamppb.New(now),
DeleteTime: timestamppb.New(now),
TemporaryHold: true,
ComponentCount: 2,
},
want: &ObjectAttrs{
Bucket: "Test",
Expand All @@ -1848,6 +1851,7 @@ func TestProtoObjectToObjectAttrs(t *testing.T) {
RetentionExpirationTime: now,
Size: 1 << 20,
TemporaryHold: true,
ComponentCount: 2,
},
},
}
Expand Down