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, StorageOp, UpsertSpec,
11};
12use helix_core::ports::Storage;
13use helix_core::PortError;
14use helix_driver_host::{AsyncMetricSink, HostStorage};
15use std::sync::Arc;
16
17#[derive(Clone)]
18pub struct NativeStorage {
19    inner: HostStorage,
20}
21
22impl NativeStorage {
23    pub async fn open(db_url: &str) -> Result<Self, PortError> {
24        Ok(Self {
25            inner: HostStorage::open_sqlite_url(db_url).await?,
26        })
27    }
28
29    pub async fn execute_raw(&self, sql: &'static str) -> Result<(), PortError> {
30        self.inner.execute_raw(sql).await
31    }
32
33    pub fn with_metric_sink(mut self, metrics: Arc<dyn AsyncMetricSink>) -> Self {
34        self.inner = self.inner.with_metric_sink(metrics);
35        self
36    }
37}
38
39#[async_trait::async_trait]
40impl Storage for NativeStorage {
41    async fn batch_upsert(&self, spec: UpsertSpec) -> Result<(), PortError> {
42        self.inner.batch_upsert(spec).await
43    }
44
45    async fn batch_update(&self, spec: BatchUpdateSpec) -> Result<(), PortError> {
46        self.inner.batch_update(spec).await
47    }
48
49    async fn monotonic_upsert(&self, spec: MonotonicUpsertSpec) -> Result<(), PortError> {
50        self.inner.monotonic_upsert(spec).await
51    }
52
53    async fn guarded_bump(&self, spec: GuardedBumpSpec) -> Result<(), PortError> {
54        self.inner.guarded_bump(spec).await
55    }
56
57    /// 将复合作用域守卫更新委托给共享 HostStorage。
58    async fn scoped_guarded_bump(&self, spec: ScopedGuardedBumpSpec) -> Result<(), PortError> {
59        self.inner.scoped_guarded_bump(spec).await
60    }
61
62    async fn get(&self, spec: GetSpec) -> Result<Option<HelixRow>, PortError> {
63        self.inner.get(spec).await
64    }
65
66    /// 将复合作用域读取委托给共享 HostStorage。
67    async fn scoped_get(&self, spec: ScopedGetSpec) -> Result<Option<HelixRow>, PortError> {
68        self.inner.scoped_get(spec).await
69    }
70
71    async fn scan(&self, spec: ScanSpec) -> Result<Vec<HelixRow>, PortError> {
72        self.inner.scan(spec).await
73    }
74
75    async fn batch_delete(&self, spec: BatchDeleteSpec) -> Result<(), PortError> {
76        self.inner.batch_delete(spec).await
77    }
78
79    async fn atomic_write(&self, ops: Vec<StorageOp>) -> Result<(), PortError> {
80        self.inner.atomic_write(ops).await
81    }
82}