hanzo-database 1.1.13

Multi-backend database abstraction for Hanzo Node - Supports LanceDB, DuckDB, PostgreSQL, and more
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! # Hanzo DB - Multi-Backend Database Abstraction
//!
//! Production-ready database abstraction for Hanzo Node, supporting:
//! - LanceDB for vector search and multimodal storage
//! - DuckDB for analytics and OLAP queries
//! - PostgreSQL for relational data
//! - Redis for caching
//! - SQLite for lightweight deployments
//!
//! Features:
//! - Unified interface across all backends
//! - Automatic backend selection based on workload
//! - Connection pooling and transaction support
//! - Migration between backends

use anyhow::{Context, Result};
use async_trait::async_trait;
use log::{debug, error, info, warn};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tokio::sync::RwLock;
use thiserror::Error;

/// Database error types
#[derive(Debug, Error)]
pub enum HanzoDbError {
    #[error("Connection error: {0}")]
    ConnectionError(String),

    #[error("Query error: {0}")]
    QueryError(String),

    #[error("Schema error: {0}")]
    SchemaError(String),

    #[error("Invalid schema: {0}")]
    InvalidSchema(String),

    #[error("Invalid data: {0}")]
    InvalidData(String),

    #[error("Transaction error: {0}")]
    TransactionError(String),

    #[error("Migration error: {0}")]
    MigrationError(String),

    #[error("Not implemented: {0}")]
    NotImplemented(String),
}

pub mod models;

#[cfg(feature = "backend-lancedb")]
pub mod vector_search;

// Migration module (only with migration feature and SQLite backend)
#[cfg(all(feature = "migration", feature = "backend-sqlite"))]
pub mod migration;

// Backend modules
pub mod backends;

// Re-exports
pub use models::*;

#[cfg(feature = "backend-lancedb")]
pub use vector_search::*;

/// Database backend type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DatabaseBackend {
    /// LanceDB for vector operations and multimodal data
    LanceDB,
    /// DuckDB for analytics and OLAP
    DuckDB,
    /// PostgreSQL for relational data
    PostgreSQL,
    /// Redis for caching
    Redis,
    /// SQLite for lightweight deployments
    SQLite,
}

impl DatabaseBackend {
    /// Select optimal backend for workload type
    pub fn for_workload(workload: WorkloadType) -> Self {
        match workload {
            WorkloadType::VectorSearch => Self::LanceDB,
            WorkloadType::Analytics => Self::DuckDB,
            WorkloadType::Transactional => Self::PostgreSQL,
            WorkloadType::Cache => Self::Redis,
            WorkloadType::Embedded => Self::SQLite,
        }
    }
}

/// Workload type for backend selection
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WorkloadType {
    /// Vector similarity search
    VectorSearch,
    /// Analytical queries (OLAP)
    Analytics,
    /// Transactional operations (OLTP)
    Transactional,
    /// High-speed caching
    Cache,
    /// Embedded/lightweight operations
    Embedded,
}

/// Unified database configuration
#[derive(Debug, Clone)]
pub struct HanzoDbConfig {
    /// Selected backend
    pub backend: DatabaseBackend,
    /// Database path (for file-based backends)
    pub path: Option<PathBuf>,
    /// Connection URL (for network backends)
    pub url: Option<String>,
    /// Connection pool size
    pub pool_size: usize,
    /// Enable write-ahead logging
    pub enable_wal: bool,
    /// Cache size in bytes
    pub cache_size: Option<usize>,
    /// Enable compression
    pub enable_compression: bool,
}

impl Default for HanzoDbConfig {
    fn default() -> Self {
        Self {
            backend: DatabaseBackend::LanceDB,
            path: Some(PathBuf::from("./storage/hanzo-db")),
            url: None,
            pool_size: 16,
            enable_wal: true,
            cache_size: Some(64 * 1024 * 1024), // 64MB
            enable_compression: true,
        }
    }
}

