bbolt-rs 1.3.10

A Rust port of the Bolt database
Documentation
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use std::cell::RefCell;
use std::fmt::{Display, Formatter};
use std::rc::Rc;
use std::time::{Duration, Instant};

use anyhow::anyhow;
use byteorder::{BigEndian, ByteOrder};
use clap::{Parser, ValueEnum};
use rand::rngs::StdRng;
use rand::seq::SliceRandom;
use rand::{RngCore, SeedableRng};
use tempfile::Builder;

use bbolt_rs::{
  Bolt, BoltOptions, BucketApi, BucketRwApi, DbApi, DbRwAPI, Error, TxApi, TxRwRefApi,
};

#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Bench {
  #[arg(short, long, default_value_t = WriteMode::Seq)]
  write_mode: WriteMode,
  #[arg(short, long, default_value_t = ReadMode::Seq)]
  read_mode: ReadMode,
  #[arg(short, long, default_value_t = 1000)]
  count: u64,
  #[arg(short, long, default_value_t = 0)]
  batch_size: u64,
  #[arg(short, long, default_value_t = 8)]
  key_size: usize,
  #[arg(short, long, default_value_t = 32)]
  value_size: usize,
  #[arg(short, long, default_value_t = 0.5f64)]
  fill_percent: f64,
  #[arg(short, long)]
  mem_backend: bool,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
enum WriteMode {
  /// Sequential Write
  Seq,
  /// Random Write
  Rnd,
  /// Sequential Nested
  SeqNest,
  /// Random Nested
  RndNest,
}

impl Display for WriteMode {
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    let str = match self {
      WriteMode::Seq => "seq",
      WriteMode::Rnd => "rnd",
      WriteMode::SeqNest => "seq-nest",
      WriteMode::RndNest => "rnd-nest",
    };
    f.write_str(str)
  }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
enum ReadMode {
  /// Sequential Read
  Seq,
  /// Random Read
  Rnd,
}

impl Display for ReadMode {
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    let str = match self {
      ReadMode::Seq => "seq",
      ReadMode::Rnd => "rnd",
    };
    f.write_str(str)
  }
}

#[derive(Debug, Copy, Clone, Default)]
struct BenchResults {
  ops: u64,
  duration: Duration,
}

impl BenchResults {
  fn op_duration(&self) -> Duration {
    if self.ops == 0 {
      Duration::from_secs(0)
    } else {
      Duration::from_secs_f64(self.duration.as_secs_f64() / self.ops as f64)
    }
  }

  fn ops_per_second(&self) -> u64 {
    let op = self.op_duration();
    if op.is_zero() {
      0
    } else {
      (1.0f64 / op.as_secs_f64()) as u64
    }
  }
}

impl Display for BenchResults {
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    f.write_fmt(format_args!(
      "{:?}\t({:?}/op)\t({} op/sec)",
      self.duration,
      self.op_duration(),
      self.ops_per_second()
    ))
  }
}

struct NestedKey {
  bucket: Rc<[u8]>,
  key: Box<[u8]>,
}

static BENCH_BUCKET_NAME: &str = "bench";

fn main() -> bbolt_rs::Result<()> {
  let mut bench = Bench::parse();

  if bench.batch_size == 0 {
    bench.batch_size = bench.count;
  } else if bench.count % bench.batch_size != 0 {
    return Err(Error::Other(anyhow!(
      "number of iterations must be divisible by the batch size"
    )));
  }
  let (_tmp_file, mut db) = if bench.mem_backend {
    (None, BoltOptions::default().open_mem()?)
  } else {
    let tmp_file = Builder::new()
      .prefix("bbolt-rs-")
      .suffix(".db")
      .tempfile()?;

    let path = tmp_file.path().to_path_buf();
    (Some(tmp_file), BoltOptions::default().open(path)?)
  };

  let mut rng = StdRng::from_entropy();
  let mut write_results = BenchResults::default();
  let mut n_keys = run_writes(&mut db, &bench, &mut write_results, &mut rng)?;
  if let Some(keys) = n_keys.as_mut() {
    keys.shuffle(&mut rng);
  }

  let mut read_results = BenchResults::default();
  run_reads(&mut db, &bench, &mut read_results, &n_keys)?;
  println!("# Write\t{}", write_results);
  println!("# Read\t{}", read_results);
  Ok(())
}

