//! Maps workspace documents onto the versioned SQL store. //! //! Within a deployment, the workspace selects a collection and the document ID selects //! a row. The v2 JSON payload stores the parent, position, and typed content. use ::documents::{Document, DocumentContent, DocumentId, DocumentKey}; use serde::{Deserialize, Serialize}; use crate::{Connection, Mutation, ReadPreference, Result, Revision, StorageError}; const COLLECTION_PREFIX: &str = "bedrock:workspace-documents:v2:"; const LEGACY_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, } #[derive(Deserialize, Serialize)] struct DocumentPayload { parent_id: Option, position: u64, content: DocumentContent, } impl Connection { /// Creates a document using the primary snapshot and its expected revision. pub async fn create_document(&self, document: Document) -> Result { if self.is_read_only() { return Err(StorageError::ReadOnly); } let (revision, existing) = self.lookup_document(&document.key, None).await?; if existing.is_some() { return Err(StorageError::DocumentAlreadyExists); } if let Some(parent) = &document.parent_id { let parent_key = DocumentKey { workspace: document.key.workspace.clone(), document: parent.clone(), }; let (_, stored) = self.lookup_document(&parent_key, Some(revision)).await?; if stored.is_none() { return Err(StorageError::Invalid("document parent")); } } let value = serde_json::to_value(DocumentPayload { parent_id: document.parent_id.map(|id| id.as_str().to_owned()), position: document.position, content: document.content, })?; self.commit( revision, &[Mutation { collection: collection(COLLECTION_PREFIX, &document.key), id: document.key.document.as_str().to_owned(), value: Some(value), }], ) .await } /// Reads the latest state of a workspace document from the primary. pub async fn get_document(&self, key: &DocumentKey) -> Result> { let (_, document) = self.lookup_document(key, None).await?; Ok(document) } async fn lookup_document( &self, key: &DocumentKey, at: Option, ) -> Result<(Revision, Option)> { let id = key.document.as_str(); let snapshot = self .get( &collection(COLLECTION_PREFIX, key), id, at, ReadPreference::Primary, ) .await?; let revision = snapshot.revision; if let Some(stored) = snapshot.document { let payload: DocumentPayload = serde_json::from_value(stored.value)?; let parent_id = payload .parent_id .map(DocumentId::new) .transpose() .map_err(|_| StorageError::Invalid("stored document parent ID"))?; return Ok(( revision, Some(StoredDocument { document: Document { key: key.clone(), parent_id, position: payload.position, content: payload.content, }, revision: stored.revision, }), )); } // Use the same snapshot when looking up a document saved with the original encoding. let legacy = self .get( &collection(LEGACY_COLLECTION_PREFIX, key), id, Some(revision), ReadPreference::Primary, ) .await?; Ok(( revision, legacy.document.map(|stored| StoredDocument { document: Document::new(key.clone(), DocumentContent::Json(stored.value)), revision: stored.revision, }), )) } } fn collection(prefix: &str, key: &DocumentKey) -> String { format!("{prefix}{}", key.workspace.as_str()) }