diff --git a/Cargo.lock b/Cargo.lock index 55805f1..67dcf8e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -39,8 +39,8 @@ checksum = "2af50177e190e07a26ab74f8b1efbfe2ef87da2116221318cb1c2e82baf7de06" name = "bedrock" version = "0.1.0" dependencies = [ - "db_connection", "serde_json", + "sql_storage", "tokio", ] @@ -154,18 +154,6 @@ dependencies = [ "typenum", ] -[[package]] -name = "db_connection" -version = "0.1.0" -dependencies = [ - "clusters", - "serde_json", - "sql_storage", - "tempfile", - "thiserror", - "tokio", -] - [[package]] name = "der" version = "0.7.10" @@ -1134,9 +1122,12 @@ dependencies = [ name = "sql_storage" version = "0.1.0" dependencies = [ + "clusters", "serde_json", "sqlx", + "tempfile", "thiserror", + "tokio", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index baaaf58..d09f87b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,6 @@ rust-version = "1.91" [workspace.dependencies] clusters = { path = "crates/clusters" } -db_connection = { path = "crates/db_connection" } sql_storage = { path = "crates/sql_storage" } serde = { version = "1", features = ["derive"] } serde_json = "1" @@ -25,6 +24,6 @@ edition.workspace = true rust-version.workspace = true [dependencies] -db_connection.workspace = true +sql_storage.workspace = true serde_json.workspace = true tokio.workspace = true diff --git a/README.md b/README.md index 1bb5d2a..713291d 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Bedrock stores JSON documents in SQLite, PostgreSQL or MySQL. Each backend has i ## Storage boundaries -`sql_storage` owns the transactional document data, revisions, and snapshots used by the application. `clusters` resolves deployment settings and `db_connection` selects the SQL driver and read endpoints. +`sql_storage` owns the transactional document data, revisions, and snapshots used by the application. `clusters` resolves deployment settings. The `sql_storage` connection module selects the SQL driver and read endpoints. Notion describes sharding online PostgreSQL block data by workspace and feeding a downstream S3 data lake from PostgreSQL changes. Bedrock currently supports selecting a configured SQL cluster and using its read replicas. Workspace sharding, S3 object storage, and data-lake ingestion are future work. [Notion's PostgreSQL sharding](https://www.notion.com/blog/sharding-postgres-at-notion) · [Notion's data lake](https://www.notion.com/blog/building-and-scaling-notions-data-lake) @@ -29,7 +29,7 @@ Set `BEDROCK_DB_CONFIG` to the path of a private JSON deployment configuration t The database must already exist for PostgreSQL and MySQL. Bedrock creates its own tables and deployment row on a writable startup. Network endpoints use TLS with certificate and hostname verification by default. For a private test database without TLS, set the cluster's `tls` to `"disabled"`. A top-level `"read_only": true` opens an existing deployment without initializing it. A SQLite deployment can set `sqlite_path` to use another file. -Applications connect with `db_connection::connect`. A write batch commits at one new revision if its expected deployment revision still matches. Reads can target the primary or prefer replicas. Use an explicit revision to require that snapshot and fall back when replicas have fallen behind. A tombstone removes a document from subsequent snapshots while preserving earlier revisions. JSON null remains a stored value. +Applications connect with `sql_storage::connect`. A write batch commits at one new revision if its expected deployment revision still matches. Reads can target the primary or prefer replicas. Use an explicit revision to require that snapshot and fall back when replicas have fallen behind. A tombstone removes a document from subsequent snapshots while preserving earlier revisions. JSON null remains a stored value. For MySQL, use `Connection::mysql_writer` to acquire, renew or release a writer lease. An active lease blocks ordinary writes. Use `commit_with_lease` while holding it. `delete_deployment` requires an active lease, deletes that deployment's documents and permanently retires its ID in the database. diff --git a/crates/db_connection/Cargo.toml b/crates/db_connection/Cargo.toml deleted file mode 100644 index 652c873..0000000 --- a/crates/db_connection/Cargo.toml +++ /dev/null @@ -1,15 +0,0 @@ -[package] -name = "db_connection" -version = "0.1.0" -edition.workspace = true -rust-version.workspace = true - -[dependencies] -clusters.workspace = true -sql_storage.workspace = true -thiserror.workspace = true - -[dev-dependencies] -serde_json.workspace = true -tempfile.workspace = true -tokio.workspace = true diff --git a/crates/sql_storage/Cargo.toml b/crates/sql_storage/Cargo.toml index 09251b4..6c143ed 100644 --- a/crates/sql_storage/Cargo.toml +++ b/crates/sql_storage/Cargo.toml @@ -5,6 +5,11 @@ edition.workspace = true rust-version.workspace = true [dependencies] +clusters.workspace = true serde_json.workspace = true sqlx = { workspace = true, features = ["mysql", "postgres", "sqlite"] } thiserror.workspace = true + +[dev-dependencies] +tempfile.workspace = true +tokio.workspace = true diff --git a/crates/db_connection/src/lib.rs b/crates/sql_storage/src/connection.rs similarity index 93% rename from crates/db_connection/src/lib.rs rename to crates/sql_storage/src/connection.rs index 123f7dd..33afeaf 100644 --- a/crates/db_connection/src/lib.rs +++ b/crates/sql_storage/src/connection.rs @@ -2,14 +2,15 @@ use std::sync::atomic::{AtomicUsize, Ordering}; -pub use clusters::{Address, Cluster, ConfigError, DeploymentConfig, Driver, Tls}; -pub use sql_storage::mysql::{Lease, MySql}; -pub use sql_storage::{Document, Error as StorageError, Mutation, Revision, Snapshot}; - -use clusters::ResolvedDeployment; -use sql_storage::{Reader, Store, mysql, postgres, sqlite}; +use clusters::{Address, ConfigError, DeploymentConfig, Driver, ResolvedDeployment}; use thiserror::Error; +use crate::{ + Error as StorageError, Mutation, Reader, Revision, Snapshot, Store, + mysql::{self, MySql}, + postgres, sqlite, +}; + #[derive(Debug, Error)] pub enum ConnectError { #[error(transparent)] diff --git a/crates/sql_storage/src/lib.rs b/crates/sql_storage/src/lib.rs index c2a1d60..887d9a4 100644 --- a/crates/sql_storage/src/lib.rs +++ b/crates/sql_storage/src/lib.rs @@ -1,9 +1,15 @@ //! Bedrock's transactional document storage for SQL databases. +mod connection; pub mod mysql; pub mod postgres; pub mod sqlite; +pub use Error as StorageError; +pub use clusters::{Address, Cluster, ConfigError, DeploymentConfig, Driver, Tls}; +pub use connection::{ConnectError, Connection, ReadPreference, connect}; +pub use mysql::{Lease, MySql}; + use std::{borrow::Cow, collections::HashSet, fmt::Write, time::Duration}; use serde_json::Value; diff --git a/crates/db_connection/tests/storage.rs b/crates/sql_storage/tests/storage.rs similarity index 99% rename from crates/db_connection/tests/storage.rs rename to crates/sql_storage/tests/storage.rs index 245957f..8631144 100644 --- a/crates/db_connection/tests/storage.rs +++ b/crates/sql_storage/tests/storage.rs @@ -1,10 +1,10 @@ use std::{collections::BTreeMap, env, time::Duration}; -use db_connection::{ +use serde_json::json; +use sql_storage::{ Cluster, Connection, DeploymentConfig, Driver, Mutation, ReadPreference, StorageError, Tls, connect, }; -use serde_json::json; fn change(id: &str, text: &str) -> Mutation { Mutation { diff --git a/deploy/bedrock-deployment.svg b/deploy/bedrock-deployment.svg index ae00a0f..0b15e4d 100644 --- a/deploy/bedrock-deployment.svg +++ b/deploy/bedrock-deployment.svg @@ -72,7 +72,7 @@ Rust Bedrock 读取配置 -通过 db_connection 选择 postgres。 +通过 sql_storage::connect 选择 postgres。 配置由 BEDROCK_DB_CONFIG 指定。 在已有 PostgreSQL 数据库内初始化 diff --git a/src/main.rs b/src/main.rs index ae7d0f0..604cba9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use std::{env, error::Error, fs}; -use db_connection::{DeploymentConfig, connect}; +use sql_storage::{DeploymentConfig, connect}; #[tokio::main] async fn main() -> Result<(), Box> {