store

package
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 16, 2020 License: Apache-2.0 Imports: 16 Imported by: 2

Documentation

Index

Constants

View Source
const LocalStorageDriver = "local"

LocalStorageDriver is a const of local storage driver.

Variables

View Source
var (
	// ErrKeyNotFound is an error which will be returned
	// when the key can not be found.
	ErrKeyNotFound = StorageError{codeKeyNotFound, "the key not found"}

	// ErrEmptyKey is an error when the key is empty.
	ErrEmptyKey = StorageError{codeEmptyKey, "the key is empty"}

	// ErrInvalidValue represents the value is invalid.
	ErrInvalidValue = StorageError{codeInvalidValue, "invalid value"}

	// ErrRangeNotSatisfiable represents the length of file is insufficient.
	ErrRangeNotSatisfiable = StorageError{codeRangeNotSatisfiable, "range not satisfiable"}
)

Functions

func IsEmptyKey

func IsEmptyKey(err error) bool

IsEmptyKey checks the error is the key is empty or nil.

func IsInvalidValue

func IsInvalidValue(err error) bool

IsInvalidValue checks the error is the value is invalid or not.

func IsKeyNotFound

func IsKeyNotFound(err error) bool

IsKeyNotFound checks the error is the key cannot be found.

func IsNilError

func IsNilError(err error) bool

IsNilError checks the error is nil or not.

func IsRangeNotSatisfiable

func IsRangeNotSatisfiable(err error) bool

IsRangeNotSatisfiable checks the error is a range not exist error or not.

func Register

func Register(name string, builder StorageBuilder)

Register defines an interface to register a driver with specified name. All drivers should call this function to register itself to the driverFactory.

Types

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager manages stores.

func NewManager

func NewManager(cfg *config.Config) (*Manager, error)

NewManager creates a store manager.

func (*Manager) Get

func (sm *Manager) Get(name string) (*Store, error)

Get a store from manager with specified name.

type Raw

type Raw struct {
	Bucket string
	Key    string
	Offset int64
	Length int64
	Trunc  bool
	WalkFn filepath.WalkFunc
}

Raw identifies a piece of data uniquely. If the length<=0, it represents all data.

type StorageBuilder

type StorageBuilder func(conf string) (StorageDriver, error)

StorageBuilder is a function that creates a new storage plugin instant with the giving conf.

type StorageDriver

type StorageDriver interface {
	// Get data from the storage based on raw information.
	// If the length<=0, the driver should return all data from the raw.offset.
	// Otherwise, just return the data which starts from raw.offset and the length is raw.length.
	Get(ctx context.Context, raw *Raw) (io.Reader, error)

	// Get data from the storage based on raw information.
	// The data should be returned in bytes.
	// If the length<=0, the storage driver should return all data from the raw.offset.
	// Otherwise, just return the data which starts from raw.offset and the length is raw.length.
	GetBytes(ctx context.Context, raw *Raw) ([]byte, error)

	// Put the data into the storage with raw information.
	// The storage will get data from io.Reader as io stream.
	// If the offset>0, the storage driver should starting at byte raw.offset off.
	Put(ctx context.Context, raw *Raw, data io.Reader) error

	// PutBytes puts the data into the storage with raw information.
	// The data is passed in bytes.
	// If the offset>0, the storage driver should starting at byte raw.offset off.
	PutBytes(ctx context.Context, raw *Raw, data []byte) error

	// Remove the data from the storage based on raw information.
	Remove(ctx context.Context, raw *Raw) error

	// Stat determines whether the data exists based on raw information.
	// If that, and return some info that in the form of struct StorageInfo.
	// If not, return the ErrNotFound.
	Stat(ctx context.Context, raw *Raw) (*StorageInfo, error)

	// GetAvailSpace returns the available disk space in B.
	GetAvailSpace(ctx context.Context, raw *Raw) (fileutils.Fsize, error)

	// Walk walks the file tree rooted at root which determined by raw.Bucket and raw.Key,
	// calling walkFn for each file or directory in the tree, including root.
	Walk(ctx context.Context, raw *Raw) error
}

StorageDriver defines an interface to manage the data stored in the driver.

NOTE: It is recommended that the lock granularity of the driver should be in piece. That means that the storage driver could read and write the different pieces of the same file concurrently.

func NewLocalStorage

func NewLocalStorage(conf string) (StorageDriver, error)

NewLocalStorage performs initialization for localStorage and return a StorageDriver.

type StorageError

type StorageError struct {
	Code int
	Msg  string
}

StorageError represents a storage error.

func (StorageError) Error

func (s StorageError) Error() string

type StorageInfo

type StorageInfo struct {
	Path       string
	Size       int64
	CreateTime time.Time
	ModTime    time.Time
}

StorageInfo includes partial meta information of the data.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store is a wrapper of the storage which implements the interface of StorageDriver.

func NewStore

func NewStore(name string, builder StorageBuilder, cfg string) (*Store, error)

NewStore creates a new Store instance.

func (*Store) Get

func (s *Store) Get(ctx context.Context, raw *Raw) (io.Reader, error)

Get the data from the storage driver in io stream.

func (*Store) GetAvailSpace added in v1.0.0

func (s *Store) GetAvailSpace(ctx context.Context, raw *Raw) (fileutils.Fsize, error)

GetAvailSpace returns the available disk space in B.

func (*Store) GetBytes

func (s *Store) GetBytes(ctx context.Context, raw *Raw) ([]byte, error)

GetBytes gets the data from the storage driver in bytes.

func (*Store) Name

func (s *Store) Name() string

Name returns the plugin name.

func (*Store) Put

func (s *Store) Put(ctx context.Context, raw *Raw, data io.Reader) error

Put puts data into the storage in io stream.

func (*Store) PutBytes

func (s *Store) PutBytes(ctx context.Context, raw *Raw, data []byte) error

PutBytes puts data into the storage in bytes.

func (*Store) Remove

func (s *Store) Remove(ctx context.Context, raw *Raw) error

Remove the data from the storage based on raw information.

func (*Store) Stat

func (s *Store) Stat(ctx context.Context, raw *Raw) (*StorageInfo, error)

Stat determines whether the data exists based on raw information. If that, and return some info that in the form of struct StorageInfo. If not, return the ErrNotFound.

func (*Store) Type

func (s *Store) Type() config.PluginType

Type returns the plugin type: StoragePlugin.

func (*Store) Walk added in v1.0.0

func (s *Store) Walk(ctx context.Context, raw *Raw) error

Walk walks the file tree rooted at root which determined by raw.Bucket and raw.Key, calling walkFn for each file or directory in the tree, including root.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL