Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/db.py
Show First 20 Lines • Show All 654 Lines • ▼ Show 20 Lines | def archive_ls(self, cur=None): | ||||
a tuple (server_id, server_url) for each archive server. | a tuple (server_id, server_url) for each archive server. | ||||
""" | """ | ||||
cur = self._cursor(cur) | cur = self._cursor(cur) | ||||
cur.execute("""SELECT id, url | cur.execute("""SELECT id, url | ||||
FROM archive | FROM archive | ||||
""") | """) | ||||
yield from cursor_to_bytes(cur) | yield from cursor_to_bytes(cur) | ||||
def content_archive_get(self, content=None, cur=None): | def content_archive_get(self, content_id, cur=None): | ||||
""" Get the archival status of a content in a specific server. | """ Get the archival status of a content in a specific server. | ||||
Retrieve from the database the archival status of the given content | Retrieve from the database the archival status of the given content | ||||
in the given archive server. | in the given archive server. | ||||
Args: | Args: | ||||
content: the sha1 of the content. May be None for all contents. | content_id: the sha1 of the content. | ||||
Yields: | Yields: | ||||
A tuple (content_id, copies_json). | A tuple (content_id, present_copies, ongoing_copies), where | ||||
ongoing_copies is a dict mapping copy to mtime. | |||||
""" | """ | ||||
query = """SELECT content_id, copies | query = """SELECT content_id, | ||||
array( | |||||
SELECT key | |||||
FROM jsonb_each(copies) | |||||
WHERE value->>'status' = 'present' | |||||
ORDER BY key | |||||
) AS present, | |||||
array( | |||||
SELECT key | |||||
FROM jsonb_each(copies) | |||||
WHERE value->>'status' = 'ongoing' | |||||
ORDER BY key | |||||
) AS ongoing, | |||||
array( | |||||
SELECT value->'mtime' | |||||
FROM jsonb_each(copies) | |||||
WHERE value->>'status' = 'ongoing' | |||||
ORDER BY key | |||||
) AS ongoing_mtime | |||||
FROM content_archive | FROM content_archive | ||||
WHERE content_id = %s | |||||
ORDER BY content_id | |||||
""" | """ | ||||
if content is not None: | |||||
query += "WHERE content_id='%s'" % content | |||||
else: | |||||
query += 'ORDER BY content_id' | |||||
cur = self._cursor(cur) | cur = self._cursor(cur) | ||||
cur.execute(query) | cur.execute(query, (content_id,)) | ||||
yield from cursor_to_bytes(cur) | content_id, present, ongoing, mtimes = cur.fetchone() | ||||
return (content_id, present, dict(zip(ongoing, mtimes))) | |||||
def content_archive_get_copies(self, previous_content=None, limit=1000, | def content_archive_get_copies(self, previous_content=None, limit=1000, | ||||
cur=None): | cur=None): | ||||
"""Get the list of copies for `limit` contents starting after | """Get the list of copies for `limit` contents starting after | ||||
`previous_content`. | `previous_content`. | ||||
Args: | Args: | ||||
previous_content: sha1 of the last content retrieved. May be None | previous_content: sha1 of the last content retrieved. May be None | ||||
▲ Show 20 Lines • Show All 76 Lines • Show Last 20 Lines |