| 1 |
|
package org.apache.lucene.index; |
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
import org.apache.lucene.store.Directory; |
| 19 |
|
import org.apache.lucene.store.RAMDirectory; |
| 20 |
|
import org.apache.lucene.analysis.standard.StandardAnalyzer; |
| 21 |
|
import org.apache.lucene.document.Document; |
| 22 |
|
import org.apache.lucene.document.Field; |
| 23 |
|
import junit.framework.TestCase; |
| 24 |
|
|
| 25 |
|
import java.io.IOException; |
| 26 |
|
|
| 27 |
|
|
|
|
|
| 85.7% |
Uncovered Elements: 7 (49) |
Complexity: 8 |
Complexity Density: 0.22 |
|
| 28 |
|
public class TestIndexWriterMerging extends TestCase |
| 29 |
|
{ |
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
|
|
|
| 82.6% |
Uncovered Elements: 4 (23) |
Complexity: 3 |
Complexity Density: 0.16 |
1
PASS
|
|
| 35 |
1
|
public void testLucene() throws IOException... |
| 36 |
|
{ |
| 37 |
|
|
| 38 |
1
|
int num=100; |
| 39 |
|
|
| 40 |
1
|
Directory indexA = new RAMDirectory(); |
| 41 |
1
|
Directory indexB = new RAMDirectory(); |
| 42 |
|
|
| 43 |
1
|
fillIndex(indexA, 0, num); |
| 44 |
1
|
boolean fail = verifyIndex(indexA, 0); |
| 45 |
1
|
if (fail) |
| 46 |
|
{ |
| 47 |
0
|
fail("Index a is invalid"); |
| 48 |
|
} |
| 49 |
|
|
| 50 |
1
|
fillIndex(indexB, num, num); |
| 51 |
1
|
fail = verifyIndex(indexB, num); |
| 52 |
1
|
if (fail) |
| 53 |
|
{ |
| 54 |
0
|
fail("Index b is invalid"); |
| 55 |
|
} |
| 56 |
|
|
| 57 |
1
|
Directory merged = new RAMDirectory(); |
| 58 |
|
|
| 59 |
1
|
IndexWriter writer = new IndexWriter(merged, new StandardAnalyzer(), true); |
| 60 |
1
|
writer.setMergeFactor(2); |
| 61 |
|
|
| 62 |
1
|
writer.addIndexes(new Directory[]{indexA, indexB}); |
| 63 |
1
|
writer.close(); |
| 64 |
|
|
| 65 |
1
|
fail = verifyIndex(merged, 0); |
| 66 |
1
|
merged.close(); |
| 67 |
|
|
| 68 |
1
|
assertFalse("The merged index is invalid", fail); |
| 69 |
|
} |
| 70 |
|
|
|
|
|
| 76.9% |
Uncovered Elements: 3 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
| 71 |
3
|
private boolean verifyIndex(Directory directory, int startAt) throws IOException... |
| 72 |
|
{ |
| 73 |
3
|
boolean fail = false; |
| 74 |
3
|
IndexReader reader = IndexReader.open(directory); |
| 75 |
|
|
| 76 |
3
|
int max = reader.maxDoc(); |
| 77 |
403
|
for (int i = 0; i < max; i++) |
| 78 |
|
{ |
| 79 |
400
|
Document temp = reader.document(i); |
| 80 |
|
|
| 81 |
|
|
| 82 |
400
|
if (!temp.getField("count").stringValue().equals((i + startAt) + "")) |
| 83 |
|
{ |
| 84 |
0
|
fail = true; |
| 85 |
0
|
System.out.println("Document " + (i + startAt) + " is returning document " + temp.getField("count").stringValue()); |
| 86 |
|
} |
| 87 |
|
} |
| 88 |
3
|
return fail; |
| 89 |
|
} |
| 90 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
| 91 |
2
|
private void fillIndex(Directory dir, int start, int numDocs) throws IOException... |
| 92 |
|
{ |
| 93 |
|
|
| 94 |
2
|
IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true); |
| 95 |
2
|
writer.setMergeFactor(2); |
| 96 |
2
|
writer.setMaxBufferedDocs(2); |
| 97 |
|
|
| 98 |
202
|
for (int i = start; i < (start + numDocs); i++) |
| 99 |
|
{ |
| 100 |
200
|
Document temp = new Document(); |
| 101 |
200
|
temp.add(new Field("count", (""+i), Field.Store.YES, Field.Index.UN_TOKENIZED)); |
| 102 |
|
|
| 103 |
200
|
writer.addDocument(temp); |
| 104 |
|
} |
| 105 |
2
|
writer.close(); |
| 106 |
|
} |
| 107 |
|
} |