Apache Lucene open-source search software
remove some api traps
Robert Muir65e170839f48
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/diskdv/DiskDocValuesProducer.java b/lucene/codecs/src/java/org/apache/lucene/codecs/diskdv/DiskDocValuesProducer.javaindex 742f0c2887b..97659b30690 100644--- a/lucene/codecs/src/java/org/apache/lucene/codecs/diskdv/DiskDocValuesProducer.java+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/diskdv/DiskDocValuesProducer.java@@ -33,7 +33,6 @@ import org.apache.lucene.index.NumericDocValues;import org.apache.lucene.index.SegmentReadState;import org.apache.lucene.index.SortedDocValues;import org.apache.lucene.store.IndexInput;-import org.apache.lucene.util.ArrayUtil;import org.apache.lucene.util.BytesRef;import org.apache.lucene.util.IOUtils;import org.apache.lucene.util.packed.BlockPackedReader;@@ -157,12 +156,13 @@ class DiskDocValuesProducer extends DocValuesProducer {long address = bytes.offset + docID * (long)bytes.maxLength;try {data.seek(address);- if (result.bytes.length < bytes.maxLength) {- result.offset = 0;- result.bytes = new byte[bytes.maxLength];- }- data.readBytes(result.bytes, result.offset, bytes.maxLength);- result.length = bytes.maxLength;+ // NOTE: we could have one buffer, but various consumers (e.g. FieldComparatorSource)+ // assume "they" own the bytes after calling this!+ final byte[] buffer = new byte[bytes.maxLength];+ data.readBytes(buffer, 0, buffer.length);+ result.bytes = buffer;+ result.offset = 0;+ result.length = buffer.length;} catch (IOException e) {throw new RuntimeException(e);}@@ -183,11 +183,12 @@ class DiskDocValuesProducer extends DocValuesProducer {int length = (int) (endAddress - startAddress);try {data.seek(startAddress);- if (result.bytes.length < length) {- result.offset = 0;- result.bytes = new byte[ArrayUtil.oversize(length, 1)];- }- data.readBytes(result.bytes, result.offset, length);+ // NOTE: we could have one buffer, but various consumers (e.g. FieldComparatorSource)+ // assume "they" own the bytes after calling this!+ final byte[] buffer = new byte[length];+ data.readBytes(buffer, 0, buffer.length);+ result.bytes = buffer;+ result.offset = 0;result.length = length;} catch (IOException e) {throw new RuntimeException(e);diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene42/Lucene42DocValuesProducer.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene42/Lucene42DocValuesProducer.javaindex b165aea63dd..53f2752f0b0 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene42/Lucene42DocValuesProducer.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene42/Lucene42DocValuesProducer.java@@ -239,7 +239,11 @@ class Lucene42DocValuesProducer extends DocValuesProducer {try {in.setPosition(0);fst.getFirstArc(firstArc);- Util.toBytesRef(Util.getByOutput(fst, ord, in, firstArc, scratchArc, scratchInts), result);+ IntsRef output = Util.getByOutput(fst, ord, in, firstArc, scratchArc, scratchInts);+ result.bytes = new byte[output.length];+ result.offset = 0;+ result.length = 0;+ Util.toBytesRef(output, result);} catch (IOException bogus) {throw new RuntimeException(bogus);}diff --git a/lucene/core/src/java/org/apache/lucene/index/BinaryDocValues.java b/lucene/core/src/java/org/apache/lucene/index/BinaryDocValues.javaindex d31ec12bf69..57ae465120a 100644--- a/lucene/core/src/java/org/apache/lucene/index/BinaryDocValues.java+++ b/lucene/core/src/java/org/apache/lucene/index/BinaryDocValues.java@@ -28,12 +28,7 @@ public abstract class BinaryDocValues {* constructors, typically implicit.) */protected BinaryDocValues() {}- /** Lookup the value for document.- *- * <p><b>NOTE</b>: you should not share the provided- * {@link BytesRef} result with other doc values sources- * (other BinaryDocValues or SortedDocValues): a single- * "private" instance should be used for each source. */+ /** Lookup the value for document. */public abstract void get(int docID, BytesRef result);/**diff --git a/lucene/core/src/java/org/apache/lucene/index/SortedDocValues.java b/lucene/core/src/java/org/apache/lucene/index/SortedDocValues.javaindex d49b274e04d..15a5da0cd2f 100644--- a/lucene/core/src/java/org/apache/lucene/index/SortedDocValues.java+++ b/lucene/core/src/java/org/apache/lucene/index/SortedDocValues.java@@ -92,6 +92,9 @@ public abstract class SortedDocValues extends BinaryDocValues {* @param key Key to look up* @param spare Spare BytesRef**/+ // nocommit: what does spare mean? its no spare: because people rely upon its return value!+ // if its 'result' then the parameter and javadoc needs changing, otherwise things need fixing+ // unconditionally set its value to "NONSENSE" bytes to see the bugs!public int lookupTerm(BytesRef key, BytesRef spare) {int low = 0;@@ -107,9 +110,8 @@ public abstract class SortedDocValues extends BinaryDocValues {} else if (cmp > 0) {high = mid - 1;} else {- // nocommit is this the right way... else caller can- // pass this spare down to DiskDV, which will then- // "use" our byte[] ...+ // nocommit: we shouldnt have to set spare at all if its actually a spare, but its not!+ // ant test -Dtestcase=TestFieldCacheRewriteMethod -Dtests.method=testRegexps -Dtests.seed=AFC4A08B212CE143 -Dtests.slow=true -Dtests.locale=th -Dtests.timezone=Canada/Mountain -Dtests.file.encoding=ISO-8859-1spare.bytes = BytesRef.EMPTY_BYTES;spare.offset = 0;spare.length = 0;@@ -117,7 +119,8 @@ public abstract class SortedDocValues extends BinaryDocValues {}}- // nocommit is this the right way...+ // nocommit: we shouldnt have to set spare at all if its actually a spare, but its not!+ // ant test -Dtestcase=TestFieldCacheRewriteMethod -Dtests.method=testRegexps -Dtests.seed=AFC4A08B212CE143 -Dtests.slow=true -Dtests.locale=th -Dtests.timezone=Canada/Mountain -Dtests.file.encoding=ISO-8859-1spare.bytes = BytesRef.EMPTY_BYTES;spare.offset = 0;spare.length = 0;diff --git a/lucene/core/src/test/org/apache/lucene/TestDemoDocValue.java b/lucene/core/src/test/org/apache/lucene/TestDemoDocValue.javaindex e9e855253d0..a4371f6d677 100644--- a/lucene/core/src/test/org/apache/lucene/TestDemoDocValue.java+++ b/lucene/core/src/test/org/apache/lucene/TestDemoDocValue.java@@ -914,4 +914,128 @@ public class TestDemoDocValue extends LuceneTestCase {ireader.close();directory.close();}++ public void testCodecUsesOwnBytes() throws IOException {+ Analyzer analyzer = new MockAnalyzer(random());++ Directory directory = newDirectory();+ // we don't use RandomIndexWriter because it might add more docvalues than we expect !!!!1+ IndexWriterConfig iwc = newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer);+ iwc.setMergePolicy(newLogMergePolicy());+ IndexWriter iwriter = new IndexWriter(directory, iwc);+ Document doc = new Document();+ doc.add(new BinaryDocValuesField("dv", new BytesRef("boo!")));+ iwriter.addDocument(doc);+ iwriter.close();++ // Now search the index:+ IndexReader ireader = DirectoryReader.open(directory); // read-only=true+ assert ireader.leaves().size() == 1;+ BinaryDocValues dv = ireader.leaves().get(0).reader().getBinaryDocValues("dv");+ byte mybytes[] = new byte[20];+ BytesRef scratch = new BytesRef(mybytes);+ dv.get(0, scratch);+ assertEquals("boo!", scratch.utf8ToString());+ assertFalse(scratch.bytes == mybytes);++ ireader.close();+ directory.close();+ }++ public void testCodecUsesOwnSortedBytes() throws IOException {+ Analyzer analyzer = new MockAnalyzer(random());++ Directory directory = newDirectory();+ // we don't use RandomIndexWriter because it might add more docvalues than we expect !!!!1+ IndexWriterConfig iwc = newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer);+ iwc.setMergePolicy(newLogMergePolicy());+ IndexWriter iwriter = new IndexWriter(directory, iwc);+ Document doc = new Document();+ doc.add(new SortedDocValuesField("dv", new BytesRef("boo!")));+ iwriter.addDocument(doc);+ iwriter.close();++ // Now search the index:+ IndexReader ireader = DirectoryReader.open(directory); // read-only=true+ assert ireader.leaves().size() == 1;+ BinaryDocValues dv = ireader.leaves().get(0).reader().getSortedDocValues("dv");+ byte mybytes[] = new byte[20];+ BytesRef scratch = new BytesRef(mybytes);+ dv.get(0, scratch);+ assertEquals("boo!", scratch.utf8ToString());+ assertFalse(scratch.bytes == mybytes);++ ireader.close();+ directory.close();+ }++ public void testCodecUsesOwnBytesEachTime() throws IOException {+ Analyzer analyzer = new MockAnalyzer(random());++ Directory directory = newDirectory();+ // we don't use RandomIndexWriter because it might add more docvalues than we expect !!!!1+ IndexWriterConfig iwc = newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer);+ iwc.setMergePolicy(newLogMergePolicy());+ IndexWriter iwriter = new IndexWriter(directory, iwc);+ Document doc = new Document();+ doc.add(new BinaryDocValuesField("dv", new BytesRef("foo!")));+ iwriter.addDocument(doc);+ doc = new Document();+ doc.add(new BinaryDocValuesField("dv", new BytesRef("bar!")));+ iwriter.addDocument(doc);+ iwriter.close();++ // Now search the index:+ IndexReader ireader = DirectoryReader.open(directory); // read-only=true+ assert ireader.leaves().size() == 1;+ BinaryDocValues dv = ireader.leaves().get(0).reader().getBinaryDocValues("dv");+ BytesRef scratch = new BytesRef();+ dv.get(0, scratch);+ assertEquals("foo!", scratch.utf8ToString());++ BytesRef scratch2 = new BytesRef();+ dv.get(1, scratch2);+ assertEquals("bar!", scratch2.utf8ToString());+ // check scratch is still valid+ assertEquals("foo!", scratch.utf8ToString());++ ireader.close();+ directory.close();+ }++ public void testCodecUsesOwnSortedBytesEachTime() throws IOException {+ Analyzer analyzer = new MockAnalyzer(random());++ Directory directory = newDirectory();+ // we don't use RandomIndexWriter because it might add more docvalues than we expect !!!!1+ IndexWriterConfig iwc = newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer);+ iwc.setMergePolicy(newLogMergePolicy());+ IndexWriter iwriter = new IndexWriter(directory, iwc);+ Document doc = new Document();+ doc.add(new SortedDocValuesField("dv", new BytesRef("foo!")));+ iwriter.addDocument(doc);+ doc = new Document();+ doc.add(new SortedDocValuesField("dv", new BytesRef("bar!")));+ iwriter.addDocument(doc);+ iwriter.close();++ // Now search the index:+ IndexReader ireader = DirectoryReader.open(directory); // read-only=true+ assert ireader.leaves().size() == 1;+ BinaryDocValues dv = ireader.leaves().get(0).reader().getSortedDocValues("dv");+ BytesRef scratch = new BytesRef();+ dv.get(0, scratch);+ assertEquals("foo!", scratch.utf8ToString());++ BytesRef scratch2 = new BytesRef();+ dv.get(1, scratch2);+ assertEquals("bar!", scratch2.utf8ToString());+ // check scratch is still valid+ assertEquals("foo!", scratch.utf8ToString());++ ireader.close();+ directory.close();+ }++ // nocommit: test add twice}