- Added a comment and cleaned up.

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150099 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Otis Gospodnetic 2003-10-13 14:31:38 +00:00
parent 6600da8d84
commit 6fe013aa7d
7 changed files with 239 additions and 251 deletions

View File

@ -506,7 +506,7 @@ PARSER_END(QueryParser)
} }
<Boost> TOKEN : { <Boost> TOKEN : {
<NUMBER: (<_NUM_CHAR>)+ ( "." (<_NUM_CHAR>)+ )? > : DEFAULT <NUMBER: (["+","-"])? (<_NUM_CHAR>)+ ( "." (<_NUM_CHAR>)+ )? > : DEFAULT
} }
<RangeIn> TOKEN : { <RangeIn> TOKEN : {

View File

@ -62,11 +62,14 @@ import org.apache.lucene.store.OutputStream;
/** Optimized implementation of a vector of bits. This is more-or-less like /** Optimized implementation of a vector of bits. This is more-or-less like
java.util.BitSet, but also includes the following: java.util.BitSet, but also includes the following:
<UL> <ul>
<LI>a count() method, which efficiently computes the number of one bits;</LI> <li>a count() method, which efficiently computes the number of one bits;</li>
<LI>optimized read from and write to disk;</LI> <li>optimized read from and write to disk;</li>
<LI>inlinable get() method;</LI> <li>inlinable get() method;</li>
</UL> </ul>
@author Doug Cutting
@version $Id$
*/ */
public final class BitVector { public final class BitVector {
@ -108,11 +111,12 @@ public final class BitVector {
computed and cached, so that, if the vector is not changed, no computed and cached, so that, if the vector is not changed, no
recomputation is done for repeated calls. */ recomputation is done for repeated calls. */
public final int count() { public final int count() {
// if the vector has been modified
if (count == -1) { if (count == -1) {
int c = 0; int c = 0;
int end = bits.length; int end = bits.length;
for (int i = 0; i < end; i++) for (int i = 0; i < end; i++)
c += BYTE_COUNTS[bits[i] & 0xFF]; // sum bits per byte c += BYTE_COUNTS[bits[i] & 0xFF]; // sum bits per byte
count = c; count = c;
} }
return count; return count;

View File

@ -71,18 +71,18 @@ class SearchTest {
IndexWriter writer = new IndexWriter(directory, analyzer, true); IndexWriter writer = new IndexWriter(directory, analyzer, true);
String[] docs = { String[] docs = {
"a b c d e", "a b c d e",
"a b c d e a b c d e", "a b c d e a b c d e",
"a b c d e f g h i j", "a b c d e f g h i j",
"a c e", "a c e",
"e c a", "e c a",
"a c e a c e", "a c e a c e",
"a c e a b c" "a c e a b c"
}; };
for (int j = 0; j < docs.length; j++) { for (int j = 0; j < docs.length; j++) {
Document d = new Document(); Document d = new Document();
d.add(Field.Text("contents", docs[j])); d.add(Field.Text("contents", docs[j]));
writer.addDocument(d); writer.addDocument(d);
} }
writer.close(); writer.close();
@ -94,30 +94,30 @@ class SearchTest {
// "\"a b c\"", // "\"a b c\"",
// "a c", // "a c",
// "\"a c\"", // "\"a c\"",
"\"a c e\"", "\"a c e\"",
}; };
Hits hits = null; Hits hits = null;
QueryParser parser = new QueryParser("contents", analyzer); QueryParser parser = new QueryParser("contents", analyzer);
parser.setPhraseSlop(4); parser.setPhraseSlop(4);
for (int j = 0; j < queries.length; j++) { for (int j = 0; j < queries.length; j++) {
Query query = parser.parse(queries[j]); Query query = parser.parse(queries[j]);
System.out.println("Query: " + query.toString("contents")); System.out.println("Query: " + query.toString("contents"));
//DateFilter filter = //DateFilter filter =
// new DateFilter("modified", Time(1997,0,1), Time(1998,0,1)); // new DateFilter("modified", Time(1997,0,1), Time(1998,0,1));
//DateFilter filter = DateFilter.Before("modified", Time(1997,00,01)); //DateFilter filter = DateFilter.Before("modified", Time(1997,00,01));
//System.out.println(filter); //System.out.println(filter);
hits = searcher.search(query); hits = searcher.search(query);
System.out.println(hits.length() + " total results"); System.out.println(hits.length() + " total results");
for (int i = 0 ; i < hits.length() && i < 10; i++) { for (int i = 0 ; i < hits.length() && i < 10; i++) {
Document d = hits.doc(i); Document d = hits.doc(i);
System.out.println(i + " " + hits.score(i) System.out.println(i + " " + hits.score(i)
// + " " + DateField.stringToDate(d.get("modified")) // + " " + DateField.stringToDate(d.get("modified"))
+ " " + d.get("contents")); + " " + d.get("contents"));
} }
} }
searcher.close(); searcher.close();

View File

@ -1,5 +1,59 @@
package org.apache.lucene.index; package org.apache.lucene.index;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Lucene" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Lucene", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.io.IOException; import java.io.IOException;
import junit.framework.TestCase; import junit.framework.TestCase;
@ -36,8 +90,8 @@ public class TestCompoundFile extends TestCase
// TestRunner.run (new TestCompoundFile("testIWCreate")); // TestRunner.run (new TestCompoundFile("testIWCreate"));
} }
public TestCompoundFile() { public TestCompoundFile() {
super(); super();
} }
@ -45,16 +99,16 @@ public class TestCompoundFile extends TestCase
public TestCompoundFile(String name) { public TestCompoundFile(String name) {
super(name); super(name);
} }
private Directory dir; private Directory dir;
public void setUp() throws IOException { public void setUp() throws IOException {
//dir = new RAMDirectory(); //dir = new RAMDirectory();
dir = FSDirectory.getDirectory("testIndex", true); dir = FSDirectory.getDirectory("testIndex", true);
} }
/** Creates a file of the specified size with random data. */ /** Creates a file of the specified size with random data. */
private void createRandomFile(Directory dir, String name, int size) private void createRandomFile(Directory dir, String name, int size)
throws IOException throws IOException
@ -66,15 +120,15 @@ public class TestCompoundFile extends TestCase
} }
os.close(); os.close();
} }
/** Creates a file of the specified size with sequential data. The first /** Creates a file of the specified size with sequential data. The first
* byte is written as the start byte provided. All subsequent bytes are * byte is written as the start byte provided. All subsequent bytes are
* computed as start + offset where offset is the number of the byte. * computed as start + offset where offset is the number of the byte.
*/ */
private void createSequenceFile(Directory dir, private void createSequenceFile(Directory dir,
String name, String name,
byte start, byte start,
int size) int size)
throws IOException throws IOException
{ {
OutputStream os = dir.createFile(name); OutputStream os = dir.createFile(name);
@ -84,35 +138,35 @@ public class TestCompoundFile extends TestCase
} }
os.close(); os.close();
} }
private void assertSameStreams(String msg, private void assertSameStreams(String msg,
InputStream expected, InputStream expected,
InputStream test) InputStream test)
throws IOException throws IOException
{ {
assertNotNull(msg + " null expected", expected); assertNotNull(msg + " null expected", expected);
assertNotNull(msg + " null test", test); assertNotNull(msg + " null test", test);
assertEquals(msg + " length", expected.length(), test.length()); assertEquals(msg + " length", expected.length(), test.length());
assertEquals(msg + " position", expected.getFilePointer(), assertEquals(msg + " position", expected.getFilePointer(),
test.getFilePointer()); test.getFilePointer());
byte expectedBuffer[] = new byte[512]; byte expectedBuffer[] = new byte[512];
byte testBuffer[] = new byte[expectedBuffer.length]; byte testBuffer[] = new byte[expectedBuffer.length];
long remainder = expected.length() - expected.getFilePointer(); long remainder = expected.length() - expected.getFilePointer();
while(remainder > 0) { while(remainder > 0) {
int readLen = (int) Math.min(remainder, expectedBuffer.length); int readLen = (int) Math.min(remainder, expectedBuffer.length);
expected.readBytes(expectedBuffer, 0, readLen); expected.readBytes(expectedBuffer, 0, readLen);
test.readBytes(testBuffer, 0, readLen); test.readBytes(testBuffer, 0, readLen);
assertEqualArrays(msg + ", remainder " + remainder, expectedBuffer, assertEqualArrays(msg + ", remainder " + remainder, expectedBuffer,
testBuffer, 0, readLen); testBuffer, 0, readLen);
remainder -= readLen; remainder -= readLen;
} }
} }
private void assertSameStreams(String msg, private void assertSameStreams(String msg,
InputStream expected, InputStream expected,
InputStream actual, InputStream actual,
long seekTo) long seekTo)
@ -122,30 +176,30 @@ public class TestCompoundFile extends TestCase
try { try {
actual.seek(seekTo); actual.seek(seekTo);
fail(msg + ", " + seekTo + ", negative seek"); fail(msg + ", " + seekTo + ", negative seek");
} catch (IOException e) { } catch (IOException e) {
/* success */ /* success */
//System.out.println("SUCCESS: Negative seek: " + e); //System.out.println("SUCCESS: Negative seek: " + e);
} }
} else if (seekTo > 0 && seekTo >= expected.length()) { } else if (seekTo > 0 && seekTo >= expected.length()) {
try { try {
actual.seek(seekTo); actual.seek(seekTo);
fail(msg + ", " + seekTo + ", seek past EOF"); fail(msg + ", " + seekTo + ", seek past EOF");
} catch (IOException e) { } catch (IOException e) {
/* success */ /* success */
//System.out.println("SUCCESS: Seek past EOF: " + e); //System.out.println("SUCCESS: Seek past EOF: " + e);
} }
} else { } else {
expected.seek(seekTo); expected.seek(seekTo);
actual.seek(seekTo); actual.seek(seekTo);
assertSameStreams(msg + ", seek(mid)", expected, actual); assertSameStreams(msg + ", seek(mid)", expected, actual);
} }
} }
private void assertSameSeekBehavior(String msg, private void assertSameSeekBehavior(String msg,
InputStream expected, InputStream expected,
InputStream actual) InputStream actual)
throws IOException throws IOException
@ -153,52 +207,52 @@ public class TestCompoundFile extends TestCase
// seek to 0 // seek to 0
long point = 0; long point = 0;
assertSameStreams(msg + ", seek(0)", expected, actual, point); assertSameStreams(msg + ", seek(0)", expected, actual, point);
// seek to middle // seek to middle
point = expected.length() / 2l; point = expected.length() / 2l;
assertSameStreams(msg + ", seek(mid)", expected, actual, point); assertSameStreams(msg + ", seek(mid)", expected, actual, point);
// seek to end - 2 // seek to end - 2
point = expected.length() - 2; point = expected.length() - 2;
assertSameStreams(msg + ", seek(end-2)", expected, actual, point); assertSameStreams(msg + ", seek(end-2)", expected, actual, point);
// seek to end - 1 // seek to end - 1
point = expected.length() - 1; point = expected.length() - 1;
assertSameStreams(msg + ", seek(end-1)", expected, actual, point); assertSameStreams(msg + ", seek(end-1)", expected, actual, point);
// seek to the end // seek to the end
point = expected.length(); point = expected.length();
assertSameStreams(msg + ", seek(end)", expected, actual, point); assertSameStreams(msg + ", seek(end)", expected, actual, point);
// seek past end // seek past end
point = expected.length() + 1; point = expected.length() + 1;
assertSameStreams(msg + ", seek(end+1)", expected, actual, point); assertSameStreams(msg + ", seek(end+1)", expected, actual, point);
} }
private void assertEqualArrays(String msg, private void assertEqualArrays(String msg,
byte[] expected, byte[] expected,
byte[] test, byte[] test,
int start, int start,
int len) int len)
{ {
assertNotNull(msg + " null expected", expected); assertNotNull(msg + " null expected", expected);
assertNotNull(msg + " null test", test); assertNotNull(msg + " null test", test);
for (int i=start; i<len; i++) { for (int i=start; i<len; i++) {
assertEquals(msg + " " + i, expected[i], test[i]); assertEquals(msg + " " + i, expected[i], test[i]);
} }
} }
// =========================================================== // ===========================================================
// Tests of the basic CompoundFile functionality // Tests of the basic CompoundFile functionality
// =========================================================== // ===========================================================
/** This test creates compound file based on a single file. /** This test creates compound file based on a single file.
* Files of different sizes are tested: 0, 1, 10, 100 bytes. * Files of different sizes are tested: 0, 1, 10, 100 bytes.
*/ */
public void testSingleFile() throws IOException { public void testSingleFile() throws IOException {
int data[] = new int[] { 0, 1, 10, 100 }; int data[] = new int[] { 0, 1, 10, 100 };
for (int i=0; i<data.length; i++) { for (int i=0; i<data.length; i++) {
@ -207,7 +261,7 @@ public class TestCompoundFile extends TestCase
CompoundFileWriter csw = new CompoundFileWriter(dir, name + ".cfs"); CompoundFileWriter csw = new CompoundFileWriter(dir, name + ".cfs");
csw.addFile(name); csw.addFile(name);
csw.close(); csw.close();
CompoundFileReader csr = new CompoundFileReader(dir, name + ".cfs"); CompoundFileReader csr = new CompoundFileReader(dir, name + ".cfs");
InputStream expected = dir.openFile(name); InputStream expected = dir.openFile(name);
InputStream actual = csr.openFile(name); InputStream actual = csr.openFile(name);
@ -218,20 +272,20 @@ public class TestCompoundFile extends TestCase
csr.close(); csr.close();
} }
} }
/** This test creates compound file based on two files. /** This test creates compound file based on two files.
* *
*/ */
public void testTwoFiles() throws IOException { public void testTwoFiles() throws IOException {
createSequenceFile(dir, "d1", (byte) 0, 15); createSequenceFile(dir, "d1", (byte) 0, 15);
createSequenceFile(dir, "d2", (byte) 0, 114); createSequenceFile(dir, "d2", (byte) 0, 114);
CompoundFileWriter csw = new CompoundFileWriter(dir, "d.csf"); CompoundFileWriter csw = new CompoundFileWriter(dir, "d.csf");
csw.addFile("d1"); csw.addFile("d1");
csw.addFile("d2"); csw.addFile("d2");
csw.close(); csw.close();
CompoundFileReader csr = new CompoundFileReader(dir, "d.csf"); CompoundFileReader csr = new CompoundFileReader(dir, "d.csf");
InputStream expected = dir.openFile("d1"); InputStream expected = dir.openFile("d1");
InputStream actual = csr.openFile("d1"); InputStream actual = csr.openFile("d1");
@ -239,7 +293,7 @@ public class TestCompoundFile extends TestCase
assertSameSeekBehavior("d1", expected, actual); assertSameSeekBehavior("d1", expected, actual);
expected.close(); expected.close();
actual.close(); actual.close();
expected = dir.openFile("d2"); expected = dir.openFile("d2");
actual = csr.openFile("d2"); actual = csr.openFile("d2");
assertSameStreams("d2", expected, actual); assertSameStreams("d2", expected, actual);
@ -248,7 +302,7 @@ public class TestCompoundFile extends TestCase
actual.close(); actual.close();
csr.close(); csr.close();
} }
/** This test creates a compound file based on a large number of files of /** This test creates a compound file based on a large number of files of
* various length. The file content is generated randomly. The sizes range * various length. The file content is generated randomly. The sizes range
* from 0 to 1Mb. Some of the sizes are selected to test the buffering * from 0 to 1Mb. Some of the sizes are selected to test the buffering
@ -270,7 +324,7 @@ public class TestCompoundFile extends TestCase
createRandomFile(dir, segment + ".big5", 3 * chunk - 1); createRandomFile(dir, segment + ".big5", 3 * chunk - 1);
createRandomFile(dir, segment + ".big6", 3 * chunk + 1); createRandomFile(dir, segment + ".big6", 3 * chunk + 1);
createRandomFile(dir, segment + ".big7", 1000 * chunk); createRandomFile(dir, segment + ".big7", 1000 * chunk);
// Setup extraneous files // Setup extraneous files
createRandomFile(dir, "onetwothree", 100); createRandomFile(dir, "onetwothree", 100);
createRandomFile(dir, segment + ".notIn", 50); createRandomFile(dir, segment + ".notIn", 50);
@ -279,14 +333,14 @@ public class TestCompoundFile extends TestCase
// Now test // Now test
CompoundFileWriter csw = new CompoundFileWriter(dir, "test.cfs"); CompoundFileWriter csw = new CompoundFileWriter(dir, "test.cfs");
final String data[] = new String[] { final String data[] = new String[] {
".zero", ".one", ".ten", ".hundred", ".big1", ".big2", ".big3", ".zero", ".one", ".ten", ".hundred", ".big1", ".big2", ".big3",
".big4", ".big5", ".big6", ".big7" ".big4", ".big5", ".big6", ".big7"
}; };
for (int i=0; i<data.length; i++) { for (int i=0; i<data.length; i++) {
csw.addFile(segment + data[i]); csw.addFile(segment + data[i]);
} }
csw.close(); csw.close();
CompoundFileReader csr = new CompoundFileReader(dir, "test.cfs"); CompoundFileReader csr = new CompoundFileReader(dir, "test.cfs");
for (int i=0; i<data.length; i++) { for (int i=0; i<data.length; i++) {
InputStream check = dir.openFile(segment + data[i]); InputStream check = dir.openFile(segment + data[i]);
@ -298,11 +352,11 @@ public class TestCompoundFile extends TestCase
} }
csr.close(); csr.close();
} }
/** Setup a larger compound file with a number of components, each of /** Setup a larger compound file with a number of components, each of
* which is a sequential file (so that we can easily tell that we are * which is a sequential file (so that we can easily tell that we are
* reading in the right byte). The methods sets up 20 files - f0 to f19, * reading in the right byte). The methods sets up 20 files - f0 to f19,
* the size of each file is 1000 bytes. * the size of each file is 1000 bytes.
*/ */
private void setUp_2() throws IOException { private void setUp_2() throws IOException {
@ -313,12 +367,12 @@ public class TestCompoundFile extends TestCase
} }
cw.close(); cw.close();
} }
public void testReadAfterClose() throws IOException { public void testReadAfterClose() throws IOException {
demo_FSInputStreamBug((FSDirectory) dir, "test"); demo_FSInputStreamBug((FSDirectory) dir, "test");
} }
private void demo_FSInputStreamBug(FSDirectory fsdir, String file) private void demo_FSInputStreamBug(FSDirectory fsdir, String file)
throws IOException throws IOException
{ {
@ -328,96 +382,96 @@ public class TestCompoundFile extends TestCase
os.writeByte((byte) i); os.writeByte((byte) i);
} }
os.close(); os.close();
InputStream in = fsdir.openFile(file); InputStream in = fsdir.openFile(file);
// This read primes the buffer in InputStream // This read primes the buffer in InputStream
byte b = in.readByte(); byte b = in.readByte();
// Close the file // Close the file
in.close(); in.close();
// ERROR: this call should fail, but succeeds because the buffer // ERROR: this call should fail, but succeeds because the buffer
// is still filled // is still filled
b = in.readByte(); b = in.readByte();
// ERROR: this call should fail, but succeeds for some reason as well // ERROR: this call should fail, but succeeds for some reason as well
in.seek(1099); in.seek(1099);
try { try {
// OK: this call correctly fails. We are now past the 1024 internal // OK: this call correctly fails. We are now past the 1024 internal
// buffer, so an actual IO is attempted, which fails // buffer, so an actual IO is attempted, which fails
b = in.readByte(); b = in.readByte();
} catch (IOException e) { } catch (IOException e) {
} }
} }
static boolean isCSInputStream(InputStream is) { static boolean isCSInputStream(InputStream is) {
return is instanceof CompoundFileReader.CSInputStream; return is instanceof CompoundFileReader.CSInputStream;
} }
static boolean isCSInputStreamOpen(InputStream is) throws IOException { static boolean isCSInputStreamOpen(InputStream is) throws IOException {
if (isCSInputStream(is)) { if (isCSInputStream(is)) {
CompoundFileReader.CSInputStream cis = CompoundFileReader.CSInputStream cis =
(CompoundFileReader.CSInputStream) is; (CompoundFileReader.CSInputStream) is;
return _TestHelper.isFSInputStreamOpen(cis.base); return _TestHelper.isFSInputStreamOpen(cis.base);
} else { } else {
return false; return false;
} }
} }
public void testClonedStreamsClosing() throws IOException { public void testClonedStreamsClosing() throws IOException {
setUp_2(); setUp_2();
CompoundFileReader cr = new CompoundFileReader(dir, "f.comp"); CompoundFileReader cr = new CompoundFileReader(dir, "f.comp");
// basic clone // basic clone
InputStream expected = dir.openFile("f11"); InputStream expected = dir.openFile("f11");
assertTrue(_TestHelper.isFSInputStreamOpen(expected)); assertTrue(_TestHelper.isFSInputStreamOpen(expected));
InputStream one = cr.openFile("f11"); InputStream one = cr.openFile("f11");
assertTrue(isCSInputStreamOpen(one)); assertTrue(isCSInputStreamOpen(one));
InputStream two = (InputStream) one.clone(); InputStream two = (InputStream) one.clone();
assertTrue(isCSInputStreamOpen(two)); assertTrue(isCSInputStreamOpen(two));
assertSameStreams("basic clone one", expected, one); assertSameStreams("basic clone one", expected, one);
expected.seek(0); expected.seek(0);
assertSameStreams("basic clone two", expected, two); assertSameStreams("basic clone two", expected, two);
// Now close the first stream // Now close the first stream
one.close(); one.close();
assertTrue("Only close when cr is closed", isCSInputStreamOpen(one)); assertTrue("Only close when cr is closed", isCSInputStreamOpen(one));
// The following should really fail since we couldn't expect to // The following should really fail since we couldn't expect to
// access a file once close has been called on it (regardless of // access a file once close has been called on it (regardless of
// buffering and/or clone magic) // buffering and/or clone magic)
expected.seek(0); expected.seek(0);
two.seek(0); two.seek(0);
assertSameStreams("basic clone two/2", expected, two); assertSameStreams("basic clone two/2", expected, two);
// Now close the compound reader // Now close the compound reader
cr.close(); cr.close();
assertFalse("Now closed one", isCSInputStreamOpen(one)); assertFalse("Now closed one", isCSInputStreamOpen(one));
assertFalse("Now closed two", isCSInputStreamOpen(two)); assertFalse("Now closed two", isCSInputStreamOpen(two));
// The following may also fail since the compound stream is closed // The following may also fail since the compound stream is closed
expected.seek(0); expected.seek(0);
two.seek(0); two.seek(0);
//assertSameStreams("basic clone two/3", expected, two); //assertSameStreams("basic clone two/3", expected, two);
// Now close the second clone // Now close the second clone
two.close(); two.close();
expected.seek(0); expected.seek(0);
two.seek(0); two.seek(0);
//assertSameStreams("basic clone two/4", expected, two); //assertSameStreams("basic clone two/4", expected, two);
expected.close(); expected.close();
} }
/** This test opens two files from a compound stream and verifies that /** This test opens two files from a compound stream and verifies that
@ -426,39 +480,39 @@ public class TestCompoundFile extends TestCase
public void testRandomAccess() throws IOException { public void testRandomAccess() throws IOException {
setUp_2(); setUp_2();
CompoundFileReader cr = new CompoundFileReader(dir, "f.comp"); CompoundFileReader cr = new CompoundFileReader(dir, "f.comp");
// Open two files // Open two files
InputStream e1 = dir.openFile("f11"); InputStream e1 = dir.openFile("f11");
InputStream e2 = dir.openFile("f3"); InputStream e2 = dir.openFile("f3");
InputStream a1 = cr.openFile("f11"); InputStream a1 = cr.openFile("f11");
InputStream a2 = dir.openFile("f3"); InputStream a2 = dir.openFile("f3");
// Seek the first pair // Seek the first pair
e1.seek(100); e1.seek(100);
a1.seek(100); a1.seek(100);
assertEquals(100, e1.getFilePointer()); assertEquals(100, e1.getFilePointer());
assertEquals(100, a1.getFilePointer()); assertEquals(100, a1.getFilePointer());
byte be1 = e1.readByte(); byte be1 = e1.readByte();
byte ba1 = a1.readByte(); byte ba1 = a1.readByte();
assertEquals(be1, ba1); assertEquals(be1, ba1);
// Now seek the second pair // Now seek the second pair
e2.seek(1027); e2.seek(1027);
a2.seek(1027); a2.seek(1027);
assertEquals(1027, e2.getFilePointer()); assertEquals(1027, e2.getFilePointer());
assertEquals(1027, a2.getFilePointer()); assertEquals(1027, a2.getFilePointer());
byte be2 = e2.readByte(); byte be2 = e2.readByte();
byte ba2 = a2.readByte(); byte ba2 = a2.readByte();
assertEquals(be2, ba2); assertEquals(be2, ba2);
// Now make sure the first one didn't move // Now make sure the first one didn't move
assertEquals(101, e1.getFilePointer()); assertEquals(101, e1.getFilePointer());
assertEquals(101, a1.getFilePointer()); assertEquals(101, a1.getFilePointer());
be1 = e1.readByte(); be1 = e1.readByte();
ba1 = a1.readByte(); ba1 = a1.readByte();
assertEquals(be1, ba1); assertEquals(be1, ba1);
// Now more the first one again, past the buffer length // Now more the first one again, past the buffer length
e1.seek(1910); e1.seek(1910);
a1.seek(1910); a1.seek(1910);
@ -467,14 +521,14 @@ public class TestCompoundFile extends TestCase
be1 = e1.readByte(); be1 = e1.readByte();
ba1 = a1.readByte(); ba1 = a1.readByte();
assertEquals(be1, ba1); assertEquals(be1, ba1);
// Now make sure the second set didn't move // Now make sure the second set didn't move
assertEquals(1028, e2.getFilePointer()); assertEquals(1028, e2.getFilePointer());
assertEquals(1028, a2.getFilePointer()); assertEquals(1028, a2.getFilePointer());
be2 = e2.readByte(); be2 = e2.readByte();
ba2 = a2.readByte(); ba2 = a2.readByte();
assertEquals(be2, ba2); assertEquals(be2, ba2);
// Move the second set back, again cross the buffer size // Move the second set back, again cross the buffer size
e2.seek(17); e2.seek(17);
a2.seek(17); a2.seek(17);
@ -484,60 +538,60 @@ public class TestCompoundFile extends TestCase
ba2 = a2.readByte(); ba2 = a2.readByte();
assertEquals(be2, ba2); assertEquals(be2, ba2);
// Finally, make sure the first set didn't move // Finally, make sure the first set didn't move
// Now make sure the first one didn't move // Now make sure the first one didn't move
assertEquals(1911, e1.getFilePointer()); assertEquals(1911, e1.getFilePointer());
assertEquals(1911, a1.getFilePointer()); assertEquals(1911, a1.getFilePointer());
be1 = e1.readByte(); be1 = e1.readByte();
ba1 = a1.readByte(); ba1 = a1.readByte();
assertEquals(be1, ba1); assertEquals(be1, ba1);
e1.close(); e1.close();
e2.close(); e2.close();
a1.close(); a1.close();
a2.close(); a2.close();
cr.close(); cr.close();
} }
/** This test opens two files from a compound stream and verifies that /** This test opens two files from a compound stream and verifies that
* their file positions are independent of each other. * their file positions are independent of each other.
*/ */
public void testRandomAccessClones() throws IOException { public void testRandomAccessClones() throws IOException {
setUp_2(); setUp_2();
CompoundFileReader cr = new CompoundFileReader(dir, "f.comp"); CompoundFileReader cr = new CompoundFileReader(dir, "f.comp");
// Open two files // Open two files
InputStream e1 = cr.openFile("f11"); InputStream e1 = cr.openFile("f11");
InputStream e2 = cr.openFile("f3"); InputStream e2 = cr.openFile("f3");
InputStream a1 = (InputStream) e1.clone(); InputStream a1 = (InputStream) e1.clone();
InputStream a2 = (InputStream) e2.clone(); InputStream a2 = (InputStream) e2.clone();
// Seek the first pair // Seek the first pair
e1.seek(100); e1.seek(100);
a1.seek(100); a1.seek(100);
assertEquals(100, e1.getFilePointer()); assertEquals(100, e1.getFilePointer());
assertEquals(100, a1.getFilePointer()); assertEquals(100, a1.getFilePointer());
byte be1 = e1.readByte(); byte be1 = e1.readByte();
byte ba1 = a1.readByte(); byte ba1 = a1.readByte();
assertEquals(be1, ba1); assertEquals(be1, ba1);
// Now seek the second pair // Now seek the second pair
e2.seek(1027); e2.seek(1027);
a2.seek(1027); a2.seek(1027);
assertEquals(1027, e2.getFilePointer()); assertEquals(1027, e2.getFilePointer());
assertEquals(1027, a2.getFilePointer()); assertEquals(1027, a2.getFilePointer());
byte be2 = e2.readByte(); byte be2 = e2.readByte();
byte ba2 = a2.readByte(); byte ba2 = a2.readByte();
assertEquals(be2, ba2); assertEquals(be2, ba2);
// Now make sure the first one didn't move // Now make sure the first one didn't move
assertEquals(101, e1.getFilePointer()); assertEquals(101, e1.getFilePointer());
assertEquals(101, a1.getFilePointer()); assertEquals(101, a1.getFilePointer());
be1 = e1.readByte(); be1 = e1.readByte();
ba1 = a1.readByte(); ba1 = a1.readByte();
assertEquals(be1, ba1); assertEquals(be1, ba1);
// Now more the first one again, past the buffer length // Now more the first one again, past the buffer length
e1.seek(1910); e1.seek(1910);
a1.seek(1910); a1.seek(1910);
@ -546,14 +600,14 @@ public class TestCompoundFile extends TestCase
be1 = e1.readByte(); be1 = e1.readByte();
ba1 = a1.readByte(); ba1 = a1.readByte();
assertEquals(be1, ba1); assertEquals(be1, ba1);
// Now make sure the second set didn't move // Now make sure the second set didn't move
assertEquals(1028, e2.getFilePointer()); assertEquals(1028, e2.getFilePointer());
assertEquals(1028, a2.getFilePointer()); assertEquals(1028, a2.getFilePointer());
be2 = e2.readByte(); be2 = e2.readByte();
ba2 = a2.readByte(); ba2 = a2.readByte();
assertEquals(be2, ba2); assertEquals(be2, ba2);
// Move the second set back, again cross the buffer size // Move the second set back, again cross the buffer size
e2.seek(17); e2.seek(17);
a2.seek(17); a2.seek(17);
@ -563,40 +617,40 @@ public class TestCompoundFile extends TestCase
ba2 = a2.readByte(); ba2 = a2.readByte();
assertEquals(be2, ba2); assertEquals(be2, ba2);
// Finally, make sure the first set didn't move // Finally, make sure the first set didn't move
// Now make sure the first one didn't move // Now make sure the first one didn't move
assertEquals(1911, e1.getFilePointer()); assertEquals(1911, e1.getFilePointer());
assertEquals(1911, a1.getFilePointer()); assertEquals(1911, a1.getFilePointer());
be1 = e1.readByte(); be1 = e1.readByte();
ba1 = a1.readByte(); ba1 = a1.readByte();
assertEquals(be1, ba1); assertEquals(be1, ba1);
e1.close(); e1.close();
e2.close(); e2.close();
a1.close(); a1.close();
a2.close(); a2.close();
cr.close(); cr.close();
} }
public void testFileNotFound() throws IOException { public void testFileNotFound() throws IOException {
setUp_2(); setUp_2();
CompoundFileReader cr = new CompoundFileReader(dir, "f.comp"); CompoundFileReader cr = new CompoundFileReader(dir, "f.comp");
// Open two files // Open two files
try { try {
InputStream e1 = cr.openFile("bogus"); InputStream e1 = cr.openFile("bogus");
fail("File not found"); fail("File not found");
} catch (IOException e) { } catch (IOException e) {
/* success */ /* success */
//System.out.println("SUCCESS: File Not Found: " + e); //System.out.println("SUCCESS: File Not Found: " + e);
} }
cr.close(); cr.close();
} }
public void testReadPastEOF() throws IOException { public void testReadPastEOF() throws IOException {
setUp_2(); setUp_2();
CompoundFileReader cr = new CompoundFileReader(dir, "f.comp"); CompoundFileReader cr = new CompoundFileReader(dir, "f.comp");
@ -604,7 +658,7 @@ public class TestCompoundFile extends TestCase
is.seek(is.length() - 10); is.seek(is.length() - 10);
byte b[] = new byte[100]; byte b[] = new byte[100];
is.readBytes(b, 0, 10); is.readBytes(b, 0, 10);
try { try {
byte test = is.readByte(); byte test = is.readByte();
fail("Single byte read past end of file"); fail("Single byte read past end of file");
@ -612,7 +666,7 @@ public class TestCompoundFile extends TestCase
/* success */ /* success */
//System.out.println("SUCCESS: single byte read past end of file: " + e); //System.out.println("SUCCESS: single byte read past end of file: " + e);
} }
is.seek(is.length() - 10); is.seek(is.length() - 10);
try { try {
is.readBytes(b, 0, 50); is.readBytes(b, 0, 50);
@ -621,81 +675,8 @@ public class TestCompoundFile extends TestCase
/* success */ /* success */
//System.out.println("SUCCESS: block read past end of file: " + e); //System.out.println("SUCCESS: block read past end of file: " + e);
} }
is.close(); is.close();
cr.close(); cr.close();
} }
// ===========================================================
// More extensive tests involving an IndexWriter
// ===========================================================
public void testIWCreate() throws IOException {
// create index writer
IndexWriter iw = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
int created = 0;
for (int i=0; i<150; i++) {
iw.addDocument(createTestDoc(String.valueOf(i)));
created ++;
}
assertEquals(created, iw.docCount());
iw.close();
// delete 500 documents
IndexReader reader = IndexReader.open(dir);
int deleted = 0;
for (int i = 10; i < created-7; i+=7) {
reader.delete(i);
deleted ++;
}
reader.close();
int remains = created - deleted;
iw = new IndexWriter(dir, new WhitespaceAnalyzer(), false);
assertEquals(created, iw.docCount());
iw.close();
reader = IndexReader.open(dir);
assertEquals(created, reader.maxDoc());
assertEquals(remains, reader.numDocs());
for (int i = 10; i < created-7; i+=7) {
assertTrue("deleted: " + i, reader.isDeleted(i));
assertFalse("deleted+1: " + i, reader.isDeleted(i + 1));
assertFalse("deleted-1: " + i, reader.isDeleted(i - 1));
}
reader.close();
iw = new IndexWriter(dir, new WhitespaceAnalyzer(), false);
iw.optimize();
assertEquals(remains, iw.docCount());
iw.close();
reader = IndexReader.open(dir);
assertEquals(remains, reader.maxDoc());
assertEquals(remains, reader.numDocs());
reader.close();
}
private Document createTestDoc(String id) {
Document doc = new Document();
doc.add(Field.Keyword("keyword_id", id));
doc.add(Field.Text("text_id", id));
doc.add(Field.Keyword("keyword_text", "KeywordText"));
doc.add(Field.Text("text", "This is a text field"));
doc.add(Field.UnIndexed("unindexed", "This is some payload unindexed text"));
doc.add(Field.UnStored("unstored", "This is unstored text"));
return doc;
}
private void verifyDoc(Document doc, String id) {
assertEquals("keyword_id", doc.get("keyword_id"), id);
assertEquals("text_id", id);
assertEquals("keyword_text", doc.get("keyword_text"), "KeywordText");
assertEquals("text", doc.get("text"), "This is some payload unindexed text");
assertEquals("unindexed", doc.get("unindexed"), "This is some payload unindexed text");
assertNull("unstored", doc.get("unstored"));
}
} }

