diff --git a/crates/sui-indexer-alt/src/handlers/sum_packages.rs b/crates/sui-indexer-alt/src/handlers/sum_packages.rs index 6bf0176fd8ca8c..d8c3254c616194 100644 --- a/crates/sui-indexer-alt/src/handlers/sum_packages.rs +++ b/crates/sui-indexer-alt/src/handlers/sum_packages.rs @@ -1,7 +1,7 @@ // Copyright (c) Mysten Labs, Inc. // SPDX-License-Identifier: Apache-2.0 -use std::sync::Arc; +use std::{collections::BTreeMap, sync::Arc}; use anyhow::{anyhow, Result}; use diesel::{upsert::excluded, ExpressionMethods}; @@ -57,13 +57,16 @@ impl Processor for SumPackages { #[async_trait::async_trait] impl Handler for SumPackages { - type Batch = Vec; + type Batch = BTreeMap, StoredPackage>; fn batch(batch: &mut Self::Batch, values: Vec) { - batch.extend(values); + for value in values { + batch.insert(value.package_id.clone(), value); + } } - async fn commit(values: &Self::Batch, conn: &mut db::Connection<'_>) -> Result { + async fn commit(batch: &Self::Batch, conn: &mut db::Connection<'_>) -> Result { + let values: Vec<_> = batch.values().cloned().collect(); let updates = values.chunks(CHUNK_ROWS).map(|chunk| { diesel::insert_into(sum_packages::table) .values(chunk)