use bytes::Bytes;
pub struct RowChunk {
rows: Vec<Bytes>,
}
impl RowChunk {
pub const fn new() -> Self {
Self { rows: Vec::new() }
}
pub fn with_capacity(capacity: usize) -> Self {
Self {
rows: Vec::with_capacity(capacity),
}
}
pub fn push(&mut self, row: Bytes) {
self.rows.push(row);
}
pub const fn is_empty(&self) -> bool {
self.rows.is_empty()
}
pub const fn len(&self) -> usize {
self.rows.len()
}
pub fn into_rows(self) -> Vec<Bytes> {
self.rows
}
}
impl Default for RowChunk {
fn default() -> Self {
Self::new()
}
}
pub struct ChunkingStrategy {
chunk_size: usize,
}
impl ChunkingStrategy {
pub const fn new(chunk_size: usize) -> Self {
Self { chunk_size }
}
pub const fn is_full(&self, chunk: &RowChunk) -> bool {
chunk.len() >= self.chunk_size
}
pub fn new_chunk(&self) -> RowChunk {
RowChunk::with_capacity(self.chunk_size)
}
}
impl Default for ChunkingStrategy {
fn default() -> Self {
Self::new(256)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_chunk_operations() {
let mut chunk = RowChunk::new();
assert!(chunk.is_empty());
chunk.push(Bytes::from_static(b"{}"));
assert_eq!(chunk.len(), 1);
assert!(!chunk.is_empty());
}
#[test]
fn test_chunking_strategy() {
let strategy = ChunkingStrategy::new(2);
let mut chunk = strategy.new_chunk();
assert!(!strategy.is_full(&chunk));
chunk.push(Bytes::from_static(b"{}"));
assert!(!strategy.is_full(&chunk));
chunk.push(Bytes::from_static(b"{}"));
assert!(strategy.is_full(&chunk));
}
}