pub mod memory;
pub mod mmap;
use crate::table::Row;
#[derive(Debug)]
pub enum Storage {
Memory(memory::MemoryStorage),
#[allow(dead_code)]
Mmap(mmap::MmapStorage),
}
impl Storage {
pub fn new_memory() -> Self {
Storage::Memory(memory::MemoryStorage::new())
}
#[allow(dead_code)]
pub fn with_rows(rows: Vec<Row>) -> Self {
Storage::Memory(memory::MemoryStorage::with_rows(rows))
}
pub fn row_count(&self) -> usize {
match self {
Storage::Memory(m) => m.row_count(),
Storage::Mmap(m) => m.row_count(),
}
}
pub fn rows(&self) -> &[Row] {
match self {
Storage::Memory(m) => m.rows(),
Storage::Mmap(m) => m.rows(),
}
}
pub fn rows_mut(&mut self) -> Option<&mut Vec<Row>> {
match self {
Storage::Memory(m) => Some(m.rows_mut()),
Storage::Mmap(_) => None, }
}
pub fn push_row(&mut self, row: Row) {
match self {
Storage::Memory(m) => m.push_row(row),
Storage::Mmap(_) => panic!("Cannot add rows to read-only mmap storage"),
}
}
#[allow(dead_code)]
pub fn is_mutable(&self) -> bool {
match self {
Storage::Memory(_) => true,
Storage::Mmap(_) => false,
}
}
pub fn replace_rows(&mut self, rows: Vec<Row>) {
match self {
Storage::Memory(m) => m.replace_rows(rows),
Storage::Mmap(_) => panic!("Cannot replace rows in read-only mmap storage"),
}
}
pub fn ensure_mutable(&mut self) {
use crate::table::Value;
use std::borrow::Cow;
match self {
Storage::Memory(_) => {
}
Storage::Mmap(mmap) => {
let rows: Vec<Row> = mmap
.rows()
.iter()
.map(|row| {
row.iter()
.map(|value| match value {
Value::String(cow) => {
Value::String(Cow::Owned(cow.to_string()))
}
v => v.clone(),
})
.collect()
})
.collect();
*self = Storage::Memory(memory::MemoryStorage::with_rows(rows));
}
}
}
#[allow(dead_code)]
pub fn columns(&self) -> Option<&[String]> {
match self {
Storage::Memory(_) => None,
Storage::Mmap(m) => Some(m.columns()),
}
}
}