Line data Source code
1 : // Copyright 2019 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 : // of this source code is governed by a BSD-style license that can be found in 3 : // the LICENSE file. 4 : 5 : package base 6 : 7 : import "github.com/cockroachdb/pebble/vfs" 8 : 9 : // Cleaner cleans obsolete files. 10 : type Cleaner interface { 11 : Clean(fs vfs.FS, fileType FileType, path string) error 12 : } 13 : 14 : // NeedsFileContents is implemented by a cleaner that needs the contents of the 15 : // files that it is being asked to clean. 16 : type NeedsFileContents interface { 17 : needsFileContents() 18 : } 19 : 20 : // DeleteCleaner deletes file. 21 : type DeleteCleaner struct{} 22 : 23 : // Clean removes file. 24 0 : func (DeleteCleaner) Clean(fs vfs.FS, fileType FileType, path string) error { 25 0 : return fs.Remove(path) 26 0 : } 27 : 28 0 : func (DeleteCleaner) String() string { 29 0 : return "delete" 30 0 : } 31 : 32 : // ArchiveCleaner archives file instead delete. 33 : type ArchiveCleaner struct{} 34 : 35 : var _ NeedsFileContents = ArchiveCleaner{} 36 : 37 : // Clean archives file. 38 : // 39 : // TODO(sumeer): for log files written to the secondary FS, the archiving will 40 : // also write to the secondary. We should consider archiving to the primary. 41 1 : func (ArchiveCleaner) Clean(fs vfs.FS, fileType FileType, path string) error { 42 1 : switch fileType { 43 1 : case FileTypeLog, FileTypeManifest, FileTypeTable: 44 1 : destDir := fs.PathJoin(fs.PathDir(path), "archive") 45 1 : 46 1 : if err := fs.MkdirAll(destDir, 0755); err != nil { 47 0 : return err 48 0 : } 49 : 50 1 : destPath := fs.PathJoin(destDir, fs.PathBase(path)) 51 1 : return fs.Rename(path, destPath) 52 : 53 1 : default: 54 1 : return fs.Remove(path) 55 : } 56 : } 57 : 58 1 : func (ArchiveCleaner) String() string { 59 1 : return "archive" 60 1 : } 61 : 62 0 : func (ArchiveCleaner) needsFileContents() { 63 0 : }