From 58c9d97b67267246e607c0133f704d9fd6ac140d Mon Sep 17 00:00:00 2001 From: Igor Bubelov Date: Tue, 19 Nov 2024 13:23:34 +0700 Subject: [PATCH] Improve async interop --- src/area/model.rs | 26 +++++++++++++++++++++++--- src/area/v2.rs | 15 +++++++-------- src/area/v3.rs | 9 ++------- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/area/model.rs b/src/area/model.rs index e90acfa..368e4fc 100644 --- a/src/area/model.rs +++ b/src/area/model.rs @@ -1,4 +1,5 @@ use crate::{error, Result}; +use deadpool_sqlite::Pool; use geojson::{GeoJson, Geometry}; use rusqlite::{named_params, Connection, OptionalExtension, Row}; use serde_json::{Map, Value}; @@ -40,7 +41,7 @@ impl Area { let sql = format!( r#" INSERT INTO {TABLE_NAME} ({COL_TAGS}, {COL_ALIAS}) - VALUES (json(:{COL_TAGS}), :{COL_ALIAS}) + VALUES (json(:{COL_TAGS}), :{COL_ALIAS}); "# ); conn.execute( @@ -51,12 +52,19 @@ impl Area { .ok_or(error::select_after_insert_failed("area")) } + pub async fn select_all_async(pool: &Pool) -> Result> { + pool.get() + .await? + .interact(|conn| Area::select_all(conn)) + .await? + } + pub fn select_all(conn: &Connection) -> Result> { let sql = format!( r#" SELECT {MAPPER_PROJECTION} FROM {TABLE_NAME} - ORDER BY {COL_UPDATED_AT}, {COL_ID} + ORDER BY {COL_UPDATED_AT}, {COL_ID}; "# ); conn.prepare(&sql)? @@ -71,7 +79,7 @@ impl Area { SELECT {MAPPER_PROJECTION} FROM {TABLE_NAME} WHERE {COL_DELETED_AT} IS NULL - ORDER BY {COL_UPDATED_AT}, {COL_ID} + ORDER BY {COL_UPDATED_AT}, {COL_ID}; "# ); conn.prepare(&sql)? @@ -80,6 +88,18 @@ impl Area { .map_err(Into::into) } + pub async fn select_updated_since_async( + updated_since: &OffsetDateTime, + limit: Option, + pool: &Pool, + ) -> Result> { + let updated_since = updated_since.clone(); + pool.get() + .await? + .interact(move |conn| Area::select_updated_since(&updated_since, limit, conn)) + .await? + } + pub fn select_updated_since( updated_since: &OffsetDateTime, limit: Option, diff --git a/src/area/v2.rs b/src/area/v2.rs index 2e430eb..05c0b6a 100644 --- a/src/area/v2.rs +++ b/src/area/v2.rs @@ -16,6 +16,7 @@ use serde::Serialize; use serde_json::Map; use serde_json::Value; use time::format_description::well_known::Rfc3339; +use time::macros::datetime; use time::OffsetDateTime; #[derive(Deserialize)] @@ -69,14 +70,12 @@ pub async fn get( Redirect::to("https://static.btcmap.org/api/v2/areas.json").permanent(), )); } - let areas = pool - .get() - .await? - .interact(move |conn| match &args.updated_since { - Some(updated_since) => Area::select_updated_since(updated_since, args.limit, conn), - None => Area::select_all(conn), - }) - .await??; + let areas = Area::select_updated_since_async( + &args.updated_since.unwrap_or(datetime!(2000-01-01 0:00 UTC)), + args.limit, + &pool, + ) + .await?; let areas_len = areas.len(); let res = Either::Left(Json(areas.into_iter().map(|it| it.into()).collect())); req.extensions_mut() diff --git a/src/area/v3.rs b/src/area/v3.rs index 02df1fb..f30b1f2 100644 --- a/src/area/v3.rs +++ b/src/area/v3.rs @@ -63,13 +63,8 @@ pub async fn get( args: Query, pool: Data, ) -> Result>, Error> { - let areas = pool - .get() - .await? - .interact(move |conn| { - Area::select_updated_since(&args.updated_since, Some(args.limit), conn) - }) - .await??; + let areas = + Area::select_updated_since_async(&args.updated_since, Some(args.limit), &pool).await?; req.extensions_mut() .insert(RequestExtension::new(areas.len())); Ok(Json(areas.into_iter().map(|it| it.into()).collect()))