diff --git a/swh/storage/algos/origin.py b/swh/storage/algos/origin.py --- a/swh/storage/algos/origin.py +++ b/swh/storage/algos/origin.py @@ -44,6 +44,7 @@ type: Optional[str] = None, allowed_statuses: Optional[Iterable[str]] = None, require_snapshot: bool = False, + limit: Optional[int] = 100, ) -> Optional[Tuple[OriginVisit, OriginVisitStatus]]: """Get the latest origin visit (and status) of an origin. Optionally, a combination of criteria can be provided, origin type, allowed statuses or if a visit has a @@ -69,9 +70,7 @@ status exist (and match the search criteria), None otherwise. """ - # visits order are from older visit to most recent. - visits = list(storage.origin_visit_get(origin_url)) - visits.reverse() + visits = list(storage.origin_visit_get(origin_url, order="desc", limit=limit)) if not visits: return None visit_status: Optional[OriginVisitStatus] = None diff --git a/swh/storage/algos/snapshot.py b/swh/storage/algos/snapshot.py --- a/swh/storage/algos/snapshot.py +++ b/swh/storage/algos/snapshot.py @@ -41,6 +41,7 @@ origin: str, allowed_statuses: Optional[Iterable[str]] = None, branches_count: Optional[int] = None, + limit: Optional[int] = 100, ) -> Optional[Snapshot]: """Get the latest snapshot for the given origin, optionally only from visits that have one of the given allowed_statuses. @@ -58,6 +59,7 @@ branches_count: Optional parameter to retrieve snapshot with all branches (default behavior when None) or not. If set to positive number, the snapshot will be partial with only that number of branches. + limit: Bound the search to a given limit Raises: ValueError if branches_count is not a positive value @@ -67,7 +69,11 @@ """ visit_and_status = origin_get_latest_visit_status( - storage, origin, allowed_statuses=allowed_statuses, require_snapshot=True + storage, + origin, + allowed_statuses=allowed_statuses, + limit=limit, + require_snapshot=True, ) if not visit_and_status: diff --git a/swh/storage/tests/algos/test_origin.py b/swh/storage/tests/algos/test_origin.py --- a/swh/storage/tests/algos/test_origin.py +++ b/swh/storage/tests/algos/test_origin.py @@ -333,3 +333,10 @@ assert actual_ov2.visit == ov2.visit assert actual_ov2.type == ov2.type assert actual_ovs22 == ovs22 + + # Last visit has no snapshot so only looking back with a limit of 1 visit, we don't + # find anything + visit_and_status = origin_get_latest_visit_status( + swh_storage, origin2.url, require_snapshot=True, limit=1 + ) + assert visit_and_status is None diff --git a/swh/storage/tests/algos/test_snapshot.py b/swh/storage/tests/algos/test_snapshot.py --- a/swh/storage/tests/algos/test_snapshot.py +++ b/swh/storage/tests/algos/test_snapshot.py @@ -147,20 +147,8 @@ ] ) - actual_snapshot = snapshot_get_latest(swh_storage, origin.url) - assert actual_snapshot is not None - assert actual_snapshot == complete_snapshot - - swh_storage.origin_visit_status_add( - [ - OriginVisitStatus( - origin=origin.url, - visit=visit_id, - date=date_now, - status="full", - snapshot=complete_snapshot.id, - ) - ] + swh_storage.origin_visit_add( + [OriginVisit(origin=origin.url, date=now(), type=data.type_visit1,)] ) actual_snapshot = snapshot_get_latest(swh_storage, origin.url) @@ -174,3 +162,8 @@ with pytest.raises(ValueError, match="branches_count must be a positive integer"): snapshot_get_latest(swh_storage, origin.url, branches_count="something-wrong") + + # Last visit has no snapshot so only looking back with a limit of 1 visit, we don't + # find anything + actual_snapshot = snapshot_get_latest(swh_storage, origin.url, limit=1) + assert actual_snapshot is None