Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/db.py
# Copyright (C) 2015-2020 The Software Heritage developers | # Copyright (C) 2015-2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import datetime | import datetime | ||||
import random | import random | ||||
import select | import select | ||||
from typing import Any, Dict, Iterable, List, Optional, Tuple | from typing import Any, Dict, Iterable, List, Optional, Tuple | ||||
from swh.core.db import BaseDb | from swh.core.db import BaseDb | ||||
from swh.core.db.db_utils import stored_procedure, jsonize as _jsonize | from swh.core.db.db_utils import stored_procedure, jsonize as _jsonize | ||||
from swh.core.db.db_utils import execute_values_generator | from swh.core.db.db_utils import execute_values_generator | ||||
from swh.model.model import OriginVisit, OriginVisitStatus, SHA1_SIZE | from swh.model.model import OriginVisit, OriginVisitStatus, SHA1_SIZE | ||||
from swh.storage.interface import ListOrder | |||||
def jsonize(d): | def jsonize(d): | ||||
return _jsonize(dict(d) if d is not None else None) | return _jsonize(dict(d) if d is not None else None) | ||||
class Db(BaseDb): | class Db(BaseDb): | ||||
"""Proxy to the SWH DB, with wrappers around stored procedures | """Proxy to the SWH DB, with wrappers around stored procedures | ||||
▲ Show 20 Lines • Show All 592 Lines • ▼ Show 20 Lines | ): | ||||
query_parts.append("LIMIT %s") | query_parts.append("LIMIT %s") | ||||
query_params.append(limit) | query_params.append(limit) | ||||
query = "\n".join(query_parts) | query = "\n".join(query_parts) | ||||
cur.execute(query, tuple(query_params)) | cur.execute(query, tuple(query_params)) | ||||
yield from cur | yield from cur | ||||
def origin_visit_get_range( | def origin_visit_get_range( | ||||
self, origin: str, visit_from: int, order: str, limit: int, cur=None, | self, origin: str, visit_from: int, order: ListOrder, limit: int, cur=None, | ||||
): | ): | ||||
assert order in ["asc", "desc"] | |||||
cur = self._cursor(cur) | cur = self._cursor(cur) | ||||
origin_visit_cols = ["o.url as origin", "ov.visit", "ov.date", "ov.type"] | origin_visit_cols = ["o.url as origin", "ov.visit", "ov.date", "ov.type"] | ||||
query_parts = [ | query_parts = [ | ||||
f"SELECT {', '.join(origin_visit_cols)} FROM origin_visit ov ", | f"SELECT {', '.join(origin_visit_cols)} FROM origin_visit ov ", | ||||
"INNER JOIN origin o ON o.id = ov.origin ", | "INNER JOIN origin o ON o.id = ov.origin ", | ||||
] | ] | ||||
query_parts.append("WHERE o.url = %s") | query_parts.append("WHERE o.url = %s") | ||||
query_params: List[Any] = [origin] | query_params: List[Any] = [origin] | ||||
if visit_from > 0: | if visit_from > 0: | ||||
op_comparison = ">" if order == "asc" else "<" | op_comparison = ">" if order == ListOrder.ASC else "<" | ||||
query_parts.append(f"and ov.visit {op_comparison} %s") | query_parts.append(f"and ov.visit {op_comparison} %s") | ||||
query_params.append(visit_from) | query_params.append(visit_from) | ||||
if order == "asc": | if order == ListOrder.ASC: | ||||
query_parts.append("ORDER BY ov.visit ASC") | query_parts.append("ORDER BY ov.visit ASC") | ||||
elif order == "desc": | elif order == ListOrder.DESC: | ||||
query_parts.append("ORDER BY ov.visit DESC") | query_parts.append("ORDER BY ov.visit DESC") | ||||
query_parts.append("LIMIT %s") | query_parts.append("LIMIT %s") | ||||
query_params.append(limit) | query_params.append(limit) | ||||
query = "\n".join(query_parts) | query = "\n".join(query_parts) | ||||
cur.execute(query, tuple(query_params)) | cur.execute(query, tuple(query_params)) | ||||
yield from cur | yield from cur | ||||
▲ Show 20 Lines • Show All 683 Lines • Show Last 20 Lines |