gsmiller commented on code in PR #11901:
URL: https://github.com/apache/lucene/pull/11901#discussion_r1045109686


##########
lucene/facet/src/java/org/apache/lucene/facet/rangeonrange/RangeOnRangeFacetCounts.java:
##########
@@ -0,0 +1,209 @@
+/*
+ * 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.facet.rangeonrange;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import org.apache.lucene.document.BinaryRangeDocValues;
+import org.apache.lucene.document.RangeFieldQuery;
+import org.apache.lucene.facet.FacetCountsWithFilterQuery;
+import org.apache.lucene.facet.FacetResult;
+import org.apache.lucene.facet.FacetsCollector;
+import org.apache.lucene.facet.LabelAndValue;
+import org.apache.lucene.index.DocValues;
+import org.apache.lucene.search.DocIdSetIterator;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.util.ArrayUtil;
+import org.apache.lucene.util.PriorityQueue;
+
+abstract class RangeOnRangeFacetCounts extends FacetCountsWithFilterQuery {
+
+  private final byte[][] encodedRanges;
+  private final String[] labels;
+  private final int numEncodedValueBytes;
+  private final int dims;
+
+  /** Counts, initialized in by subclass. */
+  protected final int[] counts;
+
+  /** Our field name. */
+  protected final String field;
+
+  /** Total number of hits. */
+  protected int totCount;
+
+  private final ArrayUtil.ByteArrayComparator comparator;
+
+  /** Type of "range overlap" we want to count. */
+  RangeFieldQuery.QueryType queryType;
+
+  protected RangeOnRangeFacetCounts(
+      String field,
+      FacetsCollector hits,
+      RangeFieldQuery.QueryType queryType,
+      Query fastMatchQuery,
+      int numEncodedValueBytes,
+      byte[][] encodedRanges,
+      String[] labels)
+      throws IOException {
+    super(fastMatchQuery);
+
+    assert encodedRanges.length == labels.length;
+    assert encodedRanges[0].length % (2 * numEncodedValueBytes) == 0;
+
+    this.encodedRanges = encodedRanges;
+    this.field = field;
+    this.labels = labels;
+    this.numEncodedValueBytes = numEncodedValueBytes;
+    this.dims = encodedRanges[0].length / (2 * this.numEncodedValueBytes);
+    this.queryType = queryType;
+    this.comparator = 
ArrayUtil.getUnsignedComparator(this.numEncodedValueBytes);
+    this.counts = new int[encodedRanges.length];
+    count(field, hits.getMatchingDocs());
+  }
+
+  /** Counts from the provided field. */
+  protected void count(String field, List<FacetsCollector.MatchingDocs> 
matchingDocs)
+      throws IOException {
+    // TODO: We currently just exhaustively check the ranges in each document 
with every range in
+    // the ranges array.
+    // We might be able to do something more efficient here by grouping the 
ranges array into a
+    // space partitioning
+    // data structure of some sort.
+
+    int missingCount = 0;
+
+    for (FacetsCollector.MatchingDocs hits : matchingDocs) {
+
+      BinaryRangeDocValues binaryRangeDocValues =
+          new BinaryRangeDocValues(
+              DocValues.getBinary(hits.context.reader(), field), dims, 
numEncodedValueBytes);
+
+      final DocIdSetIterator it = createIterator(hits);

Review Comment:
   Forgot to mention earlier, but I love that you've added support for 
fast-match queries. And that it was so straight-forward to do so after that 
functionality was factored out into its own class. Nice!



##########
lucene/facet/src/java/org/apache/lucene/facet/rangeonrange/LongRangeOnRangeFacetCounts.java:
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.facet.rangeonrange;
+
+import static org.apache.lucene.document.LongRange.verifyAndEncode;
+
+import java.io.IOException;
+import org.apache.lucene.document.RangeFieldQuery;
+import org.apache.lucene.facet.FacetsCollector;
+import org.apache.lucene.search.Query;
+
+/** Represents counts for long range on range faceting */

Review Comment:
   Could we expand the javadoc a little here to help users understand this 
faceting implementation and the ways they can use it? For example, it's 
probably worth mentioning the type of field it expects, the single- vs. 
multi-dim cases and that it's meant to pair with the range-on-range queries? 
(Same feedback for the double case). Maybe have a look at the ctor javadoc and 
such as well, and think about what we should convey to users that are looking 
at this javadoc to figure out how to use it.



##########
lucene/facet/src/java/org/apache/lucene/facet/rangeonrange/RangeOnRangeFacetCounts.java:
##########
@@ -0,0 +1,209 @@
+/*
+ * 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.facet.rangeonrange;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import org.apache.lucene.document.BinaryRangeDocValues;
+import org.apache.lucene.document.RangeFieldQuery;
+import org.apache.lucene.facet.FacetCountsWithFilterQuery;
+import org.apache.lucene.facet.FacetResult;
+import org.apache.lucene.facet.FacetsCollector;
+import org.apache.lucene.facet.LabelAndValue;
+import org.apache.lucene.index.DocValues;
+import org.apache.lucene.search.DocIdSetIterator;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.util.ArrayUtil;
+import org.apache.lucene.util.PriorityQueue;
+
+abstract class RangeOnRangeFacetCounts extends FacetCountsWithFilterQuery {
+
+  private final byte[][] encodedRanges;
+  private final String[] labels;
+  private final int numEncodedValueBytes;
+  private final int dims;
+
+  /** Counts, initialized in by subclass. */
+  protected final int[] counts;
+
+  /** Our field name. */
+  protected final String field;
+
+  /** Total number of hits. */
+  protected int totCount;
+
+  private final ArrayUtil.ByteArrayComparator comparator;
+
+  /** Type of "range overlap" we want to count. */
+  RangeFieldQuery.QueryType queryType;

Review Comment:
   Do these `protected` and pkg-private fields need this visibility? Can we 
make them `private` instead? It doesn't look like sub-classes need direct 
access to any of them. We can always increase visibility later if a sub-class 
needs it.



##########
lucene/facet/src/java/org/apache/lucene/facet/rangeonrange/RangeOnRangeFacetCounts.java:
##########
@@ -0,0 +1,209 @@
+/*
+ * 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.facet.rangeonrange;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import org.apache.lucene.document.BinaryRangeDocValues;
+import org.apache.lucene.document.RangeFieldQuery;
+import org.apache.lucene.facet.FacetCountsWithFilterQuery;
+import org.apache.lucene.facet.FacetResult;
+import org.apache.lucene.facet.FacetsCollector;
+import org.apache.lucene.facet.LabelAndValue;
+import org.apache.lucene.index.DocValues;
+import org.apache.lucene.search.DocIdSetIterator;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.util.ArrayUtil;
+import org.apache.lucene.util.PriorityQueue;
+
+abstract class RangeOnRangeFacetCounts extends FacetCountsWithFilterQuery {
+
+  private final byte[][] encodedRanges;
+  private final String[] labels;
+  private final int numEncodedValueBytes;
+  private final int dims;
+
+  /** Counts, initialized in by subclass. */
+  protected final int[] counts;
+
+  /** Our field name. */
+  protected final String field;
+
+  /** Total number of hits. */
+  protected int totCount;
+
+  private final ArrayUtil.ByteArrayComparator comparator;
+
+  /** Type of "range overlap" we want to count. */
+  RangeFieldQuery.QueryType queryType;
+
+  protected RangeOnRangeFacetCounts(
+      String field,
+      FacetsCollector hits,
+      RangeFieldQuery.QueryType queryType,
+      Query fastMatchQuery,
+      int numEncodedValueBytes,
+      byte[][] encodedRanges,
+      String[] labels)
+      throws IOException {
+    super(fastMatchQuery);
+
+    assert encodedRanges.length == labels.length;
+    assert encodedRanges[0].length % (2 * numEncodedValueBytes) == 0;
+
+    this.encodedRanges = encodedRanges;
+    this.field = field;
+    this.labels = labels;
+    this.numEncodedValueBytes = numEncodedValueBytes;
+    this.dims = encodedRanges[0].length / (2 * this.numEncodedValueBytes);
+    this.queryType = queryType;
+    this.comparator = 
ArrayUtil.getUnsignedComparator(this.numEncodedValueBytes);

Review Comment:
   minor: It looks like we only need `dims`, `numEncodedValueBytes`, 
`encodedRanges`, `comparator` and `queryType` in the `count()` method. Could we 
just pass them in and not need to maintain a field reference to them in the 
class instance? Always nice to keep the fields tight if possible. Easier to 
read and dereferences them earlier for GC.
   
   Edit: For `comparator`, you could also instantiate it in the `count` method 
itself.



##########
lucene/facet/src/java/org/apache/lucene/facet/rangeonrange/LongRange.java:
##########
@@ -0,0 +1,119 @@
+/*
+ * 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.facet.rangeonrange;
+
+import java.util.Arrays;
+import java.util.Objects;
+
+/** Represents a long range for RangeOnRange faceting */
+public class LongRange extends Range {
+  /** Minimum (inclusive). */
+  public final long[] min;
+
+  /** Maximum (inclusive). */
+  public final long[] max;
+
+  /**
+   * Represents a single dimensional long range for RangeOnRange faceting
+   *
+   * @param label the name of the range
+   * @param minIn the minimum
+   * @param minInclusive if the minimum is inclusive
+   * @param maxIn the maximum
+   * @param maxInclusive if the maximum is inclusive
+   */
+  public LongRange(
+      String label, long minIn, boolean minInclusive, long maxIn, boolean 
maxInclusive) {
+    super(label, 1);
+
+    if (minInclusive == false) {
+      if (minIn != Long.MAX_VALUE) {
+        minIn++;
+      } else {
+        failNoMatch();
+      }
+    }
+
+    if (maxInclusive == false) {
+      if (maxIn != Long.MIN_VALUE) {
+        maxIn--;
+      } else {
+        failNoMatch();
+      }
+    }
+
+    if (minIn > maxIn) {
+      failNoMatch();
+    }
+
+    this.min = new long[] {minIn};
+    this.max = new long[] {maxIn};
+  }
+
+  /**
+   * Represents a multidimensional long range for RangeOnRange faceting
+   *
+   * @param label the name of the range
+   * @param min the minimum, inclusive
+   * @param max the maximum, inclusive
+   */
+  public LongRange(String label, long[] min, long[] max) {
+    super(label, min.length);
+    checkArgs(min, max);
+    this.min = min;
+    this.max = max;
+  }
+
+  @Override
+  public String toString() {
+    return "LongRange(label: "
+        + label
+        + ", min: "
+        + Arrays.toString(min)
+        + ", max: "
+        + Arrays.toString(max)
+        + ")";
+  }
+
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) return true;
+    if (o == null || getClass() != o.getClass()) return false;
+    LongRange longRange = (LongRange) o;
+    return Arrays.equals(min, longRange.min) && Arrays.equals(max, 
longRange.max);
+  }
+
+  @Override
+  public int hashCode() {
+    return Objects.hash(label, Arrays.hashCode(min), Arrays.hashCode(max));
+  }
+
+  private static void checkArgs(final long[] min, final long[] max) {
+    if (min == null || max == null || min.length == 0 || max.length == 0) {
+      throw new IllegalArgumentException("min/max range values cannot be null 
or empty");
+    }
+    if (min.length != max.length) {
+      throw new IllegalArgumentException("min/max ranges must agree");
+    }
+
+    for (int i = 0; i < min.length; i++) {
+      if (min[i] > max[i]) {
+        throw new IllegalArgumentException("min should be less than max");

Review Comment:
   Should we delegate to `failNoMatch()` here for consistency? (Same for the 
double case).



##########
lucene/facet/src/java/org/apache/lucene/facet/rangeonrange/LongRange.java:
##########
@@ -0,0 +1,119 @@
+/*
+ * 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.facet.rangeonrange;
+
+import java.util.Arrays;
+import java.util.Objects;
+
+/** Represents a long range for RangeOnRange faceting */
+public class LongRange extends Range {
+  /** Minimum (inclusive). */
+  public final long[] min;
+
+  /** Maximum (inclusive). */
+  public final long[] max;
+
+  /**
+   * Represents a single dimensional long range for RangeOnRange faceting
+   *
+   * @param label the name of the range
+   * @param minIn the minimum
+   * @param minInclusive if the minimum is inclusive
+   * @param maxIn the maximum
+   * @param maxInclusive if the maximum is inclusive
+   */
+  public LongRange(
+      String label, long minIn, boolean minInclusive, long maxIn, boolean 
maxInclusive) {
+    super(label, 1);
+
+    if (minInclusive == false) {
+      if (minIn != Long.MAX_VALUE) {
+        minIn++;
+      } else {
+        failNoMatch();
+      }
+    }
+
+    if (maxInclusive == false) {
+      if (maxIn != Long.MIN_VALUE) {
+        maxIn--;
+      } else {
+        failNoMatch();
+      }
+    }
+
+    if (minIn > maxIn) {
+      failNoMatch();
+    }
+
+    this.min = new long[] {minIn};
+    this.max = new long[] {maxIn};
+  }
+
+  /**
+   * Represents a multidimensional long range for RangeOnRange faceting
+   *
+   * @param label the name of the range
+   * @param min the minimum, inclusive
+   * @param max the maximum, inclusive
+   */
+  public LongRange(String label, long[] min, long[] max) {
+    super(label, min.length);
+    checkArgs(min, max);
+    this.min = min;
+    this.max = max;
+  }
+
+  @Override
+  public String toString() {
+    return "LongRange(label: "
+        + label
+        + ", min: "
+        + Arrays.toString(min)
+        + ", max: "
+        + Arrays.toString(max)
+        + ")";
+  }
+
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) return true;
+    if (o == null || getClass() != o.getClass()) return false;
+    LongRange longRange = (LongRange) o;
+    return Arrays.equals(min, longRange.min) && Arrays.equals(max, 
longRange.max);

Review Comment:
   Should `equals` also check `label` to be consistent with `hashCode()`? (Same 
with the double case).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to