feat(nbstore): improve nbstore (#9512)

This commit is contained in:
EYHN
2025-01-06 09:38:03 +00:00
parent a2563d2180
commit 46c8c4a408
103 changed files with 3337 additions and 3423 deletions
+249 -138
View File
@@ -1,11 +1,14 @@
pub mod blob;
pub mod doc;
pub mod error;
pub mod pool;
pub mod storage;
pub mod sync;
use chrono::NaiveDateTime;
use napi::bindgen_prelude::*;
use napi_derive::napi;
use pool::SqliteDocStoragePool;
#[cfg(feature = "use-as-lib")]
type Result<T> = anyhow::Result<T>;
@@ -14,13 +17,10 @@ type Result<T> = anyhow::Result<T>;
type Result<T> = napi::Result<T>;
#[cfg(not(feature = "use-as-lib"))]
fn map_err(err: sqlx::Error) -> Error {
Error::from(anyhow::Error::from(err))
}
#[cfg(feature = "use-as-lib")]
fn map_err(err: sqlx::Error) -> anyhow::Error {
anyhow::Error::from(err)
impl From<error::Error> for napi::Error {
fn from(err: error::Error) -> Self {
napi::Error::new(napi::Status::GenericFailure, err.to_string())
}
}
#[cfg(feature = "use-as-lib")]
@@ -32,16 +32,16 @@ pub type Data = Uint8Array;
#[napi(object)]
pub struct DocUpdate {
pub doc_id: String,
pub created_at: NaiveDateTime,
pub timestamp: NaiveDateTime,
#[napi(ts_type = "Uint8Array")]
pub data: Data,
pub bin: Data,
}
#[napi(object)]
pub struct DocRecord {
pub doc_id: String,
#[napi(ts_type = "Uint8Array")]
pub data: Data,
pub bin: Data,
pub timestamp: NaiveDateTime,
}
@@ -79,243 +79,354 @@ pub struct ListedBlob {
}
#[napi]
pub struct DocStorage {
storage: storage::SqliteDocStorage,
pub struct DocStoragePool {
pool: SqliteDocStoragePool,
}
#[napi]
impl DocStorage {
impl DocStoragePool {
#[napi(constructor, async_runtime)]
pub fn new(path: String) -> Result<Self> {
pub fn new() -> Result<Self> {
Ok(Self {
storage: storage::SqliteDocStorage::new(path),
pool: SqliteDocStoragePool::default(),
})
}
#[napi]
/// Initialize the database and run migrations.
pub async fn connect(&self) -> Result<()> {
self.storage.connect().await.map_err(map_err)
}
#[napi]
pub async fn close(&self) -> Result<()> {
self.storage.close().await;
pub async fn connect(&self, universal_id: String, path: String) -> Result<()> {
self.pool.connect(universal_id, path).await?;
Ok(())
}
#[napi(getter)]
pub async fn is_closed(&self) -> Result<bool> {
Ok(self.storage.is_closed())
}
/**
* Flush the WAL file to the database file.
* See https://www.sqlite.org/pragma.html#pragma_wal_checkpoint:~:text=PRAGMA%20schema.wal_checkpoint%3B
*/
#[napi]
pub async fn checkpoint(&self) -> Result<()> {
self.storage.checkpoint().await.map_err(map_err)
pub async fn disconnect(&self, universal_id: String) -> Result<()> {
self.pool.disconnect(universal_id).await?;
Ok(())
}
#[napi]
pub async fn validate(&self) -> Result<bool> {
self.storage.validate().await.map_err(map_err)
}
#[napi]
pub async fn set_space_id(&self, space_id: String) -> Result<()> {
self.storage.set_space_id(space_id).await.map_err(map_err)
}
#[napi]
pub async fn push_update(&self, doc_id: String, update: Uint8Array) -> Result<NaiveDateTime> {
pub async fn set_space_id(&self, universal_id: String, space_id: String) -> Result<()> {
self
.storage
.push_update(doc_id, update)
.await
.map_err(map_err)
.pool
.ensure_storage(universal_id)?
.set_space_id(space_id)
.await?;
Ok(())
}
#[napi]
pub async fn get_doc_snapshot(&self, doc_id: String) -> Result<Option<DocRecord>> {
self.storage.get_doc_snapshot(doc_id).await.map_err(map_err)
pub async fn push_update(
&self,
universal_id: String,
doc_id: String,
update: Uint8Array,
) -> Result<NaiveDateTime> {
Ok(
self
.pool
.ensure_storage(universal_id)?
.push_update(doc_id, update)
.await?,
)
}
#[napi]
pub async fn set_doc_snapshot(&self, snapshot: DocRecord) -> Result<bool> {
self
.storage
.set_doc_snapshot(snapshot)
.await
.map_err(map_err)
pub async fn get_doc_snapshot(
&self,
universal_id: String,
doc_id: String,
) -> Result<Option<DocRecord>> {
Ok(
self
.pool
.ensure_storage(universal_id)?
.get_doc_snapshot(doc_id)
.await?,
)
}
#[napi]
pub async fn get_doc_updates(&self, doc_id: String) -> Result<Vec<DocUpdate>> {
self.storage.get_doc_updates(doc_id).await.map_err(map_err)
pub async fn set_doc_snapshot(&self, universal_id: String, snapshot: DocRecord) -> Result<bool> {
Ok(
self
.pool
.ensure_storage(universal_id)?
.set_doc_snapshot(snapshot)
.await?,
)
}
#[napi]
pub async fn get_doc_updates(
&self,
universal_id: String,
doc_id: String,
) -> Result<Vec<DocUpdate>> {
Ok(
self
.pool
.ensure_storage(universal_id)?
.get_doc_updates(doc_id)
.await?,
)
}
#[napi]
pub async fn mark_updates_merged(
&self,
universal_id: String,
doc_id: String,
updates: Vec<NaiveDateTime>,
) -> Result<u32> {
Ok(
self
.pool
.ensure_storage(universal_id)?
.mark_updates_merged(doc_id, updates)
.await?,
)
}
#[napi]
pub async fn delete_doc(&self, universal_id: String, doc_id: String) -> Result<()> {
self
.storage
.mark_updates_merged(doc_id, updates)
.await
.map_err(map_err)
.pool
.ensure_storage(universal_id)?
.delete_doc(doc_id)
.await?;
Ok(())
}
#[napi]
pub async fn delete_doc(&self, doc_id: String) -> Result<()> {
self.storage.delete_doc(doc_id).await.map_err(map_err)
pub async fn get_doc_clocks(
&self,
universal_id: String,
after: Option<NaiveDateTime>,
) -> Result<Vec<DocClock>> {
Ok(
self
.pool
.ensure_storage(universal_id)?
.get_doc_clocks(after)
.await?,
)
}
#[napi]
pub async fn get_doc_clocks(&self, after: Option<NaiveDateTime>) -> Result<Vec<DocClock>> {
self.storage.get_doc_clocks(after).await.map_err(map_err)
pub async fn get_doc_clock(
&self,
universal_id: String,
doc_id: String,
) -> Result<Option<DocClock>> {
Ok(
self
.pool
.ensure_storage(universal_id)?
.get_doc_clock(doc_id)
.await?,
)
}
#[napi]
pub async fn get_doc_clock(&self, doc_id: String) -> Result<Option<DocClock>> {
self.storage.get_doc_clock(doc_id).await.map_err(map_err)
pub async fn get_blob(&self, universal_id: String, key: String) -> Result<Option<Blob>> {
Ok(
self
.pool
.ensure_storage(universal_id)?
.get_blob(key)
.await?,
)
}
#[napi]
pub async fn get_blob(&self, key: String) -> Result<Option<Blob>> {
self.storage.get_blob(key).await.map_err(map_err)
}
#[napi]
pub async fn set_blob(&self, blob: SetBlob) -> Result<()> {
self.storage.set_blob(blob).await.map_err(map_err)
}
#[napi]
pub async fn delete_blob(&self, key: String, permanently: bool) -> Result<()> {
pub async fn set_blob(&self, universal_id: String, blob: SetBlob) -> Result<()> {
self
.storage
.pool
.ensure_storage(universal_id)?
.set_blob(blob)
.await?;
Ok(())
}
#[napi]
pub async fn delete_blob(
&self,
universal_id: String,
key: String,
permanently: bool,
) -> Result<()> {
self
.pool
.ensure_storage(universal_id)?
.delete_blob(key, permanently)
.await
.map_err(map_err)
.await?;
Ok(())
}
#[napi]
pub async fn release_blobs(&self) -> Result<()> {
self.storage.release_blobs().await.map_err(map_err)
}
#[napi]
pub async fn list_blobs(&self) -> Result<Vec<ListedBlob>> {
self.storage.list_blobs().await.map_err(map_err)
}
#[napi]
pub async fn get_peer_remote_clocks(&self, peer: String) -> Result<Vec<DocClock>> {
pub async fn release_blobs(&self, universal_id: String) -> Result<()> {
self
.storage
.get_peer_remote_clocks(peer)
.await
.map_err(map_err)
.pool
.ensure_storage(universal_id)?
.release_blobs()
.await?;
Ok(())
}
#[napi]
pub async fn get_peer_remote_clock(&self, peer: String, doc_id: String) -> Result<DocClock> {
self
.storage
.get_peer_remote_clock(peer, doc_id)
.await
.map_err(map_err)
pub async fn list_blobs(&self, universal_id: String) -> Result<Vec<ListedBlob>> {
Ok(self.pool.ensure_storage(universal_id)?.list_blobs().await?)
}
#[napi]
pub async fn get_peer_remote_clocks(
&self,
universal_id: String,
peer: String,
) -> Result<Vec<DocClock>> {
Ok(
self
.pool
.ensure_storage(universal_id)?
.get_peer_remote_clocks(peer)
.await?,
)
}
#[napi]
pub async fn get_peer_remote_clock(
&self,
universal_id: String,
peer: String,
doc_id: String,
) -> Result<DocClock> {
Ok(
self
.pool
.ensure_storage(universal_id)?
.get_peer_remote_clock(peer, doc_id)
.await?,
)
}
#[napi]
pub async fn set_peer_remote_clock(
&self,
universal_id: String,
peer: String,
doc_id: String,
clock: NaiveDateTime,
) -> Result<()> {
self
.storage
.pool
.ensure_storage(universal_id)?
.set_peer_remote_clock(peer, doc_id, clock)
.await
.map_err(map_err)
.await?;
Ok(())
}
#[napi]
pub async fn get_peer_pulled_remote_clocks(&self, peer: String) -> Result<Vec<DocClock>> {
self
.storage
.get_peer_pulled_remote_clocks(peer)
.await
.map_err(map_err)
pub async fn get_peer_pulled_remote_clocks(
&self,
universal_id: String,
peer: String,
) -> Result<Vec<DocClock>> {
Ok(
self
.pool
.ensure_storage(universal_id)?
.get_peer_pulled_remote_clocks(peer)
.await?,
)
}
#[napi]
pub async fn get_peer_pulled_remote_clock(
&self,
universal_id: String,
peer: String,
doc_id: String,
) -> Result<DocClock> {
self
.storage
.get_peer_pulled_remote_clock(peer, doc_id)
.await
.map_err(map_err)
Ok(
self
.pool
.ensure_storage(universal_id)?
.get_peer_pulled_remote_clock(peer, doc_id)
.await?,
)
}
#[napi]
pub async fn set_peer_pulled_remote_clock(
&self,
universal_id: String,
peer: String,
doc_id: String,
clock: NaiveDateTime,
) -> Result<()> {
self
.storage
.pool
.ensure_storage(universal_id)?
.set_peer_pulled_remote_clock(peer, doc_id, clock)
.await
.map_err(map_err)
.await?;
Ok(())
}
#[napi]
pub async fn get_peer_pushed_clocks(&self, peer: String) -> Result<Vec<DocClock>> {
self
.storage
.get_peer_pushed_clocks(peer)
.await
.map_err(map_err)
pub async fn get_peer_pushed_clocks(
&self,
universal_id: String,
peer: String,
) -> Result<Vec<DocClock>> {
Ok(
self
.pool
.ensure_storage(universal_id)?
.get_peer_pushed_clocks(peer)
.await?,
)
}
#[napi]
pub async fn get_peer_pushed_clock(&self, peer: String, doc_id: String) -> Result<DocClock> {
self
.storage
.get_peer_pushed_clock(peer, doc_id)
.await
.map_err(map_err)
pub async fn get_peer_pushed_clock(
&self,
universal_id: String,
peer: String,
doc_id: String,
) -> Result<DocClock> {
Ok(
self
.pool
.ensure_storage(universal_id)?
.get_peer_pushed_clock(peer, doc_id)
.await?,
)
}
#[napi]
pub async fn set_peer_pushed_clock(
&self,
universal_id: String,
peer: String,
doc_id: String,
clock: NaiveDateTime,
) -> Result<()> {
self
.storage
.pool
.ensure_storage(universal_id)?
.set_peer_pushed_clock(peer, doc_id, clock)
.await
.map_err(map_err)
.await?;
Ok(())
}
#[napi]
pub async fn clear_clocks(&self) -> Result<()> {
self.storage.clear_clocks().await.map_err(map_err)
pub async fn clear_clocks(&self, universal_id: String) -> Result<()> {
self
.pool
.ensure_storage(universal_id)?
.clear_clocks()
.await?;
Ok(())
}
}