From 36e5b18445e25504a928125abae70ff354708c06 Mon Sep 17 00:00:00 2001 From: broody Date: Fri, 10 Nov 2023 15:25:59 -0800 Subject: [PATCH] clean up --- crates/torii/client/src/client/mod.rs | 29 ++++----- crates/torii/client/src/client/storage.rs | 14 ++-- .../torii/client/src/client/subscription.rs | 29 ++++----- crates/torii/core/src/error.rs | 1 - crates/torii/grpc/src/server/mod.rs | 1 - crates/torii/grpc/src/server/query.rs | 64 ------------------- crates/torii/grpc/src/server/subscription.rs | 1 - 7 files changed, 33 insertions(+), 106 deletions(-) delete mode 100644 crates/torii/grpc/src/server/query.rs diff --git a/crates/torii/client/src/client/mod.rs b/crates/torii/client/src/client/mod.rs index 4508833abd..d14ade458f 100644 --- a/crates/torii/client/src/client/mod.rs +++ b/crates/torii/client/src/client/mod.rs @@ -110,26 +110,25 @@ impl Client { /// /// If the requested entity is not among the synced entities, it will attempt to fetch it from /// the RPC. - pub async fn entity(&self, entity: &Query) -> Result, Error> { - let Some(mut schema) = self.metadata.read().model(&entity.model).map(|m| m.schema.clone()) + pub async fn entity(&self, query: &Query) -> Result, Error> { + let Some(mut schema) = self.metadata.read().model(&query.model).map(|m| m.schema.clone()) else { return Ok(None); }; - let keys = if let Clause::Keys(clause) = entity.clone().clause { + let keys = if let Clause::Keys(clause) = query.clone().clause { clause.keys } else { return Err(Error::UnsupportedQuery); }; - if !self.subscribed_entities.is_synced(entity) { - let model = self.world_reader.model(&entity.model).await?; + if !self.subscribed_entities.is_synced(query) { + let model = self.world_reader.model(&query.model).await?; return Ok(Some(model.entity(&keys).await?)); } let Ok(Some(raw_values)) = self.storage.get_entity_storage( - cairo_short_string_to_felt(&entity.model) - .map_err(ParseError::CairoShortStringToFelt)?, + cairo_short_string_to_felt(&query.model).map_err(ParseError::CairoShortStringToFelt)?, &keys, ) else { return Ok(Some(schema)); @@ -138,7 +137,7 @@ impl Client { let layout = self .metadata .read() - .model(&entity.model) + .model(&query.model) .map(|m| m.layout.clone()) .expect("qed; layout should exist"); @@ -170,18 +169,18 @@ impl Client { /// Adds entities to the list of entities to be synced. /// /// NOTE: This will establish a new subscription stream with the server. - pub async fn add_entities_to_sync(&self, entities: Vec) -> Result<(), Error> { - for entity in &entities { - let keys = if let Clause::Keys(clause) = entity.clone().clause { + pub async fn add_entities_to_sync(&self, queries: Vec) -> Result<(), Error> { + for query in &queries { + let keys = if let Clause::Keys(clause) = query.clone().clause { clause.keys } else { return Err(Error::UnsupportedQuery); }; - self.initiate_entity(&entity.model, keys.clone()).await?; + self.initiate_entity(&query.model, keys.clone()).await?; } - self.subscribed_entities.add_entities(entities)?; + self.subscribed_entities.add_entities(queries)?; let updated_entities = self.subscribed_entities.entities.read().clone().into_iter().collect(); @@ -197,8 +196,8 @@ impl Client { /// Removes entities from the list of entities to be synced. /// /// NOTE: This will establish a new subscription stream with the server. - pub async fn remove_entities_to_sync(&self, entities: Vec) -> Result<(), Error> { - self.subscribed_entities.remove_entities(entities)?; + pub async fn remove_entities_to_sync(&self, queries: Vec) -> Result<(), Error> { + self.subscribed_entities.remove_entities(queries)?; let updated_entities = self.subscribed_entities.entities.read().clone().into_iter().collect(); diff --git a/crates/torii/client/src/client/storage.rs b/crates/torii/client/src/client/storage.rs index fd7221640b..ed89258806 100644 --- a/crates/torii/client/src/client/storage.rs +++ b/crates/torii/client/src/client/storage.rs @@ -203,13 +203,13 @@ mod tests { fn err_if_set_values_too_many() { let storage = create_dummy_storage(); let keys = vec![felt!("0x12345")]; - let entity = Query { + let query = Query { model: "Position".into(), clause: Clause::Keys(KeysClause { keys: keys.clone() }), }; let values = vec![felt!("1"), felt!("2"), felt!("3"), felt!("4"), felt!("5")]; - let model = cairo_short_string_to_felt(&entity.model).unwrap(); + let model = cairo_short_string_to_felt(&query.model).unwrap(); let result = storage.set_entity_storage(model, keys, values); assert!(storage.storage.read().is_empty()); @@ -223,13 +223,13 @@ mod tests { fn err_if_set_values_too_few() { let storage = create_dummy_storage(); let keys = vec![felt!("0x12345")]; - let entity = Query { + let query = Query { model: "Position".into(), clause: Clause::Keys(KeysClause { keys: keys.clone() }), }; let values = vec![felt!("1"), felt!("2")]; - let model = cairo_short_string_to_felt(&entity.model).unwrap(); + let model = cairo_short_string_to_felt(&query.model).unwrap(); let result = storage.set_entity_storage(model, keys, values); assert!(storage.storage.read().is_empty()); @@ -243,14 +243,14 @@ mod tests { fn set_and_get_entity_value() { let storage = create_dummy_storage(); let keys = vec![felt!("0x12345")]; - let entity = Query { + let query = Query { model: "Position".into(), clause: Clause::Keys(KeysClause { keys: keys.clone() }), }; assert!(storage.storage.read().is_empty(), "storage must be empty initially"); - let model = storage.metadata.read().model(&entity.model).cloned().unwrap(); + let model = storage.metadata.read().model(&query.model).cloned().unwrap(); let expected_storage_addresses = compute_all_storage_addresses( cairo_short_string_to_felt(&model.name).unwrap(), @@ -259,7 +259,7 @@ mod tests { ); let expected_values = vec![felt!("1"), felt!("2"), felt!("3"), felt!("4")]; - let model_name_in_felt = cairo_short_string_to_felt(&entity.model).unwrap(); + let model_name_in_felt = cairo_short_string_to_felt(&query.model).unwrap(); storage .set_entity_storage(model_name_in_felt, keys.clone(), expected_values.clone()) diff --git a/crates/torii/client/src/client/subscription.rs b/crates/torii/client/src/client/subscription.rs index 6c95a20f4f..1935f727d2 100644 --- a/crates/torii/client/src/client/subscription.rs +++ b/crates/torii/client/src/client/subscription.rs @@ -90,12 +90,12 @@ impl SubscribedEntities { Ok(()) } - pub(super) fn remove_entity(&self, entity: Query) -> Result<(), Error> { - if !self.entities.write().remove(&entity) { + pub(super) fn remove_entity(&self, query: Query) -> Result<(), Error> { + if !self.entities.write().remove(&query) { return Ok(()); } - let keys = if let Clause::Keys(clause) = entity.clause { + let keys = if let Clause::Keys(clause) = query.clause { clause.keys } else { return Err(Error::UnsupportedQuery); @@ -105,13 +105,12 @@ impl SubscribedEntities { .metadata .read() .models - .get(&entity.model) + .get(&query.model) .map(|c| c.packed_size) - .ok_or(Error::UnknownModel(entity.model.clone()))?; + .ok_or(Error::UnknownModel(query.model.clone()))?; let storage_addresses = compute_all_storage_addresses( - cairo_short_string_to_felt(&entity.model) - .map_err(ParseError::CairoShortStringToFelt)?, + cairo_short_string_to_felt(&query.model).map_err(ParseError::CairoShortStringToFelt)?, &keys, model_packed_size, ); @@ -206,11 +205,7 @@ impl SubscriptionService { let storage_entries = diff.storage_diffs.into_iter().find_map(|d| { let expected = self.world_metadata.read().world_address; let current = d.address; - if current == expected { - Some(d.storage_entries) - } else { - None - } + if current == expected { Some(d.storage_entries) } else { None } }); let Some(entries) = storage_entries else { @@ -300,26 +295,26 @@ mod tests { let metadata = self::create_dummy_metadata(); - let entity = Query { model: model_name, clause: Clause::Keys(KeysClause { keys }) }; + let query = Query { model: model_name, clause: Clause::Keys(KeysClause { keys }) }; let subscribed_entities = super::SubscribedEntities::new(Arc::new(RwLock::new(metadata))); - subscribed_entities.add_entities(vec![entity.clone()]).expect("able to add entity"); + subscribed_entities.add_entities(vec![query.clone()]).expect("able to add entity"); let actual_storage_addresses_count = subscribed_entities.subscribed_storage_addresses.read().len(); let actual_storage_addresses = subscribed_entities.subscribed_storage_addresses.read().clone(); - assert!(subscribed_entities.entities.read().contains(&entity)); + assert!(subscribed_entities.entities.read().contains(&query)); assert_eq!(actual_storage_addresses_count, expected_storage_addresses.len()); assert!(expected_storage_addresses.all(|addr| actual_storage_addresses.contains(&addr))); - subscribed_entities.remove_entities(vec![entity.clone()]).expect("able to remove entities"); + subscribed_entities.remove_entities(vec![query.clone()]).expect("able to remove entities"); let actual_storage_addresses_count_after = subscribed_entities.subscribed_storage_addresses.read().len(); assert_eq!(actual_storage_addresses_count_after, 0); - assert!(!subscribed_entities.entities.read().contains(&entity)); + assert!(!subscribed_entities.entities.read().contains(&query)); } } diff --git a/crates/torii/core/src/error.rs b/crates/torii/core/src/error.rs index 49da62fbcd..cdcf6b9b95 100644 --- a/crates/torii/core/src/error.rs +++ b/crates/torii/core/src/error.rs @@ -1,4 +1,3 @@ -use dojo_types::schema::QueryError; use starknet::core::types::{FromByteSliceError, FromStrError}; use starknet::core::utils::CairoShortStringToFeltError; diff --git a/crates/torii/grpc/src/server/mod.rs b/crates/torii/grpc/src/server/mod.rs index 6d426fbbce..7d226559a1 100644 --- a/crates/torii/grpc/src/server/mod.rs +++ b/crates/torii/grpc/src/server/mod.rs @@ -1,6 +1,5 @@ pub mod error; pub mod logger; -pub mod query; pub mod subscription; use std::future::Future; diff --git a/crates/torii/grpc/src/server/query.rs b/crates/torii/grpc/src/server/query.rs deleted file mode 100644 index 511cac2824..0000000000 --- a/crates/torii/grpc/src/server/query.rs +++ /dev/null @@ -1,64 +0,0 @@ -use serde::{Deserialize, Serialize}; -use starknet_crypto::FieldElement; - -#[derive(Debug, thiserror::Error)] -pub enum QueryError { - #[error("unsupported query")] - UnsupportedQuery, -} - -#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Eq, Clone)] -pub struct Query { - pub model: String, - pub clause: Clause, -} - -#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Eq, Clone)] -pub enum Clause { - Keys(KeysClause), - Attribute(AttributeClause), - Composite(CompositeClause), -} - -#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Eq, Clone)] -pub struct KeysClause { - pub keys: Vec, -} - -#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Eq, Clone)] -pub struct AttributeClause { - pub attribute: String, - pub operator: ComparisonOperator, - pub value: Value, -} - -#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Eq, Clone)] -pub struct CompositeClause { - pub operator: LogicalOperator, - pub clauses: Vec, -} - -#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Eq, Clone)] -pub enum LogicalOperator { - And, - Or, -} - -#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Eq, Clone)] -pub enum ComparisonOperator { - Eq, - Neq, - Gt, - Gte, - Lt, - Lte, -} - -#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Eq, Clone)] -pub enum Value { - String(String), - Int(i64), - UInt(u64), - Bool(bool), - Bytes(Vec), -} diff --git a/crates/torii/grpc/src/server/subscription.rs b/crates/torii/grpc/src/server/subscription.rs index fe15c7577a..983451fec4 100644 --- a/crates/torii/grpc/src/server/subscription.rs +++ b/crates/torii/grpc/src/server/subscription.rs @@ -3,7 +3,6 @@ use std::future::Future; use std::sync::Arc; use std::task::Poll; -use dojo_types::schema::EntityQuery; use futures_util::future::BoxFuture; use futures_util::FutureExt; use rand::Rng;