Minor cleanup in some Facet tests (#13489)

This commit is contained in:
Jakub Slowinski 2024-07-10 17:05:09 +01:00 committed by GitHub
parent 428fdb5291
commit 49e781084a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
17 changed files with 237 additions and 476 deletions

View File

@ -20,7 +20,6 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -85,7 +84,8 @@ public abstract class FacetTestCase extends LuceneTestCase {
* @param docId docId for which facet labels are needed. * @param docId docId for which facet labels are needed.
* @param dimension Retain facet labels for supplied dimension only. A null value fetches all * @param dimension Retain facet labels for supplied dimension only. A null value fetches all
* facet labels. * facet labels.
* @param facetLabelReader {@FacetLabelReader} instance use to get facet labels for input docId. * @param facetLabelReader {@link FacetLabelReader} instance use to get facet labels for input
* docId.
* @return {@code List<FacetLabel>} containing matching facet labels. * @return {@code List<FacetLabel>} containing matching facet labels.
* @throws IOException when a low-level IO issue occurs while reading facet labels. * @throws IOException when a low-level IO issue occurs while reading facet labels.
*/ */
@ -178,12 +178,9 @@ public abstract class FacetTestCase extends LuceneTestCase {
labelValues, labelValues,
i - numInRow, i - numInRow,
i, i,
new Comparator<LabelAndValue>() { (a, b) -> {
@Override assert a.value.doubleValue() == b.value.doubleValue();
public int compare(LabelAndValue a, LabelAndValue b) { return new BytesRef(a.label).compareTo(new BytesRef(b.label));
assert a.value.doubleValue() == b.value.doubleValue();
return new BytesRef(a.label).compareTo(new BytesRef(b.label));
}
}); });
} }
numInRow = 1; numInRow = 1;
@ -198,16 +195,13 @@ public abstract class FacetTestCase extends LuceneTestCase {
protected void sortLabelValues(List<LabelAndValue> labelValues) { protected void sortLabelValues(List<LabelAndValue> labelValues) {
Collections.sort( Collections.sort(
labelValues, labelValues,
new Comparator<LabelAndValue>() { (a, b) -> {
@Override if (a.value.doubleValue() > b.value.doubleValue()) {
public int compare(LabelAndValue a, LabelAndValue b) { return -1;
if (a.value.doubleValue() > b.value.doubleValue()) { } else if (a.value.doubleValue() < b.value.doubleValue()) {
return -1; return 1;
} else if (a.value.doubleValue() < b.value.doubleValue()) { } else {
return 1; return new BytesRef(a.label).compareTo(new BytesRef(b.label));
} else {
return new BytesRef(a.label).compareTo(new BytesRef(b.label));
}
} }
}); });
} }
@ -215,16 +209,13 @@ public abstract class FacetTestCase extends LuceneTestCase {
protected void sortFacetResults(List<FacetResult> results) { protected void sortFacetResults(List<FacetResult> results) {
Collections.sort( Collections.sort(
results, results,
new Comparator<FacetResult>() { (a, b) -> {
@Override if (a.value.doubleValue() > b.value.doubleValue()) {
public int compare(FacetResult a, FacetResult b) { return -1;
if (a.value.doubleValue() > b.value.doubleValue()) { } else if (b.value.doubleValue() > a.value.doubleValue()) {
return -1; return 1;
} else if (b.value.doubleValue() > a.value.doubleValue()) { } else {
return 1; return a.dim.compareTo(b.dim);
} else {
return a.dim.compareTo(b.dim);
}
} }
}); });
} }

View File

@ -80,14 +80,12 @@ public class TestLongValueFacetCounts extends FacetTestCase {
new String[0], new String[0],
6, 6,
101, 101,
new LabelAndValue[] { new LabelAndValue("0", 20),
new LabelAndValue("0", 20), new LabelAndValue("1", 20),
new LabelAndValue("1", 20), new LabelAndValue("2", 20),
new LabelAndValue("2", 20), new LabelAndValue("3", 20),
new LabelAndValue("3", 20), new LabelAndValue("4", 20),
new LabelAndValue("4", 20), new LabelAndValue("9223372036854775807", 1));
new LabelAndValue("9223372036854775807", 1)
});
r.close(); r.close();
d.close(); d.close();
@ -123,9 +121,8 @@ public class TestLongValueFacetCounts extends FacetTestCase {
new String[0], new String[0],
2, 2,
9, 9,
new LabelAndValue[] { new LabelAndValue("0", 4),
new LabelAndValue("0", 4), new LabelAndValue("1", 5), new LabelAndValue("1", 5));
});
r.close(); r.close();
d.close(); d.close();
@ -156,11 +153,9 @@ public class TestLongValueFacetCounts extends FacetTestCase {
new String[0], new String[0],
3, 3,
3, 3,
new LabelAndValue[] { new LabelAndValue("9223372036854775805", 1),
new LabelAndValue("9223372036854775805", 1), new LabelAndValue("9223372036854775806", 1),
new LabelAndValue("9223372036854775806", 1), new LabelAndValue("9223372036854775807", 1));
new LabelAndValue("9223372036854775807", 1)
});
// since we have no insight into the value order in the hashMap, we sort labels by value and // since we have no insight into the value order in the hashMap, we sort labels by value and
// count in // count in
@ -221,11 +216,7 @@ public class TestLongValueFacetCounts extends FacetTestCase {
List<FacetResult> topDimsResults2 = facets.getTopDims(0, 1); List<FacetResult> topDimsResults2 = facets.getTopDims(0, 1);
assertEquals(0, topDimsResults2.size()); assertEquals(0, topDimsResults2.size());
// test getAllDims(0) // test getAllDims(0)
expectThrows( expectThrows(IllegalArgumentException.class, () -> facets.getAllDims(0));
IllegalArgumentException.class,
() -> {
facets.getAllDims(0);
});
r.close(); r.close();
d.close(); d.close();
@ -364,8 +355,7 @@ public class TestLongValueFacetCounts extends FacetTestCase {
// test getAllChildren // test getAllChildren
expectedCounts.sort( expectedCounts.sort(
Comparator.comparing((Map.Entry<Long, Integer> a) -> a.getKey()) Map.Entry.<Long, Integer>comparingByKey().thenComparingLong(Map.Entry::getValue));
.thenComparingLong(Map.Entry::getValue));
FacetResult allChildren = facetCounts.getAllChildren("field"); FacetResult allChildren = facetCounts.getAllChildren("field");
// sort labels by value, count in ascending order // sort labels by value, count in ascending order
Arrays.sort( Arrays.sort(
@ -627,8 +617,7 @@ public class TestLongValueFacetCounts extends FacetTestCase {
// test getAllChildren // test getAllChildren
expectedCounts.sort( expectedCounts.sort(
Comparator.comparing((Map.Entry<Long, Integer> a) -> a.getKey()) Map.Entry.<Long, Integer>comparingByKey().thenComparingLong(Map.Entry::getValue));
.thenComparingLong(Map.Entry::getValue));
FacetResult allChildren = facetCounts.getAllChildren("field"); FacetResult allChildren = facetCounts.getAllChildren("field");
// sort labels by value, count in ascending order // sort labels by value, count in ascending order
Arrays.sort( Arrays.sort(
@ -833,9 +822,8 @@ public class TestLongValueFacetCounts extends FacetTestCase {
new String[0], new String[0],
2, 2,
2, 2,
new LabelAndValue[] { new LabelAndValue("42", 1),
new LabelAndValue("42", 1), new LabelAndValue("43", 1), new LabelAndValue("43", 1));
});
r.close(); r.close();
dir.close(); dir.close();

View File

@ -86,7 +86,7 @@ public class TestMultipleIndexFields extends FacetTestCase {
// prepare searcher to search against // prepare searcher to search against
IndexSearcher searcher = newSearcher(ir); IndexSearcher searcher = newSearcher(ir);
FacetsCollector sfc = performSearch(tr, ir, searcher); FacetsCollector sfc = performSearch(searcher);
// Obtain facets results and hand-test them // Obtain facets results and hand-test them
assertCorrectResults(getTaxonomyFacetCounts(tr, config, sfc)); assertCorrectResults(getTaxonomyFacetCounts(tr, config, sfc));
@ -124,7 +124,7 @@ public class TestMultipleIndexFields extends FacetTestCase {
// prepare searcher to search against // prepare searcher to search against
IndexSearcher searcher = newSearcher(ir); IndexSearcher searcher = newSearcher(ir);
FacetsCollector sfc = performSearch(tr, ir, searcher); FacetsCollector sfc = performSearch(searcher);
Map<String, Facets> facetsMap = new HashMap<>(); Map<String, Facets> facetsMap = new HashMap<>();
facetsMap.put("Author", getTaxonomyFacetCounts(tr, config, sfc, "$author")); facetsMap.put("Author", getTaxonomyFacetCounts(tr, config, sfc, "$author"));
@ -168,7 +168,7 @@ public class TestMultipleIndexFields extends FacetTestCase {
// prepare searcher to search against // prepare searcher to search against
IndexSearcher searcher = newSearcher(ir); IndexSearcher searcher = newSearcher(ir);
FacetsCollector sfc = performSearch(tr, ir, searcher); FacetsCollector sfc = performSearch(searcher);
Map<String, Facets> facetsMap = new HashMap<>(); Map<String, Facets> facetsMap = new HashMap<>();
Facets facets2 = getTaxonomyFacetCounts(tr, config, sfc, "$music"); Facets facets2 = getTaxonomyFacetCounts(tr, config, sfc, "$music");
@ -225,7 +225,7 @@ public class TestMultipleIndexFields extends FacetTestCase {
// prepare searcher to search against // prepare searcher to search against
IndexSearcher searcher = newSearcher(ir); IndexSearcher searcher = newSearcher(ir);
FacetsCollector sfc = performSearch(tr, ir, searcher); FacetsCollector sfc = performSearch(searcher);
Map<String, Facets> facetsMap = new HashMap<>(); Map<String, Facets> facetsMap = new HashMap<>();
facetsMap.put("Band", getTaxonomyFacetCounts(tr, config, sfc, "$bands")); facetsMap.put("Band", getTaxonomyFacetCounts(tr, config, sfc, "$bands"));
@ -271,7 +271,7 @@ public class TestMultipleIndexFields extends FacetTestCase {
// prepare searcher to search against // prepare searcher to search against
IndexSearcher searcher = newSearcher(ir); IndexSearcher searcher = newSearcher(ir);
FacetsCollector sfc = performSearch(tr, ir, searcher); FacetsCollector sfc = performSearch(searcher);
Map<String, Facets> facetsMap = new HashMap<>(); Map<String, Facets> facetsMap = new HashMap<>();
Facets facets2 = getTaxonomyFacetCounts(tr, config, sfc, "$music"); Facets facets2 = getTaxonomyFacetCounts(tr, config, sfc, "$music");
@ -300,9 +300,8 @@ public class TestMultipleIndexFields extends FacetTestCase {
new String[0], new String[0],
2, 2,
5, 5,
new LabelAndValue[] { new LabelAndValue("Punk", 1),
new LabelAndValue("Punk", 1), new LabelAndValue("Rock & Pop", 4), new LabelAndValue("Rock & Pop", 4));
});
assertEquals( assertEquals(
"dim=Band path=[Rock & Pop] value=4 childCount=4\n The Beatles (1)\n U2 (1)\n REM (1)\n Dave Matthews Band (1)\n", "dim=Band path=[Rock & Pop] value=4 childCount=4\n The Beatles (1)\n U2 (1)\n REM (1)\n Dave Matthews Band (1)\n",
facets.getTopChildren(10, "Band", "Rock & Pop").toString()); facets.getTopChildren(10, "Band", "Rock & Pop").toString());
@ -312,12 +311,10 @@ public class TestMultipleIndexFields extends FacetTestCase {
new String[] {"Rock & Pop"}, new String[] {"Rock & Pop"},
4, 4,
4, 4,
new LabelAndValue[] { new LabelAndValue("Dave Matthews Band", 1),
new LabelAndValue("Dave Matthews Band", 1), new LabelAndValue("REM", 1),
new LabelAndValue("REM", 1), new LabelAndValue("The Beatles", 1),
new LabelAndValue("The Beatles", 1), new LabelAndValue("U2", 1));
new LabelAndValue("U2", 1),
});
assertEquals( assertEquals(
"dim=Author path=[] value=3 childCount=3\n Mark Twain (1)\n Stephen King (1)\n Kurt Vonnegut (1)\n", "dim=Author path=[] value=3 childCount=3\n Mark Twain (1)\n Stephen King (1)\n Kurt Vonnegut (1)\n",
@ -328,15 +325,12 @@ public class TestMultipleIndexFields extends FacetTestCase {
new String[0], new String[0],
3, 3,
3, 3,
new LabelAndValue[] { new LabelAndValue("Kurt Vonnegut", 1),
new LabelAndValue("Kurt Vonnegut", 1), new LabelAndValue("Mark Twain", 1),
new LabelAndValue("Mark Twain", 1), new LabelAndValue("Stephen King", 1));
new LabelAndValue("Stephen King", 1),
});
} }
private FacetsCollector performSearch(TaxonomyReader tr, IndexReader ir, IndexSearcher searcher) private FacetsCollector performSearch(IndexSearcher searcher) throws IOException {
throws IOException {
FacetsCollector fc = new FacetsCollector(); FacetsCollector fc = new FacetsCollector();
FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc); FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc);
return fc; return fc;

View File

@ -119,7 +119,7 @@ public class TestRandomSamplingFacetsCollector extends FacetTestCase {
float ei = (float) md.totalHits / totalHits; float ei = (float) md.totalHits / totalHits;
if (ei > 0.0f) { if (ei > 0.0f) {
float oi = (float) numSampledDocs[i] / totalSampledDocs; float oi = (float) numSampledDocs[i] / totalSampledDocs;
chi_square += (Math.pow(ei - oi, 2) / ei); chi_square += (float) (Math.pow(ei - oi, 2) / ei);
} }
} }

View File

@ -468,11 +468,7 @@ public class TestStringValueFacetCounts extends FacetTestCase {
assertEquals(facetResult, topNDimsResult.get(0)); assertEquals(facetResult, topNDimsResult.get(0));
// test getAllDims(0) // test getAllDims(0)
expectThrows( expectThrows(IllegalArgumentException.class, () -> facets.getAllDims(0));
IllegalArgumentException.class,
() -> {
facets.getAllDims(0);
});
// This is a little strange, but we request all labels at this point so that when we // This is a little strange, but we request all labels at this point so that when we
// secondarily sort by label value in order to compare to the expected results, we have // secondarily sort by label value in order to compare to the expected results, we have
@ -538,8 +534,7 @@ public class TestStringValueFacetCounts extends FacetTestCase {
// sort expected counts by value, count // sort expected counts by value, count
expectedCountsSortedByValue.sort( expectedCountsSortedByValue.sort(
Comparator.comparing((Map.Entry<String, Integer> a) -> a.getKey()) Map.Entry.<String, Integer>comparingByKey().thenComparingInt(Map.Entry::getValue));
.thenComparingInt(Map.Entry::getValue));
FacetResult facetResult = facets.getAllChildren("field"); FacetResult facetResult = facets.getAllChildren("field");
assertEquals(expectedTotalDocsWithValue, facetResult.value); assertEquals(expectedTotalDocsWithValue, facetResult.value);

View File

@ -112,11 +112,7 @@ public class TestRangeFacetCounts extends FacetTestCase {
result.toString()); result.toString());
// test getTopChildren(0, dim) // test getTopChildren(0, dim)
expectThrows( expectThrows(IllegalArgumentException.class, () -> facets.getTopChildren(0, "field"));
IllegalArgumentException.class,
() -> {
facets.getTopChildren(0, "field");
});
r.close(); r.close();
d.close(); d.close();
@ -169,11 +165,7 @@ public class TestRangeFacetCounts extends FacetTestCase {
result.toString()); result.toString());
// test getTopChildren(0, dim) // test getTopChildren(0, dim)
expectThrows( expectThrows(IllegalArgumentException.class, () -> facets.getTopChildren(0, "field"));
IllegalArgumentException.class,
() -> {
facets.getTopChildren(0, "field");
});
r.close(); r.close();
d.close(); d.close();
@ -287,37 +279,19 @@ public class TestRangeFacetCounts extends FacetTestCase {
assertEquals(0, topNDimsResult.size()); assertEquals(0, topNDimsResult.size());
// test getAllDims(0) // test getAllDims(0)
expectThrows( expectThrows(IllegalArgumentException.class, () -> facets.getAllDims(0));
IllegalArgumentException.class,
() -> {
facets.getAllDims(0);
});
r.close(); r.close();
d.close(); d.close();
} }
public void testUselessRange() { public void testUselessRange() {
expectThrows(IllegalArgumentException.class, () -> new LongRange("useless", 7, true, 6, true));
expectThrows(IllegalArgumentException.class, () -> new LongRange("useless", 7, true, 7, false));
expectThrows( expectThrows(
IllegalArgumentException.class, IllegalArgumentException.class, () -> new DoubleRange("useless", 7.0, true, 6.0, true));
() -> {
new LongRange("useless", 7, true, 6, true);
});
expectThrows( expectThrows(
IllegalArgumentException.class, IllegalArgumentException.class, () -> new DoubleRange("useless", 7.0, true, 7.0, false));
() -> {
new LongRange("useless", 7, true, 7, false);
});
expectThrows(
IllegalArgumentException.class,
() -> {
new DoubleRange("useless", 7.0, true, 6.0, true);
});
expectThrows(
IllegalArgumentException.class,
() -> {
new DoubleRange("useless", 7.0, true, 7.0, false);
});
} }
public void testLongMinMax() throws Exception { public void testLongMinMax() throws Exception {

View File

@ -100,11 +100,7 @@ public class TestRangeOnRangeFacetCounts extends FacetTestCase {
result.toString()); result.toString());
// test getTopChildren(0, dim) // test getTopChildren(0, dim)
expectThrows( expectThrows(IllegalArgumentException.class, () -> facets.getTopChildren(0, "field"));
IllegalArgumentException.class,
() -> {
facets.getTopChildren(0, "field");
});
r.close(); r.close();
d.close(); d.close();
@ -160,11 +156,7 @@ public class TestRangeOnRangeFacetCounts extends FacetTestCase {
result.toString()); result.toString());
// test getTopChildren(0, dim) // test getTopChildren(0, dim)
expectThrows( expectThrows(IllegalArgumentException.class, () -> facets.getTopChildren(0, "field"));
IllegalArgumentException.class,
() -> {
facets.getTopChildren(0, "field");
});
r.close(); r.close();
d.close(); d.close();
@ -224,11 +216,7 @@ public class TestRangeOnRangeFacetCounts extends FacetTestCase {
assertEquals(0, topNDimsResult.size()); assertEquals(0, topNDimsResult.size());
// test getAllDims(0) // test getAllDims(0)
expectThrows( expectThrows(IllegalArgumentException.class, () -> facets.getAllDims(0));
IllegalArgumentException.class,
() -> {
facets.getAllDims(0);
});
r.close(); r.close();
d.close(); d.close();
@ -289,60 +277,34 @@ public class TestRangeOnRangeFacetCounts extends FacetTestCase {
result.get(0).toString()); result.get(0).toString());
// test getTopChildren(0, dim) // test getTopChildren(0, dim)
expectThrows( expectThrows(IllegalArgumentException.class, () -> facets.getTopChildren(0, "field"));
IllegalArgumentException.class,
() -> {
facets.getTopChildren(0, "field");
});
r.close(); r.close();
d.close(); d.close();
} }
public void testUselessRangeSingleDim() { public void testUselessRangeSingleDim() {
expectThrows(IllegalArgumentException.class, () -> new LongRange("useless", 7, true, 6, true));
expectThrows(IllegalArgumentException.class, () -> new LongRange("useless", 7, true, 7, false));
expectThrows( expectThrows(
IllegalArgumentException.class, IllegalArgumentException.class, () -> new DoubleRange("useless", 7.0, true, 6.0, true));
() -> {
new LongRange("useless", 7, true, 6, true);
});
expectThrows( expectThrows(
IllegalArgumentException.class, IllegalArgumentException.class, () -> new DoubleRange("useless", 7.0, true, 7.0, false));
() -> {
new LongRange("useless", 7, true, 7, false);
});
expectThrows(
IllegalArgumentException.class,
() -> {
new DoubleRange("useless", 7.0, true, 6.0, true);
});
expectThrows(
IllegalArgumentException.class,
() -> {
new DoubleRange("useless", 7.0, true, 7.0, false);
});
} }
public void testUselessMultiDimRange() { public void testUselessMultiDimRange() {
expectThrows( expectThrows(
IllegalArgumentException.class, IllegalArgumentException.class,
() -> { () -> new LongRange("useless", longArray(7L, 7L), longArray(6L, 6L)));
new LongRange("useless", longArray(7L, 7L), longArray(6L, 6L));
});
expectThrows( expectThrows(
IllegalArgumentException.class, IllegalArgumentException.class,
() -> { () -> new LongRange("useless", longArray(7L, 7L), longArray(7L, 6L)));
new LongRange("useless", longArray(7L, 7L), longArray(7L, 6L));
});
expectThrows( expectThrows(
IllegalArgumentException.class, IllegalArgumentException.class,
() -> { () -> new DoubleRange("useless", doubleArray(7.0, 7.0), doubleArray(6.0, 6.0)));
new DoubleRange("useless", doubleArray(7.0, 7.0), doubleArray(6.0, 6.0));
});
expectThrows( expectThrows(
IllegalArgumentException.class, IllegalArgumentException.class,
() -> { () -> new DoubleRange("useless", doubleArray(7.0, 7.0), doubleArray(7.0, 6.0)));
new DoubleRange("useless", doubleArray(7.0, 7.0), doubleArray(7.0, 6.0));
});
} }
public void testSingleDimLongMinMax() throws Exception { public void testSingleDimLongMinMax() throws Exception {
@ -769,11 +731,7 @@ public class TestRangeOnRangeFacetCounts extends FacetTestCase {
result.toString()); result.toString());
// test getTopChildren(0, dim) // test getTopChildren(0, dim)
expectThrows( expectThrows(IllegalArgumentException.class, () -> facets.getTopChildren(0, "field"));
IllegalArgumentException.class,
() -> {
facets.getTopChildren(0, "field");
});
IOUtils.close(r, d); IOUtils.close(r, d);
} }
@ -830,11 +788,7 @@ public class TestRangeOnRangeFacetCounts extends FacetTestCase {
result.toString()); result.toString());
// test getTopChildren(0, dim) // test getTopChildren(0, dim)
expectThrows( expectThrows(IllegalArgumentException.class, () -> facets.getTopChildren(0, "field"));
IllegalArgumentException.class,
() -> {
facets.getTopChildren(0, "field");
});
IOUtils.close(r, d); IOUtils.close(r, d);
} }

