REFACTOR A solid final draft.

This commit is contained in:
Elf M. Sternberg 2020-11-12 14:30:39 -08:00
parent 013ca18c62
commit e3fe863235
3 changed files with 471 additions and 525 deletions

View File

@ -7,7 +7,6 @@ pub use crate::errors::NoteStoreError;
pub use crate::store::NoteStore; pub use crate::store::NoteStore;
pub use crate::structs::{Note, NoteKind, NoteRelationship, PageRelationship}; pub use crate::structs::{Note, NoteKind, NoteRelationship, PageRelationship};
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -113,6 +112,4 @@ mod tests {
assert_eq!(newpages[1].parent_id, Some(newroot.id.clone())); assert_eq!(newpages[1].parent_id, Some(newroot.id.clone()));
assert_eq!(newpages[2].parent_id, Some(newpages[1].id.clone())); assert_eq!(newpages[2].parent_id, Some(newpages[1].id.clone()));
} }
} }

View File

@ -3,8 +3,8 @@ use lazy_static::lazy_static;
use regex::Regex; use regex::Regex;
use slug::slugify; use slug::slugify;
use sqlx::{sqlite::Sqlite, Acquire, Done, Executor, Transaction}; use sqlx::{sqlite::Sqlite, Acquire, Done, Executor, Transaction};
use std::collections::HashSet;
use std::cmp; use std::cmp;
use std::collections::HashSet;
type SqlResult<T> = sqlx::Result<T>; type SqlResult<T> = sqlx::Result<T>;
@ -35,7 +35,6 @@ lazy_static! {
); );
} }
lazy_static! { lazy_static! {
static ref SELECT_NOTES_BACKREFERENCING_PAGE_SQL: &'static str = static ref SELECT_NOTES_BACKREFERENCING_PAGE_SQL: &'static str =
include_str!("sql/select_notes_backreferencing_page.sql"); include_str!("sql/select_notes_backreferencing_page.sql");
@ -52,10 +51,7 @@ where
E: Executor<'a, Database = Sqlite>, E: Executor<'a, Database = Sqlite>,
{ {
let initialize_sql = include_str!("sql/initialize_database.sql"); let initialize_sql = include_str!("sql/initialize_database.sql");
sqlx::query(initialize_sql) sqlx::query(initialize_sql).execute(executor).await.map(|_| ())
.execute(executor)
.await
.map(|_| ())
} }
// ___ _ _ _ __ _ // ___ _ _ _ __ _
@ -70,14 +66,10 @@ async fn select_object_by_query<'a, E>(executor: E, query: &str, field: &str) ->
where where
E: Executor<'a, Database = Sqlite>, E: Executor<'a, Database = Sqlite>,
{ {
let r: Vec<RowNote> = sqlx::query_as(query) let r: Vec<RowNote> = sqlx::query_as(query).bind(field).fetch_all(executor).await?;
.bind(field)
.fetch_all(executor)
.await?;
Ok(r.into_iter().map(|z| Note::from(z)).collect()) Ok(r.into_iter().map(|z| Note::from(z)).collect())
} }
// Select the requested page via its id. This is fairly rare; // Select the requested page via its id. This is fairly rare;
// pages should usually be picked up via their title, but if you're // pages should usually be picked up via their title, but if you're
// navigating to an instance, this is how you specify the page in a // navigating to an instance, this is how you specify the page in a
@ -108,10 +100,7 @@ where
// of arrays, and inside each array is a list from a root page to // of arrays, and inside each array is a list from a root page to
// the note that references the give page. Clients may choose how // the note that references the give page. Clients may choose how
// they want to display that collection. // they want to display that collection.
pub(crate) async fn select_backreferences_for_page<'a, E>( pub(crate) async fn select_backreferences_for_page<'a, E>(executor: E, page_id: &str) -> SqlResult<Vec<Note>>
executor: E,
page_id: &str,
) -> SqlResult<Vec<Note>>
where where
E: Executor<'a, Database = Sqlite>, E: Executor<'a, Database = Sqlite>,
{ {
@ -148,7 +137,7 @@ where
} }
// Inserts a single note into the notes table. That is all. // Inserts a single note into the notes table. That is all.
pub(crate) async fn bulk_insert_notes<'a, E>(executor: E, notes: &[NewNote]) -> SqlResult<()> pub(crate) async fn insert_bulk_notes<'a, E>(executor: E, notes: &[NewNote]) -> SqlResult<()>
where where
E: Executor<'a, Database = Sqlite>, E: Executor<'a, Database = Sqlite>,
{ {
@ -157,11 +146,10 @@ where
} }
let insert_pattern = "VALUES (?, ?, ?, ?, ?, ?)".to_string(); let insert_pattern = "VALUES (?, ?, ?, ?, ?, ?)".to_string();
let insert_bulk_notes_sql = let insert_bulk_notes_sql = "INSERT INTO notes (id, content, kind, creation_date, updated_date, lastview_date) "
"INSERT INTO notes (id, content, kind, creation_date, updated_date, lastview_date) ".to_string() .to_string()
+ &[insert_pattern.as_str()] + &[insert_pattern.as_str()].repeat(notes.len()).join(", ")
.repeat(notes.len()) + &";".to_string();
.join(", ") + &";".to_string();
let mut request = sqlx::query(&insert_bulk_notes_sql); let mut request = sqlx::query(&insert_bulk_notes_sql);
for note in notes { for note in notes {
@ -248,11 +236,7 @@ pub(crate) fn create_page(title: &str, slug: &str) -> NewNote {
// \___/| .__/\__,_\__,_|\__\___| \___/|_||_\___| |_|\_\___/\__\___| // \___/| .__/\__,_\__,_|\__\___| \___/|_||_\___| |_|\_\___/\__\___|
// |_| // |_|
pub(crate) async fn update_note_content<'a, E>( pub(crate) async fn update_note_content<'a, E>(executor: E, note_id: &str, content: &str) -> SqlResult<()>
executor: E,
note_id: &NoteId,
content: &str,
) -> SqlResult<()>
where where
E: Executor<'a, Database = Sqlite>, E: Executor<'a, Database = Sqlite>,
{ {
@ -329,7 +313,7 @@ where
Ok(()) Ok(())
} }
pub(crate) async fn make_room_for_new_note<'a, E>( pub(crate) async fn make_room_for_new_note_relationship<'a, E>(
executor: E, executor: E,
parent_id: &str, parent_id: &str,
location: i64, location: i64,
@ -362,14 +346,11 @@ where
let row_count = assert_max_child_location_for_note(executor, note_id).await? + 1; let row_count = assert_max_child_location_for_note(executor, note_id).await? + 1;
Ok(match comp_loc { Ok(match comp_loc {
Some(location) => cmp::min(row_count, location), Some(location) => cmp::min(row_count, location),
None => row_count None => row_count,
}) })
} }
pub(crate) async fn assert_max_child_location_for_note<'a, E>( pub(crate) async fn assert_max_child_location_for_note<'a, E>(executor: E, note_id: &str) -> SqlResult<i64>
executor: E,
note_id: &str,
) -> SqlResult<i64>
where where
E: Executor<'a, Database = Sqlite>, E: Executor<'a, Database = Sqlite>,
{ {
@ -403,11 +384,10 @@ where
} }
let insert_pattern = format!("(?, ?, '{}')", PageRelationshipKind::Page.to_string()); let insert_pattern = format!("(?, ?, '{}')", PageRelationshipKind::Page.to_string());
let insert_note_page_references_sql = let insert_note_page_references_sql = "INSERT INTO note_page_relationships (note_id, page_id, kind) VALUES "
"INSERT INTO note_page_relationships (note_id, page_id, kind) VALUES ".to_string() .to_string()
+ &[insert_pattern.as_str()] + &[insert_pattern.as_str()].repeat(references.len()).join(", ")
.repeat(references.len()) + &";".to_string();
.join(", ") + &";".to_string();
let mut request = sqlx::query(&insert_note_page_references_sql); let mut request = sqlx::query(&insert_note_page_references_sql);
for reference in references { for reference in references {
@ -417,15 +397,11 @@ where
request.execute(executor).await.map(|_| ()) request.execute(executor).await.map(|_| ())
} }
pub(crate) async fn delete_bulk_note_to_page_relationships<'a, E>( pub(crate) async fn delete_bulk_note_to_page_relationships<'a, E>(executor: E, note_id: &str) -> SqlResult<()>
executor: E,
note_id: &str,
) -> SqlResult<()>
where where
E: Executor<'a, Database = Sqlite>, E: Executor<'a, Database = Sqlite>,
{ {
let delete_note_to_page_relationship_sql = let delete_note_to_page_relationship_sql = "DELETE FROM note_page_relationships WHERE and note_id = ?;";
"DELETE FROM note_page_relationships WHERE and note_id = ?;";
let _ = sqlx::query(delete_note_to_page_relationship_sql) let _ = sqlx::query(delete_note_to_page_relationship_sql)
.bind(note_id) .bind(note_id)
.execute(executor) .execute(executor)
@ -435,10 +411,7 @@ where
// Given the references supplied, and the references found in the datastore, // Given the references supplied, and the references found in the datastore,
// return a list of the references not found in the datastore. // return a list of the references not found in the datastore.
pub(crate) fn diff_references( pub(crate) fn diff_references(references: &[String], found_references: &[PageTitle]) -> Vec<String> {
references: &[String],
found_references: &[PageTitle],
) -> Vec<String> {
let all: HashSet<String> = references.iter().cloned().collect(); let all: HashSet<String> = references.iter().cloned().collect();
let found: HashSet<String> = found_references.iter().map(|r| r.content.clone()).collect(); let found: HashSet<String> = found_references.iter().map(|r| r.content.clone()).collect();
all.difference(&found).cloned().collect() all.difference(&found).cloned().collect()
@ -471,9 +444,8 @@ where
); );
} }
let find_all_references_for_sql = SELECT_ALL_REFERENCES_FOR_SQL_BASE.to_string() let find_all_references_for_sql =
+ &["?"].repeat(references.len()).join(",") SELECT_ALL_REFERENCES_FOR_SQL_BASE.to_string() + &["?"].repeat(references.len()).join(",") + &");".to_string();
+ &");".to_string();
let mut request = sqlx::query_as(&find_all_references_for_sql); let mut request = sqlx::query_as(&find_all_references_for_sql);
for id in references.iter() { for id in references.iter() {
@ -514,10 +486,7 @@ where
} }
} }
pub(crate) async fn delete_note_to_page_relationships<'a, E>( pub(crate) async fn delete_note_to_page_relationships<'a, E>(executor: E, note_id: &str) -> SqlResult<()>
executor: E,
note_id: &str,
) -> SqlResult<()>
where where
E: Executor<'a, Database = Sqlite>, E: Executor<'a, Database = Sqlite>,
{ {
@ -557,7 +526,7 @@ where
// After removing a note, recalculate the position of all notes under // After removing a note, recalculate the position of all notes under
// the parent note, such that there order is now completely // the parent note, such that there order is now completely
// sequential. // sequential.
pub(crate) async fn close_hole_for_deleted_note<'a, E>( pub(crate) async fn close_hole_for_deleted_note_relationship<'a, E>(
executor: E, executor: E,
parent_id: &str, parent_id: &str,
location: i64, location: i64,
@ -579,26 +548,24 @@ where
Ok(()) Ok(())
} }
// Given a list of references found in the content, generate the // Given a list of references found in the content, generate the
// references that do not previously exist, returning all found // references that do not previously exist, returning all found
// references. NOTE: The function signature for this is for a // references. NOTE: The function signature for this is for a
// transaction, and uses a nested transaction. // transaction, and uses a nested transaction.
pub(crate) async fn validate_or_generate_all_found_references( pub(crate) async fn validate_or_generate_all_found_references(
txi: &mut Transaction<'_, Sqlite>, txi: &mut Transaction<'_, Sqlite>,
references: &[String] references: &[String],
) -> SqlResult<Vec<String>> { ) -> SqlResult<Vec<String>> {
let mut tx = txi.begin().await?; let mut tx = txi.begin().await?;
let found_references = let found_references = find_all_page_from_list_of_references(&mut tx, &references).await?;
find_all_page_from_list_of_references(&mut tx, &references).await?;
let new_references = diff_references(&references, &found_references); let new_references = diff_references(&references, &found_references);
let mut new_page: Vec<NewNote> = vec![]; let mut new_page: Vec<NewNote> = vec![];
for one_reference in new_references.iter() { for one_reference in new_references.iter() {
let slug = generate_slug(&mut tx, one_reference).await?; let slug = generate_slug(&mut tx, one_reference).await?;
new_page.push(create_page(&one_reference, &slug)); new_page.push(create_page(&one_reference, &slug));
} }
let _ = bulk_insert_notes(&mut tx, &new_page).await?; let _ = insert_bulk_notes(&mut tx, &new_page).await?;
let mut all_reference_ids: Vec<String> = found_references.iter().map(|r| r.id.clone()).collect(); let mut all_reference_ids: Vec<String> = found_references.iter().map(|r| r.id.clone()).collect();
all_reference_ids.append(&mut new_page.iter().map(|r| r.id.clone()).collect()); all_reference_ids.append(&mut new_page.iter().map(|r| r.id.clone()).collect());
@ -618,8 +585,7 @@ pub(crate) async fn count_existing_note_relationships<'a, E>(executor: E, note_i
where where
E: Executor<'a, Database = Sqlite>, E: Executor<'a, Database = Sqlite>,
{ {
let count_existing_note_relationships_sql = let count_existing_note_relationships_sql = "SELECT COUNT(*) as count FROM note_relationships WHERE note_id = ?;";
"SELECT COUNT(*) as count FROM note_relationships WHERE note_id = ?;";
let count: RowCount = sqlx::query_as(&count_existing_note_relationships_sql) let count: RowCount = sqlx::query_as(&count_existing_note_relationships_sql)
.bind(note_id) .bind(note_id)
.fetch_one(executor) .fetch_one(executor)

View File

@ -80,9 +80,7 @@ impl NoteStore {
/// to its original empty form. Do not use unless you /// to its original empty form. Do not use unless you
/// really, really want that to happen. /// really, really want that to happen.
pub async fn reset_database(&self) -> NoteResult<()> { pub async fn reset_database(&self) -> NoteResult<()> {
reset_database(&*self.0) reset_database(&*self.0).await.map_err(NoteStoreError::DBError)
.await
.map_err(NoteStoreError::DBError)
} }
/// Fetch page by slug /// Fetch page by slug
@ -139,12 +137,7 @@ impl NoteStore {
Ok((vec![Note::from(page)], vec![])) Ok((vec![Note::from(page)], vec![]))
} }
pub async fn add_note( pub async fn add_note(&self, note: &NewNote, parent_id: &str, location: Option<i64>) -> NoteResult<String> {
&self,
note: &NewNote,
parent_id: &str,
location: Option<i64>,
) -> NoteResult<String> {
let kind = RelationshipKind::Direct; let kind = RelationshipKind::Direct;
let new_id = self.insert_note(note, parent_id, location, kind).await?; let new_id = self.insert_note(note, parent_id, location, kind).await?;
Ok(new_id) Ok(new_id)
@ -165,17 +158,11 @@ impl NoteStore {
let old_note_kind = old_note.kind; let old_note_kind = old_note.kind;
let _ = delete_note_to_note_relationship(&mut tx, &old_parent_id, &note_id).await?; let _ = delete_note_to_note_relationship(&mut tx, &old_parent_id, &note_id).await?;
let _ = close_hole_for_deleted_note(&mut tx, &old_parent_id, old_note_location).await?; let _ = close_hole_for_deleted_note_relationship(&mut tx, &old_parent_id, old_note_location).await?;
let new_location = determine_max_child_location_for_note(&mut tx, &new_parent_id, Some(new_location)).await?; let new_location = determine_max_child_location_for_note(&mut tx, &new_parent_id, Some(new_location)).await?;
let _ = make_room_for_new_note(&mut tx, &new_parent_id, new_location).await?; let _ = make_room_for_new_note_relationship(&mut tx, &new_parent_id, new_location).await?;
let _ = insert_note_to_note_relationship( let _ =
&mut tx, insert_note_to_note_relationship(&mut tx, &new_parent_id, &note_id, new_location, &old_note_kind).await?;
&new_parent_id,
&note_id,
new_location,
&old_note_kind,
)
.await?;
tx.commit().await?; tx.commit().await?;
Ok(()) Ok(())
} }
@ -184,14 +171,11 @@ impl NoteStore {
/// outgoing edge reference list every time. /// outgoing edge reference list every time.
pub async fn update_note_content(&self, note_id: &str, content: &str) -> NoteResult<()> { pub async fn update_note_content(&self, note_id: &str, content: &str) -> NoteResult<()> {
let references = build_references(&content); let references = build_references(&content);
let note_id = NoteId(note_id.to_string());
let mut tx = self.0.begin().await?; let mut tx = self.0.begin().await?;
let _ = update_note_content(&mut tx, &note_id, &content).await?; let _ = update_note_content(&mut tx, &note_id, &content).await?;
let _ = delete_bulk_note_to_page_relationships(&mut tx, &note_id).await?; let _ = delete_bulk_note_to_page_relationships(&mut tx, &note_id).await?;
let known_reference_ids = validate_or_generate_all_found_references(&mut tx, &references).await?; let known_reference_ids = validate_or_generate_all_found_references(&mut tx, &references).await?;
let _ = insert_bulk_note_to_page_relationships(&mut tx, &note_id, &known_reference_ids) let _ = insert_bulk_note_to_page_relationships(&mut tx, &note_id, &known_reference_ids).await?;
.await?;
tx.commit().await?; tx.commit().await?;
Ok(()) Ok(())
} }
@ -200,10 +184,11 @@ impl NoteStore {
/// references from that note to pages are also deleted. /// references from that note to pages are also deleted.
pub async fn delete_note(&self, note_id: &str, note_parent_id: &str) -> NoteResult<()> { pub async fn delete_note(&self, note_id: &str, note_parent_id: &str) -> NoteResult<()> {
let mut tx = self.0.begin().await?; let mut tx = self.0.begin().await?;
let note_id = NoteId(note_id.to_string());
let parent_id = ParentId(note_parent_id.to_string());
if *parent_id != *note_id { let note_id = note_id.to_string();
let parent_id = note_parent_id.to_string();
if parent_id != note_id {
let _ = delete_note_to_note_relationship(&mut tx, &parent_id, &note_id); let _ = delete_note_to_note_relationship(&mut tx, &parent_id, &note_id);
} }
// The big one: if zero parents report having an interest in this note, then it, // The big one: if zero parents report having an interest in this note, then it,
@ -259,14 +244,12 @@ impl NoteStore {
let mut tx = self.0.begin().await?; let mut tx = self.0.begin().await?;
let location = determine_max_child_location_for_note(&mut tx, parent_id, location).await?; let location = determine_max_child_location_for_note(&mut tx, parent_id, location).await?;
let note_id = NoteId(note.id.clone());
insert_note(&mut tx, &note).await?; insert_note(&mut tx, &note).await?;
make_room_for_new_note(&mut tx, &parent_id, location).await?; make_room_for_new_note_relationship(&mut tx, &parent_id, location).await?;
insert_note_to_note_relationship(&mut tx, &parent_id, &note_id, location, &kind).await?; insert_note_to_note_relationship(&mut tx, &parent_id, &note.id, location, &kind).await?;
let known_reference_ids = validate_or_generate_all_found_references(&mut tx, &references).await?; let known_reference_ids = validate_or_generate_all_found_references(&mut tx, &references).await?;
let _ = insert_bulk_note_to_page_relationships(&mut tx, &note_id, &known_reference_ids) let _ = insert_bulk_note_to_page_relationships(&mut tx, &note.id, &known_reference_ids).await?;
.await?;
tx.commit().await?; tx.commit().await?;
Ok(note_id.to_string()) Ok(note.id.to_string())
} }
} }