Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
[features]
default = [
# Storages
"sled-storage",
"csv-storage",
"sheet-storage",
"memory-storage",
"sled-database",
"csv-database",
"sheet-database",
"memory-database",
# Functionality
"alter-table",
"auto-increment",
Expand All @@ -25,10 +25,10 @@
]

# Storages
sled-storage = ["sled", "bincode"]
csv-storage = ["csv", "linecount"]
sheet-storage = ["umya-spreadsheet"]
memory-storage = []
sled-database = ["sled", "bincode"]
csv-database = ["csv", "linecount"]
sheet-database = ["umya-spreadsheet"]
memory-database = []

# Functionality
alter-table = []
Expand Down
4 changes: 2 additions & 2 deletions src/data/index.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {
crate::{result::Result, Column, Ingredient, Method, Recipe, StorageInner, Value},
crate::{result::Result, Column, DatabaseInner, Ingredient, Method, Recipe, Value},
rayon::prelude::*,
serde::{Deserialize, Serialize},
std::{cmp::Ordering, collections::HashMap},
Expand Down Expand Up @@ -30,7 +30,7 @@ impl Index {
}
pub async fn reset(
&self,
storage: &mut StorageInner,
storage: &mut DatabaseInner,
table: &str,
columns: &[Column],
) -> Result<()> {
Expand Down
4 changes: 2 additions & 2 deletions src/data/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl SchemaDiff {
changes.push(RenameTable(table_name.clone()))
}
if let Some(column_defs) = &self.column_defs {
for (index, column_def) in column_defs.into_iter() {
for (index, column_def) in column_defs.iter() {
match (index, column_def) {
(None, None) => (),
(Some(index), Some(column_def)) => {
Expand All @@ -164,7 +164,7 @@ impl SchemaDiff {
}
}
if let Some(indexes) = &self.indexes {
for (index, index_def) in indexes.into_iter() {
for (index, index_def) in indexes.iter() {
match (index, index_def) {
(None, None) => (),
(Some(index), Some(index_def)) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use {super::StorageError, crate::Result, async_trait::async_trait};
use {
crate::{DatabaseError, Result},
async_trait::async_trait,
};

#[async_trait(?Send)]
pub trait AutoIncrement {
Expand All @@ -16,7 +19,7 @@ pub trait AutoIncrement {
/*start_value*/ i64,
)>,
> {
Err(StorageError::Unimplemented.into())
Err(DatabaseError::Unimplemented.into())
}

async fn set_increment_value(
Expand All @@ -25,6 +28,6 @@ pub trait AutoIncrement {
_column_name: &str,
_end: i64,
) -> Result<()> {
Err(StorageError::Unimplemented.into())
Err(DatabaseError::Unimplemented.into())
}
}
14 changes: 7 additions & 7 deletions src/store/store.rs → src/database/base.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
use {
crate::{IndexFilter, Plane, Result, Schema, StorageError, Value},
crate::{DatabaseError, IndexFilter, Plane, Result, Schema, Value},
async_trait::async_trait,
};

/// `Store` -> `SELECT`
#[async_trait(?Send)]
pub trait Store {
pub trait DBBase {
async fn fetch_schema(&self, _table_name: &str) -> Result<Option<Schema>> {
Err(StorageError::Unimplemented.into())
Err(DatabaseError::Unimplemented.into())
}
async fn scan_schemas(&self) -> Result<Vec<Schema>> {
Err(StorageError::Unimplemented.into())
Err(DatabaseError::Unimplemented.into())
}

async fn scan_data(&self, _table_name: &str) -> Result<Plane> {
Err(StorageError::Unimplemented.into())
Err(DatabaseError::Unimplemented.into())
}

async fn scan_data_indexed(
&self,
_table_name: &str,
_index_filters: IndexFilter,
) -> Result<Plane> {
Err(StorageError::Unimplemented.into())
Err(DatabaseError::Unimplemented.into())
}
async fn scan_index(
&self,
_table_name: &str,
_index_filter: IndexFilter,
) -> Result<Vec<Value>> {
Err(StorageError::Unimplemented.into())
Err(DatabaseError::Unimplemented.into())
}
}
99 changes: 99 additions & 0 deletions src/database/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
mod auto_increment;
mod base;
mod mutable;

use std::sync::{Mutex, MutexGuard};
use {
crate::Result,
serde::{Deserialize, Serialize},
std::fmt::Debug,
thiserror::Error,
};

pub use {auto_increment::AutoIncrement, base::DBBase, mutable::DBMut};

#[derive(Error, Serialize, Debug, PartialEq)]
pub enum DatabaseError {
#[error("this database has not yet implemented this method")]
Unimplemented,
#[error("tried to connect to an unknown database")]
UnknownConnection,
#[error("table not found")]
TableNotFound,
}

#[derive(Serialize, Deserialize)]
pub enum Connection {
Unknown,
#[cfg(feature = "memory-database")]
Memory,
#[cfg(feature = "sled-database")]
Sled(String),
#[cfg(feature = "csv-database")]
CSV(String, crate::CSVSettings),
#[cfg(feature = "sheet-database")]
Sheet(String),
}
impl Default for Connection {
fn default() -> Self {
Connection::Unknown
}
}
impl TryFrom<Connection> for Database {
type Error = crate::Error;
fn try_from(connection: Connection) -> Result<Database> {
use {
crate::{CSVDatabase, MemoryDatabase, SheetDatabase, SledDatabase},
Connection::*,
};
let database: Mutex<Box<DatabaseInner>> = Mutex::new(match &connection {
#[cfg(feature = "memory-database")]
Memory => Box::new(MemoryDatabase::new()),
#[cfg(feature = "sled-database")]
Sled(path) => Box::new(SledDatabase::new(path)?),
#[cfg(feature = "csv-database")]
CSV(path, settings) => Box::new(CSVDatabase::new_with_settings(path, settings.clone())?),
#[cfg(feature = "sheet-database")]
Sheet(path) => Box::new(SheetDatabase::new(path)?),
Unknown => return Err(DatabaseError::UnknownConnection.into()),
});
Ok(Database {
database,
source_connection: connection,
})
}
}

pub struct Database {
source_connection: Connection,
database: Mutex<Box<DatabaseInner>>,
}
impl Database {
pub fn new(database: Box<DatabaseInner>) -> Self {
let database = Mutex::new(database);
Self {
database,
source_connection: Connection::default(),
}
}
pub fn get(&self) -> MutexGuard<Box<DatabaseInner>> {
self.database
.lock()
.expect("Unreachable: Database wasn't replaced!")
}
pub fn get_mut(&mut self) -> &mut Box<DatabaseInner> {
self.database
.get_mut()
.expect("Unreachable: Database wasn't replaced!")
}
pub fn into_source(self) -> Connection {
self.source_connection
}
pub fn from_source(connection: Connection) -> Result<Self> {
connection.try_into()
}
}

pub type DatabaseInner = dyn DBFull;

pub trait DBFull: DBBase + DBMut + AutoIncrement {}
18 changes: 9 additions & 9 deletions src/store/store_mut.rs → src/database/mutable.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
use {
crate::{Result, Row, Schema, SchemaDiff, StorageError, Value},
crate::{DatabaseError, Result, Row, Schema, SchemaDiff, Value},
async_trait::async_trait,
};

/// `StoreMut` -> `INSERT`, `CREATE`, `DELETE`, `DROP`, `UPDATE`
#[async_trait(?Send)]
pub trait StoreMut {
pub trait DBMut {
async fn insert_schema(&mut self, _schema: &Schema) -> Result<()> {
Err(StorageError::Unimplemented.into())
Err(DatabaseError::Unimplemented.into())
}

async fn delete_schema(&mut self, _table_name: &str) -> Result<()> {
Err(StorageError::Unimplemented.into())
Err(DatabaseError::Unimplemented.into())
} // Shouldn't this be AlterTable?

async fn insert_data(&mut self, _table_name: &str, _rows: Vec<Row>) -> Result<()> {
Err(StorageError::Unimplemented.into())
Err(DatabaseError::Unimplemented.into())
}

async fn update_data(&mut self, _table_name: &str, _rows: Vec<(Value, Row)>) -> Result<()> {
Err(StorageError::Unimplemented.into())
Err(DatabaseError::Unimplemented.into())
}

async fn delete_data(&mut self, _table_name: &str, _keys: Vec<Value>) -> Result<()> {
Err(StorageError::Unimplemented.into())
Err(DatabaseError::Unimplemented.into())
}

async fn update_index(
Expand All @@ -32,10 +32,10 @@ pub trait StoreMut {
_table_name: &str,
_keys: Vec<(Value, Value)>,
) -> Result<()> {
Err(StorageError::Unimplemented.into())
Err(DatabaseError::Unimplemented.into())
}

async fn alter_table(&mut self, _table_name: &str, _schema_diff: SchemaDiff) -> Result<()> {
Err(StorageError::Unimplemented.into())
Err(DatabaseError::Unimplemented.into())
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use {
super::CSVStorage,
super::CSVDatabase,
crate::{AutoIncrement, Result, WIPError},
async_trait::async_trait,
linecount::count_lines,
};

#[async_trait(?Send)]
impl AutoIncrement for CSVStorage {
impl AutoIncrement for CSVDatabase {
async fn generate_increment_values(
&mut self,
_table_name: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use {
super::{utils::csv_reader, CSVStorage},
crate::{Plane, Result, Row, Schema, Store, Value, WIPError},
super::{utils::csv_reader, CSVDatabase},
crate::{DBBase, Plane, Result, Row, Schema, Value, WIPError},
async_trait::async_trait,
};

#[async_trait(?Send)]
impl Store for CSVStorage {
impl DBBase for CSVDatabase {
async fn fetch_schema(&self, _table_name: &str) -> Result<Option<Schema>> {
Ok(self.schema.clone())
}
Expand Down
18 changes: 9 additions & 9 deletions src/storages/csv_storage/mod.rs → src/databases/csv/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
mod auto_increment;
mod store;
mod store_mut;
mod base;
mod mutable;
mod utils;

use {
crate::{data::Schema, Column, FullStorage, Result, Storage, ValueType, WIPError},
crate::{data::Schema, Column, DBFull, Database, Result, ValueType, WIPError},
csv::ReaderBuilder,
serde::{Deserialize, Serialize},
std::{
Expand All @@ -16,12 +16,12 @@ use {
};

#[derive(Error, Serialize, Debug, PartialEq)]
pub enum CSVStorageError {
pub enum CSVDatabaseError {
#[error("CSV storages only support one table at a time")]
OnlyOneTableAllowed,
}

pub struct CSVStorage {
pub struct CSVDatabase {
schema: Option<Schema>,
path: String,
pub csv_settings: CSVSettings,
Expand All @@ -40,14 +40,14 @@ impl Default for CSVSettings {
}
}

impl FullStorage for CSVStorage {}
impl DBFull for CSVDatabase {}

impl Storage {
pub fn new_csv(storage: CSVStorage) -> Self {
impl Database {
pub fn new_csv(storage: CSVDatabase) -> Self {
Self::new(Box::new(storage))
}
}
impl CSVStorage {
impl CSVDatabase {
pub fn new(path: &str) -> Result<Self> {
Self::new_with_settings(path, CSVSettings::default())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use {
super::{CSVStorage, CSVStorageError},
crate::{Cast, Result, Row, Schema, StoreMut, WIPError},
super::{CSVDatabase, CSVDatabaseError},
crate::{Cast, DBMut, Result, Row, Schema, WIPError},
async_trait::async_trait,
csv::WriterBuilder,
std::{fs::OpenOptions, io::Write},
};

#[async_trait(?Send)]
impl StoreMut for CSVStorage {
impl DBMut for CSVDatabase {
async fn insert_schema(&mut self, schema: &Schema) -> Result<()> {
if self.schema.is_some() {
return Err(CSVStorageError::OnlyOneTableAllowed.into());
return Err(CSVDatabaseError::OnlyOneTableAllowed.into());
}

let mut writer = WriterBuilder::new()
Expand Down
Loading