-
Notifications
You must be signed in to change notification settings - Fork 2.3k
pass in order terms as sorted to TermInSetQuery() #17714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
3460102
pass in order terms as sorted to TermInSetQuery()
mkhludnev bce1acf
slightly more elegant solution
mkhludnev 52e62cb
Attempting mocking TermInSetQ constructor.
mkhludnev e9ac1c3
Handle ids as well.
mkhludnev 392ae18
forbidden api
mkhludnev e36f4bf
make unnecessary method slow but correct.
mkhludnev ee1c7c7
make unnecessary method slow but correct.
mkhludnev d5f8dd9
Polish test coverage
mkhludnev 1f18c4d
CHANGELOG.md
mkhludnev 2d73904
assertThrows
mkhludnev 74af8c7
spotlessApply
mkhludnev f72fb69
coverage tests and refactoring
mkhludnev a8b33da
javadoc
mkhludnev 4c27562
javadoc
mkhludnev 703d3c4
Merge branch 'main' into terms-sorted
mkhludnev d725111
mark nocommit
mkhludnev 5d5a85f
one more nocommit test
mkhludnev 2938244
forbidden api
mkhludnev 38f93f7
no commit for out of line tests
mkhludnev c76663e
Review
mkhludnev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
182 changes: 182 additions & 0 deletions
182
server/src/main/java/org/opensearch/index/mapper/BytesRefsCollectionBuilder.java
This file contains hidden or 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,182 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.index.mapper; | ||
|
||
import org.apache.lucene.search.TermInSetQuery; | ||
import org.apache.lucene.util.BytesRef; | ||
|
||
import java.util.AbstractSet; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Comparator; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.SortedSet; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Purposed for passing terms into {@link TermInSetQuery}. | ||
* If the given terms are sorted already, it wrap it with a SortedSet stub. | ||
* Otherwise, it passes terms as list. | ||
*/ | ||
public class BytesRefsCollectionBuilder implements Consumer<BytesRef>, Supplier<Collection<BytesRef>> { | ||
|
||
/** | ||
* Strategy for building BytesRef collection. | ||
* */ | ||
protected interface ConsumerStrategy extends Function<BytesRef, ConsumerStrategy>, Supplier<Collection<BytesRef>> {} | ||
|
||
public BytesRefsCollectionBuilder(int sizeExpected) { | ||
terms = new ArrayList<>(sizeExpected); | ||
} | ||
|
||
protected final List<BytesRef> terms; | ||
protected ConsumerStrategy delegate = createStartStrategy(); | ||
|
||
@Override | ||
public void accept(BytesRef bytesRef) { | ||
delegate = delegate.apply(bytesRef); | ||
} | ||
|
||
@Override | ||
public Collection<BytesRef> get() { | ||
Collection<BytesRef> result = delegate.get(); | ||
delegate = createFrozenStrategy(result); | ||
return result; | ||
} | ||
|
||
protected ConsumerStrategy createStartStrategy() { | ||
return new ConsumerStrategy() { | ||
@Override | ||
public ConsumerStrategy apply(BytesRef firstBytes) { | ||
terms.add(firstBytes); // firstly, just store | ||
return createSortedStrategy(firstBytes); | ||
} | ||
|
||
@Override | ||
public Collection<BytesRef> get() { | ||
return terms; // empty list | ||
} | ||
}; | ||
} | ||
|
||
protected ConsumerStrategy createSortedStrategy(BytesRef firstBytes) { | ||
return new ConsumerStrategy() { | ||
BytesRef prev = firstBytes; | ||
|
||
@Override | ||
public ConsumerStrategy apply(BytesRef bytesRef) { | ||
terms.add(bytesRef); | ||
if (bytesRef.compareTo(prev) >= 0) { // keep checking sorted | ||
prev = bytesRef; | ||
return this; | ||
} else { // isn't sorted | ||
return createUnsortedStrategy(); | ||
} | ||
} | ||
|
||
@Override | ||
public Collection<BytesRef> get() { | ||
return new SortedBytesSet(terms); | ||
} | ||
}; | ||
} | ||
|
||
protected ConsumerStrategy createUnsortedStrategy() { | ||
return new ConsumerStrategy() { | ||
@Override | ||
public ConsumerStrategy apply(BytesRef bytesRef) { // just storing | ||
terms.add(bytesRef); | ||
return this; | ||
} | ||
|
||
@Override | ||
public Collection<BytesRef> get() { | ||
return terms; | ||
} | ||
}; | ||
} | ||
|
||
protected ConsumerStrategy createFrozenStrategy(Collection<BytesRef> result) { | ||
return new ConsumerStrategy() { | ||
|
||
@Override | ||
public ConsumerStrategy apply(BytesRef bytesRef) { | ||
throw new IllegalStateException("already build"); | ||
} | ||
|
||
@Override | ||
public Collection<BytesRef> get() { | ||
return result; | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* {@link SortedSet<BytesRef>} for passing into TermInSetQuery() | ||
* */ | ||
protected static class SortedBytesSet extends AbstractSet<BytesRef> implements SortedSet<BytesRef> { | ||
|
||
private final List<BytesRef> bytesRefs; | ||
|
||
public SortedBytesSet(List<BytesRef> bytesRefs) { | ||
this.bytesRefs = bytesRefs; | ||
} | ||
|
||
@Override | ||
public Iterator<BytesRef> iterator() { | ||
return bytesRefs.iterator(); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return bytesRefs.size(); | ||
} | ||
|
||
@Override | ||
public Comparator<? super BytesRef> comparator() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public SortedSet<BytesRef> subSet(BytesRef fromElement, BytesRef toElement) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public SortedSet<BytesRef> headSet(BytesRef toElement) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public SortedSet<BytesRef> tailSet(BytesRef fromElement) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public BytesRef first() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public BytesRef last() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
/** | ||
* Dedicated for {@link TermInSetQuery#TermInSetQuery(String, Collection)}. | ||
*/ | ||
@Override | ||
public <T> T[] toArray(T[] a) { | ||
return bytesRefs.toArray(a); | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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
82 changes: 82 additions & 0 deletions
82
server/src/test/java/org/opensearch/index/mapper/BytesRefsCollectionBuilderTests.java
This file contains hidden or 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,82 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.index.mapper; | ||
|
||
import org.apache.lucene.util.BytesRef; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.SortedSet; | ||
import java.util.stream.Stream; | ||
|
||
public class BytesRefsCollectionBuilderTests extends OpenSearchTestCase { | ||
|
||
public void testBuildSortedNotSorted() { | ||
String[] seedStrings = generateRandomStringArray(10, 10, false, true); | ||
List<BytesRef> bytesRefList = Arrays.stream(seedStrings).map(BytesRef::new).toList(); | ||
List<BytesRef> sortedBytesRefs = bytesRefList.stream().sorted().toList(); | ||
|
||
Collection<BytesRef> sortedSet = assertCollectionBuilt(sortedBytesRefs); | ||
assertCollectionBuilt(bytesRefList); | ||
|
||
assertTrue(sortedSet instanceof SortedSet<BytesRef>); | ||
assertNull(((SortedSet<BytesRef>) sortedSet).comparator()); | ||
} | ||
|
||
public void testBuildFooBar() { | ||
String[] reverseOrderStrings = new String[] { "foo", "bar" }; | ||
List<BytesRef> bytesRefList = Arrays.stream(reverseOrderStrings).map(BytesRef::new).toList(); | ||
List<BytesRef> sortedBytesRefs = bytesRefList.stream().sorted().toList(); | ||
|
||
Collection<BytesRef> sortedSet = assertCollectionBuilt(sortedBytesRefs); | ||
Collection<BytesRef> reverseList = assertCollectionBuilt(bytesRefList); | ||
|
||
assertTrue(sortedSet instanceof SortedSet<BytesRef>); | ||
assertNull(((SortedSet<BytesRef>) sortedSet).comparator()); | ||
|
||
assertTrue(reverseList instanceof List<BytesRef>); | ||
} | ||
|
||
public void testFrozen() { | ||
BytesRefsCollectionBuilder builder = new BytesRefsCollectionBuilder(1); | ||
String[] seedStrings = generateRandomStringArray(5, 10, false, true); | ||
Arrays.stream(seedStrings).map(BytesRef::new).forEachOrdered(builder); | ||
Collection<BytesRef> bytesRefCollection = builder.get(); | ||
assertNotNull(bytesRefCollection); | ||
assertEquals(seedStrings.length, bytesRefCollection.size()); | ||
assertThrows(IllegalStateException.class, () -> builder.accept(new BytesRef("illegal state"))); | ||
assertSame(bytesRefCollection, builder.get()); | ||
} | ||
|
||
private static Collection<BytesRef> assertCollectionBuilt(List<BytesRef> sortedBytesRefs) { | ||
BytesRefsCollectionBuilder builder = new BytesRefsCollectionBuilder(1); | ||
sortedBytesRefs.stream().forEachOrdered(builder); | ||
Collection<BytesRef> bytesRefCollection = builder.get(); | ||
assertEquals(bytesRefCollection.size(), sortedBytesRefs.size()); | ||
for (Iterator<BytesRef> iterator = bytesRefCollection.iterator(), iterator2 = sortedBytesRefs.iterator(); iterator.hasNext() | ||
|| iterator2.hasNext();) { | ||
assertTrue(iterator.next().bytesEquals(iterator2.next())); | ||
} | ||
return bytesRefCollection; | ||
} | ||
|
||
public void testCoverUnsupported() { | ||
BytesRefsCollectionBuilder builder = new BytesRefsCollectionBuilder(1); | ||
Stream.of("in", "order").map(BytesRef::new).forEachOrdered(builder); | ||
SortedSet<BytesRef> bytesRefCollection = (SortedSet<BytesRef>) builder.get(); | ||
assertThrows(UnsupportedOperationException.class, () -> bytesRefCollection.subSet(new BytesRef("a"), new BytesRef("z"))); | ||
assertThrows(UnsupportedOperationException.class, () -> bytesRefCollection.headSet(new BytesRef("a"))); | ||
assertThrows(UnsupportedOperationException.class, () -> bytesRefCollection.tailSet(new BytesRef("a"))); | ||
assertThrows(UnsupportedOperationException.class, bytesRefCollection::first); | ||
assertThrows(UnsupportedOperationException.class, bytesRefCollection::last); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.