hyperdb-api
A pure-Rust implementation of the Hyper database API. Create, read, and manipulate
Hyper database files (.hyper) without any C library dependencies.
- 22-24M rows/sec inserts, 18M rows/sec queries (100M row benchmark)
- Streaming by default — constant memory for billion-row results
- Both sync (
Connection) and async (AsyncConnection) APIs - No feature flags — everything is always available
Installation
[]
= "0.1"
Runtime Requirements
The hyperd executable (Hyper database server) must be available. Set its path via:
Quick Start
use ;
Async Quick Start
use ;
async
Connection Pooling
For high-concurrency async applications, use the built-in connection pool:
use ;
use ;
async
| Option | Default | Description |
|---|---|---|
max_size(n) |
16 | Maximum connections in pool |
create_mode(mode) |
DoNotCreate |
Database creation mode |
auth(user, pass) |
None | Authentication credentials |
gRPC Transport
The API supports gRPC as an alternative to TCP for read-only queries with Arrow IPC
results. The unified Connection type auto-detects transport based on URL:
use ;
// http:// or https:// → gRPC (auto-detected)
let conn = connect?;
// Same query API as TCP
let mut result = conn.execute_query?;
Start Hyper with gRPC enabled:
use ;
let mut params = new;
params.set_listen_mode;
let hyper = new?;
| Transfer Mode | Best For |
|---|---|
SYNC |
Small results |
ASYNC |
Very large results |
ADAPTIVE (default) |
Most workloads |
Query Execution
Convenience Methods
// Fetch single row (error if no rows)
let user = conn.fetch_one?;
// Fetch optional row (None if no rows)
let user = conn.fetch_optional?;
// Fetch all rows
let users = conn.fetch_all?;
// Fetch scalar value
let count: i64 = conn.fetch_scalar?;
Parameterized Queries
Always use parameterized queries with user input to prevent SQL injection:
let mut result = conn.query_params?;
// For INSERT/UPDATE/DELETE:
let rows_affected = conn.command_params?;
Supported parameter types: i16, i32, i64, f32, f64, bool, &str, String,
Option<T>, Date, Time, Timestamp, OffsetTimestamp, Vec<u8>, &[u8].
Streaming Results
Results are always streaming with constant memory usage:
// Chunked iteration (batch processing, maximum performance)
let mut result = conn.execute_query?;
while let Some = result.next_chunk?
// Row iterator (simpler, slightly more overhead)
let result = conn.execute_query?;
for row in result.rows
Bulk Data Insertion
Inserter (COPY Protocol)
The high-performance Inserter uses HyperBinary COPY protocol for 22M+ rows/sec:
let mut inserter = new?;
inserter.add_row?;
inserter.add_row?; // NULL name
inserter.execute?;
Type-specific methods are also available (similar to the C++ API):
inserter.add_i32?;
inserter.add_str?;
inserter.add_f64?;
inserter.end_row?;
Column Mappings
Insert data with computed columns using MappedInserter:
let mappings = vec!;
let mut inserter = with_column_mappings?;
Arrow Format
Insert pre-formatted Arrow IPC data:
let mut inserter = new?;
inserter.insert_data?;
let rows = inserter.execute?;
Multi-threaded Insertion
For maximum throughput on multi-core systems, use InsertChunk and ChunkSender to
parallelize data encoding:
let sender = new?;
// Workers encode chunks in parallel using InsertChunk
let chunk = from_table_definition;
// ... populate chunk ...
sender.send_chunk?;
let total_rows = sender.finish?;
Catalog Operations
let catalog = new;
// List schemas and tables
let schemas = catalog.?;
let tables = catalog.get_table_names?;
// Check existence
if catalog.has_table?
SQL-Safe Names
Type-safe SQL identifier handling with automatic escaping:
use ;
// Simple construction
let name = try_new?;
// Qualified names (dot-separated parsing)
let table: TableName = "mydb.public.users".parse?;
// Fluent builder
let table = try_new?.with_schema?;
// Macros
let table = table_name!?;
Most API methods accept strings directly via impl TryInto<T>:
// No manual conversion needed
if catalog.has_table?
Transactions
let txn = conn.transaction?;
txn.execute_command?;
txn.execute_command?;
txn.commit?; // both inserts are committed; drop without commit() auto-rolls-back
See docs/TRANSACTIONS.md for full details.
Connection Features
Query Cancellation
Thread-safe cancellation from another thread:
let conn = new;
// ... in another thread:
conn.cancel?; // Cancels running query (SQLSTATE 57014)
Notice Receiver
conn.set_notice_receiver;
Query Statistics
Per-query performance metrics from Hyper's internal log:
use LogFileStatsProvider;
conn.enable_query_stats;
conn.execute_command?;
if let Some = conn.last_query_stats
Logging
The API uses the tracing crate for structured logging:
# Enable via environment variable
RUST_LOG=hyperdb_api=info
# Debug-level for auth details and chunk progress
RUST_LOG=hyperdb_api=debug
Examples
| Example | Description | Command |
|---|---|---|
insert_data_into_single_table |
Create table and insert data | cargo run -p hyperdb-api --example insert_data_into_single_table |
insert_data_into_multiple_tables |
Multiple related tables | cargo run -p hyperdb-api --example insert_data_into_multiple_tables |
create_hyper_file_from_csv |
Load CSV into Hyper | cargo run -p hyperdb-api --example create_hyper_file_from_csv |
read_and_print_data_from_existing_hyper_file |
Read and query data | cargo run -p hyperdb-api --example read_and_print_data_from_existing_hyper_file |
insert_data_with_expressions |
Column mappings | cargo run -p hyperdb-api --example insert_data_with_expressions |
insert_geospatial_data_to_a_hyper_file |
Geography column | cargo run -p hyperdb-api --example insert_geospatial_data_to_a_hyper_file |
arrow |
Arrow RecordBatch read/write | cargo run -p hyperdb-api --example arrow |
async_usage |
AsyncConnection + Tokio | cargo run -p hyperdb-api --example async_usage |
threaded_inserter |
Multi-threaded bulk insert | cargo run -p hyperdb-api --example threaded_inserter |
grpc_query |
gRPC transport + Arrow IPC | cargo run -p hyperdb-api --example grpc_query |
connection_pool |
Async connection pooling | cargo run -p hyperdb-api --example connection_pool |
transactions |
RAII guards, multi-table rollback, DDL, reconnect semantics | cargo run -p hyperdb-api --example transactions |
Run all examples:
Companion Crates
sea-query-hyperdb
HyperDB dialect backend for sea-query — use for window functions, CTEs, complex JOINs, and type-safe query composition.
[]
= "0.32"
= "0.1"
use ;
use HyperQueryBuilder;
let sql = select
.column
.from
.and_where
.to_string;
let result = conn.fetch_all?;
hyperdb-api-salesforce
Salesforce Data Cloud OAuth authentication — JWT Bearer Token, Username-Password, and Refresh Token flows.
[]
= "0.1"
See hyperdb-api-salesforce/README.md for setup instructions.
Acknowledgments
This crate depends on hyperdb-api-core, which includes code adapted from
sfackler/rust-postgres (the
postgres-protocol, tokio-postgres, and postgres-types crates by Steven
Fackler, MIT or Apache-2.0). See the
NOTICE file at
the workspace root for the full third-party attribution list. (Relative links
to NOTICE work on GitHub but not on crates.io; the absolute URL above is
crates.io-friendly.)
License
Apache-2.0