From 84ac3652415b662aae9580c008a5aa996d58c9f4 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Sat, 3 May 2025 00:15:02 +0300 Subject: Rename to AutereDB --- autere_db/benches/utils.rs | 122 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 autere_db/benches/utils.rs (limited to 'autere_db/benches/utils.rs') diff --git a/autere_db/benches/utils.rs b/autere_db/benches/utils.rs new file mode 100644 index 0000000..9b0a0ea --- /dev/null +++ b/autere_db/benches/utils.rs @@ -0,0 +1,122 @@ +use autere_db::*; +use rand::distributions::Alphanumeric; +use rand::Rng; +use std::fmt::Debug; + +#[derive(Eq, PartialEq, Clone, Debug)] +pub enum Field { + Id, + Name, + Data, +} + +#[derive(PartialEq, Eq, Debug, Clone)] +pub struct Inst { + pub id: i64, + pub name: String, + pub data: Vec, +} + +impl AsRef for Field { + fn as_ref(&self) -> &str { + match self { + Field::Id => "id", + Field::Name => "name", + Field::Data => "data", + } + } +} + +impl Into for Field { + fn into(self) -> String { + self.as_ref().to_string() + } +} + +impl From for Record { + fn from(inst: Inst) -> Self { + vec![ + Value::Int(inst.id), + Value::String(inst.name), + Value::Bytes(inst.data), + ] + .into() + } +} + +impl From for Inst { + fn from(record: Record) -> Self { + let mut it = record.into_iter(); + Inst { + id: match it.next().unwrap() { + Value::Int(id) => id, + _ => panic!("Expected Int"), + }, + name: match it.next().unwrap() { + Value::String(name) => name, + _ => panic!("Expected String"), + }, + data: match it.next().unwrap() { + Value::Bytes(data) => data, + _ => panic!("Expected Bytes"), + }, + } + } +} + +impl Inst { + pub fn fields() -> Vec { + vec![Field::Id, Field::Name, Field::Data] + } + pub fn primary_key() -> Field { + Field::Id + } + pub fn secondary_keys() -> Vec { + vec![Field::Name] + } +} + +// Function to generate a random integer +pub fn random_int(from: i64, to: i64) -> i64 { + let mut rng = rand::thread_rng(); + rng.gen_range(from..to) +} + +// Function to generate a random string +pub fn random_string(len: usize) -> String { + let mut rng = rand::thread_rng(); + (0..len).map(|_| rng.sample(Alphanumeric) as char).collect() +} + +// Function to generate random bytes +pub fn random_bytes(len: usize) -> Vec { + let mut rng = rand::thread_rng(); + (0..len).map(|_| rng.gen()).collect() +} + +// Function to generate a random Inst +pub fn random_inst(from_id: i64, to_id: i64) -> Inst { + Inst { + id: random_int(from_id, to_id), // Random int value between 0..1000 + name: random_string(5), // Random string of length 5 + data: random_bytes(10), // Random bytes of length 10 + } +} + +pub fn prefill_db( + db: &mut DB, + insts: &mut Vec, + n_records: usize, + compact: bool, +) -> DBResult<()> { + for i in 0..(n_records - insts.len()) { + let inst = random_inst(0, n_records as i64); + insts.push(inst.clone()); + db.upsert(inst)?; + if i % 1000 == 0 && compact { + db.do_maintenance_tasks()?; + } + } + + Ok(()) +} -- cgit v1.3