#[cfg(feature = "address-reservations")]
pub mod address_allocator;
#[cfg(feature = "author-fencing")]
pub mod author_fence;
#[cfg(not(target_arch = "wasm32"))]
mod background_compaction;
#[cfg(test)]
mod boundary_tests;
mod branch;
#[cfg(feature = "causal")]
pub mod causal;
#[cfg(feature = "causal-batches")]
pub mod causal_batch;
#[cfg(feature = "causal-merkle")]
pub mod causal_merkle;
#[cfg(feature = "causal-sessions")]
pub mod causal_named;
#[cfg(feature = "causal-projection")]
pub mod causal_projection;
#[cfg(feature = "causal")]
pub mod causal_replica;
#[cfg(feature = "causal-sessions")]
pub mod causal_session;
mod changes;
mod checkpoint;
pub mod cluster;
mod commit;
#[cfg(test)]
mod constraint_cost_tests;
mod constraint_index;
#[cfg(test)]
mod cost_tests;
mod delta;
#[cfg(feature = "etcd")]
pub mod etcd;
mod group;
mod journal;
#[cfg(feature = "physical-lsm")]
pub mod lsm;
pub mod maintenance;
mod named;
mod object_cache;
pub mod paged;
pub mod partitioned;
mod pessimistic;
mod query_reservation;
mod query_scratch;
pub mod raft;
pub mod reclaiming;
pub mod recovery;
pub mod replication;
mod rows;
mod savepoint;
#[cfg(test)]
mod shared_cost_tests;
mod sorted_runs;
#[cfg(all(feature = "sqlite", not(target_arch = "wasm32")))]
pub mod sqlite;
mod timed_log;
mod transaction;
mod validation;
use crate::{GenericLink, LinkError, TransitionLogStore};
use doublets::data::LinkReference;
use std::collections::BTreeMap;
use std::sync::{Arc, Mutex, MutexGuard};
#[cfg(not(target_arch = "wasm32"))]
pub use background_compaction::BackgroundCompaction;
pub use branch::Branch;
pub use checkpoint::restore_backup;
pub use delta::DeltaLog;
pub use group::GroupCommitOutcome;
pub use journal::MemoryLog;
use rows::Rows;
pub use savepoint::Savepoint;
pub use timed_log::{revision_at, TimedLog};
pub use transaction::EngineTransaction;
pub use validation::SerializableValidation;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum StateStructure {
#[default]
Flat,
Shared,
SortedRuns,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum RevisionEncoding {
#[default]
Snapshot,
Changes,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ConcurrencyControl {
#[default]
Optimistic,
Exclusive,
Pessimistic,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum IsolationLevel {
ReadCommitted,
Snapshot,
#[default]
Serializable,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum HistoryPolicy {
#[default]
None,
Last(usize),
All,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Constraint {
References,
UniquePairs,
UniqueNames,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ConstraintValidation {
#[default]
Scan,
Indexed,
}
#[derive(Debug, Clone, Default)]
pub struct EngineOptions {
pub state_structure: StateStructure,
pub revision_encoding: RevisionEncoding,
pub concurrency: ConcurrencyControl,
pub lock_wait: Option<std::time::Duration>,
pub isolation: IsolationLevel,
pub serializable_validation: SerializableValidation,
pub history: HistoryPolicy,
pub constraints: Vec<Constraint>,
pub constraint_validation: ConstraintValidation,
pub trace: bool,
pub require_durable_flush: bool,
}
impl EngineOptions {
fn tracks_generations(&self) -> bool {
self.concurrency == ConcurrencyControl::Pessimistic
|| (self.concurrency == ConcurrencyControl::Optimistic
&& (self.isolation != IsolationLevel::Serializable
|| self.serializable_validation == SerializableValidation::ReadSet))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Row<T> {
pub link: GenericLink<T>,
pub name: Option<String>,
}
#[derive(Debug, Clone)]
pub struct Snapshot<T> {
pub revision: u64,
rows: Arc<Rows<T>>,
}
impl<T: LinkReference> Snapshot<T> {
pub fn links(&self) -> Vec<GenericLink<T>> {
self.rows.values().map(|row| row.link).collect()
}
pub fn get(&self, id: T) -> Option<&Row<T>> {
self.rows.get(&id)
}
pub fn rows(&self) -> &BTreeMap<T, Row<T>> {
self.rows.materialize()
}
pub fn iter(&self) -> impl Iterator<Item = (&T, &Row<T>)> {
self.rows.iter()
}
}
struct State<T, L> {
log: L,
current: Snapshot<T>,
history: BTreeMap<u64, (Snapshot<T>, u128)>,
changed: BTreeMap<T, u64>,
write_owners: BTreeMap<T, Arc<()>>,
lock_waits: Option<pessimistic::LockWaits>,
next: u128,
committed_next: u128,
active: usize,
poisoned: bool,
compacted_revision: u64,
options: EngineOptions,
constraint_index: Option<constraint_index::ConstraintIndex<T>>,
}
pub struct Engine<T, L> {
state: Arc<Mutex<State<T, L>>>,
}
impl<T, L> Clone for Engine<T, L> {
fn clone(&self) -> Self {
Self {
state: self.state.clone(),
}
}
}
fn error(message: &str) -> LinkError {
LinkError::Transaction(message.into())
}
impl<T: LinkReference, L: TransitionLogStore> Engine<T, L> {
pub fn open(mut log: L, options: EngineOptions) -> Result<Self, LinkError> {
if options.serializable_validation == SerializableValidation::ReadSet
&& options.isolation != IsolationLevel::Serializable
{
return Err(error("read-set validation requires serializable isolation"));
}
if options.lock_wait.is_some() && options.concurrency != ConcurrencyControl::Pessimistic {
return Err(error("lock waiting requires pessimistic concurrency"));
}
if options.require_durable_flush && !log.supports_durable_flush() {
return Err(error("selected log does not support durable flush"));
}
if options.history == HistoryPolicy::Last(0) {
return Err(error("history limit must be positive"));
}
let entries = log.read_log_entries()?;
let mut state = State::empty(log, options);
for (index, entry) in entries.iter().enumerate() {
state.replay(index, entry)?;
}
if state.options.constraint_validation == ConstraintValidation::Indexed {
state.constraint_index = constraint_index::ConstraintIndex::build(
&state.options.constraints,
&state.current.rows,
)?;
}
Ok(Self {
state: Arc::new(Mutex::new(state)),
})
}
fn lock(&self) -> Result<MutexGuard<'_, State<T, L>>, LinkError> {
let state = self
.state
.lock()
.map_err(|_| error("engine mutex poisoned"))?;
if state.poisoned {
return Err(error(
"indeterminate journal outcome; close and reopen before continuing",
));
}
Ok(state)
}
pub fn snapshot(&self) -> Result<Snapshot<T>, LinkError> {
Ok(self.lock()?.current.clone())
}
pub fn snapshot_at(&self, revision: u64) -> Result<Snapshot<T>, LinkError> {
let state = self.lock()?;
if revision == state.current.revision {
return Ok(state.current.clone());
}
state
.history
.get(&revision)
.map(|(snapshot, _)| snapshot.clone())
.ok_or_else(|| error("revision is not retained"))
}
pub fn begin(&self) -> Result<EngineTransaction<T, L>, LinkError> {
let mut state = self.lock()?;
if state.options.concurrency == ConcurrencyControl::Exclusive && state.active > 0 {
return Err(error("busy: an exclusive transaction is active"));
}
state.active += 1;
Ok(EngineTransaction {
engine: self.clone(),
base: state.current.clone(),
writes: BTreeMap::new(),
savepoints: Vec::new(),
reserved: std::collections::VecDeque::new(),
closed: false,
isolation: state.options.isolation,
reads: (state.options.serializable_validation == SerializableValidation::ReadSet
&& state.options.concurrency != ConcurrencyControl::Exclusive)
.then(|| Mutex::new(validation::ReadSet::default())),
locks: (state.options.concurrency == ConcurrencyControl::Pessimistic)
.then(pessimistic::WriteLocks::default),
})
}
pub fn with_log<R>(
&self,
operation: impl FnOnce(&mut L) -> Result<R, LinkError>,
) -> Result<R, LinkError> {
let mut state = self.lock()?;
match operation(&mut state.log) {
Ok(result) => Ok(result),
Err(error) => {
state.poisoned = true;
Err(error)
}
}
}
pub fn into_log(self) -> Result<L, Self> {
match Arc::try_unwrap(self.state) {
Ok(mutex) => Ok(mutex
.into_inner()
.unwrap_or_else(|poison| poison.into_inner())
.log),
Err(state) => Err(Self { state }),
}
}
}
impl<T: LinkReference, L> State<T, L> {
fn empty(log: L, options: EngineOptions) -> Self {
let mut state = State {
current: Snapshot {
revision: 0,
rows: Arc::new(Rows::new(options.state_structure)),
},
history: BTreeMap::new(),
changed: BTreeMap::new(),
write_owners: BTreeMap::new(),
lock_waits: options.lock_wait.map(pessimistic::LockWaits::new),
next: 1,
committed_next: 1,
active: 0,
poisoned: false,
compacted_revision: 0,
options,
constraint_index: None,
log,
};
state.retain();
state
}
fn replay(&mut self, index: usize, entry: &str) -> Result<(), LinkError> {
let (snapshot, next) = if index == 0 && entry.starts_with("engine3|") {
let decoded = journal::decode_checkpoint(entry, self.options.state_structure)?;
self.history.clear();
decoded
} else {
journal::decode_after(
entry,
&self.current,
self.next,
self.options.state_structure,
)?
};
self.validate(&snapshot.rows)?;
self.next = next;
self.committed_next = next;
self.current = snapshot;
self.retain();
Ok(())
}
fn retain(&mut self) {
match self.options.history {
HistoryPolicy::None => {}
HistoryPolicy::All => {
self.history.insert(
self.current.revision,
(self.current.clone(), self.committed_next),
);
}
HistoryPolicy::Last(limit) => {
self.history.insert(
self.current.revision,
(self.current.clone(), self.committed_next),
);
while self.history.len() > limit {
self.history.pop_first();
}
}
}
}
fn validate(&self, rows: &Rows<T>) -> Result<(), LinkError> {
let zero = T::from_byte(0);
for constraint in &self.options.constraints {
match constraint {
Constraint::References => {
for row in rows.values() {
if [row.link.source, row.link.target]
.iter()
.any(|id| *id != zero && !rows.contains_key(id))
{
return Err(error("constraint: dangling link reference"));
}
}
}
Constraint::UniquePairs => {
let mut pairs = std::collections::BTreeSet::new();
for row in rows.values() {
if !pairs.insert((row.link.source, row.link.target)) {
return Err(error("constraint: duplicate pair"));
}
}
}
Constraint::UniqueNames => {
let mut names = std::collections::BTreeSet::new();
for name in rows.values().filter_map(|row| row.name.as_ref()) {
if !names.insert(name) {
return Err(error("constraint: duplicate name"));
}
}
}
}
}
Ok(())
}
}