Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/storage.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 copy | import copy | ||||
import datetime | import datetime | ||||
import itertools | import itertools | ||||
import json | import json | ||||
from collections import defaultdict | from collections import defaultdict | ||||
from concurrent.futures import ThreadPoolExecutor | from concurrent.futures import ThreadPoolExecutor | ||||
from contextlib import contextmanager | from contextlib import contextmanager | ||||
from typing import Any, Dict, List, Mapping, Optional | from typing import Any, Dict, List, Optional | ||||
import dateutil.parser | import dateutil.parser | ||||
import psycopg2 | import psycopg2 | ||||
import psycopg2.pool | import psycopg2.pool | ||||
from swh.core.api import remote_api_endpoint | from swh.core.api import remote_api_endpoint | ||||
from swh.model.model import SHA1_SIZE | from swh.model.model import SHA1_SIZE | ||||
from swh.model.hashutil import ALGORITHMS, hash_to_bytes, hash_to_hex | from swh.model.hashutil import ALGORITHMS, hash_to_bytes, hash_to_hex | ||||
▲ Show 20 Lines • Show All 1,582 Lines • ▼ Show 20 Lines | def origin_visit_get_latest( | ||||
require_snapshot=require_snapshot, cur=cur) | require_snapshot=require_snapshot, cur=cur) | ||||
if origin_visit: | if origin_visit: | ||||
return dict(zip(db.origin_visit_get_cols, origin_visit)) | return dict(zip(db.origin_visit_get_cols, origin_visit)) | ||||
@remote_api_endpoint('origin/visit/get_random') | @remote_api_endpoint('origin/visit/get_random') | ||||
@timed | @timed | ||||
@db_transaction() | @db_transaction() | ||||
def origin_visit_get_random( | def origin_visit_get_random( | ||||
self, type: str, db=None, cur=None) -> Mapping[str, Any]: | self, type: str, db=None, cur=None) -> Optional[Dict[str, Any]]: | ||||
"""Randomly select one successful origin visit with <type> | """Randomly select one successful origin visit with <type> | ||||
made in the last 3 months. | made in the last 3 months. | ||||
Returns: | Returns: | ||||
dict representing an origin visit, in the same format as | dict representing an origin visit, in the same format as | ||||
:py:meth:`origin_visit_get`. | :py:meth:`origin_visit_get`. | ||||
""" | """ | ||||
data: Dict[str, Any] = {} | |||||
result = db.origin_visit_get_random(type, cur) | result = db.origin_visit_get_random(type, cur) | ||||
if result: | if result: | ||||
data = dict(zip(db.origin_visit_get_cols, result)) | return dict(zip(db.origin_visit_get_cols, result)) | ||||
return data | else: | ||||
return None | |||||
@remote_api_endpoint('object/find_by_sha1_git') | @remote_api_endpoint('object/find_by_sha1_git') | ||||
@timed | @timed | ||||
@db_transaction(statement_timeout=2000) | @db_transaction(statement_timeout=2000) | ||||
def object_find_by_sha1_git(self, ids, db=None, cur=None): | def object_find_by_sha1_git(self, ids, db=None, cur=None): | ||||
"""Return the objects found with the given ids. | """Return the objects found with the given ids. | ||||
Args: | Args: | ||||
▲ Show 20 Lines • Show All 507 Lines • Show Last 20 Lines |