Skip to content

Commit 7ce4651

Browse files
committed
cleanup
1 parent 74326a7 commit 7ce4651

File tree

31 files changed

+332
-243
lines changed

31 files changed

+332
-243
lines changed

.github/workflows/redbit.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ jobs:
5050
cargo test --package demo --features integration,ci
5151
cargo test --package btc --features integration,ci
5252
cargo test --package cardano --features integration,ci
53-
cargo test --package ergo --features integration,ci
53+
# cargo test --package ergo --features integration,ci
5454
5555
criterion:
5656
needs: integration
@@ -73,7 +73,7 @@ jobs:
7373
cargo bench --package demo --bench demo_benchmark -- --quiet
7474
cargo bench --package btc --bench btc_benchmark -- --quiet
7575
cargo bench --package cardano --bench cardano_benchmark -- --quiet
76-
cargo bench --package ergo --bench ergo_benchmark -- --quiet
76+
# cargo bench --package ergo --bench ergo_benchmark -- --quiet
7777
7878
- name: Upload Criterion report to Pages artifact
7979
id: deployment

chains/btc/src/hook.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
use crate::model_v1::*;
22
use std::ops::Index;
33

4-
// temporary hack for testing purposes, will be removed
5-
pub(crate) fn flush(tx_context: &TransactionWriteTxContext) -> Result<(), AppError> {
6-
let ids_router = tx_context.inputs.input_id.router();
7-
let ptrs_router = tx_context.inputs.input_utxo_pointer_by_id.router();
8-
ids_router.ready_for_flush(1)?;
9-
ptrs_router.ready_for_flush(1)?;
10-
Ok(())
11-
}
12-
134
pub(crate) fn write_from_input_refs_using_hash(tx_context: &TransactionWriteTxContext, parent: BlockPointer, input_refs: Vec<InputRef>, is_last: bool) -> Result<(), AppError> {
145
let ids_router = tx_context.inputs.input_id.router();
156
let ptrs_router = tx_context.inputs.input_utxo_pointer_by_id.router();

chains/cardano/src/hook.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
use crate::model_v1::*;
22
use std::ops::Index;
33

4-
// temporary hack for testing purposes, will be removed
5-
pub(crate) fn flush(tx_context: &TransactionWriteTxContext) -> Result<(), AppError> {
6-
let ids_router = tx_context.inputs.input_id.router();
7-
let ptrs_router = tx_context.inputs.input_utxo_pointer_by_id.router();
8-
ids_router.ready_for_flush(1)?;
9-
ptrs_router.ready_for_flush(1)?;
10-
Ok(())
11-
}
12-
134
pub(crate) fn write_from_input_refs_using_hash(tx_context: &TransactionWriteTxContext, parent: BlockPointer, input_refs: Vec<InputRef>, is_last: bool) -> Result<(), AppError> {
145
let ids_router = tx_context.inputs.input_id.router();
156
let ptrs_router = tx_context.inputs.input_utxo_pointer_by_id.router();

chains/demo/src/hook.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
use crate::model_v1::*;
22
use std::ops::Index;
33

4-
// temporary hack for testing purposes, will be removed
5-
pub(crate) fn flush(tx_context: &TransactionWriteTxContext) -> Result<(), AppError> {
6-
let ids_router = tx_context.inputs.input_id.router();
7-
let ptrs_router = tx_context.inputs.input_utxo_pointer_by_id.router();
8-
ids_router.ready_for_flush(1)?;
9-
ptrs_router.ready_for_flush(1)?;
10-
Ok(())
11-
}
12-
134
pub(crate) fn write_from_input_refs_using_hash(tx_context: &TransactionWriteTxContext, parent: BlockPointer, input_refs: Vec<InputRef>, is_last: bool) -> Result<(), AppError> {
145
let ids_router = tx_context.inputs.input_id.router();
156
let ptrs_router = tx_context.inputs.input_utxo_pointer_by_id.router();

chains/demo/tests/entity_test.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ mod entity_tests {
9797
let transaction = blocks.first().unwrap().transactions.first().unwrap();
9898

9999
let found_by_hash = Transaction::stream_by_hash(transaction_tx, transaction.hash.clone(), None).unwrap().try_collect::<Vec<Transaction>>().await.unwrap();
100-
assert_eq!(found_by_hash.len(), 3);
100+
assert_eq!(found_by_hash.len(), 1);
101101
assert!(found_by_hash.iter().any(|tx| tx.id == transaction.id));
102102
assert!(found_by_hash.iter().any(|tx| tx.id == transaction.id));
103103
}
@@ -110,7 +110,7 @@ mod entity_tests {
110110
let utxo = blocks.first().unwrap().transactions.first().unwrap().utxos.first().unwrap();
111111

112112
let found_by_address = Utxo::stream_by_address(utxo_tx, utxo.address.clone(), None).unwrap().try_collect::<Vec<Utxo>>().await.unwrap();
113-
assert_eq!(found_by_address.len(), 9);
113+
assert_eq!(found_by_address.len(), 1);
114114
assert!(found_by_address.iter().any(|tx| tx.id == utxo.id));
115115
assert!(found_by_address.iter().any(|tx| tx.id == utxo.id));
116116
}
@@ -150,7 +150,7 @@ mod entity_tests {
150150
let transaction = blocks.first().unwrap().transactions.first().unwrap();
151151

152152
let found_by_hash = Transaction::get_by_hash(&transaction_tx, &transaction.hash).expect("Failed to query by hash");
153-
assert_eq!(found_by_hash.len(), 3);
153+
assert_eq!(found_by_hash.len(), 1);
154154
assert!(found_by_hash.iter().any(|tx| tx.id == transaction.id));
155155
assert!(found_by_hash.iter().any(|tx| tx.id == transaction.id));
156156
}
@@ -163,12 +163,11 @@ mod entity_tests {
163163
let utxo = blocks.first().unwrap().transactions.first().unwrap().utxos.first().unwrap();
164164

165165
let found_by_address = Utxo::get_by_address(&utxo_tx, &utxo.address).expect("Failed to query by address");
166-
assert_eq!(found_by_address.len(), 9);
166+
assert_eq!(found_by_address.len(), 1);
167167
assert!(found_by_address.iter().any(|tx| tx.id == utxo.id));
168168
assert!(found_by_address.iter().any(|tx| tx.id == utxo.id));
169169
}
170170

171-
172171
#[tokio::test]
173172
async fn it_should_get_entities_by_range_on_pk() {
174173
let (blocks, _storage_owner, storage) = init_temp_storage("db_test", 0).await;

chains/ergo/src/hook.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
use crate::model_v1::*;
22

3-
// temporary hack for testing purposes
4-
pub(crate) fn flush(tx_context: &TransactionWriteTxContext) -> Result<(), AppError> {
5-
let ids_router = tx_context.inputs.input_id.router();
6-
let ptrs_router = tx_context.inputs.input_utxo_pointer_by_id.router();
7-
ids_router.ready_for_flush(1)?;
8-
ptrs_router.ready_for_flush(1)?;
9-
Ok(())
10-
}
11-
123
pub(crate) fn write_from_input_refs_using_utxos(tx_context: &TransactionWriteTxContext, parent: BlockPointer, input_refs: Vec<BoxId>, is_last: bool) -> Result<(), AppError> {
134
let ids_router = tx_context.inputs.input_id.router();
145
let ptrs_router = tx_context.inputs.input_utxo_pointer_by_id.router();

macros/src/column/column_impls.rs

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
use proc_macro2::{Literal, Ident, TokenStream};
1+
use crate::column::column_codec::*;
2+
use crate::macro_utils;
3+
use crate::macro_utils::InnerKind;
4+
use proc_macro2::{Ident, Literal, TokenStream};
25
use quote::quote;
3-
use syn::{parse_str, Attribute, Path, Type};
46
use syn::punctuated::Punctuated;
57
use syn::token::Comma;
6-
use crate::column::column_codec::*;
7-
use crate::macro_utils::InnerKind;
8+
use syn::{parse_str, Attribute, FieldsNamed, Path, Type};
89

910
pub fn generate_column_impls(
1011
struct_ident: &Ident,
1112
new_type: &Type,
1213
inner_type: &Type,
1314
binary_encoding_opt: Option<String>,
1415
) -> (TokenStream, Option<Attribute>, Punctuated<Path, Comma>) {
15-
let kind = crate::macro_utils::classify_inner_type(inner_type);
16+
let kind = macro_utils::classify_inner_type(inner_type);
1617

1718
let binary_encoding = binary_encoding_opt.unwrap_or_else(|| "hex".to_string());
1819
let mut schema_example = quote! { vec![Some(json!(#struct_ident::default().url_encode()))] };
@@ -21,7 +22,7 @@ pub fn generate_column_impls(
2122
let mut schema_type = quote! { SchemaType::Type(Type::String) };
2223
let mut default_code = quote! { Self(Default::default()) };
2324
let mut url_encoded_code = quote! { format!("{}", self.0) };
24-
let mut iterable_code = quote! { compile_error!("IterableColumn::next is not supported for this type.") };
25+
let mut iterable_code = quote! { compile_error!("Sampleable::next is not supported for this type.") };
2526
let mut custom_db_codec = quote! {};
2627
let mut cache_key_codec = quote! {};
2728

@@ -180,7 +181,7 @@ pub fn generate_column_impls(
180181
}
181182
}
182183

183-
impl IterableColumn for #struct_ident {
184+
impl Sampleable for #struct_ident {
184185
fn next_value(&self) -> Self {
185186
#iterable_code
186187
}
@@ -210,3 +211,61 @@ pub fn generate_column_impls(
210211

211212
(impls, struct_attr, extra_derive_impls)
212213
}
214+
215+
fn make_zero_or_default_field_expr(field_ty: &Type) -> TokenStream {
216+
if macro_utils::classify_integer_type(field_ty).is_some() {
217+
quote! { 0 as #field_ty }
218+
} else {
219+
quote! { Default::default() }
220+
}
221+
}
222+
223+
fn gen_field_inits_zero(fields: &FieldsNamed) -> Vec<TokenStream> {
224+
fields.named.iter().map(|f| {
225+
let name = f.ident.as_ref().expect("named field");
226+
let ty = &f.ty;
227+
let expr = make_zero_or_default_field_expr(ty);
228+
quote! { #name: #expr, }
229+
}).collect()
230+
}
231+
232+
fn make_next_field_expr(field_ident: &syn::Ident, field_ty: &Type) -> TokenStream {
233+
if macro_utils::classify_integer_type(field_ty).is_some() {
234+
quote! { self.#field_ident.wrapping_add(1) }
235+
} else {
236+
quote! { self.#field_ident.clone() }
237+
}
238+
}
239+
240+
fn gen_next_field_inits(fields: &FieldsNamed) -> Vec<TokenStream> {
241+
fields.named.iter().map(|f| {
242+
let name = f.ident.as_ref().expect("named field");
243+
let ty = &f.ty;
244+
let expr = make_next_field_expr(name, ty);
245+
quote! { #name: #expr, }
246+
}).collect()
247+
}
248+
249+
pub fn gen_default_impl(struct_ident: &Ident, struct_type: &Type, fields: &FieldsNamed) -> TokenStream {
250+
let default_inits = gen_field_inits_zero(fields);
251+
252+
quote! {
253+
impl ::core::default::Default for #struct_type {
254+
fn default() -> Self {
255+
#struct_ident { #(#default_inits)* }
256+
}
257+
}
258+
}
259+
}
260+
261+
pub fn gen_sampleable_impl(struct_ident: &Ident, struct_type: &Type, fields: &FieldsNamed) -> TokenStream {
262+
let next_inits = gen_next_field_inits(fields);
263+
264+
quote! {
265+
impl Sampleable for #struct_type {
266+
fn next_value(&self) -> Self {
267+
#struct_ident { #(#next_inits)* }
268+
}
269+
}
270+
}
271+
}

macros/src/column/init.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub fn default_init_expr(column_type: &Type) -> TokenStream {
66
quote! {
77
{
88
let value = <#column_type as Default>::default();
9-
<#column_type as IterableColumn>::nth_value(&value, sample_index)
9+
<#column_type as Sampleable>::nth_value(&value, sample_index)
1010
}
1111
}
1212
}

macros/src/column/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ mod get_keys_by;
1212
pub mod column_impls;
1313
pub mod column_codec;
1414
pub mod info;
15+
pub mod transient;
1516

1617
use crate::entity;
1718
use crate::entity::context;
1819
use crate::entity::context::TxContextItem;
19-
use crate::entity::query::{RangeQuery, FilterQueryItem};
20+
use crate::entity::query::{FilterQueryItem, RangeQuery};
2021
use crate::field_parser::{ColumnProps, EntityDef, FieldDef, IndexingType, OneToManyParentDef, Used};
2122
use crate::rest::*;
2223
use crate::table::{DictTableDefs, IndexTableDefs, PlainTableDef, TableDef};

macros/src/column/stream_by.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn by_dict_def(
1313
dict_table_var: &Ident,
1414
) -> FunctionDef {
1515
let fn_name = format_ident!("stream_by_{}", column_name);
16-
let EntityDef { key_def, entity_name, entity_type, query_type, info_type:_, read_ctx_type, write_ctx_type: _} = &entity_def;
16+
let EntityDef { key_def, entity_name, entity_type, query_type, read_ctx_type, ..} = &entity_def;
1717
let pk_type = key_def.field_def().tpe;
1818
let fn_stream = quote! {
1919
pub fn #fn_name(tx_context: #read_ctx_type, val: #column_type, query: Option<#query_type>) -> Result<Pin<Box<dyn futures::Stream<Item = Result<#entity_type, AppError>> + Send>>, AppError> {
@@ -142,7 +142,7 @@ pub fn by_dict_def(
142142

143143
pub fn by_index_def(entity_def: &EntityDef, column_name: &Ident, column_type: &Type, index_table: &Ident) -> FunctionDef {
144144
let fn_name = format_ident!("stream_by_{}", column_name);
145-
let EntityDef { key_def, entity_name, entity_type, query_type, info_type:_, read_ctx_type, write_ctx_type: _} = &entity_def;
145+
let EntityDef { key_def, entity_name, entity_type, query_type, read_ctx_type, ..} = &entity_def;
146146
let pk_type = key_def.field_def().tpe;
147147
let fn_stream = quote! {
148148
pub fn #fn_name(tx_context: #read_ctx_type, val: #column_type, query: Option<#query_type>) -> Result<Pin<Box<dyn futures::Stream<Item = Result<#entity_type, AppError>> + Send>>, AppError> {

0 commit comments

Comments
 (0)