Skip to content

Commit

Permalink
Storage trait changes (#268)
Browse files Browse the repository at this point in the history
* Changes for new storage provider API

* Remove SchemaStore trait

* Rename SqlStorage to SqlStore

* Move storage types into own module

* Consistent exporting from models module

* Remove outdated comment

* Update p2panda_rs

* Remove use of deprecated conversion traits on StorageEntry

* Remove use of EntryWithOperation trait

* Introduce StorageDocument struct

* Add document_id column to document_views table

* Introduce DocumentRow struct

* Complete re-write of DocumentStore (with docs ;-p)

* Update Cargo.lock

* Update all effected modules

* Make the ugly SQL pretty

* Use ref for p2panda dependency

* Use where for readability

* Correct SqlStore doc string

* Typo

* License header

* Docs for all db modules

* Re-organize db modules a little

* fmt

* Update CHANGELOG

* Improve CHANGELOG entry
  • Loading branch information
sandreae committed Feb 10, 2023
1 parent 926e939 commit 7f8a216
Show file tree
Hide file tree
Showing 44 changed files with 1,617 additions and 1,415 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Implement API changes to p2panda-rs storage traits, new and breaking db migration [#268](https://github.com/p2panda/aquadoggo/pull/268)

### Fixed

- Fix race-condition of mutably shared static schema store during testing [#269](https://github.com/p2panda/aquadoggo/pull/269)
Expand Down
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions aquadoggo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ lipmaa-link = "^0.2.2"
log = "^0.4.17"
once_cell = "^1.12.0"
openssl-probe = "^0.1.5"
p2panda-rs = { version = "^0.6.0", features = [
p2panda-rs = { git = "https://github.com/p2panda/p2panda", ref = "ce4f4a33c6a58c23bbef50f05dc2c25d4fd9c130", features = [
"storage-provider",
] }
serde = { version = "^1.0.144", features = ["derive"] }
Expand Down Expand Up @@ -66,7 +66,7 @@ env_logger = "^0.9.0"
http = "^0.2.8"
hyper = "^0.14.19"
once_cell = "^1.12.0"
p2panda-rs = { version = "^0.6.0", features = [
p2panda-rs = { git = "https://github.com/p2panda/p2panda", ref = "ce4f4a33c6a58c23bbef50f05dc2c25d4fd9c130", features = [
"test-utils",
"storage-provider",
] }
Expand Down
3 changes: 3 additions & 0 deletions aquadoggo/migrations/20230114140233_alter-documents.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- SPDX-License-Identifier: AGPL-3.0-or-later

ALTER TABLE document_views ADD COLUMN document_id TEXT NOT NULL;
33 changes: 25 additions & 8 deletions aquadoggo/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
use std::ops::Deref;
use std::sync::Arc;

use p2panda_rs::storage_provider::traits::StorageProvider;
use p2panda_rs::storage_provider::traits::{DocumentStore, EntryStore, LogStore, OperationStore};

use crate::config::Configuration;
use crate::db::provider::SqlStorage;
use crate::db::SqlStore;
use crate::schema::SchemaProvider;

/// Inner data shared across all services.
#[derive(Debug)]
pub struct Data<S: StorageProvider> {
pub struct Data<S>
where
S: EntryStore + OperationStore + LogStore + DocumentStore,
{
/// Node configuration.
pub config: Configuration,

Expand All @@ -22,7 +25,10 @@ pub struct Data<S: StorageProvider> {
pub schema_provider: SchemaProvider,
}

impl<S: StorageProvider> Data<S> {
impl<S> Data<S>
where
S: EntryStore + OperationStore + LogStore + DocumentStore,
{
pub fn new(store: S, config: Configuration, schema_provider: SchemaProvider) -> Self {
Self {
config,
Expand All @@ -34,22 +40,33 @@ impl<S: StorageProvider> Data<S> {

/// Data shared across all services.
#[derive(Debug)]
pub struct Context<S: StorageProvider = SqlStorage>(pub Arc<Data<S>>);
pub struct Context<S: EntryStore + OperationStore + LogStore + DocumentStore = SqlStore>(
pub Arc<Data<S>>,
);

impl<S: StorageProvider> Context<S> {
impl<S> Context<S>
where
S: EntryStore + OperationStore + LogStore + DocumentStore,
{
/// Returns a new instance of `Context`.
pub fn new(store: S, config: Configuration, schema_provider: SchemaProvider) -> Self {
Self(Arc::new(Data::new(store, config, schema_provider)))
}
}

impl<S: StorageProvider> Clone for Context<S> {
impl<S> Clone for Context<S>
where
S: EntryStore + OperationStore + LogStore + DocumentStore,
{
fn clone(&self) -> Self {
Self(self.0.clone())
}
}

impl<S: StorageProvider> Deref for Context<S> {
impl<S> Deref for Context<S>
where
S: EntryStore + OperationStore + LogStore + DocumentStore,
{
type Target = Data<S>;

fn deref(&self) -> &Self::Target {
Expand Down
8 changes: 6 additions & 2 deletions aquadoggo/src/db/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

use p2panda_rs::schema::error::{SchemaError, SchemaIdError};
use p2panda_rs::schema::system::SystemSchemaError;
use p2panda_rs::storage_provider::error::DocumentStorageError;
use p2panda_rs::storage_provider::error::{DocumentStorageError, OperationStorageError};

/// `SQLStorage` errors.
#[derive(thiserror::Error, Debug)]
pub enum SqlStorageError {
pub enum SqlStoreError {
#[error("SQL query failed: {0}")]
Transaction(String),

Expand Down Expand Up @@ -37,4 +37,8 @@ pub enum SchemaStoreError {
/// Error returned from `DocumentStore` methods.
#[error(transparent)]
DocumentStorageError(#[from] DocumentStorageError),

/// Error returned from `OperationStore` methods.
#[error(transparent)]
OperationStorageError(#[from] OperationStorageError),
}
21 changes: 18 additions & 3 deletions aquadoggo/src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

//! Persistent storage for an `aquadoggo` node supporting both Postgres and SQLite databases.
//!
//! The main interface is [`SqlStore`] which offers an interface onto the database by implementing
//! the storage traits defined in `p2panda-rs` as well as some implementation specific features.
use anyhow::{Error, Result};
use sqlx::any::{Any, AnyPool, AnyPoolOptions};
use sqlx::migrate;
use sqlx::migrate::MigrateDatabase;

pub mod errors;
pub mod models;
pub mod provider;
pub mod stores;
pub mod traits;
pub mod utils;
pub mod types;

/// SQL based persistent storage that implements `EntryStore`, `OperationStore`, `LogStore` and `DocumentStore`.
#[derive(Clone, Debug)]
pub struct SqlStore {
pub(crate) pool: Pool,
}

impl SqlStore {
/// Create a new `SqlStore` using the provided db `Pool`.
pub fn new(pool: Pool) -> Self {
Self { pool }
}
}

/// Re-export of generic connection pool type.
pub type Pool = AnyPool;
Expand Down
19 changes: 19 additions & 0 deletions aquadoggo/src/db/models/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,22 @@ pub struct DocumentViewFieldRow {
/// The actual value contained in this field.
pub value: String,
}

/// A struct representing a single row of a document table.
#[derive(FromRow, Debug, Clone)]
pub struct DocumentRow {
/// The id of this document
pub document_id: String,

/// The id of this documents most recent view.
pub document_view_id: String,

/// The id of the author of this document.
pub public_key: String,

/// The id of this documents schema.
pub schema_id: String,

/// Flag for if this document is deleted.
pub is_deleted: bool,
}
6 changes: 5 additions & 1 deletion aquadoggo/src/db/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

pub mod document;
//! Structs representing rows in SQL tables. Needed when coercing results returned from a
//! query using the `sqlx` library.
mod document;
mod entry;
mod log;
mod operation;
mod task;
pub mod utils;

pub use self::log::LogRow;
pub use document::{DocumentRow, DocumentViewFieldRow};
pub use entry::EntryRow;
pub use operation::{OperationFieldsJoinedRow, OperationRow};
pub use task::TaskRow;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

//! Utility methods for parsing database rows into p2panda data types.
use std::collections::BTreeMap;

use p2panda_rs::document::{DocumentId, DocumentViewFields, DocumentViewId, DocumentViewValue};
Expand All @@ -11,9 +12,9 @@ use p2panda_rs::operation::{
};
use p2panda_rs::schema::SchemaId;

use crate::db::models::document::DocumentViewFieldRow;
use crate::db::models::DocumentViewFieldRow;
use crate::db::models::OperationFieldsJoinedRow;
use crate::db::stores::StorageOperation;
use crate::db::types::StorageOperation;

/// Takes a vector of `OperationFieldsJoinedRow` and parses them into an `VerifiedOperation`
/// struct.
Expand All @@ -33,6 +34,7 @@ pub fn parse_operation_rows(
let schema_id: SchemaId = first_row.schema_id.parse().unwrap();
let public_key = PublicKey::new(&first_row.public_key).unwrap();
let operation_id = first_row.operation_id.parse().unwrap();
let document_id = first_row.document_id.parse().unwrap();

let mut relation_lists: BTreeMap<String, Vec<DocumentId>> = BTreeMap::new();
let mut pinned_relation_lists: BTreeMap<String, Vec<DocumentViewId>> = BTreeMap::new();
Expand Down Expand Up @@ -161,6 +163,7 @@ pub fn parse_operation_rows(
.unwrap();

let operation = StorageOperation {
document_id,
id: operation_id,
version: operation.version(),
action: operation.action(),
Expand Down Expand Up @@ -355,7 +358,7 @@ mod tests {
use p2panda_rs::test_utils::fixtures::{create_operation, schema_id};
use rstest::rstest;

use crate::db::models::{document::DocumentViewFieldRow, OperationFieldsJoinedRow};
use crate::db::models::{DocumentViewFieldRow, OperationFieldsJoinedRow};

use super::{parse_document_view_field_rows, parse_operation_rows, parse_value_to_string_vec};

Expand Down
Loading

0 comments on commit 7f8a216

Please sign in to comment.