use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, OnceLock, Weak};
use crate::error::{Error, Result};
use crate::graph_model::{Node, NodeId, Relationship, RelationshipId};
use crate::storage_interface::{
StorageInterface, TransactionState, TransactionalStorage, WritableStorage,
};
use crate::value::{Properties, Value};
use super::mvcc::{FixedStore, SharedDependency, WriteSet};
use super::page::{PageLayout, RecordReader};
use super::transaction::{self, ChangeSet, DatabaseLock};
use super::workspace_spill::{SpillFile, SpillMap};
use super::Neo4jStorage;
const PROPERTY_STORE: &str = "neostore.propertystore.db";
const PROPERTY_ID_STORE: &str = "neostore.propertystore.db.id";
const STRING_STORE: &str = "neostore.propertystore.db.strings";
const STRING_ID_STORE: &str = "neostore.propertystore.db.strings.id";
const ARRAY_STORE: &str = "neostore.propertystore.db.arrays";
const ARRAY_ID_STORE: &str = "neostore.propertystore.db.arrays.id";
const PROPERTY_KEY_TOKEN_STORE: &str = "neostore.propertystore.db.index";
const PROPERTY_KEY_TOKEN_ID_STORE: &str = "neostore.propertystore.db.index.id";
const PROPERTY_KEY_NAME_STORE: &str = "neostore.propertystore.db.index.keys";
const PROPERTY_KEY_NAME_ID_STORE: &str = "neostore.propertystore.db.index.keys.id";
const NODE_STORE: &str = "neostore.nodestore.db";
const NODE_ID_STORE: &str = "neostore.nodestore.db.id";
const NODE_LABEL_STORE: &str = "neostore.nodestore.db.labels";
const NODE_LABEL_ID_STORE: &str = "neostore.nodestore.db.labels.id";
const LABEL_TOKEN_STORE: &str = "neostore.labeltokenstore.db";
const LABEL_TOKEN_ID_STORE: &str = "neostore.labeltokenstore.db.id";
const LABEL_NAME_STORE: &str = "neostore.labeltokenstore.db.names";
const LABEL_NAME_ID_STORE: &str = "neostore.labeltokenstore.db.names.id";
const RELATIONSHIP_TYPE_TOKEN_STORE: &str = "neostore.relationshiptypestore.db";
const RELATIONSHIP_TYPE_TOKEN_ID_STORE: &str = "neostore.relationshiptypestore.db.id";
const RELATIONSHIP_TYPE_NAME_STORE: &str = "neostore.relationshiptypestore.db.names";
const RELATIONSHIP_TYPE_NAME_ID_STORE: &str = "neostore.relationshiptypestore.db.names.id";
const COUNTS_STORE: &str = "neostore.counts.db";
const RELATIONSHIP_STORE: &str = "neostore.relationshipstore.db";
const RELATIONSHIP_ID_STORE: &str = "neostore.relationshipstore.db.id";
const RELATIONSHIP_GROUP_ID_STORE: &str = "neostore.relationshipgroupstore.db.id";
const RELATIONSHIP_GROUP_STORE: &str = "neostore.relationshipgroupstore.db";
const RELATIONSHIP_GROUP_DEGREE_STORE: &str = "neostore.relationshipgroupstore.degrees.db";
const PROPERTY_RECORD_SIZE: usize = 41;
const NODE_RECORD_SIZE: usize = 15;
const RELATIONSHIP_RECORD_SIZE: usize = 34;
const RELATIONSHIP_GROUP_RECORD_SIZE: usize = 25;
const PROPERTY_KEY_TOKEN_RECORD_SIZE: usize = 9;
const LABEL_TOKEN_RECORD_SIZE: usize = 5;
const DYNAMIC_LABEL_FLAG: u64 = 0x80_0000_0000;
const MAX_DYNAMIC_LABEL_REFERENCE: u64 = 0x0f_ffff_ffff;
const NEO4J_DENSE_NODE_THRESHOLD: u64 = 50;
#[cfg(test)]
thread_local! {
static POST_COMMIT_REFRESH_FAILURE: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}
#[cfg(test)]
pub(super) fn fail_next_post_commit_refresh() {
POST_COMMIT_REFRESH_FAILURE.with(|failure| failure.set(true));
}
#[cfg(test)]
fn post_commit_refresh_checkpoint() -> Result<()> {
POST_COMMIT_REFRESH_FAILURE.with(|failure| {
if failure.replace(false) {
Err(std::io::Error::other("injected post-commit refresh failure").into())
} else {
Ok(())
}
})
}
#[cfg(not(test))]
fn post_commit_refresh_checkpoint() -> Result<()> {
Ok(())
}
#[derive(Clone, Debug)]
struct TransactionWorkspace {
state: TransactionState,
snapshot_version: u64,
write_set: WriteSet,
max_workspace_bytes: Option<usize>,
max_records: Option<usize>,
spill_threshold_bytes: Option<usize>,
max_journal_bytes: Option<u64>,
base_snapshot: Neo4jStorage,
property_records: SpillMap<[u8; PROPERTY_RECORD_SIZE]>,
node_records: SpillMap<[u8; NODE_RECORD_SIZE]>,
node_label_records: SpillMap<Vec<u8>>,
relationship_records: SpillMap<[u8; RELATIONSHIP_RECORD_SIZE]>,
relationship_group_records: SpillMap<[u8; RELATIONSHIP_GROUP_RECORD_SIZE]>,
string_records: SpillMap<Vec<u8>>,
array_records: SpillMap<Vec<u8>>,
property_key_token_records: SpillMap<[u8; PROPERTY_KEY_TOKEN_RECORD_SIZE]>,
property_key_name_records: SpillMap<Vec<u8>>,
label_token_records: SpillMap<[u8; LABEL_TOKEN_RECORD_SIZE]>,
label_name_records: SpillMap<Vec<u8>>,
relationship_type_token_records: SpillMap<[u8; LABEL_TOKEN_RECORD_SIZE]>,
relationship_type_name_records: SpillMap<Vec<u8>>,
string_chain_ids: BTreeMap<u64, Vec<u64>>,
array_chain_ids: BTreeMap<u64, Vec<u64>>,
node_label_chain_ids: BTreeMap<u64, Vec<u64>>,
allocated_property_records: u64,
reused_property_records: Vec<u64>,
freed_property_records: BTreeSet<u64>,
allocated_string_records: u64,
reused_string_records: Vec<u64>,
freed_string_records: BTreeSet<u64>,
allocated_array_records: u64,
reused_array_records: Vec<u64>,
freed_array_records: BTreeSet<u64>,
allocated_property_key_tokens: u64,
reused_property_key_tokens: Vec<u64>,
allocated_property_key_name_records: u64,
allocated_label_tokens: u64,
reused_label_tokens: Vec<u64>,
allocated_label_name_records: u64,
allocated_relationship_type_tokens: u64,
reused_relationship_type_tokens: Vec<u64>,
allocated_relationship_type_name_records: u64,
allocated_node_records: u64,
reused_node_records: Vec<u64>,
freed_node_records: BTreeSet<u64>,
allocated_node_label_records: u64,
reused_node_label_records: Vec<u64>,
freed_node_label_records: BTreeSet<u64>,
allocated_relationship_records: u64,
reused_relationship_records: Vec<u64>,
freed_relationship_records: BTreeSet<u64>,
allocated_relationship_group_records: u64,
reused_relationship_group_records: Vec<u64>,
freed_relationship_group_records: BTreeSet<u64>,
node_label_count_deltas: BTreeMap<u32, i64>,
allowed_missing_node_label_count_keys: BTreeSet<u32>,
relationship_count_deltas: BTreeMap<super::counts::RelationshipCountKey, i64>,
relationship_group_degree_deltas: BTreeMap<super::counts::RelationshipGroupDegreeKey, i64>,
}
#[derive(Clone, Debug)]
pub(crate) struct Neo4jWritableSavepoint {
storage: Neo4jStorage,
transaction: TransactionWorkspace,
}
#[derive(Debug)]
pub(crate) struct Neo4jWritableStorage {
storage: Neo4jStorage,
coordinator: Arc<MvccCoordinator>,
snapshot_version: u64,
transaction: Option<TransactionWorkspace>,
snapshot_registration: Option<SnapshotRegistration>,
poisoned: bool,
next_spill_threshold_bytes: Option<usize>,
next_max_spill_bytes: Option<u64>,
next_max_journal_bytes: Option<u64>,
next_spill_transaction_id: Option<u64>,
property_id_header: super::id_generator::IdGeneratorHeader,
property_record_capacity: u64,
string_id_header: super::id_generator::IdGeneratorHeader,
string_layout: super::dynamic::DynamicStoreLayout,
array_id_header: super::id_generator::IdGeneratorHeader,
array_layout: super::dynamic::DynamicStoreLayout,
property_key_token_id_header: super::id_generator::IdGeneratorHeader,
property_key_token_capacity: u64,
property_key_name_id_header: super::id_generator::IdGeneratorHeader,
property_key_name_layout: super::dynamic::DynamicStoreLayout,
label_token_id_header: super::id_generator::IdGeneratorHeader,
label_token_capacity: u64,
label_name_id_header: super::id_generator::IdGeneratorHeader,
label_name_layout: super::dynamic::DynamicStoreLayout,
relationship_type_token_id_header: super::id_generator::IdGeneratorHeader,
relationship_type_token_capacity: u64,
relationship_type_name_id_header: super::id_generator::IdGeneratorHeader,
relationship_type_name_layout: super::dynamic::DynamicStoreLayout,
node_id_header: super::id_generator::IdGeneratorHeader,
node_record_capacity: u64,
node_label_id_header: super::id_generator::IdGeneratorHeader,
node_label_layout: super::dynamic::DynamicStoreLayout,
relationship_id_header: super::id_generator::IdGeneratorHeader,
relationship_record_capacity: u64,
relationship_group_id_header: super::id_generator::IdGeneratorHeader,
relationship_group_record_capacity: u64,
}
#[derive(Debug)]
struct MvccCoordinator {
_lock: DatabaseLock,
state: Mutex<MvccState>,
}
#[derive(Debug, Default)]
struct MvccState {
version: u64,
poisoned: bool,
active_snapshots: BTreeMap<u64, usize>,
record_history: BTreeMap<super::mvcc::FixedRecordDependency, Vec<VersionedBeforeImage>>,
}
#[derive(Debug)]
struct VersionedBeforeImage {
committed_version: u64,
image: Vec<u8>,
}
#[derive(Debug)]
struct SnapshotRegistration {
coordinator: Arc<MvccCoordinator>,
version: u64,
}
impl Drop for SnapshotRegistration {
fn drop(&mut self) {
if let Ok(mut state) = self.coordinator.state.lock() {
state.unregister_snapshot(self.version);
}
}
}
impl MvccState {
fn register_snapshot(&mut self, version: u64) -> Result<()> {
let count = self.active_snapshots.entry(version).or_default();
*count = count.checked_add(1).ok_or(Error::Transaction(
crate::TransactionErrorKind::ResourceLimit,
))?;
Ok(())
}
fn unregister_snapshot(&mut self, version: u64) {
if let Some(count) = self.active_snapshots.get_mut(&version) {
*count -= 1;
if *count == 0 {
self.active_snapshots.remove(&version);
}
}
self.prune_record_history();
}
fn record_commit(&mut self, version: u64, write_set: &WriteSet) {
for (dependency, image) in write_set.captured_before_images() {
self.record_history
.entry(dependency)
.or_default()
.push(VersionedBeforeImage {
committed_version: version,
image: image.to_vec(),
});
}
self.prune_record_history();
}
fn before_image_at(
&self,
dependency: super::mvcc::FixedRecordDependency,
snapshot_version: u64,
) -> Option<&[u8]> {
self.record_history
.get(&dependency)?
.iter()
.find_map(|entry| {
(entry.committed_version > snapshot_version).then_some(entry.image.as_slice())
})
}
fn record_changed_after(
&self,
dependency: super::mvcc::FixedRecordDependency,
snapshot_version: u64,
) -> bool {
self.record_history.get(&dependency).is_some_and(|entries| {
entries
.iter()
.any(|entry| entry.committed_version > snapshot_version)
})
}
fn prune_record_history(&mut self) {
if self.active_snapshots.is_empty() {
self.record_history.clear();
return;
}
let active_snapshots = self.active_snapshots.keys().copied().collect::<Vec<_>>();
self.record_history.retain(|_, entries| {
let needed_versions = active_snapshots
.iter()
.filter_map(|snapshot_version| {
entries
.iter()
.find(|entry| entry.committed_version > *snapshot_version)
.map(|entry| entry.committed_version)
})
.collect::<BTreeSet<_>>();
entries.retain(|entry| needed_versions.contains(&entry.committed_version));
!entries.is_empty()
});
}
}
static MVCC_COORDINATORS: OnceLock<Mutex<BTreeMap<PathBuf, Weak<MvccCoordinator>>>> =
OnceLock::new();
fn lock_error(context: &str) -> Error {
Error::Validation(format!("{context} lock is poisoned"))
}
fn mvcc_coordinator(database_directory: &Path) -> Result<Arc<MvccCoordinator>> {
let key = std::fs::canonicalize(database_directory).map_err(|source| {
crate::Neo4jStoreError::Read {
path: database_directory.to_path_buf(),
source,
}
})?;
let registry = MVCC_COORDINATORS.get_or_init(|| Mutex::new(BTreeMap::new()));
let mut registry = registry
.lock()
.map_err(|_| lock_error("MVCC coordinator registry"))?;
registry.retain(|_, coordinator| coordinator.strong_count() != 0);
if let Some(coordinator) = registry.get(&key).and_then(Weak::upgrade) {
return Ok(coordinator);
}
let database_lock = DatabaseLock::acquire(database_directory)?;
super::workspace_spill::cleanup_stale(database_directory)?;
transaction::recover(database_directory)?;
super::checkpoint::validate_clean_shutdown(database_directory)?;
let coordinator = Arc::new(MvccCoordinator {
_lock: database_lock,
state: Mutex::new(MvccState::default()),
});
registry.insert(key, Arc::downgrade(&coordinator));
Ok(coordinator)
}
fn stage_id_lifecycle_changes(
change_set: &mut ChangeSet,
database_directory: &Path,
id_store: &str,
header: super::id_generator::IdGeneratorHeader,
allocated: u64,
reused: &[u64],
freed: &BTreeSet<u64>,
) -> Result<()> {
if reused.is_empty() && freed.is_empty() {
return Ok(());
}
let path = database_directory.join(id_store);
let freed = freed.iter().copied().collect::<Vec<_>>();
if allocated == 0 && reused.is_empty() {
let update = super::id_generator::prepare_mark_deleted_and_free(&path, &freed)?;
change_set.overwrite(id_store, update.root_page_offset, update.root_page)?;
for (offset, bytes) in update.header_patches {
change_set.overwrite(id_store, offset, bytes)?;
}
return Ok(());
}
let high_id_limit = header
.high_id
.checked_add(allocated)
.ok_or_else(|| Error::UnsupportedWriteQuery(format!("{id_store} high ID overflow")))?;
let update = super::id_generator::prepare_reuse_and_free_ids(
&path,
reused.len(),
&freed,
high_id_limit,
)?;
if update.reused_ids != reused {
return Err(Error::UnsupportedWriteQuery(format!(
"reusable {id_store} ID selection changed before commit: expected {:?}, got {:?}",
reused, update.reused_ids
)));
}
change_set.overwrite(id_store, update.root_page_offset, update.root_page)?;
for (offset, bytes) in update.header_patches {
change_set.overwrite(id_store, offset, bytes)?;
}
Ok(())
}
fn reusable_ids_still_match(
database_directory: &Path,
id_store: &str,
header: super::id_generator::IdGeneratorHeader,
allocated: u64,
reused: &[u64],
freed: &BTreeSet<u64>,
) -> Result<bool> {
if reused.is_empty() {
return Ok(true);
}
let high_id_limit = header
.high_id
.checked_add(allocated)
.ok_or_else(|| Error::UnsupportedWriteQuery(format!("{id_store} high ID overflow")))?;
let freed = freed.iter().copied().collect::<Vec<_>>();
let update = super::id_generator::prepare_reuse_and_free_ids(
&database_directory.join(id_store),
reused.len(),
&freed,
high_id_limit,
)?;
Ok(update.reused_ids == reused)
}
fn reusable_token_ids_still_match(
database_directory: &Path,
id_store: &str,
reused: &[u64],
) -> Result<bool> {
if reused.is_empty() {
return Ok(true);
}
Ok(super::id_generator::prepare_take_free_ids(
&database_directory.join(id_store),
reused.len(),
)?
.is_some_and(|update| update.ids == reused))
}
impl Neo4jWritableStorage {
pub(crate) fn refresh_read_view(&mut self) -> Result<()> {
if self.transaction.is_some() {
return Ok(());
}
if self.poisoned {
return Err(Error::Transaction(
crate::TransactionErrorKind::StoragePoisoned,
));
}
let coordinator = Arc::clone(&self.coordinator);
let mut state = coordinator
.state
.lock()
.map_err(|_| lock_error("MVCC coordinator"))?;
if state.poisoned {
self.poisoned = true;
return Err(Error::Transaction(
crate::TransactionErrorKind::StoragePoisoned,
));
}
if self.snapshot_version != state.version {
let database_directory = self.storage.database_directory().to_path_buf();
if let Err(error) = self.reload_from_disk(&database_directory) {
self.poisoned = true;
state.poisoned = true;
return Err(error);
}
self.snapshot_version = state.version;
}
Ok(())
}
pub(crate) fn configure_workspace_spill(
&mut self,
threshold_bytes: Option<usize>,
max_spill_bytes: Option<u64>,
max_journal_bytes: Option<u64>,
transaction_id: Option<u64>,
) {
self.next_spill_threshold_bytes = threshold_bytes;
self.next_max_spill_bytes = max_spill_bytes;
self.next_max_journal_bytes = max_journal_bytes;
self.next_spill_transaction_id = transaction_id;
}
pub(crate) fn workspace_usage(&self) -> Result<crate::neo4j_backend::WorkspaceUsage> {
let Some(transaction) = &self.transaction else {
return Ok(crate::neo4j_backend::WorkspaceUsage::default());
};
transaction.workspace_usage()
}
pub(crate) fn validate_workspace_limits(
&self,
max_workspace_bytes: Option<usize>,
) -> Result<()> {
let bytes = self.storage.estimated_clone_heap_bytes()?;
if max_workspace_bytes.is_some_and(|limit| bytes > limit) {
Err(Error::Transaction(
crate::TransactionErrorKind::ResourceLimit,
))
} else {
Ok(())
}
}
pub(crate) fn savepoint_usage(&self) -> Result<crate::neo4j_backend::WorkspaceUsage> {
let transaction = self.active_transaction()?;
let mut usage = transaction.workspace_usage()?;
let storage_bytes = self.storage.estimated_clone_heap_bytes()?;
let embedded_storage_bytes = 2_usize
.checked_mul(std::mem::size_of::<Neo4jStorage>())
.ok_or(Error::Transaction(
crate::TransactionErrorKind::ResourceLimit,
))?;
let savepoint_fixed_bytes = std::mem::size_of::<Neo4jWritableSavepoint>()
.checked_sub(embedded_storage_bytes)
.ok_or(Error::Transaction(
crate::TransactionErrorKind::ResourceLimit,
))?;
usage.bytes = usage
.bytes
.checked_add(storage_bytes)
.and_then(|bytes| bytes.checked_add(savepoint_fixed_bytes))
.ok_or(Error::Transaction(
crate::TransactionErrorKind::ResourceLimit,
))?;
Ok(usage)
}
pub(crate) fn set_workspace_limits(
&mut self,
max_workspace_bytes: Option<usize>,
max_records: Option<usize>,
) -> Result<()> {
let transaction = self
.transaction
.as_mut()
.ok_or(Error::Transaction(crate::TransactionErrorKind::NotActive))?;
transaction.max_workspace_bytes = max_workspace_bytes;
transaction.max_records = max_records;
self.enforce_workspace_limits()
}
fn enforce_workspace_limits(&mut self) -> Result<()> {
let Some(transaction) = &self.transaction else {
return Err(Error::Transaction(crate::TransactionErrorKind::NotActive));
};
let usage = transaction.workspace_usage()?;
if transaction
.max_workspace_bytes
.is_some_and(|limit| usage.bytes > limit)
|| transaction
.max_records
.is_some_and(|limit| usage.records > limit)
{
self.mark_transaction_failed();
Err(Error::Transaction(
crate::TransactionErrorKind::ResourceLimit,
))
} else {
Ok(())
}
}
fn finish_mutation<T>(&mut self, value: T) -> Result<T> {
self.active_transaction_mut()?.refresh_write_set();
self.capture_before_images()?;
self.spill_workspace_if_needed()?;
self.enforce_workspace_limits()?;
Ok(value)
}
fn capture_before_images(&mut self) -> Result<()> {
let coordinator = Arc::clone(&self.coordinator);
let coordinator_state = coordinator
.state
.lock()
.map_err(|_| lock_error("MVCC coordinator"))?;
let snapshot_version = self.active_transaction()?.snapshot_version;
let dependencies = self.active_transaction()?.write_set.pending_fixed();
if dependencies.is_empty() {
return Ok(());
}
let database_directory = self.storage.database_directory().to_path_buf();
let format = self.storage.format();
let mut by_store: BTreeMap<FixedStore, Vec<_>> = BTreeMap::new();
for dependency in dependencies {
if let Some(image) = coordinator_state.before_image_at(dependency, snapshot_version) {
self.active_transaction_mut()?
.write_set
.capture_before_image(dependency, image.to_vec());
} else {
by_store
.entry(dependency.store)
.or_default()
.push(dependency);
}
}
for (store, dependencies) in by_store {
let (relative_path, record_size, record_type) = self.fixed_store_spec(store);
let path = database_directory.join(relative_path);
let layout = PageLayout::new(&path, format, record_size)?;
let mut reader = RecordReader::open(&path, record_type, layout)?;
for dependency in dependencies {
let image = reader.read(dependency.record_id)?;
self.active_transaction_mut()?
.write_set
.capture_before_image(dependency, image);
}
}
Ok(())
}
fn fixed_store_spec(&self, store: FixedStore) -> (&'static str, usize, &'static str) {
match store {
FixedStore::Property => (PROPERTY_STORE, PROPERTY_RECORD_SIZE, "property"),
FixedStore::Node => (NODE_STORE, NODE_RECORD_SIZE, "node"),
FixedStore::NodeLabel => (
NODE_LABEL_STORE,
self.node_label_layout.record_size,
"dynamic node label",
),
FixedStore::Relationship => {
(RELATIONSHIP_STORE, RELATIONSHIP_RECORD_SIZE, "relationship")
}
FixedStore::RelationshipGroup => (
RELATIONSHIP_GROUP_STORE,
RELATIONSHIP_GROUP_RECORD_SIZE,
"relationship group",
),
FixedStore::String => (
STRING_STORE,
self.string_layout.record_size,
"dynamic string",
),
FixedStore::Array => (ARRAY_STORE, self.array_layout.record_size, "dynamic array"),
FixedStore::PropertyKeyToken => (
PROPERTY_KEY_TOKEN_STORE,
PROPERTY_KEY_TOKEN_RECORD_SIZE,
"property key token",
),
FixedStore::PropertyKeyName => (
PROPERTY_KEY_NAME_STORE,
self.property_key_name_layout.record_size,
"dynamic property key name",
),
FixedStore::LabelToken => (LABEL_TOKEN_STORE, LABEL_TOKEN_RECORD_SIZE, "label token"),
FixedStore::LabelName => (
LABEL_NAME_STORE,
self.label_name_layout.record_size,
"dynamic label name",
),
FixedStore::RelationshipTypeToken => (
RELATIONSHIP_TYPE_TOKEN_STORE,
LABEL_TOKEN_RECORD_SIZE,
"relationship type token",
),
FixedStore::RelationshipTypeName => (
RELATIONSHIP_TYPE_NAME_STORE,
self.relationship_type_name_layout.record_size,
"dynamic relationship type name",
),
}
}
fn fixed_dependencies_match(
&self,
transaction: &TransactionWorkspace,
coordinator_state: &MvccState,
) -> Result<bool> {
let Some(dependencies) = transaction.write_set.rebase_dependencies() else {
return Ok(false);
};
let database_directory = self.storage.database_directory();
let format = self.storage.format();
let mut readers = BTreeMap::new();
for (dependency, before_image) in dependencies {
if coordinator_state.record_changed_after(dependency, transaction.snapshot_version) {
return Ok(false);
}
if let std::collections::btree_map::Entry::Vacant(entry) =
readers.entry(dependency.store)
{
let (relative_path, record_size, record_type) =
self.fixed_store_spec(dependency.store);
let path = database_directory.join(relative_path);
let layout = PageLayout::new(&path, format, record_size)?;
entry.insert(RecordReader::open(path, record_type, layout)?);
}
let current = readers
.get_mut(&dependency.store)
.ok_or_else(|| Error::Validation("MVCC record reader is missing".to_owned()))?
.read(dependency.record_id)?;
if current != before_image {
return Ok(false);
}
}
if transaction.write_set.requires_reused_id_validation() {
self.reused_record_ids_match(transaction)
} else {
Ok(true)
}
}
fn reused_record_ids_match(&self, transaction: &TransactionWorkspace) -> Result<bool> {
let database_directory = self.storage.database_directory();
macro_rules! validate {
($store:expr, $header:expr, $allocated:expr, $reused:expr, $freed:expr) => {
if !reusable_ids_still_match(
database_directory,
$store,
$header,
$allocated,
$reused,
$freed,
)? {
return Ok(false);
}
};
}
validate!(
PROPERTY_ID_STORE,
self.property_id_header,
transaction.allocated_property_records,
&transaction.reused_property_records,
&transaction.freed_property_records
);
validate!(
STRING_ID_STORE,
self.string_id_header,
transaction.allocated_string_records,
&transaction.reused_string_records,
&transaction.freed_string_records
);
validate!(
ARRAY_ID_STORE,
self.array_id_header,
transaction.allocated_array_records,
&transaction.reused_array_records,
&transaction.freed_array_records
);
validate!(
NODE_ID_STORE,
self.node_id_header,
transaction.allocated_node_records,
&transaction.reused_node_records,
&transaction.freed_node_records
);
validate!(
NODE_LABEL_ID_STORE,
self.node_label_id_header,
transaction.allocated_node_label_records,
&transaction.reused_node_label_records,
&transaction.freed_node_label_records
);
validate!(
RELATIONSHIP_ID_STORE,
self.relationship_id_header,
transaction.allocated_relationship_records,
&transaction.reused_relationship_records,
&transaction.freed_relationship_records
);
validate!(
RELATIONSHIP_GROUP_ID_STORE,
self.relationship_group_id_header,
transaction.allocated_relationship_group_records,
&transaction.reused_relationship_group_records,
&transaction.freed_relationship_group_records
);
if !reusable_token_ids_still_match(
database_directory,
PROPERTY_KEY_TOKEN_ID_STORE,
&transaction.reused_property_key_tokens,
)? || !reusable_token_ids_still_match(
database_directory,
LABEL_TOKEN_ID_STORE,
&transaction.reused_label_tokens,
)? || !reusable_token_ids_still_match(
database_directory,
RELATIONSHIP_TYPE_TOKEN_ID_STORE,
&transaction.reused_relationship_type_tokens,
)? {
return Ok(false);
}
Ok(true)
}
fn spill_workspace_if_needed(&mut self) -> Result<()> {
let transaction = self.active_transaction_mut()?;
let Some(threshold) = transaction.spill_threshold_bytes else {
return Ok(());
};
if transaction.hot_record_bytes()? <= threshold {
return Ok(());
}
transaction.spill_record_maps()
}
pub(crate) fn open(database_directory: impl AsRef<Path>) -> Result<Self> {
let database_directory = database_directory.as_ref();
let coordinator = mvcc_coordinator(database_directory)?;
let coordinator_state = coordinator
.state
.lock()
.map_err(|_| lock_error("MVCC coordinator"))?;
if coordinator_state.poisoned {
return Err(Error::Transaction(
crate::TransactionErrorKind::StoragePoisoned,
));
}
let snapshot_version = coordinator_state.version;
let storage = Neo4jStorage::open_unlocked(database_directory)?;
super::schema::validate_writable(database_directory, storage.format())?;
let property_path = database_directory.join(PROPERTY_STORE);
let property_layout =
PageLayout::new(&property_path, storage.format(), PROPERTY_RECORD_SIZE)?;
let property_records = std::fs::metadata(&property_path)
.map_err(|source| crate::Neo4jStoreError::Read {
path: property_path,
source,
})?
.len();
let property_record_capacity = property_layout.record_count(property_records);
let property_id_header = super::id_generator::validate_writable(
database_directory,
PROPERTY_STORE,
property_record_capacity,
)?;
let string_path = database_directory.join(STRING_STORE);
let string_layout = super::dynamic::store_layout(&string_path, storage.format())?;
let string_id_header = super::id_generator::validate_writable(
database_directory,
STRING_STORE,
string_layout.record_capacity,
)?;
let array_path = database_directory.join(ARRAY_STORE);
let array_layout = super::dynamic::store_layout(&array_path, storage.format())?;
let array_id_header = super::id_generator::validate_writable(
database_directory,
ARRAY_STORE,
array_layout.record_capacity,
)?;
let property_key_token_path = database_directory.join(PROPERTY_KEY_TOKEN_STORE);
let property_key_token_layout = PageLayout::new(
&property_key_token_path,
storage.format(),
PROPERTY_KEY_TOKEN_RECORD_SIZE,
)?;
let property_key_token_file_size = std::fs::metadata(&property_key_token_path)
.map_err(|source| crate::Neo4jStoreError::Read {
path: property_key_token_path,
source,
})?
.len();
let property_key_token_capacity =
property_key_token_layout.record_count(property_key_token_file_size);
let property_key_token_id_header = super::id_generator::validate_writable(
database_directory,
PROPERTY_KEY_TOKEN_STORE,
property_key_token_capacity,
)?;
let property_key_name_path = database_directory.join(PROPERTY_KEY_NAME_STORE);
let property_key_name_layout =
super::dynamic::store_layout(&property_key_name_path, storage.format())?;
let property_key_name_id_header = super::id_generator::validate_writable(
database_directory,
PROPERTY_KEY_NAME_STORE,
property_key_name_layout.record_capacity,
)?;
let label_token_path = database_directory.join(LABEL_TOKEN_STORE);
let label_token_layout =
PageLayout::new(&label_token_path, storage.format(), LABEL_TOKEN_RECORD_SIZE)?;
let label_token_file_size = std::fs::metadata(&label_token_path)
.map_err(|source| crate::Neo4jStoreError::Read {
path: label_token_path,
source,
})?
.len();
let label_token_capacity = label_token_layout.record_count(label_token_file_size);
let label_token_id_header = super::id_generator::validate_writable(
database_directory,
LABEL_TOKEN_STORE,
label_token_capacity,
)?;
let label_name_path = database_directory.join(LABEL_NAME_STORE);
let label_name_layout = super::dynamic::store_layout(&label_name_path, storage.format())?;
let label_name_id_header = super::id_generator::validate_writable(
database_directory,
LABEL_NAME_STORE,
label_name_layout.record_capacity,
)?;
let relationship_type_token_path = database_directory.join(RELATIONSHIP_TYPE_TOKEN_STORE);
let relationship_type_token_layout = PageLayout::new(
&relationship_type_token_path,
storage.format(),
LABEL_TOKEN_RECORD_SIZE,
)?;
let relationship_type_token_file_size = std::fs::metadata(&relationship_type_token_path)
.map_err(|source| crate::Neo4jStoreError::Read {
path: relationship_type_token_path,
source,
})?
.len();
let relationship_type_token_capacity =
relationship_type_token_layout.record_count(relationship_type_token_file_size);
let relationship_type_token_id_header = super::id_generator::validate_writable(
database_directory,
RELATIONSHIP_TYPE_TOKEN_STORE,
relationship_type_token_capacity,
)?;
let relationship_type_name_path = database_directory.join(RELATIONSHIP_TYPE_NAME_STORE);
let relationship_type_name_layout =
super::dynamic::store_layout(&relationship_type_name_path, storage.format())?;
let relationship_type_name_id_header = super::id_generator::validate_writable(
database_directory,
RELATIONSHIP_TYPE_NAME_STORE,
relationship_type_name_layout.record_capacity,
)?;
let node_path = database_directory.join(NODE_STORE);
let node_layout = PageLayout::new(&node_path, storage.format(), NODE_RECORD_SIZE)?;
let node_file_size = std::fs::metadata(&node_path)
.map_err(|source| crate::Neo4jStoreError::Read {
path: node_path,
source,
})?
.len();
let node_record_capacity = node_layout.record_count(node_file_size);
let node_id_header = super::id_generator::validate_writable(
database_directory,
NODE_STORE,
node_record_capacity,
)?;
let node_label_path = database_directory.join(NODE_LABEL_STORE);
let node_label_layout = super::dynamic::store_layout(&node_label_path, storage.format())?;
let node_label_id_header = super::id_generator::validate_writable(
database_directory,
NODE_LABEL_STORE,
node_label_layout.record_capacity,
)?;
let relationship_path = database_directory.join(RELATIONSHIP_STORE);
let relationship_layout = PageLayout::new(
&relationship_path,
storage.format(),
RELATIONSHIP_RECORD_SIZE,
)?;
let relationship_file_size = std::fs::metadata(&relationship_path)
.map_err(|source| crate::Neo4jStoreError::Read {
path: relationship_path,
source,
})?
.len();
let relationship_record_capacity = relationship_layout.record_count(relationship_file_size);
let relationship_id_header = super::id_generator::validate_writable(
database_directory,
RELATIONSHIP_STORE,
relationship_record_capacity,
)?;
let relationship_group_path = database_directory.join(RELATIONSHIP_GROUP_STORE);
let relationship_group_layout = PageLayout::new(
&relationship_group_path,
storage.format(),
RELATIONSHIP_GROUP_RECORD_SIZE,
)?;
let relationship_group_file_size = std::fs::metadata(&relationship_group_path)
.map_err(|source| crate::Neo4jStoreError::Read {
path: relationship_group_path,
source,
})?
.len();
let relationship_group_record_capacity =
relationship_group_layout.record_count(relationship_group_file_size);
let relationship_group_id_header = super::id_generator::validate_writable(
database_directory,
RELATIONSHIP_GROUP_STORE,
relationship_group_record_capacity,
)?;
super::counts::validate_relationship_group_degrees(
&database_directory.join(RELATIONSHIP_GROUP_DEGREE_STORE),
)?;
drop(coordinator_state);
Ok(Self {
storage,
coordinator,
snapshot_version,
transaction: None,
snapshot_registration: None,
poisoned: false,
next_spill_threshold_bytes: None,
next_max_spill_bytes: None,
next_max_journal_bytes: None,
next_spill_transaction_id: None,
property_id_header,
property_record_capacity,
string_id_header,
string_layout,
array_id_header,
array_layout,
property_key_token_id_header,
property_key_token_capacity,
property_key_name_id_header,
property_key_name_layout,
label_token_id_header,
label_token_capacity,
label_name_id_header,
label_name_layout,
relationship_type_token_id_header,
relationship_type_token_capacity,
relationship_type_name_id_header,
relationship_type_name_layout,
node_id_header,
node_record_capacity,
node_label_id_header,
node_label_layout,
relationship_id_header,
relationship_record_capacity,
relationship_group_id_header,
relationship_group_record_capacity,
})
}
fn active_transaction_mut(&mut self) -> Result<&mut TransactionWorkspace> {
match self.transaction.as_mut() {
Some(transaction) if transaction.state == TransactionState::Active => Ok(transaction),
Some(_) => Err(Error::Transaction(
crate::TransactionErrorKind::RollbackOnly,
)),
None => Err(Error::Transaction(crate::TransactionErrorKind::NotActive)),
}
}
pub(crate) fn savepoint(&self) -> Result<Neo4jWritableSavepoint> {
let transaction = self.active_transaction()?;
Ok(Neo4jWritableSavepoint {
storage: self.storage.clone(),
transaction: transaction.clone(),
})
}
pub(crate) fn rollback_to_savepoint(
&mut self,
savepoint: &Neo4jWritableSavepoint,
) -> Result<()> {
if self.transaction.is_none() {
return Err(Error::Transaction(crate::TransactionErrorKind::NotActive));
}
self.storage = savepoint.storage.clone();
self.transaction = Some(savepoint.transaction.clone());
Ok(())
}
fn active_transaction(&self) -> Result<&TransactionWorkspace> {
match self.transaction.as_ref() {
Some(transaction) if transaction.state == TransactionState::Active => Ok(transaction),
Some(_) => Err(Error::Transaction(
crate::TransactionErrorKind::RollbackOnly,
)),
None => Err(Error::Transaction(crate::TransactionErrorKind::NotActive)),
}
}
fn prepare_workspace_dynamic_record_deletions(
&self,
store_name: &str,
record_type: &str,
record_ids: &[u64],
context: &str,
) -> Result<Vec<(u64, Vec<u8>)>> {
let transaction = self.active_transaction()?;
let pending_records = match store_name {
STRING_STORE => &transaction.string_records,
ARRAY_STORE => &transaction.array_records,
NODE_LABEL_STORE => &transaction.node_label_records,
_ => {
return Err(Error::Validation(format!(
"unsupported dynamic workspace store {store_name}"
)))
}
};
let mut deletions = Vec::with_capacity(record_ids.len());
for record_id in record_ids {
if let Some(bytes) = pending_records.get(record_id)? {
if bytes.first().is_none_or(|byte| byte & 0x10 == 0) {
return Err(Error::Validation(format!(
"{context}: pending {record_type} record {record_id} is not in use"
)));
}
let mut bytes = bytes;
bytes[0] &= !0x10;
deletions.push((*record_id, bytes));
} else {
deletions.extend(super::dynamic::prepare_record_deletions(
&self.storage.database_directory().join(store_name),
self.storage.format(),
record_type,
&[*record_id],
context,
)?);
}
}
Ok(deletions)
}
fn prepare_workspace_dynamic_chain_deletion(
&self,
store_name: &str,
record_type: &str,
first_id: u64,
context: &str,
) -> Result<Vec<(u64, Vec<u8>)>> {
let transaction = self.active_transaction()?;
let pending_chain = match store_name {
STRING_STORE => transaction.string_chain_ids.get(&first_id),
ARRAY_STORE => transaction.array_chain_ids.get(&first_id),
NODE_LABEL_STORE => transaction.node_label_chain_ids.get(&first_id),
_ => None,
};
if let Some(record_ids) = pending_chain {
self.prepare_workspace_dynamic_record_deletions(
store_name,
record_type,
record_ids,
context,
)
} else {
super::dynamic::prepare_chain_deletion(
&self.storage.database_directory().join(store_name),
self.storage.format(),
record_type,
first_id,
context,
)
}
}
fn reload_from_disk(&mut self, database_directory: &Path) -> Result<()> {
let storage = Neo4jStorage::open_unlocked(database_directory)?;
let property_id_header = super::id_generator::validate_writable(
database_directory,
PROPERTY_STORE,
self.property_record_capacity,
)?;
let string_id_header = super::id_generator::validate_writable(
database_directory,
STRING_STORE,
self.string_layout.record_capacity,
)?;
let array_id_header = super::id_generator::validate_writable(
database_directory,
ARRAY_STORE,
self.array_layout.record_capacity,
)?;
let property_key_token_id_header = super::id_generator::validate_writable(
database_directory,
PROPERTY_KEY_TOKEN_STORE,
self.property_key_token_capacity,
)?;
let property_key_name_id_header = super::id_generator::validate_writable(
database_directory,
PROPERTY_KEY_NAME_STORE,
self.property_key_name_layout.record_capacity,
)?;
let label_token_id_header = super::id_generator::validate_writable(
database_directory,
LABEL_TOKEN_STORE,
self.label_token_capacity,
)?;
let label_name_id_header = super::id_generator::validate_writable(
database_directory,
LABEL_NAME_STORE,
self.label_name_layout.record_capacity,
)?;
let relationship_type_token_id_header = super::id_generator::validate_writable(
database_directory,
RELATIONSHIP_TYPE_TOKEN_STORE,
self.relationship_type_token_capacity,
)?;
let relationship_type_name_id_header = super::id_generator::validate_writable(
database_directory,
RELATIONSHIP_TYPE_NAME_STORE,
self.relationship_type_name_layout.record_capacity,
)?;
let node_id_header = super::id_generator::validate_writable(
database_directory,
NODE_STORE,
self.node_record_capacity,
)?;
let node_label_id_header = super::id_generator::validate_writable(
database_directory,
NODE_LABEL_STORE,
self.node_label_layout.record_capacity,
)?;
let relationship_id_header = super::id_generator::validate_writable(
database_directory,
RELATIONSHIP_STORE,
self.relationship_record_capacity,
)?;
let relationship_group_id_header = super::id_generator::validate_writable(
database_directory,
RELATIONSHIP_GROUP_STORE,
self.relationship_group_record_capacity,
)?;
self.storage = storage;
self.property_id_header = property_id_header;
self.string_id_header = string_id_header;
self.array_id_header = array_id_header;
self.property_key_token_id_header = property_key_token_id_header;
self.property_key_name_id_header = property_key_name_id_header;
self.label_token_id_header = label_token_id_header;
self.label_name_id_header = label_name_id_header;
self.relationship_type_token_id_header = relationship_type_token_id_header;
self.relationship_type_name_id_header = relationship_type_name_id_header;
self.node_id_header = node_id_header;
self.node_label_id_header = node_label_id_header;
self.relationship_id_header = relationship_id_header;
self.relationship_group_id_header = relationship_group_id_header;
Ok(())
}
fn relationship_count_keys(
&mut self,
start: NodeId,
end: NodeId,
type_id: u32,
) -> Result<Vec<super::counts::RelationshipCountKey>> {
let convert_label_ids = |labels: Vec<u32>| {
labels
.into_iter()
.map(|label_id| {
i32::try_from(label_id).map_err(|_| {
Error::UnsupportedWriteQuery(format!(
"label token ID {label_id} exceeds relationship counts key range"
))
})
})
.collect::<Result<Vec<_>>>()
};
let type_id = i32::try_from(type_id).map_err(|_| {
Error::UnsupportedWriteQuery(
"relationship type token ID exceeds counts key range".to_owned(),
)
})?;
let start_labels = convert_label_ids(self.storage.node_label_ids(start)?)?;
let end_labels = convert_label_ids(self.storage.node_label_ids(end)?)?;
let mut keys = vec![
super::counts::RelationshipCountKey {
start_label_id: -1,
type_id: -1,
end_label_id: -1,
},
super::counts::RelationshipCountKey {
start_label_id: -1,
type_id,
end_label_id: -1,
},
];
keys.extend(start_labels.into_iter().flat_map(|label_id| {
[
super::counts::RelationshipCountKey {
start_label_id: label_id,
type_id: -1,
end_label_id: -1,
},
super::counts::RelationshipCountKey {
start_label_id: label_id,
type_id,
end_label_id: -1,
},
]
}));
keys.extend(end_labels.into_iter().flat_map(|label_id| {
[
super::counts::RelationshipCountKey {
start_label_id: -1,
type_id: -1,
end_label_id: label_id,
},
super::counts::RelationshipCountKey {
start_label_id: -1,
type_id,
end_label_id: label_id,
},
]
}));
Ok(keys)
}
fn apply_relationship_counts_for_label_change(
&mut self,
node: NodeId,
label_id: u32,
delta: i64,
) -> Result<()> {
let label_id = i32::try_from(label_id).map_err(|_| {
Error::UnsupportedWriteQuery("label token ID exceeds counts key range".to_owned())
})?;
let neo4j_node_id = self.storage.node_neo4j_id(node)?;
let mut changes = Vec::new();
for relationship in &self.storage.relationships.records {
let type_id = i32::try_from(relationship.type_id).map_err(|_| {
Error::UnsupportedWriteQuery(
"relationship type token ID exceeds counts key range".to_owned(),
)
})?;
if relationship.start == neo4j_node_id {
changes.push(super::counts::RelationshipCountKey {
start_label_id: label_id,
type_id: -1,
end_label_id: -1,
});
changes.push(super::counts::RelationshipCountKey {
start_label_id: label_id,
type_id,
end_label_id: -1,
});
}
if relationship.end == neo4j_node_id {
changes.push(super::counts::RelationshipCountKey {
start_label_id: -1,
type_id: -1,
end_label_id: label_id,
});
changes.push(super::counts::RelationshipCountKey {
start_label_id: -1,
type_id,
end_label_id: label_id,
});
}
}
let transaction = self.active_transaction_mut()?;
for key in changes {
let entry = transaction
.relationship_count_deltas
.entry(key)
.or_default();
*entry = entry.checked_add(delta).ok_or_else(|| {
Error::UnsupportedWriteQuery("relationship count delta overflow".to_owned())
})?;
if *entry == 0 {
transaction.relationship_count_deltas.remove(&key);
}
}
Ok(())
}
fn prepare_property_chain_deletion(
&self,
first_property: Option<u64>,
context: &str,
) -> Result<EntityPropertyDeletion> {
let deletion = self
.storage
.properties
.prepare_chain_deletion(first_property)
.map_err(Error::UnsupportedWriteQuery)?;
let mut strings = Vec::new();
for first_id in deletion.dynamic_strings {
strings.extend(self.prepare_workspace_dynamic_chain_deletion(
STRING_STORE,
"dynamic string property",
first_id,
context,
)?);
}
let mut arrays = Vec::new();
for first_id in deletion.dynamic_arrays {
arrays.extend(self.prepare_workspace_dynamic_chain_deletion(
ARRAY_STORE,
"dynamic array property",
first_id,
context,
)?);
}
Ok(EntityPropertyDeletion {
property_records: deletion.property_records,
strings,
arrays,
})
}
fn collect_property_chain_deletion(&mut self, deletion: EntityPropertyDeletion) -> Result<()> {
let transaction = self.active_transaction_mut()?;
for (record_id, bytes) in deletion.property_records {
transaction.property_records.insert(record_id, bytes);
transaction.freed_property_records.insert(record_id);
}
for (record_id, bytes) in deletion.strings {
transaction.string_records.insert(record_id, bytes);
transaction.freed_string_records.insert(record_id);
}
for (record_id, bytes) in deletion.arrays {
transaction.array_records.insert(record_id, bytes);
transaction.freed_array_records.insert(record_id);
}
Ok(())
}
fn prepare_dynamic_property_deletion(
&self,
existing: Option<super::property::DynamicPropertyReference>,
retained_record_id: Option<u64>,
) -> Result<Option<DynamicPropertyDeletion>> {
let Some(existing) = existing else {
return Ok(None);
};
if retained_record_id == Some(existing.first_record_id) {
return Ok(None);
}
let (store_name, record_type) = match existing.kind {
super::property::DynamicPropertyKind::String => {
(STRING_STORE, "dynamic string property")
}
super::property::DynamicPropertyKind::Array => (ARRAY_STORE, "dynamic array property"),
};
let records = self.prepare_workspace_dynamic_chain_deletion(
store_name,
record_type,
existing.first_record_id,
"dynamic property replacement",
)?;
Ok(Some(DynamicPropertyDeletion {
kind: existing.kind,
records,
}))
}
fn collect_dynamic_property_deletion(
&mut self,
deletion: Option<DynamicPropertyDeletion>,
) -> Result<()> {
let Some(deletion) = deletion else {
return Ok(());
};
let transaction = self.active_transaction_mut()?;
for (record_id, bytes) in deletion.records {
match deletion.kind {
super::property::DynamicPropertyKind::String => {
transaction.string_records.insert(record_id, bytes);
transaction.freed_string_records.insert(record_id);
}
super::property::DynamicPropertyKind::Array => {
transaction.array_records.insert(record_id, bytes);
transaction.freed_array_records.insert(record_id);
}
}
}
Ok(())
}
fn property_allocation_candidate(&mut self) -> Result<(Option<u64>, bool)> {
let transaction = self.active_transaction()?;
if transaction.freed_property_records.is_empty() {
let reuse_count = transaction
.reused_property_records
.len()
.checked_add(1)
.ok_or_else(|| {
Error::UnsupportedWriteQuery("property reuse count overflow".to_owned())
})?;
if self.property_id_header.num_unused_ids >= reuse_count as u64 {
let path = self.storage.database_directory().join(PROPERTY_ID_STORE);
if let Some(record_id) =
super::id_generator::prepare_take_free_ids(&path, reuse_count)?.and_then(
|update| {
next_reusable_id(&update.ids, &transaction.reused_property_records)
},
)
{
self.storage.properties.remove_record_for_reuse(record_id);
return Ok((Some(record_id), true));
}
}
}
let allocated = transaction.allocated_property_records;
Ok((
self.property_id_header
.high_id
.checked_add(allocated)
.filter(|id| *id < self.property_record_capacity),
false,
))
}
fn string_allocation_ids(&mut self, count: usize) -> Result<(Vec<u64>, u64)> {
let transaction = self.active_transaction()?;
if transaction.freed_string_records.is_empty() {
let total = transaction
.reused_string_records
.len()
.checked_add(count)
.ok_or_else(|| {
Error::UnsupportedWriteQuery("string reuse count overflow".to_owned())
})?;
if self.string_id_header.num_unused_ids >= total as u64 {
let path = self.storage.database_directory().join(STRING_ID_STORE);
if let Some(update) = super::id_generator::prepare_take_free_ids(&path, total)? {
if update.ids.len() == total
&& update.ids[..transaction.reused_string_records.len()]
== transaction.reused_string_records
{
let ids = update.ids[transaction.reused_string_records.len()..].to_vec();
return Ok((ids, 0));
}
}
}
}
let mut ids = Vec::with_capacity(count);
let allocated = extend_dynamic_chain_ids(
&mut ids,
count,
self.string_id_header.high_id,
transaction.allocated_string_records,
self.string_layout.record_capacity,
"string",
)?;
Ok((ids, allocated))
}
fn array_allocation_ids(&mut self, count: usize) -> Result<(Vec<u64>, u64)> {
let transaction = self.active_transaction()?;
if transaction.freed_array_records.is_empty() {
let total = transaction
.reused_array_records
.len()
.checked_add(count)
.ok_or_else(|| {
Error::UnsupportedWriteQuery("array reuse count overflow".to_owned())
})?;
if self.array_id_header.num_unused_ids >= total as u64 {
let path = self.storage.database_directory().join(ARRAY_ID_STORE);
if let Some(update) = super::id_generator::prepare_take_free_ids(&path, total)? {
if update.ids.len() == total
&& update.ids[..transaction.reused_array_records.len()]
== transaction.reused_array_records
{
let ids = update.ids[transaction.reused_array_records.len()..].to_vec();
return Ok((ids, 0));
}
}
}
}
let mut ids = Vec::with_capacity(count);
let allocated = extend_dynamic_chain_ids(
&mut ids,
count,
self.array_id_header.high_id,
transaction.allocated_array_records,
self.array_layout.record_capacity,
"array",
)?;
Ok((ids, allocated))
}
fn node_label_allocation_ids(&mut self, count: usize) -> Result<(Vec<u64>, u64)> {
let transaction = self.active_transaction()?;
if transaction.freed_node_label_records.is_empty() {
let total = transaction
.reused_node_label_records
.len()
.checked_add(count)
.ok_or_else(|| {
Error::UnsupportedWriteQuery("node-label reuse count overflow".to_owned())
})?;
if self.node_label_id_header.num_unused_ids >= total as u64 {
let path = self.storage.database_directory().join(NODE_LABEL_ID_STORE);
if let Some(update) = super::id_generator::prepare_take_free_ids(&path, total)? {
if update.ids.len() == total
&& update.ids[..transaction.reused_node_label_records.len()]
== transaction.reused_node_label_records
{
let ids =
update.ids[transaction.reused_node_label_records.len()..].to_vec();
return Ok((ids, 0));
}
}
}
}
let mut ids = Vec::with_capacity(count);
let allocated = extend_dynamic_chain_ids(
&mut ids,
count,
self.node_label_id_header.high_id,
transaction.allocated_node_label_records,
self.node_label_layout.record_capacity,
"node-label",
)?;
if ids.iter().any(|id| *id > MAX_DYNAMIC_LABEL_REFERENCE) {
return Err(Error::UnsupportedWriteQuery(
"dynamic node-label record ID exceeds the node record field".to_owned(),
));
}
Ok((ids, allocated))
}
fn node_allocation_candidate(&self) -> Result<(u64, bool)> {
let transaction = self.active_transaction()?;
let reuse_count = transaction
.reused_node_records
.len()
.checked_add(1)
.ok_or_else(|| Error::UnsupportedWriteQuery("node reuse count overflow".to_owned()))?;
if self.node_id_header.num_unused_ids >= reuse_count as u64 {
let path = self.storage.database_directory().join(NODE_ID_STORE);
if let Some(record_id) = super::id_generator::prepare_take_free_ids(&path, reuse_count)?
.and_then(|update| next_reusable_id(&update.ids, &transaction.reused_node_records))
{
return Ok((record_id, true));
}
}
let transaction = self.active_transaction()?;
let record_id = self
.node_id_header
.high_id
.checked_add(transaction.allocated_node_records)
.filter(|id| *id < self.node_record_capacity)
.ok_or_else(|| {
Error::UnsupportedWriteQuery(
"node store has no preallocated record capacity".to_owned(),
)
})?;
Ok((record_id, false))
}
fn relationship_allocation_candidate(&self) -> Result<(u64, bool)> {
let transaction = self.active_transaction()?;
let reuse_count = transaction
.reused_relationship_records
.len()
.checked_add(1)
.ok_or_else(|| {
Error::UnsupportedWriteQuery("relationship reuse count overflow".to_owned())
})?;
if self.relationship_id_header.num_unused_ids >= reuse_count as u64 {
let path = self
.storage
.database_directory()
.join(RELATIONSHIP_ID_STORE);
if let Some(record_id) = super::id_generator::prepare_take_free_ids(&path, reuse_count)?
.and_then(|update| {
next_reusable_id(&update.ids, &transaction.reused_relationship_records)
})
{
return Ok((record_id, true));
}
}
let transaction = self.active_transaction()?;
let record_id = self
.relationship_id_header
.high_id
.checked_add(transaction.allocated_relationship_records)
.filter(|id| *id < self.relationship_record_capacity)
.ok_or_else(|| {
Error::UnsupportedWriteQuery(
"relationship store has no preallocated record capacity".to_owned(),
)
})?;
Ok((record_id, false))
}
fn relationship_group_allocation_candidate(&self) -> Result<(u64, bool)> {
let transaction = self.active_transaction()?;
let reuse_count = transaction
.reused_relationship_group_records
.len()
.checked_add(1)
.ok_or_else(|| {
Error::UnsupportedWriteQuery("relationship group reuse count overflow".to_owned())
})?;
if self.relationship_group_id_header.num_unused_ids >= reuse_count as u64 {
let path = self
.storage
.database_directory()
.join(RELATIONSHIP_GROUP_ID_STORE);
if let Some(record_id) = super::id_generator::prepare_take_free_ids(&path, reuse_count)?
.and_then(|update| {
next_reusable_id(&update.ids, &transaction.reused_relationship_group_records)
})
{
return Ok((record_id, true));
}
}
let pending_allocations = self
.active_transaction()?
.allocated_relationship_group_records;
let record_id = self
.relationship_group_id_header
.high_id
.checked_add(pending_allocations)
.filter(|id| *id < self.relationship_group_record_capacity)
.ok_or_else(|| {
Error::UnsupportedWriteQuery(
"relationship group store has no preallocated record capacity".to_owned(),
)
})?;
Ok((record_id, false))
}
fn ensure_dense_relationship_group(&mut self, node_id: NodeId, type_id: u32) -> Result<()> {
if self
.storage
.has_dense_relationship_group(node_id, type_id)?
{
return Ok(());
}
let pending_allocations = self
.active_transaction()?
.allocated_relationship_group_records;
let (group_id, reused_record) = self.relationship_group_allocation_candidate()?;
let creation = self
.storage
.create_dense_relationship_group(node_id, type_id, group_id)?;
self.active_transaction_mut()?
.relationship_group_records
.insert(group_id, creation.group_patch);
if let Some((record_id, bytes)) = creation.previous_patch {
self.active_transaction_mut()?
.relationship_group_records
.insert(record_id, bytes);
}
if let Some(node_head) = creation.node_head {
let node_neo4j_id = self.storage.node_neo4j_id(node_id)?;
self.stage_node_relationship_head(node_neo4j_id, node_head)?;
}
if reused_record {
self.active_transaction_mut()?
.reused_relationship_group_records
.push(group_id);
} else {
self.active_transaction_mut()?
.allocated_relationship_group_records =
pending_allocations.checked_add(1).ok_or_else(|| {
Error::UnsupportedWriteQuery(
"relationship group allocation overflow".to_owned(),
)
})?;
}
Ok(())
}
fn stage_node_head(&mut self, record_id: u64, new_head: u64) -> Result<()> {
let database_directory = self.storage.database_directory().to_path_buf();
let format = self.storage.format();
let transaction = self.active_transaction_mut()?;
if let Some(bytes) = transaction.node_records.get_mut(&record_id)? {
super::node::set_next_property(bytes, new_head);
} else {
let bytes = super::node::read_with_next_property(
&database_directory,
format,
record_id,
new_head,
)?;
transaction.node_records.insert(record_id, bytes);
}
Ok(())
}
fn stage_node_relationship_head(&mut self, record_id: u64, new_head: u64) -> Result<()> {
let database_directory = self.storage.database_directory().to_path_buf();
let format = self.storage.format();
let transaction = self.active_transaction_mut()?;
if let Some(bytes) = transaction.node_records.get_mut(&record_id)? {
super::node::set_next_relationship(bytes, new_head);
} else {
let bytes = super::node::read_with_next_relationship(
&database_directory,
format,
record_id,
new_head,
)?;
transaction.node_records.insert(record_id, bytes);
}
Ok(())
}
#[allow(dead_code)]
fn stage_node_dense_relationship_head(
&mut self,
record_id: u64,
relationship_group_id: u64,
) -> Result<()> {
let database_directory = self.storage.database_directory().to_path_buf();
let format = self.storage.format();
let transaction = self.active_transaction_mut()?;
if let Some(bytes) = transaction.node_records.get_mut(&record_id)? {
super::node::set_dense_relationship_head(bytes, relationship_group_id)
.map_err(Error::UnsupportedWriteQuery)?;
} else {
let mut bytes = super::node::read_with_next_relationship(
&database_directory,
format,
record_id,
relationship_group_id,
)?;
super::node::set_dense_relationship_head(&mut bytes, relationship_group_id)
.map_err(Error::UnsupportedWriteQuery)?;
transaction.node_records.insert(record_id, bytes);
}
Ok(())
}
fn stage_relationship_group_degree_delta(
&mut self,
group_id: u64,
direction: u8,
delta: i64,
) -> Result<()> {
if delta == 0 {
return Ok(());
}
if direction > 2 {
return Err(Error::UnsupportedWriteQuery(
"relationship group degree direction must be 0, 1, or 2".to_owned(),
));
}
let transaction = self.active_transaction_mut()?;
let key = super::counts::RelationshipGroupDegreeKey {
group_id,
direction,
};
let entry = transaction
.relationship_group_degree_deltas
.entry(key)
.or_default();
*entry = entry.checked_add(delta).ok_or_else(|| {
Error::UnsupportedWriteQuery("relationship group degree delta overflow".to_owned())
})?;
if *entry == 0 {
transaction.relationship_group_degree_deltas.remove(&key);
}
Ok(())
}
fn stage_relationship_head(&mut self, record_id: u64, new_head: u64) -> Result<()> {
let database_directory = self.storage.database_directory().to_path_buf();
let format = self.storage.format();
let transaction = self.active_transaction_mut()?;
if let Some(bytes) = transaction.relationship_records.get_mut(&record_id)? {
super::relationship::set_next_property(bytes, new_head);
} else {
let bytes = super::relationship::read_with_next_property(
&database_directory,
format,
record_id,
new_head,
)?;
transaction.relationship_records.insert(record_id, bytes);
}
Ok(())
}
fn collect_property_update(
&mut self,
update: super::StagedPropertyUpdate,
allocation_id: Option<u64>,
reused_candidate: bool,
) -> Result<()> {
if update.new_head.is_some() && update.new_head != allocation_id {
return Err(Error::Validation(
"property update used an unexpected allocation ID".to_owned(),
));
}
let transaction = self.active_transaction_mut()?;
for (record_id, bytes) in update.property_records {
transaction.property_records.insert(record_id, bytes);
}
if let Some(record_id) = update.new_head {
if reused_candidate {
transaction.reused_property_records.push(record_id);
return Ok(());
}
transaction.allocated_property_records = transaction
.allocated_property_records
.checked_add(1)
.ok_or_else(|| {
Error::UnsupportedWriteQuery("property allocation overflow".to_owned())
})?;
}
Ok(())
}
fn prepare_dynamic_string(
&mut self,
value: &Value,
existing: Option<super::property::DynamicPropertyReference>,
) -> Result<Option<DynamicStringUpdate>> {
let Value::String(value) = value else {
return Ok(None);
};
if value.len() <= 27 {
return Ok(None);
}
if let Some(existing) = existing {
if existing.kind != super::property::DynamicPropertyKind::String {
return Err(Error::UnsupportedWriteQuery(
"replacing a dynamic array requires dynamic record reclamation".to_owned(),
));
}
let mut ids = {
let transaction = self.active_transaction()?;
if let Some(ids) = transaction.string_chain_ids.get(&existing.first_record_id) {
ids.clone()
} else {
let path = self.storage.database_directory().join(STRING_STORE);
let mut store = super::dynamic::DynamicStore::open(
&path,
self.storage.format(),
"dynamic string property",
)?;
store
.read_chain_with_ids(
existing.first_record_id,
"existing dynamic string replacement",
)?
.1
}
};
let required_records = value.len().div_ceil(self.string_layout.payload_size);
let freed_records = if required_records < ids.len() {
let freed_ids = ids.split_off(required_records);
self.prepare_workspace_dynamic_record_deletions(
STRING_STORE,
"dynamic string property",
&freed_ids,
"dynamic string shrink",
)?
} else {
Vec::new()
};
let additional_records = required_records.saturating_sub(ids.len());
let (new_ids, allocated_records) = if additional_records > 0 {
self.string_allocation_ids(additional_records)?
} else {
(Vec::new(), 0)
};
let reused_records = if allocated_records == 0 {
new_ids.clone()
} else {
Vec::new()
};
ids.extend(new_ids);
let records =
super::dynamic::encode_chain_for_ids(value.as_bytes(), &ids, self.string_layout)
.map_err(Error::UnsupportedWriteQuery)?;
return Ok(Some(DynamicStringUpdate {
first_id: existing.first_record_id,
allocated_records,
reused_records,
record_ids: ids,
records,
freed_records,
}));
}
let required_records = value.len().div_ceil(self.string_layout.payload_size);
let (record_ids, allocated_records) = self.string_allocation_ids(required_records)?;
let first_id = record_ids[0];
let records =
super::dynamic::encode_chain_for_ids(value.as_bytes(), &record_ids, self.string_layout)
.map_err(Error::UnsupportedWriteQuery)?;
Ok(Some(DynamicStringUpdate {
first_id,
allocated_records,
reused_records: if allocated_records == 0 {
record_ids.clone()
} else {
Vec::new()
},
record_ids,
records,
freed_records: Vec::new(),
}))
}
fn collect_dynamic_string(&mut self, update: Option<DynamicStringUpdate>) -> Result<()> {
let Some(update) = update else {
return Ok(());
};
let transaction = self.active_transaction_mut()?;
transaction.allocated_string_records = transaction
.allocated_string_records
.checked_add(update.allocated_records)
.ok_or_else(|| {
Error::UnsupportedWriteQuery("dynamic string allocation overflow".to_owned())
})?;
transaction
.reused_string_records
.extend(update.reused_records);
transaction
.string_chain_ids
.insert(update.first_id, update.record_ids);
for (record_id, bytes) in update.records {
transaction.string_records.insert(record_id, bytes);
}
for (record_id, bytes) in update.freed_records {
transaction.string_records.insert(record_id, bytes);
transaction.freed_string_records.insert(record_id);
}
Ok(())
}
fn prepare_dynamic_array(
&mut self,
value: &Value,
existing: Option<super::property::DynamicPropertyReference>,
) -> Result<Option<DynamicArrayUpdate>> {
let Some(payload) = super::property::encode_dynamic_array_payload(value)
.map_err(Error::UnsupportedWriteQuery)?
else {
return Ok(None);
};
if let Some(existing) = existing {
if existing.kind != super::property::DynamicPropertyKind::Array {
return Err(Error::UnsupportedWriteQuery(
"replacing a dynamic string requires dynamic record reclamation".to_owned(),
));
}
let mut ids = {
let transaction = self.active_transaction()?;
if let Some(ids) = transaction.array_chain_ids.get(&existing.first_record_id) {
ids.clone()
} else {
let path = self.storage.database_directory().join(ARRAY_STORE);
let mut store = super::dynamic::DynamicStore::open(
&path,
self.storage.format(),
"dynamic array property",
)?;
store
.read_chain_with_ids(
existing.first_record_id,
"existing dynamic array replacement",
)?
.1
}
};
let required_records = payload.len().div_ceil(self.array_layout.payload_size);
let freed_records = if required_records < ids.len() {
let freed_ids = ids.split_off(required_records);
self.prepare_workspace_dynamic_record_deletions(
ARRAY_STORE,
"dynamic array property",
&freed_ids,
"dynamic array shrink",
)?
} else {
Vec::new()
};
let additional_records = required_records.saturating_sub(ids.len());
let (new_ids, allocated_records) = if additional_records > 0 {
self.array_allocation_ids(additional_records)?
} else {
(Vec::new(), 0)
};
let reused_records = if allocated_records == 0 {
new_ids.clone()
} else {
Vec::new()
};
ids.extend(new_ids);
let records = super::dynamic::encode_chain_for_ids(&payload, &ids, self.array_layout)
.map_err(Error::UnsupportedWriteQuery)?;
return Ok(Some(DynamicArrayUpdate {
first_id: existing.first_record_id,
allocated_records,
reused_records,
record_ids: ids,
records,
freed_records,
}));
}
let required_records = payload.len().div_ceil(self.array_layout.payload_size);
let (record_ids, allocated_records) = self.array_allocation_ids(required_records)?;
let first_id = record_ids[0];
let records =
super::dynamic::encode_chain_for_ids(&payload, &record_ids, self.array_layout)
.map_err(Error::UnsupportedWriteQuery)?;
Ok(Some(DynamicArrayUpdate {
first_id,
allocated_records,
reused_records: if allocated_records == 0 {
record_ids.clone()
} else {
Vec::new()
},
record_ids,
records,
freed_records: Vec::new(),
}))
}
fn collect_dynamic_array(&mut self, update: Option<DynamicArrayUpdate>) -> Result<()> {
let Some(update) = update else {
return Ok(());
};
let transaction = self.active_transaction_mut()?;
transaction.allocated_array_records = transaction
.allocated_array_records
.checked_add(update.allocated_records)
.ok_or_else(|| {
Error::UnsupportedWriteQuery("dynamic array allocation overflow".to_owned())
})?;
transaction
.reused_array_records
.extend(update.reused_records);
transaction
.array_chain_ids
.insert(update.first_id, update.record_ids);
for (record_id, bytes) in update.records {
transaction.array_records.insert(record_id, bytes);
}
for (record_id, bytes) in update.freed_records {
transaction.array_records.insert(record_id, bytes);
transaction.freed_array_records.insert(record_id);
}
Ok(())
}
fn prepare_node_label_update(
&mut self,
record_id: u64,
label_ids: &[u32],
) -> Result<NodeLabelUpdate> {
let transaction = self.active_transaction()?;
let mut node_record = if let Some(bytes) = transaction.node_records.get(&record_id)? {
bytes
} else {
super::node::read_record(
self.storage.database_directory(),
self.storage.format(),
record_id,
)?
};
let current_field = super::node::label_field(&node_record);
if current_field & DYNAMIC_LABEL_FLAG != 0 {
let first_id = current_field & MAX_DYNAMIC_LABEL_REFERENCE;
let mut record_ids = if let Some(ids) = transaction.node_label_chain_ids.get(&first_id)
{
ids.clone()
} else {
let path = self.storage.database_directory().join(NODE_LABEL_STORE);
let mut store = super::dynamic::DynamicStore::open(
&path,
self.storage.format(),
"dynamic node label",
)?;
store
.read_chain_with_ids(
first_id,
&format!("node {record_id} dynamic label replacement"),
)?
.1
};
let inline_label_field = super::node::encode_inline_label_field(label_ids).ok();
let payload = if inline_label_field.is_some() {
Vec::new()
} else {
super::node::encode_dynamic_label_payload(record_id, label_ids)
.map_err(Error::UnsupportedWriteQuery)?
};
let required_records = payload.len().div_ceil(self.node_label_layout.payload_size);
let freed_records = if required_records < record_ids.len() {
let freed_ids = record_ids.split_off(required_records);
self.prepare_workspace_dynamic_record_deletions(
NODE_LABEL_STORE,
"dynamic node label",
&freed_ids,
"dynamic node-label shrink",
)?
} else {
Vec::new()
};
if let Some(label_field) = inline_label_field {
super::node::set_label_field(&mut node_record, label_field);
return Ok(NodeLabelUpdate {
node_record,
first_id: None,
record_ids: Vec::new(),
records: Vec::new(),
freed_records,
allocated_records: 0,
reused_records: Vec::new(),
});
}
let mut reused_records = Vec::new();
let allocated_records = if required_records > record_ids.len() {
let additional_records = required_records - record_ids.len();
let (additional_ids, allocated) =
self.node_label_allocation_ids(additional_records)?;
if allocated == 0 {
reused_records = additional_ids.clone();
}
record_ids.extend(additional_ids);
allocated
} else {
0
};
let records =
super::dynamic::encode_chain_for_ids(&payload, &record_ids, self.node_label_layout)
.map_err(Error::UnsupportedWriteQuery)?;
return Ok(NodeLabelUpdate {
node_record,
first_id: Some(first_id),
record_ids,
records,
freed_records,
allocated_records: allocated_records as u64,
reused_records,
});
}
if let Ok(label_field) = super::node::encode_inline_label_field(label_ids) {
super::node::set_label_field(&mut node_record, label_field);
return Ok(NodeLabelUpdate {
node_record,
first_id: None,
record_ids: Vec::new(),
records: Vec::new(),
freed_records: Vec::new(),
allocated_records: 0,
reused_records: Vec::new(),
});
}
let payload = super::node::encode_dynamic_label_payload(record_id, label_ids)
.map_err(Error::UnsupportedWriteQuery)?;
let required_records = payload.len().div_ceil(self.node_label_layout.payload_size);
let (record_ids, allocated_records) = self.node_label_allocation_ids(required_records)?;
let first_id = record_ids[0];
let records =
super::dynamic::encode_chain_for_ids(&payload, &record_ids, self.node_label_layout)
.map_err(Error::UnsupportedWriteQuery)?;
super::node::set_label_field(&mut node_record, DYNAMIC_LABEL_FLAG | first_id);
Ok(NodeLabelUpdate {
node_record,
first_id: Some(first_id),
record_ids,
allocated_records,
reused_records: if allocated_records == 0 {
records.iter().map(|(record_id, _)| *record_id).collect()
} else {
Vec::new()
},
records,
freed_records: Vec::new(),
})
}
fn collect_node_label_update(&mut self, record_id: u64, update: NodeLabelUpdate) -> Result<()> {
let transaction = self.active_transaction_mut()?;
transaction
.node_records
.insert(record_id, update.node_record);
transaction.allocated_node_label_records = transaction
.allocated_node_label_records
.checked_add(update.allocated_records)
.ok_or_else(|| {
Error::UnsupportedWriteQuery("dynamic node-label allocation overflow".to_owned())
})?;
transaction
.reused_node_label_records
.extend(update.reused_records);
if let Some(first_id) = update.first_id {
transaction
.node_label_chain_ids
.insert(first_id, update.record_ids);
}
transaction.node_label_records.extend(update.records);
for (record_id, bytes) in update.freed_records {
transaction.node_label_records.insert(record_id, bytes);
transaction.freed_node_label_records.insert(record_id);
}
Ok(())
}
fn ensure_label_token(&mut self, label: &str) -> Result<()> {
if self.storage.has_label(label) {
return Ok(());
}
if label.is_empty() {
return Err(Error::UnsupportedWriteQuery(
"Neo4j label token names cannot be empty".to_owned(),
));
}
let transaction = self.active_transaction()?;
let reuse_count = transaction
.reused_label_tokens
.len()
.checked_add(1)
.ok_or_else(|| {
Error::UnsupportedWriteQuery("label token reuse count overflow".to_owned())
})?;
let high_token_id = self
.label_token_id_header
.high_id
.checked_add(transaction.allocated_label_tokens)
.ok_or_else(|| Error::UnsupportedWriteQuery("label token ID overflow".to_owned()))?;
let (token_id, reused_token) = if self.label_token_id_header.num_unused_ids
>= reuse_count as u64
{
let path = self.storage.database_directory().join(LABEL_TOKEN_ID_STORE);
if let Some(token_id) = super::id_generator::prepare_take_free_ids(&path, reuse_count)?
.and_then(|update| next_reusable_id(&update.ids, &transaction.reused_label_tokens))
{
(token_id, true)
} else {
(high_token_id, false)
}
} else {
(high_token_id, false)
};
if token_id >= self.label_token_capacity {
return Err(Error::UnsupportedWriteQuery(
"label token store has no preallocated record capacity".to_owned(),
));
}
let token_id = u32::try_from(token_id).map_err(|_| {
Error::UnsupportedWriteQuery("label token ID exceeds 32 bits".to_owned())
})?;
i32::try_from(token_id).map_err(|_| {
Error::UnsupportedWriteQuery("label token ID exceeds counts key range".to_owned())
})?;
let name_id = self
.label_name_id_header
.high_id
.checked_add(transaction.allocated_label_name_records)
.ok_or_else(|| Error::UnsupportedWriteQuery("label name ID overflow".to_owned()))?;
let name_records = super::dynamic::encode_append_only_chain(
label.as_bytes(),
name_id,
self.label_name_layout,
)
.map_err(Error::UnsupportedWriteQuery)?;
let name_id = u32::try_from(name_id).map_err(|_| {
Error::UnsupportedWriteQuery("label name ID exceeds 32 bits".to_owned())
})?;
let token_record = super::token::encode_label_token(name_id);
self.storage
.insert_label_token(token_id, label.to_owned())?;
let transaction = self.active_transaction_mut()?;
if reused_token {
transaction.reused_label_tokens.push(token_id as u64);
} else {
transaction.allocated_label_tokens = transaction
.allocated_label_tokens
.checked_add(1)
.ok_or_else(|| {
Error::UnsupportedWriteQuery("label token allocation overflow".to_owned())
})?;
}
transaction.allocated_label_name_records = transaction
.allocated_label_name_records
.checked_add(name_records.len() as u64)
.ok_or_else(|| {
Error::UnsupportedWriteQuery("label name allocation overflow".to_owned())
})?;
transaction
.label_token_records
.insert(token_id as u64, token_record);
transaction
.allowed_missing_node_label_count_keys
.insert(token_id);
for (record_id, bytes) in name_records {
transaction.label_name_records.insert(record_id, bytes);
}
Ok(())
}
fn ensure_property_key_token(&mut self, key: &str) -> Result<()> {
if self.storage.has_property_key(key) {
return Ok(());
}
if key.is_empty() {
return Err(Error::UnsupportedWriteQuery(
"Neo4j property key token names cannot be empty".to_owned(),
));
}
let transaction = self.active_transaction()?;
let reuse_count = transaction
.reused_property_key_tokens
.len()
.checked_add(1)
.ok_or_else(|| {
Error::UnsupportedWriteQuery("property key token reuse count overflow".to_owned())
})?;
let high_token_id = self
.property_key_token_id_header
.high_id
.checked_add(transaction.allocated_property_key_tokens)
.ok_or_else(|| {
Error::UnsupportedWriteQuery("property key token ID overflow".to_owned())
})?;
let (token_id, reused_token) = if self.property_key_token_id_header.num_unused_ids
>= reuse_count as u64
{
let path = self
.storage
.database_directory()
.join(PROPERTY_KEY_TOKEN_ID_STORE);
if let Some(token_id) = super::id_generator::prepare_take_free_ids(&path, reuse_count)?
.and_then(|update| {
next_reusable_id(&update.ids, &transaction.reused_property_key_tokens)
})
{
(token_id, true)
} else {
(high_token_id, false)
}
} else {
(high_token_id, false)
};
if token_id >= self.property_key_token_capacity {
return Err(Error::UnsupportedWriteQuery(
"property key token store has no preallocated record capacity".to_owned(),
));
}
if token_id > 0x00ff_ffff {
return Err(Error::UnsupportedWriteQuery(format!(
"property key token ID {token_id} exceeds 24 bits"
)));
}
let name_id = self
.property_key_name_id_header
.high_id
.checked_add(transaction.allocated_property_key_name_records)
.ok_or_else(|| {
Error::UnsupportedWriteQuery("property key name ID overflow".to_owned())
})?;
let name_records = super::dynamic::encode_append_only_chain(
key.as_bytes(),
name_id,
self.property_key_name_layout,
)
.map_err(Error::UnsupportedWriteQuery)?;
let name_id = u32::try_from(name_id).map_err(|_| {
Error::UnsupportedWriteQuery("property key name ID exceeds 32 bits".to_owned())
})?;
let token_id = token_id as u32;
let token_record = super::token::encode_property_key_token(name_id);
self.storage
.insert_property_key_token(token_id, key.to_owned())?;
let transaction = self.active_transaction_mut()?;
if reused_token {
transaction.reused_property_key_tokens.push(token_id as u64);
} else {
transaction.allocated_property_key_tokens = transaction
.allocated_property_key_tokens
.checked_add(1)
.ok_or_else(|| {
Error::UnsupportedWriteQuery(
"property key token allocation overflow".to_owned(),
)
})?;
}
transaction.allocated_property_key_name_records = transaction
.allocated_property_key_name_records
.checked_add(name_records.len() as u64)
.ok_or_else(|| {
Error::UnsupportedWriteQuery("property key name allocation overflow".to_owned())
})?;
transaction
.property_key_token_records
.insert(token_id as u64, token_record);
for (record_id, bytes) in name_records {
transaction
.property_key_name_records
.insert(record_id, bytes);
}
Ok(())
}
fn ensure_relationship_type_token(&mut self, relationship_type: &str) -> Result<()> {
if self.storage.has_relationship_type(relationship_type) {
return Ok(());
}
if relationship_type.is_empty() {
return Err(Error::UnsupportedWriteQuery(
"Neo4j relationship type token names cannot be empty".to_owned(),
));
}
let transaction = self.active_transaction()?;
let reuse_count = transaction
.reused_relationship_type_tokens
.len()
.checked_add(1)
.ok_or_else(|| {
Error::UnsupportedWriteQuery(
"relationship type token reuse count overflow".to_owned(),
)
})?;
let high_token_id = self
.relationship_type_token_id_header
.high_id
.checked_add(transaction.allocated_relationship_type_tokens)
.ok_or_else(|| {
Error::UnsupportedWriteQuery("relationship type token ID overflow".to_owned())
})?;
let (token_id, reused_token) = if self.relationship_type_token_id_header.num_unused_ids
>= reuse_count as u64
{
let path = self
.storage
.database_directory()
.join(RELATIONSHIP_TYPE_TOKEN_ID_STORE);
if let Some(token_id) = super::id_generator::prepare_take_free_ids(&path, reuse_count)?
.and_then(|update| {
next_reusable_id(&update.ids, &transaction.reused_relationship_type_tokens)
})
{
(token_id, true)
} else {
(high_token_id, false)
}
} else {
(high_token_id, false)
};
if token_id >= self.relationship_type_token_capacity {
return Err(Error::UnsupportedWriteQuery(
"relationship type token store has no preallocated record capacity".to_owned(),
));
}
if token_id > u16::MAX as u64 {
return Err(Error::UnsupportedWriteQuery(format!(
"relationship type token ID {token_id} exceeds 16 bits"
)));
}
let name_id = self
.relationship_type_name_id_header
.high_id
.checked_add(transaction.allocated_relationship_type_name_records)
.ok_or_else(|| {
Error::UnsupportedWriteQuery("relationship type name ID overflow".to_owned())
})?;
let name_records = super::dynamic::encode_append_only_chain(
relationship_type.as_bytes(),
name_id,
self.relationship_type_name_layout,
)
.map_err(Error::UnsupportedWriteQuery)?;
let name_id = u32::try_from(name_id).map_err(|_| {
Error::UnsupportedWriteQuery("relationship type name ID exceeds 32 bits".to_owned())
})?;
let token_id = token_id as u32;
let token_record = super::token::encode_label_token(name_id);
self.storage
.insert_relationship_type_token(token_id, relationship_type.to_owned())?;
let transaction = self.active_transaction_mut()?;
if reused_token {
transaction
.reused_relationship_type_tokens
.push(token_id as u64);
} else {
transaction.allocated_relationship_type_tokens = transaction
.allocated_relationship_type_tokens
.checked_add(1)
.ok_or_else(|| {
Error::UnsupportedWriteQuery(
"relationship type token allocation overflow".to_owned(),
)
})?;
}
transaction.allocated_relationship_type_name_records = transaction
.allocated_relationship_type_name_records
.checked_add(name_records.len() as u64)
.ok_or_else(|| {
Error::UnsupportedWriteQuery(
"relationship type name allocation overflow".to_owned(),
)
})?;
transaction
.relationship_type_token_records
.insert(token_id as u64, token_record);
for (record_id, bytes) in name_records {
transaction
.relationship_type_name_records
.insert(record_id, bytes);
}
Ok(())
}
}
impl TransactionWorkspace {
fn refresh_write_set(&mut self) {
macro_rules! fixed_records {
($field:ident, $store:expr) => {
for record_id in self.$field.keys() {
self.write_set.insert_fixed($store, record_id);
}
};
}
fixed_records!(property_records, FixedStore::Property);
fixed_records!(node_records, FixedStore::Node);
fixed_records!(node_label_records, FixedStore::NodeLabel);
fixed_records!(relationship_records, FixedStore::Relationship);
fixed_records!(relationship_group_records, FixedStore::RelationshipGroup);
fixed_records!(string_records, FixedStore::String);
fixed_records!(array_records, FixedStore::Array);
fixed_records!(property_key_token_records, FixedStore::PropertyKeyToken);
fixed_records!(property_key_name_records, FixedStore::PropertyKeyName);
fixed_records!(label_token_records, FixedStore::LabelToken);
fixed_records!(label_name_records, FixedStore::LabelName);
fixed_records!(
relationship_type_token_records,
FixedStore::RelationshipTypeToken
);
fixed_records!(
relationship_type_name_records,
FixedStore::RelationshipTypeName
);
if !self.string_chain_ids.is_empty()
|| !self.array_chain_ids.is_empty()
|| !self.node_label_chain_ids.is_empty()
{
self.write_set
.insert_shared(SharedDependency::DynamicChains);
}
if self.has_id_generator_allocation() {
self.write_set.insert_shared(SharedDependency::IdAllocation);
}
if self.has_id_generator_reuse() {
self.write_set.insert_shared(SharedDependency::IdReuse);
}
if self.has_id_generator_reclamation() {
self.write_set
.insert_shared(SharedDependency::IdReclamation);
}
if self.allocated_node_records != 0
|| !self.reused_node_records.is_empty()
|| !self.freed_node_records.is_empty()
|| !self.node_label_count_deltas.is_empty()
|| !self.allowed_missing_node_label_count_keys.is_empty()
|| !self.relationship_count_deltas.is_empty()
{
self.write_set.insert_shared(SharedDependency::Counts);
}
if !self.relationship_group_degree_deltas.is_empty() {
self.write_set
.insert_shared(SharedDependency::RelationshipGroupDegrees);
}
if self.has_token_changes() {
self.write_set.insert_shared(SharedDependency::Tokens);
}
}
fn has_id_generator_allocation(&self) -> bool {
self.allocated_property_records != 0
|| self.allocated_string_records != 0
|| self.allocated_array_records != 0
|| self.allocated_node_records != 0
|| self.allocated_node_label_records != 0
|| self.allocated_relationship_records != 0
|| self.allocated_relationship_group_records != 0
|| self.allocated_property_key_tokens != 0
|| self.allocated_property_key_name_records != 0
|| self.allocated_label_tokens != 0
|| self.allocated_label_name_records != 0
|| self.allocated_relationship_type_tokens != 0
|| self.allocated_relationship_type_name_records != 0
}
fn has_id_generator_reuse(&self) -> bool {
!self.reused_property_records.is_empty()
|| !self.reused_string_records.is_empty()
|| !self.reused_array_records.is_empty()
|| !self.reused_node_records.is_empty()
|| !self.reused_node_label_records.is_empty()
|| !self.reused_relationship_records.is_empty()
|| !self.reused_relationship_group_records.is_empty()
|| !self.reused_property_key_tokens.is_empty()
|| !self.reused_label_tokens.is_empty()
|| !self.reused_relationship_type_tokens.is_empty()
}
fn has_id_generator_reclamation(&self) -> bool {
!self.freed_property_records.is_empty()
|| !self.freed_string_records.is_empty()
|| !self.freed_array_records.is_empty()
|| !self.freed_node_records.is_empty()
|| !self.freed_node_label_records.is_empty()
|| !self.freed_relationship_records.is_empty()
|| !self.freed_relationship_group_records.is_empty()
}
fn has_token_changes(&self) -> bool {
self.allocated_property_key_tokens != 0
|| !self.reused_property_key_tokens.is_empty()
|| self.allocated_property_key_name_records != 0
|| self.allocated_label_tokens != 0
|| !self.reused_label_tokens.is_empty()
|| self.allocated_label_name_records != 0
|| self.allocated_relationship_type_tokens != 0
|| !self.reused_relationship_type_tokens.is_empty()
|| self.allocated_relationship_type_name_records != 0
}
fn hot_record_bytes(&self) -> Result<usize> {
let maps = [
self.property_records.hot_bytes()?,
self.node_records.hot_bytes()?,
self.node_label_records.hot_bytes()?,
self.relationship_records.hot_bytes()?,
self.relationship_group_records.hot_bytes()?,
self.string_records.hot_bytes()?,
self.array_records.hot_bytes()?,
self.property_key_token_records.hot_bytes()?,
self.property_key_name_records.hot_bytes()?,
self.label_token_records.hot_bytes()?,
self.label_name_records.hot_bytes()?,
self.relationship_type_token_records.hot_bytes()?,
self.relationship_type_name_records.hot_bytes()?,
];
maps.into_iter().try_fold(0usize, |total, bytes| {
total.checked_add(bytes).ok_or(Error::Transaction(
crate::TransactionErrorKind::ResourceLimit,
))
})
}
fn spill_record_maps(&mut self) -> Result<()> {
self.property_records.spill()?;
self.node_records.spill()?;
self.node_label_records.spill()?;
self.relationship_records.spill()?;
self.relationship_group_records.spill()?;
self.string_records.spill()?;
self.array_records.spill()?;
self.property_key_token_records.spill()?;
self.property_key_name_records.spill()?;
self.label_token_records.spill()?;
self.label_name_records.spill()?;
self.relationship_type_token_records.spill()?;
self.relationship_type_name_records.spill()
}
fn has_changes(&self) -> bool {
!self.property_records.is_empty()
|| !self.node_records.is_empty()
|| !self.node_label_records.is_empty()
|| !self.relationship_records.is_empty()
|| !self.relationship_group_records.is_empty()
|| !self.string_records.is_empty()
|| !self.array_records.is_empty()
|| !self.property_key_token_records.is_empty()
|| !self.property_key_name_records.is_empty()
|| !self.label_token_records.is_empty()
|| !self.label_name_records.is_empty()
|| !self.relationship_type_token_records.is_empty()
|| !self.relationship_type_name_records.is_empty()
|| !self.string_chain_ids.is_empty()
|| !self.array_chain_ids.is_empty()
|| !self.node_label_chain_ids.is_empty()
|| self.allocated_property_records != 0
|| !self.reused_property_records.is_empty()
|| !self.freed_property_records.is_empty()
|| self.allocated_string_records != 0
|| !self.reused_string_records.is_empty()
|| !self.freed_string_records.is_empty()
|| self.allocated_array_records != 0
|| !self.reused_array_records.is_empty()
|| !self.freed_array_records.is_empty()
|| self.allocated_property_key_tokens != 0
|| !self.reused_property_key_tokens.is_empty()
|| self.allocated_property_key_name_records != 0
|| self.allocated_label_tokens != 0
|| !self.reused_label_tokens.is_empty()
|| self.allocated_label_name_records != 0
|| self.allocated_relationship_type_tokens != 0
|| !self.reused_relationship_type_tokens.is_empty()
|| self.allocated_relationship_type_name_records != 0
|| self.allocated_node_records != 0
|| !self.reused_node_records.is_empty()
|| !self.freed_node_records.is_empty()
|| self.allocated_node_label_records != 0
|| !self.reused_node_label_records.is_empty()
|| !self.freed_node_label_records.is_empty()
|| self.allocated_relationship_records != 0
|| !self.reused_relationship_records.is_empty()
|| !self.freed_relationship_records.is_empty()
|| self.allocated_relationship_group_records != 0
|| !self.reused_relationship_group_records.is_empty()
|| !self.freed_relationship_group_records.is_empty()
|| !self.node_label_count_deltas.is_empty()
|| !self.allowed_missing_node_label_count_keys.is_empty()
|| !self.relationship_count_deltas.is_empty()
|| !self.relationship_group_degree_deltas.is_empty()
}
fn workspace_usage(&self) -> Result<crate::neo4j_backend::WorkspaceUsage> {
const BTREE_ENTRY_OVERHEAD: usize = 3 * std::mem::size_of::<usize>();
let mut bytes = self.base_snapshot.estimated_clone_heap_bytes()?;
let mut records = 0_usize;
fn add(
bytes: &mut usize,
records: &mut usize,
count: usize,
entry_bytes: usize,
) -> Result<()> {
*bytes = bytes
.checked_add(count.checked_mul(entry_bytes).ok_or(Error::Transaction(
crate::TransactionErrorKind::ResourceLimit,
))?)
.ok_or(Error::Transaction(
crate::TransactionErrorKind::ResourceLimit,
))?;
*records = records.checked_add(count).ok_or(Error::Transaction(
crate::TransactionErrorKind::ResourceLimit,
))?;
Ok(())
}
macro_rules! fixed_map {
($field:ident, $value_size:expr) => {
add(
&mut bytes,
&mut records,
self.$field.len(),
std::mem::size_of::<u64>() + BTREE_ENTRY_OVERHEAD,
)?;
bytes = bytes
.checked_add(self.$field.total_bytes()?)
.ok_or(Error::Transaction(
crate::TransactionErrorKind::ResourceLimit,
))?;
};
}
macro_rules! dynamic_map {
($field:ident) => {
add(
&mut bytes,
&mut records,
self.$field.len(),
std::mem::size_of::<u64>()
+ std::mem::size_of::<Vec<u8>>()
+ BTREE_ENTRY_OVERHEAD,
)?;
bytes = bytes
.checked_add(self.$field.total_bytes()?)
.ok_or(Error::Transaction(
crate::TransactionErrorKind::ResourceLimit,
))?;
};
}
macro_rules! id_chain_map {
($field:ident) => {
for ids in self.$field.values() {
let id_bytes = ids.len().checked_mul(std::mem::size_of::<u64>()).ok_or(
Error::Transaction(crate::TransactionErrorKind::ResourceLimit),
)?;
let entry_bytes = std::mem::size_of::<u64>()
.checked_add(std::mem::size_of::<Vec<u64>>())
.and_then(|bytes| bytes.checked_add(id_bytes))
.and_then(|bytes| bytes.checked_add(BTREE_ENTRY_OVERHEAD))
.ok_or(Error::Transaction(
crate::TransactionErrorKind::ResourceLimit,
))?;
add(&mut bytes, &mut records, 1, entry_bytes)?;
}
};
}
macro_rules! id_vector_entries {
($field:ident) => {
add(
&mut bytes,
&mut records,
self.$field.len(),
std::mem::size_of::<u64>(),
)?;
};
}
macro_rules! id_set_entries {
($field:ident) => {
add(
&mut bytes,
&mut records,
self.$field.len(),
std::mem::size_of::<u64>() + BTREE_ENTRY_OVERHEAD,
)?;
};
}
fixed_map!(property_records, PROPERTY_RECORD_SIZE);
fixed_map!(node_records, NODE_RECORD_SIZE);
fixed_map!(relationship_records, RELATIONSHIP_RECORD_SIZE);
fixed_map!(relationship_group_records, RELATIONSHIP_GROUP_RECORD_SIZE);
fixed_map!(property_key_token_records, PROPERTY_KEY_TOKEN_RECORD_SIZE);
fixed_map!(label_token_records, LABEL_TOKEN_RECORD_SIZE);
fixed_map!(relationship_type_token_records, LABEL_TOKEN_RECORD_SIZE);
dynamic_map!(node_label_records);
dynamic_map!(string_records);
dynamic_map!(array_records);
dynamic_map!(property_key_name_records);
dynamic_map!(label_name_records);
dynamic_map!(relationship_type_name_records);
id_chain_map!(string_chain_ids);
id_chain_map!(array_chain_ids);
id_chain_map!(node_label_chain_ids);
id_vector_entries!(reused_property_records);
id_set_entries!(freed_property_records);
id_vector_entries!(reused_string_records);
id_set_entries!(freed_string_records);
id_vector_entries!(reused_array_records);
id_set_entries!(freed_array_records);
id_vector_entries!(reused_property_key_tokens);
id_vector_entries!(reused_label_tokens);
id_vector_entries!(reused_relationship_type_tokens);
id_vector_entries!(reused_node_records);
id_set_entries!(freed_node_records);
id_vector_entries!(reused_node_label_records);
id_set_entries!(freed_node_label_records);
id_vector_entries!(reused_relationship_records);
id_set_entries!(freed_relationship_records);
id_vector_entries!(reused_relationship_group_records);
id_set_entries!(freed_relationship_group_records);
add(
&mut bytes,
&mut records,
self.node_label_count_deltas.len(),
std::mem::size_of::<u32>() + std::mem::size_of::<i64>() + BTREE_ENTRY_OVERHEAD,
)?;
add(
&mut bytes,
&mut records,
self.allowed_missing_node_label_count_keys.len(),
std::mem::size_of::<u32>() + BTREE_ENTRY_OVERHEAD,
)?;
add(
&mut bytes,
&mut records,
self.relationship_count_deltas.len(),
std::mem::size_of::<super::counts::RelationshipCountKey>()
+ std::mem::size_of::<i64>()
+ BTREE_ENTRY_OVERHEAD,
)?;
add(
&mut bytes,
&mut records,
self.relationship_group_degree_deltas.len(),
std::mem::size_of::<super::counts::RelationshipGroupDegreeKey>()
+ std::mem::size_of::<i64>()
+ BTREE_ENTRY_OVERHEAD,
)?;
bytes = bytes
.checked_add(
self.write_set
.fixed_heap_bytes()
.and_then(|bytes| {
self.write_set
.shared_len()
.checked_mul(
std::mem::size_of::<SharedDependency>() + BTREE_ENTRY_OVERHEAD,
)
.and_then(|shared| bytes.checked_add(shared))
})
.ok_or(Error::Transaction(
crate::TransactionErrorKind::ResourceLimit,
))?,
)
.ok_or(Error::Transaction(
crate::TransactionErrorKind::ResourceLimit,
))?;
Ok(crate::neo4j_backend::WorkspaceUsage { bytes, records })
}
}
fn next_reusable_id(selected: &[u64], previously_selected: &[u64]) -> Option<u64> {
let expected_len = previously_selected.len().checked_add(1)?;
(selected.len() == expected_len && selected.starts_with(previously_selected))
.then(|| selected[expected_len - 1])
}
fn extend_dynamic_chain_ids(
ids: &mut Vec<u64>,
required_records: usize,
high_id: u64,
pending_allocations: u64,
record_capacity: u64,
value_kind: &str,
) -> Result<u64> {
if required_records < ids.len() {
return Err(Error::UnsupportedWriteQuery(format!(
"shrinking a dynamic {value_kind} requires dynamic record reclamation"
)));
}
let additional_records = required_records - ids.len();
if additional_records == 0 {
return Ok(0);
}
let additional_records = u64::try_from(additional_records).map_err(|_| {
Error::UnsupportedWriteQuery(format!("dynamic {value_kind} record count overflow"))
})?;
let first_new_id = high_id
.checked_add(pending_allocations)
.ok_or_else(|| Error::UnsupportedWriteQuery(format!("dynamic {value_kind} ID overflow")))?;
let end_id = first_new_id
.checked_add(additional_records)
.filter(|end| *end <= record_capacity)
.ok_or_else(|| {
Error::UnsupportedWriteQuery(format!(
"dynamic {value_kind} store has no preallocated record capacity"
))
})?;
ids.extend(first_new_id..end_id);
Ok(additional_records)
}
struct DynamicStringUpdate {
first_id: u64,
allocated_records: u64,
reused_records: Vec<u64>,
record_ids: Vec<u64>,
records: Vec<(u64, Vec<u8>)>,
freed_records: Vec<(u64, Vec<u8>)>,
}
struct DynamicArrayUpdate {
first_id: u64,
allocated_records: u64,
reused_records: Vec<u64>,
record_ids: Vec<u64>,
records: Vec<(u64, Vec<u8>)>,
freed_records: Vec<(u64, Vec<u8>)>,
}
struct NodeLabelUpdate {
node_record: [u8; NODE_RECORD_SIZE],
first_id: Option<u64>,
record_ids: Vec<u64>,
records: Vec<(u64, Vec<u8>)>,
freed_records: Vec<(u64, Vec<u8>)>,
allocated_records: u64,
reused_records: Vec<u64>,
}
struct EntityPropertyDeletion {
property_records: Vec<(u64, [u8; PROPERTY_RECORD_SIZE])>,
strings: Vec<(u64, Vec<u8>)>,
arrays: Vec<(u64, Vec<u8>)>,
}
struct DynamicPropertyDeletion {
kind: super::property::DynamicPropertyKind,
records: Vec<(u64, Vec<u8>)>,
}
impl StorageInterface for Neo4jWritableStorage {
fn database_metadata(&self) -> Option<&crate::storage_interface::DatabaseMetadata> {
StorageInterface::database_metadata(&self.storage)
}
fn schema_metadata(&self) -> Option<&crate::storage_interface::SchemaMetadata> {
StorageInterface::schema_metadata(&self.storage)
}
fn node_ids(&self) -> Vec<NodeId> {
self.storage.node_ids()
}
fn relationship_ids(&self) -> Vec<RelationshipId> {
self.storage.relationship_ids()
}
fn node(&self, id: NodeId) -> Option<&Node> {
self.storage.node(id)
}
fn relationship(&self, id: RelationshipId) -> Option<&Relationship> {
self.storage.relationship(id)
}
fn outgoing_relationships(&self, id: NodeId) -> &[RelationshipId] {
self.storage.outgoing_relationships(id)
}
fn incoming_relationships(&self, id: NodeId) -> &[RelationshipId] {
self.storage.incoming_relationships(id)
}
fn nodes_with_label(&self, label: &str) -> &[NodeId] {
self.storage.nodes_with_label(label)
}
fn node_by_external_id(&self, external_id: &str) -> Option<NodeId> {
self.storage.node_by_external_id(external_id)
}
fn relationship_by_external_id(&self, external_id: &str) -> Option<RelationshipId> {
self.storage.relationship_by_external_id(external_id)
}
fn label_tokens(&self) -> Vec<String> {
self.storage.public_label_token_names()
}
fn relationship_type_tokens(&self) -> Vec<String> {
self.storage.public_relationship_type_token_names()
}
fn property_key_tokens(&self) -> Vec<String> {
self.storage.public_property_key_token_names()
}
fn node_property_type(&self, id: NodeId, property_key: &str) -> Option<String> {
StorageInterface::node_property_type(&self.storage, id, property_key)
}
fn relationship_property_type(&self, id: RelationshipId, property_key: &str) -> Option<String> {
StorageInterface::relationship_property_type(&self.storage, id, property_key)
}
}
impl TransactionalStorage for Neo4jWritableStorage {
fn begin_transaction(&mut self) -> Result<()> {
if self.poisoned {
return Err(Error::Transaction(
crate::TransactionErrorKind::StoragePoisoned,
));
}
if self.transaction.is_some() {
return Err(Error::Transaction(
crate::TransactionErrorKind::AlreadyActive,
));
}
let spill_threshold_bytes = self.next_spill_threshold_bytes.take();
let max_spill_bytes = self.next_max_spill_bytes.take();
let max_journal_bytes = self.next_max_journal_bytes.take();
let spill_transaction_id = self.next_spill_transaction_id.take();
let spill = spill_threshold_bytes
.map(|_| {
SpillFile::create(
self.storage.database_directory(),
max_spill_bytes,
spill_transaction_id,
)
})
.transpose()?;
let coordinator = Arc::clone(&self.coordinator);
let mut coordinator_state = coordinator
.state
.lock()
.map_err(|_| lock_error("MVCC coordinator"))?;
if coordinator_state.poisoned {
self.poisoned = true;
return Err(Error::Transaction(
crate::TransactionErrorKind::StoragePoisoned,
));
}
if self.snapshot_version != coordinator_state.version {
let database_directory = self.storage.database_directory().to_path_buf();
if let Err(error) = self.reload_from_disk(&database_directory) {
self.poisoned = true;
coordinator_state.poisoned = true;
return Err(error);
}
self.snapshot_version = coordinator_state.version;
}
coordinator_state.register_snapshot(self.snapshot_version)?;
drop(coordinator_state);
self.snapshot_registration = Some(SnapshotRegistration {
coordinator,
version: self.snapshot_version,
});
self.transaction = Some(TransactionWorkspace {
state: TransactionState::Active,
snapshot_version: self.snapshot_version,
write_set: WriteSet::default(),
max_workspace_bytes: None,
max_records: None,
spill_threshold_bytes,
max_journal_bytes,
base_snapshot: self.storage.clone(),
property_records: SpillMap::new(1, spill.clone()),
node_records: SpillMap::new(2, spill.clone()),
node_label_records: SpillMap::new(3, spill.clone()),
relationship_records: SpillMap::new(4, spill.clone()),
relationship_group_records: SpillMap::new(5, spill.clone()),
string_records: SpillMap::new(6, spill.clone()),
array_records: SpillMap::new(7, spill.clone()),
property_key_token_records: SpillMap::new(8, spill.clone()),
property_key_name_records: SpillMap::new(9, spill.clone()),
label_token_records: SpillMap::new(10, spill.clone()),
label_name_records: SpillMap::new(11, spill.clone()),
relationship_type_token_records: SpillMap::new(12, spill.clone()),
relationship_type_name_records: SpillMap::new(13, spill),
string_chain_ids: BTreeMap::new(),
array_chain_ids: BTreeMap::new(),
node_label_chain_ids: BTreeMap::new(),
allocated_property_records: 0,
reused_property_records: Vec::new(),
freed_property_records: BTreeSet::new(),
allocated_string_records: 0,
reused_string_records: Vec::new(),
freed_string_records: BTreeSet::new(),
allocated_array_records: 0,
reused_array_records: Vec::new(),
freed_array_records: BTreeSet::new(),
allocated_property_key_tokens: 0,
reused_property_key_tokens: Vec::new(),
allocated_property_key_name_records: 0,
allocated_label_tokens: 0,
reused_label_tokens: Vec::new(),
allocated_label_name_records: 0,
allocated_relationship_type_tokens: 0,
reused_relationship_type_tokens: Vec::new(),
allocated_relationship_type_name_records: 0,
allocated_node_records: 0,
reused_node_records: Vec::new(),
freed_node_records: BTreeSet::new(),
allocated_node_label_records: 0,
reused_node_label_records: Vec::new(),
freed_node_label_records: BTreeSet::new(),
allocated_relationship_records: 0,
reused_relationship_records: Vec::new(),
freed_relationship_records: BTreeSet::new(),
allocated_relationship_group_records: 0,
reused_relationship_group_records: Vec::new(),
freed_relationship_group_records: BTreeSet::new(),
node_label_count_deltas: BTreeMap::new(),
allowed_missing_node_label_count_keys: BTreeSet::new(),
relationship_count_deltas: BTreeMap::new(),
relationship_group_degree_deltas: BTreeMap::new(),
});
Ok(())
}
fn commit_transaction(&mut self) -> Result<()> {
self.require_active_transaction()?;
let _snapshot_registration = self.snapshot_registration.take().ok_or_else(|| {
Error::Validation("active transaction has no MVCC snapshot registration".to_owned())
})?;
let mut transaction = self
.transaction
.take()
.ok_or(Error::Transaction(crate::TransactionErrorKind::NotActive))?;
if !transaction.has_changes() {
return Ok(());
}
if !transaction.write_set.pending_fixed().is_empty() {
self.storage = transaction.base_snapshot;
return Err(Error::Validation(
"transaction has uncaptured MVCC before images".to_owned(),
));
}
let coordinator = Arc::clone(&self.coordinator);
let mut coordinator_state = coordinator
.state
.lock()
.map_err(|_| lock_error("MVCC coordinator"))?;
if coordinator_state.poisoned {
self.storage = transaction.base_snapshot;
self.poisoned = true;
return Err(Error::Transaction(
crate::TransactionErrorKind::StoragePoisoned,
));
}
let rebasing = transaction.snapshot_version != coordinator_state.version;
if rebasing {
match self.fixed_dependencies_match(&transaction, &coordinator_state) {
Ok(true) => {}
Ok(false) => {
self.storage = transaction.base_snapshot;
return Err(Error::Transaction(crate::TransactionErrorKind::Conflict));
}
Err(error) => {
self.storage = transaction.base_snapshot;
return Err(error);
}
}
}
let Some(committed_version) = coordinator_state.version.checked_add(1) else {
self.storage = transaction.base_snapshot;
self.poisoned = true;
coordinator_state.poisoned = true;
return Err(Error::Transaction(
crate::TransactionErrorKind::StoragePoisoned,
));
};
let database_directory = self.storage.database_directory().to_path_buf();
let mut committed_storage_poisoned = false;
let commit_result = (|| {
let property_path = database_directory.join(PROPERTY_STORE);
let layout =
PageLayout::new(&property_path, self.storage.format(), PROPERTY_RECORD_SIZE)?;
let mut change_set =
ChangeSet::new_with_limit(&database_directory, transaction.max_journal_bytes)?;
transaction
.property_records
.drain_try_for_each(|record_id, bytes| {
let offset = layout.offset(record_id, &property_path)?;
change_set.overwrite(PROPERTY_STORE, offset, bytes.to_vec())
})?;
let node_path = database_directory.join(NODE_STORE);
let node_layout = PageLayout::new(&node_path, self.storage.format(), NODE_RECORD_SIZE)?;
transaction
.node_records
.drain_try_for_each(|record_id, bytes| {
let offset = node_layout.offset(record_id, &node_path)?;
change_set.overwrite(NODE_STORE, offset, bytes.to_vec())
})?;
let node_label_path = database_directory.join(NODE_LABEL_STORE);
let node_label_layout = PageLayout::new(
&node_label_path,
self.storage.format(),
self.node_label_layout.record_size,
)?;
transaction
.node_label_records
.drain_try_for_each(|record_id, bytes| {
let offset = node_label_layout.offset(record_id, &node_label_path)?;
change_set.overwrite(NODE_LABEL_STORE, offset, bytes)
})?;
let relationship_path = database_directory.join(RELATIONSHIP_STORE);
let relationship_layout = PageLayout::new(
&relationship_path,
self.storage.format(),
RELATIONSHIP_RECORD_SIZE,
)?;
transaction
.relationship_records
.drain_try_for_each(|record_id, bytes| {
let offset = relationship_layout.offset(record_id, &relationship_path)?;
change_set.overwrite(RELATIONSHIP_STORE, offset, bytes.to_vec())
})?;
let relationship_group_path = database_directory.join(RELATIONSHIP_GROUP_STORE);
let relationship_group_layout = PageLayout::new(
&relationship_group_path,
self.storage.format(),
RELATIONSHIP_GROUP_RECORD_SIZE,
)?;
transaction
.relationship_group_records
.drain_try_for_each(|record_id, bytes| {
let offset =
relationship_group_layout.offset(record_id, &relationship_group_path)?;
change_set.overwrite(RELATIONSHIP_GROUP_STORE, offset, bytes.to_vec())
})?;
let string_path = database_directory.join(STRING_STORE);
let string_layout = PageLayout::new(
&string_path,
self.storage.format(),
self.string_layout.record_size,
)?;
transaction
.string_records
.drain_try_for_each(|record_id, bytes| {
let offset = string_layout.offset(record_id, &string_path)?;
change_set.overwrite(STRING_STORE, offset, bytes)
})?;
let array_path = database_directory.join(ARRAY_STORE);
let array_layout = PageLayout::new(
&array_path,
self.storage.format(),
self.array_layout.record_size,
)?;
transaction
.array_records
.drain_try_for_each(|record_id, bytes| {
let offset = array_layout.offset(record_id, &array_path)?;
change_set.overwrite(ARRAY_STORE, offset, bytes)
})?;
let property_key_token_path = database_directory.join(PROPERTY_KEY_TOKEN_STORE);
let property_key_token_layout = PageLayout::new(
&property_key_token_path,
self.storage.format(),
PROPERTY_KEY_TOKEN_RECORD_SIZE,
)?;
transaction
.property_key_token_records
.drain_try_for_each(|record_id, bytes| {
let offset =
property_key_token_layout.offset(record_id, &property_key_token_path)?;
change_set.overwrite(PROPERTY_KEY_TOKEN_STORE, offset, bytes.to_vec())
})?;
let property_key_name_path = database_directory.join(PROPERTY_KEY_NAME_STORE);
let property_key_name_layout = PageLayout::new(
&property_key_name_path,
self.storage.format(),
self.property_key_name_layout.record_size,
)?;
transaction
.property_key_name_records
.drain_try_for_each(|record_id, bytes| {
let offset =
property_key_name_layout.offset(record_id, &property_key_name_path)?;
change_set.overwrite(PROPERTY_KEY_NAME_STORE, offset, bytes)
})?;
let label_token_path = database_directory.join(LABEL_TOKEN_STORE);
let label_token_layout = PageLayout::new(
&label_token_path,
self.storage.format(),
LABEL_TOKEN_RECORD_SIZE,
)?;
transaction
.label_token_records
.drain_try_for_each(|record_id, bytes| {
let offset = label_token_layout.offset(record_id, &label_token_path)?;
change_set.overwrite(LABEL_TOKEN_STORE, offset, bytes.to_vec())
})?;
let label_name_path = database_directory.join(LABEL_NAME_STORE);
let label_name_layout = PageLayout::new(
&label_name_path,
self.storage.format(),
self.label_name_layout.record_size,
)?;
transaction
.label_name_records
.drain_try_for_each(|record_id, bytes| {
let offset = label_name_layout.offset(record_id, &label_name_path)?;
change_set.overwrite(LABEL_NAME_STORE, offset, bytes)
})?;
let relationship_type_token_path =
database_directory.join(RELATIONSHIP_TYPE_TOKEN_STORE);
let relationship_type_token_layout = PageLayout::new(
&relationship_type_token_path,
self.storage.format(),
LABEL_TOKEN_RECORD_SIZE,
)?;
transaction
.relationship_type_token_records
.drain_try_for_each(|record_id, bytes| {
let offset = relationship_type_token_layout
.offset(record_id, &relationship_type_token_path)?;
change_set.overwrite(RELATIONSHIP_TYPE_TOKEN_STORE, offset, bytes.to_vec())
})?;
let relationship_type_name_path = database_directory.join(RELATIONSHIP_TYPE_NAME_STORE);
let relationship_type_name_layout = PageLayout::new(
&relationship_type_name_path,
self.storage.format(),
self.relationship_type_name_layout.record_size,
)?;
transaction
.relationship_type_name_records
.drain_try_for_each(|record_id, bytes| {
let offset = relationship_type_name_layout
.offset(record_id, &relationship_type_name_path)?;
change_set.overwrite(RELATIONSHIP_TYPE_NAME_STORE, offset, bytes)
})?;
if transaction.allocated_property_records > 0 {
let allocation = super::id_generator::allocate_high_ids(
self.property_id_header,
transaction.allocated_property_records,
)
.map_err(Error::UnsupportedWriteQuery)?;
for (offset, bytes) in allocation.header_patches {
change_set.overwrite(PROPERTY_ID_STORE, offset, bytes)?;
}
}
if transaction.allocated_string_records > 0 {
let allocation = super::id_generator::allocate_high_ids(
self.string_id_header,
transaction.allocated_string_records,
)
.map_err(Error::UnsupportedWriteQuery)?;
for (offset, bytes) in allocation.header_patches {
change_set.overwrite(STRING_ID_STORE, offset, bytes)?;
}
}
if transaction.allocated_array_records > 0 {
let allocation = super::id_generator::allocate_high_ids(
self.array_id_header,
transaction.allocated_array_records,
)
.map_err(Error::UnsupportedWriteQuery)?;
for (offset, bytes) in allocation.header_patches {
change_set.overwrite(ARRAY_ID_STORE, offset, bytes)?;
}
}
stage_id_lifecycle_changes(
&mut change_set,
&database_directory,
PROPERTY_ID_STORE,
self.property_id_header,
transaction.allocated_property_records,
&transaction.reused_property_records,
&transaction.freed_property_records,
)?;
stage_id_lifecycle_changes(
&mut change_set,
&database_directory,
STRING_ID_STORE,
self.string_id_header,
transaction.allocated_string_records,
&transaction.reused_string_records,
&transaction.freed_string_records,
)?;
stage_id_lifecycle_changes(
&mut change_set,
&database_directory,
ARRAY_ID_STORE,
self.array_id_header,
transaction.allocated_array_records,
&transaction.reused_array_records,
&transaction.freed_array_records,
)?;
if transaction.allocated_property_key_tokens > 0 {
let allocation = super::id_generator::allocate_high_ids(
self.property_key_token_id_header,
transaction.allocated_property_key_tokens,
)
.map_err(Error::UnsupportedWriteQuery)?;
for (offset, bytes) in allocation.header_patches {
change_set.overwrite(PROPERTY_KEY_TOKEN_ID_STORE, offset, bytes)?;
}
}
if !transaction.reused_property_key_tokens.is_empty() {
let update = super::id_generator::prepare_take_free_ids(
&database_directory.join(PROPERTY_KEY_TOKEN_ID_STORE),
transaction.reused_property_key_tokens.len(),
)?
.ok_or_else(|| {
Error::UnsupportedWriteQuery(
"reusable property key token IDs unavailable".to_owned(),
)
})?;
if update.ids != transaction.reused_property_key_tokens {
return Err(Error::UnsupportedWriteQuery(
"reusable property key token selection changed".to_owned(),
));
}
change_set.overwrite(
PROPERTY_KEY_TOKEN_ID_STORE,
update.root_page_offset,
update.root_page,
)?;
for (offset, bytes) in update.header_patches {
change_set.overwrite(PROPERTY_KEY_TOKEN_ID_STORE, offset, bytes)?;
}
}
if transaction.allocated_property_key_name_records > 0 {
let allocation = super::id_generator::allocate_high_ids(
self.property_key_name_id_header,
transaction.allocated_property_key_name_records,
)
.map_err(Error::UnsupportedWriteQuery)?;
for (offset, bytes) in allocation.header_patches {
change_set.overwrite(PROPERTY_KEY_NAME_ID_STORE, offset, bytes)?;
}
}
if transaction.allocated_label_tokens > 0 {
let allocation = super::id_generator::allocate_high_ids(
self.label_token_id_header,
transaction.allocated_label_tokens,
)
.map_err(Error::UnsupportedWriteQuery)?;
for (offset, bytes) in allocation.header_patches {
change_set.overwrite(LABEL_TOKEN_ID_STORE, offset, bytes)?;
}
}
if !transaction.reused_label_tokens.is_empty() {
let update = super::id_generator::prepare_take_free_ids(
&database_directory.join(LABEL_TOKEN_ID_STORE),
transaction.reused_label_tokens.len(),
)?
.ok_or_else(|| {
Error::UnsupportedWriteQuery("reusable label token IDs unavailable".to_owned())
})?;
if update.ids != transaction.reused_label_tokens {
return Err(Error::UnsupportedWriteQuery(
"reusable label token selection changed".to_owned(),
));
}
change_set.overwrite(
LABEL_TOKEN_ID_STORE,
update.root_page_offset,
update.root_page,
)?;
for (offset, bytes) in update.header_patches {
change_set.overwrite(LABEL_TOKEN_ID_STORE, offset, bytes)?;
}
}
if transaction.allocated_label_name_records > 0 {
let allocation = super::id_generator::allocate_high_ids(
self.label_name_id_header,
transaction.allocated_label_name_records,
)
.map_err(Error::UnsupportedWriteQuery)?;
for (offset, bytes) in allocation.header_patches {
change_set.overwrite(LABEL_NAME_ID_STORE, offset, bytes)?;
}
}
if transaction.allocated_relationship_type_tokens > 0 {
let allocation = super::id_generator::allocate_high_ids(
self.relationship_type_token_id_header,
transaction.allocated_relationship_type_tokens,
)
.map_err(Error::UnsupportedWriteQuery)?;
for (offset, bytes) in allocation.header_patches {
change_set.overwrite(RELATIONSHIP_TYPE_TOKEN_ID_STORE, offset, bytes)?;
}
}
if !transaction.reused_relationship_type_tokens.is_empty() {
let update = super::id_generator::prepare_take_free_ids(
&database_directory.join(RELATIONSHIP_TYPE_TOKEN_ID_STORE),
transaction.reused_relationship_type_tokens.len(),
)?
.ok_or_else(|| {
Error::UnsupportedWriteQuery(
"reusable relationship type token IDs unavailable".to_owned(),
)
})?;
if update.ids != transaction.reused_relationship_type_tokens {
return Err(Error::UnsupportedWriteQuery(
"reusable relationship type token selection changed".to_owned(),
));
}
change_set.overwrite(
RELATIONSHIP_TYPE_TOKEN_ID_STORE,
update.root_page_offset,
update.root_page,
)?;
for (offset, bytes) in update.header_patches {
change_set.overwrite(RELATIONSHIP_TYPE_TOKEN_ID_STORE, offset, bytes)?;
}
}
if transaction.allocated_relationship_type_name_records > 0 {
let allocation = super::id_generator::allocate_high_ids(
self.relationship_type_name_id_header,
transaction.allocated_relationship_type_name_records,
)
.map_err(Error::UnsupportedWriteQuery)?;
for (offset, bytes) in allocation.header_patches {
change_set.overwrite(RELATIONSHIP_TYPE_NAME_ID_STORE, offset, bytes)?;
}
}
if transaction.allocated_node_records > 0 {
let allocation = super::id_generator::allocate_high_ids(
self.node_id_header,
transaction.allocated_node_records,
)
.map_err(Error::UnsupportedWriteQuery)?;
for (offset, bytes) in allocation.header_patches {
change_set.overwrite(NODE_ID_STORE, offset, bytes)?;
}
}
stage_id_lifecycle_changes(
&mut change_set,
&database_directory,
NODE_ID_STORE,
self.node_id_header,
transaction.allocated_node_records,
&transaction.reused_node_records,
&transaction.freed_node_records,
)?;
if transaction.allocated_relationship_records > 0 {
let allocation = super::id_generator::allocate_high_ids(
self.relationship_id_header,
transaction.allocated_relationship_records,
)
.map_err(Error::UnsupportedWriteQuery)?;
for (offset, bytes) in allocation.header_patches {
change_set.overwrite(RELATIONSHIP_ID_STORE, offset, bytes)?;
}
}
if transaction.allocated_relationship_group_records > 0 {
let allocation = super::id_generator::allocate_high_ids(
self.relationship_group_id_header,
transaction.allocated_relationship_group_records,
)
.map_err(Error::UnsupportedWriteQuery)?;
for (offset, bytes) in allocation.header_patches {
change_set.overwrite(RELATIONSHIP_GROUP_ID_STORE, offset, bytes)?;
}
}
stage_id_lifecycle_changes(
&mut change_set,
&database_directory,
RELATIONSHIP_GROUP_ID_STORE,
self.relationship_group_id_header,
transaction.allocated_relationship_group_records,
&transaction.reused_relationship_group_records,
&transaction.freed_relationship_group_records,
)?;
stage_id_lifecycle_changes(
&mut change_set,
&database_directory,
RELATIONSHIP_ID_STORE,
self.relationship_id_header,
transaction.allocated_relationship_records,
&transaction.reused_relationship_records,
&transaction.freed_relationship_records,
)?;
if transaction.allocated_node_records > 0
|| !transaction.reused_node_records.is_empty()
|| !transaction.freed_node_records.is_empty()
|| !transaction.node_label_count_deltas.is_empty()
|| !transaction.relationship_count_deltas.is_empty()
{
let mut count_increments = Vec::new();
let created_node_records = transaction
.allocated_node_records
.checked_add(transaction.reused_node_records.len() as u64)
.ok_or_else(|| {
Error::UnsupportedWriteQuery("node count increment overflow".to_owned())
})?;
let allocated = i64::try_from(created_node_records).map_err(|_| {
Error::UnsupportedWriteQuery("node count increment exceeds i64".to_owned())
})?;
let freed = i64::try_from(transaction.freed_node_records.len()).map_err(|_| {
Error::UnsupportedWriteQuery("node count decrement exceeds i64".to_owned())
})?;
let global_delta = allocated.checked_sub(freed).ok_or_else(|| {
Error::UnsupportedWriteQuery("node count delta overflow".to_owned())
})?;
if global_delta != 0 {
count_increments.push((-1, global_delta));
}
for (label_id, increment) in &transaction.node_label_count_deltas {
let label_id = i32::try_from(*label_id).map_err(|_| {
Error::UnsupportedWriteQuery(format!(
"label token ID {label_id} exceeds counts key range"
))
})?;
count_increments.push((label_id, *increment));
}
let new_keys = transaction
.allowed_missing_node_label_count_keys
.iter()
.map(|label_id| i32::try_from(*label_id))
.collect::<std::result::Result<Vec<_>, _>>()
.map_err(|_| {
Error::UnsupportedWriteQuery(
"new label token ID exceeds counts key range".to_owned(),
)
})?;
let relationship_count_deltas = transaction
.relationship_count_deltas
.iter()
.map(|(key, delta)| (*key, *delta))
.collect::<Vec<_>>();
if !count_increments.is_empty() || !relationship_count_deltas.is_empty() {
let counts = super::counts::prepare_counts_changes(
&database_directory.join(COUNTS_STORE),
&count_increments,
&new_keys,
&relationship_count_deltas,
&[],
)?;
change_set.overwrite(
COUNTS_STORE,
counts.root_page_offset,
counts.root_page,
)?;
}
}
if !transaction.relationship_group_degree_deltas.is_empty() {
let deltas = transaction
.relationship_group_degree_deltas
.iter()
.map(|(key, delta)| (*key, *delta))
.collect::<Vec<_>>();
let update = super::counts::prepare_relationship_group_degree_changes(
&database_directory.join(RELATIONSHIP_GROUP_DEGREE_STORE),
&deltas,
)?;
change_set.overwrite(
RELATIONSHIP_GROUP_DEGREE_STORE,
update.root_page_offset,
update.root_page,
)?;
}
if transaction.allocated_node_label_records > 0 {
let allocation = super::id_generator::allocate_high_ids(
self.node_label_id_header,
transaction.allocated_node_label_records,
)
.map_err(Error::UnsupportedWriteQuery)?;
for (offset, bytes) in allocation.header_patches {
change_set.overwrite(NODE_LABEL_ID_STORE, offset, bytes)?;
}
}
stage_id_lifecycle_changes(
&mut change_set,
&database_directory,
NODE_LABEL_ID_STORE,
self.node_label_id_header,
transaction.allocated_node_label_records,
&transaction.reused_node_label_records,
&transaction.freed_node_label_records,
)?;
match transaction::commit_with_limit(change_set, transaction.max_journal_bytes)? {
transaction::DurableCommitOutcome::Committed => Ok(()),
transaction::DurableCommitOutcome::CommittedStoragePoisoned(_error) => {
committed_storage_poisoned = true;
Ok(())
}
transaction::DurableCommitOutcome::RolledBack(error) => Err(error),
}
})();
if let Err(error) = commit_result {
self.storage = transaction.base_snapshot;
if matches!(
&error,
Error::Neo4jStore(crate::Neo4jStoreError::CommitRecovery { .. })
) {
self.poisoned = true;
coordinator_state.poisoned = true;
return Err(error);
}
if let Err(recovery_error) = self.reload_from_disk(&database_directory) {
self.poisoned = true;
coordinator_state.poisoned = true;
return Err(crate::Neo4jStoreError::CommitRecovery {
path: database_directory,
commit_error: Box::new(error),
recovery_error: Box::new(recovery_error),
}
.into());
}
return Err(error);
}
coordinator_state.version = committed_version;
coordinator_state.record_commit(committed_version, &transaction.write_set);
self.snapshot_version = coordinator_state.version;
if committed_storage_poisoned {
self.poisoned = true;
coordinator_state.poisoned = true;
return Ok(());
}
let mut refresh_result: Result<()> = (|| {
post_commit_refresh_checkpoint()?;
if transaction.allocated_property_records > 0
|| !transaction.reused_property_records.is_empty()
|| !transaction.freed_property_records.is_empty()
{
self.property_id_header = super::id_generator::validate_writable(
&database_directory,
PROPERTY_STORE,
self.property_record_capacity,
)?;
}
if transaction.allocated_string_records > 0
|| !transaction.reused_string_records.is_empty()
|| !transaction.freed_string_records.is_empty()
{
self.string_id_header = super::id_generator::validate_writable(
&database_directory,
STRING_STORE,
self.string_layout.record_capacity,
)?;
}
if transaction.allocated_array_records > 0
|| !transaction.reused_array_records.is_empty()
|| !transaction.freed_array_records.is_empty()
{
self.array_id_header = super::id_generator::validate_writable(
&database_directory,
ARRAY_STORE,
self.array_layout.record_capacity,
)?;
}
if transaction.allocated_property_key_tokens > 0
|| !transaction.reused_property_key_tokens.is_empty()
{
self.property_key_token_id_header = super::id_generator::validate_writable(
&database_directory,
PROPERTY_KEY_TOKEN_STORE,
self.property_key_token_capacity,
)?;
}
if transaction.allocated_property_key_name_records > 0 {
self.property_key_name_id_header = super::id_generator::validate_writable(
&database_directory,
PROPERTY_KEY_NAME_STORE,
self.property_key_name_layout.record_capacity,
)?;
}
if transaction.allocated_label_tokens > 0 || !transaction.reused_label_tokens.is_empty()
{
self.label_token_id_header = super::id_generator::validate_writable(
&database_directory,
LABEL_TOKEN_STORE,
self.label_token_capacity,
)?;
}
if transaction.allocated_label_name_records > 0 {
self.label_name_id_header = super::id_generator::validate_writable(
&database_directory,
LABEL_NAME_STORE,
self.label_name_layout.record_capacity,
)?;
}
if transaction.allocated_relationship_type_tokens > 0
|| !transaction.reused_relationship_type_tokens.is_empty()
{
self.relationship_type_token_id_header = super::id_generator::validate_writable(
&database_directory,
RELATIONSHIP_TYPE_TOKEN_STORE,
self.relationship_type_token_capacity,
)?;
}
if transaction.allocated_relationship_type_name_records > 0 {
self.relationship_type_name_id_header = super::id_generator::validate_writable(
&database_directory,
RELATIONSHIP_TYPE_NAME_STORE,
self.relationship_type_name_layout.record_capacity,
)?;
}
if transaction.allocated_node_records > 0
|| !transaction.reused_node_records.is_empty()
|| !transaction.freed_node_records.is_empty()
{
self.node_id_header = super::id_generator::validate_writable(
&database_directory,
NODE_STORE,
self.node_record_capacity,
)?;
}
if transaction.allocated_node_label_records > 0
|| !transaction.reused_node_label_records.is_empty()
|| !transaction.freed_node_label_records.is_empty()
{
self.node_label_id_header = super::id_generator::validate_writable(
&database_directory,
NODE_LABEL_STORE,
self.node_label_layout.record_capacity,
)?;
}
if transaction.allocated_relationship_records > 0
|| !transaction.reused_relationship_records.is_empty()
|| !transaction.freed_relationship_records.is_empty()
{
self.relationship_id_header = super::id_generator::validate_writable(
&database_directory,
RELATIONSHIP_STORE,
self.relationship_record_capacity,
)?;
}
if transaction.allocated_relationship_group_records > 0
|| !transaction.reused_relationship_group_records.is_empty()
|| !transaction.freed_relationship_group_records.is_empty()
{
self.relationship_group_id_header = super::id_generator::validate_writable(
&database_directory,
RELATIONSHIP_GROUP_STORE,
self.relationship_group_record_capacity,
)?;
}
Ok(())
})();
if rebasing && refresh_result.is_ok() {
refresh_result = self.reload_from_disk(&database_directory);
}
if refresh_result.is_err() {
self.poisoned = true;
coordinator_state.poisoned = true;
}
Ok(())
}
fn rollback_transaction(&mut self) -> Result<()> {
let transaction = self
.transaction
.take()
.ok_or(Error::Transaction(crate::TransactionErrorKind::NotActive))?;
let _snapshot_registration = self.snapshot_registration.take().ok_or_else(|| {
Error::Validation("active transaction has no MVCC snapshot registration".to_owned())
})?;
self.storage = transaction.base_snapshot;
Ok(())
}
fn transaction_state(&self) -> Option<TransactionState> {
self.transaction
.as_ref()
.map(|transaction| transaction.state)
}
fn transaction_state_mut(&mut self) -> Option<&mut TransactionState> {
self.transaction
.as_mut()
.map(|transaction| &mut transaction.state)
}
}
impl WritableStorage for Neo4jWritableStorage {
fn ensure_label_token(&mut self, label: &str) -> Result<()> {
Neo4jWritableStorage::ensure_label_token(self, label)?;
self.finish_mutation(())
}
fn ensure_relationship_type_token(&mut self, relationship_type: &str) -> Result<()> {
Neo4jWritableStorage::ensure_relationship_type_token(self, relationship_type)?;
self.finish_mutation(())
}
fn ensure_property_key_token(&mut self, property_key: &str) -> Result<()> {
Neo4jWritableStorage::ensure_property_key_token(self, property_key)?;
self.finish_mutation(())
}
fn create_node(&mut self, labels: Vec<String>, properties: Properties) -> Result<NodeId> {
for label in &labels {
self.ensure_label_token(label)?;
}
let labels = self.storage.existing_labels(&labels)?;
let labels_without_counts = labels
.iter()
.filter_map(|(label_id, label)| {
self.storage
.nodes_with_label(label)
.is_empty()
.then_some(*label_id)
})
.collect::<Vec<_>>();
let label_ids = labels.iter().map(|(id, _)| *id).collect::<Vec<_>>();
let (record_id, reused_record) = self.node_allocation_candidate()?;
let dynamic_label_records = match super::node::encode_inline_label_field(&label_ids) {
Ok(label_field) => (label_field, Vec::new(), Vec::new(), 0),
Err(_) => {
let payload = super::node::encode_dynamic_label_payload(record_id, &label_ids)
.map_err(Error::UnsupportedWriteQuery)?;
let required_records = payload.len().div_ceil(self.node_label_layout.payload_size);
let (record_ids, allocated_records) =
self.node_label_allocation_ids(required_records)?;
let first_id = record_ids[0];
let encoded = super::dynamic::encode_chain_for_ids(
&payload,
&record_ids,
self.node_label_layout,
)
.map_err(Error::UnsupportedWriteQuery)?;
(
DYNAMIC_LABEL_FLAG | first_id,
record_ids,
encoded,
allocated_records,
)
}
};
let (label_field, dynamic_label_record_ids, dynamic_label_records, allocated_label_records) =
dynamic_label_records;
let bytes = super::node::encode_record(None, None, false, label_field)
.map_err(Error::UnsupportedWriteQuery)?;
let id = self.storage.insert_node(record_id, labels)?;
let transaction = self.active_transaction_mut()?;
transaction.node_records.insert(record_id, bytes);
if reused_record {
transaction.reused_node_records.push(record_id);
} else {
transaction.allocated_node_records = transaction
.allocated_node_records
.checked_add(1)
.ok_or_else(|| {
Error::UnsupportedWriteQuery("node allocation overflow".to_owned())
})?;
}
transaction.allocated_node_label_records = transaction
.allocated_node_label_records
.checked_add(allocated_label_records)
.ok_or_else(|| {
Error::UnsupportedWriteQuery("dynamic node-label allocation overflow".to_owned())
})?;
if allocated_label_records == 0 {
transaction
.reused_node_label_records
.extend(&dynamic_label_record_ids);
}
if label_field & DYNAMIC_LABEL_FLAG != 0 {
transaction.node_label_chain_ids.insert(
label_field & MAX_DYNAMIC_LABEL_REFERENCE,
dynamic_label_record_ids,
);
}
transaction.node_label_records.extend(dynamic_label_records);
for label_id in label_ids {
let delta = transaction
.node_label_count_deltas
.entry(label_id)
.or_default();
*delta = delta.checked_add(1).ok_or_else(|| {
Error::UnsupportedWriteQuery("label count delta overflow".to_owned())
})?;
}
transaction
.allowed_missing_node_label_count_keys
.extend(labels_without_counts);
for (key, value) in properties {
self.set_node_property(id, key, value)?;
}
self.finish_mutation(id)
}
fn create_relationship(
&mut self,
start: NodeId,
end: NodeId,
relationship_type: String,
properties: Properties,
) -> Result<RelationshipId> {
self.active_transaction_mut()?;
let (start_neo4j_id, start_head, start_dense) =
self.storage.node_relationship_state(start)?;
let (end_neo4j_id, end_head, end_dense) = self.storage.node_relationship_state(end)?;
self.ensure_relationship_type_token(&relationship_type)?;
let type_id = self.storage.relationship_type_id(&relationship_type)?;
let (record_id, reused_record) = self.relationship_allocation_candidate()?;
if start_dense {
self.ensure_dense_relationship_group(start, type_id)?;
}
if end_dense && start != end {
self.ensure_dense_relationship_group(end, type_id)?;
}
let start_dense_head = if start_dense {
Some(self.storage.connect_dense_relationship_head(
start,
type_id,
if start == end { 2 } else { 0 },
record_id,
)?)
} else {
None
};
let end_dense_head = if end_dense && start != end {
Some(
self.storage
.connect_dense_relationship_head(end, type_id, 1, record_id)?,
)
} else {
None
};
let (start_degree, start_patch) = if let Some(head) = &start_dense_head {
(head.previous.unwrap_or(0), head.relationship_patch)
} else {
self.storage
.connect_sparse_relationship_head(start, record_id)?
};
let (end_degree, end_patch) = if start == end {
(start_degree, None)
} else if let Some(head) = &end_dense_head {
(head.previous.unwrap_or(0), head.relationship_patch)
} else {
self.storage
.connect_sparse_relationship_head(end, record_id)?
};
if (!start_dense && start_degree > NEO4J_DENSE_NODE_THRESHOLD)
|| (!end_dense && end_degree > NEO4J_DENSE_NODE_THRESHOLD)
{
return Err(Error::UnsupportedWriteQuery(
"sparse relationship creation reached Neo4j dense-node threshold; automatic sparse-to-dense conversion is not supported".to_owned(),
));
}
let record = super::relationship::RelationshipRecord {
neo4j_id: record_id,
start: start_neo4j_id,
end: end_neo4j_id,
type_id,
next_property: None,
first_previous: start_dense_head
.as_ref()
.map(|head| head.previous)
.unwrap_or(Some(start_degree)),
first_next: start_dense_head
.as_ref()
.map(|head| head.next)
.unwrap_or(start_head),
second_previous: if start == end {
start_dense_head
.as_ref()
.map(|head| head.previous)
.unwrap_or(Some(end_degree))
} else {
end_dense_head
.as_ref()
.map(|head| head.previous)
.unwrap_or(Some(end_degree))
},
second_next: if start == end {
start_dense_head
.as_ref()
.map(|head| head.next)
.unwrap_or(end_head)
} else {
end_dense_head
.as_ref()
.map(|head| head.next)
.unwrap_or(end_head)
},
first_in_start_chain: true,
first_in_end_chain: true,
start_node_dense: start_dense,
end_node_dense: end_dense,
properties: Properties::new(),
};
let bytes = super::relationship::encode_catalog_record(&record)
.map_err(Error::UnsupportedWriteQuery)?;
let id = self
.storage
.insert_relationship(record, relationship_type)?;
if !start_dense {
self.stage_node_relationship_head(start_neo4j_id, record_id)?;
}
if end != start && !end_dense {
self.stage_node_relationship_head(end_neo4j_id, record_id)?;
}
if let Some(head) = start_dense_head {
self.active_transaction_mut()?
.relationship_group_records
.insert(head.group_id, head.group_patch);
if let Some(delta) = head.degree_delta {
self.stage_relationship_group_degree_delta(
head.group_id,
if start == end { 2 } else { 0 },
delta,
)?;
}
}
if let Some(head) = end_dense_head {
self.active_transaction_mut()?
.relationship_group_records
.insert(head.group_id, head.group_patch);
if let Some(delta) = head.degree_delta {
self.stage_relationship_group_degree_delta(head.group_id, 1, delta)?;
}
}
let transaction = self.active_transaction_mut()?;
if let Some((old_id, bytes)) = start_patch {
transaction.relationship_records.insert(old_id, bytes);
}
if let Some((old_id, bytes)) = end_patch {
transaction.relationship_records.insert(old_id, bytes);
}
transaction.relationship_records.insert(record_id, bytes);
if reused_record {
transaction.reused_relationship_records.push(record_id);
} else {
transaction.allocated_relationship_records = transaction
.allocated_relationship_records
.checked_add(1)
.ok_or_else(|| {
Error::UnsupportedWriteQuery(
"relationship record allocation overflow".to_owned(),
)
})?;
}
let count_keys = self.relationship_count_keys(start, end, type_id)?;
let transaction = self.active_transaction_mut()?;
for key in count_keys {
let delta = transaction
.relationship_count_deltas
.entry(key)
.or_default();
*delta = delta.checked_add(1).ok_or_else(|| {
Error::UnsupportedWriteQuery("relationship count delta overflow".to_owned())
})?;
}
for (key, value) in properties {
self.set_relationship_property(id, key, value)?;
}
self.finish_mutation(id)
}
fn set_node_property(&mut self, id: NodeId, key: String, value: Value) -> Result<()> {
self.active_transaction_mut()?;
let node = self
.storage
.node(id)
.ok_or_else(|| Error::Execution(format!("node id {id} does not exist")))?;
if node.properties.get(&key) == Some(&value) {
return Ok(());
}
let existing_dynamic = self.storage.node_dynamic_property(id, &key)?;
self.ensure_property_key_token(&key)?;
let (allocation_id, reused_candidate) = self.property_allocation_candidate()?;
let dynamic_string = self.prepare_dynamic_string(
&value,
existing_dynamic
.filter(|reference| reference.kind == super::property::DynamicPropertyKind::String),
)?;
let dynamic_string_id = dynamic_string.as_ref().map(|update| update.first_id);
let dynamic_array = self.prepare_dynamic_array(
&value,
existing_dynamic
.filter(|reference| reference.kind == super::property::DynamicPropertyKind::Array),
)?;
let dynamic_array_id = dynamic_array.as_ref().map(|update| update.first_id);
let dynamic_deletion = self.prepare_dynamic_property_deletion(
existing_dynamic,
dynamic_string_id.or(dynamic_array_id),
)?;
let update = self.storage.stage_node_property_update(
id,
key,
value,
allocation_id,
dynamic_string_id,
dynamic_array_id,
)?;
if let Some(new_head) = update.new_head {
self.stage_node_head(update.owner_record_id, new_head)?;
}
self.collect_property_update(update, allocation_id, reused_candidate)?;
self.collect_dynamic_string(dynamic_string)?;
self.collect_dynamic_array(dynamic_array)?;
self.collect_dynamic_property_deletion(dynamic_deletion)?;
self.finish_mutation(())
}
fn remove_node_property(&mut self, id: NodeId, key: &str) -> Result<()> {
self.active_transaction_mut()?;
let dynamic = self.storage.node_dynamic_property(id, key)?;
let dynamic_deletion = self.prepare_dynamic_property_deletion(dynamic, None)?;
let Some(removal) = self.storage.stage_node_property_removal(id, key)? else {
return self.finish_mutation(());
};
debug_assert_eq!(removal.dynamic, dynamic);
if let Some(new_head) = removal.owner_head {
self.stage_node_head(removal.owner_record_id, new_head.unwrap_or(u32::MAX as u64))?;
}
let transaction = self.active_transaction_mut()?;
transaction
.property_records
.extend(removal.property_records);
if let Some(record_id) = removal.freed_record_id {
transaction.freed_property_records.insert(record_id);
}
self.collect_dynamic_property_deletion(dynamic_deletion)?;
self.finish_mutation(())
}
fn replace_node_properties(&mut self, id: NodeId, properties: Properties) -> Result<()> {
self.active_transaction_mut()?;
let current = self
.storage
.node(id)
.ok_or_else(|| Error::Execution(format!("node id {id} does not exist")))?
.properties
.clone();
for (key, value) in &properties {
if current.get(key) != Some(value) {
self.set_node_property(id, key.clone(), value.clone())?;
}
}
for key in current.keys() {
if !properties.contains_key(key) {
self.remove_node_property(id, key)?;
}
}
self.finish_mutation(())
}
fn set_node_label(&mut self, id: NodeId, label: String) -> Result<()> {
self.active_transaction_mut()?;
let node = self
.storage
.node(id)
.ok_or_else(|| Error::Execution(format!("node id {id} does not exist")))?;
if node.labels.contains(&label) {
return Ok(());
}
self.ensure_label_token(&label)?;
let resolved = self.storage.existing_labels(std::slice::from_ref(&label))?;
let label_id = resolved[0].0;
let allow_missing_count = self.storage.nodes_with_label(&label).is_empty();
let mut label_ids = self.storage.node_label_ids(id)?;
label_ids.push(label_id);
let neo4j_id = self.storage.node_neo4j_id(id)?;
let update = self.prepare_node_label_update(neo4j_id, &label_ids)?;
let next_increment = self
.transaction
.as_ref()
.and_then(|transaction| transaction.node_label_count_deltas.get(&label_id))
.copied()
.unwrap_or_default()
.checked_add(1)
.ok_or_else(|| Error::UnsupportedWriteQuery("label count overflow".to_owned()))?;
self.collect_node_label_update(neo4j_id, update)?;
self.storage.insert_node_label(id, label)?;
self.apply_relationship_counts_for_label_change(id, label_id, 1)?;
let transaction = self.active_transaction_mut()?;
if allow_missing_count {
transaction
.allowed_missing_node_label_count_keys
.insert(label_id);
}
if next_increment == 0 {
transaction.node_label_count_deltas.remove(&label_id);
} else {
transaction
.node_label_count_deltas
.insert(label_id, next_increment);
}
self.finish_mutation(())
}
fn remove_node_label(&mut self, id: NodeId, label: &str) -> Result<()> {
self.active_transaction_mut()?;
let node = self
.storage
.node(id)
.ok_or_else(|| Error::Execution(format!("node id {id} does not exist")))?;
if !node.labels.iter().any(|node_label| node_label == label) {
return Ok(());
}
let resolved = self.storage.existing_labels(&[label.to_owned()])?;
let label_id = resolved[0].0;
let mut label_ids = self.storage.node_label_ids(id)?;
label_ids.retain(|existing| *existing != label_id);
let neo4j_id = self.storage.node_neo4j_id(id)?;
let update = self.prepare_node_label_update(neo4j_id, &label_ids)?;
let next_delta = self
.transaction
.as_ref()
.and_then(|transaction| transaction.node_label_count_deltas.get(&label_id))
.copied()
.unwrap_or_default()
.checked_sub(1)
.ok_or_else(|| Error::UnsupportedWriteQuery("label count delta overflow".to_owned()))?;
self.collect_node_label_update(neo4j_id, update)?;
self.storage.remove_node_label(id, label)?;
self.apply_relationship_counts_for_label_change(id, label_id, -1)?;
let transaction = self.active_transaction_mut()?;
if next_delta == 0 {
transaction.node_label_count_deltas.remove(&label_id);
} else {
transaction
.node_label_count_deltas
.insert(label_id, next_delta);
}
self.finish_mutation(())
}
fn set_relationship_property(
&mut self,
id: RelationshipId,
key: String,
value: Value,
) -> Result<()> {
self.active_transaction_mut()?;
let relationship = self
.storage
.relationship(id)
.ok_or_else(|| Error::Execution(format!("relationship id {id} does not exist")))?;
if relationship.properties.get(&key) == Some(&value) {
return Ok(());
}
let existing_dynamic = self.storage.relationship_dynamic_property(id, &key)?;
self.ensure_property_key_token(&key)?;
let (allocation_id, reused_candidate) = self.property_allocation_candidate()?;
let dynamic_string = self.prepare_dynamic_string(
&value,
existing_dynamic
.filter(|reference| reference.kind == super::property::DynamicPropertyKind::String),
)?;
let dynamic_string_id = dynamic_string.as_ref().map(|update| update.first_id);
let dynamic_array = self.prepare_dynamic_array(
&value,
existing_dynamic
.filter(|reference| reference.kind == super::property::DynamicPropertyKind::Array),
)?;
let dynamic_array_id = dynamic_array.as_ref().map(|update| update.first_id);
let dynamic_deletion = self.prepare_dynamic_property_deletion(
existing_dynamic,
dynamic_string_id.or(dynamic_array_id),
)?;
let update = self.storage.stage_relationship_property_update(
id,
key,
value,
allocation_id,
dynamic_string_id,
dynamic_array_id,
)?;
if let Some(new_head) = update.new_head {
self.stage_relationship_head(update.owner_record_id, new_head)?;
}
self.collect_property_update(update, allocation_id, reused_candidate)?;
self.collect_dynamic_string(dynamic_string)?;
self.collect_dynamic_array(dynamic_array)?;
self.collect_dynamic_property_deletion(dynamic_deletion)?;
self.finish_mutation(())
}
fn remove_relationship_property(&mut self, id: RelationshipId, key: &str) -> Result<()> {
self.active_transaction_mut()?;
let dynamic = self.storage.relationship_dynamic_property(id, key)?;
let dynamic_deletion = self.prepare_dynamic_property_deletion(dynamic, None)?;
let Some(removal) = self.storage.stage_relationship_property_removal(id, key)? else {
return self.finish_mutation(());
};
debug_assert_eq!(removal.dynamic, dynamic);
if let Some(new_head) = removal.owner_head {
self.stage_relationship_head(
removal.owner_record_id,
new_head.unwrap_or(u32::MAX as u64),
)?;
}
let transaction = self.active_transaction_mut()?;
transaction
.property_records
.extend(removal.property_records);
if let Some(record_id) = removal.freed_record_id {
transaction.freed_property_records.insert(record_id);
}
self.collect_dynamic_property_deletion(dynamic_deletion)?;
self.finish_mutation(())
}
fn replace_relationship_properties(
&mut self,
id: RelationshipId,
properties: Properties,
) -> Result<()> {
self.active_transaction_mut()?;
let current = self
.storage
.relationship(id)
.ok_or_else(|| Error::Execution(format!("relationship id {id} does not exist")))?
.properties
.clone();
for (key, value) in &properties {
if current.get(key) != Some(value) {
self.set_relationship_property(id, key.clone(), value.clone())?;
}
}
for key in current.keys() {
if !properties.contains_key(key) {
self.remove_relationship_property(id, key)?;
}
}
self.finish_mutation(())
}
fn delete_relationship(&mut self, id: RelationshipId) -> Result<()> {
self.active_transaction_mut()?;
let relationship = self
.storage
.relationship(id)
.ok_or_else(|| Error::Execution(format!("relationship id {id} does not exist")))?
.clone();
let neo4j_id = relationship.external_id.parse::<u64>().map_err(|_| {
Error::Validation(format!(
"relationship {id} has a non-numeric Neo4j record ID"
))
})?;
let (type_id, first_property) = self
.storage
.relationships
.records
.iter()
.find(|record| record.neo4j_id == neo4j_id)
.map(|record| (record.type_id, record.next_property))
.ok_or_else(|| Error::Validation("relationship catalog is inconsistent".to_owned()))?;
let count_keys =
self.relationship_count_keys(relationship.start, relationship.end, type_id)?;
let property_deletion = self.prepare_property_chain_deletion(
first_property,
&format!("relationship {neo4j_id} property deletion"),
)?;
let deleted = self.storage.delete_sparse_relationship(id)?;
for (node_id, head) in &deleted.node_heads {
self.stage_node_relationship_head(*node_id, head.unwrap_or(u32::MAX as u64))?;
}
self.collect_property_chain_deletion(property_deletion)?;
self.active_transaction_mut()?
.relationship_records
.extend(deleted.record_patches);
self.active_transaction_mut()?
.relationship_group_records
.extend(deleted.group_patches);
self.active_transaction_mut()?
.freed_relationship_group_records
.extend(deleted.freed_group_records);
for (group_id, direction, delta) in deleted.degree_deltas {
self.stage_relationship_group_degree_delta(group_id, direction, delta)?;
}
self.active_transaction_mut()?
.freed_relationship_records
.insert(deleted.neo4j_id);
for key in count_keys {
let transaction = self.active_transaction_mut()?;
let delta = transaction
.relationship_count_deltas
.entry(key)
.or_default();
*delta = delta.checked_sub(1).ok_or_else(|| {
Error::UnsupportedWriteQuery("relationship count delta overflow".to_owned())
})?;
if *delta == 0 {
transaction.relationship_count_deltas.remove(&key);
}
}
debug_assert_eq!(deleted.start, relationship.start);
debug_assert_eq!(deleted.end, relationship.end);
debug_assert_eq!(deleted.type_id, type_id);
self.finish_mutation(())
}
fn delete_node(&mut self, id: NodeId) -> Result<()> {
self.active_transaction_mut()?;
let neo4j_id = self.storage.node_neo4j_id(id)?;
let first_property = self
.storage
.nodes
.records
.get(id)
.and_then(|node| node.next_property);
let mut bytes =
if let Some(bytes) = self.active_transaction()?.node_records.get(&neo4j_id)? {
bytes
} else {
super::node::read_record(
self.storage.database_directory(),
self.storage.format(),
neo4j_id,
)?
};
let dynamic_label_records = if super::node::label_field(&bytes) & DYNAMIC_LABEL_FLAG != 0 {
let first_id = super::node::label_field(&bytes) & MAX_DYNAMIC_LABEL_REFERENCE;
let records = self.prepare_workspace_dynamic_chain_deletion(
NODE_LABEL_STORE,
"dynamic node label",
first_id,
&format!("node {neo4j_id} dynamic label deletion"),
)?;
Some(records)
} else {
None
};
let property_deletion = self.prepare_property_chain_deletion(
first_property,
&format!("node {neo4j_id} property deletion"),
)?;
let deleted = self.storage.delete_isolated_node(id)?;
bytes[0] &= !1;
self.collect_property_chain_deletion(property_deletion)?;
let transaction = self.active_transaction_mut()?;
transaction.node_records.insert(deleted.neo4j_id, bytes);
transaction.freed_node_records.insert(deleted.neo4j_id);
if let Some(records) = dynamic_label_records {
for (record_id, bytes) in records {
transaction.node_label_records.insert(record_id, bytes);
transaction.freed_node_label_records.insert(record_id);
}
}
for label_id in deleted.label_ids {
let delta = transaction
.node_label_count_deltas
.entry(label_id)
.or_default();
*delta = delta.checked_sub(1).ok_or_else(|| {
Error::UnsupportedWriteQuery("node label count delta overflow".to_owned())
})?;
if *delta == 0 {
transaction.node_label_count_deltas.remove(&label_id);
}
}
self.finish_mutation(())
}
fn detach_delete_node(&mut self, id: NodeId) -> Result<()> {
self.active_transaction_mut()?;
self.storage
.node(id)
.ok_or_else(|| Error::Execution(format!("node id {id} does not exist")))?;
let relationship_ids = self
.storage
.outgoing_relationships(id)
.iter()
.chain(self.storage.incoming_relationships(id))
.copied()
.collect::<BTreeSet<_>>();
for relationship_id in relationship_ids {
self.delete_relationship(relationship_id)?;
}
self.delete_node(id)
}
}
#[cfg(test)]
mod tests {
use std::fs;
use std::io::{Seek, SeekFrom, Write};
use super::*;
fn copy_directory(source: &Path, target: &Path) {
fs::create_dir_all(target).unwrap();
for entry in fs::read_dir(source).unwrap() {
let entry = entry.unwrap();
let source_path = entry.path();
let target_path = target.join(entry.file_name());
if entry.file_type().unwrap().is_dir() {
copy_directory(&source_path, &target_path);
} else {
fs::copy(source_path, target_path).unwrap();
}
}
}
fn mark_ids_free(path: &Path, ids: &[u64]) {
let update = super::super::id_generator::prepare_mark_deleted_and_free(path, ids).unwrap();
let mut file = fs::OpenOptions::new().write(true).open(path).unwrap();
file.seek(SeekFrom::Start(update.root_page_offset)).unwrap();
file.write_all(&update.root_page).unwrap();
for (offset, bytes) in update.header_patches {
file.seek(SeekFrom::Start(offset)).unwrap();
file.write_all(&bytes).unwrap();
}
file.sync_all().unwrap();
}
#[test]
fn mvcc_history_retains_only_the_first_change_after_each_active_snapshot() {
let mut state = MvccState::default();
state.register_snapshot(0).unwrap();
let mut first = WriteSet::default();
first.insert_fixed(FixedStore::Property, 7);
let dependency = first.pending_fixed()[0];
first.capture_before_image(dependency, vec![1]);
state.record_commit(1, &first);
state.register_snapshot(1).unwrap();
let mut second = WriteSet::default();
second.insert_fixed(FixedStore::Property, 7);
second.capture_before_image(dependency, vec![2]);
state.record_commit(2, &second);
assert_eq!(state.record_history[&dependency].len(), 2);
state.unregister_snapshot(1);
assert_eq!(state.record_history[&dependency].len(), 1);
assert_eq!(state.record_history[&dependency][0].committed_version, 1);
state.unregister_snapshot(0);
assert!(state.record_history.is_empty());
}
#[test]
fn mvcc_history_waits_for_every_transaction_at_the_same_snapshot() {
let mut state = MvccState::default();
state.register_snapshot(0).unwrap();
state.register_snapshot(0).unwrap();
let mut write_set = WriteSet::default();
write_set.insert_fixed(FixedStore::Node, 4);
let dependency = write_set.pending_fixed()[0];
write_set.capture_before_image(dependency, vec![1]);
state.record_commit(1, &write_set);
assert_eq!(state.active_snapshots[&0], 2);
assert!(state.record_changed_after(dependency, 0));
state.unregister_snapshot(0);
assert_eq!(state.active_snapshots[&0], 1);
assert!(state.record_changed_after(dependency, 0));
assert_eq!(state.before_image_at(dependency, 0), Some(&[1][..]));
state.unregister_snapshot(0);
assert!(state.active_snapshots.is_empty());
assert!(state.record_history.is_empty());
}
fn stress_payload(round: usize, lane: usize) -> String {
format!("{round:016x}-{lane}-{}", "x".repeat(32))
}
fn apply_stress_property(
writable: &mut Neo4jWritableStorage,
node: NodeId,
value: &Option<String>,
) {
match value {
Some(value) => writable
.set_node_property(node, "payload".to_owned(), Value::String(value.clone()))
.unwrap(),
None => writable.remove_node_property(node, "payload").unwrap(),
}
}
fn run_deterministic_mvcc_rebase_stress(rounds: usize) {
const NODE_COUNT: usize = 8;
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let nodes = (0..NODE_COUNT)
.map(|index| {
setup
.create_node(
Vec::new(),
Properties::from([(
"payload".to_owned(),
Value::String(stress_payload(usize::MAX, index)),
)]),
)
.unwrap()
})
.collect::<Vec<_>>();
setup.commit_transaction().unwrap();
drop(setup);
let mut expected = (0..NODE_COUNT)
.map(|index| Some(stress_payload(usize::MAX, index)))
.collect::<Vec<_>>();
let mut first = Neo4jWritableStorage::open(&database).unwrap();
let mut second = Neo4jWritableStorage::open(&database).unwrap();
let mut random = 0x4d59_5df4_d0f3_3173_u64;
let mut conflicts = 0_usize;
let mut stale_commits = 0_usize;
for round in 0..rounds {
random = random
.wrapping_mul(6_364_136_223_846_793_005)
.wrapping_add(1_442_695_040_888_963_407);
let first_index = (random as usize) % NODE_COUNT;
let second_index = if round % 4 == 0 {
first_index
} else {
(first_index + 1 + ((random >> 32) as usize % (NODE_COUNT - 1))) % NODE_COUNT
};
let first_value = if round % 6 == 0 && expected[first_index].is_some() {
None
} else {
Some(stress_payload(round, 0))
};
let second_value = if round % 10 == 0 && expected[second_index].is_some() {
None
} else {
Some(stress_payload(round, 1))
};
first.begin_transaction().unwrap();
second.begin_transaction().unwrap();
apply_stress_property(&mut first, nodes[first_index], &first_value);
apply_stress_property(&mut second, nodes[second_index], &second_value);
first.commit_transaction().unwrap();
expected[first_index] = first_value;
match second.commit_transaction() {
Ok(()) => {
stale_commits += 1;
expected[second_index] = second_value;
}
Err(Error::Transaction(crate::TransactionErrorKind::Conflict)) => {
conflicts += 1;
second.begin_transaction().unwrap();
apply_stress_property(&mut second, nodes[second_index], &second_value);
second.commit_transaction().unwrap();
expected[second_index] = second_value;
}
Err(error) => panic!("unexpected MVCC stress commit error: {error:?}"),
}
}
assert!(conflicts > 0, "stress run did not exercise conflicts");
assert!(
stale_commits > 0,
"stress run did not exercise stale rebasing"
);
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
for (index, expected) in expected.into_iter().enumerate() {
assert_eq!(
verifier
.node(nodes[index])
.unwrap()
.properties
.get("payload")
.cloned(),
expected.map(Value::String),
"unexpected payload for stress node {index}"
);
}
}
#[test]
fn deterministic_mvcc_rebase_stress_matches_the_reference_model() {
run_deterministic_mvcc_rebase_stress(80);
}
#[test]
#[ignore = "long-running MVCC stress test"]
fn deterministic_mvcc_rebase_stress_long_running() {
let rounds = std::env::var("LITTLEGRAPH_MVCC_STRESS_ROUNDS")
.ok()
.and_then(|value| value.parse().ok())
.unwrap_or(2_000);
run_deterministic_mvcc_rebase_stress(rounds);
}
#[test]
fn extends_dynamic_chain_after_all_pending_allocations() {
let mut ids = vec![3, 9];
let allocated = extend_dynamic_chain_ids(&mut ids, 5, 20, 4, 30, "string").unwrap();
assert_eq!(allocated, 3);
assert_eq!(ids, [3, 9, 24, 25, 26]);
let allocated = extend_dynamic_chain_ids(&mut ids, 5, u64::MAX, 1, 30, "string").unwrap();
assert_eq!(allocated, 0);
assert_eq!(ids, [3, 9, 24, 25, 26]);
}
#[test]
fn reusable_id_selection_must_extend_the_existing_prefix() {
assert_eq!(next_reusable_id(&[3], &[]), Some(3));
assert_eq!(next_reusable_id(&[3, 9, 12], &[3, 9]), Some(12));
assert_eq!(next_reusable_id(&[3, 10, 12], &[3, 9]), None);
assert_eq!(next_reusable_id(&[3, 9], &[3, 9]), None);
assert_eq!(next_reusable_id(&[3, 9, 12, 15], &[3, 9]), None);
}
#[test]
fn rejects_invalid_dynamic_chain_extensions_without_mutating_ids() {
let original = vec![3, 9];
let mut ids = original.clone();
let error = extend_dynamic_chain_ids(&mut ids, 1, 20, 0, 30, "array").unwrap_err();
assert!(error.to_string().contains("reclamation"));
assert_eq!(ids, original);
let mut ids = original.clone();
let error =
extend_dynamic_chain_ids(&mut ids, 3, u64::MAX, 1, u64::MAX, "array").unwrap_err();
assert!(error.to_string().contains("ID overflow"));
assert_eq!(ids, original);
let mut ids = original.clone();
let error = extend_dynamic_chain_ids(&mut ids, 4, 20, 0, 21, "array").unwrap_err();
assert!(error.to_string().contains("preallocated record capacity"));
assert_eq!(ids, original);
}
#[test]
fn workspace_usage_charges_dynamic_vector_values() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut writable = Neo4jWritableStorage::open(&database).unwrap();
writable.begin_transaction().unwrap();
let before = writable.workspace_usage().unwrap();
writable
.transaction
.as_mut()
.unwrap()
.string_chain_ids
.insert(42, vec![1, 2, 3]);
let after = writable.workspace_usage().unwrap();
assert_eq!(after.records - before.records, 1);
assert_eq!(
after.bytes - before.bytes,
std::mem::size_of::<u64>()
+ std::mem::size_of::<Vec<u64>>()
+ 3 * std::mem::size_of::<u64>()
+ 3 * std::mem::size_of::<usize>()
);
let before_record = after;
writable
.transaction
.as_mut()
.unwrap()
.string_records
.insert(42, vec![0; 5]);
let after_record = writable.workspace_usage().unwrap();
assert_eq!(after_record.records - before_record.records, 1);
assert_eq!(
after_record.bytes - before_record.bytes,
std::mem::size_of::<u64>()
+ std::mem::size_of::<Vec<u8>>()
+ 5
+ 3 * std::mem::size_of::<usize>()
);
let before_lifecycle = after_record;
let transaction = writable.transaction.as_mut().unwrap();
transaction.reused_property_records.push(7);
transaction.freed_property_records.insert(8);
let after_lifecycle = writable.workspace_usage().unwrap();
assert_eq!(after_lifecycle.records - before_lifecycle.records, 2);
assert_eq!(
after_lifecycle.bytes - before_lifecycle.bytes,
2 * std::mem::size_of::<u64>() + 3 * std::mem::size_of::<usize>()
);
writable.rollback_transaction().unwrap();
}
#[test]
fn spilling_preserves_total_workspace_usage_and_releases_record_payloads() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut writable = Neo4jWritableStorage::open(&database).unwrap();
writable.configure_workspace_spill(Some(0), Some(1024 * 1024), None, Some(99));
writable.begin_transaction().unwrap();
let transaction = writable.transaction.as_mut().unwrap();
transaction.node_records.insert(1, [7; NODE_RECORD_SIZE]);
transaction.string_records.insert(2, vec![8; 4096]);
let before = transaction.workspace_usage().unwrap();
assert_eq!(
transaction.hot_record_bytes().unwrap(),
4096 + NODE_RECORD_SIZE
);
transaction.spill_record_maps().unwrap();
assert_eq!(transaction.hot_record_bytes().unwrap(), 0);
let after = transaction.workspace_usage().unwrap();
assert_eq!(after.bytes, before.bytes);
assert_eq!(after.records, before.records);
assert_eq!(
transaction.node_records.get(&1).unwrap(),
Some([7; NODE_RECORD_SIZE])
);
assert_eq!(
transaction.string_records.get(&2).unwrap(),
Some(vec![8; 4096])
);
writable.rollback_transaction().unwrap();
}
#[test]
fn node_creation_populates_physical_and_shared_dependencies() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut writable = Neo4jWritableStorage::open(&database).unwrap();
writable.begin_transaction().unwrap();
writable
.create_node(vec!["Person".to_owned()], Properties::new())
.unwrap();
let write_set = &writable.transaction.as_ref().unwrap().write_set;
assert!(write_set.contains_fixed(FixedStore::Node, 0));
assert!(write_set.contains_fixed(FixedStore::LabelToken, 0));
assert!(write_set.contains_shared(SharedDependency::IdAllocation));
assert!(write_set.contains_shared(SharedDependency::Counts));
assert!(write_set.contains_shared(SharedDependency::Tokens));
assert_eq!(
write_set.before_image(FixedStore::Node, 0),
Some(&[0; NODE_RECORD_SIZE][..])
);
writable.rollback_transaction().unwrap();
}
#[test]
fn later_mutations_do_not_replace_the_first_before_image() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut writable = Neo4jWritableStorage::open(&database).unwrap();
writable.begin_transaction().unwrap();
let node = writable.create_node(Vec::new(), Properties::new()).unwrap();
let before = writable
.transaction
.as_ref()
.unwrap()
.write_set
.before_image(FixedStore::Node, 0)
.unwrap()
.to_vec();
writable
.set_node_property(node, "name".to_owned(), Value::String("Alice".to_owned()))
.unwrap();
assert_eq!(
writable
.transaction
.as_ref()
.unwrap()
.write_set
.before_image(FixedStore::Node, 0),
Some(before.as_slice())
);
writable.rollback_transaction().unwrap();
}
#[test]
fn stale_mutations_capture_the_snapshot_image_from_history() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut stale = Neo4jWritableStorage::open(&database).unwrap();
let mut committer = Neo4jWritableStorage::open(&database).unwrap();
stale.begin_transaction().unwrap();
committer.begin_transaction().unwrap();
committer
.create_node(Vec::new(), Properties::new())
.unwrap();
committer.commit_transaction().unwrap();
stale.create_node(Vec::new(), Properties::new()).unwrap();
let write_set = &stale.transaction.as_ref().unwrap().write_set;
assert_eq!(
write_set.before_image(FixedStore::Node, 0),
Some(&[0; NODE_RECORD_SIZE][..])
);
stale.rollback_transaction().unwrap();
}
#[test]
fn stale_disjoint_fixed_record_updates_rebase_and_refresh_the_session() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let first_node = setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), Value::Integer(1))]),
)
.unwrap();
let second_node = setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), Value::Integer(2))]),
)
.unwrap();
setup.commit_transaction().unwrap();
let mut first = Neo4jWritableStorage::open(&database).unwrap();
let mut second = Neo4jWritableStorage::open(&database).unwrap();
first.begin_transaction().unwrap();
second.begin_transaction().unwrap();
first
.set_node_property(first_node, "value".to_owned(), Value::Integer(10))
.unwrap();
second
.set_node_property(second_node, "value".to_owned(), Value::Integer(20))
.unwrap();
assert!(first
.transaction
.as_ref()
.unwrap()
.write_set
.rebase_dependencies()
.is_some());
assert!(second
.transaction
.as_ref()
.unwrap()
.write_set
.rebase_dependencies()
.is_some());
first.commit_transaction().unwrap();
second.commit_transaction().unwrap();
assert_eq!(
second.storage.node(first_node).unwrap().properties["value"],
Value::Integer(10)
);
assert_eq!(
second.storage.node(second_node).unwrap().properties["value"],
Value::Integer(20)
);
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert_eq!(
verifier.node(first_node).unwrap().properties["value"],
Value::Integer(10)
);
assert_eq!(
verifier.node(second_node).unwrap().properties["value"],
Value::Integer(20)
);
}
#[test]
fn late_disjoint_update_uses_history_and_releases_it_after_commit() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let first_node = setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), Value::Integer(1))]),
)
.unwrap();
let second_node = setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), Value::Integer(2))]),
)
.unwrap();
setup.commit_transaction().unwrap();
let mut stale = Neo4jWritableStorage::open(&database).unwrap();
let mut committer = Neo4jWritableStorage::open(&database).unwrap();
stale.begin_transaction().unwrap();
committer.begin_transaction().unwrap();
committer
.set_node_property(first_node, "value".to_owned(), Value::Integer(10))
.unwrap();
committer.commit_transaction().unwrap();
{
let state = stale.coordinator.state.lock().unwrap();
assert_eq!(state.active_snapshots.values().sum::<usize>(), 1);
assert!(!state.record_history.is_empty());
}
stale
.set_node_property(second_node, "value".to_owned(), Value::Integer(20))
.unwrap();
stale.commit_transaction().unwrap();
let state = stale.coordinator.state.lock().unwrap();
assert!(state.active_snapshots.is_empty());
assert!(state.record_history.is_empty());
drop(state);
assert_eq!(
stale.storage.node(first_node).unwrap().properties["value"],
Value::Integer(10)
);
assert_eq!(
stale.storage.node(second_node).unwrap().properties["value"],
Value::Integer(20)
);
}
#[test]
fn late_overlapping_update_uses_history_and_conflicts() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let node = setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), Value::Integer(1))]),
)
.unwrap();
setup.commit_transaction().unwrap();
let mut stale = Neo4jWritableStorage::open(&database).unwrap();
let mut committer = Neo4jWritableStorage::open(&database).unwrap();
stale.begin_transaction().unwrap();
committer.begin_transaction().unwrap();
committer
.set_node_property(node, "value".to_owned(), Value::Integer(10))
.unwrap();
committer.commit_transaction().unwrap();
stale
.set_node_property(node, "value".to_owned(), Value::Integer(20))
.unwrap();
assert!(matches!(
stale.commit_transaction(),
Err(Error::Transaction(crate::TransactionErrorKind::Conflict))
));
let state = stale.coordinator.state.lock().unwrap();
assert!(state.active_snapshots.is_empty());
assert!(state.record_history.is_empty());
drop(state);
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert_eq!(
verifier.node(node).unwrap().properties["value"],
Value::Integer(10)
);
}
#[test]
fn overlapping_update_conflicts_after_record_bytes_return_to_snapshot_image() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let node = setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), Value::Integer(1))]),
)
.unwrap();
setup.commit_transaction().unwrap();
let mut stale = Neo4jWritableStorage::open(&database).unwrap();
stale.begin_transaction().unwrap();
let mut first = Neo4jWritableStorage::open(&database).unwrap();
first.begin_transaction().unwrap();
first
.set_node_property(node, "value".to_owned(), Value::Integer(2))
.unwrap();
first.commit_transaction().unwrap();
let mut second = Neo4jWritableStorage::open(&database).unwrap();
second.begin_transaction().unwrap();
second
.set_node_property(node, "value".to_owned(), Value::Integer(1))
.unwrap();
second.commit_transaction().unwrap();
stale
.set_node_property(node, "value".to_owned(), Value::Integer(3))
.unwrap();
{
let transaction = stale.transaction.as_ref().unwrap();
let (dependency, before_image) = transaction
.write_set
.rebase_dependencies()
.unwrap()
.into_iter()
.find(|(dependency, _)| dependency.store == FixedStore::Property)
.unwrap();
let (relative_path, record_size, record_type) =
stale.fixed_store_spec(dependency.store);
let path = database.join(relative_path);
let layout = PageLayout::new(&path, stale.storage.format(), record_size).unwrap();
let mut reader = RecordReader::open(path, record_type, layout).unwrap();
assert_eq!(reader.read(dependency.record_id).unwrap(), before_image);
}
assert!(matches!(
stale.commit_transaction(),
Err(Error::Transaction(crate::TransactionErrorKind::Conflict))
));
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert_eq!(
verifier.node(node).unwrap().properties["value"],
Value::Integer(1)
);
}
#[test]
fn mvcc_version_overflow_is_rejected_before_journal_publication() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut writable = Neo4jWritableStorage::open(&database).unwrap();
writable.begin_transaction().unwrap();
let node = writable
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), Value::Integer(1))]),
)
.unwrap();
writable.commit_transaction().unwrap();
writable.begin_transaction().unwrap();
writable
.set_node_property(node, "value".to_owned(), Value::Integer(2))
.unwrap();
writable.coordinator.state.lock().unwrap().version = u64::MAX;
assert!(matches!(
writable.commit_transaction(),
Err(Error::Transaction(
crate::TransactionErrorKind::StoragePoisoned
))
));
assert!(writable.poisoned);
assert!(writable.coordinator.state.lock().unwrap().poisoned);
assert!(!database.join(".littlegraph-write-journal").exists());
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert_eq!(
verifier.node(node).unwrap().properties["value"],
Value::Integer(1)
);
}
#[test]
fn late_disjoint_in_place_dynamic_chain_updates_rebase() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let first_node = setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), Value::String("a".repeat(40)))]),
)
.unwrap();
let second_node = setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), Value::String("b".repeat(40)))]),
)
.unwrap();
setup.commit_transaction().unwrap();
let mut stale = Neo4jWritableStorage::open(&database).unwrap();
let mut committer = Neo4jWritableStorage::open(&database).unwrap();
stale.begin_transaction().unwrap();
committer.begin_transaction().unwrap();
committer
.set_node_property(
first_node,
"value".to_owned(),
Value::String("c".repeat(40)),
)
.unwrap();
committer.commit_transaction().unwrap();
stale
.set_node_property(
second_node,
"value".to_owned(),
Value::String("d".repeat(40)),
)
.unwrap();
let write_set = &stale.transaction.as_ref().unwrap().write_set;
assert!(write_set.contains_shared(SharedDependency::DynamicChains));
assert!(!write_set.contains_shared(SharedDependency::IdAllocation));
assert!(write_set.rebase_dependencies().is_some());
stale.commit_transaction().unwrap();
assert_eq!(
stale.storage.node(first_node).unwrap().properties["value"],
Value::String("c".repeat(40))
);
assert_eq!(
stale.storage.node(second_node).unwrap().properties["value"],
Value::String("d".repeat(40))
);
}
#[test]
fn disjoint_dynamic_chain_resize_rebases_with_high_id_allocation() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let resized_node = setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), Value::String("a".repeat(40)))]),
)
.unwrap();
let other_node = setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), Value::String("b".repeat(40)))]),
)
.unwrap();
setup.commit_transaction().unwrap();
let mut stale = Neo4jWritableStorage::open(&database).unwrap();
let mut committer = Neo4jWritableStorage::open(&database).unwrap();
stale.begin_transaction().unwrap();
committer.begin_transaction().unwrap();
committer
.set_node_property(
other_node,
"value".to_owned(),
Value::String("c".repeat(40)),
)
.unwrap();
committer.commit_transaction().unwrap();
stale
.set_node_property(
resized_node,
"value".to_owned(),
Value::String("d".repeat(80)),
)
.unwrap();
let write_set = &stale.transaction.as_ref().unwrap().write_set;
assert!(write_set.contains_shared(SharedDependency::DynamicChains));
assert!(write_set.contains_shared(SharedDependency::IdAllocation));
assert!(write_set.rebase_dependencies().is_some());
stale.commit_transaction().unwrap();
assert_eq!(
stale.storage.node(other_node).unwrap().properties["value"],
Value::String("c".repeat(40))
);
assert_eq!(
stale.storage.node(resized_node).unwrap().properties["value"],
Value::String("d".repeat(80))
);
}
#[test]
fn stale_dynamic_string_resize_reuses_validated_free_ids() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let replacement = Value::String("c".repeat(200));
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let reclaimed_node = setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), Value::String("a".repeat(500)))]),
)
.unwrap();
let resized_node = setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), Value::String("b".repeat(40)))]),
)
.unwrap();
let unrelated_node = setup
.create_node(
Vec::new(),
Properties::from([("marker".to_owned(), Value::Integer(1))]),
)
.unwrap();
setup.commit_transaction().unwrap();
setup.begin_transaction().unwrap();
setup
.set_node_property(reclaimed_node, "value".to_owned(), Value::Integer(0))
.unwrap();
setup.commit_transaction().unwrap();
let mut stale = Neo4jWritableStorage::open(&database).unwrap();
let mut committer = Neo4jWritableStorage::open(&database).unwrap();
stale.begin_transaction().unwrap();
committer.begin_transaction().unwrap();
stale
.set_node_property(resized_node, "value".to_owned(), replacement.clone())
.unwrap();
let transaction = stale.transaction.as_ref().unwrap();
assert!(!transaction.reused_string_records.is_empty());
assert_eq!(transaction.allocated_string_records, 0);
assert!(transaction.freed_string_records.is_empty());
assert!(transaction
.write_set
.contains_shared(SharedDependency::DynamicChains));
assert!(transaction
.write_set
.contains_shared(SharedDependency::IdReuse));
assert!(!transaction
.write_set
.contains_shared(SharedDependency::IdAllocation));
committer
.set_node_property(unrelated_node, "marker".to_owned(), Value::Integer(2))
.unwrap();
committer.commit_transaction().unwrap();
stale.commit_transaction().unwrap();
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert_eq!(
verifier.node(reclaimed_node).unwrap().properties["value"],
Value::Integer(0)
);
assert_eq!(
verifier.node(resized_node).unwrap().properties["value"],
replacement
);
assert_eq!(
verifier.node(unrelated_node).unwrap().properties["marker"],
Value::Integer(2)
);
}
#[test]
fn stale_record_allocation_rebases_after_an_unrelated_record_update() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let existing = setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), Value::Integer(1))]),
)
.unwrap();
setup.commit_transaction().unwrap();
let mut stale = Neo4jWritableStorage::open(&database).unwrap();
let mut committer = Neo4jWritableStorage::open(&database).unwrap();
stale.begin_transaction().unwrap();
committer.begin_transaction().unwrap();
committer
.set_node_property(existing, "value".to_owned(), Value::Integer(2))
.unwrap();
committer.commit_transaction().unwrap();
let created = stale.create_node(Vec::new(), Properties::new()).unwrap();
let write_set = &stale.transaction.as_ref().unwrap().write_set;
assert!(write_set.contains_shared(SharedDependency::IdAllocation));
assert!(write_set.rebase_dependencies().is_some());
stale.commit_transaction().unwrap();
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert_eq!(verifier.query_nodes.len(), 2);
assert!(verifier.node(created).is_some());
assert_eq!(
verifier.node(existing).unwrap().properties["value"],
Value::Integer(2)
);
}
#[test]
fn allocation_candidates_do_not_consume_reusable_records() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut writable = Neo4jWritableStorage::open(&database).unwrap();
writable.begin_transaction().unwrap();
let start = writable.create_node(Vec::new(), Properties::new()).unwrap();
let end = writable.create_node(Vec::new(), Properties::new()).unwrap();
let relationship = writable
.create_relationship(start, end, "LINK".to_owned(), Properties::new())
.unwrap();
writable
.set_node_property(end, "old".to_owned(), Value::Integer(1))
.unwrap();
writable.commit_transaction().unwrap();
writable.begin_transaction().unwrap();
writable.delete_relationship(relationship).unwrap();
writable.delete_node(end).unwrap();
writable.commit_transaction().unwrap();
writable.begin_transaction().unwrap();
let (property_record_id, reused_property) =
writable.property_allocation_candidate().unwrap();
let (node_record_id, reused_node) = writable.node_allocation_candidate().unwrap();
let (relationship_record_id, reused_relationship) =
writable.relationship_allocation_candidate().unwrap();
assert!(reused_property);
assert!(reused_node);
assert!(reused_relationship);
let transaction = writable.transaction.as_ref().unwrap();
assert!(transaction.reused_property_records.is_empty());
assert!(transaction.reused_node_records.is_empty());
assert!(transaction.reused_relationship_records.is_empty());
writable
.set_node_property(start, "new".to_owned(), Value::Integer(2))
.unwrap();
let recreated_end = writable.create_node(Vec::new(), Properties::new()).unwrap();
writable
.create_relationship(start, recreated_end, "LINK".to_owned(), Properties::new())
.unwrap();
let transaction = writable.transaction.as_ref().unwrap();
assert_eq!(
transaction.reused_property_records,
[property_record_id.unwrap()]
);
assert_eq!(transaction.reused_node_records, [node_record_id]);
assert_eq!(
transaction.reused_relationship_records,
[relationship_record_id]
);
writable.rollback_transaction().unwrap();
drop(writable);
let group_id_path = database.join(RELATIONSHIP_GROUP_ID_STORE);
fs::write(&group_id_path, super::super::id_generator::empty_file(1)).unwrap();
mark_ids_free(&group_id_path, &[0]);
let mut writable = Neo4jWritableStorage::open(&database).unwrap();
writable.begin_transaction().unwrap();
assert_eq!(
writable.relationship_group_allocation_candidate().unwrap(),
(0, true)
);
assert!(writable
.transaction
.as_ref()
.unwrap()
.reused_relationship_group_records
.is_empty());
writable.rollback_transaction().unwrap();
}
#[test]
fn create_then_delete_keeps_new_and_reused_node_ids_reusable() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut writable = Neo4jWritableStorage::open(&database).unwrap();
writable.begin_transaction().unwrap();
let created = writable.create_node(Vec::new(), Properties::new()).unwrap();
writable.delete_node(created).unwrap();
writable.commit_transaction().unwrap();
let header = super::super::id_generator::validate_writable(
&database,
NODE_STORE,
writable.node_record_capacity,
)
.unwrap();
assert_eq!(header.high_id, 1);
assert_eq!(header.num_unused_ids, 1);
assert_eq!(
super::super::counts::locate_node_count(&database.join(COUNTS_STORE), -1)
.unwrap()
.count,
0
);
writable.begin_transaction().unwrap();
let reused = writable.create_node(Vec::new(), Properties::new()).unwrap();
assert_eq!(writable.storage.node(reused).unwrap().external_id, "0");
writable.delete_node(reused).unwrap();
writable.commit_transaction().unwrap();
let header = super::super::id_generator::validate_writable(
&database,
NODE_STORE,
writable.node_record_capacity,
)
.unwrap();
assert_eq!(header.high_id, 1);
assert_eq!(header.num_unused_ids, 1);
assert_eq!(
super::super::counts::locate_node_count(&database.join(COUNTS_STORE), -1)
.unwrap()
.count,
0
);
writable.begin_transaction().unwrap();
let reused = writable.create_node(Vec::new(), Properties::new()).unwrap();
assert_eq!(writable.storage.node(reused).unwrap().external_id, "0");
writable.commit_transaction().unwrap();
assert_eq!(writable.storage.query_nodes.len(), 1);
}
#[test]
fn dynamic_to_inline_update_reclaims_a_new_record_for_later_reuse() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut writable = Neo4jWritableStorage::open(&database).unwrap();
writable.begin_transaction().unwrap();
let node = writable.create_node(Vec::new(), Properties::new()).unwrap();
writable.commit_transaction().unwrap();
let initial_string_high_id = writable.string_id_header.high_id;
writable.begin_transaction().unwrap();
writable
.set_node_property(node, "value".to_owned(), Value::String("a".repeat(40)))
.unwrap();
writable
.set_node_property(node, "value".to_owned(), Value::Integer(7))
.unwrap();
let transaction = writable.transaction.as_ref().unwrap();
let dynamic_record_count = 40_u64.div_ceil(writable.string_layout.payload_size as u64);
let dynamic_record_ids = (initial_string_high_id
..initial_string_high_id + dynamic_record_count)
.collect::<Vec<_>>();
assert_eq!(transaction.allocated_string_records, dynamic_record_count);
assert_eq!(
transaction
.freed_string_records
.iter()
.copied()
.collect::<Vec<_>>(),
dynamic_record_ids
);
writable.commit_transaction().unwrap();
let header = super::super::id_generator::validate_writable(
&database,
STRING_STORE,
writable.string_layout.record_capacity,
)
.unwrap();
assert_eq!(
header.high_id,
initial_string_high_id + dynamic_record_count
);
assert_eq!(header.num_unused_ids, dynamic_record_count);
assert_eq!(
writable.storage.node(node).unwrap().properties["value"],
Value::Integer(7)
);
writable.begin_transaction().unwrap();
writable
.set_node_property(node, "value".to_owned(), Value::String("b".repeat(40)))
.unwrap();
let transaction = writable.transaction.as_ref().unwrap();
assert_eq!(transaction.allocated_string_records, 0);
assert_eq!(transaction.reused_string_records, dynamic_record_ids);
writable.commit_transaction().unwrap();
let header = super::super::id_generator::validate_writable(
&database,
STRING_STORE,
writable.string_layout.record_capacity,
)
.unwrap();
assert_eq!(
header.high_id,
initial_string_high_id + dynamic_record_count
);
assert_eq!(header.num_unused_ids, 0);
}
#[test]
fn removing_a_sole_dynamic_node_property_unlinks_and_reuses_its_records() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut writable = Neo4jWritableStorage::open(&database).unwrap();
writable.begin_transaction().unwrap();
let node = writable
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), Value::String("a".repeat(40)))]),
)
.unwrap();
writable.commit_transaction().unwrap();
let property_record_id = writable.storage.nodes.records[node]
.next_property
.expect("created property chain");
writable.begin_transaction().unwrap();
writable.remove_node_property(node, "value").unwrap();
let transaction = writable.transaction.as_ref().unwrap();
let freed_string_ids = transaction
.freed_string_records
.iter()
.copied()
.collect::<Vec<_>>();
assert_eq!(
transaction.freed_property_records,
BTreeSet::from([property_record_id])
);
assert!(!freed_string_ids.is_empty());
writable.commit_transaction().unwrap();
assert_eq!(writable.storage.nodes.records[node].next_property, None);
assert!(!writable
.storage
.node(node)
.unwrap()
.properties
.contains_key("value"));
let property_header = super::super::id_generator::validate_writable(
&database,
PROPERTY_STORE,
writable.property_record_capacity,
)
.unwrap();
assert_eq!(property_header.num_unused_ids, 1);
let string_header = super::super::id_generator::validate_writable(
&database,
STRING_STORE,
writable.string_layout.record_capacity,
)
.unwrap();
assert_eq!(string_header.num_unused_ids, freed_string_ids.len() as u64);
writable.begin_transaction().unwrap();
writable
.set_node_property(node, "value".to_owned(), Value::String("b".repeat(40)))
.unwrap();
let transaction = writable.transaction.as_ref().unwrap();
assert_eq!(transaction.reused_property_records, [property_record_id]);
assert_eq!(transaction.reused_string_records, freed_string_ids);
writable.commit_transaction().unwrap();
assert_eq!(
writable.storage.nodes.records[node].next_property,
Some(property_record_id)
);
}
#[test]
fn removing_a_sole_relationship_property_updates_its_owner_and_reuses_the_record() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut writable = Neo4jWritableStorage::open(&database).unwrap();
writable.begin_transaction().unwrap();
let start = writable.create_node(Vec::new(), Properties::new()).unwrap();
let end = writable.create_node(Vec::new(), Properties::new()).unwrap();
let relationship = writable
.create_relationship(
start,
end,
"LINK".to_owned(),
Properties::from([("value".to_owned(), Value::Integer(1))]),
)
.unwrap();
writable.commit_transaction().unwrap();
let property_record_id = writable.storage.relationships.records[relationship]
.next_property
.expect("created property chain");
writable.begin_transaction().unwrap();
writable
.remove_relationship_property(relationship, "value")
.unwrap();
assert_eq!(
writable
.transaction
.as_ref()
.unwrap()
.freed_property_records,
BTreeSet::from([property_record_id])
);
writable.commit_transaction().unwrap();
assert_eq!(
writable.storage.relationships.records[relationship].next_property,
None
);
assert!(!writable
.storage
.relationship(relationship)
.unwrap()
.properties
.contains_key("value"));
writable.begin_transaction().unwrap();
writable
.set_relationship_property(relationship, "value".to_owned(), Value::Integer(2))
.unwrap();
assert_eq!(
writable
.transaction
.as_ref()
.unwrap()
.reused_property_records,
[property_record_id]
);
writable.commit_transaction().unwrap();
assert_eq!(
writable.storage.relationships.records[relationship].next_property,
Some(property_record_id)
);
}
#[test]
fn create_then_delete_relationship_restores_heads_counts_and_id_reuse() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut writable = Neo4jWritableStorage::open(&database).unwrap();
writable.begin_transaction().unwrap();
let start = writable.create_node(Vec::new(), Properties::new()).unwrap();
let end = writable.create_node(Vec::new(), Properties::new()).unwrap();
writable.commit_transaction().unwrap();
let initial_relationship_high_id = writable.relationship_id_header.high_id;
let initial_property_high_id = writable.property_id_header.high_id;
let initial_string_high_id = writable.string_id_header.high_id;
let dynamic_record_count = 40_u64.div_ceil(writable.string_layout.payload_size as u64);
writable.begin_transaction().unwrap();
let relationship = writable
.create_relationship(
start,
end,
"LINK".to_owned(),
Properties::from([("payload".to_owned(), Value::String("a".repeat(40)))]),
)
.unwrap();
assert_eq!(
writable
.storage
.relationship(relationship)
.unwrap()
.external_id,
initial_relationship_high_id.to_string()
);
writable.delete_relationship(relationship).unwrap();
writable.commit_transaction().unwrap();
assert!(writable.storage.relationship(relationship).is_none());
assert!(writable.storage.outgoing_relationships(start).is_empty());
assert!(writable.storage.incoming_relationships(end).is_empty());
assert!(writable.storage.nodes.records[start]
.next_relationship
.is_none());
assert!(writable.storage.nodes.records[end]
.next_relationship
.is_none());
let header = super::super::id_generator::validate_writable(
&database,
RELATIONSHIP_STORE,
writable.relationship_record_capacity,
)
.unwrap();
assert_eq!(header.high_id, initial_relationship_high_id + 1);
assert_eq!(header.num_unused_ids, 1);
let property_header = super::super::id_generator::validate_writable(
&database,
PROPERTY_STORE,
writable.property_record_capacity,
)
.unwrap();
assert_eq!(property_header.high_id, initial_property_high_id + 1);
assert_eq!(property_header.num_unused_ids, 1);
let string_header = super::super::id_generator::validate_writable(
&database,
STRING_STORE,
writable.string_layout.record_capacity,
)
.unwrap();
assert_eq!(
string_header.high_id,
initial_string_high_id + dynamic_record_count
);
assert_eq!(string_header.num_unused_ids, dynamic_record_count);
let relationship_count_key = super::super::counts::RelationshipCountKey {
start_label_id: -1,
type_id: -1,
end_label_id: -1,
};
assert!(super::super::counts::locate_relationship_count(
&database.join(COUNTS_STORE),
relationship_count_key,
)
.is_err());
writable.begin_transaction().unwrap();
let relationship = writable
.create_relationship(
start,
end,
"LINK".to_owned(),
Properties::from([("payload".to_owned(), Value::String("b".repeat(40)))]),
)
.unwrap();
assert_eq!(
writable
.storage
.relationship(relationship)
.unwrap()
.external_id,
initial_relationship_high_id.to_string()
);
let transaction = writable.transaction.as_ref().unwrap();
assert_eq!(
transaction.reused_property_records,
[initial_property_high_id]
);
assert_eq!(
transaction.reused_string_records,
(initial_string_high_id..initial_string_high_id + dynamic_record_count)
.collect::<Vec<_>>()
);
writable.commit_transaction().unwrap();
let header = super::super::id_generator::validate_writable(
&database,
RELATIONSHIP_STORE,
writable.relationship_record_capacity,
)
.unwrap();
assert_eq!(header.high_id, initial_relationship_high_id + 1);
assert_eq!(header.num_unused_ids, 0);
let property_header = super::super::id_generator::validate_writable(
&database,
PROPERTY_STORE,
writable.property_record_capacity,
)
.unwrap();
assert_eq!(property_header.high_id, initial_property_high_id + 1);
assert_eq!(property_header.num_unused_ids, 0);
let string_header = super::super::id_generator::validate_writable(
&database,
STRING_STORE,
writable.string_layout.record_capacity,
)
.unwrap();
assert_eq!(
string_header.high_id,
initial_string_high_id + dynamic_record_count
);
assert_eq!(string_header.num_unused_ids, 0);
assert!(writable.storage.relationship(relationship).is_some());
assert_eq!(
writable.storage.outgoing_relationships(start),
[relationship]
);
assert_eq!(writable.storage.incoming_relationships(end), [relationship]);
let count = super::super::counts::locate_relationship_count(
&database.join(COUNTS_STORE),
relationship_count_key,
)
.unwrap();
assert_eq!(count.count, 1);
}
#[test]
fn stale_mixed_reused_and_high_id_property_allocations_rebase() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let reusable = setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), Value::Integer(0))]),
)
.unwrap();
setup.create_node(Vec::new(), Properties::new()).unwrap();
setup.create_node(Vec::new(), Properties::new()).unwrap();
setup
.create_node(
Vec::new(),
Properties::from([("marker".to_owned(), Value::Integer(1))]),
)
.unwrap();
setup.commit_transaction().unwrap();
setup.begin_transaction().unwrap();
setup.delete_node(reusable).unwrap();
setup.commit_transaction().unwrap();
let mut stale = Neo4jWritableStorage::open(&database).unwrap();
let mut committer = Neo4jWritableStorage::open(&database).unwrap();
stale.begin_transaction().unwrap();
committer.begin_transaction().unwrap();
let first_target = stale.storage.node_by_external_id("1").unwrap();
let second_target = stale.storage.node_by_external_id("2").unwrap();
stale
.set_node_property(first_target, "value".to_owned(), Value::Integer(10))
.unwrap();
stale
.set_node_property(second_target, "value".to_owned(), Value::Integer(20))
.unwrap();
let transaction = stale.transaction.as_ref().unwrap();
assert_eq!(transaction.reused_property_records, [0]);
assert_eq!(transaction.allocated_property_records, 1);
assert!(transaction
.write_set
.contains_shared(SharedDependency::IdReuse));
assert!(transaction
.write_set
.contains_shared(SharedDependency::IdAllocation));
let unrelated = committer.storage.node_by_external_id("3").unwrap();
committer
.set_node_property(unrelated, "marker".to_owned(), Value::Integer(2))
.unwrap();
let transaction = committer.transaction.as_ref().unwrap();
assert!(transaction.reused_property_records.is_empty());
assert_eq!(transaction.allocated_property_records, 0);
assert!(!transaction
.write_set
.contains_shared(SharedDependency::IdReuse));
committer.commit_transaction().unwrap();
stale.commit_transaction().unwrap();
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert!(verifier.node_by_external_id("0").is_none());
let first_target = verifier.node_by_external_id("1").unwrap();
let second_target = verifier.node_by_external_id("2").unwrap();
let unrelated = verifier.node_by_external_id("3").unwrap();
assert_eq!(
verifier.node(first_target).unwrap().properties["value"],
Value::Integer(10)
);
assert_eq!(
verifier.node(second_target).unwrap().properties["value"],
Value::Integer(20)
);
assert_eq!(
verifier.node(unrelated).unwrap().properties["marker"],
Value::Integer(2)
);
let header = super::super::id_generator::validate_writable(
&database,
PROPERTY_STORE,
stale.property_record_capacity,
)
.unwrap();
assert_eq!(header.high_id, 3);
assert_eq!(header.num_unused_ids, 0);
}
#[test]
fn stale_in_place_property_updates_do_not_consume_a_reusable_record() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let disposable = setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), Value::Integer(0))]),
)
.unwrap();
let source = setup.create_node(Vec::new(), Properties::new()).unwrap();
let target = setup.create_node(Vec::new(), Properties::new()).unwrap();
setup
.create_node(
Vec::new(),
Properties::from([("marker".to_owned(), Value::Integer(1))]),
)
.unwrap();
setup
.create_relationship(
source,
target,
"LINK".to_owned(),
Properties::from([("score".to_owned(), Value::Integer(1))]),
)
.unwrap();
setup.commit_transaction().unwrap();
setup.begin_transaction().unwrap();
setup.delete_node(disposable).unwrap();
setup.commit_transaction().unwrap();
let mut stale = Neo4jWritableStorage::open(&database).unwrap();
let mut committer = Neo4jWritableStorage::open(&database).unwrap();
stale.begin_transaction().unwrap();
committer.begin_transaction().unwrap();
let relationship = stale.storage.relationship_by_external_id("0").unwrap();
stale
.set_relationship_property(relationship, "score".to_owned(), Value::Integer(2))
.unwrap();
let node = committer.storage.node_by_external_id("3").unwrap();
committer
.set_node_property(node, "marker".to_owned(), Value::Integer(2))
.unwrap();
for transaction in [
stale.transaction.as_ref().unwrap(),
committer.transaction.as_ref().unwrap(),
] {
assert!(transaction.reused_property_records.is_empty());
assert_eq!(transaction.allocated_property_records, 0);
assert!(!transaction
.write_set
.contains_shared(SharedDependency::IdReuse));
assert!(!transaction
.write_set
.contains_shared(SharedDependency::IdAllocation));
}
committer.commit_transaction().unwrap();
stale.commit_transaction().unwrap();
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
let relationship = verifier.relationship_by_external_id("0").unwrap();
assert_eq!(
verifier.relationship(relationship).unwrap().properties["score"],
Value::Integer(2)
);
let node = verifier.node_by_external_id("3").unwrap();
assert_eq!(
verifier.node(node).unwrap().properties["marker"],
Value::Integer(2)
);
let header = super::super::id_generator::validate_writable(
&database,
PROPERTY_STORE,
stale.property_record_capacity,
)
.unwrap();
assert_eq!(header.high_id, 3);
assert_eq!(header.num_unused_ids, 1);
}
#[test]
fn concurrent_allocations_of_the_same_physical_record_conflict() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut first = Neo4jWritableStorage::open(&database).unwrap();
let mut second = Neo4jWritableStorage::open(&database).unwrap();
first.begin_transaction().unwrap();
second.begin_transaction().unwrap();
first.create_node(Vec::new(), Properties::new()).unwrap();
second.create_node(Vec::new(), Properties::new()).unwrap();
for writable in [&first, &second] {
let write_set = &writable.transaction.as_ref().unwrap().write_set;
assert!(write_set.contains_fixed(FixedStore::Node, 0));
assert!(write_set.contains_shared(SharedDependency::IdAllocation));
assert!(write_set.rebase_dependencies().is_some());
}
first.commit_transaction().unwrap();
assert!(matches!(
second.commit_transaction(),
Err(Error::Transaction(crate::TransactionErrorKind::Conflict))
));
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert_eq!(verifier.query_nodes.len(), 1);
let header = super::super::id_generator::validate_writable(
&database,
NODE_STORE,
second.node_record_capacity,
)
.unwrap();
assert_eq!(header.high_id, 1);
}
#[test]
fn stale_id_reuse_rebases_when_the_free_list_selection_is_unchanged() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let reusable = setup.create_node(Vec::new(), Properties::new()).unwrap();
setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), Value::Integer(1))]),
)
.unwrap();
setup.commit_transaction().unwrap();
setup.begin_transaction().unwrap();
setup.delete_node(reusable).unwrap();
setup.commit_transaction().unwrap();
let mut stale = Neo4jWritableStorage::open(&database).unwrap();
let mut committer = Neo4jWritableStorage::open(&database).unwrap();
stale.begin_transaction().unwrap();
committer.begin_transaction().unwrap();
stale.create_node(Vec::new(), Properties::new()).unwrap();
let survivor = committer.storage.node_by_external_id("1").unwrap();
committer
.set_node_property(survivor, "value".to_owned(), Value::Integer(2))
.unwrap();
let write_set = &stale.transaction.as_ref().unwrap().write_set;
assert!(write_set.contains_shared(SharedDependency::IdReuse));
assert!(!write_set.contains_shared(SharedDependency::Tokens));
assert!(write_set.rebase_dependencies().is_some());
committer.commit_transaction().unwrap();
stale.commit_transaction().unwrap();
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert_eq!(verifier.query_nodes.len(), 2);
assert!(verifier.node_by_external_id("0").is_some());
assert_eq!(
verifier
.node(verifier.node_by_external_id("1").unwrap())
.unwrap()
.properties["value"],
Value::Integer(2)
);
let header = super::super::id_generator::validate_writable(
&database,
NODE_STORE,
stale.node_record_capacity,
)
.unwrap();
assert_eq!(header.high_id, 2);
assert_eq!(header.num_unused_ids, 0);
}
#[test]
fn stale_mixed_reused_and_high_id_node_allocations_rebase() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let reusable = setup.create_node(Vec::new(), Properties::new()).unwrap();
setup
.create_node(
Vec::new(),
Properties::from([("marker".to_owned(), Value::Integer(1))]),
)
.unwrap();
setup.commit_transaction().unwrap();
setup.begin_transaction().unwrap();
setup.delete_node(reusable).unwrap();
setup.commit_transaction().unwrap();
let mut stale = Neo4jWritableStorage::open(&database).unwrap();
let mut committer = Neo4jWritableStorage::open(&database).unwrap();
stale.begin_transaction().unwrap();
committer.begin_transaction().unwrap();
stale.create_node(Vec::new(), Properties::new()).unwrap();
stale.create_node(Vec::new(), Properties::new()).unwrap();
let transaction = stale.transaction.as_ref().unwrap();
assert_eq!(transaction.reused_node_records, [0]);
assert_eq!(transaction.allocated_node_records, 1);
assert!(transaction
.write_set
.contains_shared(SharedDependency::IdReuse));
assert!(transaction
.write_set
.contains_shared(SharedDependency::IdAllocation));
assert!(transaction
.write_set
.contains_shared(SharedDependency::Counts));
let current_survivor = committer.storage.node_by_external_id("1").unwrap();
committer
.set_node_property(current_survivor, "marker".to_owned(), Value::Integer(2))
.unwrap();
committer.commit_transaction().unwrap();
stale.commit_transaction().unwrap();
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert_eq!(verifier.query_nodes.len(), 3);
for external_id in ["0", "1", "2"] {
assert!(verifier.node_by_external_id(external_id).is_some());
}
assert_eq!(
verifier
.node(verifier.node_by_external_id("1").unwrap())
.unwrap()
.properties["marker"],
Value::Integer(2)
);
let header = super::super::id_generator::validate_writable(
&database,
NODE_STORE,
stale.node_record_capacity,
)
.unwrap();
assert_eq!(header.high_id, 3);
assert_eq!(header.num_unused_ids, 0);
assert_eq!(
super::super::counts::locate_node_count(&database.join(COUNTS_STORE), -1)
.unwrap()
.count,
3
);
}
#[test]
fn stale_id_reuse_conflicts_when_a_lower_free_id_changes_selection() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
setup.create_node(Vec::new(), Properties::new()).unwrap();
let initially_freed = setup.create_node(Vec::new(), Properties::new()).unwrap();
setup.create_node(Vec::new(), Properties::new()).unwrap();
setup.commit_transaction().unwrap();
setup.begin_transaction().unwrap();
setup.delete_node(initially_freed).unwrap();
setup.commit_transaction().unwrap();
let mut stale = Neo4jWritableStorage::open(&database).unwrap();
let mut reclaimer = Neo4jWritableStorage::open(&database).unwrap();
stale.begin_transaction().unwrap();
reclaimer.begin_transaction().unwrap();
stale.create_node(Vec::new(), Properties::new()).unwrap();
let lower_id = reclaimer.storage.node_by_external_id("0").unwrap();
reclaimer.delete_node(lower_id).unwrap();
reclaimer.commit_transaction().unwrap();
let transaction = stale.transaction.as_ref().unwrap();
assert_eq!(transaction.reused_node_records, [1]);
assert!(transaction
.write_set
.contains_shared(SharedDependency::IdReuse));
assert!(matches!(
stale.commit_transaction(),
Err(Error::Transaction(crate::TransactionErrorKind::Conflict))
));
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert!(verifier.node_by_external_id("0").is_none());
assert!(verifier.node_by_external_id("1").is_none());
assert!(verifier.node_by_external_id("2").is_some());
let header = super::super::id_generator::validate_writable(
&database,
NODE_STORE,
reclaimer.node_record_capacity,
)
.unwrap();
assert_eq!(header.num_unused_ids, 2);
}
#[test]
fn changed_reuse_prefix_falls_back_to_high_id_and_retry_reselects() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let nodes = (0..4)
.map(|_| setup.create_node(Vec::new(), Properties::new()).unwrap())
.collect::<Vec<_>>();
setup.commit_transaction().unwrap();
setup.begin_transaction().unwrap();
for node in &nodes[1..] {
setup.delete_node(*node).unwrap();
}
setup.commit_transaction().unwrap();
let mut stale = Neo4jWritableStorage::open(&database).unwrap();
let mut committer = Neo4jWritableStorage::open(&database).unwrap();
stale.begin_transaction().unwrap();
committer.begin_transaction().unwrap();
stale.create_node(Vec::new(), Properties::new()).unwrap();
committer
.create_node(Vec::new(), Properties::new())
.unwrap();
committer.commit_transaction().unwrap();
stale.create_node(Vec::new(), Properties::new()).unwrap();
let transaction = stale.transaction.as_ref().unwrap();
assert_eq!(transaction.reused_node_records, [1]);
assert_eq!(transaction.allocated_node_records, 1);
assert!(transaction.write_set.contains_fixed(FixedStore::Node, 4));
assert!(transaction
.write_set
.contains_shared(SharedDependency::IdReuse));
assert!(transaction
.write_set
.contains_shared(SharedDependency::IdAllocation));
assert!(matches!(
stale.commit_transaction(),
Err(Error::Transaction(crate::TransactionErrorKind::Conflict))
));
stale.begin_transaction().unwrap();
stale.create_node(Vec::new(), Properties::new()).unwrap();
stale.create_node(Vec::new(), Properties::new()).unwrap();
let transaction = stale.transaction.as_ref().unwrap();
assert_eq!(transaction.reused_node_records, [2, 3]);
assert_eq!(transaction.allocated_node_records, 0);
stale.commit_transaction().unwrap();
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert_eq!(verifier.query_nodes.len(), 4);
for external_id in ["0", "1", "2", "3"] {
assert!(verifier.node_by_external_id(external_id).is_some());
}
assert!(verifier.node_by_external_id("4").is_none());
let header = super::super::id_generator::validate_writable(
&database,
NODE_STORE,
stale.node_record_capacity,
)
.unwrap();
assert_eq!(header.high_id, 4);
assert_eq!(header.num_unused_ids, 0);
}
#[test]
fn stale_token_allocation_rebases_after_an_unrelated_record_update() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let first = setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), Value::Integer(1))]),
)
.unwrap();
let second = setup.create_node(Vec::new(), Properties::new()).unwrap();
setup.commit_transaction().unwrap();
let mut stale = Neo4jWritableStorage::open(&database).unwrap();
let mut committer = Neo4jWritableStorage::open(&database).unwrap();
stale.begin_transaction().unwrap();
committer.begin_transaction().unwrap();
committer
.set_node_property(first, "value".to_owned(), Value::Integer(2))
.unwrap();
committer.commit_transaction().unwrap();
stale.set_node_label(second, "Fresh".to_owned()).unwrap();
let write_set = &stale.transaction.as_ref().unwrap().write_set;
assert!(write_set.contains_shared(SharedDependency::Tokens));
assert!(write_set.contains_shared(SharedDependency::IdAllocation));
assert!(write_set.rebase_dependencies().is_some());
stale.commit_transaction().unwrap();
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert!(verifier.has_label("Fresh"));
assert_eq!(
verifier.node(second).unwrap().labels,
vec!["Fresh".to_owned()]
);
assert_eq!(
verifier.node(first).unwrap().properties["value"],
Value::Integer(2)
);
}
#[test]
fn stale_token_reuse_rebases_when_the_free_list_selection_is_unchanged() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let labeled = setup.create_node(Vec::new(), Properties::new()).unwrap();
let unrelated = setup
.create_node(
Vec::new(),
Properties::from([("marker".to_owned(), Value::Integer(1))]),
)
.unwrap();
setup.commit_transaction().unwrap();
drop(setup);
let label_id_path = database.join(LABEL_TOKEN_ID_STORE);
fs::write(&label_id_path, super::super::id_generator::empty_file(2)).unwrap();
mark_ids_free(&label_id_path, &[0, 1]);
let mut stale = Neo4jWritableStorage::open(&database).unwrap();
let mut committer = Neo4jWritableStorage::open(&database).unwrap();
stale.begin_transaction().unwrap();
committer.begin_transaction().unwrap();
stale.set_node_label(labeled, "Fresh".to_owned()).unwrap();
stale.set_node_label(labeled, "Active".to_owned()).unwrap();
let transaction = stale.transaction.as_ref().unwrap();
assert_eq!(transaction.reused_label_tokens, [0, 1]);
assert_eq!(transaction.allocated_label_tokens, 0);
assert!(transaction
.write_set
.contains_shared(SharedDependency::IdReuse));
assert!(transaction
.write_set
.contains_shared(SharedDependency::Tokens));
committer
.set_node_property(unrelated, "marker".to_owned(), Value::Integer(2))
.unwrap();
committer.commit_transaction().unwrap();
stale.commit_transaction().unwrap();
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert_eq!(verifier.node(labeled).unwrap().labels, ["Fresh", "Active"]);
assert_eq!(
verifier.node(unrelated).unwrap().properties["marker"],
Value::Integer(2)
);
let header = super::super::id_generator::validate_writable(
&database,
LABEL_TOKEN_STORE,
stale.label_token_capacity,
)
.unwrap();
assert_eq!(header.high_id, 2);
assert_eq!(header.num_unused_ids, 0);
}
#[test]
fn stale_mixed_reused_and_high_id_token_allocations_rebase() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let node = setup.create_node(Vec::new(), Properties::new()).unwrap();
let unrelated = setup
.create_node(
Vec::new(),
Properties::from([("marker".to_owned(), Value::Integer(1))]),
)
.unwrap();
setup.commit_transaction().unwrap();
drop(setup);
let label_id_path = database.join(LABEL_TOKEN_ID_STORE);
fs::write(&label_id_path, super::super::id_generator::empty_file(1)).unwrap();
mark_ids_free(&label_id_path, &[0]);
let mut stale = Neo4jWritableStorage::open(&database).unwrap();
let mut committer = Neo4jWritableStorage::open(&database).unwrap();
stale.begin_transaction().unwrap();
committer.begin_transaction().unwrap();
stale.set_node_label(node, "Fresh".to_owned()).unwrap();
stale.set_node_label(node, "Active".to_owned()).unwrap();
let transaction = stale.transaction.as_ref().unwrap();
assert_eq!(transaction.reused_label_tokens, [0]);
assert_eq!(transaction.allocated_label_tokens, 1);
assert!(transaction
.write_set
.contains_shared(SharedDependency::IdReuse));
assert!(transaction
.write_set
.contains_shared(SharedDependency::IdAllocation));
assert!(transaction
.write_set
.contains_shared(SharedDependency::Tokens));
committer
.set_node_property(unrelated, "marker".to_owned(), Value::Integer(2))
.unwrap();
committer.commit_transaction().unwrap();
stale.commit_transaction().unwrap();
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert_eq!(verifier.node(node).unwrap().labels, ["Fresh", "Active"]);
assert_eq!(
verifier.node(unrelated).unwrap().properties["marker"],
Value::Integer(2)
);
assert_eq!(verifier.nodes_with_label("Fresh"), [node]);
assert_eq!(verifier.nodes_with_label("Active"), [node]);
let header = super::super::id_generator::validate_writable(
&database,
LABEL_TOKEN_STORE,
stale.label_token_capacity,
)
.unwrap();
assert_eq!(header.high_id, 2);
assert_eq!(header.num_unused_ids, 0);
}
#[test]
fn allocations_in_disjoint_token_stores_rebase() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let nodes = (0..4)
.map(|_| setup.create_node(Vec::new(), Properties::new()).unwrap())
.collect::<Vec<_>>();
setup.commit_transaction().unwrap();
let mut property_key = Neo4jWritableStorage::open(&database).unwrap();
let mut label = Neo4jWritableStorage::open(&database).unwrap();
let mut relationship_type = Neo4jWritableStorage::open(&database).unwrap();
property_key.begin_transaction().unwrap();
label.begin_transaction().unwrap();
relationship_type.begin_transaction().unwrap();
property_key
.set_node_property(nodes[0], "score".to_owned(), Value::Integer(7))
.unwrap();
label.set_node_label(nodes[1], "Active".to_owned()).unwrap();
let relationship = relationship_type
.create_relationship(nodes[2], nodes[3], "FOLLOWS".to_owned(), Properties::new())
.unwrap();
let property_write_set = &property_key.transaction.as_ref().unwrap().write_set;
assert!(property_write_set.contains_fixed(FixedStore::PropertyKeyToken, 0));
assert!(property_write_set.contains_fixed(FixedStore::PropertyKeyName, 1));
let label_write_set = &label.transaction.as_ref().unwrap().write_set;
assert!(label_write_set.contains_fixed(FixedStore::LabelToken, 0));
assert!(label_write_set.contains_fixed(FixedStore::LabelName, 1));
let relationship_type_write_set =
&relationship_type.transaction.as_ref().unwrap().write_set;
assert!(relationship_type_write_set.contains_fixed(FixedStore::RelationshipTypeToken, 0));
assert!(relationship_type_write_set.contains_fixed(FixedStore::RelationshipTypeName, 1));
for writable in [&property_key, &label, &relationship_type] {
let write_set = &writable.transaction.as_ref().unwrap().write_set;
assert!(write_set.contains_shared(SharedDependency::Tokens));
assert!(write_set.contains_shared(SharedDependency::IdAllocation));
assert!(write_set.rebase_dependencies().is_some());
}
property_key.commit_transaction().unwrap();
label.commit_transaction().unwrap();
relationship_type.commit_transaction().unwrap();
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert_eq!(
verifier.node(nodes[0]).unwrap().properties["score"],
Value::Integer(7)
);
assert_eq!(
verifier.node(nodes[1]).unwrap().labels,
vec!["Active".to_owned()]
);
assert_eq!(
verifier.relationship(relationship).unwrap().r#type,
"FOLLOWS"
);
assert!(verifier
.property_key_tokens()
.values()
.any(|name| name == "score"));
assert!(verifier
.label_tokens()
.values()
.any(|name| name == "Active"));
assert!(verifier
.relationship_type_tokens()
.values()
.any(|name| name == "FOLLOWS"));
}
#[test]
fn concurrent_allocations_of_the_same_token_conflict() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let first_node = setup.create_node(Vec::new(), Properties::new()).unwrap();
let second_node = setup.create_node(Vec::new(), Properties::new()).unwrap();
setup.commit_transaction().unwrap();
let mut first = Neo4jWritableStorage::open(&database).unwrap();
let mut second = Neo4jWritableStorage::open(&database).unwrap();
first.begin_transaction().unwrap();
second.begin_transaction().unwrap();
first
.set_node_label(first_node, "Fresh".to_owned())
.unwrap();
second
.set_node_label(second_node, "Fresh".to_owned())
.unwrap();
for writable in [&first, &second] {
let write_set = &writable.transaction.as_ref().unwrap().write_set;
assert!(write_set.contains_fixed(FixedStore::LabelToken, 0));
assert!(write_set.contains_shared(SharedDependency::Tokens));
assert!(write_set.rebase_dependencies().is_some());
}
first.commit_transaction().unwrap();
assert!(matches!(
second.commit_transaction(),
Err(Error::Transaction(crate::TransactionErrorKind::Conflict))
));
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert_eq!(verifier.nodes_with_label("Fresh").len(), 1);
let label_id = verifier
.label_tokens()
.iter()
.find_map(|(id, label)| (label == "Fresh").then_some(*id))
.unwrap();
assert_eq!(
super::super::counts::locate_node_count(
&database.join(COUNTS_STORE),
i32::try_from(label_id).unwrap()
)
.unwrap()
.count,
1
);
}
#[test]
fn stale_dynamic_chain_reclamation_rebases_onto_current_id_tree() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let first = setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), Value::String("a".repeat(40)))]),
)
.unwrap();
let reclaimed = setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), Value::String("b".repeat(80)))]),
)
.unwrap();
setup.commit_transaction().unwrap();
let mut stale = Neo4jWritableStorage::open(&database).unwrap();
let mut committer = Neo4jWritableStorage::open(&database).unwrap();
stale.begin_transaction().unwrap();
committer.begin_transaction().unwrap();
committer
.set_node_property(first, "value".to_owned(), Value::String("c".repeat(40)))
.unwrap();
committer.commit_transaction().unwrap();
stale
.set_node_property(reclaimed, "value".to_owned(), Value::Integer(7))
.unwrap();
let transaction = stale.transaction.as_ref().unwrap();
assert!(!transaction.freed_string_records.is_empty());
let write_set = &transaction.write_set;
assert!(write_set.contains_shared(SharedDependency::IdReclamation));
assert!(!write_set.contains_shared(SharedDependency::IdAllocation));
assert!(write_set.rebase_dependencies().is_some());
stale.commit_transaction().unwrap();
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert_eq!(
verifier.node(first).unwrap().properties["value"],
Value::String("c".repeat(40))
);
assert_eq!(
verifier.node(reclaimed).unwrap().properties["value"],
Value::Integer(7)
);
}
#[test]
fn stale_dynamic_property_removal_rebases_owner_and_id_reclamation() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let removed = setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), Value::String("a".repeat(40)))]),
)
.unwrap();
let unrelated = setup
.create_node(
Vec::new(),
Properties::from([("marker".to_owned(), Value::Integer(1))]),
)
.unwrap();
setup.commit_transaction().unwrap();
let mut stale = Neo4jWritableStorage::open(&database).unwrap();
let mut committer = Neo4jWritableStorage::open(&database).unwrap();
stale.begin_transaction().unwrap();
committer.begin_transaction().unwrap();
stale.remove_node_property(removed, "value").unwrap();
let transaction = stale.transaction.as_ref().unwrap();
assert_eq!(transaction.freed_property_records.len(), 1);
assert!(!transaction.freed_string_records.is_empty());
assert!(transaction
.write_set
.contains_shared(SharedDependency::IdReclamation));
assert!(transaction.write_set.rebase_dependencies().is_some());
committer
.set_node_property(unrelated, "marker".to_owned(), Value::Integer(2))
.unwrap();
committer.commit_transaction().unwrap();
stale.commit_transaction().unwrap();
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert!(!verifier
.node(removed)
.unwrap()
.properties
.contains_key("value"));
assert_eq!(verifier.nodes.records[removed].next_property, None);
assert_eq!(
verifier.node(unrelated).unwrap().properties["marker"],
Value::Integer(2)
);
}
#[test]
fn concurrent_disjoint_node_deletions_merge_id_reclamation() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let first_node = setup.create_node(Vec::new(), Properties::new()).unwrap();
let second_node = setup.create_node(Vec::new(), Properties::new()).unwrap();
setup.commit_transaction().unwrap();
let mut first = Neo4jWritableStorage::open(&database).unwrap();
let mut second = Neo4jWritableStorage::open(&database).unwrap();
first.begin_transaction().unwrap();
second.begin_transaction().unwrap();
first.delete_node(first_node).unwrap();
second.delete_node(second_node).unwrap();
for writable in [&first, &second] {
let write_set = &writable.transaction.as_ref().unwrap().write_set;
assert!(write_set.contains_shared(SharedDependency::IdReclamation));
assert!(!write_set.contains_shared(SharedDependency::IdAllocation));
assert!(write_set.contains_shared(SharedDependency::Counts));
assert!(write_set.rebase_dependencies().is_some());
}
first.commit_transaction().unwrap();
second.commit_transaction().unwrap();
assert!(second.storage.query_nodes.is_empty());
let header = super::super::id_generator::validate_writable(
&database,
NODE_STORE,
second.node_record_capacity,
)
.unwrap();
assert_eq!(header.num_unused_ids, 2);
}
#[test]
fn stale_node_reclamation_merges_after_a_high_id_allocation() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let deleted = setup.create_node(Vec::new(), Properties::new()).unwrap();
setup.commit_transaction().unwrap();
let mut stale = Neo4jWritableStorage::open(&database).unwrap();
let mut allocator = Neo4jWritableStorage::open(&database).unwrap();
stale.begin_transaction().unwrap();
allocator.begin_transaction().unwrap();
let created = allocator
.create_node(Vec::new(), Properties::new())
.unwrap();
allocator.commit_transaction().unwrap();
stale.delete_node(deleted).unwrap();
let write_set = &stale.transaction.as_ref().unwrap().write_set;
assert!(write_set.contains_shared(SharedDependency::IdReclamation));
assert!(!write_set.contains_shared(SharedDependency::IdAllocation));
assert!(write_set.rebase_dependencies().is_some());
stale.commit_transaction().unwrap();
assert_eq!(allocator.storage.node(created).unwrap().external_id, "1");
assert!(stale.storage.node_by_external_id("0").is_none());
assert!(stale.storage.node_by_external_id("1").is_some());
assert_eq!(stale.storage.query_nodes.len(), 1);
let header = super::super::id_generator::validate_writable(
&database,
NODE_STORE,
stale.node_record_capacity,
)
.unwrap();
assert_eq!(header.high_id, 2);
assert_eq!(header.num_unused_ids, 1);
assert_eq!(
super::super::counts::locate_node_count(&database.join(COUNTS_STORE), -1)
.unwrap()
.count,
1
);
}
#[test]
fn stale_high_id_allocation_merges_after_reclamation() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let deleted = setup.create_node(Vec::new(), Properties::new()).unwrap();
setup.commit_transaction().unwrap();
let mut allocator = Neo4jWritableStorage::open(&database).unwrap();
let mut reclaimer = Neo4jWritableStorage::open(&database).unwrap();
allocator.begin_transaction().unwrap();
reclaimer.begin_transaction().unwrap();
allocator
.create_node(Vec::new(), Properties::new())
.unwrap();
reclaimer.delete_node(deleted).unwrap();
reclaimer.commit_transaction().unwrap();
assert!(allocator
.transaction
.as_ref()
.unwrap()
.write_set
.contains_shared(SharedDependency::IdAllocation));
allocator.commit_transaction().unwrap();
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert_eq!(verifier.query_nodes.len(), 1);
assert!(verifier.node_by_external_id("1").is_some());
let header = super::super::id_generator::validate_writable(
&database,
NODE_STORE,
reclaimer.node_record_capacity,
)
.unwrap();
assert_eq!(header.high_id, 2);
assert_eq!(header.num_unused_ids, 1);
}
#[test]
fn concurrent_disjoint_relationship_deletions_merge_counts_and_id_reclamation() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let nodes = (0..4)
.map(|_| setup.create_node(Vec::new(), Properties::new()).unwrap())
.collect::<Vec<_>>();
let first_relationship = setup
.create_relationship(nodes[0], nodes[1], "LINK".to_owned(), Properties::new())
.unwrap();
let second_relationship = setup
.create_relationship(nodes[2], nodes[3], "LINK".to_owned(), Properties::new())
.unwrap();
setup.commit_transaction().unwrap();
let mut first = Neo4jWritableStorage::open(&database).unwrap();
let mut second = Neo4jWritableStorage::open(&database).unwrap();
first.begin_transaction().unwrap();
second.begin_transaction().unwrap();
first.delete_relationship(first_relationship).unwrap();
second.delete_relationship(second_relationship).unwrap();
for writable in [&first, &second] {
let write_set = &writable.transaction.as_ref().unwrap().write_set;
assert!(write_set.contains_shared(SharedDependency::IdReclamation));
assert!(!write_set.contains_shared(SharedDependency::IdAllocation));
assert!(write_set.contains_shared(SharedDependency::Counts));
assert!(write_set.rebase_dependencies().is_some());
}
first.commit_transaction().unwrap();
second.commit_transaction().unwrap();
assert!(second.storage.query_relationships.is_empty());
assert_eq!(second.storage.query_nodes.len(), 4);
let header = super::super::id_generator::validate_writable(
&database,
RELATIONSHIP_STORE,
second.relationship_record_capacity,
)
.unwrap();
assert_eq!(header.num_unused_ids, 2);
let count = super::super::counts::locate_relationship_count(
&database.join(COUNTS_STORE),
super::super::counts::RelationshipCountKey {
start_label_id: -1,
type_id: -1,
end_label_id: -1,
},
)
.unwrap();
assert_eq!(count.count, 0);
}
#[test]
fn stale_mixed_reused_and_high_id_relationship_allocations_rebase() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let source = setup.create_node(Vec::new(), Properties::new()).unwrap();
let target = setup.create_node(Vec::new(), Properties::new()).unwrap();
let unrelated = setup
.create_node(
Vec::new(),
Properties::from([("marker".to_owned(), Value::Integer(1))]),
)
.unwrap();
let reusable = setup
.create_relationship(source, target, "LINK".to_owned(), Properties::new())
.unwrap();
setup.commit_transaction().unwrap();
setup.begin_transaction().unwrap();
setup.delete_relationship(reusable).unwrap();
setup.commit_transaction().unwrap();
let mut stale = Neo4jWritableStorage::open(&database).unwrap();
let mut committer = Neo4jWritableStorage::open(&database).unwrap();
stale.begin_transaction().unwrap();
committer.begin_transaction().unwrap();
stale
.create_relationship(source, target, "LINK".to_owned(), Properties::new())
.unwrap();
stale
.create_relationship(source, target, "LINK".to_owned(), Properties::new())
.unwrap();
let transaction = stale.transaction.as_ref().unwrap();
assert_eq!(transaction.reused_relationship_records, [0]);
assert_eq!(transaction.allocated_relationship_records, 1);
assert!(transaction
.write_set
.contains_shared(SharedDependency::IdReuse));
assert!(transaction
.write_set
.contains_shared(SharedDependency::IdAllocation));
assert!(transaction
.write_set
.contains_shared(SharedDependency::Counts));
committer
.set_node_property(unrelated, "marker".to_owned(), Value::Integer(2))
.unwrap();
committer.commit_transaction().unwrap();
stale.commit_transaction().unwrap();
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert_eq!(verifier.query_relationships.len(), 2);
assert!(verifier.relationship_by_external_id("0").is_some());
assert!(verifier.relationship_by_external_id("1").is_some());
assert_eq!(
verifier.node(unrelated).unwrap().properties["marker"],
Value::Integer(2)
);
let header = super::super::id_generator::validate_writable(
&database,
RELATIONSHIP_STORE,
stale.relationship_record_capacity,
)
.unwrap();
assert_eq!(header.high_id, 2);
assert_eq!(header.num_unused_ids, 0);
let count = super::super::counts::locate_relationship_count(
&database.join(COUNTS_STORE),
super::super::counts::RelationshipCountKey {
start_label_id: -1,
type_id: -1,
end_label_id: -1,
},
)
.unwrap();
assert_eq!(count.count, 2);
}
#[test]
fn disjoint_in_place_dynamic_array_updates_rebase() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let initial = Value::List(vec![Value::Integer(2); 20]);
let replacement = Value::List(vec![Value::Integer(3); 20]);
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let first_node = setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), initial.clone())]),
)
.unwrap();
let second_node = setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), initial)]),
)
.unwrap();
setup.commit_transaction().unwrap();
let mut first = Neo4jWritableStorage::open(&database).unwrap();
let mut second = Neo4jWritableStorage::open(&database).unwrap();
first.begin_transaction().unwrap();
second.begin_transaction().unwrap();
first
.set_node_property(first_node, "value".to_owned(), replacement.clone())
.unwrap();
second
.set_node_property(second_node, "value".to_owned(), replacement.clone())
.unwrap();
assert!(!second
.transaction
.as_ref()
.unwrap()
.write_set
.contains_shared(SharedDependency::IdAllocation));
first.commit_transaction().unwrap();
second.commit_transaction().unwrap();
assert_eq!(
second.storage.node(first_node).unwrap().properties["value"],
replacement
);
assert_eq!(
second.storage.node(second_node).unwrap().properties["value"],
Value::List(vec![Value::Integer(3); 20])
);
}
#[test]
fn stale_dynamic_array_resize_reuses_validated_free_ids() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let reclaimed_value = Value::List((0..500).map(Value::Integer).collect());
let initial_value = Value::List((0..20).map(Value::Integer).collect());
let replacement = Value::List((1000..1200).map(Value::Integer).collect());
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let reclaimed_node = setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), reclaimed_value)]),
)
.unwrap();
let resized_node = setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), initial_value)]),
)
.unwrap();
let unrelated_node = setup
.create_node(
Vec::new(),
Properties::from([("marker".to_owned(), Value::Integer(1))]),
)
.unwrap();
setup.commit_transaction().unwrap();
setup.begin_transaction().unwrap();
setup
.set_node_property(reclaimed_node, "value".to_owned(), Value::Integer(0))
.unwrap();
setup.commit_transaction().unwrap();
let mut stale = Neo4jWritableStorage::open(&database).unwrap();
let mut committer = Neo4jWritableStorage::open(&database).unwrap();
stale.begin_transaction().unwrap();
committer.begin_transaction().unwrap();
stale
.set_node_property(resized_node, "value".to_owned(), replacement.clone())
.unwrap();
let transaction = stale.transaction.as_ref().unwrap();
assert!(!transaction.reused_array_records.is_empty());
assert_eq!(transaction.allocated_array_records, 0);
assert!(transaction.freed_array_records.is_empty());
assert!(transaction
.write_set
.contains_shared(SharedDependency::DynamicChains));
assert!(transaction
.write_set
.contains_shared(SharedDependency::IdReuse));
assert!(!transaction
.write_set
.contains_shared(SharedDependency::IdAllocation));
assert!(transaction.write_set.rebase_dependencies().is_some());
committer
.set_node_property(unrelated_node, "marker".to_owned(), Value::Integer(2))
.unwrap();
committer.commit_transaction().unwrap();
stale.commit_transaction().unwrap();
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert_eq!(
verifier.node(reclaimed_node).unwrap().properties["value"],
Value::Integer(0)
);
assert_eq!(
verifier.node(resized_node).unwrap().properties["value"],
replacement
);
assert_eq!(
verifier.node(unrelated_node).unwrap().properties["marker"],
Value::Integer(2)
);
}
#[test]
fn disjoint_existing_label_updates_merge_counts_deltas() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
setup
.create_node(vec!["Active".to_owned()], Properties::new())
.unwrap();
let first_node = setup.create_node(Vec::new(), Properties::new()).unwrap();
let second_node = setup.create_node(Vec::new(), Properties::new()).unwrap();
setup.commit_transaction().unwrap();
let mut first = Neo4jWritableStorage::open(&database).unwrap();
let mut second = Neo4jWritableStorage::open(&database).unwrap();
first.begin_transaction().unwrap();
second.begin_transaction().unwrap();
first
.set_node_label(first_node, "Active".to_owned())
.unwrap();
second
.set_node_label(second_node, "Active".to_owned())
.unwrap();
let write_set = &second.transaction.as_ref().unwrap().write_set;
assert!(write_set.contains_shared(SharedDependency::Counts));
assert!(!write_set.contains_shared(SharedDependency::Tokens));
assert!(!write_set.contains_shared(SharedDependency::IdAllocation));
assert!(write_set.rebase_dependencies().is_some());
first.commit_transaction().unwrap();
second.commit_transaction().unwrap();
assert_eq!(second.storage.nodes_with_label("Active").len(), 3);
let label_id = second
.storage
.label_tokens()
.iter()
.find_map(|(id, label)| (label == "Active").then_some(*id))
.unwrap();
assert_eq!(
super::super::counts::locate_node_count(
&database.join(COUNTS_STORE),
i32::try_from(label_id).unwrap()
)
.unwrap()
.count,
3
);
}
#[test]
fn disjoint_dynamic_node_label_updates_merge_chains_and_counts() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let labels = (0..8)
.map(|index| format!("Label{index}"))
.collect::<Vec<_>>();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
setup
.create_node(vec!["Extra".to_owned()], Properties::new())
.unwrap();
let first_node = setup
.create_node(labels.clone(), Properties::new())
.unwrap();
let second_node = setup.create_node(labels, Properties::new()).unwrap();
setup.commit_transaction().unwrap();
let mut first = Neo4jWritableStorage::open(&database).unwrap();
let mut second = Neo4jWritableStorage::open(&database).unwrap();
first.begin_transaction().unwrap();
second.begin_transaction().unwrap();
first
.set_node_label(first_node, "Extra".to_owned())
.unwrap();
second
.set_node_label(second_node, "Extra".to_owned())
.unwrap();
for writable in [&first, &second] {
let transaction = writable.transaction.as_ref().unwrap();
assert!(!transaction.node_label_chain_ids.is_empty());
assert!(transaction
.write_set
.contains_shared(SharedDependency::DynamicChains));
assert!(transaction
.write_set
.contains_shared(SharedDependency::Counts));
assert!(transaction.write_set.rebase_dependencies().is_some());
}
first.commit_transaction().unwrap();
second.commit_transaction().unwrap();
assert_eq!(second.storage.nodes_with_label("Extra").len(), 3);
assert!(second
.storage
.node(first_node)
.unwrap()
.labels
.contains(&"Extra".to_owned()));
assert!(second
.storage
.node(second_node)
.unwrap()
.labels
.contains(&"Extra".to_owned()));
let label_id = second
.storage
.label_tokens()
.iter()
.find_map(|(id, label)| (label == "Extra").then_some(*id))
.unwrap();
assert_eq!(
super::super::counts::locate_node_count(
&database.join(COUNTS_STORE),
i32::try_from(label_id).unwrap()
)
.unwrap()
.count,
3
);
}
#[test]
fn stale_dynamic_node_label_resize_reuses_validated_free_ids() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let labels = (0..20)
.map(|index| format!("Label{index}"))
.collect::<Vec<_>>();
let initial_labels = labels[..8].to_vec();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
setup
.create_node(labels.clone(), Properties::new())
.unwrap();
let reclaimed_node = setup
.create_node(labels.clone(), Properties::new())
.unwrap();
let resized_node = setup
.create_node(initial_labels, Properties::new())
.unwrap();
let unrelated_node = setup
.create_node(
Vec::new(),
Properties::from([("marker".to_owned(), Value::Integer(1))]),
)
.unwrap();
setup.commit_transaction().unwrap();
setup.begin_transaction().unwrap();
for label in &labels[7..] {
setup.remove_node_label(reclaimed_node, label).unwrap();
}
setup.commit_transaction().unwrap();
let mut stale = Neo4jWritableStorage::open(&database).unwrap();
let mut committer = Neo4jWritableStorage::open(&database).unwrap();
stale.begin_transaction().unwrap();
committer.begin_transaction().unwrap();
for label in &labels[8..] {
stale.set_node_label(resized_node, label.clone()).unwrap();
}
let transaction = stale.transaction.as_ref().unwrap();
assert!(!transaction.reused_node_label_records.is_empty());
assert_eq!(transaction.allocated_node_label_records, 0);
assert!(transaction.freed_node_label_records.is_empty());
assert!(transaction
.write_set
.contains_shared(SharedDependency::DynamicChains));
assert!(transaction
.write_set
.contains_shared(SharedDependency::IdReuse));
assert!(transaction
.write_set
.contains_shared(SharedDependency::Counts));
assert!(!transaction
.write_set
.contains_shared(SharedDependency::IdAllocation));
assert!(!transaction
.write_set
.contains_shared(SharedDependency::Tokens));
committer
.set_node_property(unrelated_node, "marker".to_owned(), Value::Integer(2))
.unwrap();
committer.commit_transaction().unwrap();
stale.commit_transaction().unwrap();
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert_eq!(verifier.node(resized_node).unwrap().labels, labels);
assert_eq!(
verifier.node(unrelated_node).unwrap().properties["marker"],
Value::Integer(2)
);
for label in &labels[8..] {
assert_eq!(verifier.nodes_with_label(label).len(), 2);
}
}
#[test]
fn concurrent_dynamic_node_label_resizes_conflict_and_retry() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let labels = (0..20)
.map(|index| format!("Label{index}"))
.collect::<Vec<_>>();
let initial_labels = labels[..8].to_vec();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
setup
.create_node(labels.clone(), Properties::new())
.unwrap();
let first_node = setup
.create_node(initial_labels.clone(), Properties::new())
.unwrap();
let second_node = setup
.create_node(initial_labels.clone(), Properties::new())
.unwrap();
setup.commit_transaction().unwrap();
let mut first = Neo4jWritableStorage::open(&database).unwrap();
let mut second = Neo4jWritableStorage::open(&database).unwrap();
first.begin_transaction().unwrap();
second.begin_transaction().unwrap();
for label in &labels[8..] {
first.set_node_label(first_node, label.clone()).unwrap();
second.set_node_label(second_node, label.clone()).unwrap();
}
let first_transaction = first.transaction.as_ref().unwrap();
let second_transaction = second.transaction.as_ref().unwrap();
assert!(first_transaction.allocated_node_label_records > 0);
assert_eq!(
first_transaction.allocated_node_label_records,
second_transaction.allocated_node_label_records
);
let first_records = first_transaction
.node_label_records
.keys()
.collect::<BTreeSet<_>>();
let second_records = second_transaction
.node_label_records
.keys()
.collect::<BTreeSet<_>>();
let overlapping_records = first_records
.intersection(&second_records)
.copied()
.collect::<Vec<_>>();
assert_eq!(
overlapping_records.len() as u64,
first_transaction.allocated_node_label_records
);
for record_id in overlapping_records {
assert!(first_transaction
.write_set
.contains_fixed(FixedStore::NodeLabel, record_id));
assert!(second_transaction
.write_set
.contains_fixed(FixedStore::NodeLabel, record_id));
}
for transaction in [first_transaction, second_transaction] {
assert!(transaction
.write_set
.contains_shared(SharedDependency::DynamicChains));
assert!(transaction
.write_set
.contains_shared(SharedDependency::IdAllocation));
assert!(transaction
.write_set
.contains_shared(SharedDependency::Counts));
assert!(transaction.write_set.rebase_dependencies().is_some());
}
first.commit_transaction().unwrap();
assert!(matches!(
second.commit_transaction(),
Err(Error::Transaction(crate::TransactionErrorKind::Conflict))
));
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert_eq!(verifier.node(first_node).unwrap().labels, labels);
assert_eq!(verifier.node(second_node).unwrap().labels, initial_labels);
for label in &labels[8..] {
assert_eq!(verifier.nodes_with_label(label).len(), 2);
}
drop(verifier);
second.begin_transaction().unwrap();
for label in &labels[8..] {
second.set_node_label(second_node, label.clone()).unwrap();
}
second.commit_transaction().unwrap();
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert_eq!(verifier.node(first_node).unwrap().labels, labels);
assert_eq!(verifier.node(second_node).unwrap().labels, labels);
for label in &labels[8..] {
assert_eq!(verifier.nodes_with_label(label).len(), 3);
}
}
#[test]
fn disjoint_dynamic_to_inline_label_updates_merge_reclamation_and_counts() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let labels = (0..8)
.map(|index| format!("Label{index}"))
.collect::<Vec<_>>();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let first_node = setup
.create_node(labels.clone(), Properties::new())
.unwrap();
let second_node = setup.create_node(labels, Properties::new()).unwrap();
setup.commit_transaction().unwrap();
let mut first = Neo4jWritableStorage::open(&database).unwrap();
let mut second = Neo4jWritableStorage::open(&database).unwrap();
first.begin_transaction().unwrap();
second.begin_transaction().unwrap();
first.remove_node_label(first_node, "Label7").unwrap();
second.remove_node_label(second_node, "Label7").unwrap();
for writable in [&first, &second] {
let transaction = writable.transaction.as_ref().unwrap();
assert!(!transaction.freed_node_label_records.is_empty());
assert!(transaction
.write_set
.contains_shared(SharedDependency::IdReclamation));
assert!(transaction
.write_set
.contains_shared(SharedDependency::Counts));
assert!(transaction.write_set.rebase_dependencies().is_some());
}
let reclaimed_records = first
.transaction
.as_ref()
.unwrap()
.freed_node_label_records
.len()
+ second
.transaction
.as_ref()
.unwrap()
.freed_node_label_records
.len();
first.commit_transaction().unwrap();
second.commit_transaction().unwrap();
assert!(second.storage.nodes_with_label("Label7").is_empty());
assert!(!second
.storage
.node(first_node)
.unwrap()
.labels
.contains(&"Label7".to_owned()));
assert!(!second
.storage
.node(second_node)
.unwrap()
.labels
.contains(&"Label7".to_owned()));
let label_id = second
.storage
.label_tokens()
.iter()
.find_map(|(id, label)| (label == "Label7").then_some(*id))
.unwrap();
assert_eq!(
super::super::counts::locate_node_count(
&database.join(COUNTS_STORE),
i32::try_from(label_id).unwrap()
)
.unwrap()
.count,
0
);
let header = super::super::id_generator::validate_writable(
&database,
NODE_LABEL_STORE,
second.node_label_layout.record_capacity,
)
.unwrap();
assert_eq!(header.num_unused_ids, reclaimed_records as u64);
}
#[test]
fn overlapping_label_updates_conflict_before_merging_counts() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
setup
.create_node(vec!["Active".to_owned()], Properties::new())
.unwrap();
let target = setup.create_node(Vec::new(), Properties::new()).unwrap();
setup.commit_transaction().unwrap();
let mut first = Neo4jWritableStorage::open(&database).unwrap();
let mut second = Neo4jWritableStorage::open(&database).unwrap();
first.begin_transaction().unwrap();
second.begin_transaction().unwrap();
first.set_node_label(target, "Active".to_owned()).unwrap();
second.set_node_label(target, "Active".to_owned()).unwrap();
first.commit_transaction().unwrap();
assert!(matches!(
second.commit_transaction(),
Err(Error::Transaction(crate::TransactionErrorKind::Conflict))
));
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert_eq!(verifier.nodes_with_label("Active").len(), 2);
}
#[test]
fn stale_overlapping_fixed_record_updates_conflict() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let node = setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), Value::Integer(1))]),
)
.unwrap();
setup.commit_transaction().unwrap();
let mut first = Neo4jWritableStorage::open(&database).unwrap();
let mut second = Neo4jWritableStorage::open(&database).unwrap();
first.begin_transaction().unwrap();
second.begin_transaction().unwrap();
first
.set_node_property(node, "value".to_owned(), Value::Integer(10))
.unwrap();
second
.set_node_property(node, "value".to_owned(), Value::Integer(20))
.unwrap();
first.commit_transaction().unwrap();
assert!(matches!(
second.commit_transaction(),
Err(Error::Transaction(crate::TransactionErrorKind::Conflict))
));
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert_eq!(
verifier.node(node).unwrap().properties["value"],
Value::Integer(10)
);
}
#[test]
fn failed_rebased_journal_restores_the_current_store_images() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let first_node = setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), Value::Integer(1))]),
)
.unwrap();
let second_node = setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), Value::Integer(2))]),
)
.unwrap();
setup.commit_transaction().unwrap();
let mut first = Neo4jWritableStorage::open(&database).unwrap();
let mut rebased = Neo4jWritableStorage::open(&database).unwrap();
first.begin_transaction().unwrap();
rebased.begin_transaction().unwrap();
first
.set_node_property(first_node, "value".to_owned(), Value::Integer(10))
.unwrap();
rebased
.set_node_property(second_node, "value".to_owned(), Value::Integer(20))
.unwrap();
first.commit_transaction().unwrap();
transaction::fail_next_commit_after_patch(0);
assert!(rebased.commit_transaction().is_err());
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert_eq!(
verifier.node(first_node).unwrap().properties["value"],
Value::Integer(10)
);
assert_eq!(
verifier.node(second_node).unwrap().properties["value"],
Value::Integer(2)
);
assert!(!database.join(".littlegraph-write-journal").exists());
}
#[test]
fn failed_rebased_allocations_restore_records_counts_and_id_state() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let reusable = setup.create_node(Vec::new(), Properties::new()).unwrap();
setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), Value::Integer(1))]),
)
.unwrap();
setup.commit_transaction().unwrap();
setup.begin_transaction().unwrap();
setup.delete_node(reusable).unwrap();
setup.commit_transaction().unwrap();
let mut first = Neo4jWritableStorage::open(&database).unwrap();
let mut rebased = Neo4jWritableStorage::open(&database).unwrap();
let existing = first.storage.node_by_external_id("1").unwrap();
first.begin_transaction().unwrap();
rebased.begin_transaction().unwrap();
first
.set_node_property(existing, "value".to_owned(), Value::Integer(10))
.unwrap();
let recreated = rebased.create_node(Vec::new(), Properties::new()).unwrap();
assert_eq!(rebased.storage.node(recreated).unwrap().external_id, "0");
first.commit_transaction().unwrap();
transaction::fail_next_commit_after_store_sync(2);
assert!(rebased.commit_transaction().is_err());
{
let state = rebased.coordinator.state.lock().unwrap();
assert!(state.active_snapshots.is_empty());
assert!(state.record_history.is_empty());
}
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert!(verifier.node_by_external_id("0").is_none());
let existing = verifier.node_by_external_id("1").unwrap();
assert_eq!(
verifier.node(existing).unwrap().properties["value"],
Value::Integer(10)
);
assert_eq!(
super::super::counts::locate_node_count(&database.join(COUNTS_STORE), -1)
.unwrap()
.count,
1
);
drop(verifier);
let header = super::super::id_generator::validate_writable(
&database,
NODE_STORE,
rebased.node_record_capacity,
)
.unwrap();
assert_eq!(header.num_unused_ids, 1);
assert!(!database.join(".littlegraph-write-journal").exists());
rebased.begin_transaction().unwrap();
let recreated = rebased.create_node(Vec::new(), Properties::new()).unwrap();
assert_eq!(rebased.storage.node(recreated).unwrap().external_id, "0");
rebased.commit_transaction().unwrap();
let header = super::super::id_generator::validate_writable(
&database,
NODE_STORE,
rebased.node_record_capacity,
)
.unwrap();
assert_eq!(header.num_unused_ids, 0);
let mut first = Neo4jWritableStorage::open(&database).unwrap();
let mut rebased_allocation = Neo4jWritableStorage::open(&database).unwrap();
let existing = first.storage.node_by_external_id("1").unwrap();
first.begin_transaction().unwrap();
rebased_allocation.begin_transaction().unwrap();
first
.set_node_property(existing, "value".to_owned(), Value::Integer(20))
.unwrap();
let allocated = rebased_allocation
.create_node(Vec::new(), Properties::new())
.unwrap();
assert_eq!(
rebased_allocation
.storage
.node(allocated)
.unwrap()
.external_id,
"2"
);
first.commit_transaction().unwrap();
transaction::fail_next_commit_after_store_sync(2);
assert!(rebased_allocation.commit_transaction().is_err());
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert!(verifier.node_by_external_id("2").is_none());
let existing = verifier.node_by_external_id("1").unwrap();
assert_eq!(
verifier.node(existing).unwrap().properties["value"],
Value::Integer(20)
);
assert_eq!(
super::super::counts::locate_node_count(&database.join(COUNTS_STORE), -1)
.unwrap()
.count,
2
);
drop(verifier);
let header = super::super::id_generator::validate_writable(
&database,
NODE_STORE,
rebased_allocation.node_record_capacity,
)
.unwrap();
assert_eq!(header.high_id, 2);
assert_eq!(header.num_unused_ids, 0);
assert!(!database.join(".littlegraph-write-journal").exists());
rebased_allocation.begin_transaction().unwrap();
let allocated = rebased_allocation
.create_node(Vec::new(), Properties::new())
.unwrap();
assert_eq!(
rebased_allocation
.storage
.node(allocated)
.unwrap()
.external_id,
"2"
);
rebased_allocation.commit_transaction().unwrap();
let header = super::super::id_generator::validate_writable(
&database,
NODE_STORE,
rebased_allocation.node_record_capacity,
)
.unwrap();
assert_eq!(header.high_id, 3);
assert_eq!(header.num_unused_ids, 0);
}
#[test]
fn failed_rebased_token_reuse_restores_token_name_counts_and_node() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
setup.create_node(Vec::new(), Properties::new()).unwrap();
setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), Value::Integer(1))]),
)
.unwrap();
setup.commit_transaction().unwrap();
drop(setup);
let label_id_path = database.join(LABEL_TOKEN_ID_STORE);
fs::write(&label_id_path, super::super::id_generator::empty_file(1)).unwrap();
mark_ids_free(&label_id_path, &[0]);
let mut first = Neo4jWritableStorage::open(&database).unwrap();
let mut rebased = Neo4jWritableStorage::open(&database).unwrap();
let initial_label_name_high_id = rebased.label_name_id_header.high_id;
let target = rebased.storage.node_by_external_id("0").unwrap();
let existing = first.storage.node_by_external_id("1").unwrap();
first.begin_transaction().unwrap();
rebased.begin_transaction().unwrap();
first
.set_node_property(existing, "value".to_owned(), Value::Integer(10))
.unwrap();
rebased
.set_node_label(target, "RecoveredLabel".to_owned())
.unwrap();
assert_eq!(
rebased.transaction.as_ref().unwrap().reused_label_tokens,
[0]
);
first.commit_transaction().unwrap();
transaction::fail_next_commit_after_store_sync(2);
assert!(rebased.commit_transaction().is_err());
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert!(!verifier.has_label("RecoveredLabel"));
let target = verifier.node_by_external_id("0").unwrap();
assert!(verifier.node(target).unwrap().labels.is_empty());
let existing = verifier.node_by_external_id("1").unwrap();
assert_eq!(
verifier.node(existing).unwrap().properties["value"],
Value::Integer(10)
);
assert!(super::super::counts::locate_node_count(&database.join(COUNTS_STORE), 0).is_err());
drop(verifier);
let token_header = super::super::id_generator::validate_writable(
&database,
LABEL_TOKEN_STORE,
rebased.label_token_capacity,
)
.unwrap();
assert_eq!(token_header.num_unused_ids, 1);
let name_header = super::super::id_generator::validate_writable(
&database,
LABEL_NAME_STORE,
rebased.label_name_layout.record_capacity,
)
.unwrap();
assert_eq!(name_header.high_id, initial_label_name_high_id);
assert!(!database.join(".littlegraph-write-journal").exists());
rebased.begin_transaction().unwrap();
let target = rebased.storage.node_by_external_id("0").unwrap();
rebased
.set_node_label(target, "RecoveredLabel".to_owned())
.unwrap();
rebased.commit_transaction().unwrap();
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert!(verifier.has_label("RecoveredLabel"));
let target = verifier.node_by_external_id("0").unwrap();
assert_eq!(verifier.node(target).unwrap().labels, ["RecoveredLabel"]);
assert_eq!(
super::super::counts::locate_node_count(&database.join(COUNTS_STORE), 0)
.unwrap()
.count,
1
);
let token_header = super::super::id_generator::validate_writable(
&database,
LABEL_TOKEN_STORE,
rebased.label_token_capacity,
)
.unwrap();
assert_eq!(token_header.num_unused_ids, 0);
let name_header = super::super::id_generator::validate_writable(
&database,
LABEL_NAME_STORE,
rebased.label_name_layout.record_capacity,
)
.unwrap();
assert!(name_header.high_id > initial_label_name_high_id);
}
#[test]
fn failed_rebased_dynamic_resize_restores_chain_owner_and_free_list() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let initial = Value::String("b".repeat(40));
let replacement = Value::String("c".repeat(200));
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let reclaimed_node = setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), Value::String("a".repeat(500)))]),
)
.unwrap();
let resized_node = setup
.create_node(
Vec::new(),
Properties::from([("value".to_owned(), initial.clone())]),
)
.unwrap();
let unrelated_node = setup
.create_node(
Vec::new(),
Properties::from([("marker".to_owned(), Value::Integer(1))]),
)
.unwrap();
setup.commit_transaction().unwrap();
setup.begin_transaction().unwrap();
setup
.set_node_property(reclaimed_node, "value".to_owned(), Value::Integer(0))
.unwrap();
setup.commit_transaction().unwrap();
let mut rebased = Neo4jWritableStorage::open(&database).unwrap();
let mut first = Neo4jWritableStorage::open(&database).unwrap();
let initial_header = rebased.string_id_header;
rebased.begin_transaction().unwrap();
first.begin_transaction().unwrap();
rebased
.set_node_property(resized_node, "value".to_owned(), replacement.clone())
.unwrap();
let reused_count = rebased
.transaction
.as_ref()
.unwrap()
.reused_string_records
.len() as u64;
assert!(reused_count > 0);
first
.set_node_property(unrelated_node, "marker".to_owned(), Value::Integer(2))
.unwrap();
first.commit_transaction().unwrap();
transaction::fail_next_commit_after_store_sync(2);
assert!(rebased.commit_transaction().is_err());
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert_eq!(
verifier.node(reclaimed_node).unwrap().properties["value"],
Value::Integer(0)
);
assert_eq!(
verifier.node(resized_node).unwrap().properties["value"],
initial
);
assert_eq!(
verifier.node(unrelated_node).unwrap().properties["marker"],
Value::Integer(2)
);
drop(verifier);
let header = super::super::id_generator::validate_writable(
&database,
STRING_STORE,
rebased.string_layout.record_capacity,
)
.unwrap();
assert_eq!(header.high_id, initial_header.high_id);
assert_eq!(header.num_unused_ids, initial_header.num_unused_ids);
assert!(!database.join(".littlegraph-write-journal").exists());
rebased.begin_transaction().unwrap();
rebased
.set_node_property(resized_node, "value".to_owned(), replacement.clone())
.unwrap();
rebased.commit_transaction().unwrap();
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert_eq!(
verifier.node(resized_node).unwrap().properties["value"],
replacement
);
let header = super::super::id_generator::validate_writable(
&database,
STRING_STORE,
rebased.string_layout.record_capacity,
)
.unwrap();
assert_eq!(header.high_id, initial_header.high_id);
assert_eq!(
header.num_unused_ids,
initial_header.num_unused_ids - reused_count
);
}
#[test]
fn failed_rebased_relationship_reuse_restores_heads_counts_and_free_list() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let source = setup.create_node(Vec::new(), Properties::new()).unwrap();
let target = setup.create_node(Vec::new(), Properties::new()).unwrap();
let unrelated = setup
.create_node(
Vec::new(),
Properties::from([("marker".to_owned(), Value::Integer(1))]),
)
.unwrap();
let reusable = setup
.create_relationship(source, target, "LINK".to_owned(), Properties::new())
.unwrap();
setup.commit_transaction().unwrap();
setup.begin_transaction().unwrap();
setup.delete_relationship(reusable).unwrap();
setup.commit_transaction().unwrap();
let mut rebased = Neo4jWritableStorage::open(&database).unwrap();
let mut first = Neo4jWritableStorage::open(&database).unwrap();
rebased.begin_transaction().unwrap();
first.begin_transaction().unwrap();
let recreated = rebased
.create_relationship(source, target, "LINK".to_owned(), Properties::new())
.unwrap();
assert_eq!(
rebased.storage.relationship(recreated).unwrap().external_id,
"0"
);
first
.set_node_property(unrelated, "marker".to_owned(), Value::Integer(2))
.unwrap();
first.commit_transaction().unwrap();
transaction::fail_next_commit_after_store_sync(3);
assert!(rebased.commit_transaction().is_err());
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert!(verifier.relationship_by_external_id("0").is_none());
assert!(verifier.outgoing_relationships(source).is_empty());
assert!(verifier.incoming_relationships(target).is_empty());
assert_eq!(
verifier.node(unrelated).unwrap().properties["marker"],
Value::Integer(2)
);
let count = super::super::counts::locate_relationship_count(
&database.join(COUNTS_STORE),
super::super::counts::RelationshipCountKey {
start_label_id: -1,
type_id: -1,
end_label_id: -1,
},
)
.unwrap();
assert_eq!(count.count, 0);
drop(verifier);
let header = super::super::id_generator::validate_writable(
&database,
RELATIONSHIP_STORE,
rebased.relationship_record_capacity,
)
.unwrap();
assert_eq!(header.num_unused_ids, 1);
assert!(!database.join(".littlegraph-write-journal").exists());
rebased.begin_transaction().unwrap();
let recreated = rebased
.create_relationship(source, target, "LINK".to_owned(), Properties::new())
.unwrap();
assert_eq!(
rebased.storage.relationship(recreated).unwrap().external_id,
"0"
);
rebased.commit_transaction().unwrap();
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert!(verifier.relationship_by_external_id("0").is_some());
assert_eq!(verifier.outgoing_relationships(source).len(), 1);
assert_eq!(verifier.incoming_relationships(target).len(), 1);
let count = super::super::counts::locate_relationship_count(
&database.join(COUNTS_STORE),
super::super::counts::RelationshipCountKey {
start_label_id: -1,
type_id: -1,
end_label_id: -1,
},
)
.unwrap();
assert_eq!(count.count, 1);
let header = super::super::id_generator::validate_writable(
&database,
RELATIONSHIP_STORE,
rebased.relationship_record_capacity,
)
.unwrap();
assert_eq!(header.num_unused_ids, 0);
}
#[test]
fn failed_rebased_relationship_reclamation_restores_record_heads_and_count() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let source = setup.create_node(Vec::new(), Properties::new()).unwrap();
let target = setup.create_node(Vec::new(), Properties::new()).unwrap();
let unrelated = setup
.create_node(
Vec::new(),
Properties::from([("marker".to_owned(), Value::Integer(1))]),
)
.unwrap();
let relationship = setup
.create_relationship(source, target, "LINK".to_owned(), Properties::new())
.unwrap();
setup.commit_transaction().unwrap();
let mut rebased = Neo4jWritableStorage::open(&database).unwrap();
let mut first = Neo4jWritableStorage::open(&database).unwrap();
rebased.begin_transaction().unwrap();
first.begin_transaction().unwrap();
rebased.delete_relationship(relationship).unwrap();
first
.set_node_property(unrelated, "marker".to_owned(), Value::Integer(2))
.unwrap();
first.commit_transaction().unwrap();
transaction::fail_next_commit_after_store_sync(3);
assert!(rebased.commit_transaction().is_err());
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert!(verifier.relationship_by_external_id("0").is_some());
assert_eq!(verifier.outgoing_relationships(source).len(), 1);
assert_eq!(verifier.incoming_relationships(target).len(), 1);
assert_eq!(
verifier.node(unrelated).unwrap().properties["marker"],
Value::Integer(2)
);
let count = super::super::counts::locate_relationship_count(
&database.join(COUNTS_STORE),
super::super::counts::RelationshipCountKey {
start_label_id: -1,
type_id: -1,
end_label_id: -1,
},
)
.unwrap();
assert_eq!(count.count, 1);
drop(verifier);
let header = super::super::id_generator::validate_writable(
&database,
RELATIONSHIP_STORE,
rebased.relationship_record_capacity,
)
.unwrap();
assert_eq!(header.num_unused_ids, 0);
assert!(!database.join(".littlegraph-write-journal").exists());
rebased.begin_transaction().unwrap();
let relationship = rebased.storage.relationship_by_external_id("0").unwrap();
rebased.delete_relationship(relationship).unwrap();
rebased.commit_transaction().unwrap();
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert!(verifier.relationship_by_external_id("0").is_none());
assert!(verifier.outgoing_relationships(source).is_empty());
assert!(verifier.incoming_relationships(target).is_empty());
let count = super::super::counts::locate_relationship_count(
&database.join(COUNTS_STORE),
super::super::counts::RelationshipCountKey {
start_label_id: -1,
type_id: -1,
end_label_id: -1,
},
)
.unwrap();
assert_eq!(count.count, 0);
let header = super::super::id_generator::validate_writable(
&database,
RELATIONSHIP_STORE,
rebased.relationship_record_capacity,
)
.unwrap();
assert_eq!(header.num_unused_ids, 1);
}
#[test]
fn failed_rebased_node_label_resize_restores_owner_counts_and_free_list() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let labels = (0..20)
.map(|index| format!("Label{index}"))
.collect::<Vec<_>>();
let initial_labels = labels[..8].to_vec();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
setup
.create_node(labels.clone(), Properties::new())
.unwrap();
let reclaimed_node = setup
.create_node(labels.clone(), Properties::new())
.unwrap();
let resized_node = setup
.create_node(initial_labels.clone(), Properties::new())
.unwrap();
let unrelated_node = setup
.create_node(
Vec::new(),
Properties::from([("marker".to_owned(), Value::Integer(1))]),
)
.unwrap();
setup.commit_transaction().unwrap();
setup.begin_transaction().unwrap();
for label in &labels[7..] {
setup.remove_node_label(reclaimed_node, label).unwrap();
}
setup.commit_transaction().unwrap();
let mut rebased = Neo4jWritableStorage::open(&database).unwrap();
let mut first = Neo4jWritableStorage::open(&database).unwrap();
let initial_header = rebased.node_label_id_header;
rebased.begin_transaction().unwrap();
first.begin_transaction().unwrap();
for label in &labels[8..] {
rebased.set_node_label(resized_node, label.clone()).unwrap();
}
let reused_count = rebased
.transaction
.as_ref()
.unwrap()
.reused_node_label_records
.len() as u64;
assert!(reused_count > 0);
first
.set_node_property(unrelated_node, "marker".to_owned(), Value::Integer(2))
.unwrap();
first.commit_transaction().unwrap();
transaction::fail_next_commit_after_store_sync(3);
assert!(rebased.commit_transaction().is_err());
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert_eq!(verifier.node(resized_node).unwrap().labels, initial_labels);
assert_eq!(
verifier.node(unrelated_node).unwrap().properties["marker"],
Value::Integer(2)
);
for label in &labels[8..] {
assert_eq!(verifier.nodes_with_label(label).len(), 1);
}
drop(verifier);
let header = super::super::id_generator::validate_writable(
&database,
NODE_LABEL_STORE,
rebased.node_label_layout.record_capacity,
)
.unwrap();
assert_eq!(header.high_id, initial_header.high_id);
assert_eq!(header.num_unused_ids, initial_header.num_unused_ids);
assert!(!database.join(".littlegraph-write-journal").exists());
rebased.begin_transaction().unwrap();
for label in &labels[8..] {
rebased.set_node_label(resized_node, label.clone()).unwrap();
}
rebased.commit_transaction().unwrap();
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert_eq!(verifier.node(resized_node).unwrap().labels, labels);
for label in &labels[8..] {
assert_eq!(verifier.nodes_with_label(label).len(), 2);
}
let header = super::super::id_generator::validate_writable(
&database,
NODE_LABEL_STORE,
rebased.node_label_layout.record_capacity,
)
.unwrap();
assert_eq!(header.high_id, initial_header.high_id);
assert_eq!(
header.num_unused_ids,
initial_header.num_unused_ids - reused_count
);
}
#[test]
fn savepoint_rollback_restores_the_physical_write_set() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut writable = Neo4jWritableStorage::open(&database).unwrap();
writable.begin_transaction().unwrap();
writable.create_node(Vec::new(), Properties::new()).unwrap();
let savepoint = writable.savepoint().unwrap();
let saved_write_set = writable.transaction.as_ref().unwrap().write_set.clone();
writable.create_node(Vec::new(), Properties::new()).unwrap();
assert_ne!(
writable.transaction.as_ref().unwrap().write_set,
saved_write_set
);
writable.rollback_to_savepoint(&savepoint).unwrap();
assert_eq!(
writable.transaction.as_ref().unwrap().write_set,
saved_write_set
);
writable.rollback_transaction().unwrap();
}
#[test]
fn savepoint_rollback_restores_reused_token_ids_and_catalog() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut setup = Neo4jWritableStorage::open(&database).unwrap();
setup.begin_transaction().unwrap();
let node = setup.create_node(Vec::new(), Properties::new()).unwrap();
setup.commit_transaction().unwrap();
drop(setup);
let label_id_path = database.join(LABEL_TOKEN_ID_STORE);
fs::write(&label_id_path, super::super::id_generator::empty_file(1)).unwrap();
mark_ids_free(&label_id_path, &[0]);
let mut writable = Neo4jWritableStorage::open(&database).unwrap();
writable.begin_transaction().unwrap();
let savepoint = writable.savepoint().unwrap();
writable
.set_node_label(node, "Discarded".to_owned())
.unwrap();
assert_eq!(
writable.transaction.as_ref().unwrap().reused_label_tokens,
[0]
);
assert!(writable.storage.has_label("Discarded"));
writable.rollback_to_savepoint(&savepoint).unwrap();
let transaction = writable.transaction.as_ref().unwrap();
assert!(transaction.reused_label_tokens.is_empty());
assert!(!transaction
.write_set
.contains_shared(SharedDependency::IdReuse));
assert!(!writable.storage.has_label("Discarded"));
writable.set_node_label(node, "Kept".to_owned()).unwrap();
assert_eq!(
writable.transaction.as_ref().unwrap().reused_label_tokens,
[0]
);
writable.commit_transaction().unwrap();
let verifier = Neo4jStorage::open_unlocked(&database).unwrap();
assert!(!verifier.has_label("Discarded"));
assert_eq!(verifier.node(node).unwrap().labels, ["Kept"]);
assert_eq!(verifier.nodes_with_label("Kept"), [node]);
let header = super::super::id_generator::validate_writable(
&database,
LABEL_TOKEN_STORE,
writable.label_token_capacity,
)
.unwrap();
assert_eq!(header.num_unused_ids, 0);
}
#[test]
fn spilling_preserves_physical_dependency_keys() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut writable = Neo4jWritableStorage::open(&database).unwrap();
writable.configure_workspace_spill(Some(0), Some(1024 * 1024), None, Some(99));
writable.begin_transaction().unwrap();
let transaction = writable.transaction.as_mut().unwrap();
transaction.node_records.insert(7, [1; NODE_RECORD_SIZE]);
transaction.string_records.insert(11, vec![2; 256]);
transaction.refresh_write_set();
let before = transaction.write_set.clone();
transaction.spill_record_maps().unwrap();
transaction.refresh_write_set();
assert_eq!(transaction.write_set, before);
assert!(transaction.write_set.contains_fixed(FixedStore::Node, 7));
assert!(transaction.write_set.contains_fixed(FixedStore::String, 11));
writable.rollback_transaction().unwrap();
}
#[test]
fn workspace_usage_charges_physical_dependency_keys() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut writable = Neo4jWritableStorage::open(&database).unwrap();
writable.begin_transaction().unwrap();
let transaction = writable.transaction.as_mut().unwrap();
transaction.node_records.insert(7, [1; NODE_RECORD_SIZE]);
let without_write_set = transaction.workspace_usage().unwrap();
transaction.refresh_write_set();
let with_write_set = transaction.workspace_usage().unwrap();
assert_eq!(
with_write_set.bytes - without_write_set.bytes,
transaction.write_set.fixed_heap_bytes().unwrap()
);
assert_eq!(with_write_set.records, without_write_set.records);
writable.capture_before_images().unwrap();
let with_before_image = writable.workspace_usage().unwrap();
assert_eq!(
with_before_image.bytes - with_write_set.bytes,
NODE_RECORD_SIZE
);
assert_eq!(with_before_image.records, with_write_set.records);
writable.rollback_transaction().unwrap();
}
#[test]
fn failed_spill_setup_does_not_leak_options_into_the_next_transaction() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut writable = Neo4jWritableStorage::open(&database).unwrap();
writable.configure_workspace_spill(Some(0), Some(7), None, Some(99));
assert!(matches!(
writable.begin_transaction(),
Err(Error::Transaction(
crate::TransactionErrorKind::ResourceLimit
))
));
assert!(writable.transaction.is_none());
writable.begin_transaction().unwrap();
assert!(writable
.transaction
.as_ref()
.unwrap()
.spill_threshold_bytes
.is_none());
writable.rollback_transaction().unwrap();
}
#[test]
fn transaction_change_detection_includes_lifecycle_and_chain_state() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut writable = Neo4jWritableStorage::open(&database).unwrap();
writable.begin_transaction().unwrap();
let transaction = writable.transaction.as_mut().unwrap();
assert!(!transaction.has_changes());
transaction.reused_node_records.push(7);
assert!(transaction.has_changes());
transaction.reused_node_records.clear();
transaction.string_chain_ids.insert(3, vec![3, 4]);
assert!(transaction.has_changes());
writable.rollback_transaction().unwrap();
}
#[test]
fn refresh_write_set_classifies_every_shared_dependency() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut writable = Neo4jWritableStorage::open(&database).unwrap();
writable.begin_transaction().unwrap();
let transaction = writable.transaction.as_mut().unwrap();
transaction.string_chain_ids.insert(1, vec![1]);
transaction.allocated_array_records = 1;
transaction.reused_node_records.push(2);
transaction.freed_relationship_records.insert(3);
transaction.node_label_count_deltas.insert(4, 1);
transaction.relationship_group_degree_deltas.insert(
super::super::counts::RelationshipGroupDegreeKey {
group_id: 5,
direction: 0,
},
1,
);
transaction.allocated_label_tokens = 1;
transaction.refresh_write_set();
for dependency in [
SharedDependency::DynamicChains,
SharedDependency::IdAllocation,
SharedDependency::IdReuse,
SharedDependency::IdReclamation,
SharedDependency::Counts,
SharedDependency::RelationshipGroupDegrees,
SharedDependency::Tokens,
] {
assert!(transaction.write_set.contains_shared(dependency));
}
assert_eq!(transaction.write_set.shared_len(), 7);
writable.rollback_transaction().unwrap();
}
#[test]
fn refresh_write_set_classifies_every_fixed_store() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut writable = Neo4jWritableStorage::open(&database).unwrap();
writable.begin_transaction().unwrap();
let transaction = writable.transaction.as_mut().unwrap();
transaction
.property_records
.insert(0, [0; PROPERTY_RECORD_SIZE]);
transaction.node_records.insert(0, [0; NODE_RECORD_SIZE]);
transaction.node_label_records.insert(0, vec![0]);
transaction
.relationship_records
.insert(0, [0; RELATIONSHIP_RECORD_SIZE]);
transaction
.relationship_group_records
.insert(0, [0; RELATIONSHIP_GROUP_RECORD_SIZE]);
transaction.string_records.insert(0, vec![0]);
transaction.array_records.insert(0, vec![0]);
transaction
.property_key_token_records
.insert(0, [0; PROPERTY_KEY_TOKEN_RECORD_SIZE]);
transaction.property_key_name_records.insert(0, vec![0]);
transaction
.label_token_records
.insert(0, [0; LABEL_TOKEN_RECORD_SIZE]);
transaction.label_name_records.insert(0, vec![0]);
transaction
.relationship_type_token_records
.insert(0, [0; LABEL_TOKEN_RECORD_SIZE]);
transaction
.relationship_type_name_records
.insert(0, vec![0]);
transaction.refresh_write_set();
for store in [
FixedStore::Property,
FixedStore::Node,
FixedStore::NodeLabel,
FixedStore::Relationship,
FixedStore::RelationshipGroup,
FixedStore::String,
FixedStore::Array,
FixedStore::PropertyKeyToken,
FixedStore::PropertyKeyName,
FixedStore::LabelToken,
FixedStore::LabelName,
FixedStore::RelationshipTypeToken,
FixedStore::RelationshipTypeName,
] {
assert!(transaction.write_set.contains_fixed(store, 0));
}
assert_eq!(transaction.write_set.pending_fixed().len(), 13);
writable.rollback_transaction().unwrap();
}
#[test]
fn savepoint_usage_charges_fixed_checkpoint_state() {
let home = tempfile::tempdir().unwrap();
let database = home.path().join("data/databases/neo4j");
Neo4jStorage::initialize_if_empty(&database).unwrap();
let mut writable = Neo4jWritableStorage::open(&database).unwrap();
writable.begin_transaction().unwrap();
let workspace = writable.workspace_usage().unwrap();
let storage_bytes = writable.storage.estimated_clone_heap_bytes().unwrap();
let usage = writable.savepoint_usage().unwrap();
let embedded_storage_bytes = 2 * std::mem::size_of::<Neo4jStorage>();
let fixed_bytes = std::mem::size_of::<Neo4jWritableSavepoint>() - embedded_storage_bytes;
assert_eq!(usage.bytes, workspace.bytes + storage_bytes + fixed_bytes);
assert!(fixed_bytes > 0);
writable.rollback_transaction().unwrap();
}
#[test]
#[ignore = "requires NEO4J_REAL_FIXTURE generated by tools/neo4j-fixture-generator"]
fn commits_and_rolls_back_fixed_size_properties_on_a_real_store() {
let source = std::env::var("NEO4J_REAL_FIXTURE").unwrap();
let directory = tempfile::tempdir().unwrap();
let source = Path::new(&source);
let source_data = source.parent().unwrap().parent().unwrap();
let source_transactions = source_data
.join("transactions")
.join(source.file_name().unwrap());
let database = directory.path().join("data/databases/neo4j");
let transactions = directory.path().join("data/transactions/neo4j");
copy_directory(source, &database);
copy_directory(&source_transactions, &transactions);
let mut writable = Neo4jWritableStorage::open(&database).unwrap();
writable.begin_transaction().unwrap();
writable
.ensure_relationship_type_token("ROLLED_BACK_TYPE")
.unwrap();
writable.rollback_transaction().unwrap();
assert!(!writable.storage.has_relationship_type("ROLLED_BACK_TYPE"));
let new_relationship_type = format!("FRESH_{}", "TYPE".repeat(20));
writable.begin_transaction().unwrap();
writable
.ensure_relationship_type_token(&new_relationship_type)
.unwrap();
writable
.ensure_relationship_type_token(&new_relationship_type)
.unwrap();
assert_eq!(
writable
.transaction
.as_ref()
.unwrap()
.allocated_relationship_type_tokens,
1
);
writable.commit_transaction().unwrap();
assert!(writable
.storage
.has_relationship_type(&new_relationship_type));
let alice_id = writable
.storage
.query_nodes
.iter()
.position(|node| node.properties.get("name") == Some(&Value::String("Alice".into())))
.unwrap();
let relationship_id = writable
.storage
.query_relationships
.iter()
.position(|relationship| relationship.properties.contains_key("since"))
.unwrap();
let target_ids = writable
.storage
.query_nodes
.iter()
.filter(|node| node.labels.iter().any(|label| label == "Target"))
.take(2)
.map(|node| node.id)
.collect::<Vec<_>>();
assert_eq!(target_ids.len(), 2);
writable.begin_transaction().unwrap();
let created_relationship = writable
.create_relationship(
target_ids[0],
target_ids[1],
new_relationship_type.clone(),
Properties::from([("since".to_owned(), Value::Integer(2026))]),
)
.unwrap();
let created_loop = writable
.create_relationship(
target_ids[1],
target_ids[1],
new_relationship_type.clone(),
Properties::new(),
)
.unwrap();
assert_eq!(
writable.relationship(created_relationship).unwrap().start,
target_ids[0]
);
assert_eq!(
writable.relationship(created_loop).unwrap().end,
target_ids[1]
);
writable.commit_transaction().unwrap();
writable.begin_transaction().unwrap();
writable
.set_node_property(alice_id, "age".into(), Value::Integer(43))
.unwrap();
writable
.set_node_property(alice_id, "active".into(), Value::Boolean(false))
.unwrap();
writable
.set_relationship_property(relationship_id, "since".into(), Value::Integer(2026))
.unwrap();
assert_eq!(
writable.node(alice_id).unwrap().properties.get("age"),
Some(&Value::Integer(43))
);
assert_eq!(
writable
.relationship(relationship_id)
.unwrap()
.properties
.get("since"),
Some(&Value::Integer(2026))
);
writable.commit_transaction().unwrap();
assert!(!database.join(".littlegraph-write-journal").exists());
writable.begin_transaction().unwrap();
writable.remove_node_property(alice_id, "active").unwrap();
writable.commit_transaction().unwrap();
assert_eq!(
writable.node(alice_id).unwrap().properties.get("active"),
None
);
writable.begin_transaction().unwrap();
writable
.set_node_property(alice_id, "age".into(), Value::Integer(44))
.unwrap();
writable.rollback_transaction().unwrap();
assert_eq!(
writable.node(alice_id).unwrap().properties.get("age"),
Some(&Value::Integer(43))
);
drop(writable);
let reopened = Neo4jStorage::open(&database).unwrap();
assert!(reopened.has_relationship_type(&new_relationship_type));
assert_eq!(
reopened
.query_relationships
.iter()
.filter(|relationship| relationship.r#type == new_relationship_type)
.count(),
2
);
assert!(reopened.query_relationships.iter().any(|relationship| {
relationship.r#type == new_relationship_type && relationship.start == relationship.end
}));
let alice = reopened
.query_nodes
.iter()
.find(|node| node.properties.get("name") == Some(&Value::String("Alice".into())))
.unwrap();
assert_eq!(alice.properties.get("age"), Some(&Value::Integer(43)));
assert_eq!(alice.properties.get("active"), None);
assert!(reopened
.query_relationships
.iter()
.any(
|relationship| relationship.properties.get("since") == Some(&Value::Integer(2026))
));
}
}