-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add sqlite read/write pool split
For now, put a basic struct in ceramic_core that manages access to the pools. The sqlx details are not hidden and still relied upon by crates interating with the database.
- Loading branch information
Showing
12 changed files
with
84 additions
and
59 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use std::{path::Path, str::FromStr}; | ||
|
||
use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions}; | ||
|
||
#[derive(Clone, Debug)] | ||
/// The sqlite pool is split into a writer and a reader pool. | ||
/// Wrapper around the sqlx::SqlitePool | ||
pub struct SqlitePool { | ||
writer: sqlx::SqlitePool, | ||
reader: sqlx::SqlitePool, | ||
} | ||
|
||
impl SqlitePool { | ||
/// Connect to the sqlite database at the given path. Creates the database if it does not exist. | ||
/// Uses WAL journal mode. | ||
pub async fn connect(path: impl AsRef<Path>) -> anyhow::Result<Self> { | ||
let db_path = format!("sqlite:{}", path.as_ref().display()); | ||
let conn_opts = SqliteConnectOptions::from_str(&db_path)? | ||
.journal_mode(SqliteJournalMode::Wal) | ||
.create_if_missing(true) | ||
// .synchronous(sqlx::sqlite::SqliteSynchronous::Normal) // normally enough in WAL mode? | ||
.optimize_on_close(true, None); | ||
|
||
let ro_opts = conn_opts.clone().read_only(true); | ||
|
||
let writer = SqlitePoolOptions::new() | ||
.max_connections(1) | ||
.connect_with(conn_opts) | ||
.await?; | ||
let reader = SqlitePoolOptions::new() | ||
.max_connections(32) //TODO | ||
.connect_with(ro_opts) | ||
.await?; | ||
|
||
Ok(Self { writer, reader }) | ||
} | ||
|
||
/// Get a reference to the writer database pool. The writer pool has only one connection. | ||
pub fn writer(&self) -> &sqlx::SqlitePool { | ||
&self.writer | ||
} | ||
|
||
/// Get a reference to the reader database pool. The reader pool has many connections. | ||
pub fn reader(&self) -> &sqlx::SqlitePool { | ||
&self.reader | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.