Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 6 additions & 25 deletions crates/core/src/knowledge/storage.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{Error, Result, StorageDescriptor, StorageHandler, WritableStorageHandler};
use crate::{Core, Storage};
use crate::{Core, Storage, storage};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::json;
Expand Down Expand Up @@ -46,9 +46,7 @@ impl StorageHandler for PostgreSqlStorage {
_storage: &Storage,
pointer: &str,
) -> Result<Vec<u8>> {
sqlx::query_scalar("SELECT data FROM inkcre.storage_blobs WHERE id=$1")
.bind(blob_id(pointer)?)
.fetch_optional(connection)
storage::blob_read(connection, blob_id(pointer)?)
.await?
.ok_or_else(|| crate::Error::NotFound("storage blob").into())
}
Expand All @@ -66,12 +64,8 @@ impl WritableStorageHandler for PostgreSqlStorage {
_storage: &Storage,
data: &[u8],
) -> Result<String> {
let id: Uuid =
sqlx::query_scalar("INSERT INTO inkcre.storage_blobs(data) VALUES($1) RETURNING id")
.bind(data)
.fetch_one(connection)
.await?;
Ok(json!({"blob_id":id}).to_string())
let id = storage::blob_create(connection, data).await?;
Ok(Core::postgresql_blob_pointer(id))
}
async fn update(
&self,
Expand All @@ -81,15 +75,7 @@ impl WritableStorageHandler for PostgreSqlStorage {
pointer: &str,
data: &[u8],
) -> Result<bool> {
Ok(
sqlx::query("UPDATE inkcre.storage_blobs SET data=$2 WHERE id=$1")
.bind(blob_id(pointer)?)
.bind(data)
.execute(connection)
.await?
.rows_affected()
> 0,
)
Ok(storage::blob_update(connection, blob_id(pointer)?, data).await?)
}
async fn delete(
&self,
Expand All @@ -98,12 +84,7 @@ impl WritableStorageHandler for PostgreSqlStorage {
_storage: &Storage,
pointer: &str,
) -> Result<bool> {
Ok(sqlx::query("DELETE FROM inkcre.storage_blobs WHERE id=$1")
.bind(blob_id(pointer)?)
.execute(connection)
.await?
.rows_affected()
> 0)
Ok(storage::blob_delete(connection, blob_id(pointer)?).await?)
}
}

Expand Down
74 changes: 47 additions & 27 deletions crates/core/src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{Core, Error, HydratedContent, Result, Storage};
use sqlx::PgExecutor;
use uuid::Uuid;

impl Core {
Expand All @@ -18,39 +19,16 @@ impl Core {
)
}
pub async fn blob_create(&self, data: &[u8]) -> Result<Uuid> {
Ok(
sqlx::query_scalar("INSERT INTO inkcre.storage_blobs(data) VALUES($1) RETURNING id")
.bind(data)
.fetch_one(self.pool())
.await?,
)
Ok(blob_create(self.pool(), data).await?)
}
pub async fn blob_read(&self, id: Uuid) -> Result<Option<Vec<u8>>> {
Ok(
sqlx::query_scalar("SELECT data FROM inkcre.storage_blobs WHERE id=$1")
.bind(id)
.fetch_optional(self.pool())
.await?,
)
Ok(blob_read(self.pool(), id).await?)
}
pub async fn blob_update(&self, id: Uuid, data: &[u8]) -> Result<bool> {
Ok(
sqlx::query("UPDATE inkcre.storage_blobs SET data=$2 WHERE id=$1")
.bind(id)
.bind(data)
.execute(self.pool())
.await?
.rows_affected()
> 0,
)
Ok(blob_update(self.pool(), id, data).await?)
}
pub async fn blob_delete(&self, id: Uuid) -> Result<bool> {
Ok(sqlx::query("DELETE FROM inkcre.storage_blobs WHERE id=$1")
.bind(id)
.execute(self.pool())
.await?
.rows_affected()
> 0)
Ok(blob_delete(self.pool(), id).await?)
}
/// Pointer grammar is owned by the PostgreSQL storage implementation.
pub fn postgresql_blob_pointer(id: Uuid) -> String {
Expand All @@ -65,3 +43,45 @@ impl Core {
.map_err(Into::into)
}
}

pub(crate) async fn blob_create(executor: impl PgExecutor<'_>, data: &[u8]) -> sqlx::Result<Uuid> {
sqlx::query_scalar("INSERT INTO inkcre.storage_blobs(data) VALUES($1) RETURNING id")
.bind(data)
.fetch_one(executor)
.await
}

pub(crate) async fn blob_read(
executor: impl PgExecutor<'_>,
id: Uuid,
) -> sqlx::Result<Option<Vec<u8>>> {
sqlx::query_scalar("SELECT data FROM inkcre.storage_blobs WHERE id=$1")
.bind(id)
.fetch_optional(executor)
.await
}

pub(crate) async fn blob_update(
executor: impl PgExecutor<'_>,
id: Uuid,
data: &[u8],
) -> sqlx::Result<bool> {
Ok(
sqlx::query("UPDATE inkcre.storage_blobs SET data=$2 WHERE id=$1")
.bind(id)
.bind(data)
.execute(executor)
.await?
.rows_affected()
> 0,
)
}

pub(crate) async fn blob_delete(executor: impl PgExecutor<'_>, id: Uuid) -> sqlx::Result<bool> {
Ok(sqlx::query("DELETE FROM inkcre.storage_blobs WHERE id=$1")
.bind(id)
.execute(executor)
.await?
.rows_affected()
> 0)
}