fn run_reads(
  db: &mut Bolt, options: &Bench, read_results: &mut BenchResults, n_keys: &Option<Vec<NestedKey>>,
) -> bbolt_rs::Result<()> {
  let start = Instant::now();
  let ops = match (options.read_mode, options.write_mode) {
    (ReadMode::Seq, WriteMode::RndNest | WriteMode::SeqNest) => {
      run_reads_sequential_nested(db, options)?
    }
    (ReadMode::Rnd, WriteMode::RndNest | WriteMode::SeqNest) => {
      let keys = n_keys.as_ref().unwrap();
      run_reads_random_nested(db, options, keys)?
    }
    (ReadMode::Seq, _) => run_reads_sequential(db, options)?,
    (ReadMode::Rnd, _) => {
      let keys = n_keys.as_ref().unwrap();
      run_reads_random(db, options, keys)?
    }
  };

  read_results.ops = ops;
  read_results.duration = start.elapsed();
  Ok(())
}

fn run_writes(
  db: &mut Bolt, options: &Bench, write_results: &mut BenchResults, rng: &mut StdRng,
) -> bbolt_rs::Result<Option<Vec<NestedKey>>> {
  let start = Instant::now();
  let n_keys = match options.write_mode {
    WriteMode::Seq => {
      let mut i = 0u32;
      run_write_with_sources(db, options, move || {
        i += 1;
        i
      })?
    }
    WriteMode::Rnd => run_write_with_sources(db, options, || rng.next_u32())?,
    WriteMode::SeqNest => {
      let mut i = 0u32;
      run_write_nested_with_sources(db, options, move || {
        i += 1;
        i
      })?
    }
    WriteMode::RndNest => run_write_nested_with_sources(db, options, || rng.next_u32())?,
  };

  write_results.ops = options.count;
  write_results.duration = start.elapsed();
  Ok(n_keys)
}

fn run_write_with_sources<F>(
  db: &mut Bolt, options: &Bench, mut key_source: F,
) -> bbolt_rs::Result<Option<Vec<NestedKey>>>
where
  F: FnMut() -> u32,
{
  let mut n_keys = if options.read_mode == ReadMode::Rnd {
    Some(Vec::with_capacity(options.count as usize))
  } else {
    None
  };

  for _ in (0..options.count).step_by(options.batch_size as usize) {
    db.update(|mut tx| {
      let mut b = tx.create_bucket_if_not_exists(BENCH_BUCKET_NAME)?;
      let bucket = Rc::new([]);
      b.set_fill_percent(options.fill_percent);
      for _ in 0..options.batch_size {
        let mut key = vec![0u8; options.key_size];
        let value = vec![0u8; options.value_size];
        let k = key_source();
        BigEndian::write_u32(&mut key, k);
        b.put(&key, &value)?;
        if let Some(keys) = n_keys.as_mut() {
          keys.push(NestedKey {
            bucket: bucket.clone(),
            key: key.into(),
          })
        }
      }
      Ok(())
    })?
  }
  Ok(n_keys)
}

fn run_write_nested_with_sources<F>(
  db: &mut Bolt, options: &Bench, mut key_source: F,
) -> bbolt_rs::Result<Option<Vec<NestedKey>>>
where
  F: FnMut() -> u32,
{
  let mut n_keys = if options.read_mode == ReadMode::Rnd {
    Some(Vec::with_capacity(options.count as usize))
  } else {
    None
  };

  for _ in (0..options.count).step_by(options.batch_size as usize) {
    db.update(|mut tx| {
      let mut top = tx.create_bucket_if_not_exists(BENCH_BUCKET_NAME)?;
      top.set_fill_percent(options.fill_percent);
      let name = key_source().to_be_bytes();
      let bucket: Rc<[u8]> = Rc::from(name);
      let mut b = top.create_bucket_if_not_exists(&name)?;
      b.set_fill_percent(options.fill_percent);
      for _ in 0..options.batch_size {
        let mut key = vec![0u8; options.key_size];
        let value = vec![0u8; options.value_size];
        let k = key_source();
        BigEndian::write_u32(&mut key, k);
        b.put(&key, &value)?;
        if let Some(keys) = n_keys.as_mut() {
          keys.push(NestedKey {
            bucket: bucket.clone(),
            key: key.into(),
          })
        }
      }
      Ok(())
    })?
  }
  Ok(n_keys)
}

