pub struct Database<T: VectorType> { /* private fields */ }Expand description
数据库核心入口实例
Implementations§
Source§impl<T: VectorType + Serialize + DeserializeOwned> Database<T>
impl<T: VectorType + Serialize + DeserializeOwned> Database<T>
Source§impl<T: VectorType + Serialize + DeserializeOwned> Database<T>
impl<T: VectorType + Serialize + DeserializeOwned> Database<T>
Sourcepub fn open_with_sync(
path: &str,
dim: usize,
sync_mode: SyncMode,
) -> Result<Self>
pub fn open_with_sync( path: &str, dim: usize, sync_mode: SyncMode, ) -> Result<Self>
打开或创建数据库,指定 WAL 同步模式 (向后兼容)
Sourcepub fn open_with_config(path: &str, config: Config) -> Result<Self>
pub fn open_with_config(path: &str, config: Config) -> Result<Self>
打开或创建数据库(高级配置入口)
Sourcepub fn set_sync_mode(&mut self, mode: SyncMode)
pub fn set_sync_mode(&mut self, mode: SyncMode)
运行时切换 WAL 同步模式
Sourcepub fn set_hook(&mut self, hook: impl SearchHook + 'static)
pub fn set_hook(&mut self, hook: impl SearchHook + 'static)
注册自定义检索管线 Hook
Hook 允许开发者在检索管线的 6 个关键阶段插入自定义逻辑:
on_pre_search— 查询预处理on_custom_recall— 自定义召回(可替代内置)on_post_recall— 召回后处理on_pre_graph_expand— 图扩散前拦截on_rerank— 自定义重排序on_post_search— 最终后处理
§示例
struct MyHook;
impl SearchHook for MyHook {
fn on_post_recall(&self, hits: &mut Vec<SearchHit>, _ctx: &mut HookContext) {
hits.retain(|h| h.score > 0.5);
}
}
db.set_hook(MyHook);Sourcepub fn clear_hook(&mut self)
pub fn clear_hook(&mut self)
移除当前 Hook,恢复为默认的 NoopHook
Sourcepub fn hook(&self) -> &dyn SearchHook
pub fn hook(&self) -> &dyn SearchHook
获取当前 Hook 的引用(主要用于测试和调试)
Sourcepub fn build_quiver_index(&mut self, config: Option<QuIVerConfig>) -> Result<()>
pub fn build_quiver_index(&mut self, config: Option<QuIVerConfig>) -> Result<()>
构建 QuIVer BQ-native HNSW 图索引
从当前所有活跃数据构建 ANN 图索引,替代默认的三级火箭管线。 构建后的搜索将自动使用 QuIVer 的 O(log N) 图搜索。
冷热分离:
- Hot: 2-bit BQ 签名 + HNSW 图拓扑 (~2 bits/dim/node)
- Cold: f32 原始向量(仅精排阶段按需访问)
事务安全: delete / update_vector 会使索引自动失效,管线回退到三级火箭。
use triviumdb::index::quiver::QuIVerConfig;
db.build_quiver_index(None); // 使用默认配置 (m=16, ef_c=128, α=1.2)Sourcepub fn set_memory_limit(&mut self, bytes: usize)
pub fn set_memory_limit(&mut self, bytes: usize)
设置内存上限(字节)
当 MemTable 估算内存超过此值时,写操作后会自动触发 flush 落盘。 设为 0 表示无限制(默认)。
Sourcepub fn estimated_memory(&self) -> usize
pub fn estimated_memory(&self) -> usize
查询当前 MemTable 估算内存占用(字节)
Sourcepub fn enable_auto_compaction(&mut self, interval: Duration)
pub fn enable_auto_compaction(&mut self, interval: Duration)
启动后台自动 Compaction 线程
pub fn disable_auto_compaction(&mut self)
pub fn insert(&mut self, vector: &[T], payload: Value) -> Result<NodeId>
pub fn insert_with_id( &mut self, id: NodeId, vector: &[T], payload: Value, ) -> Result<()>
pub fn link( &mut self, src: NodeId, dst: NodeId, label: &str, weight: f32, ) -> Result<()>
pub fn delete(&mut self, id: NodeId) -> Result<()>
pub fn unlink(&mut self, src: NodeId, dst: NodeId) -> Result<()>
pub fn update_payload(&mut self, id: NodeId, payload: Value) -> Result<()>
pub fn update_vector(&mut self, id: NodeId, vector: &[T]) -> Result<()>
Sourcepub fn leiden_cluster(
&self,
min_community_size: usize,
max_iterations: Option<usize>,
with_centroids: Option<bool>,
) -> Result<LeidenResult>
pub fn leiden_cluster( &self, min_community_size: usize, max_iterations: Option<usize>, with_centroids: Option<bool>, ) -> Result<LeidenResult>
基于内存图谱进行 Leiden/Louvain 近似快速聚类(无锁设计)
pub fn index_keyword(&mut self, id: NodeId, keyword: &str) -> Result<()>
pub fn index_text(&mut self, id: NodeId, text: &str) -> Result<()>
pub fn build_text_index(&mut self) -> Result<()>
pub fn get_payload(&self, id: NodeId) -> Option<Value>
pub fn get_edges(&self, id: NodeId) -> Vec<Edge>
pub fn get_all_ids(&self) -> Vec<NodeId> ⓘ
pub fn search( &self, query_vector: &[T], top_k: usize, expand_depth: usize, min_score: f32, ) -> Result<Vec<SearchHit>>
pub fn search_advanced( &self, query_vector: &[T], config: &SearchConfig, ) -> Result<Vec<SearchHit>>
Sourcepub fn search_hybrid_with_context(
&self,
query_text: Option<&str>,
query_vector: Option<&[T]>,
config: &SearchConfig,
) -> Result<(Vec<SearchHit>, HookContext)>
pub fn search_hybrid_with_context( &self, query_text: Option<&str>, query_vector: Option<&[T]>, config: &SearchConfig, ) -> Result<(Vec<SearchHit>, HookContext)>
带 Hook 上下文的混合检索(完整版)
与 search_hybrid 相同,但额外返回 HookContext,
开发者可以从中读取 Hook 各阶段注入的自定义数据和计时统计。
Sourcepub fn search_hybrid(
&self,
query_text: Option<&str>,
query_vector: Option<&[T]>,
config: &SearchConfig,
) -> Result<Vec<SearchHit>>
pub fn search_hybrid( &self, query_text: Option<&str>, query_vector: Option<&[T]>, config: &SearchConfig, ) -> Result<Vec<SearchHit>>
全能混合检索核心引擎 (Hybrid Advanced Pipeline)
包含文本稀疏索引 + 稠密连续向量空间 + 图谱数学约束的真正完全体检索引擎。
具体实现委托给 pipeline::execute_pipeline。
pub fn get(&self, id: NodeId) -> Option<NodeView<T>>
pub fn neighbors(&self, id: NodeId, depth: usize) -> Vec<NodeId> ⓘ
Sourcepub fn create_index(&mut self, field: &str)
pub fn create_index(&mut self, field: &str)
创建属性索引:对指定 payload 字段建立倒排索引,加速 MATCH/FIND 查询
db.create_index("name"); // 之后 MATCH (a {name: "Alice"}) 将使用 O(1) 索引
db.create_index("type"); // FIND {type: "event"} 同样受益Sourcepub fn drop_index(&mut self, field: &str)
pub fn drop_index(&mut self, field: &str)
删除属性索引
Sourcepub fn tql(&self, input: &str) -> Result<TqlResult<T>>
pub fn tql(&self, input: &str) -> Result<TqlResult<T>>
TQL (Trivium Query Language) 统一查询入口
支持三种查询模式:
FIND {type: "event"} RETURN *— 文档过滤MATCH (a)-[:knows]->(b) WHERE b.age > 20 RETURN b— 图遍历SEARCH VECTOR [...] TOP 10 RETURN *— 向量检索
let results = db.tql("FIND {type: \"event\", heat: {$gte: 0.7}} RETURN * LIMIT 10")?;Sourcepub fn tql_mut(&mut self, input: &str) -> Result<TqlMutResult>
pub fn tql_mut(&mut self, input: &str) -> Result<TqlMutResult>
TQL 写操作入口
支持三种写操作:
CREATE ({name: "Alice", age: 30})— 创建节点MATCH (a) WHERE a.name == "Alice" SET a.age == 31— 更新字段MATCH (a) WHERE a.name == "Alice" DELETE a— 删除节点MATCH (a) WHERE a.name == "Alice" DETACH DELETE a— 删除节点及其边MATCH (a), (b) WHERE ... CREATE (a)-[:knows]->(b)— 创建边
也兼容读查询(自动降级为 tql()),返回 affected=0。
let result = db.tql_mut(r#"CREATE ({name: "Alice", age: 30})"#)?;
assert_eq!(result.created_ids.len(), 1);Sourcepub fn flush(&mut self) -> Result<()>
pub fn flush(&mut self) -> Result<()>
将内存数据持久化到磁盘
安全顺序(防止崩溃丢数据):
- 原子写入 .tdb(写 .tmp → fsync → rename)
- 确认 .tdb 写入成功后,才清除 WAL
pub fn close(self) -> Result<()>
pub fn node_count(&self) -> usize
pub fn contains(&self, id: NodeId) -> bool
pub fn dim(&self) -> usize
Sourcepub fn all_node_ids(&self) -> Vec<NodeId> ⓘ
pub fn all_node_ids(&self) -> Vec<NodeId> ⓘ
获取所有活跃节点的 ID 列表
Sourcepub fn migrate_to(
&self,
new_path: &str,
new_dim: usize,
) -> Result<(Database<T>, Vec<NodeId>)>where
T: Serialize + DeserializeOwned,
pub fn migrate_to(
&self,
new_path: &str,
new_dim: usize,
) -> Result<(Database<T>, Vec<NodeId>)>where
T: Serialize + DeserializeOwned,
维度迁移:从当前数据库导出所有节点和边到一个新维度的数据库。
Sourcepub fn begin_tx(&mut self) -> Transaction<'_, T>
pub fn begin_tx(&mut self) -> Transaction<'_, T>
开启一个轻量级事务
事务期间所有写操作仅缓冲在内存中,调用 commit() 后原子性写入。
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for Database<T>
impl<T> !RefUnwindSafe for Database<T>
impl<T> Send for Database<T>
impl<T> Sync for Database<T>
impl<T> Unpin for Database<T>
impl<T> UnsafeUnpin for Database<T>
impl<T> !UnwindSafe for Database<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more