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
4 changes: 3 additions & 1 deletion codegen/src/worktable/generator/queries/type.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashSet;

use proc_macro2::{Ident, Span, TokenStream};
use quote::quote;

Expand All @@ -9,7 +11,7 @@ impl Generator {
let name_generator = WorktableNameGenerator::from_table_name(self.name.to_string());
let avt_type_ident = name_generator.get_available_type_ident();

let unique_types: std::collections::HashSet<String> = self
let unique_types: HashSet<String> = self
.columns
.indexes
.iter()
Expand Down
7 changes: 3 additions & 4 deletions codegen/src/worktable/generator/table/index_fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,12 @@ impl Generator {

Ok(quote! {
pub fn #fn_name(&self, by: #type_) -> SelectQueryBuilder<#row_ident, impl DoubleEndedIterator<Item = #row_ident> + '_, #column_range_type> {
let rows: Vec<#row_ident> = self.0.indexes.#field_ident
let rows = self.0.indexes.#field_ident
.get(&by)
.into_iter()
.filter_map(|(_, link)| self.0.data.select(*link).ok())
.collect();
.filter_map(|(_, link)| self.0.data.select(*link).ok());

SelectQueryBuilder::new(rows.into_iter())
SelectQueryBuilder::new(rows)
}
})
}
Expand Down
100 changes: 66 additions & 34 deletions codegen/src/worktable/generator/table/select_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,22 @@ use crate::worktable::generator::Generator;
use quote::ToTokens;
use syn::Type;

const RANGE_VARIANTS: &[&str] = &["", "Inclusive", "From", "To", "ToInclusive"];

fn is_numeric_type(ty: &Type) -> bool {
matches!(
ty.to_token_stream().to_string().as_str(),
"i8" | "i16" | "i32" | "i64" | "i128" | "u8" | "u16" | "u32" | "u64" | "u128"
"i8" | "i16"
| "i32"
| "i64"
| "i128"
| "u8"
| "u16"
| "u32"
| "u64"
| "u128"
| "f32"
| "f64"
)
}

Expand All @@ -31,39 +43,48 @@ impl Generator {
.collect();

let column_range_variants = unique_types.iter().map(|type_name| {
let variant_ident = Ident::new(
&type_name.to_string().to_case(Case::Pascal),
Span::call_site(),
);
let ty_ident = Ident::new(&type_name.to_string(), Span::call_site());
let variants: Vec<_> = RANGE_VARIANTS
.iter()
.map(|v| {
let variant_ident = Ident::new(
&format!("{}{}", type_name.to_string().to_case(Case::Pascal), v),
Span::call_site(),
);
let range_ident = Ident::new(&format!("Range{}", v), Span::call_site());
quote! {
#variant_ident(std::ops::#range_ident<#ty_ident>),
}
})
.collect();

quote! {
#variant_ident(std::ops::RangeInclusive<#ty_ident>),
#(#variants)*
}
});

let from_impls = unique_types.iter().map(|type_name| {
let variant_ident = Ident::new(
&type_name.to_string().to_case(Case::Pascal),
Span::call_site(),
);
let type_ident = Ident::new(&type_name.to_string(), Span::call_site());
let ty_ident = Ident::new(&type_name.to_string(), Span::call_site());
let variants: Vec<_> = RANGE_VARIANTS
.iter()
.map(|v| {
let variant_ident = Ident::new(
&format!("{}{}", type_name.to_string().to_case(Case::Pascal), v),
Span::call_site(),
);
let range_ident = Ident::new(&format!("Range{}", v), Span::call_site());
quote! {
impl From<std::ops::#range_ident<#ty_ident>> for #column_range_type {
fn from(range: std::ops::#range_ident<#ty_ident>) -> Self {
Self::#variant_ident(range)
}
}
}
})
.collect();

quote! {
impl From<std::ops::RangeInclusive<#type_ident>> for #column_range_type {
fn from(range: std::ops::RangeInclusive<#type_ident>) -> Self {
Self::#variant_ident(range)
}
}
impl From<std::ops::Range<#type_ident>> for #column_range_type {
fn from(range: std::ops::Range<#type_ident>) -> Self {
let end = if range.end > range.start {
range.end.saturating_sub(1)
} else {
range.end
};
Self::#variant_ident(range.start..=end)
}
}
#(#variants)*
}
});

