github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/bithash/checkpoint.go (about) 1 // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors. 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 bithash 16 17 import ( 18 "os" 19 "path/filepath" 20 21 "github.com/cockroachdb/errors" 22 "github.com/cockroachdb/errors/oserror" 23 "github.com/zuoyebang/bitalosdb/internal/vfs" 24 ) 25 26 func (b *Bithash) Checkpoint(fs vfs.FS, destDir string) (err error) { 27 if _, err = os.Stat(destDir); !oserror.IsNotExist(err) { 28 return errors.Errorf("bithash: checkpoint dir exist %s", destDir) 29 } 30 31 var dir vfs.File 32 err = os.Mkdir(destDir, 0755) 33 if err != nil { 34 return err 35 } 36 dir, err = fs.OpenDir(destDir) 37 if err != nil { 38 return err 39 } 40 41 if err = b.closeMutableWriters(); err != nil { 42 return err 43 } 44 45 manifest := MakeFilepath(fs, b.dirname, fileTypeManifest, 0) 46 destManifest := MakeFilepath(fs, destDir, fileTypeManifest, 0) 47 err = vfs.Copy(fs, manifest, destManifest) 48 if err != nil { 49 return err 50 } 51 52 fileNumMap := MakeFilepath(fs, b.dirname, fileTypeFileNumMap, 0) 53 destFileNumMap := MakeFilepath(fs, destDir, fileTypeFileNumMap, 0) 54 if err = vfs.Copy(fs, fileNumMap, destFileNumMap); err != nil { 55 return err 56 } 57 58 compactLog := MakeFilepath(fs, b.dirname, fileTypeCompactLog, 0) 59 destCompactLog := MakeFilepath(fs, destDir, fileTypeCompactLog, 0) 60 if err = vfs.Copy(fs, compactLog, destCompactLog); err != nil { 61 return err 62 } 63 64 copyTable := func() error { 65 b.meta.mu.RLock() 66 defer b.meta.mu.RUnlock() 67 for fn := range b.meta.mu.filesMeta { 68 filename := MakeFilepath(fs, b.dirname, fileTypeTable, fn) 69 if _, err = fs.Stat(filename); err != nil { 70 return err 71 } 72 err = vfs.LinkOrCopy(fs, filename, filepath.Join(destDir, fs.PathBase(filename))) 73 if err != nil { 74 return err 75 } 76 } 77 return nil 78 } 79 err = copyTable() 80 if err != nil { 81 return err 82 } 83 84 err = dir.Sync() 85 if err != nil { 86 return err 87 } 88 err = dir.Close() 89 dir = nil 90 91 return err 92 }