v_queue 0.3.1

simple file based queue
Documentation
use crate::compress::writer::dict_path;
use zstd::bulk::Decompressor;

// Lazily loaded per-part dictionary used to decompress v3 records.
pub struct PartDictionary {
    dict: Option<Vec<u8>>,
    decompressor: Option<Decompressor<'static>>,
    part_id: Option<u32>,
}

impl PartDictionary {
    pub fn new() -> Self {
        PartDictionary {
            dict: None,
            decompressor: None,
            part_id: None,
        }
    }

    pub fn ensure_loaded(&mut self, base_path: &str, queue_name: &str, part_id: u32) -> bool {
        if self.part_id == Some(part_id) && self.decompressor.is_some() {
            return true;
        }

        let path = dict_path(base_path, queue_name, part_id);
        let dict = match std::fs::read(&path) {
            Ok(d) => d,
            Err(e) => {
                error!("[queue:consumer] fail read dictionary {}, err={}", path, e);
                return false;
            },
        };

        match Decompressor::with_dictionary(&dict) {
            Ok(d) => {
                self.decompressor = Some(d);
                self.dict = Some(dict);
                self.part_id = Some(part_id);
                true
            },
            Err(e) => {
                error!("[queue:consumer] fail build decompressor, err={}", e);
                false
            },
        }
    }

    pub fn decompress(&mut self, compressed: &[u8], out: &mut [u8]) -> Result<usize, ()> {
        match self.decompressor.as_mut() {
            Some(d) => d.decompress_to_buffer(compressed, out).map_err(|_| ()),
            None => Err(()),
        }
    }
}