Debian Codegraph @

third_party/rust/rkv/src/bin/rand.rs

raw
1 // Copyright 2018-2019 Mozilla
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4 // this file except in compliance with the License. You may obtain a copy of the
5 // License at http://www.apache.org/licenses/LICENSE-2.0
6 // Unless required by applicable law or agreed to in writing, software distributed
7 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
9 // specific language governing permissions and limitations under the License.
10
11 //! A command-line utility to create an LMDB environment containing random data.
12 //! It requires one flag, `-s path/to/environment`, which specifies the location
13 //! where the tool should create the environment. Optionally, you may specify
14 //! the number of key/value pairs to create via the `-n <number>` flag
15 //! (for which the default value is 50).
16
17 use std::{env::args, fs, fs::File, io::Read, path::Path};
18
19 use rkv::{
20 backend::{BackendEnvironmentBuilder, Lmdb},
21 Rkv, StoreOptions, Value,
22 };
23
24 fn main() {
25 let mut args = args();
26 let mut database = None;
27 let mut path = None;
28 let mut num_pairs = 50;
29
30 // The first arg is the name of the program, which we can ignore.
31 args.next();
32
33 while let Some(arg) = args.next() {
34 if &arg[0..1] == "-" {
35 match &arg[1..] {
36 "s" => {
37 database = match args.next() {
38 None => panic!("-s must be followed by database arg"),
39 Some(str) => Some(str),
40 };
41 }
42 "n" => {
43 num_pairs = match args.next() {
44 None => panic!("-s must be followed by number of pairs"),
45 Some(str) => str.parse().expect("number"),
46 };
47 }
48 str => panic!("arg -{} not recognized", str),
49 }
50 } else {
51 if path.is_some() {
52 panic!("must provide only one path to the LMDB environment");
53 }
54 path = Some(arg);
55 }
56 }
57
58 if path.is_none() {
59 panic!("must provide a path to the LMDB environment");
60 }
61
62 let path = path.unwrap();
63 fs::create_dir_all(&path).expect("dir created");
64
65 let mut builder = Rkv::environment_builder::<Lmdb>();
66 builder.set_max_dbs(2);
67 // Allocate enough map to accommodate the largest random collection.
68 // We currently do this by allocating twice the maximum possible size
69 // of the pairs (assuming maximum key and value sizes).
70 builder.set_map_size((511 + 65535) * num_pairs * 2);
71 let rkv = Rkv::from_builder(Path::new(&path), builder).expect("Rkv");
72 let store = rkv
73 .open_single(database.as_deref(), StoreOptions::create())
74 .expect("opened");
75 let mut writer = rkv.write().expect("writer");
76
77 // Generate random values for the number of keys and key/value lengths.
78 // On Linux, "Just use /dev/urandom!" <https://www.2uo.de/myths-about-urandom/>.
79 // On macOS it doesn't matter (/dev/random and /dev/urandom are identical).
80 let mut random = File::open("/dev/urandom").unwrap();
81 let mut nums = [0u8; 4];
82 random.read_exact(&mut nums).unwrap();
83
84 // Generate 0–255 pairs.
85 for _ in 0..num_pairs {
86 // Generate key and value lengths. The key must be 1–511 bytes long.
87 // The value length can be 0 and is essentially unbounded; we generate
88 // value lengths of 0–0xffff (65535).
89 // NB: the modulus method for generating a random number within a range
90 // introduces distribution skew, but we don't need it to be perfect.
91 let key_len = ((u16::from(nums[0]) + (u16::from(nums[1]) << 8)) % 511 + 1) as usize;
92 let value_len = (u16::from(nums[2]) + (u16::from(nums[3]) << 8)) as usize;
93
94 let mut key: Vec<u8> = vec![0; key_len];
95 random.read_exact(&mut key[0..key_len]).unwrap();
96
97 let mut value: Vec<u8> = vec![0; value_len];
98 random.read_exact(&mut value[0..value_len]).unwrap();
99
100 store
101 .put(&mut writer, key, &Value::Blob(&value))
102 .expect("wrote");
103 }
104
105 writer.commit().expect("committed");
106 }