IBAMR  IBAMR version 0.19.
Public Member Functions | Private Types | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
IBTK::RestartCleaner Class Reference

Class RestartCleaner provides functionality to manage restart directories. More...

#include <ibtk/RestartCleaner.h>

Public Member Functions

 RestartCleaner (const std::string &object_name, SAMRAI::tbox::Pointer< SAMRAI::tbox::Database > input_db)
 Constructor using SAMRAI Database configuration. More...
 
 ~RestartCleaner ()=default
 Destructor. More...
 
void cleanup ()
 Scan and cleanup old restart directories. More...
 
std::vector< intgetAvailableRestartRestoreNumbers () const
 Get available restart restore numbers. More...
 

Private Types

enum  CleanupStrategy { CleanupStrategy::KEEP_RECENT_N }
 Internal strategy enumeration. More...
 

Private Member Functions

 RestartCleaner ()=delete
 
 RestartCleaner (const RestartCleaner &from)=delete
 
RestartCleaneroperator= (const RestartCleaner &that)=delete
 
CleanupStrategy parseStrategy (const std::string &strategy_str) const
 Parse strategy string to enum. More...
 
void executeStrategy () const
 Execute cleanup based on current strategy. More...
 
std::optional< intparseRestartRestoreNumber (const std::string &dirname) const
 Parse restart restore number from directory name. More...
 
std::vector< std::filesystem::path > getAllRestartDirs (const std::string &restart_dir) const
 Get all restart directories from the base path. More...
 
void keepRecentN () const
 KEEP_RECENT_N strategy implementation. More...
 

Private Attributes

std::string d_object_name
 
std::string d_restart_base_path
 
CleanupStrategy d_strategy
 
int d_keep_restart_count
 
bool d_enable_logging
 
bool d_dry_run
 

Static Private Attributes

static const std::regex s_restart_dir_pattern
 

Detailed Description

This class provides methods to automatically clean up old restart directories while keeping the most recent ones. It is designed to work with IBAMR's standard restart directory naming convention and integrates with the SAMRAI Database configuration system.

The main functionalities include:

  1. Scan a specified directory for subdirectories matching the pattern "restore.NNNNNN..."
  2. Parse restart restore numbers from these directory names
  3. Sort directories based on restart restore numbers
  4. Keep the N most recent directories and delete the rest
Note
This class assumes restart directories follow the naming pattern "restore.NNNNNN..." where the restart restore number is zero-padded to a minimum of 6 digits by IBAMR. For example: restore.000005, restore.123456, restore.1000000 (7 digits). Restart restore numbers with fewer than 6 digits are always zero-padded by IBAMR.
This is a stateless utility class. Each cleanup operation performs a complete scan of the target directory and makes decisions based on the current filesystem state. No internal state is maintained between operations.
In parallel environments, file operations are performed only on the master processor (rank 0) to ensure consistency and avoid race conditions.

Supported cleanup strategies:

Sample usage:

// Configuration-driven usage with SAMRAI Database
Pointer<Database> restart_db = input_db->getDatabase("RestartCleaner");
RestartCleaner cleaner("RestartCleaner", restart_db);
cleaner.cleanup(); // Use dry_run=true in config to preview without deleting
// Check available restart restore numbers
auto restore_numbers = cleaner.getAvailableRestoreNumbers();

Member Enumeration Documentation

◆ CleanupStrategy

Enumerator
KEEP_RECENT_N 

Constructor & Destructor Documentation

◆ RestartCleaner() [1/3]

IBTK::RestartCleaner::RestartCleaner ( const std::string &  object_name,
SAMRAI::tbox::Pointer< SAMRAI::tbox::Database input_db 
)
explicit

This is the constructor for RestartCleaner and integrates with IBAMR's standard configuration system.

The input database is searched for the following keys:

  • 'restart_directory': string (required)
  • 'keep_recent_files': int (default: 5)
  • 'cleanup_strategy': string (default: "KEEP_RECENT_N")
  • 'enable_logging': bool (default: true)
  • 'dry_run': bool (default: false) - when true, only logs actions without deleting
Parameters
object_nameName for this object (used in error messages and logging)
input_dbDatabase containing configuration parameters

◆ ~RestartCleaner()

IBTK::RestartCleaner::~RestartCleaner ( )
default

◆ RestartCleaner() [2/3]

IBTK::RestartCleaner::RestartCleaner ( )
privatedelete

◆ RestartCleaner() [3/3]

IBTK::RestartCleaner::RestartCleaner ( const RestartCleaner from)
privatedelete

Member Function Documentation

◆ cleanup()

void IBTK::RestartCleaner::cleanup ( )

This method performs the complete cleanup process:

  1. Scans the base directory for restart folders
  2. Parses restart restore numbers from directory names
  3. Sorts directories by restart restore number
  4. Keeps the N most recent directories and deletes the rest

In parallel environments, file operations are performed only on the master processor (rank 0) to ensure consistency.

Note
Call cleanup() after each restart save to maintain only the most recent directories.
When dry_run is enabled, this method only logs what would be deleted without actually removing any files.

◆ getAvailableRestartRestoreNumbers()

std::vector<int> IBTK::RestartCleaner::getAvailableRestartRestoreNumbers ( ) const

Scans the restart directory and returns restart restore numbers for all currently existing restore directories, sorted in ascending order.

Returns
Vector of restart restore numbers for available restore directories, sorted in ascending order

◆ operator=()

RestartCleaner& IBTK::RestartCleaner::operator= ( const RestartCleaner that)
privatedelete

◆ parseStrategy()

CleanupStrategy IBTK::RestartCleaner::parseStrategy ( const std::string &  strategy_str) const
private

◆ executeStrategy()

void IBTK::RestartCleaner::executeStrategy ( ) const
private

◆ parseRestartRestoreNumber()

std::optional<int> IBTK::RestartCleaner::parseRestartRestoreNumber ( const std::string &  dirname) const
private

Extracts the restart restore number from directory names following the pattern "restore.NNNNNN..." where the restart restore number is zero-padded to at least 6 digits.

Parameters
dirnameDirectory name to parse
Returns
Restart restore number if parsing succeeds, std::nullopt otherwise

◆ getAllRestartDirs()

std::vector<std::filesystem::path> IBTK::RestartCleaner::getAllRestartDirs ( const std::string &  restart_dir) const
private

Scans the restart base directory and returns all subdirectories that match the restart naming pattern.

Parameters
restart_dirBase directory to scan
Returns
Vector of filesystem paths to valid restart directories

◆ keepRecentN()

void IBTK::RestartCleaner::keepRecentN ( ) const
private

Member Data Documentation

◆ d_object_name

std::string IBTK::RestartCleaner::d_object_name
private

◆ d_restart_base_path

std::string IBTK::RestartCleaner::d_restart_base_path
private

◆ d_strategy

CleanupStrategy IBTK::RestartCleaner::d_strategy
private

◆ d_keep_restart_count

int IBTK::RestartCleaner::d_keep_restart_count
private

◆ d_enable_logging

bool IBTK::RestartCleaner::d_enable_logging
private

◆ d_dry_run

bool IBTK::RestartCleaner::d_dry_run
private

◆ s_restart_dir_pattern

const std::regex IBTK::RestartCleaner::s_restart_dir_pattern
staticprivate

The documentation for this class was generated from the following file:
IBTK::RestartCleaner::RestartCleaner
RestartCleaner()=delete