Skip to content

Commit

Permalink
- fix namings
Browse files Browse the repository at this point in the history
  • Loading branch information
psmagin committed Sep 11, 2024
1 parent 096a784 commit 8fba363
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static java.util.Collections.emptyList;
import static java.util.Collections.emptySet;
import static org.apache.commons.collections4.MapUtils.getObject;
import static org.apache.commons.lang3.StringUtils.defaultIfBlank;
import static org.apache.commons.lang3.StringUtils.truncate;
import static org.folio.search.utils.CollectionUtils.subtract;
import static org.folio.search.utils.SearchConverterUtils.getNewAsMap;
import static org.folio.search.utils.SearchConverterUtils.getOldAsMap;
Expand All @@ -19,7 +21,6 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.folio.search.domain.dto.ResourceEvent;
import org.folio.search.domain.dto.ResourceEventType;
import org.folio.search.domain.dto.TenantConfiguredFeature;
Expand Down Expand Up @@ -99,7 +100,6 @@ public List<ResourceEvent> prepareEventsOnSharing(ResourceEvent event) {
.toList();
}


private List<ResourceEvent> getResourceEventsForDeletion(List<String> idsForDelete,
List<InstanceClassificationEntityAgg> entityAggList,
String tenant) {
Expand Down Expand Up @@ -139,13 +139,13 @@ private ResourceEvent toResourceEvent(InstanceClassificationEntityAgg source, St
}

private String getClassificationId(String number, String typeId) {
return ShaUtils.sha(StringUtils.truncate(number.replace("\\", "\\\\"), 50), typeId);
return ShaUtils.sha(truncate(number.replace("\\", "\\\\"), 50), typeId);
}

@NotNull
private List<String> toIds(Set<Map<String, Object>> subtract) {
return subtract.stream()
.map(map -> getClassificationId(MapUtils.getString(map, CLASSIFICATION_NUMBER_FIELD),
.map(map -> getClassificationId(defaultIfBlank(MapUtils.getString(map, CLASSIFICATION_NUMBER_FIELD), ""),
MapUtils.getString(map, CLASSIFICATION_TYPE_FIELD)))
.collect(Collectors.toCollection(ArrayList::new));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static java.util.Collections.emptyList;
import static java.util.Collections.emptySet;
import static org.apache.commons.collections4.MapUtils.getObject;
import static org.apache.commons.lang3.StringUtils.defaultIfBlank;
import static org.apache.commons.lang3.StringUtils.truncate;
import static org.folio.search.utils.CollectionUtils.subtract;
import static org.folio.search.utils.SearchConverterUtils.getNewAsMap;
import static org.folio.search.utils.SearchConverterUtils.getOldAsMap;
Expand All @@ -17,7 +19,6 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.folio.search.domain.dto.ResourceEvent;
import org.folio.search.domain.dto.ResourceEventType;
import org.folio.search.model.entity.InstanceContributorEntityAgg;
Expand All @@ -41,19 +42,19 @@ public class ContributorResourceExtractor implements ChildResourceExtractor {

@Override
public List<ResourceEvent> prepareEvents(ResourceEvent event) {
var oldSubjects = getSubjects(getOldAsMap(event));
var newSubjects = getSubjects(getNewAsMap(event));
var oldEntities = getEntities(getOldAsMap(event));
var newEntities = getEntities(getNewAsMap(event));

if (oldSubjects.equals(newSubjects)) {
if (oldEntities.equals(newEntities)) {
return emptyList();
}

var tenant = event.getTenant();
var subjectsForCreate = subtract(newSubjects, oldSubjects);
var subjectsForDelete = subtract(oldSubjects, newSubjects);
var entitiesForCreate = subtract(newEntities, oldEntities);
var entitiesForDelete = subtract(oldEntities, newEntities);

var idsForCreate = toIds(subjectsForCreate);
var idsForDelete = toIds(subjectsForDelete);
var idsForCreate = toIds(entitiesForCreate);
var idsForDelete = toIds(entitiesForDelete);

List<String> idsForFetch = new ArrayList<>();
idsForFetch.addAll(idsForCreate);
Expand All @@ -70,20 +71,20 @@ public List<ResourceEvent> prepareEvents(ResourceEvent event) {

@Override
public List<ResourceEvent> prepareEventsOnSharing(ResourceEvent event) {
var subjects = getSubjects(getOldAsMap(event));
var entities = getEntities(getOldAsMap(event));

if (!subjects.equals(getSubjects(getNewAsMap(event)))) {
log.warn("Classifications are different on Update for instance sharing");
if (!entities.equals(getEntities(getNewAsMap(event)))) {
log.warn("Contributors are different on Update for instance sharing");
return emptyList();
}

var tenant = event.getTenant();

var entitiesForDelete = toIds(subjects);
var entityAggList = contributorRepository.fetchByIds(entitiesForDelete);
var ids = toIds(entities);
var entityAggList = contributorRepository.fetchByIds(ids);

return entityAggList.stream()
.map(entities -> toResourceEvent(entities, tenant))
.map(e -> toResourceEvent(e, tenant))
.toList();
}

Expand All @@ -93,9 +94,9 @@ private List<ResourceEvent> getResourceEventsForDeletion(List<String> idsForDele
var notFoundEntitiesForDelete = new ArrayList<>(idsForDelete);
var iterator = notFoundEntitiesForDelete.iterator();
while (iterator.hasNext()) {
var classification = iterator.next();
var entityId = iterator.next();
for (InstanceContributorEntityAgg agg : entityAggList) {
if (agg.id().equals(classification)) {
if (agg.id().equals(entityId)) {
iterator.remove();
}
}
Expand Down Expand Up @@ -126,22 +127,22 @@ private ResourceEvent toResourceEvent(InstanceContributorEntityAgg source, Strin
._new(jsonConverter.convertToMap(resource));
}

private String getSubjectId(String number, String typeId, String authorityId) {
return ShaUtils.sha(StringUtils.truncate(number.replace("\\", "\\\\"), 255),
private String getEntityId(String name, String typeId, String authorityId) {
return ShaUtils.sha(truncate(name.replace("\\", "\\\\"), 255),
typeId, authorityId);
}

@NotNull
private List<String> toIds(Set<Map<String, Object>> subtract) {
return subtract.stream()
.map(map -> getSubjectId(MapUtils.getString(map, "name"),
.map(map -> getEntityId(defaultIfBlank(MapUtils.getString(map, "name"), ""),
MapUtils.getString(map, "contributorNameTypeId"),
MapUtils.getString(map, "authorityId")))
.collect(Collectors.toCollection(ArrayList::new));
}

@SuppressWarnings("unchecked")
private Set<Map<String, Object>> getSubjects(Map<String, Object> event) {
private Set<Map<String, Object>> getEntities(Map<String, Object> event) {
var object = getObject(event, CONTRIBUTORS_FIELD, emptyList());
if (object == null) {
return emptySet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static java.util.Collections.emptyList;
import static java.util.Collections.emptySet;
import static org.apache.commons.collections4.MapUtils.getObject;
import static org.apache.commons.lang3.StringUtils.defaultIfBlank;
import static org.apache.commons.lang3.StringUtils.truncate;
import static org.folio.search.utils.CollectionUtils.subtract;
import static org.folio.search.utils.SearchConverterUtils.getNewAsMap;
import static org.folio.search.utils.SearchConverterUtils.getOldAsMap;
Expand All @@ -17,7 +19,6 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.folio.search.domain.dto.ResourceEvent;
import org.folio.search.domain.dto.ResourceEventType;
import org.folio.search.model.entity.InstanceSubjectEntityAgg;
Expand Down Expand Up @@ -73,30 +74,29 @@ public List<ResourceEvent> prepareEventsOnSharing(ResourceEvent event) {
var subjects = getSubjects(getOldAsMap(event));

if (!subjects.equals(getSubjects(getNewAsMap(event)))) {
log.warn("Classifications are different on Update for instance sharing");
log.warn("Subjects are different on Update for instance sharing");
return emptyList();
}

var tenant = event.getTenant();

var entitiesForDelete = toIds(subjects);
var entityAggList = subjectRepository.fetchByIds(entitiesForDelete);
var ids = toIds(subjects);
var entityAggList = subjectRepository.fetchByIds(ids);

return entityAggList.stream()
.map(entities -> toResourceEvent(entities, tenant))
.toList();
}


private List<ResourceEvent> getResourceEventsForDeletion(List<String> idsForDelete,
List<InstanceSubjectEntityAgg> entityAggList,
String tenant) {
var notFoundEntitiesForDelete = new ArrayList<>(idsForDelete);
var iterator = notFoundEntitiesForDelete.iterator();
while (iterator.hasNext()) {
var classification = iterator.next();
var entityId = iterator.next();
for (InstanceSubjectEntityAgg agg : entityAggList) {
if (agg.id().equals(classification)) {
if (agg.id().equals(entityId)) {
iterator.remove();
}
}
Expand Down Expand Up @@ -126,14 +126,14 @@ private ResourceEvent toResourceEvent(InstanceSubjectEntityAgg source, String te
._new(jsonConverter.convertToMap(resource));
}

private String getSubjectId(String number, String typeId) {
return ShaUtils.sha(StringUtils.truncate(number.replace("\\", "\\\\"), 255), typeId);
private String getEntityId(String number, String typeId) {
return ShaUtils.sha(truncate(number.replace("\\", "\\\\"), 255), typeId);
}

@NotNull
private List<String> toIds(Set<Map<String, Object>> subtract) {
return subtract.stream()
.map(map -> getSubjectId(MapUtils.getString(map, "value"),
.map(map -> getEntityId(defaultIfBlank(MapUtils.getString(map, "value"), ""),
MapUtils.getString(map, "authorityId")))
.collect(Collectors.toCollection(ArrayList::new));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ public boolean isSupportedLanguage(String languageCode) {
}

@Override
public boolean isMultilangField(ResourceType resourceName, String path) {
return this.getPlainFieldByPath(resourceName, path).filter(PlainFieldDescription::isMultilang).isPresent();
public boolean isMultilangField(ResourceType resourceType, String path) {
return this.getPlainFieldByPath(resourceType, path).filter(PlainFieldDescription::isMultilang).isPresent();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public interface MetadataResourceProvider {
/**
* Finds resource description by given resource name.
*
* @param resourceType resource name as {@link String} value.
* @param resourceType resource type as {@link ResourceType} value.
* @return {@link Optional} of {@link ResourceDescription} if it has been found, it would be empty otherwise.
*/
default Optional<ResourceDescription> getResourceDescription(ResourceType resourceType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void init() {
/**
* Provides {@link ResourceDescription} object for given resource name.
*
* @param resourceType name of resource as {@link String}
* @param resourceType resource type as {@link ResourceType} value.
* @return {@link ResourceDescription} object
* @throws ResourceDescriptionException if resource description is not found for the given name.
*/
Expand All @@ -76,7 +76,7 @@ public ResourceDescription get(ResourceType resourceType) {
/**
* Provides {@link ResourceDescription} object as {@link Optional} for given resource name.
*
* @param resourceType name of resource as {@link ResourceType}
* @param resourceType type of resource as {@link ResourceType}
*/
public Optional<ResourceDescription> find(ResourceType resourceType) {
return Optional.ofNullable(resourceDescriptions.get(resourceType));
Expand All @@ -94,17 +94,17 @@ public Collection<ResourceDescription> findAll() {
/**
* Returns all supported resource types.
*
* @return {@link Collection} with all resource names as {@link String} values.
* @return {@link Collection} with all resource names as {@link ResourceType} values.
*/
public Collection<ResourceType> getResourceTypes() {
return resourceDescriptions.keySet();
}

/**
* Returns name of secondary resources that linked to the given resource name.
* Returns types of secondary resources that linked to the given resource type.
*
* @param resource - resource name to check as {@link String}.
* @return {@link Collection} with secondary resource names.
* @param resource - resource type to check as {@link ResourceType}.
* @return {@link Collection} with secondary resource types.
*/
public Collection<ResourceType> getSecondaryResourceTypes(ResourceType resource) {
log.debug("getSecondaryResourceNames:: by [resource: {}]", resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public interface SearchFieldProvider {
/**
* Provides list of fields for given search type.
*
* @param resource resource type as {@link String}
* @param resource resource type as {@link ResourceType}
* @param searchType search type as {@link String}
* @return list of fields.
*/
Expand All @@ -33,7 +33,7 @@ public interface SearchFieldProvider {
/**
* Provides plain field description for given path.
*
* @param resource resource type as {@link String}
* @param resource resource type as {@link ResourceType}
* @param path path to field as {@link String}
* @return {@link Optional} of resource field description by path, it would be empty if plain field by path not found
*/
Expand All @@ -42,7 +42,7 @@ public interface SearchFieldProvider {
/**
* Provides list of fields of source fields for resource and response group type.
*
* @param resource resource type as {@link String}
* @param resource resource type as {@link ResourceType}
* @param groupType - response group type as {@link ResponseGroupType} object
* @return array of fields.
*/
Expand All @@ -58,16 +58,16 @@ public interface SearchFieldProvider {
/**
* Checks if field by path is multi-language or not.
*
* @param resourceName resource name as {@link String} object
* @param resourceType resource type as {@link ResourceType} object
* @param path path to the field as {@link String} object
* @return true if field by path is multi-language, false - otherwise
*/
boolean isMultilangField(ResourceType resourceName, String path);
boolean isMultilangField(ResourceType resourceType, String path);

/**
* Checks if field by path is full-text or not.
*
* @param resourceName resource name as {@link String} object
* @param resourceName resource type as {@link ResourceType} object
* @param path path to the field as {@link String} object
* @return true if field by path is full-text, false - otherwise
*/
Expand All @@ -77,7 +77,7 @@ public interface SearchFieldProvider {
* Apply resource field modifiers for field.
*
* @param field that should be modified {@link String} object
* @param resource resource name as {@link String} object
* @param resource resource type as {@link ResourceType} object
* @return modified field
*/
String getModifiedField(String field, ResourceType resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,11 @@ public List<MergeRangeEntity> createMergeRanges(String tenantId) {
var rangeSize = reindexConfig.getMergeRangeSize();
for (var recordType : InventoryRecordType.values()) {
var recordsCount = inventoryService.fetchInventoryRecordsCount(recordType);
if (recordsCount == 0) {
log.info("createMergeRanges:: constructed empty range [tenantId: {}, entityType: {}]",
tenantId, recordType);
var range = RangeGenerator.emptyRange();
var mergeRangeEntity = mergeEntity(recordType, tenantId, range.lowerBound(), range.upperBound());
mergeRangeEntities.add(mergeRangeEntity);
} else {
var ranges = constructMergeRangeRecords(recordsCount, rangeSize, recordType, tenantId);
if (CollectionUtils.isNotEmpty(ranges)) {
log.info("createMergeRanges:: constructed [tenantId: {}, entityType: {}, count: {}]",
tenantId, recordType, ranges.size());
mergeRangeEntities.addAll(ranges);
}
var ranges = constructMergeRangeRecords(recordsCount, rangeSize, recordType, tenantId);
if (CollectionUtils.isNotEmpty(ranges)) {
log.info("createMergeRanges:: constructed [tenantId: {}, entityType: {}, count: {}]",
tenantId, recordType, ranges.size());
mergeRangeEntities.addAll(ranges);
}
}
return mergeRangeEntities;
Expand Down Expand Up @@ -91,7 +83,13 @@ private List<MergeRangeEntity> constructMergeRangeRecords(int recordsCount,
String tenantId) {
log.info("constructMergeRangeRecords:: [tenantId: {}, recordType: {}, recordsCount: {}, rangeSize: {}]",
tenantId, recordType, recordsCount, rangeSize);

if (recordsCount == 0) {
log.info("constructMergeRangeRecords:: constructed empty range [tenantId: {}, entityType: {}]",
tenantId, recordType);
var range = RangeGenerator.emptyRange();
var mergeRangeEntity = mergeEntity(recordType, tenantId, range.lowerBound(), range.upperBound());
return List.of(mergeRangeEntity);
}
var rangesCount = (int) Math.ceil((double) recordsCount / rangeSize);
return RangeGenerator.createRanges(rangesCount).stream()
.map(range -> mergeEntity(recordType, tenantId, range.lowerBound(), range.upperBound()))
Expand Down

0 comments on commit 8fba363

Please sign in to comment.