2018-11-06 11:40:06 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package kvmetainfo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/encryption"
|
|
|
|
"storj.io/storj/pkg/pb"
|
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ storj.ReadOnlyStream = (*readonlyStream)(nil)
|
|
|
|
|
|
|
|
type readonlyStream struct {
|
2018-11-14 09:26:18 +00:00
|
|
|
db *DB
|
2018-11-06 11:40:06 +00:00
|
|
|
|
|
|
|
info storj.Object
|
|
|
|
encryptedPath storj.Path
|
|
|
|
streamKey *storj.Key // lazySegmentReader derivedKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func (stream *readonlyStream) Info() storj.Object { return stream.info }
|
|
|
|
|
|
|
|
func (stream *readonlyStream) SegmentsAt(ctx context.Context, byteOffset int64, limit int64) (infos []storj.Segment, more bool, err error) {
|
2018-11-16 13:59:27 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2018-11-06 11:40:06 +00:00
|
|
|
if stream.info.FixedSegmentSize <= 0 {
|
|
|
|
return nil, false, errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
index := byteOffset / stream.info.FixedSegmentSize
|
|
|
|
return stream.Segments(ctx, index, limit)
|
|
|
|
}
|
|
|
|
|
2018-11-19 11:08:28 +00:00
|
|
|
func (stream *readonlyStream) segment(ctx context.Context, index int64) (segment storj.Segment, err error) {
|
2018-11-16 13:59:27 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2018-11-19 11:08:28 +00:00
|
|
|
segment = storj.Segment{
|
2018-11-06 11:40:06 +00:00
|
|
|
Index: index,
|
|
|
|
}
|
|
|
|
|
2018-11-19 11:08:28 +00:00
|
|
|
var segmentPath storj.Path
|
2018-11-06 11:40:06 +00:00
|
|
|
isLastSegment := segment.Index+1 == stream.info.SegmentCount
|
|
|
|
if !isLastSegment {
|
2018-11-19 11:08:28 +00:00
|
|
|
segmentPath = getSegmentPath(stream.encryptedPath, index)
|
2018-11-06 11:40:06 +00:00
|
|
|
_, meta, err := stream.db.segments.Get(ctx, segmentPath)
|
|
|
|
if err != nil {
|
|
|
|
return segment, err
|
|
|
|
}
|
|
|
|
|
|
|
|
segmentMeta := pb.SegmentMeta{}
|
|
|
|
err = proto.Unmarshal(meta.Data, &segmentMeta)
|
|
|
|
if err != nil {
|
|
|
|
return segment, err
|
|
|
|
}
|
|
|
|
|
|
|
|
segment.Size = stream.info.FixedSegmentSize
|
|
|
|
copy(segment.EncryptedKeyNonce[:], segmentMeta.KeyNonce)
|
|
|
|
segment.EncryptedKey = segmentMeta.EncryptedKey
|
|
|
|
} else {
|
2018-11-19 11:08:28 +00:00
|
|
|
segmentPath = storj.JoinPaths("l", stream.encryptedPath)
|
2018-11-06 11:40:06 +00:00
|
|
|
segment.Size = stream.info.LastSegment.Size
|
|
|
|
segment.EncryptedKeyNonce = stream.info.LastSegment.EncryptedKeyNonce
|
|
|
|
segment.EncryptedKey = stream.info.LastSegment.EncryptedKey
|
|
|
|
}
|
|
|
|
|
2018-11-19 11:08:28 +00:00
|
|
|
contentKey, err := encryption.DecryptKey(segment.EncryptedKey, stream.Info().EncryptionScheme.Cipher, stream.streamKey, &segment.EncryptedKeyNonce)
|
2018-11-06 11:40:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return segment, err
|
|
|
|
}
|
|
|
|
|
2018-11-19 11:08:28 +00:00
|
|
|
nonce := new(storj.Nonce)
|
|
|
|
_, err = encryption.Increment(nonce, index+1)
|
|
|
|
if err != nil {
|
|
|
|
return segment, err
|
|
|
|
}
|
|
|
|
|
2018-11-26 18:21:44 +00:00
|
|
|
pointer, _, _, err := stream.db.pointers.Get(ctx, segmentPath)
|
2018-11-19 11:08:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return segment, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if pointer.GetType() == pb.Pointer_INLINE {
|
|
|
|
segment.Inline, err = encryption.Decrypt(pointer.InlineSegment, stream.info.EncryptionScheme.Cipher, contentKey, nonce)
|
|
|
|
} else {
|
|
|
|
segment.PieceID = storj.PieceID(pointer.Remote.PieceId)
|
|
|
|
segment.Pieces = make([]storj.Piece, 0, len(pointer.Remote.RemotePieces))
|
|
|
|
for _, piece := range pointer.Remote.RemotePieces {
|
|
|
|
var nodeID storj.NodeID
|
2018-11-29 18:39:27 +00:00
|
|
|
copy(nodeID[:], piece.NodeId.Bytes())
|
2018-11-19 11:08:28 +00:00
|
|
|
segment.Pieces = append(segment.Pieces, storj.Piece{Number: byte(piece.PieceNum), Location: nodeID})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-06 11:40:06 +00:00
|
|
|
return segment, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (stream *readonlyStream) Segments(ctx context.Context, index int64, limit int64) (infos []storj.Segment, more bool, err error) {
|
2018-11-16 13:59:27 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2018-11-06 11:40:06 +00:00
|
|
|
if index < 0 {
|
|
|
|
return nil, false, errors.New("invalid argument")
|
|
|
|
}
|
|
|
|
if limit <= 0 {
|
|
|
|
limit = defaultSegmentLimit
|
|
|
|
}
|
|
|
|
if index >= stream.info.SegmentCount {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
infos = make([]storj.Segment, 0, limit)
|
|
|
|
for ; index < stream.info.SegmentCount && limit > 0; index++ {
|
|
|
|
limit--
|
|
|
|
segment, err := stream.segment(ctx, index)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
infos = append(infos, segment)
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:31:33 +00:00
|
|
|
more = index < stream.info.SegmentCount
|
2018-11-06 11:40:06 +00:00
|
|
|
return infos, more, nil
|
|
|
|
}
|
2018-11-30 13:50:52 +00:00
|
|
|
|
|
|
|
type mutableStream struct {
|
|
|
|
db *DB
|
|
|
|
info storj.Object
|
|
|
|
}
|
|
|
|
|
|
|
|
func (stream *mutableStream) Info() storj.Object { return stream.info }
|
|
|
|
|
|
|
|
func (stream *mutableStream) AddSegments(ctx context.Context, segments ...storj.Segment) error {
|
|
|
|
return errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (stream *mutableStream) UpdateSegments(ctx context.Context, segments ...storj.Segment) error {
|
|
|
|
return errors.New("not implemented")
|
|
|
|
}
|