Skip to content

Commit

Permalink
Torii indexer add id col to model tables (#979)
Browse files Browse the repository at this point in the history
* Torii indexer add id col to model tables

* fix clippy too many arguments

* fix tests
  • Loading branch information
broody authored Oct 5, 2023
1 parent 5fa2e1e commit b9bb438
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 141 deletions.
4 changes: 2 additions & 2 deletions crates/torii/core/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ async fn process_event<P: Provider + Sync>(
db: &mut Sql,
provider: &P,
processors: &[Box<dyn EventProcessor<P>>],
block: &BlockWithTxs,
_block: &BlockWithTxs,
invoke_receipt: &InvokeTransactionReceipt,
event: &Event,
event_idx: usize,
Expand All @@ -241,7 +241,7 @@ async fn process_event<P: Provider + Sync>(

for processor in processors {
if get_selector_from_name(&processor.event_key())? == event.keys[0] {
processor.process(world, db, provider, block, invoke_receipt, event).await?;
processor.process(world, db, provider, invoke_receipt, event, event_idx).await?;
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/torii/core/src/processors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ pub trait EventProcessor<P: Provider + Sync> {
world: &WorldContractReader<'_, P>,
db: &mut Sql,
provider: &P,
block: &BlockWithTxs,
invoke_receipt: &InvokeTransactionReceipt,
event: &Event,
event_idx: usize,
) -> Result<(), Error>;
}

Expand Down
4 changes: 2 additions & 2 deletions crates/torii/core/src/processors/register_model.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::{Error, Ok, Result};
use async_trait::async_trait;
use starknet::core::types::{BlockId, BlockTag, BlockWithTxs, Event, InvokeTransactionReceipt};
use starknet::core::types::{BlockId, BlockTag, Event, InvokeTransactionReceipt};
use starknet::core::utils::parse_cairo_short_string;
use starknet::providers::Provider;
use torii_client::contract::world::WorldContractReader;
Expand All @@ -23,9 +23,9 @@ impl<P: Provider + Sync + 'static> EventProcessor<P> for RegisterModelProcessor
world: &WorldContractReader<'_, P>,
db: &mut Sql,
_provider: &P,
_block: &BlockWithTxs,
_invoke_receipt: &InvokeTransactionReceipt,
event: &Event,
_event_idx: usize,
) -> Result<(), Error> {
let name = parse_cairo_short_string(&event.data[0])?;

Expand Down
4 changes: 2 additions & 2 deletions crates/torii/core/src/processors/register_system.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::{Error, Ok, Result};
use async_trait::async_trait;
use dojo_world::manifest::System;
use starknet::core::types::{BlockWithTxs, Event, InvokeTransactionReceipt};
use starknet::core::types::{Event, InvokeTransactionReceipt};
use starknet::core::utils::parse_cairo_short_string;
use starknet::providers::Provider;
use torii_client::contract::world::WorldContractReader;
Expand All @@ -24,9 +24,9 @@ impl<P: Provider + Sync> EventProcessor<P> for RegisterSystemProcessor {
_world: &WorldContractReader<'_, P>,
db: &mut Sql,
_provider: &P,
_block: &BlockWithTxs,
_invoke_receipt: &InvokeTransactionReceipt,
event: &Event,
_event_idx: usize,
) -> Result<(), Error> {
let name = parse_cairo_short_string(&event.data[0])?;

Expand Down
8 changes: 4 additions & 4 deletions crates/torii/core/src/processors/store_set_record.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::{Error, Ok, Result};
use async_trait::async_trait;
use starknet::core::types::{BlockId, BlockTag, BlockWithTxs, Event, InvokeTransactionReceipt};
use starknet::core::types::{BlockId, BlockTag, Event, InvokeTransactionReceipt};
use starknet::core::utils::parse_cairo_short_string;
use starknet::providers::Provider;
use starknet_crypto::FieldElement;
Expand All @@ -27,17 +27,17 @@ impl<P: Provider + Sync + 'static> EventProcessor<P> for StoreSetRecordProcessor
world: &WorldContractReader<'_, P>,
db: &mut Sql,
_provider: &P,
_block: &BlockWithTxs,
_transaction_receipt: &InvokeTransactionReceipt,
transaction_receipt: &InvokeTransactionReceipt,
event: &Event,
event_idx: usize,
) -> Result<(), Error> {
let name = parse_cairo_short_string(&event.data[MODEL_INDEX])?;
info!("store set record: {}", name);

let model = world.model(&name, BlockId::Tag(BlockTag::Pending)).await?;
let keys = values_at(&event.data, NUM_KEYS_INDEX)?;
let entity = model.entity(keys, BlockId::Tag(BlockTag::Pending)).await?;
db.set_entity(entity).await?;
db.set_entity(entity, transaction_receipt.block_number, event_idx).await?;
Ok(())
}
}
Expand Down
31 changes: 19 additions & 12 deletions crates/torii/core/src/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl Sql {
Ok(())
}

pub async fn set_entity(&mut self, entity: Ty) -> Result<()> {
pub async fn set_entity(&mut self, entity: Ty, block_num: u64, event_idx: usize) -> Result<()> {
let keys = if let Ty::Struct(s) = &entity {
let mut keys = Vec::new();
for m in s.keys() {
Expand Down Expand Up @@ -162,7 +162,8 @@ impl Sql {
));

let path = vec![entity.name()];
self.build_set_entity_queries_recursive(path, &entity_id, &entity);
let id = format!("0x{:064x}:0x{:04x}", block_num, event_idx);
self.build_set_entity_queries_recursive(path, &id, &entity_id, &entity);

self.execute().await?;

Expand Down Expand Up @@ -222,7 +223,7 @@ impl Sql {
let keys_str = felts_sql_string(&event.keys);
let data_str = felts_sql_string(&event.data);

let id = format!("{:#x}:{}", transaction_hash, event_idx);
let id = format!("0x{:064x}:0x{:04x}", transaction_hash, event_idx);
let query = format!(
"INSERT OR IGNORE INTO events (id, keys, data, transaction_hash) VALUES ('{}', '{}', \
'{}', '{:#x}')",
Expand Down Expand Up @@ -263,12 +264,18 @@ impl Sql {
}
}

fn build_set_entity_queries_recursive(&mut self, path: Vec<String>, id: &str, entity: &Ty) {
fn build_set_entity_queries_recursive(
&mut self,
path: Vec<String>,
id: &str,
entity_id: &str,
entity: &Ty,
) {
match entity {
Ty::Struct(s) => {
let table_id = path.join("$");
let mut columns = vec!["entity_id".to_string()];
let mut values = vec![format!("'{id}'")];
let mut columns = vec!["id".to_string(), "entity_id".to_string()];
let mut values = vec![format!("'{id}', '{entity_id}'")];

for member in s.children.iter() {
match &member.ty {
Expand All @@ -295,7 +302,9 @@ impl Sql {
let mut path_clone = path.clone();
path_clone.push(member.ty.name());

self.build_set_entity_queries_recursive(path_clone, id, &member.ty);
self.build_set_entity_queries_recursive(
path_clone, id, entity_id, &member.ty,
);
}
}
}
Expand All @@ -304,7 +313,7 @@ impl Sql {
let mut path_clone = path.clone();
path_clone.push(child.1.name());
// self.build_entity_query(path_clone.clone(), id, &child.1);
self.build_set_entity_queries_recursive(path_clone, id, &child.1);
self.build_set_entity_queries_recursive(path_clone, id, entity_id, &child.1);
}
}
_ => {}
Expand All @@ -315,7 +324,7 @@ impl Sql {
let table_id = path.join("$");

let mut query = format!(
"CREATE TABLE IF NOT EXISTS [{table_id}] (entity_id TEXT NOT NULL PRIMARY KEY, "
"CREATE TABLE IF NOT EXISTS [{table_id}] (id TEXT NOT NULL PRIMARY KEY, entity_id, "
);

if let Ty::Struct(s) = model {
Expand Down Expand Up @@ -352,9 +361,7 @@ impl Sql {
// If this is not the Model's root table, create a reference to the parent.
if path.len() > 1 {
let parent_table_id = path[..path.len() - 1].join("$");
query.push_str(&format!(
"FOREIGN KEY (entity_id) REFERENCES {parent_table_id} (entity_id), "
));
query.push_str(&format!("FOREIGN KEY (id) REFERENCES {parent_table_id} (id), "));
};

query.push_str("FOREIGN KEY (entity_id) REFERENCES entities(id));");
Expand Down
Loading

0 comments on commit b9bb438

Please sign in to comment.