Skip to content

Apache Lucene open-source search software

Merged /lucene/dev/trunk:r1433031-1435376

Robert Muir619d43122aa7
diff --cc lucene/core/src/java/org/apache/lucene/codecs/lucene41/Lucene41DocValuesProducer.java
index 795d8701911,00000000000..9318ae493c2
mode 100644,000000..100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene41/Lucene41DocValuesProducer.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene41/Lucene41DocValuesProducer.java
@@@ -1,288 -1,0 +1,288 @@@
+package org.apache.lucene.codecs.lucene41;
+
+/*
+ * 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.
+ */
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.lucene.codecs.CodecUtil;
+import org.apache.lucene.codecs.DocValuesProducer;
+import org.apache.lucene.index.BinaryDocValues;
+import org.apache.lucene.index.CorruptIndexException;
+import org.apache.lucene.index.FieldInfo;
+import org.apache.lucene.index.FieldInfos;
+import org.apache.lucene.index.IndexFileNames;
+import org.apache.lucene.index.NumericDocValues;
+import org.apache.lucene.index.SegmentReadState;
+import org.apache.lucene.index.SortedDocValues;
+import org.apache.lucene.index.TermsEnum;
+import org.apache.lucene.store.IndexInput;
+import org.apache.lucene.util.BytesRef;
+import org.apache.lucene.util.IOUtils;
+import org.apache.lucene.util.IntsRef;
+import org.apache.lucene.util.fst.BytesRefFSTEnum;
+import org.apache.lucene.util.fst.BytesRefFSTEnum.InputOutput;
+import org.apache.lucene.util.fst.FST;
+import org.apache.lucene.util.fst.FST.Arc;
+import org.apache.lucene.util.fst.FST.BytesReader;
+import org.apache.lucene.util.fst.PositiveIntOutputs;
+import org.apache.lucene.util.fst.Util;
+import org.apache.lucene.util.packed.PackedInts;
+
+class Lucene41DocValuesProducer extends DocValuesProducer {
+ // metadata maps (just file pointers and minimal stuff)
+ private final Map<Integer,NumericEntry> numerics;
+ private final Map<Integer,BinaryEntry> binaries;
+ private final Map<Integer,FSTEntry> fsts;
+ private final IndexInput data;
+
+ // ram instances we have already loaded
+ private final Map<Integer,NumericDocValues> numericInstances =
+ new HashMap<Integer,NumericDocValues>();
+
+ // if this thing needs some TL state then we might put something
+ // else in this map.
+ private final Map<Integer,BinaryDocValues> binaryInstances =
+ new HashMap<Integer,BinaryDocValues>();
+
+ private final Map<Integer,FST<Long>> fstInstances =
+ new HashMap<Integer,FST<Long>>();
+
+ Lucene41DocValuesProducer(SegmentReadState state, String dataCodec, String dataExtension, String metaCodec, String metaExtension) throws IOException {
+ String metaName = IndexFileNames.segmentFileName(state.segmentInfo.name, state.segmentSuffix, metaExtension);
+ // read in the entries from the metadata file.
+ IndexInput in = state.directory.openInput(metaName, state.context);
+ boolean success = false;
+ try {
+ CodecUtil.checkHeader(in, metaCodec,
+ Lucene41DocValuesConsumer.VERSION_START,
+ Lucene41DocValuesConsumer.VERSION_START);
+ numerics = new HashMap<Integer,NumericEntry>();
+ binaries = new HashMap<Integer,BinaryEntry>();
+ fsts = new HashMap<Integer,FSTEntry>();
+ readFields(in, state.fieldInfos);
+ success = true;
+ } finally {
+ if (success) {
+ IOUtils.close(in);
+ } else {
+ IOUtils.closeWhileHandlingException(in);
+ }
+ }
+
+ String dataName = IndexFileNames.segmentFileName(state.segmentInfo.name, state.segmentSuffix, dataExtension);
+ data = state.directory.openInput(dataName, state.context);
+ CodecUtil.checkHeader(data, dataCodec,
+ Lucene41DocValuesConsumer.VERSION_START,
+ Lucene41DocValuesConsumer.VERSION_START);
+ }
+
+ private void readFields(IndexInput meta, FieldInfos infos) throws IOException {
+ int fieldNumber = meta.readVInt();
+ while (fieldNumber != -1) {
+ int fieldType = meta.readByte();
+ if (fieldType == Lucene41DocValuesConsumer.NUMBER) {
+ NumericEntry entry = new NumericEntry();
+ entry.offset = meta.readLong();
+ entry.tableized = meta.readByte() != 0;
+ numerics.put(fieldNumber, entry);
+ } else if (fieldType == Lucene41DocValuesConsumer.BYTES) {
+ BinaryEntry entry = new BinaryEntry();
+ entry.offset = meta.readLong();
+ entry.numBytes = meta.readLong();
+ entry.minLength = meta.readVInt();
+ entry.maxLength = meta.readVInt();
+ binaries.put(fieldNumber, entry);
+ } else if (fieldType == Lucene41DocValuesConsumer.FST) {
+ FSTEntry entry = new FSTEntry();
+ entry.offset = meta.readLong();
+ entry.numOrds = meta.readVInt();
+ fsts.put(fieldNumber, entry);
+ } else {
+ throw new CorruptIndexException("invalid entry type: " + fieldType + ", input=" + meta);
+ }
+ fieldNumber = meta.readVInt();
+ }
+ }
+
+ @Override
+ public synchronized NumericDocValues getNumeric(FieldInfo field) throws IOException {
+ NumericDocValues instance = numericInstances.get(field.number);
+ if (instance == null) {
+ instance = loadNumeric(field);
+ numericInstances.put(field.number, instance);
+ }
+ return instance;
+ }
+
+ private NumericDocValues loadNumeric(FieldInfo field) throws IOException {
+ NumericEntry entry = numerics.get(field.number);
+ data.seek(entry.offset);
+ if (entry.tableized) {
+ int size = data.readVInt();
+ final long decode[] = new long[size];
+ for (int i = 0; i < decode.length; i++) {
+ decode[i] = data.readLong();
+ }
+ final long minValue = data.readLong();
+ assert minValue == 0;
+ final PackedInts.Reader reader = PackedInts.getReader(data);
+ return new NumericDocValues() {
+ @Override
+ public long get(int docID) {
+ return decode[(int)reader.get(docID)];
+ }
+ };
+ } else {
+ final long minValue = data.readLong();
+ final PackedInts.Reader reader = PackedInts.getReader(data);
+ return new NumericDocValues() {
+ @Override
+ public long get(int docID) {
+ return minValue + reader.get(docID);
+ }
+ };
+ }
+ }
+
+ @Override
+ public synchronized BinaryDocValues getBinary(FieldInfo field) throws IOException {
+ BinaryDocValues instance = binaryInstances.get(field.number);
+ if (instance == null) {
+ instance = loadBinary(field);
+ binaryInstances.put(field.number, instance);
+ }
+ return instance;
+ }
+
+ private BinaryDocValues loadBinary(FieldInfo field) throws IOException {
+ BinaryEntry entry = binaries.get(field.number);
+ data.seek(entry.offset);
+ assert entry.numBytes < Integer.MAX_VALUE; // nocommit
+ final byte[] bytes = new byte[(int)entry.numBytes];
+ data.readBytes(bytes, 0, bytes.length);
+ if (entry.minLength == entry.maxLength) {
+ final int fixedLength = entry.minLength;
+ return new BinaryDocValues() {
+ @Override
+ public void get(int docID, BytesRef result) {
+ result.bytes = bytes;
+ result.offset = docID * fixedLength;
+ result.length = fixedLength;
+ }
+ };
+ } else {
+ final NumericDocValues addresses = getNumeric(field);
+ return new BinaryDocValues() {
+ @Override
+ public void get(int docID, BytesRef result) {
+ int startAddress = docID == 0 ? 0 : (int) addresses.get(docID-1);
+ int endAddress = (int)addresses.get(docID);
+ result.bytes = bytes;
+ result.offset = startAddress;
+ result.length = endAddress - startAddress;
+ }
+ };
+ }
+ }
+
+ @Override
+ public SortedDocValues getSorted(FieldInfo field) throws IOException {
+ final FSTEntry entry = fsts.get(field.number);
+ FST<Long> instance;
+ synchronized(this) {
+ instance = fstInstances.get(field.number);
+ if (instance == null) {
+ data.seek(entry.offset);
+ instance = new FST<Long>(data, PositiveIntOutputs.getSingleton(true));
+ fstInstances.put(field.number, instance);
+ }
+ }
+ final NumericDocValues docToOrd = getNumeric(field);
+ final FST<Long> fst = instance;
+
+ // per-thread resources
- final BytesReader in = fst.getBytesReader(0);
++ final BytesReader in = fst.getBytesReader();
+ final Arc<Long> firstArc = new Arc<Long>();
+ final Arc<Long> scratchArc = new Arc<Long>();
+ final IntsRef scratchInts = new IntsRef();
+ final BytesRefFSTEnum<Long> fstEnum = new BytesRefFSTEnum<Long>(fst);
+
+ return new SortedDocValues() {
+ @Override
+ public int getOrd(int docID) {
+ return (int) docToOrd.get(docID);
+ }
+
+ @Override
+ public void lookupOrd(int ord, BytesRef result) {
+ try {
+ in.setPosition(0);
+ fst.getFirstArc(firstArc);
+ Util.toBytesRef(Util.getByOutput(fst, ord, in, firstArc, scratchArc, scratchInts), result);
+ } catch (IOException bogus) {
+ throw new RuntimeException(bogus);
+ }
+ }
+
+ @Override
+ public int lookupTerm(BytesRef key, BytesRef spare) {
+ try {
+ InputOutput<Long> o = fstEnum.seekCeil(key);
+ if (o == null) {
+ return -getValueCount()-1;
+ } else if (o.input.equals(key)) {
+ return o.output.intValue();
+ } else {
+ return (int) -o.output-1;
+ }
+ } catch (IOException bogus) {
+ throw new RuntimeException(bogus);
+ }
+ }
+
+ @Override
+ public int getValueCount() {
+ return entry.numOrds;
+ }
+ };
+ }
+
+ @Override
+ public void close() throws IOException {
+ data.close();
+ }
+
+ static class NumericEntry {
+ long offset;
+ boolean tableized;
+ }
+
+ static class BinaryEntry {
+ long offset;
+ long numBytes;
+ int minLength;
+ int maxLength;
+ }
+
+ static class FSTEntry {
+ long offset;
+ int numOrds;
+ }
+}
diff --cc lucene/facet/src/java/org/apache/lucene/facet/associations/AssociationsIterator.java
index ff2663302ef,7f81461a11c..b764afda507
--- a/lucene/facet/src/java/org/apache/lucene/facet/associations/AssociationsIterator.java
+++ b/lucene/facet/src/java/org/apache/lucene/facet/associations/AssociationsIterator.java
@@@ -2,9 -2,9 +2,8 @@@ package org.apache.lucene.facet.associa
import java.io.IOException;
- import org.apache.lucene.facet.search.PayloadIterator;
import org.apache.lucene.index.AtomicReaderContext;
- import org.apache.lucene.index.Term;
-import org.apache.lucene.index.DocValues;
-import org.apache.lucene.index.DocValues.Source;
++import org.apache.lucene.index.BinaryDocValues;
import org.apache.lucene.store.ByteArrayDataInput;
import org.apache.lucene.util.BytesRef;
@@@ -30,25 -30,29 +29,23 @@@
*
* @lucene.experimental
*/
- public abstract class AssociationsPayloadIterator<T extends CategoryAssociation> {
+ public abstract class AssociationsIterator<T extends CategoryAssociation> {
- private final PayloadIterator pi;
private final T association;
+ private final String dvField;
- private final boolean useDirectSource;
+ private final BytesRef bytes = new BytesRef(32);
+
- private DocValues.Source current;
++ private BinaryDocValues current;
- /**
- * Marking whether there are associations (at all) in the given index
- */
- private boolean hasAssociations = false;
-
/**
* Construct a new associations iterator. The given
* {@link CategoryAssociation} is used to deserialize the association values.
* It is assumed that all association values can be deserialized with the
* given {@link CategoryAssociation}.
- *
- * <p>
- * <b>NOTE:</b> if {@code useDirectSource} is {@code false}, then a
- * {@link DocValues#getSource()} is used, which is an in-memory {@link Source}.
*/
- public AssociationsPayloadIterator(String field, T association) throws IOException {
- pi = new PayloadIterator(new Term(field, association.getCategoryListID()));
- public AssociationsIterator(String field, T association, boolean useDirectSource) throws IOException {
++ public AssociationsIterator(String field, T association) throws IOException {
this.association = association;
+ this.dvField = field + association.getCategoryListID();
- this.useDirectSource = useDirectSource;
}
/**
@@@ -57,8 -61,14 +54,8 @@@
* of the documents belonging to the association given to the constructor.
*/
public final boolean setNextReader(AtomicReaderContext context) throws IOException {
- hasAssociations = pi.setNextReader(context);
- return hasAssociations;
- DocValues dv = context.reader().docValues(dvField);
- if (dv == null) {
- current = null;
- return false;
- }
-
- current = useDirectSource ? dv.getDirectSource() : dv.getSource();
- return true;
++ current = context.reader().getBinaryDocValues(dvField);
++ return current != null;
}
/**
@@@ -68,15 -78,11 +65,11 @@@
* extending classes.
*/
protected final boolean setNextDoc(int docID) throws IOException {
- if (!hasAssociations) { // there are no associations at all
- return false;
- current.getBytes(docID, bytes);
++ current.get(docID, bytes);
+ if (bytes.length == 0) {
+ return false; // no associations for the requested document
}
- BytesRef bytes = pi.getPayload(docID);
- if (bytes == null) { // no associations for the requested document
- return false;
- }
-
ByteArrayDataInput in = new ByteArrayDataInput(bytes.bytes, bytes.offset, bytes.length);
while (!in.eof()) {
int ordinal = in.readInt();
diff --cc lucene/facet/src/java/org/apache/lucene/facet/associations/FloatAssociationsIterator.java
index 4ddb077a546,d07b7cf428d..daa791e35f6
--- a/lucene/facet/src/java/org/apache/lucene/facet/associations/FloatAssociationsIterator.java
+++ b/lucene/facet/src/java/org/apache/lucene/facet/associations/FloatAssociationsIterator.java
@@@ -26,12 -28,25 +26,15 @@@ import org.apache.lucene.util.collectio
*
* @lucene.experimental
*/
- public class FloatAssociationsPayloadIterator extends AssociationsPayloadIterator<CategoryFloatAssociation> {
+ public class FloatAssociationsIterator extends AssociationsIterator<CategoryFloatAssociation> {
private final IntToFloatMap ordinalAssociations = new IntToFloatMap();
--
- public FloatAssociationsPayloadIterator(String field, CategoryFloatAssociation association) throws IOException {
- /**
- * Constructs a new {@link FloatAssociationsIterator} which uses an
- * in-memory {@link DocValues#getSource() DocValues source}.
- */
- public FloatAssociationsIterator(String field, CategoryFloatAssociation association) throws IOException {
- this(field, association, false);
- }
+
+ /**
- * Constructs a new {@link FloatAssociationsIterator} which uses a
- * {@link DocValues} {@link Source} per {@code useDirectSource}.
++ * Constructs a new {@link FloatAssociationsIterator}.
+ */
- public FloatAssociationsIterator(String field, CategoryFloatAssociation association, boolean useDirectSource)
- throws IOException {
- super(field, association, useDirectSource);
++ public FloatAssociationsIterator(String field, CategoryFloatAssociation association) throws IOException {
+ super(field, association);
}
@Override
diff --cc lucene/facet/src/java/org/apache/lucene/facet/associations/IntAssociationsIterator.java
index c400c6d8768,65178c0659a..0a7b74b52bc
--- a/lucene/facet/src/java/org/apache/lucene/facet/associations/IntAssociationsIterator.java
+++ b/lucene/facet/src/java/org/apache/lucene/facet/associations/IntAssociationsIterator.java
@@@ -30,8 -32,21 +30,11 @@@ public class IntAssociationsIterator ex
private final IntToIntMap ordinalAssociations = new IntToIntMap();
- public IntAssociationsPayloadIterator(String field, CategoryIntAssociation association) throws IOException {
+ /**
- * Constructs a new {@link IntAssociationsIterator} which uses an
- * in-memory {@link DocValues#getSource() DocValues source}.
++ * Constructs a new {@link IntAssociationsIterator}.
+ */
+ public IntAssociationsIterator(String field, CategoryIntAssociation association) throws IOException {
- this(field, association, false);
- }
-
- /**
- * Constructs a new {@link IntAssociationsIterator} which uses a
- * {@link DocValues} {@link Source} per {@code useDirectSource}.
- */
- public IntAssociationsIterator(String field, CategoryIntAssociation association, boolean useDirectSource)
- throws IOException {
- super(field, association, useDirectSource);
+ super(field, association);
}
@Override
diff --cc lucene/facet/src/java/org/apache/lucene/facet/search/DocValuesCategoryListIterator.java
index b8639c3cc0d,bdef15cf276..f0d86d02880
--- a/lucene/facet/src/java/org/apache/lucene/facet/search/DocValuesCategoryListIterator.java
+++ b/lucene/facet/src/java/org/apache/lucene/facet/search/DocValuesCategoryListIterator.java
@@@ -3,7 -3,8 +3,7 @@@ package org.apache.lucene.facet.search
import java.io.IOException;
import org.apache.lucene.index.AtomicReaderContext;
- import org.apache.lucene.index.Term;
-import org.apache.lucene.index.DocValues;
-import org.apache.lucene.index.DocValues.Source;
++import org.apache.lucene.index.BinaryDocValues;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.IntsRef;
import org.apache.lucene.util.encoding.IntDecoder;
@@@ -25,57 -26,80 +25,62 @@@
* limitations under the License.
*/
- /**
- * A {@link CategoryListIterator} which reads the category ordinals from a
- * payload.
- *
- * @lucene.experimental
- */
- public class PayloadCategoryListIteraor implements CategoryListIterator {
-
-/** A {@link CategoryListIterator} which reads the ordinals from a {@link DocValues}. */
++/** A {@link CategoryListIterator} which reads the ordinals from a {@link BinaryDocValues}. */
+ public class DocValuesCategoryListIterator implements CategoryListIterator {
+
private final IntDecoder decoder;
- private final Term term;
- private final PayloadIterator pi;
+ private final String field;
private final int hashCode;
- private final boolean useDirectSource;
+ private final BytesRef bytes = new BytesRef(32);
- public PayloadCategoryListIteraor(Term term, IntDecoder decoder) throws IOException {
- pi = new PayloadIterator(term);
- private DocValues.Source current;
++ private BinaryDocValues current;
+
+ /**
- * Constructs a new {@link DocValuesCategoryListIterator} which uses an
- * in-memory {@link Source}.
++ * Constructs a new {@link DocValuesCategoryListIterator}.
+ */
+ public DocValuesCategoryListIterator(String field, IntDecoder decoder) {
- this(field, decoder, false);
- }
-
- /**
- * Constructs a new {@link DocValuesCategoryListIterator} which uses either a
- * {@link DocValues#getDirectSource() direct source} or
- * {@link DocValues#getSource() in-memory} one.
- */
- public DocValuesCategoryListIterator(String field, IntDecoder decoder, boolean useDirectSource) {
+ this.field = field;
this.decoder = decoder;
- hashCode = term.hashCode();
- this.term = term;
+ this.hashCode = field.hashCode();
- this.useDirectSource = useDirectSource;
}
-
+
@Override
- public boolean equals(Object other) {
- if (!(other instanceof PayloadCategoryListIteraor)) {
+ public int hashCode() {
+ return hashCode;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (!(o instanceof DocValuesCategoryListIterator)) {
return false;
}
- PayloadCategoryListIteraor that = (PayloadCategoryListIteraor) other;
- if (hashCode != that.hashCode) {
+ DocValuesCategoryListIterator other = (DocValuesCategoryListIterator) o;
+ if (hashCode != other.hashCode) {
return false;
}
// Hash codes are the same, check equals() to avoid cases of hash-collisions.
- return term.equals(that.term);
+ return field.equals(other.field);
}
-
- @Override
- public int hashCode() {
- return hashCode;
- }
-
+
@Override
public boolean setNextReader(AtomicReaderContext context) throws IOException {
- return pi.setNextReader(context);
- DocValues dv = context.reader().docValues(field);
- if (dv == null) {
- current = null;
- return false;
- }
-
- current = useDirectSource ? dv.getDirectSource() : dv.getSource();
- return true;
++ current = context.reader().getBinaryDocValues(field);
++ return current != null;
}
@Override
public void getOrdinals(int docID, IntsRef ints) throws IOException {
- current.getBytes(docID, bytes);
++ current.get(docID, bytes);
ints.length = 0;
- BytesRef payload = pi.getPayload(docID);
- if (payload != null) {
- decoder.decode(payload, ints);
+ if (bytes.length > 0) {
+ decoder.decode(bytes, ints);
}
}
-
+
+ @Override
+ public String toString() {
+ return field;
+ }
+
}
diff --cc lucene/facet/src/test/org/apache/lucene/facet/search/TestMultipleCategoryLists.java
index 12a054c5be2,51967c58463..a614dc707ba
--- a/lucene/facet/src/test/org/apache/lucene/facet/search/TestMultipleCategoryLists.java
+++ b/lucene/facet/src/test/org/apache/lucene/facet/search/TestMultipleCategoryLists.java
@@@ -185,9 -179,14 +179,14 @@@ public class TestMultipleCategoryLists
IOUtils.close(dirs[0]);
}
- private void assertPostingListExists(String field, String text, IndexReader ir) throws IOException {
- DocsEnum de = _TestUtil.docs(random(), ir, field, new BytesRef(text), null, null, DocsEnum.FLAG_NONE);
- assertTrue(de.nextDoc() != DocIdSetIterator.NO_MORE_DOCS);
+ private void assertOrdinalsExist(String field, IndexReader ir) throws IOException {
+ for (AtomicReaderContext context : ir.leaves()) {
+ AtomicReader r = context.reader();
- if (r.docValues(field) != null) {
++ if (r.getBinaryDocValues(field) != null) {
+ return; // not all segments must have this DocValues
+ }
+ }
+ fail("no ordinals found for " + field);
}
@Test