From ebe4b52e7abf708bfc3ab4bb1de8010f2b0bb26a Mon Sep 17 00:00:00 2001 From: Alex Coats Date: Tue, 14 Nov 2023 11:56:11 -0500 Subject: [PATCH] insert parents --- src/bin/inx-chronicle/inx/mod.rs | 3 +- src/db/mongodb/collections/block.rs | 14 ++++------ src/db/mongodb/collections/parents.rs | 40 ++++++++++++++++++++++++--- 3 files changed, 43 insertions(+), 14 deletions(-) diff --git a/src/bin/inx-chronicle/inx/mod.rs b/src/bin/inx-chronicle/inx/mod.rs index a505d675a..5160847a6 100644 --- a/src/bin/inx-chronicle/inx/mod.rs +++ b/src/bin/inx-chronicle/inx/mod.rs @@ -12,7 +12,7 @@ use chronicle::{ db::{ mongodb::collections::{ ApplicationStateCollection, BlockCollection, CommittedSlotCollection, LedgerUpdateCollection, - OutputCollection, + OutputCollection, ParentsCollection, }, MongoDb, }, @@ -314,6 +314,7 @@ impl InxWorker { .try_fold(JoinSet::new(), |mut tasks, batch| async { let db = self.db.clone(); tasks.spawn(async move { + db.collection::().insert_blocks(&batch).await?; db.collection::() .insert_blocks_with_metadata(batch) .await?; diff --git a/src/db/mongodb/collections/block.rs b/src/db/mongodb/collections/block.rs index 4e6a17592..4f245ef9f 100644 --- a/src/db/mongodb/collections/block.rs +++ b/src/db/mongodb/collections/block.rs @@ -218,19 +218,15 @@ impl BlockCollection { /// Inserts [`SignedBlock`]s together with their associated [`BlockMetadata`]. #[instrument(skip_all, err, level = "trace")] - pub async fn insert_blocks_with_metadata(&self, blocks_with_metadata: I) -> Result<(), DbError> + pub async fn insert_blocks_with_metadata(&self, blocks_with_metadata: I) -> Result<(), DbError> where - I: IntoIterator, + I: IntoIterator, I::IntoIter: Send + Sync, - BlockDocument: From, { - let blocks_with_metadata = blocks_with_metadata.into_iter().map(BlockDocument::from); + let docs = blocks_with_metadata.into_iter().map(BlockDocument::from); - self.insert_many_ignore_duplicates( - blocks_with_metadata, - InsertManyOptions::builder().ordered(false).build(), - ) - .await?; + self.insert_many_ignore_duplicates(docs, InsertManyOptions::builder().ordered(false).build()) + .await?; Ok(()) } diff --git a/src/db/mongodb/collections/parents.rs b/src/db/mongodb/collections/parents.rs index 66001d3db..3c7b8f55b 100644 --- a/src/db/mongodb/collections/parents.rs +++ b/src/db/mongodb/collections/parents.rs @@ -2,13 +2,21 @@ // SPDX-License-Identifier: Apache-2.0 use futures::{prelude::stream::TryStreamExt, Stream}; -use iota_sdk::types::block::BlockId; -use mongodb::{bson::doc, options::IndexOptions, IndexModel}; +use iota_sdk::types::block::{Block, BlockId}; +use mongodb::{ + bson::doc, + options::{IndexOptions, InsertManyOptions}, + IndexModel, +}; use serde::{Deserialize, Serialize}; +use tracing::instrument; use crate::{ - db::{mongodb::DbError, MongoDb, MongoDbCollection, MongoDbCollectionExt}, - model::SerializeToBson, + db::{ + mongodb::{DbError, InsertIgnoreDuplicatesExt}, + MongoDb, MongoDbCollection, MongoDbCollectionExt, + }, + model::{block_metadata::BlockWithMetadata, SerializeToBson}, }; /// Chronicle Parents record which relates child to parent. @@ -58,6 +66,30 @@ impl MongoDbCollection for ParentsCollection { } impl ParentsCollection { + /// Inserts [`SignedBlock`]s together with their associated [`BlockMetadata`]. + #[instrument(skip_all, err, level = "trace")] + pub async fn insert_blocks<'a, I>(&self, blocks_with_metadata: I) -> Result<(), DbError> + where + I: IntoIterator, + I::IntoIter: Send + Sync, + { + let docs = blocks_with_metadata.into_iter().flat_map(|b| { + match b.block.inner().block() { + Block::Basic(b) => b.strong_parents().into_iter(), + Block::Validation(b) => b.strong_parents().into_iter(), + } + .map(|parent_id| ParentsDocument { + parent_id: *parent_id, + child_id: b.metadata.block_id, + }) + }); + + self.insert_many_ignore_duplicates(docs, InsertManyOptions::builder().ordered(false).build()) + .await?; + + Ok(()) + } + /// Get the children of a block as a stream of [`BlockId`]s. pub async fn get_block_children( &self,