Skip to content

Commit 9bc023d

Browse files
committed
Introduce suggestion icon struct
1 parent a4ec8b2 commit 9bc023d

File tree

7 files changed

+2031
-1746
lines changed

7 files changed

+2031
-1746
lines changed

components/suggest/src/db.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::{
2626
DownloadedWikipediaSuggestion, SuggestRecordId,
2727
},
2828
schema::{clear_database, SuggestConnectionInitializer},
29-
suggestion::{cook_raw_suggestion_url, AmpSuggestionType, Suggestion},
29+
suggestion::{cook_raw_suggestion_url, AmpSuggestionType, Suggestion, SuggestionIcon},
3030
Result, SuggestionQuery,
3131
};
3232

@@ -274,7 +274,7 @@ impl<'a> SuggestDao<'a> {
274274
amp.iab_category,
275275
amp.impression_url,
276276
amp.click_url,
277-
i.data AS icon,
277+
i.data AS icon_data,
278278
i.mimetype AS icon_mimetype
279279
FROM
280280
amp_custom_details amp
@@ -290,6 +290,12 @@ impl<'a> SuggestDao<'a> {
290290
let cooked_url = cook_raw_suggestion_url(&raw_url);
291291
let raw_click_url = row.get::<_, String>("click_url")?;
292292
let cooked_click_url = cook_raw_suggestion_url(&raw_click_url);
293+
let icon_data = row.get::<_, Option<_>>("icon_data")?;
294+
let icon_mime_type = row.get::<_, Option<_>>("icon_mimetype")?;
295+
let icon = icon_data.map(|data| SuggestionIcon {
296+
data,
297+
mime_type: icon_mime_type.unwrap_or_default(),
298+
});
293299

294300
Ok(Suggestion::Amp {
295301
block_id: row.get("block_id")?,
@@ -300,8 +306,7 @@ impl<'a> SuggestDao<'a> {
300306
raw_url,
301307
full_keyword: full_keyword_from_db
302308
.unwrap_or_else(|| full_keyword(keyword_lowercased, &keywords)),
303-
icon: row.get("icon")?,
304-
icon_mimetype: row.get("icon_mimetype")?,
309+
icon,
305310
impression_url: row.get("impression_url")?,
306311
click_url: cooked_click_url,
307312
raw_click_url,
@@ -353,7 +358,7 @@ impl<'a> SuggestDao<'a> {
353358
},
354359
|row| row.get(0),
355360
)?;
356-
let (icon, icon_mimetype) = self
361+
let icon = self
357362
.conn
358363
.try_query_row(
359364
"SELECT i.data, i.mimetype
@@ -365,21 +370,20 @@ impl<'a> SuggestDao<'a> {
365370
":suggestion_id": suggestion_id
366371
},
367372
|row| -> Result<_> {
368-
Ok((
369-
row.get::<_, Option<Vec<u8>>>(0)?,
370-
row.get::<_, Option<String>>(1)?,
371-
))
373+
Ok(Some(SuggestionIcon {
374+
data: row.get::<_, Vec<u8>>(0)?,
375+
mime_type: row.get::<_, String>(1)?,
376+
}))
372377
},
373378
true,
374379
)?
375-
.unwrap_or((None, None));
380+
.unwrap_or(None);
376381

377382
Ok(Suggestion::Wikipedia {
378383
title,
379384
url: raw_url,
380385
full_keyword: full_keyword(keyword_lowercased, &keywords),
381386
icon,
382-
icon_mimetype,
383387
})
384388
},
385389
)?;
@@ -961,7 +965,7 @@ impl<'a> SuggestDao<'a> {
961965
}
962966

963967
/// Inserts or replaces an icon for a suggestion into the database.
964-
pub fn put_icon(&mut self, icon_id: &str, data: &[u8], mimetype: &str) -> Result<()> {
968+
pub fn put_icon(&mut self, icon_id: &str, data: &[u8], mime_type: &str) -> Result<()> {
965969
self.conn.execute(
966970
"INSERT OR REPLACE INTO icons(
967971
id,
@@ -976,7 +980,7 @@ impl<'a> SuggestDao<'a> {
976980
named_params! {
977981
":id": icon_id,
978982
":data": data,
979-
":mimetype": mimetype,
983+
":mimetype": mime_type,
980984
},
981985
)?;
982986
Ok(())

components/suggest/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub use config::{SuggestGlobalConfig, SuggestProviderConfig};
2222
pub use error::SuggestApiError;
2323
pub use provider::SuggestionProvider;
2424
pub use store::{SuggestIngestionConstraints, SuggestStore, SuggestStoreBuilder};
25-
pub use suggestion::{raw_suggestion_url_matches, Suggestion};
25+
pub use suggestion::{raw_suggestion_url_matches, Suggestion, SuggestionIcon};
2626

2727
pub(crate) type Result<T> = std::result::Result<T, error::Error>;
2828
pub type SuggestApiResult<T> = std::result::Result<T, error::SuggestApiError>;

components/suggest/src/schema.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use sql_support::open_database::{self, ConnectionInitializer};
1515
/// [`SuggestConnectionInitializer::upgrade_from`].
1616
/// a. If suggestions should be re-ingested after the migration, call `clear_database()` inside
1717
/// the migration.
18-
pub const VERSION: u32 = 19;
18+
pub const VERSION: u32 = 20;
1919

2020
/// The current Suggest database schema.
2121
pub const SQL: &str = "
@@ -195,6 +195,11 @@ CREATE TABLE IF NOT EXISTS dismissed_suggestions (
195195
)?;
196196
Ok(())
197197
}
198+
19 => {
199+
// Need to update new icons for Yelp especially in new schema.
200+
let _ = clear_database(tx);
201+
Ok(())
202+
}
198203
_ => Err(open_database::Error::IncompatibleVersion(version)),
199204
}
200205
}

0 commit comments

Comments
 (0)