Skip to content

Commit 1ebbd1a

Browse files
committed
Suggest: shorten the type names from the rs module.
Nan suggested this in the review of #6236 and I think it's a great idea.
1 parent 87ddcfc commit 1ebbd1a

File tree

5 files changed

+43
-82
lines changed

5 files changed

+43
-82
lines changed

components/suggest/src/benchmarks/client.rs

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
use crate::{
6-
rs::{
7-
SuggestRemoteSettingsClient, SuggestRemoteSettingsRecord,
8-
SuggestRemoteSettingsRecordRequest,
9-
},
10-
Result,
11-
};
5+
use crate::{rs, Result};
126
use parking_lot::Mutex;
137
use remote_settings::{Client, RemoteSettingsConfig};
148
use std::collections::HashMap;
@@ -17,11 +11,10 @@ use std::collections::HashMap;
1711
///
1812
/// This should be used to run a full ingestion.
1913
/// Then it can be converted into a [RemoteSettingsBenchmarkClient], which allows benchmark code to exclude the network request time.
20-
/// [RemoteSettingsBenchmarkClient] implements [SuggestRemoteSettingsClient] by getting data from a HashMap rather than hitting the network.
14+
/// [RemoteSettingsBenchmarkClient] implements [rs::Client] by getting data from a HashMap rather than hitting the network.
2115
pub struct RemoteSettingsWarmUpClient {
2216
client: Client,
23-
pub get_records_responses:
24-
Mutex<HashMap<SuggestRemoteSettingsRecordRequest, Vec<SuggestRemoteSettingsRecord>>>,
17+
pub get_records_responses: Mutex<HashMap<rs::RecordRequest, Vec<rs::Record>>>,
2518
}
2619

2720
impl RemoteSettingsWarmUpClient {
@@ -45,13 +38,9 @@ impl Default for RemoteSettingsWarmUpClient {
4538
}
4639
}
4740

