aa47e70f03
Instead of using string or []byte we will be using dedicated type SegmentKey. Change-Id: I6ca8039f0741f6f9837c69a6d070228ed10f2220
90 lines
2.3 KiB
Go
90 lines
2.3 KiB
Go
// Copyright (C) 2020 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package objectdeletion
|
|
|
|
import (
|
|
"context"
|
|
|
|
"storj.io/common/pb"
|
|
"storj.io/storj/satellite/metainfo/metabase"
|
|
)
|
|
|
|
// ObjectState determines how an object should be handled during
|
|
// a delete operation.
|
|
// It also stores pointers related to an object.
|
|
type ObjectState struct {
|
|
ObjectIdentifier
|
|
|
|
LastSegment *pb.Pointer
|
|
ZeroSegment *pb.Pointer
|
|
|
|
OtherSegments []*pb.Pointer
|
|
}
|
|
|
|
// Status returns the current object status.
|
|
func (state *ObjectState) Status() ObjectStatus {
|
|
switch {
|
|
case state.LastSegment == nil && state.ZeroSegment == nil:
|
|
return ObjectMissing
|
|
case state.LastSegment != nil && state.ZeroSegment == nil:
|
|
return ObjectSingleSegment
|
|
case state.LastSegment != nil && state.ZeroSegment != nil:
|
|
return ObjectMultiSegment
|
|
case state.LastSegment == nil && state.ZeroSegment != nil:
|
|
return ObjectActiveOrZombie
|
|
default:
|
|
return ObjectMissing
|
|
}
|
|
}
|
|
|
|
// ObjectStatus is used to determine how to retrieve segment information
|
|
// from the database.
|
|
type ObjectStatus byte
|
|
|
|
const (
|
|
// ObjectMissing represents when there's no object with that particular name.
|
|
ObjectMissing = ObjectStatus(iota)
|
|
// ObjectMultiSegment represents a multi segment object.
|
|
ObjectMultiSegment
|
|
// ObjectSingleSegment represents a single segment object.
|
|
ObjectSingleSegment
|
|
// ObjectActiveOrZombie represents either an object is being uploaded, deleted or it's a zombie.
|
|
ObjectActiveOrZombie
|
|
)
|
|
|
|
// CreateObjectStates creates the current object states.
|
|
func CreateObjectStates(ctx context.Context, requests []*ObjectIdentifier, pointers []*pb.Pointer, paths []metabase.SegmentKey) (map[string]*ObjectState, error) {
|
|
|
|
// Fetch headers to figure out the status of objects.
|
|
objects := make(map[string]*ObjectState)
|
|
for _, req := range requests {
|
|
objects[req.Key()] = &ObjectState{
|
|
ObjectIdentifier: *req,
|
|
}
|
|
}
|
|
|
|
for i, p := range paths {
|
|
// Update our state map.
|
|
id, segment, err := ParseSegmentPath(p)
|
|
if err != nil {
|
|
return nil, Error.Wrap(err)
|
|
}
|
|
|
|
state, ok := objects[id.Key()]
|
|
if !ok {
|
|
return nil, Error.Wrap(err)
|
|
}
|
|
|
|
switch segment {
|
|
case lastSegmentIndex:
|
|
state.LastSegment = pointers[i]
|
|
case firstSegmentIndex:
|
|
state.ZeroSegment = pointers[i]
|
|
default:
|
|
return nil, Error.New("pointerDB failure")
|
|
}
|
|
}
|
|
return objects, nil
|
|
}
|