Skip to content

Commit

Permalink
Fix fresh name issue
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobVanGeffen committed Dec 19, 2024
1 parent 473f9ae commit b795e7e
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions examples/batch_solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const CONFIG_FILE_EXTENSION: &str = ".cfg";
// TODO make this an option
const TARGET_STORAGE_MODE: BoardState = BoardState::Turn;

fn get_fresh_name(dir: &PathBuf) -> Result<String, std::io::Error> {
Ok(format!("solve{}", std::fs::read_dir(dir)?.count()))
fn get_fresh_name(metadata: &SolveDBMetadata) -> String {
format!("solve{}", metadata.nonce)
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand All @@ -32,6 +32,7 @@ struct SolveDBMetadata {
solves: HashMap<String, Vec<SolveMetadata>>,
path: PathBuf,
configs: Vec<String>,
nonce: usize,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -79,7 +80,7 @@ struct Args {

/// Directory representing the solve DB
#[arg(short, long, default_value = ".")]
dir: String,
dir: PathBuf,

/// Max number of iterations to run
#[arg(short = 'n', long, default_value = "1000")]
Expand Down Expand Up @@ -196,6 +197,18 @@ fn canonicalize_board(board: &str) -> Result<String, String> {
.join(""))
}

/// Persist the metadata
/// # Panics
/// Panics if the metadata cannot be serialized.
/// Also panics if the metadata file cannot be written to.
fn persist_metadata(metadata: &SolveDBMetadata) {
std::fs::write(
&metadata.path.join(METADATA_FILENAME),
serde_json::to_string_pretty(&metadata).expect("Could not serialize metadata"),
)
.expect("Couldn't write metadata file");
}

fn main() -> Result<(), String> {
let args = Args::parse();
let dir = PathBuf::from(args.dir);
Expand All @@ -214,25 +227,32 @@ fn main() -> Result<(), String> {
*/
/********************************************************************************************/

// Make a new name for this solve
let solve_name = get_fresh_name(&dir).expect("Can't read from SDB directory");

// Get the solve DB metadata, if it exists.
// Otherwise, create the solve DB
let metadata_path = dir.join(METADATA_FILENAME);
let mut metadata = if metadata_path
.try_exists()
.map_err(|e| format!("Error checking SDB metadata file path existence: {e:?}"))?
{
serde_read(&metadata_path)?
let mut new_metadata: SolveDBMetadata = serde_read(&metadata_path)?;

// Update the nonce, and write out the metadata w/ new nonce
new_metadata.nonce += 1;
persist_metadata(&new_metadata);

new_metadata
} else {
SolveDBMetadata {
solves: HashMap::new(),
path: dir.clone().canonicalize().unwrap(),
configs: Vec::new(),
nonce: 0,
}
};

// Make a new name for this solve
let solve_name = get_fresh_name(&metadata);

// Load config
let config_read_path = args.config;
let mut config: SolveConfig = serde_read(&config_read_path)?;
Expand Down Expand Up @@ -292,11 +312,7 @@ fn main() -> Result<(), String> {

// Add the config file to the SDB metadata
metadata.configs.push(file_name.clone());
std::fs::write(
&metadata_path,
serde_json::to_string_pretty(&metadata).expect("Could not serialize metadata"),
)
.expect("Couldn't write metadata file");
persist_metadata(&metadata);

file_name
};
Expand Down Expand Up @@ -405,11 +421,7 @@ fn main() -> Result<(), String> {
.entry(board)
.or_insert(Vec::new())
.push(board_metadata);
std::fs::write(
&metadata_path,
serde_json::to_string_pretty(&metadata).expect("Could not serialize metadata"),
)
.expect("Couldn't write metadata file");
persist_metadata(&metadata);
}

Ok(())
Expand Down

0 comments on commit b795e7e

Please sign in to comment.