diff --git a/karp-backend/src/karp/entry_commands.py b/karp-backend/src/karp/entry_commands.py index 87f275fe..4363fa78 100644 --- a/karp-backend/src/karp/entry_commands.py +++ b/karp-backend/src/karp/entry_commands.py @@ -5,7 +5,7 @@ from karp.lex_core.value_objects import unique_id from karp.search.generic_resources import GenericResourceViews from karp.search_infrastructure import GenericEntryTransformer -from karp.search_infrastructure.repositories.es6_indicies import Es6IndexUnitOfWork +from karp.search_infrastructure.repositories.es6_indicies import Es6Index from karp.timings import utc_now @@ -13,12 +13,12 @@ class EntryCommands: def __init__( self, resource_uow, - index_uow: Es6IndexUnitOfWork, + index: Es6Index, entry_transformer: GenericEntryTransformer, resource_views: GenericResourceViews, ): self.resource_uow: ResourceUnitOfWork = resource_uow - self.index_uow = index_uow + self.index = index self.entry_transformer = entry_transformer self.resource_views = resource_views @@ -183,40 +183,34 @@ def delete_entry(self, resource_id, _id, user, version, message="Entry deleted") self._entry_deleted_handler(entry) def _entry_added_handler(self, entry): - with self.index_uow as uw: - entry_dto = EntryDto( - id=entry.id, - resource=entry.resource_id, - entry=entry.body, - message=entry.message or "", - lastModified=entry.last_modified, - lastModifiedBy=entry.last_modified_by, - version=1, - ) - uw.repo.add_entries( - entry.resource_id, - [self.entry_transformer.transform(entry.resource_id, entry_dto)], - ) - uw.commit() + entry_dto = EntryDto( + id=entry.id, + resource=entry.resource_id, + entry=entry.body, + message=entry.message or "", + lastModified=entry.last_modified, + lastModifiedBy=entry.last_modified_by, + version=1, + ) + self.index.add_entries( + entry.resource_id, + [self.entry_transformer.transform(entry.resource_id, entry_dto)], + ) def _entry_updated_handler(self, entry): - with self.index_uow as uw: - entry_dto = EntryDto( - id=entry.id, - resource=entry.resource_id, - entry=entry.body, - message=entry.message, - lastModified=entry.last_modified, - lastModifiedBy=entry.last_modified_by, - version=entry.version, - ) - uw.repo.add_entries( - entry.resource_id, - [self.entry_transformer.transform(entry.resource_id, entry_dto)], - ) - uw.commit() + entry_dto = EntryDto( + id=entry.id, + resource=entry.resource_id, + entry=entry.body, + message=entry.message, + lastModified=entry.last_modified, + lastModifiedBy=entry.last_modified_by, + version=entry.version, + ) + self.index.add_entries( + entry.resource_id, + [self.entry_transformer.transform(entry.resource_id, entry_dto)], + ) def _entry_deleted_handler(self, entry): - with self.index_uow as uw: - uw.repo.delete_entry(entry.resource_id, entry_id=entry.id) - uw.commit() + self.index.delete_entry(entry.resource_id, entry_id=entry.id) diff --git a/karp-backend/src/karp/main/modules.py b/karp-backend/src/karp/main/modules.py index 31a53e7c..39335ee8 100644 --- a/karp-backend/src/karp/main/modules.py +++ b/karp-backend/src/karp/main/modules.py @@ -12,8 +12,8 @@ from karp.search_infrastructure import ( GenericPreProcessor, GenericEntryTransformer, - Es6IndexUnitOfWork, ) +from karp.search_infrastructure.repositories.es6_indicies import Es6Index try: from importlib.metadata import entry_points @@ -84,32 +84,32 @@ class CommandsMod(injector.Module): # noqa: D101 def entry_commands( self, resource_uow: ResourceUnitOfWork, - index_uow: Es6IndexUnitOfWork, + index: Es6Index, entry_transformer: GenericEntryTransformer, resource_views: GenericResourceViews, ) -> EntryCommands: return EntryCommands( resource_uow=resource_uow, - index_uow=index_uow, + index=index, entry_transformer=entry_transformer, resource_views=resource_views, ) @injector.provider def resource_commands( - self, resource_uow: ResourceUnitOfWork, index_uow: Es6IndexUnitOfWork + self, resource_uow: ResourceUnitOfWork, index: Es6Index ) -> ResourceCommands: - return ResourceCommands(resource_uow=resource_uow, index_uow=index_uow) + return ResourceCommands(resource_uow=resource_uow, index=index) @injector.provider def search_commands( self, - index_uow: Es6IndexUnitOfWork, + index: Es6Index, resource_views: GenericResourceViews, pre_processor: GenericPreProcessor, ) -> SearchCommands: return SearchCommands( - index_uow=index_uow, resource_views=resource_views, pre_processor=pre_processor + index=index, resource_views=resource_views, pre_processor=pre_processor ) diff --git a/karp-backend/src/karp/resource_commands.py b/karp-backend/src/karp/resource_commands.py index a1c5b7f6..be867779 100644 --- a/karp-backend/src/karp/resource_commands.py +++ b/karp-backend/src/karp/resource_commands.py @@ -5,16 +5,16 @@ from karp.lex.domain import entities from karp.lex.domain.errors import IntegrityError, ResourceNotFound from karp.lex_core.value_objects import make_unique_id -from karp.search_infrastructure.repositories.es6_indicies import Es6IndexUnitOfWork +from karp.search_infrastructure.repositories.es6_indicies import Es6Index from karp.timings import utc_now logger = logging.getLogger(__name__) class ResourceCommands: - def __init__(self, resource_uow, index_uow): + def __init__(self, resource_uow, index): self.resource_uow: ResourceUnitOfWork = resource_uow - self.index_uow: Es6IndexUnitOfWork = index_uow + self.index: Es6Index = index def create_resource(self, resource_id, name, config, user): with self.resource_uow as uow: @@ -80,16 +80,10 @@ def delete_resource(self, resource_id, user, message): self._deleting_index(resource_id) def _deleting_index(self, resource_id): - with self.index_uow as uw: - uw.repo.delete_index(resource_id) - uw.commit() + self.index.delete_index(resource_id) def _create_search_servie_handler(self, resource): - with self.index_uow as uw: - uw.repo.create_index(resource.resource_id, resource.config) - uw.commit() + self.index.create_index(resource.resource_id, resource.config) def _resource_published_handler(self, resource_id): - with self.index_uow as uw: - uw.repo.publish_index(resource_id) - uw.commit() + self.index.publish_index(resource_id) diff --git a/karp-backend/src/karp/search_commands.py b/karp-backend/src/karp/search_commands.py index b4ea3738..74beb176 100644 --- a/karp-backend/src/karp/search_commands.py +++ b/karp-backend/src/karp/search_commands.py @@ -1,11 +1,8 @@ import logging -from karp.lex.application.repositories import EntryUnitOfWork -from karp.lex.domain.errors import EntryNotFound, ResourceNotFound -from karp.lex_core.value_objects import unique_id from karp.search.generic_resources import GenericResourceViews -from karp.search_infrastructure import Es6IndexUnitOfWork, GenericPreProcessor -from karp.timings import utc_now +from karp.search_infrastructure import GenericPreProcessor +from karp.search_infrastructure.repositories.es6_indicies import Es6Index logger = logging.getLogger(__name__) @@ -13,21 +10,19 @@ class SearchCommands: def __init__( self, - index_uow: Es6IndexUnitOfWork, + index: Es6Index, resource_views: GenericResourceViews, pre_processor: GenericPreProcessor, ): super().__init__() - self.index_uow = index_uow + self.index = index self.resource_views = resource_views self.pre_processor = pre_processor def reindex_resource(self, resource_id): logger.debug("Reindexing resource '%s'", resource_id) - with self.index_uow as uw: - uw.repo.create_index( - resource_id, - self.resource_views.get_resource_config(resource_id), - ) - uw.repo.add_entries(resource_id, self.pre_processor.process(resource_id)) - uw.commit() + self.index.create_index( + resource_id, + self.resource_views.get_resource_config(resource_id), + ) + self.index.add_entries(resource_id, self.pre_processor.process(resource_id)) diff --git a/karp-backend/src/karp/search_infrastructure/__init__.py b/karp-backend/src/karp/search_infrastructure/__init__.py index 367f5bcb..0740d082 100644 --- a/karp-backend/src/karp/search_infrastructure/__init__.py +++ b/karp-backend/src/karp/search_infrastructure/__init__.py @@ -9,13 +9,11 @@ from karp.search_infrastructure.queries import ( Es6SearchService, ) +from karp.search_infrastructure.repositories.es6_indicies import Es6Index from karp.search_infrastructure.transformers import ( GenericEntryTransformer, GenericPreProcessor, ) -from karp.search_infrastructure.repositories import ( - Es6IndexUnitOfWork, -) from karp.search_infrastructure.elasticsearch6 import Es6MappingRepository from karp.search.generic_resources import GenericResourceViews @@ -27,12 +25,12 @@ class SearchInfrastructure(injector.Module): # noqa: D101 @injector.provider def entry_transformer( # noqa: D102 self, - index_uow: Es6IndexUnitOfWork, + index: Es6Index, resource_repo: SqlReadOnlyResourceRepository, entry_views: GenericEntryViews, ) -> GenericEntryTransformer: return GenericEntryTransformer( - index_uow=index_uow, + index=index, resource_repo=resource_repo, entry_views=entry_views, ) @@ -81,12 +79,12 @@ def es6_search_service( # noqa: D102 ) @injector.provider - def es6_index_uow( + def es6_index( self, es: elasticsearch.Elasticsearch, mapping_repo: Es6MappingRepository, - ) -> Es6IndexUnitOfWork: - return Es6IndexUnitOfWork( + ) -> Es6Index: + return Es6Index( es=es, mapping_repo=mapping_repo, ) diff --git a/karp-backend/src/karp/search_infrastructure/repositories/__init__.py b/karp-backend/src/karp/search_infrastructure/repositories/__init__.py index c7d3b45e..e69de29b 100644 --- a/karp-backend/src/karp/search_infrastructure/repositories/__init__.py +++ b/karp-backend/src/karp/search_infrastructure/repositories/__init__.py @@ -1 +0,0 @@ -from .es6_indicies import Es6IndexUnitOfWork diff --git a/karp-backend/src/karp/search_infrastructure/repositories/es6_indicies.py b/karp-backend/src/karp/search_infrastructure/repositories/es6_indicies.py index 8d75e467..ec9e4f71 100644 --- a/karp-backend/src/karp/search_infrastructure/repositories/es6_indicies.py +++ b/karp-backend/src/karp/search_infrastructure/repositories/es6_indicies.py @@ -5,7 +5,6 @@ import elasticsearch from elasticsearch import exceptions as es_exceptions # noqa: F401 -from karp.foundation.unit_of_work import UnitOfWork from karp.lex.domain.entities import Entry from karp.search.application.repositories import ( IndexEntry, @@ -237,32 +236,3 @@ def create_es6_mapping(config: Dict) -> Dict: # noqa: D103 } } return mapping - - -class Es6IndexUnitOfWork(UnitOfWork): # noqa: D101 - def __init__( # noqa: D107 - self, - es: elasticsearch.Elasticsearch, - mapping_repo: Es6MappingRepository, - ) -> None: - super().__init__() - self._index = Es6Index( - es=es, - mapping_repo=mapping_repo, - ) - - def _commit(self): # noqa: ANN202 - logger.debug("Calling _commit in Es6IndexUnitOfWork") - - def rollback(self): # noqa: ANN201, D102 - return super().rollback() - - @property - def repo(self) -> Es6Index: # noqa: D102 - return self._index - - def _close(self): # noqa: ANN202 - pass - - def begin(self): # noqa: ANN201, D102 - return self diff --git a/karp-backend/src/karp/search_infrastructure/transformers/generic_entry_transformer.py b/karp-backend/src/karp/search_infrastructure/transformers/generic_entry_transformer.py index 5961dbf6..3de8feb2 100644 --- a/karp-backend/src/karp/search_infrastructure/transformers/generic_entry_transformer.py +++ b/karp-backend/src/karp/search_infrastructure/transformers/generic_entry_transformer.py @@ -8,7 +8,7 @@ from karp.lex.domain import errors as lex_errors from karp.lex_infrastructure import GenericEntryViews, SqlReadOnlyResourceRepository from karp.search.application.repositories.indicies import IndexEntry -from karp.search_infrastructure.repositories.es6_indicies import Es6IndexUnitOfWork +from karp.search_infrastructure.repositories.es6_indicies import Es6Index logger = logging.getLogger(__name__) @@ -16,12 +16,12 @@ class GenericEntryTransformer: def __init__( self, - index_uow: Es6IndexUnitOfWork, + index: Es6Index, resource_repo: SqlReadOnlyResourceRepository, entry_views: GenericEntryViews, ) -> None: super().__init__() - self.index_uow = index_uow + self.index = index self.resource_repo = resource_repo self.entry_views = entry_views @@ -30,13 +30,11 @@ def transform(self, resource_id: str, src_entry: EntryDto) -> IndexEntry: "transforming entry", extra={"entity_id": src_entry.id, "resource_id": resource_id}, ) - index_entry = self.index_uow.repo.create_empty_object() + index_entry = self.index.create_empty_object() index_entry.id = str(src_entry.id) - self.index_uow.repo.assign_field(index_entry, "_entry_version", src_entry.version) - self.index_uow.repo.assign_field(index_entry, "_last_modified", src_entry.last_modified) - self.index_uow.repo.assign_field( - index_entry, "_last_modified_by", src_entry.last_modified_by - ) + self.index.assign_field(index_entry, "_entry_version", src_entry.version) + self.index.assign_field(index_entry, "_last_modified", src_entry.last_modified) + self.index.assign_field(index_entry, "_last_modified_by", src_entry.last_modified_by) resource = self.resource_repo.get_by_resource_id(resource_id) if not resource: raise lex_errors.ResourceNotFound(None, resource_id=resource_id) @@ -67,22 +65,20 @@ def _transform_to_index_entry( # noqa: ANN202, C901 if field_name in _src_entry: for subfield in _src_entry[field_name]: if field_conf["type"] == "object": - subfield_content = self.index_uow.repo.create_empty_object() + subfield_content = self.index.create_empty_object() self._transform_to_index_entry( resource, subfield, subfield_content, field_conf["fields"].items(), ) - self.index_uow.repo.add_to_list_field( - field_content, subfield_content.entry - ) + self.index.add_to_list_field(field_content, subfield_content.entry) else: - self.index_uow.repo.add_to_list_field(field_content, subfield) - self.index_uow.repo.assign_field(_index_entry, field_name, field_content) + self.index.add_to_list_field(field_content, subfield) + self.index.assign_field(_index_entry, field_name, field_content) elif field_conf["type"] == "object": - field_content = self.index_uow.repo.create_empty_object() + field_content = self.index.create_empty_object() if field_name in _src_entry: self._transform_to_index_entry( resource, @@ -90,7 +86,7 @@ def _transform_to_index_entry( # noqa: ANN202, C901 field_content, field_conf["fields"].items(), ) - self.index_uow.repo.assign_field(_index_entry, field_name, field_content) + self.index.assign_field(_index_entry, field_name, field_content) elif field_conf["type"] in ( "integer", @@ -101,4 +97,4 @@ def _transform_to_index_entry( # noqa: ANN202, C901 ): if field_name in _src_entry: field_content = _src_entry[field_name] - self.index_uow.repo.assign_field(_index_entry, field_name, field_content) + self.index.assign_field(_index_entry, field_name, field_content) diff --git a/tests/unit/search/adapters.py b/tests/unit/search/adapters.py index 5f3a5aff..33cae0de 100644 --- a/tests/unit/search/adapters.py +++ b/tests/unit/search/adapters.py @@ -4,11 +4,9 @@ import injector -from karp.foundation import repository -from karp.search_infrastructure import Es6IndexUnitOfWork +from karp.search_infrastructure.repositories.es6_indicies import Es6Index from karp.timings import utc_now from karp.search.application.repositories import IndexEntry -from tests.foundation.adapters import InMemoryUnitOfWork @dataclasses.dataclass @@ -16,7 +14,7 @@ class SearchUnitTestContext: container: injector.Injector -class InMemoryIndex(repository.Repository): +class InMemoryIndex(Es6Index): @dataclasses.dataclass class Index: config: Dict @@ -76,18 +74,8 @@ def _by_id(self, id) -> None: # noqa: A002 return None -class InMemoryIndexUnitOfWork(InMemoryUnitOfWork, Es6IndexUnitOfWork): - def __init__(self): - Es6IndexUnitOfWork.__init__(self) - self._index = InMemoryIndex() - - @property - def repo(self) -> InMemoryIndex: - return self._index - - class InMemorySearchInfrastructure(injector.Module): @injector.provider @injector.singleton - def index_uow(self) -> Es6IndexUnitOfWork: - return InMemoryIndexUnitOfWork() + def index(self) -> Es6Index: + return InMemoryIndex() diff --git a/tests/unit/search_infrastructure/test_es6_insanity_checks.py b/tests/unit/search_infrastructure/test_es6_insanity_checks.py index c3804a86..4e4a31cc 100644 --- a/tests/unit/search_infrastructure/test_es6_insanity_checks.py +++ b/tests/unit/search_infrastructure/test_es6_insanity_checks.py @@ -1,7 +1,6 @@ import contextlib # noqa: I001 from karp.search_infrastructure.repositories.es6_indicies import ( Es6Index, - Es6IndexUnitOfWork, ) @@ -9,7 +8,3 @@ class TestEs6Index: def test_can_instantiate_es6_index(self): # noqa: ANN201 with contextlib.suppress(AttributeError): Es6Index(es=None, mapping_repo=None) - - def test_can_instantiate_es6_index_uow(self): # noqa: ANN201 - with contextlib.suppress(AttributeError): - Es6IndexUnitOfWork(es=None, mapping_repo=None)