-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- adds a new uow to commit a parent record and bulk index all records and drafts
- Loading branch information
Showing
7 changed files
with
259 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2023 CERN. | ||
# | ||
# Invenio-Drafts-Resources is free software; you can redistribute it and/or | ||
# modify it under the terms of the MIT License; see LICENSE file for more | ||
# details. | ||
|
||
"""Unit of work.""" | ||
|
||
from invenio_records_resources.services.uow import RecordCommitOp | ||
|
||
|
||
class ParentRecordCommitOp(RecordCommitOp): | ||
"""Parent record commit operation, bulk indexing records and drafts.""" | ||
|
||
def __init__( | ||
self, | ||
parent, | ||
indexer_context=None, | ||
): | ||
"""Initialize the parent record commit operation. | ||
:params indexer_context: a dict containing record/draft cls and indexers, | ||
or service. Expected keys: `record_cls, draft_cls, indexer, draft_indexer`. | ||
`None` if indexing disabled. | ||
""" | ||
super().__init__(parent, indexer=None) | ||
self._indexer_context = indexer_context | ||
if indexer_context: | ||
_service = indexer_context.get("service", None) | ||
if _service is not None: | ||
self._record_cls = getattr(_service, "record_cls") | ||
self._draft_cls = getattr(_service, "draft_cls") | ||
self._record_indexer = getattr(_service, "indexer") | ||
self._draft_indexer = getattr(_service, "draft_indexer") | ||
else: | ||
self._record_cls = indexer_context["record_cls"] | ||
self._draft_cls = indexer_context["draft_cls"] | ||
self._record_indexer = indexer_context["indexer"] | ||
self._draft_indexer = indexer_context["draft_indexer"] | ||
|
||
def _get_siblings(self): | ||
"""Get all record and draft versions by parent. | ||
This operation might be slow when a record has a high number of versions and drafts. | ||
However, given that often versions should be re-indexed as soon as possible when the parent | ||
is committed, the fetching operation is done synchronously. | ||
""" | ||
records_ids = self._record_cls.get_records_by_parent( | ||
self._record, with_deleted=False, ids_only=True | ||
) | ||
drafts_ids = self._draft_cls.get_records_by_parent( | ||
self._record, with_deleted=False, ids_only=True | ||
) | ||
return records_ids, drafts_ids | ||
|
||
def on_commit(self, uow): | ||
"""No commit operation.""" | ||
pass | ||
|
||
def on_post_commit(self, uow): | ||
"""Bulk index as last operation.""" | ||
if self._indexer_context is not None: | ||
records_ids, drafts_ids = self._get_siblings() | ||
if records_ids: | ||
self._record_indexer.bulk_index(records_ids) | ||
if drafts_ids: | ||
self._draft_indexer.bulk_index(drafts_ids) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.