Apache Lucene open-source search software
Merged /lucene/dev/trunk:r1514641-1514711
Robert Muir7a4f7c669ba1
diff --cc lucene/core/src/java/org/apache/lucene/codecs/lucene42/Lucene42DocValuesConsumer.javaindex edd3bbd240a,dd11efaff82..0a2f92f22a8--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene42/Lucene42DocValuesConsumer.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene42/Lucene42DocValuesConsumer.java@@@ -219,12 -216,12 +219,15 @@@ class Lucene42DocValuesConsumer extendint maxLength = Integer.MIN_VALUE;final long startFP = data.getFilePointer();for(BytesRef v : values) {- if (v.length > Lucene42DocValuesFormat.MAX_BINARY_FIELD_LENGTH) {+ final int length = v == null ? 0 : v.length;++ if (length > Lucene42DocValuesFormat.MAX_BINARY_FIELD_LENGTH) {+ throw new IllegalArgumentException("DocValuesField \"" + field.name + "\" is too large, must be <= " + Lucene42DocValuesFormat.MAX_BINARY_FIELD_LENGTH);+ }- minLength = Math.min(minLength, v.length);- maxLength = Math.max(maxLength, v.length);- data.writeBytes(v.bytes, v.offset, v.length);+ minLength = Math.min(minLength, length);+ maxLength = Math.max(maxLength, length);+ if (v != null) {+ data.writeBytes(v.bytes, v.offset, v.length);+ }}meta.writeLong(startFP);meta.writeLong(data.getFilePointer() - startFP);diff --cc lucene/core/src/java/org/apache/lucene/index/BinaryDocValuesWriter.javaindex 553f9ff1ff7,5eab02d417c..3a3e301a9b2--- a/lucene/core/src/java/org/apache/lucene/index/BinaryDocValuesWriter.java+++ b/lucene/core/src/java/org/apache/lucene/index/BinaryDocValuesWriter.java@@@ -22,12 -22,11 +22,13 @@@ import java.util.Iteratorimport java.util.NoSuchElementException;import org.apache.lucene.codecs.DocValuesConsumer;- import org.apache.lucene.util.ByteBlockPool.DirectTrackingAllocator;- import org.apache.lucene.util.ByteBlockPool;+ import org.apache.lucene.store.DataInput;+ import org.apache.lucene.store.DataOutput;import org.apache.lucene.util.BytesRef;import org.apache.lucene.util.Counter;+import org.apache.lucene.util.OpenBitSet;+import org.apache.lucene.util.RamUsageEstimator;+ import org.apache.lucene.util.PagedBytes;import org.apache.lucene.util.packed.AppendingDeltaPackedLongBuffer;import org.apache.lucene.util.packed.PackedInts;@@@ -38,22 -34,31 +36,35 @@@* segment flushes. */class BinaryDocValuesWriter extends DocValuesWriter {- private final ByteBlockPool pool;+ /** Maximum length for a binary field; we set this to "a+ * bit" below Integer.MAX_VALUE because the exact max+ * allowed byte[] is JVM dependent, so we want to avoid+ * a case where a large value worked in one JVM but+ * failed later at search time with a different JVM. */+ private static final int MAX_LENGTH = Integer.MAX_VALUE-256;++ // 32 KB block sizes for PagedBytes storage:+ private final static int BLOCK_BITS = 15;++ private final PagedBytes bytes;+ private final DataOutput bytesOut;++ private final Counter iwBytesUsed;private final AppendingDeltaPackedLongBuffer lengths;+ private final OpenBitSet docsWithField;- private final Counter iwBytesUsed;- private long bytesUsed;private final FieldInfo fieldInfo;- private int addedValues = 0;+ private int addedValues;+ private long bytesUsed;public BinaryDocValuesWriter(FieldInfo fieldInfo, Counter iwBytesUsed) {this.fieldInfo = fieldInfo;- this.pool = new ByteBlockPool(new DirectTrackingAllocator(iwBytesUsed));+ this.bytes = new PagedBytes(BLOCK_BITS);+ this.bytesOut = bytes.getDataOutput();this.lengths = new AppendingDeltaPackedLongBuffer(PackedInts.COMPACT);this.iwBytesUsed = iwBytesUsed;+ this.docsWithField = new OpenBitSet();+ this.bytesUsed = docsWithFieldBytesUsed();+ iwBytesUsed.addAndGet(bytesUsed);}public void addValue(int docID, BytesRef value) {@@@ -74,18 -79,17 +85,23 @@@}addedValues++;lengths.add(value.length);- pool.append(value);+ try {+ bytesOut.writeBytes(value.bytes, value.offset, value.length);+ } catch (IOException ioe) {+ // Should never happen!+ throw new RuntimeException(ioe);+ }+ docsWithField.set(docID);updateBytesUsed();}++ private long docsWithFieldBytesUsed() {+ // nocommit: this is not correct+ return docsWithField.getBits().length*RamUsageEstimator.NUM_BYTES_LONG;+ }-+private void updateBytesUsed() {- final long newBytesUsed = docsWithFieldBytesUsed();- final long newBytesUsed = lengths.ramBytesUsed() + bytes.ramBytesUsed();++ final long newBytesUsed = lengths.ramBytesUsed() + bytes.ramBytesUsed() + docsWithFieldBytesUsed();iwBytesUsed.addAndGet(newBytesUsed - bytesUsed);bytesUsed = newBytesUsed;}@@@ -138,18 -142,19 +155,22 @@@int length = (int) lengthsIterator.next();value.grow(length);value.length = length;- pool.readBytes(byteOffset, value.bytes, value.offset, value.length);- byteOffset += length;+ try {+ bytesIterator.readBytes(value.bytes, value.offset, value.length);+ } catch (IOException ioe) {+ // Should never happen!+ throw new RuntimeException(ioe);+ }+ if (docsWithField.get(upto)) {+ v = value;+ } else {+ v = null;+ }} else {- // This is to handle last N documents not having- // this DV field in the end of the segment:- value.length = 0;+ v = null;}upto++;- return value;+ return v;}@Overridediff --cc lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40DocValuesWriter.javaindex 15b3081c848,c2c21d505f3..90c161cf696--- a/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40DocValuesWriter.java+++ b/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40DocValuesWriter.java@@@ -159,9 -156,9 +159,12 @@@ class Lucene40DocValuesWriter extends Dint minLength = Integer.MAX_VALUE;int maxLength = Integer.MIN_VALUE;for (BytesRef b : values) {+ if (b == null) {+ b = new BytesRef(); // 4.0 doesnt distinguish+ }+ if (b.length > Lucene40DocValuesFormat.MAX_BINARY_FIELD_LENGTH) {+ throw new IllegalArgumentException("DocValuesField \"" + field.name + "\" is too large, must be <= " + Lucene40DocValuesFormat.MAX_BINARY_FIELD_LENGTH);+ }minLength = Math.min(minLength, b.length);maxLength = Math.max(maxLength, b.length);if (uniqueValues != null) {diff --cc lucene/test-framework/src/java/org/apache/lucene/index/BaseDocValuesFormatTestCase.javaindex 7dd38ae1c69,d4f8866732c..008eeea7d32--- a/lucene/test-framework/src/java/org/apache/lucene/index/BaseDocValuesFormatTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/index/BaseDocValuesFormatTestCase.java@@@ -2200,205 -2198,173 +2201,373 @@@ public abstract class BaseDocValuesFormdoTestNumericsVsStoredFields(longs);}}++ public void testTwoNumbersOneMissing() throws IOException {+ assumeTrue("Codec does not support getDocsWithField", codecSupportsDocsWithField("dv1"));+ Directory directory = newDirectory();+ IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, null);+ conf.setMergePolicy(newLogMergePolicy());+ RandomIndexWriter iw = new RandomIndexWriter(random(), directory, conf);+ Document doc = new Document();+ doc.add(new StringField("id", "0", Field.Store.YES));+ doc.add(new NumericDocValuesField("dv1", 0));+ iw.addDocument(doc);+ doc = new Document();+ doc.add(new StringField("id", "1", Field.Store.YES));+ iw.addDocument(doc);+ iw.forceMerge(1);+ iw.close();++ IndexReader ir = DirectoryReader.open(directory);+ assertEquals(1, ir.leaves().size());+ AtomicReader ar = ir.leaves().get(0).reader();+ NumericDocValues dv = ar.getNumericDocValues("dv1");+ assertEquals(0, dv.get(0));+ assertEquals(0, dv.get(1));+ Bits docsWithField = ar.getDocsWithField("dv1");+ assertTrue(docsWithField.get(0));+ assertFalse(docsWithField.get(1));+ ir.close();+ directory.close();+ }++ public void testTwoNumbersOneMissingWithMerging() throws IOException {+ assumeTrue("Codec does not support getDocsWithField", codecSupportsDocsWithField("dv1"));+ Directory directory = newDirectory();+ IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, null);+ conf.setMergePolicy(newLogMergePolicy());+ RandomIndexWriter iw = new RandomIndexWriter(random(), directory, conf);+ Document doc = new Document();+ doc.add(new StringField("id", "0", Field.Store.YES));+ doc.add(new NumericDocValuesField("dv1", 0));+ iw.addDocument(doc);+ iw.commit();+ doc = new Document();+ doc.add(new StringField("id", "1", Field.Store.YES));+ iw.addDocument(doc);+ iw.forceMerge(1);+ iw.close();++ IndexReader ir = DirectoryReader.open(directory);+ assertEquals(1, ir.leaves().size());+ AtomicReader ar = ir.leaves().get(0).reader();+ NumericDocValues dv = ar.getNumericDocValues("dv1");+ assertEquals(0, dv.get(0));+ assertEquals(0, dv.get(1));+ Bits docsWithField = ar.getDocsWithField("dv1");+ assertTrue(docsWithField.get(0));+ assertFalse(docsWithField.get(1));+ ir.close();+ directory.close();+ }++ public void testThreeNumbersOneMissingWithMerging() throws IOException {+ assumeTrue("Codec does not support getDocsWithField", codecSupportsDocsWithField("dv1"));+ Directory directory = newDirectory();+ IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, null);+ conf.setMergePolicy(newLogMergePolicy());+ RandomIndexWriter iw = new RandomIndexWriter(random(), directory, conf);+ Document doc = new Document();+ doc.add(new StringField("id", "0", Field.Store.YES));+ doc.add(new NumericDocValuesField("dv1", 0));+ iw.addDocument(doc);+ doc = new Document();+ doc.add(new StringField("id", "1", Field.Store.YES));+ iw.addDocument(doc);+ iw.commit();+ doc = new Document();+ doc.add(new StringField("id", "2", Field.Store.YES));+ doc.add(new NumericDocValuesField("dv1", 5));+ iw.addDocument(doc);+ iw.forceMerge(1);+ iw.close();++ IndexReader ir = DirectoryReader.open(directory);+ assertEquals(1, ir.leaves().size());+ AtomicReader ar = ir.leaves().get(0).reader();+ NumericDocValues dv = ar.getNumericDocValues("dv1");+ assertEquals(0, dv.get(0));+ assertEquals(0, dv.get(1));+ assertEquals(5, dv.get(2));+ Bits docsWithField = ar.getDocsWithField("dv1");+ assertTrue(docsWithField.get(0));+ assertFalse(docsWithField.get(1));+ assertTrue(docsWithField.get(2));+ ir.close();+ directory.close();+ }++ public void testTwoBytesOneMissing() throws IOException {+ assumeTrue("Codec does not support getDocsWithField", codecSupportsDocsWithField("dv1"));+ Directory directory = newDirectory();+ IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, null);+ conf.setMergePolicy(newLogMergePolicy());+ RandomIndexWriter iw = new RandomIndexWriter(random(), directory, conf);+ Document doc = new Document();+ doc.add(new StringField("id", "0", Field.Store.YES));+ doc.add(new BinaryDocValuesField("dv1", new BytesRef()));+ iw.addDocument(doc);+ doc = new Document();+ doc.add(new StringField("id", "1", Field.Store.YES));+ iw.addDocument(doc);+ iw.forceMerge(1);+ iw.close();++ IndexReader ir = DirectoryReader.open(directory);+ assertEquals(1, ir.leaves().size());+ AtomicReader ar = ir.leaves().get(0).reader();+ BinaryDocValues dv = ar.getBinaryDocValues("dv1");+ BytesRef ref = new BytesRef();+ dv.get(0, ref);+ assertEquals(new BytesRef(), ref);+ dv.get(1, ref);+ assertEquals(new BytesRef(), ref);+ Bits docsWithField = ar.getDocsWithField("dv1");+ assertTrue(docsWithField.get(0));+ assertFalse(docsWithField.get(1));+ ir.close();+ directory.close();+ }++ public void testTwoBytesOneMissingWithMerging() throws IOException {+ assumeTrue("Codec does not support getDocsWithField", codecSupportsDocsWithField("dv1"));+ Directory directory = newDirectory();+ IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, null);+ conf.setMergePolicy(newLogMergePolicy());+ RandomIndexWriter iw = new RandomIndexWriter(random(), directory, conf);+ Document doc = new Document();+ doc.add(new StringField("id", "0", Field.Store.YES));+ doc.add(new BinaryDocValuesField("dv1", new BytesRef()));+ iw.addDocument(doc);+ iw.commit();+ doc = new Document();+ doc.add(new StringField("id", "1", Field.Store.YES));+ iw.addDocument(doc);+ iw.forceMerge(1);+ iw.close();++ IndexReader ir = DirectoryReader.open(directory);+ assertEquals(1, ir.leaves().size());+ AtomicReader ar = ir.leaves().get(0).reader();+ BinaryDocValues dv = ar.getBinaryDocValues("dv1");+ BytesRef ref = new BytesRef();+ dv.get(0, ref);+ assertEquals(new BytesRef(), ref);+ dv.get(1, ref);+ assertEquals(new BytesRef(), ref);+ Bits docsWithField = ar.getDocsWithField("dv1");+ assertTrue(docsWithField.get(0));+ assertFalse(docsWithField.get(1));+ ir.close();+ directory.close();+ }++ public void testThreeBytesOneMissingWithMerging() throws IOException {+ assumeTrue("Codec does not support getDocsWithField", codecSupportsDocsWithField("dv1"));+ Directory directory = newDirectory();+ IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, null);+ conf.setMergePolicy(newLogMergePolicy());+ RandomIndexWriter iw = new RandomIndexWriter(random(), directory, conf);+ Document doc = new Document();+ doc.add(new StringField("id", "0", Field.Store.YES));+ doc.add(new BinaryDocValuesField("dv1", new BytesRef()));+ iw.addDocument(doc);+ doc = new Document();+ doc.add(new StringField("id", "1", Field.Store.YES));+ iw.addDocument(doc);+ iw.commit();+ doc = new Document();+ doc.add(new StringField("id", "2", Field.Store.YES));+ doc.add(new BinaryDocValuesField("dv1", new BytesRef("boo")));+ iw.addDocument(doc);+ iw.forceMerge(1);+ iw.close();++ IndexReader ir = DirectoryReader.open(directory);+ assertEquals(1, ir.leaves().size());+ AtomicReader ar = ir.leaves().get(0).reader();+ BinaryDocValues dv = ar.getBinaryDocValues("dv1");+ BytesRef ref = new BytesRef();+ dv.get(0, ref);+ assertEquals(new BytesRef(), ref);+ dv.get(1, ref);+ assertEquals(new BytesRef(), ref);+ dv.get(2, ref);+ assertEquals(new BytesRef("boo"), ref);+ Bits docsWithField = ar.getDocsWithField("dv1");+ assertTrue(docsWithField.get(0));+ assertFalse(docsWithField.get(1));+ assertTrue(docsWithField.get(2));+ ir.close();+ directory.close();+ }+ // LUCENE-4853+ public void testHugeBinaryValues() throws Exception {+ Analyzer analyzer = new MockAnalyzer(random());+ // FSDirectory because SimpleText will consume gobbs of+ // space when storing big binary values:+ Directory d = newFSDirectory(_TestUtil.getTempDir("hugeBinaryValues"));+ boolean doFixed = random().nextBoolean();+ int numDocs;+ int fixedLength = 0;+ if (doFixed) {+ // Sometimes make all values fixed length since some+ // codecs have different code paths for this:+ numDocs = _TestUtil.nextInt(random(), 10, 20);+ fixedLength = _TestUtil.nextInt(random(), 65537, 256*1024);+ } else {+ numDocs = _TestUtil.nextInt(random(), 100, 200);+ }+ IndexWriter w = new IndexWriter(d, newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer));+ List<byte[]> docBytes = new ArrayList<byte[]>();+ long totalBytes = 0;+ for(int docID=0;docID<numDocs;docID++) {+ // we don't use RandomIndexWriter because it might add+ // more docvalues than we expect !!!!++ // Must be > 64KB in size to ensure more than 2 pages in+ // PagedBytes would be needed:+ int numBytes;+ if (doFixed) {+ numBytes = fixedLength;+ } else if (docID == 0 || random().nextInt(5) == 3) {+ numBytes = _TestUtil.nextInt(random(), 65537, 3*1024*1024);+ } else {+ numBytes = _TestUtil.nextInt(random(), 1, 1024*1024);+ }+ totalBytes += numBytes;+ if (totalBytes > 5 * 1024*1024) {+ break;+ }+ byte[] bytes = new byte[numBytes];+ random().nextBytes(bytes);+ docBytes.add(bytes);+ Document doc = new Document();+ BytesRef b = new BytesRef(bytes);+ b.length = bytes.length;+ doc.add(new BinaryDocValuesField("field", b));+ doc.add(new StringField("id", ""+docID, Field.Store.YES));+ try {+ w.addDocument(doc);+ } catch (IllegalArgumentException iae) {+ if (iae.getMessage().indexOf("is too large") == -1) {+ throw iae;+ } else {+ // OK: some codecs can't handle binary DV > 32K+ assertFalse(codecAcceptsHugeBinaryValues("field"));+ w.rollback();+ d.close();+ return;+ }+ }+ }++ DirectoryReader r;+ try {+ r = w.getReader();+ } catch (IllegalArgumentException iae) {+ if (iae.getMessage().indexOf("is too large") == -1) {+ throw iae;+ } else {+ assertFalse(codecAcceptsHugeBinaryValues("field"));++ // OK: some codecs can't handle binary DV > 32K+ w.rollback();+ d.close();+ return;+ }+ }+ w.close();++ AtomicReader ar = SlowCompositeReaderWrapper.wrap(r);++ BinaryDocValues s = FieldCache.DEFAULT.getTerms(ar, "field");+ for(int docID=0;docID<docBytes.size();docID++) {+ StoredDocument doc = ar.document(docID);+ BytesRef bytes = new BytesRef();+ s.get(docID, bytes);+ byte[] expected = docBytes.get(Integer.parseInt(doc.get("id")));+ assertEquals(expected.length, bytes.length);+ assertEquals(new BytesRef(expected), bytes);+ }++ assertTrue(codecAcceptsHugeBinaryValues("field"));++ ar.close();+ d.close();+ }++ public void testHugeBinaryValueLimit() throws Exception {+ // We only test DVFormats that have a limit+ assumeFalse("test requires codec with limits on max binary field length", codecAcceptsHugeBinaryValues("field"));+ Analyzer analyzer = new MockAnalyzer(random());+ // FSDirectory because SimpleText will consume gobbs of+ // space when storing big binary values:+ Directory d = newFSDirectory(_TestUtil.getTempDir("hugeBinaryValues"));+ boolean doFixed = random().nextBoolean();+ int numDocs;+ int fixedLength = 0;+ if (doFixed) {+ // Sometimes make all values fixed length since some+ // codecs have different code paths for this:+ numDocs = _TestUtil.nextInt(random(), 10, 20);+ fixedLength = Lucene42DocValuesFormat.MAX_BINARY_FIELD_LENGTH;+ } else {+ numDocs = _TestUtil.nextInt(random(), 100, 200);+ }+ IndexWriter w = new IndexWriter(d, newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer));+ List<byte[]> docBytes = new ArrayList<byte[]>();+ long totalBytes = 0;+ for(int docID=0;docID<numDocs;docID++) {+ // we don't use RandomIndexWriter because it might add+ // more docvalues than we expect !!!!++ // Must be > 64KB in size to ensure more than 2 pages in+ // PagedBytes would be needed:+ int numBytes;+ if (doFixed) {+ numBytes = fixedLength;+ } else if (docID == 0 || random().nextInt(5) == 3) {+ numBytes = Lucene42DocValuesFormat.MAX_BINARY_FIELD_LENGTH;+ } else {+ numBytes = _TestUtil.nextInt(random(), 1, Lucene42DocValuesFormat.MAX_BINARY_FIELD_LENGTH);+ }+ totalBytes += numBytes;+ if (totalBytes > 5 * 1024*1024) {+ break;+ }+ byte[] bytes = new byte[numBytes];+ random().nextBytes(bytes);+ docBytes.add(bytes);+ Document doc = new Document();+ BytesRef b = new BytesRef(bytes);+ b.length = bytes.length;+ doc.add(new BinaryDocValuesField("field", b));+ doc.add(new StringField("id", ""+docID, Field.Store.YES));+ w.addDocument(doc);+ }++ DirectoryReader r = w.getReader();+ w.close();++ AtomicReader ar = SlowCompositeReaderWrapper.wrap(r);++ BinaryDocValues s = FieldCache.DEFAULT.getTerms(ar, "field");+ for(int docID=0;docID<docBytes.size();docID++) {+ StoredDocument doc = ar.document(docID);+ BytesRef bytes = new BytesRef();+ s.get(docID, bytes);+ byte[] expected = docBytes.get(Integer.parseInt(doc.get("id")));+ assertEquals(expected.length, bytes.length);+ assertEquals(new BytesRef(expected), bytes);+ }++ ar.close();+ d.close();+ }++ protected boolean codecAcceptsHugeBinaryValues(String field) {+ return true;+ }}diff --cc lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.javaindex 38b2592fdeb,92e10d5869a..baf09e121a3--- a/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java+++ b/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java@@@ -743,13 -743,16 +743,17 @@@ public class _TestUtilreturn p.getName();}}+++ public static String getDocValuesFormat(String field) {+ return getDocValuesFormat(Codec.getDefault(), field);+ }public static String getDocValuesFormat(Codec codec, String field) {- DocValuesFormat d = codec.docValuesFormat();- if (d instanceof PerFieldDocValuesFormat) {- return ((PerFieldDocValuesFormat)d).getDocValuesFormatForField(field).getName();+ DocValuesFormat f = codec.docValuesFormat();+ if (f instanceof PerFieldDocValuesFormat) {+ return ((PerFieldDocValuesFormat) f).getDocValuesFormatForField(field).getName();} else {- return d.getName();+ return f.getName();}}