Skip to content

Commit

Permalink
Only update the record when fields have changed
Browse files Browse the repository at this point in the history
For the token and metadata program add additional check to only update
the record if the values of the fields have changed and the slot is
larger so the slot column only changes when fields are updated. There
are accounts that are referencd in instructions which trigger account
update but they aren't changed. Every time update statement occurs
indexs are rewritten causing locks. This noop allows insert spam but
skips updating the record and thus no triggering of index updates.
  • Loading branch information
kespinola committed Dec 18, 2024
1 parent 322a7ee commit 25a50dd
Show file tree
Hide file tree
Showing 4 changed files with 576 additions and 104 deletions.
205 changes: 172 additions & 33 deletions program_transformers/src/asset_upserts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use {
},
},
sea_orm::{
sea_query::OnConflict, ConnectionTrait, DbBackend, DbErr, EntityTrait, QueryTrait, Set,
TransactionTrait,
sea_query::{Alias, Expr, OnConflict},
Condition, ConnectionTrait, DbErr, EntityTrait, Set, TransactionTrait,
},
serde_json::value::Value,
sqlx::types::Decimal,
Expand All @@ -33,7 +33,7 @@ pub async fn upsert_assets_token_account_columns<T: ConnectionTrait + Transactio
slot_updated_token_account: Set(columns.slot_updated_token_account),
..Default::default()
};
let mut query = asset::Entity::insert(active_model)
asset::Entity::insert(active_model)
.on_conflict(
OnConflict::columns([asset::Column::Id])
.update_columns([
Expand All @@ -42,14 +42,43 @@ pub async fn upsert_assets_token_account_columns<T: ConnectionTrait + Transactio
asset::Column::Delegate,
asset::Column::SlotUpdatedTokenAccount,
])
.action_cond_where(
Condition::all()
.add(
Condition::any()
.add(
Expr::tbl(Alias::new("excluded"), asset::Column::Owner)
.ne(Expr::tbl(asset::Entity, asset::Column::Owner)),
)
.add(
Expr::tbl(Alias::new("excluded"), asset::Column::Frozen)
.ne(Expr::tbl(asset::Entity, asset::Column::Frozen)),
)
.add(
Expr::tbl(Alias::new("excluded"), asset::Column::Delegate)
.ne(Expr::tbl(asset::Entity, asset::Column::Delegate)),
)
.add(
Expr::tbl(
Alias::new("excluded"),
asset::Column::SlotUpdatedTokenAccount,
)
.ne(Expr::tbl(
asset::Entity,
asset::Column::SlotUpdatedTokenAccount,
)),
),
)
.add(
Expr::tbl(asset::Entity, asset::Column::SlotUpdatedTokenAccount)
.lte(columns.slot_updated_token_account.unwrap_or_default()),
),
)
.to_owned(),
)
.build(DbBackend::Postgres);
.exec_without_returning(txn_or_conn)
.await?;

query.sql = format!(
"{} WHERE (excluded.slot_updated_token_account >= asset.slot_updated_token_account OR asset.slot_updated_token_account IS NULL) AND asset.owner_type = 'single'",
query.sql);
txn_or_conn.execute(query).await?;
Ok(())
}

Expand Down Expand Up @@ -78,7 +107,8 @@ pub async fn upsert_assets_mint_account_columns<T: ConnectionTrait + Transaction
owner_type: Set(owner_type),
..Default::default()
};
let mut query = asset::Entity::insert(active_model)

asset::Entity::insert(active_model)
.on_conflict(
OnConflict::columns([asset::Column::Id])
.update_columns([
Expand All @@ -87,14 +117,32 @@ pub async fn upsert_assets_mint_account_columns<T: ConnectionTrait + Transaction
asset::Column::SlotUpdatedMintAccount,
asset::Column::OwnerType,
])
.action_cond_where(
Condition::all()
.add(
Condition::any()
.add(
Expr::tbl(Alias::new("excluded"), asset::Column::Supply)
.ne(Expr::tbl(asset::Entity, asset::Column::Supply)),
)
.add(
Expr::tbl(Alias::new("excluded"), asset::Column::SupplyMint)
.ne(Expr::tbl(asset::Entity, asset::Column::SupplyMint)),
)
.add(
Expr::tbl(Alias::new("excluded"), asset::Column::OwnerType)
.ne(Expr::tbl(asset::Entity, asset::Column::OwnerType)),
),
)
.add(
Expr::tbl(asset::Entity, asset::Column::SlotUpdatedMintAccount)
.lte(columns.slot_updated_mint_account as i64),
),
)
.to_owned(),
)
.build(DbBackend::Postgres);

query.sql = format!(
"{} WHERE excluded.slot_updated_mint_account >= asset.slot_updated_mint_account OR asset.slot_updated_mint_account IS NULL",
query.sql);
txn_or_conn.execute(query).await?;
.exec_without_returning(txn_or_conn)
.await?;
Ok(())
}

