| 1 |
|
package org.apache.lucene.index; |
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 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 |
|
|
| 36 |
|
|
| 37 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (39) |
Complexity: 7 |
Complexity Density: 0.24 |
|
| 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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (24) |
Complexity: 4 |
Complexity Density: 0.22 |
|
| 47 |
2
|
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 |
|
|
| 58 |
15
|
content = this.term1 + " " + this.term2; |
| 59 |
985
|
} else if (i % 15 == 0) { |
| 60 |
|
|
| 61 |
62
|
content = this.term1 + " " + this.term1; |
| 62 |
|
} else { |
| 63 |
|
|
| 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 |
|
|
| 72 |
2
|
writer.optimize(); |
| 73 |
2
|
writer.close(); |
| 74 |
|
|
| 75 |
|
|
| 76 |
2
|
SegmentReader reader = (SegmentReader) IndexReader.open(directory); |
| 77 |
|
|
| 78 |
|
|
| 79 |
2
|
reader.proxStream = new SeeksCountingStream(reader.proxStream); |
| 80 |
|
|
| 81 |
2
|
this.searcher = new IndexSearcher(reader); |
| 82 |
|
} |
| 83 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 84 |
2
|
private Hits search() throws IOException {... |
| 85 |
|
|
| 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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
| 92 |
2
|
private void performTest(int numHits) throws IOException {... |
| 93 |
2
|
createIndex(numHits); |
| 94 |
2
|
this.seeksCounter = 0; |
| 95 |
2
|
Hits hits = search(); |
| 96 |
|
|
| 97 |
2
|
assertEquals(numHits, hits.length()); |
| 98 |
|
|
| 99 |
|
|
| 100 |
2
|
assertEquals(numHits, this.seeksCounter); |
| 101 |
|
} |
| 102 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1
PASS
|
|
| 103 |
1
|
public void testLazySkipping() throws IOException {... |
| 104 |
|
|
| 105 |
1
|
performTest(5); |
| 106 |
1
|
performTest(10); |
| 107 |
|
} |
| 108 |
|
|
| 109 |
|
|
| 110 |
|
|
| 111 |
|
|
|
|
|
| 64.7% |
Uncovered Elements: 6 (17) |
Complexity: 8 |
Complexity Density: 0.89 |
|
| 112 |
|
class SeeksCountingStream extends IndexInput { |
| 113 |
|
private IndexInput input; |
| 114 |
|
|
| 115 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 116 |
6
|
SeeksCountingStream(IndexInput input) {... |
| 117 |
6
|
this.input = input; |
| 118 |
|
} |
| 119 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 120 |
287
|
public byte readByte() throws IOException {... |
| 121 |
287
|
return this.input.readByte(); |
| 122 |
|
} |
| 123 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 124 |
0
|
public void readBytes(byte[] b, int offset, int len) throws IOException {... |
| 125 |
0
|
this.input.readBytes(b, offset, len); |
| 126 |
|
} |
| 127 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 128 |
2
|
public void close() throws IOException {... |
| 129 |
2
|
this.input.close(); |
| 130 |
|
} |
| 131 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 132 |
0
|
public long getFilePointer() {... |
| 133 |
0
|
return this.input.getFilePointer(); |
| 134 |
|
} |
| 135 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 136 |
15
|
public void seek(long pos) throws IOException {... |
| 137 |
15
|
TestLazyProxSkipping.this.seeksCounter++; |
| 138 |
15
|
this.input.seek(pos); |
| 139 |
|
} |
| 140 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 141 |
0
|
public long length() {... |
| 142 |
0
|
return this.input.length(); |
| 143 |
|
} |
| 144 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 145 |
4
|
public Object clone() {... |
| 146 |
4
|
return new SeeksCountingStream((IndexInput) this.input.clone()); |
| 147 |
|
} |
| 148 |
|
|
| 149 |
|
} |
| 150 |
|
} |