2019-01-18 15:10:21 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-04-25 09:46:32 +01:00
|
|
|
package metainfo
|
2019-01-18 15:10:21 +00:00
|
|
|
|
|
|
|
import (
|
2019-06-04 12:55:38 +01:00
|
|
|
"context"
|
2019-07-08 23:16:50 +01:00
|
|
|
"time"
|
2019-06-04 12:55:38 +01:00
|
|
|
|
2019-01-18 15:10:21 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2019-07-08 23:32:18 +01:00
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
2019-01-18 15:10:21 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2019-07-12 13:57:02 +01:00
|
|
|
"storj.io/storj/pkg/macaroon"
|
2019-01-18 15:10:21 +00:00
|
|
|
"storj.io/storj/pkg/pb"
|
2019-07-08 23:32:18 +01:00
|
|
|
"storj.io/storj/pkg/storj"
|
2019-01-18 15:10:21 +00:00
|
|
|
"storj.io/storj/storage"
|
2019-07-28 06:55:36 +01:00
|
|
|
"storj.io/storj/uplink/storage/meta"
|
2019-01-18 15:10:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Service structure
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Service
|
2019-01-18 15:10:21 +00:00
|
|
|
type Service struct {
|
2019-07-08 23:32:18 +01:00
|
|
|
logger *zap.Logger
|
2019-09-10 14:24:16 +01:00
|
|
|
DB PointerDB
|
2019-07-08 23:32:18 +01:00
|
|
|
bucketsDB BucketsDB
|
2019-01-18 15:10:21 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 09:46:32 +01:00
|
|
|
// NewService creates new metainfo service
|
2019-09-10 14:24:16 +01:00
|
|
|
func NewService(logger *zap.Logger, db PointerDB, bucketsDB BucketsDB) *Service {
|
2019-07-08 23:32:18 +01:00
|
|
|
return &Service{logger: logger, DB: db, bucketsDB: bucketsDB}
|
2019-01-18 15:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put puts pointer to db under specific path
|
2019-06-05 15:23:10 +01:00
|
|
|
func (s *Service) Put(ctx context.Context, path string, pointer *pb.Pointer) (err error) {
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-01-18 15:10:21 +00:00
|
|
|
// Update the pointer with the creation date
|
2019-07-08 23:16:50 +01:00
|
|
|
pointer.CreationDate = time.Now()
|
2019-01-18 15:10:21 +00:00
|
|
|
|
|
|
|
pointerBytes, err := proto.Marshal(pointer)
|
|
|
|
if err != nil {
|
2019-07-23 15:28:06 +01:00
|
|
|
return Error.Wrap(err)
|
2019-01-18 15:10:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 17:59:46 +01:00
|
|
|
// CompareAndSwap is used instead of Put to avoid overwriting existing pointers
|
|
|
|
err = s.DB.CompareAndSwap(ctx, []byte(path), nil, pointerBytes)
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdatePieces atomically adds toAdd pieces and removes toRemove pieces from
|
|
|
|
// the pointer under path. ref is the pointer that caller received via Get
|
|
|
|
// prior to calling this method.
|
|
|
|
//
|
|
|
|
// It will first check if the pointer has been deleted or replaced. Then it
|
|
|
|
// will remove the toRemove pieces and then it will add the toAdd pieces.
|
|
|
|
// Replacing the node ID and the hash of a piece can be done by adding the
|
|
|
|
// piece to both toAdd and toRemove.
|
|
|
|
func (s *Service) UpdatePieces(ctx context.Context, path string, ref *pb.Pointer, toAdd, toRemove []*pb.RemotePiece) (pointer *pb.Pointer, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
for {
|
|
|
|
// read the pointer
|
|
|
|
oldPointerBytes, err := s.DB.Get(ctx, []byte(path))
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshal the pointer
|
|
|
|
pointer = &pb.Pointer{}
|
|
|
|
err = proto.Unmarshal(oldPointerBytes, pointer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if pointer has been replaced
|
|
|
|
if !pointer.GetCreationDate().Equal(ref.GetCreationDate()) {
|
|
|
|
return nil, Error.New("pointer has been replaced")
|
|
|
|
}
|
|
|
|
|
|
|
|
// put all existing pieces to a map
|
|
|
|
pieceMap := make(map[int32]*pb.RemotePiece)
|
|
|
|
for _, piece := range pointer.GetRemote().GetRemotePieces() {
|
|
|
|
pieceMap[piece.PieceNum] = piece
|
|
|
|
}
|
2019-01-18 15:10:21 +00:00
|
|
|
|
2019-07-25 17:59:46 +01:00
|
|
|
// remove the toRemove pieces from the map
|
|
|
|
// only if all piece number, node id and hash match
|
|
|
|
for _, piece := range toRemove {
|
|
|
|
if piece == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
existing := pieceMap[piece.PieceNum]
|
2019-09-16 17:11:12 +01:00
|
|
|
if existing != nil && existing.NodeId == piece.NodeId {
|
2019-07-25 17:59:46 +01:00
|
|
|
delete(pieceMap, piece.PieceNum)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the toAdd pieces to the map
|
|
|
|
for _, piece := range toAdd {
|
|
|
|
if piece == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, exists := pieceMap[piece.PieceNum]
|
|
|
|
if exists {
|
|
|
|
return nil, Error.New("piece to add already exists (piece no: %d)", piece.PieceNum)
|
|
|
|
}
|
|
|
|
pieceMap[piece.PieceNum] = piece
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy the pieces from the map back to the pointer
|
|
|
|
var pieces []*pb.RemotePiece
|
|
|
|
for _, piece := range pieceMap {
|
2019-09-16 17:11:12 +01:00
|
|
|
// clear hashes so we don't store them
|
|
|
|
piece.Hash = nil
|
2019-07-25 17:59:46 +01:00
|
|
|
pieces = append(pieces, piece)
|
|
|
|
}
|
|
|
|
pointer.GetRemote().RemotePieces = pieces
|
|
|
|
|
|
|
|
// marshal the pointer
|
|
|
|
newPointerBytes, err := proto.Marshal(pointer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// write the pointer using compare-and-swap
|
|
|
|
err = s.DB.CompareAndSwap(ctx, []byte(path), oldPointerBytes, newPointerBytes)
|
|
|
|
if storage.ErrValueChanged.Has(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
return pointer, nil
|
|
|
|
}
|
2019-01-18 15:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get gets pointer from db
|
2019-06-05 15:23:10 +01:00
|
|
|
func (s *Service) Get(ctx context.Context, path string) (pointer *pb.Pointer, err error) {
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-06-05 15:23:10 +01:00
|
|
|
pointerBytes, err := s.DB.Get(ctx, []byte(path))
|
2019-01-18 15:10:21 +00:00
|
|
|
if err != nil {
|
2019-07-25 17:59:46 +01:00
|
|
|
return nil, Error.Wrap(err)
|
2019-01-18 15:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pointer = &pb.Pointer{}
|
|
|
|
err = proto.Unmarshal(pointerBytes, pointer)
|
|
|
|
if err != nil {
|
2019-07-25 17:59:46 +01:00
|
|
|
return nil, Error.Wrap(err)
|
2019-01-18 15:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pointer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// List returns all Path keys in the pointers bucket
|
2019-06-05 15:23:10 +01:00
|
|
|
func (s *Service) List(ctx context.Context, prefix string, startAfter string, endBefore string, recursive bool, limit int32,
|
2019-01-18 15:10:21 +00:00
|
|
|
metaFlags uint32) (items []*pb.ListResponse_Item, more bool, err error) {
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-01-18 15:10:21 +00:00
|
|
|
|
|
|
|
var prefixKey storage.Key
|
|
|
|
if prefix != "" {
|
|
|
|
prefixKey = storage.Key(prefix)
|
|
|
|
if prefix[len(prefix)-1] != storage.Delimiter {
|
|
|
|
prefixKey = append(prefixKey, storage.Delimiter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-05 15:23:10 +01:00
|
|
|
rawItems, more, err := storage.ListV2(ctx, s.DB, storage.ListOptions{
|
2019-01-18 15:10:21 +00:00
|
|
|
Prefix: prefixKey,
|
|
|
|
StartAfter: storage.Key(startAfter),
|
|
|
|
EndBefore: storage.Key(endBefore),
|
|
|
|
Recursive: recursive,
|
|
|
|
Limit: int(limit),
|
|
|
|
IncludeValue: metaFlags != meta.None,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-07-25 17:59:46 +01:00
|
|
|
return nil, false, Error.Wrap(err)
|
2019-01-18 15:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, rawItem := range rawItems {
|
2019-06-04 12:55:38 +01:00
|
|
|
items = append(items, s.createListItem(ctx, rawItem, metaFlags))
|
2019-01-18 15:10:21 +00:00
|
|
|
}
|
|
|
|
return items, more, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// createListItem creates a new list item with the given path. It also adds
|
|
|
|
// the metadata according to the given metaFlags.
|
2019-06-04 12:55:38 +01:00
|
|
|
func (s *Service) createListItem(ctx context.Context, rawItem storage.ListItem, metaFlags uint32) *pb.ListResponse_Item {
|
|
|
|
defer mon.Task()(&ctx)(nil)
|
2019-01-18 15:10:21 +00:00
|
|
|
item := &pb.ListResponse_Item{
|
|
|
|
Path: rawItem.Key.String(),
|
|
|
|
IsPrefix: rawItem.IsPrefix,
|
|
|
|
}
|
|
|
|
if item.IsPrefix {
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
|
|
|
err := s.setMetadata(item, rawItem.Value, metaFlags)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Warn("err retrieving metadata", zap.Error(err))
|
|
|
|
}
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
|
|
|
// getMetadata adds the metadata to the given item pointer according to the
|
|
|
|
// given metaFlags
|
|
|
|
func (s *Service) setMetadata(item *pb.ListResponse_Item, data []byte, metaFlags uint32) (err error) {
|
|
|
|
if metaFlags == meta.None || len(data) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pr := &pb.Pointer{}
|
|
|
|
err = proto.Unmarshal(data, pr)
|
|
|
|
if err != nil {
|
2019-07-25 17:59:46 +01:00
|
|
|
return Error.Wrap(err)
|
2019-01-18 15:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start with an empty pointer to and add only what's requested in
|
|
|
|
// metaFlags to safe to transfer payload
|
|
|
|
item.Pointer = &pb.Pointer{}
|
|
|
|
if metaFlags&meta.Modified != 0 {
|
|
|
|
item.Pointer.CreationDate = pr.GetCreationDate()
|
|
|
|
}
|
|
|
|
if metaFlags&meta.Expiration != 0 {
|
|
|
|
item.Pointer.ExpirationDate = pr.GetExpirationDate()
|
|
|
|
}
|
|
|
|
if metaFlags&meta.Size != 0 {
|
|
|
|
item.Pointer.SegmentSize = pr.GetSegmentSize()
|
|
|
|
}
|
|
|
|
if metaFlags&meta.UserDefined != 0 {
|
|
|
|
item.Pointer.Metadata = pr.GetMetadata()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes from item from db
|
2019-06-05 15:23:10 +01:00
|
|
|
func (s *Service) Delete(ctx context.Context, path string) (err error) {
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-06-05 15:23:10 +01:00
|
|
|
return s.DB.Delete(ctx, []byte(path))
|
2019-01-18 15:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate iterates over items in db
|
2019-06-05 15:23:10 +01:00
|
|
|
func (s *Service) Iterate(ctx context.Context, prefix string, first string, recurse bool, reverse bool, f func(context.Context, storage.Iterator) error) (err error) {
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-01-18 15:10:21 +00:00
|
|
|
opts := storage.IterateOptions{
|
|
|
|
Prefix: storage.Key(prefix),
|
|
|
|
First: storage.Key(first),
|
|
|
|
Recurse: recurse,
|
|
|
|
Reverse: reverse,
|
|
|
|
}
|
2019-06-05 15:23:10 +01:00
|
|
|
return s.DB.Iterate(ctx, opts, f)
|
2019-01-18 15:10:21 +00:00
|
|
|
}
|
2019-07-08 23:32:18 +01:00
|
|
|
|
|
|
|
// CreateBucket creates a new bucket in the buckets db
|
|
|
|
func (s *Service) CreateBucket(ctx context.Context, bucket storj.Bucket) (_ storj.Bucket, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
return s.bucketsDB.CreateBucket(ctx, bucket)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBucket returns an existing bucket in the buckets db
|
|
|
|
func (s *Service) GetBucket(ctx context.Context, bucketName []byte, projectID uuid.UUID) (_ storj.Bucket, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
return s.bucketsDB.GetBucket(ctx, bucketName, projectID)
|
|
|
|
}
|
|
|
|
|
2019-07-19 16:17:34 +01:00
|
|
|
// UpdateBucket returns an updated bucket in the buckets db
|
|
|
|
func (s *Service) UpdateBucket(ctx context.Context, bucket storj.Bucket) (_ storj.Bucket, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
return s.bucketsDB.UpdateBucket(ctx, bucket)
|
|
|
|
}
|
|
|
|
|
2019-07-08 23:32:18 +01:00
|
|
|
// DeleteBucket deletes a bucket from the bucekts db
|
|
|
|
func (s *Service) DeleteBucket(ctx context.Context, bucketName []byte, projectID uuid.UUID) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
return s.bucketsDB.DeleteBucket(ctx, bucketName, projectID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListBuckets returns a list of buckets for a project
|
2019-07-12 13:57:02 +01:00
|
|
|
func (s *Service) ListBuckets(ctx context.Context, projectID uuid.UUID, listOpts storj.BucketListOptions, allowedBuckets macaroon.AllowedBuckets) (bucketList storj.BucketList, err error) {
|
2019-07-08 23:32:18 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
return s.bucketsDB.ListBuckets(ctx, projectID, listOpts, allowedBuckets)
|
|
|
|
}
|