remove some api traps

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene4547@1439460 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2013-01-28 15:58:58 +00:00
parent 9c91842a94
commit 65e170839f
5 changed files with 150 additions and 23 deletions

View File

@ -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);

View File

@ -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);
}

View File

@ -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);
/**

View File

@ -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-1
spare.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-1
spare.bytes = BytesRef.EMPTY_BYTES;
spare.offset = 0;
spare.length = 0;

View File

@ -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
}