lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mikemcc...@apache.org
Subject lucene-solr:branch_7x: LUCENE-8378: add DocIdSetIterator.range method
Date Thu, 05 Jul 2018 14:06:30 GMT
Repository: lucene-solr
Updated Branches:
  refs/heads/branch_7x 7b6662c3d -> 607e81757


LUCENE-8378: add DocIdSetIterator.range method


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/607e8175
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/607e8175
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/607e8175

Branch: refs/heads/branch_7x
Commit: 607e81757a45289ef8f0b9dbff4cab9f894014f5
Parents: 7b6662c
Author: Mike McCandless <mikemccand@apache.org>
Authored: Thu Jul 5 10:01:53 2018 -0400
Committer: Mike McCandless <mikemccand@apache.org>
Committed: Thu Jul 5 10:02:26 2018 -0400

----------------------------------------------------------------------
 lucene/CHANGES.txt                              |  3 ++
 .../apache/lucene/search/DocIdSetIterator.java  | 41 +++++++++++++++
 .../lucene/search/TestDocIdSetIterator.java     | 55 ++++++++++++++++++++
 3 files changed, 99 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/607e8175/lucene/CHANGES.txt
----------------------------------------------------------------------
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index f0a8642..57f5f4c 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -14,6 +14,9 @@ API Changes:
   StopAnalyzer is also deprecated, and a stop word set should be explicitly
   passed to the constructor.  (Alan Woodward)
 
+* LUCENE-8378: Add DocIdSetIterator.range static method to return an iterator
+  matching a range of docids (Mike McCandless)
+
 Bug Fixes:
 
 * LUCENE-8380: UTF8TaxonomyWriterCache inconsistency. (Ruslan Torobaev, Dawid Weiss)

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/607e8175/lucene/core/src/java/org/apache/lucene/search/DocIdSetIterator.java
----------------------------------------------------------------------
diff --git a/lucene/core/src/java/org/apache/lucene/search/DocIdSetIterator.java b/lucene/core/src/java/org/apache/lucene/search/DocIdSetIterator.java
index 70d320a..4b9bf26 100644
--- a/lucene/core/src/java/org/apache/lucene/search/DocIdSetIterator.java
+++ b/lucene/core/src/java/org/apache/lucene/search/DocIdSetIterator.java
@@ -91,6 +91,47 @@ public abstract class DocIdSetIterator {
     };
   }
 
+  /** A {@link DocIdSetIterator} that matches a range documents from
+   *  minDocID (inclusive) to maxDocID (exclusive). */
+  public static final DocIdSetIterator range(int minDoc, int maxDoc) {
+    if (minDoc >= maxDoc) {
+        throw new IllegalArgumentException("minDoc must be < maxDoc but got minDoc=" +
minDoc + " maxDoc=" + maxDoc);
+    }
+    if (minDoc < 0) {
+      throw new IllegalArgumentException("minDoc must be >= 0 but got minDoc=" + minDoc);
+    }
+    return new DocIdSetIterator() {
+      private int doc = -1;
+
+      @Override
+      public int docID() {
+        return doc;
+      }
+
+      @Override
+      public int nextDoc() throws IOException {
+        return advance(doc + 1);
+      }
+
+      @Override
+      public int advance(int target) throws IOException {
+        if (target < minDoc) {
+            doc = minDoc;
+        } else if (target >= maxDoc) {
+            doc = NO_MORE_DOCS;
+        } else {
+            doc = target;
+        }
+        return doc;
+      }
+
+      @Override
+      public long cost() {
+        return maxDoc - minDoc;
+      }
+    };
+  }
+
   /**
    * When returned by {@link #nextDoc()}, {@link #advance(int)} and
    * {@link #docID()} it means there are no more docs in the iterator.

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/607e8175/lucene/core/src/test/org/apache/lucene/search/TestDocIdSetIterator.java
----------------------------------------------------------------------
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestDocIdSetIterator.java b/lucene/core/src/test/org/apache/lucene/search/TestDocIdSetIterator.java
new file mode 100644
index 0000000..3d38f4a
--- /dev/null
+++ b/lucene/core/src/test/org/apache/lucene/search/TestDocIdSetIterator.java
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.lucene.search;
+
+import org.apache.lucene.util.LuceneTestCase;
+
+import static org.apache.lucene.search.DocIdSetIterator.NO_MORE_DOCS;
+
+public class TestDocIdSetIterator extends LuceneTestCase {
+  public void testRangeBasic() throws Exception {
+    DocIdSetIterator disi = DocIdSetIterator.range(5, 8);
+    assertEquals(-1, disi.docID());
+    assertEquals(5, disi.nextDoc());
+    assertEquals(6, disi.nextDoc());
+    assertEquals(7, disi.nextDoc());
+    assertEquals(NO_MORE_DOCS, disi.nextDoc());
+  }
+
+  public void testInvalidRange() throws Exception {
+    expectThrows(IllegalArgumentException.class, () -> {DocIdSetIterator.range(5, 4);});
+  }
+
+  public void testInvalidMin() throws Exception {
+    expectThrows(IllegalArgumentException.class, () -> {DocIdSetIterator.range(-1, 4);});
+  }
+
+  public void testEmpty() throws Exception {
+    expectThrows(IllegalArgumentException.class, () -> {DocIdSetIterator.range(7, 7);});
+  }
+
+  public void testAdvance() throws Exception {
+    DocIdSetIterator disi = DocIdSetIterator.range(5, 20);
+    assertEquals(-1, disi.docID());
+    assertEquals(5, disi.nextDoc());
+    assertEquals(17, disi.advance(17));
+    assertEquals(18, disi.nextDoc());
+    assertEquals(19, disi.nextDoc());
+    assertEquals(NO_MORE_DOCS, disi.nextDoc());
+  }
+}


Mime
View raw message