swh.indexer.indexer module#
- swh.indexer.indexer.write_to_temp(filename: str, data: bytes, working_directory: str) Iterator[str][source]#
Write the sha1’s content in a temporary file.
- Parameters:
filename – one of sha1’s many filenames
data – the sha1’s content to write in temporary file
working_directory – the directory into which the file is written
- Returns:
The path to the temporary file created. That file is filled in with the raw content’s data.
- class swh.indexer.indexer.TId#
type of the ids of index()ed objects.
alias of TypeVar(‘TId’)
- class swh.indexer.indexer.TData#
type of the objects passed to index().
alias of TypeVar(‘TData’)
- class swh.indexer.indexer.TResult#
return type of index()
alias of TypeVar(‘TResult’)
- class swh.indexer.indexer.BaseIndexer(config=None, **kw)[source]#
Bases:
Generic[TId,TData,TResult]Base class for indexers to inherit from.
The main entry point is the
run()function which is in charge of triggering the computations on the batch dict/ids received.Indexers can:
filter out ids whose data has already been indexed.
retrieve ids data from storage or objstorage
index this data depending on the object and store the result in storage.
Indexers are kafka journal client so they consumes specific topics. Topics will be inferred from the required class attribute “object_types”.
To implement a new object type indexer, inherit from the BaseIndexer and implement indexing:
run():object_ids are different depending on object. For example: sha1 for content, sha1_git for revision, directory, release, and id for origin
To implement a new concrete indexer, inherit from the object level classes:
ContentIndexer,DirectoryIndexer,OriginIndexer.Then you need to implement the following functions:
filter():filter out data already indexed (in storage).
index_object():compute index on id with data (retrieved from the storage or the objstorage by the id key) and return the resulting index computation.
persist_index_computations():persist the results of multiple index computations in the storage.
The new indexer implementation can also override the following functions:
prepare():Configuration preparation for the indexer. When overriding, this must call the super().prepare() instruction.
check():Configuration check for the indexer. When overriding, this must call the super().check() instruction.
register_tools():This should return a dict of the tool(s) to use when indexing or filtering.
Prepare and check that the indexer is ready to run.
- USE_TOOLS = True#
- catch_exceptions = True#
Prevents exceptions in index() from raising too high. Set to False in tests to properly catch all exceptions.
- storage: StorageInterface#
- idx_storage: IndexerStorageInterface#
- prepare(**kw) None[source]#
Prepare the indexer’s needed runtime configuration. Without this step, the indexer cannot possibly run.
- check() None[source]#
Check the indexer’s configuration is ok before proceeding. If ok, does nothing. If not raise error.
- register_tools(tools: Dict[str, Any] | List[Dict[str, Any]]) List[Dict[str, Any]][source]#
Permit to register tools to the storage.
Add a sensible default which can be overridden if not sufficient. (For now, all indexers use only one tool)
Expects the self.config[‘tools’] property to be set with one or more tools.
- Parameters:
tools – Either a dict or a list of dict.
- Returns:
List of dicts with additional id key.
- Return type:
- Raises:
ValueError – if not a list nor a dict.
- index(id: TId, data: TData | None, **kwargs) List[TResult][source]#
Index computation for the id and associated raw data.
- Parameters:
id – identifier or Dict object
data – id’s data from storage or objstorage depending on object type
- Returns:
a dict that makes sense for the
persist_index_computations()method.- Return type:
- class swh.indexer.indexer.ContentIndexer(config=None, **kw)[source]#
Bases:
BaseIndexer[HashDict,bytes,TResult],Generic[TResult]A content indexer working on the journal (method process_journal_objects) or on a list of ids directly (method run).
Note:
ContentIndexeris not an instantiable object. To use it, one should inherit from this class and override the methods mentioned in theBaseIndexerclass.Prepare and check that the indexer is ready to run.
- run(objects: List[Dict], **kwargs) Tuple[Dict, List][source]#
Given a list of ids:
retrieve the content from the storage
execute the indexing computations
store the results
- Parameters:
objects – Objects read from a kafka topics
**kwargs – passed to the index method
- Returns:
A summary Dict of the task’s status
- class swh.indexer.indexer.OriginIndexer(config=None, **kw)[source]#
Bases:
BaseIndexer[str,None,TResult],Generic[TResult]An object type indexer, inherits from the
BaseIndexerand implements Origin indexing using the run methodNote: the
OriginIndexeris not an instantiable object. To use it in another context one should inherit from this class and override the methods mentioned in theBaseIndexerclass.Implementation wise, the default behavior of this class is to consume origin_visit_status objects. Override the
process_journal_objects()if you need to consume more topics.Prepare and check that the indexer is ready to run.
- filter_objects(objects: Iterable[Dict]) Iterator[Dict][source]#
Index origin on origin visit status with status ‘full’.
- map_objects_to_typed_objects(objects: Iterable[Dict]) Iterator[Origin][source]#
Transform iterable of objects into an Origin iterator.
- run(objects: List[Dict], **kwargs) Tuple[Dict, List][source]#
Given a list of dict representing either origin or origin visit status:
retrieve the corresponding origins from storage
execute the indexing computations on those origins
finally store the results
return a summary of such computations
- Parameters:
objects – list of origin visit status read from a kafka topics.
**kwargs – passed to the index method
- Returns:
Tuple of summary and indexation results
- class swh.indexer.indexer.DirectoryIndexer(config=None, **kw)[source]#
Bases:
BaseIndexer[bytes,Directory,TResult],Generic[TResult]An object type indexer, inherits from the
BaseIndexerand implements Directory indexing using the run methodNote: the
DirectoryIndexeris not an instantiable object. To use it in another context one should inherit from this class and override the methods mentioned in theBaseIndexerclass.Prepare and check that the indexer is ready to run.