Skip to content

Commit

Permalink
refactor: drop exculsion clauses on cl_items to allow for reindexing
Browse files Browse the repository at this point in the history
  • Loading branch information
kespinola committed Sep 14, 2024
1 parent 4c3f649 commit 2da4039
Showing 1 changed file with 7 additions and 40 deletions.
47 changes: 7 additions & 40 deletions program_transformers/src/bubblegum/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@ use {
crate::error::{ProgramTransformerError, ProgramTransformerResult},
das_core::DownloadMetadataInfo,
digital_asset_types::dao::{
asset, asset_authority, asset_creators, asset_data, asset_grouping, backfill_items,
cl_audits_v2, cl_items,
asset, asset_authority, asset_creators, asset_data, asset_grouping, cl_audits_v2, cl_items,
sea_orm_active_enums::{
ChainMutability, Instruction, Mutability, OwnerType, RoyaltyTargetType,
SpecificationAssetClass, SpecificationVersions,
},
},
mpl_bubblegum::types::{Collection, Creator},
sea_orm::{
entity::{ActiveValue, ColumnTrait, EntityTrait},
entity::{ActiveValue, EntityTrait},
prelude::*,
query::{JsonValue, QueryFilter, QuerySelect, QueryTrait},
query::{JsonValue, QueryTrait},
sea_query::query::OnConflict,
ConnectionTrait, DbBackend, TransactionTrait,
},
spl_account_compression::events::ChangeLogEventV1,
tracing::{debug, error, info},
tracing::{debug, error},
};

pub async fn save_changelog_event<'c, T>(
change_log_event: &ChangeLogEventV1,
change_log_event: &ChangeLogEventV2,
slot: u64,
txn_id: &str,
txn: &T,
Expand Down Expand Up @@ -80,7 +79,7 @@ where
};

i += 1;
let mut query = cl_items::Entity::insert(item)
let query = cl_items::Entity::insert(item)
.on_conflict(
OnConflict::columns([cl_items::Column::Tree, cl_items::Column::NodeIdx])
.update_columns([
Expand All @@ -92,7 +91,6 @@ where
.to_owned(),
)
.build(DbBackend::Postgres);
query.sql = format!("{} WHERE excluded.seq > cl_items.seq", query.sql);
txn.execute(query)
.await
.map_err(|db_err| ProgramTransformerError::StorageWriteError(db_err.to_string()))?;
Expand Down Expand Up @@ -121,7 +119,7 @@ where
cl_audits_v2::Column::LeafIdx,
cl_audits_v2::Column::Seq,
])
.do_nothing()
.update_columns([cl_audits_v2::Column::Tx, cl_audits_v2::Column::Instruction])
.to_owned(),
)
.build(DbBackend::Postgres);
Expand All @@ -132,37 +130,6 @@ where
}
}

// If and only if the entire path of nodes was inserted into the `cl_items` table, then insert
// a single row into the `backfill_items` table. This way if an incomplete path was inserted
// into `cl_items` due to an error, a gap will be created for the tree and the backfiller will
// fix it.
if i - 1 == depth as i64 {
// See if the tree already exists in the `backfill_items` table.
let rows = backfill_items::Entity::find()
.filter(backfill_items::Column::Tree.eq(tree_id))
.limit(1)
.all(txn)
.await?;

// If the tree does not exist in `backfill_items` and the sequence number is greater than 1,
// then we know we will need to backfill the tree from sequence number 1 up to the current
// sequence number. So in this case we set at flag to force checking the tree.
let force_chk = rows.is_empty() && change_log_event.seq > 1;

info!("Adding to backfill_items table at level {}", i - 1);
let item = backfill_items::ActiveModel {
tree: ActiveValue::Set(tree_id.to_vec()),
seq: ActiveValue::Set(change_log_event.seq as i64),
slot: ActiveValue::Set(slot as i64),
force_chk: ActiveValue::Set(force_chk),
backfilled: ActiveValue::Set(false),
failed: ActiveValue::Set(false),
..Default::default()
};

backfill_items::Entity::insert(item).exec(txn).await?;
}

Ok(())
}

Expand Down

0 comments on commit 2da4039

Please sign in to comment.