Skip to content

Commit

Permalink
remove Es6IndexUnitOfWork, use Es6Index instead
Browse files Browse the repository at this point in the history
  • Loading branch information
majsan committed Feb 5, 2024
1 parent 9af82bb commit 709de15
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 147 deletions.
66 changes: 30 additions & 36 deletions karp-backend/src/karp/entry_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
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


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

Expand Down Expand Up @@ -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)
14 changes: 7 additions & 7 deletions karp-backend/src/karp/main/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)


Expand Down
18 changes: 6 additions & 12 deletions karp-backend/src/karp/resource_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
23 changes: 9 additions & 14 deletions karp-backend/src/karp/search_commands.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
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__)


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))
14 changes: 6 additions & 8 deletions karp-backend/src/karp/search_infrastructure/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
)
Expand Down Expand Up @@ -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,
)
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
from .es6_indicies import Es6IndexUnitOfWork
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
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__)


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

Expand All @@ -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)
Expand Down Expand Up @@ -67,30 +65,28 @@ 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,
_src_entry[field_name],
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",
Expand All @@ -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)
Loading

0 comments on commit 709de15

Please sign in to comment.