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