Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
38   150   15   3.17
6   89   0.39   6
12     1.25  
2    
 
  TestLazyProxSkipping       Line # 38 29 7 100% 1.0
  TestLazyProxSkipping.SeeksCountingStream       Line # 112 9 8 64.7% 0.64705884
 
  (1)
 
1    package org.apache.lucene.index;
2   
3    /**
4    * Copyright 2006 The Apache Software Foundation
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10    * http://www.apache.org/licenses/LICENSE-2.0
11    *
12    * Unless required by applicable law or agreed to in writing, software
13    * distributed under the License is distributed on an "AS IS" BASIS,
14    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    * See the License for the specific language governing permissions and
16    * limitations under the License.
17    */
18   
19    import java.io.IOException;
20   
21    import org.apache.lucene.analysis.WhitespaceAnalyzer;
22    import org.apache.lucene.document.Document;
23    import org.apache.lucene.document.Field;
24    import org.apache.lucene.search.Hits;
25    import org.apache.lucene.search.IndexSearcher;
26    import org.apache.lucene.search.PhraseQuery;
27    import org.apache.lucene.search.Searcher;
28    import org.apache.lucene.store.Directory;
29    import org.apache.lucene.store.IndexInput;
30    import org.apache.lucene.store.RAMDirectory;
31   
32    import junit.framework.TestCase;
33   
34    /**
35    * Tests lazy skipping on the proximity file.
36    *
37    */
 
38    public class TestLazyProxSkipping extends TestCase {
39    private Searcher searcher;
40    private int seeksCounter = 0;
41   
42    private String field = "tokens";
43    private String term1 = "xx";
44    private String term2 = "yy";
45    private String term3 = "zz";
46   
 
47  2 toggle private void createIndex(int numHits) throws IOException {
48  2 int numDocs = 500;
49   
50  2 Directory directory = new RAMDirectory();
51  2 IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true);
52   
53  1002 for (int i = 0; i < numDocs; i++) {
54  1000 Document doc = new Document();
55  1000 String content;
56  1000 if (i % (numDocs / numHits) == 0) {
57    // add a document that matches the query "term1 term2"
58  15 content = this.term1 + " " + this.term2;
59  985 } else if (i % 15 == 0) {
60    // add a document that only contains term1
61  62 content = this.term1 + " " + this.term1;
62    } else {
63    // add a document that contains term2 but not term 1
64  923 content = this.term3 + " " + this.term2;
65    }
66   
67  1000 doc.add(new Field(this.field, content, Field.Store.YES, Field.Index.TOKENIZED));
68  1000 writer.addDocument(doc);
69    }
70   
71    // make sure the index has only a single segment
72  2 writer.optimize();
73  2 writer.close();
74   
75    // the index is a single segment, thus IndexReader.open() returns an instance of SegmentReader
76  2 SegmentReader reader = (SegmentReader) IndexReader.open(directory);
77   
78    // we decorate the proxStream with a wrapper class that allows to count the number of calls of seek()
79  2 reader.proxStream = new SeeksCountingStream(reader.proxStream);
80   
81  2 this.searcher = new IndexSearcher(reader);
82    }
83   
 
84  2 toggle private Hits search() throws IOException {
85    // create PhraseQuery "term1 term2" and search
86  2 PhraseQuery pq = new PhraseQuery();
87  2 pq.add(new Term(this.field, this.term1));
88  2 pq.add(new Term(this.field, this.term2));
89  2 return this.searcher.search(pq);
90    }
91   
 
92  2 toggle private void performTest(int numHits) throws IOException {
93  2 createIndex(numHits);
94  2 this.seeksCounter = 0;
95  2 Hits hits = search();
96    // verify that the right number of docs was found
97  2 assertEquals(numHits, hits.length());
98   
99    // check if the number of calls of seek() does not exceed the number of hits
100  2 assertEquals(numHits, this.seeksCounter);
101    }
102   
 
103  1 toggle public void testLazySkipping() throws IOException {
104    // test whether only the minimum amount of seeks() are performed
105  1 performTest(5);
106  1 performTest(10);
107    }
108   
109   
110    // Simply extends IndexInput in a way that we are able to count the number
111    // of invocations of seek()
 
112    class SeeksCountingStream extends IndexInput {
113    private IndexInput input;
114   
115   
 
116  6 toggle SeeksCountingStream(IndexInput input) {
117  6 this.input = input;
118    }
119   
 
120  287 toggle public byte readByte() throws IOException {
121  287 return this.input.readByte();
122    }
123   
 
124  0 toggle public void readBytes(byte[] b, int offset, int len) throws IOException {
125  0 this.input.readBytes(b, offset, len);
126    }
127   
 
128  2 toggle public void close() throws IOException {
129  2 this.input.close();
130    }
131   
 
132  0 toggle public long getFilePointer() {
133  0 return this.input.getFilePointer();
134    }
135   
 
136  15 toggle public void seek(long pos) throws IOException {
137  15 TestLazyProxSkipping.this.seeksCounter++;
138  15 this.input.seek(pos);
139    }
140   
 
141  0 toggle public long length() {
142  0 return this.input.length();
143    }
144   
 
145  4 toggle public Object clone() {
146  4 return new SeeksCountingStream((IndexInput) this.input.clone());
147    }
148   
149    }
150    }