github.com/matrixorigin/matrixone@v0.7.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 "io" 20 "time" 21 ) 22 23 // FileService is a write-once file system 24 type FileService interface { 25 // Name is file service's name 26 // service name is case-insensitive 27 Name() string 28 29 // Write writes a new file 30 // returns ErrFileExisted if file already existed 31 // returns ErrSizeNotMatch if provided size does not match data 32 // entries in vector should be written atomically. if write failed, following reads must not succeed. 33 Write(ctx context.Context, vector IOVector) error 34 35 // Read reads a file to fill IOEntries 36 // returns ErrFileNotFound if requested file not found 37 // returns ErrUnexpectedEOF if less data is read than requested size 38 // returns ErrEmptyRange if no data at specified offset and size 39 // returns ErrEmptyVector if no IOEntry is passed 40 Read(ctx context.Context, vector *IOVector) error 41 42 // List lists sub-entries in a dir 43 List(ctx context.Context, dirPath string) ([]DirEntry, error) 44 45 // Delete deletes multi file 46 // returns ErrFileNotFound if requested file not found 47 Delete(ctx context.Context, filePaths ...string) error 48 49 // Stat returns infomations about a file 50 // returns ErrFileNotFound if requested file not found 51 StatFile(ctx context.Context, filePath string) (*DirEntry, error) 52 } 53 54 type IOVector struct { 55 // FilePath indicates where to find the file 56 // a path has two parts, service name and file name, separated by ':' 57 // service name is optional, if omitted, the receiver FileService will use the default name of the service 58 // file name parts are separated by '/' 59 // valid characters in file name: 0-9 a-z A-Z / ! - _ . * ' ( ) 60 // and all printable non-ASCII characters 61 // example: 62 // s3:a/b/c S3:a/b/c represents the same file 'a/b/c' located in 'S3' service 63 FilePath string 64 // io entries 65 // empty Entries is not allowed 66 // when writing, overlapping Entries is not allowed 67 Entries []IOEntry 68 // ExpireAt specifies the expire time of the file 69 // implementations may or may not delete the file after this time 70 // zero value means no expire 71 ExpireAt time.Time 72 } 73 74 type IOEntry struct { 75 // offset in file 76 // when writing or mutating, offset can be arbitrary value, gaps between provided data are zero-filled 77 // when reading, valid offsets are in range [0, len(file) - 1] 78 Offset int64 79 80 // number of bytes to read or write, [1, len(file)] 81 // when reading, pass -1 to read to the end of file 82 Size int64 83 84 // raw content 85 // when reading, if len(Data) < Size, a new Size-lengthed byte slice will be allocated 86 Data []byte 87 88 // when reading, if Writer is not nil, write data to it instead of setting Data field 89 WriterForRead io.Writer 90 91 // when reading, if ReadCloser is not nil, set an io.ReadCloser instead of setting Data field 92 ReadCloserForRead *io.ReadCloser 93 94 // when writing, if Reader is not nil, read data from it instead of reading Data field 95 // number of bytes to be read is specified by Size field 96 // if number of bytes is unknown, set Size field to -1 97 ReaderForWrite io.Reader 98 99 // when reading, if the ToObject field is not nil, the returning object will be set to this field 100 // caches may choose to cache this object instead of caching []byte 101 // Data, WriterForRead, ReadCloserForRead may be empty if Object is not null 102 // if ToObject is provided, caller should always read Object instead of Data, WriterForRead or ReadCloserForRead 103 Object any 104 105 // ToObject constructs an object from entry contents 106 // reader or data must not be retained after returns 107 // reader always contains entry contents 108 // data may contains entry contents if available 109 // if data is empty, the io.Reader must be fully read before returning nil error 110 // return an *RC value to make the object pinnable 111 // cache implementations should not evict an *RC value with non-zero reference 112 ToObject func(reader io.Reader, data []byte) (object any, objectSize int64, err error) 113 114 // ObjectSize indicates the memory bytes to hold the object 115 // set from ToObject returning value 116 // used in capacity limited caches 117 ObjectSize int64 118 119 // done indicates whether the entry is filled with data 120 // for implementing cascade cache 121 done bool 122 } 123 124 // DirEntry is a file or dir 125 type DirEntry struct { 126 // file name, not full path 127 Name string 128 IsDir bool 129 Size int64 130 }