diff --git a/Cargo.lock b/Cargo.lock index 8d11f44..e63f0ac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -147,7 +147,7 @@ dependencies = [ "admin_backend", "axum", "clusters", - "db", + "database", "tokio", ] @@ -357,7 +357,7 @@ dependencies = [ ] [[package]] -name = "db" +name = "database" version = "0.1.0" dependencies = [ "clusters", diff --git a/Cargo.toml b/Cargo.toml index d9dc6c7..f1b2a97 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ rust-version = "1.91" [workspace.dependencies] axum = { version = "0.8", default-features = false, features = ["http1", "json", "tokio"] } clusters = { path = "crates/clusters" } -db = { path = "crates/db" } +database = { path = "crates/database" } documents = { path = "crates/documents" } deadpool-postgres = "0.14.1" mysql_async = { version = "0.36.1", default-features = false, features = ["minimal-rust", "ring", "rustls-tls", "tls12"] } diff --git a/README.md b/README.md index 3ac5b1d..e63744f 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ # Bedrock storage -Bedrock stores JSON documents in SQLite, PostgreSQL or MySQL. Each backend has its own module in the `db` crate. SQLite is the default and creates `bedrock.sqlite3` in the current directory on first start. +Bedrock stores JSON documents in SQLite, PostgreSQL or MySQL. Each backend has its own module in the `database` crate. SQLite is the default and creates `bedrock.sqlite3` in the current directory on first start. ## Storage boundaries -`db` owns the transactional document data, revisions, and snapshots used by the application. `clusters` resolves deployment settings. The `db` connection module selects the SQL driver and read endpoints. The backend modules share document types, validation, and encoding in `common.rs`. +`database` owns the transactional document data, revisions, and snapshots used by the application. `clusters` resolves deployment settings. The `database` connection module selects the SQL driver and read endpoints. The backend modules share document types, validation, and encoding in `common.rs`. Notion describes sharding online PostgreSQL block data by workspace and feeding a downstream S3 data lake from PostgreSQL changes. Bedrock supports named SQL roles and read replicas. The catalog can describe S3 buckets. Object I/O, workspace routing, 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) @@ -40,7 +40,7 @@ Set `BEDROCK_DB_CONFIG` to a private YAML deployment configuration. See the [mul 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 SQL resource's `tls` to `"disabled"`. A top-level `"read_only": true` opens an existing deployment without initializing it. For SQLite, set the resource's `primary` to the file path. -Applications use `db::connect` for the documents role or `db::connect_sql` for an explicitly resolved SQL role. 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 use `database::connect` for the documents role or `database::connect_sql` for an explicitly resolved SQL role. 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/backend/Cargo.toml b/crates/backend/Cargo.toml index 71e0e89..fd768ab 100644 --- a/crates/backend/Cargo.toml +++ b/crates/backend/Cargo.toml @@ -8,5 +8,5 @@ rust-version.workspace = true admin_backend = { path = "../admin_backend" } axum.workspace = true clusters.workspace = true -db.workspace = true +database.workspace = true tokio = { workspace = true, features = ["net", "signal", "sync", "time"] } diff --git a/crates/backend/src/lib.rs b/crates/backend/src/lib.rs index 1adda8c..c624316 100644 --- a/crates/backend/src/lib.rs +++ b/crates/backend/src/lib.rs @@ -2,14 +2,14 @@ use std::{sync::Arc, time::Duration}; use axum::{Router, extract::State, http::StatusCode, routing::get}; -pub fn router(db: db::Connection) -> Router { +pub fn router(db: database::Connection) -> Router { Router::new() .route("/healthz", get(|| async { StatusCode::NO_CONTENT })) .route("/readyz", get(readiness)) .with_state(Arc::new(db)) } -async fn readiness(State(db): State>) -> StatusCode { +async fn readiness(State(db): State>) -> StatusCode { match tokio::time::timeout(Duration::from_secs(2), db.revision()).await { Ok(Ok(_)) => StatusCode::NO_CONTENT, _ => StatusCode::SERVICE_UNAVAILABLE, diff --git a/crates/backend/src/main.rs b/crates/backend/src/main.rs index 0e18b80..86a6869 100644 --- a/crates/backend/src/main.rs +++ b/crates/backend/src/main.rs @@ -2,7 +2,7 @@ use std::{env, error::Error, io, net::SocketAddr, sync::Arc}; use axum::Router; use clusters::{DOCUMENTS, DeploymentConfig, ResolvedDeployment}; -use db::{ConnectError, connect_resolved, connect_sql}; +use database::{ConnectError, connect_resolved, connect_sql}; use tokio::{net::TcpListener, sync::watch}; #[tokio::main] diff --git a/crates/db/Cargo.toml b/crates/database/Cargo.toml similarity index 97% rename from crates/db/Cargo.toml rename to crates/database/Cargo.toml index 2422574..92e1a4d 100644 --- a/crates/db/Cargo.toml +++ b/crates/database/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "db" +name = "database" version = "0.1.0" edition.workspace = true rust-version.workspace = true diff --git a/crates/db/src/common.rs b/crates/database/src/common.rs similarity index 100% rename from crates/db/src/common.rs rename to crates/database/src/common.rs diff --git a/crates/db/src/connection.rs b/crates/database/src/connection.rs similarity index 100% rename from crates/db/src/connection.rs rename to crates/database/src/connection.rs diff --git a/crates/db/src/documents.rs b/crates/database/src/documents.rs similarity index 100% rename from crates/db/src/documents.rs rename to crates/database/src/documents.rs diff --git a/crates/db/src/lib.rs b/crates/database/src/lib.rs similarity index 100% rename from crates/db/src/lib.rs rename to crates/database/src/lib.rs diff --git a/crates/db/src/mysql.rs b/crates/database/src/mysql.rs similarity index 100% rename from crates/db/src/mysql.rs rename to crates/database/src/mysql.rs diff --git a/crates/db/src/postgres.rs b/crates/database/src/postgres.rs similarity index 100% rename from crates/db/src/postgres.rs rename to crates/database/src/postgres.rs diff --git a/crates/db/src/sqlite.rs b/crates/database/src/sqlite.rs similarity index 100% rename from crates/db/src/sqlite.rs rename to crates/database/src/sqlite.rs diff --git a/crates/db/tests/storage.rs b/crates/database/tests/storage.rs similarity index 99% rename from crates/db/tests/storage.rs rename to crates/database/tests/storage.rs index 8c138ae..e4a40cd 100644 --- a/crates/db/tests/storage.rs +++ b/crates/database/tests/storage.rs @@ -1,6 +1,6 @@ use std::{collections::BTreeMap, env, time::Duration}; -use db::{ +use database::{ Connection, DeploymentConfig, Driver, Mutation, ReadPreference, Resource, SqlCluster, StorageError, Tls, connect, }; diff --git a/crates/db/tests/workspace_documents.rs b/crates/database/tests/workspace_documents.rs similarity index 97% rename from crates/db/tests/workspace_documents.rs rename to crates/database/tests/workspace_documents.rs index d0e873f..02f19ac 100644 --- a/crates/db/tests/workspace_documents.rs +++ b/crates/database/tests/workspace_documents.rs @@ -1,6 +1,8 @@ use std::{collections::BTreeMap, path::Path}; -use db::{DeploymentConfig, Driver, Mutation, Resource, SqlCluster, StorageError, Tls, connect}; +use database::{ + DeploymentConfig, Driver, Mutation, Resource, SqlCluster, StorageError, Tls, connect, +}; use documents::{ Contextualize, Document, DocumentContent, DocumentId, DocumentKey, DocumentTree, WorkspaceId, };