github.com/matrixorigin/matrixone@v1.2.0/pkg/fileservice/file_services.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 "strings" 20 21 "github.com/matrixorigin/matrixone/pkg/common/moerr" 22 ) 23 24 type FileServices struct { 25 defaultName string 26 mappings map[string]FileService 27 } 28 29 func NewFileServices(defaultName string, fss ...FileService) (*FileServices, error) { 30 f := &FileServices{ 31 defaultName: defaultName, 32 mappings: make(map[string]FileService), 33 } 34 for _, fs := range fss { 35 name := strings.ToLower(fs.Name()) 36 if _, ok := f.mappings[name]; ok { 37 return nil, moerr.NewDupServiceNameNoCtx(name) 38 } 39 f.mappings[name] = fs 40 } 41 return f, nil 42 } 43 44 var _ FileService = &FileServices{} 45 46 func (f *FileServices) Delete(ctx context.Context, filePaths ...string) error { 47 for _, filePath := range filePaths { 48 if err := f.deleteSingle(ctx, filePath); err != nil { 49 return err 50 } 51 } 52 return nil 53 } 54 55 func (f *FileServices) Close() { 56 for _, fs := range f.mappings { 57 fs.Close() 58 } 59 } 60 61 func (f *FileServices) deleteSingle(ctx context.Context, filePath string) error { 62 path, err := ParsePathAtService(filePath, "") 63 if err != nil { 64 return err 65 } 66 if path.Service == "" { 67 path.Service = f.defaultName 68 } 69 fs, err := Get[FileService](f, path.Service) 70 if err != nil { 71 return err 72 } 73 return fs.Delete(ctx, filePath) 74 } 75 76 func (f *FileServices) List(ctx context.Context, dirPath string) ([]DirEntry, error) { 77 path, err := ParsePathAtService(dirPath, "") 78 if err != nil { 79 return nil, err 80 } 81 if path.Service == "" { 82 path.Service = f.defaultName 83 } 84 fs, err := Get[FileService](f, path.Service) 85 if err != nil { 86 return nil, err 87 } 88 return fs.List(ctx, dirPath) 89 } 90 91 func (f *FileServices) Name() string { 92 return f.defaultName 93 } 94 95 func (f *FileServices) Read(ctx context.Context, vector *IOVector) error { 96 path, err := ParsePathAtService(vector.FilePath, "") 97 if err != nil { 98 return err 99 } 100 if path.Service == "" { 101 path.Service = f.defaultName 102 } 103 fs, err := Get[FileService](f, path.Service) 104 if err != nil { 105 return err 106 } 107 return fs.Read(ctx, vector) 108 } 109 110 func (f *FileServices) ReadCache(ctx context.Context, vector *IOVector) error { 111 path, err := ParsePathAtService(vector.FilePath, "") 112 if err != nil { 113 return err 114 } 115 if path.Service == "" { 116 path.Service = f.defaultName 117 } 118 fs, err := Get[FileService](f, path.Service) 119 if err != nil { 120 return err 121 } 122 return fs.ReadCache(ctx, vector) 123 } 124 125 func (f *FileServices) Write(ctx context.Context, vector IOVector) error { 126 path, err := ParsePathAtService(vector.FilePath, "") 127 if err != nil { 128 return err 129 } 130 if path.Service == "" { 131 path.Service = f.defaultName 132 } 133 fs, err := Get[FileService](f, path.Service) 134 if err != nil { 135 return err 136 } 137 return fs.Write(ctx, vector) 138 } 139 140 func (f *FileServices) StatFile(ctx context.Context, filePath string) (*DirEntry, error) { 141 path, err := ParsePathAtService(filePath, "") 142 if err != nil { 143 return nil, err 144 } 145 if path.Service == "" { 146 path.Service = f.defaultName 147 } 148 fs, err := Get[FileService](f, path.Service) 149 if err != nil { 150 return nil, err 151 } 152 return fs.StatFile(ctx, filePath) 153 } 154 155 func (f *FileServices) PrefetchFile(ctx context.Context, filePath string) error { 156 path, err := ParsePathAtService(filePath, "") 157 if err != nil { 158 return err 159 } 160 if path.Service == "" { 161 path.Service = f.defaultName 162 } 163 fs, err := Get[FileService](f, path.Service) 164 if err != nil { 165 return err 166 } 167 return fs.PrefetchFile(ctx, filePath) 168 }