FeatherDB Core
Core types, traits, and utilities shared across all FeatherDB crates.
Overview
This crate provides the foundational types that all other FeatherDB components depend on. It has minimal dependencies to avoid circular dependencies in the workspace.
Components
1. Value Types (value.rs)
The fundamental data type for all database values.
Capabilities:
- Type conversions (
as_bool(),as_i64(),as_f64(),as_str(),as_bytes()) - Serialization/deserialization to bytes
- Ordering and comparison (implements
Ord) - Hashing (implements
Hash) - Display formatting
- Memory size estimation for GC tracking
Column Types:
Type Compatibility and Coercion:
// Check if a value is compatible with a column type
let text_type = Text ;
assert!;
assert!; // NULL compatible with any type
// Coerce values between types
let int_type = Integer;
let coerced = int_type.coerce?; // Value::Integer(3)
let coerced = int_type.coerce?; // Value::Integer(1)
let text_type = Text ;
let coerced = text_type.coerce?; // Value::Text("42")
Supported Coercions:
| From | To | Result |
|---|---|---|
| Integer | Boolean | 0 = false, non-zero = true |
| Real | Integer | Truncates decimal |
| Boolean | Integer | false = 0, true = 1 |
| Integer | Real | Exact conversion |
| Integer/Real | Text | String representation |
| Integer | Timestamp | Unix milliseconds |
2. Error Types (error.rs)
Comprehensive error handling with context and intelligent suggestions.
Error Helpers:
// Check if error is retriable (TransactionConflict, DatabaseLocked)
if error.is_retriable
// Check if user error vs system error
if error.is_user_error else
// Access rich query error context
if let Some = error.as_query_error
Rich Query Errors with Context:
Query errors produce formatted output similar to Rust compiler errors:
Error: Column 'naem' not found
--> query:1:8
|
1 | SELECT naem FROM users
| ^^^^
|
= suggestion: Did you mean 'name'?
Levenshtein Distance for Suggestions:
use ;
// Calculate edit distance between strings
let distance = levenshtein_distance; // 2
// Find best match from candidates
let columns = vec!;
let suggestion = find_best_match; // Some("name")
// Suggest corrections for SQL keyword typos
let keyword = suggest_keyword; // Some("SELECT")
let keyword = suggest_keyword; // Some("FROM")
let keyword = suggest_keyword; // Some("WHERE")
The suggest_keyword function handles 100+ common SQL keyword typos including:
- SELECT, FROM, WHERE, INSERT, UPDATE, DELETE
- CREATE, TABLE, INDEX, DROP, ALTER
- JOIN, LEFT, RIGHT, INNER, OUTER
- ORDER, GROUP, HAVING, LIMIT, OFFSET
- PRIMARY, FOREIGN, UNIQUE, CONSTRAINT
- BEGIN, COMMIT, ROLLBACK, TRANSACTION
3. Configuration (lib.rs)
Database configuration with builder pattern.
Encryption Configuration:
WAL Sync Modes:
Compression Configuration:
Eviction Policy Types:
Builder Pattern:
let config = new
.create_if_missing
.page_size
.buffer_pool_size_mb
.with_password
// WAL configuration
.with_group_commit
.group_commit_interval_ms
.group_commit_max_batch
// Compression
.with_zstd_compression_level
.compression_threshold
// Eviction policy
.with_lru2_eviction;
// Or use specific compression presets
let config = new
.with_lz4_compression; // Fast compression
.with_zstd_compression; // Default level (3)
// Check configuration
assert!;
assert!;
4. Core Identifiers
Type-safe identifiers for database objects:
;
;
; // Log Sequence Number
All identifiers implement From<u64> and Into<u64> for easy conversion.
5. Row Traits (row.rs)
Traits for converting between Rust types and database rows.
/// Convert a single Rust value to/from database Value
/// Convert a Rust struct to/from database rows
/// Table schema information
/// Column definition for schema generation
Built-in ToValue/FromValue Implementations:
booli32,i64f64String,&strVec<u8>Option<T>where T: ToValue/FromValue
Usage with derive macro:
use Table;
// Automatically implements ToRow, FromRow, TableSchema
let user = User ;
let values = user.to_values;
let restored = from_values?;
6. Serialization Format
Values are serialized with a type tag followed by the data:
| Type | Tag | Format |
|---|---|---|
| Null | 0 | (no data) |
| Boolean | 1 | 1 byte (0 or 1) |
| Integer | 2 | 8 bytes little-endian i64 |
| Real | 3 | 8 bytes little-endian f64 |
| Text | 4 | 4 bytes length (LE u32) + UTF-8 bytes |
| Blob | 5 | 4 bytes length (LE u32) + raw bytes |
| Timestamp | 6 | 8 bytes little-endian i64 |
Serialized Sizes:
- Null: 1 byte
- Boolean: 2 bytes
- Integer/Real/Timestamp: 9 bytes
- Text/Blob: 5 + content length
7. Constants
System-wide constants:
Usage Examples
Working with Values
use Value;
// Create values
let int_val = Integer;
let text_val = Text;
let null_val = Null;
// Type checking
assert!;
assert_eq!;
assert_eq!;
// Comparison (NULL is less than everything)
assert!;
assert!;
// Cross-type numeric comparison
assert!;
// Serialization
let mut buf = Vecnew;
int_val.serialize;
let restored = deserialize?;
assert_eq!;
// Memory estimation for GC
let size = text_val.size_estimate; // 24 + string length
Error Handling with Suggestions
use ;
// Rich query errors with formatting
let err = new
.with_span
.with_suggestion
.with_help;
println!;
Configuration
use ;
// Simple configuration
let config = new;
// Full configuration with all options
let config = new
.create_if_missing
.page_size
.buffer_pool_size_mb
.with_password
.with_group_commit
.group_commit_interval_ms
.with_zstd_compression_level
.with_lru2_eviction;
// Custom WAL configuration
let wal_config = new
.sync_mode
.group_commit_interval_ms
.group_commit_max_batch;
let config = new
.wal_config;
Testing
# Run all core tests (using Make)
# Or with cargo directly
# Run with output
# Run coverage (from project root)
Design Principles
- Minimal Dependencies: Only essential crates (thiserror, serde, bytes)
- No Circular Dependencies: Core is at the bottom of the dependency graph
- Type Safety: Strong typing for identifiers prevents mixing
- Serialization: All types can round-trip to bytes
- Error Context: Errors include enough context for debugging and suggestions
- Builder Pattern: Configuration uses fluent builder API
Public Exports
// From lib.rs
pub use ;
pub use ;
pub use ;
;
;
;
Future Improvements
- JSON value type
- Decimal/numeric type for financial data
- Array/nested types