Changeset View
Changeset View
Standalone View
Standalone View
swh/provenance/postgresql/provenancedb_without_path.py
from datetime import datetime | from datetime import datetime | ||||
from typing import Generator, Optional, Set, Tuple | from typing import Generator, Optional, Set, Tuple | ||||
import psycopg2 | import psycopg2 | ||||
import psycopg2.extras | import psycopg2.extras | ||||
from .provenancedb_base import ProvenanceDBBase | from swh.model.model import Sha1Git | ||||
######################################################################################## | from .provenancedb_base import ProvenanceDBBase | ||||
######################################################################################## | |||||
######################################################################################## | |||||
class ProvenanceWithoutPathDB(ProvenanceDBBase): | class ProvenanceWithoutPathDB(ProvenanceDBBase): | ||||
def content_find_first( | def content_find_first( | ||||
self, blob: bytes | self, id: Sha1Git | ||||
) -> Optional[Tuple[bytes, bytes, datetime, bytes]]: | ) -> Optional[Tuple[Sha1Git, Sha1Git, datetime, bytes]]: | ||||
self.cursor.execute( | self.cursor.execute( | ||||
""" | """ | ||||
SELECT C.sha1 AS blob, | SELECT C.sha1 AS blob, | ||||
R.sha1 AS rev, | R.sha1 AS rev, | ||||
R.date AS date, | R.date AS date, | ||||
'\\x'::bytea as path | '\\x'::bytea as path | ||||
FROM content AS C | FROM content AS C | ||||
INNER JOIN content_in_revision AS CR ON (CR.content = C.id) | INNER JOIN content_in_revision AS CR ON (CR.content = C.id) | ||||
INNER JOIN revision as R ON (CR.revision = R.id) | INNER JOIN revision as R ON (CR.revision = R.id) | ||||
WHERE C.sha1=%s | WHERE C.sha1=%s | ||||
ORDER BY date, rev ASC LIMIT 1 | ORDER BY date, rev ASC LIMIT 1 | ||||
""", | """, | ||||
(blob,), | (id,), | ||||
) | ) | ||||
return self.cursor.fetchone() | return self.cursor.fetchone() | ||||
def content_find_all( | def content_find_all( | ||||
self, blob: bytes, limit: Optional[int] = None | self, id: Sha1Git, limit: Optional[int] = None | ||||
) -> Generator[Tuple[bytes, bytes, datetime, bytes], None, None]: | ) -> Generator[Tuple[Sha1Git, Sha1Git, datetime, bytes], None, None]: | ||||
early_cut = f"LIMIT {limit}" if limit is not None else "" | early_cut = f"LIMIT {limit}" if limit is not None else "" | ||||
self.cursor.execute( | self.cursor.execute( | ||||
f""" | f""" | ||||
(SELECT C.sha1 AS blob, | (SELECT C.sha1 AS blob, | ||||
R.sha1 AS rev, | R.sha1 AS rev, | ||||
R.date AS date, | R.date AS date, | ||||
'\\x'::bytea as path | '\\x'::bytea as path | ||||
FROM content AS C | FROM content AS C | ||||
INNER JOIN content_in_revision AS CR ON (CR.content = C.id) | INNER JOIN content_in_revision AS CR ON (CR.content = C.id) | ||||
INNER JOIN revision AS R ON (CR.revision = R.id) | INNER JOIN revision AS R ON (CR.revision = R.id) | ||||
WHERE C.sha1=%s) | WHERE C.sha1=%s) | ||||
UNION | UNION | ||||
(SELECT C.sha1 AS content, | (SELECT C.sha1 AS content, | ||||
R.sha1 AS revision, | R.sha1 AS revision, | ||||
R.date AS date, | R.date AS date, | ||||
'\\x'::bytea as path | '\\x'::bytea as path | ||||
FROM content AS C | FROM content AS C | ||||
INNER JOIN content_in_directory AS CD ON (C.id = CD.content) | INNER JOIN content_in_directory AS CD ON (C.id = CD.content) | ||||
INNER JOIN directory_in_revision AS DR ON (CD.directory = DR.directory) | INNER JOIN directory_in_revision AS DR ON (CD.directory = DR.directory) | ||||
INNER JOIN revision AS R ON (DR.revision = R.id) | INNER JOIN revision AS R ON (DR.revision = R.id) | ||||
WHERE C.sha1=%s) | WHERE C.sha1=%s) | ||||
ORDER BY date, rev, path {early_cut} | ORDER BY date, rev, path {early_cut} | ||||
""", | """, | ||||
(blob, blob), | (id, id), | ||||
) | ) | ||||
yield from self.cursor.fetchall() | yield from self.cursor.fetchall() | ||||
def insert_relation(self, relation: str, data: Set[Tuple[bytes, bytes, bytes]]): | def insert_relation(self, relation: str, data: Set[Tuple[Sha1Git, Sha1Git, bytes]]): | ||||
if data: | if data: | ||||
assert relation in ( | assert relation in ( | ||||
"content_in_revision", | "content_in_revision", | ||||
"content_in_directory", | "content_in_directory", | ||||
"directory_in_revision", | "directory_in_revision", | ||||
) | ) | ||||
src, dst = relation.split("_in_") | src, dst = relation.split("_in_") | ||||
Show All 13 Lines |