Apache Lucene open-source search software
LUCENE-2186: more javadocs and code cleanups
Simon Willnauera798ef362b9d
diff --git a/lucene/src/java/org/apache/lucene/document/AbstractField.java b/lucene/src/java/org/apache/lucene/document/AbstractField.javaindex 2fd4397d12f..54ea023ba28 100755--- a/lucene/src/java/org/apache/lucene/document/AbstractField.java+++ b/lucene/src/java/org/apache/lucene/document/AbstractField.java@@ -49,6 +49,8 @@ public abstract class AbstractField implements Fieldable {// length/offset for all primitive typesprotected int binaryLength;protected int binaryOffset;+ protected PerDocFieldValues docValues;+protected AbstractField(){@@ -294,8 +296,7 @@ public abstract class AbstractField implements Fieldable {result.append('>');return result.toString();}- protected PerDocFieldValues docValues;-+public PerDocFieldValues getDocValues() {return docValues;}diff --git a/lucene/src/java/org/apache/lucene/document/DocValuesField.java b/lucene/src/java/org/apache/lucene/document/DocValuesField.javanew file mode 100644index 00000000000..1f9295859d4--- /dev/null+++ b/lucene/src/java/org/apache/lucene/document/DocValuesField.java@@ -0,0 +1,287 @@+package org.apache.lucene.document;++/**+ * 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.Reader;+import java.util.Comparator;++import org.apache.lucene.analysis.TokenStream;+import org.apache.lucene.document.Field.Index;+import org.apache.lucene.document.Field.Store;+import org.apache.lucene.document.Field.TermVector;+import org.apache.lucene.index.values.PerDocFieldValues;+import org.apache.lucene.index.values.Type;+import org.apache.lucene.util.BytesRef;++/**+ * <p>+ * This class provides a {@link AbstractField} that enables storing of typed+ * per-document values for scoring, sorting or value retrieval. Here's an+ * example usage, adding an int value:+ *+ * <pre>+ * document.add(new DocValuesField(name).setInt(value));+ * </pre>+ *+ * For optimal performance, re-use the <code>DocValuesField</code> and+ * {@link Document} instance for more than one document:+ *+ * <pre>+ * DocValuesField field = new DocValuesField(name);+ * Document document = new Document();+ * document.add(field);+ *+ * for(all documents) {+ * ...+ * field.setIntValue(value)+ * writer.addDocument(document);+ * ...+ * }+ * </pre>+ *+ * <p>+ * If doc values are stored in addition to an indexed ({@link Index}) or stored+ * ({@link Store}) value it's recommended to use the {@link DocValuesField}'s+ * {@link #set(AbstractField)} API:+ *+ * <pre>+ * DocValuesField field = new DocValuesField(name);+ * Field indexedField = new Field(name, stringValue, Stored.NO, Indexed.ANALYZED);+ * Document document = new Document();+ * document.add(indexedField);+ * field.set(indexedField);+ * for(all documents) {+ * ...+ * field.setIntValue(value)+ * writer.addDocument(document);+ * ...+ * }+ * </pre>+ *+ * */+@SuppressWarnings("serial")+public class DocValuesField extends AbstractField implements PerDocFieldValues {++ protected BytesRef bytes;+ protected double doubleValue;+ protected long longValue;+ protected Type type;+ protected Comparator<BytesRef> bytesComparator;++ /**+ * Creates a new {@link DocValuesField} with the given name.+ */+ public DocValuesField(String name) {+ super(name, Store.NO, Index.NO, TermVector.NO);+ setDocValues(this);+ }++ /**+ * Creates a {@link DocValuesField} prototype+ */+ DocValuesField() {+ this("");+ }++ /**+ * Sets the given <code>long</code> value and sets the field's {@link Type} to+ * {@link Type#PACKED_INTS} unless already set. If you want to change the+ * default type use {@link #setType(Type)}.+ */+ public void setInt(long value) {+ if (type == null) {+ type = Type.PACKED_INTS;+ }+ longValue = value;+ }++ /**+ * Sets the given <code>float</code> value and sets the field's {@link Type}+ * to {@link Type#SIMPLE_FLOAT_4BYTE} unless already set. If you want to+ * change the type use {@link #setType(Type)}.+ */+ public void setFloat(float value) {+ if (type == null) {+ type = Type.SIMPLE_FLOAT_4BYTE;+ }+ doubleValue = value;+ }++ /**+ * Sets the given <code>double</code> value and sets the field's {@link Type}+ * to {@link Type#SIMPLE_FLOAT_8BYTE} unless already set. If you want to+ * change the default type use {@link #setType(Type)}.+ */+ public void setFloat(double value) {+ if (type == null) {+ type = Type.SIMPLE_FLOAT_8BYTE;+ }+ doubleValue = value;+ }++ /**+ * Sets the given {@link BytesRef} value and the field's {@link Type}. The+ * comparator for this field is set to <code>null</code>. If a+ * <code>null</code> comparator is set the default comparator for the given+ * {@link Type} is used.+ */+ public void setBytes(BytesRef value, Type type) {+ setBytes(value, type, null);+ }++ /**+ * Sets the given {@link BytesRef} value, the field's {@link Type} and the+ * field's comparator. If the {@link Comparator} is set to <code>null</code>+ * the default for the given {@link Type} is used instead.+ *+ * @throws IllegalArgumentException+ * if the value or the type are null+ */+ public void setBytes(BytesRef value, Type type, Comparator<BytesRef> comp) {+ if (value == null) {+ throw new IllegalArgumentException("value must not be null");+ }+ setType(type);+ if (bytes == null) {+ bytes = new BytesRef(value);+ } else {+ bytes.copy(value);+ }+ bytesComparator = comp;+ }++ /**+ * Returns the set {@link BytesRef} or <code>null</code> if not set.+ */+ public BytesRef getBytes() {+ return bytes;+ }++ /**+ * Returns the set {@link BytesRef} comparator or <code>null</code> if not set+ */+ public Comparator<BytesRef> bytesComparator() {+ return bytesComparator;+ }++ /**+ * Returns the set floating point value or <code>0.0d</code> if not set.+ */+ public double getFloat() {+ return doubleValue;+ }++ /**+ * Returns the set <code>long</code> value of <code>0</code> if not set.+ */+ public long getInt() {+ return longValue;+ }++ /**+ * Sets the {@link BytesRef} comparator for this field. If the field has a+ * numeric {@link Type} the comparator will be ignored.+ */+ public void setBytesComparator(Comparator<BytesRef> comp) {+ this.bytesComparator = comp;+ }++ /**+ * Sets the {@link Type} for this field.+ */+ public void setType(Type type) {+ if (type == null) {+ throw new IllegalArgumentException("Type must not be null");+ }+ this.type = type;+ }++ /**+ * Returns the field's {@link Type}+ */+ public Type type() {+ return type;+ }++ /**+ * Returns always <code>null</code>+ */+ public Reader readerValue() {+ return null;+ }++ /**+ * Returns always <code>null</code>+ */+ public String stringValue() {+ return null;+ }++ /**+ * Returns always <code>null</code>+ */+ public TokenStream tokenStreamValue() {+ return null;+ }++ /**+ * Sets this {@link DocValuesField} to the given {@link AbstractField} and+ * returns the given field. Any modifications to this instance will be visible+ * to the given field.+ */+ public <T extends AbstractField> T set(T field) {+ field.setDocValues(this);+ return field;+ }++ /**+ * Sets a new {@link PerDocFieldValues} instance on the given field with the+ * given type and returns it.+ *+ */+ public static <T extends AbstractField> T set(T field, Type type) {+ if (field instanceof DocValuesField)+ return field;+ final DocValuesField valField = new DocValuesField();+ switch (type) {+ case BYTES_FIXED_DEREF:+ case BYTES_FIXED_SORTED:+ case BYTES_FIXED_STRAIGHT:+ case BYTES_VAR_DEREF:+ case BYTES_VAR_SORTED:+ case BYTES_VAR_STRAIGHT:+ BytesRef ref = field.isBinary() ? new BytesRef(field.getBinaryValue(),+ field.getBinaryOffset(), field.getBinaryLength()) : new BytesRef(+ field.stringValue());+ valField.setBytes(ref, type);+ break;+ case PACKED_INTS:+ valField.setInt(Long.parseLong(field.stringValue()));+ break;+ case SIMPLE_FLOAT_4BYTE:+ valField.setFloat(Float.parseFloat(field.stringValue()));+ break;+ case SIMPLE_FLOAT_8BYTE:+ valField.setFloat(Double.parseDouble(field.stringValue()));+ break;+ default:+ throw new IllegalArgumentException("unknown type: " + type);+ }+ return valField.set(field);+ }++}diff --git a/lucene/src/java/org/apache/lucene/document/Fieldable.java b/lucene/src/java/org/apache/lucene/document/Fieldable.javaindex 60a456af33d..35d2d06d611 100755--- a/lucene/src/java/org/apache/lucene/document/Fieldable.java+++ b/lucene/src/java/org/apache/lucene/document/Fieldable.java@@ -18,6 +18,7 @@ package org.apache.lucene.document;import org.apache.lucene.analysis.TokenStream;import org.apache.lucene.index.FieldInvertState; // for javadocs+import org.apache.lucene.index.values.DocValues;import org.apache.lucene.index.values.PerDocFieldValues;import org.apache.lucene.index.values.Type;import org.apache.lucene.search.PhraseQuery; // for javadocs@@ -212,11 +213,28 @@ public interface Fieldable extends Serializable {*/void setOmitTermFreqAndPositions(boolean omitTermFreqAndPositions);- public PerDocFieldValues getDocValues() ;-+ /**+ * Returns the {@link PerDocFieldValues}+ */+ public PerDocFieldValues getDocValues();++ /**+ * Sets the {@link PerDocFieldValues} for this field. If+ * {@link PerDocFieldValues} is set this field will store per-document values+ *+ * @see DocValues+ */public void setDocValues(PerDocFieldValues docValues);-++ /**+ * Returns <code>true</code> iff {@link PerDocFieldValues} are set on this+ * field.+ */public boolean hasDocValues();-++ /**+ * Returns the {@link Type} of the set {@link PerDocFieldValues} or+ * <code>null</code> if not set.+ */public Type docValuesType();}diff --git a/lucene/src/java/org/apache/lucene/document/ValuesField.java b/lucene/src/java/org/apache/lucene/document/ValuesField.javadeleted file mode 100644index 105ac86a355..00000000000--- a/lucene/src/java/org/apache/lucene/document/ValuesField.java+++ /dev/null@@ -1,156 +0,0 @@-package org.apache.lucene.document;--/**- * 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.Reader;-import java.util.Comparator;--import org.apache.lucene.analysis.TokenStream;-import org.apache.lucene.document.Field.Index;-import org.apache.lucene.document.Field.Store;-import org.apache.lucene.document.Field.TermVector;-import org.apache.lucene.index.values.PerDocFieldValues;-import org.apache.lucene.index.values.Type;-import org.apache.lucene.util.BytesRef;--/**- *- */-@SuppressWarnings("serial")-public class ValuesField extends AbstractField implements PerDocFieldValues {-- protected BytesRef bytes;- protected double doubleValue;- protected long longValue;- protected Type type;- protected Comparator<BytesRef> bytesComparator;-- public ValuesField(String name) {- super(name, Store.NO, Index.NO, TermVector.NO);- setDocValues(this);- }-- ValuesField() {- this("");- }-- public void setInt(long value) {- type = Type.PACKED_INTS;- longValue = value;- }-- public void setFloat(float value) {- type = Type.SIMPLE_FLOAT_4BYTE;- doubleValue = value;- }-- public void setFloat(double value) {- type = Type.SIMPLE_FLOAT_8BYTE;- doubleValue = value;- }-- public void setBytes(BytesRef value, Type type) {- setBytes(value, type, null);-- }-- public void setBytes(BytesRef value, Type type, Comparator<BytesRef> comp) {- this.type = type;- if (bytes == null) {- this.bytes = new BytesRef();- }- bytes.copy(value);- bytesComparator = comp;- }-- public BytesRef getBytes() {- return bytes;- }-- public Comparator<BytesRef> bytesComparator() {- return bytesComparator;- }-- public double getFloat() {- return doubleValue;- }-- public long getInt() {- return longValue;- }-- public void setBytesComparator(Comparator<BytesRef> comp) {- this.bytesComparator = comp;- }-- public void setType(Type type) {- this.type = type;- }-- public Type type() {- return type;- }-- public Reader readerValue() {- return null;- }-- public String stringValue() {- return null;- }-- public TokenStream tokenStreamValue() {- return tokenStream;- }-- public <T extends AbstractField> T set(T field) {- field.setDocValues(this);- return field;- }-- public static <T extends AbstractField> T set(T field, Type type) {- if (field instanceof ValuesField)- return field;- final ValuesField valField = new ValuesField();- switch (type) {- case BYTES_FIXED_DEREF:- case BYTES_FIXED_SORTED:- case BYTES_FIXED_STRAIGHT:- case BYTES_VAR_DEREF:- case BYTES_VAR_SORTED:- case BYTES_VAR_STRAIGHT:- BytesRef ref = field.isBinary() ? new BytesRef(field.getBinaryValue(),- field.getBinaryOffset(), field.getBinaryLength()) : new BytesRef(- field.stringValue());- valField.setBytes(ref, type);- break;- case PACKED_INTS:- valField.setInt(Long.parseLong(field.stringValue()));- break;- case SIMPLE_FLOAT_4BYTE:- valField.setFloat(Float.parseFloat(field.stringValue()));- break;- case SIMPLE_FLOAT_8BYTE:- valField.setFloat(Double.parseDouble(field.stringValue()));- break;- default:- throw new IllegalArgumentException("unknown type: " + type);- }- return valField.set(field);- }--}diff --git a/lucene/src/java/org/apache/lucene/index/values/Bytes.java b/lucene/src/java/org/apache/lucene/index/values/Bytes.javaindex 5521e373425..d3dedd406a2 100644--- a/lucene/src/java/org/apache/lucene/index/values/Bytes.java+++ b/lucene/src/java/org/apache/lucene/index/values/Bytes.java@@ -178,6 +178,7 @@ public final class Bytes {throw new IllegalArgumentException("Illegal Mode: " + mode);}+ // TODO open up this API?static abstract class BytesBaseSource extends Source {protected final IndexInput datIn;protected final IndexInput idxIn;@@ -325,6 +326,7 @@ public final class Bytes {}}+ // TODO: open up this API?!static abstract class BytesWriterBase extends Writer {private final Directory dir;diff --git a/lucene/src/java/org/apache/lucene/index/values/DocValues.java b/lucene/src/java/org/apache/lucene/index/values/DocValues.javaindex f6d928ca734..44ae7873e35 100644--- a/lucene/src/java/org/apache/lucene/index/values/DocValues.java+++ b/lucene/src/java/org/apache/lucene/index/values/DocValues.java@@ -129,7 +129,7 @@ public abstract class DocValues implements Closeable {* close {@link DocValues} instances.*/public void close() throws IOException {- this.cache.close(this);+ cache.close(this);}/**@@ -141,9 +141,12 @@ public abstract class DocValues implements Closeable {* <p>* Note: All instances previously obtained from {@link #load()} or* {@link #loadSorted(Comparator)} will be closed.+ * @throws IllegalArgumentException if the given cache is <code>null</code>+ **/public void setCache(SourceCache cache) {- assert cache != null : "cache must not be null";+ if (cache == null)+ throw new IllegalArgumentException("cache must not be null");synchronized (this.cache) {this.cache.close(this);this.cache = cache;diff --git a/lucene/src/java/org/apache/lucene/index/values/DocValuesEnum.java b/lucene/src/java/org/apache/lucene/index/values/DocValuesEnum.javaindex 11914e79213..60f9d655732 100644--- a/lucene/src/java/org/apache/lucene/index/values/DocValuesEnum.java+++ b/lucene/src/java/org/apache/lucene/index/values/DocValuesEnum.java@@ -23,7 +23,21 @@ import org.apache.lucene.util.AttributeSource;import org.apache.lucene.util.BytesRef;import org.apache.lucene.util.FloatsRef;import org.apache.lucene.util.LongsRef;+/**+ * {@link DocValuesEnum} is a {@link DocIdSetIterator} iterating <tt>byte[]</tt>+ * , <tt>long</tt> and <tt>double</tt> stored per document. Depending on the+ * enum's {@link Type} ({@link #type()}) the enum might skip over documents that+ * have no value stored. Types like {@link Type#BYTES_VAR_STRAIGHT} might not+ * skip over documents even if there is no value associated with a document. The+ * value for document without values again depends on the types implementation+ * although a reference for a {@link Type} returned from a accessor method+ * {@link #getFloat()}, {@link #getInt()} or {@link #bytes()} will never be+ * <code>null</code> even if a document has no value.+ * <p>+ * Note: Only the reference for the enum's type are initialized to non+ * <code>null</code> ie. {@link #getInt()} will always return <code>null</code>+ * if the enum's Type is {@link Type#SIMPLE_FLOAT_4BYTE}.** @lucene.experimental*/@@ -34,10 +48,17 @@ public abstract class DocValuesEnum extends DocIdSetIterator {protected FloatsRef floatsRef;protected LongsRef intsRef;+ /**+ * Creates a new {@link DocValuesEnum} for the given type. The+ * {@link AttributeSource} for this enum is set to <code>null</code>+ */protected DocValuesEnum(Type enumType) {this(null, enumType);}+ /**+ * Creates a new {@link DocValuesEnum} for the given type.+ */protected DocValuesEnum(AttributeSource source, Type enumType) {this.source = source;this.enumType = enumType;@@ -56,32 +77,57 @@ public abstract class DocValuesEnum extends DocIdSetIterator {case SIMPLE_FLOAT_4BYTE:case SIMPLE_FLOAT_8BYTE:floatsRef = new FloatsRef(1);- break;+ break;}}+ /**+ * Returns the type of this enum+ */public Type type() {return enumType;}+ /**+ * Returns a {@link BytesRef} or <code>null</code> if this enum doesn't+ * enumerate byte[] values+ */public BytesRef bytes() {return bytesRef;}+ /**+ * Returns a {@link FloatsRef} or <code>null</code> if this enum doesn't+ * enumerate floating point values+ */public FloatsRef getFloat() {return floatsRef;}+ /**+ * Returns a {@link LongsRef} or <code>null</code> if this enum doesn't+ * enumerate integer values.+ */public LongsRef getInt() {return intsRef;}-- protected void copyReferences(DocValuesEnum valuesEnum) {++ /**+ * Copies the internal state from the given enum+ */+ protected void copyFrom(DocValuesEnum valuesEnum) {intsRef = valuesEnum.intsRef;floatsRef = valuesEnum.floatsRef;bytesRef = valuesEnum.bytesRef;+ source = valuesEnum.source;}+ /**+ * Returns the {@link AttributeSource} associated with this enum.+ * <p>+ * Note: this method might create a new AttribueSource if no+ * {@link AttributeSource} has been provided during enum creation.+ */public AttributeSource attributes() {if (source == null) {source = new AttributeSource();@@ -89,28 +135,37 @@ public abstract class DocValuesEnum extends DocIdSetIterator {return source;}+ /**+ * Closes the enum+ *+ * @throws IOException+ * if an {@link IOException} occurs+ */public abstract void close() throws IOException;+ /**+ * Returns an empty {@link DocValuesEnum} for the given {@link Type}.+ */public static DocValuesEnum emptyEnum(Type type) {return new DocValuesEnum(type) {@Overridepublic int nextDoc() throws IOException {return NO_MORE_DOCS;}-+@Overridepublic int docID() {return NO_MORE_DOCS;}-+@Overridepublic int advance(int target) throws IOException {return NO_MORE_DOCS;}-+@Overridepublic void close() throws IOException {-+}};}diff --git a/lucene/src/java/org/apache/lucene/index/values/FixedDerefBytesImpl.java b/lucene/src/java/org/apache/lucene/index/values/FixedDerefBytesImpl.javaindex 04da04ce9fd..4f582c5c7f1 100644--- a/lucene/src/java/org/apache/lucene/index/values/FixedDerefBytesImpl.java+++ b/lucene/src/java/org/apache/lucene/index/values/FixedDerefBytesImpl.java@@ -215,7 +215,7 @@ class FixedDerefBytesImpl {valueCount = idx.size();}- protected void copyReferences(DocValuesEnum valuesEnum) {+ protected void copyFrom(DocValuesEnum valuesEnum) {bytesRef = valuesEnum.bytesRef;if (bytesRef.bytes.length < size) {bytesRef.grow(size);diff --git a/lucene/src/java/org/apache/lucene/index/values/FixedStraightBytesImpl.java b/lucene/src/java/org/apache/lucene/index/values/FixedStraightBytesImpl.javaindex 993e0898f8a..82ae24b7355 100644--- a/lucene/src/java/org/apache/lucene/index/values/FixedStraightBytesImpl.java+++ b/lucene/src/java/org/apache/lucene/index/values/FixedStraightBytesImpl.java@@ -198,7 +198,7 @@ class FixedStraightBytesImpl {fp = datIn.getFilePointer();}- protected void copyReferences(DocValuesEnum valuesEnum) {+ protected void copyFrom(DocValuesEnum valuesEnum) {bytesRef = valuesEnum.bytesRef;if (bytesRef.bytes.length < size) {bytesRef.grow(size);diff --git a/lucene/src/java/org/apache/lucene/index/values/Ints.java b/lucene/src/java/org/apache/lucene/index/values/Ints.javaindex b374b039571..52dbcc6f2e5 100644--- a/lucene/src/java/org/apache/lucene/index/values/Ints.java+++ b/lucene/src/java/org/apache/lucene/index/values/Ints.java@@ -28,7 +28,7 @@ import org.apache.lucene.store.Directory;* @lucene.experimental*/public class Ints {-//TODO - add bulk copy where possible+ // TODO - add bulk copy where possibleprivate Ints() {}diff --git a/lucene/src/java/org/apache/lucene/index/values/MultiDocValues.java b/lucene/src/java/org/apache/lucene/index/values/MultiDocValues.javaindex f310378cf54..5ffa1b1a9cf 100644--- a/lucene/src/java/org/apache/lucene/index/values/MultiDocValues.java+++ b/lucene/src/java/org/apache/lucene/index/values/MultiDocValues.java@@ -124,7 +124,7 @@ public class MultiDocValues extends DocValues {maxDoc = last.start + last.length;final DocValuesIndex idx = docValuesIdx[0];currentEnum = idx.docValues.getEnum(this.attributes());- currentEnum.copyReferences(this);+ currentEnum.copyFrom(this);intsRef = currentEnum.intsRef;currentMax = idx.length;currentStart = 0;@@ -148,8 +148,8 @@ public class MultiDocValues extends DocValues {if (target >= currentMax) {final int idx = ReaderUtil.subIndex(target, starts);currentEnum.close();- currentEnum = docValuesIdx[idx].docValues.getEnum(this.attributes());- currentEnum.copyReferences(this);+ currentEnum = docValuesIdx[idx].docValues.getEnum();+ currentEnum.copyFrom(this);currentStart = docValuesIdx[idx].start;currentMax = currentStart + docValuesIdx[idx].length;relativeDoc = target - currentStart;diff --git a/lucene/src/java/org/apache/lucene/index/values/PerDocFieldValues.java b/lucene/src/java/org/apache/lucene/index/values/PerDocFieldValues.javaindex f60fcd4e0e5..78958c23ca3 100644--- a/lucene/src/java/org/apache/lucene/index/values/PerDocFieldValues.java+++ b/lucene/src/java/org/apache/lucene/index/values/PerDocFieldValues.java@@ -18,36 +18,84 @@ package org.apache.lucene.index.values;*/import java.util.Comparator;+import org.apache.lucene.document.DocValuesField;+import org.apache.lucene.document.Fieldable;+import org.apache.lucene.index.codecs.docvalues.DocValuesConsumer;import org.apache.lucene.util.BytesRef;/**+ * Per document and field values consumed by {@link DocValuesConsumer}.+ * @see DocValuesField+ * @see Fieldable#setDocValues(PerDocFieldValues)** @lucene.experimental*/public interface PerDocFieldValues {+ /**+ * Sets the given <code>long</code> value.+ */public void setInt(long value);+ /**+ * Sets the given <code>float</code> value.+ */public void setFloat(float value);+ /**+ * Sets the given <code>double</code> value.+ */public void setFloat(double value);+ /**+ * Sets the given {@link BytesRef} value and the field's {@link Type}. The+ * comparator for this field is set to <code>null</code>. If a+ * <code>null</code> comparator is set the default comparator for the given+ * {@link Type} is used.+ */public void setBytes(BytesRef value, Type type);+ /**+ * Sets the given {@link BytesRef} value, the field's {@link Type} and the+ * field's comparator. If the {@link Comparator} is set to <code>null</code>+ * the default for the given {@link Type} is used instead.+ */public void setBytes(BytesRef value, Type type, Comparator<BytesRef> comp);+ /**+ * Returns the set {@link BytesRef} or <code>null</code> if not set.+ */public BytesRef getBytes();+ /**+ * Returns the set {@link BytesRef} comparator or <code>null</code> if not set+ */public Comparator<BytesRef> bytesComparator();+ /**+ * Returns the set floating point value or <code>0.0d</code> if not set.+ */public double getFloat();+ /**+ * Returns the set <code>long</code> value of <code>0</code> if not set.+ */public long getInt();+ /**+ * Sets the {@link BytesRef} comparator for this field. If the field has a+ * numeric {@link Type} the comparator will be ignored.+ */public void setBytesComparator(Comparator<BytesRef> comp);+ /**+ * Sets the {@link Type}+ */public void setType(Type type);+ /**+ * Returns the {@link Type}+ */public Type type();}\ No newline at end of filediff --git a/lucene/src/java/org/apache/lucene/index/values/SourceCache.java b/lucene/src/java/org/apache/lucene/index/values/SourceCache.javaindex 81ef54f4677..565df06eb02 100644--- a/lucene/src/java/org/apache/lucene/index/values/SourceCache.java+++ b/lucene/src/java/org/apache/lucene/index/values/SourceCache.java@@ -20,40 +20,94 @@ package org.apache.lucene.index.values;import java.io.IOException;import java.util.Comparator;+import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.values.DocValues.SortedSource;import org.apache.lucene.index.values.DocValues.Source;import org.apache.lucene.util.BytesRef;/**- * Per {@link DocValues} {@link Source} cache.+ * Abstract base class for {@link DocValues} {@link Source} /+ * {@link SortedSource} cache.+ * <p>+ * {@link Source} and {@link SortedSource} instances loaded via+ * {@link DocValues#load()} and {@link DocValues#loadSorted(Comparator)} are+ * entirely memory resident and need to be maintained by the caller. Each call+ * to {@link DocValues#load()} or {@link DocValues#loadSorted(Comparator)} will+ * cause an entire reload of the underlying data. Source and+ * {@link SortedSource} instances obtained from {@link DocValues#getSource()}+ * and {@link DocValues#getSource()} respectively are maintained by a+ * {@link SourceCache} that is closed ({@link #close(DocValues)}) once the+ * {@link IndexReader} that created the {@link DocValues} instance is closed.+ * <p>+ * Unless {@link Source} and {@link SortedSource} instances are managed by+ * another entity it is recommended to use the cached variants to obtain a+ * source instance.+ * <p>+ * Implementation of this API must be thread-safe.+ *+ * @see DocValues#setCache(SourceCache)+ * @see DocValues#getSource()+ * @see DocValues#getSortedSorted(Comparator)+ ** @lucene.experimental*/public abstract class SourceCache {++ /**+ * Atomically loads a {@link Source} into the cache from the given+ * {@link DocValues} and returns it iff no other {@link Source} has already+ * been cached. Otherwise the cached source is returned.+ * <p>+ * This method will not return <code>null</code>+ */public abstract Source load(DocValues values) throws IOException;+ /**+ * Atomically loads a {@link SortedSource} into the cache from the given+ * {@link DocValues} and returns it iff no other {@link SortedSource} has+ * already been cached. Otherwise the cached source is returned.+ * <p>+ * This method will not return <code>null</code>+ */public abstract SortedSource loadSorted(DocValues values,Comparator<BytesRef> comp) throws IOException;+ /**+ * Atomically invalidates the cached {@link Source} and {@link SortedSource}+ * instances if any and empties the cache.+ */public abstract void invalidate(DocValues values);+ /**+ * Atomically closes the cache and frees all resources.+ */public synchronized void close(DocValues values) {invalidate(values);}+ /**+ * Simple per {@link DocValues} instance cache implementation that holds a+ * {@link Source} and {@link SortedSource} reference as a member variable.+ * <p>+ * If a {@link DirectSourceCache} instance is closed or invalidated the cached+ * reference are simply set to <code>null</code>+ */public static final class DirectSourceCache extends SourceCache {private Source ref;private SortedSource sortedRef;public synchronized Source load(DocValues values) throws IOException {- if (ref == null)+ if (ref == null) {ref = values.load();+ }return ref;}public synchronized SortedSource loadSorted(DocValues values,Comparator<BytesRef> comp) throws IOException {- if (sortedRef == null)+ if (sortedRef == null) {sortedRef = values.loadSorted(comp);+ }return sortedRef;}diff --git a/lucene/src/java/org/apache/lucene/index/values/Type.java b/lucene/src/java/org/apache/lucene/index/values/Type.javaindex b02fd0a9194..446f8a7b810 100644--- a/lucene/src/java/org/apache/lucene/index/values/Type.java+++ b/lucene/src/java/org/apache/lucene/index/values/Type.java@@ -1,7 +1,4 @@package org.apache.lucene.index.values;--import org.apache.lucene.index.values.DocValues.SortedSource;-/*** Licensed to the Apache Software Foundation (ASF) under one or more* contributor license agreements. See the NOTICE file distributed with@@ -19,6 +16,8 @@ import org.apache.lucene.index.values.DocValues.SortedSource;* limitations under the License.*/+import org.apache.lucene.index.values.DocValues.SortedSource;+/*** {@link Type} specifies the type of the {@link DocValues} for a certain field.* A {@link Type} can specify the actual data type for a field, used compressiondiff --git a/lucene/src/test/org/apache/lucene/index/values/TestDocValuesIndexing.java b/lucene/src/test/org/apache/lucene/index/values/TestDocValuesIndexing.javaindex b3ad9f5a213..32ad59b7132 100644--- a/lucene/src/test/org/apache/lucene/index/values/TestDocValuesIndexing.java+++ b/lucene/src/test/org/apache/lucene/index/values/TestDocValuesIndexing.java@@ -28,7 +28,7 @@ import org.apache.lucene.analysis.MockAnalyzer;import org.apache.lucene.document.AbstractField;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;-import org.apache.lucene.document.ValuesField;+import org.apache.lucene.document.DocValuesField;import org.apache.lucene.document.Field.Index;import org.apache.lucene.document.Field.Store;import org.apache.lucene.index.CorruptIndexException;@@ -71,10 +71,7 @@ public class TestDocValuesIndexing extends LuceneTestCase {* TODO: Roadmap to land on trunk** - Add documentation for:- * - Source and ValuesEnum* - DocValues- * - ValuesField- * - Values* - Add @lucene.experimental to all necessary classes* - add test for unoptimized case with deletes* - run RAT@@ -106,7 +103,7 @@ public class TestDocValuesIndexing extends LuceneTestCase {IndexWriter writer = new IndexWriter(dir, writerConfig(false));for (int i = 0; i < 5; i++) {Document doc = new Document();- ValuesField valuesField = new ValuesField("docId");+ DocValuesField valuesField = new DocValuesField("docId");valuesField.setInt(i);doc.add(valuesField);doc.add(new Field("docId", "" + i, Store.NO, Index.ANALYZED));@@ -532,11 +529,11 @@ public class TestDocValuesIndexing extends LuceneTestCase {OpenBitSet deleted = new OpenBitSet(numValues);Document doc = new Document();Index idx = IDX_VALUES[random.nextInt(IDX_VALUES.length)];- AbstractField field = random.nextBoolean() ? new ValuesField(value.name())+ AbstractField field = random.nextBoolean() ? new DocValuesField(value.name()): newField(value.name(), _TestUtil.randomRealisticUnicodeString(random,10), idx == Index.NO ? Store.YES : Store.NO, idx);doc.add(field);- ValuesField valField = new ValuesField("prototype");+ DocValuesField valField = new DocValuesField("prototype");final BytesRef bytesRef = new BytesRef();final String idBase = value.name() + "_";@@ -564,7 +561,9 @@ public class TestDocValuesIndexing extends LuceneTestCase {for (int j = 0; j < b.length; j++) {b[j] = upto++;}- valField.setBytes(bytesRef, value);+ if (bytesRef != null) {+ valField.setBytes(bytesRef, value);+ }}doc.removeFields("id");doc.add(new Field("id", idBase + i, Store.YES,