| From 25cc5b6395b23a4da2dfd18377b5a910e07f3cac Mon Sep 17 00:00:00 2001 |
| From: Martijn van Groningen <mvg@apache.org> |
| Date: Wed, 1 Jun 2016 11:42:00 +0200 |
| Subject: [PATCH 1/1] LUCENE-7307: Add getters to the PointInSetQuery and |
| PointRangeQuery queries. |
| |
| --- |
| lucene/CHANGES.txt | 3 ++ |
| .../org/apache/lucene/search/PointInSetQuery.java | 57 ++++++++++++++++++++++ |
| .../org/apache/lucene/search/PointRangeQuery.java | 20 ++++++++ |
| .../org/apache/lucene/search/TestPointQueries.java | 21 ++++++++ |
| 4 files changed, 101 insertions(+) |
| |
| diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt |
| index 5dc8a2c..f718045 100644 |
| --- a/lucene/CHANGES.txt |
| +++ b/lucene/CHANGES.txt |
| @@ -168,6 +168,9 @@ Other |
| * LUCENE-7206: Improve the ToParentBlockJoinQuery's explain by including the explain |
| of the best matching child doc. (Ilya Kasnacheev, Jeff Evans via Martijn van Groningen) |
| |
| +* LUCENE-7307: Add getters to the PointInSetQuery and PointRangeQuery queries. |
| + (Martijn van Groningen, Adrien Grand) |
| + |
| Build |
| |
| * LUCENE-7292: Use '-release' instead of '-source/-target' during |
| diff --git a/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.java b/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.java |
| index c19e457..e1a2c18 100644 |
| --- a/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.java |
| +++ b/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.java |
| @@ -17,7 +17,15 @@ |
| package org.apache.lucene.search; |
| |
| import java.io.IOException; |
| +import java.util.AbstractCollection; |
| +import java.util.ArrayList; |
| import java.util.Arrays; |
| +import java.util.Collection; |
| +import java.util.Collections; |
| +import java.util.Iterator; |
| +import java.util.List; |
| +import java.util.NoSuchElementException; |
| +import java.util.Set; |
| |
| import org.apache.lucene.document.IntPoint; |
| import org.apache.lucene.index.FieldInfo; |
| @@ -28,6 +36,7 @@ import org.apache.lucene.index.PointValues.Relation; |
| import org.apache.lucene.index.PointValues; |
| import org.apache.lucene.index.PrefixCodedTerms.TermIterator; |
| import org.apache.lucene.index.PrefixCodedTerms; |
| +import org.apache.lucene.index.Term; |
| import org.apache.lucene.util.BytesRef; |
| import org.apache.lucene.util.BytesRefBuilder; |
| import org.apache.lucene.util.BytesRefIterator; |
| @@ -303,6 +312,54 @@ public abstract class PointInSetQuery extends Query { |
| } |
| } |
| |
| + public Collection<byte[]> getPackedPoints() { |
| + return new AbstractCollection<byte[]>() { |
| + |
| + @Override |
| + public Iterator<byte[]> iterator() { |
| + int size = (int) sortedPackedPoints.size(); |
| + PrefixCodedTerms.TermIterator iterator = sortedPackedPoints.iterator(); |
| + return new Iterator<byte[]>() { |
| + |
| + int upto = 0; |
| + |
| + @Override |
| + public boolean hasNext() { |
| + return upto < size; |
| + } |
| + |
| + @Override |
| + public byte[] next() { |
| + if (upto == size) { |
| + throw new NoSuchElementException(); |
| + } |
| + |
| + upto++; |
| + BytesRef next = iterator.next(); |
| + return Arrays.copyOfRange(next.bytes, next.offset, next.length); |
| + } |
| + }; |
| + } |
| + |
| + @Override |
| + public int size() { |
| + return (int) sortedPackedPoints.size(); |
| + } |
| + }; |
| + } |
| + |
| + public String getField() { |
| + return field; |
| + } |
| + |
| + public int getNumDims() { |
| + return numDims; |
| + } |
| + |
| + public int getBytesPerDim() { |
| + return bytesPerDim; |
| + } |
| + |
| @Override |
| public final int hashCode() { |
| int hash = classHash(); |
| diff --git a/lucene/core/src/java/org/apache/lucene/search/PointRangeQuery.java b/lucene/core/src/java/org/apache/lucene/search/PointRangeQuery.java |
| index fb7051d..63de04c 100644 |
| --- a/lucene/core/src/java/org/apache/lucene/search/PointRangeQuery.java |
| +++ b/lucene/core/src/java/org/apache/lucene/search/PointRangeQuery.java |
| @@ -218,6 +218,26 @@ public abstract class PointRangeQuery extends Query { |
| }; |
| } |
| |
| + public String getField() { |
| + return field; |
| + } |
| + |
| + public int getNumDims() { |
| + return numDims; |
| + } |
| + |
| + public int getBytesPerDim() { |
| + return bytesPerDim; |
| + } |
| + |
| + public byte[] getLowerPoint() { |
| + return lowerPoint.clone(); |
| + } |
| + |
| + public byte[] getUpperPoint() { |
| + return upperPoint.clone(); |
| + } |
| + |
| @Override |
| public final int hashCode() { |
| int hash = classHash(); |
| diff --git a/lucene/core/src/test/org/apache/lucene/search/TestPointQueries.java b/lucene/core/src/test/org/apache/lucene/search/TestPointQueries.java |
| index 078c8da..cf8372d 100644 |
| --- a/lucene/core/src/test/org/apache/lucene/search/TestPointQueries.java |
| +++ b/lucene/core/src/test/org/apache/lucene/search/TestPointQueries.java |
| @@ -22,9 +22,12 @@ import java.nio.charset.StandardCharsets; |
| import java.util.ArrayList; |
| import java.util.Arrays; |
| import java.util.BitSet; |
| +import java.util.Collection; |
| import java.util.Comparator; |
| import java.util.HashSet; |
| +import java.util.Iterator; |
| import java.util.List; |
| +import java.util.NoSuchElementException; |
| import java.util.Set; |
| import java.util.concurrent.CountDownLatch; |
| import java.util.concurrent.atomic.AtomicBoolean; |
| @@ -1854,6 +1857,24 @@ public class TestPointQueries extends LuceneTestCase { |
| assertEquals("bytes:{[12] [2a]}", BinaryPoint.newSetQuery("bytes", new byte[] {42}, new byte[] {18}).toString()); |
| } |
| |
| + public void testPointInSetQueryGetPackedPoints() throws Exception { |
| + int numValues = randomIntValue(1, 32); |
| + List<byte[]> values = new ArrayList<>(numValues); |
| + for (byte i = 0; i < numValues; i++) { |
| + values.add(new byte[]{i}); |
| + } |
| + |
| + PointInSetQuery query = (PointInSetQuery) BinaryPoint.newSetQuery("field", values.toArray(new byte[][]{})); |
| + Collection<byte[]> packedPoints = query.getPackedPoints(); |
| + assertEquals(numValues, packedPoints.size()); |
| + Iterator<byte[]> iterator = packedPoints.iterator(); |
| + for (byte[] expectedValue : values) { |
| + assertArrayEquals(expectedValue, iterator.next()); |
| + } |
| + expectThrows(NoSuchElementException.class, () -> iterator.next()); |
| + assertFalse(iterator.hasNext()); |
| + } |
| + |
| public void testRangeOptimizesIfAllPointsMatch() throws IOException { |
| final int numDims = TestUtil.nextInt(random(), 1, 3); |
| Directory dir = newDirectory(); |
| -- |
| 2.7.4 (Apple Git-66) |
| |