use super::super::types::{ConstraintChangeOp, ReplicatedWrite};
use nodedb_physical::physical_plan::CrdtOp;
pub(super) fn encode(op: &CrdtOp) -> Option<ReplicatedWrite> {
Some(match op {
CrdtOp::Apply {
collection,
document_id,
delta,
peer_id,
mutation_id: _,
surrogate: _,
provenance,
constraint_version_required,
} => apply(
collection,
document_id,
delta,
*peer_id,
super::entry::encode_provenance(provenance),
*constraint_version_required,
),
CrdtOp::ImportSnapshot {
tenant_id,
collection,
bytes,
} => import_snapshot(*tenant_id, collection, bytes),
CrdtOp::ListInsert {
collection,
document_id,
list_path,
index,
fields_json,
surrogate: _,
} => list_insert(collection, document_id, list_path, *index, fields_json),
CrdtOp::ListDelete {
collection,
document_id,
list_path,
index,
surrogate: _,
} => list_delete(collection, document_id, list_path, *index),
CrdtOp::ListMove {
collection,
document_id,
list_path,
from_index,
to_index,
surrogate: _,
} => list_move(collection, document_id, list_path, *from_index, *to_index),
CrdtOp::DocUpsert {
collection,
document_id,
fields_json,
surrogate,
partial,
returning: _,
} => doc_upsert(
collection,
document_id,
surrogate.as_u32(),
fields_json,
*partial,
),
CrdtOp::DocDelete {
collection,
document_id,
surrogate,
returning: _,
} => doc_delete(collection, document_id, surrogate.as_u32()),
CrdtOp::SetConstraints {
collection,
constraint_version,
constraints,
} => set_constraints(collection, *constraint_version, constraints),
CrdtOp::DropConstraints {
collection,
constraint_version,
} => drop_constraints(collection, *constraint_version),
CrdtOp::Read { .. }
| CrdtOp::ReadConstraints { .. }
| CrdtOp::SetPolicy { .. }
| CrdtOp::GetPolicy { .. }
| CrdtOp::ReadAtVersion { .. }
| CrdtOp::GetVersionVector { .. }
| CrdtOp::ExportDelta { .. }
| CrdtOp::CompactAtVersion { .. }
| CrdtOp::RestoreToVersion { .. } => return None,
})
}
pub(super) fn set_constraints(
collection: &str,
constraint_version: u64,
constraints: &[Vec<u8>],
) -> ReplicatedWrite {
ReplicatedWrite::ConstraintChange {
collection: collection.to_owned(),
op: ConstraintChangeOp::Set,
constraint_version,
constraints: constraints.to_vec(),
}
}
pub(super) fn drop_constraints(collection: &str, constraint_version: u64) -> ReplicatedWrite {
ReplicatedWrite::ConstraintChange {
collection: collection.to_owned(),
op: ConstraintChangeOp::Drop,
constraint_version,
constraints: Vec::new(),
}
}
pub(super) fn apply(
collection: &str,
document_id: &str,
delta: &[u8],
peer_id: u64,
provenance: Option<Vec<u8>>,
constraint_version_required: u64,
) -> ReplicatedWrite {
ReplicatedWrite::CrdtApply {
collection: collection.to_owned(),
document_id: document_id.to_owned(),
delta: delta.to_vec(),
peer_id,
provenance,
constraint_version_required,
}
}
pub(super) fn import_snapshot(tenant_id: u64, collection: &str, bytes: &[u8]) -> ReplicatedWrite {
ReplicatedWrite::CrdtImportCollection {
tenant_id,
collection: collection.to_owned(),
bytes: bytes.to_vec(),
}
}
pub(super) fn list_insert(
collection: &str,
document_id: &str,
list_path: &str,
index: usize,
fields_json: &str,
) -> ReplicatedWrite {
ReplicatedWrite::CrdtListInsert {
collection: collection.to_owned(),
document_id: document_id.to_owned(),
list_path: list_path.to_owned(),
index: index as u64,
fields_json: fields_json.to_owned(),
}
}
pub(super) fn list_delete(
collection: &str,
document_id: &str,
list_path: &str,
index: usize,
) -> ReplicatedWrite {
ReplicatedWrite::CrdtListDelete {
collection: collection.to_owned(),
document_id: document_id.to_owned(),
list_path: list_path.to_owned(),
index: index as u64,
}
}
pub(super) fn list_move(
collection: &str,
document_id: &str,
list_path: &str,
from_index: usize,
to_index: usize,
) -> ReplicatedWrite {
ReplicatedWrite::CrdtListMove {
collection: collection.to_owned(),
document_id: document_id.to_owned(),
list_path: list_path.to_owned(),
from_index: from_index as u64,
to_index: to_index as u64,
}
}
pub(super) fn doc_upsert(
collection: &str,
document_id: &str,
surrogate: u32,
fields_json: &str,
partial: bool,
) -> ReplicatedWrite {
ReplicatedWrite::CrdtDocUpsert {
collection: collection.to_owned(),
document_id: document_id.to_owned(),
surrogate,
fields_json: fields_json.to_owned(),
partial,
}
}
pub(super) fn doc_delete(collection: &str, document_id: &str, surrogate: u32) -> ReplicatedWrite {
ReplicatedWrite::CrdtDocDelete {
collection: collection.to_owned(),
document_id: document_id.to_owned(),
surrogate,
}
}