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

Method poll_write_batch

nodedb/src/data/executor/handlers/write_batch.rs:23–197  ·  view source on GitHub ↗

Batch-coalesce consecutive PointPut tasks from the front of the task queue. Opens ONE redb WriteTransaction, executes all PointPuts within it, commits once, and sends individual responses. This amortizes the fsync cost across N writes instead of paying it per-write. Returns the number of tasks processed (0 if the front of the queue is not a batchable PointPut, in which case the caller should fal

(&mut self)

Source from the content-addressed store, hash-verified

21 /// is not a batchable PointPut, in which case the caller should fall
22 /// back to `poll_one`).
23 pub fn poll_write_batch(&mut self) -> usize {
24 // Check if the front of the queue is a non-expired PointPut.
25 let front_is_put = self.task_queue.front().is_some_and(|t| {
26 matches!(
27 t.plan(),
28 PhysicalPlan::Document(DocumentOp::PointPut { .. })
29 ) && !t.is_expired()
30 });
31 if !front_is_put {
32 return 0;
33 }
34
35 // Collect consecutive non-expired PointPuts (max 64).
36 let mut batch: Vec<ExecutionTask> = Vec::with_capacity(64);
37 while batch.len() < 64 {
38 let is_put = self.task_queue.front().is_some_and(|t| {
39 matches!(
40 t.plan(),
41 PhysicalPlan::Document(DocumentOp::PointPut { .. })
42 ) && !t.is_expired()
43 });
44 if !is_put {
45 break;
46 }
47 if let Some(task) = self.task_queue.pop_front() {
48 batch.push(task);
49 } else {
50 break;
51 }
52 }
53
54 // Single write: no batching benefit, let poll_one handle it
55 // (poll_one also handles idempotency cache and other bookkeeping).
56 if batch.len() <= 1 {
57 for t in batch.into_iter().rev() {
58 self.task_queue.push_front(t);
59 }
60 return 0;
61 }
62
63 // Open ONE transaction for the entire batch.
64 let txn = match self.sparse.begin_write() {
65 Ok(t) => t,
66 Err(_) => {
67 // Can't open txn — put tasks back, let poll_one handle individually.
68 for t in batch.into_iter().rev() {
69 self.task_queue.push_front(t);
70 }
71 return 0;
72 }
73 };
74
75 // Execute each PointPut within the shared transaction.
76 // Track per-task success/failure for individual responses, and
77 // capture the prior stored bytes per row so the Event Plane emit
78 // below can resolve Insert vs Update from the actual mutation.
79 let mut results: Vec<Result<Option<Vec<u8>>, crate::bridge::envelope::Response>> =
80 Vec::with_capacity(batch.len());

Callers 1

tickMethod · 0.80

Calls 15

surrogate_to_doc_idFunction · 0.85
frontMethod · 0.80
pop_frontMethod · 0.80
push_frontMethod · 0.80
begin_writeMethod · 0.80
planMethod · 0.80
apply_point_putMethod · 0.80
response_errorMethod · 0.80
to_stringMethod · 0.80
try_pushMethod · 0.80
emit_put_eventMethod · 0.80
response_okMethod · 0.80

Tested by

no test coverage detected