Skip to main content

helix_driver_native/
storage.rs

1//! NativeStorage — PC/Tauri storage shell over the shared host storage.
2//!
3//! SQLite schema, migration, parameter binding, row conversion, and StorageOp
4//! semantics live in `helix-driver-host`. This crate keeps the native public
5//! type name so Tauri-side code and existing tests do not learn the shared
6//! implementation details.
7
8use helix_core::effect::{
9    BatchDeleteSpec, BatchUpdateSpec, GetSpec, GuardedBumpSpec, MonotonicUpsertSpec,
10    Row as HelixRow, ScanSpec, ScopedGetSpec, ScopedGuardedBumpSpec, ScopedMaxSpec, ScopedScanSpec,
11    StorageOp, UpsertSpec,
12};
13use helix_core::ports::Storage;
14use helix_core::PortError;
15use helix_driver_host::{AsyncMetricSink, HostStorage};
16use std::sync::Arc;
17
18#[derive(Clone)]
19pub struct NativeStorage {
20    inner: HostStorage,
21}
22
23impl NativeStorage {
24    pub async fn open(db_url: &str) -> Result<Self, PortError> {
25        Ok(Self {
26            inner: HostStorage::open_sqlite_url(db_url).await?,
27        })
28    }
29
30    pub async fn execute_raw(&self, sql: &'static str) -> Result<(), PortError> {
31        self.inner.execute_raw(sql).await
32    }
33
34    pub fn with_metric_sink(mut self, metrics: Arc<dyn AsyncMetricSink>) -> Self {
35        self.inner = self.inner.with_metric_sink(metrics);
36        self
37    }
38}
39
40#[async_trait::async_trait]
41impl Storage for NativeStorage {
42    async fn batch_upsert(&self, spec: UpsertSpec) -> Result<(), PortError> {
43        self.inner.batch_upsert(spec).await
44    }
45
46    async fn batch_update(&self, spec: BatchUpdateSpec) -> Result<(), PortError> {
47        self.inner.batch_update(spec).await
48    }
49
50    async fn monotonic_upsert(&self, spec: MonotonicUpsertSpec) -> Result<(), PortError> {
51        self.inner.monotonic_upsert(spec).await
52    }
53
54    async fn guarded_bump(&self, spec: GuardedBumpSpec) -> Result<(), PortError> {
55        self.inner.guarded_bump(spec).await
56    }
57
58    /// 将复合作用域守卫更新委托给共享 HostStorage。
59    async fn scoped_guarded_bump(&self, spec: ScopedGuardedBumpSpec) -> Result<(), PortError> {
60        self.inner.scoped_guarded_bump(spec).await
61    }
62
63    async fn get(&self, spec: GetSpec) -> Result<Option<HelixRow>, PortError> {
64        self.inner.get(spec).await
65    }
66
67    /// 将复合作用域读取委托给共享 HostStorage。
68    async fn scoped_get(&self, spec: ScopedGetSpec) -> Result<Option<HelixRow>, PortError> {
69        self.inner.scoped_get(spec).await
70    }
71
72    /// 将作用域集合最大值读取委托给共享 HostStorage。
73    async fn scoped_max(&self, spec: ScopedMaxSpec) -> Result<Option<HelixRow>, PortError> {
74        self.inner.scoped_max(spec).await
75    }
76
77    /// 将作用域集合有界多行读取委托给共享 HostStorage。
78    async fn scoped_scan(&self, spec: ScopedScanSpec) -> Result<Vec<HelixRow>, PortError> {
79        self.inner.scoped_scan(spec).await
80    }
81
82    async fn scan(&self, spec: ScanSpec) -> Result<Vec<HelixRow>, PortError> {
83        self.inner.scan(spec).await
84    }
85
86    async fn batch_delete(&self, spec: BatchDeleteSpec) -> Result<(), PortError> {
87        self.inner.batch_delete(spec).await
88    }
89
90    async fn atomic_write(&self, ops: Vec<StorageOp>) -> Result<(), PortError> {
91        self.inner.atomic_write(ops).await
92    }
93}