| 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 junit.framework.TestCase; |
| 20 |
|
import org.apache.lucene.analysis.Analyzer; |
| 21 |
|
import org.apache.lucene.analysis.SimpleAnalyzer; |
| 22 |
|
import org.apache.lucene.analysis.standard.StandardAnalyzer; |
| 23 |
|
import org.apache.lucene.document.Document; |
| 24 |
|
import org.apache.lucene.document.Field; |
| 25 |
|
import org.apache.lucene.document.Field.Index; |
| 26 |
|
import org.apache.lucene.document.Field.Store; |
| 27 |
|
import org.apache.lucene.store.Directory; |
| 28 |
|
import org.apache.lucene.store.FSDirectory; |
| 29 |
|
import org.apache.lucene.store.RAMDirectory; |
| 30 |
|
|
| 31 |
|
import java.io.File; |
| 32 |
|
import java.io.IOException; |
| 33 |
|
import java.util.EmptyStackException; |
| 34 |
|
import java.util.Random; |
| 35 |
|
import java.util.Stack; |
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
@author |
| 42 |
|
|
|
|
|
| 95.5% |
Uncovered Elements: 5 (111) |
Complexity: 13 |
Complexity Density: 0.13 |
|
| 43 |
|
public class TestIndexModifier extends TestCase { |
| 44 |
|
|
| 45 |
|
private int docCount = 0; |
| 46 |
|
|
| 47 |
|
private final Term allDocTerm = new Term("all", "x"); |
| 48 |
|
|
|
|
|
| 98% |
Uncovered Elements: 1 (51) |
Complexity: 2 |
Complexity Density: 0.04 |
1
PASS
|
|
| 49 |
1
|
public void testIndex() throws IOException {... |
| 50 |
1
|
Directory ramDir = new RAMDirectory(); |
| 51 |
1
|
IndexModifier i = new IndexModifier(ramDir, new StandardAnalyzer(), true); |
| 52 |
1
|
i.addDocument(getDoc()); |
| 53 |
1
|
assertEquals(1, i.docCount()); |
| 54 |
1
|
i.flush(); |
| 55 |
1
|
i.addDocument(getDoc(), new SimpleAnalyzer()); |
| 56 |
1
|
assertEquals(2, i.docCount()); |
| 57 |
1
|
i.optimize(); |
| 58 |
1
|
assertEquals(2, i.docCount()); |
| 59 |
1
|
i.flush(); |
| 60 |
1
|
i.deleteDocument(0); |
| 61 |
1
|
assertEquals(1, i.docCount()); |
| 62 |
1
|
i.flush(); |
| 63 |
1
|
assertEquals(1, i.docCount()); |
| 64 |
1
|
i.addDocument(getDoc()); |
| 65 |
1
|
i.addDocument(getDoc()); |
| 66 |
1
|
i.flush(); |
| 67 |
|
|
| 68 |
1
|
i.deleteDocuments(allDocTerm); |
| 69 |
1
|
assertEquals(0, i.docCount()); |
| 70 |
1
|
i.optimize(); |
| 71 |
1
|
assertEquals(0, i.docCount()); |
| 72 |
|
|
| 73 |
|
|
| 74 |
1
|
assertNull(i.getInfoStream()); |
| 75 |
1
|
assertTrue(i.getUseCompoundFile()); |
| 76 |
1
|
assertEquals(10, i.getMaxBufferedDocs()); |
| 77 |
1
|
assertEquals(10000, i.getMaxFieldLength()); |
| 78 |
1
|
assertEquals(10, i.getMergeFactor()); |
| 79 |
|
|
| 80 |
1
|
i.setMaxBufferedDocs(100); |
| 81 |
1
|
i.setMergeFactor(25); |
| 82 |
1
|
i.setMaxFieldLength(250000); |
| 83 |
1
|
i.addDocument(getDoc()); |
| 84 |
1
|
i.setUseCompoundFile(false); |
| 85 |
1
|
i.flush(); |
| 86 |
1
|
assertEquals(100, i.getMaxBufferedDocs()); |
| 87 |
1
|
assertEquals(25, i.getMergeFactor()); |
| 88 |
1
|
assertEquals(250000, i.getMaxFieldLength()); |
| 89 |
1
|
assertFalse(i.getUseCompoundFile()); |
| 90 |
|
|
| 91 |
|
|
| 92 |
1
|
i.deleteDocuments(allDocTerm); |
| 93 |
1
|
i.setMaxBufferedDocs(100); |
| 94 |
1
|
i.setMergeFactor(25); |
| 95 |
1
|
i.setMaxFieldLength(250000); |
| 96 |
1
|
i.addDocument(getDoc()); |
| 97 |
1
|
i.setUseCompoundFile(false); |
| 98 |
1
|
i.optimize(); |
| 99 |
1
|
assertEquals(100, i.getMaxBufferedDocs()); |
| 100 |
1
|
assertEquals(25, i.getMergeFactor()); |
| 101 |
1
|
assertEquals(250000, i.getMaxFieldLength()); |
| 102 |
1
|
assertFalse(i.getUseCompoundFile()); |
| 103 |
|
|
| 104 |
1
|
i.close(); |
| 105 |
1
|
try { |
| 106 |
1
|
i.docCount(); |
| 107 |
0
|
fail(); |
| 108 |
|
} catch (IllegalStateException e) { |
| 109 |
|
|
| 110 |
|
} |
| 111 |
|
} |
| 112 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1
PASS
|
|
| 113 |
1
|
public void testExtendedIndex() throws IOException {... |
| 114 |
1
|
Directory ramDir = new RAMDirectory(); |
| 115 |
1
|
PowerIndex powerIndex = new PowerIndex(ramDir, new StandardAnalyzer(), true); |
| 116 |
1
|
powerIndex.addDocument(getDoc()); |
| 117 |
1
|
powerIndex.addDocument(getDoc()); |
| 118 |
1
|
powerIndex.addDocument(getDoc()); |
| 119 |
1
|
powerIndex.addDocument(getDoc()); |
| 120 |
1
|
powerIndex.addDocument(getDoc()); |
| 121 |
1
|
powerIndex.flush(); |
| 122 |
1
|
assertEquals(5, powerIndex.docFreq(allDocTerm)); |
| 123 |
1
|
powerIndex.close(); |
| 124 |
|
} |
| 125 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
| 126 |
11
|
private Document getDoc() {... |
| 127 |
11
|
Document doc = new Document(); |
| 128 |
11
|
doc.add(new Field("body", new Integer(docCount).toString(), Field.Store.YES, Field.Index.UN_TOKENIZED)); |
| 129 |
11
|
doc.add(new Field("all", "x", Field.Store.YES, Field.Index.UN_TOKENIZED)); |
| 130 |
11
|
docCount++; |
| 131 |
11
|
return doc; |
| 132 |
|
} |
| 133 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1
PASS
|
|
| 134 |
1
|
public void testIndexWithThreads() throws IOException {... |
| 135 |
1
|
testIndexInternal(0); |
| 136 |
1
|
testIndexInternal(10); |
| 137 |
1
|
testIndexInternal(50); |
| 138 |
|
} |
| 139 |
|
|
|
|
|
| 86.7% |
Uncovered Elements: 4 (30) |
Complexity: 6 |
Complexity Density: 0.23 |
|
| 140 |
3
|
private void testIndexInternal(int maxWait) throws IOException {... |
| 141 |
3
|
final boolean create = true; |
| 142 |
|
|
| 143 |
|
|
| 144 |
3
|
String tempDir = System.getProperty("java.io.tmpdir"); |
| 145 |
3
|
if (tempDir == null) |
| 146 |
0
|
throw new IOException("java.io.tmpdir undefined, cannot run test"); |
| 147 |
3
|
File indexDir = new File(tempDir, "lucenetestindex"); |
| 148 |
3
|
Directory rd = FSDirectory.getDirectory(indexDir, create); |
| 149 |
3
|
IndexThread.id = 0; |
| 150 |
3
|
IndexThread.idStack.clear(); |
| 151 |
3
|
IndexModifier index = new IndexModifier(rd, new StandardAnalyzer(), create); |
| 152 |
3
|
IndexThread thread1 = new IndexThread(index, maxWait, 1); |
|