Skip to content

Commit 8e8c869

Browse files
mimi89999mhammond
authored andcommitted
Add get_visit_count_for_host function
1 parent 0d8cc04 commit 8e8c869

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

components/places/src/ffi.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,18 @@ impl PlacesConnection {
297297
self.with_conn(|conn| history::get_visit_count(conn, exclude_types))
298298
}
299299

300+
#[handle_error(crate::Error)]
301+
pub fn get_visit_count_for_host(
302+
&self,
303+
host: String,
304+
before: PlacesTimestamp,
305+
exclude_types: VisitTransitionSet,
306+
) -> ApiResult<i64> {
307+
self.with_conn(|conn| {
308+
history::get_visit_count_for_host(conn, host.as_str(), before, exclude_types)
309+
})
310+
}
311+
300312
#[handle_error(crate::Error)]
301313
pub fn get_visit_page(
302314
&self,

components/places/src/places.udl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ interface PlacesConnection {
9797
[Throws=PlacesApiError]
9898
i64 get_visit_count(VisitTransitionSet exclude_types);
9999

100+
[Throws=PlacesApiError]
101+
i64 get_visit_count_for_host(string host, PlacesTimestamp before, VisitTransitionSet exclude_types);
102+
100103
[Throws=PlacesApiError]
101104
sequence<HistoryVisitInfo> get_visit_page(i64 offset, i64 count, VisitTransitionSet exclude_types);
102105
// TODO: bound should be a `PlacesTimestamp`?

components/places/src/storage/history.rs

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,6 +1425,32 @@ pub fn get_visit_count(db: &PlacesDb, exclude_types: VisitTransitionSet) -> Resu
14251425
Ok(count)
14261426
}
14271427

1428+
pub fn get_visit_count_for_host(
1429+
db: &PlacesDb,
1430+
host: &str,
1431+
before: Timestamp,
1432+
exclude_types: VisitTransitionSet,
1433+
) -> Result<i64> {
1434+
let allowed_types = exclude_types.complement();
1435+
let count = db.query_row_and_then_cachable(
1436+
"SELECT COUNT(*)
1437+
FROM moz_historyvisits
1438+
JOIN moz_places ON moz_places.id = moz_historyvisits.place_id
1439+
JOIN moz_origins ON moz_origins.id = moz_places.origin_id
1440+
WHERE moz_origins.host = :host
1441+
AND visit_date < :before
1442+
AND ((1 << visit_type) & :allowed_types) != 0",
1443+
rusqlite::named_params! {
1444+
":host": host,
1445+
":before": before,
1446+
":allowed_types": allowed_types,
1447+
},
1448+
|r| r.get(0),
1449+
true,
1450+
)?;
1451+
Ok(count)
1452+
}
1453+
14281454
pub fn get_visit_page(
14291455
db: &PlacesDb,
14301456
offset: i64,
@@ -3440,4 +3466,120 @@ mod tests {
34403466
.collect::<HashSet<_>>()
34413467
);
34423468
}
3469+
3470+
#[test]
3471+
fn test_get_visit_count_for_host() {
3472+
let _ = env_logger::try_init();
3473+
let conn = PlacesDb::open_in_memory(ConnectionType::ReadWrite).unwrap();
3474+
let start_timestamp = Timestamp::now();
3475+
let to_add = [
3476+
(
3477+
"http://example.com/0",
3478+
start_timestamp.0 - 200_200,
3479+
VisitType::Link,
3480+
),
3481+
(
3482+
"http://example.com/1",
3483+
start_timestamp.0 - 200_100,
3484+
VisitType::Link,
3485+
),
3486+
(
3487+
"https://example.com/0",
3488+
start_timestamp.0 - 200_000,
3489+
VisitType::Link,
3490+
),
3491+
(
3492+
"https://example1.com/0",
3493+
start_timestamp.0 - 100_600,
3494+
VisitType::Link,
3495+
),
3496+
(
3497+
"https://example1.com/0",
3498+
start_timestamp.0 - 100_500,
3499+
VisitType::Reload,
3500+
),
3501+
(
3502+
"https://example1.com/1",
3503+
start_timestamp.0 - 100_400,
3504+
VisitType::Link,
3505+
),
3506+
(
3507+
"https://example.com/2",
3508+
start_timestamp.0 - 100_300,
3509+
VisitType::Link,
3510+
),
3511+
(
3512+
"https://example.com/1",
3513+
start_timestamp.0 - 100_200,
3514+
VisitType::Link,
3515+
),
3516+
(
3517+
"https://example.com/0",
3518+
start_timestamp.0 - 100_100,
3519+
VisitType::Link,
3520+
),
3521+
];
3522+
3523+
for &(url, when, visit_type) in &to_add {
3524+
apply_observation(
3525+
&conn,
3526+
VisitObservation::new(Url::parse(url).unwrap())
3527+
.with_at(Timestamp(when))
3528+
.with_visit_type(visit_type),
3529+
)
3530+
.unwrap()
3531+
.unwrap();
3532+
}
3533+
3534+
assert_eq!(
3535+
get_visit_count_for_host(
3536+
&conn,
3537+
"example.com",
3538+
Timestamp(start_timestamp.0 - 100_000),
3539+
VisitTransitionSet::for_specific(&[])
3540+
)
3541+
.unwrap(),
3542+
6
3543+
);
3544+
assert_eq!(
3545+
get_visit_count_for_host(
3546+
&conn,
3547+
"example1.com",
3548+
Timestamp(start_timestamp.0 - 100_000),
3549+
VisitTransitionSet::for_specific(&[])
3550+
)
3551+
.unwrap(),
3552+
3
3553+
);
3554+
assert_eq!(
3555+
get_visit_count_for_host(
3556+
&conn,
3557+
"example.com",
3558+
Timestamp(start_timestamp.0 - 200_000),
3559+
VisitTransitionSet::for_specific(&[])
3560+
)
3561+
.unwrap(),
3562+
2
3563+
);
3564+
assert_eq!(
3565+
get_visit_count_for_host(
3566+
&conn,
3567+
"example1.com",
3568+
Timestamp(start_timestamp.0 - 100_500),
3569+
VisitTransitionSet::for_specific(&[])
3570+
)
3571+
.unwrap(),
3572+
1
3573+
);
3574+
assert_eq!(
3575+
get_visit_count_for_host(
3576+
&conn,
3577+
"example1.com",
3578+
Timestamp(start_timestamp.0 - 100_000),
3579+
VisitTransitionSet::for_specific(&[VisitType::Reload])
3580+
)
3581+
.unwrap(),
3582+
2
3583+
);
3584+
}
34433585
}

0 commit comments

Comments
 (0)