Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/db.py
Show First 20 Lines • Show All 287 Lines • ▼ Show 20 Lines | content_find_cols = [ | ||||
"sha256", | "sha256", | ||||
"blake2s256", | "blake2s256", | ||||
"length", | "length", | ||||
"ctime", | "ctime", | ||||
"status", | "status", | ||||
] | ] | ||||
def content_find( | def content_find( | ||||
self, sha1=None, sha1_git=None, sha256=None, blake2s256=None, cur=None | self, | ||||
sha1: Optional[bytes] = None, | |||||
sha1_git: Optional[bytes] = None, | |||||
sha256: Optional[bytes] = None, | |||||
blake2s256: Optional[bytes] = None, | |||||
cur=None, | |||||
): | ): | ||||
"""Find the content optionally on a combination of the following | """Find the content optionally on a combination of the following | ||||
checksums sha1, sha1_git, sha256 or blake2s256. | checksums sha1, sha1_git, sha256 or blake2s256. | ||||
Args: | Args: | ||||
sha1: sha1 content | sha1: sha1 content | ||||
git_sha1: the sha1 computed `a la git` sha1 of the content | git_sha1: the sha1 computed `a la git` sha1 of the content | ||||
sha256: sha256 content | sha256: sha256 content | ||||
blake2s256: blake2s256 content | blake2s256: blake2s256 content | ||||
Returns: | Returns: | ||||
The tuple (sha1, sha1_git, sha256, blake2s256) if found or None. | The tuple (sha1, sha1_git, sha256, blake2s256) if found or None. | ||||
""" | """ | ||||
cur = self._cursor(cur) | cur = self._cursor(cur) | ||||
checksum_dict = { | checksum_dict = { | ||||
"sha1": sha1, | "sha1": sha1, | ||||
"sha1_git": sha1_git, | "sha1_git": sha1_git, | ||||
"sha256": sha256, | "sha256": sha256, | ||||
"blake2s256": blake2s256, | "blake2s256": blake2s256, | ||||
} | } | ||||
query_parts = [f"SELECT {','.join(self.content_find_cols)} FROM content WHERE "] | |||||
query_params = [] | |||||
where_parts = [] | where_parts = [] | ||||
args = [] | # Adds only those keys which have values exist | ||||
# Adds only those keys which have value other than None | |||||
for algorithm in checksum_dict: | for algorithm in checksum_dict: | ||||
if checksum_dict[algorithm] is not None: | if checksum_dict[algorithm] is not None: | ||||
args.append(checksum_dict[algorithm]) | where_parts.append(f"{algorithm} = %s") | ||||
where_parts.append(algorithm + "= %s") | query_params.append(checksum_dict[algorithm]) | ||||
query = " AND ".join(where_parts) | |||||
cur.execute( | query_parts.append(" AND ".join(where_parts)) | ||||
"""SELECT %s | query = "\n".join(query_parts) | ||||
ardumont: Not necessary change but the format was off, there was no type...
So might as well align it… | |||||
FROM content WHERE %s | cur.execute(query, query_params) | ||||
""" | |||||
% (",".join(self.content_find_cols), query), | |||||
args, | |||||
) | |||||
content = cur.fetchall() | content = cur.fetchall() | ||||
return content | return content | ||||
def content_get_random(self, cur=None): | def content_get_random(self, cur=None): | ||||
return self._get_random_row_from_table("content", ["sha1_git"], "sha1_git", cur) | return self._get_random_row_from_table("content", ["sha1_git"], "sha1_git", cur) | ||||
def directory_missing_from_list(self, directories, cur=None): | def directory_missing_from_list(self, directories, cur=None): | ||||
cur = self._cursor(cur) | cur = self._cursor(cur) | ||||
▲ Show 20 Lines • Show All 1,003 Lines • Show Last 20 Lines |
Not necessary change but the format was off, there was no type...
So might as well align it with other db implementations.