Apache Solr open-source search software
LUCENE-3606: SimpleText norms
Robert Muirb8235a5bb2c1
diff --git a/lucene/src/java/org/apache/lucene/index/SegmentCoreReaders.java b/lucene/src/java/org/apache/lucene/index/SegmentCoreReaders.javaindex 9e89d47b2ac..57769522e42 100644--- a/lucene/src/java/org/apache/lucene/index/SegmentCoreReaders.java+++ b/lucene/src/java/org/apache/lucene/index/SegmentCoreReaders.java@@ -94,7 +94,9 @@ final class SegmentCoreReaders {// Ask codec for its Fieldsfields = format.fieldsProducer(segmentReadState);assert fields != null;- // ask codec for its Norms+ // ask codec for its Norms:+ // TODO: since we don't write any norms file if there are no norms,+ // kinda jaky to assume the codec handles the case of no norms file at all gracefully?!norms = codec.normsFormat().normsReader(cfsDir, si, fieldInfos, context, dir);perDocProducer = codec.docValuesFormat().docsProducer(segmentReadState);success = true;diff --git a/lucene/src/java/org/apache/lucene/index/codecs/simpletext/SimpleTextCodec.java b/lucene/src/java/org/apache/lucene/index/codecs/simpletext/SimpleTextCodec.javaindex f2d2cbfa2ad..15136e63720 100644--- a/lucene/src/java/org/apache/lucene/index/codecs/simpletext/SimpleTextCodec.java+++ b/lucene/src/java/org/apache/lucene/index/codecs/simpletext/SimpleTextCodec.java@@ -26,7 +26,6 @@ import org.apache.lucene.index.codecs.SegmentInfosFormat;import org.apache.lucene.index.codecs.StoredFieldsFormat;import org.apache.lucene.index.codecs.TermVectorsFormat;import org.apache.lucene.index.codecs.lucene40.Lucene40DocValuesFormat;-import org.apache.lucene.index.codecs.lucene40.Lucene40NormsFormat;/*** plain text index format.@@ -43,7 +42,7 @@ public final class SimpleTextCodec extends Codec {// TODO: need a plain-text implprivate final DocValuesFormat docValues = new Lucene40DocValuesFormat();// TODO: need a plain-text impl (using the above)- private final NormsFormat normsFormat = new Lucene40NormsFormat();+ private final NormsFormat normsFormat = new SimpleTextNormsFormat();public SimpleTextCodec() {super("SimpleText");diff --git a/lucene/src/java/org/apache/lucene/index/codecs/simpletext/SimpleTextNormsFormat.java b/lucene/src/java/org/apache/lucene/index/codecs/simpletext/SimpleTextNormsFormat.javanew file mode 100644index 00000000000..acf0cb3b24b--- /dev/null+++ b/lucene/src/java/org/apache/lucene/index/codecs/simpletext/SimpleTextNormsFormat.java@@ -0,0 +1,54 @@+package org.apache.lucene.index.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.Set;++import org.apache.lucene.index.FieldInfos;+import org.apache.lucene.index.SegmentInfo;+import org.apache.lucene.index.SegmentWriteState;+import org.apache.lucene.index.codecs.NormsFormat;+import org.apache.lucene.index.codecs.NormsReader;+import org.apache.lucene.index.codecs.NormsWriter;+import org.apache.lucene.store.Directory;+import org.apache.lucene.store.IOContext;++/**+ * plain-text norms format+ * <p>+ * <b><font color="red">FOR RECREATIONAL USE ONLY</font></B>+ * @lucene.experimental+ */+public class SimpleTextNormsFormat extends NormsFormat {++ @Override+ public NormsReader normsReader(Directory dir, SegmentInfo info, FieldInfos fields, IOContext context, Directory separateNormsDir) throws IOException {+ return new SimpleTextNormsReader(dir, info, fields, context);+ }++ @Override+ public NormsWriter normsWriter(SegmentWriteState state) throws IOException {+ return new SimpleTextNormsWriter(state.directory, state.segmentName, state.context);+ }++ @Override+ public void files(Directory dir, SegmentInfo info, Set<String> files) throws IOException {+ SimpleTextNormsReader.files(dir, info, files);+ }+}diff --git a/lucene/src/java/org/apache/lucene/index/codecs/simpletext/SimpleTextNormsReader.java b/lucene/src/java/org/apache/lucene/index/codecs/simpletext/SimpleTextNormsReader.javanew file mode 100644index 00000000000..db00b104688--- /dev/null+++ b/lucene/src/java/org/apache/lucene/index/codecs/simpletext/SimpleTextNormsReader.java@@ -0,0 +1,106 @@+package org.apache.lucene.index.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.HashMap;+import java.util.Map;+import java.util.Set;++import org.apache.lucene.index.FieldInfos;+import org.apache.lucene.index.IndexFileNames;+import org.apache.lucene.index.SegmentInfo;+import org.apache.lucene.index.codecs.NormsReader;+import org.apache.lucene.store.Directory;+import org.apache.lucene.store.IOContext;+import org.apache.lucene.store.IndexInput;+import org.apache.lucene.util.BytesRef;+import org.apache.lucene.util.IOUtils;+import org.apache.lucene.util.StringHelper;++import static org.apache.lucene.index.codecs.simpletext.SimpleTextNormsWriter.*;++/**+ * Reads plain-text norms+ * <p>+ * <b><font color="red">FOR RECREATIONAL USE ONLY</font></B>+ * @lucene.experimental+ */+public class SimpleTextNormsReader extends NormsReader {+ private Map<String,byte[]> norms = new HashMap<String,byte[]>();++ public SimpleTextNormsReader(Directory directory, SegmentInfo si, FieldInfos fields, IOContext context) throws IOException {+ if (fields.hasNorms()) {+ readNorms(directory.openInput(IndexFileNames.segmentFileName(si.name, "", NORMS_EXTENSION), context), si.docCount);+ }+ }++ // we read in all the norms up front into a hashmap+ private void readNorms(IndexInput in, int maxDoc) throws IOException {+ BytesRef scratch = new BytesRef();+ boolean success = false;+ try {+ SimpleTextUtil.readLine(in, scratch);+ while (!scratch.equals(END)) {+ assert StringHelper.startsWith(scratch, FIELD);+ String fieldName = readString(FIELD.length, scratch);+ byte bytes[] = new byte[maxDoc];+ for (int i = 0; i < bytes.length; i++) {+ SimpleTextUtil.readLine(in, scratch);+ assert StringHelper.startsWith(scratch, DOC);+ SimpleTextUtil.readLine(in, scratch);+ assert StringHelper.startsWith(scratch, NORM);+ bytes[i] = scratch.bytes[scratch.offset + NORM.length];+ }+ norms.put(fieldName, bytes);+ SimpleTextUtil.readLine(in, scratch);+ assert StringHelper.startsWith(scratch, FIELD) || scratch.equals(END);+ }+ success = true;+ } finally {+ if (success) {+ IOUtils.close(in);+ } else {+ IOUtils.closeWhileHandlingException(in);+ }+ }+ }++ @Override+ public byte[] norms(String name) throws IOException {+ return norms.get(name);+ }++ @Override+ public void close() throws IOException {+ norms = null;+ }++ static void files(Directory dir, SegmentInfo info, Set<String> files) throws IOException {+ // TODO: This is what SI always did... but we can do this cleaner?+ // like first FI that has norms but doesn't have separate norms?+ final String normsFileName = IndexFileNames.segmentFileName(info.name, "", SimpleTextNormsWriter.NORMS_EXTENSION);+ if (dir.fileExists(normsFileName)) {+ files.add(normsFileName);+ }+ }++ private String readString(int offset, BytesRef scratch) {+ return new String(scratch.bytes, scratch.offset+offset, scratch.length-offset, IOUtils.CHARSET_UTF_8);+ }+}diff --git a/lucene/src/java/org/apache/lucene/index/codecs/simpletext/SimpleTextNormsWriter.java b/lucene/src/java/org/apache/lucene/index/codecs/simpletext/SimpleTextNormsWriter.javanew file mode 100644index 00000000000..842357a7638--- /dev/null+++ b/lucene/src/java/org/apache/lucene/index/codecs/simpletext/SimpleTextNormsWriter.java@@ -0,0 +1,114 @@+package org.apache.lucene.index.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 org.apache.lucene.index.FieldInfo;+import org.apache.lucene.index.IndexFileNames;+import org.apache.lucene.index.codecs.NormsWriter;+import org.apache.lucene.store.Directory;+import org.apache.lucene.store.IOContext;+import org.apache.lucene.store.IndexOutput;+import org.apache.lucene.util.BytesRef;+import org.apache.lucene.util.IOUtils;++/**+ * Writes plain-text norms+ * <p>+ * <b><font color="red">FOR RECREATIONAL USE ONLY</font></B>+ * @lucene.experimental+ */+public class SimpleTextNormsWriter extends NormsWriter {+ private IndexOutput out;+ private int docid = 0;++ /** Extension of norms file */+ static final String NORMS_EXTENSION = "len";++ private final BytesRef scratch = new BytesRef();++ final static BytesRef END = new BytesRef("END");+ final static BytesRef FIELD = new BytesRef("field ");+ final static BytesRef DOC = new BytesRef(" doc ");+ final static BytesRef NORM = new BytesRef(" norm ");++ public SimpleTextNormsWriter(Directory directory, String segment, IOContext context) throws IOException {+ final String normsFileName = IndexFileNames.segmentFileName(segment, "", NORMS_EXTENSION);+ out = directory.createOutput(normsFileName, context);+ }++ @Override+ public void startField(FieldInfo info) throws IOException {+ assert info.omitNorms == false;+ docid = 0;+ write(FIELD);+ write(info.name);+ newLine();+ }++ @Override+ public void writeNorm(byte norm) throws IOException {+ write(DOC);+ write(Integer.toString(docid));+ newLine();++ write(NORM);+ write(norm);+ newLine();+ docid++;+ }++ @Override+ public void finish(int numDocs) throws IOException {+ if (docid != numDocs) {+ throw new RuntimeException("mergeNorms produced an invalid result: docCount is " + numDocs+ + " but only saw " + docid + " file=" + out.toString() + "; now aborting this merge to prevent index corruption");+ }+ write(END);+ newLine();+ }++ @Override+ public void close() throws IOException {+ try {+ IOUtils.close(out);+ } finally {+ out = null;+ }+ }++ private void write(String s) throws IOException {+ SimpleTextUtil.write(out, s, scratch);+ }++ private void write(BytesRef bytes) throws IOException {+ SimpleTextUtil.write(out, bytes);+ }++ private void write(byte b) throws IOException {+ scratch.grow(1);+ scratch.bytes[scratch.offset] = b;+ scratch.length = 1;+ SimpleTextUtil.write(out, scratch);+ }++ private void newLine() throws IOException {+ SimpleTextUtil.writeNewline(out);+ }+}diff --git a/lucene/src/test/org/apache/lucene/index/TestOmitNorms.java b/lucene/src/test/org/apache/lucene/index/TestOmitNorms.javaindex 03ae5d98b38..112cc7f1a89 100644--- a/lucene/src/test/org/apache/lucene/index/TestOmitNorms.java+++ b/lucene/src/test/org/apache/lucene/index/TestOmitNorms.java@@ -180,7 +180,8 @@ public class TestOmitNorms extends LuceneTestCase {private void assertNoNrm(Directory dir) throws Throwable {final String[] files = dir.listAll();for (int i = 0; i < files.length; i++) {- assertFalse(files[i].endsWith(".nrm"));+ // TODO: this relies upon filenames+ assertFalse(files[i].endsWith(".nrm") || files[i].endsWith(".len"));}}