Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
broody committed Nov 14, 2023
1 parent e5b32cc commit 36e5b18
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 106 deletions.
29 changes: 14 additions & 15 deletions crates/torii/client/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<Ty>, Error> {
let Some(mut schema) = self.metadata.read().model(&entity.model).map(|m| m.schema.clone())
pub async fn entity(&self, query: &Query) -> Result<Option<Ty>, 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));
Expand All @@ -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");

Expand Down Expand Up @@ -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<Query>) -> 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<Query>) -> 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();
Expand All @@ -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<Query>) -> Result<(), Error> {
self.subscribed_entities.remove_entities(entities)?;
pub async fn remove_entities_to_sync(&self, queries: Vec<Query>) -> Result<(), Error> {
self.subscribed_entities.remove_entities(queries)?;

let updated_entities =
self.subscribed_entities.entities.read().clone().into_iter().collect();
Expand Down
14 changes: 7 additions & 7 deletions crates/torii/client/src/client/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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(),
Expand All @@ -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())
Expand Down
29 changes: 12 additions & 17 deletions crates/torii/client/src/client/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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));
}
}
1 change: 0 additions & 1 deletion crates/torii/core/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use dojo_types::schema::QueryError;
use starknet::core::types::{FromByteSliceError, FromStrError};
use starknet::core::utils::CairoShortStringToFeltError;

Expand Down
1 change: 0 additions & 1 deletion crates/torii/grpc/src/server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod error;
pub mod logger;
pub mod query;
pub mod subscription;

use std::future::Future;
Expand Down
64 changes: 0 additions & 64 deletions crates/torii/grpc/src/server/query.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/torii/grpc/src/server/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 36e5b18

Please sign in to comment.