Skip to main content

reifydb_engine/bulk_insert/primitive/
ringbuffer.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_type::params::Params;
5
6use crate::bulk_insert::builder::{BulkInsertBuilder, ValidationMode};
7
8/// Buffered ring buffer insert operation
9#[derive(Debug, Clone)]
10pub struct PendingRingBufferInsert {
11	pub namespace: String,
12	pub ringbuffer: String,
13	pub rows: Vec<Params>,
14}
15
16impl PendingRingBufferInsert {
17	pub fn new(namespace: String, ringbuffer: String) -> Self {
18		Self {
19			namespace,
20			ringbuffer,
21			rows: Vec::new(),
22		}
23	}
24
25	pub fn add_row(&mut self, params: Params) {
26		self.rows.push(params);
27	}
28
29	pub fn add_rows<I: IntoIterator<Item = Params>>(&mut self, iter: I) {
30		self.rows.extend(iter);
31	}
32}
33
34/// Builder for inserting rows into a specific ring buffer.
35///
36/// Created by calling `ringbuffer()` on a `BulkInsertBuilder`.
37/// Call `done()` to finish and return to the main builder.
38pub struct RingBufferInsertBuilder<'a, 'e, V: ValidationMode> {
39	parent: &'a mut BulkInsertBuilder<'e, V>,
40	pending: PendingRingBufferInsert,
41}
42
43impl<'a, 'e, V: ValidationMode> RingBufferInsertBuilder<'a, 'e, V> {
44	/// Create a new ring buffer insert builder.
45	pub(crate) fn new(parent: &'a mut BulkInsertBuilder<'e, V>, namespace: String, ringbuffer: String) -> Self {
46		Self {
47			parent,
48			pending: PendingRingBufferInsert::new(namespace, ringbuffer),
49		}
50	}
51
52	/// Add a single row from named params.
53	///
54	/// # Example
55	///
56	/// ```ignore
57	/// builder.row(params!{ timestamp: 12345, event_type: "login" })
58	/// ```
59	pub fn row(mut self, params: Params) -> Self {
60		self.pending.add_row(params);
61		self
62	}
63
64	/// Add multiple rows from an iterator.
65	///
66	/// # Example
67	///
68	/// ```ignore
69	/// let rows = vec![
70	///     params!{ timestamp: 12345, event_type: "login" },
71	///     params!{ timestamp: 12346, event_type: "logout" },
72	/// ];
73	/// builder.rows(rows)
74	/// ```
75	pub fn rows<I>(mut self, iter: I) -> Self
76	where
77		I: IntoIterator<Item = Params>,
78	{
79		self.pending.add_rows(iter);
80		self
81	}
82
83	/// Finish this ring buffer insert and return to the main builder.
84	///
85	/// This allows chaining to insert into additional targets.
86	pub fn done(self) -> &'a mut BulkInsertBuilder<'e, V> {
87		self.parent.add_ringbuffer_insert(self.pending);
88		self.parent
89	}
90}