View File

@ -160,88 +160,43 @@ public class TestFacetLabel extends FacetTestCase {
// empty or null components should not be allowed. // empty or null components should not be allowed.
for (String[] components : components_tests) { for (String[] components : components_tests) {
expectThrows(IllegalArgumentException.class, () -> new FacetLabel(components));
expectThrows(IllegalArgumentException.class, () -> new FacetField("dim", components));
expectThrows( expectThrows(
IllegalArgumentException.class, IllegalArgumentException.class,
() -> { () -> new AssociationFacetField(new BytesRef(), "dim", components));
new FacetLabel(components);
});
expectThrows( expectThrows(
IllegalArgumentException.class, IllegalArgumentException.class,
() -> { () -> new IntAssociationFacetField(17, "dim", components));
new FacetField("dim", components);
});
expectThrows( expectThrows(
IllegalArgumentException.class, IllegalArgumentException.class,
() -> { () -> new FloatAssociationFacetField(17.0f, "dim", components));
new AssociationFacetField(new BytesRef(), "dim", components);
});
expectThrows(
IllegalArgumentException.class,
() -> {
new IntAssociationFacetField(17, "dim", components);
});
expectThrows(
IllegalArgumentException.class,
() -> {
new FloatAssociationFacetField(17.0f, "dim", components);
});
} }
expectThrows(IllegalArgumentException.class, () -> new FacetField(null, new String[] {"abc"}));
expectThrows(IllegalArgumentException.class, () -> new FacetField("", "abc"));
expectThrows( expectThrows(
IllegalArgumentException.class, IllegalArgumentException.class,
() -> { () -> new IntAssociationFacetField(17, null, new String[] {"abc"}));
new FacetField(null, new String[] {"abc"});
});
expectThrows( expectThrows(
IllegalArgumentException.class, IllegalArgumentException.class,
() -> { () -> new IntAssociationFacetField(17, "", new String[] {"abc"}));
new FacetField("", new String[] {"abc"});
});
expectThrows( expectThrows(
IllegalArgumentException.class, IllegalArgumentException.class,
() -> { () -> new FloatAssociationFacetField(17.0f, null, new String[] {"abc"}));
new IntAssociationFacetField(17, null, new String[] {"abc"});
});
expectThrows( expectThrows(
IllegalArgumentException.class, IllegalArgumentException.class,
() -> { () -> new FloatAssociationFacetField(17.0f, "", new String[] {"abc"}));
new IntAssociationFacetField(17, "", new String[] {"abc"});
});
expectThrows( expectThrows(
IllegalArgumentException.class, IllegalArgumentException.class,
() -> { () -> new AssociationFacetField(new BytesRef(), null, "abc"));
new FloatAssociationFacetField(17.0f, null, new String[] {"abc"});
});
expectThrows( expectThrows(
IllegalArgumentException.class, IllegalArgumentException.class,
() -> { () -> new AssociationFacetField(new BytesRef(), "", new String[] {"abc"}));
new FloatAssociationFacetField(17.0f, "", new String[] {"abc"});
});
expectThrows( expectThrows(
IllegalArgumentException.class, IllegalArgumentException.class, () -> new SortedSetDocValuesFacetField(null, "abc"));
() -> { expectThrows(IllegalArgumentException.class, () -> new SortedSetDocValuesFacetField("", "abc"));
new AssociationFacetField(new BytesRef(), null, new String[] {"abc"}); expectThrows(IllegalArgumentException.class, () -> new SortedSetDocValuesFacetField("dim", ""));
});
expectThrows(
IllegalArgumentException.class,
() -> {
new AssociationFacetField(new BytesRef(), "", new String[] {"abc"});
});
expectThrows(
IllegalArgumentException.class,
() -> {
new SortedSetDocValuesFacetField(null, "abc");
});
expectThrows(
IllegalArgumentException.class,
() -> {
new SortedSetDocValuesFacetField("", "abc");
});
expectThrows(
IllegalArgumentException.class,
() -> {
new SortedSetDocValuesFacetField("dim", "");
});
} }
@Test @Test
@ -258,10 +213,6 @@ public class TestFacetLabel extends FacetTestCase {
// long paths should not be allowed // long paths should not be allowed
final String longPath = bigComp; final String longPath = bigComp;
expectThrows( expectThrows(IllegalArgumentException.class, () -> new FacetLabel("dim", longPath));
IllegalArgumentException.class,
() -> {
new FacetLabel("dim", longPath);
});
} }
} }

