Skip to content

Commit

Permalink
chore(rocks_db): move ShallowTempDir to benches crate
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc committed Sep 5, 2024
1 parent 6ef1d58 commit a843f6a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 42 deletions.
10 changes: 5 additions & 5 deletions benches/benches/vm_set/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ use fuel_core::{
GenesisDatabase,
},
service::Config,
state::{
historical_rocksdb::HistoricalRocksDB,
rocks_db::ShallowTempDir,
},
state::historical_rocksdb::HistoricalRocksDB,
};
use fuel_core_benches::{
utils::ShallowTempDir,
*,
};
use fuel_core_benches::*;
use fuel_core_storage::{
tables::FuelBlocks,
transactional::{
Expand Down
1 change: 1 addition & 0 deletions benches/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use std::{

pub mod default_gas_costs;
pub mod import;
pub mod utils;

pub use fuel_core_storage::vm_storage::VmStorage;
pub use rand::Rng;
Expand Down
40 changes: 40 additions & 0 deletions benches/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use rand::RngCore;
use std::{
env,
path::PathBuf,
};

/// Reimplementation of `tempdir::TempDir` that allows creating a new
/// instance without actually creating a new directory on the filesystem.
/// This is needed since rocksdb requires empty directory for checkpoints.
pub struct ShallowTempDir {
path: PathBuf,
}

impl Default for ShallowTempDir {
fn default() -> Self {
Self::new()
}
}

impl ShallowTempDir {
/// Creates a random directory.
pub fn new() -> Self {
let mut rng = rand::thread_rng();
let mut path = env::temp_dir();
path.push(format!("fuel-core-shallow-{}", rng.next_u64()));
Self { path }
}

/// Returns the path of the directory.
pub fn path(&self) -> &PathBuf {
&self.path
}
}

impl Drop for ShallowTempDir {
fn drop(&mut self) {
// Ignore errors
let _ = std::fs::remove_dir_all(&self.path);
}
}
37 changes: 0 additions & 37 deletions crates/fuel-core/src/state/rocks_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ use fuel_core_storage::{
Result as StorageResult,
};
use itertools::Itertools;
use rand::RngCore;
use rocksdb::{
BlockBasedOptions,
BoundColumnFamily,
Expand All @@ -55,7 +54,6 @@ use rocksdb::{
use std::{
cmp,
collections::BTreeMap,
env,
fmt,
fmt::Formatter,
iter,
Expand All @@ -69,41 +67,6 @@ use tempfile::TempDir;

type DB = DBWithThreadMode<MultiThreaded>;

/// Reimplementation of `tempdir::TempDir` that allows creating a new
/// instance without actually creating a new directory on the filesystem.
/// This is needed since rocksdb requires empty directory for checkpoints.
pub struct ShallowTempDir {
path: PathBuf,
}

impl Default for ShallowTempDir {
fn default() -> Self {
Self::new()
}
}

impl ShallowTempDir {
/// Creates a random directory.
pub fn new() -> Self {
let mut rng = rand::thread_rng();
let mut path = env::temp_dir();
path.push(format!("fuel-core-shallow-{}", rng.next_u64()));
Self { path }
}

/// Returns the path of the directory.
pub fn path(&self) -> &PathBuf {
&self.path
}
}

impl Drop for ShallowTempDir {
fn drop(&mut self) {
// Ignore errors
let _ = std::fs::remove_dir_all(&self.path);
}
}

type DropFn = Box<dyn FnOnce() + Send + Sync>;
#[derive(Default)]
struct DropResources {
Expand Down

0 comments on commit a843f6a

Please sign in to comment.