Skip to content

Commit

Permalink
[CIRC-2018] Combine items with related records during pagination proc…
Browse files Browse the repository at this point in the history
…ess (#1424)

* CIRC-2018 implement combining by page

* CIRC-2018 add test

* CIRC-2018 fix code smells

* CIRC-2018 fix code smells

* CIRC-2018 fix code smells

* CIRC-2018 remove unused import

* CIRC-2018 add test

* CIRC-2018 refactoring

* CIRC-2018 rename interface

* CIRC-2018 inline variable

* CIRC-2018 add executing fetching in another thread

* CIRC-2018 rename parameter

* CIRC-2018 rename parameter

* CIRC-2018 rename parameter

* CIRC-2018 add logging

(cherry picked from commit 2736ad8)
  • Loading branch information
roman-barannyk committed Feb 22, 2024
1 parent 704a479 commit d2689f8
Show file tree
Hide file tree
Showing 12 changed files with 329 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
import static org.folio.circulation.support.ValidationErrorFailure.failedValidation;
import static org.folio.circulation.support.fetching.RecordFetching.findWithCqlQuery;
import static org.folio.circulation.support.fetching.RecordFetching.findWithMultipleCqlIndexValues;
import static org.folio.circulation.support.fetching.RecordFetching.findWithMultipleCqlIndexValuesAndCombine;
import static org.folio.circulation.support.http.client.CqlQuery.exactMatch;
import static org.folio.circulation.support.results.ResultBinding.mapResult;
import static org.folio.circulation.support.utils.LogUtil.collectionAsString;

import java.lang.invoke.MethodHandles;
import java.util.Collection;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.folio.circulation.domain.Holdings;
import org.folio.circulation.domain.MultipleRecords;
import org.folio.circulation.storage.mappers.HoldingsMapper;
Expand All @@ -19,6 +25,8 @@
import io.vertx.core.json.JsonObject;

public class HoldingsRepository {
private static final String HOLDINGS_RECORDS = "holdingsRecords";
private static final Logger log = LogManager.getLogger(MethodHandles.lookup().lookupClass());
private final CollectionResourceClient holdingsClient;

public HoldingsRepository(CollectionResourceClient holdingsClient) {
Expand All @@ -42,7 +50,7 @@ CompletableFuture<Result<MultipleRecords<Holdings>>> fetchByInstanceId(String in
final var mapper = new HoldingsMapper();

final var holdingsRecordFetcher = findWithCqlQuery(
holdingsClient, "holdingsRecords", mapper::toDomain);
holdingsClient, HOLDINGS_RECORDS, mapper::toDomain);

return holdingsRecordFetcher.findByQuery(exactMatch("instanceId", instanceId));
}
Expand All @@ -52,8 +60,20 @@ CompletableFuture<Result<MultipleRecords<Holdings>>> fetchByIds(

final var mapper = new HoldingsMapper();

return findWithMultipleCqlIndexValues(holdingsClient, "holdingsRecords",
return findWithMultipleCqlIndexValues(holdingsClient, HOLDINGS_RECORDS,
mapper::toDomain)
.findByIds(holdingsRecordIds);
}

<T> CompletableFuture<Result<MultipleRecords<T>>> fetchByIdsAndCombine(
Collection<String> holdingsRecordIds,
Function<Result<MultipleRecords<Holdings>>, Result<MultipleRecords<T>>> combineFunction) {

log.debug("fetchByIdsAndCombine:: parameters instanceIds: {}",
() -> collectionAsString(holdingsRecordIds));

return findWithMultipleCqlIndexValuesAndCombine(holdingsClient,
HOLDINGS_RECORDS, new HoldingsMapper()::toDomain, combineFunction)
.findByIdsAndCombine(holdingsRecordIds);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static java.util.concurrent.CompletableFuture.completedFuture;
import static org.folio.circulation.support.fetching.RecordFetching.findWithMultipleCqlIndexValues;
import static org.folio.circulation.support.fetching.RecordFetching.findWithMultipleCqlIndexValuesAndCombine;
import static org.folio.circulation.support.results.Result.succeeded;
import static org.folio.circulation.support.utils.LogUtil.collectionAsString;
import static org.folio.circulation.support.utils.LogUtil.multipleRecordsAsString;
Expand All @@ -28,6 +29,7 @@

public class InstanceRepository {
private static final Logger log = LogManager.getLogger(MethodHandles.lookup().lookupClass());
private static final String INSTANCES = "instances";
private final CollectionResourceClient instancesClient;

public InstanceRepository(Clients clients) {
Expand Down Expand Up @@ -55,11 +57,23 @@ public CompletableFuture<Result<MultipleRecords<Instance>>> fetchByIds(

InstanceMapper mapper = new InstanceMapper();

return findWithMultipleCqlIndexValues(instancesClient, "instances",
return findWithMultipleCqlIndexValues(instancesClient, INSTANCES,
mapper::toDomain)
.findByIds(instanceIds);
}

<T> CompletableFuture<Result<MultipleRecords<T>>> fetchByIdsAndCombine(
Collection<String> instanceIds,
Function<Result<MultipleRecords<Instance>>, Result<MultipleRecords<T>>> combineFunction) {

log.debug("fetchByIdsAndCombine:: parameters instanceIds: {}",
() -> collectionAsString(instanceIds));

return findWithMultipleCqlIndexValuesAndCombine(instancesClient, INSTANCES,
new InstanceMapper()::toDomain, combineFunction)
.findByIdsAndCombine(instanceIds);
}


public CompletableFuture<Result<MultipleRecords<Request>>> findInstancesForRequests(MultipleRecords<Request> multipleRequests) {
log.debug("findInstancesForRequests:: parameters multipleRequests: {}", () -> multipleRecordsAsString(multipleRequests));
Expand All @@ -79,7 +93,7 @@ public CompletableFuture<Result<MultipleRecords<Request>>> findInstancesForReque

InstanceMapper mapper = new InstanceMapper();

return findWithMultipleCqlIndexValues(instancesClient, "instances", mapper::toDomain)
return findWithMultipleCqlIndexValues(instancesClient, INSTANCES, mapper::toDomain)
.findByIds(instanceIdsToFetch)
.thenApply(multipleInstancesResult -> multipleInstancesResult.next(
multipleInstances -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.folio.circulation.infrastructure.storage.inventory;

import static java.util.concurrent.CompletableFuture.completedFuture;
import static java.util.concurrent.CompletableFuture.supplyAsync;
import static java.util.function.Function.identity;
import static org.folio.circulation.domain.ItemStatus.AVAILABLE;
import static org.folio.circulation.domain.MultipleRecords.CombinationMatchers.matchRecordsById;
Expand Down Expand Up @@ -177,11 +178,12 @@ private CompletableFuture<Result<MultipleRecords<Location>>> fetchLocations(
private CompletableFuture<Result<MultipleRecords<Item>>> fetchMaterialTypes(
Result<MultipleRecords<Item>> result) {

return result.after(items ->
materialTypeRepository.getMaterialTypes(items)
.thenApply(mapResult(materialTypes -> items.combineRecords(materialTypes,
return result.after(items -> supplyAsync(() ->
materialTypeRepository.getMaterialTypesAndCombine(
items, mapResult(materialTypes -> items.combineRecords(materialTypes,
matchRecordsById(Item::getMaterialTypeId, MaterialType::getId),
Item::withMaterialType, MaterialType.unknown()))));
Item::withMaterialType, MaterialType.unknown()))))
.thenCompose(Function.identity()));
}

private CompletableFuture<Result<MultipleRecords<Item>>> fetchLoanTypes(
Expand All @@ -190,10 +192,11 @@ private CompletableFuture<Result<MultipleRecords<Item>>> fetchLoanTypes(
return result.after(items -> {
final var loanTypeIdsToFetch = items.toKeys(Item::getLoanTypeId);

return loanTypeRepository.findByIds(loanTypeIdsToFetch)
.thenApply(mapResult(loanTypes -> items.combineRecords(loanTypes,
return supplyAsync(() -> loanTypeRepository.findByIdsAndCombine(loanTypeIdsToFetch, mapResult(
loanTypes -> items.combineRecords(loanTypes,
matchRecordsById(Item::getLoanTypeId, LoanType::getId),
Item::withLoanType, LoanType.unknown())));
Item::withLoanType, LoanType.unknown()))))
.thenCompose(Function.identity());
});
}

Expand All @@ -203,10 +206,10 @@ private CompletableFuture<Result<MultipleRecords<Item>>> fetchInstances(
return result.after(items -> {
final var instanceIds = items.toKeys(Item::getInstanceId);

return instanceRepository.fetchByIds(instanceIds)
.thenApply(mapResult(instances -> items.combineRecords(instances,
matchRecordsById(Item::getInstanceId, Instance::getId),
Item::withInstance, Instance.unknown())));
return supplyAsync(() -> instanceRepository.fetchByIdsAndCombine(instanceIds, mapResult(
instances -> items.combineRecords(instances, matchRecordsById(
Item::getInstanceId, Instance::getId), Item::withInstance, Instance.unknown()))))
.thenCompose(Function.identity());
});
}

Expand All @@ -216,10 +219,11 @@ private CompletableFuture<Result<MultipleRecords<Item>>> fetchHoldingsRecords(
return result.after(items -> {
final var holdingsIds = items.toKeys(Item::getHoldingsRecordId);

return holdingsRepository.fetchByIds(holdingsIds)
.thenApply(mapResult(holdings -> items.combineRecords(holdings,
return supplyAsync(() -> holdingsRepository.fetchByIdsAndCombine(holdingsIds,
mapResult(holdings -> items.combineRecords(holdings,
matchRecordsById(Item::getHoldingsRecordId, Holdings::getId),
Item::withHoldings, Holdings.unknown())));
Item::withHoldings, Holdings.unknown()))))
.thenCompose(Function.identity());
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package org.folio.circulation.infrastructure.storage.inventory;

import static org.folio.circulation.support.fetching.RecordFetching.findWithMultipleCqlIndexValues;
import static org.folio.circulation.support.fetching.RecordFetching.findWithMultipleCqlIndexValuesAndCombine;
import static org.folio.circulation.support.results.Result.succeeded;
import static org.folio.circulation.support.results.ResultBinding.mapResult;
import static org.folio.circulation.support.utils.LogUtil.collectionAsString;

import java.lang.invoke.MethodHandles;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.folio.circulation.domain.LoanType;
import org.folio.circulation.domain.MultipleRecords;
import org.folio.circulation.storage.mappers.LoanTypeMapper;
Expand All @@ -15,6 +21,8 @@
import org.folio.circulation.support.results.Result;

public class LoanTypeRepository {
private static final String LOAN_TYPES = "loantypes";
private static final Logger log = LogManager.getLogger(MethodHandles.lookup().lookupClass());
public final CollectionResourceClient loanTypesClient;

public LoanTypeRepository(CollectionResourceClient loanTypesClient) {
Expand All @@ -35,7 +43,18 @@ CompletableFuture<Result<MultipleRecords<LoanType>>> findByIds(Set<String> ids)
final var mapper = new LoanTypeMapper();

return findWithMultipleCqlIndexValues(loanTypesClient,
"loantypes", mapper::toDomain)
LOAN_TYPES, mapper::toDomain)
.findByIds(ids);
}

<T> CompletableFuture<Result<MultipleRecords<T>>> findByIdsAndCombine(Set<String> ids,
Function<Result<MultipleRecords<LoanType>>, Result<MultipleRecords<T>>> combineFunction) {

log.debug("findByIdsAndCombine:: parameters instanceIds: {}",
() -> collectionAsString(ids));

return findWithMultipleCqlIndexValuesAndCombine(loanTypesClient,
LOAN_TYPES, new LoanTypeMapper()::toDomain, combineFunction)
.findByIdsAndCombine(ids);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import static java.util.Objects.isNull;
import static org.folio.circulation.support.fetching.RecordFetching.findWithMultipleCqlIndexValues;
import static org.folio.circulation.support.fetching.RecordFetching.findWithMultipleCqlIndexValuesAndCombine;
import static org.folio.circulation.support.results.Result.succeeded;
import static org.folio.circulation.support.utils.LogUtil.multipleRecordsAsString;

import java.lang.invoke.MethodHandles;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -21,6 +23,7 @@

public class MaterialTypeRepository {
private static final Logger log = LogManager.getLogger(MethodHandles.lookup().lookupClass());
private static final String MATERIAL_TYPES = "mtypes";
private final CollectionResourceClient materialTypesStorageClient;

public MaterialTypeRepository(Clients clients) {
Expand Down Expand Up @@ -55,8 +58,20 @@ public CompletableFuture<Result<MultipleRecords<MaterialType>>> getMaterialTypes
final var materialTypeIds = inventoryRecords.toKeys(Item::getMaterialTypeId);

final var fetcher
= findWithMultipleCqlIndexValues(materialTypesStorageClient, "mtypes", mapper::toDomain);
= findWithMultipleCqlIndexValues(materialTypesStorageClient, MATERIAL_TYPES, mapper::toDomain);

return fetcher.findByIds(materialTypeIds);
}

<T> CompletableFuture<Result<MultipleRecords<T>>> getMaterialTypesAndCombine(
MultipleRecords<Item> inventoryRecords,
Function<Result<MultipleRecords<MaterialType>>, Result<MultipleRecords<T>>> combineFunction) {

log.debug("getMaterialTypesAndCombine:: parameters inventoryRecords: {}",
() -> multipleRecordsAsString(inventoryRecords));
final var fetcher = findWithMultipleCqlIndexValuesAndCombine(materialTypesStorageClient,
MATERIAL_TYPES, new MaterialTypeMapper()::toDomain, combineFunction);

return fetcher.findByIdsAndCombine(inventoryRecords.toKeys(Item::getMaterialTypeId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.folio.circulation.support;

import java.util.Collection;
import java.util.concurrent.CompletableFuture;

import org.folio.circulation.domain.MultipleRecords;
import org.folio.circulation.support.results.Result;

public interface FindByIdsAndCombine<R> {
CompletableFuture<Result<MultipleRecords<R>>> findByIdsAndCombine(Collection<String> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.folio.circulation.support.results.Result;

public interface FindWithMultipleCqlIndexValues<T> {
CompletableFuture<Result<MultipleRecords<T>>> findByIds(
CompletableFuture<Result<MultipleRecords<T>>> findByIds(
Collection<String> ids);

CompletableFuture<Result<MultipleRecords<T>>> findByIdIndexAndQuery(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import static java.util.stream.Collectors.collectingAndThen;
import static org.apache.commons.collections4.ListUtils.partition;
import static org.folio.circulation.domain.MultipleRecords.empty;
import static org.folio.circulation.support.results.Result.of;
import static org.folio.circulation.support.fetching.MultipleCqlIndexValuesCriteria.byId;
import static org.folio.circulation.support.fetching.MultipleCqlIndexValuesCriteria.byIndex;
import static org.folio.circulation.support.http.client.PageLimit.maximumLimit;
import static org.folio.circulation.support.results.Result.of;

import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -18,8 +18,8 @@
import org.folio.circulation.domain.MultipleRecords;
import org.folio.circulation.support.FindWithCqlQuery;
import org.folio.circulation.support.FindWithMultipleCqlIndexValues;
import org.folio.circulation.support.results.Result;
import org.folio.circulation.support.http.client.CqlQuery;
import org.folio.circulation.support.results.Result;

import lombok.val;

Expand Down
Loading

0 comments on commit d2689f8

Please sign in to comment.