Skip to main content

fraiseql_wire/stream/chunking/
mod.rs

1//! Chunking logic for batching rows
2
3use bytes::Bytes;
4
5/// Row chunk (batch of raw JSON bytes)
6pub struct RowChunk {
7    rows: Vec<Bytes>,
8}
9
10impl RowChunk {
11    /// Create new chunk
12    #[must_use]
13    pub const fn new() -> Self {
14        Self { rows: Vec::new() }
15    }
16
17    /// Create with capacity
18    #[must_use]
19    pub fn with_capacity(capacity: usize) -> Self {
20        Self {
21            rows: Vec::with_capacity(capacity),
22        }
23    }
24
25    /// Add row to chunk
26    pub fn push(&mut self, row: Bytes) {
27        self.rows.push(row);
28    }
29
30    /// Check if chunk is empty
31    #[must_use]
32    pub const fn is_empty(&self) -> bool {
33        self.rows.is_empty()
34    }
35
36    /// Get chunk size
37    #[must_use]
38    pub const fn len(&self) -> usize {
39        self.rows.len()
40    }
41
42    /// Consume chunk and return rows
43    #[must_use]
44    pub fn into_rows(self) -> Vec<Bytes> {
45        self.rows
46    }
47}
48
49impl Default for RowChunk {
50    fn default() -> Self {
51        Self::new()
52    }
53}
54
55/// Chunking strategy
56pub struct ChunkingStrategy {
57    chunk_size: usize,
58}
59
60impl ChunkingStrategy {
61    /// Create new strategy with given chunk size
62    #[must_use]
63    pub const fn new(chunk_size: usize) -> Self {
64        Self { chunk_size }
65    }
66
67    /// Check if chunk is full
68    #[must_use]
69    pub const fn is_full(&self, chunk: &RowChunk) -> bool {
70        chunk.len() >= self.chunk_size
71    }
72
73    /// Create new chunk with appropriate capacity
74    #[must_use]
75    pub fn new_chunk(&self) -> RowChunk {
76        RowChunk::with_capacity(self.chunk_size)
77    }
78}
79
80impl Default for ChunkingStrategy {
81    fn default() -> Self {
82        Self::new(256)
83    }
84}
85
86#[cfg(test)]
87mod tests;