/// Unified database trait
#[async_trait]
pub trait HanzoDatabase: Send + Sync {
    /// Initialize the database
    async fn init(&self) -> Result<()>;
    
    /// Create a table
    async fn create_table(&self, name: &str, schema: TableSchema) -> Result<()>;
    
    /// Insert data
    async fn insert(&self, table: &str, data: &[Record]) -> Result<()>;
    
    /// Query data
    async fn query(&self, query: Query) -> Result<QueryResult>;
    
    /// Vector search
    async fn vector_search(&self, query: VectorQuery) -> Result<Vec<SearchResult>>;
    
    /// Begin transaction
    async fn begin_transaction(&self) -> Result<Transaction>;
    
    /// Optimize database
    async fn optimize(&self) -> Result<()>;
    
    /// Get database statistics
    async fn stats(&self) -> Result<DatabaseStats>;
}

/// Table schema definition
#[derive(Debug, Clone)]
pub struct TableSchema {
    pub columns: Vec<Column>,
    pub indexes: Vec<Index>,
    pub constraints: Vec<Constraint>,
}

/// Column definition
#[derive(Debug, Clone)]
pub struct Column {
    pub name: String,
    pub data_type: DataType,
    pub nullable: bool,
    pub default: Option<Value>,
}

/// Data type enumeration
#[derive(Debug, Clone)]
pub enum DataType {
    // Scalar types
    Boolean,
    Int32,
    Int64,
    Float32,
    Float64,
    String,
    Binary,
    Timestamp,
    
    // Vector types
    Vector(usize), // dimension
    
    // Complex types
    Json,
    Array(Box<DataType>),
    Struct(Vec<(String, DataType)>),
}

/// Index definition
#[derive(Debug, Clone)]
pub struct Index {
    pub name: String,
    pub columns: Vec<String>,
    pub index_type: IndexType,
}

#[derive(Debug, Clone)]
pub enum IndexType {
    BTree,
    Hash,
    IVF_PQ { nlist: usize, nprobe: usize },
    HNSW { max_elements: usize, m: usize },
}

/// Database constraint
#[derive(Debug, Clone)]
pub enum Constraint {
    PrimaryKey(Vec<String>),
    ForeignKey { columns: Vec<String>, references: String },
    Unique(Vec<String>),
    Check(String),
}

/// Query structure
#[derive(Debug, Clone)]
pub struct Query {
    pub table: String,
    pub select: Vec<String>,
    pub filter: Option<Filter>,
    pub order_by: Vec<OrderBy>,
    pub limit: Option<usize>,
    pub offset: Option<usize>,
}

/// Filter expression
#[derive(Debug, Clone)]
pub enum Filter {
    Eq(String, Value),
    Ne(String, Value),
    Gt(String, Value),
    Gte(String, Value),
    Lt(String, Value),
    Lte(String, Value),
    In(String, Vec<Value>),
    Like(String, String),
    And(Box<Filter>, Box<Filter>),
    Or(Box<Filter>, Box<Filter>),
    Not(Box<Filter>),
}

/// Order by clause
#[derive(Debug, Clone)]
pub struct OrderBy {
    pub column: String,
    pub ascending: bool,
}

/// Value type
#[derive(Debug, Clone)]
pub enum Value {
    Null,
    Bool(bool),
    Int32(i32),
    Int64(i64),
    Float32(f32),
    Float64(f64),
    String(String),
    Binary(Vec<u8>),
    Timestamp(i64),
    Vector(Vec<f32>),
    Json(serde_json::Value),
}

/// Record type
#[derive(Debug, Clone)]
pub struct Record {
    pub values: Vec<(String, Value)>,
}

/// Query result
#[derive(Debug)]
pub struct QueryResult {
    pub columns: Vec<String>,
    pub rows: Vec<Record>,
    pub row_count: usize,
}

/// Vector query
#[derive(Debug, Clone)]
pub struct VectorQuery {
    pub table: String,
    pub vector: Vec<f32>,
    pub k: usize,
    pub filter: Option<Filter>,
    pub metric: DistanceMetric,
}

