Skip to content

Commit

Permalink
ensured we can have multi user concurrent access to the rocksdb
Browse files Browse the repository at this point in the history
  • Loading branch information
Gary Mawdsley committed Nov 20, 2024
1 parent ca773f4 commit 4c6d19f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
9 changes: 8 additions & 1 deletion src/audit_adapters/merkle_audit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ impl IrrefutableAudit for MerkleBasedAuditSystem {
println!("Initialising Merkle based audit system");
let (sender, receiver) = tokio_mpsc::channel(100);

let db = Arc::new(DB::open_default("../RocksDBs/audit_db")?);
let mut opts = rocksdb::Options::default();
opts.create_if_missing(true);
opts.set_max_background_jobs(4);
opts.set_use_fsync(true);
opts.set_keep_log_file_num(10);
opts.set_allow_concurrent_memtable_write(true);

let db = Arc::new(DB::open(&opts, "../RocksDBs/audit_db")?);

let audit = Arc::new(MerkleBasedAuditSystem { sender, db });
MerkleBasedAuditSystem::spawn_event_handler(audit.clone(), receiver)?;
Expand Down
16 changes: 7 additions & 9 deletions src/bin/audit_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ struct StoredEvent {
}

fn main() -> Result<(), Box<dyn Error>> {
// Open the same DB path as used in MerkleBasedAuditSystem
let db = DB::open_default("../RocksDBs/audit_db")?;
let mut opts = rocksdb::Options::default();
opts.set_max_background_jobs(4);
opts.set_allow_concurrent_memtable_write(true);
// Open in read-only mode since we're just reading
let db = DB::open_for_read_only(&opts, "../RocksDBs/audit_db", false)?;

println!("Reading audit events from database...\n");

Expand All @@ -26,16 +29,11 @@ fn main() -> Result<(), Box<dyn Error>> {
let key_str = String::from_utf8(key.to_vec())?;

let stored_event: StoredEvent = bincode::deserialize(&value)?;
let timestamp = DateTime::<Utc>::from_timestamp(stored_event.timestamp as i64, 0)
let _timestamp = DateTime::<Utc>::from_timestamp(stored_event.timestamp as i64, 0)
.unwrap()
.to_rfc3339();

println!("Event Key: {}", key_str);
println!("Timestamp: {}", timestamp);
println!("Event Type: {}", stored_event.event.event_type);
println!("File Path: {}", stored_event.event.file_path);
println!("Creation Time: {}", stored_event.event.creation_time);
println!("-------------------\n");
println!("{:>12.12}, {}", stored_event.event.event_type.to_uppercase(), key_str.replace(":", " :: "));
}

Ok(())
Expand Down

0 comments on commit 4c6d19f

Please sign in to comment.