idx_file/
lib.rs

1mod allocator;
2
3use std::{
4    ops::{Deref, DerefMut},
5    path::Path,
6};
7
8pub use allocator::IdxFileAllocator;
9pub use avltriee::{search, Avltriee, AvltrieeIter, AvltrieeNode, AvltrieeSearch, AvltrieeUpdate};
10
11pub use file_mmap::FileMmap;
12
13pub type IdxFileAvlTriee<T, I> = Avltriee<T, I, IdxFileAllocator<T>>;
14
15pub struct IdxFile<T, I: ?Sized = T> {
16    triee: IdxFileAvlTriee<T, I>,
17}
18
19impl<T, I: ?Sized> Deref for IdxFile<T, I> {
20    type Target = IdxFileAvlTriee<T, I>;
21
22    fn deref(&self) -> &Self::Target {
23        &self.triee
24    }
25}
26
27impl<T, I: ?Sized> DerefMut for IdxFile<T, I> {
28    fn deref_mut(&mut self) -> &mut Self::Target {
29        &mut self.triee
30    }
31}
32
33impl<T, I: ?Sized> IdxFile<T, I> {
34    /// Opens the file and creates the IdxFile<T>.
35    /// # Arguments
36    /// * `path` - Path of file to save data
37    /// * `allocation_lot` - Extends the specified size when the file size becomes insufficient due to data addition.
38    /// If you expect to add a lot of data, specifying a larger size will improve performance.
39    pub fn new<P: AsRef<Path>>(path: P, allocation_lot: u32) -> Self {
40        Self {
41            triee: Avltriee::with_allocator(IdxFileAllocator::new(path, allocation_lot)),
42        }
43    }
44}
45
46#[test]
47fn test_insert_10000() {
48    use std::path::PathBuf;
49
50    let dir = "./test/";
51    if std::path::Path::new(dir).exists() {
52        std::fs::remove_dir_all(dir).unwrap();
53    }
54    std::fs::create_dir_all(dir).unwrap();
55    let path = PathBuf::from("./test/test.i".to_string());
56    let mut idx: IdxFile<u32> = IdxFile::new(path, 1000000);
57
58    const TEST_LENGTH: u32 = 1000;
59
60    for i in 1..=TEST_LENGTH {
61        idx.insert(&i);
62    }
63
64    println!("iter");
65    for row in idx.iter() {
66        println!(" {} : {}", row, unsafe { idx.value_unchecked(row) });
67    }
68
69    println!("iter_by");
70    for row in idx.iter_by(&100) {
71        println!(" {} : {}", row, unsafe { idx.value_unchecked(row) });
72    }
73
74    println!("iter_from");
75    for row in idx.iter_from(&100) {
76        println!(" {} : {}", row, unsafe { idx.value_unchecked(row) });
77    }
78
79    println!("iter_to");
80    for row in idx.iter_to(&200) {
81        println!(" {} : {}", row, unsafe { idx.value_unchecked(row) });
82    }
83
84    println!("iter_range");
85    for row in idx.iter_range(&100, &200) {
86        println!(" {} : {}", row, unsafe { idx.value_unchecked(row) });
87    }
88
89    println!("OK:{}", idx.rows_count());
90}