github.com/matrixorigin/matrixone@v1.2.0/pkg/fileservice/file_service.go (about)

     1  // Copyright 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package fileservice
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"io"
    21  	"strings"
    22  	"time"
    23  
    24  	"github.com/matrixorigin/matrixone/pkg/fileservice/memorycache"
    25  )
    26  
    27  // FileService is a write-once file system
    28  type FileService interface {
    29  	// Name is file service's name
    30  	// service name is case-insensitive
    31  	Name() string
    32  
    33  	// Write writes a new file
    34  	// returns ErrFileExisted if file already existed
    35  	// returns ErrSizeNotMatch if provided size does not match data
    36  	// entries in vector should be written atomically. if write failed, following reads must not succeed.
    37  	Write(ctx context.Context, vector IOVector) error
    38  
    39  	// Read reads a file to fill IOEntries
    40  	// returns ErrFileNotFound if requested file not found
    41  	// returns ErrUnexpectedEOF if less data is read than requested size
    42  	// returns ErrEmptyRange if no data at specified offset and size
    43  	// returns ErrEmptyVector if no IOEntry is passed
    44  	Read(ctx context.Context, vector *IOVector) error
    45  
    46  	// ReadCache reads cached data if any
    47  	// if cache hit, IOEntry.CachedData will be set
    48  	ReadCache(ctx context.Context, vector *IOVector) error
    49  
    50  	// List lists sub-entries in a dir
    51  	List(ctx context.Context, dirPath string) ([]DirEntry, error)
    52  
    53  	// Delete deletes multi file
    54  	// returns ErrFileNotFound if requested file not found
    55  	Delete(ctx context.Context, filePaths ...string) error
    56  
    57  	// Stat returns infomations about a file
    58  	// returns ErrFileNotFound if requested file not found
    59  	StatFile(ctx context.Context, filePath string) (*DirEntry, error)
    60  
    61  	// PrefetchFile prefetches a file
    62  	PrefetchFile(ctx context.Context, filePath string) error
    63  
    64  	Close()
    65  }
    66  
    67  type IOVector struct {
    68  
    69  	// FilePath indicates where to find the file
    70  	// a path has two parts, service name and file name, separated by ':'
    71  	// service name is optional, if omitted, the receiver FileService will use the default name of the service
    72  	// file name parts are separated by '/'
    73  	// valid characters in file name: 0-9 a-z A-Z / ! - _ . * ' ( )
    74  	// and all printable non-ASCII characters
    75  	// example:
    76  	// s3:a/b/c S3:a/b/c represents the same file 'a/b/c' located in 'S3' service
    77  	FilePath string
    78  
    79  	// io entries
    80  	// empty Entries is not allowed
    81  	// when writing, overlapping Entries is not allowed
    82  	Entries []IOEntry
    83  
    84  	// ExpireAt specifies the expire time of the file
    85  	// implementations may or may not delete the file after this time
    86  	// zero value means no expire
    87  	ExpireAt time.Time
    88  
    89  	// Policy controls policy for the vector
    90  	Policy Policy
    91  
    92  	// Caches indicates extra caches to operate on
    93  	Caches []IOVectorCache
    94  }
    95  
    96  type IOEntry struct {
    97  	// offset in file
    98  	// when writing or mutating, offset can be arbitrary value, gaps between provided data are zero-filled
    99  	// when reading, valid offsets are in range [0, len(file) - 1]
   100  	Offset int64
   101  
   102  	// number of bytes to read or write, [1, len(file)]
   103  	// when reading, pass -1 to read to the end of file
   104  	Size int64
   105  
   106  	// raw content
   107  	// when reading, if len(Data) < Size, a new Size-lengthed byte slice will be allocated
   108  	Data []byte
   109  
   110  	// when reading, if Writer is not nil, write data to it instead of setting Data field
   111  	WriterForRead io.Writer
   112  
   113  	// when reading, if ReadCloser is not nil, set an io.ReadCloser instead of setting Data field
   114  	ReadCloserForRead *io.ReadCloser
   115  
   116  	// when writing, if Reader is not nil, read data from it instead of reading Data field
   117  	// number of bytes to be read is specified by Size field
   118  	// if number of bytes is unknown, set Size field to -1
   119  	ReaderForWrite io.Reader
   120  
   121  	// When reading, if the ToCacheData field is not nil, the returning object's byte slice will be set to this field
   122  	// Data, WriterForRead, ReadCloserForRead may be empty if CachedData is not null
   123  	// if ToCacheData is provided, caller should always read CachedData instead of Data, WriterForRead or ReadCloserForRead
   124  	CachedData memorycache.CacheData
   125  
   126  	// ToCacheData constructs an object byte slice from entry contents
   127  	// reader or data must not be retained after returns
   128  	// reader always contains entry contents
   129  	// data may contains entry contents if available
   130  	// if data is empty, the io.Reader must be fully read before returning nil error
   131  	ToCacheData func(reader io.Reader, data []byte, allocator CacheDataAllocator) (cacheData memorycache.CacheData, err error)
   132  
   133  	// done indicates whether the entry is filled with data
   134  	// for implementing cascade cache
   135  	done bool
   136  
   137  	// fromCache indicates which cache filled the entry
   138  	fromCache IOVectorCache
   139  
   140  	allocator CacheDataAllocator
   141  }
   142  
   143  func (i IOEntry) String() string {
   144  	buf := new(strings.Builder)
   145  	buf.WriteString("IOEntry(")
   146  	fmt.Fprintf(buf, "offset = %v", i.Offset)
   147  	fmt.Fprintf(buf, ", size = %v", i.Size)
   148  	buf.WriteString(")")
   149  	return buf.String()
   150  }
   151  
   152  type CacheDataAllocator interface {
   153  	Alloc(size int) memorycache.CacheData
   154  }
   155  
   156  // DirEntry is a file or dir
   157  type DirEntry struct {
   158  	// file name, not full path
   159  	Name  string
   160  	IsDir bool
   161  	Size  int64
   162  }