Push a row into the collector. Returns `Some(TriggerBatch)` if the push completes a batch (hit batch_size) or if the new row targets a different collection (flushes the old batch first).
(
&mut self,
collection: &str,
operation: &str,
tenant_id: u64,
row: TriggerBatchRow,
)
| 251 | /// Returns `Some(TriggerBatch)` if the push completes a batch (hit batch_size) |
| 252 | /// or if the new row targets a different collection (flushes the old batch first). |
| 253 | pub fn push( |
| 254 | &mut self, |
| 255 | collection: &str, |
| 256 | operation: &str, |
| 257 | tenant_id: u64, |
| 258 | row: TriggerBatchRow, |
| 259 | ) -> Option<TriggerBatch> { |
| 260 | // If the pending batch targets a different collection/operation, flush it first. |
| 261 | let flushed = if let Some(ref pending) = self.pending { |
| 262 | if pending.collection != collection || pending.operation != operation { |
| 263 | self.flush() |
| 264 | } else { |
| 265 | None |
| 266 | } |
| 267 | } else { |
| 268 | None |
| 269 | }; |
| 270 | |
| 271 | // Start new batch if needed. |
| 272 | if self.pending.is_none() { |
| 273 | self.pending = Some(PendingBatch { |
| 274 | collection: collection.to_string(), |
| 275 | operation: operation.to_string(), |
| 276 | tenant_id, |
| 277 | rows: Vec::with_capacity(self.batch_size), |
| 278 | }); |
| 279 | } |
| 280 | |
| 281 | // Add the row. |
| 282 | if let Some(ref mut pending) = self.pending { |
| 283 | pending.rows.push(row); |
| 284 | |
| 285 | // If batch is full, flush it. |
| 286 | if pending.rows.len() >= self.batch_size { |
| 287 | let batch = self.flush(); |
| 288 | // If we already flushed a different-collection batch, return that. |
| 289 | // The full batch will be returned on the next call or flush. |
| 290 | return flushed.or(batch); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | flushed |
| 295 | } |
| 296 | |
| 297 | /// Flush the pending batch, returning it if non-empty. |
| 298 | pub fn flush(&mut self) -> Option<TriggerBatch> { |