storj/satellite/metainfo/expireddeletion/expireddeleter.go
Michal Niewrzal aa47e70f03 satellite/metainfo: use metabase.SegmentKey with metainfo.Service
Instead of using string or []byte we will be using dedicated type
SegmentKey.

Change-Id: I6ca8039f0741f6f9837c69a6d070228ed10f2220
2020-09-03 15:11:32 +00:00

66 lines
1.8 KiB
Go

// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package expireddeletion
import (
"context"
"time"
"go.uber.org/zap"
"storj.io/common/pb"
"storj.io/common/storj"
"storj.io/storj/satellite/metainfo"
"storj.io/storj/storage"
)
var _ metainfo.Observer = (*expiredDeleter)(nil)
// expiredDeleter implements the metainfo loop observer interface for expired segment cleanup
//
// architecture: Observer
type expiredDeleter struct {
log *zap.Logger
metainfo *metainfo.Service
}
// RemoteSegment deletes the segment if it is expired.
func (ed *expiredDeleter) RemoteSegment(ctx context.Context, path metainfo.ScopedPath, pointer *pb.Pointer) (err error) {
defer mon.Task()(&ctx, path.Raw)(&err)
return ed.deleteSegmentIfExpired(ctx, path, pointer)
}
// InlineSegment deletes the segment if it is expired.
func (ed *expiredDeleter) InlineSegment(ctx context.Context, path metainfo.ScopedPath, pointer *pb.Pointer) (err error) {
defer mon.Task()(&ctx, path.Raw)(&err)
return ed.deleteSegmentIfExpired(ctx, path, pointer)
}
// Object returns nil because the expired deleter only cares about segments.
func (ed *expiredDeleter) Object(ctx context.Context, path metainfo.ScopedPath, pointer *pb.Pointer) (err error) {
return nil
}
func (ed *expiredDeleter) deleteSegmentIfExpired(ctx context.Context, path metainfo.ScopedPath, pointer *pb.Pointer) error {
// delete segment if expired
if !pointer.ExpirationDate.IsZero() && pointer.ExpirationDate.Before(time.Now().UTC()) {
pointerBytes, err := pb.Marshal(pointer)
if err != nil {
return err
}
err = ed.metainfo.Delete(ctx, path.Encode(), pointerBytes)
if storj.ErrObjectNotFound.Has(err) {
// segment already deleted
return nil
} else if storage.ErrValueChanged.Has(err) {
// segment was replaced
return nil
}
return err
}
return nil
}