use tracing::warn;
use nodedb_types::Surrogate;
use crate::data::executor::core_loop::CoreLoop;
use crate::data::executor::handlers::point::apply_put::PointPutParams;
use crate::data::executor::task::ExecutionTask;
use crate::engine::crdt::tenant_state::TenantCrdtEngine;
use crate::engine::document::crdt_store::loro_value_to_json;
use crate::engine::document::store::surrogate_to_doc_id;
impl CoreLoop {
pub(super) fn encode_crdt_row(
engine: &TenantCrdtEngine,
collection: &str,
document_id: &str,
) -> Option<Vec<u8>> {
let loro_val = engine.read_row(collection, document_id)?;
let json = loro_value_to_json(&loro_val);
nodedb_types::json_to_msgpack(&json).ok()
}
pub(super) fn materialize_synced_document(
&mut self,
task: &ExecutionTask,
tid: u64,
collection: &str,
surrogate: Surrogate,
value: &[u8],
) {
self.materialize_document_write(task, tid, collection, surrogate, value, false);
}
pub(super) fn materialize_document_write(
&mut self,
task: &ExecutionTask,
tid: u64,
collection: &str,
surrogate: Surrogate,
value: &[u8],
index_text: bool,
) {
let database_id = task.request.database_id.as_u64();
let storage_key = surrogate_to_doc_id(surrogate);
let txn = match self.sparse.begin_write() {
Ok(t) => t,
Err(e) => {
warn!(core = self.core_id, %collection, error = %e, "crdt sync materialize: begin_write failed");
return;
}
};
let prior = match self.apply_point_put(
&txn,
PointPutParams {
database_id,
tid,
collection,
document_id: storage_key.as_str(),
surrogate,
value,
index_text,
user_roles: &task.request.user_roles,
enforce: false,
wal_lsn: task.wal_lsn(),
},
) {
Ok(p) => p,
Err(e) => {
warn!(
core = self.core_id,
%collection,
document_id = %storage_key,
error = %e,
"crdt sync materialize into sparse document store failed"
);
return;
}
};
if let Err(e) = txn.commit() {
warn!(core = self.core_id, %collection, error = %e, "crdt sync materialize: commit failed");
return;
}
self.checkpoint_coordinator.mark_dirty("sparse", 1);
self.emit_put_event(
task,
tid,
collection,
storage_key.as_str(),
value,
prior.prior_value.as_deref(),
);
}
}