Debian Codegraph @

third_party/rust/rkv/tests/multi-integer-store.rs

raw
1 // Copyright 2018 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 #![cfg(all(feature = "db-dup-sort", feature = "db-int-key"))]
11 #![allow(clippy::many_single_char_names)]
12
13 use std::fs;
14
15 use serde_derive::Serialize;
16 use tempfile::Builder;
17
18 use rkv::{backend::SafeMode, PrimitiveInt, Rkv, StoreOptions, Value};
19
20 #[test]
21 fn test_multi_integer_keys() {
22 let root = Builder::new()
23 .prefix("test_integer_keys")
24 .tempdir()
25 .expect("tempdir");
26 fs::create_dir_all(root.path()).expect("dir created");
27
28 let k = Rkv::new::<SafeMode>(root.path()).expect("new succeeded");
29 let s = k
30 .open_multi_integer("s", StoreOptions::create())
31 .expect("open");
32
33 macro_rules! test_integer_keys {
34 ($store:expr, $key:expr) => {{
35 let mut writer = k.write().expect("writer");
36
37 $store
38 .put(&mut writer, $key, &Value::Str("hello1"))
39 .expect("write");
40 $store
41 .put(&mut writer, $key, &Value::Str("hello2"))
42 .expect("write");
43 $store
44 .put(&mut writer, $key, &Value::Str("hello3"))
45 .expect("write");
46 let vals = $store
47 .get(&writer, $key)
48 .expect("read")
49 .map(|result| result.expect("ok"))
50 .map(|(_, v)| v)
51 .collect::<Vec<Value>>();
52 assert_eq!(
53 vals,
54 vec![
55 Value::Str("hello1"),
56 Value::Str("hello2"),
57 Value::Str("hello3")
58 ]
59 );
60 writer.commit().expect("committed");
61
62 let reader = k.read().expect("reader");
63 let vals = $store
64 .get(&reader, $key)
65 .expect("read")
66 .map(|result| result.expect("ok"))
67 .map(|(_, v)| v)
68 .collect::<Vec<Value>>();
69 assert_eq!(
70 vals,
71 vec![
72 Value::Str("hello1"),
73 Value::Str("hello2"),
74 Value::Str("hello3")
75 ]
76 );
77 }};
78 }
79
80 // The integer module provides only the u32 integer key variant
81 // of IntegerStore, so we can use it without further ado.
82 test_integer_keys!(s, std::u32::MIN);
83 test_integer_keys!(s, std::u32::MAX);
84
85 // If you want to use another integer key variant, you need to implement
86 // a newtype, implement PrimitiveInt, and implement or derive Serialize
87 // for it. Here we do so for the i32 type.
88
89 // DANGER! Doing this enables you to open a store with multiple,
90 // different integer key types, which may result in unexpected behavior.
91 // Make sure you know what you're doing!
92
93 let t = k
94 .open_multi_integer("s", StoreOptions::create())
95 .expect("open");
96
97 #[derive(Serialize)]
98 struct I32(i32);
99 impl PrimitiveInt for I32 {}
100 test_integer_keys!(t, I32(std::i32::MIN));
101 test_integer_keys!(t, I32(std::i32::MAX));
102
103 let u = k
104 .open_multi_integer("s", StoreOptions::create())
105 .expect("open");
106
107 #[derive(Serialize)]
108 struct U16(u16);
109 impl PrimitiveInt for U16 {}
110 test_integer_keys!(u, U16(std::u16::MIN));
111 test_integer_keys!(u, U16(std::u16::MAX));
112
113 let v = k
114 .open_multi_integer("s", StoreOptions::create())
115 .expect("open");
116
117 #[derive(Serialize)]
118 struct U64(u64);
119 impl PrimitiveInt for U64 {}
120 test_integer_keys!(v, U64(std::u64::MIN));
121 test_integer_keys!(v, U64(std::u64::MAX));
122 }