Skip to content

Commit

Permalink
Improve async interop
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Nov 19, 2024
1 parent 0a906b7 commit 58c9d97
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
26 changes: 23 additions & 3 deletions src/area/model.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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(
Expand All @@ -51,12 +52,19 @@ impl Area {
.ok_or(error::select_after_insert_failed("area"))
}

pub async fn select_all_async(pool: &Pool) -> Result<Vec<Area>> {
pool.get()
.await?
.interact(|conn| Area::select_all(conn))
.await?
}

pub fn select_all(conn: &Connection) -> Result<Vec<Area>> {
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)?
Expand All @@ -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)?
Expand All @@ -80,6 +88,18 @@ impl Area {
.map_err(Into::into)
}

pub async fn select_updated_since_async(
updated_since: &OffsetDateTime,
limit: Option<i64>,
pool: &Pool,
) -> Result<Vec<Area>> {
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<i64>,
Expand Down
15 changes: 7 additions & 8 deletions src/area/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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()
Expand Down
9 changes: 2 additions & 7 deletions src/area/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,8 @@ pub async fn get(
args: Query<GetArgs>,
pool: Data<Pool>,
) -> Result<Json<Vec<GetItem>>, 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()))
Expand Down

0 comments on commit 58c9d97

Please sign in to comment.