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(torii-grpc): Add total_count on RetrieveEntitiesResponse #1545

Merged
merged 4 commits into from
Feb 24, 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
3 changes: 2 additions & 1 deletion crates/torii/client/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@
/// type of entites matching keys and/or models.
pub async fn entities(&self, query: Query) -> Result<Vec<Entity>, Error> {
let mut grpc_client = self.inner.write().await;
let RetrieveEntitiesResponse { entities } = grpc_client.retrieve_entities(query).await?;
let RetrieveEntitiesResponse { entities, total_count: _ } =
grpc_client.retrieve_entities(query).await?;

Check warning on line 162 in crates/torii/client/src/client/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/client/src/client/mod.rs#L161-L162

Added lines #L161 - L162 were not covered by tests
Ok(entities.into_iter().map(TryInto::try_into).collect::<Result<Vec<Entity>, _>>()?)
}

Expand Down
1 change: 1 addition & 0 deletions crates/torii/grpc/proto/world.proto
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ message RetrieveEntitiesRequest {

message RetrieveEntitiesResponse {
repeated types.Entity entities = 1;
uint32 total_count = 2;
}
60 changes: 49 additions & 11 deletions crates/torii/grpc/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
&self,
limit: u32,
offset: u32,
) -> Result<Vec<proto::types::Entity>, Error> {
) -> Result<(Vec<proto::types::Entity>, u32), Error> {

Check warning on line 123 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L123

Added line #L123 was not covered by tests
self.entities_by_hashed_keys(None, limit, offset).await
}

Expand All @@ -129,7 +129,7 @@
hashed_keys: Option<proto::types::HashedKeysClause>,
limit: u32,
offset: u32,
) -> Result<Vec<proto::types::Entity>, Error> {
) -> Result<(Vec<proto::types::Entity>, u32), Error> {

Check warning on line 132 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L132

Added line #L132 was not covered by tests
// TODO: use prepared statement for where clause
let filter_ids = match hashed_keys {
Some(hashed_keys) => {
Expand All @@ -148,6 +148,18 @@
None => String::new(),
};

// count query that matches filter_ids
let count_query = format!(
r#"
SELECT count(*)
FROM entities
{filter_ids}
"#
);
// total count of rows without limit and offset
let total_count: u32 = sqlx::query_scalar(&count_query).fetch_one(&self.pool).await?;

Check warning on line 160 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L154-L160

Added lines #L154 - L160 were not covered by tests

// query to filter with limit and offset

Check warning on line 162 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L162

Added line #L162 was not covered by tests
let query = format!(
r#"
SELECT entities.id, group_concat(entity_model.model_id) as model_names
Expand Down Expand Up @@ -188,15 +200,15 @@
})
}

Ok(entities)
Ok((entities, total_count))
}

async fn entities_by_keys(
&self,
keys_clause: proto::types::KeysClause,
limit: u32,
offset: u32,
) -> Result<Vec<proto::types::Entity>, Error> {
) -> Result<(Vec<proto::types::Entity>, u32), Error> {

Check warning on line 211 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L211

Added line #L211 was not covered by tests
let keys = keys_clause
.keys
.iter()
Expand All @@ -211,6 +223,20 @@
.collect::<Result<Vec<_>, Error>>()?;
let keys_pattern = keys.join("/") + "/%";

let count_query = format!(
r#"
SELECT count(*)
FROM entities
JOIN entity_model ON entities.id = entity_model.entity_id
WHERE entity_model.model_id = '{}' and entities.keys LIKE ?
"#,
keys_clause.model
);

// total count of rows that matches keys_pattern without limit and offset

Check warning on line 236 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L226-L236

Added lines #L226 - L236 were not covered by tests
let total_count =
sqlx::query_scalar(&count_query).bind(&keys_pattern).fetch_one(&self.pool).await?;

Check warning on line 239 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L239

Added line #L239 was not covered by tests
let models_query = format!(
r#"
SELECT group_concat(entity_model.model_id) as model_names
Expand All @@ -229,6 +255,7 @@
let model_names = models_str.split(',').collect::<Vec<&str>>();
let schemas = self.model_cache.schemas(model_names).await?;

// query to filter with limit and offset

Check warning on line 258 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L258

Added line #L258 was not covered by tests
let entities_query = format!(
"{} WHERE entities.keys LIKE ? ORDER BY entities.event_id DESC LIMIT ? OFFSET ?",
build_sql_query(&schemas)?
Expand All @@ -240,15 +267,21 @@
.fetch_all(&self.pool)
.await?;

db_entities.iter().map(|row| Self::map_row_to_entity(row, &schemas)).collect()
Ok((

Check warning on line 270 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L270

Added line #L270 was not covered by tests
db_entities
.iter()
.map(|row| Self::map_row_to_entity(row, &schemas))
.collect::<Result<Vec<_>, Error>>()?,
total_count,
))

Check warning on line 276 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L273-L276

Added lines #L273 - L276 were not covered by tests
}

async fn entities_by_member(
&self,
member_clause: proto::types::MemberClause,
_limit: u32,
_offset: u32,
) -> Result<Vec<proto::types::Entity>, Error> {
) -> Result<(Vec<proto::types::Entity>, u32), Error> {

Check warning on line 284 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L284

Added line #L284 was not covered by tests
let comparison_operator = ComparisonOperator::from_repr(member_clause.operator as usize)
.expect("invalid comparison operator");

Expand Down Expand Up @@ -297,16 +330,21 @@

let db_entities =
sqlx::query(&member_query).bind(comparison_value).fetch_all(&self.pool).await?;

db_entities.iter().map(|row| Self::map_row_to_entity(row, &schemas)).collect()
let entities_collection = db_entities
.iter()
.map(|row| Self::map_row_to_entity(row, &schemas))
.collect::<Result<Vec<_>, Error>>()?;
// Since there is not limit and offset, total_count is same as number of entities
let total_count = entities_collection.len() as u32;

Check warning on line 338 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L333-L338

Added lines #L333 - L338 were not covered by tests
Ok((entities_collection, total_count))
}

async fn entities_by_composite(
&self,
_composite: proto::types::CompositeClause,
_limit: u32,
_offset: u32,
) -> Result<Vec<proto::types::Entity>, Error> {
) -> Result<(Vec<proto::types::Entity>, u32), Error> {

Check warning on line 347 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L347

Added line #L347 was not covered by tests
// TODO: Implement
Err(QueryError::UnsupportedQuery.into())
}
Expand Down Expand Up @@ -373,7 +411,7 @@
&self,
query: proto::types::Query,
) -> Result<proto::world::RetrieveEntitiesResponse, Error> {
let entities = match query.clause {
let (entities, total_count) = match query.clause {
None => self.entities_all(query.limit, query.offset).await?,
Some(clause) => {
let clause_type =
Expand Down Expand Up @@ -409,7 +447,7 @@
}
};

Ok(RetrieveEntitiesResponse { entities })
Ok(RetrieveEntitiesResponse { entities, total_count })
}

fn map_row_to_entity(row: &SqliteRow, schemas: &[Ty]) -> Result<proto::types::Entity, Error> {
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/types-test/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ source = "git+https://github.com/dojoengine/dojo?tag=v0.3.11#1e651b5d4d3b79b14a7

[[package]]
name = "types_test"
version = "0.5.0"
version = "0.5.1"
dependencies = [
"dojo",
]
2 changes: 1 addition & 1 deletion examples/spawn-and-move/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dependencies = [

[[package]]
name = "dojo_examples"
version = "0.5.0"
version = "0.5.1"
dependencies = [
"dojo",
]
Expand Down
Loading