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
//! Unified database layer for the Brainwires storage system.
//!
//! This module replaces the former split between `clients/` (VectorDatabase
//! implementations) and `stores/backends/` (StorageBackend implementations).
//! Each database now lives in its own submodule as a **single struct** that
//! wraps a **single shared connection** and can implement one or both of the
//! core traits:
//!
//! - [`StorageBackend`] — generic CRUD + vector search for domain stores
//! (conversations, messages, tasks, plans, images, tiered memory, etc.)
//! - [`VectorDatabase`] — RAG-style embedding storage with hybrid search
//! for the codebase indexing subsystem in `brainwires-cognition`
//!
//! ## Trait implementation matrix
//!
//! | Database | Struct | `StorageBackend` | `VectorDatabase` | Feature flag |
//! |------------|---------------------|:---:|:---:|----------------------|
//! | LanceDB | `LanceDatabase` | YES | YES | `lance-backend` (default) |
//! | PostgreSQL | `PostgresDatabase` | YES | YES | `postgres-backend` |
//! | MySQL | `MySqlDatabase` | YES | NO | `mysql-backend` |
//! | SurrealDB | `SurrealDatabase` | YES | YES | `surrealdb-backend` |
//! | Qdrant | `QdrantDatabase` | NO | YES | `qdrant-backend` |
//! | Pinecone | `PineconeDatabase` | NO | YES | `pinecone-backend` |
//! | Milvus | `MilvusDatabase` | NO | YES | `milvus-backend` |
//! | Weaviate | `WeaviateDatabase` | NO | YES | `weaviate-backend` |
//! | NornicDB | `NornicDatabase` | NO | YES | `nornicdb-backend` |
//!
//! ## Connection sharing
//!
//! Databases that implement both traits share a single connection. Construct
//! the struct once, wrap it in `Arc`, and pass the same instance to domain
//! stores (via `StorageBackend`) **and** to the RAG subsystem (via
//! `VectorDatabase`):
//!
//! ```ignore
//! use std::sync::Arc;
//!
//! let db = Arc::new(LanceDatabase::new("/path/to/db").await?);
//!
//! // Domain stores use StorageBackend
//! let messages = MessageStore::new(db.clone(), embeddings.clone());
//! let conversations = ConversationStore::new(db.clone());
//!
//! // RAG system uses VectorDatabase — same connection, no overhead
//! let rag = RagClient::with_vector_db(db.clone());
//! ```
//!
//! ## Feature flags
//!
//! Each backend is gated behind a Cargo feature flag so only the backends
//! you need are compiled:
//!
//! - `lance-backend` — LanceDB (embedded, default via `native`)
//! - `postgres-backend` — PostgreSQL + pgvector
//! - `mysql-backend` — MySQL / MariaDB
//! - `surrealdb-backend` — SurrealDB (native MTREE vector search)
//! - `qdrant-backend` — Qdrant
//! - `pinecone-backend` — Pinecone cloud
//! - `milvus-backend` — Milvus
//! - `weaviate-backend` — Weaviate
//! - `nornicdb-backend` — NornicDB (REST transport)
//! - `nornicdb-bolt` — NornicDB with Neo4j Bolt transport
//! - `nornicdb-grpc` — NornicDB with Qdrant-compatible gRPC transport
//! - `nornicdb-full` — NornicDB with all transports
//!
//! ## Supporting modules
//!
//! - `types` — `Record`, `FieldDef`, `FieldValue`, `Filter`, `ScoredRecord`
//! - `capabilities` — runtime capability discovery via [`BackendCapabilities`]
//! - `sql` — shared SQL dialect layer for SQL-based backends
//! - `bm25_helpers` — shared BM25 scoring for backends with client-side keyword search
// ── Core abstractions ───────────────────────────────────────────────────
// ── Shared SQL generation ───────────────────────────────────────────────
/// Shared SQL generation layer for SQL-based database backends.
///
/// Provides [`SqlDialect`](sql::SqlDialect) implementations and builder
/// functions for PostgreSQL, MySQL, and SurrealDB.
// ── Database backends ───────────────────────────────────────────────────
/// LanceDB — embedded vector database (default backend).
///
/// Implements both [`StorageBackend`](traits::StorageBackend) and
/// [`VectorDatabase`](traits::VectorDatabase) with a shared
/// `lancedb::Connection`.
/// Qdrant — dedicated vector database server.
///
/// Implements [`VectorDatabase`](traits::VectorDatabase) only.
/// PostgreSQL + pgvector — relational database with vector search.
///
/// Implements both [`StorageBackend`](traits::StorageBackend) and
/// [`VectorDatabase`](traits::VectorDatabase) with a shared
/// `deadpool_postgres::Pool`.
/// MySQL / MariaDB — relational database with client-side vector search.
///
/// Implements [`StorageBackend`](traits::StorageBackend) only. Vector search
/// is performed client-side via cosine similarity (MySQL has no native vector
/// type).
/// SurrealDB — multi-model database with native MTREE vector indexing.
///
/// Implements both [`StorageBackend`](traits::StorageBackend) and
/// [`VectorDatabase`](traits::VectorDatabase) with native KNN search.
/// Pinecone — managed cloud vector database.
///
/// Implements [`VectorDatabase`](traits::VectorDatabase) only.
/// Milvus — open-source vector database.
///
/// Implements [`VectorDatabase`](traits::VectorDatabase) only.
/// Weaviate — vector search engine with built-in hybrid search.
///
/// Implements [`VectorDatabase`](traits::VectorDatabase) only.
/// NornicDB — graph + vector database with cognitive memory tiers.
///
/// Implements [`VectorDatabase`](traits::VectorDatabase) only.
/// Shared BM25 scoring helpers for backends with client-side keyword search.
// ── Re-exports ──────────────────────────────────────────────────────────
pub use BackendCapabilities;
pub use ;
pub use ;
// Backend struct re-exports
pub use LanceDatabase;
pub use QdrantDatabase;
pub use PostgresDatabase;
pub use MySqlDatabase;
pub use SurrealDatabase;
pub use PineconeDatabase;
pub use MilvusDatabase;
pub use WeaviateDatabase;
pub use NornicDatabase;
// Re-export core types for convenience
pub use ;