From 6fe013aa7de7499f19af544a496233fc8b5306a0 Mon Sep 17 00:00:00 2001 From: Otis Gospodnetic Date: Mon, 13 Oct 2003 14:31:38 +0000 Subject: [PATCH] - Added a comment and cleaned up. git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150099 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/lucene/queryParser/QueryParser.jj | 2 +- .../org/apache/lucene/util/BitVector.java | 16 +- src/test/org/apache/lucene/SearchTest.java | 40 +- .../apache/lucene/index/TestCompoundFile.java | 401 +++++++++--------- .../apache/lucene/index/TestIndexWriter.java | 19 +- .../org/apache/lucene/store/_TestHelper.java | 9 +- .../org/apache/lucene/util/TestBitVector.java | 3 +- 7 files changed, 239 insertions(+), 251 deletions(-) diff --git a/src/java/org/apache/lucene/queryParser/QueryParser.jj b/src/java/org/apache/lucene/queryParser/QueryParser.jj index 02ea937f3f2..a25d004fc17 100644 --- a/src/java/org/apache/lucene/queryParser/QueryParser.jj +++ b/src/java/org/apache/lucene/queryParser/QueryParser.jj @@ -506,7 +506,7 @@ PARSER_END(QueryParser) } TOKEN : { -)+ ( "." (<_NUM_CHAR>)+ )? > : DEFAULT +)+ ( "." (<_NUM_CHAR>)+ )? > : DEFAULT } TOKEN : { diff --git a/src/java/org/apache/lucene/util/BitVector.java b/src/java/org/apache/lucene/util/BitVector.java index 280fe2d823e..828486294c0 100644 --- a/src/java/org/apache/lucene/util/BitVector.java +++ b/src/java/org/apache/lucene/util/BitVector.java @@ -62,11 +62,14 @@ import org.apache.lucene.store.OutputStream; /** Optimized implementation of a vector of bits. This is more-or-less like java.util.BitSet, but also includes the following: -
    -
  • a count() method, which efficiently computes the number of one bits;
  • -
  • optimized read from and write to disk;
  • -
  • inlinable get() method;
  • -
+
    +
  • a count() method, which efficiently computes the number of one bits;
  • +
  • optimized read from and write to disk;
  • +
  • inlinable get() method;
  • +
+ + @author Doug Cutting + @version $Id$ */ public final class BitVector { @@ -108,11 +111,12 @@ public final class BitVector { computed and cached, so that, if the vector is not changed, no recomputation is done for repeated calls. */ public final int count() { + // if the vector has been modified if (count == -1) { int c = 0; int end = bits.length; 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; } return count; diff --git a/src/test/org/apache/lucene/SearchTest.java b/src/test/org/apache/lucene/SearchTest.java index 2eb291ec725..dee78f190b1 100644 --- a/src/test/org/apache/lucene/SearchTest.java +++ b/src/test/org/apache/lucene/SearchTest.java @@ -71,18 +71,18 @@ class SearchTest { IndexWriter writer = new IndexWriter(directory, analyzer, true); String[] docs = { - "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 c e", - "e c a", - "a c e a c e", - "a c e a b c" + "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 c e", + "e c a", + "a c e a c e", + "a c e a b c" }; for (int j = 0; j < docs.length; j++) { - Document d = new Document(); - d.add(Field.Text("contents", docs[j])); - writer.addDocument(d); + Document d = new Document(); + d.add(Field.Text("contents", docs[j])); + writer.addDocument(d); } writer.close(); @@ -94,30 +94,30 @@ class SearchTest { // "\"a b c\"", // "a c", // "\"a c\"", - "\"a c e\"", + "\"a c e\"", }; Hits hits = null; QueryParser parser = new QueryParser("contents", analyzer); parser.setPhraseSlop(4); for (int j = 0; j < queries.length; j++) { - Query query = parser.parse(queries[j]); - System.out.println("Query: " + query.toString("contents")); + Query query = parser.parse(queries[j]); + System.out.println("Query: " + query.toString("contents")); //DateFilter filter = // new DateFilter("modified", Time(1997,0,1), Time(1998,0,1)); //DateFilter filter = DateFilter.Before("modified", Time(1997,00,01)); //System.out.println(filter); - hits = searcher.search(query); + hits = searcher.search(query); - System.out.println(hits.length() + " total results"); - for (int i = 0 ; i < hits.length() && i < 10; i++) { - Document d = hits.doc(i); - System.out.println(i + " " + hits.score(i) + System.out.println(hits.length() + " total results"); + for (int i = 0 ; i < hits.length() && i < 10; i++) { + Document d = hits.doc(i); + System.out.println(i + " " + hits.score(i) // + " " + DateField.stringToDate(d.get("modified")) - + " " + d.get("contents")); - } + + " " + d.get("contents")); + } } searcher.close(); diff --git a/src/test/org/apache/lucene/index/TestCompoundFile.java b/src/test/org/apache/lucene/index/TestCompoundFile.java index 8a7cf958049..c6fd731fb9c 100644 --- a/src/test/org/apache/lucene/index/TestCompoundFile.java +++ b/src/test/org/apache/lucene/index/TestCompoundFile.java @@ -1,5 +1,59 @@ 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 + * . + */ + import java.io.IOException; import junit.framework.TestCase; @@ -36,8 +90,8 @@ public class TestCompoundFile extends TestCase // TestRunner.run (new TestCompoundFile("testIWCreate")); } - - + + public TestCompoundFile() { super(); } @@ -45,16 +99,16 @@ public class TestCompoundFile extends TestCase public TestCompoundFile(String name) { super(name); } - + private Directory dir; - - + + public void setUp() throws IOException { //dir = new RAMDirectory(); - dir = FSDirectory.getDirectory("testIndex", true); + dir = FSDirectory.getDirectory("testIndex", true); } - - + + /** Creates a file of the specified size with random data. */ private void createRandomFile(Directory dir, String name, int size) throws IOException @@ -66,15 +120,15 @@ public class TestCompoundFile extends TestCase } os.close(); } - + /** Creates a file of the specified size with sequential data. The first * byte is written as the start byte provided. All subsequent bytes are * computed as start + offset where offset is the number of the byte. */ - private void createSequenceFile(Directory dir, - String name, - byte start, - int size) + private void createSequenceFile(Directory dir, + String name, + byte start, + int size) throws IOException { OutputStream os = dir.createFile(name); @@ -84,35 +138,35 @@ public class TestCompoundFile extends TestCase } os.close(); } - - - private void assertSameStreams(String msg, - InputStream expected, + + + private void assertSameStreams(String msg, + InputStream expected, InputStream test) throws IOException { assertNotNull(msg + " null expected", expected); assertNotNull(msg + " null test", test); assertEquals(msg + " length", expected.length(), test.length()); - assertEquals(msg + " position", expected.getFilePointer(), + assertEquals(msg + " position", expected.getFilePointer(), test.getFilePointer()); - + byte expectedBuffer[] = new byte[512]; byte testBuffer[] = new byte[expectedBuffer.length]; - + long remainder = expected.length() - expected.getFilePointer(); while(remainder > 0) { int readLen = (int) Math.min(remainder, expectedBuffer.length); expected.readBytes(expectedBuffer, 0, readLen); test.readBytes(testBuffer, 0, readLen); - assertEqualArrays(msg + ", remainder " + remainder, expectedBuffer, + assertEqualArrays(msg + ", remainder " + remainder, expectedBuffer, testBuffer, 0, readLen); remainder -= readLen; } } - private void assertSameStreams(String msg, + private void assertSameStreams(String msg, InputStream expected, InputStream actual, long seekTo) @@ -122,30 +176,30 @@ public class TestCompoundFile extends TestCase try { actual.seek(seekTo); fail(msg + ", " + seekTo + ", negative seek"); - } catch (IOException e) { - /* success */ + } catch (IOException e) { + /* success */ //System.out.println("SUCCESS: Negative seek: " + e); } - + } else if (seekTo > 0 && seekTo >= expected.length()) { try { actual.seek(seekTo); fail(msg + ", " + seekTo + ", seek past EOF"); - } catch (IOException e) { - /* success */ + } catch (IOException e) { + /* success */ //System.out.println("SUCCESS: Seek past EOF: " + e); } - + } else { expected.seek(seekTo); actual.seek(seekTo); assertSameStreams(msg + ", seek(mid)", expected, actual); } } - - - - private void assertSameSeekBehavior(String msg, + + + + private void assertSameSeekBehavior(String msg, InputStream expected, InputStream actual) throws IOException @@ -153,52 +207,52 @@ public class TestCompoundFile extends TestCase // seek to 0 long point = 0; assertSameStreams(msg + ", seek(0)", expected, actual, point); - + // seek to middle point = expected.length() / 2l; assertSameStreams(msg + ", seek(mid)", expected, actual, point); - + // seek to end - 2 point = expected.length() - 2; assertSameStreams(msg + ", seek(end-2)", expected, actual, point); - + // seek to end - 1 point = expected.length() - 1; assertSameStreams(msg + ", seek(end-1)", expected, actual, point); - + // seek to the end point = expected.length(); assertSameStreams(msg + ", seek(end)", expected, actual, point); - + // seek past end point = expected.length() + 1; assertSameStreams(msg + ", seek(end+1)", expected, actual, point); } - - - private void assertEqualArrays(String msg, - byte[] expected, - byte[] test, - int start, - int len) + + + private void assertEqualArrays(String msg, + byte[] expected, + byte[] test, + int start, + int len) { assertNotNull(msg + " null expected", expected); assertNotNull(msg + " null test", test); - + for (int i=start; i