From bb841c77690f3ef235c117bf50696d19accc189b Mon Sep 17 00:00:00 2001 From: "Elf M. Sternberg" Date: Mon, 12 Oct 2020 20:44:27 -0700 Subject: [PATCH] Named ID references. --- server/nm-store/Cargo.toml | 1 + server/nm-store/src/#store.rs# | 366 +++++++++++++++++++++++++++++++++ server/nm-store/src/.#store.rs | 1 + server/nm-store/src/store.rs | 56 +++-- 4 files changed, 403 insertions(+), 21 deletions(-) create mode 100644 server/nm-store/src/#store.rs# create mode 120000 server/nm-store/src/.#store.rs diff --git a/server/nm-store/Cargo.toml b/server/nm-store/Cargo.toml index d0d9960..ded8910 100644 --- a/server/nm-store/Cargo.toml +++ b/server/nm-store/Cargo.toml @@ -15,6 +15,7 @@ friendly_id = "0.3.0" thiserror = "1.0.20" derive_builder = "0.9.0" lazy_static = "1.4.0" +shrinkwraprs = "0.3.0" regex = "1.3.9" slug = "0.1.4" tokio = { version = "0.2.22", features = ["rt-threaded", "blocking"] } diff --git a/server/nm-store/src/#store.rs# b/server/nm-store/src/#store.rs# new file mode 100644 index 0000000..6b6deb0 --- /dev/null +++ b/server/nm-store/src/#store.rs# @@ -0,0 +1,366 @@ +use crate::errors::NoteStoreError; +use crate::row_structs::{ + JustId, JustSlugs, NewNote, NewNoteBuilder, NewPage, NewPageBuilder, RawNote, RawPage, +}; +use friendly_id; +use lazy_static::lazy_static; +use regex::Regex; +use shrinkwraprs::Shrinkwrap; +use slug::slugify; +use sqlx; +use sqlx::{ + sqlite::{Sqlite, SqlitePool}, + Executor, +}; +use std::sync::Arc; + +#[derive(Shrinkwrap, Copy, Clone)] +struct ParentId(i64); + +#[derive(Shrinkwrap, Copy, Clone)] +struct NoteId(i64); + +/// A handle to our Sqlite database. +#[derive(Clone, Debug)] +pub struct NoteStore(Arc); + +type NoteResult = core::result::Result; +type SqlResult = sqlx::Result; + +impl NoteStore { + pub async fn new(url: &str) -> NoteResult { + let pool = SqlitePool::connect(url).await?; + Ok(NoteStore(Arc::new(pool))) + } + + // Erase all the data in the database and restore it + // to its original empty form. Do not use unless you + // really, really want that to happen. + pub async fn reset_database(&self) -> NoteResult<()> { + reset_database(&*self.0) + .await + .map_err(NoteStoreError::DBError) + } + + /// Fetch page by slug + /// + /// Supports the use case of the user navigating to a known place + /// via a bookmark or other URL. Since the title isn't clear from + /// the slug, the slug is insufficient to generate a new page, so + /// this use case says that in the event of a failure to find the + /// requested page, return a basic NotFound. + pub async fn get_page_by_slug(&self, slug: &str) -> NoteResult<(RawPage, Vec)> { + // let select_note_collection_for_root = include_str!("sql/select_note_collection_for_root.sql"); + let mut tx = self.0.begin().await?; + let page = select_page_by_slug(&mut tx, slug).await?; + // let notes = sqlx::query_as(select_note_collection_for_root) + // .bind(page.note_id) + // .fetch(&tx) + // .await?; + tx.commit().await?; + Ok((page, vec![])) + } + + /// Fetch page by title + /// + /// Supports the use case of the user navigating to a page via + /// the page's formal title. Since the title is the key reference + /// of the system, if no page with that title is found, a page with + /// that title is generated automatically. + pub async fn get_page_by_title(&self, title: &str) -> NoteResult<(RawPage, Vec)> { + let mut tx = self.0.begin().await?; + let (page, notes) = match select_page_by_title(&mut tx, title).await { + Ok(page) => { + let note_id = page.note_id; + ( + page, + select_note_collection_from_root(&mut tx, note_id).await?, + ) + } + Err(sqlx::Error::RowNotFound) => { + let page = { + let new_root_note = create_unique_root_note(); + let new_root_note_id = insert_one_new_note(&mut tx, &new_root_note).await?; + let new_page_slug = generate_slug(&mut tx, title).await?; + let new_page = create_new_page_for(&title, &new_page_slug, new_root_note_id); + let _ = insert_one_new_page(&mut tx, &new_page).await?; + select_page_by_title(&mut tx, &title).await? + }; + let note_id = page.note_id; + ( + page, + select_note_collection_from_root(&mut tx, note_id).await?, + ) + } + Err(e) => return Err(NoteStoreError::DBError(e)), + }; + tx.commit().await?; + Ok((page, notes)) + } + + /// Insert a new note at the requested position under a parent note. + pub async fn insert_nested_note( + &self, + note: NewNote, + parent_note_uuid: &str, + position: i32, + ) -> NoteResult { + let mut new_note = note.clone(); + new_note.uuid = friendly_id::create(); + let mut tx = self.0.begin().await?; + let parent_id = select_note_id_for_uuid(&mut tx, parent_note_uuid).await?; + let new_note_id = insert_one_new_note(&mut tx, &new_note).await?; + let _ = make_room_for_new_note(&mut tx, parent_id, position).await?; + let _ = insert_note_note_relationship(&mut tx, parent_id, new_note_id, position).await?; + tx.commit().await?; + Ok(new_note.uuid) + } + + pub async fn move_note(&self, note_uuid: &str, old_parent_note_uuid: &str, new_parent_note_uuid: &str, new_position: &str) -> NoteResult<()> { + + // Get IDs of all things. + // Delete old note/parent relationship and tighten order + // Create new order under new parent + // Insert new note/parent relationship + + + todo!(); + } +} + +// ___ _ _ +// | _ \_ _(_)_ ____ _| |_ ___ +// | _/ '_| \ V / _` | _/ -_) +// |_| |_| |_|\_/\__,_|\__\___| +// + +// I'm putting a lot of faith in Rust's ability to inline stuff. I'm +// sure this is okay. But really, this lets the API be clean and +// coherent and easily readable, and hides away the gnarliness of some +// of the SQL queries. + +async fn reset_database<'a, E>(executor: E) -> SqlResult<()> +where + E: Executor<'a, Database = Sqlite>, +{ + let initialize_sql = include_str!("sql/initialize_database.sql"); + sqlx::query(initialize_sql) + .execute(executor) + .await + .map(|_| ()) +} + +async fn select_page_by_slug<'a, E>(executor: E, slug: &str) -> SqlResult +where + E: Executor<'a, Database = Sqlite>, +{ + let select_one_page_by_slug_sql = concat!( + "SELECT id, title, slug, note_id, creation_date, updated_date, ", + "lastview_date, deleted_date FROM pages WHERE slug=?;" + ); + Ok(sqlx::query_as(&select_one_page_by_slug_sql) + .bind(&slug) + .fetch_one(executor) + .await?) +} + +async fn select_page_by_title<'a, E>(executor: E, title: &str) -> SqlResult +where + E: Executor<'a, Database = Sqlite>, +{ + let select_one_page_by_title_sql = concat!( + "SELECT id, title, slug, note_id, creation_date, updated_date, ", + "lastview_date, deleted_date FROM pages WHERE title=?;" + ); + Ok(sqlx::query_as(&select_one_page_by_title_sql) + .bind(&title) + .fetch_one(executor) + .await?) +} + +async fn select_note_id_for_uuid<'a, E>(executor: E, uuid: &str) -> SqlResult +where + E: Executor<'a, Database = Sqlite>, +{ + let select_note_id_for_uuid_sql = "SELECT id FROM notes WHERE uuid = ?"; + let id: JustId = sqlx::query_as(&select_note_id_for_uuid_sql) + .bind(&uuid) + .fetch_one(executor) + .await?; + Ok(ParentId(id.id)) +} + +async fn make_room_for_new_note<'a, E>( + executor: E, + parent_id: ParentId, + position: i32, +) -> SqlResult<()> +where + E: Executor<'a, Database = Sqlite>, +{ + let make_room_for_new_note_sql = concat!( + "UPDATE note_relationships ", + "SET position = position + 1 ", + "WHERE position >= ? and parent_id = ?;" + ); + + sqlx::query(make_room_for_new_note_sql) + .bind(&position) + .bind(&*parent_id) + .execute(executor) + .await + .map(|_| ()) +} + +async fn insert_note_note_relationship<'a, E>( + executor: E, + parent_id: ParentId, + note_id: NoteId, + position: i32, +) -> SqlResult<()> +where + E: Executor<'a, Database = Sqlite>, +{ + let insert_note_note_relationship_sql = concat!( + "INSERT INTO note_relationships (parent_id, note_id, position, nature) ", + "values (?, ?, ?, ?)" + ); + + sqlx::query(insert_note_note_relationship_sql) + .bind(&*parent_id) + .bind(&*note_id) + .bind(&position) + .bind("note") + .execute(executor) + .await + .map(|_| ()) +} + +async fn select_note_collection_from_root<'a, E>(executor: E, root: i64) -> SqlResult> +where + E: Executor<'a, Database = Sqlite>, +{ + let select_note_collection_from_root_sql = + include_str!("sql/select_note_collection_from_root.sql"); + Ok(sqlx::query_as(&select_note_collection_from_root_sql) + .bind(&root) + .fetch_all(executor) + .await?) +} + +async fn insert_one_new_note<'a, E>(executor: E, note: &NewNote) -> SqlResult +where + E: Executor<'a, Database = Sqlite>, +{ + let insert_one_note_sql = concat!( + "INSERT INTO notes ( ", + " uuid, ", + " content, ", + " notetype, ", + " creation_date, ", + " updated_date, ", + " lastview_date) ", + "VALUES (?, ?, ?, ?, ?, ?);" + ); + + Ok(NoteId( + sqlx::query(insert_one_note_sql) + .bind(¬e.uuid) + .bind(¬e.content) + .bind(¬e.notetype) + .bind(¬e.creation_date) + .bind(¬e.updated_date) + .bind(¬e.lastview_date) + .execute(executor) + .await? + .last_insert_rowid(), + )) +} + +fn find_maximal_slug(slugs: &Vec) -> Option { + lazy_static! { + static ref RE_CAP_NUM: Regex = Regex::new(r"-(\d+)$").unwrap(); + } + + if slugs.len() == 0 { + return None; + } + + let mut slug_counters: Vec = slugs + .iter() + .filter_map(|slug| RE_CAP_NUM.captures(&slug.slug)) + .map(|cap| cap.get(1).unwrap().as_str().parse::().unwrap()) + .collect(); + slug_counters.sort_unstable(); + slug_counters.pop() +} + +// Given an initial string and an existing collection of slugs, +// generate a new slug that does not conflict with the current +// collection. +async fn generate_slug<'a, E>(executor: E, title: &str) -> SqlResult +where + E: Executor<'a, Database = Sqlite>, +{ + lazy_static! { + static ref RE_STRIP_NUM: Regex = Regex::new(r"-\d+$").unwrap(); + } + + let initial_slug = slugify(title); + let sample_slug = RE_STRIP_NUM.replace_all(&initial_slug, ""); + let slug_finder_sql = "SELECT slug FROM pages WHERE slug LIKE '?%';"; + let similar_slugs: Vec = sqlx::query_as(&slug_finder_sql) + .bind(&*sample_slug) + .fetch_all(executor) + .await?; + let maximal_slug = find_maximal_slug(&similar_slugs); + match maximal_slug { + None => Ok(initial_slug), + Some(max_slug) => Ok(format!("{}-{}", initial_slug, max_slug + 1)), + } +} + +async fn insert_one_new_page<'a, E>(executor: E, page: &NewPage) -> SqlResult +where + E: Executor<'a, Database = Sqlite>, +{ + let insert_one_page_sql = concat!( + "INSERT INTO pages ( ", + " slug, ", + " title, ", + " note_id, ", + " creation_date, ", + " updated_date, ", + " lastview_date) ", + "VALUES (?, ?, ?, ?, ?, ?);" + ); + + Ok(sqlx::query(insert_one_page_sql) + .bind(&page.slug) + .bind(&page.title) + .bind(&page.note_id) + .bind(&page.creation_date) + .bind(&page.updated_date) + .bind(&page.lastview_date) + .execute(executor) + .await? + .last_insert_rowid()) +} + +fn create_unique_root_note() -> NewNote { + NewNoteBuilder::default() + .uuid(friendly_id::create()) + .content("".to_string()) + .notetype("root".to_string()) + .build() + .unwrap() +} + +fn create_new_page_for(title: &str, slug: &str, note_id: NoteId) -> NewPage { + NewPageBuilder::default() + .slug(slug.to_string()) + .title(title.to_string()) + .note_id(*note_id) + .build() + .unwrap() +} diff --git a/server/nm-store/src/.#store.rs b/server/nm-store/src/.#store.rs new file mode 120000 index 0000000..20ac428 --- /dev/null +++ b/server/nm-store/src/.#store.rs @@ -0,0 +1 @@ +elf@elsa.12937:1599450965 \ No newline at end of file diff --git a/server/nm-store/src/store.rs b/server/nm-store/src/store.rs index b68bd31..5ef80b8 100644 --- a/server/nm-store/src/store.rs +++ b/server/nm-store/src/store.rs @@ -5,6 +5,7 @@ use crate::row_structs::{ use friendly_id; use lazy_static::lazy_static; use regex::Regex; +use shrinkwraprs::Shrinkwrap; use slug::slugify; use sqlx; use sqlx::{ @@ -13,6 +14,12 @@ use sqlx::{ }; use std::sync::Arc; +#[derive(Shrinkwrap, Copy, Clone)] +struct ParentId(i64); + +#[derive(Shrinkwrap, Copy, Clone)] +struct NoteId(i64); + /// A handle to our Sqlite database. #[derive(Clone, Debug)] pub struct NoteStore(Arc); @@ -91,6 +98,7 @@ impl NoteStore { Ok((page, notes)) } + /// Insert a new note at the requested position under a parent note. pub async fn insert_nested_note( &self, note: NewNote, @@ -159,7 +167,7 @@ where .await?) } -async fn select_note_id_for_uuid<'a, E>(executor: E, uuid: &str) -> SqlResult +async fn select_note_id_for_uuid<'a, E>(executor: E, uuid: &str) -> SqlResult where E: Executor<'a, Database = Sqlite>, { @@ -168,10 +176,14 @@ where .bind(&uuid) .fetch_one(executor) .await?; - Ok(id.id) + Ok(ParentId(id.id)) } -async fn make_room_for_new_note<'a, E>(executor: E, parent_id: i64, position: i32) -> SqlResult<()> +async fn make_room_for_new_note<'a, E>( + executor: E, + parent_id: ParentId, + position: i32, +) -> SqlResult<()> where E: Executor<'a, Database = Sqlite>, { @@ -183,7 +195,7 @@ where sqlx::query(make_room_for_new_note_sql) .bind(&position) - .bind(&parent_id) + .bind(&*parent_id) .execute(executor) .await .map(|_| ()) @@ -191,8 +203,8 @@ where async fn insert_note_note_relationship<'a, E>( executor: E, - parent_id: i64, - note_id: i64, + parent_id: ParentId, + note_id: NoteId, position: i32, ) -> SqlResult<()> where @@ -204,8 +216,8 @@ where ); sqlx::query(insert_note_note_relationship_sql) - .bind(&parent_id) - .bind(¬e_id) + .bind(&*parent_id) + .bind(&*note_id) .bind(&position) .bind("note") .execute(executor) @@ -225,7 +237,7 @@ where .await?) } -async fn insert_one_new_note<'a, E>(executor: E, note: &NewNote) -> SqlResult +async fn insert_one_new_note<'a, E>(executor: E, note: &NewNote) -> SqlResult where E: Executor<'a, Database = Sqlite>, { @@ -240,16 +252,18 @@ where "VALUES (?, ?, ?, ?, ?, ?);" ); - Ok(sqlx::query(insert_one_note_sql) - .bind(¬e.uuid) - .bind(¬e.content) - .bind(¬e.notetype) - .bind(¬e.creation_date) - .bind(¬e.updated_date) - .bind(¬e.lastview_date) - .execute(executor) - .await? - .last_insert_rowid()) + Ok(NoteId( + sqlx::query(insert_one_note_sql) + .bind(¬e.uuid) + .bind(¬e.content) + .bind(¬e.notetype) + .bind(¬e.creation_date) + .bind(¬e.updated_date) + .bind(¬e.lastview_date) + .execute(executor) + .await? + .last_insert_rowid(), + )) } fn find_maximal_slug(slugs: &Vec) -> Option { @@ -331,11 +345,11 @@ fn create_unique_root_note() -> NewNote { .unwrap() } -fn create_new_page_for(title: &str, slug: &str, note_id: i64) -> NewPage { +fn create_new_page_for(title: &str, slug: &str, note_id: NoteId) -> NewPage { NewPageBuilder::default() .slug(slug.to_string()) .title(title.to_string()) - .note_id(note_id) + .note_id(*note_id) .build() .unwrap() }