resolver.rs

Overview

This file defines a data loader utility for efficiently retrieving blockchain block data from a SQLite database. The primary purpose is to batch load multiple blocks in a single SQL query to optimize database access, reducing the number of queries and improving performance when resolving blocks by their IDs. It interfaces with the database schema defined elsewhere and converts raw database records into application-level block objects.

BlockLoader Struct

pub struct BlockLoader {
    pub pool: SqlitePool,
}

Implementation of Loader Trait for BlockLoader

BlockLoader implements the Loader<String> trait from the async_graphql::dataloader module, which allows it to asynchronously batch load data based on string keys.

Associated Types

load Method

async fn load(
    &self,
    keys: &[String],
) -> anyhow::Result<HashMap<String, Self::Value>, Self::Error>

Usage Example

let loader = BlockLoader { pool: sqlite_pool };
let block_ids = vec!["block1".to_string(), "block2".to_string()];
let blocks_map = loader.load(&block_ids).await?;
if let Some(block) = blocks_map.get("block1") {
    // Use block object here
}

Implementation Details and Algorithms

Interactions with Other Components

Mermaid Diagram

classDiagram
class BlockLoader {
+pool: SqlitePool
+load(keys: &[String]) async -> Result<HashMap<String, Block>, Error>
}
class Loader~String~ {
<<trait>>
+load(keys: &[String]) async -> Result<HashMap<String, Value>, Error>
}
class Block {
<<struct>>
}
class db::Block {
<<struct>>
}
BlockLoader ..|> Loader~String~
BlockLoader --> SqlitePool
BlockLoader --> db::Block
BlockLoader --> Block