-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from openlawlibrary/ndusan/optimize-history-da…
…tabase refact: optimize stelae database by hashing composite keys; small ergonomic improvements
- Loading branch information
Showing
48 changed files
with
1,603 additions
and
1,361 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//! Manager for the changed library document model. | ||
use super::ChangedLibraryDocument; | ||
use crate::db::{models::BATCH_SIZE, DatabaseTransaction}; | ||
use async_trait::async_trait; | ||
use sqlx::QueryBuilder; | ||
|
||
#[async_trait] | ||
impl super::TxManager for DatabaseTransaction { | ||
/// Upsert a bulk of changed library documents into the database. | ||
/// | ||
/// # Errors | ||
/// Errors if the changed library documents cannot be inserted into the database. | ||
async fn insert_bulk( | ||
&mut self, | ||
changed_library_document: Vec<ChangedLibraryDocument>, | ||
) -> anyhow::Result<()> { | ||
let mut query_builder = QueryBuilder::new( | ||
"INSERT OR IGNORE INTO changed_library_document ( library_mpath, document_change_id ) ", | ||
); | ||
for chunk in changed_library_document.chunks(BATCH_SIZE) { | ||
query_builder.push_values(chunk, |mut bindings, cl| { | ||
bindings | ||
.push_bind(&cl.library_mpath) | ||
.push_bind(&cl.document_change_id); | ||
}); | ||
let query = query_builder.build(); | ||
query.execute(&mut *self.tx).await?; | ||
} | ||
Ok(()) | ||
} | ||
} |
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,34 @@ | ||
use async_trait::async_trait; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
pub mod manager; | ||
|
||
/// Trait for managing transactional changed library documents. | ||
#[async_trait] | ||
pub trait TxManager { | ||
/// Insert bulk of changed library documents. | ||
async fn insert_bulk( | ||
&mut self, | ||
changed_library_document: Vec<ChangedLibraryDocument>, | ||
) -> anyhow::Result<()>; | ||
} | ||
|
||
#[derive(sqlx::FromRow, Deserialize, Serialize)] | ||
/// Model for library (collection) change events. | ||
pub struct ChangedLibraryDocument { | ||
/// Foreign key reference to `document_change` id. | ||
pub document_change_id: String, | ||
/// Materialized path to the library | ||
pub library_mpath: String, | ||
} | ||
|
||
impl ChangedLibraryDocument { | ||
/// Create a new library change. | ||
#[must_use] | ||
pub const fn new(document_change_id: String, library_mpath: String) -> Self { | ||
Self { | ||
document_change_id, | ||
library_mpath, | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//! Manager for the document model. | ||
use crate::db::DatabaseTransaction; | ||
use async_trait::async_trait; | ||
|
||
#[async_trait] | ||
impl super::TxManager for DatabaseTransaction { | ||
/// Upsert a new document into the database. | ||
/// | ||
/// # Errors | ||
/// Errors if the document cannot be inserted into the database. | ||
async fn create(&mut self, doc_id: &str) -> anyhow::Result<Option<i64>> { | ||
let statement = " | ||
INSERT OR IGNORE INTO document ( doc_id ) | ||
VALUES ( $1 ) | ||
"; | ||
let id = sqlx::query(statement) | ||
.bind(doc_id) | ||
.execute(&mut *self.tx) | ||
.await? | ||
.last_insert_id(); | ||
Ok(id) | ||
} | ||
} |
Oops, something went wrong.