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): entity update without being set before #3072

Merged
merged 9 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 13 additions & 8 deletions crates/torii/grpc/src/server/subscriptions/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,19 @@ impl Service {
) -> Result<(), Error> {
let mut closed_stream = Vec::new();
let hashed = Felt::from_str(&entity.id).map_err(ParseError::FromStr)?;
// sometimes for some reason keys isxx empty. investigate the issue
let keys = entity
.keys
.trim_end_matches(SQL_FELT_DELIMITER)
.split(SQL_FELT_DELIMITER)
.map(Felt::from_str)
.collect::<Result<Vec<_>, _>>()
.map_err(ParseError::FromStr)?;
// keys is empty when an entity is updated with StoreUpdateRecord or Member but the entity has never been set before.
// In that case, we dont know the keys
let keys = if entity.keys.is_empty() {
vec![]
} else {
entity
.keys
.trim_end_matches(SQL_FELT_DELIMITER)
.split(SQL_FELT_DELIMITER)
.map(Felt::from_str)
.collect::<Result<Vec<_>, _>>()
.map_err(ParseError::FromStr)?
};

for (idx, sub) in subs.subscribers.read().await.iter() {
// Check if the subscriber is interested in this entity
Expand Down
17 changes: 7 additions & 10 deletions crates/torii/sqlite/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
use tokio::sync::{oneshot, Semaphore};
use tokio::task::JoinSet;
use tokio::time::Instant;
use tracing::{debug, error};
use tracing::{debug, error, warn};

use crate::constants::TOKENS_TABLE;
use crate::simple_broker::SimpleBroker;
Expand Down Expand Up @@ -451,15 +451,12 @@ impl<'c, P: Provider + Sync + Send + 'static> Executor<'c, P> {
entity_updated.updated_model = Some(entity);
entity_updated.deleted = false;

let optimistic_entity = OptimisticEntity {
id: entity_updated.id.clone(),
keys: entity_updated.keys.clone(),
event_id: entity_updated.event_id.clone(),
executed_at: entity_updated.executed_at,
created_at: entity_updated.created_at,
updated_at: entity_updated.updated_at,
updated_model: entity_updated.updated_model.clone(),
deleted: entity_updated.deleted,
if entity_updated.keys.is_empty() {
warn!(target: LOG_TARGET, "Entity update ignored. An entity must be set with StoreSetRecord before it can be updated with StoreUpdateRecord or StoreUpdateMember.");
}

let optimistic_entity = unsafe {
std::mem::transmute::<EntityUpdated, OptimisticEntity>(entity_updated.clone())
};
SimpleBroker::publish(optimistic_entity);

Expand Down
Loading