Skip to content

Commit

Permalink
insert parents
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Coats committed Nov 14, 2023
1 parent 76d0fd9 commit ebe4b52
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/bin/inx-chronicle/inx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use chronicle::{
db::{
mongodb::collections::{
ApplicationStateCollection, BlockCollection, CommittedSlotCollection, LedgerUpdateCollection,
OutputCollection,
OutputCollection, ParentsCollection,
},
MongoDb,
},
Expand Down Expand Up @@ -314,6 +314,7 @@ impl InxWorker {
.try_fold(JoinSet::new(), |mut tasks, batch| async {
let db = self.db.clone();
tasks.spawn(async move {

Check failure on line 316 in src/bin/inx-chronicle/inx/mod.rs

View workflow job for this annotation

GitHub Actions / check and test / ubuntu-latest, stable

implementation of `FnOnce` is not general enough

Check failure on line 316 in src/bin/inx-chronicle/inx/mod.rs

View workflow job for this annotation

GitHub Actions / check and test / ubuntu-latest, stable

implementation of `Iterator` is not general enough

Check failure on line 316 in src/bin/inx-chronicle/inx/mod.rs

View workflow job for this annotation

GitHub Actions / check and test / ubuntu-latest, stable

higher-ranked lifetime error
db.collection::<ParentsCollection>().insert_blocks(&batch).await?;
db.collection::<BlockCollection>()
.insert_blocks_with_metadata(batch)
.await?;
Expand Down
14 changes: 5 additions & 9 deletions src/db/mongodb/collections/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<I, B>(&self, blocks_with_metadata: I) -> Result<(), DbError>
pub async fn insert_blocks_with_metadata<I>(&self, blocks_with_metadata: I) -> Result<(), DbError>
where
I: IntoIterator<Item = B>,
I: IntoIterator<Item = BlockWithMetadata>,
I::IntoIter: Send + Sync,
BlockDocument: From<B>,
{
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(())
}
Expand Down
40 changes: 36 additions & 4 deletions src/db/mongodb/collections/parents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<Item = &'a BlockWithMetadata>,
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,
Expand Down

0 comments on commit ebe4b52

Please sign in to comment.