Skip to content

Commit

Permalink
chore: refine file cache management in genesis data import
Browse files Browse the repository at this point in the history
FileCacheManager has been integrated into the genesis_utxo.rs and genesis_ord.rs files. In the process of importing data, this allows for a detailed control over the memory cache, potentially enhancing performance. The respective bytes read calculations have also been optimized.

TODO: read file ahead
  • Loading branch information
popcnt1 committed Jul 7, 2024
1 parent ba898a8 commit 4508a0a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
3 changes: 1 addition & 2 deletions crates/rooch/src/commands/statedb/commands/genesis_ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,12 +466,12 @@ fn produce_ord_updates(tx: SyncSender<BatchUpdatesOrd>, input: PathBuf, batch_si

for line in reader.by_ref().lines().take(batch_size) {
let line = line.unwrap();
bytes_read += line.len() as u64 + 1; // Add line.len() + 1, assuming that the line terminator is '\n'

if is_title_line {
is_title_line = false;
if line.starts_with("# export at") {
// skip block height info
bytes_read += line.len() as u64 + 1; // Add line.len() + 1, assuming that the line terminator is '\n'
continue;
}
}
Expand All @@ -488,7 +488,6 @@ fn produce_ord_updates(tx: SyncSender<BatchUpdatesOrd>, input: PathBuf, batch_si
let (key2, state2) = gen_inscription_ids_update(index, inscription_id);
updates.inscription_ids_updates.put(key2, state2);
index += 1;
bytes_read += line.len() as u64 + 1;
}
let _ = file_cache_mgr.drop_cache_range(cache_drop_offset, bytes_read);
cache_drop_offset += bytes_read;
Expand Down
9 changes: 9 additions & 0 deletions crates/rooch/src/commands/statedb/commands/genesis_utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use moveos_types::moveos_std::simple_multimap::{Element, SimpleMultiMap};
use moveos_types::startup_info::StartupInfo;
use moveos_types::state::{KeyState, MoveState, State};
use redb::{Database, ReadOnlyTable};
use rooch_common::fs::file_cache::FileCacheManager;
use rooch_config::{RoochOpt, R_OPT_NET_HELP};
use rooch_db::RoochDB;
use rooch_types::address::BitcoinAddress;
Expand Down Expand Up @@ -399,6 +400,9 @@ pub fn produce_utxo_updates(
batch_size: usize,
utxo_ord_map_db: Option<Arc<Database>>,
) {
let file_cache_mgr = FileCacheManager::new(input.clone()).unwrap();
let mut cache_drop_offset: u64 = 0;

let mut csv_reader = BufReader::with_capacity(8 * 1024 * 1024, File::open(input).unwrap());
let mut is_title_line = true;
let mut address_mapping_checker = HashMap::new();
Expand All @@ -410,12 +414,15 @@ pub fn produce_utxo_updates(
}
};
loop {
let mut bytes_read = 0;

let mut updates = BatchUpdates {
utxo_updates: UpdateSet::new(),
rooch_to_bitcoin_mapping_updates: UpdateSet::new(),
};
for line in csv_reader.by_ref().lines().take(batch_size) {
let line = line.unwrap();
bytes_read += line.len() as u64 + 1; // Add line.len() + 1, assuming that the line terminator is '\n'

if is_title_line {
is_title_line = false;
Expand Down Expand Up @@ -447,6 +454,8 @@ pub fn produce_utxo_updates(
}
}
}
let _ = file_cache_mgr.drop_cache_range(cache_drop_offset, bytes_read);
cache_drop_offset += bytes_read;
if updates.utxo_updates.is_empty() {
break;
}
Expand Down

0 comments on commit 4508a0a

Please sign in to comment.