github.com/matrixorigin/matrixone@v0.7.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) deleteSingle(ctx context.Context, filePath string) error { 56 path, err := ParsePathAtService(filePath, "") 57 if err != nil { 58 return err 59 } 60 if path.Service == "" { 61 path.Service = f.defaultName 62 } 63 fs, err := Get[FileService](f, path.Service) 64 if err != nil { 65 return err 66 } 67 return fs.Delete(ctx, filePath) 68 } 69 70 func (f *FileServices) List(ctx context.Context, dirPath string) ([]DirEntry, error) { 71 path, err := ParsePathAtService(dirPath, "") 72 if err != nil { 73 return nil, err 74 } 75 if path.Service == "" { 76 path.Service = f.defaultName 77 } 78 fs, err := Get[FileService](f, path.Service) 79 if err != nil { 80 return nil, err 81 } 82 return fs.List(ctx, dirPath) 83 } 84 85 func (f *FileServices) Name() string { 86 return f.defaultName 87 } 88 89 func (f *FileServices) Read(ctx context.Context, vector *IOVector) error { 90 path, err := ParsePathAtService(vector.FilePath, "") 91 if err != nil { 92 return err 93 } 94 if path.Service == "" { 95 path.Service = f.defaultName 96 } 97 fs, err := Get[FileService](f, path.Service) 98 if err != nil { 99 return err 100 } 101 return fs.Read(ctx, vector) 102 } 103 104 func (f *FileServices) Write(ctx context.Context, vector IOVector) error { 105 path, err := ParsePathAtService(vector.FilePath, "") 106 if err != nil { 107 return err 108 } 109 if path.Service == "" { 110 path.Service = f.defaultName 111 } 112 fs, err := Get[FileService](f, path.Service) 113 if err != nil { 114 return err 115 } 116 return fs.Write(ctx, vector) 117 } 118 119 func (f *FileServices) StatFile(ctx context.Context, filePath string) (*DirEntry, error) { 120 path, err := ParsePathAtService(filePath, "") 121 if err != nil { 122 return nil, err 123 } 124 if path.Service == "" { 125 path.Service = f.defaultName 126 } 127 fs, err := Get[FileService](f, path.Service) 128 if err != nil { 129 return nil, err 130 } 131 return fs.StatFile(ctx, filePath) 132 }