Skip to content

Commit

Permalink
fix(reindex-upload): Fix hex IDs ranges generation for Subject, Class…
Browse files Browse the repository at this point in the history
…ification and Contribution in Reindex Upload (#710)

* fix(reindex): Fix hex IDs ranges generation for subject, classification and contributors to cover values starting from 'f'

Closes: MSEARCH-907
  • Loading branch information
mukhiddin-yusuf authored Dec 10, 2024
1 parent afc034a commit 5d20850
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* Populate typeId and sourceId on subjects re-indexing ([MSEARCH-891](https://folio-org.atlassian.net/browse/MSEARCH-891))
* Change browse_config type_ids column type from varchar to array ([MSEARCH-890](https://folio-org.atlassian.net/browse/MSEARCH-890))
* Fix old browse config returned on get after upsert ([MSEARCH-897](https://folio-org.atlassian.net/browse/MSEARCH-897))
* Fix generation of IDs ranges in Reindex Upload for Subject, Classification and Contributor ([MSEARCH-907](https://folio-org.atlassian.net/browse/MSEARCH-907))

### Tech Dept
* Description ([ISSUE](https://folio-org.atlassian.net/browse/ISSUE))
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/org/folio/search/service/reindex/RangeGenerator.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.folio.search.service.reindex;

import static java.lang.String.format;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;

public class RangeGenerator {

Expand Down Expand Up @@ -50,11 +53,15 @@ public static List<Range> createHexRanges(int length) {

// Loop from 0 to the maximum possible value with 'length' digits in hexadecimal
for (int i = 0; i < totalElements; i++) {
String lowerBound = String.format(formatString, i);
String upperBound = String.format(formatString, i + 1);
String lowerBound = format(formatString, i);
String upperBound = format(formatString, i + 1);
ranges.add(new Range(lowerBound, upperBound));
}

String lowerBound = format(formatString, totalElements);
String upperBound = StringUtils.repeat('x', length);
ranges.add(new Range(lowerBound, upperBound));

return ranges;
}

Expand All @@ -71,7 +78,7 @@ private static BigInteger upperUuidBound() {
}

private static String fromBigint(BigInteger bigint) {
return String.format("%032X", bigint);
return format("%032X", bigint);
}

public record Range(String lowerBound, String upperBound) { }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.folio.search.service.reindex;

import static org.apache.commons.lang3.StringUtils.repeat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.folio.spring.testing.type.UnitTest;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

@UnitTest
class RangeGeneratorTest {

@CsvSource({
"2,000001d8825cc3beb7e2cee2223565e17b26fac8",
"3,000001d8825cc3beb7e2cee2223565e17b26fac8",
"4,000001d8825cc3beb7e2cee2223565e17b26fac8",
"2,51703becc548d684616abd8ef7286917b7f7922d",
"3,51703becc548d684616abd8ef7286917b7f7922d",
"4,51703becc548d684616abd8ef7286917b7f7922d",
"2,ab7d50afb15f9474bd499cc2e9688c412ae11d48",
"3,ab7d50afb15f9474bd499cc2e9688c412ae11d48",
"4,ab7d50afb15f9474bd499cc2e9688c412ae11d48",
"2,e175fc5b818e1092be59370632c95fe590980f3d",
"3,e175fc5b818e1092be59370632c95fe590980f3d",
"4,e175fc5b818e1092be59370632c95fe590980f3d",
"2,fffffd04267fd060893a57167a54e2ad97432c04",
"3,fffffd04267fd060893a57167a54e2ad97432c04",
"4,fffffd04267fd060893a57167a54e2ad97432c04"
})
@ParameterizedTest(name = "generate hex ranges for length: {0}, hexValue: {1}")
void shouldGenerateHexRanges(int length, String hexValue) {
var ranges = RangeGenerator.createHexRanges(length);

assertThat(ranges)
.satisfies(hexRanges ->
assertTrue(
hexRanges.stream()
.anyMatch(range -> hexValue.compareTo(range.lowerBound()) > 0 && hexValue.compareTo(range.upperBound()) < 0)
)
)
.extracting(RangeGenerator.Range::lowerBound, RangeGenerator.Range::upperBound)
.startsWith(tuple(repeat("0", length), repeat("0", length - 1) + "1"))
.endsWith(tuple(repeat("f", length), repeat("x", length)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ void getUploadRanges_returnList_whenNoUploadRangesAndNotPopulate() {

// assert
assertThat(ranges)
.hasSize(15)
.hasSize(16)
.are(new Condition<>(range -> range.getEntityType() == ReindexEntityType.SUBJECT, "subject range"))
.extracting(UploadRangeEntity::getLower, UploadRangeEntity::getUpper)
.startsWith(tuple("0", "1"))
.contains(tuple("a", "b"))
.endsWith(tuple("e", "f"));
.endsWith(tuple("f", "x"));
}

@Test
Expand Down

0 comments on commit 5d20850

Please sign in to comment.