Skip to content

Commit

Permalink
start uupdating grpc for order and updated filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo committed Dec 5, 2024
1 parent eba095d commit 424a166
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 22 deletions.
9 changes: 8 additions & 1 deletion crates/torii/core/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ pub fn build_sql_query(
table_name: &str,
entity_relation_column: &str,
where_clause: Option<&str>,
order_by: Option<&str>,
limit: Option<u32>,
offset: Option<u32>,
) -> Result<(String, String), Error> {
Expand Down Expand Up @@ -196,7 +197,12 @@ pub fn build_sql_query(
count_query += &format!(" WHERE {}", where_clause);
}

query += &format!(" ORDER BY {}.event_id DESC", table_name);
// Use custom order by if provided, otherwise default to event_id DESC
if let Some(order_clause) = order_by {
query += &format!(" ORDER BY {}", order_clause);
} else {
query += &format!(" ORDER BY {}.event_id DESC", table_name);
}

if let Some(limit) = limit {
query += &format!(" LIMIT {}", limit);
Expand Down Expand Up @@ -487,6 +493,7 @@ mod tests {
None,
None,
None,
None,
)
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion crates/torii/core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,4 @@ pub struct ContractCursor {
pub contract_address: String,
pub last_pending_block_tx: Option<String>,
pub last_pending_block_contract_tx: Option<String>,
}
}
22 changes: 9 additions & 13 deletions crates/torii/grpc/proto/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,17 @@ message ModelUpdate {
ModelDiff model_diff = 2;
}

message Pagination {
// Updated at time-based pagination
uint64 updated_before = 1;
uint64 updated_after = 2;

// Standard offset pagination
uint32 limit = 3;
uint32 offset = 4;
}

message Query {
Clause clause = 1;
bool dont_include_hashed_keys = 2;
repeated OrderBy order_by = 3;
Pagination pagination = 4;
// Standard offset pagination
uint32 limit = 2;
uint32 offset = 3;
bool dont_include_hashed_keys = 4;
repeated OrderBy order_by = 5;

// Updated at time-based filter
uint64 updated_before = 6;
uint64 updated_after = 7;
}

message EventQuery {
Expand Down
55 changes: 48 additions & 7 deletions crates/torii/grpc/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ impl DojoWorld {
entity_relation_column: &str,
entities: Vec<(String, String)>,
dont_include_hashed_keys: bool,
where_clause: Option<&str>,
order_by: Option<&str>,
) -> Result<Vec<proto::types::Entity>, Error> {
// Group entities by their model combinations
let mut model_groups: HashMap<String, Vec<String>> = HashMap::new();
Expand Down Expand Up @@ -293,6 +295,11 @@ impl DojoWorld {
}
}

let where_clause = where_clause.map_or(
"[{table}].id IN (SELECT id FROM temp_entity_ids WHERE model_group = ?)",
|clause| &format!("{} AND [{table}].id IN (SELECT id FROM temp_entity_ids WHERE model_group = ?)", clause),
);

for (models_str, _) in model_groups {
let model_ids =
models_str.split(',').map(|id| Felt::from_str(id).unwrap()).collect::<Vec<_>>();
Expand All @@ -303,9 +310,8 @@ impl DojoWorld {
&schemas,
table,
entity_relation_column,
Some(&format!(
"[{table}].id IN (SELECT id FROM temp_entity_ids WHERE model_group = ?)"
)),
Some(&where_clause),
order_by,
None,
None,
)?;
Expand Down Expand Up @@ -377,6 +383,7 @@ impl DojoWorld {
limit: Option<u32>,
offset: Option<u32>,
dont_include_hashed_keys: bool,
where_clause: Option<&str>,
) -> Result<(Vec<proto::types::Entity>, u32), Error> {
// TODO: use prepared statement for where clause
let filter_ids = match hashed_keys {
Expand Down Expand Up @@ -465,9 +472,19 @@ impl DojoWorld {
limit: Option<u32>,
offset: Option<u32>,
dont_include_hashed_keys: bool,
updated_before: Option<u64>,
updated_after: Option<u64>,
order_by: Option<&str>,
) -> Result<(Vec<proto::types::Entity>, u32), Error> {
let keys_pattern = build_keys_pattern(keys_clause)?;

let mut where_clause = vec![];
if let Some(updated_before) = updated_before {
where_clause.push(format!("updated_at <= {}", updated_before));
}
if let Some(updated_after) = updated_after {
where_clause.push(format!("updated_at >= {}", updated_after));
}
// total count of rows that matches keys_pattern without limit and offset
let count_query = format!(
r#"
Expand Down Expand Up @@ -628,6 +645,9 @@ impl DojoWorld {
limit: Option<u32>,
offset: Option<u32>,
dont_include_hashed_keys: bool,
updated_before: Option<u64>,
updated_after: Option<u64>,
order_by: Option<&str>,
) -> Result<(Vec<proto::types::Entity>, u32), Error> {
let comparison_operator = ComparisonOperator::from_repr(member_clause.operator as usize)
.expect("invalid comparison operator");
Expand Down Expand Up @@ -675,14 +695,23 @@ impl DojoWorld {
self.model_cache.models(&model_ids).await?.into_iter().map(|m| m.schema).collect();

// Use the member name directly as the column name since it's already flattened
let mut where_clause = format!(
"[{}].[{}] {comparison_operator} ?",
member_clause.model, member_clause.member
);
if let Some(updated_before) = updated_before {
where_clause += &format!(" AND updated_at <= {}", updated_before);
}
if let Some(updated_after) = updated_after {
where_clause += &format!(" AND updated_at >= {}", updated_after);
}

let (entity_query, count_query) = build_sql_query(
&schemas,
table,
entity_relation_column,
Some(&format!(
"[{}].[{}] {comparison_operator} ?",
member_clause.model, member_clause.member
)),
Some(&where_clause),
order_by,
limit,
offset,
)?;
Expand Down Expand Up @@ -909,6 +938,15 @@ impl DojoWorld {
entity_relation_column: &str,
query: proto::types::Query,
) -> Result<proto::world::RetrieveEntitiesResponse, Error> {
let order_by = query
.order_by
.map(|order_by| {
format!(
"[{}] [{}] {}",
order_by.model, order_by.member, order_by.direction
)
});

let (entities, total_count) = match query.clause {
None => {
self.entities_all(
Expand Down Expand Up @@ -963,6 +1001,9 @@ impl DojoWorld {
Some(query.limit),
Some(query.offset),
query.dont_include_hashed_keys,
query.updated_before,
query.updated_after,
order_by,
)
.await?
}
Expand Down

0 comments on commit 424a166

Please sign in to comment.