bitrush_index/lib.rs
1// This code is released under the
2// General Public License (GPL), version 3
3// http://www.gnu.org/licenses/gpl-3.0.en.html
4// (c) Lorenzo Vannucci
5
6//! # Bitrush-Index
7//! Bitrush-Index is a Rust library that provides a serializable bitmap index
8//! able to index millions values/sec on a single thread. On default this
9//! library build bitmap-index using [`ozbcbitmap`] but if you want you can
10//! also use another compressed/uncrompressed bitmap.
11//! Only equality-query (A = X) are supported.
12//!
13//! ## Example
14//!```
15//! use bitrush_index::{
16//! BitmapIndex,
17//! OZBCBitmap,
18//! };
19//!
20//! use rand::Rng;
21//! use std::time::Instant;
22//!
23//! fn main() {
24//! // number of values to insert (defalut 10M)
25//! const N: usize = (1 << 20) * 10;
26//! let mut rng = rand::thread_rng();
27//!
28//! let build_options = bitrush_index::new_default_index_options::<u32>();
29//! let mut b_index = match BitmapIndex::<OZBCBitmap, u32>::new(build_options) {
30//! Ok(b_index) => b_index,
31//! Err(err) => panic!("Error occured creating bitmap index: {:?}", err)
32//! };
33//! let mut values: Vec<u32> = Vec::new();
34//!
35//! for _i in 0..N {
36//! let val: u32 = rng.gen::<u32>();
37//! values.push(val);
38//! }
39//! println!("--------------------------------------------------");
40//! println!("Inserting {} values in bitmap index...", N);
41//! let timer = Instant::now();
42//! //if index is opened in memory mode you can ignore the push_values result.
43//! let _result_index = b_index.push_values(&values);
44//! let time_b_index_insert = timer.elapsed();
45//! println!("Bitmap index created in {:?}.", time_b_index_insert);
46//! println!("Insert per second = {}.", N / (time_b_index_insert.as_millis() as usize) * 1000);
47//! println!("--------------------------------------------------");
48//!
49//! let random_index: usize = rng.gen::<usize>() % values.len();
50//! let val_to_find = values[random_index];
51//! let timer = Instant::now();
52//! let values_indexes: Vec<u64> = match b_index.run_query(val_to_find, None, None) {
53//! Ok(indexes) => indexes,
54//! Err(err) => panic!("Error occured running looking for value = {}, error: {:?}", val_to_find, err)
55//! };
56//!
57//! let time_linear_search = timer.elapsed();
58//! println!("Bitmap index search runned in {:?}, match values founded: {}.", time_linear_search, values_indexes.len());
59//! println!("--------------------------------------------------");
60//! }
61//!```
62//!
63//! [`ozbcbitmap`]: ./ozbcbitmap/mod.rs
64
65mod bitmap_index;
66pub use bitmap_index::{
67 BitValue,
68 BitmapIndex,
69 StorageIdx,
70 Bitmap,
71 MetaData,
72 BuildOptions,
73 ChunkSize
74};
75
76mod ozbcbitmap;
77pub use ozbcbitmap::OZBCBitmap;
78
79/// Return default options to create a BitmapIndex.
80pub fn new_default_index_options<U: BitValue>() -> BuildOptions {
81 let value_size = std::mem::size_of::<U>();
82 if value_size == 1 {
83 return BuildOptions::new(8, ChunkSize::M32);
84 }
85 BuildOptions::new(16, ChunkSize::M16)
86}