Skip to content

Commit

Permalink
Made ability to Lazy Deserialize entities
Browse files Browse the repository at this point in the history
  • Loading branch information
amigin committed Sep 20, 2024
1 parent 1a75b40 commit 3c20f35
Show file tree
Hide file tree
Showing 21 changed files with 436 additions and 191 deletions.
3 changes: 2 additions & 1 deletion my-no-sql-abstractions/src/my_no_sql_entity.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
pub trait MyNoSqlEntity {
const TABLE_NAME: &'static str;
const LAZY_DESERIALIZATION: bool;
fn get_partition_key(&self) -> &str;
fn get_row_key(&self) -> &str;
fn get_time_stamp(&self) -> i64;
}

pub trait MyNoSqlEntitySerializer: Sized {
fn serialize_entity(&self) -> Vec<u8>;
fn deserialize_entity(src: &[u8]) -> Option<Self>;
fn deserialize_entity(src: &[u8]) -> Result<Self, String>;
}

pub trait GetMyNoSqlEntity {
Expand Down
14 changes: 8 additions & 6 deletions my-no-sql-core/src/entity_serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,33 @@ where
serde_json::to_vec(&entity).unwrap()
}

pub fn deserialize<TMyNoSqlEntity>(data: &[u8]) -> TMyNoSqlEntity
pub fn deserialize<TMyNoSqlEntity>(data: &[u8]) -> Result<TMyNoSqlEntity, String>
where
TMyNoSqlEntity: MyNoSqlEntity + DeserializeOwned,
{
let parse_result: Result<TMyNoSqlEntity, _> = serde_json::from_slice(&data);

match parse_result {
Ok(el) => return el,
Ok(el) => return Ok(el),
Err(err) => {
let slice_iterator = SliceIterator::new(data);
let json_first_line_iterator = JsonFirstLineReader::new(slice_iterator);
let db_entity = DbJsonEntity::new(json_first_line_iterator);

match db_entity {
Ok(db_entity) => {
panic!(
return Err(format!(
"Table: {}. Can not parse entity with PartitionKey: [{}] and RowKey: [{}]. Err: {:?}",
TMyNoSqlEntity::TABLE_NAME, db_entity.get_partition_key(data), db_entity.get_row_key(data), err
);
))
;
}
Err(err) => {
panic!(
return Err(format!(
"Table: {}. Can not extract partitionKey and rowKey. Looks like entity broken at all. Err: {:?}",
TMyNoSqlEntity::TABLE_NAME, err
)
))

}
}
}
Expand Down
14 changes: 7 additions & 7 deletions my-no-sql-data-writer/src/my_no_sql_data_writer/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ pub async fn get_entity<TEntity: MyNoSqlEntity + MyNoSqlEntitySerializer + Sync
check_error(&mut response).await?;

if is_ok_result(&response) {
let entity = TEntity::deserialize_entity(response.get_body_as_slice().await?);
return Ok(entity);
let entity = TEntity::deserialize_entity(response.get_body_as_slice().await?).unwrap();
return Ok(Some(entity));
}

return Ok(None);
Expand Down Expand Up @@ -333,8 +333,8 @@ pub async fn delete_row<TEntity: MyNoSqlEntity + MyNoSqlEntitySerializer + Sync
check_error(&mut response).await?;

if response.get_status_code() == 200 {
let entity = TEntity::deserialize_entity(response.get_body_as_slice().await?);
return Ok(entity);
let entity = TEntity::deserialize_entity(response.get_body_as_slice().await?).unwrap();
return Ok(Some(entity));
}

return Ok(None);
Expand Down Expand Up @@ -559,6 +559,7 @@ mod tests {

impl MyNoSqlEntity for TestEntity {
const TABLE_NAME: &'static str = "test";
const LAZY_DESERIALIZATION: bool = false;

fn get_partition_key(&self) -> &str {
&self.partition_key
Expand All @@ -578,9 +579,8 @@ mod tests {
my_no_sql_core::entity_serializer::serialize(self)
}

fn deserialize_entity(src: &[u8]) -> Option<Self> {
let result: Self = my_no_sql_core::entity_serializer::deserialize(src);
result.into()
fn deserialize_entity(src: &[u8]) -> Result<Self, String> {
my_no_sql_core::entity_serializer::deserialize(src)
}
}

Expand Down
5 changes: 2 additions & 3 deletions my-no-sql-macros/src/entity_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,8 @@ pub fn get_fn_standard_serialize_deserialize() -> proc_macro2::TokenStream {
}


fn deserialize_entity(src: &[u8]) -> Option<Self> {
let result = my_no_sql_sdk::core::entity_serializer::deserialize(src);
Some(result)
fn deserialize_entity(src: &[u8]) -> Result<Self, String> {
my_no_sql_sdk::core::entity_serializer::deserialize(src)
}
}
}
1 change: 1 addition & 0 deletions my-no-sql-macros/src/enum_model/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub fn generate(
impl my_no_sql_sdk::abstractions::MyNoSqlEntity for #struct_name {

const TABLE_NAME: &'static str = "";
const LAZY_DESERIALIZATION: bool = true;


fn get_partition_key(&self) -> &str {
Expand Down
11 changes: 7 additions & 4 deletions my-no-sql-macros/src/enum_of_my_no_sql_entity/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pub fn generate(

const TABLE_NAME: &'static str = #table_name;

const LAZY_DESERIALIZATION: bool = true;

fn get_partition_key(&self) -> &str {
use my_no_sql_sdk::abstractions::MyNoSqlEntity;
match self {
Expand Down Expand Up @@ -85,7 +87,7 @@ pub fn generate(
my_no_sql_sdk::core::entity_serializer::inject_partition_key_and_row_key(result, self.get_partition_key(), row_key)

}
fn deserialize_entity(src: &[u8]) -> Option<Self> {
fn deserialize_entity(src: &[u8]) -> Result<Self, String> {
#deserialize_cases
}
}
Expand Down Expand Up @@ -178,12 +180,12 @@ fn get_deserialize_cases(enum_cases: &[EnumCase]) -> Result<proc_macro2::TokenSt
if let Some(row_key) = #model_ident::ROW_KEY{
if entity_partition_key == #model_ident::PARTITION_KEY && entity_row_key == row_key {
let item = #model_ident::deserialize_entity(src)?;
return Self::#enum_case_ident(item).into();
return Ok(Self::#enum_case_ident(item));
}
}else{
if entity_partition_key == #model_ident::PARTITION_KEY {
let item = #model_ident::deserialize_entity(src)?;
return Self::#enum_case_ident(item).into();
return Ok(Self::#enum_case_ident(item));
}

}
Expand All @@ -199,7 +201,8 @@ fn get_deserialize_cases(enum_cases: &[EnumCase]) -> Result<proc_macro2::TokenSt
}

result.push(quote::quote!{
return None;
use my_no_sql_sdk::abstractions::MyNoSqlEntity;
Err(format!("Table: '{}'. Unknown Enum Case for the record with PartitionKey: {} and RowKey: {}", Self::TABLE_NAME, entity_partition_key, entity_row_key))
});

Ok(quote::quote!(#(#result)*))
Expand Down
2 changes: 2 additions & 0 deletions my-no-sql-macros/src/my_no_sql_entity/generate_base_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub fn generate_base_impl(

const TABLE_NAME: &'static str = #table_name;

const LAZY_DESERIALIZATION: bool = false;

fn get_partition_key(&self) -> &str {
&self.partition_key
}
Expand Down
56 changes: 36 additions & 20 deletions my-no-sql-tcp-reader/src/data_reader_entities_set.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
use std::{collections::BTreeMap, sync::Arc};

use my_no_sql_abstractions::MyNoSqlEntity;
use my_no_sql_abstractions::{MyNoSqlEntity, MyNoSqlEntitySerializer};

use crate::subscribers::MyNoSqlDataReaderCallBacksPusher;
use crate::subscribers::{LazyMyNoSqlEntity, MyNoSqlDataReaderCallBacksPusher};

pub struct DataReaderEntitiesSet<TMyNoSqlEntity: MyNoSqlEntity + Send + Sync + 'static> {
entities: Option<BTreeMap<String, BTreeMap<String, Arc<TMyNoSqlEntity>>>>,
pub struct DataReaderEntitiesSet<
TMyNoSqlEntity: MyNoSqlEntity + MyNoSqlEntitySerializer + Send + Sync + 'static,
> {
entities: Option<BTreeMap<String, BTreeMap<String, LazyMyNoSqlEntity<TMyNoSqlEntity>>>>,
table_name: &'static str,
}

impl<TMyNoSqlEntity: MyNoSqlEntity + Send + Sync + 'static> DataReaderEntitiesSet<TMyNoSqlEntity> {
impl<TMyNoSqlEntity: MyNoSqlEntity + MyNoSqlEntitySerializer + Send + Sync + 'static>
DataReaderEntitiesSet<TMyNoSqlEntity>
{
pub fn new(table_name: &'static str) -> Self {
Self {
entities: None,
Expand All @@ -21,13 +25,21 @@ impl<TMyNoSqlEntity: MyNoSqlEntity + Send + Sync + 'static> DataReaderEntitiesSe
self.entities.is_some()
}

pub fn as_ref(&self) -> Option<&BTreeMap<String, BTreeMap<String, Arc<TMyNoSqlEntity>>>> {
pub fn as_ref(
&self,
) -> Option<&BTreeMap<String, BTreeMap<String, LazyMyNoSqlEntity<TMyNoSqlEntity>>>> {
self.entities.as_ref()
}

pub fn as_mut(
&mut self,
) -> Option<&mut BTreeMap<String, BTreeMap<String, LazyMyNoSqlEntity<TMyNoSqlEntity>>>> {
self.entities.as_mut()
}

fn init_and_get_table(
&mut self,
) -> &mut BTreeMap<String, BTreeMap<String, Arc<TMyNoSqlEntity>>> {
) -> &mut BTreeMap<String, BTreeMap<String, LazyMyNoSqlEntity<TMyNoSqlEntity>>> {
if self.entities.is_none() {
println!("MyNoSqlTcpReader table {} is initialized", self.table_name);
self.entities = Some(BTreeMap::new());
Expand All @@ -39,9 +51,9 @@ impl<TMyNoSqlEntity: MyNoSqlEntity + Send + Sync + 'static> DataReaderEntitiesSe

pub fn init_table<'s>(
&'s mut self,
data: BTreeMap<String, Vec<TMyNoSqlEntity>>,
data: BTreeMap<String, Vec<LazyMyNoSqlEntity<TMyNoSqlEntity>>>,
) -> InitTableResult<'s, TMyNoSqlEntity> {
let mut new_table: BTreeMap<String, BTreeMap<String, Arc<TMyNoSqlEntity>>> =
let mut new_table: BTreeMap<String, BTreeMap<String, LazyMyNoSqlEntity<TMyNoSqlEntity>>> =
BTreeMap::new();

for (partition_key, src_entities_by_partition) in data {
Expand All @@ -50,7 +62,6 @@ impl<TMyNoSqlEntity: MyNoSqlEntity + Send + Sync + 'static> DataReaderEntitiesSe
let by_partition = new_table.get_mut(partition_key.as_str()).unwrap();

for entity in src_entities_by_partition {
let entity = Arc::new(entity);
by_partition.insert(entity.get_row_key().to_string(), entity);
}
}
Expand All @@ -66,7 +77,7 @@ impl<TMyNoSqlEntity: MyNoSqlEntity + Send + Sync + 'static> DataReaderEntitiesSe
pub fn init_partition<'s>(
&'s mut self,
partition_key: &str,
src_entities: BTreeMap<String, Vec<TMyNoSqlEntity>>,
src_entities: BTreeMap<String, Vec<LazyMyNoSqlEntity<TMyNoSqlEntity>>>,
) -> InitPartitionResult<'s, TMyNoSqlEntity> {
let entities = self.init_and_get_table();

Expand All @@ -76,7 +87,7 @@ impl<TMyNoSqlEntity: MyNoSqlEntity + Send + Sync + 'static> DataReaderEntitiesSe

for (row_key, entities) in src_entities {
for entity in entities {
new_partition.insert(row_key.clone(), Arc::new(entity));
new_partition.insert(row_key.clone(), entity);
}
}

Expand All @@ -90,7 +101,7 @@ impl<TMyNoSqlEntity: MyNoSqlEntity + Send + Sync + 'static> DataReaderEntitiesSe

pub fn update_rows(
&mut self,
src_data: BTreeMap<String, Vec<TMyNoSqlEntity>>,
src_data: BTreeMap<String, Vec<LazyMyNoSqlEntity<TMyNoSqlEntity>>>,
callbacks: &Option<Arc<MyNoSqlDataReaderCallBacksPusher<TMyNoSqlEntity>>>,
) {
let entities = self.init_and_get_table();
Expand All @@ -109,7 +120,6 @@ impl<TMyNoSqlEntity: MyNoSqlEntity + Send + Sync + 'static> DataReaderEntitiesSe
let by_partition = entities.get_mut(partition_key.as_str()).unwrap();

for entity in src_entities {
let entity = Arc::new(entity);
if let Some(updates) = updates.as_mut() {
updates.push(entity.clone());
}
Expand Down Expand Up @@ -179,12 +189,18 @@ impl<TMyNoSqlEntity: MyNoSqlEntity + Send + Sync + 'static> DataReaderEntitiesSe
}
}

pub struct InitTableResult<'s, TMyNoSqlEntity: MyNoSqlEntity + Send + Sync + 'static> {
pub table_now: &'s BTreeMap<String, BTreeMap<String, Arc<TMyNoSqlEntity>>>,
pub table_before: Option<BTreeMap<String, BTreeMap<String, Arc<TMyNoSqlEntity>>>>,
pub struct InitTableResult<
's,
TMyNoSqlEntity: MyNoSqlEntity + MyNoSqlEntitySerializer + Send + Sync + 'static,
> {
pub table_now: &'s BTreeMap<String, BTreeMap<String, LazyMyNoSqlEntity<TMyNoSqlEntity>>>,
pub table_before: Option<BTreeMap<String, BTreeMap<String, LazyMyNoSqlEntity<TMyNoSqlEntity>>>>,
}

pub struct InitPartitionResult<'s, TMyNoSqlEntity: MyNoSqlEntity + Send + Sync + 'static> {
pub partition_before: &'s BTreeMap<String, Arc<TMyNoSqlEntity>>,
pub partition_now: Option<BTreeMap<String, Arc<TMyNoSqlEntity>>>,
pub struct InitPartitionResult<
's,
TMyNoSqlEntity: MyNoSqlEntity + MyNoSqlEntitySerializer + Send + Sync + 'static,
> {
pub partition_before: &'s BTreeMap<String, LazyMyNoSqlEntity<TMyNoSqlEntity>>,
pub partition_now: Option<BTreeMap<String, LazyMyNoSqlEntity<TMyNoSqlEntity>>>,
}
Loading

0 comments on commit 3c20f35

Please sign in to comment.