Add a workspace document model and SQL storage adapter
This commit is contained in:
@@ -30,6 +30,8 @@ pub enum Error {
|
||||
},
|
||||
#[error("the write conflicts with the current revision or lease")]
|
||||
WriteConflict,
|
||||
#[error("the workspace document already exists")]
|
||||
DocumentAlreadyExists,
|
||||
#[error("lease is missing, expired, or belongs to another store")]
|
||||
LeaseLost,
|
||||
}
|
||||
|
||||
@@ -5,10 +5,12 @@ mod connection;
|
||||
pub mod mysql;
|
||||
pub mod postgres;
|
||||
pub mod sqlite;
|
||||
mod workspace_documents;
|
||||
|
||||
pub use clusters::{Address, Cluster, ConfigError, DeploymentConfig, Driver, Tls};
|
||||
pub use connection::{ConnectError, Connection, ReadPreference, connect};
|
||||
pub use mysql::{Lease, MySql};
|
||||
pub use workspace_documents::StoredDocument;
|
||||
|
||||
pub use common::Error as StorageError;
|
||||
pub use common::{
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
//! Maps workspace documents onto the versioned SQL store.
|
||||
//!
|
||||
//! Within a deployment, the workspace selects a reserved collection prefix plus its ID.
|
||||
//! The document ID becomes the row key in that collection, and the body is JSON.
|
||||
|
||||
use documents::{Document, DocumentKey};
|
||||
|
||||
use crate::{Connection, Mutation, ReadPreference, Result, Revision, StorageError};
|
||||
|
||||
// This collection namespace belongs to the workspace-document adapter.
|
||||
const COLLECTION_PREFIX: &str = "bedrock:workspace-documents:v1:";
|
||||
|
||||
/// A logical document and the deployment revision at which it was last written.
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct StoredDocument {
|
||||
pub document: Document,
|
||||
pub revision: Revision,
|
||||
}
|
||||
|
||||
impl Connection {
|
||||
/// Creates a document using the primary snapshot and its expected revision.
|
||||
pub async fn create_document(&self, document: Document) -> Result<Revision> {
|
||||
if self.is_read_only() {
|
||||
return Err(StorageError::ReadOnly);
|
||||
}
|
||||
let collection = collection(&document.key);
|
||||
let id = document.key.document.as_str();
|
||||
let snapshot = self
|
||||
.get(&collection, id, None, ReadPreference::Primary)
|
||||
.await?;
|
||||
if snapshot.document.is_some() {
|
||||
return Err(StorageError::DocumentAlreadyExists);
|
||||
}
|
||||
|
||||
self.commit(
|
||||
snapshot.revision,
|
||||
&[Mutation {
|
||||
collection,
|
||||
id: id.to_owned(),
|
||||
value: Some(document.body),
|
||||
}],
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Reads the latest state of a workspace document from the primary.
|
||||
pub async fn get_document(&self, key: &DocumentKey) -> Result<Option<StoredDocument>> {
|
||||
let snapshot = self
|
||||
.get(
|
||||
&collection(key),
|
||||
key.document.as_str(),
|
||||
None,
|
||||
ReadPreference::Primary,
|
||||
)
|
||||
.await?;
|
||||
Ok(snapshot.document.map(|stored| StoredDocument {
|
||||
document: Document {
|
||||
key: key.clone(),
|
||||
body: stored.value,
|
||||
},
|
||||
revision: stored.revision,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
fn collection(key: &DocumentKey) -> String {
|
||||
format!("{COLLECTION_PREFIX}{}", key.workspace.as_str())
|
||||
}
|
||||
Reference in New Issue
Block a user