Skip to content

Commit

Permalink
Made calculation of the total records count optional
Browse files Browse the repository at this point in the history
  • Loading branch information
RuslanLavrov committed Feb 6, 2024
1 parent 3306c5f commit 0356e73
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,18 @@ public interface RecordDao {
* and returns {@link RecordsIdentifiersCollection} representing list of pairs of recordsId and externalId
*
* @param matchedField describes searching condition
* @param returnTotalRecords indicates that amount of total records should/shouldn't be calculated
* and populated into {@link RecordsIdentifiersCollection.totalRecords}
* @param typeConnection record type
* @param externalIdRequired specifies whether necessary not to consider records with {@code externalId == null} while searching
* @param offset offset to skip over a number of elements
* @param limit limit of records
* @param tenantId tenant id
* @return {@link Future} of {@link RecordsIdentifiersCollection}
*/
Future<RecordsIdentifiersCollection> getMatchedRecordsIdentifiers(MatchField matchedField, TypeConnection typeConnection, boolean externalIdRequired, int offset, int limit, String tenantId);
Future<RecordsIdentifiersCollection> getMatchedRecordsIdentifiers(MatchField matchedField, boolean returnTotalRecords,
TypeConnection typeConnection, boolean externalIdRequired,
int offset, int limit, String tenantId);

/**
* Streams {@link Record} by {@link Condition} and ordered by collection of {@link OrderField}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.jooq.impl.DSL.condition;
import static org.jooq.impl.DSL.countDistinct;
import static org.jooq.impl.DSL.field;
import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.DSL.max;
import static org.jooq.impl.DSL.name;
import static org.jooq.impl.DSL.primaryKey;
Expand Down Expand Up @@ -121,6 +122,7 @@
import org.jooq.SelectOnConditionStep;
import org.jooq.SortOrder;
import org.jooq.Table;
import org.jooq.TableLike;
import org.jooq.UpdateConditionStep;
import org.jooq.UpdateSetFirstStep;
import org.jooq.UpdateSetMoreStep;
Expand Down Expand Up @@ -495,25 +497,31 @@ private void appendJoinAlternative(SelectJoinStep<?> selectJoinStep, ParseLeader
}

@Override
public Future<RecordsIdentifiersCollection> getMatchedRecordsIdentifiers(MatchField matchedField, TypeConnection typeConnection,
boolean externalIdRequired, int offset, int limit, String tenantId) {
public Future<RecordsIdentifiersCollection> getMatchedRecordsIdentifiers(MatchField matchedField, boolean returnTotalRecords,
TypeConnection typeConnection, boolean externalIdRequired,
int offset, int limit, String tenantId) {
Table<org.jooq.Record> marcIndexersPartitionTable = table(name(MARC_INDEXERS_PARTITION_PREFIX + matchedField.getTag()));
if (matchedField.getValue() instanceof MissingValue) {
return Future.succeededFuture(new RecordsIdentifiersCollection().withTotalRecords(0));
}

return getQueryExecutor(tenantId).transaction(txQE -> txQE.query(dsl -> {
SelectConditionStep<Record1<Integer>> countQuery = select(countDistinct(RECORDS_LB.ID))
.from(RECORDS_LB)
.innerJoin(marcIndexersPartitionTable)
.on(RECORDS_LB.ID.eq(field(TABLE_FIELD_TEMPLATE, UUID.class, marcIndexersPartitionTable, name(MARC_ID))))
.innerJoin(MARC_RECORDS_TRACKING)
.on(MARC_RECORDS_TRACKING.MARC_ID.eq(field(TABLE_FIELD_TEMPLATE, UUID.class, marcIndexersPartitionTable, name(MARC_ID)))
.and(MARC_RECORDS_TRACKING.VERSION.eq(field(TABLE_FIELD_TEMPLATE, Integer.class, marcIndexersPartitionTable, name(VERSION)))))
.where(filterRecordByType(typeConnection.getRecordType().value())
.and(filterRecordByState(Record.State.ACTUAL.value()))
.and(externalIdRequired ? filterRecordByExternalIdNonNull() : DSL.noCondition())
.and(getMatchedFieldCondition(matchedField, marcIndexersPartitionTable.getName())));
TableLike<Record1<Integer>> countQuery;
if (returnTotalRecords) {
countQuery = select(countDistinct(RECORDS_LB.ID))
.from(RECORDS_LB)
.innerJoin(marcIndexersPartitionTable)
.on(RECORDS_LB.ID.eq(field(TABLE_FIELD_TEMPLATE, UUID.class, marcIndexersPartitionTable, name(MARC_ID))))
.innerJoin(MARC_RECORDS_TRACKING)
.on(MARC_RECORDS_TRACKING.MARC_ID.eq(field(TABLE_FIELD_TEMPLATE, UUID.class, marcIndexersPartitionTable, name(MARC_ID)))
.and(MARC_RECORDS_TRACKING.VERSION.eq(field(TABLE_FIELD_TEMPLATE, Integer.class, marcIndexersPartitionTable, name(VERSION)))))
.where(filterRecordByType(typeConnection.getRecordType().value())
.and(filterRecordByState(Record.State.ACTUAL.value()))
.and(externalIdRequired ? filterRecordByExternalIdNonNull() : DSL.noCondition())
.and(getMatchedFieldCondition(matchedField, marcIndexersPartitionTable.getName())));
} else {
countQuery = select(inline(null, Integer.class).as(COUNT));
}

SelectConditionStep<org.jooq.Record> searchQuery = dsl
.select(List.of(RECORDS_LB.ID, RECORDS_LB.EXTERNAL_ID))
Expand All @@ -535,12 +543,12 @@ public Future<RecordsIdentifiersCollection> getMatchedRecordsIdentifiers(MatchFi
.orderBy(searchQuery.field(ID).asc())
.offset(offset)
.limit(limit);
})).map(this::toRecordsIdentifiersCollection);
})).map(result -> toRecordsIdentifiersCollection(result, returnTotalRecords));
}

private RecordsIdentifiersCollection toRecordsIdentifiersCollection(QueryResult result) {
private RecordsIdentifiersCollection toRecordsIdentifiersCollection(QueryResult result, boolean returnTotalRecords) {
Integer countResult = asRow(result.unwrap()).getInteger(COUNT);
if (countResult == null || countResult == 0) {
if (returnTotalRecords && (countResult == null || countResult == 0)) {
return new RecordsIdentifiersCollection().withTotalRecords(0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public Future<RecordsIdentifiersCollection> getMatchedRecordsIdentifiers(RecordM
return processDefaultMatchField(matchField, tenantId, typeConnection,
recordMatchingDto.getOffset(), recordMatchingDto.getLimit());
}
return recordDao.getMatchedRecordsIdentifiers(matchField, typeConnection, true,
return recordDao.getMatchedRecordsIdentifiers(matchField, recordMatchingDto.getReturnTotalRecordsCount(), typeConnection, true,
recordMatchingDto.getOffset(), recordMatchingDto.getLimit(), tenantId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import static org.folio.rest.jaxrs.model.Record.RecordType.MARC_HOLDING;
import static org.hamcrest.Matchers.everyItem;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.oneOf;

@RunWith(VertxUnitRunner.class)
Expand Down Expand Up @@ -379,4 +380,44 @@ public void shouldReturnUnprocessableEntityIfValuesIsNotSpecified() {
.statusCode(HttpStatus.SC_UNPROCESSABLE_ENTITY);
}

@Test
public void shouldNotReturnTotalRecordsIfReturnTotalRecordsIsFalse(TestContext context) throws IOException {
String parsedRecordContent = new ObjectMapper()
.readValue(TestUtil.readFileFromPath(PARSED_MARC_WITH_035_FIELD_SAMPLE_PATH), JsonObject.class).encode();
int expectedRecordCount = 3;

for (int i = 0; i < expectedRecordCount; i++) {
String recordId = UUID.randomUUID().toString();
Record record = new Record()
.withId(recordId)
.withMatchedId(recordId)
.withSnapshotId(snapshot.getJobExecutionId())
.withGeneration(0)
.withRecordType(MARC_BIB)
.withRawRecord(new RawRecord().withId(recordId).withContent(rawRecordContent))
.withParsedRecord(new ParsedRecord().withId(recordId).withContent(parsedRecordContent))
.withExternalIdsHolder(new ExternalIdsHolder().withInstanceId(UUID.randomUUID().toString()));

postRecords(context, record);
}

RestAssured.given()
.spec(spec)
.when()
.body(new RecordMatchingDto()
.withRecordType(RecordMatchingDto.RecordType.MARC_BIB)
.withReturnTotalRecordsCount(false)
.withFilters(List.of(new Filter()
.withValues(List.of("(OCoLC)63611770", "1234567"))
.withField("035")
.withIndicator1("")
.withIndicator2("")
.withSubfield("a"))))
.post(RECORDS_MATCHING_PATH)
.then()
.statusCode(HttpStatus.SC_OK)
.body("totalRecords", nullValue())
.body("identifiers.size()", is(expectedRecordCount));
}

}

0 comments on commit 0356e73

Please sign in to comment.