fileservice

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2022 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFileNotFound  = errors.New("file not found")
	ErrFileExisted   = errors.New("file existed")
	ErrUnexpectedEOF = io.ErrUnexpectedEOF
	ErrSizeNotMatch  = errors.New("size not match")
	ErrEmptyRange    = errors.New("empty range")
)

Functions

This section is empty.

Types

type DirEntry

type DirEntry struct {
	// file name, not full path
	Name  string
	IsDir bool
	Size  int
}

DirEntry is a file or dir

type FileService

type FileService interface {
	// Write writes a new file
	// returns ErrFileExisted if file already existed
	// returns ErrSizeNotMatch if provided size does not match data
	// entries in vector should be written atomically. if write failed, following reads must not succeed.
	Write(ctx context.Context, vector IOVector) error

	// Read reads a file to fill IOEntries
	// returns ErrFileNotFound if requested file not found
	// returns ErrUnexpectedEOF if less data is read than requested size
	// returns ErrEmptyRange if no data at specified offset and size
	Read(ctx context.Context, vector *IOVector) error

	// List lists sub-entries in a dir
	List(ctx context.Context, dirPath string) ([]DirEntry, error)

	// Delete deletes a file
	// returns ErrFileNotFound if requested file not found
	Delete(ctx context.Context, filePath string) error
}

FileService is a write-once file system

type IOEntry

type IOEntry struct {
	// offset in file, [0, len(file) - 1]
	Offset int

	// number of bytes to be read of written, [1, len(file)]
	Size int

	// raw content
	// when reading, if len(Data) < Size, a new Size-lengthed byte slice will be allocated
	Data []byte

	// when reading, if Writer is not nil, write data to it instead of setting Data field
	WriterForRead io.Writer

	// when reading, if ReadCloser is not nil, set an io.ReadCloser instead of setting Data field
	ReadCloserForRead *io.ReadCloser

	// when writing, if Reader is not nil, read data from it instead of reading Data field
	ReaderForWrite io.Reader
}

type IOVector

type IOVector struct {
	// path to file, '/' separated
	FilePath string
	// io entries
	// empty entry not allowed
	Entries []IOEntry
}

type LocalFS

type LocalFS struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

LocalFS is a FileService implementation backed by local file system

func NewLocalFS

func NewLocalFS(rootPath string) (*LocalFS, error)

func (*LocalFS) Delete

func (l *LocalFS) Delete(ctx context.Context, filePath string) error

func (*LocalFS) List

func (l *LocalFS) List(ctx context.Context, dirPath string) (ret []DirEntry, err error)

func (*LocalFS) Mutate

func (l *LocalFS) Mutate(ctx context.Context, vector IOVector) error

func (*LocalFS) Read

func (l *LocalFS) Read(ctx context.Context, vector *IOVector) error

func (*LocalFS) Write

func (l *LocalFS) Write(ctx context.Context, vector IOVector) error

type MemoryFS

type MemoryFS struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

MemoryFS is an in-memory FileService implementation

func NewMemoryFS

func NewMemoryFS() (*MemoryFS, error)

func (*MemoryFS) Delete

func (m *MemoryFS) Delete(ctx context.Context, filePath string) error

func (*MemoryFS) List

func (m *MemoryFS) List(ctx context.Context, dirPath string) (entries []DirEntry, err error)

func (*MemoryFS) Read

func (m *MemoryFS) Read(ctx context.Context, vector *IOVector) error

func (*MemoryFS) Write

func (m *MemoryFS) Write(ctx context.Context, vector IOVector) error

type MutableFileService

type MutableFileService interface {
	FileService

	// Mutate mutates file contents
	Mutate(ctx context.Context, vector IOVector) error
}

MutableFileService is an extension interface to FileService that allow mutation

type S3Config

type S3Config struct {
	Endpoint  string
	Region    string
	APIKey    string
	APISecret string
	Bucket    string
	// KeyPrefix enables multiple fs instances in one bucket
	KeyPrefix string
}

type S3FS

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

S3FS is a FileService implementation backed by S3

func NewS3FS

func NewS3FS(
	endpoint string,
	bucket string,
	keyPrefix string,
) (*S3FS, error)

func (*S3FS) Delete

func (m *S3FS) Delete(ctx context.Context, filePath string) error

func (*S3FS) List

func (m *S3FS) List(ctx context.Context, dirPath string) (entries []DirEntry, err error)

func (*S3FS) Read

func (m *S3FS) Read(ctx context.Context, vector *IOVector) error

func (*S3FS) Write

func (m *S3FS) Write(ctx context.Context, vector IOVector) error

type S3FSMinio

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

S3FSMinio is a FileService implementation backed by S3, using minio client

func NewS3FSMinio

func NewS3FSMinio(config S3Config) (*S3FSMinio, error)

func (*S3FSMinio) Delete

func (m *S3FSMinio) Delete(ctx context.Context, filePath string) error

func (*S3FSMinio) List

func (m *S3FSMinio) List(ctx context.Context, dirPath string) (entries []DirEntry, err error)

func (*S3FSMinio) Read

func (m *S3FSMinio) Read(ctx context.Context, vector *IOVector) error

func (*S3FSMinio) Write

func (m *S3FSMinio) Write(ctx context.Context, vector IOVector) error

Jump to

Keyboard shortcuts

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