Apache Solr open-source search software
shadow SimpleNorms w/ SimpleText impl (only writing...)
Michael McCandless963979d0b543
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextCodec.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextCodec.javaindex 5aefe72c76d..3f1b901ea5c 100644--- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextCodec.java+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextCodec.java@@ -25,6 +25,7 @@ import org.apache.lucene.codecs.NormsFormat;import org.apache.lucene.codecs.PostingsFormat;import org.apache.lucene.codecs.SegmentInfoFormat;import org.apache.lucene.codecs.SimpleDocValuesFormat;+import org.apache.lucene.codecs.SimpleNormsFormat;import org.apache.lucene.codecs.StoredFieldsFormat;import org.apache.lucene.codecs.TermVectorsFormat;@@ -44,6 +45,7 @@ public final class SimpleTextCodec extends Codec {private final DocValuesFormat docValues = new SimpleTextDocValuesFormat();// TODO: need a plain-text impl (using the above)private final NormsFormat normsFormat = new SimpleTextNormsFormat();+ private final SimpleNormsFormat simpleNormsFormat = new SimpleTextSimpleNormsFormat();private final LiveDocsFormat liveDocs = new SimpleTextLiveDocsFormat();// nocommit rename@@ -87,6 +89,11 @@ public final class SimpleTextCodec extends Codec {public NormsFormat normsFormat() {return normsFormat;}++ @Override+ public SimpleNormsFormat simpleNormsFormat() {+ return simpleNormsFormat;+ }@Overridepublic LiveDocsFormat liveDocsFormat() {diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextSimpleDocValuesFormat.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextSimpleDocValuesFormat.javaindex 346ad7a66ed..c1c4dc598df 100644--- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextSimpleDocValuesFormat.java+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextSimpleDocValuesFormat.java@@ -77,12 +77,12 @@ public class SimpleTextSimpleDocValuesFormat extends SimpleDocValuesFormat {@Overridepublic SimpleDVConsumer fieldsConsumer(SegmentWriteState state) throws IOException {- return new SimpleTextDocValuesWriter(state);+ return new SimpleTextDocValuesWriter(state, "dat");}@Overridepublic SimpleDVProducer fieldsProducer(SegmentReadState state) throws IOException {- return new SimpleTextDocValuesReader(state);+ return new SimpleTextDocValuesReader(state, "dat");}/** the .dat file contains the data.@@ -146,8 +146,8 @@ public class SimpleTextSimpleDocValuesFormat extends SimpleDocValuesFormat {final int numDocs;private final Set<String> fieldsSeen = new HashSet<String>(); // for asserting- SimpleTextDocValuesWriter(SegmentWriteState state) throws IOException {- data = state.directory.createOutput(IndexFileNames.segmentFileName(state.segmentInfo.name, state.segmentSuffix, "dat"), state.context);+ SimpleTextDocValuesWriter(SegmentWriteState state, String ext) throws IOException {+ data = state.directory.createOutput(IndexFileNames.segmentFileName(state.segmentInfo.name, state.segmentSuffix, ext), state.context);numDocs = state.segmentInfo.getDocCount();}@@ -400,9 +400,9 @@ public class SimpleTextSimpleDocValuesFormat extends SimpleDocValuesFormat {final BytesRef scratch = new BytesRef();final Map<String,OneField> fields = new HashMap<String,OneField>();- SimpleTextDocValuesReader(SegmentReadState state) throws IOException {+ SimpleTextDocValuesReader(SegmentReadState state, String ext) throws IOException {//System.out.println("dir=" + dir + " seg=" + si.name);- data = state.directory.openInput(IndexFileNames.segmentFileName(state.segmentInfo.name, state.segmentSuffix, "dat"), state.context);+ data = state.directory.openInput(IndexFileNames.segmentFileName(state.segmentInfo.name, state.segmentSuffix, ext), state.context);maxDoc = state.segmentInfo.getDocCount();while(true) {readLine();diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextSimpleNormsFormat.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextSimpleNormsFormat.javanew file mode 100644index 00000000000..68cf92d5272--- /dev/null+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextSimpleNormsFormat.java@@ -0,0 +1,85 @@+package org.apache.lucene.codecs.simpletext;++/*+ * 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.Comparator;++import org.apache.lucene.codecs.SimpleDVConsumer;+import org.apache.lucene.codecs.SimpleDVProducer;+import org.apache.lucene.codecs.SimpleNormsFormat;+import org.apache.lucene.codecs.simpletext.SimpleTextSimpleDocValuesFormat.SimpleTextDocValuesReader;+import org.apache.lucene.codecs.simpletext.SimpleTextSimpleDocValuesFormat.SimpleTextDocValuesWriter;+import org.apache.lucene.index.AtomicReader;+import org.apache.lucene.index.FieldInfo;+import org.apache.lucene.index.FieldInfos;+import org.apache.lucene.index.SegmentReadState;+import org.apache.lucene.index.SegmentWriteState;+import org.apache.lucene.util.BytesRef;++/**+ * plain-text norms format.+ * <p>+ * <b><font color="red">FOR RECREATIONAL USE ONLY</font></B>+ *+ * @lucene.experimental+ */+public class SimpleTextSimpleNormsFormat extends SimpleNormsFormat {+ // nocommit put back to len once we replace current norms format:+ private static final String NORMS_SEG_SUFFIX = "slen";++ @Override+ public SimpleDVConsumer normsConsumer(SegmentWriteState state) throws IOException {+ return new SimpleTextSimpleNormsConsumer(state);+ }++ @Override+ public SimpleDVProducer normsProducer(SegmentReadState state) throws IOException {+ return new SimpleTextSimpleNormsProducer(state);+ }++ /**+ * Reads plain-text norms.+ * <p>+ * <b><font color="red">FOR RECREATIONAL USE ONLY</font></B>+ *+ * @lucene.experimental+ */+ public static class SimpleTextSimpleNormsProducer extends SimpleTextDocValuesReader {+ public SimpleTextSimpleNormsProducer(SegmentReadState state) throws IOException {+ // All we do is change the suffix from .dat -> .len;+ // otherwise this is a normal simple doc values file:+ super(state, NORMS_SEG_SUFFIX);+ }+ }++ /**+ * Writes plain-text norms.+ * <p>+ * <b><font color="red">FOR RECREATIONAL USE ONLY</font></B>+ *+ * @lucene.experimental+ */+ public static class SimpleTextSimpleNormsConsumer extends SimpleTextDocValuesWriter {+ public SimpleTextSimpleNormsConsumer(SegmentWriteState state) throws IOException {+ // All we do is change the suffix from .dat -> .len;+ // otherwise this is a normal simple doc values file:+ super(state, NORMS_SEG_SUFFIX);+ }+ }+}diff --git a/lucene/core/src/java/org/apache/lucene/codecs/Codec.java b/lucene/core/src/java/org/apache/lucene/codecs/Codec.javaindex 46b8b2901e7..67c932135db 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/Codec.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/Codec.java@@ -89,6 +89,11 @@ public abstract class Codec implements NamedSPILoader.NamedSPI {/** Encodes/decodes document normalization values */public abstract NormsFormat normsFormat();+ public SimpleNormsFormat simpleNormsFormat() {+ // nocommit make this abstract+ return null;+ }+/** Encodes/decodes live docs */public abstract LiveDocsFormat liveDocsFormat();diff --git a/lucene/core/src/java/org/apache/lucene/codecs/SimpleNormsFormat.java b/lucene/core/src/java/org/apache/lucene/codecs/SimpleNormsFormat.javanew file mode 100644index 00000000000..1e09c6bb961--- /dev/null+++ b/lucene/core/src/java/org/apache/lucene/codecs/SimpleNormsFormat.java@@ -0,0 +1,41 @@+package org.apache.lucene.codecs;++/*+ * 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 org.apache.lucene.index.SegmentReadState;+import org.apache.lucene.index.SegmentWriteState;++/**+ * format for normalization factors+ */+public abstract class SimpleNormsFormat {+ /** Sole constructor. (For invocation by subclass+ * constructors, typically implicit.) */+ protected SimpleNormsFormat() {+ }++ /** Returns a {@link SimpleDVConsumer} to write norms to the+ * index. */+ public abstract SimpleDVConsumer normsConsumer(SegmentWriteState state) throws IOException;++ /** Returns a {@link SimpleDVProducer} to read norms from the+ * index. */+ public abstract SimpleDVProducer normsProducer(SegmentReadState state) throws IOException;+}diff --git a/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterPerThread.java b/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterPerThread.javaindex 792bcec6db9..8c7f49270de 100644--- a/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterPerThread.java+++ b/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterPerThread.java@@ -73,7 +73,7 @@ class DocumentsWriterPerThread {--> code: FreqProxTermsWriter / FreqProxTermsWriterPerField--> code: TermVectorsTermsWriter / TermVectorsTermsWriterPerField--> InvertedDocEndConsumer / InvertedDocConsumerPerField- --> code: NormsWriter / NormsWriterPerField+ --> code: NormsConsumer / NormsConsumerPerField--> StoredFieldsConsumer--> TwoStoredFieldConsumers-> code: StoredFieldsProcessordiff --git a/lucene/core/src/java/org/apache/lucene/index/NormsConsumer.java b/lucene/core/src/java/org/apache/lucene/index/NormsConsumer.javaindex 25f69320097..be037dc7d2c 100644--- a/lucene/core/src/java/org/apache/lucene/index/NormsConsumer.java+++ b/lucene/core/src/java/org/apache/lucene/index/NormsConsumer.java@@ -23,6 +23,8 @@ import java.util.Map;import org.apache.lucene.codecs.DocValuesConsumer;import org.apache.lucene.codecs.NormsFormat;import org.apache.lucene.codecs.PerDocConsumer;+import org.apache.lucene.codecs.SimpleDVConsumer;+import org.apache.lucene.codecs.SimpleNormsFormat;import org.apache.lucene.index.DocValues.Type;import org.apache.lucene.util.IOUtils;@@ -48,14 +50,20 @@ final class NormsConsumer extends InvertedDocEndConsumer {}}- /** Produce _X.nrm if any document had a field with norms- * not disabled */@Overridepublic void flush(Map<String,InvertedDocEndConsumerPerField> fieldsToFlush, SegmentWriteState state) throws IOException {boolean success = false;+ SimpleDVConsumer normsConsumer = null;boolean anythingFlushed = false;try {if (state.fieldInfos.hasNorms()) {+ SimpleNormsFormat normsFormat = state.segmentInfo.getCodec().simpleNormsFormat();++ // nocommit change this to assert normsFormat != null+ if (normsFormat != null) {+ normsConsumer = normsFormat.normsConsumer(state);+ }+for (FieldInfo fi : state.fieldInfos) {final NormsConsumerPerField toWrite = (NormsConsumerPerField) fieldsToFlush.get(fi.name);// we must check the final value of omitNorms for the fieldinfo, it could have@@ -63,7 +71,7 @@ final class NormsConsumer extends InvertedDocEndConsumer {if (!fi.omitsNorms()) {if (toWrite != null && toWrite.initialized()) {anythingFlushed = true;- final Type type = toWrite.flush(state.segmentInfo.getDocCount());+ final Type type = toWrite.flush(state, normsConsumer);assert fi.getNormType() == type;} else if (fi.isIndexed()) {anythingFlushed = true;@@ -71,6 +79,9 @@ final class NormsConsumer extends InvertedDocEndConsumer {}}}+ if (normsConsumer != null) {++ }}success = true;@@ -79,9 +90,9 @@ final class NormsConsumer extends InvertedDocEndConsumer {}} finally {if (success) {- IOUtils.close(consumer);+ IOUtils.close(consumer, normsConsumer);} else {- IOUtils.closeWhileHandlingException(consumer);+ IOUtils.closeWhileHandlingException(consumer, normsConsumer);}}}diff --git a/lucene/core/src/java/org/apache/lucene/index/NormsConsumerPerField.java b/lucene/core/src/java/org/apache/lucene/index/NormsConsumerPerField.javaindex a936c3722b5..9e19f8918d7 100644--- a/lucene/core/src/java/org/apache/lucene/index/NormsConsumerPerField.java+++ b/lucene/core/src/java/org/apache/lucene/index/NormsConsumerPerField.java@@ -18,6 +18,7 @@ package org.apache.lucene.index;import java.io.IOException;import org.apache.lucene.codecs.DocValuesConsumer;+import org.apache.lucene.codecs.SimpleDVConsumer;import org.apache.lucene.index.DocValues.Type;import org.apache.lucene.search.similarities.Similarity;@@ -30,6 +31,7 @@ final class NormsConsumerPerField extends InvertedDocEndConsumerPerField implemeprivate final Norm norm;private final NormsConsumer parent;private Type initType;+ private final NumberDVWriter simpleNormsWriter;public NormsConsumerPerField(final DocInverterPerField docInverterPerField, final FieldInfo fieldInfo, NormsConsumer parent) {this.fieldInfo = fieldInfo;@@ -38,6 +40,7 @@ final class NormsConsumerPerField extends InvertedDocEndConsumerPerField implemefieldState = docInverterPerField.fieldState;similarity = docState.similarity;norm = new Norm();+ simpleNormsWriter = new NumberDVWriter(fieldInfo, docState.docWriter.bytesUsed);}@Override@@ -56,14 +59,37 @@ final class NormsConsumerPerField extends InvertedDocEndConsumerPerField implemeDocValuesConsumer consumer = getConsumer(norm.type());consumer.add(docState.docID, field);}++ long norm = similarity.computeSimpleNorm(fieldState);+ if (norm != -1) {+ // nocommit is -1 really a safe "not set" value!?+ // nocommit shouldn't we require that it's either+ // all -1's or none? a sim can't not compute norms+ // for only some docs? hmm unless the field is+ // missing for this doc... but then finish() isn't+ // called?+ simpleNormsWriter.addValue(docState.docID, norm);+ }}}- Type flush(int docCount) throws IOException {+ Type flush(SegmentWriteState state, SimpleDVConsumer normsConsumer) throws IOException {+ int docCount = state.segmentInfo.getDocCount();if (!initialized()) {return null; // null type - not omitted but not written}consumer.finish(docCount);+ // nocommit change to assert normsConsumer != null+ if (normsConsumer != null) {+ // nocommit we need to change the suffix? ie so norms+ // don't step on dvs? hmmm.... where does this happen+ // today ...+ simpleNormsWriter.finish(docCount);+ simpleNormsWriter.flush(state, normsConsumer);+ } else {+ // nocommit remove:+ simpleNormsWriter.reset();+ }return initType;}diff --git a/lucene/core/src/java/org/apache/lucene/index/NumberDVWriter.java b/lucene/core/src/java/org/apache/lucene/index/NumberDVWriter.javaindex 6a9cde1854a..08be504189c 100644--- a/lucene/core/src/java/org/apache/lucene/index/NumberDVWriter.java+++ b/lucene/core/src/java/org/apache/lucene/index/NumberDVWriter.java@@ -29,7 +29,7 @@ import org.apache.lucene.util.packed.PackedInts;/** Buffers up pending long per doc, then flushes when* segment flushes. */-// nocommit name?+// nocommit rename to NumericDVWriter?// nocommit make this a consumer in the chain?class NumberDVWriter extends DocValuesWriter {@@ -116,9 +116,9 @@ class NumberDVWriter extends DocValuesWriter {reset();}- // nocommit do we really need this...? can't parent alloc+ // nocommit do we really need this...? can't/doesn't parent alloc// a new instance after flush?- private void reset() {+ void reset() {pending = new AppendingLongBuffer();updateBytesUsed();anyValues = false;diff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/Similarity.java b/lucene/core/src/java/org/apache/lucene/search/similarities/Similarity.javaindex 9aff3b9e412..bfa9053dec5 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/Similarity.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/Similarity.java@@ -23,6 +23,7 @@ import org.apache.lucene.document.ByteDocValuesField; // javadocimport org.apache.lucene.document.FloatDocValuesField; // javadocimport org.apache.lucene.index.AtomicReader; // javadocimport org.apache.lucene.index.AtomicReaderContext;+import org.apache.lucene.index.DocValues;import org.apache.lucene.index.FieldInvertState;import org.apache.lucene.index.Norm;import org.apache.lucene.search.BooleanQuery;@@ -163,7 +164,19 @@ public abstract class Similarity {* @param norm holds the computed norm value when this method returns*/public abstract void computeNorm(FieldInvertState state, Norm norm);-++ // nocommit replace computeNorm w/ this, make it abstract:+ public long computeSimpleNorm(FieldInvertState state) {+ Norm normScratch = new Norm();+ computeNorm(state, normScratch);+ if (normScratch.type() != null && DocValues.isNumber(normScratch.type())) {+ return normScratch.field().numericValue().longValue();+ } else {+ // nocommit hmm how to return "not set"...?+ return -1;+ }+ }+/*** Compute any collection-level weight (e.g. IDF, average document length, etc) needed for scoring a query.*diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.javaindex 81be3744b0f..3dc46c32a3c 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java@@ -1039,7 +1039,7 @@ public class TestIndexWriter extends LuceneTestCase {allowInterrupt = true;}} catch (ThreadInterruptedException re) {- if (true || VERBOSE) {+ if (VERBOSE) {System.out.println("TEST: got interrupt");re.printStackTrace(System.out);}