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 1 : func (DeleteCleaner) Clean(fs vfs.FS, fileType FileType, path string) error { 25 1 : return fs.Remove(path) 26 1 : } 27 : 28 1 : func (DeleteCleaner) String() string { 29 1 : return "delete" 30 1 : } 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 2 : func (ArchiveCleaner) Clean(fs vfs.FS, fileType FileType, path string) error { 42 2 : switch fileType { 43 2 : case FileTypeLog, FileTypeManifest, FileTypeTable: 44 2 : destDir := fs.PathJoin(fs.PathDir(path), "archive") 45 2 : 46 2 : if err := fs.MkdirAll(destDir, 0755); err != nil { 47 0 : return err 48 0 : } 49 : 50 2 : destPath := fs.PathJoin(destDir, fs.PathBase(path)) 51 2 : return fs.Rename(path, destPath) 52 : 53 2 : default: 54 2 : return fs.Remove(path) 55 : } 56 : } 57 : 58 2 : func (ArchiveCleaner) String() string { 59 2 : return "archive" 60 2 : } 61 : 62 0 : func (ArchiveCleaner) needsFileContents() { 63 0 : }