Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(grpc): add entity models for returned models #2805

Merged
merged 5 commits into from
Dec 13, 2024
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
1 change: 1 addition & 0 deletions crates/torii/grpc/proto/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ message Query {
uint32 offset = 3;
bool dont_include_hashed_keys = 4;
repeated OrderBy order_by = 5;
repeated string entity_models = 6;
}

message EventQuery {
Expand Down
58 changes: 51 additions & 7 deletions crates/torii/grpc/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration;

use dojo_types::naming::compute_selector_from_tag;
use dojo_types::primitive::{Primitive, PrimitiveError};
use dojo_types::schema::Ty;
use dojo_world::contracts::naming::compute_selector_from_names;
Expand Down Expand Up @@ -227,6 +228,7 @@ impl DojoWorld {
offset: u32,
dont_include_hashed_keys: bool,
order_by: Option<&str>,
entity_models: Vec<String>,
) -> Result<(Vec<proto::types::Entity>, u32), Error> {
self.query_by_hashed_keys(
table,
Expand All @@ -237,6 +239,7 @@ impl DojoWorld {
Some(offset),
dont_include_hashed_keys,
order_by,
entity_models,
)
.await
}
Expand All @@ -262,7 +265,11 @@ impl DojoWorld {
entities: Vec<(String, String)>,
dont_include_hashed_keys: bool,
order_by: Option<&str>,
entity_models: Vec<String>,
) -> Result<Vec<proto::types::Entity>, Error> {
let entity_models =
entity_models.iter().map(|tag| compute_selector_from_tag(tag)).collect::<Vec<Felt>>();

tracing::debug!(
"Fetching entities from table {table} with {} entity/model pairs",
entities.len()
Expand Down Expand Up @@ -315,10 +322,27 @@ impl DojoWorld {
let query_start = std::time::Instant::now();
for (models_str, entity_ids) in &model_groups {
tracing::debug!("Processing model group with {} entities", entity_ids.len());
let model_ids =
models_str.split(',').map(|id| Felt::from_str(id).unwrap()).collect::<Vec<_>>();
let schemas =
self.model_cache.models(&model_ids).await?.into_iter().map(|m| m.schema).collect();
let model_ids = models_str
.split(',')
.filter_map(|id| {
let model_id = Felt::from_str(id).unwrap();
if entity_models.is_empty() || entity_models.contains(&model_id) {
Some(model_id)
} else {
None
}
})
glihm marked this conversation as resolved.
Show resolved Hide resolved
.collect::<Vec<_>>();
let schemas = self
.model_cache
.models(&model_ids)
.await?
.into_iter()
.map(|m| m.schema)
.collect::<Vec<_>>();
if schemas.is_empty() {
continue;
}

let (entity_query, _) = build_sql_query(
&schemas,
Expand Down Expand Up @@ -405,6 +429,7 @@ impl DojoWorld {
offset: Option<u32>,
dont_include_hashed_keys: bool,
order_by: Option<&str>,
entity_models: Vec<String>,
) -> Result<(Vec<proto::types::Entity>, u32), Error> {
// TODO: use prepared statement for where clause
let filter_ids = match hashed_keys {
Expand Down Expand Up @@ -484,6 +509,7 @@ impl DojoWorld {
db_entities,
dont_include_hashed_keys,
order_by,
entity_models,
)
.await?;
Ok((entities, total_count))
Expand All @@ -500,6 +526,7 @@ impl DojoWorld {
offset: Option<u32>,
dont_include_hashed_keys: bool,
order_by: Option<&str>,
entity_models: Vec<String>,
) -> Result<(Vec<proto::types::Entity>, u32), Error> {
let keys_pattern = build_keys_pattern(keys_clause)?;

Expand Down Expand Up @@ -627,6 +654,7 @@ impl DojoWorld {
db_entities,
dont_include_hashed_keys,
order_by,
entity_models,
)
.await?;
Ok((entities, total_count))
Expand Down Expand Up @@ -670,7 +698,10 @@ impl DojoWorld {
offset: Option<u32>,
dont_include_hashed_keys: bool,
order_by: Option<&str>,
entity_models: Vec<String>,
) -> Result<(Vec<proto::types::Entity>, u32), Error> {
let entity_models =
entity_models.iter().map(|model| compute_selector_from_tag(model)).collect::<Vec<_>>();
let comparison_operator = ComparisonOperator::from_repr(member_clause.operator as usize)
.expect("invalid comparison operator");

Expand Down Expand Up @@ -710,9 +741,15 @@ impl DojoWorld {

let model_ids = models_str
.split(',')
.map(Felt::from_str)
.collect::<Result<Vec<_>, _>>()
.map_err(ParseError::FromStr)?;
.filter_map(|id| {
let model_id = Felt::from_str(id).unwrap();
if entity_models.is_empty() || entity_models.contains(&model_id) {
Some(model_id)
} else {
None
}
})
.collect::<Vec<_>>();
let schemas =
self.model_cache.models(&model_ids).await?.into_iter().map(|m| m.schema).collect();

Expand Down Expand Up @@ -760,6 +797,7 @@ impl DojoWorld {
offset: Option<u32>,
dont_include_hashed_keys: bool,
order_by: Option<&str>,
entity_models: Vec<String>,
) -> Result<(Vec<proto::types::Entity>, u32), Error> {
let (where_clause, having_clause, join_clause, bind_values) =
build_composite_clause(table, model_relation_table, &composite)?;
Expand Down Expand Up @@ -829,6 +867,7 @@ impl DojoWorld {
db_entities,
dont_include_hashed_keys,
order_by,
entity_models,
)
.await?;
Ok((entities, total_count))
Expand Down Expand Up @@ -996,6 +1035,7 @@ impl DojoWorld {
query.offset,
query.dont_include_hashed_keys,
order_by,
query.entity_models,
)
.await?
}
Expand All @@ -1018,6 +1058,7 @@ impl DojoWorld {
Some(query.offset),
query.dont_include_hashed_keys,
order_by,
query.entity_models,
)
.await?
}
Expand All @@ -1031,6 +1072,7 @@ impl DojoWorld {
Some(query.offset),
query.dont_include_hashed_keys,
order_by,
query.entity_models,
)
.await?
}
Expand All @@ -1044,6 +1086,7 @@ impl DojoWorld {
Some(query.offset),
query.dont_include_hashed_keys,
order_by,
query.entity_models,
)
.await?
}
Expand All @@ -1057,6 +1100,7 @@ impl DojoWorld {
Some(query.offset),
query.dont_include_hashed_keys,
order_by,
query.entity_models,
)
.await?
}
Expand Down
1 change: 1 addition & 0 deletions crates/torii/grpc/src/server/tests/entities_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ async fn test_entities_queries(sequencer: &RunnerCtx) {
None,
false,
None,
vec![],
)
.await
.unwrap()
Expand Down
2 changes: 2 additions & 0 deletions crates/torii/grpc/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ pub struct Query {
pub offset: u32,
pub dont_include_hashed_keys: bool,
pub order_by: Vec<OrderBy>,
pub entity_models: Vec<String>,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Eq, Clone)]
Expand Down Expand Up @@ -269,6 +270,7 @@ impl From<Query> for proto::types::Query {
offset: value.offset,
dont_include_hashed_keys: value.dont_include_hashed_keys,
order_by: value.order_by.into_iter().map(|o| o.into()).collect(),
entity_models: value.entity_models,
}
}
}
Expand Down
Loading