fix LineFileDocs to seek to random start on open

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1056702 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2011-01-08 13:45:44 +00:00
parent bdd7fea1e4
commit ba13db6827
3 changed files with 44 additions and 19 deletions

View File

@ -65,7 +65,7 @@ public class TestNRTThreads extends LuceneTestCase {
CodecProvider.getDefault().setDefaultFieldCodec("Standard"); CodecProvider.getDefault().setDefaultFieldCodec("Standard");
} }
final LineFileDocs docs = new LineFileDocs(true); final LineFileDocs docs = new LineFileDocs(random);
final File tempDir = _TestUtil.getTempDir("nrtopenfiles"); final File tempDir = _TestUtil.getTempDir("nrtopenfiles");
final MockDirectoryWrapper dir = new MockDirectoryWrapper(random, FSDirectory.open(tempDir)); final MockDirectoryWrapper dir = new MockDirectoryWrapper(random, FSDirectory.open(tempDir));
final IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer()); final IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer());

View File

@ -18,6 +18,7 @@ package org.apache.lucene.util;
*/ */
import java.io.Closeable; import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.BufferedReader; import java.io.BufferedReader;
@ -26,6 +27,7 @@ import java.io.InputStream;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import java.util.Random;
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
@ -36,21 +38,19 @@ import org.apache.lucene.document.Field;
public class LineFileDocs implements Closeable { public class LineFileDocs implements Closeable {
private BufferedReader reader; private BufferedReader reader;
private final boolean forever;
private final static int BUFFER_SIZE = 1 << 16; // 64K private final static int BUFFER_SIZE = 1 << 16; // 64K
private final AtomicInteger id = new AtomicInteger(); private final AtomicInteger id = new AtomicInteger();
private final String path; private final String path;
// If forever is true, we rewind the file at EOF (repeat // If forever is true, we rewind the file at EOF (repeat
// the docs over and over) // the docs over and over)
public LineFileDocs(String path, boolean forever) throws IOException { public LineFileDocs(Random random, String path) throws IOException {
this.path = path; this.path = path;
this.forever = forever; open(random);
open();
} }
public LineFileDocs(boolean forever) throws IOException { public LineFileDocs(Random random) throws IOException {
this(LuceneTestCase.TEST_LINE_DOCS_FILE, forever); this(random, LuceneTestCase.TEST_LINE_DOCS_FILE);
} }
public synchronized void close() throws IOException { public synchronized void close() throws IOException {
@ -60,22 +60,49 @@ public class LineFileDocs implements Closeable {
} }
} }
private synchronized void open() throws IOException { private synchronized void open(Random random) throws IOException {
InputStream is = getClass().getResourceAsStream(path); InputStream is = getClass().getResourceAsStream(path);
if (is == null) { if (is == null) {
// if its not in classpath, we load it as absolute filesystem path (e.g. Hudson's home dir) // if its not in classpath, we load it as absolute filesystem path (e.g. Hudson's home dir)
is = new FileInputStream(path); is = new FileInputStream(path);
} }
File file = new File(path);
long size;
if (file.exists()) {
size = file.length();
} else {
size = is.available();
}
if (path.endsWith(".gz")) { if (path.endsWith(".gz")) {
is = new GZIPInputStream(is); is = new GZIPInputStream(is);
} // guestimate:
final InputStream in = new BufferedInputStream(is, BUFFER_SIZE); size *= 2.8;
reader = new BufferedReader(new InputStreamReader(in, "UTF-8"), BUFFER_SIZE);
} }
public synchronized void reset() throws IOException { final InputStream in = new BufferedInputStream(is, BUFFER_SIZE);
reader = new BufferedReader(new InputStreamReader(in, "UTF-8"), BUFFER_SIZE);
// Override sizes for currently "known" line files:
if (path.equals("europarl.lines.txt.gz")) {
size = 15129506L;
} else if (path.equals("/home/hudson/lucene-data/enwiki.random.lines.txt.gz")) {
size = 3038178822L;
}
// Randomly seek to starting point:
if (random != null && size > 3) {
final long seekTo = (random.nextLong()&Long.MAX_VALUE) % (size/3);
if (LuceneTestCase.VERBOSE) {
System.out.println("TEST: LineFileDocs: seek to fp=" + seekTo + " on open");
}
reader.skip(seekTo);
reader.readLine();
}
}
public synchronized void reset(Random random) throws IOException {
close(); close();
open(); open(random);
id.set(0); id.set(0);
} }
@ -117,16 +144,14 @@ public class LineFileDocs implements Closeable {
synchronized(this) { synchronized(this) {
line = reader.readLine(); line = reader.readLine();
if (line == null) { if (line == null) {
if (forever) { // Always rewind at end:
if (LuceneTestCase.VERBOSE) { if (LuceneTestCase.VERBOSE) {
System.out.println("TEST: LineFileDocs: now rewind file..."); System.out.println("TEST: LineFileDocs: now rewind file...");
} }
close(); close();
open(); open(null);
line = reader.readLine(); line = reader.readLine();
} }
return null;
}
} }
DocState docState = threadDocs.get(); DocState docState = threadDocs.get();

View File

@ -944,7 +944,7 @@ public class TestFSTs extends LuceneTestCase {
CodecProvider.getDefault().setDefaultFieldCodec("Standard"); CodecProvider.getDefault().setDefaultFieldCodec("Standard");
} }
final LineFileDocs docs = new LineFileDocs(false); final LineFileDocs docs = new LineFileDocs(random);
final int RUN_TIME_SEC = LuceneTestCase.TEST_NIGHTLY ? 100 : 1; final int RUN_TIME_SEC = LuceneTestCase.TEST_NIGHTLY ? 100 : 1;
final IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer()).setMaxBufferedDocs(-1).setRAMBufferSizeMB(64); final IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer()).setMaxBufferedDocs(-1).setRAMBufferSizeMB(64);
final File tempDir = _TestUtil.getTempDir("fstlines"); final File tempDir = _TestUtil.getTempDir("fstlines");