Skip to content

Commit

Permalink
fix: native_model
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-herlemont committed Dec 18, 2023
1 parent bcbe4f1 commit 61a97bd
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 35 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ 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.2" }
doc-comment = "0.3.3"

# 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"
Expand All @@ -35,6 +35,7 @@ tokio = { version = "1.33", features = ["test-util","macros"] }
bincode = { version = "2.0.0-rc.3", features = ["serde"] }
criterion = { version = "0.5.1" }


[features]
default = []

Expand Down
4 changes: 2 additions & 2 deletions native_db_macro/src/model_native_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ impl ModelNativeDB {
.collect::<Vec<_>>();

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,
}
Expand Down
4 changes: 2 additions & 2 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -321,6 +321,6 @@ impl DatabaseBuilder {
}

pub(crate) struct ModelBuilder {
pub(crate) model: Model,
pub(crate) model: DatabaseModel,
pub(crate) native_model_options: NativeModelOptions,
}
2 changes: 1 addition & 1 deletion src/db_type/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,12 @@ pub use model::*;
pub use native_db_macro::*;
pub use native_db_macro::*;
pub use serialization::*;

#[cfg(doctest)]
#[macro_use]
extern crate doc_comment;

#[cfg(doctest)]
doc_comment! {
include_str!("../README.md")
}
4 changes: 2 additions & 2 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use std::collections::HashSet;

/// Model of the Item. Returned by the [`<your_item>::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<DatabaseKeyDefinition<DatabaseSecondaryKeyOptions>>,
}

impl Model {
impl DatabaseModel {
pub fn check_secondary_options<F>(
&self,
secondary_key: &DatabaseKeyDefinition<DatabaseSecondaryKeyOptions>,
Expand Down
2 changes: 1 addition & 1 deletion src/table_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DatabaseKeyDefinition<DatabaseSecondaryKeyOptions>, SecondaryTableDefinition<'a>>,
Expand Down
12 changes: 6 additions & 6 deletions src/transaction/internal/private_readable_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -17,17 +17,17 @@ pub trait PrivateReadableTransaction<'db, 'txn> {

fn table_definitions(&self) -> &HashMap<String, PrimaryTableDefinition>;

fn get_primary_table(&'txn self, model: &Model) -> Result<Self::RedbPrimaryTable>;
fn get_primary_table(&'txn self, model: &DatabaseModel) -> Result<Self::RedbPrimaryTable>;

fn get_secondary_table(
&'txn self,
model: &Model,
model: &DatabaseModel,
secondary_key: &DatabaseKeyDefinition<DatabaseSecondaryKeyOptions>,
) -> Result<Self::RedbSecondaryTable>;

fn get_by_primary_key(
&'txn self,
model: Model,
model: DatabaseModel,
key: impl InnerKeyValue,
) -> Result<Option<DatabaseOutputValue>> {
let table = self.get_primary_table(&model)?;
Expand All @@ -38,7 +38,7 @@ pub trait PrivateReadableTransaction<'db, 'txn> {

fn get_by_secondary_key(
&'txn self,
model: Model,
model: DatabaseModel,
key_def: impl KeyDefinition<DatabaseSecondaryKeyOptions>,
key: impl InnerKeyValue,
) -> Result<Option<DatabaseOutputValue>> {
Expand All @@ -60,7 +60,7 @@ pub trait PrivateReadableTransaction<'db, 'txn> {
))
}

fn primary_len(&'txn self, model: Model) -> Result<u64> {
fn primary_len(&'txn self, model: DatabaseModel) -> Result<u64> {
let table = self.get_primary_table(&model)?;
let result = table.len()?;
Ok(result)
Expand Down
6 changes: 3 additions & 3 deletions src/transaction/internal/r_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -26,7 +26,7 @@ where
&self.table_definitions
}

fn get_primary_table(&'txn self, model: &Model) -> Result<Self::RedbPrimaryTable> {
fn get_primary_table(&'txn self, model: &DatabaseModel) -> Result<Self::RedbPrimaryTable> {
let table_definition = self
.table_definitions()
.get(model.primary_key.unique_table_name.as_str())
Expand All @@ -39,7 +39,7 @@ where

fn get_secondary_table(
&'txn self,
model: &Model,
model: &DatabaseModel,
secondary_key: &DatabaseKeyDefinition<DatabaseSecondaryKeyOptions>,
) -> Result<Self::RedbSecondaryTable> {
let main_table_definition = self
Expand Down
14 changes: 7 additions & 7 deletions src/transaction/internal/rw_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -30,7 +30,7 @@ where
&self.primary_table_definitions
}

fn get_primary_table(&'txn self, model: &Model) -> Result<Self::RedbPrimaryTable> {
fn get_primary_table(&'txn self, model: &DatabaseModel) -> Result<Self::RedbPrimaryTable> {
let table_definition = self
.table_definitions()
.get(model.primary_key.unique_table_name.as_str())
Expand All @@ -43,7 +43,7 @@ where

fn get_secondary_table(
&'txn self,
model: &Model,
model: &DatabaseModel,
secondary_key: &DatabaseKeyDefinition<DatabaseSecondaryKeyOptions>,
) -> Result<Self::RedbSecondaryTable> {
let main_table_definition = self
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)> {
Expand All @@ -164,7 +164,7 @@ impl<'db> InternalRwTransaction<'db> {

pub(crate) fn concrete_primary_drain<'a>(
&self,
model: Model,
model: DatabaseModel,
) -> Result<Vec<DatabaseOutputValue>> {
let mut items = vec![];
let mut key_items = HashSet::new();
Expand Down
9 changes: 0 additions & 9 deletions tests/skeptic.rs

This file was deleted.

0 comments on commit 61a97bd

Please sign in to comment.