documents_db.rs

Overview

This file defines core data structures and an interface trait related to the storage and retrieval of blockchain archival records within a document-oriented database context. It focuses on representing blockchain data entities such as blocks, transactions, accounts, and messages in a serialized or database-storable format. The primary purpose is to provide an abstraction layer for inserting these entities into a database, with error handling and concurrency support.

The file interacts closely with the sqlite module, from which it imports entity definitions (ArchBlock, ArchTransaction, ArchAccount, ArchMessage). These entities represent the archival forms of blockchain components, which are persisted and manipulated via implementations of the DocumentsDb trait.


Data Structures

SerializedItem

#[derive(Clone, Debug)]
pub struct SerializedItem {
    pub id: String,
    pub data: serde_json::Value,
}
let item = SerializedItem {
    id: "block123".to_string(),
    data: serde_json::json!({"height": 123, "hash": "abc123"}),
};
println!("{:?}", item);

DBStoredRecord

pub enum DBStoredRecord {
    Block(Box<ArchBlock>),
    Transactions(Vec<ArchTransaction>),
    Accounts(Vec<ArchAccount>),
    Messages(Vec<ArchMessage>),
}
impl fmt::Debug for DBStoredRecord {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            DBStoredRecord::Block(val) => write!(f, "Block({})", val.id),
            DBStoredRecord::Transactions(val) => write!(f, "Transactions({})", val.len()),
            DBStoredRecord::Accounts(val) => write!(f, "Accounts({})", val.len()),
            DBStoredRecord::Messages(val) => write!(f, "Messages({})", val.len()),
        }
    }
}

This allows for output like Block(abc123) or Transactions(10) during debugging.


Trait: DocumentsDb

pub trait DocumentsDb: Send + Sync {
    fn put_block(&self, item: ArchBlock) -> anyhow::Result<()>;
    fn put_accounts(&self, items: Vec<ArchAccount>) -> anyhow::Result<()>;
    fn put_messages(&self, items: Vec<ArchMessage>) -> anyhow::Result<()>;
    fn put_transactions(&self, items: Vec<ArchTransaction>) -> anyhow::Result<()>;
    fn has_delivery_problems(&self) -> bool;
}
fn store_block(db: &impl DocumentsDb, block: ArchBlock) -> anyhow::Result<()> {
    db.put_block(block)?;
    Ok(())
}

Implementation Details and Algorithms


Interactions with Other Modules


Diagram: Structure of documents_db.rs

classDiagram
class SerializedItem {
+id: String
+data: JSON Value
}
class DBStoredRecord {
<<enum>>
+Block: ArchBlock (boxed)
+Transactions: Vec<ArchTransaction>
+Accounts: Vec<ArchAccount>
+Messages: Vec<ArchMessage>
}
class DocumentsDb {
<<trait>>
+put_block()
+put_accounts()
+put_messages()
+put_transactions()
+has_delivery_problems()
}
SerializedItem --> "uses" JSON
DBStoredRecord --> ArchBlock
DBStoredRecord --> ArchTransaction
DBStoredRecord --> ArchAccount
DBStoredRecord --> ArchMessage

This diagram illustrates the main data structures and the interface trait, showing their properties and relationships to the imported archival types.