Expand Down Expand Up @@ -144,26 +192,15 @@ pub async fn upsert_assets_metadata_account_columns<T: ConnectionTrait + Transac
mpl_core_unknown_external_plugins: Set(columns.mpl_core_unknown_external_plugins),
..Default::default()
};
let mut query = asset::Entity::insert(active_model)

asset::Entity::insert(active_model)
.on_conflict(
OnConflict::columns([asset::Column::Id])
.update_columns([
asset::Column::SpecificationVersion,
asset::Column::SpecificationAssetClass,
asset::Column::TreeId,
asset::Column::Nonce,
asset::Column::Seq,
asset::Column::Leaf,
asset::Column::DataHash,
asset::Column::CreatorHash,
asset::Column::Compressed,
asset::Column::Compressible,
asset::Column::RoyaltyTargetType,
asset::Column::RoyaltyTarget,
asset::Column::RoyaltyAmount,
asset::Column::AssetData,
asset::Column::SlotUpdatedMetadataAccount,
asset::Column::Burnt,
asset::Column::MplCorePlugins,
asset::Column::MplCoreUnknownPlugins,
asset::Column::MplCoreCollectionNumMinted,
Expand All @@ -172,13 +209,115 @@ pub async fn upsert_assets_metadata_account_columns<T: ConnectionTrait + Transac
asset::Column::MplCoreExternalPlugins,
asset::Column::MplCoreUnknownExternalPlugins,
])
.action_cond_where(
Condition::all()
.add(
Condition::any()
.add(
Expr::tbl(
Alias::new("excluded"),
asset::Column::SpecificationAssetClass,
)
.ne(Expr::tbl(
asset::Entity,
asset::Column::SpecificationAssetClass,
)),
)
.add(
Expr::tbl(Alias::new("excluded"), asset::Column::RoyaltyAmount)
.ne(Expr::tbl(asset::Entity, asset::Column::RoyaltyAmount)),
)
.add(
Expr::tbl(Alias::new("excluded"), asset::Column::AssetData)
.ne(Expr::tbl(asset::Entity, asset::Column::AssetData)),
)
.add(
Expr::tbl(
Alias::new("excluded"),
asset::Column::SlotUpdatedMetadataAccount,
)
.ne(Expr::tbl(
asset::Entity,
asset::Column::SlotUpdatedMetadataAccount,
)),
)
.add(
Expr::tbl(
Alias::new("excluded"),
asset::Column::MplCorePlugins,
)
.ne(Expr::tbl(asset::Entity, asset::Column::MplCorePlugins)),
)
.add(
Expr::tbl(
Alias::new("excluded"),
asset::Column::MplCoreUnknownPlugins,
)
.ne(Expr::tbl(
asset::Entity,
asset::Column::MplCoreUnknownPlugins,
)),
)
.add(
Expr::tbl(
Alias::new("excluded"),
asset::Column::MplCoreCollectionNumMinted,
)
.ne(Expr::tbl(
asset::Entity,
asset::Column::MplCoreCollectionNumMinted,
)),
)
.add(
Expr::tbl(
Alias::new("excluded"),
asset::Column::MplCoreCollectionCurrentSize,
)
.ne(Expr::tbl(
asset::Entity,
asset::Column::MplCoreCollectionCurrentSize,
)),
)
.add(
Expr::tbl(
Alias::new("excluded"),
asset::Column::MplCorePluginsJsonVersion,
)
.ne(Expr::tbl(
asset::Entity,
asset::Column::MplCorePluginsJsonVersion,
)),
)
.add(
Expr::tbl(
Alias::new("excluded"),
asset::Column::MplCoreExternalPlugins,
)
.ne(Expr::tbl(
asset::Entity,
asset::Column::MplCoreExternalPlugins,
)),
)
.add(
Expr::tbl(
Alias::new("excluded"),
asset::Column::MplCoreUnknownExternalPlugins,
)
.ne(Expr::tbl(
asset::Entity,
asset::Column::MplCoreUnknownExternalPlugins,
)),
),
)
.add(
Expr::tbl(asset::Entity, asset::Column::SlotUpdatedMetadataAccount)
.lte(columns.slot_updated_metadata_account as i64),
),
)
.to_owned(),
)
.build(DbBackend::Postgres);
.exec_without_returning(txn_or_conn)
.await?;

query.sql = format!(
"{} WHERE excluded.slot_updated_metadata_account >= asset.slot_updated_metadata_account OR asset.slot_updated_metadata_account IS NULL",
query.sql);
txn_or_conn.execute(query).await?;
Ok(())
}
Loading

0 comments on commit 25a50dd

Please sign in to comment.