persy 1.8.0

Transactional Persistence Engine
Documentation
pub use crate::journal::{
    JournalId, JournalRead,
    records::{
        Cleanup, Commit, CreateSegment, DeleteRecord, DropSegment, FreedPage, InsertRecord, Metadata, NewSegmentPage,
        PrepareCommit, ReadRecord, Rollback, RollbackPage, Start, UpdateRecord,
    },
};
use crate::{
    IndexOpsError, IndexType, OpenError, PE, Persy, PersyId, ToSegmentId,
    allocator::Allocator,
    device::{Device, FileDevice},
    error::GenericError,
    journal::JournalShared,
    persy::{DEFAULT_PAGE_EXP, PERSY_TAG_BYTES, PersyImpl},
    util::io::{InfallibleRead, InfallibleReadFormat, read_u64},
};

use std::{fs, sync::Arc};

mod inspect_tree;
#[cfg(test)]
mod tests;

pub type TreeInspectorResult<T> = std::result::Result<T, GenericError>;

pub use inspect_tree::PrintTreeInspector;
pub(crate) use inspect_tree::inspect_tree;

pub trait TreeInspector<K, V> {
    fn start_node(&mut self, _node_id: PersyId, _prev: Option<K>, _next: Option<K>) -> TreeInspectorResult<()> {
        Ok(())
    }
    fn failed_load(&mut self, _node_id: PersyId) -> TreeInspectorResult<()> {
        Ok(())
    }
    fn end_node(&mut self, _node_id: PersyId) -> TreeInspectorResult<()> {
        Ok(())
    }
    fn start_leaf(&mut self, _node_id: PersyId, _prev: Option<K>, _next: Option<K>) -> TreeInspectorResult<()> {
        Ok(())
    }
    fn end_leaf(&mut self, _node_id: PersyId) -> TreeInspectorResult<()> {
        Ok(())
    }
    fn start_key(&mut self, _node_pos: u32, _k: K) -> TreeInspectorResult<()> {
        Ok(())
    }
    fn end_key(&mut self, _node_pos: u32, _k: K) -> TreeInspectorResult<()> {
        Ok(())
    }
    fn value(&mut self, _node_pos: u32, _v: V) -> TreeInspectorResult<()> {
        Ok(())
    }
    fn empty(&mut self) -> TreeInspectorResult<()> {
        Ok(())
    }
}

pub trait PersyInspect {
    fn inspect_tree<K, V, I>(&self, index_name: &str, inspector: &mut I) -> Result<(), PE<IndexOpsError>>
    where
        K: IndexType,
        V: IndexType,
        I: TreeInspector<K, V>;

    fn page_state_scan(&self) -> Result<PageStateIter, PE<GenericError>>;
    fn inspect_record_direct(&self, id: &PersyId) -> Result<RecordState, PE<GenericError>>;
    fn inspect_record(&self, segment: impl ToSegmentId, id: &PersyId) -> Result<RecordState, PE<GenericError>>;
}

pub fn inspec_log<P, I>(path: P, inspector: &mut I) -> Result<(), PE<OpenError>>
where
    P: AsRef<std::path::Path>,
    I: JournalRead,
{
    let f = fs::OpenOptions::new().write(false).read(true).open(path)?;
    let device = Box::new(FileDevice::new(f)?);
    let journal_page;
    {
        let mut pg = device.load_page_raw(0, DEFAULT_PAGE_EXP)?;
        pg.read_u16(); //THIS NOW is 0 all the times
        let _address_page = pg.read_u64();
        journal_page = pg.read_u64();
        let _allocator_page = pg.read_u64();
        let mut buff = [0u8; 4];
        pg.read_exact(&mut buff);
        // Checking alternatively the for bytes to be 0 for backward compatibility
        if buff != *PERSY_TAG_BYTES && buff != [0u8; 4] {
            return Err(PE::PE(OpenError::NotPersyFile));
        }
    }
    let mut page = device.load_page(journal_page)?;
    let version = page.read_u8();
    assert_eq!(version, 0);
    let (buffer, _) = Allocator::read_root_page_buffer(&mut page, 11);
    let first_page = read_u64(&buffer[0..8]);
    JournalShared::inspect(first_page, inspector, *device)?;

    Ok(())
}

#[derive(Debug, PartialEq, Eq)]
pub enum RecordState {
    SegmentNotExists,
    IdNotMatchSegment,
    IdPageOutOfFile,
    IdPageNotExists,
    IdPagePositionOutOfPage,
    IdNotAnAddressPage,
    Exists(Vec<u8>),
    NotExists,
    Deleted,
    PointFreePage(u64),
    PointOverFileSize(u64),
    MismatchRecordId(u64, Vec<u8>),
    PointNotAPage(u64),
}

pub struct PageState {
    position: u64,
    exp: u8,
    free: bool,
}

impl PageState {
    pub(crate) fn new(position: u64, exp: u8, free: bool) -> Self {
        Self { position, exp, free }
    }
    pub fn position(&self) -> u64 {
        self.position
    }
    pub fn size(&self) -> u64 {
        1 << self.exp
    }
    pub fn size_exp(&self) -> u8 {
        self.exp
    }
    pub fn is_free(&self) -> bool {
        self.free
    }
}

pub struct PageStateIter {
    persy_impl: Arc<PersyImpl>,
    next: u64,
}
impl PageStateIter {
    fn new(persy_impl: Arc<PersyImpl>) -> Self {
        Self { persy_impl, next: 0 }
    }
}
impl Iterator for PageStateIter {
    type Item = PageState;
    fn next(&mut self) -> Option<Self::Item> {
        let next = if self.next == 0 {
            Some(PageState::new(self.next, crate::persy::DEFAULT_PAGE_EXP, false))
        } else {
            self.persy_impl.page_state(self.next)
        };
        if let Some(n) = &next {
            self.next = n.position() + n.size();
        }
        next
    }
}

impl PersyInspect for Persy {
    fn inspect_tree<K, V, I>(&self, index_name: &str, inspector: &mut I) -> Result<(), PE<IndexOpsError>>
    where
        K: IndexType,
        V: IndexType,
        I: TreeInspector<K, V>,
    {
        let index_id = self
            .solve_index_id(index_name)
            .map_err(|e| PE::PE(IndexOpsError::from(e.error())))?;
        self.persy_impl
            .inspect_tree::<K::Wrapper, V::Wrapper, I>(index_id, inspector)?;
        Ok(())
    }

    fn page_state_scan(&self) -> Result<PageStateIter, PE<GenericError>> {
        Ok(PageStateIter::new(self.persy_impl.clone()))
    }

    fn inspect_record_direct(&self, id: &PersyId) -> Result<RecordState, PE<GenericError>> {
        Ok(self.persy_impl.inspect_record(None, &id.0)?)
    }

    fn inspect_record(&self, segment: impl ToSegmentId, id: &PersyId) -> Result<RecordState, PE<GenericError>> {
        let segment_id = match self.solve_segment_id(segment) {
            Ok(s) => s,
            Err(_) => {
                return Ok(RecordState::SegmentNotExists);
            }
        };
        Ok(self.persy_impl.inspect_record(Some(segment_id), &id.0)?)
    }
}