48-
impl SuggestRemoteSettingsClient for RemoteSettingsWarmUpClient {
49-
fn get_records(
50-
&self,
51-
request: SuggestRemoteSettingsRecordRequest,
52-
) -> Result<Vec<SuggestRemoteSettingsRecord>> {
53-
let response =
54-
<Client as SuggestRemoteSettingsClient>::get_records(&self.client, request.clone())?;
41+
impl rs::Client for RemoteSettingsWarmUpClient {
42+
fn get_records(&self, request: rs::RecordRequest) -> Result<Vec<rs::Record>> {
43+
let response = <Client as rs::Client>::get_records(&self.client, request.clone())?;
5544
self.get_records_responses
5645
.lock()
5746
.insert(request, response.clone());
@@ -61,15 +50,11 @@ impl SuggestRemoteSettingsClient for RemoteSettingsWarmUpClient {
6150

6251
#[derive(Clone)]
6352
pub struct RemoteSettingsBenchmarkClient {
64-
pub get_records_responses:
65-
HashMap<SuggestRemoteSettingsRecordRequest, Vec<SuggestRemoteSettingsRecord>>,
53+
pub get_records_responses: HashMap<rs::RecordRequest, Vec<rs::Record>>,
6654
}
6755

68-
impl SuggestRemoteSettingsClient for RemoteSettingsBenchmarkClient {
69-
fn get_records(
70-
&self,
71-
request: SuggestRemoteSettingsRecordRequest,
72-
) -> Result<Vec<SuggestRemoteSettingsRecord>> {
56+
impl rs::Client for RemoteSettingsBenchmarkClient {
57+
fn get_records(&self, request: rs::RecordRequest) -> Result<Vec<rs::Record>> {
7358
Ok(self
7459
.get_records_responses
7560
.get(&request)

components/suggest/src/db.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::{
2222
rs::{
2323
DownloadedAmoSuggestion, DownloadedAmpSuggestion, DownloadedAmpWikipediaSuggestion,
2424
DownloadedMdnSuggestion, DownloadedPocketSuggestion, DownloadedWeatherData,
25-
DownloadedWikipediaSuggestion, SuggestRecordId, SuggestRemoteSettingsRecord,
25+
DownloadedWikipediaSuggestion, Record, SuggestRecordId,
2626
},
2727
schema::{clear_database, SuggestConnectionInitializer},
2828
suggestion::{cook_raw_suggestion_url, AmpSuggestionType, Suggestion},
@@ -134,21 +134,13 @@ impl<'a> SuggestDao<'a> {
134134
//
135135
// These methods combine several low-level calls into one logical operation.
136136

137-
pub fn handle_ingested_record(
138-
&mut self,
139-
last_ingest_key: &str,
140-
record: &SuggestRemoteSettingsRecord,
141-
) -> Result<()> {
137+
pub fn handle_ingested_record(&mut self, last_ingest_key: &str, record: &Record) -> Result<()> {
142138
// Advance the last fetch time, so that we can resume
143139
// fetching after this record if we're interrupted.
144140
self.put_last_ingest_if_newer(last_ingest_key, record.last_modified)
145141
}
146142

147-
pub fn handle_deleted_record(
148-
&mut self,
149-
last_ingest_key: &str,
150-
record: &SuggestRemoteSettingsRecord,
151-
) -> Result<()> {
143+
pub fn handle_deleted_record(&mut self, last_ingest_key: &str, record: &Record) -> Result<()> {
152144
let record_id = SuggestRecordId::from(&record.id);
153145
// Drop either the icon or suggestions, records only contain one or the other
154146
match record_id.as_icon_id() {

components/suggest/src/rs.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,13 @@ pub(crate) const DEFAULT_RECORDS_TYPES: [SuggestRecordType; 9] = [
6464
/// A trait for a client that downloads suggestions from Remote Settings.
6565
///
6666
/// This trait lets tests use a mock client.
67-
pub(crate) trait SuggestRemoteSettingsClient {
67+
pub(crate) trait Client {
6868
/// Fetch a list of records and attachment data
69-
fn get_records(
70-
&self,
71-
request: SuggestRemoteSettingsRecordRequest,
72-
) -> Result<Vec<SuggestRemoteSettingsRecord>>;
69+
fn get_records(&self, request: RecordRequest) -> Result<Vec<Record>>;
7370
}
7471

75-
impl SuggestRemoteSettingsClient for remote_settings::Client {
76-
fn get_records(
77-
&self,
78-
request: SuggestRemoteSettingsRecordRequest,
79-
) -> Result<Vec<SuggestRemoteSettingsRecord>> {
72+
impl Client for remote_settings::Client {
73+
fn get_records(&self, request: RecordRequest) -> Result<Vec<Record>> {
8074
let options = request.into();
8175
self.get_records_with_options(&options)?
8276
.records
@@ -87,21 +81,21 @@ impl SuggestRemoteSettingsClient for remote_settings::Client {
8781
.as_ref()
8882
.map(|a| self.get_attachment(&a.location))
8983
.transpose()?;
90-
Ok(SuggestRemoteSettingsRecord::new(record, attachment_data))
84+
Ok(Record::new(record, attachment_data))
9185
})
9286
.collect()
9387
}
9488
}
9589

9690
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
97-
pub struct SuggestRemoteSettingsRecordRequest {
91+
pub struct RecordRequest {
9892
pub record_type: Option<String>,
9993
pub last_modified: Option<u64>,
10094
pub limit: Option<u64>,
10195
}
10296

103-
impl From<SuggestRemoteSettingsRecordRequest> for GetItemsOptions {
104-
fn from(value: SuggestRemoteSettingsRecordRequest) -> Self {
97+
impl From<RecordRequest> for GetItemsOptions {
98+
fn from(value: RecordRequest) -> Self {
10599
let mut options = GetItemsOptions::new();
106100

107101
// Remote Settings returns records in descending modification order
@@ -130,7 +124,7 @@ impl From<SuggestRemoteSettingsRecordRequest> for GetItemsOptions {
130124
///
131125
/// This is `remote_settings::RemoteSettingsRecord`, plus the downloaded attachment data.
132126
#[derive(Clone, Debug, Default)]
133-
pub struct SuggestRemoteSettingsRecord {
127+
pub struct Record {
134128
pub id: String,
135129
pub last_modified: u64,
136130
pub deleted: bool,
@@ -139,7 +133,7 @@ pub struct SuggestRemoteSettingsRecord {
139133
pub attachment_data: Option<Vec<u8>>,
140134
}
141135

142-
impl SuggestRemoteSettingsRecord {
136+
impl Record {
143137
pub fn new(record: RemoteSettingsRecord, attachment_data: Option<Vec<u8>>) -> Self {
144138
Self {
145139
id: record.id,
@@ -634,9 +628,9 @@ mod test {
634628
#[test]
635629
fn test_remote_settings_limits() {
636630
fn check_limit(suggestion_limit: Option<u64>, expected_record_limit: Option<&str>) {
637-
let request = SuggestRemoteSettingsRecordRequest {
631+
let request = RecordRequest {
638632
limit: suggestion_limit,
639-
..SuggestRemoteSettingsRecordRequest::default()
633+
..RecordRequest::default()
640634
};
641635
let options: GetItemsOptions = request.into();
642636
let actual_record_limit = options

components/suggest/src/store.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ use crate::{
2222
error::Error,
2323
provider::SuggestionProvider,
2424
rs::{
25-
SuggestAttachment, SuggestRecord, SuggestRecordId, SuggestRecordType,
26-
SuggestRemoteSettingsClient, SuggestRemoteSettingsRecord,
27-
SuggestRemoteSettingsRecordRequest, DEFAULT_RECORDS_TYPES, REMOTE_SETTINGS_COLLECTION,
25+
Client, Record, RecordRequest, SuggestAttachment, SuggestRecord, SuggestRecordId,
26+
SuggestRecordType, DEFAULT_RECORDS_TYPES, REMOTE_SETTINGS_COLLECTION,
2827
},
2928
Result, SuggestApiResult, Suggestion, SuggestionQuery,
3029
};
@@ -311,7 +310,7 @@ impl<S> SuggestStoreInner<S> {
311310

312311
impl<S> SuggestStoreInner<S>
313312
where
314-
S: SuggestRemoteSettingsClient,
313+
S: Client,
315314
{
316315
pub fn ingest(&self, constraints: SuggestIngestionConstraints) -> Result<()> {
317316
let writer = &self.dbs()?.writer;
@@ -344,7 +343,7 @@ where
344343
writer: &SuggestDb,
345344
constraints: &SuggestIngestionConstraints,
346345
) -> Result<()> {
347-
let request = SuggestRemoteSettingsRecordRequest {
346+
let request = RecordRequest {
348347
record_type: Some(ingest_record_type.to_string()),
349348
last_modified: writer.read(|dao| {
350349
dao.get_meta::<u64>(ingest_record_type.last_ingest_meta_key().as_str())
@@ -361,7 +360,7 @@ where
361360
&self,
362361
last_ingest_key: &str,
363362
writer: &SuggestDb,
364-
records: &[SuggestRemoteSettingsRecord],
363+
records: &[Record],
365364
) -> Result<()> {
366365
for record in records {
367366
let record_id = SuggestRecordId::from(&record.id);
@@ -493,7 +492,7 @@ where
493492
&self,
494493
last_ingest_key: &str,
495494
writer: &SuggestDb,
496-
record: &SuggestRemoteSettingsRecord,
495+
record: &Record,
497496
ingestion_handler: impl FnOnce(&mut SuggestDao<'_>, &SuggestRecordId) -> Result<()>,
498497
) -> Result<()> {
499498
let record_id = SuggestRecordId::from(&record.id);
@@ -516,7 +515,7 @@ where
516515
&self,
517516
last_ingest_key: &str,
518517
writer: &SuggestDb,
519-
record: &SuggestRemoteSettingsRecord,
518+
record: &Record,
520519
ingestion_handler: impl FnOnce(&mut SuggestDao<'_>, &SuggestRecordId, &[T]) -> Result<()>,
521520
) -> Result<()>
522521
where
@@ -549,7 +548,7 @@ where
549548
#[cfg(feature = "benchmark_api")]
550549
impl<S> SuggestStoreInner<S>
551550
where
552-
S: SuggestRemoteSettingsClient,
551+
S: Client,
553552
{
554553
pub fn into_settings_client(self) -> S {
555554
self.settings_client
@@ -681,7 +680,7 @@ mod tests {
681680
/// Creates a unique in-memory Suggest store.
682681
fn unique_test_store<S>(settings_client: S) -> SuggestStoreInner<S>
683682
where
684-
S: SuggestRemoteSettingsClient,
683+
S: Client,
685684
{
686685
let mut unique_suffix = [0u8; 8];
687686
rand::fill(&mut unique_suffix).expect("Failed to generate unique suffix for test store");
@@ -758,11 +757,8 @@ mod tests {
758757
}
759758
}
760759

761-
impl SuggestRemoteSettingsClient for SnapshotSettingsClient {
762-
fn get_records(
763-
&self,
764-
_request: SuggestRemoteSettingsRecordRequest,
765-
) -> Result<Vec<SuggestRemoteSettingsRecord>> {
760+
impl Client for SnapshotSettingsClient {
761+
fn get_records(&self, _request: RecordRequest) -> Result<Vec<Record>> {
766762
let snapshot = self.snapshot.borrow();
767763
snapshot
768764
.records
@@ -780,7 +776,7 @@ mod tests {
780776
.transpose()?
781777
.cloned();
782778

783-
Ok(SuggestRemoteSettingsRecord::new(r.clone(), attachment))
779+
Ok(Record::new(r.clone(), attachment))
784780
})
785781
.collect()
786782
}

components/suggest/src/testing/client.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ use serde_json::json;
99
use serde_json::Value as JsonValue;
1010

1111
use crate::{
12-
rs::{
13-
SuggestRemoteSettingsClient, SuggestRemoteSettingsRecord,
14-
SuggestRemoteSettingsRecordRequest,
15-
},
12+
rs::{Client, Record, RecordRequest},
1613
testing::JsonExt,
1714
Result,
1815
};
@@ -22,7 +19,7 @@ use crate::{
2219
/// MockRemoteSettingsClient uses the builder pattern for its API: most methods input `self` and
2320
/// return a modified version of it.
2421
pub struct MockRemoteSettingsClient {
25-
pub records: HashMap<String, Vec<SuggestRemoteSettingsRecord>>,
22+
pub records: HashMap<String, Vec<Record>>,
2623
pub last_modified_timestamp: u64,
2724
}
2825

@@ -43,7 +40,7 @@ impl MockRemoteSettingsClient {
4340
pub fn with_record(mut self, record_type: &str, record_id: &str, items: JsonValue) -> Self {
4441
let location = format!("{record_type}-{record_id}.json");
4542
let records = self.records.entry(record_type.to_string()).or_default();
46-
records.push(SuggestRemoteSettingsRecord {
43+
records.push(Record {
4744
id: record_id.to_string(),
4845
last_modified: self.last_modified_timestamp,
4946
deleted: false,
@@ -67,7 +64,7 @@ impl MockRemoteSettingsClient {
6764
/// This is used by remote settings to indicated a deleted record
6865
pub fn with_tombstone(mut self, record_type: &str, record_id: &str) -> Self {
6966
let records = self.records.entry(record_type.to_string()).or_default();
70-
records.push(SuggestRemoteSettingsRecord {
67+
records.push(Record {
7168
id: record_id.to_string(),
7269
last_modified: self.last_modified_timestamp,
7370
deleted: true,
@@ -84,7 +81,7 @@ impl MockRemoteSettingsClient {
8481
let record_id = format!("icon-{icon_id}");
8582
let location = format!("icon-{icon_id}.png");
8683
let records = self.records.entry("icon".to_string()).or_default();
87-
records.push(SuggestRemoteSettingsRecord {
84+
records.push(Record {
8885
id: record_id.to_string(),
8986
last_modified: self.last_modified_timestamp,
9087
deleted: false,
@@ -113,11 +110,8 @@ pub struct MockIcon {
113110
pub mimetype: &'static str,
114111
}
115112

116-
impl SuggestRemoteSettingsClient for MockRemoteSettingsClient {
117-
fn get_records(
118-
&self,
119-
request: SuggestRemoteSettingsRecordRequest,
120-
) -> Result<Vec<SuggestRemoteSettingsRecord>> {
113+
impl Client for MockRemoteSettingsClient {
114+
fn get_records(&self, request: RecordRequest) -> Result<Vec<Record>> {
121115
let record_type = request.record_type.unwrap_or_else(|| {
122116
panic!("MockRemoteSettingsClient.get_records: record_type required ")
123117
});

0 commit comments

Comments
 (0)