fraiseql_wire/stream/chunking/
mod.rs1use bytes::Bytes;
4
5pub struct RowChunk {
7 rows: Vec<Bytes>,
8}
9
10impl RowChunk {
11 #[must_use]
13 pub const fn new() -> Self {
14 Self { rows: Vec::new() }
15 }
16
17 #[must_use]
19 pub fn with_capacity(capacity: usize) -> Self {
20 Self {
21 rows: Vec::with_capacity(capacity),
22 }
23 }
24
25 pub fn push(&mut self, row: Bytes) {
27 self.rows.push(row);
28 }
29
30 #[must_use]
32 pub const fn is_empty(&self) -> bool {
33 self.rows.is_empty()
34 }
35
36 #[must_use]
38 pub const fn len(&self) -> usize {
39 self.rows.len()
40 }
41
42 #[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
55pub struct ChunkingStrategy {
57 chunk_size: usize,
58}
59
60impl ChunkingStrategy {
61 #[must_use]
63 pub const fn new(chunk_size: usize) -> Self {
64 Self { chunk_size }
65 }
66
67 #[must_use]
69 pub const fn is_full(&self, chunk: &RowChunk) -> bool {
70 chunk.len() >= self.chunk_size
71 }
72
73 #[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;