#[derive(Debug, Clone, Copy)]
pub enum DistanceMetric {
    L2,
    Cosine,
    InnerProduct,
}

/// Search result
#[derive(Debug, Clone)]
pub struct SearchResult {
    pub record: Record,
    pub score: f32,
}

/// Transaction handle
pub struct Transaction {
    inner: Arc<RwLock<TransactionInner>>,
}

struct TransactionInner {
    backend: DatabaseBackend,
    // Backend-specific transaction handle
    handle: Box<dyn std::any::Any + Send + Sync>,
}

impl Transaction {
    /// Commit the transaction
    pub async fn commit(self) -> Result<()> {
        let inner = self.inner.write().await;
        // Backend-specific commit logic
        Ok(())
    }
    
    /// Rollback the transaction
    pub async fn rollback(self) -> Result<()> {
        let inner = self.inner.write().await;
        // Backend-specific rollback logic
        Ok(())
    }
}

/// Database statistics
#[derive(Debug, Clone)]
pub struct DatabaseStats {
    pub backend: DatabaseBackend,
    pub table_count: usize,
    pub total_rows: usize,
    pub total_size_bytes: usize,
    pub index_count: usize,
    pub cache_hit_rate: f64,
}

/// Create a Hanzo database instance
pub async fn connect(config: HanzoDbConfig) -> Result<Arc<dyn HanzoDatabase>> {
    match config.backend {
        #[cfg(feature = "backend-lancedb")]
        DatabaseBackend::LanceDB => {
            let db = backends::lancedb::LanceDbBackend::new(config).await?;
            Ok(Arc::new(db))
        }
        #[cfg(not(feature = "backend-lancedb"))]
        DatabaseBackend::LanceDB => {
            anyhow::bail!("LanceDB backend not enabled. Compile with --features backend-lancedb")
        }
        #[cfg(feature = "backend-duckdb")]
        DatabaseBackend::DuckDB => {
            let db = backends::duckdb::DuckDbBackend::new(config).await?;
            Ok(Arc::new(db))
        }
        #[cfg(not(feature = "backend-duckdb"))]
        DatabaseBackend::DuckDB => {
            anyhow::bail!("DuckDB backend not enabled. Compile with --features backend-duckdb")
        }
        #[cfg(feature = "backend-postgres")]
        DatabaseBackend::PostgreSQL => {
            let db = backends::postgres::PostgresBackend::new(config).await?;
            Ok(Arc::new(db))
        }
        #[cfg(not(feature = "backend-postgres"))]
        DatabaseBackend::PostgreSQL => {
            anyhow::bail!("PostgreSQL backend not enabled. Compile with --features backend-postgres")
        }
        #[cfg(feature = "backend-redis")]
        DatabaseBackend::Redis => {
            let db = backends::redis::RedisBackend::new(config).await?;
            Ok(Arc::new(db))
        }
        #[cfg(not(feature = "backend-redis"))]
        DatabaseBackend::Redis => {
            anyhow::bail!("Redis backend not enabled. Compile with --features backend-redis")
        }
        #[cfg(feature = "backend-sqlite")]
        DatabaseBackend::SQLite => {
            let db = backends::sqlite::SqliteBackend::new(config).await?;
            Ok(Arc::new(db))
        }
        #[cfg(not(feature = "backend-sqlite"))]
        DatabaseBackend::SQLite => {
            anyhow::bail!("SQLite backend not enabled. Compile with --features backend-sqlite")
        }
    }
}


#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_backend_selection() {
        assert_eq!(
            DatabaseBackend::for_workload(WorkloadType::VectorSearch),
            DatabaseBackend::LanceDB
        );
        assert_eq!(
            DatabaseBackend::for_workload(WorkloadType::Analytics),
            DatabaseBackend::DuckDB
        );
        assert_eq!(
            DatabaseBackend::for_workload(WorkloadType::Transactional),
            DatabaseBackend::PostgreSQL
        );
    }
}