brk_interface
Unified data query and formatting interface for Bitcoin datasets with intelligent search and multi-format output.
Overview
This crate provides a high-level interface for querying and formatting data from BRK's indexer and computer components. It offers intelligent vector search with fuzzy matching, parameter validation, range queries, and multi-format output (JSON, CSV) with efficient caching and pagination support.
Key Features:
- Unified query interface across indexer and computer data sources
- Intelligent search with fuzzy matching and helpful error messages
- Multi-format output: JSON, CSV with proper formatting
- Range-based data queries with flexible from/to parameters
- Comprehensive pagination support for large datasets
- Schema validation with JSON Schema generation for API documentation
- Efficient caching system for error messages and repeated queries
Target Use Cases:
- REST API backends requiring flexible data queries
- Data export tools supporting multiple output formats
- Interactive applications with user-friendly error messaging
- Research platforms requiring structured data access
Installation
Quick Start
use ;
use Indexer;
use Computer;
// Initialize with indexer and computer
let indexer = build?;
let computer = build?;
let interface = build;
// Query data with parameters
let params = Params ;
// Search and format results
let output = interface.search_and_format?;
println!;
API Overview
Core Types
Interface<'a>
: Main query interface coordinating indexer and computer accessParams
: Query parameters including index, IDs, range, and formatting optionsIndex
: Enumeration of available data indexes (Height, Date, Address, etc.)Format
: Output format specification (JSON, CSV)Output
: Formatted query results with multiple value types
Key Methods
Interface::build(indexer: &Indexer, computer: &Computer) -> Self
Creates interface instance with references to data sources.
search(&self, params: &Params) -> Result<Vec<(String, &&dyn AnyCollectableVec)>>
Searches for vectors matching the query parameters with intelligent error handling.
format(&self, vecs: Vec<...>, params: &ParamsOpt) -> Result<Output>
Formats search results according to specified output format and range parameters.
search_and_format(&self, params: Params) -> Result<Output>
Combined search and formatting operation for single-call data retrieval.
Query Parameters
Core Parameters:
index
: Data index to query (height, date, address, etc.)ids
: Vector IDs to retrieve from the specified indexfrom
/to
: Optional range filtering (inclusive start, exclusive end)format
: Output format (defaults to JSON)
Pagination Parameters:
offset
: Number of entries to skiplimit
: Maximum entries to return
Examples
Basic Data Query
use ;
let interface = build;
// Query block heights to hashes
let params = Params ;
match interface.search_and_format?
CSV Export with Range Query
use ;
// Export price data as CSV
let params = Params ;
match interface.search_and_format?
Intelligent Error Handling
use ;
// Query with typo in vector ID
let params = Params ;
// Interface provides helpful error with suggestions
match interface.search
Architecture
Data Source Integration
The interface acts as a bridge between:
- Indexer: Raw blockchain data vectors (blocks, transactions, addresses)
- Computer: Computed analytics vectors (prices, statistics, aggregations)
- Unified Access: Single query interface for both data sources
Search Implementation
- Parameter Validation: Validates index existence and parameter consistency
- Vector Resolution: Maps vector IDs to actual data structures
- Fuzzy Matching: Provides suggestions for mistyped vector names
- Error Caching: Caches error messages to avoid repeated expensive operations
Output Formatting
JSON Output:
- Single value: Direct value serialization
- List: Array of values
- Matrix: Array of arrays for multi-vector queries
CSV Output:
- Column headers from vector IDs
- Row-wise data iteration with proper escaping
Caching Strategy
- Error Message Caching: 1000-entry LRU cache for error messages
- Search Result Caching: Upstream caching in server/client layers
- Static Data Caching: Index and vector metadata cached during initialization
Configuration
Index Types
Available indexes include:
Height
: Block height-based indexingDate
: Calendar date indexingAddress
: Bitcoin address indexingTransaction
: Transaction hash indexing- Custom indexes from computer analytics
Format Options
- JSON: Structured data with nested objects/arrays
- CSV: Comma-separated values with proper escaping
Code Analysis Summary
Main Structure: Interface
struct coordinating between Indexer
and Computer
data sources
Query System: Parameter-driven search with Params
struct supporting range queries and formatting options
Error Handling: Intelligent fuzzy matching with cached error messages and helpful suggestions
Output Formats: Multi-format support (JSON, CSV) with proper data serialization
Caching: quick_cache
integration for error messages and expensive operations
Search Logic: nucleo-matcher
fuzzy search for user-friendly vector name resolution
Architecture: Abstraction layer providing unified access to heterogeneous Bitcoin data sources
This README was generated by Claude Code