-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reindex): Initiate Full Reindex
Closes: MSEARCH-794
- Loading branch information
1 parent
60647a5
commit 2647269
Showing
62 changed files
with
1,234 additions
and
222 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
26 changes: 26 additions & 0 deletions
26
src/main/java/org/folio/search/client/ConsortiumTenantsClient.java
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,26 @@ | ||
package org.folio.search.client; | ||
|
||
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; | ||
|
||
import java.util.List; | ||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
@FeignClient("consortia") | ||
public interface ConsortiumTenantsClient { | ||
|
||
/** | ||
* Get tenants by consortium id. | ||
* | ||
* @return consortium tenants if executed under consortium central 'tenantId' context | ||
* */ | ||
@GetMapping(value = "/{consortiumId}/tenants", produces = APPLICATION_JSON_VALUE) | ||
ConsortiumTenants getConsortiumTenants(@PathVariable("consortiumId") String consortiumId, | ||
@RequestParam("limit") int limit); | ||
|
||
record ConsortiumTenants(List<ConsortiumTenant> tenants) { } | ||
|
||
record ConsortiumTenant(String id, boolean isCentral) { } | ||
} |
64 changes: 64 additions & 0 deletions
64
src/main/java/org/folio/search/client/InventoryClient.java
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,64 @@ | ||
package org.folio.search.client; | ||
|
||
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; | ||
import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM_VALUE; | ||
|
||
import org.folio.search.model.client.CqlQuery; | ||
import org.folio.search.model.service.ResultList; | ||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
@FeignClient | ||
public interface InventoryClient { | ||
|
||
@GetMapping( | ||
path = "/instance-storage/instances", | ||
consumes = APPLICATION_OCTET_STREAM_VALUE, | ||
produces = APPLICATION_JSON_VALUE) | ||
ResultList<InventoryInstanceDto> getInstances(@RequestParam("query") CqlQuery cql, | ||
@RequestParam("offset") int offset, | ||
@RequestParam("limit") int limit); | ||
|
||
@GetMapping(path = "/instance-storage/instances", produces = APPLICATION_JSON_VALUE) | ||
ResultList<InventoryInstanceDto> getInstances(@RequestParam("limit") int limit, | ||
@RequestParam("totalRecords") TotalRecordsType totalRecordsType); | ||
|
||
@GetMapping( | ||
path = "/item-storage/items", | ||
consumes = APPLICATION_OCTET_STREAM_VALUE, | ||
produces = APPLICATION_JSON_VALUE) | ||
ResultList<InventoryItemDto> getItems(@RequestParam("query") CqlQuery cql, | ||
@RequestParam("offset") int offset, | ||
@RequestParam("limit") int limit); | ||
|
||
@GetMapping(path = "/item-storage/items", produces = APPLICATION_JSON_VALUE) | ||
ResultList<InventoryItemDto> getItems(@RequestParam("limit") int limit, | ||
@RequestParam("totalRecords") TotalRecordsType totalRecordsType); | ||
|
||
@GetMapping( | ||
path = "/holdings-storage/holdings", | ||
consumes = APPLICATION_OCTET_STREAM_VALUE, | ||
produces = APPLICATION_JSON_VALUE) | ||
ResultList<InventoryHoldingDto> getHoldings(@RequestParam("query") CqlQuery cql, | ||
@RequestParam("offset") int offset, | ||
@RequestParam("limit") int limit); | ||
|
||
@GetMapping(path = "/holdings-storage/holdings", produces = APPLICATION_JSON_VALUE) | ||
ResultList<InventoryHoldingDto> getHoldings(@RequestParam("limit") int limit, | ||
@RequestParam("totalRecords") TotalRecordsType totalRecordsType); | ||
|
||
@PostMapping(path = "/inventory-reindex-records/publish", consumes = APPLICATION_JSON_VALUE) | ||
void publishReindexRecords(ReindexRecords reindexRecords); | ||
|
||
record InventoryInstanceDto(String id) {} | ||
|
||
record InventoryItemDto(String id) {} | ||
|
||
record InventoryHoldingDto(String id) {} | ||
|
||
record ReindexRecords(String id, String recordType, ReindexRecordsRange recordIdsRange) {} | ||
|
||
record ReindexRecordsRange(String from, String to) {} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/org/folio/search/client/TotalRecordsType.java
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,16 @@ | ||
package org.folio.search.client; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum TotalRecordsType { | ||
|
||
EXACT("exact"), | ||
ESTIMATED("estimated"), | ||
NONE("none"), | ||
AUTO("auto"); | ||
|
||
private final String value; | ||
} |
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
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
28 changes: 28 additions & 0 deletions
28
src/main/java/org/folio/search/exception/FolioIntegrationException.java
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,28 @@ | ||
package org.folio.search.exception; | ||
|
||
import static org.folio.search.model.types.ErrorCode.INTEGRATION_ERROR; | ||
|
||
/** | ||
* Handles exceptional cases of module integration with other Folio modules. | ||
*/ | ||
public class FolioIntegrationException extends BaseSearchException { | ||
|
||
/** | ||
* Initialize exception with provided message and error code. | ||
* | ||
* @param message exception message | ||
*/ | ||
public FolioIntegrationException(String message) { | ||
super(message, INTEGRATION_ERROR); | ||
} | ||
|
||
/** | ||
* Initialize exception with provided message and error code. | ||
* | ||
* @param message exception message | ||
* @param cause cause Exception | ||
*/ | ||
public FolioIntegrationException(String message, Throwable cause) { | ||
super(message, cause, INTEGRATION_ERROR); | ||
} | ||
} |
133 changes: 133 additions & 0 deletions
133
src/main/java/org/folio/search/integration/InventoryService.java
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,133 @@ | ||
package org.folio.search.integration; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.UUID; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.apache.commons.lang3.ObjectUtils; | ||
import org.folio.search.client.InventoryClient; | ||
import org.folio.search.client.TotalRecordsType; | ||
import org.folio.search.exception.FolioIntegrationException; | ||
import org.folio.search.model.client.CqlQuery; | ||
import org.folio.search.model.reindex.MergeRangeEntity; | ||
import org.folio.search.model.types.InventoryRecordType; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@Log4j2 | ||
public class InventoryService { | ||
|
||
private final InventoryClient inventoryClient; | ||
|
||
public InventoryService(InventoryClient inventoryClient) { | ||
this.inventoryClient = inventoryClient; | ||
} | ||
|
||
public List<UUID> fetchInventoryRecordIds(InventoryRecordType recordType, CqlQuery cqlQuery, int offset, int limit) { | ||
if (recordType == null) { | ||
log.warn("No Inventory Record Type was provided to fetch Inventory Record"); | ||
return Collections.emptyList(); | ||
} | ||
|
||
try { | ||
return switch (recordType) { | ||
case INSTANCE -> fetchInstances(cqlQuery, offset, limit); | ||
case ITEM -> fetchItems(cqlQuery, offset, limit); | ||
case HOLDING -> fetchHoldings(cqlQuery, offset, limit); | ||
}; | ||
} catch (Exception e) { | ||
log.warn("Failed to fetch Inventory records for {}", recordType); | ||
throw new FolioIntegrationException("Failed to fetch inventory records for %s".formatted(recordType.name()), e); | ||
} | ||
} | ||
|
||
public int fetchInventoryRecordCount(InventoryRecordType recordType) { | ||
if (recordType == null) { | ||
log.warn("No Inventory Record Type was provided to fetch Inventory Count"); | ||
return 0; | ||
} | ||
|
||
try { | ||
var result = switch (recordType) { | ||
case INSTANCE -> inventoryClient.getInstances(0, TotalRecordsType.EXACT); | ||
case ITEM -> inventoryClient.getItems(0, TotalRecordsType.EXACT); | ||
case HOLDING -> inventoryClient.getHoldings(0, TotalRecordsType.EXACT); | ||
}; | ||
|
||
if (result == null) { | ||
log.warn("Failed to retrieve Inventory Instances count"); | ||
return 0; | ||
} | ||
|
||
return result.getTotalRecords(); | ||
} catch (Exception e) { | ||
log.warn("Failed to fetch Inventory record counts for {}", recordType); | ||
throw new FolioIntegrationException( | ||
"Failed to fetch inventory record counts for %s".formatted(recordType.name()), e); | ||
} | ||
} | ||
|
||
public void publishReindexRecordsRange(MergeRangeEntity rangeEntity) { | ||
if (rangeEntity == null | ||
|| ObjectUtils.anyNull(rangeEntity.getId(), rangeEntity.getLowerId(), rangeEntity.getUpperId())) { | ||
log.warn("invalid Range Entity: [rangeEntity: {}]", rangeEntity); | ||
return; | ||
} | ||
|
||
var from = rangeEntity.getLowerId().toString(); | ||
var to = rangeEntity.getUpperId().toString(); | ||
var recordsRange = new InventoryClient.ReindexRecords( | ||
rangeEntity.getId().toString(), | ||
rangeEntity.getEntityType().name(), | ||
new InventoryClient.ReindexRecordsRange(from, to)); | ||
|
||
try { | ||
inventoryClient.publishReindexRecords(recordsRange); | ||
} catch (Exception e) { | ||
log.warn("Failed to publish reindex records range {}", recordsRange); | ||
throw new FolioIntegrationException("Failed to publish reindex records range", e); | ||
} | ||
} | ||
|
||
private List<UUID> fetchInstances(CqlQuery cqlQuery, int offset, int limit) { | ||
var result = inventoryClient.getInstances(cqlQuery, offset, limit); | ||
|
||
if (result == null) { | ||
log.warn("Failed to retrieve Inventory Instances, [query: {}, offset: {}, limit: {}]", cqlQuery, offset, limit); | ||
return Collections.emptyList(); | ||
} | ||
|
||
return result.getResult().stream() | ||
.map(InventoryClient.InventoryInstanceDto::id) | ||
.map(UUID::fromString) | ||
.toList(); | ||
} | ||
|
||
private List<UUID> fetchItems(CqlQuery cqlQuery, int offset, int limit) { | ||
var result = inventoryClient.getItems(cqlQuery, offset, limit); | ||
|
||
if (result == null) { | ||
log.warn("Failed to retrieve Inventory Items, [query: {}, offset: {}, limit: {}]", cqlQuery, offset, limit); | ||
return Collections.emptyList(); | ||
} | ||
|
||
return result.getResult().stream() | ||
.map(InventoryClient.InventoryItemDto::id) | ||
.map(UUID::fromString) | ||
.toList(); | ||
} | ||
|
||
private List<UUID> fetchHoldings(CqlQuery cqlQuery, int offset, int limit) { | ||
var result = inventoryClient.getHoldings(cqlQuery, offset, limit); | ||
|
||
if (result == null) { | ||
log.warn("Failed to retrieve Inventory Holdings, [query: {}, offset: {}, limit: {}]", cqlQuery, offset, limit); | ||
return Collections.emptyList(); | ||
} | ||
|
||
return result.getResult().stream() | ||
.map(InventoryClient.InventoryHoldingDto::id) | ||
.map(UUID::fromString) | ||
.toList(); | ||
} | ||
} |
Oops, something went wrong.