mirror of https://github.com/apache/lucene.git
Misc code cleanups (#12974)
This commit is contained in:
parent
248f067d52
commit
1a939410dd
|
@ -16,12 +16,13 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.lucene.analysis.ja;
|
package org.apache.lucene.analysis.ja;
|
||||||
|
|
||||||
|
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||||
|
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.LineNumberReader;
|
import java.io.LineNumberReader;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import org.apache.lucene.analysis.Analyzer;
|
import org.apache.lucene.analysis.Analyzer;
|
||||||
|
@ -70,10 +71,9 @@ public class TestSearchMode extends BaseTokenStreamTestCase {
|
||||||
throw new FileNotFoundException(
|
throw new FileNotFoundException(
|
||||||
"Cannot find " + SEGMENTATION_FILENAME + " in test classpath");
|
"Cannot find " + SEGMENTATION_FILENAME + " in test classpath");
|
||||||
}
|
}
|
||||||
try {
|
try (is) {
|
||||||
LineNumberReader reader =
|
LineNumberReader reader = new LineNumberReader(new InputStreamReader(is, UTF_8));
|
||||||
new LineNumberReader(new InputStreamReader(is, StandardCharsets.UTF_8));
|
String line;
|
||||||
String line = null;
|
|
||||||
while ((line = reader.readLine()) != null) {
|
while ((line = reader.readLine()) != null) {
|
||||||
// Remove comments
|
// Remove comments
|
||||||
line = line.replaceAll("#.*$", "");
|
line = line.replaceAll("#.*$", "");
|
||||||
|
@ -98,10 +98,16 @@ public class TestSearchMode extends BaseTokenStreamTestCase {
|
||||||
expectedPosLengths[tokIDX] = 1;
|
expectedPosLengths[tokIDX] = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assertAnalyzesTo(analyzer, sourceText, expectedTokens, expectedPosIncrs);
|
assertAnalyzesTo(
|
||||||
|
analyzer,
|
||||||
|
sourceText,
|
||||||
|
expectedTokens,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
expectedPosIncrs,
|
||||||
|
expectedPosLengths);
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
is.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,10 +117,9 @@ public class TestSearchMode extends BaseTokenStreamTestCase {
|
||||||
throw new FileNotFoundException(
|
throw new FileNotFoundException(
|
||||||
"Cannot find " + SEGMENTATION_FILENAME + " in test classpath");
|
"Cannot find " + SEGMENTATION_FILENAME + " in test classpath");
|
||||||
}
|
}
|
||||||
try {
|
try (is) {
|
||||||
LineNumberReader reader =
|
LineNumberReader reader = new LineNumberReader(new InputStreamReader(is, UTF_8));
|
||||||
new LineNumberReader(new InputStreamReader(is, StandardCharsets.UTF_8));
|
String line;
|
||||||
String line = null;
|
|
||||||
while ((line = reader.readLine()) != null) {
|
while ((line = reader.readLine()) != null) {
|
||||||
// Remove comments
|
// Remove comments
|
||||||
line = line.replaceAll("#.*$", "");
|
line = line.replaceAll("#.*$", "");
|
||||||
|
@ -130,9 +135,9 @@ public class TestSearchMode extends BaseTokenStreamTestCase {
|
||||||
String[] tmpExpectedTokens = fields[1].split("\\s+");
|
String[] tmpExpectedTokens = fields[1].split("\\s+");
|
||||||
|
|
||||||
List<String> expectedTokenList = new ArrayList<>();
|
List<String> expectedTokenList = new ArrayList<>();
|
||||||
for (int tokIDX = 0; tokIDX < tmpExpectedTokens.length; tokIDX++) {
|
for (String expectedToken : tmpExpectedTokens) {
|
||||||
if (!tmpExpectedTokens[tokIDX].endsWith("/0")) {
|
if (!expectedToken.endsWith("/0")) {
|
||||||
expectedTokenList.add(tmpExpectedTokens[tokIDX]);
|
expectedTokenList.add(expectedToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,11 +150,13 @@ public class TestSearchMode extends BaseTokenStreamTestCase {
|
||||||
assertAnalyzesTo(
|
assertAnalyzesTo(
|
||||||
analyzerNoOriginal,
|
analyzerNoOriginal,
|
||||||
sourceText,
|
sourceText,
|
||||||
expectedTokenList.toArray(new String[expectedTokenList.size()]),
|
expectedTokenList.toArray(new String[0]),
|
||||||
expectedPosIncrs);
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
expectedPosIncrs,
|
||||||
|
expectedPosLengths);
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
is.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,15 +28,6 @@ final class PForUtil {
|
||||||
|
|
||||||
private static final int MAX_EXCEPTIONS = 7;
|
private static final int MAX_EXCEPTIONS = 7;
|
||||||
|
|
||||||
// IDENTITY_PLUS_ONE[i] == i + 1
|
|
||||||
private static final long[] IDENTITY_PLUS_ONE = new long[ForUtil.BLOCK_SIZE];
|
|
||||||
|
|
||||||
static {
|
|
||||||
for (int i = 0; i < ForUtil.BLOCK_SIZE; ++i) {
|
|
||||||
IDENTITY_PLUS_ONE[i] = i + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static boolean allEqual(long[] l) {
|
static boolean allEqual(long[] l) {
|
||||||
for (int i = 1; i < ForUtil.BLOCK_SIZE; ++i) {
|
for (int i = 1; i < ForUtil.BLOCK_SIZE; ++i) {
|
||||||
if (l[i] != l[0]) {
|
if (l[i] != l[0]) {
|
||||||
|
|
|
@ -55,7 +55,6 @@ import org.junit.Assume;
|
||||||
public class TestDirectoryReader extends LuceneTestCase {
|
public class TestDirectoryReader extends LuceneTestCase {
|
||||||
|
|
||||||
public void testDocument() throws IOException {
|
public void testDocument() throws IOException {
|
||||||
SegmentReader[] readers = new SegmentReader[2];
|
|
||||||
Directory dir = newDirectory();
|
Directory dir = newDirectory();
|
||||||
Document doc1 = new Document();
|
Document doc1 = new Document();
|
||||||
Document doc2 = new Document();
|
Document doc2 = new Document();
|
||||||
|
@ -80,8 +79,6 @@ public class TestDirectoryReader extends LuceneTestCase {
|
||||||
assertNotNull(vector);
|
assertNotNull(vector);
|
||||||
|
|
||||||
reader.close();
|
reader.close();
|
||||||
if (readers[0] != null) readers[0].close();
|
|
||||||
if (readers[1] != null) readers[1].close();
|
|
||||||
dir.close();
|
dir.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -482,11 +479,8 @@ public class TestDirectoryReader extends LuceneTestCase {
|
||||||
|
|
||||||
public void testOpenReaderAfterDelete() throws IOException {
|
public void testOpenReaderAfterDelete() throws IOException {
|
||||||
Path dirFile = createTempDir("deletetest");
|
Path dirFile = createTempDir("deletetest");
|
||||||
Directory dir = newFSDirectory(dirFile);
|
BaseDirectoryWrapper dir = newFSDirectory(dirFile);
|
||||||
if (dir instanceof BaseDirectoryWrapper) {
|
dir.setCheckIndexOnClose(false); // we will hit NoSuchFileException in MDW since we nuked it!
|
||||||
((BaseDirectoryWrapper) dir)
|
|
||||||
.setCheckIndexOnClose(false); // we will hit NoSuchFileException in MDW since we nuked it!
|
|
||||||
}
|
|
||||||
expectThrowsAnyOf(
|
expectThrowsAnyOf(
|
||||||
Arrays.asList(FileNotFoundException.class, NoSuchFileException.class),
|
Arrays.asList(FileNotFoundException.class, NoSuchFileException.class),
|
||||||
() -> DirectoryReader.open(dir));
|
() -> DirectoryReader.open(dir));
|
||||||
|
@ -702,7 +696,7 @@ public class TestDirectoryReader extends LuceneTestCase {
|
||||||
|
|
||||||
assertEquals(sis.getSegmentsFileName(), c.getSegmentsFileName());
|
assertEquals(sis.getSegmentsFileName(), c.getSegmentsFileName());
|
||||||
|
|
||||||
assertTrue(c.equals(r.getIndexCommit()));
|
assertEquals(c, r.getIndexCommit());
|
||||||
|
|
||||||
// Change the index
|
// Change the index
|
||||||
writer =
|
writer =
|
||||||
|
@ -717,8 +711,8 @@ public class TestDirectoryReader extends LuceneTestCase {
|
||||||
|
|
||||||
DirectoryReader r2 = DirectoryReader.openIfChanged(r);
|
DirectoryReader r2 = DirectoryReader.openIfChanged(r);
|
||||||
assertNotNull(r2);
|
assertNotNull(r2);
|
||||||
assertFalse(c.equals(r2.getIndexCommit()));
|
assertNotEquals(c, r2.getIndexCommit());
|
||||||
assertFalse(r2.getIndexCommit().getSegmentCount() == 1);
|
assertNotEquals(1, r2.getIndexCommit().getSegmentCount());
|
||||||
r2.close();
|
r2.close();
|
||||||
|
|
||||||
writer =
|
writer =
|
||||||
|
@ -753,11 +747,7 @@ public class TestDirectoryReader extends LuceneTestCase {
|
||||||
public void testNoDir() throws Throwable {
|
public void testNoDir() throws Throwable {
|
||||||
Path tempDir = createTempDir("doesnotexist");
|
Path tempDir = createTempDir("doesnotexist");
|
||||||
Directory dir = newFSDirectory(tempDir);
|
Directory dir = newFSDirectory(tempDir);
|
||||||
expectThrows(
|
expectThrows(IndexNotFoundException.class, () -> DirectoryReader.open(dir));
|
||||||
IndexNotFoundException.class,
|
|
||||||
() -> {
|
|
||||||
DirectoryReader.open(dir);
|
|
||||||
});
|
|
||||||
dir.close();
|
dir.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -779,7 +769,7 @@ public class TestDirectoryReader extends LuceneTestCase {
|
||||||
Collection<String> files = commit.getFileNames();
|
Collection<String> files = commit.getFileNames();
|
||||||
HashSet<String> seen = new HashSet<>();
|
HashSet<String> seen = new HashSet<>();
|
||||||
for (final String fileName : files) {
|
for (final String fileName : files) {
|
||||||
assertTrue("file " + fileName + " was duplicated", !seen.contains(fileName));
|
assertFalse("file " + fileName + " was duplicated", seen.contains(fileName));
|
||||||
seen.add(fileName);
|
seen.add(fileName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -970,13 +960,7 @@ public class TestDirectoryReader extends LuceneTestCase {
|
||||||
writer.commit();
|
writer.commit();
|
||||||
final DirectoryReader reader = DirectoryReader.open(writer);
|
final DirectoryReader reader = DirectoryReader.open(writer);
|
||||||
final int[] closeCount = new int[1];
|
final int[] closeCount = new int[1];
|
||||||
final IndexReader.ClosedListener listener =
|
final IndexReader.ClosedListener listener = key -> closeCount[0]++;
|
||||||
new IndexReader.ClosedListener() {
|
|
||||||
@Override
|
|
||||||
public void onClose(IndexReader.CacheKey key) {
|
|
||||||
closeCount[0]++;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
reader.getReaderCacheHelper().addClosedListener(listener);
|
reader.getReaderCacheHelper().addClosedListener(listener);
|
||||||
|
|
||||||
|
@ -1002,11 +986,7 @@ public class TestDirectoryReader extends LuceneTestCase {
|
||||||
DirectoryReader r = DirectoryReader.open(writer);
|
DirectoryReader r = DirectoryReader.open(writer);
|
||||||
writer.close();
|
writer.close();
|
||||||
r.storedFields().document(0);
|
r.storedFields().document(0);
|
||||||
expectThrows(
|
expectThrows(IllegalArgumentException.class, () -> r.storedFields().document(1));
|
||||||
IllegalArgumentException.class,
|
|
||||||
() -> {
|
|
||||||
r.storedFields().document(1);
|
|
||||||
});
|
|
||||||
r.close();
|
r.close();
|
||||||
dir.close();
|
dir.close();
|
||||||
}
|
}
|
||||||
|
@ -1044,9 +1024,9 @@ public class TestDirectoryReader extends LuceneTestCase {
|
||||||
r.decRef();
|
r.decRef();
|
||||||
r.close();
|
r.close();
|
||||||
|
|
||||||
for (int i = 0; i < threads.length; i++) {
|
for (IncThread thread : threads) {
|
||||||
threads[i].join();
|
thread.join();
|
||||||
assertNull(threads[i].failed);
|
assertNull(thread.failed);
|
||||||
}
|
}
|
||||||
assertFalse(r.tryIncRef());
|
assertFalse(r.tryIncRef());
|
||||||
writer.close();
|
writer.close();
|
||||||
|
|
|
@ -32,7 +32,6 @@ import java.util.Map;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
import org.apache.lucene.codecs.Codec;
|
import org.apache.lucene.codecs.Codec;
|
||||||
import org.apache.lucene.codecs.StoredFieldsFormat;
|
import org.apache.lucene.codecs.StoredFieldsFormat;
|
||||||
import org.apache.lucene.codecs.simpletext.SimpleTextCodec;
|
import org.apache.lucene.codecs.simpletext.SimpleTextCodec;
|
||||||
|
@ -136,12 +135,9 @@ public abstract class BaseStoredFieldsFormatTestCase extends BaseIndexFileFormat
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int field : fieldIDs) {
|
for (int field : fieldIDs) {
|
||||||
final String s;
|
|
||||||
if (rand.nextInt(4) != 3) {
|
if (rand.nextInt(4) != 3) {
|
||||||
s = TestUtil.randomUnicodeString(rand, 1000);
|
final String s = TestUtil.randomUnicodeString(rand, 1000);
|
||||||
doc.add(newField("f" + field, s, customType2));
|
doc.add(newField("f" + field, s, customType2));
|
||||||
} else {
|
|
||||||
s = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
w.addDocument(doc);
|
w.addDocument(doc);
|
||||||
|
@ -163,7 +159,7 @@ public abstract class BaseStoredFieldsFormatTestCase extends BaseIndexFileFormat
|
||||||
System.out.println("TEST: " + docs.size() + " docs in index; now load fields");
|
System.out.println("TEST: " + docs.size() + " docs in index; now load fields");
|
||||||
}
|
}
|
||||||
if (docs.size() > 0) {
|
if (docs.size() > 0) {
|
||||||
String[] idsList = docs.keySet().toArray(new String[docs.size()]);
|
String[] idsList = docs.keySet().toArray(new String[0]);
|
||||||
|
|
||||||
for (int x = 0; x < 2; x++) {
|
for (int x = 0; x < 2; x++) {
|
||||||
DirectoryReader r = maybeWrapWithMergingReader(w.getReader());
|
DirectoryReader r = maybeWrapWithMergingReader(w.getReader());
|
||||||
|
@ -326,6 +322,7 @@ public abstract class BaseStoredFieldsFormatTestCase extends BaseIndexFileFormat
|
||||||
assertTrue("got f=" + f, f instanceof StoredField);
|
assertTrue("got f=" + f, f instanceof StoredField);
|
||||||
assertEquals(docID, ids.nextDoc());
|
assertEquals(docID, ids.nextDoc());
|
||||||
assertEquals(answers[(int) ids.longValue()], f.numericValue());
|
assertEquals(answers[(int) ids.longValue()], f.numericValue());
|
||||||
|
assertEquals(typeAnswers[(int) ids.longValue()], f.numericValue().getClass());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
r.close();
|
r.close();
|
||||||
|
@ -458,7 +455,7 @@ public abstract class BaseStoredFieldsFormatTestCase extends BaseIndexFileFormat
|
||||||
readThreads.add(
|
readThreads.add(
|
||||||
new Thread() {
|
new Thread() {
|
||||||
|
|
||||||
int[] queries;
|
final int[] queries;
|
||||||
|
|
||||||
{
|
{
|
||||||
queries = new int[readsPerThread];
|
queries = new int[readsPerThread];
|
||||||
|
@ -729,7 +726,7 @@ public abstract class BaseStoredFieldsFormatTestCase extends BaseIndexFileFormat
|
||||||
assumeWorkingMMapOnWindows();
|
assumeWorkingMMapOnWindows();
|
||||||
|
|
||||||
// "big" as "much bigger than the chunk size"
|
// "big" as "much bigger than the chunk size"
|
||||||
// for this test we force a FS dir
|
// for this test we force an FS dir
|
||||||
// we can't just use newFSDirectory, because this test doesn't really index anything.
|
// we can't just use newFSDirectory, because this test doesn't really index anything.
|
||||||
// so if we get NRTCachingDir+SimpleText, we make massive stored fields and OOM (LUCENE-4484)
|
// so if we get NRTCachingDir+SimpleText, we make massive stored fields and OOM (LUCENE-4484)
|
||||||
Directory dir =
|
Directory dir =
|
||||||
|
@ -952,7 +949,7 @@ public abstract class BaseStoredFieldsFormatTestCase extends BaseIndexFileFormat
|
||||||
List<IndexableField> expectedFields =
|
List<IndexableField> expectedFields =
|
||||||
docs.get(testID).getFields().stream()
|
docs.get(testID).getFields().stream()
|
||||||
.filter(f -> f.fieldType().stored())
|
.filter(f -> f.fieldType().stored())
|
||||||
.collect(Collectors.toList());
|
.toList();
|
||||||
Document actualDoc = actualStoredFields.document(hits.scoreDocs[0].doc);
|
Document actualDoc = actualStoredFields.document(hits.scoreDocs[0].doc);
|
||||||
assertEquals(expectedFields.size(), actualDoc.getFields().size());
|
assertEquals(expectedFields.size(), actualDoc.getFields().size());
|
||||||
for (IndexableField expectedField : expectedFields) {
|
for (IndexableField expectedField : expectedFields) {
|
||||||
|
@ -1010,7 +1007,7 @@ public abstract class BaseStoredFieldsFormatTestCase extends BaseIndexFileFormat
|
||||||
|
|
||||||
/** Test realistic data, which typically compresses better than random data. */
|
/** Test realistic data, which typically compresses better than random data. */
|
||||||
public void testLineFileDocs() throws IOException {
|
public void testLineFileDocs() throws IOException {
|
||||||
// Use a FS dir and a non-randomized IWC to not slow down indexing
|
// Use an FS dir and a non-randomized IWC to not slow down indexing
|
||||||
try (Directory dir = newFSDirectory(createTempDir())) {
|
try (Directory dir = newFSDirectory(createTempDir())) {
|
||||||
try (LineFileDocs docs = new LineFileDocs(random());
|
try (LineFileDocs docs = new LineFileDocs(random());
|
||||||
IndexWriter w = new IndexWriter(dir, new IndexWriterConfig())) {
|
IndexWriter w = new IndexWriter(dir, new IndexWriterConfig())) {
|
||||||
|
|
Loading…
Reference in New Issue