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
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ performance_measurement_codegen = { path = "performance_measurement/codegen", ve
performance_measurement = { path = "performance_measurement", version = "0.1.0", optional = true }
indexset = { version = "0.11.2", features = ["concurrent", "cdc", "multimap"] }
convert_case = "0.6.0"
ordered-float = "5.0.0"
serde = { version = "1.0.215", features = ["derive"] }
5 changes: 3 additions & 2 deletions codegen/src/worktable/generator/index.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::name_generator::WorktableNameGenerator;
use crate::worktable::generator::Generator;

use crate::worktable::generator::queries::r#type::map_to_uppercase;
use proc_macro2::{Ident, Literal, Span, TokenStream};
use quote::quote;

Expand Down Expand Up @@ -188,7 +189,7 @@ impl Generator {

let match_arm = if let Some(t) = self.columns.columns_map.get(&idx.field) {
let type_str = t.to_string();
let variant_ident = Ident::new(&type_str.to_uppercase(), Span::mixed_site());
let variant_ident = Ident::new(&map_to_uppercase(&type_str), Span::mixed_site());

let (new_value_expr, old_value_expr) = if type_str == "String" {
(quote! { new.to_string() }, quote! { old.to_string() })
Expand Down Expand Up @@ -317,7 +318,7 @@ impl Generator {

let match_arm = if let Some(t) = self.columns.columns_map.get(&idx.field) {
let type_str = t.to_string();
let variant_ident = Ident::new(&type_str.to_uppercase(), Span::mixed_site());
let variant_ident = Ident::new(&map_to_uppercase(&type_str), Span::mixed_site());

let (new_value_expr, old_value_expr) = if type_str == "String" {
(quote! { new.to_string() }, quote! { old.to_string() })
Expand Down
51 changes: 43 additions & 8 deletions codegen/src/worktable/generator/queries/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ use quote::quote;
use crate::name_generator::WorktableNameGenerator;
use crate::worktable::generator::Generator;

pub fn map_to_uppercase(str: &str) -> String {
if str.contains("OrderedFloat") {
let mut split = str.split("<");
let _ = split.next();
let inner_type = split
.next()
.expect("OrderedFloat def contains inner type")
.replace(">", "");
format!("Ordered{}", inner_type.to_uppercase().trim())
} else {
str.to_uppercase()
}
}

impl Generator {
pub fn gen_available_types_def(&mut self) -> syn::Result<TokenStream> {
let name_generator = WorktableNameGenerator::from_table_name(self.name.to_string());
Expand All @@ -22,9 +36,12 @@ impl Generator {
let rows: Vec<_> = unique_types
.iter()
.map(|s| {
let type_ident = Ident::new(s.to_string().as_str(), Span::mixed_site());
let type_upper =
Ident::new(s.to_string().to_uppercase().as_str(), Span::mixed_site());
let type_ident: TokenStream = s
.to_string()
.parse()
.expect("should be valid because parsed from declaration");
let type_upper = map_to_uppercase(s);
let type_upper = Ident::new(type_upper.as_str(), Span::mixed_site());
Some(quote! {
#[from]
#type_upper(#type_ident),
Expand All @@ -34,8 +51,7 @@ impl Generator {

if !rows.is_empty() {
Ok(quote! {
#[derive(rkyv::Archive, Debug, derive_more::Display, rkyv::Deserialize, Clone, rkyv::Serialize)]
#[derive(From, PartialEq)]
#[derive(Clone, Debug, derive_more::Display, From, PartialEq)]
#[non_exhaustive]
pub enum #avt_type_ident {
#(#rows)*
Expand Down Expand Up @@ -68,9 +84,28 @@ impl Generator {
.get(i)
.ok_or(syn::Error::new(i.span(), "Unexpected column name"))?;

Ok::<_, syn::Error>(quote! {
pub #i: #type_,
})
let def = if type_.to_string().contains("OrderedFloat") {
let inner_type = type_.to_string();
let mut split = inner_type.split("<");
let _ = split.next();
let inner_type = split
.next()
.expect("OrderedFloat def contains inner type")
.to_uppercase()
.replace(">", "");
let ident = Ident::new(
format!("Ordered{}Def", inner_type.trim()).as_str(),
Span::call_site(),
);
quote! {
#[rkyv(with = #ident)]
pub #i: #type_,
}
} else {
quote! {pub #i: #type_,}
};

Ok::<_, syn::Error>(def)
})
.collect::<Result<Vec<_>, _>>()?;

Expand Down
21 changes: 20 additions & 1 deletion codegen/src/worktable/generator/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,26 @@ impl Generator {
.columns_map
.iter()
.map(|(name, type_)| {
quote! {pub #name: #type_,}
if type_.to_string().contains("OrderedFloat") {
let inner_type = type_.to_string();
let mut split = inner_type.split("<");
let _ = split.next();
let inner_type = split
.next()
.expect("OrderedFloat def contains inner type")
.to_uppercase()
.replace(">", "");
let ident = Ident::new(
format!("Ordered{}Def", inner_type.trim()).as_str(),
Span::call_site(),
);
quote! {
#[rkyv(with = #ident)]
pub #name: #type_,
}
} else {
quote! {pub #name: #type_,}
}
})
.collect();

Expand Down
9 changes: 9 additions & 0 deletions codegen/src/worktable/model/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ use proc_macro2::{Ident, TokenStream};
use quote::quote;
use syn::spanned::Spanned;

fn is_float(ident: &Ident) -> bool {
matches!(ident.to_string().as_str(), "f64" | "f32")
}

#[derive(Debug, Clone)]
pub struct Columns {
pub columns_map: HashMap<Ident, TokenStream>,
Expand All @@ -31,6 +35,11 @@ impl Columns {

for row in rows {
let type_ = &row.type_;
let type_ = if is_float(type_) {
quote! { ordered_float::OrderedFloat<#type_> }
} else {
quote! { #type_ }
};
let type_ = if row.optional {
quote! { core::option::Option<#type_> }
} else {
Expand Down
2 changes: 1 addition & 1 deletion examples/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn main() {
attr2: 345,
test: 1,
id: 0,
attr_float: 100.0,
attr_float: 100.0.into(),
attr_string: "String_attr".to_string(),
};

Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod row;
mod table;
pub use data_bucket;
mod persistence;
mod util;

// mod ty;
// mod value;
Expand All @@ -29,6 +30,7 @@ pub mod prelude {
};
pub use crate::primary_key::{PrimaryKeyGenerator, PrimaryKeyGeneratorState, TablePrimaryKey};
pub use crate::table::select::{Order, QueryParams, SelectQueryBuilder, SelectQueryExecutor};
pub use crate::util::{OrderedF32Def, OrderedF64Def};
pub use crate::{
lock::Lock, Difference, IndexMap, IndexMultiMap, TableIndex, TableIndexCdc, TableRow,
TableSecondaryIndex, TableSecondaryIndexCdc, WorkTable, WorkTableError,
Expand Down
3 changes: 3 additions & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod ordered_float;

pub use ordered_float::{OrderedF32Def, OrderedF64Def};
29 changes: 29 additions & 0 deletions src/util/ordered_float.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use rkyv::{Archive, Deserialize, Serialize};

#[derive(Archive, Serialize, Deserialize)]
#[rkyv(remote = ordered_float::OrderedFloat<f64>, archived = ArchivedF64)]
#[rkyv(derive(Debug))]
pub struct OrderedF64Def {
#[rkyv(getter = std::ops::Deref::deref)]
value: f64,
}

impl From<OrderedF64Def> for ordered_float::OrderedFloat<f64> {
fn from(value: OrderedF64Def) -> Self {
ordered_float::OrderedFloat(value.value)
}
}

#[derive(Archive, Serialize, Deserialize)]
#[rkyv(remote = ordered_float::OrderedFloat<f32>, archived = ArchivedF32)]
#[rkyv(derive(Debug))]
pub struct OrderedF32Def {
#[rkyv(getter = std::ops::Deref::deref)]
value: f32,
}

impl From<OrderedF32Def> for ordered_float::OrderedFloat<f32> {
fn from(value: OrderedF32Def) -> Self {
ordered_float::OrderedFloat(value.value)
}
}
20 changes: 10 additions & 10 deletions tests/persistence/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn test_space_insert_sync() {
let row = TestSyncRow {
another: 42,
non_unique: 0,
field: 0.234,
field: 0.234.into(),
id: table.get_next_pk().0,
};
table.insert(row.clone()).unwrap();
Expand Down Expand Up @@ -91,7 +91,7 @@ fn test_space_insert_many_sync() {
let row = TestSyncRow {
another: i,
non_unique: (i % 4) as u32,
field: i as f64 / 100.0,
field: (i as f64 / 100.0).into(),
id: table.get_next_pk().0,
};
table.insert(row.clone()).unwrap();
Expand Down Expand Up @@ -135,15 +135,15 @@ fn test_space_update_full_sync() {
let row = TestSyncRow {
another: 42,
non_unique: 0,
field: 0.0,
field: 0.0.into(),
id: table.get_next_pk().0,
};
table.insert(row.clone()).unwrap();
table
.update(TestSyncRow {
another: 13,
non_unique: 0,
field: 0.0,
field: 0.0.into(),
id: row.id,
})
.await
Expand Down Expand Up @@ -184,7 +184,7 @@ fn test_space_update_query_pk_sync() {
let row = TestSyncRow {
another: 42,
non_unique: 0,
field: 0.0,
field: 0.0.into(),
id: table.get_next_pk().0,
};
table.insert(row.clone()).unwrap();
Expand Down Expand Up @@ -228,12 +228,12 @@ fn test_space_update_query_unique_sync() {
let row = TestSyncRow {
another: 42,
non_unique: 0,
field: 0.0,
field: 0.0.into(),
id: table.get_next_pk().0,
};
table.insert(row.clone()).unwrap();
table
.update_field_by_another(FieldByAnotherQuery { field: 1.0 }, 42)
.update_field_by_another(FieldByAnotherQuery { field: 1.0.into() }, 42)
.await
.unwrap();
table.wait_for_ops().await;
Expand Down Expand Up @@ -272,7 +272,7 @@ fn test_space_update_query_non_unique_sync() {
let row = TestSyncRow {
another: 42,
non_unique: 10,
field: 0.0,
field: 0.0.into(),
id: table.get_next_pk().0,
};
table.insert(row.clone()).unwrap();
Expand Down Expand Up @@ -313,7 +313,7 @@ fn test_space_delete_sync() {
let row = TestSyncRow {
another: 42,
non_unique: 0,
field: 0.0,
field: 0.0.into(),
id: table.get_next_pk().0,
};
table.insert(row.clone()).unwrap();
Expand Down Expand Up @@ -353,7 +353,7 @@ fn test_space_delete_query_sync() {
let row = TestSyncRow {
another: 42,
non_unique: 0,
field: 0.0,
field: 0.0.into(),
id: table.get_next_pk().0,
};
table.insert(row.clone()).unwrap();
Expand Down
50 changes: 0 additions & 50 deletions tests/worktable/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,6 @@ worktable! (
}
);

worktable! (
name: TestFloat,
columns: {
id: u64 primary_key autoincrement,
test: i64,
another: f64,
exchange: String
},
indexes: {
test_idx: test unique,
exchnage_idx: exchange,
}
);

#[test]
fn table_name() {
let table = TestWorkTable::default();
Expand Down Expand Up @@ -659,42 +645,6 @@ fn select_all_where_by_eq_string_test() {
assert_eq!(equal.len(), 1);
}

#[test]
fn select_all_range_float_test() {
let table = TestFloatWorkTable::default();

let row1 = TestFloatRow {
id: table.get_next_pk().into(),
test: 3,
another: 100.0,
exchange: "M".to_string(),
};
let row2 = TestFloatRow {
id: table.get_next_pk().into(),
test: 1,
another: 200.0,
exchange: "N".to_string(),
};
let row3 = TestFloatRow {
id: table.get_next_pk().into(),
test: 2,
another: 300.0,
exchange: "P".to_string(),
};

let _ = table.insert(row1.clone()).unwrap();
let _ = table.insert(row2.clone()).unwrap();
let _ = table.insert(row3.clone()).unwrap();

let all = table
.select_all()
.where_by(|row| row.another > 99.0 && row.another < 300.0)
.execute()
.unwrap();

assert_eq!(all.len(), 2);
}

#[test]
fn select_all_where_by_contains_string_test() {
let table = TestWorkTable::default();
Expand Down
Loading
Loading