| 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 |
#![cfg(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_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.open_integer("s", StoreOptions::create()).expect("open"); |
| 30 |
|
| 31 |
macro_rules! test_integer_keys { |
| 32 |
($store:expr, $key:expr) => {{ |
| 33 |
let mut writer = k.write().expect("writer"); |
| 34 |
|
| 35 |
$store |
| 36 |
.put(&mut writer, $key, &Value::Str("hello!")) |
| 37 |
.expect("write"); |
| 38 |
assert_eq!( |
| 39 |
$store.get(&writer, $key).expect("read"), |
| 40 |
Some(Value::Str("hello!")) |
| 41 |
); |
| 42 |
writer.commit().expect("committed"); |
| 43 |
|
| 44 |
let reader = k.read().expect("reader"); |
| 45 |
assert_eq!( |
| 46 |
$store.get(&reader, $key).expect("read"), |
| 47 |
Some(Value::Str("hello!")) |
| 48 |
); |
| 49 |
}}; |
| 50 |
} |
| 51 |
|
| 52 |
// The integer module provides only the u32 integer key variant |
| 53 |
// of IntegerStore, so we can use it without further ado. |
| 54 |
test_integer_keys!(s, std::u32::MIN); |
| 55 |
test_integer_keys!(s, std::u32::MAX); |
| 56 |
|
| 57 |
// If you want to use another integer key variant, you need to implement |
| 58 |
// a newtype, implement PrimitiveInt, and implement or derive Serialize |
| 59 |
// for it. Here we do so for the i32 type. |
| 60 |
|
| 61 |
// DANGER! Doing this enables you to open a store with multiple, |
| 62 |
// different integer key types, which may result in unexpected behavior. |
| 63 |
// Make sure you know what you're doing! |
| 64 |
|
| 65 |
let t = k.open_integer("s", StoreOptions::create()).expect("open"); |
| 66 |
|
| 67 |
#[derive(Serialize)] |
| 68 |
struct I32(i32); |
| 69 |
impl PrimitiveInt for I32 {} |
| 70 |
test_integer_keys!(t, I32(std::i32::MIN)); |
| 71 |
test_integer_keys!(t, I32(std::i32::MAX)); |
| 72 |
|
| 73 |
let u = k.open_integer("s", StoreOptions::create()).expect("open"); |
| 74 |
|
| 75 |
#[derive(Serialize)] |
| 76 |
struct U16(u16); |
| 77 |
impl PrimitiveInt for U16 {} |
| 78 |
test_integer_keys!(u, U16(std::u16::MIN)); |
| 79 |
test_integer_keys!(u, U16(std::u16::MAX)); |
| 80 |
|
| 81 |
let v = k.open_integer("s", StoreOptions::create()).expect("open"); |
| 82 |
|
| 83 |
#[derive(Serialize)] |
| 84 |
struct U64(u64); |
| 85 |
impl PrimitiveInt for U64 {} |
| 86 |
test_integer_keys!(v, U64(std::u64::MIN)); |
| 87 |
test_integer_keys!(v, U64(std::u64::MAX)); |
| 88 |
} |