diff --git a/Cargo.toml b/Cargo.toml index 7dd0075a..436141ef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,11 +20,10 @@ native_db_macro = { version = "0.4.3", path = "native_db_macro" } thiserror = "1.0" uuid = { version = "1", features = [ "v4"] } serde = { version = "1.0" } +native_model = { version = "0.4.0" } # Optional tokio support tokio = { version = "1", features = ["sync"], optional = true } -#native_model = { git = "https://github.com/vincent-herlemont/native_model.git", rev = "69d42f5ec9a4ad671525233e2835efeaae95862e" } -native_model = { path = "../native_model" } [dev-dependencies] assert_fs = "1.0" diff --git a/native_db_macro/src/model_native_db.rs b/native_db_macro/src/model_native_db.rs index 91caf83f..aa752dc5 100644 --- a/native_db_macro/src/model_native_db.rs +++ b/native_db_macro/src/model_native_db.rs @@ -100,10 +100,10 @@ impl ModelNativeDB { .collect::>(); quote! { - fn native_db_model() -> native_db::Model { + fn native_db_model() -> native_db::DatabaseModel { let mut secondary_tables_name = std::collections::HashSet::new(); #(#secondary_keys)* - native_db::Model { + native_db::DatabaseModel { primary_key: #primary_key, secondary_keys: secondary_tables_name, } diff --git a/src/builder.rs b/src/builder.rs index a424d3a4..0ecf4f34 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -1,6 +1,6 @@ use crate::db_type::Result; use crate::table_definition::NativeModelOptions; -use crate::{watch, Database, Input, Model}; +use crate::{watch, Database, DatabaseModel, Input}; use std::collections::HashMap; use std::path::Path; use std::sync::atomic::AtomicU64; @@ -321,6 +321,6 @@ impl DatabaseBuilder { } pub(crate) struct ModelBuilder { - pub(crate) model: Model, + pub(crate) model: DatabaseModel, pub(crate) native_model_options: NativeModelOptions, } diff --git a/src/db_type/input.rs b/src/db_type/input.rs index f629633c..9e484c1e 100644 --- a/src/db_type/input.rs +++ b/src/db_type/input.rs @@ -44,7 +44,7 @@ impl DatabaseInput { } pub trait Input: Sized + native_model::Model { - fn native_db_model() -> crate::Model; + fn native_db_model() -> crate::DatabaseModel; fn native_db_primary_key(&self) -> DatabaseInnerKeyValue; diff --git a/src/model.rs b/src/model.rs index 0b5ffd37..01380f91 100644 --- a/src/model.rs +++ b/src/model.rs @@ -3,12 +3,12 @@ use std::collections::HashSet; /// Model of the Item. Returned by the [`::native_db_model()`](crate::Input::native_db_model) method. #[derive(Clone, Debug)] -pub struct Model { +pub struct DatabaseModel { pub primary_key: DatabaseKeyDefinition<()>, pub secondary_keys: HashSet>, } -impl Model { +impl DatabaseModel { pub fn check_secondary_options( &self, secondary_key: &DatabaseKeyDefinition, diff --git a/src/table_definition.rs b/src/table_definition.rs index cc759931..7744ac73 100644 --- a/src/table_definition.rs +++ b/src/table_definition.rs @@ -9,7 +9,7 @@ pub(crate) type RedbSecondaryTableDefinition<'a> = redb::TableDefinition<'a, DatabaseInnerKeyValue, DatabaseInnerKeyValue>; pub struct PrimaryTableDefinition<'a> { - pub(crate) model: crate::Model, + pub(crate) model: crate::DatabaseModel, pub(crate) redb: RedbPrimaryTableDefinition<'a>, pub(crate) secondary_tables: HashMap, SecondaryTableDefinition<'a>>, diff --git a/src/transaction/internal/private_readable_transaction.rs b/src/transaction/internal/private_readable_transaction.rs index cea84097..468816f9 100644 --- a/src/transaction/internal/private_readable_transaction.rs +++ b/src/transaction/internal/private_readable_transaction.rs @@ -3,7 +3,7 @@ use crate::db_type::{ Error, InnerKeyValue, KeyDefinition, Result, }; use crate::table_definition::PrimaryTableDefinition; -use crate::Model; +use crate::DatabaseModel; use redb::ReadableTable; use std::collections::HashMap; @@ -17,17 +17,17 @@ pub trait PrivateReadableTransaction<'db, 'txn> { fn table_definitions(&self) -> &HashMap; - fn get_primary_table(&'txn self, model: &Model) -> Result; + fn get_primary_table(&'txn self, model: &DatabaseModel) -> Result; fn get_secondary_table( &'txn self, - model: &Model, + model: &DatabaseModel, secondary_key: &DatabaseKeyDefinition, ) -> Result; fn get_by_primary_key( &'txn self, - model: Model, + model: DatabaseModel, key: impl InnerKeyValue, ) -> Result> { let table = self.get_primary_table(&model)?; @@ -38,7 +38,7 @@ pub trait PrivateReadableTransaction<'db, 'txn> { fn get_by_secondary_key( &'txn self, - model: Model, + model: DatabaseModel, key_def: impl KeyDefinition, key: impl InnerKeyValue, ) -> Result> { @@ -60,7 +60,7 @@ pub trait PrivateReadableTransaction<'db, 'txn> { )) } - fn primary_len(&'txn self, model: Model) -> Result { + fn primary_len(&'txn self, model: DatabaseModel) -> Result { let table = self.get_primary_table(&model)?; let result = table.len()?; Ok(result) diff --git a/src/transaction/internal/r_transaction.rs b/src/transaction/internal/r_transaction.rs index 617087bd..ea3dfc94 100644 --- a/src/transaction/internal/r_transaction.rs +++ b/src/transaction/internal/r_transaction.rs @@ -3,7 +3,7 @@ use crate::db_type::{ }; use crate::table_definition::PrimaryTableDefinition; use crate::transaction::internal::private_readable_transaction::PrivateReadableTransaction; -use crate::Model; +use crate::DatabaseModel; use std::collections::HashMap; pub struct InternalRTransaction<'db> { @@ -26,7 +26,7 @@ where &self.table_definitions } - fn get_primary_table(&'txn self, model: &Model) -> Result { + fn get_primary_table(&'txn self, model: &DatabaseModel) -> Result { let table_definition = self .table_definitions() .get(model.primary_key.unique_table_name.as_str()) @@ -39,7 +39,7 @@ where fn get_secondary_table( &'txn self, - model: &Model, + model: &DatabaseModel, secondary_key: &DatabaseKeyDefinition, ) -> Result { let main_table_definition = self diff --git a/src/transaction/internal/rw_transaction.rs b/src/transaction/internal/rw_transaction.rs index 4a6a30ff..b10c8bb9 100644 --- a/src/transaction/internal/rw_transaction.rs +++ b/src/transaction/internal/rw_transaction.rs @@ -5,7 +5,7 @@ use crate::db_type::{ use crate::table_definition::PrimaryTableDefinition; use crate::transaction::internal::private_readable_transaction::PrivateReadableTransaction; use crate::watch::WatcherRequest; -use crate::{Input, Model}; +use crate::{DatabaseModel, Input}; use redb::ReadableTable; use redb::TableHandle; use std::collections::{HashMap, HashSet}; @@ -30,7 +30,7 @@ where &self.primary_table_definitions } - fn get_primary_table(&'txn self, model: &Model) -> Result { + fn get_primary_table(&'txn self, model: &DatabaseModel) -> Result { let table_definition = self .table_definitions() .get(model.primary_key.unique_table_name.as_str()) @@ -43,7 +43,7 @@ where fn get_secondary_table( &'txn self, - model: &Model, + model: &DatabaseModel, secondary_key: &DatabaseKeyDefinition, ) -> Result { let main_table_definition = self @@ -73,7 +73,7 @@ impl<'db> InternalRwTransaction<'db> { pub(crate) fn concrete_insert( &self, - model: Model, + model: DatabaseModel, item: DatabaseInput, ) -> Result<(WatcherRequest, DatabaseOutputValue)> { let already_exists; @@ -118,7 +118,7 @@ impl<'db> InternalRwTransaction<'db> { pub(crate) fn concrete_remove( &self, - model: Model, + model: DatabaseModel, item: DatabaseInput, ) -> Result<(WatcherRequest, DatabaseOutputValue)> { let keys = &item.secondary_keys; @@ -153,7 +153,7 @@ impl<'db> InternalRwTransaction<'db> { pub(crate) fn concrete_update( &self, - model: Model, + model: DatabaseModel, old_item: DatabaseInput, updated_item: DatabaseInput, ) -> Result<(WatcherRequest, DatabaseOutputValue, DatabaseOutputValue)> { @@ -164,7 +164,7 @@ impl<'db> InternalRwTransaction<'db> { pub(crate) fn concrete_primary_drain<'a>( &self, - model: Model, + model: DatabaseModel, ) -> Result> { let mut items = vec![]; let mut key_items = HashSet::new();