2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-09-28 07:59:27 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package filestore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/storj/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Error is the default filestore error class
|
|
|
|
var Error = errs.Class("filestore error")
|
|
|
|
|
|
|
|
var _ storage.Blobs = (*Store)(nil)
|
|
|
|
|
|
|
|
// Store implements a blob store
|
|
|
|
type Store struct {
|
|
|
|
dir *Dir
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new disk blob store in the specified directory
|
|
|
|
func New(dir *Dir) *Store {
|
|
|
|
return &Store{dir}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAt creates a new disk blob store in the specified directory
|
|
|
|
func NewAt(path string) (*Store, error) {
|
|
|
|
dir, err := NewDir(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
return &Store{dir}, nil
|
|
|
|
}
|
|
|
|
|
2019-03-11 08:06:56 +00:00
|
|
|
// Open loads blob with the specified hash
|
|
|
|
func (store *Store) Open(ctx context.Context, ref storage.BlobRef) (storage.BlobReader, error) {
|
|
|
|
file, openErr := store.dir.Open(ref)
|
2018-09-28 07:59:27 +01:00
|
|
|
if openErr != nil {
|
|
|
|
if os.IsNotExist(openErr) {
|
|
|
|
return nil, openErr
|
|
|
|
}
|
|
|
|
return nil, Error.Wrap(openErr)
|
|
|
|
}
|
2019-03-11 08:06:56 +00:00
|
|
|
return newBlobReader(file), nil
|
2018-09-28 07:59:27 +01:00
|
|
|
}
|
|
|
|
|
2019-03-11 08:06:56 +00:00
|
|
|
// Delete deletes blobs with the specified ref
|
|
|
|
func (store *Store) Delete(ctx context.Context, ref storage.BlobRef) error {
|
|
|
|
err := store.dir.Delete(ref)
|
|
|
|
return Error.Wrap(err)
|
2018-09-28 07:59:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GarbageCollect tries to delete any files that haven't yet been deleted
|
|
|
|
func (store *Store) GarbageCollect(ctx context.Context) error {
|
|
|
|
err := store.dir.GarbageCollect()
|
2019-03-11 08:06:56 +00:00
|
|
|
return Error.Wrap(err)
|
2018-09-28 07:59:27 +01:00
|
|
|
}
|
|
|
|
|
2019-03-11 08:06:56 +00:00
|
|
|
// Create creates a new blob that can be written
|
|
|
|
// optionally takes a size argument for performance improvements, -1 is unknown size
|
|
|
|
func (store *Store) Create(ctx context.Context, ref storage.BlobRef, size int64) (storage.BlobWriter, error) {
|
2018-09-28 07:59:27 +01:00
|
|
|
file, err := store.dir.CreateTemporaryFile(size)
|
|
|
|
if err != nil {
|
2019-03-11 08:06:56 +00:00
|
|
|
return nil, Error.Wrap(err)
|
2018-09-28 07:59:27 +01:00
|
|
|
}
|
2019-03-11 08:06:56 +00:00
|
|
|
return newBlobWriter(ref, store, file), nil
|
2018-09-28 07:59:27 +01:00
|
|
|
}
|