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

Refactor(torii-core): Optimize and clean sql statements #927

Merged
merged 5 commits into from
Oct 25, 2023
Merged
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
36 changes: 13 additions & 23 deletions crates/torii/core/src/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use chrono::{DateTime, Utc};
use dojo_types::primitive::Primitive;
use dojo_types::schema::Ty;
use sqlx::pool::PoolConnection;
use sqlx::sqlite::SqliteRow;
use sqlx::{Executor, Pool, Row, Sqlite};
use starknet::core::types::{Event, FieldElement};
use starknet_crypto::poseidon_hash_many;
Expand Down Expand Up @@ -90,31 +91,24 @@ impl Sql {
.iter()
.map(|x| <FieldElement as TryInto<u8>>::try_into(*x).unwrap())
.collect::<Vec<u8>>();
self.query_queue.push(format!(
let insert_models = format!(
"INSERT INTO models (id, name, class_hash, layout, packed_size, unpacked_size) VALUES \
('{id}', '{name}', '{class_hash:#x}', '{layout}', '{packed_size}', \
'{unpacked_size}') ON CONFLICT(id) DO UPDATE SET class_hash='{class_hash:#x}', \
layout='{layout}', packed_size='{packed_size}', unpacked_size='{unpacked_size}'",
layout='{layout}', packed_size='{packed_size}', unpacked_size='{unpacked_size}' \
RETURNING created_at",
id = model.name(),
name = model.name(),
layout = hex::encode(&layout_blob)
));
);
// execute first to get created_at
let query_result: SqliteRow = sqlx::query(&insert_models).fetch_one(&self.pool).await?;

let mut model_idx = 0_usize;
self.build_register_queries_recursive(&model, vec![model.name()], &mut model_idx);

self.execute().await?;

// Since previous query has not been executed, we have to make sure created_at exists
let created_at: DateTime<Utc> =
match sqlx::query("SELECT created_at FROM models WHERE id = ?")
.bind(model.name())
.fetch_one(&self.pool)
.await
{
Ok(query_result) => query_result.try_get("created_at")?,
Err(_) => Utc::now(),
};
let created_at: DateTime<Utc> = query_result.try_get("created_at")?;

SimpleBroker::publish(ModelType {
id: model.name(),
Expand Down Expand Up @@ -153,24 +147,20 @@ impl Sql {
};

let keys_str = felts_sql_string(&keys);
self.query_queue.push(format!(
let insert_entities = format!(
"INSERT INTO entities (id, keys, model_names, event_id) VALUES ('{}', '{}', '{}', \
'{}') ON CONFLICT(id) DO UPDATE SET model_names=excluded.model_names, \
updated_at=CURRENT_TIMESTAMP, event_id=excluded.event_id",
updated_at=CURRENT_TIMESTAMP, event_id=excluded.event_id RETURNING created_at",
entity_id, keys_str, model_names, event_id
));
);
// execute first to get created_at
let query_result: SqliteRow = sqlx::query(&insert_entities).fetch_one(&self.pool).await?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use query_as here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging. Feel free to follow up with the change


let path = vec![entity.name()];
self.build_set_entity_queries_recursive(path, event_id, &entity_id, &entity);

self.execute().await?;

let query_result = sqlx::query("SELECT created_at FROM entities WHERE id = ?")
.bind(entity_id.clone())
.fetch_one(&self.pool)
.await?;
let created_at: DateTime<Utc> = query_result.try_get("created_at")?;

SimpleBroker::publish(Entity {
id: entity_id.clone(),
keys: keys_str,
Expand Down