1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Standard Lib
use std::fs::File;
use std::io::BufReader;
use std::path::PathBuf;

// Third Party
use serde_json;

pub mod bzip;
pub mod page;
pub mod search;
use crate::bzip::{create_bz_table, indexing_bzip_blocks, BZipTable};
use crate::page::Page;
use crate::search::{Searchable, Searcher};

fn create_directory_if_not_exists(path: &str) {
    let path = path.replace("~", std::env::var("HOME").unwrap().as_str());
    if !std::path::Path::new(&path).exists() {
        std::fs::create_dir_all(path).unwrap();
    }
}

pub fn initial_indexing(input_bz_path: PathBuf, meta_path: PathBuf) -> std::io::Result<()> {
    // Create meta directory if doesn't exist
    create_directory_if_not_exists(meta_path.to_str().unwrap());

    // Index bzip blocks
    let f = File::open(&input_bz_path.to_str().unwrap()).expect("No bzip file found");

    println!("Indexing bzip blocks");
    let mut reader = BufReader::new(f);

    let output_bzip_path = meta_path.join("table.json");
    let table: BZipTable =
        create_bz_table(&mut reader, output_bzip_path.to_str().unwrap()).unwrap();
    for i in 0..table.length {
        println!("{}: {:?}", i, table.blocks[i]);
    }
    //

    let table: BZipTable =
        serde_json::de::from_reader(File::open(meta_path.join("table.json")).unwrap()).unwrap();

    println!("Indeing pages in blocks");

    // Might be a bit memory hungry
    let pages: Vec<Page> = indexing_bzip_blocks(&table, &input_bz_path).unwrap();

    let output_searcher = meta_path.join("map.index");
    let mut searcher = Searcher::new();
    searcher
        .create_searcher(&pages, output_searcher.to_str().unwrap())
        .unwrap();
    return Ok(());
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn placeholder() {}
}