retention and replace
This commit is contained in:
+72
-5
@@ -107,21 +107,47 @@ func (s *Service) CreateVersion(ctx context.Context, actorID, documentID ident.U
|
||||
blob, err := s.db.Queries.GetBlob(ctx, staged.Root)
|
||||
newBlob := false
|
||||
newLocation := false
|
||||
healBlob := false
|
||||
blobLocator := ""
|
||||
|
||||
switch {
|
||||
case err == nil:
|
||||
if blob.State != BlobStatePresent {
|
||||
_ = s.store.Remove(ctx, staged.Locator)
|
||||
_ = s.setStagingState(ctx, stagingID, StagingStateDiscarded)
|
||||
return nil, fmt.Errorf("%w: %x", ErrBlobUnavailable, staged.Root)
|
||||
}
|
||||
if blob.Size != staged.Size {
|
||||
_ = s.store.Remove(ctx, staged.Locator)
|
||||
_ = s.setStagingState(ctx, stagingID, StagingStateDiscarded)
|
||||
return nil, fmt.Errorf("%w: taille incohérente", ErrBlobUnavailable)
|
||||
}
|
||||
|
||||
if blob.State != BlobStatePresent {
|
||||
_, locationErr := s.db.Queries.GetBlobLocation(ctx, database.GetBlobLocationParams{
|
||||
Root: staged.Root,
|
||||
BackendID: s.backendID,
|
||||
})
|
||||
switch {
|
||||
case locationErr == nil:
|
||||
blobLocator, err = s.store.Replace(ctx, staged)
|
||||
if err != nil {
|
||||
_ = s.store.Remove(ctx, staged.Locator)
|
||||
_ = s.setStagingState(ctx, stagingID, StagingStateDiscarded)
|
||||
return nil, err
|
||||
}
|
||||
case errors.Is(locationErr, sql.ErrNoRows):
|
||||
blobLocator, err = s.store.Replace(ctx, staged)
|
||||
if err != nil {
|
||||
_ = s.store.Remove(ctx, staged.Locator)
|
||||
_ = s.setStagingState(ctx, stagingID, StagingStateDiscarded)
|
||||
return nil, err
|
||||
}
|
||||
newLocation = true
|
||||
default:
|
||||
_ = s.store.Remove(ctx, staged.Locator)
|
||||
_ = s.setStagingState(ctx, stagingID, StagingStateDiscarded)
|
||||
return nil, locationErr
|
||||
}
|
||||
healBlob = true
|
||||
break
|
||||
}
|
||||
|
||||
location, locationErr := s.db.Queries.GetBlobLocation(ctx, database.GetBlobLocationParams{
|
||||
Root: staged.Root,
|
||||
BackendID: s.backendID,
|
||||
@@ -193,6 +219,47 @@ func (s *Service) CreateVersion(ctx context.Context, actorID, documentID ident.U
|
||||
return err
|
||||
}
|
||||
}
|
||||
if healBlob {
|
||||
if err := q.UpdateBlobVerification(ctx, database.UpdateBlobVerificationParams{
|
||||
State: BlobStatePresent,
|
||||
LastVerifiedAt: sql.NullInt64{Int64: now, Valid: true},
|
||||
VerifyError: sql.NullString{},
|
||||
Root: staged.Root,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if !newLocation {
|
||||
if err := q.UpdateBlobLocationState(ctx, database.UpdateBlobLocationStateParams{
|
||||
State: BlobLocationPresent,
|
||||
Root: staged.Root,
|
||||
BackendID: s.backendID,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !newBlob {
|
||||
existing, err := q.GetBlob(ctx, staged.Root)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %x", ErrBlobUnavailable, staged.Root)
|
||||
}
|
||||
if existing.State != BlobStatePresent || existing.Size != staged.Size {
|
||||
return fmt.Errorf("%w: %x", ErrBlobUnavailable, staged.Root)
|
||||
}
|
||||
}
|
||||
if !newLocation {
|
||||
existingLocation, err := q.GetBlobLocation(ctx, database.GetBlobLocationParams{
|
||||
Root: staged.Root,
|
||||
BackendID: s.backendID,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %x", ErrBlobUnavailable, staged.Root)
|
||||
}
|
||||
if existingLocation.State != BlobLocationPresent {
|
||||
return fmt.Errorf("%w: %x", ErrBlobUnavailable, staged.Root)
|
||||
}
|
||||
}
|
||||
|
||||
seq, err := q.NextDocumentVersionSeq(ctx, documentID)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user