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-01-18 15:10:21 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
|
|
|
"github.com/golang/protobuf/ptypes"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/pb"
|
|
|
|
"storj.io/storj/pkg/storage/meta"
|
|
|
|
"storj.io/storj/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Service structure
|
|
|
|
type Service struct {
|
|
|
|
logger *zap.Logger
|
|
|
|
DB storage.KeyValueStore
|
|
|
|
}
|
|
|
|
|
2019-04-25 09:46:32 +01:00
|
|
|
// NewService creates new metainfo service
|
2019-01-18 15:10:21 +00:00
|
|
|
func NewService(logger *zap.Logger, db storage.KeyValueStore) *Service {
|
|
|
|
return &Service{logger: logger, DB: db}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
pointer.CreationDate = ptypes.TimestampNow()
|
|
|
|
|
|
|
|
pointerBytes, err := proto.Marshal(pointer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(kaloyan): make sure that we know we are overwriting the pointer!
|
|
|
|
// In such case we should delete the pieces of the old segment if it was
|
|
|
|
// a remote one.
|
2019-06-05 15:23:10 +01:00
|
|
|
if err = s.DB.Put(ctx, []byte(path), pointerBytes); err != nil {
|
2019-01-18 15:10:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pointer = &pb.Pointer{}
|
|
|
|
err = proto.Unmarshal(pointerBytes, pointer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.New("error unmarshaling pointer: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|