Something went wrong. Try again.
contributor fork of @ptr.pet hydrant forked from ptr.pet/hydrant
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366#[cfg(feature = "indexer")]use std::collections::HashMap;use std::time::Duration;
#[cfg(feature = "indexer")]use bytes::Bytes;#[cfg(feature = "indexer")]use jacquard_common::types::cid::IpldCid;#[cfg(feature = "indexer")]use jacquard_common::types::did::Did;use miette::{IntoDiagnostic, Result};
#[cfg(feature = "indexer")]use crate::db::collection_id::CollectionId;#[cfg(feature = "indexer")]use crate::db::collections::CollectionKey;#[cfg(feature = "indexer")]use crate::db::types::{DbAction, DbRkey, DbTid};use crate::db::{CountDeltas, Db};#[cfg(feature = "indexer")]use crate::db::{LifecycleCountBatch, keys};#[cfg(feature = "indexer")]use crate::ops::record_events::{EmitOp, RecordEmitter, RecordEventOrigin, RecordEvents};#[cfg(feature = "indexer")]use crate::state::AppState;#[cfg(feature = "indexer")]use crate::types::{GaugeState, RepoState};#[cfg(feature = "indexer")]use smol_str::SmolStr;
#[cfg(feature = "firehose-diagnostics")]type TxnInstant = std::time::Instant;
#[cfg(not(feature = "firehose-diagnostics"))]#[derive(Clone, Copy)]struct TxnInstant;
#[cfg(not(feature = "firehose-diagnostics"))]impl TxnInstant { #[inline(always)] fn now() -> Self { Self }
#[inline(always)] fn elapsed(&self) -> Duration { Duration::ZERO }}
pub(crate) struct TxnCommitTimings { pub(crate) stage_counts: Duration, pub(crate) stage_and_commit: Duration, pub(crate) apply_counts: Duration,}
/// one atomic database write, including its in-memory count projections.////// count deltas are persisted in the same fjall batch and applied in memory only/// after that batch commits. dropping a transaction discards both.pub(crate) struct Txn<'db> { pub(crate) batch: fjall::OwnedWriteBatch, pub(crate) db: &'db Db, pub(crate) counts: CountDeltas, #[cfg(feature = "indexer")] lifecycle: Option<LifecycleCountBatch<'db>>,}
impl<'db> Txn<'db> { #[allow(dead_code)] pub(crate) fn new(db: &'db Db) -> Self { Self { batch: db.inner.batch(), db, counts: CountDeltas::default(), #[cfg(feature = "indexer")] lifecycle: None, } }
pub(crate) fn from_parts( db: &'db Db, batch: fjall::OwnedWriteBatch, counts: CountDeltas, ) -> Self { Self { batch, db, counts, #[cfg(feature = "indexer")] lifecycle: None, } }
#[cfg(feature = "indexer")] pub(crate) fn records<'txn, 'did, 'repo>( &'txn mut self, state: &AppState, commit_rev: &DbTid, did: &'did Did<'repo>, ) -> RecordTxn<'txn, 'db, 'did, 'repo> { self.record_scope(state, commit_rev, did, RecordEventOrigin::Live, false) }
#[cfg(feature = "indexer")] pub(crate) fn backfill_records<'txn, 'did, 'repo>( &'txn mut self, state: &AppState, commit_rev: &DbTid, did: &'did Did<'repo>, ) -> RecordTxn<'txn, 'db, 'did, 'repo> { self.record_scope(state, commit_rev, did, RecordEventOrigin::Backfill, true) }
#[cfg(feature = "indexer")] fn record_scope<'txn, 'did, 'repo>( &'txn mut self, state: &AppState, commit_rev: &DbTid, did: &'did Did<'repo>, origin: RecordEventOrigin, count_ephemeral_records: bool, ) -> RecordTxn<'txn, 'db, 'did, 'repo> { RecordTxn { txn: self, emitter: RecordEmitter::new(state, commit_rev, origin), did, ephemeral: state.ephemeral, only_index_links: state.only_index_links, count_ephemeral_records, records_delta: 0, blocks_count: 0, collection_deltas: HashMap::new(), last_collection: None, } }
#[cfg(feature = "indexer")] pub(crate) fn transition_lifecycle( &mut self, did: &Did<'_>, gauge: GaugeState, ) -> Result<bool> { let lifecycle = self .lifecycle .get_or_insert_with(|| self.db.lifecycle_counts()); lifecycle.transition(&mut self.batch, did, gauge) }
#[cfg(feature = "indexer")] pub(crate) fn transition_pending_key( &mut self, did: &Did<'_>, pending_key: &[u8], gauge: GaugeState, ) -> Result<bool> { let lifecycle = self .lifecycle .get_or_insert_with(|| self.db.lifecycle_counts()); lifecycle.transition_pending_key(&mut self.batch, did, pending_key, gauge) }
#[allow(dead_code)] pub(crate) fn commit(self) -> Result<()> { self.commit_with(|_| Ok(())).map(|_| ()) }
/// stages count reservations, lets the caller add commit-coupled writes, /// commits once, then applies the in-memory projections. #[allow(dead_code)] pub(crate) fn commit_with<T, F>(mut self, stage: F) -> Result<(T, TxnCommitTimings)> where F: FnOnce(&mut fjall::OwnedWriteBatch) -> Result<T>, { let stage_counts_started = TxnInstant::now(); #[cfg(feature = "indexer")] let lifecycle_reservation = self .lifecycle .map(|lifecycle| lifecycle.stage(&mut self.batch)); let count_reservation = self.db.stage_count_deltas(&mut self.batch, &self.counts); let stage_counts = stage_counts_started.elapsed();
let stage_and_commit_started = TxnInstant::now(); let staged = stage(&mut self.batch)?; self.batch.commit().into_diagnostic()?; let stage_and_commit = stage_and_commit_started.elapsed();
let apply_counts_started = TxnInstant::now(); self.db.apply_count_deltas(&self.counts); drop(count_reservation); #[cfg(feature = "indexer")] if let Some(reservation) = lifecycle_reservation { self.db.apply_lifecycle_counts(reservation); } let apply_counts = apply_counts_started.elapsed();
Ok(( staged, TxnCommitTimings { stage_counts, stage_and_commit, apply_counts, }, )) }}
/// one commit's record mutations within a larger atomic transaction.#[cfg(feature = "indexer")]pub(crate) struct RecordTxn<'txn, 'db, 'did, 'repo> { txn: &'txn mut Txn<'db>, emitter: RecordEmitter, did: &'did Did<'repo>, ephemeral: bool, only_index_links: bool, count_ephemeral_records: bool, records_delta: i64, blocks_count: i64, collection_deltas: HashMap<String, i64>, /// the last collection resolved to an id. /// /// records in a commit and in a backfill arrive grouped by collection, so /// this turns one dictionary lookup per record into one per run. last_collection: Option<(SmolStr, CollectionId)>,}
#[cfg(feature = "indexer")]impl RecordTxn<'_, '_, '_, '_> { /// how this collection should be spelled in a key, interning it if needed. fn collection_key<'a>(&mut self, collection: &'a str) -> Result<CollectionKey<'a>> { if let Some((cached, id)) = &self.last_collection && cached == collection { return Ok(CollectionKey::Id(*id)); } let key = self.txn.db.indexer.collections.key_for(collection)?; if let CollectionKey::Id(id) = key { self.last_collection = Some((SmolStr::new(collection), id)); } Ok(key) }
pub(crate) fn put_record( &mut self, collection: &str, rkey: &DbRkey, cid: IpldCid, block: &Bytes, action: DbAction, ) -> Result<()> { self.blocks_count += 1; if action == DbAction::Create && (!self.ephemeral || self.count_ephemeral_records) { self.records_delta += 1; }
if !self.ephemeral { let collection_key = self.collection_key(collection)?; let cid_bytes = cid.to_bytes(); if !self.only_index_links { self.txn.batch.insert( &self.txn.db.indexer.blocks, keys::block_key(collection, &cid_bytes), block.as_ref(), ); } self.txn.batch.insert( &self.txn.db.indexer.records, keys::record_key(self.did, collection_key, rkey), cid_bytes, ); if action == DbAction::Create { *self .collection_deltas .entry(collection.to_owned()) .or_default() += 1; } crate::ops::backlink_ops::index_record( &mut self.txn.batch, self.txn.db, self.did, collection, &rkey.to_smolstr(), block, )?; }
self.emitter.emit( &mut self.txn.batch, self.txn.db, EmitOp { did: self.did, collection, rkey, action, cid: Some(cid), block: Some(block), }, ) }
pub(crate) fn delete_record(&mut self, collection: &str, rkey: &DbRkey) -> Result<()> { if !self.ephemeral || self.count_ephemeral_records { self.records_delta -= 1; }
if !self.ephemeral { let collection_key = self.collection_key(collection)?; self.txn.batch.remove( &self.txn.db.indexer.records, keys::record_key(self.did, collection_key, rkey), ); *self .collection_deltas .entry(collection.to_owned()) .or_default() -= 1; crate::ops::backlink_ops::delete_record( &mut self.txn.batch, self.txn.db, self.did, collection, &rkey.to_smolstr(), )?; }
self.emitter.emit( &mut self.txn.batch, self.txn.db, EmitOp { did: self.did, collection, rkey, action: DbAction::Delete, cid: None, block: None, }, ) }
pub(crate) fn update_repo_state(&mut self, state: &RepoState<'_>) -> Result<()> { self.txn.batch.insert( &self.txn.db.repos, keys::repo_key(self.did), crate::db::ser_repo_state(state)?, ); Ok(()) }
pub(crate) fn finish(self) -> Result<RecordEvents> { if !self.ephemeral { for (collection, delta) in &self.collection_deltas { crate::db::update_record_count( &mut self.txn.batch, self.txn.db, self.did, collection, *delta, )?; } } self.txn.counts.add_records(self.records_delta); self.txn.counts.add_blocks(self.blocks_count);
Ok(self.emitter.finish()) }}