Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/cassandra/converters.py
# Copyright (C) 2019-2020 The Software Heritage developers | # Copyright (C) 2019-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 | ||||
from copy import deepcopy | import datetime | ||||
import json | import json | ||||
import attr | |||||
from copy import deepcopy | |||||
from typing import Any, Dict, Tuple | from typing import Any, Dict, Tuple | ||||
import attr | from cassandra.cluster import ResultSet | ||||
from swh.model.model import ( | from swh.model.model import ( | ||||
RevisionType, | |||||
ObjectType, | ObjectType, | ||||
OriginVisitStatus, | |||||
Revision, | Revision, | ||||
RevisionType, | |||||
Release, | Release, | ||||
Sha1Git, | Sha1Git, | ||||
) | ) | ||||
from swh.model.hashutil import DEFAULT_ALGORITHMS | from swh.model.hashutil import DEFAULT_ALGORITHMS | ||||
from ..converters import git_headers_to_db, db_to_git_headers | from ..converters import git_headers_to_db, db_to_git_headers | ||||
from .common import Row | from .common import Row | ||||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | |||||
def row_to_content_hashes(row: Row) -> Dict[str, bytes]: | def row_to_content_hashes(row: Row) -> Dict[str, bytes]: | ||||
"""Convert cassandra row to a content hashes | """Convert cassandra row to a content hashes | ||||
""" | """ | ||||
hashes = {} | hashes = {} | ||||
for algo in DEFAULT_ALGORITHMS: | for algo in DEFAULT_ALGORITHMS: | ||||
hashes[algo] = getattr(row, algo) | hashes[algo] = getattr(row, algo) | ||||
return hashes | return hashes | ||||
def row_to_visit_status(row: ResultSet) -> OriginVisitStatus: | |||||
"""Format a row representing a visit_status to an actual dict representing an | |||||
OriginVisitStatus. | |||||
""" | |||||
return OriginVisitStatus.from_dict( | |||||
{ | |||||
**row._asdict(), | |||||
"origin": row.origin, | |||||
"date": row.date.replace(tzinfo=datetime.timezone.utc), | |||||
"metadata": (json.loads(row.metadata) if row.metadata else None), | |||||
} | |||||
) |