diff --git a/swh/indexer/storage/__init__.py b/swh/indexer/storage/__init__.py index ce83daa..4d7c3ad 100644 --- a/swh/indexer/storage/__init__.py +++ b/swh/indexer/storage/__init__.py @@ -1,647 +1,648 @@ # Copyright (C) 2015-2020 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information from collections import Counter, defaultdict +import itertools import json from typing import Dict, List, Optional import psycopg2 import psycopg2.pool from swh.core.db.common import db_transaction, db_transaction_generator from swh.model.hashutil import hash_to_bytes, hash_to_hex from swh.model.model import SHA1_SIZE from swh.storage.exc import StorageDBError from swh.storage.utils import get_partition_bounds_bytes from . import converters from .db import Db from .exc import DuplicateId, IndexerStorageArgumentException from .interface import PagedResult, Sha1 from .metrics import process_metrics, send_metric, timed +from .model import ( + ContentCtagsRow, + ContentLanguageRow, + ContentLicenseRow, + ContentMetadataRow, + ContentMimetypeRow, + OriginIntrinsicMetadataRow, + RevisionIntrinsicMetadataRow, +) INDEXER_CFG_KEY = "indexer_storage" MAPPING_NAMES = ["codemeta", "gemspec", "maven", "npm", "pkg-info"] def get_indexer_storage(cls, args): """Get an indexer storage object of class `storage_class` with arguments `storage_args`. Args: cls (str): storage's class, either 'local' or 'remote' args (dict): dictionary of arguments passed to the storage class constructor Returns: an instance of swh.indexer's storage (either local or remote) Raises: ValueError if passed an unknown storage class. """ if cls == "remote": from .api.client import RemoteStorage as IndexerStorage elif cls == "local": from . import IndexerStorage elif cls == "memory": from .in_memory import IndexerStorage else: raise ValueError("Unknown indexer storage class `%s`" % cls) return IndexerStorage(**args) def check_id_duplicates(data): """ - If any two dictionaries in `data` have the same id, raises + If any two row models in `data` have the same unique key, raises a `ValueError`. Values associated to the key must be hashable. Args: data (List[dict]): List of dictionaries to be inserted >>> check_id_duplicates([ - ... {'id': 'foo', 'data': 'spam'}, - ... {'id': 'bar', 'data': 'egg'}, + ... ContentLanguageRow(id=b'foo', indexer_configuration_id=42, lang="python"), + ... ContentLanguageRow(id=b'foo', indexer_configuration_id=32, lang="python"), ... ]) >>> check_id_duplicates([ - ... {'id': 'foo', 'data': 'spam'}, - ... {'id': 'foo', 'data': 'egg'}, + ... ContentLanguageRow(id=b'foo', indexer_configuration_id=42, lang="python"), + ... ContentLanguageRow(id=b'foo', indexer_configuration_id=42, lang="python"), ... ]) Traceback (most recent call last): ... - swh.indexer.storage.exc.DuplicateId: ['foo'] - """ - counter = Counter(item["id"] for item in data) + swh.indexer.storage.exc.DuplicateId: [{'id': b'foo', 'indexer_configuration_id': 42}] + """ # noqa + counter = Counter(tuple(sorted(item.unique_key().items())) for item in data) duplicates = [id_ for (id_, count) in counter.items() if count >= 2] if duplicates: - raise DuplicateId(duplicates) + raise DuplicateId(list(map(dict, duplicates))) class IndexerStorage: """SWH Indexer Storage """ def __init__(self, db, min_pool_conns=1, max_pool_conns=10): """ Args: db_conn: either a libpq connection string, or a psycopg2 connection """ try: if isinstance(db, psycopg2.extensions.connection): self._pool = None self._db = Db(db) else: self._pool = psycopg2.pool.ThreadedConnectionPool( min_pool_conns, max_pool_conns, db ) self._db = None except psycopg2.OperationalError as e: raise StorageDBError(e) def get_db(self): if self._db: return self._db return Db.from_pool(self._pool) def put_db(self, db): if db is not self._db: db.put_conn() @timed @db_transaction() def check_config(self, *, check_write, db=None, cur=None): # Check permissions on one of the tables if check_write: check = "INSERT" else: check = "SELECT" cur.execute( "select has_table_privilege(current_user, 'content_mimetype', %s)", # noqa (check,), ) return cur.fetchone()[0] @timed @db_transaction_generator() def content_mimetype_missing(self, mimetypes, db=None, cur=None): for obj in db.content_mimetype_missing_from_list(mimetypes, cur): yield obj[0] @timed @db_transaction() def get_partition( self, indexer_type: str, indexer_configuration_id: int, partition_id: int, nb_partitions: int, page_token: Optional[str] = None, limit: int = 1000, with_textual_data=False, db=None, cur=None, ) -> PagedResult[Sha1]: """Retrieve ids of content with `indexer_type` within within partition partition_id bound by limit. Args: **indexer_type**: Type of data content to index (mimetype, language, etc...) **indexer_configuration_id**: The tool used to index data **partition_id**: index of the partition to fetch **nb_partitions**: total number of partitions to split into **page_token**: opaque token used for pagination **limit**: Limit result (default to 1000) **with_textual_data** (bool): Deal with only textual content (True) or all content (all contents by defaults, False) Raises: IndexerStorageArgumentException for; - limit to None - wrong indexer_type provided Returns: PagedResult of Sha1. If next_page_token is None, there is no more data to fetch """ if limit is None: raise IndexerStorageArgumentException("limit should not be None") if indexer_type not in db.content_indexer_names: err = f"Wrong type. Should be one of [{','.join(db.content_indexer_names)}]" raise IndexerStorageArgumentException(err) start, end = get_partition_bounds_bytes(partition_id, nb_partitions, SHA1_SIZE) if page_token is not None: start = hash_to_bytes(page_token) if end is None: end = b"\xff" * SHA1_SIZE next_page_token: Optional[str] = None ids = [ row[0] for row in db.content_get_range( indexer_type, start, end, indexer_configuration_id, limit=limit + 1, with_textual_data=with_textual_data, cur=cur, ) ] if len(ids) >= limit: next_page_token = hash_to_hex(ids[-1]) ids = ids[:limit] assert len(ids) <= limit return PagedResult(results=ids, next_page_token=next_page_token) @timed @db_transaction() def content_mimetype_get_partition( self, indexer_configuration_id: int, partition_id: int, nb_partitions: int, page_token: Optional[str] = None, limit: int = 1000, db=None, cur=None, ) -> PagedResult[Sha1]: return self.get_partition( "mimetype", indexer_configuration_id, partition_id, nb_partitions, page_token=page_token, limit=limit, db=db, cur=cur, ) @timed @process_metrics @db_transaction() def content_mimetype_add( self, mimetypes: List[Dict], conflict_update: bool = False, db=None, cur=None ) -> Dict[str, int]: """Add mimetypes to the storage (if conflict_update is True, this will override existing data if any). Returns: A dict with the number of new elements added to the storage. """ - check_id_duplicates(mimetypes) + check_id_duplicates(map(ContentMimetypeRow.from_dict, mimetypes)) mimetypes.sort(key=lambda m: m["id"]) db.mktemp_content_mimetype(cur) db.copy_to( mimetypes, "tmp_content_mimetype", ["id", "mimetype", "encoding", "indexer_configuration_id"], cur, ) count = db.content_mimetype_add_from_temp(conflict_update, cur) return {"content_mimetype:add": count} @timed @db_transaction_generator() def content_mimetype_get(self, ids, db=None, cur=None): for c in db.content_mimetype_get_from_list(ids, cur): yield converters.db_to_mimetype(dict(zip(db.content_mimetype_cols, c))) @timed @db_transaction_generator() def content_language_missing(self, languages, db=None, cur=None): for obj in db.content_language_missing_from_list(languages, cur): yield obj[0] @timed @db_transaction_generator() def content_language_get(self, ids, db=None, cur=None): for c in db.content_language_get_from_list(ids, cur): yield converters.db_to_language(dict(zip(db.content_language_cols, c))) @timed @process_metrics @db_transaction() def content_language_add( self, languages: List[Dict], conflict_update: bool = False, db=None, cur=None ) -> Dict[str, int]: - check_id_duplicates(languages) + check_id_duplicates(map(ContentLanguageRow.from_dict, languages)) languages.sort(key=lambda m: m["id"]) db.mktemp_content_language(cur) # empty language is mapped to 'unknown' db.copy_to( ( { "id": lang["id"], "lang": "unknown" if not lang["lang"] else lang["lang"], "indexer_configuration_id": lang["indexer_configuration_id"], } for lang in languages ), "tmp_content_language", ["id", "lang", "indexer_configuration_id"], cur, ) count = db.content_language_add_from_temp(conflict_update, cur) return {"content_language:add": count} @timed @db_transaction_generator() def content_ctags_missing(self, ctags, db=None, cur=None): for obj in db.content_ctags_missing_from_list(ctags, cur): yield obj[0] @timed @db_transaction_generator() def content_ctags_get(self, ids, db=None, cur=None): for c in db.content_ctags_get_from_list(ids, cur): yield converters.db_to_ctags(dict(zip(db.content_ctags_cols, c))) @timed @process_metrics @db_transaction() def content_ctags_add( self, ctags: List[Dict], conflict_update: bool = False, db=None, cur=None ) -> Dict[str, int]: - check_id_duplicates(ctags) + rows = list(itertools.chain.from_iterable(map(converters.ctags_to_db, ctags))) + check_id_duplicates(map(ContentCtagsRow.from_dict, rows)) ctags.sort(key=lambda m: m["id"]) - def _convert_ctags(__ctags): - """Convert ctags dict to list of ctags. - - """ - for ctags in __ctags: - yield from converters.ctags_to_db(ctags) - db.mktemp_content_ctags(cur) db.copy_to( - list(_convert_ctags(ctags)), + rows, tblname="tmp_content_ctags", columns=["id", "name", "kind", "line", "lang", "indexer_configuration_id"], cur=cur, ) count = db.content_ctags_add_from_temp(conflict_update, cur) return {"content_ctags:add": count} @timed @db_transaction_generator() def content_ctags_search( self, expression, limit=10, last_sha1=None, db=None, cur=None ): for obj in db.content_ctags_search(expression, last_sha1, limit, cur=cur): yield converters.db_to_ctags(dict(zip(db.content_ctags_cols, obj))) @timed @db_transaction_generator() def content_fossology_license_get(self, ids, db=None, cur=None): d = defaultdict(list) for c in db.content_fossology_license_get_from_list(ids, cur): license = dict(zip(db.content_fossology_license_cols, c)) id_ = license["id"] d[id_].append(converters.db_to_fossology_license(license)) for id_, facts in d.items(): yield {id_: facts} @timed @process_metrics @db_transaction() def content_fossology_license_add( self, licenses: List[Dict], conflict_update: bool = False, db=None, cur=None ) -> Dict[str, int]: - check_id_duplicates(licenses) + rows = list( + itertools.chain.from_iterable( + map(converters.fossology_license_to_db, licenses) + ) + ) + check_id_duplicates(map(ContentLicenseRow.from_dict, rows)) licenses.sort(key=lambda m: m["id"]) db.mktemp_content_fossology_license(cur) db.copy_to( - ( - { - "id": sha1["id"], - "indexer_configuration_id": sha1["indexer_configuration_id"], - "license": license, - } - for sha1 in licenses - for license in sha1["licenses"] - ), + rows, tblname="tmp_content_fossology_license", columns=["id", "license", "indexer_configuration_id"], cur=cur, ) count = db.content_fossology_license_add_from_temp(conflict_update, cur) return {"content_fossology_license:add": count} @timed @db_transaction() def content_fossology_license_get_partition( self, indexer_configuration_id: int, partition_id: int, nb_partitions: int, page_token: Optional[str] = None, limit: int = 1000, db=None, cur=None, ) -> PagedResult[Sha1]: return self.get_partition( "fossology_license", indexer_configuration_id, partition_id, nb_partitions, page_token=page_token, limit=limit, with_textual_data=True, db=db, cur=cur, ) @timed @db_transaction_generator() def content_metadata_missing(self, metadata, db=None, cur=None): for obj in db.content_metadata_missing_from_list(metadata, cur): yield obj[0] @timed @db_transaction_generator() def content_metadata_get(self, ids, db=None, cur=None): for c in db.content_metadata_get_from_list(ids, cur): yield converters.db_to_metadata(dict(zip(db.content_metadata_cols, c))) @timed @process_metrics @db_transaction() def content_metadata_add( self, metadata: List[Dict], conflict_update: bool = False, db=None, cur=None ) -> Dict[str, int]: - check_id_duplicates(metadata) + check_id_duplicates(map(ContentMetadataRow.from_dict, metadata)) metadata.sort(key=lambda m: m["id"]) db.mktemp_content_metadata(cur) db.copy_to( metadata, "tmp_content_metadata", ["id", "metadata", "indexer_configuration_id"], cur, ) count = db.content_metadata_add_from_temp(conflict_update, cur) return { "content_metadata:add": count, } @timed @db_transaction_generator() def revision_intrinsic_metadata_missing(self, metadata, db=None, cur=None): for obj in db.revision_intrinsic_metadata_missing_from_list(metadata, cur): yield obj[0] @timed @db_transaction_generator() def revision_intrinsic_metadata_get(self, ids, db=None, cur=None): for c in db.revision_intrinsic_metadata_get_from_list(ids, cur): yield converters.db_to_metadata( dict(zip(db.revision_intrinsic_metadata_cols, c)) ) @timed @process_metrics @db_transaction() def revision_intrinsic_metadata_add( self, metadata: List[Dict], conflict_update: bool = False, db=None, cur=None ) -> Dict[str, int]: - check_id_duplicates(metadata) + check_id_duplicates(map(RevisionIntrinsicMetadataRow.from_dict, metadata)) metadata.sort(key=lambda m: m["id"]) db.mktemp_revision_intrinsic_metadata(cur) db.copy_to( metadata, "tmp_revision_intrinsic_metadata", ["id", "metadata", "mappings", "indexer_configuration_id"], cur, ) count = db.revision_intrinsic_metadata_add_from_temp(conflict_update, cur) return { "revision_intrinsic_metadata:add": count, } @timed @process_metrics @db_transaction() def revision_intrinsic_metadata_delete( self, entries: List[Dict], db=None, cur=None ) -> Dict: count = db.revision_intrinsic_metadata_delete(entries, cur) return {"revision_intrinsic_metadata:del": count} @timed @db_transaction_generator() def origin_intrinsic_metadata_get(self, ids, db=None, cur=None): for c in db.origin_intrinsic_metadata_get_from_list(ids, cur): yield converters.db_to_metadata( dict(zip(db.origin_intrinsic_metadata_cols, c)) ) @timed @process_metrics @db_transaction() def origin_intrinsic_metadata_add( self, metadata: List[Dict], conflict_update: bool = False, db=None, cur=None ) -> Dict[str, int]: - check_id_duplicates(metadata) + check_id_duplicates(map(OriginIntrinsicMetadataRow.from_dict, metadata)) metadata.sort(key=lambda m: m["id"]) db.mktemp_origin_intrinsic_metadata(cur) db.copy_to( metadata, "tmp_origin_intrinsic_metadata", ["id", "metadata", "indexer_configuration_id", "from_revision", "mappings"], cur, ) count = db.origin_intrinsic_metadata_add_from_temp(conflict_update, cur) return { "origin_intrinsic_metadata:add": count, } @timed @process_metrics @db_transaction() def origin_intrinsic_metadata_delete( self, entries: List[Dict], db=None, cur=None ) -> Dict: count = db.origin_intrinsic_metadata_delete(entries, cur) return { "origin_intrinsic_metadata:del": count, } @timed @db_transaction_generator() def origin_intrinsic_metadata_search_fulltext( self, conjunction, limit=100, db=None, cur=None ): for c in db.origin_intrinsic_metadata_search_fulltext( conjunction, limit=limit, cur=cur ): yield converters.db_to_metadata( dict(zip(db.origin_intrinsic_metadata_cols, c)) ) @timed @db_transaction() def origin_intrinsic_metadata_search_by_producer( self, page_token="", limit=100, ids_only=False, mappings=None, tool_ids=None, db=None, cur=None, ): assert isinstance(page_token, str) # we go to limit+1 to check whether we should add next_page_token in # the response res = db.origin_intrinsic_metadata_search_by_producer( page_token, limit + 1, ids_only, mappings, tool_ids, cur ) result = {} if ids_only: result["origins"] = [origin for (origin,) in res] if len(result["origins"]) > limit: result["origins"][limit:] = [] result["next_page_token"] = result["origins"][-1] else: result["origins"] = [ converters.db_to_metadata( dict(zip(db.origin_intrinsic_metadata_cols, c)) ) for c in res ] if len(result["origins"]) > limit: result["origins"][limit:] = [] result["next_page_token"] = result["origins"][-1]["id"] return result @timed @db_transaction() def origin_intrinsic_metadata_stats(self, db=None, cur=None): mapping_names = [m for m in MAPPING_NAMES] select_parts = [] # Count rows for each mapping for mapping_name in mapping_names: select_parts.append( ( "sum(case when (mappings @> ARRAY['%s']) " " then 1 else 0 end)" ) % mapping_name ) # Total select_parts.append("sum(1)") # Rows whose metadata has at least one key that is not '@context' select_parts.append( "sum(case when ('{}'::jsonb @> (metadata - '@context')) " " then 0 else 1 end)" ) cur.execute( "select " + ", ".join(select_parts) + " from origin_intrinsic_metadata" ) results = dict(zip(mapping_names + ["total", "non_empty"], cur.fetchone())) return { "total": results.pop("total"), "non_empty": results.pop("non_empty"), "per_mapping": results, } @timed @db_transaction_generator() def indexer_configuration_add(self, tools, db=None, cur=None): db.mktemp_indexer_configuration(cur) db.copy_to( tools, "tmp_indexer_configuration", ["tool_name", "tool_version", "tool_configuration"], cur, ) tools = db.indexer_configuration_add_from_temp(cur) count = 0 for line in tools: yield dict(zip(db.indexer_configuration_cols, line)) count += 1 send_metric( "indexer_configuration:add", count, method_name="indexer_configuration_add" ) @timed @db_transaction() def indexer_configuration_get(self, tool, db=None, cur=None): tool_conf = tool["tool_configuration"] if isinstance(tool_conf, dict): tool_conf = json.dumps(tool_conf) idx = db.indexer_configuration_get( tool["tool_name"], tool["tool_version"], tool_conf ) if not idx: return None return dict(zip(db.indexer_configuration_cols, idx)) diff --git a/swh/indexer/storage/converters.py b/swh/indexer/storage/converters.py index 7cee1c9..354db52 100644 --- a/swh/indexer/storage/converters.py +++ b/swh/indexer/storage/converters.py @@ -1,140 +1,152 @@ # Copyright (C) 2015-2017 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information def ctags_to_db(ctags): """Convert a ctags entry into a ready ctags entry. Args: ctags (dict): ctags entry with the following keys: - id (bytes): content's identifier - tool_id (int): tool id used to compute ctags - ctags ([dict]): List of dictionary with the following keys: - name (str): symbol's name - kind (str): symbol's kind - line (int): symbol's line in the content - language (str): language Returns: list: list of ctags entries as dicts with the following keys: - id (bytes): content's identifier - name (str): symbol's name - kind (str): symbol's kind - language (str): language for that content - tool_id (int): tool id used to compute ctags """ id = ctags["id"] tool_id = ctags["indexer_configuration_id"] for ctag in ctags["ctags"]: yield { "id": id, "name": ctag["name"], "kind": ctag["kind"], "line": ctag["line"], "lang": ctag["lang"], "indexer_configuration_id": tool_id, } +def fossology_license_to_db(licenses): + """Similar to ctags_to_db, but for licenses.""" + id = licenses["id"] + tool_id = licenses["indexer_configuration_id"] + for license in licenses["licenses"]: + yield { + "id": id, + "indexer_configuration_id": tool_id, + "license": license, + } + + def db_to_ctags(ctag): """Convert a ctags entry into a ready ctags entry. Args: ctags (dict): ctags entry with the following keys: - id (bytes): content's identifier - ctags ([dict]): List of dictionary with the following keys: - name (str): symbol's name - kind (str): symbol's kind - line (int): symbol's line in the content - language (str): language Returns: list: list of ctags ready entry (dict with the following keys): - id (bytes): content's identifier - name (str): symbol's name - kind (str): symbol's kind - language (str): language for that content - tool (dict): tool used to compute the ctags """ return { "id": ctag["id"], "name": ctag["name"], "kind": ctag["kind"], "line": ctag["line"], "lang": ctag["lang"], "tool": { "id": ctag["tool_id"], "name": ctag["tool_name"], "version": ctag["tool_version"], "configuration": ctag["tool_configuration"], }, } def db_to_mimetype(mimetype): """Convert a ctags entry into a ready ctags output. """ return { "id": mimetype["id"], "encoding": mimetype["encoding"], "mimetype": mimetype["mimetype"], "tool": { "id": mimetype["tool_id"], "name": mimetype["tool_name"], "version": mimetype["tool_version"], "configuration": mimetype["tool_configuration"], }, } def db_to_language(language): """Convert a language entry into a ready language output. """ return { "id": language["id"], "lang": language["lang"], "tool": { "id": language["tool_id"], "name": language["tool_name"], "version": language["tool_version"], "configuration": language["tool_configuration"], }, } def db_to_metadata(metadata): """Convert a metadata entry into a ready metadata output. """ metadata["tool"] = { "id": metadata["tool_id"], "name": metadata["tool_name"], "version": metadata["tool_version"], "configuration": metadata["tool_configuration"], } del metadata["tool_id"], metadata["tool_configuration"] del metadata["tool_version"], metadata["tool_name"] return metadata def db_to_fossology_license(license): return { "licenses": license["licenses"], "tool": { "id": license["tool_id"], "name": license["tool_name"], "version": license["tool_version"], "configuration": license["tool_configuration"], }, } diff --git a/swh/indexer/storage/in_memory.py b/swh/indexer/storage/in_memory.py index 75c7fe4..b839215 100644 --- a/swh/indexer/storage/in_memory.py +++ b/swh/indexer/storage/in_memory.py @@ -1,545 +1,540 @@ # Copyright (C) 2018-2020 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information from collections import Counter, defaultdict import itertools import json import math import operator import re from typing import ( Any, Dict, Generic, Iterable, Iterator, List, Optional, Set, Tuple, Type, TypeVar, ) from swh.core.collections import SortedList from swh.model.hashutil import hash_to_bytes, hash_to_hex -from swh.model.model import SHA1_SIZE +from swh.model.model import SHA1_SIZE, Sha1Git from swh.storage.utils import get_partition_bounds_bytes -from . import MAPPING_NAMES, check_id_duplicates +from . import MAPPING_NAMES, check_id_duplicates, converters from .exc import IndexerStorageArgumentException from .interface import PagedResult, Sha1 from .model import ( BaseRow, ContentCtagsRow, ContentLanguageRow, - ContentLicensesRow, + ContentLicenseRow, ContentMetadataRow, ContentMimetypeRow, - KeyDict, OriginIntrinsicMetadataRow, RevisionIntrinsicMetadataRow, ) SHA1_DIGEST_SIZE = 160 def _transform_tool(tool): return { "id": tool["id"], "name": tool["tool_name"], "version": tool["tool_version"], "configuration": tool["tool_configuration"], } def check_id_types(data: List[Dict[str, Any]]): """Checks all elements of the list have an 'id' whose type is 'bytes'.""" if not all(isinstance(item.get("id"), bytes) for item in data): raise IndexerStorageArgumentException("identifiers must be bytes.") +def _key_from_dict(d): + return tuple(sorted(d.items())) + + ToolId = int TValue = TypeVar("TValue", bound=BaseRow) class SubStorage(Generic[TValue]): """Implements common missing/get/add logic for each indexer type.""" - _data: Dict[Tuple[Sha1, ToolId], Dict[str, Any]] + _data: Dict[Sha1, Dict[Tuple, Dict[str, Any]]] _tools_per_id: Dict[Sha1, Set[ToolId]] def __init__(self, row_class: Type[TValue], tools): self.row_class = row_class self._tools = tools self._sorted_ids = SortedList[bytes, Sha1]() - self._data = {} + self._data = defaultdict(dict) self._tools_per_id = defaultdict(set) - def missing(self, keys: Iterable[KeyDict]) -> Iterator[Sha1]: + def _key_from_dict(self, d) -> Tuple: + """Like the global _key_from_dict, but filters out dict keys that don't + belong in the unique key.""" + return _key_from_dict({k: d[k] for k in self.row_class.UNIQUE_KEY_FIELDS}) + + def missing(self, keys: Iterable[Dict]) -> Iterator[Sha1]: """List data missing from storage. Args: data (iterable): dictionaries with keys: - **id** (bytes): sha1 identifier - **indexer_configuration_id** (int): tool used to compute the results Yields: missing sha1s """ for key in keys: tool_id = key["indexer_configuration_id"] id_ = key["id"] if tool_id not in self._tools_per_id.get(id_, set()): yield id_ def get(self, ids: Iterable[Sha1]) -> Iterator[TValue]: """Retrieve data per id. Args: ids (iterable): sha1 checksums Yields: dict: dictionaries with the following keys: - **id** (bytes) - **tool** (dict): tool used to compute metadata - arbitrary data (as provided to `add`) """ for id_ in ids: - for tool_id in self._tools_per_id.get(id_, set()): - key = (id_, tool_id) + for entry in self._data[id_].values(): + entry = entry.copy() + tool_id = entry.pop("indexer_configuration_id") yield self.row_class( - id=id_, - tool=_transform_tool(self._tools[tool_id]), - **self._data[key], + id=id_, tool=_transform_tool(self._tools[tool_id]), **entry, ) def get_all(self) -> Iterator[TValue]: yield from self.get(self._sorted_ids) def get_partition( self, indexer_configuration_id: int, partition_id: int, nb_partitions: int, page_token: Optional[str] = None, limit: int = 1000, ) -> PagedResult[Sha1]: """Retrieve ids of content with `indexer_type` within partition partition_id bound by limit. Args: **indexer_type**: Type of data content to index (mimetype, language, etc...) **indexer_configuration_id**: The tool used to index data **partition_id**: index of the partition to fetch **nb_partitions**: total number of partitions to split into **page_token**: opaque token used for pagination **limit**: Limit result (default to 1000) **with_textual_data** (bool): Deal with only textual content (True) or all content (all contents by defaults, False) Raises: IndexerStorageArgumentException for; - limit to None - wrong indexer_type provided Returns: PagedResult of Sha1. If next_page_token is None, there is no more data to fetch """ if limit is None: raise IndexerStorageArgumentException("limit should not be None") (start, end) = get_partition_bounds_bytes( partition_id, nb_partitions, SHA1_SIZE ) if page_token: start = hash_to_bytes(page_token) if end is None: end = b"\xff" * SHA1_SIZE next_page_token: Optional[str] = None ids: List[Sha1] = [] sha1s = (sha1 for sha1 in self._sorted_ids.iter_from(start)) for counter, sha1 in enumerate(sha1s): if sha1 > end: break if counter >= limit: next_page_token = hash_to_hex(sha1) break ids.append(sha1) assert len(ids) <= limit return PagedResult(results=ids, next_page_token=next_page_token) def add(self, data: Iterable[TValue], conflict_update: bool) -> int: """Add data not present in storage. Args: data (iterable): dictionaries with keys: - **id**: sha1 - **indexer_configuration_id**: tool used to compute the results - arbitrary data conflict_update (bool): Flag to determine if we want to overwrite (true) or skip duplicates (false) """ data = list(data) - check_id_duplicates(obj.to_dict() for obj in data) + check_id_duplicates(data) count = 0 for obj in data: item = obj.to_dict() - tool_id = item.pop("indexer_configuration_id") id_ = item.pop("id") - data_item = item - if not conflict_update and tool_id in self._tools_per_id.get(id_, set()): + tool_id = item["indexer_configuration_id"] + key = _key_from_dict(obj.unique_key()) + if not conflict_update and key in self._data[id_]: # Duplicate, should not be updated continue - key = (id_, tool_id) - self._data[key] = data_item + self._data[id_][key] = item self._tools_per_id[id_].add(tool_id) count += 1 if id_ not in self._sorted_ids: self._sorted_ids.add(id_) return count - def add_merge( - self, new_data: Iterable[TValue], conflict_update: bool, merged_key: str - ) -> int: - added = 0 - all_subitems: List - for new_obj in new_data: - new_item = new_obj.to_dict() - id_ = new_item["id"] - tool_id = new_item["indexer_configuration_id"] - if conflict_update: - all_subitems = [] - else: - existing = list(self.get([id_])) - all_subitems = [ - old_subitem - for existing_item in existing - if existing_item.tool["id"] == tool_id # type: ignore - for old_subitem in getattr(existing_item, merged_key) - ] - for new_subitem in new_item[merged_key]: - if new_subitem not in all_subitems: - all_subitems.append(new_subitem) - added += self.add( - [ - self.row_class( - id=id_, - indexer_configuration_id=tool_id, - **{merged_key: all_subitems}, # type: ignore - ) # FIXME: this only works for classes with three attributes - ], - conflict_update=True, - ) - if id_ not in self._sorted_ids: - self._sorted_ids.add(id_) - return added - - def delete(self, entries: List[KeyDict]) -> int: + def delete(self, entries: List[Dict]) -> int: """Delete entries and return the number of entries deleted. """ deleted = 0 for entry in entries: (id_, tool_id) = (entry["id"], entry["indexer_configuration_id"]) - key = (id_, tool_id) if tool_id in self._tools_per_id[id_]: self._tools_per_id[id_].remove(tool_id) - if key in self._data: - deleted += 1 - del self._data[key] + if id_ in self._data: + key = self._key_from_dict(entry) + if key in self._data[id_]: + deleted += 1 + del self._data[id_][key] return deleted class IndexerStorage: """In-memory SWH indexer storage.""" def __init__(self): self._tools = {} self._mimetypes = SubStorage(ContentMimetypeRow, self._tools) self._languages = SubStorage(ContentLanguageRow, self._tools) self._content_ctags = SubStorage(ContentCtagsRow, self._tools) - self._licenses = SubStorage(ContentLicensesRow, self._tools) + self._licenses = SubStorage(ContentLicenseRow, self._tools) self._content_metadata = SubStorage(ContentMetadataRow, self._tools) self._revision_intrinsic_metadata = SubStorage( RevisionIntrinsicMetadataRow, self._tools ) self._origin_intrinsic_metadata = SubStorage( OriginIntrinsicMetadataRow, self._tools ) def check_config(self, *, check_write): return True def content_mimetype_missing(self, mimetypes): yield from self._mimetypes.missing(mimetypes) def content_mimetype_get_partition( self, indexer_configuration_id: int, partition_id: int, nb_partitions: int, page_token: Optional[str] = None, limit: int = 1000, ) -> PagedResult[Sha1]: return self._mimetypes.get_partition( indexer_configuration_id, partition_id, nb_partitions, page_token, limit ) def content_mimetype_add( self, mimetypes: List[Dict], conflict_update: bool = False ) -> Dict[str, int]: check_id_types(mimetypes) added = self._mimetypes.add( map(ContentMimetypeRow.from_dict, mimetypes), conflict_update ) return {"content_mimetype:add": added} def content_mimetype_get(self, ids): yield from (obj.to_dict() for obj in self._mimetypes.get(ids)) def content_language_missing(self, languages): yield from self._languages.missing(languages) def content_language_get(self, ids): yield from (obj.to_dict() for obj in self._languages.get(ids)) def content_language_add( self, languages: List[Dict], conflict_update: bool = False ) -> Dict[str, int]: check_id_types(languages) added = self._languages.add( map(ContentLanguageRow.from_dict, languages), conflict_update ) return {"content_language:add": added} def content_ctags_missing(self, ctags): yield from self._content_ctags.missing(ctags) def content_ctags_get(self, ids): for item in self._content_ctags.get(ids): - for item_ctags_item in item.ctags: - yield {"id": item.id, "tool": item.tool, **item_ctags_item.to_dict()} + yield {"id": item.id, "tool": item.tool, **item.to_dict()} def content_ctags_add( self, ctags: List[Dict], conflict_update: bool = False ) -> Dict[str, int]: check_id_types(ctags) - added = self._content_ctags.add_merge( - map(ContentCtagsRow.from_dict, ctags), conflict_update, "ctags" + added = self._content_ctags.add( + map( + ContentCtagsRow.from_dict, + itertools.chain.from_iterable(map(converters.ctags_to_db, ctags)), + ), + conflict_update, ) return {"content_ctags:add": added} def content_ctags_search(self, expression, limit=10, last_sha1=None): nb_matches = 0 + items_per_id: Dict[Tuple[Sha1Git, ToolId], List[ContentCtagsRow]] = {} for item in sorted(self._content_ctags.get_all()): if item.id <= (last_sha1 or bytes(0 for _ in range(SHA1_DIGEST_SIZE))): continue - for ctags_item in item.ctags: - if ctags_item.name != expression: + items_per_id.setdefault( + (item.id, item.indexer_configuration_id), [] + ).append(item) + + for items in items_per_id.values(): + ctags = [] + for item in items: + if item.name != expression: continue nb_matches += 1 - yield { - "id": item.id, - "tool": item.tool, - **ctags_item.to_dict(), - } - if nb_matches >= limit: - return + if nb_matches > limit: + break + item_dict = item.to_dict() + id_ = item_dict.pop("id") + tool = item_dict.pop("tool") + ctags.append(item_dict) + + if ctags: + for ctag in ctags: + yield {"id": id_, "tool": tool, **ctag} def content_fossology_license_get(self, ids): # Rewrites the output of SubStorage.get from the old format to # the new one. SubStorage.get should be updated once all other # *_get methods use the new format. # See: https://forge.softwareheritage.org/T1433 - res = {} - for obj in self._licenses.get(ids): - d = obj.to_dict() - res.setdefault(d.pop("id"), []).append(d) - for (id_, facts) in res.items(): - yield {id_: facts} + for id_ in ids: + items = {} + for obj in self._licenses.get([id_]): + items.setdefault(obj.tool["id"], (obj.tool, []))[1].append(obj.license) + if items: + yield { + id_: [ + {"tool": tool, "licenses": licenses} + for (tool, licenses) in items.values() + ] + } def content_fossology_license_add( self, licenses: List[Dict], conflict_update: bool = False ) -> Dict[str, int]: check_id_types(licenses) - added = self._licenses.add_merge( - map(ContentLicensesRow.from_dict, licenses), conflict_update, "licenses" + added = self._licenses.add( + map( + ContentLicenseRow.from_dict, + itertools.chain.from_iterable( + map(converters.fossology_license_to_db, licenses) + ), + ), + conflict_update, ) - return {"fossology_license_add:add": added} + return {"content_fossology_license:add": added} def content_fossology_license_get_partition( self, indexer_configuration_id: int, partition_id: int, nb_partitions: int, page_token: Optional[str] = None, limit: int = 1000, ) -> PagedResult[Sha1]: return self._licenses.get_partition( indexer_configuration_id, partition_id, nb_partitions, page_token, limit ) def content_metadata_missing(self, metadata): yield from self._content_metadata.missing(metadata) def content_metadata_get(self, ids): yield from (obj.to_dict() for obj in self._content_metadata.get(ids)) def content_metadata_add( self, metadata: List[Dict], conflict_update: bool = False ) -> Dict[str, int]: check_id_types(metadata) added = self._content_metadata.add( map(ContentMetadataRow.from_dict, metadata), conflict_update ) return {"content_metadata:add": added} def revision_intrinsic_metadata_missing(self, metadata): yield from self._revision_intrinsic_metadata.missing(metadata) def revision_intrinsic_metadata_get(self, ids): yield from (obj.to_dict() for obj in self._revision_intrinsic_metadata.get(ids)) def revision_intrinsic_metadata_add( self, metadata: List[Dict], conflict_update: bool = False ) -> Dict[str, int]: check_id_types(metadata) added = self._revision_intrinsic_metadata.add( map(RevisionIntrinsicMetadataRow.from_dict, metadata), conflict_update ) return {"revision_intrinsic_metadata:add": added} def revision_intrinsic_metadata_delete(self, entries: List[Dict]) -> Dict: deleted = self._revision_intrinsic_metadata.delete(entries) return {"revision_intrinsic_metadata:del": deleted} def origin_intrinsic_metadata_get(self, ids): yield from (obj.to_dict() for obj in self._origin_intrinsic_metadata.get(ids)) def origin_intrinsic_metadata_add( self, metadata: List[Dict], conflict_update: bool = False ) -> Dict[str, int]: added = self._origin_intrinsic_metadata.add( map(OriginIntrinsicMetadataRow.from_dict, metadata), conflict_update ) return {"origin_intrinsic_metadata:add": added} def origin_intrinsic_metadata_delete(self, entries: List[Dict]) -> Dict: deleted = self._origin_intrinsic_metadata.delete(entries) return {"origin_intrinsic_metadata:del": deleted} def origin_intrinsic_metadata_search_fulltext(self, conjunction, limit=100): # A very crude fulltext search implementation, but that's enough # to work on English metadata tokens_re = re.compile("[a-zA-Z0-9]+") search_tokens = list(itertools.chain(*map(tokens_re.findall, conjunction))) def rank(data): # Tokenize the metadata text = json.dumps(data.metadata) text_tokens = tokens_re.findall(text) text_token_occurences = Counter(text_tokens) # Count the number of occurrences of search tokens in the text score = 0 for search_token in search_tokens: if text_token_occurences[search_token] == 0: # Search token is not in the text. return 0 score += text_token_occurences[search_token] # Normalize according to the text's length return score / math.log(len(text_tokens)) results = [ (rank(data), data) for data in self._origin_intrinsic_metadata.get_all() ] results = [(rank_, data) for (rank_, data) in results if rank_ > 0] results.sort( key=operator.itemgetter(0), reverse=True # Don't try to order 'data' ) for (rank_, result) in results[:limit]: yield result.to_dict() def origin_intrinsic_metadata_search_by_producer( self, page_token="", limit=100, ids_only=False, mappings=None, tool_ids=None ): assert isinstance(page_token, str) nb_results = 0 if mappings is not None: mappings = frozenset(mappings) if tool_ids is not None: tool_ids = frozenset(tool_ids) origins = [] # we go to limit+1 to check whether we should add next_page_token in # the response for entry in self._origin_intrinsic_metadata.get_all(): if entry.id <= page_token: continue if nb_results >= (limit + 1): break if mappings is not None and mappings.isdisjoint(entry.mappings): continue if tool_ids is not None and entry.tool["id"] not in tool_ids: continue origins.append(entry.to_dict()) nb_results += 1 result = {} if len(origins) > limit: origins = origins[:limit] result["next_page_token"] = origins[-1]["id"] if ids_only: origins = [origin["id"] for origin in origins] result["origins"] = origins return result def origin_intrinsic_metadata_stats(self): mapping_count = {m: 0 for m in MAPPING_NAMES} total = non_empty = 0 for data in self._origin_intrinsic_metadata.get_all(): total += 1 if set(data.metadata) - {"@context"}: non_empty += 1 for mapping in data.mappings: mapping_count[mapping] += 1 return {"per_mapping": mapping_count, "total": total, "non_empty": non_empty} def indexer_configuration_add(self, tools): inserted = [] for tool in tools: tool = tool.copy() id_ = self._tool_key(tool) tool["id"] = id_ self._tools[id_] = tool inserted.append(tool) return inserted def indexer_configuration_get(self, tool): return self._tools.get(self._tool_key(tool)) def _tool_key(self, tool): return hash( ( tool["tool_name"], tool["tool_version"], json.dumps(tool["tool_configuration"], sort_keys=True), ) ) diff --git a/swh/indexer/storage/model.py b/swh/indexer/storage/model.py index 0c12ca8..eec1c43 100644 --- a/swh/indexer/storage/model.py +++ b/swh/indexer/storage/model.py @@ -1,119 +1,114 @@ # Copyright (C) 2020 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information """Classes used internally by the in-memory idx-storage, and will be used for the interface of the idx-storage in the near future.""" from __future__ import annotations -from typing import Any, Dict, List, Optional, Type, TypeVar +from typing import Any, Dict, List, Optional, Tuple, Type, TypeVar import attr -from typing_extensions import TypedDict from swh.model.model import Sha1Git, dictify - -class KeyDict(TypedDict): - id: Sha1Git - indexer_configuration_id: int - - TSelf = TypeVar("TSelf") @attr.s class BaseRow: + UNIQUE_KEY_FIELDS: Tuple = ("id", "indexer_configuration_id") + id = attr.ib(type=Sha1Git) indexer_configuration_id = attr.ib(type=Optional[int], default=None, kw_only=True) tool = attr.ib(type=Optional[Dict], default=None, kw_only=True) def __attrs_post_init__(self): if self.indexer_configuration_id is None and self.tool is None: raise TypeError("Either indexer_configuration_id or tool must be not None.") if self.indexer_configuration_id is not None and self.tool is not None: raise TypeError( "indexer_configuration_id and tool are mutually exclusive; " "only one may be not None." ) def anonymize(self: TSelf) -> Optional[TSelf]: # Needed to implement swh.journal.writer.ValueProtocol return None def to_dict(self) -> Dict[str, Any]: """Wrapper of `attr.asdict` that can be overridden by subclasses that have special handling of some of the fields.""" d = dictify(attr.asdict(self, recurse=False)) if d["indexer_configuration_id"] is None: del d["indexer_configuration_id"] if d["tool"] is None: del d["tool"] return d @classmethod def from_dict(cls: Type[TSelf], d) -> TSelf: return cls(**d) # type: ignore + def unique_key(self) -> Dict: + if self.indexer_configuration_id is None: + raise ValueError( + "Can only call unique_key() on objects with indexer_configuration_id." + ) + return {key: getattr(self, key) for key in self.UNIQUE_KEY_FIELDS} + @attr.s class ContentMimetypeRow(BaseRow): mimetype = attr.ib(type=str) encoding = attr.ib(type=str) @attr.s class ContentLanguageRow(BaseRow): lang = attr.ib(type=str) @attr.s -class ContentCtagsEntry: +class ContentCtagsRow(BaseRow): + UNIQUE_KEY_FIELDS = ( + "id", + "indexer_configuration_id", + "name", + "kind", + "line", + "lang", + ) + name = attr.ib(type=str) kind = attr.ib(type=str) line = attr.ib(type=int) lang = attr.ib(type=str) - def to_dict(self) -> Dict[str, Any]: - return dictify(attr.asdict(self, recurse=False)) - - @classmethod - def from_dict(cls, d) -> ContentCtagsEntry: - return cls(**d) - @attr.s -class ContentCtagsRow(BaseRow): - ctags = attr.ib(type=List[ContentCtagsEntry]) +class ContentLicenseRow(BaseRow): + UNIQUE_KEY_FIELDS = ("id", "indexer_configuration_id", "license") - @classmethod - def from_dict(cls, d) -> ContentCtagsRow: - d = d.copy() - items = d.pop("ctags") - return cls(ctags=[ContentCtagsEntry.from_dict(item) for item in items], **d,) - - -@attr.s -class ContentLicensesRow(BaseRow): - licenses = attr.ib(type=List[str]) + license = attr.ib(type=str) @attr.s class ContentMetadataRow(BaseRow): metadata = attr.ib(type=Dict[str, Any]) @attr.s class RevisionIntrinsicMetadataRow(BaseRow): metadata = attr.ib(type=Dict[str, Any]) mappings = attr.ib(type=List[str]) @attr.s class OriginIntrinsicMetadataRow(BaseRow): metadata = attr.ib(type=Dict[str, Any]) from_revision = attr.ib(type=Sha1Git) mappings = attr.ib(type=List[str]) diff --git a/swh/indexer/tests/storage/test_storage.py b/swh/indexer/tests/storage/test_storage.py index a27bb27..f2bbf79 100644 --- a/swh/indexer/tests/storage/test_storage.py +++ b/swh/indexer/tests/storage/test_storage.py @@ -1,1894 +1,1947 @@ # Copyright (C) 2015-2020 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import inspect import math import threading from typing import Dict import pytest from swh.indexer.storage.exc import DuplicateId, IndexerStorageArgumentException from swh.indexer.storage.interface import IndexerStorageInterface from swh.model.hashutil import hash_to_bytes def prepare_mimetypes_from(fossology_licenses): """Fossology license needs some consistent data in db to run. """ mimetypes = [] for c in fossology_licenses: mimetypes.append( { "id": c["id"], "mimetype": "text/plain", # for filtering on textual data to work "encoding": "utf-8", "indexer_configuration_id": c["indexer_configuration_id"], } ) return mimetypes def endpoint_name(etype: str, ename: str) -> str: """Compute the storage's endpoint's name >>> endpoint_name('content_mimetype', 'add') 'content_mimetype_add' >>> endpoint_name('content_fosso_license', 'delete') 'content_fosso_license_delete' """ return f"{etype}_{ename}" def endpoint(storage, etype: str, ename: str): return getattr(storage, endpoint_name(etype, ename)) def expected_summary(count: int, etype: str, ename: str = "add") -> Dict[str, int]: """Compute the expected summary The key is determine according to etype and ename >>> expected_summary(10, 'content_mimetype', 'add') {'content_mimetype:add': 10} >>> expected_summary(9, 'origin_intrinsic_metadata', 'delete') {'origin_intrinsic_metadata:del': 9} """ pattern = ename[0:3] key = endpoint_name(etype, ename).replace(f"_{ename}", f":{pattern}") return {key: count} def test_check_config(swh_indexer_storage): assert swh_indexer_storage.check_config(check_write=True) assert swh_indexer_storage.check_config(check_write=False) def test_types(swh_indexer_storage): """Checks all methods of StorageInterface are implemented by this backend, and that they have the same signature.""" # Create an instance of the protocol (which cannot be instantiated # directly, so this creates a subclass, then instantiates it) interface = type("_", (IndexerStorageInterface,), {})() assert "content_mimetype_add" in dir(interface) missing_methods = [] for meth_name in dir(interface): if meth_name.startswith("_"): continue interface_meth = getattr(interface, meth_name) try: concrete_meth = getattr(swh_indexer_storage, meth_name) except AttributeError: missing_methods.append(meth_name) continue expected_signature = inspect.signature(interface_meth) actual_signature = inspect.signature(concrete_meth) assert expected_signature == actual_signature, meth_name assert missing_methods == [] class StorageETypeTester: """Base class for testing a series of common behaviour between a bunch of endpoint types supported by an IndexerStorage. This is supposed to be inherited with the following class attributes: - endpoint_type - tool_name - example_data See below for example usage. """ def test_missing(self, swh_indexer_storage_with_data): storage, data = swh_indexer_storage_with_data etype = self.endpoint_type tool_id = data.tools[self.tool_name]["id"] # given 2 (hopefully) unknown objects query = [ {"id": data.sha1_1, "indexer_configuration_id": tool_id,}, {"id": data.sha1_2, "indexer_configuration_id": tool_id,}, ] # we expect these are both returned by the xxx_missing endpoint actual_missing = endpoint(storage, etype, "missing")(query) assert list(actual_missing) == [ data.sha1_1, data.sha1_2, ] # now, when we add one of them summary = endpoint(storage, etype, "add")( [ { "id": data.sha1_2, **self.example_data[0], "indexer_configuration_id": tool_id, } ] ) assert summary == expected_summary(1, etype) # we expect only the other one returned actual_missing = endpoint(storage, etype, "missing")(query) assert list(actual_missing) == [data.sha1_1] def test_add__drop_duplicate(self, swh_indexer_storage_with_data): storage, data = swh_indexer_storage_with_data etype = self.endpoint_type tool_id = data.tools[self.tool_name]["id"] # add the first object data_v1 = { "id": data.sha1_2, **self.example_data[0], "indexer_configuration_id": tool_id, } summary = endpoint(storage, etype, "add")([data_v1]) assert summary == expected_summary(1, etype) # should be able to retrieve it actual_data = list(endpoint(storage, etype, "get")([data.sha1_2])) expected_data_v1 = [ { "id": data.sha1_2, **self.example_data[0], "tool": data.tools[self.tool_name], } ] assert actual_data == expected_data_v1 # now if we add a modified version of the same object (same id) data_v2 = data_v1.copy() data_v2.update(self.example_data[1]) summary2 = endpoint(storage, etype, "add")([data_v2]) assert summary2 == expected_summary(0, etype) # not added # we expect to retrieve the original data, not the modified one actual_data = list(endpoint(storage, etype, "get")([data.sha1_2])) assert actual_data == expected_data_v1 def test_add__update_in_place_duplicate(self, swh_indexer_storage_with_data): storage, data = swh_indexer_storage_with_data etype = self.endpoint_type tool = data.tools[self.tool_name] data_v1 = { "id": data.sha1_2, **self.example_data[0], "indexer_configuration_id": tool["id"], } # given summary = endpoint(storage, etype, "add")([data_v1]) assert summary == expected_summary(1, etype) # not added # when actual_data = list(endpoint(storage, etype, "get")([data.sha1_2])) expected_data_v1 = [{"id": data.sha1_2, **self.example_data[0], "tool": tool,}] # then assert actual_data == expected_data_v1 # given data_v2 = data_v1.copy() data_v2.update(self.example_data[1]) endpoint(storage, etype, "add")([data_v2], conflict_update=True) assert summary == expected_summary(1, etype) # modified so counted actual_data = list(endpoint(storage, etype, "get")([data.sha1_2])) expected_data_v2 = [{"id": data.sha1_2, **self.example_data[1], "tool": tool,}] # data did change as the v2 was used to overwrite v1 assert actual_data == expected_data_v2 def test_add__update_in_place_deadlock(self, swh_indexer_storage_with_data): storage, data = swh_indexer_storage_with_data etype = self.endpoint_type tool = data.tools[self.tool_name] hashes = [ hash_to_bytes("34973274ccef6ab4dfaaf86599792fa9c3fe4{:03d}".format(i)) for i in range(1000) ] data_v1 = [ { "id": hash_, **self.example_data[0], "indexer_configuration_id": tool["id"], } for hash_ in hashes ] data_v2 = [ { "id": hash_, **self.example_data[1], "indexer_configuration_id": tool["id"], } for hash_ in hashes ] # Remove one item from each, so that both queries have to succeed for # all items to be in the DB. data_v2a = data_v2[1:] data_v2b = list(reversed(data_v2[0:-1])) # given endpoint(storage, etype, "add")(data_v1) # when actual_data = list(endpoint(storage, etype, "get")(hashes)) expected_data_v1 = [ {"id": hash_, **self.example_data[0], "tool": tool,} for hash_ in hashes ] # then assert actual_data == expected_data_v1 # given def f1(): endpoint(storage, etype, "add")(data_v2a, conflict_update=True) def f2(): endpoint(storage, etype, "add")(data_v2b, conflict_update=True) t1 = threading.Thread(target=f1) t2 = threading.Thread(target=f2) t2.start() t1.start() t1.join() t2.join() actual_data = sorted( endpoint(storage, etype, "get")(hashes), key=lambda x: x["id"] ) expected_data_v2 = [ {"id": hash_, **self.example_data[1], "tool": tool,} for hash_ in hashes ] assert actual_data == expected_data_v2 def test_add__duplicate_twice(self, swh_indexer_storage_with_data): storage, data = swh_indexer_storage_with_data etype = self.endpoint_type tool = data.tools[self.tool_name] data_rev1 = { "id": data.revision_id_2, **self.example_data[0], "indexer_configuration_id": tool["id"], } data_rev2 = { "id": data.revision_id_2, **self.example_data[1], "indexer_configuration_id": tool["id"], } # when summary = endpoint(storage, etype, "add")([data_rev1]) assert summary == expected_summary(1, etype) with pytest.raises(DuplicateId): endpoint(storage, etype, "add")( [data_rev2, data_rev2], conflict_update=True ) # then actual_data = list( endpoint(storage, etype, "get")([data.revision_id_2, data.revision_id_1]) ) expected_data = [ {"id": data.revision_id_2, **self.example_data[0], "tool": tool,} ] assert actual_data == expected_data def test_get(self, swh_indexer_storage_with_data): storage, data = swh_indexer_storage_with_data etype = self.endpoint_type tool = data.tools[self.tool_name] query = [data.sha1_2, data.sha1_1] data1 = { "id": data.sha1_2, **self.example_data[0], "indexer_configuration_id": tool["id"], } # when summary = endpoint(storage, etype, "add")([data1]) assert summary == expected_summary(1, etype) # then actual_data = list(endpoint(storage, etype, "get")(query)) # then expected_data = [{"id": data.sha1_2, **self.example_data[0], "tool": tool,}] assert actual_data == expected_data class TestIndexerStorageContentMimetypes(StorageETypeTester): """Test Indexer Storage content_mimetype related methods """ endpoint_type = "content_mimetype" tool_name = "file" example_data = [ {"mimetype": "text/plain", "encoding": "utf-8",}, {"mimetype": "text/html", "encoding": "us-ascii",}, ] def test_generate_content_mimetype_get_partition_failure(self, swh_indexer_storage): """get_partition call with wrong limit input should fail""" storage = swh_indexer_storage indexer_configuration_id = None with pytest.raises( IndexerStorageArgumentException, match="limit should not be None" ): storage.content_mimetype_get_partition( indexer_configuration_id, 0, 3, limit=None ) def test_generate_content_mimetype_get_partition_no_limit( self, swh_indexer_storage_with_data ): """get_partition should return result""" storage, data = swh_indexer_storage_with_data mimetypes = data.mimetypes expected_ids = set([c["id"] for c in mimetypes]) indexer_configuration_id = mimetypes[0]["indexer_configuration_id"] assert len(mimetypes) == 16 nb_partitions = 16 actual_ids = [] for partition_id in range(nb_partitions): actual_result = storage.content_mimetype_get_partition( indexer_configuration_id, partition_id, nb_partitions ) assert actual_result.next_page_token is None actual_ids.extend(actual_result.results) assert len(actual_ids) == len(expected_ids) for actual_id in actual_ids: assert actual_id in expected_ids def test_generate_content_mimetype_get_partition_full( self, swh_indexer_storage_with_data ): """get_partition for a single partition should return available ids """ storage, data = swh_indexer_storage_with_data mimetypes = data.mimetypes expected_ids = set([c["id"] for c in mimetypes]) indexer_configuration_id = mimetypes[0]["indexer_configuration_id"] actual_result = storage.content_mimetype_get_partition( indexer_configuration_id, 0, 1 ) assert actual_result.next_page_token is None actual_ids = actual_result.results assert len(actual_ids) == len(expected_ids) for actual_id in actual_ids: assert actual_id in expected_ids def test_generate_content_mimetype_get_partition_empty( self, swh_indexer_storage_with_data ): """get_partition when at least one of the partitions is empty""" storage, data = swh_indexer_storage_with_data mimetypes = data.mimetypes expected_ids = set([c["id"] for c in mimetypes]) indexer_configuration_id = mimetypes[0]["indexer_configuration_id"] # nb_partitions = smallest power of 2 such that at least one of # the partitions is empty nb_mimetypes = len(mimetypes) nb_partitions = 1 << math.floor(math.log2(nb_mimetypes) + 1) seen_ids = [] for partition_id in range(nb_partitions): actual_result = storage.content_mimetype_get_partition( indexer_configuration_id, partition_id, nb_partitions, limit=nb_mimetypes + 1, ) for actual_id in actual_result.results: seen_ids.append(actual_id) # Limit is higher than the max number of results assert actual_result.next_page_token is None assert set(seen_ids) == expected_ids def test_generate_content_mimetype_get_partition_with_pagination( self, swh_indexer_storage_with_data ): """get_partition should return ids provided with pagination """ storage, data = swh_indexer_storage_with_data mimetypes = data.mimetypes expected_ids = set([c["id"] for c in mimetypes]) indexer_configuration_id = mimetypes[0]["indexer_configuration_id"] nb_partitions = 4 actual_ids = [] for partition_id in range(nb_partitions): next_page_token = None while True: actual_result = storage.content_mimetype_get_partition( indexer_configuration_id, partition_id, nb_partitions, limit=2, page_token=next_page_token, ) actual_ids.extend(actual_result.results) next_page_token = actual_result.next_page_token if next_page_token is None: break assert len(set(actual_ids)) == len(set(expected_ids)) for actual_id in actual_ids: assert actual_id in expected_ids class TestIndexerStorageContentLanguage(StorageETypeTester): """Test Indexer Storage content_language related methods """ endpoint_type = "content_language" tool_name = "pygments" example_data = [ {"lang": "haskell",}, {"lang": "common-lisp",}, ] class TestIndexerStorageContentCTags(StorageETypeTester): """Test Indexer Storage content_ctags related methods """ endpoint_type = "content_ctags" tool_name = "universal-ctags" example_data = [ { "ctags": [ {"name": "done", "kind": "variable", "line": 119, "lang": "OCaml",} ] }, { "ctags": [ {"name": "done", "kind": "variable", "line": 100, "lang": "Python",}, {"name": "main", "kind": "function", "line": 119, "lang": "Python",}, ] }, ] # the following tests are disabled because CTAGS behaves differently @pytest.mark.skip def test_add__drop_duplicate(self): pass @pytest.mark.skip def test_add__update_in_place_duplicate(self): pass @pytest.mark.skip def test_add__update_in_place_deadlock(self): pass @pytest.mark.skip def test_add__duplicate_twice(self): pass @pytest.mark.skip def test_get(self): pass def test_content_ctags_search(self, swh_indexer_storage_with_data): storage, data = swh_indexer_storage_with_data # 1. given tool = data.tools["universal-ctags"] tool_id = tool["id"] ctag1 = { "id": data.sha1_1, "indexer_configuration_id": tool_id, "ctags": [ {"name": "hello", "kind": "function", "line": 133, "lang": "Python",}, {"name": "counter", "kind": "variable", "line": 119, "lang": "Python",}, {"name": "hello", "kind": "variable", "line": 210, "lang": "Python",}, ], } ctag2 = { "id": data.sha1_2, "indexer_configuration_id": tool_id, "ctags": [ {"name": "hello", "kind": "variable", "line": 100, "lang": "C",}, {"name": "result", "kind": "variable", "line": 120, "lang": "C",}, ], } storage.content_ctags_add([ctag1, ctag2]) # 1. when actual_ctags = list(storage.content_ctags_search("hello", limit=1)) # 1. then assert actual_ctags == [ { "id": ctag1["id"], "tool": tool, "name": "hello", "kind": "function", "line": 133, "lang": "Python", } ] # 2. when actual_ctags = list( storage.content_ctags_search("hello", limit=1, last_sha1=ctag1["id"]) ) # 2. then assert actual_ctags == [ { "id": ctag2["id"], "tool": tool, "name": "hello", "kind": "variable", "line": 100, "lang": "C", } ] # 3. when actual_ctags = list(storage.content_ctags_search("hello")) # 3. then assert actual_ctags == [ { "id": ctag1["id"], "tool": tool, "name": "hello", "kind": "function", "line": 133, "lang": "Python", }, { "id": ctag1["id"], "tool": tool, "name": "hello", "kind": "variable", "line": 210, "lang": "Python", }, { "id": ctag2["id"], "tool": tool, "name": "hello", "kind": "variable", "line": 100, "lang": "C", }, ] # 4. when actual_ctags = list(storage.content_ctags_search("counter")) # then assert actual_ctags == [ { "id": ctag1["id"], "tool": tool, "name": "counter", "kind": "variable", "line": 119, "lang": "Python", } ] # 5. when actual_ctags = list(storage.content_ctags_search("result", limit=1)) # then assert actual_ctags == [ { "id": ctag2["id"], "tool": tool, "name": "result", "kind": "variable", "line": 120, "lang": "C", } ] def test_content_ctags_search_no_result(self, swh_indexer_storage): storage = swh_indexer_storage actual_ctags = list(storage.content_ctags_search("counter")) assert not actual_ctags def test_content_ctags_add__add_new_ctags_added( self, swh_indexer_storage_with_data ): storage, data = swh_indexer_storage_with_data # given tool = data.tools["universal-ctags"] tool_id = tool["id"] ctag_v1 = { "id": data.sha1_2, "indexer_configuration_id": tool_id, "ctags": [ {"name": "done", "kind": "variable", "line": 100, "lang": "Scheme",} ], } # given storage.content_ctags_add([ctag_v1]) storage.content_ctags_add([ctag_v1]) # conflict does nothing # when actual_ctags = list(storage.content_ctags_get([data.sha1_2])) # then expected_ctags = [ { "id": data.sha1_2, "name": "done", "kind": "variable", "line": 100, "lang": "Scheme", "tool": tool, } ] assert actual_ctags == expected_ctags # given ctag_v2 = ctag_v1.copy() ctag_v2.update( { "ctags": [ {"name": "defn", "kind": "function", "line": 120, "lang": "Scheme",} ] } ) storage.content_ctags_add([ctag_v2]) expected_ctags = [ { "id": data.sha1_2, "name": "done", "kind": "variable", "line": 100, "lang": "Scheme", "tool": tool, }, { "id": data.sha1_2, "name": "defn", "kind": "function", "line": 120, "lang": "Scheme", "tool": tool, }, ] actual_ctags = list(storage.content_ctags_get([data.sha1_2])) assert actual_ctags == expected_ctags def test_content_ctags_add__update_in_place(self, swh_indexer_storage_with_data): storage, data = swh_indexer_storage_with_data # given tool = data.tools["universal-ctags"] tool_id = tool["id"] ctag_v1 = { "id": data.sha1_2, "indexer_configuration_id": tool_id, "ctags": [ {"name": "done", "kind": "variable", "line": 100, "lang": "Scheme",} ], } # given storage.content_ctags_add([ctag_v1]) # when actual_ctags = list(storage.content_ctags_get([data.sha1_2])) # then expected_ctags = [ { "id": data.sha1_2, "name": "done", "kind": "variable", "line": 100, "lang": "Scheme", "tool": tool, } ] assert actual_ctags == expected_ctags # given ctag_v2 = ctag_v1.copy() ctag_v2.update( { "ctags": [ { "name": "done", "kind": "variable", "line": 100, "lang": "Scheme", }, { "name": "defn", "kind": "function", "line": 120, "lang": "Scheme", }, ] } ) storage.content_ctags_add([ctag_v2], conflict_update=True) actual_ctags = list(storage.content_ctags_get([data.sha1_2])) # ctag did change as the v2 was used to overwrite v1 expected_ctags = [ { "id": data.sha1_2, "name": "done", "kind": "variable", "line": 100, "lang": "Scheme", "tool": tool, }, { "id": data.sha1_2, "name": "defn", "kind": "function", "line": 120, "lang": "Scheme", "tool": tool, }, ] assert actual_ctags == expected_ctags + def test_add_empty(self, swh_indexer_storage_with_data): + (storage, data) = swh_indexer_storage_with_data + etype = self.endpoint_type + tool = data.tools[self.tool_name] + + summary = endpoint(storage, etype, "add")( + [{"id": data.sha1_2, "indexer_configuration_id": tool["id"], "ctags": [],}] + ) + assert summary == {"content_ctags:add": 0} + + actual_ctags = list(endpoint(storage, etype, "get")([data.sha1_2])) + + assert actual_ctags == [] + + def test_get_unknown(self, swh_indexer_storage_with_data): + (storage, data) = swh_indexer_storage_with_data + etype = self.endpoint_type + + actual_ctags = list(endpoint(storage, etype, "get")([data.sha1_2])) + + assert actual_ctags == [] + class TestIndexerStorageContentMetadata(StorageETypeTester): """Test Indexer Storage content_metadata related methods """ tool_name = "swh-metadata-detector" endpoint_type = "content_metadata" example_data = [ { "metadata": { "other": {}, "codeRepository": { "type": "git", "url": "https://github.com/moranegg/metadata_test", }, "description": "Simple package.json test for indexer", "name": "test_metadata", "version": "0.0.1", }, }, {"metadata": {"other": {}, "name": "test_metadata", "version": "0.0.1"},}, ] class TestIndexerStorageRevisionIntrinsicMetadata(StorageETypeTester): """Test Indexer Storage revision_intrinsic_metadata related methods """ tool_name = "swh-metadata-detector" endpoint_type = "revision_intrinsic_metadata" example_data = [ { "metadata": { "other": {}, "codeRepository": { "type": "git", "url": "https://github.com/moranegg/metadata_test", }, "description": "Simple package.json test for indexer", "name": "test_metadata", "version": "0.0.1", }, "mappings": ["mapping1"], }, { "metadata": {"other": {}, "name": "test_metadata", "version": "0.0.1"}, "mappings": ["mapping2"], }, ] def test_revision_intrinsic_metadata_delete(self, swh_indexer_storage_with_data): storage, data = swh_indexer_storage_with_data etype = self.endpoint_type tool = data.tools[self.tool_name] query = [data.sha1_2, data.sha1_1] data1 = { "id": data.sha1_2, **self.example_data[0], "indexer_configuration_id": tool["id"], } # when summary = endpoint(storage, etype, "add")([data1]) assert summary == expected_summary(1, etype) summary2 = endpoint(storage, etype, "delete")( [{"id": data.sha1_2, "indexer_configuration_id": tool["id"],}] ) assert summary2 == expected_summary(1, etype, "del") # then actual_data = list(endpoint(storage, etype, "get")(query)) # then assert not actual_data def test_revision_intrinsic_metadata_delete_nonexisting( self, swh_indexer_storage_with_data ): storage, data = swh_indexer_storage_with_data etype = self.endpoint_type tool = data.tools[self.tool_name] endpoint(storage, etype, "delete")( [{"id": data.sha1_2, "indexer_configuration_id": tool["id"],}] ) -class TestIndexerStorageContentFossologyLicence: +class TestIndexerStorageContentFossologyLicense: + endpoint_type = "content_fossology_license" + tool_name = "nomos" + def test_content_fossology_license_add__new_license_added( self, swh_indexer_storage_with_data ): storage, data = swh_indexer_storage_with_data # given tool = data.tools["nomos"] tool_id = tool["id"] license_v1 = { "id": data.sha1_1, "licenses": ["Apache-2.0"], "indexer_configuration_id": tool_id, } # given storage.content_fossology_license_add([license_v1]) # conflict does nothing storage.content_fossology_license_add([license_v1]) # when actual_licenses = list(storage.content_fossology_license_get([data.sha1_1])) # then expected_license = {data.sha1_1: [{"licenses": ["Apache-2.0"], "tool": tool,}]} assert actual_licenses == [expected_license] # given license_v2 = license_v1.copy() license_v2.update( {"licenses": ["BSD-2-Clause"],} ) storage.content_fossology_license_add([license_v2]) actual_licenses = list(storage.content_fossology_license_get([data.sha1_1])) expected_license = { data.sha1_1: [{"licenses": ["Apache-2.0", "BSD-2-Clause"], "tool": tool}] } # license did not change as the v2 was dropped. assert actual_licenses == [expected_license] def test_generate_content_fossology_license_get_partition_failure( self, swh_indexer_storage_with_data ): """get_partition call with wrong limit input should fail""" storage, data = swh_indexer_storage_with_data indexer_configuration_id = None with pytest.raises( IndexerStorageArgumentException, match="limit should not be None" ): storage.content_fossology_license_get_partition( indexer_configuration_id, 0, 3, limit=None, ) def test_generate_content_fossology_license_get_partition_no_limit( self, swh_indexer_storage_with_data ): """get_partition should return results""" storage, data = swh_indexer_storage_with_data # craft some consistent mimetypes fossology_licenses = data.fossology_licenses mimetypes = prepare_mimetypes_from(fossology_licenses) indexer_configuration_id = fossology_licenses[0]["indexer_configuration_id"] storage.content_mimetype_add(mimetypes, conflict_update=True) # add fossology_licenses to storage storage.content_fossology_license_add(fossology_licenses) # All ids from the db expected_ids = set([c["id"] for c in fossology_licenses]) assert len(fossology_licenses) == 10 assert len(mimetypes) == 10 nb_partitions = 4 actual_ids = [] for partition_id in range(nb_partitions): actual_result = storage.content_fossology_license_get_partition( indexer_configuration_id, partition_id, nb_partitions ) assert actual_result.next_page_token is None actual_ids.extend(actual_result.results) assert len(set(actual_ids)) == len(expected_ids) for actual_id in actual_ids: assert actual_id in expected_ids def test_generate_content_fossology_license_get_partition_full( self, swh_indexer_storage_with_data ): """get_partition for a single partition should return available ids """ storage, data = swh_indexer_storage_with_data # craft some consistent mimetypes fossology_licenses = data.fossology_licenses mimetypes = prepare_mimetypes_from(fossology_licenses) indexer_configuration_id = fossology_licenses[0]["indexer_configuration_id"] storage.content_mimetype_add(mimetypes, conflict_update=True) # add fossology_licenses to storage storage.content_fossology_license_add(fossology_licenses) # All ids from the db expected_ids = set([c["id"] for c in fossology_licenses]) actual_result = storage.content_fossology_license_get_partition( indexer_configuration_id, 0, 1 ) assert actual_result.next_page_token is None actual_ids = actual_result.results assert len(set(actual_ids)) == len(expected_ids) for actual_id in actual_ids: assert actual_id in expected_ids def test_generate_content_fossology_license_get_partition_empty( self, swh_indexer_storage_with_data ): """get_partition when at least one of the partitions is empty""" storage, data = swh_indexer_storage_with_data # craft some consistent mimetypes fossology_licenses = data.fossology_licenses mimetypes = prepare_mimetypes_from(fossology_licenses) indexer_configuration_id = fossology_licenses[0]["indexer_configuration_id"] storage.content_mimetype_add(mimetypes, conflict_update=True) # add fossology_licenses to storage storage.content_fossology_license_add(fossology_licenses) # All ids from the db expected_ids = set([c["id"] for c in fossology_licenses]) # nb_partitions = smallest power of 2 such that at least one of # the partitions is empty nb_licenses = len(fossology_licenses) nb_partitions = 1 << math.floor(math.log2(nb_licenses) + 1) seen_ids = [] for partition_id in range(nb_partitions): actual_result = storage.content_fossology_license_get_partition( indexer_configuration_id, partition_id, nb_partitions, limit=nb_licenses + 1, ) for actual_id in actual_result.results: seen_ids.append(actual_id) # Limit is higher than the max number of results assert actual_result.next_page_token is None assert set(seen_ids) == expected_ids def test_generate_content_fossology_license_get_partition_with_pagination( self, swh_indexer_storage_with_data ): """get_partition should return ids provided with paginationv """ storage, data = swh_indexer_storage_with_data # craft some consistent mimetypes fossology_licenses = data.fossology_licenses mimetypes = prepare_mimetypes_from(fossology_licenses) indexer_configuration_id = fossology_licenses[0]["indexer_configuration_id"] storage.content_mimetype_add(mimetypes, conflict_update=True) # add fossology_licenses to storage storage.content_fossology_license_add(fossology_licenses) # All ids from the db expected_ids = [c["id"] for c in fossology_licenses] nb_partitions = 4 actual_ids = [] for partition_id in range(nb_partitions): next_page_token = None while True: actual_result = storage.content_fossology_license_get_partition( indexer_configuration_id, partition_id, nb_partitions, limit=2, page_token=next_page_token, ) actual_ids.extend(actual_result.results) next_page_token = actual_result.next_page_token if next_page_token is None: break assert len(set(actual_ids)) == len(set(expected_ids)) for actual_id in actual_ids: assert actual_id in expected_ids + def test_add_empty(self, swh_indexer_storage_with_data): + (storage, data) = swh_indexer_storage_with_data + etype = self.endpoint_type + tool = data.tools[self.tool_name] + + summary = endpoint(storage, etype, "add")( + [ + { + "id": data.sha1_2, + "indexer_configuration_id": tool["id"], + "licenses": [], + } + ] + ) + assert summary == {"content_fossology_license:add": 0} + + actual_license = list(endpoint(storage, etype, "get")([data.sha1_2])) + + assert actual_license == [] + + def test_get_unknown(self, swh_indexer_storage_with_data): + (storage, data) = swh_indexer_storage_with_data + etype = self.endpoint_type + + actual_license = list(endpoint(storage, etype, "get")([data.sha1_2])) + + assert actual_license == [] + class TestIndexerStorageOriginIntrinsicMetadata: def test_origin_intrinsic_metadata_get(self, swh_indexer_storage_with_data): storage, data = swh_indexer_storage_with_data # given tool_id = data.tools["swh-metadata-detector"]["id"] metadata = { "version": None, "name": None, } metadata_rev = { "id": data.revision_id_2, "metadata": metadata, "mappings": ["mapping1"], "indexer_configuration_id": tool_id, } metadata_origin = { "id": data.origin_url_1, "metadata": metadata, "indexer_configuration_id": tool_id, "mappings": ["mapping1"], "from_revision": data.revision_id_2, } # when storage.revision_intrinsic_metadata_add([metadata_rev]) storage.origin_intrinsic_metadata_add([metadata_origin]) # then actual_metadata = list( storage.origin_intrinsic_metadata_get([data.origin_url_1, "no://where"]) ) expected_metadata = [ { "id": data.origin_url_1, "metadata": metadata, "tool": data.tools["swh-metadata-detector"], "from_revision": data.revision_id_2, "mappings": ["mapping1"], } ] assert actual_metadata == expected_metadata def test_origin_intrinsic_metadata_delete(self, swh_indexer_storage_with_data): storage, data = swh_indexer_storage_with_data # given tool_id = data.tools["swh-metadata-detector"]["id"] metadata = { "version": None, "name": None, } metadata_rev = { "id": data.revision_id_2, "metadata": metadata, "mappings": ["mapping1"], "indexer_configuration_id": tool_id, } metadata_origin = { "id": data.origin_url_1, "metadata": metadata, "indexer_configuration_id": tool_id, "mappings": ["mapping1"], "from_revision": data.revision_id_2, } metadata_origin2 = metadata_origin.copy() metadata_origin2["id"] = data.origin_url_2 # when storage.revision_intrinsic_metadata_add([metadata_rev]) storage.origin_intrinsic_metadata_add([metadata_origin, metadata_origin2]) storage.origin_intrinsic_metadata_delete( [{"id": data.origin_url_1, "indexer_configuration_id": tool_id}] ) # then actual_metadata = list( storage.origin_intrinsic_metadata_get( [data.origin_url_1, data.origin_url_2, "no://where"] ) ) for item in actual_metadata: item["indexer_configuration_id"] = item.pop("tool")["id"] assert actual_metadata == [metadata_origin2] def test_origin_intrinsic_metadata_delete_nonexisting( self, swh_indexer_storage_with_data ): storage, data = swh_indexer_storage_with_data tool_id = data.tools["swh-metadata-detector"]["id"] storage.origin_intrinsic_metadata_delete( [{"id": data.origin_url_1, "indexer_configuration_id": tool_id}] ) def test_origin_intrinsic_metadata_add_drop_duplicate( self, swh_indexer_storage_with_data ): storage, data = swh_indexer_storage_with_data # given tool_id = data.tools["swh-metadata-detector"]["id"] metadata_v1 = { "version": None, "name": None, } metadata_rev_v1 = { "id": data.revision_id_1, "metadata": metadata_v1.copy(), "mappings": [], "indexer_configuration_id": tool_id, } metadata_origin_v1 = { "id": data.origin_url_1, "metadata": metadata_v1.copy(), "indexer_configuration_id": tool_id, "mappings": [], "from_revision": data.revision_id_1, } # given storage.revision_intrinsic_metadata_add([metadata_rev_v1]) storage.origin_intrinsic_metadata_add([metadata_origin_v1]) # when actual_metadata = list( storage.origin_intrinsic_metadata_get([data.origin_url_1, "no://where"]) ) expected_metadata_v1 = [ { "id": data.origin_url_1, "metadata": metadata_v1, "tool": data.tools["swh-metadata-detector"], "from_revision": data.revision_id_1, "mappings": [], } ] assert actual_metadata == expected_metadata_v1 # given metadata_v2 = metadata_v1.copy() metadata_v2.update( {"name": "test_metadata", "author": "MG",} ) metadata_rev_v2 = metadata_rev_v1.copy() metadata_origin_v2 = metadata_origin_v1.copy() metadata_rev_v2["metadata"] = metadata_v2 metadata_origin_v2["metadata"] = metadata_v2 storage.revision_intrinsic_metadata_add([metadata_rev_v2]) storage.origin_intrinsic_metadata_add([metadata_origin_v2]) # then actual_metadata = list( storage.origin_intrinsic_metadata_get([data.origin_url_1]) ) # metadata did not change as the v2 was dropped. assert actual_metadata == expected_metadata_v1 def test_origin_intrinsic_metadata_add_update_in_place_duplicate( self, swh_indexer_storage_with_data ): storage, data = swh_indexer_storage_with_data # given tool_id = data.tools["swh-metadata-detector"]["id"] metadata_v1 = { "version": None, "name": None, } metadata_rev_v1 = { "id": data.revision_id_2, "metadata": metadata_v1, "mappings": [], "indexer_configuration_id": tool_id, } metadata_origin_v1 = { "id": data.origin_url_1, "metadata": metadata_v1.copy(), "indexer_configuration_id": tool_id, "mappings": [], "from_revision": data.revision_id_2, } # given storage.revision_intrinsic_metadata_add([metadata_rev_v1]) storage.origin_intrinsic_metadata_add([metadata_origin_v1]) # when actual_metadata = list( storage.origin_intrinsic_metadata_get([data.origin_url_1]) ) # then expected_metadata_v1 = [ { "id": data.origin_url_1, "metadata": metadata_v1, "tool": data.tools["swh-metadata-detector"], "from_revision": data.revision_id_2, "mappings": [], } ] assert actual_metadata == expected_metadata_v1 # given metadata_v2 = metadata_v1.copy() metadata_v2.update( {"name": "test_update_duplicated_metadata", "author": "MG",} ) metadata_rev_v2 = metadata_rev_v1.copy() metadata_origin_v2 = metadata_origin_v1.copy() metadata_rev_v2["metadata"] = metadata_v2 metadata_origin_v2 = { "id": data.origin_url_1, "metadata": metadata_v2.copy(), "indexer_configuration_id": tool_id, "mappings": ["npm"], "from_revision": data.revision_id_1, } storage.revision_intrinsic_metadata_add([metadata_rev_v2], conflict_update=True) storage.origin_intrinsic_metadata_add( [metadata_origin_v2], conflict_update=True ) actual_metadata = list( storage.origin_intrinsic_metadata_get([data.origin_url_1]) ) expected_metadata_v2 = [ { "id": data.origin_url_1, "metadata": metadata_v2, "tool": data.tools["swh-metadata-detector"], "from_revision": data.revision_id_1, "mappings": ["npm"], } ] # metadata did change as the v2 was used to overwrite v1 assert actual_metadata == expected_metadata_v2 def test_origin_intrinsic_metadata_add__update_in_place_deadlock( self, swh_indexer_storage_with_data ): storage, data = swh_indexer_storage_with_data # given tool_id = data.tools["swh-metadata-detector"]["id"] ids = list(range(10)) example_data1 = { "metadata": {"version": None, "name": None,}, "mappings": [], } example_data2 = { "metadata": {"version": "v1.1.1", "name": "foo",}, "mappings": [], } metadata_rev_v1 = { "id": data.revision_id_2, "metadata": {"version": None, "name": None,}, "mappings": [], "indexer_configuration_id": tool_id, } data_v1 = [ { "id": "file:///tmp/origin%d" % id_, "from_revision": data.revision_id_2, **example_data1, "indexer_configuration_id": tool_id, } for id_ in ids ] data_v2 = [ { "id": "file:///tmp/origin%d" % id_, "from_revision": data.revision_id_2, **example_data2, "indexer_configuration_id": tool_id, } for id_ in ids ] # Remove one item from each, so that both queries have to succeed for # all items to be in the DB. data_v2a = data_v2[1:] data_v2b = list(reversed(data_v2[0:-1])) # given storage.revision_intrinsic_metadata_add([metadata_rev_v1]) storage.origin_intrinsic_metadata_add(data_v1) # when origins = ["file:///tmp/origin%d" % i for i in ids] actual_data = list(storage.origin_intrinsic_metadata_get(origins)) expected_data_v1 = [ { "id": "file:///tmp/origin%d" % id_, "from_revision": data.revision_id_2, **example_data1, "tool": data.tools["swh-metadata-detector"], } for id_ in ids ] # then assert actual_data == expected_data_v1 # given def f1(): storage.origin_intrinsic_metadata_add(data_v2a, conflict_update=True) def f2(): storage.origin_intrinsic_metadata_add(data_v2b, conflict_update=True) t1 = threading.Thread(target=f1) t2 = threading.Thread(target=f2) t2.start() t1.start() t1.join() t2.join() actual_data = list(storage.origin_intrinsic_metadata_get(origins)) expected_data_v2 = [ { "id": "file:///tmp/origin%d" % id_, "from_revision": data.revision_id_2, **example_data2, "tool": data.tools["swh-metadata-detector"], } for id_ in ids ] assert len(actual_data) == len(expected_data_v2) assert sorted(actual_data, key=lambda x: x["id"]) == expected_data_v2 def test_origin_intrinsic_metadata_add__duplicate_twice( self, swh_indexer_storage_with_data ): storage, data = swh_indexer_storage_with_data # given tool_id = data.tools["swh-metadata-detector"]["id"] metadata = { "developmentStatus": None, "name": None, } metadata_rev = { "id": data.revision_id_2, "metadata": metadata, "mappings": ["mapping1"], "indexer_configuration_id": tool_id, } metadata_origin = { "id": data.origin_url_1, "metadata": metadata, "indexer_configuration_id": tool_id, "mappings": ["mapping1"], "from_revision": data.revision_id_2, } # when storage.revision_intrinsic_metadata_add([metadata_rev]) with pytest.raises(DuplicateId): storage.origin_intrinsic_metadata_add([metadata_origin, metadata_origin]) def test_origin_intrinsic_metadata_search_fulltext( self, swh_indexer_storage_with_data ): storage, data = swh_indexer_storage_with_data # given tool_id = data.tools["swh-metadata-detector"]["id"] metadata1 = { "author": "John Doe", } metadata1_rev = { "id": data.revision_id_1, "metadata": metadata1, "mappings": [], "indexer_configuration_id": tool_id, } metadata1_origin = { "id": data.origin_url_1, "metadata": metadata1, "mappings": [], "indexer_configuration_id": tool_id, "from_revision": data.revision_id_1, } metadata2 = { "author": "Jane Doe", } metadata2_rev = { "id": data.revision_id_2, "metadata": metadata2, "mappings": [], "indexer_configuration_id": tool_id, } metadata2_origin = { "id": data.origin_url_2, "metadata": metadata2, "mappings": [], "indexer_configuration_id": tool_id, "from_revision": data.revision_id_2, } # when storage.revision_intrinsic_metadata_add([metadata1_rev]) storage.origin_intrinsic_metadata_add([metadata1_origin]) storage.revision_intrinsic_metadata_add([metadata2_rev]) storage.origin_intrinsic_metadata_add([metadata2_origin]) # then search = storage.origin_intrinsic_metadata_search_fulltext assert set([res["id"] for res in search(["Doe"])]) == set( [data.origin_url_1, data.origin_url_2] ) assert [res["id"] for res in search(["John", "Doe"])] == [data.origin_url_1] assert [res["id"] for res in search(["John"])] == [data.origin_url_1] assert not list(search(["John", "Jane"])) def test_origin_intrinsic_metadata_search_fulltext_rank( self, swh_indexer_storage_with_data ): storage, data = swh_indexer_storage_with_data # given tool_id = data.tools["swh-metadata-detector"]["id"] # The following authors have "Random Person" to add some more content # to the JSON data, to work around normalization quirks when there # are few words (rank/(1+ln(nb_words)) is very sensitive to nb_words # for small values of nb_words). metadata1 = {"author": ["Random Person", "John Doe", "Jane Doe",]} metadata1_rev = { "id": data.revision_id_1, "metadata": metadata1, "mappings": [], "indexer_configuration_id": tool_id, } metadata1_origin = { "id": data.origin_url_1, "metadata": metadata1, "mappings": [], "indexer_configuration_id": tool_id, "from_revision": data.revision_id_1, } metadata2 = {"author": ["Random Person", "Jane Doe",]} metadata2_rev = { "id": data.revision_id_2, "metadata": metadata2, "mappings": [], "indexer_configuration_id": tool_id, } metadata2_origin = { "id": data.origin_url_2, "metadata": metadata2, "mappings": [], "indexer_configuration_id": tool_id, "from_revision": data.revision_id_2, } # when storage.revision_intrinsic_metadata_add([metadata1_rev]) storage.origin_intrinsic_metadata_add([metadata1_origin]) storage.revision_intrinsic_metadata_add([metadata2_rev]) storage.origin_intrinsic_metadata_add([metadata2_origin]) # then search = storage.origin_intrinsic_metadata_search_fulltext assert [res["id"] for res in search(["Doe"])] == [ data.origin_url_1, data.origin_url_2, ] assert [res["id"] for res in search(["Doe"], limit=1)] == [data.origin_url_1] assert [res["id"] for res in search(["John"])] == [data.origin_url_1] assert [res["id"] for res in search(["Jane"])] == [ data.origin_url_2, data.origin_url_1, ] assert [res["id"] for res in search(["John", "Jane"])] == [data.origin_url_1] def _fill_origin_intrinsic_metadata(self, swh_indexer_storage_with_data): storage, data = swh_indexer_storage_with_data tool1_id = data.tools["swh-metadata-detector"]["id"] tool2_id = data.tools["swh-metadata-detector2"]["id"] metadata1 = { "@context": "foo", "author": "John Doe", } metadata1_rev = { "id": data.revision_id_1, "metadata": metadata1, "mappings": ["npm"], "indexer_configuration_id": tool1_id, } metadata1_origin = { "id": data.origin_url_1, "metadata": metadata1, "mappings": ["npm"], "indexer_configuration_id": tool1_id, "from_revision": data.revision_id_1, } metadata2 = { "@context": "foo", "author": "Jane Doe", } metadata2_rev = { "id": data.revision_id_2, "metadata": metadata2, "mappings": ["npm", "gemspec"], "indexer_configuration_id": tool2_id, } metadata2_origin = { "id": data.origin_url_2, "metadata": metadata2, "mappings": ["npm", "gemspec"], "indexer_configuration_id": tool2_id, "from_revision": data.revision_id_2, } metadata3 = { "@context": "foo", } metadata3_rev = { "id": data.revision_id_3, "metadata": metadata3, "mappings": ["npm", "gemspec"], "indexer_configuration_id": tool2_id, } metadata3_origin = { "id": data.origin_url_3, "metadata": metadata3, "mappings": ["pkg-info"], "indexer_configuration_id": tool2_id, "from_revision": data.revision_id_3, } storage.revision_intrinsic_metadata_add([metadata1_rev]) storage.origin_intrinsic_metadata_add([metadata1_origin]) storage.revision_intrinsic_metadata_add([metadata2_rev]) storage.origin_intrinsic_metadata_add([metadata2_origin]) storage.revision_intrinsic_metadata_add([metadata3_rev]) storage.origin_intrinsic_metadata_add([metadata3_origin]) def test_origin_intrinsic_metadata_search_by_producer( self, swh_indexer_storage_with_data ): storage, data = swh_indexer_storage_with_data self._fill_origin_intrinsic_metadata(swh_indexer_storage_with_data) tool1 = data.tools["swh-metadata-detector"] tool2 = data.tools["swh-metadata-detector2"] endpoint = storage.origin_intrinsic_metadata_search_by_producer # test pagination # no 'page_token' param, return all origins result = endpoint(ids_only=True) assert result["origins"] == [ data.origin_url_1, data.origin_url_2, data.origin_url_3, ] assert "next_page_token" not in result # 'page_token' is < than origin_1, return everything result = endpoint(page_token=data.origin_url_1[:-1], ids_only=True) assert result["origins"] == [ data.origin_url_1, data.origin_url_2, data.origin_url_3, ] assert "next_page_token" not in result # 'page_token' is origin_3, return nothing result = endpoint(page_token=data.origin_url_3, ids_only=True) assert not result["origins"] assert "next_page_token" not in result # test limit argument result = endpoint(page_token=data.origin_url_1[:-1], limit=2, ids_only=True) assert result["origins"] == [data.origin_url_1, data.origin_url_2] assert result["next_page_token"] == result["origins"][-1] result = endpoint(page_token=data.origin_url_1, limit=2, ids_only=True) assert result["origins"] == [data.origin_url_2, data.origin_url_3] assert "next_page_token" not in result result = endpoint(page_token=data.origin_url_2, limit=2, ids_only=True) assert result["origins"] == [data.origin_url_3] assert "next_page_token" not in result # test mappings filtering result = endpoint(mappings=["npm"], ids_only=True) assert result["origins"] == [data.origin_url_1, data.origin_url_2] assert "next_page_token" not in result result = endpoint(mappings=["npm", "gemspec"], ids_only=True) assert result["origins"] == [data.origin_url_1, data.origin_url_2] assert "next_page_token" not in result result = endpoint(mappings=["gemspec"], ids_only=True) assert result["origins"] == [data.origin_url_2] assert "next_page_token" not in result result = endpoint(mappings=["pkg-info"], ids_only=True) assert result["origins"] == [data.origin_url_3] assert "next_page_token" not in result result = endpoint(mappings=["foobar"], ids_only=True) assert not result["origins"] assert "next_page_token" not in result # test pagination + mappings result = endpoint(mappings=["npm"], limit=1, ids_only=True) assert result["origins"] == [data.origin_url_1] assert result["next_page_token"] == result["origins"][-1] # test tool filtering result = endpoint(tool_ids=[tool1["id"]], ids_only=True) assert result["origins"] == [data.origin_url_1] assert "next_page_token" not in result result = endpoint(tool_ids=[tool2["id"]], ids_only=True) assert sorted(result["origins"]) == [data.origin_url_2, data.origin_url_3] assert "next_page_token" not in result result = endpoint(tool_ids=[tool1["id"], tool2["id"]], ids_only=True) assert sorted(result["origins"]) == [ data.origin_url_1, data.origin_url_2, data.origin_url_3, ] assert "next_page_token" not in result # test ids_only=False assert endpoint(mappings=["gemspec"])["origins"] == [ { "id": data.origin_url_2, "metadata": {"@context": "foo", "author": "Jane Doe",}, "mappings": ["npm", "gemspec"], "tool": tool2, "from_revision": data.revision_id_2, } ] def test_origin_intrinsic_metadata_stats(self, swh_indexer_storage_with_data): storage, data = swh_indexer_storage_with_data self._fill_origin_intrinsic_metadata(swh_indexer_storage_with_data) result = storage.origin_intrinsic_metadata_stats() assert result == { "per_mapping": { "gemspec": 1, "npm": 2, "pkg-info": 1, "codemeta": 0, "maven": 0, }, "total": 3, "non_empty": 2, } class TestIndexerStorageIndexerCondifuration: def test_indexer_configuration_add(self, swh_indexer_storage_with_data): storage, data = swh_indexer_storage_with_data tool = { "tool_name": "some-unknown-tool", "tool_version": "some-version", "tool_configuration": {"debian-package": "some-package"}, } actual_tool = storage.indexer_configuration_get(tool) assert actual_tool is None # does not exist # add it actual_tools = list(storage.indexer_configuration_add([tool])) assert len(actual_tools) == 1 actual_tool = actual_tools[0] assert actual_tool is not None # now it exists new_id = actual_tool.pop("id") assert actual_tool == tool actual_tools2 = list(storage.indexer_configuration_add([tool])) actual_tool2 = actual_tools2[0] assert actual_tool2 is not None # now it exists new_id2 = actual_tool2.pop("id") assert new_id == new_id2 assert actual_tool == actual_tool2 def test_indexer_configuration_add_multiple(self, swh_indexer_storage_with_data): storage, data = swh_indexer_storage_with_data tool = { "tool_name": "some-unknown-tool", "tool_version": "some-version", "tool_configuration": {"debian-package": "some-package"}, } actual_tools = list(storage.indexer_configuration_add([tool])) assert len(actual_tools) == 1 new_tools = [ tool, { "tool_name": "yet-another-tool", "tool_version": "version", "tool_configuration": {}, }, ] actual_tools = list(storage.indexer_configuration_add(new_tools)) assert len(actual_tools) == 2 # order not guaranteed, so we iterate over results to check for tool in actual_tools: _id = tool.pop("id") assert _id is not None assert tool in new_tools def test_indexer_configuration_get_missing(self, swh_indexer_storage_with_data): storage, data = swh_indexer_storage_with_data tool = { "tool_name": "unknown-tool", "tool_version": "3.1.0rc2-31-ga2cbb8c", "tool_configuration": {"command_line": "nomossa "}, } actual_tool = storage.indexer_configuration_get(tool) assert actual_tool is None def test_indexer_configuration_get(self, swh_indexer_storage_with_data): storage, data = swh_indexer_storage_with_data tool = { "tool_name": "nomos", "tool_version": "3.1.0rc2-31-ga2cbb8c", "tool_configuration": {"command_line": "nomossa "}, } actual_tool = storage.indexer_configuration_get(tool) assert actual_tool expected_tool = tool.copy() del actual_tool["id"] assert expected_tool == actual_tool def test_indexer_configuration_metadata_get_missing_context( self, swh_indexer_storage_with_data ): storage, data = swh_indexer_storage_with_data tool = { "tool_name": "swh-metadata-translator", "tool_version": "0.0.1", "tool_configuration": {"context": "unknown-context"}, } actual_tool = storage.indexer_configuration_get(tool) assert actual_tool is None def test_indexer_configuration_metadata_get(self, swh_indexer_storage_with_data): storage, data = swh_indexer_storage_with_data tool = { "tool_name": "swh-metadata-translator", "tool_version": "0.0.1", "tool_configuration": {"type": "local", "context": "NpmMapping"}, } storage.indexer_configuration_add([tool]) actual_tool = storage.indexer_configuration_get(tool) assert actual_tool expected_tool = tool.copy() expected_tool["id"] = actual_tool["id"] assert expected_tool == actual_tool diff --git a/swh/indexer/tests/test_fossology_license.py b/swh/indexer/tests/test_fossology_license.py index 5cee306..8d56b9e 100644 --- a/swh/indexer/tests/test_fossology_license.py +++ b/swh/indexer/tests/test_fossology_license.py @@ -1,147 +1,147 @@ # Copyright (C) 2017-2018 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information from typing import Any, Dict import unittest from unittest.mock import patch import pytest from swh.indexer import fossology_license from swh.indexer.fossology_license import ( FossologyLicenseIndexer, FossologyLicensePartitionIndexer, compute_license, ) from swh.indexer.tests.utils import ( BASE_TEST_CONFIG, SHA1_TO_LICENSES, CommonContentIndexerPartitionTest, CommonContentIndexerTest, fill_obj_storage, fill_storage, filter_dict, ) class BasicTest(unittest.TestCase): @patch("swh.indexer.fossology_license.subprocess") def test_compute_license(self, mock_subprocess): """Computing licenses from a raw content should return results """ for path, intermediary_result, output in [ (b"some/path", None, []), (b"some/path/2", [], []), (b"other/path", " contains license(s) GPL,AGPL", ["GPL", "AGPL"]), ]: mock_subprocess.check_output.return_value = intermediary_result actual_result = compute_license(path) self.assertEqual(actual_result, {"licenses": output, "path": path,}) def mock_compute_license(path): """path is the content identifier """ if isinstance(id, bytes): path = path.decode("utf-8") # path is something like /tmp/tmpXXX/ so we keep only the sha1 part path = path.split("/")[-1] return {"licenses": SHA1_TO_LICENSES.get(path)} CONFIG = { **BASE_TEST_CONFIG, "workdir": "/tmp", "tools": { "name": "nomos", "version": "3.1.0rc2-31-ga2cbb8c", "configuration": {"command_line": "nomossa ",}, }, } # type: Dict[str, Any] RANGE_CONFIG = dict(list(CONFIG.items()) + [("write_batch_size", 100)]) class TestFossologyLicenseIndexer(CommonContentIndexerTest, unittest.TestCase): """Language indexer test scenarios: - Known sha1s in the input list have their data indexed - Unknown sha1 in the input list are not indexed """ def get_indexer_results(self, ids): yield from self.idx_storage.content_fossology_license_get(ids) def setUp(self): super().setUp() # replace actual license computation with a mock self.orig_compute_license = fossology_license.compute_license fossology_license.compute_license = mock_compute_license self.indexer = FossologyLicenseIndexer(CONFIG) self.indexer.catch_exceptions = False self.idx_storage = self.indexer.idx_storage fill_storage(self.indexer.storage) fill_obj_storage(self.indexer.objstorage) self.id0 = "01c9379dfc33803963d07c1ccc748d3fe4c96bb5" self.id1 = "688a5ef812c53907562fe379d4b3851e69c7cb15" self.id2 = "da39a3ee5e6b4b0d3255bfef95601890afd80709" # empty content tool = {k.replace("tool_", ""): v for (k, v) in self.indexer.tool.items()} # then self.expected_results = { self.id0: {"tool": tool, "licenses": SHA1_TO_LICENSES[self.id0],}, self.id1: {"tool": tool, "licenses": SHA1_TO_LICENSES[self.id1],}, - self.id2: {"tool": tool, "licenses": SHA1_TO_LICENSES[self.id2],}, + self.id2: None, } def tearDown(self): super().tearDown() fossology_license.compute_license = self.orig_compute_license class TestFossologyLicensePartitionIndexer( CommonContentIndexerPartitionTest, unittest.TestCase ): """Range Fossology License Indexer tests. - new data within range are indexed - no data outside a range are indexed - with filtering existing indexed data prior to compute new index - without filtering existing indexed data prior to compute new index """ def setUp(self): super().setUp() # replace actual license computation with a mock self.orig_compute_license = fossology_license.compute_license fossology_license.compute_license = mock_compute_license self.indexer = FossologyLicensePartitionIndexer(config=RANGE_CONFIG) self.indexer.catch_exceptions = False fill_storage(self.indexer.storage) fill_obj_storage(self.indexer.objstorage) def tearDown(self): super().tearDown() fossology_license.compute_license = self.orig_compute_license def test_fossology_w_no_tool(): with pytest.raises(ValueError): FossologyLicenseIndexer(config=filter_dict(CONFIG, "tools")) def test_fossology_range_w_no_tool(): with pytest.raises(ValueError): FossologyLicensePartitionIndexer(config=filter_dict(RANGE_CONFIG, "tools")) diff --git a/swh/indexer/tests/utils.py b/swh/indexer/tests/utils.py index 2f41a85..52b2af5 100644 --- a/swh/indexer/tests/utils.py +++ b/swh/indexer/tests/utils.py @@ -1,761 +1,764 @@ # Copyright (C) 2017-2020 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import abc import functools from typing import Any, Dict import unittest from hypothesis import strategies from swh.core.api.classes import stream_results from swh.indexer.storage import INDEXER_CFG_KEY from swh.model import hashutil from swh.model.hashutil import hash_to_bytes from swh.model.model import ( Content, Directory, DirectoryEntry, Origin, OriginVisit, OriginVisitStatus, Person, Revision, RevisionType, Snapshot, SnapshotBranch, TargetType, Timestamp, TimestampWithTimezone, ) from swh.storage.utils import now BASE_TEST_CONFIG: Dict[str, Dict[str, Any]] = { "storage": {"cls": "memory"}, "objstorage": {"cls": "memory", "args": {},}, INDEXER_CFG_KEY: {"cls": "memory", "args": {},}, } ORIGINS = [ Origin(url="https://github.com/SoftwareHeritage/swh-storage"), Origin(url="rsync://ftp.gnu.org/gnu/3dldf"), Origin(url="https://forge.softwareheritage.org/source/jesuisgpl/"), Origin(url="https://pypi.org/project/limnoria/"), Origin(url="http://0-512-md.googlecode.com/svn/"), Origin(url="https://github.com/librariesio/yarn-parser"), Origin(url="https://github.com/librariesio/yarn-parser.git"), ] ORIGIN_VISITS = [ {"type": "git", "origin": ORIGINS[0].url}, {"type": "ftp", "origin": ORIGINS[1].url}, {"type": "deposit", "origin": ORIGINS[2].url}, {"type": "pypi", "origin": ORIGINS[3].url}, {"type": "svn", "origin": ORIGINS[4].url}, {"type": "git", "origin": ORIGINS[5].url}, {"type": "git", "origin": ORIGINS[6].url}, ] DIRECTORY = Directory( id=hash_to_bytes("34f335a750111ca0a8b64d8034faec9eedc396be"), entries=( DirectoryEntry( name=b"index.js", type="file", target=hash_to_bytes("01c9379dfc33803963d07c1ccc748d3fe4c96bb5"), perms=0o100644, ), DirectoryEntry( name=b"package.json", type="file", target=hash_to_bytes("26a9f72a7c87cc9205725cfd879f514ff4f3d8d5"), perms=0o100644, ), DirectoryEntry( name=b".github", type="dir", target=Directory(entries=()).id, perms=0o040000, ), ), ) DIRECTORY2 = Directory( id=b"\xf8zz\xa1\x12`<1$\xfav\xf9\x01\xfd5\x85F`\xf2\xb6", entries=( DirectoryEntry( name=b"package.json", type="file", target=hash_to_bytes("f5305243b3ce7ef8dc864ebc73794da304025beb"), perms=0o100644, ), ), ) REVISION = Revision( id=hash_to_bytes("c6201cb1b9b9df9a7542f9665c3b5dfab85e9775"), message=b"Improve search functionality", author=Person( name=b"Andrew Nesbitt", fullname=b"Andrew Nesbitt ", email=b"andrewnez@gmail.com", ), committer=Person( name=b"Andrew Nesbitt", fullname=b"Andrew Nesbitt ", email=b"andrewnez@gmail.com", ), committer_date=TimestampWithTimezone( timestamp=Timestamp(seconds=1380883849, microseconds=0,), offset=120, negative_utc=False, ), type=RevisionType.GIT, synthetic=False, date=TimestampWithTimezone( timestamp=Timestamp(seconds=1487596456, microseconds=0,), offset=0, negative_utc=False, ), directory=DIRECTORY2.id, parents=(), ) REVISIONS = [REVISION] SNAPSHOTS = [ Snapshot( id=hash_to_bytes("a50fde72265343b7d28cecf6db20d98a81d21965"), branches={ b"refs/heads/add-revision-origin-cache": SnapshotBranch( target=b'L[\xce\x1c\x88\x8eF\t\xf1"\x19\x1e\xfb\xc0s\xe7/\xe9l\x1e', target_type=TargetType.REVISION, ), b"refs/head/master": SnapshotBranch( target=b"8K\x12\x00d\x03\xcc\xe4]bS\xe3\x8f{\xd7}\xac\xefrm", target_type=TargetType.REVISION, ), b"HEAD": SnapshotBranch( target=b"refs/head/master", target_type=TargetType.ALIAS ), b"refs/tags/v0.0.103": SnapshotBranch( target=b'\xb6"Im{\xfdLb\xb0\x94N\xea\x96m\x13x\x88+\x0f\xdd', target_type=TargetType.RELEASE, ), }, ), Snapshot( id=hash_to_bytes("2c67f69a416bca4e1f3fcd848c588fab88ad0642"), branches={ b"3DLDF-1.1.4.tar.gz": SnapshotBranch( target=b'dJ\xfb\x1c\x91\xf4\x82B%]6\xa2\x90|\xd3\xfc"G\x99\x11', target_type=TargetType.REVISION, ), b"3DLDF-2.0.2.tar.gz": SnapshotBranch( target=b"\xb6\x0e\xe7\x9e9\xac\xaa\x19\x9e=\xd1\xc5\x00\\\xc6\xfc\xe0\xa6\xb4V", # noqa target_type=TargetType.REVISION, ), b"3DLDF-2.0.3-examples.tar.gz": SnapshotBranch( target=b"!H\x19\xc0\xee\x82-\x12F1\xbd\x97\xfe\xadZ\x80\x80\xc1\x83\xff", # noqa target_type=TargetType.REVISION, ), b"3DLDF-2.0.3.tar.gz": SnapshotBranch( target=b"\x8e\xa9\x8e/\xea}\x9feF\xf4\x9f\xfd\xee\xcc\x1a\xb4`\x8c\x8by", # noqa target_type=TargetType.REVISION, ), b"3DLDF-2.0.tar.gz": SnapshotBranch( target=b"F6*\xff(?\x19a\xef\xb6\xc2\x1fv$S\xe3G\xd3\xd1m", target_type=TargetType.REVISION, ), }, ), Snapshot( id=hash_to_bytes("68c0d26104d47e278dd6be07ed61fafb561d0d20"), branches={ b"master": SnapshotBranch( target=b"\xe7n\xa4\x9c\x9f\xfb\xb7\xf76\x11\x08{\xa6\xe9\x99\xb1\x9e]q\xeb", # noqa target_type=TargetType.REVISION, ) }, ), Snapshot( id=hash_to_bytes("f255245269e15fc99d284affd79f766668de0b67"), branches={ b"HEAD": SnapshotBranch( target=b"releases/2018.09.09", target_type=TargetType.ALIAS ), b"releases/2018.09.01": SnapshotBranch( target=b"<\xee1(\xe8\x8d_\xc1\xc9\xa6rT\xf1\x1d\xbb\xdfF\xfdw\xcf", target_type=TargetType.REVISION, ), b"releases/2018.09.09": SnapshotBranch( target=b"\x83\xb9\xb6\xc7\x05\xb1%\xd0\xfem\xd8kA\x10\x9d\xc5\xfa2\xf8t", # noqa target_type=TargetType.REVISION, ), }, ), Snapshot( id=hash_to_bytes("a1a28c0ab387a8f9e0618cb705eab81fc448f473"), branches={ b"master": SnapshotBranch( target=b"\xe4?r\xe1,\x88\xab\xec\xe7\x9a\x87\xb8\xc9\xad#.\x1bw=\x18", target_type=TargetType.REVISION, ) }, ), Snapshot( id=hash_to_bytes("bb4fd3a836930ce629d912864319637040ff3040"), branches={ b"HEAD": SnapshotBranch( target=REVISION.id, target_type=TargetType.REVISION, ) }, ), Snapshot( id=hash_to_bytes("bb4fd3a836930ce629d912864319637040ff3040"), branches={ b"HEAD": SnapshotBranch( target=REVISION.id, target_type=TargetType.REVISION, ) }, ), ] SHA1_TO_LICENSES = { "01c9379dfc33803963d07c1ccc748d3fe4c96bb5": ["GPL"], "02fb2c89e14f7fab46701478c83779c7beb7b069": ["Apache2.0"], "103bc087db1d26afc3a0283f38663d081e9b01e6": ["MIT"], "688a5ef812c53907562fe379d4b3851e69c7cb15": ["AGPL"], "da39a3ee5e6b4b0d3255bfef95601890afd80709": [], } SHA1_TO_CTAGS = { "01c9379dfc33803963d07c1ccc748d3fe4c96bb5": [ {"name": "foo", "kind": "str", "line": 10, "lang": "bar",} ], "d4c647f0fc257591cc9ba1722484229780d1c607": [ {"name": "let", "kind": "int", "line": 100, "lang": "haskell",} ], "688a5ef812c53907562fe379d4b3851e69c7cb15": [ {"name": "symbol", "kind": "float", "line": 99, "lang": "python",} ], } OBJ_STORAGE_DATA = { "01c9379dfc33803963d07c1ccc748d3fe4c96bb5": b"this is some text", "688a5ef812c53907562fe379d4b3851e69c7cb15": b"another text", "8986af901dd2043044ce8f0d8fc039153641cf17": b"yet another text", "02fb2c89e14f7fab46701478c83779c7beb7b069": b""" import unittest import logging from swh.indexer.mimetype import MimetypeIndexer from swh.indexer.tests.test_utils import MockObjStorage class MockStorage(): def content_mimetype_add(self, mimetypes): self.state = mimetypes self.conflict_update = conflict_update def indexer_configuration_add(self, tools): return [{ 'id': 10, }] """, "103bc087db1d26afc3a0283f38663d081e9b01e6": b""" #ifndef __AVL__ #define __AVL__ typedef struct _avl_tree avl_tree; typedef struct _data_t { int content; } data_t; """, "93666f74f1cf635c8c8ac118879da6ec5623c410": b""" (should 'pygments (recognize 'lisp 'easily)) """, "26a9f72a7c87cc9205725cfd879f514ff4f3d8d5": b""" { "name": "test_metadata", "version": "0.0.1", "description": "Simple package.json test for indexer", "repository": { "type": "git", "url": "https://github.com/moranegg/metadata_test" } } """, "d4c647f0fc257591cc9ba1722484229780d1c607": b""" { "version": "5.0.3", "name": "npm", "description": "a package manager for JavaScript", "keywords": [ "install", "modules", "package manager", "package.json" ], "preferGlobal": true, "config": { "publishtest": false }, "homepage": "https://docs.npmjs.com/", "author": "Isaac Z. Schlueter (http://blog.izs.me)", "repository": { "type": "git", "url": "https://github.com/npm/npm" }, "bugs": { "url": "https://github.com/npm/npm/issues" }, "dependencies": { "JSONStream": "~1.3.1", "abbrev": "~1.1.0", "ansi-regex": "~2.1.1", "ansicolors": "~0.3.2", "ansistyles": "~0.1.3" }, "devDependencies": { "tacks": "~1.2.6", "tap": "~10.3.2" }, "license": "Artistic-2.0" } """, "a7ab314d8a11d2c93e3dcf528ca294e7b431c449": b""" """, "da39a3ee5e6b4b0d3255bfef95601890afd80709": b"", # was 626364 / b'bcd' "e3e40fee6ff8a52f06c3b428bfe7c0ed2ef56e92": b"unimportant content for bcd", # was 636465 / b'cde' now yarn-parser package.json "f5305243b3ce7ef8dc864ebc73794da304025beb": b""" { "name": "yarn-parser", "version": "1.0.0", "description": "Tiny web service for parsing yarn.lock files", "main": "index.js", "scripts": { "start": "node index.js", "test": "mocha" }, "engines": { "node": "9.8.0" }, "repository": { "type": "git", "url": "git+https://github.com/librariesio/yarn-parser.git" }, "keywords": [ "yarn", "parse", "lock", "dependencies" ], "author": "Andrew Nesbitt", "license": "AGPL-3.0", "bugs": { "url": "https://github.com/librariesio/yarn-parser/issues" }, "homepage": "https://github.com/librariesio/yarn-parser#readme", "dependencies": { "@yarnpkg/lockfile": "^1.0.0", "body-parser": "^1.15.2", "express": "^4.14.0" }, "devDependencies": { "chai": "^4.1.2", "mocha": "^5.2.0", "request": "^2.87.0", "test": "^0.6.0" } } """, } YARN_PARSER_METADATA = { "@context": "https://doi.org/10.5063/schema/codemeta-2.0", "url": "https://github.com/librariesio/yarn-parser#readme", "codeRepository": "git+git+https://github.com/librariesio/yarn-parser.git", "author": [{"type": "Person", "name": "Andrew Nesbitt"}], "license": "https://spdx.org/licenses/AGPL-3.0", "version": "1.0.0", "description": "Tiny web service for parsing yarn.lock files", "issueTracker": "https://github.com/librariesio/yarn-parser/issues", "name": "yarn-parser", "keywords": ["yarn", "parse", "lock", "dependencies"], "type": "SoftwareSourceCode", } json_dict_keys = strategies.one_of( strategies.characters(), strategies.just("type"), strategies.just("url"), strategies.just("name"), strategies.just("email"), strategies.just("@id"), strategies.just("@context"), strategies.just("repository"), strategies.just("license"), strategies.just("repositories"), strategies.just("licenses"), ) """Hypothesis strategy that generates strings, with an emphasis on those that are often used as dictionary keys in metadata files.""" generic_json_document = strategies.recursive( strategies.none() | strategies.booleans() | strategies.floats() | strategies.characters(), lambda children: ( strategies.lists(children, min_size=1) | strategies.dictionaries(json_dict_keys, children, min_size=1) ), ) """Hypothesis strategy that generates possible values for values of JSON metadata files.""" def json_document_strategy(keys=None): """Generates an hypothesis strategy that generates metadata files for a JSON-based format that uses the given keys.""" if keys is None: keys = strategies.characters() else: keys = strategies.one_of(map(strategies.just, keys)) return strategies.dictionaries(keys, generic_json_document, min_size=1) def _tree_to_xml(root, xmlns, data): def encode(s): "Skips unpaired surrogates generated by json_document_strategy" return s.encode("utf8", "replace") def to_xml(data, indent=b" "): if data is None: return b"" elif isinstance(data, (bool, str, int, float)): return indent + encode(str(data)) elif isinstance(data, list): return b"\n".join(to_xml(v, indent=indent) for v in data) elif isinstance(data, dict): lines = [] for (key, value) in data.items(): lines.append(indent + encode("<{}>".format(key))) lines.append(to_xml(value, indent=indent + b" ")) lines.append(indent + encode("".format(key))) return b"\n".join(lines) else: raise TypeError(data) return b"\n".join( [ '<{} xmlns="{}">'.format(root, xmlns).encode(), to_xml(data), "".format(root).encode(), ] ) class TreeToXmlTest(unittest.TestCase): def test_leaves(self): self.assertEqual( _tree_to_xml("root", "http://example.com", None), b'\n\n', ) self.assertEqual( _tree_to_xml("root", "http://example.com", True), b'\n True\n', ) self.assertEqual( _tree_to_xml("root", "http://example.com", "abc"), b'\n abc\n', ) self.assertEqual( _tree_to_xml("root", "http://example.com", 42), b'\n 42\n', ) self.assertEqual( _tree_to_xml("root", "http://example.com", 3.14), b'\n 3.14\n', ) def test_dict(self): self.assertIn( _tree_to_xml("root", "http://example.com", {"foo": "bar", "baz": "qux"}), [ b'\n' b" \n bar\n \n" b" \n qux\n \n" b"", b'\n' b" \n qux\n \n" b" \n bar\n \n" b"", ], ) def test_list(self): self.assertEqual( _tree_to_xml( "root", "http://example.com", [{"foo": "bar"}, {"foo": "baz"},] ), b'\n' b" \n bar\n \n" b" \n baz\n \n" b"", ) def xml_document_strategy(keys, root, xmlns): """Generates an hypothesis strategy that generates metadata files for an XML format that uses the given keys.""" return strategies.builds( functools.partial(_tree_to_xml, root, xmlns), json_document_strategy(keys) ) def filter_dict(d, keys): "return a copy of the dict with keys deleted" if not isinstance(keys, (list, tuple)): keys = (keys,) return dict((k, v) for (k, v) in d.items() if k not in keys) def fill_obj_storage(obj_storage): """Add some content in an object storage.""" for (obj_id, content) in OBJ_STORAGE_DATA.items(): obj_storage.add(content, obj_id=hash_to_bytes(obj_id)) def fill_storage(storage): storage.origin_add(ORIGINS) storage.directory_add([DIRECTORY, DIRECTORY2]) storage.revision_add(REVISIONS) storage.snapshot_add(SNAPSHOTS) for visit, snapshot in zip(ORIGIN_VISITS, SNAPSHOTS): assert snapshot.id is not None visit = storage.origin_visit_add( [OriginVisit(origin=visit["origin"], date=now(), type=visit["type"])] )[0] visit_status = OriginVisitStatus( origin=visit.origin, visit=visit.visit, date=now(), status="full", snapshot=snapshot.id, ) storage.origin_visit_status_add([visit_status]) contents = [] for (obj_id, content) in OBJ_STORAGE_DATA.items(): content_hashes = hashutil.MultiHash.from_data(content).digest() contents.append( Content( data=content, length=len(content), status="visible", sha1=hash_to_bytes(obj_id), sha1_git=hash_to_bytes(obj_id), sha256=content_hashes["sha256"], blake2s256=content_hashes["blake2s256"], ) ) storage.content_add(contents) class CommonContentIndexerTest(metaclass=abc.ABCMeta): legacy_get_format = False """True if and only if the tested indexer uses the legacy format. see: https://forge.softwareheritage.org/T1433 """ def get_indexer_results(self, ids): """Override this for indexers that don't have a mock storage.""" return self.indexer.idx_storage.state def assert_legacy_results_ok(self, sha1s, expected_results=None): # XXX old format, remove this when all endpoints are # updated to the new one # see: https://forge.softwareheritage.org/T1433 sha1s = [ sha1 if isinstance(sha1, bytes) else hash_to_bytes(sha1) for sha1 in sha1s ] actual_results = list(self.get_indexer_results(sha1s)) if expected_results is None: expected_results = self.expected_results self.assertEqual( len(expected_results), len(actual_results), (expected_results, actual_results), ) for indexed_data in actual_results: _id = indexed_data["id"] expected_data = expected_results[hashutil.hash_to_hex(_id)].copy() expected_data["id"] = _id self.assertEqual(indexed_data, expected_data) def assert_results_ok(self, sha1s, expected_results=None): if self.legacy_get_format: self.assert_legacy_results_ok(sha1s, expected_results) return sha1s = [ sha1 if isinstance(sha1, bytes) else hash_to_bytes(sha1) for sha1 in sha1s ] actual_results = list(self.get_indexer_results(sha1s)) if expected_results is None: expected_results = self.expected_results self.assertEqual( - len(expected_results), - len(actual_results), + sum(res is not None for res in expected_results.values()), + sum(sum(map(len, res.values())) for res in actual_results), (expected_results, actual_results), ) for indexed_data in actual_results: (_id, indexed_data) = list(indexed_data.items())[0] - expected_data = expected_results[hashutil.hash_to_hex(_id)].copy() - expected_data = [expected_data] - self.assertEqual(indexed_data, expected_data) + if expected_results.get(hashutil.hash_to_hex(_id)) is None: + self.assertEqual(indexed_data, []) + else: + expected_data = expected_results[hashutil.hash_to_hex(_id)].copy() + expected_data = [expected_data] + self.assertEqual(indexed_data, expected_data) def test_index(self): """Known sha1 have their data indexed """ sha1s = [self.id0, self.id1, self.id2] # when self.indexer.run(sha1s, policy_update="update-dups") self.assert_results_ok(sha1s) # 2nd pass self.indexer.run(sha1s, policy_update="ignore-dups") self.assert_results_ok(sha1s) def test_index_one_unknown_sha1(self): """Unknown sha1 are not indexed""" sha1s = [ self.id1, "799a5ef812c53907562fe379d4b3851e69c7cb15", # unknown "800a5ef812c53907562fe379d4b3851e69c7cb15", ] # unknown # when self.indexer.run(sha1s, policy_update="update-dups") # then expected_results = { k: v for k, v in self.expected_results.items() if k in sha1s } self.assert_results_ok(sha1s, expected_results) class CommonContentIndexerPartitionTest: """Allows to factorize tests on range indexer. """ def setUp(self): self.contents = sorted(OBJ_STORAGE_DATA) def assert_results_ok(self, partition_id, nb_partitions, actual_results): expected_ids = [ c.sha1 for c in stream_results( self.indexer.storage.content_get_partition, partition_id=partition_id, nb_partitions=nb_partitions, ) ] actual_results = list(actual_results) for indexed_data in actual_results: _id = indexed_data["id"] assert isinstance(_id, bytes) assert _id in expected_ids _tool_id = indexed_data["indexer_configuration_id"] assert _tool_id == self.indexer.tool["id"] def test__index_contents(self): """Indexing contents without existing data results in indexed data """ partition_id = 0 nb_partitions = 4 actual_results = list( self.indexer._index_contents(partition_id, nb_partitions, indexed={}) ) self.assert_results_ok(partition_id, nb_partitions, actual_results) def test__index_contents_with_indexed_data(self): """Indexing contents with existing data results in less indexed data """ partition_id = 3 nb_partitions = 4 # first pass actual_results = list( self.indexer._index_contents(partition_id, nb_partitions, indexed={}) ) self.assert_results_ok(partition_id, nb_partitions, actual_results) indexed_ids = set(res["id"] for res in actual_results) actual_results = list( self.indexer._index_contents( partition_id, nb_partitions, indexed=indexed_ids ) ) # already indexed, so nothing new assert actual_results == [] def test_generate_content_get(self): """Optimal indexing should result in indexed data """ partition_id = 0 nb_partitions = 4 actual_results = self.indexer.run( partition_id, nb_partitions, skip_existing=False ) assert actual_results == {"status": "uneventful"} # why? def test_generate_content_get_no_result(self): """No result indexed returns False""" actual_results = self.indexer.run(0, 0, incremental=False) assert actual_results == {"status": "uneventful"}