Skip to content

Commit

Permalink
Simplify sync
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Oct 17, 2023
1 parent c7c28bf commit 9436ba8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 61 deletions.
12 changes: 1 addition & 11 deletions src/command/generate_android_icons.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::model::element;
use crate::model::Element;
use crate::model::OverpassElement;
use crate::Connection;
use crate::Result;
use rusqlite::named_params;
use tracing::info;

pub async fn run(conn: &Connection) -> Result<()> {
Expand All @@ -25,15 +23,7 @@ pub async fn run(conn: &Connection) -> Result<()> {

if old_icon != new_icon {
info!(element.id, old_icon, new_icon, "Updating icon");

conn.execute(
element::INSERT_TAG,
named_params! {
":element_id": element.id,
":tag_name": "$.icon:android",
":tag_value": new_icon,
},
)?;
Element::insert_tag(&element.id, "icon:android", &new_icon, &conn)?;
tokio::time::sleep(tokio::time::Duration::from_millis(5)).await;
}

Expand Down
11 changes: 1 addition & 10 deletions src/command/generate_element_categories.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::model::element;
use crate::model::Element;
use crate::model::OverpassElement;
use crate::Connection;
Expand Down Expand Up @@ -26,15 +25,7 @@ pub async fn run(conn: &Connection) -> Result<()> {

if new_category != old_category {
info!(element.id, old_category, new_category, "Updating category",);

conn.execute(
element::INSERT_TAG,
named_params! {
":element_id": element.id,
":tag_name": "$.category",
":tag_value": new_category,
},
)?;
Element::insert_tag(&element.id, "category", &new_category, &conn)?;
tokio::time::sleep(tokio::time::Duration::from_millis(5)).await;
}

Expand Down
29 changes: 3 additions & 26 deletions src/command/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,7 @@ async fn process_elements(fresh_elements: Vec<OverpassElement>, mut db: Connecti

if new_android_icon != old_android_icon {
info!(old_android_icon, new_android_icon, "Updating Android icon");

tx.execute(
element::INSERT_TAG,
named_params! {
":element_id": &element.id,
":tag_name": "$.icon:android",
":tag_value": &new_android_icon,
},
)?;
Element::insert_tag(&element.id, "icon:android", &new_android_icon, &tx)?;
}
}

Expand Down Expand Up @@ -217,23 +209,8 @@ async fn process_elements(fresh_elements: Vec<OverpassElement>, mut db: Connecti
let category = element.generate_category();
let android_icon = element.generate_android_icon();

tx.execute(
element::INSERT_TAG,
named_params! {
":element_id": &element.id,
":tag_name": "$.category",
":tag_value": &category,
},
)?;

tx.execute(
element::INSERT_TAG,
named_params! {
":element_id": &element.id,
":tag_name": "$.icon:android",
":tag_value": &android_icon,
},
)?;
Element::insert_tag(&element.id, "category", &category, &tx)?;
Element::insert_tag(&element.id, "icon:android", &android_icon, &tx)?;

info!(category, android_icon);

Expand Down
9 changes: 1 addition & 8 deletions src/controller/element_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,7 @@ async fn post_tags(
match element {
Some(element) => {
if args.value.len() > 0 {
conn.execute(
element::INSERT_TAG,
named_params! {
":element_id": element.id,
":tag_name": format!("$.{}", args.name),
":tag_value": args.value,
},
)?;
Element::insert_tag(&element.id, &args.name, &args.value, &conn)?;
} else {
conn.execute(
element::DELETE_TAG,
Expand Down
40 changes: 34 additions & 6 deletions src/model/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,28 @@ impl Element {
Ok(())
}

pub fn insert_tag(
id: &str,
tag_name: &str,
tag_value: &str,
conn: &Connection,
) -> crate::Result<()> {
let tag_name = format!("$.{tag_name}");

let query = r#"
UPDATE element
SET tags = json_set(tags, :tag_name, :tag_value)
WHERE id = :id
"#;

conn.execute(
query,
named_params! { ":id": id, ":tag_name": tag_name, ":tag_value": tag_value },
)?;

Ok(())
}

pub fn get_btcmap_tag_value_str(&self, name: &str) -> &str {
self.tags
.get(name)
Expand Down Expand Up @@ -189,12 +211,6 @@ impl Element {
}
}

pub static INSERT_TAG: &str = r#"
UPDATE element
SET tags = json_set(tags, :tag_name, :tag_value)
WHERE id = :element_id
"#;

pub static DELETE_TAG: &str = r#"
UPDATE element
SET tags = json_remove(tags, :tag_name)
Expand Down Expand Up @@ -354,4 +370,16 @@ mod test {
assert_eq!(&tag_value, &element.tags[tag_name],);
Ok(())
}

#[test]
fn insert_tag() -> Result<()> {
let conn = db::setup_connection()?;
let tag_name = "foo";
let tag_value = "bar";
Element::insert(&OverpassElement::mock(), &conn)?;
Element::insert_tag("node:1", tag_name, tag_value, &conn)?;
let element = Element::select_by_id("node:1", &conn)?.unwrap();
assert_eq!(tag_value, element.tags[tag_name].as_str().unwrap());
Ok(())
}
}

0 comments on commit 9436ba8

Please sign in to comment.