use std::fs::File;
use std::io;
use std::path::Path;
use fst;
use memmap::Mmap;
use crate::error::{Error, Result};
use crate::util::{fst_map_builder_file, fst_map_file};
#[derive(Debug)]
pub struct IndexReader {
idx: fst::Map<Mmap>,
}
impl IndexReader {
pub fn from_path<P: AsRef<Path>>(path: P) -> Result<IndexReader> {
Ok(IndexReader { idx: unsafe { fst_map_file(path)? } })
}
pub fn get(&self, key: &[u8]) -> Option<u64> {
self.idx.get(key)
}
}
pub struct IndexSortedWriter<W> {
wtr: fst::MapBuilder<W>,
}
impl IndexSortedWriter<io::BufWriter<File>> {
pub fn from_path<P: AsRef<Path>>(
path: P,
) -> Result<IndexSortedWriter<io::BufWriter<File>>> {
Ok(IndexSortedWriter { wtr: fst_map_builder_file(path)? })
}
}
impl<W: io::Write> IndexSortedWriter<W> {
pub fn insert(&mut self, key: &[u8], value: u64) -> Result<()> {
self.wtr.insert(key, value).map_err(Error::fst)?;
Ok(())
}
pub fn finish(self) -> Result<()> {
self.wtr.finish().map_err(Error::fst)?;
Ok(())
}
}