github.com/cockroachdb/pebble@v1.1.1-0.20240513155919-3622ade60459/internal/base/cleaner.go (about)

     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  func (DeleteCleaner) Clean(fs vfs.FS, fileType FileType, path string) error {
    25  	return fs.Remove(path)
    26  }
    27  
    28  func (DeleteCleaner) String() string {
    29  	return "delete"
    30  }
    31  
    32  // ArchiveCleaner archives file instead delete.
    33  type ArchiveCleaner struct{}
    34  
    35  var _ NeedsFileContents = ArchiveCleaner{}
    36  
    37  // Clean archives file.
    38  func (ArchiveCleaner) Clean(fs vfs.FS, fileType FileType, path string) error {
    39  	switch fileType {
    40  	case FileTypeLog, FileTypeManifest, FileTypeTable:
    41  		destDir := fs.PathJoin(fs.PathDir(path), "archive")
    42  
    43  		if err := fs.MkdirAll(destDir, 0755); err != nil {
    44  			return err
    45  		}
    46  
    47  		destPath := fs.PathJoin(destDir, fs.PathBase(path))
    48  		return fs.Rename(path, destPath)
    49  
    50  	default:
    51  		return fs.Remove(path)
    52  	}
    53  }
    54  
    55  func (ArchiveCleaner) String() string {
    56  	return "archive"
    57  }
    58  
    59  func (ArchiveCleaner) needsFileContents() {
    60  }