aboutsummaryrefslogtreecommitdiffstats
path: root/log_db/benches/utils.rs
blob: d25a5afb12c13557113e4c89d4da81984233327e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use log_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<u8>,
}

impl AsRef<str> for Field {
    fn as_ref(&self) -> &str {
        match self {
            Field::Id => "id",
            Field::Name => "name",
            Field::Data => "data",
        }
    }
}

impl Into<String> for Field {
    fn into(self) -> String {
        self.as_ref().to_string()
    }
}

impl Inst {
    pub fn fields() -> Vec<Field> {
        vec![Field::Id, Field::Name, Field::Data]
    }
    pub fn primary_key() -> Field {
        Field::Id
    }
    pub fn secondary_keys() -> Vec<Field> {
        vec![Field::Name]
    }

    pub fn into_record(self) -> Vec<Value> {
        vec![
            Value::Int(self.id),
            Value::String(self.name),
            Value::Bytes(self.data),
        ]
    }
    pub fn from_record(record: Vec<Value>) -> 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"),
            },
        }
    }
}

// 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<u8> {
    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<Inst>,
    insts: &mut Vec<Inst>,
    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(())
}