Changeset View
Changeset View
Standalone View
Standalone View
swh/provenance/postgresql/archive.py
from typing import Any, Dict, Iterable, List | from typing import Any, Dict, Iterable, List | ||||
from methodtools import lru_cache | from methodtools import lru_cache | ||||
import psycopg2 | import psycopg2 | ||||
from swh.model.model import Sha1Git | from swh.model.model import Sha1Git | ||||
from swh.storage.postgresql.storage import Storage | from swh.storage.postgresql.storage import Storage | ||||
class ArchivePostgreSQL: | class ArchivePostgreSQL: | ||||
def __init__(self, conn: psycopg2.extensions.connection): | def __init__(self, conn: psycopg2.extensions.connection) -> None: | ||||
self.conn = conn | self.conn = conn | ||||
self.storage = Storage(conn, objstorage={"cls": "memory"}) | self.storage = Storage(conn, objstorage={"cls": "memory"}) | ||||
def directory_ls(self, id: Sha1Git) -> Iterable[Dict[str, Any]]: | def directory_ls(self, id: Sha1Git) -> Iterable[Dict[str, Any]]: | ||||
entries = self.directory_ls_internal(id) | entries = self._directory_ls(id) | ||||
yield from entries | yield from entries | ||||
@lru_cache(maxsize=100000) | @lru_cache(maxsize=100000) | ||||
def directory_ls_internal(self, id: Sha1Git) -> List[Dict[str, Any]]: | def _directory_ls(self, id: Sha1Git) -> List[Dict[str, Any]]: | ||||
# TODO: add file size filtering | # TODO: add file size filtering | ||||
with self.conn.cursor() as cursor: | with self.conn.cursor() as cursor: | ||||
cursor.execute( | cursor.execute( | ||||
""" | """ | ||||
WITH | WITH | ||||
dir AS (SELECT id AS dir_id, dir_entries, file_entries, rev_entries | dir AS (SELECT id AS dir_id, dir_entries, file_entries, rev_entries | ||||
FROM directory WHERE id=%s), | FROM directory WHERE id=%s), | ||||
ls_d AS (SELECT dir_id, UNNEST(dir_entries) AS entry_id FROM dir), | ls_d AS (SELECT dir_id, UNNEST(dir_entries) AS entry_id FROM dir), | ||||
▲ Show 20 Lines • Show All 82 Lines • Show Last 20 Lines |