littlegraph-core 0.2.1

Embedded openCypher graph database backed by Neo4j record storage
Documentation
use crate::error::{Error, Result};
use crate::neo4j_storage::{Neo4jStorage, Neo4jWritableSavepoint, Neo4jWritableStorage};
use crate::storage_interface::{StorageInterface, TransactionalStorage, WritableStorage};

#[derive(Debug)]
pub(crate) enum Neo4jBackend {
    ReadOnly(Box<Neo4jStorage>),
    Writable(Box<Neo4jWritableStorage>),
}

#[derive(Clone, Debug)]
pub(crate) enum Neo4jBackendSavepoint {
    ReadOnly,
    Writable(Box<Neo4jWritableSavepoint>),
}

#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct WorkspaceUsage {
    pub(crate) bytes: usize,
    pub(crate) records: usize,
}

impl WorkspaceUsage {
    pub(crate) fn checked_add(self, other: Self) -> Result<Self> {
        Ok(Self {
            bytes: self
                .bytes
                .checked_add(other.bytes)
                .ok_or(Error::Transaction(
                    crate::TransactionErrorKind::ResourceLimit,
                ))?,
            records: self
                .records
                .checked_add(other.records)
                .ok_or(Error::Transaction(
                    crate::TransactionErrorKind::ResourceLimit,
                ))?,
        })
    }
}

impl Neo4jBackend {
    pub(crate) fn read(&self) -> &dyn StorageInterface {
        match self {
            Self::ReadOnly(storage) => storage.as_ref(),
            Self::Writable(storage) => storage.as_ref(),
        }
    }

    pub(crate) fn write(&mut self) -> Result<&mut dyn WritableStorage> {
        match self {
            Self::ReadOnly(_) => Err(Error::UnsupportedWriteQuery(
                "Neo4j storage is read-only".to_owned(),
            )),
            Self::Writable(storage) => Ok(storage.as_mut()),
        }
    }

    pub(crate) fn is_writable(&self) -> bool {
        matches!(self, Self::Writable(_))
    }

    pub(crate) fn refresh_read_view(&mut self) -> Result<()> {
        if let Self::Writable(storage) = self {
            storage.refresh_read_view()?;
        }
        Ok(())
    }

    pub(crate) fn begin_transaction(
        &mut self,
        max_workspace_bytes: Option<usize>,
        max_records: Option<usize>,
        spill_threshold_bytes: Option<usize>,
        max_spill_bytes: Option<u64>,
        max_journal_bytes: Option<u64>,
        transaction_id: Option<u64>,
    ) -> Result<()> {
        if let Self::Writable(storage) = self {
            storage.validate_workspace_limits(max_workspace_bytes)?;
            storage.configure_workspace_spill(
                spill_threshold_bytes,
                max_spill_bytes,
                max_journal_bytes,
                transaction_id,
            );
            storage.begin_transaction()?;
            storage.set_workspace_limits(max_workspace_bytes, max_records)?;
        }
        Ok(())
    }

    pub(crate) fn commit_transaction(&mut self) -> Result<()> {
        if let Self::Writable(storage) = self {
            storage.commit_transaction()?;
        }
        Ok(())
    }

    pub(crate) fn rollback_transaction(&mut self) -> Result<()> {
        if let Self::Writable(storage) = self {
            storage.rollback_transaction()?;
        }
        Ok(())
    }

    pub(crate) fn mark_transaction_failed(&mut self) {
        if let Self::Writable(storage) = self {
            storage.mark_transaction_failed();
        }
    }

    pub(crate) fn savepoint(&self) -> Result<Neo4jBackendSavepoint> {
        match self {
            Self::ReadOnly(_) => Ok(Neo4jBackendSavepoint::ReadOnly),
            Self::Writable(storage) => storage
                .savepoint()
                .map(Box::new)
                .map(Neo4jBackendSavepoint::Writable),
        }
    }

    pub(crate) fn rollback_to_savepoint(
        &mut self,
        savepoint: &Neo4jBackendSavepoint,
    ) -> Result<()> {
        match (self, savepoint) {
            (Self::ReadOnly(_), Neo4jBackendSavepoint::ReadOnly) => Ok(()),
            (Self::Writable(storage), Neo4jBackendSavepoint::Writable(savepoint)) => {
                storage.rollback_to_savepoint(savepoint)
            }
            _ => Err(Error::Transaction(
                crate::TransactionErrorKind::InvalidSavepoint,
            )),
        }
    }

    pub(crate) fn workspace_usage(&self) -> Result<WorkspaceUsage> {
        match self {
            Self::ReadOnly(_) => Ok(WorkspaceUsage::default()),
            Self::Writable(storage) => storage.workspace_usage(),
        }
    }

    pub(crate) fn set_workspace_limits(
        &mut self,
        max_workspace_bytes: Option<usize>,
        max_records: Option<usize>,
    ) -> Result<()> {
        if let Self::Writable(storage) = self {
            storage.set_workspace_limits(max_workspace_bytes, max_records)?;
        }
        Ok(())
    }

    pub(crate) fn savepoint_usage(&self) -> Result<WorkspaceUsage> {
        match self {
            Self::ReadOnly(_) => Ok(WorkspaceUsage::default()),
            Self::Writable(storage) => storage.savepoint_usage(),
        }
    }
}