Skip to content

Commit

Permalink
feat: add partial index for transaction id (#293)
Browse files Browse the repository at this point in the history
Add partial index for transaction id
  • Loading branch information
Alexandcoats authored Jun 15, 2022
1 parent af48d9c commit dca0e88
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/db/collections/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,15 @@ impl MongoDb {
collection
.create_index(
IndexModel::builder()
.keys(doc! { "block.payload.transaction_id": 1, "metadata.inclusion_state": 1 })
.keys(doc! { "block.payload.transaction_id": 1 })
.options(
IndexOptions::builder()
// There can be multiple blocks that have the same transaction id and a _conflicting_
// inclusion state.
.unique(false)
.unique(true)
.name("transaction_id_index".to_string())
.partial_filter_expression(doc! { "block.payload.transaction_id": { "$exists": true } })
.partial_filter_expression(doc! {
"block.payload.transaction_id": { "$exists": true } ,
"metadata.inclusion_state": { "$eq": LedgerInclusionState::Included },
})
.build(),
)
.build(),
Expand Down Expand Up @@ -208,6 +209,7 @@ impl MongoDb {
.aggregate(
vec![
doc! { "$match": {
"metadata.inclusion_state": LedgerInclusionState::Included,
"block.payload.transaction_id": &output_id.transaction_id,
"$expr": { "$gt": [{ "$size": "$block.payload.essence.outputs" }, &(output_id.index as i64)] }
} },
Expand All @@ -232,6 +234,7 @@ impl MongoDb {
.aggregate(
vec![
doc! { "$match": {
"metadata.inclusion_state": LedgerInclusionState::Included,
"block.payload.transaction_id": &output_id.transaction_id,
"$expr": { "$gt": [{ "$size": "$block.payload.essence.outputs" }, &(output_id.index as i64)] }
} },
Expand Down Expand Up @@ -278,6 +281,7 @@ impl MongoDb {
.aggregate(
vec![
doc! { "$match": {
"metadata.inclusion_state": LedgerInclusionState::Included,
"block.payload.transaction_id": &output_id.transaction_id,
"$expr": { "$gt": [{ "$size": "$block.payload.essence.outputs" }, &(output_id.index as i64)] }
} },
Expand Down

0 comments on commit dca0e88

Please sign in to comment.