View File

@ -37,30 +37,33 @@ public class TestIndexWriter extends TestCase
assertEquals(100, writer.docCount()); assertEquals(100, writer.docCount());
writer.close(); writer.close();
// delete 50 documents // delete 40 documents
reader = IndexReader.open(dir); reader = IndexReader.open(dir);
for (i = 0; i < 50; i++) { for (i = 0; i < 40; i++) {
reader.delete(i); reader.delete(i);
} }
reader.close(); reader.close();
writer = new IndexWriter(dir, new WhitespaceAnalyzer(), false); // test doc count before segments are merged/index is optimized
writer = new IndexWriter(dir, new WhitespaceAnalyzer(), false);
assertEquals(100, writer.docCount()); assertEquals(100, writer.docCount());
writer.close(); writer.close();
reader = IndexReader.open(dir); reader = IndexReader.open(dir);
assertEquals(100, reader.maxDoc()); assertEquals(100, reader.maxDoc());
assertEquals(50, reader.numDocs()); assertEquals(60, reader.numDocs());
reader.close(); reader.close();
writer = new IndexWriter(dir, new WhitespaceAnalyzer(), false); // optimize the index and check that the new doc count is correct
writer = new IndexWriter(dir, new WhitespaceAnalyzer(), false);
writer.optimize(); writer.optimize();
assertEquals(50, writer.docCount()); assertEquals(60, writer.docCount());
writer.close(); writer.close();
// check that the index reader gives the same numbers.
reader = IndexReader.open(dir); reader = IndexReader.open(dir);
assertEquals(50, reader.maxDoc()); assertEquals(60, reader.maxDoc());
assertEquals(50, reader.numDocs()); assertEquals(60, reader.numDocs());
reader.close(); reader.close();
} }
catch (IOException e) { catch (IOException e) {

View File

@ -5,7 +5,6 @@ import java.io.IOException;
/** This class provides access to package-level features defined in the /** This class provides access to package-level features defined in the
* store package. It is used for testing only. * store package. It is used for testing only.
*/ */
public class _TestHelper { public class _TestHelper {
/** Returns true if the instance of the provided input stream is actually /** Returns true if the instance of the provided input stream is actually
@ -14,8 +13,8 @@ public class _TestHelper {
public static boolean isFSInputStream(InputStream is) { public static boolean isFSInputStream(InputStream is) {
return is instanceof FSInputStream; return is instanceof FSInputStream;
} }
/** Returns true if the provided input stream is an FSInputStream and /** Returns true if the provided input stream is an FSInputStream and
* is a clone, that is it does not own its underlying file descriptor. * is a clone, that is it does not own its underlying file descriptor.
*/ */
public static boolean isFSInputStreamClone(InputStream is) { public static boolean isFSInputStreamClone(InputStream is) {
@ -29,7 +28,7 @@ public class _TestHelper {
/** Given an instance of FSDirectory.FSInputStream, this method returns /** Given an instance of FSDirectory.FSInputStream, this method returns
* true if the underlying file descriptor is valid, and false otherwise. * true if the underlying file descriptor is valid, and false otherwise.
* This can be used to determine if the OS file has been closed. * This can be used to determine if the OS file has been closed.
* The descriptor becomes invalid when the non-clone instance of the * The descriptor becomes invalid when the non-clone instance of the
* FSInputStream that owns this descriptor is closed. However, the * FSInputStream that owns this descriptor is closed. However, the
* descriptor may possibly become invalid in other ways as well. * descriptor may possibly become invalid in other ways as well.
*/ */
@ -44,4 +43,4 @@ public class _TestHelper {
} }
} }
} }

View File

@ -196,7 +196,8 @@ public class TestBitVector extends TestCase
} }
/** /**
* Compare two BitVectors (really, this should be an equals method on the BitVector itself... * Compare two BitVectors.
* This should really be an equals method on the BitVector itself.
* @param bv One bit vector * @param bv One bit vector
* @param compare The second to compare * @param compare The second to compare
*/ */