| 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-dup-sort")] |
| 11 |
|
| 12 |
use std::fs; |
| 13 |
|
| 14 |
use tempfile::Builder; |
| 15 |
|
| 16 |
use rkv::{ |
| 17 |
backend::{SafeMode, SafeModeDatabase, SafeModeRoCursor, SafeModeRwTransaction}, |
| 18 |
Readable, Rkv, StoreOptions, Value, Writer, |
| 19 |
}; |
| 20 |
|
| 21 |
/// Consider a struct like this: |
| 22 |
/// struct Sample { |
| 23 |
/// id: u64, |
| 24 |
/// value: String, |
| 25 |
/// date: String, |
| 26 |
/// } |
| 27 |
/// We would like to index all of the fields so that we can search for the struct not only |
| 28 |
/// by ID but also by value and date. When we index the fields individually in their own |
| 29 |
/// tables, it is important that we run all operations within a single transaction to |
| 30 |
/// ensure coherence of the indices. |
| 31 |
/// This test features helper functions for reading and writing the parts of the struct. |
| 32 |
/// Note that the reader functions take `Readable` because they might run within a Read |
| 33 |
/// Transaction or a Write Transaction. The test demonstrates fetching values via both. |
| 34 |
|
| 35 |
type SingleStore = rkv::SingleStore<SafeModeDatabase>; |
| 36 |
type MultiStore = rkv::MultiStore<SafeModeDatabase>; |
| 37 |
|
| 38 |
#[test] |
| 39 |
fn read_many() { |
| 40 |
let root = Builder::new() |
| 41 |
.prefix("test_txns") |
| 42 |
.tempdir() |
| 43 |
.expect("tempdir"); |
| 44 |
fs::create_dir_all(root.path()).expect("dir created"); |
| 45 |
let k = Rkv::new::<SafeMode>(root.path()).expect("new succeeded"); |
| 46 |
let samplestore = k.open_single("s", StoreOptions::create()).expect("open"); |
| 47 |
let datestore = k.open_multi("m", StoreOptions::create()).expect("open"); |
| 48 |
let valuestore = k.open_multi("m", StoreOptions::create()).expect("open"); |
| 49 |
|
| 50 |
{ |
| 51 |
let mut writer = k.write().expect("env write lock"); |
| 52 |
|
| 53 |
for id in 0..30_u64 { |
| 54 |
let value = format!("value{id}"); |
| 55 |
let date = format!("2019-06-{id}"); |
| 56 |
put_id_field(&mut writer, datestore, &date, id); |
| 57 |
put_id_field(&mut writer, valuestore, &value, id); |
| 58 |
put_sample(&mut writer, samplestore, id, &value); |
| 59 |
} |
| 60 |
|
| 61 |
// now we read in the same transaction |
| 62 |
for id in 0..30_u64 { |
| 63 |
let value = format!("value{id}"); |
| 64 |
let date = format!("2019-06-{id}"); |
| 65 |
let ids = get_ids_by_field(&writer, datestore, &date); |
| 66 |
let ids2 = get_ids_by_field(&writer, valuestore, &value); |
| 67 |
let samples = get_samples(&writer, samplestore, &ids); |
| 68 |
let samples2 = get_samples(&writer, samplestore, &ids2); |
| 69 |
println!("{samples:?}, {samples2:?}"); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
{ |
| 74 |
let reader = k.read().expect("env read lock"); |
| 75 |
for id in 0..30_u64 { |
| 76 |
let value = format!("value{id}"); |
| 77 |
let date = format!("2019-06-{id}"); |
| 78 |
let ids = get_ids_by_field(&reader, datestore, &date); |
| 79 |
let ids2 = get_ids_by_field(&reader, valuestore, &value); |
| 80 |
let samples = get_samples(&reader, samplestore, &ids); |
| 81 |
let samples2 = get_samples(&reader, samplestore, &ids2); |
| 82 |
println!("{samples:?}, {samples2:?}"); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
fn get_ids_by_field<'t, T>(txn: &'t T, store: MultiStore, field: &'t str) -> Vec<u64> |
| 88 |
where |
| 89 |
T: Readable<'t, Database = SafeModeDatabase, RoCursor = SafeModeRoCursor<'t>>, |
| 90 |
{ |
| 91 |
store |
| 92 |
.get(txn, field) |
| 93 |
.expect("get iterator") |
| 94 |
.map(|id| match id.expect("field") { |
| 95 |
(_, Value::U64(id)) => id, |
| 96 |
_ => panic!("getting value in iter"), |
| 97 |
}) |
| 98 |
.collect::<Vec<u64>>() |
| 99 |
} |
| 100 |
|
| 101 |
fn get_samples<'t, T>(txn: &'t T, samplestore: SingleStore, ids: &[u64]) -> Vec<String> |
| 102 |
where |
| 103 |
T: Readable<'t, Database = SafeModeDatabase, RoCursor = SafeModeRoCursor<'t>>, |
| 104 |
{ |
| 105 |
ids.iter() |
| 106 |
.map(|id| { |
| 107 |
let bytes = id.to_be_bytes(); |
| 108 |
match samplestore.get(txn, &bytes).expect("fetch sample") { |
| 109 |
Some(Value::Str(sample)) => String::from(sample), |
| 110 |
Some(_) => panic!("wrong type"), |
| 111 |
None => panic!("no sample for this id!"), |
| 112 |
} |
| 113 |
}) |
| 114 |
.collect::<Vec<String>>() |
| 115 |
} |
| 116 |
|
| 117 |
fn put_sample( |
| 118 |
txn: &mut Writer<SafeModeRwTransaction>, |
| 119 |
samplestore: SingleStore, |
| 120 |
id: u64, |
| 121 |
value: &str, |
| 122 |
) { |
| 123 |
let idbytes = id.to_be_bytes(); |
| 124 |
samplestore |
| 125 |
.put(txn, &idbytes, &Value::Str(value)) |
| 126 |
.expect("put id"); |
| 127 |
} |
| 128 |
|
| 129 |
fn put_id_field(txn: &mut Writer<SafeModeRwTransaction>, store: MultiStore, field: &str, id: u64) { |
| 130 |
store.put(txn, field, &Value::U64(id)).expect("put id"); |
| 131 |
} |