[GitHub] [lucene-solr] romseygeek commented on a change in pull request #1440: LUCENE-9330: Make SortFields responsible for index sorting and serialization

2020-04-22 Thread GitBox


romseygeek commented on a change in pull request #1440:
URL: https://github.com/apache/lucene-solr/pull/1440#discussion_r412783967



##
File path: 
lucene/core/src/java/org/apache/lucene/index/DefaultIndexingChain.java
##
@@ -527,45 +589,61 @@ private void indexPoint(PerField fp, IndexableField 
field) throws IOException {
 fp.pointValuesWriter.addPackedValue(docState.docID, field.binaryValue());
   }
 
-  private void validateIndexSortDVType(Sort indexSort, String fieldName, 
DocValuesType dvType) {
+  private void validateIndexSortDVType(Sort indexSort, String fieldToValidate, 
DocValuesType dvType) throws IOException {
 for (SortField sortField : indexSort.getSort()) {
-  if (sortField.getField().equals(fieldName)) {
-switch (dvType) {
-  case NUMERIC:
-if (sortField.getType().equals(SortField.Type.INT) == false &&
-  sortField.getType().equals(SortField.Type.LONG) == false &&
-  sortField.getType().equals(SortField.Type.FLOAT) == false &&
-  sortField.getType().equals(SortField.Type.DOUBLE) == false) {
-  throw new IllegalArgumentException("invalid doc value type:" + 
dvType + " for sortField:" + sortField);
-}
-break;
+  IndexSorter sorter = sortField.getIndexSorter();
+  assert sorter != null;

Review comment:
   It's checked in `IndexWriterConfig.setSort()` but I'll add an exception 
here as well; you could conceivably get here via a funky Codec that 
instantiates a Sort with no IndexSorter defined.





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.

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



[GitHub] [lucene-solr] romseygeek commented on a change in pull request #1440: LUCENE-9330: Make SortFields responsible for index sorting and serialization

2020-04-22 Thread GitBox


romseygeek commented on a change in pull request #1440:
URL: https://github.com/apache/lucene-solr/pull/1440#discussion_r412786736



##
File path: lucene/core/src/java/org/apache/lucene/search/SortField.java
##
@@ -392,4 +491,31 @@ public SortField rewrite(IndexSearcher searcher) throws 
IOException {
   public boolean needsScores() {
 return type == Type.SCORE;
   }
+
+  /**
+   * Returns an {@link IndexSorter} used for sorting index segments by this 
SortField.
+   *
+   * If the SortField cannot be used for index sorting (for example, if it 
uses scores or
+   * other query-dependent values) then this method should return {@code null}
+   *
+   * SortFields that implement this method should also implement a constructor 
that
+   * takes a {@link DataInput} for deserialization, to match the {@link 
IndexSorter#serialize(DataOutput)}
+   * method on the returned IndexSorter
+   */

Review comment:
   I'm not sure this API is particularly internal? If people want to 
implement their own SortFields and make them indexable they will need to 
implement this method, which makes it public IMO.
   
   We should probably take `@lucene.experimental` off `getFieldComparator()` as 
well, it's been there since 2004...





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.

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



[GitHub] [lucene-solr] romseygeek commented on a change in pull request #1440: LUCENE-9330: Make SortFields responsible for index sorting and serialization

2020-04-22 Thread GitBox


romseygeek commented on a change in pull request #1440:
URL: https://github.com/apache/lucene-solr/pull/1440#discussion_r412829154



##
File path: lucene/core/src/java/org/apache/lucene/index/IndexSorter.java
##
@@ -0,0 +1,500 @@
+/*
+ * 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.index;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+
+import org.apache.lucene.search.FieldComparator;
+import org.apache.lucene.search.SortField;
+import org.apache.lucene.store.DataInput;
+import org.apache.lucene.store.DataOutput;
+import org.apache.lucene.util.LongValues;
+import org.apache.lucene.util.NumericUtils;
+import org.apache.lucene.util.packed.PackedInts;
+
+import static org.apache.lucene.search.DocIdSetIterator.NO_MORE_DOCS;
+
+/**
+ * Handles how documents should be sorted in an index, both within a segment 
and between
+ * segments.
+ *
+ * Implementers must provide the following methods:
+ * {@link #getDocComparator(LeafReader)} - an object that determines how 
documents within a segment are to be sorted
+ * {@link #getComparableProviders(List)} - an array of objects that return a 
sortable long value per document and segment
+ * {@link #serialize(DataOutput)} - how the sort should be written into the 
segment header
+ * {@link #getProviderName()} - the SPI-registered name of a {@link 
SortFieldProvider} to deserialize the sort
+ *
+ * The companion {@link SortFieldProvider} should be registered with SPI via 
{@code META-INF/services}
+ */
+public interface IndexSorter {
+
+  /** Used for sorting documents across segments */
+  public interface ComparableProvider {
+/**
+ * Returns a long so that the natural ordering of long values matches the
+ * ordering of doc IDs for the given comparator
+ */
+long getAsComparableLong(int docID) throws IOException;
+  }
+
+  /** A comparator of doc IDs, used for sorting documents within a segment */
+  public interface DocComparator {
+/** Compare docID1 against docID2. The contract for the return value is the
+ *  same as {@link Comparator#compare(Object, Object)}. */
+int compare(int docID1, int docID2);
+  }
+
+  /**
+   * Get an array of {@link ComparableProvider}, one per segment, for merge 
sorting documents in different segments
+   * @param readers the readers to be merged
+   */
+  public abstract ComparableProvider[] getComparableProviders(List readers) throws IOException;
+
+  /**
+   * Get a comparator that determines the sort order of docs within a single 
Reader.
+   *
+   * NB We cannot simply use the {@link FieldComparator} API because it 
requires docIDs to be sent
+   * in-order. The default implementations allocate array[maxDoc] to hold 
native values for comparison,
+   * but 1) they are transient (only alive while sorting this one segment) and 
2) in the typical
+   * index sorting case, they are only used to sort newly flushed segments, 
which will be smaller
+   * than merged segments
+   *
+   * @param reader the Reader to sort
+   */
+  public abstract DocComparator getDocComparator(LeafReader reader) throws 
IOException;
+
+  /**
+   * Serializes the parent SortField.  This is used to write Sort information 
into the Segment header
+   *
+   * @see SortFieldProvider#loadSortField(DataInput)
+   */
+  public abstract void serialize(DataOutput out) throws IOException;
+
+  /**
+   * The SPI-registered name of a {@link SortFieldProvider} that will 
deserialize the parent SortField
+   */
+  public abstract String getProviderName();
+
+  /**
+   * Provide a NumericDocValues instance for a LeafReader
+   */
+  public interface NumericDocValuesProvider {
+/**
+ * Returns the NumericDocValues instance for this LeafReader
+ */
+NumericDocValues get(LeafReader reader) throws IOException;
+  }
+
+  /**
+   * Provide a SortedDocValues instance for a LeafReader
+   */
+  public interface SortedDocValuesProvider {
+/**
+ * Returns the SortedDocValues instance for this LeafReader
+ */
+SortedDocValues get(LeafReader reader) throws IOException;
+  }
+
+  /**
+   * Serialize an object into a DataOutput
+   */
+  public interface Serializ

[GitHub] [lucene-solr] romseygeek commented on a change in pull request #1440: LUCENE-9330: Make SortFields responsible for index sorting and serialization

2020-04-23 Thread GitBox


romseygeek commented on a change in pull request #1440:
URL: https://github.com/apache/lucene-solr/pull/1440#discussion_r413689133



##
File path: 
lucene/core/src/java/org/apache/lucene/index/DefaultIndexingChain.java
##
@@ -527,45 +589,63 @@ private void indexPoint(PerField fp, IndexableField 
field) throws IOException {
 fp.pointValuesWriter.addPackedValue(docState.docID, field.binaryValue());
   }
 
-  private void validateIndexSortDVType(Sort indexSort, String fieldName, 
DocValuesType dvType) {
+  private void validateIndexSortDVType(Sort indexSort, String fieldToValidate, 
DocValuesType dvType) throws IOException {
 for (SortField sortField : indexSort.getSort()) {
-  if (sortField.getField().equals(fieldName)) {
-switch (dvType) {
-  case NUMERIC:
-if (sortField.getType().equals(SortField.Type.INT) == false &&
-  sortField.getType().equals(SortField.Type.LONG) == false &&
-  sortField.getType().equals(SortField.Type.FLOAT) == false &&
-  sortField.getType().equals(SortField.Type.DOUBLE) == false) {
-  throw new IllegalArgumentException("invalid doc value type:" + 
dvType + " for sortField:" + sortField);
-}
-break;
+  IndexSorter sorter = sortField.getIndexSorter();
+  if (sorter == null) {
+throw new IllegalStateException("Cannot sort index with sort order " + 
sortField);
+  }
+  sorter.getDocComparator(new DocValuesLeafReader() {
+@Override
+public NumericDocValues getNumericDocValues(String field) {
+  if (Objects.equals(field, fieldToValidate) && dvType != 
DocValuesType.NUMERIC) {
+throw new IllegalArgumentException("SortField " + sortField + " 
expected field [" + field + "] to be NUMERIC but it is [" + dvType + "]");
+  }
+  return DocValues.emptyNumeric();
+}
 
-  case BINARY:
-throw new IllegalArgumentException("invalid doc value type:" + 
dvType + " for sortField:" + sortField);
+@Override
+public BinaryDocValues getBinaryDocValues(String field) {
+  if (Objects.equals(field, fieldToValidate) && dvType != 
DocValuesType.BINARY) {
+throw new IllegalArgumentException("SortField " + sortField + " 
expected field [" + field + "] to be BINARY but it is [" + dvType + "]");
+  }
+  return DocValues.emptyBinary();
+}
 
-  case SORTED:
-if (sortField.getType().equals(SortField.Type.STRING) == false) {
-  throw new IllegalArgumentException("invalid doc value type:" + 
dvType + " for sortField:" + sortField);
-}
-break;
+@Override
+public SortedDocValues getSortedDocValues(String field) {
+  if (Objects.equals(field, fieldToValidate) && dvType != 
DocValuesType.SORTED) {
+throw new IllegalArgumentException("SortField " + sortField + " 
expected field [" + field + "] to be SORTED but it is [" + dvType + "]");
+  }
+  return DocValues.emptySorted();
+}
 
-  case SORTED_NUMERIC:
-if (sortField instanceof SortedNumericSortField == false) {
-  throw new IllegalArgumentException("invalid doc value type:" + 
dvType + " for sortField:" + sortField);
-}
-break;
+@Override
+public SortedNumericDocValues getSortedNumericDocValues(String field) {
+  if (Objects.equals(field, fieldToValidate) && dvType != 
DocValuesType.SORTED_NUMERIC) {
+throw new IllegalArgumentException("SortField " + sortField + " 
expected field [" + field + "] to be SORTED_NUMERIC but it is [" + dvType + 
"]");
+  }
+  return DocValues.emptySortedNumeric(0);
+}
 
-  case SORTED_SET:
-if (sortField instanceof SortedSetSortField == false) {
-  throw new IllegalArgumentException("invalid doc value type:" + 
dvType + " for sortField:" + sortField);
-}
-break;
+@Override
+public SortedSetDocValues getSortedSetDocValues(String field) {
+  if (Objects.equals(field, fieldToValidate) && dvType != 
DocValuesType.SORTED_SET) {
+throw new IllegalArgumentException("SortField " + sortField + " 
expected field [" + field + "] to be SORTED_SET but it is [" + dvType + "]");
+  }
+  return DocValues.emptySortedSet();
+}
 
-  default:
-throw new IllegalArgumentException("invalid doc value type:" + 
dvType + " for sortField:" + sortField);
+@Override
+public FieldInfos getFieldInfos() {
+  throw new UnsupportedOperationException();
 }
-break;
-  }
+
+@Override
+public int maxDoc() {
+  return 0;

Review comment:
   `IndexSorter.getDocComparator(Reader)` calls `maxDoc()` on the reader to 
allocate its comparison arrays.  We co

[GitHub] [lucene-solr] romseygeek commented on a change in pull request #1440: LUCENE-9330: Make SortFields responsible for index sorting and serialization

2020-04-23 Thread GitBox


romseygeek commented on a change in pull request #1440:
URL: https://github.com/apache/lucene-solr/pull/1440#discussion_r413692946



##
File path: 
lucene/core/src/java/org/apache/lucene/index/SortedDocValuesWriter.java
##
@@ -79,11 +78,6 @@ public void addValue(int docID, BytesRef value) {
 lastDocID = docID;
   }
 
-  @Override
-  public void finish(int maxDoc) {
-updateBytesUsed();
-  }

Review comment:
   It was always called either immediately before `flush` or 
`getDocComparator`, so it seemed to make sense to just fold it directly into 
those methods.





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.

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