MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / observe_document_in_txn

Method observe_document_in_txn

nodedb/src/engine/sparse/stats.rs:277–324  ·  view source on GitHub ↗

Update statistics within an externally-owned write transaction. Opens the COLUMN_STATS table once and reads/writes all fields in a single table open, eliminating per-field transaction overhead.

(
        &self,
        txn: &WriteTransaction,
        tenant_id: u64,
        collection: &str,
        doc: &serde_json::Value,
    )

Source from the content-addressed store, hash-verified

275 /// Opens the COLUMN_STATS table once and reads/writes all fields in a
276 /// single table open, eliminating per-field transaction overhead.
277 pub fn observe_document_in_txn(
278 &self,
279 txn: &WriteTransaction,
280 tenant_id: u64,
281 collection: &str,
282 doc: &serde_json::Value,
283 ) -> crate::Result<()> {
284 let Some(obj) = doc.as_object() else {
285 return Ok(());
286 };
287 if obj.is_empty() {
288 return Ok(());
289 }
290
291 let mut table = txn
292 .open_table(COLUMN_STATS)
293 .map_err(|e| crate::Error::Storage {
294 engine: "stats".into(),
295 detail: format!("open table: {e}"),
296 })?;
297
298 for (field, value) in obj {
299 let key = format!("{tenant_id}:{collection}:{field}");
300
301 // Read existing stats from the same write transaction.
302 let mut stats: ColumnStats = table
303 .get(key.as_str())
304 .ok()
305 .flatten()
306 .and_then(|guard| zerompk::from_msgpack(guard.value()).ok())
307 .unwrap_or_default();
308
309 stats.observe(Some(value));
310
311 let bytes = zerompk::to_msgpack_vec(&stats).map_err(|e| crate::Error::Storage {
312 engine: "stats".into(),
313 detail: format!("serialize: {e}"),
314 })?;
315 table
316 .insert(key.as_str(), bytes.as_slice())
317 .map_err(|e| crate::Error::Storage {
318 engine: "stats".into(),
319 detail: format!("insert: {e}"),
320 })?;
321 }
322
323 Ok(())
324 }
325}
326
327#[cfg(test)]

Callers 1

apply_point_putMethod · 0.80

Calls 8

as_objectMethod · 0.80
is_emptyMethod · 0.45
okMethod · 0.45
getMethod · 0.45
as_strMethod · 0.45
observeMethod · 0.45
insertMethod · 0.45
as_sliceMethod · 0.45

Tested by

no test coverage detected