forked from kopia/kopia
-
Notifications
You must be signed in to change notification settings - Fork 0
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
gcs immu #10
Closed
Closed
gcs immu #10
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
807cf9d
gcs immu
KastenMike 8f07556
no sorting
KastenMike 90ca12e
remove npe
KastenMike a9ed3cf
tweaks
KastenMike 49ffdb2
refactor + toBlobID
KastenMike 4402e37
dont break interface
KastenMike 342e7e3
locked
KastenMike 4b2579a
logs in english
KastenMike f5e0d99
more logs
KastenMike 8b9ca0e
Merge branch 'master' of ssh://github.com/KastenMike/kopia into gcs-immu
KastenMike 5c53ac9
remove printlns
KastenMike 4f9cbcf
lint
KastenMike 383cf50
remove more logs
KastenMike File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package gcs | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/kopia/kopia/repo/blob" | ||
"github.com/kopia/kopia/repo/blob/readonly" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type gcsPointInTimeStorage struct { | ||
gcsStorage | ||
|
||
pointInTime time.Time | ||
} | ||
|
||
func (gcs *gcsPointInTimeStorage) ListBlobs(ctx context.Context, blobIDPrefix blob.ID, cb func(bm blob.Metadata) error) error { | ||
var ( | ||
previousID blob.ID | ||
vs []versionMetadata | ||
) | ||
err := gcs.listBlobVersions(ctx, blobIDPrefix, func(vm versionMetadata) error { | ||
if vm.BlobID != previousID { | ||
// different blob, process previous one | ||
if v, found := newestAtUnlessDeleted(vs, gcs.pointInTime); found { | ||
if err := cb(v.Metadata); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
previousID = vm.BlobID | ||
vs = vs[:0] // reset for next blob | ||
} | ||
|
||
vs = append(vs, vm) | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
return errors.Wrapf(err, "could not list blob versions at time %s", gcs.pointInTime) | ||
} | ||
|
||
// process last blob | ||
if v, found := newestAtUnlessDeleted(vs, gcs.pointInTime); found { | ||
if err := cb(v.Metadata); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (gcs *gcsPointInTimeStorage) GetBlob(ctx context.Context, b blob.ID, offset, length int64, output blob.OutputBuffer) error { | ||
// getMetadata returns the specific blob version at time t | ||
m, err := gcs.getMetadata(ctx, b) | ||
if err != nil { | ||
return errors.Wrap(err, "getting metadata") | ||
} | ||
|
||
return gcs.getBlobWithVersion(ctx, b, m.Version, offset, length, output) | ||
} | ||
|
||
func (gcs *gcsPointInTimeStorage) GetMetadata(ctx context.Context, b blob.ID) (blob.Metadata, error) { | ||
bm, err := gcs.getMetadata(ctx, b) | ||
|
||
return bm.Metadata, err | ||
} | ||
|
||
func (gcs *gcsPointInTimeStorage) getMetadata(ctx context.Context, b blob.ID) (versionMetadata, error) { | ||
var vml []versionMetadata | ||
|
||
if err := gcs.getBlobVersions(ctx, b, func(m versionMetadata) error { | ||
// only include versions older than s.pointInTime | ||
if !m.Timestamp.After(gcs.pointInTime) { | ||
vml = append(vml, m) | ||
} | ||
|
||
return nil | ||
}); err != nil { | ||
return versionMetadata{}, errors.Wrapf(err, "could not get version metadata for blob %s", b) | ||
} | ||
|
||
if v, found := newestAtUnlessDeleted(vml, gcs.pointInTime); found { | ||
return v, nil | ||
} | ||
|
||
return versionMetadata{}, blob.ErrBlobNotFound | ||
} | ||
|
||
// newestAtUnlessDeleted returns the last version in the list older than the PIT. | ||
func newestAtUnlessDeleted(vx []versionMetadata, t time.Time) (v versionMetadata, found bool) { | ||
vs := getOlderThan(vx, t) | ||
|
||
if len(vs) == 0 { | ||
return versionMetadata{}, false | ||
} | ||
|
||
v = vs[len(vs)-1] | ||
|
||
return v, !v.IsDeleteMarker | ||
} | ||
|
||
// Removes versions that are newer than t. The filtering is done in place | ||
// and uses the same slice storage as vs. Assumes entries in vs are in descending | ||
// timestamp order. | ||
func getOlderThan(vs []versionMetadata, t time.Time) []versionMetadata { | ||
for i := range vs { | ||
if vs[i].Timestamp.After(t) { | ||
return vs[:i] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
} | ||
|
||
return vs | ||
} | ||
|
||
// maybePointInTimeStore wraps s with a point-in-time store when s is versioned | ||
// and a point-in-time value is specified. Otherwise s is returned. | ||
func maybePointInTimeStore(ctx context.Context, gcs *gcsStorage, pointInTime *time.Time) (blob.Storage, error) { | ||
if pit := gcs.Options.PointInTime; pit == nil || pit.IsZero() { | ||
return gcs, nil | ||
} | ||
|
||
// Does the bucket supports versioning? | ||
attrs, err := gcs.bucket.Attrs(ctx) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "could not get determine if bucket '%s' supports versioning", gcs.BucketName) | ||
} | ||
|
||
if !attrs.VersioningEnabled { | ||
return nil, errors.Errorf("cannot create point-in-time view for non-versioned bucket '%s'", gcs.BucketName) | ||
} | ||
|
||
return readonly.NewWrapper(&gcsPointInTimeStorage{ | ||
gcsStorage: *gcs, | ||
pointInTime: *pointInTime, | ||
}), nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kreare