View File

@ -287,11 +287,7 @@ public class TestSearcherTaxonomyManager extends FacetTestCase {
tw.replaceTaxonomy(taxoDir2); tw.replaceTaxonomy(taxoDir2);
taxoDir2.close(); taxoDir2.close();
expectThrows( expectThrows(IllegalStateException.class, mgr::maybeRefresh);
IllegalStateException.class,
() -> {
mgr.maybeRefresh();
});
w.close(); w.close();
IOUtils.close(mgr, tw, taxoDir, dir); IOUtils.close(mgr, tw, taxoDir, dir);

View File

@ -150,7 +150,7 @@ public class TestTaxonomyCombined extends FacetTestCase {
if (path.length == 0) { if (path.length == 0) {
return "<empty>"; return "<empty>";
} }
return "<" + path.toString() + ">"; return "<" + path + ">";
} }
/** /**
@ -525,21 +525,10 @@ public class TestTaxonomyCombined extends FacetTestCase {
} }
// check parent of of invalid ordinals: // check parent of of invalid ordinals:
expectThrows(IndexOutOfBoundsException.class, () -> tw.getParent(-1));
expectThrows( expectThrows(
IndexOutOfBoundsException.class, IndexOutOfBoundsException.class, () -> tw.getParent(TaxonomyReader.INVALID_ORDINAL));
() -> { expectThrows(IndexOutOfBoundsException.class, () -> tw.getParent(tr.getSize()));
tw.getParent(-1);
});
expectThrows(
IndexOutOfBoundsException.class,
() -> {
tw.getParent(TaxonomyReader.INVALID_ORDINAL);
});
expectThrows(
IndexOutOfBoundsException.class,
() -> {
tw.getParent(tr.getSize());
});
} }
/** /**

View File

@ -227,9 +227,8 @@ public class TestTaxonomyFacetAssociations extends FacetTestCase {
2, 2,
-1, -1,
Map.of("a", 100, "b", 50), Map.of("a", 100, "b", 50),
new LabelAndValue[] { new LabelAndValue("a", 200),
new LabelAndValue("a", 200), new LabelAndValue("b", 150), new LabelAndValue("b", 150));
});
assertEquals( assertEquals(
"Wrong count for category 'a'!", 200, facets.getSpecificValue("int", "a").intValue()); "Wrong count for category 'a'!", 200, facets.getSpecificValue("int", "a").intValue());
assertEquals( assertEquals(
@ -311,9 +310,8 @@ public class TestTaxonomyFacetAssociations extends FacetTestCase {
2, 2,
-1f, -1f,
Map.of("a", 100, "b", 50), Map.of("a", 100, "b", 50),
new LabelAndValue[] { new LabelAndValue("a", 50.0f),
new LabelAndValue("a", 50.0f), new LabelAndValue("b", 9.999995f), new LabelAndValue("b", 9.999995f));
});
assertEquals( assertEquals(
"Wrong count for category 'a'!", "Wrong count for category 'a'!",
@ -424,23 +422,11 @@ public class TestTaxonomyFacetAssociations extends FacetTestCase {
Facets facets = Facets facets =
new TaxonomyFacetFloatAssociations( new TaxonomyFacetFloatAssociations(
"wrong_field", taxoReader, config, fc, AssociationAggregationFunction.SUM); "wrong_field", taxoReader, config, fc, AssociationAggregationFunction.SUM);
expectThrows( expectThrows(IllegalArgumentException.class, () -> facets.getSpecificValue("float"));
IllegalArgumentException.class,
() -> {
facets.getSpecificValue("float");
});
expectThrows( expectThrows(IllegalArgumentException.class, () -> facets.getTopChildren(10, "float"));
IllegalArgumentException.class,
() -> {
facets.getTopChildren(10, "float");
});
expectThrows( expectThrows(IllegalArgumentException.class, () -> facets.getAllChildren("float"));
IllegalArgumentException.class,
() -> {
facets.getAllChildren("float");
});
} }
public void testMixedTypesInSameIndexField() throws Exception { public void testMixedTypesInSameIndexField() throws Exception {
@ -455,10 +441,7 @@ public class TestTaxonomyFacetAssociations extends FacetTestCase {
doc.add(new IntAssociationFacetField(14, "a", "x")); doc.add(new IntAssociationFacetField(14, "a", "x"));
doc.add(new FloatAssociationFacetField(55.0f, "b", "y")); doc.add(new FloatAssociationFacetField(55.0f, "b", "y"));
expectThrows( expectThrows(
IllegalArgumentException.class, IllegalArgumentException.class, () -> writer.addDocument(config.build(taxoWriter, doc)));
() -> {
writer.addDocument(config.build(taxoWriter, doc));
});
writer.close(); writer.close();
IOUtils.close(taxoWriter, dir, taxoDir); IOUtils.close(taxoWriter, dir, taxoDir);
} }
@ -475,10 +458,7 @@ public class TestTaxonomyFacetAssociations extends FacetTestCase {
Document doc = new Document(); Document doc = new Document();
doc.add(new IntAssociationFacetField(14, "a", "x")); doc.add(new IntAssociationFacetField(14, "a", "x"));
expectThrows( expectThrows(
IllegalArgumentException.class, IllegalArgumentException.class, () -> writer.addDocument(config.build(taxoWriter, doc)));
() -> {
writer.addDocument(config.build(taxoWriter, doc));
});
writer.close(); writer.close();
IOUtils.close(taxoWriter, dir, taxoDir); IOUtils.close(taxoWriter, dir, taxoDir);
@ -496,10 +476,7 @@ public class TestTaxonomyFacetAssociations extends FacetTestCase {
Document doc = new Document(); Document doc = new Document();
doc.add(new IntAssociationFacetField(14, "a", "x")); doc.add(new IntAssociationFacetField(14, "a", "x"));
expectThrows( expectThrows(
IllegalArgumentException.class, IllegalArgumentException.class, () -> writer.addDocument(config.build(taxoWriter, doc)));
() -> {
writer.addDocument(config.build(taxoWriter, doc));
});
writer.close(); writer.close();
IOUtils.close(taxoWriter, dir, taxoDir); IOUtils.close(taxoWriter, dir, taxoDir);
@ -528,9 +505,8 @@ public class TestTaxonomyFacetAssociations extends FacetTestCase {
new String[0], new String[0],
2, 2,
-1, -1,
new LabelAndValue[] { new LabelAndValue("a", 100),
new LabelAndValue("a", 100), new LabelAndValue("b", 150), new LabelAndValue("b", 150));
});
assertEquals( assertEquals(
"Wrong count for category 'a'!", 100, facets.getSpecificValue("int", "a").intValue()); "Wrong count for category 'a'!", 100, facets.getSpecificValue("int", "a").intValue());

View File

@ -122,8 +122,7 @@ public class TestTaxonomyFacetCounts2 extends FacetTestCase {
doc.add(new StringField(A.field(), A.text(), Store.NO)); doc.add(new StringField(A.field(), A.text(), Store.NO));
} }
private static void addFacets(Document doc, FacetsConfig config, boolean updateTermExpectedCounts) private static void addFacets(Document doc, boolean updateTermExpectedCounts) throws IOException {
throws IOException {
List<FacetField> docCategories = randomCategories(random()); List<FacetField> docCategories = randomCategories(random());
for (FacetField ff : docCategories) { for (FacetField ff : docCategories) {
doc.add(ff); doc.add(ff);
@ -163,29 +162,27 @@ public class TestTaxonomyFacetCounts2 extends FacetTestCase {
indexWriter.commit(); // flush a segment indexWriter.commit(); // flush a segment
} }
private static void indexDocsWithFacetsNoTerms( private static void indexDocsWithFacetsNoTerms(IndexWriter indexWriter, TaxonomyWriter taxoWriter)
IndexWriter indexWriter, TaxonomyWriter taxoWriter, Map<String, Integer> expectedCounts)
throws IOException { throws IOException {
Random random = random(); Random random = random();
int numDocs = atLeast(random, 2); int numDocs = atLeast(random, 2);
FacetsConfig config = getConfig(); FacetsConfig config = getConfig();
for (int i = 0; i < numDocs; i++) { for (int i = 0; i < numDocs; i++) {
Document doc = new Document(); Document doc = new Document();
addFacets(doc, config, false); addFacets(doc, false);
indexWriter.addDocument(config.build(taxoWriter, doc)); indexWriter.addDocument(config.build(taxoWriter, doc));
} }
indexWriter.commit(); // flush a segment indexWriter.commit(); // flush a segment
} }
private static void indexDocsWithFacetsAndTerms( private static void indexDocsWithFacetsAndTerms(
IndexWriter indexWriter, TaxonomyWriter taxoWriter, Map<String, Integer> expectedCounts) IndexWriter indexWriter, TaxonomyWriter taxoWriter) throws IOException {
throws IOException {
Random random = random(); Random random = random();
int numDocs = atLeast(random, 2); int numDocs = atLeast(random, 2);
FacetsConfig config = getConfig(); FacetsConfig config = getConfig();
for (int i = 0; i < numDocs; i++) { for (int i = 0; i < numDocs; i++) {
Document doc = new Document(); Document doc = new Document();
addFacets(doc, config, true); addFacets(doc, true);
addField(doc); addField(doc);
indexWriter.addDocument(config.build(taxoWriter, doc)); indexWriter.addDocument(config.build(taxoWriter, doc));
} }
@ -193,8 +190,7 @@ public class TestTaxonomyFacetCounts2 extends FacetTestCase {
} }
private static void indexDocsWithFacetsAndSomeTerms( private static void indexDocsWithFacetsAndSomeTerms(
IndexWriter indexWriter, TaxonomyWriter taxoWriter, Map<String, Integer> expectedCounts) IndexWriter indexWriter, TaxonomyWriter taxoWriter) throws IOException {
throws IOException {
Random random = random(); Random random = random();
int numDocs = atLeast(random, 2); int numDocs = atLeast(random, 2);
FacetsConfig config = getConfig(); FacetsConfig config = getConfig();
@ -204,7 +200,7 @@ public class TestTaxonomyFacetCounts2 extends FacetTestCase {
if (hasContent) { if (hasContent) {
addField(doc); addField(doc);
} }
addFacets(doc, config, hasContent); addFacets(doc, hasContent);
indexWriter.addDocument(config.build(taxoWriter, doc)); indexWriter.addDocument(config.build(taxoWriter, doc));
} }
indexWriter.commit(); // flush a segment indexWriter.commit(); // flush a segment
@ -256,13 +252,13 @@ public class TestTaxonomyFacetCounts2 extends FacetTestCase {
indexDocsNoFacets(indexWriter); indexDocsNoFacets(indexWriter);
// segment w/ categories, no content // segment w/ categories, no content
indexDocsWithFacetsNoTerms(indexWriter, taxoWriter, allExpectedCounts); indexDocsWithFacetsNoTerms(indexWriter, taxoWriter);
// segment w/ categories and content // segment w/ categories and content
indexDocsWithFacetsAndTerms(indexWriter, taxoWriter, allExpectedCounts); indexDocsWithFacetsAndTerms(indexWriter, taxoWriter);
// segment w/ categories and some content // segment w/ categories and some content
indexDocsWithFacetsAndSomeTerms(indexWriter, taxoWriter, allExpectedCounts); indexDocsWithFacetsAndSomeTerms(indexWriter, taxoWriter);
indexWriter.close(); indexWriter.close();
IOUtils.close(taxoWriter); IOUtils.close(taxoWriter);

View File

@ -134,11 +134,7 @@ public class TestTaxonomyFacetValueSource extends FacetTestCase {
// test getTopChildren(0, dim) // test getTopChildren(0, dim)
final Facets f = facets; final Facets f = facets;
expectThrows( expectThrows(IllegalArgumentException.class, () -> f.getTopChildren(0, "Author"));
IllegalArgumentException.class,
() -> {
f.getTopChildren(0, "Author");
});
taxoReader.close(); taxoReader.close();
searcher.getIndexReader().close(); searcher.getIndexReader().close();
@ -207,11 +203,7 @@ public class TestTaxonomyFacetValueSource extends FacetTestCase {
List<FacetResult> results = facets.getAllDims(10); List<FacetResult> results = facets.getAllDims(10);
// test getAllDims(0) // test getAllDims(0)
expectThrows( expectThrows(IllegalArgumentException.class, () -> facets.getAllDims(0));
IllegalArgumentException.class,
() -> {
facets.getAllDims(0);
});
assertEquals(3, results.size()); assertEquals(3, results.size());
assertEquals( assertEquals(
@ -236,18 +228,10 @@ public class TestTaxonomyFacetValueSource extends FacetTestCase {
assertEquals(results, allDimsResults); assertEquals(results, allDimsResults);
// test getTopDims(0, 1) // test getTopDims(0, 1)
expectThrows( expectThrows(IllegalArgumentException.class, () -> facets.getTopDims(0, 1));
IllegalArgumentException.class,
() -> {
facets.getTopDims(0, 1);
});
// test getTopDims(1, 0) with topNChildren = 0 // test getTopDims(1, 0) with topNChildren = 0
expectThrows( expectThrows(IllegalArgumentException.class, () -> facets.getTopDims(1, 0));
IllegalArgumentException.class,
() -> {
facets.getTopDims(1, 0);
});
IOUtils.close(searcher.getIndexReader(), taxoReader, dir, taxoDir); IOUtils.close(searcher.getIndexReader(), taxoReader, dir, taxoDir);
} }
@ -297,17 +281,9 @@ public class TestTaxonomyFacetValueSource extends FacetTestCase {
// test default implementation of getTopDims // test default implementation of getTopDims
List<FacetResult> topDimsResults = facets.getTopDims(10, 10); List<FacetResult> topDimsResults = facets.getTopDims(10, 10);
assertTrue(topDimsResults.isEmpty()); assertTrue(topDimsResults.isEmpty());
expectThrows( expectThrows(IllegalArgumentException.class, () -> facets.getSpecificValue("a"));
IllegalArgumentException.class,
() -> {
facets.getSpecificValue("a");
});
expectThrows( expectThrows(IllegalArgumentException.class, () -> facets.getTopChildren(10, "a"));
IllegalArgumentException.class,
() -> {
facets.getTopChildren(10, "a");
});
IOUtils.close(searcher.getIndexReader(), taxoReader, dir, taxoDir); IOUtils.close(searcher.getIndexReader(), taxoReader, dir, taxoDir);
} }
@ -694,7 +670,7 @@ public class TestTaxonomyFacetValueSource extends FacetTestCase {
"dim" + i, "dim" + i,
new String[0], new String[0],
aggregatedValue, aggregatedValue,
labelValues.toArray(new LabelAndValue[labelValues.size()]), labelValues.toArray(new LabelAndValue[0]),
labelValues.size())); labelValues.size()));
} }
} }
@ -718,8 +694,8 @@ public class TestTaxonomyFacetValueSource extends FacetTestCase {
sortTies(actual); sortTies(actual);
if (VERBOSE) { if (VERBOSE) {
System.out.println("expected=\n" + expected.toString()); System.out.println("expected=\n" + expected);
System.out.println("actual=\n" + actual.toString()); System.out.println("actual=\n" + actual);
} }
assertFloatValuesEquals(expected, actual); assertFloatValuesEquals(expected, actual);

View File

@ -43,20 +43,18 @@ public class TestAddTaxonomy extends FacetTestCase {
Thread[] addThreads = new Thread[4]; Thread[] addThreads = new Thread[4];
for (int j = 0; j < addThreads.length; j++) { for (int j = 0; j < addThreads.length; j++) {
addThreads[j] = addThreads[j] =
new Thread() { new Thread(
@Override () -> {
public void run() { Random random = random();
Random random = random(); while (numCats.decrementAndGet() > 0) {
while (numCats.decrementAndGet() > 0) { String cat = Integer.toString(random.nextInt(range));
String cat = Integer.toString(random.nextInt(range)); try {
try { tw.addCategory(new FacetLabel("a", cat));
tw.addCategory(new FacetLabel("a", cat)); } catch (IOException e) {
} catch (IOException e) { throw new RuntimeException(e);
throw new RuntimeException(e); }
} }
} });
}
};
} }
for (Thread t : addThreads) t.start(); for (Thread t : addThreads) t.start();
@ -83,11 +81,9 @@ public class TestAddTaxonomy extends FacetTestCase {
} }
private void validate(Directory dest, Directory src, OrdinalMap ordMap) throws Exception { private void validate(Directory dest, Directory src, OrdinalMap ordMap) throws Exception {
DirectoryTaxonomyReader destTR = new DirectoryTaxonomyReader(dest); try (DirectoryTaxonomyReader destTR = new DirectoryTaxonomyReader(dest)) {
try {
final int destSize = destTR.getSize(); final int destSize = destTR.getSize();
DirectoryTaxonomyReader srcTR = new DirectoryTaxonomyReader(src); try (DirectoryTaxonomyReader srcTR = new DirectoryTaxonomyReader(src)) {
try {
int[] map = ordMap.getMap(); int[] map = ordMap.getMap();
// validate taxo sizes // validate taxo sizes
@ -107,11 +103,7 @@ public class TestAddTaxonomy extends FacetTestCase {
assertTrue(cp + " not found in destination", destOrdinal > 0); assertTrue(cp + " not found in destination", destOrdinal > 0);
assertEquals(destOrdinal, map[j]); assertEquals(destOrdinal, map[j]);
} }
} finally {
srcTR.close();
} }
} finally {
destTR.close();
} }
} }
@ -209,19 +201,17 @@ public class TestAddTaxonomy extends FacetTestCase {
Directory dest = newDirectory(); Directory dest = newDirectory();
final DirectoryTaxonomyWriter destTW = new DirectoryTaxonomyWriter(dest); final DirectoryTaxonomyWriter destTW = new DirectoryTaxonomyWriter(dest);
Thread t = Thread t =
new Thread() { new Thread(
@Override () -> {
public void run() { for (int i = 0; i < numCategories; i++) {
for (int i = 0; i < numCategories; i++) { try {
try { destTW.addCategory(new FacetLabel("a", Integer.toString(i)));
destTW.addCategory(new FacetLabel("a", Integer.toString(i))); } catch (IOException e) {
} catch (IOException e) { // shouldn't happen - if it does, let the test fail on uncaught exception.
// shouldn't happen - if it does, let the test fail on uncaught exception. throw new RuntimeException(e);
throw new RuntimeException(e); }
} }
} });
}
};
t.start(); t.start();
OrdinalMap map = new MemoryOrdinalMap(); OrdinalMap map = new MemoryOrdinalMap();

View File

@ -109,35 +109,32 @@ public class TestConcurrentFacetedIndexing extends FacetTestCase {
for (int i = 0; i < indexThreads.length; i++) { for (int i = 0; i < indexThreads.length; i++) {
indexThreads[i] = indexThreads[i] =
new Thread() { new Thread(
() -> {
Random random = random();
while (numDocs.decrementAndGet() > 0) {
try {
Document doc = new Document();
int numCats = random.nextInt(3) + 1; // 1-3
while (numCats-- > 0) {
FacetField ff = newCategory();
doc.add(ff);
@Override FacetLabel label = new FacetLabel(ff.dim, ff.path);
public void run() { // add all prefixes to values
Random random = random(); int level = label.length;
while (numDocs.decrementAndGet() > 0) { while (level > 0) {
try { String s = FacetsConfig.pathToString(label.components, level);
Document doc = new Document(); values.put(s, s);
int numCats = random.nextInt(3) + 1; // 1-3 --level;
while (numCats-- > 0) { }
FacetField ff = newCategory();
doc.add(ff);
FacetLabel label = new FacetLabel(ff.dim, ff.path);
// add all prefixes to values
int level = label.length;
while (level > 0) {
String s = FacetsConfig.pathToString(label.components, level);
values.put(s, s);
--level;
} }
iw.addDocument(config.build(tw, doc));
} catch (IOException e) {
throw new RuntimeException(e);
} }
iw.addDocument(config.build(tw, doc));
} catch (IOException e) {
throw new RuntimeException(e);
} }
} });
}
};
} }
for (Thread t : indexThreads) t.start(); for (Thread t : indexThreads) t.start();

View File

@ -121,11 +121,7 @@ public class TestDirectoryTaxonomyReader extends FacetTestCase {
DirectoryTaxonomyReader ltr = new DirectoryTaxonomyReader(dir); DirectoryTaxonomyReader ltr = new DirectoryTaxonomyReader(dir);
ltr.close(); ltr.close();
expectThrows( expectThrows(AlreadyClosedException.class, ltr::getSize);
AlreadyClosedException.class,
() -> {
ltr.getSize();
});
dir.close(); dir.close();
} }
@ -662,13 +658,13 @@ public class TestDirectoryTaxonomyReader extends FacetTestCase {
final int maxNumberOfLabelsToIndex = 1000; final int maxNumberOfLabelsToIndex = 1000;
final int maxNumberOfUniqueLabelsToIndex = maxNumberOfLabelsToIndex / 2; final int maxNumberOfUniqueLabelsToIndex = maxNumberOfLabelsToIndex / 2;
final int cacheSize = maxNumberOfUniqueLabelsToIndex / 2; // to cause some cache evictions final int cacheSize = maxNumberOfUniqueLabelsToIndex / 2; // to cause some cache evictions
String randomArray[] = new String[RandomizedTest.randomIntBetween(1, maxNumberOfLabelsToIndex)]; String[] randomArray = new String[RandomizedTest.randomIntBetween(1, maxNumberOfLabelsToIndex)];
// adding a smaller bound on ints ensures that we will have some duplicate ordinals in random // adding a smaller bound on ints ensures that we will have some duplicate ordinals in random
// test cases // test cases
Arrays.setAll( Arrays.setAll(
randomArray, i -> Integer.toString(random().nextInt(maxNumberOfUniqueLabelsToIndex))); randomArray, i -> Integer.toString(random().nextInt(maxNumberOfUniqueLabelsToIndex)));
FacetLabel allPaths[] = new FacetLabel[randomArray.length]; FacetLabel[] allPaths = new FacetLabel[randomArray.length];
for (int i = 0; i < randomArray.length; i++) { for (int i = 0; i < randomArray.length; i++) {
allPaths[i] = new FacetLabel(randomArray[i]); allPaths[i] = new FacetLabel(randomArray[i]);
@ -684,7 +680,7 @@ public class TestDirectoryTaxonomyReader extends FacetTestCase {
DirectoryTaxonomyReader r1 = new DirectoryTaxonomyReader(src); DirectoryTaxonomyReader r1 = new DirectoryTaxonomyReader(src);
r1.setCacheSize(cacheSize); r1.setCacheSize(cacheSize);
int allOrdinals[] = r1.getBulkOrdinals(allPaths); int[] allOrdinals = r1.getBulkOrdinals(allPaths);
// Assert getPath and getBulkPath first, then assert getOrdinal and getBulkOrdinals. // Assert getPath and getBulkPath first, then assert getOrdinal and getBulkOrdinals.
// Create multiple threads to check result correctness and thread contention in the cache. // Create multiple threads to check result correctness and thread contention in the cache.
@ -692,43 +688,43 @@ public class TestDirectoryTaxonomyReader extends FacetTestCase {
Thread[] addThreads = new Thread[RandomNumbers.randomIntBetween(random(), 1, 12)]; Thread[] addThreads = new Thread[RandomNumbers.randomIntBetween(random(), 1, 12)];
for (int z = 0; z < addThreads.length; z++) { for (int z = 0; z < addThreads.length; z++) {
addThreads[z] = addThreads[z] =
new Thread() { new Thread(
@Override () -> {
public void run() { // each thread iterates for numThreadIterations times
// each thread iterates for numThreadIterations times int numThreadIterations = random().nextInt(10);
int numThreadIterations = random().nextInt(10); for (int threadIterations = 0;
for (int threadIterations = 0; threadIterations < numThreadIterations;
threadIterations < numThreadIterations; threadIterations++) {
threadIterations++) {
// length of the FacetLabel array that we are going to check // length of the FacetLabel array that we are going to check
int numOfOrdinalsToCheck = RandomizedTest.randomIntBetween(1, allOrdinals.length); int numOfOrdinalsToCheck =
int[] ordinals = new int[numOfOrdinalsToCheck]; RandomizedTest.randomIntBetween(1, allOrdinals.length);
FacetLabel[] path = new FacetLabel[numOfOrdinalsToCheck]; int[] ordinals = new int[numOfOrdinalsToCheck];
FacetLabel[] path = new FacetLabel[numOfOrdinalsToCheck];
for (int i = 0; i < numOfOrdinalsToCheck; i++) { for (int i = 0; i < numOfOrdinalsToCheck; i++) {
// we deliberately allow it to choose repeat indexes as this will exercise the // we deliberately allow it to choose repeat indexes as this will exercise the
// cache // cache
int ordinalIndex = random().nextInt(allOrdinals.length); int ordinalIndex = random().nextInt(allOrdinals.length);
ordinals[i] = allOrdinals[ordinalIndex]; ordinals[i] = allOrdinals[ordinalIndex];
path[i] = allPaths[ordinalIndex]; path[i] = allPaths[ordinalIndex];
} }
try { try {
// main check for correctness is done here // main check for correctness is done here
if (assertGettingOrdinals) { if (assertGettingOrdinals) {
assertGettingOrdinals(r1, ordinals, path); assertGettingOrdinals(r1, ordinals, path);
} else { } else {
assertGettingPaths(r1, path, ordinals); assertGettingPaths(r1, path, ordinals);
}
} catch (IOException e) {
// this should ideally never occur, but if it does just rethrow the error to
// the
// caller
throw new RuntimeException(e);
} }
} catch (IOException e) {
// this should ideally never occur, but if it does just rethrow the error to the
// caller
throw new RuntimeException(e);
} }
} });
}
};
} }
for (Thread t : addThreads) t.start(); for (Thread t : addThreads) t.start();
for (Thread t : addThreads) t.join(); for (Thread t : addThreads) t.join();

View File

@ -318,37 +318,35 @@ public class TestDirectoryTaxonomyWriter extends FacetTestCase {
Thread[] addThreads = new Thread[RandomNumbers.randomIntBetween(random(), 1, 12)]; Thread[] addThreads = new Thread[RandomNumbers.randomIntBetween(random(), 1, 12)];
for (int z = 0; z < addThreads.length; z++) { for (int z = 0; z < addThreads.length; z++) {
addThreads[z] = addThreads[z] =
new Thread() { new Thread(
@Override () -> {
public void run() { Random random = random();
Random random = random(); while (numCats.decrementAndGet() > 0) {
while (numCats.decrementAndGet() > 0) { try {
try { int value = random.nextInt(range);
int value = random.nextInt(range); FacetLabel cp =
FacetLabel cp = new FacetLabel(
new FacetLabel( Integer.toString(value / 1000),
Integer.toString(value / 1000), Integer.toString(value / 10000),
Integer.toString(value / 10000), Integer.toString(value / 100000),
Integer.toString(value / 100000), Integer.toString(value));
Integer.toString(value)); int ord = tw.addCategory(cp);
int ord = tw.addCategory(cp); assertTrue(
assertTrue( "invalid parent for ordinal " + ord + ", category " + cp,
"invalid parent for ordinal " + ord + ", category " + cp, tw.getParent(ord) != -1);
tw.getParent(ord) != -1); String l1 = FacetsConfig.pathToString(cp.components, 1);
String l1 = FacetsConfig.pathToString(cp.components, 1); String l2 = FacetsConfig.pathToString(cp.components, 2);
String l2 = FacetsConfig.pathToString(cp.components, 2); String l3 = FacetsConfig.pathToString(cp.components, 3);
String l3 = FacetsConfig.pathToString(cp.components, 3); String l4 = FacetsConfig.pathToString(cp.components, 4);
String l4 = FacetsConfig.pathToString(cp.components, 4); values.put(l1, l1);
values.put(l1, l1); values.put(l2, l2);
values.put(l2, l2); values.put(l3, l3);
values.put(l3, l3); values.put(l4, l4);
values.put(l4, l4); } catch (IOException e) {
} catch (IOException e) { throw new RuntimeException(e);
throw new RuntimeException(e); }
} }
} });
}
};
} }
for (Thread t : addThreads) t.start(); for (Thread t : addThreads) t.start();