From a11de9ddee415eef83bdd897ab075b41dc88b53f Mon Sep 17 00:00:00 2001 From: HuangYi Date: Tue, 14 Nov 2023 17:39:39 +0800 Subject: [PATCH] Problem: code cleanup not backported (backport: #1215) --- memiavl/db.go | 9 +++++---- memiavl/db_test.go | 2 +- memiavl/import.go | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/memiavl/db.go b/memiavl/db.go index 8530ecf706..54d977984c 100644 --- a/memiavl/db.go +++ b/memiavl/db.go @@ -20,6 +20,7 @@ import ( const ( DefaultSnapshotInterval = 1000 LockFileName = "LOCK" + TmpSuffix = "-tmp" ) var errReadOnly = errors.New("db is read-only") @@ -259,7 +260,7 @@ func removeTmpDirs(rootDir string) error { } for _, entry := range entries { - if !entry.IsDir() || !strings.HasSuffix(entry.Name(), "-tmp") { + if !entry.IsDir() || !strings.HasSuffix(entry.Name(), TmpSuffix) { continue } @@ -628,7 +629,7 @@ func (db *DB) RewriteSnapshot() error { } snapshotDir := snapshotName(db.lastCommitInfo.Version) - tmpDir := snapshotDir + "-tmp" + tmpDir := snapshotDir + TmpSuffix path := filepath.Join(db.dir, tmpDir) if err := db.MultiTree.WriteSnapshot(path); err != nil { return errors.Join(err, os.RemoveAll(path)) @@ -835,7 +836,7 @@ func currentPath(root string) string { } func currentTmpPath(root string) string { - return filepath.Join(root, "current-tmp") + return currentPath(root) + TmpSuffix } func currentVersion(root string) (int64, error) { @@ -981,7 +982,7 @@ func traverseSnapshots(dir string, ascending bool, callback func(int64) (bool, e // atomicRemoveDir is equavalent to `mv snapshot snapshot-tmp && rm -r snapshot-tmp` func atomicRemoveDir(path string) error { - tmpPath := path + "-tmp" + tmpPath := path + TmpSuffix if err := os.Rename(path, tmpPath); err != nil { return err } diff --git a/memiavl/db_test.go b/memiavl/db_test.go index 363b81a5b8..030c2fd8cd 100644 --- a/memiavl/db_test.go +++ b/memiavl/db_test.go @@ -45,7 +45,7 @@ func TestRemoveSnapshotDir(t *testing.T) { defer os.RemoveAll(dbDir) snapshotDir := filepath.Join(dbDir, snapshotName(0)) - tmpDir := snapshotDir + "-tmp" + tmpDir := snapshotDir + TmpSuffix if err := os.MkdirAll(tmpDir, os.ModePerm); err != nil { t.Fatalf("Failed to create dummy snapshot directory: %v", err) } diff --git a/memiavl/import.go b/memiavl/import.go index c62c6f7b0a..8d96f79835 100644 --- a/memiavl/import.go +++ b/memiavl/import.go @@ -25,7 +25,7 @@ func Import( return snapshottypes.SnapshotItem{}, fmt.Errorf("version overflows uint32: %d", height) } snapshotDir := snapshotName(int64(height)) - tmpDir := snapshotDir + "-tmp" + tmpDir := snapshotDir + TmpSuffix var fileLock FileLock fileLock, err = LockFile(filepath.Join(dir, LockFileName))