Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-herlemont committed May 20, 2024
1 parent 4ffb739 commit f85ec22
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 72 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ chrono = { version = "0.4", features = ["serde"] }
rand = "0.8"
once_cell = "1.19"
dinghy-test = "0.7.1"
itertools = "0.12.1"

[features]
default = [ "upgrade_0_5_x" ]
Expand Down
3 changes: 3 additions & 0 deletions src/db_type/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,7 @@ pub enum Error {

#[error("You can not migrate the table {0} because it is a legacy model")]
MigrateLegacyModel(String),

#[error("Model migration error")]
ModelMigrationError(#[from] native_model::Error),
}
10 changes: 5 additions & 5 deletions src/serialization.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
pub fn bincode_encode_to_vec<T>(value: &T) -> Option<Vec<u8>>
pub fn bincode_encode_to_vec<T>(value: &T) -> crate::db_type::Result<Vec<u8>>
where
T: serde::Serialize + native_model::Model,
{
native_model::encode(value).ok()
native_model::encode(value).map_err(|e| e.into())
}

pub fn bincode_decode_from_slice<T>(slice: &[u8]) -> Option<(T, usize)>
pub fn bincode_decode_from_slice<T>(slice: &[u8]) -> crate::db_type::Result<(T, usize)>
where
T: serde::de::DeserializeOwned + native_model::Model,
{
let (data, _) = native_model::decode(slice.to_vec()).ok()?;
Some((data, 0))
let (data, _) = native_model::decode(slice.to_vec())?;
Ok((data, 0))
}
6 changes: 4 additions & 2 deletions src/transaction/query/drain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ pub struct RwDrain<'db, 'txn> {
}

impl<'db, 'txn> RwDrain<'db, 'txn> {
pub fn primary<T: Input>(&self) -> Result<Vec<Result<T>>> {
// TODO: Remove nested Result
pub fn primary<T: Input>(&self) -> Result<Vec<T>> {
let model = T::native_db_model();
let out = self.internal.concrete_primary_drain(model)?;
Ok(out.into_iter().map(|b| b.inner()).collect())
let out = out.into_iter().map(|b| b.inner()).collect::<Result<Vec<T>>>()?;
Ok(out)
}

/// **TODO: needs to be implemented**
Expand Down
32 changes: 24 additions & 8 deletions src/transaction/query/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ impl RGet<'_, '_> {
/// Ok(())
/// }
/// ```
pub fn primary<T: Input>(&self, key: impl InnerKeyValue) -> Result<Option<Result<T>>> {
pub fn primary<T: Input>(&self, key: impl InnerKeyValue) -> Result<Option<T>> {
let model = T::native_db_model();
let result = self.internal.get_by_primary_key(model, key)?;
Ok(result.map(|value| value.inner()))
if let Some(value) = result {
Ok(Some(value.inner()?))
} else {
Ok(None)
}
}

/// Get a value from the database by secondary key.
Expand Down Expand Up @@ -84,10 +88,14 @@ impl RGet<'_, '_> {
&self,
key_def: impl KeyDefinition<DatabaseSecondaryKeyOptions>,
key: impl InnerKeyValue,
) -> Result<Option<Result<T>>> {
) -> Result<Option<T>> {
let model = T::native_db_model();
let result = self.internal.get_by_secondary_key(model, key_def, key)?;
Ok(result.map(|value| value.inner()))
if let Some(value) = result {
Ok(Some(value.inner()?))
} else {
Ok(None)
}
}
}

Expand All @@ -99,10 +107,14 @@ impl RwGet<'_, '_> {
/// Get a value from the database by primary key.
///
/// Same as [`RGet::primary()`](struct.RGet.html#method.primary).
pub fn primary<T: Input>(&self, key: impl InnerKeyValue) -> Result<Option<Result<T>>> {
pub fn primary<T: Input>(&self, key: impl InnerKeyValue) -> Result<Option<T>> {
let model = T::native_db_model();
let result = self.internal.get_by_primary_key(model, key)?;
Ok(result.map(|value| value.inner()))
if let Some(value) = result {
Ok(Some(value.inner()?))
} else {
Ok(None)
}
}

/// Get a value from the database by secondary key.
Expand All @@ -112,9 +124,13 @@ impl RwGet<'_, '_> {
&self,
key_def: impl KeyDefinition<DatabaseSecondaryKeyOptions>,
key: impl InnerKeyValue,
) -> Result<Option<Result<T>>> {
) -> Result<Option<T>> {
let model = T::native_db_model();
let result = self.internal.get_by_secondary_key(model, key_def, key)?;
Ok(result.map(|value| value.inner()))
if let Some(value) = result {
Ok(Some(value.inner()?))
} else {
Ok(None)
}
}
}
9 changes: 6 additions & 3 deletions src/transaction/query/scan/primary_scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ where
/// use native_db::*;
/// use native_model::{native_model, Model};
/// use serde::{Deserialize, Serialize};
/// use itertools::Itertools;
///
/// #[derive(Serialize, Deserialize)]
/// #[native_model(id=1, version=1)]
Expand All @@ -48,7 +49,7 @@ where
/// let r = db.r_transaction()?;
///
/// // Get all values
/// let _values: Vec<Data> = r.scan().primary()?.all().collect();
/// let _values: Vec<Data> = r.scan().primary()?.all().try_collect()?;
/// Ok(())
/// }
/// ```
Expand All @@ -70,6 +71,7 @@ where
/// use native_db::*;
/// use native_model::{native_model, Model};
/// use serde::{Deserialize, Serialize};
/// use itertools::Itertools;
///
/// #[derive(Serialize, Deserialize)]
/// #[native_model(id=1, version=1)]
Expand All @@ -88,7 +90,7 @@ where
/// let r = db.r_transaction()?;
///
/// // Get the values from 5 to the end
/// let _values: Vec<Data> = r.scan().primary()?.range(5u64..).collect();
/// let _values: Vec<Data> = r.scan().primary()?.range(5u64..).try_collect()?;
/// Ok(())
/// }
/// ```
Expand All @@ -111,6 +113,7 @@ where
/// use native_db::*;
/// use native_model::{native_model, Model};
/// use serde::{Deserialize, Serialize};
/// use itertools::Itertools;
///
/// #[derive(Serialize, Deserialize)]
/// #[native_model(id=1, version=1)]
Expand All @@ -129,7 +132,7 @@ where
/// let r = db.r_transaction()?;
///
/// // Get the values starting with "victor"
/// let _values: Vec<Data> = r.scan().primary()?.start_with("victor").collect();
/// let _values: Vec<Data> = r.scan().primary()?.start_with("victor").try_collect()?;
/// Ok(())
/// }
/// ```
Expand Down
9 changes: 6 additions & 3 deletions src/transaction/query/scan/secondary_scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ where
/// use native_db::*;
/// use native_model::{native_model, Model};
/// use serde::{Deserialize, Serialize};
/// use itertools::Itertools;
///
/// #[derive(Serialize, Deserialize)]
/// #[native_model(id=1, version=1)]
Expand All @@ -60,7 +61,7 @@ where
/// let r = db.r_transaction()?;
///
/// // Get only values that have the secondary key set (name is not None)
/// let _values: Vec<Data> = r.scan().secondary(DataKey::name)?.all().collect();
/// let _values: Vec<Data> = r.scan().secondary(DataKey::name)?.all().try_collect()?;
/// Ok(())
/// }
/// ```
Expand All @@ -85,6 +86,7 @@ where
/// use native_db::*;
/// use native_model::{native_model, Model};
/// use serde::{Deserialize, Serialize};
/// use itertools::Itertools;
///
/// #[derive(Serialize, Deserialize)]
/// #[native_model(id=1, version=1)]
Expand All @@ -105,7 +107,7 @@ where
/// let r = db.r_transaction()?;
///
/// // Get only values that have the secondary key name from C to the end
/// let _values: Vec<Data> = r.scan().secondary(DataKey::name)?.range("C"..).collect();
/// let _values: Vec<Data> = r.scan().secondary(DataKey::name)?.range("C"..).try_collect()?;
/// Ok(())
/// }
/// ```
Expand Down Expand Up @@ -134,6 +136,7 @@ where
/// use native_db::*;
/// use native_model::{native_model, Model};
/// use serde::{Deserialize, Serialize};
/// use itertools::Itertools;
///
/// #[derive(Serialize, Deserialize)]
/// #[native_model(id=1, version=1)]
Expand All @@ -154,7 +157,7 @@ where
/// let r = db.r_transaction()?;
///
/// // Get only values that have the secondary key name starting with "hello"
/// let _values: Vec<Data> = r.scan().secondary(DataKey::name)?.start_with("hello").collect();
/// let _values: Vec<Data> = r.scan().secondary(DataKey::name)?.start_with("hello").try_collect()?;
/// Ok(())
/// }
/// ```
Expand Down
Binary file modified tests/data/db_0_6_0
Binary file not shown.
18 changes: 9 additions & 9 deletions tests/deserialization_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use native_db::*;

use native_model::{native_model, Model};
use serde::{Deserialize, Serialize};
use itertools::Itertools;

#[derive(Serialize, Deserialize, Eq, PartialEq, Clone, Debug)]
#[native_model(id = 1, version = 1)]
#[native_db]
Expand Down Expand Up @@ -32,14 +34,12 @@ fn create_local_database_for_tests() {
let root_project_path = env!("CARGO_MANIFEST_DIR");
let database_path: String = format!("{}/tests/data/db_0_6_0", root_project_path);


println!("database_path: {}", database_path);

let mut builder = DatabaseBuilder::new();
builder.define::<Item1>().unwrap();
builder.define::<Item2>().unwrap();
let db = builder.open(&database_path).unwrap();
let r = db.r_transaction().unwrap();
let data: Vec<Item1> = r.scan().primary().unwrap().all().collect();
assert_eq!(data.len(), 1);
// let mut builder = DatabaseBuilder::new();
// builder.define::<Item1>().unwrap();
// builder.define::<Item2>().unwrap();
// let db = builder.open(&database_path).unwrap();
// let r = db.r_transaction().unwrap();
// let data: Vec<Item1> = r.scan().primary().unwrap().all().try_collect().unwrap();
// assert_eq!(data.len(), 1);
}
13 changes: 7 additions & 6 deletions tests/migrate/with_secondary_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};
use shortcut_assert_fs::TmpFs;
use std::convert::TryFrom;
use std::convert::TryInto;
use native_db::db_type::Result;

#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)]
#[native_model(id = 1, version = 1)]
Expand Down Expand Up @@ -155,12 +156,12 @@ fn test_migrate() {
);

// Get Alexandre Dumas by first name
let item: Vec<ItemV2> = r_txn
let item = r_txn
.scan()
.secondary(ItemV2Key::first_name_key)
.unwrap()
.start_with("Alexandre")
.collect();
.collect::<Result<Vec<ItemV2>>>().unwrap();
assert_eq!(
item,
vec![ItemV2 {
Expand All @@ -171,12 +172,12 @@ fn test_migrate() {
);

// Get Julien Verne by last name
let item: Vec<ItemV2> = r_txn
let item = r_txn
.scan()
.secondary(ItemV2Key::last_name_key)
.unwrap()
.start_with("Verne")
.collect();
.collect::<Result<Vec<ItemV2>>>().unwrap();
assert_eq!(
item,
vec![ItemV2 {
Expand All @@ -200,7 +201,7 @@ struct ItemV3 {

impl TryFrom<ItemV3> for ItemV2 {
type Error = db_type::Error;
fn try_from(item: ItemV3) -> Result<Self, Self::Error> {
fn try_from(item: ItemV3) -> std::result::Result<Self, Self::Error> {
Ok(ItemV2 {
id: item.id,
first_name: item.first_name,
Expand All @@ -211,7 +212,7 @@ impl TryFrom<ItemV3> for ItemV2 {

impl TryFrom<ItemV2> for ItemV3 {
type Error = db_type::Error;
fn try_from(item: ItemV2) -> Result<Self, Self::Error> {
fn try_from(item: ItemV2) -> std::result::Result<Self, Self::Error> {
Ok(ItemV3 {
id: item.id,
first_name: item.first_name,
Expand Down
Loading

0 comments on commit f85ec22

Please sign in to comment.