Apache Lucene open-source search software
Merged /lucene/dev/trunk:r1430124-1432061
Robert Muirf7c90c50f346
diff --cc lucene/core/src/java/org/apache/lucene/index/StoredFieldsProcessor.javaindex b0d7cf7b8ac,00000000000..eea05b13200mode 100644,000000..100644--- a/lucene/core/src/java/org/apache/lucene/index/StoredFieldsProcessor.java+++ b/lucene/core/src/java/org/apache/lucene/index/StoredFieldsProcessor.java@@@ -1,154 -1,0 +1,156 @@@+package org.apache.lucene.index;++/*+ * 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.codecs.Codec;+import org.apache.lucene.codecs.StoredFieldsWriter;+import org.apache.lucene.store.IOContext;+import org.apache.lucene.util.ArrayUtil;+import org.apache.lucene.util.RamUsageEstimator;++/** This is a StoredFieldsConsumer that writes stored fields. */+final class StoredFieldsProcessor extends StoredFieldsConsumer {++ StoredFieldsWriter fieldsWriter;+ final DocumentsWriterPerThread docWriter;+ int lastDocID;++ int freeCount;++ final DocumentsWriterPerThread.DocState docState;+ final Codec codec;++ public StoredFieldsProcessor(DocumentsWriterPerThread docWriter) {+ this.docWriter = docWriter;+ this.docState = docWriter.docState;+ this.codec = docWriter.codec;+ }++ private int numStoredFields;+ private StorableField[] storedFields;+ private FieldInfo[] fieldInfos;++ public void reset() {+ numStoredFields = 0;+ storedFields = new StorableField[1];+ fieldInfos = new FieldInfo[1];+ }++ @Override+ public void startDocument() {+ reset();+ }++ @Override+ public void flush(SegmentWriteState state) throws IOException {+ int numDocs = state.segmentInfo.getDocCount();++ if (numDocs > 0) {+ // It's possible that all documents seen in this segment+ // hit non-aborting exceptions, in which case we will+ // not have yet init'd the FieldsWriter:+ initFieldsWriter(state.context);+ fill(numDocs);+ }++ if (fieldsWriter != null) {+ try {+ fieldsWriter.finish(state.fieldInfos, numDocs);+ } finally {+ fieldsWriter.close();+ fieldsWriter = null;+ lastDocID = 0;+ }+ }+ }++ private synchronized void initFieldsWriter(IOContext context) throws IOException {+ if (fieldsWriter == null) {+ fieldsWriter = codec.storedFieldsFormat().fieldsWriter(docWriter.directory, docWriter.getSegmentInfo(), context);+ lastDocID = 0;+ }+ }++ int allocCount;++ @Override+ void abort() {+ reset();++ if (fieldsWriter != null) {+ fieldsWriter.abort();+ fieldsWriter = null;+ lastDocID = 0;+ }+ }++ /** Fills in any hole in the docIDs */+ void fill(int docID) throws IOException {+ // We must "catch up" for all docs before us+ // that had no stored fields:+ while(lastDocID < docID) {+ fieldsWriter.startDocument(0);+ lastDocID++;++ fieldsWriter.finishDocument();+ }+ }++ @Override+ void finishDocument() throws IOException {+ assert docWriter.writer.testPoint("StoredFieldsWriter.finishDocument start");++ initFieldsWriter(IOContext.DEFAULT);+ fill(docState.docID);++ if (fieldsWriter != null && numStoredFields > 0) {+ fieldsWriter.startDocument(numStoredFields);+ for (int i = 0; i < numStoredFields; i++) {+ fieldsWriter.writeField(fieldInfos[i], storedFields[i]);+ }++ fieldsWriter.finishDocument();+ lastDocID++;+ }++ reset();+ assert docWriter.writer.testPoint("StoredFieldsWriter.finishDocument end");+ }++ @Override+ public void addField(int docID, StorableField field, FieldInfo fieldInfo) {+ if (field.fieldType().stored()) {+ if (numStoredFields == storedFields.length) {+ int newSize = ArrayUtil.oversize(numStoredFields + 1, RamUsageEstimator.NUM_BYTES_OBJECT_REF);+ StorableField[] newArray = new StorableField[newSize];+ System.arraycopy(storedFields, 0, newArray, 0, numStoredFields);+ storedFields = newArray;++ FieldInfo[] newInfoArray = new FieldInfo[newSize];+ System.arraycopy(fieldInfos, 0, newInfoArray, 0, numStoredFields);+ fieldInfos = newInfoArray;+ }++ storedFields[numStoredFields] = field;+ fieldInfos[numStoredFields] = fieldInfo;+ numStoredFields++;++ assert docState.testPoint("StoredFieldsWriterPerThread.processFields.writeField");+ }+ }+}diff --cc lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.javaindex 59e2d670d0c,15204547cbe..18629bc22c7--- a/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java+++ b/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java@@@ -875,4 -831,103 +877,102 @@@ public class TestPackedInts extends Lucin.close();dir.close();}-+ public void testBlockPackedReaderWriter() throws IOException {+ final int iters = atLeast(2);+ for (int iter = 0; iter < iters; ++iter) {+ final int blockSize = 64 * _TestUtil.nextInt(random(), 1, 1 << 12);+ final int valueCount = random().nextInt(1 << 18);+ final long[] values = new long[valueCount];+ long minValue = 0;+ int bpv = 0;+ for (int i = 0; i < valueCount; ++i) {+ if (i % blockSize == 0) {+ minValue = rarely() ? random().nextInt(256) : rarely() ? -5 : random().nextLong();+ bpv = random().nextInt(65);+ }+ if (bpv == 0) {+ values[i] = minValue;+ } else if (bpv == 64) {+ values[i] = random().nextLong();+ } else {+ values[i] = minValue + _TestUtil.nextLong(random(), 0, (1L << bpv) - 1);+ }+ }++ final Directory dir = newDirectory();+ final IndexOutput out = dir.createOutput("out.bin", IOContext.DEFAULT);+ final BlockPackedWriter writer = new BlockPackedWriter(out, blockSize);+ for (int i = 0; i < valueCount; ++i) {+ assertEquals(i, writer.ord());+ writer.add(values[i]);+ }+ assertEquals(valueCount, writer.ord());+ writer.finish();+ assertEquals(valueCount, writer.ord());+ final long fp = out.getFilePointer();+ out.close();++ DataInput in = dir.openInput("out.bin", IOContext.DEFAULT);+ if (random().nextBoolean()) {+ byte[] buf = new byte[(int) fp];+ in.readBytes(buf, 0, (int) fp);+ ((IndexInput) in).close();+ in = new ByteArrayDataInput(buf);+ }+ final BlockPackedReader reader = new BlockPackedReader(in, PackedInts.VERSION_CURRENT, blockSize, valueCount);+ for (int i = 0; i < valueCount; ) {+ if (random().nextBoolean()) {+ assertEquals("" + i, values[i], reader.next());+ ++i;+ } else {+ final LongsRef nextValues = reader.next(_TestUtil.nextInt(random(), 1, 1024));+ for (int j = 0; j < nextValues.length; ++j) {+ assertEquals("" + (i + j), values[i + j], nextValues.longs[nextValues.offset + j]);+ }+ i += nextValues.length;+ }+ assertEquals(i, reader.ord());+ }+ assertEquals(fp, in instanceof ByteArrayDataInput ? ((ByteArrayDataInput) in).getPosition() : ((IndexInput) in).getFilePointer());+ try {+ reader.next();+ assertTrue(false);+ } catch (IOException e) {+ // OK+ }++ if (in instanceof ByteArrayDataInput) {+ ((ByteArrayDataInput) in).setPosition(0);+ } else {+ ((IndexInput) in).seek(0L);+ }+ final BlockPackedReader reader2 = new BlockPackedReader(in, PackedInts.VERSION_CURRENT, blockSize, valueCount);+ int i = 0;+ while (true) {+ final int skip = _TestUtil.nextInt(random(), 0, valueCount - i);+ reader2.skip(skip);+ i += skip;+ assertEquals(i, reader2.ord());+ if (i == valueCount) {+ break;+ } else {+ assertEquals(values[i], reader2.next());+ ++i;+ }+ }+ assertEquals(fp, in instanceof ByteArrayDataInput ? ((ByteArrayDataInput) in).getPosition() : ((IndexInput) in).getFilePointer());+ try {+ reader2.skip(1);+ assertTrue(false);+ } catch (IOException e) {+ // OK+ }++ if (in instanceof IndexInput) {+ ((IndexInput) in).close();+ }+ dir.close();+ }+ }+}