Expand Down Expand Up @@ -106,15 +127,26 @@ impl Generator {
is_numeric_type(&syn::parse_str::<Type>(&ty.to_token_stream().to_string()).unwrap())
})
.map(|(column, ty)| {
let col_lit = Literal::string(column.to_string().as_str());
let col_ident = Ident::new(&column.to_string(), Span::call_site());
let variant_ident =
Ident::new(&ty.to_string().to_case(Case::Pascal), Span::call_site());
let variants: Vec<_> = RANGE_VARIANTS
.iter()
.map(|v| {
let col_lit = Literal::string(column.to_string().as_str());
let col_ident = Ident::new(&column.to_string(), Span::call_site());
let variant_ident = Ident::new(
&format!("{}{}", ty.to_string().to_case(Case::Pascal), v),
Span::call_site(),
);
quote! {
(#col_lit, #column_range_type::#variant_ident(range)) => {
Box::new(iter.filter(move |row| range.contains(&row.#col_ident)))
as Box<dyn DoubleEndedIterator<Item = #row_type>>
},
}
})
.collect();

quote! {
(#col_lit, #column_range_type::#variant_ident(range)) => {
Box::new(iter.filter(move |row| range.contains(&row.#col_ident)))
as Box<dyn DoubleEndedIterator<Item = #row_type>>
},
#(#variants)*
}
});

Expand Down
9 changes: 3 additions & 6 deletions src/persistence/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ pub struct PersistenceConfig {
}

impl PersistenceConfig {
pub fn new<S1: Into<String>, S2: Into<String>>(
config_path: S1,
table_files_dir: S2,
) -> eyre::Result<Self> {
Ok(Self {
pub fn new<S1: Into<String>, S2: Into<String>>(config_path: S1, table_files_dir: S2) -> Self {
Self {
config_path: config_path.into(),
tables_path: table_files_dir.into(),
})
}
}
}
4 changes: 2 additions & 2 deletions tests/persistence/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ worktable!(
pub const TEST_ROW_COUNT: usize = 100;

pub async fn get_empty_test_wt() -> TestPersistWorkTable {
let config = PersistenceConfig::new("tests/data", "tests/data").unwrap();
let config = PersistenceConfig::new("tests/data", "tests/data");
TestPersistWorkTable::new(config).await.unwrap()
}

Expand All @@ -64,7 +64,7 @@ pub async fn get_test_wt() -> TestPersistWorkTable {
}

pub async fn get_test_wt_without_secondary_indexes() -> TestWithoutSecondaryIndexesWorkTable {
let config = PersistenceConfig::new("tests/data", "tests/data").unwrap();
let config = PersistenceConfig::new("tests/data", "tests/data");
let table = TestWithoutSecondaryIndexesWorkTable::new(config)
.await
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions tests/persistence/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ async fn test_data_parse() {

#[tokio::test]
async fn test_space_parse() {
let config = PersistenceConfig::new("tests/data/expected", "tests/data/expected").unwrap();
let config = PersistenceConfig::new("tests/data/expected", "tests/data/expected");
let table = TestPersistWorkTable::load_from_file(config).await.unwrap();
let expected = get_test_wt().await;

Expand All @@ -139,7 +139,7 @@ async fn test_space_parse() {
async fn test_space_parse_no_file() {
remove_dir_if_exists("tests/non-existent".to_string()).await;

let config = PersistenceConfig::new("tests/non-existent", "tests/non-existent").unwrap();
let config = PersistenceConfig::new("tests/non-existent", "tests/non-existent");
let table = TestPersistWorkTable::load_from_file(config).await.unwrap();
let expected = get_empty_test_wt().await;
assert_eq!(
Expand Down
18 changes: 6 additions & 12 deletions tests/persistence/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use worktable::prelude::{PersistenceConfig, PrimaryKeyGeneratorState};

#[test]
fn test_space_insert_sync() {
let config =
PersistenceConfig::new("tests/data/sync/insert", "tests/data/sync/insert").unwrap();
let config = PersistenceConfig::new("tests/data/sync/insert", "tests/data/sync/insert");

let runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(2)
Expand Down Expand Up @@ -40,8 +39,7 @@ fn test_space_insert_sync() {
#[test]
fn test_space_insert_many_sync() {
let config =
PersistenceConfig::new("tests/data/sync/insert_many", "tests/data/sync/insert_many")
.unwrap();
PersistenceConfig::new("tests/data/sync/insert_many", "tests/data/sync/insert_many");

let runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(2)
Expand Down Expand Up @@ -86,8 +84,7 @@ fn test_space_insert_many_sync() {
#[test]
fn test_space_update_full_sync() {
let config =
PersistenceConfig::new("tests/data/sync/update_full", "tests/data/sync/update_full")
.unwrap();
PersistenceConfig::new("tests/data/sync/update_full", "tests/data/sync/update_full");

let runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(2)
Expand Down Expand Up @@ -132,8 +129,7 @@ fn test_space_update_query_sync() {
let config = PersistenceConfig::new(
"tests/data/sync/update_query",
"tests/data/sync/update_query",
)
.unwrap();
);

let runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(2)
Expand Down Expand Up @@ -172,8 +168,7 @@ fn test_space_update_query_sync() {

#[test]
fn test_space_delete_sync() {
let config =
PersistenceConfig::new("tests/data/sync/delete", "tests/data/sync/delete").unwrap();
let config = PersistenceConfig::new("tests/data/sync/delete", "tests/data/sync/delete");

let runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(2)
Expand Down Expand Up @@ -211,8 +206,7 @@ fn test_space_delete_query_sync() {
let config = PersistenceConfig::new(
"tests/data/sync/delete_query",
"tests/data/sync/delete_query",
)
.unwrap();
);

let runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(2)
Expand Down
Loading