/
cleaner.go
60 lines (46 loc) · 1.32 KB
/
cleaner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2019 The LevelDB-Go and Pebble Authors. All rights reserved. Use
// of this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
package base
import (
"path/filepath"
"github.com/cockroachdb/pebble/vfs"
)
// Cleaner cleans obsolete files.
type Cleaner interface {
Clean(fs vfs.FS, fileType FileType, bytesPerSync int, path string) error
}
// DeleteCleaner deletes file.
type DeleteCleaner struct{}
// Clean removes file.
func (DeleteCleaner) Clean(fs vfs.FS, fileType FileType, bytesPerSync int, path string) error {
return fs.Remove(path)
}
func (DeleteCleaner) String() string {
return "Delete"
}
// ArchiveCleaner archives file instead delete.
type ArchiveCleaner struct{}
// Clean archives file.
func (ArchiveCleaner) Clean(fs vfs.FS, fileType FileType, bytesPerSync int, path string) error {
switch fileType {
case FileTypeLog, FileTypeManifest, FileTypeTable:
destDir := filepath.Dir(path) + "/archive"
fs := SyncingFS{
FS: fs,
SyncOpts: vfs.SyncingFileOptions{
BytesPerSync: bytesPerSync,
},
}
if err := fs.MkdirAll(destDir, 0755); err != nil {
return err
}
destPath := fs.PathJoin(destDir, fs.PathBase(path))
return fs.Rename(path, destPath)
default:
return fs.Remove(path)
}
}
func (ArchiveCleaner) String() string {
return "Archive"
}