fn run_reads_sequential(db: &Bolt, options: &Bench) -> bbolt_rs::Result<u64> {
  let results = RefCell::new(0u64);
  db.view(|tx| {
    let mut result = results.borrow_mut();
    let t = Instant::now();
    loop {
      let mut count = 0;
      let b = tx.bucket(BENCH_BUCKET_NAME).unwrap();
      for (_k, _v) in b.iter_entries() {
        count += 1;
      }

      if options.write_mode == WriteMode::Seq && count != options.count {
        return Err(Error::Other(anyhow!(
          "read seq: iter mismatch: expected {}, got {}",
          options.count,
          count
        )));
      }
      *result += count;

      if t.elapsed() > Duration::from_secs(1) {
        break;
      }
    }
    Ok(())
  })?;
  Ok(results.take())
}

fn run_reads_random(db: &Bolt, options: &Bench, keys: &[NestedKey]) -> bbolt_rs::Result<u64> {
  let results = RefCell::new(0u64);
  db.view(|tx| {
    let mut result = results.borrow_mut();
    let t = Instant::now();
    loop {
      let mut count = 0;
      let b = tx.bucket(BENCH_BUCKET_NAME).unwrap();
      for entry in keys.iter() {
        let v = b.get(&entry.key);
        v.ok_or_else(|| anyhow!("invalid value"))?;
        count += 1;
      }
      if options.write_mode == WriteMode::Seq && count != options.count {
        return Err(Error::Other(anyhow!(
          "read seq: iter mismatch: expected {}, got {}",
          options.count,
          count
        )));
      }
      *result += count;

      if t.elapsed() > Duration::from_secs(1) {
        break;
      }
    }
    Ok(())
  })?;
  Ok(results.take())
}

fn run_reads_sequential_nested(db: &Bolt, options: &Bench) -> bbolt_rs::Result<u64> {
  let results = RefCell::new(0u64);
  db.view(|tx| {
    let mut result = results.borrow_mut();
    let t = Instant::now();
    loop {
      let top = tx.bucket(BENCH_BUCKET_NAME).unwrap();
      let mut count = 0u64;
      for (_k, b) in top.iter_buckets() {
        for (_k, _v) in b.iter_entries() {
          count += 1;
        }
      }

      if options.write_mode == WriteMode::Seq && count != options.count {
        return Err(Error::Other(anyhow!(
          "read seq: iter mismatch: expected {}, got {}",
          options.count,
          count
        )));
      }
      *result += count;

      if t.elapsed() > Duration::from_secs(1) {
        break;
      }
    }
    Ok(())
  })?;
  Ok(results.take())
}

fn run_reads_random_nested(
  db: &Bolt, options: &Bench, keys: &[NestedKey],
) -> bbolt_rs::Result<u64> {
  let results = RefCell::new(0u64);
  db.view(|tx| {
    let mut result = results.borrow_mut();
    let t = Instant::now();
    loop {
      let top = tx.bucket(BENCH_BUCKET_NAME).unwrap();
      let mut count = 0u64;
      for entry in keys.iter() {
        let b = top.bucket(&entry.bucket).unwrap();
        let v = b.get(&entry.key);
        v.ok_or_else(|| anyhow!("invalid value"))?;
        count += 1;
      }

      if options.write_mode == WriteMode::Seq && count != options.count {
        return Err(Error::Other(anyhow!(
          "read seq: iter mismatch: expected {}, got {}",
          options.count,
          count
        )));
      }
      *result += count;

      if t.elapsed() > Duration::from_secs(1) {
        break;
      }
    }
    Ok(())
  })?;
  Ok(results.take())
}