Apache Solr open-source search software
LUCENE-5698 - forbidden APIs related fixes
Tommaso Teofili6df32fbc35c4
diff --git a/lucene/classification/src/test/org/apache/lucene/classification/Test20NewsgroupsClassification.java b/lucene/classification/src/test/org/apache/lucene/classification/Test20NewsgroupsClassification.javaindex 407dcb6c90d..92d93590721 100644--- a/lucene/classification/src/test/org/apache/lucene/classification/Test20NewsgroupsClassification.java+++ b/lucene/classification/src/test/org/apache/lucene/classification/Test20NewsgroupsClassification.java@@ -19,8 +19,8 @@ package org.apache.lucene.classification;import java.io.Closeable;-import java.io.File;import java.io.IOException;+import java.nio.file.DirectoryStream;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;@@ -28,8 +28,10 @@ import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.concurrent.ExecutorService;-import java.util.concurrent.Executors;import java.util.concurrent.Future;+import java.util.concurrent.LinkedBlockingQueue;+import java.util.concurrent.ThreadPoolExecutor;+import java.util.concurrent.TimeUnit;import java.util.stream.Stream;import com.carrotsearch.randomizedtesting.annotations.TimeoutSuite;@@ -65,6 +67,8 @@ import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.util.BytesRef;import org.apache.lucene.util.LuceneTestCase;+import org.apache.lucene.util.NamedThreadFactory;+import org.apache.lucene.util.TestUtil;import org.apache.lucene.util.TimeUnits;import org.junit.Test;@@ -132,15 +136,15 @@ public final class Test20NewsgroupsClassification extends LuceneTestCase {Analyzer analyzer = new StandardAnalyzer();if (index) {- System.out.format("Indexing 20 Newsgroups...%n");+ System.out.println("Indexing 20 Newsgroups...");long startIndex = System.currentTimeMillis();IndexWriter indexWriter = new IndexWriter(directory, new IndexWriterConfig(analyzer));- int docsIndexed = buildIndex(new File(PATH_TO_20N), indexWriter);+ int docsIndexed = buildIndex(Paths.get(PATH_TO_20N).getParent(), indexWriter);long endIndex = System.currentTimeMillis();- System.out.format("Indexed %d pages in %ds %n", docsIndexed, (endIndex - startIndex) / 1000);+ System.out.println("Indexed " + docsIndexed + " docs in " + (endIndex - startIndex) / 1000 + "s");indexWriter.close();@@ -154,7 +158,7 @@ public final class Test20NewsgroupsClassification extends LuceneTestCase {if (index && split) {// split the index- System.out.format("Splitting the index...%n");+ System.out.println("Splitting the index...");long startSplit = System.currentTimeMillis();DatasetSplitter datasetSplitter = new DatasetSplitter(0.2, 0);@@ -162,12 +166,9 @@ public final class Test20NewsgroupsClassification extends LuceneTestCase {reader.close();reader = DirectoryReader.open(train); // using the train index from now onlong endSplit = System.currentTimeMillis();- System.out.format("Splitting done in %ds %n", (endSplit - startSplit) / 1000);+ System.out.println("Splitting done in " + (endSplit - startSplit) / 1000 + "s");}- final long startTime = System.currentTimeMillis();--classifiers.add(new KNearestNeighborClassifier(reader, new ClassicSimilarity(), analyzer, null, 1, 0, 0, CATEGORY_FIELD, BODY_FIELD));classifiers.add(new KNearestNeighborClassifier(reader, null, analyzer, null, 1, 0, 0, CATEGORY_FIELD, BODY_FIELD));classifiers.add(new KNearestNeighborClassifier(reader, new ClassicSimilarity(), analyzer, null, 3, 0, 0, CATEGORY_FIELD, BODY_FIELD));@@ -200,12 +201,14 @@ public final class Test20NewsgroupsClassification extends LuceneTestCase {maxdoc = reader.maxDoc();}- System.out.format("Starting evaluation on %d docs...%n", maxdoc);+ System.out.println("Starting evaluation on " + maxdoc + " docs...");- ExecutorService service = Executors.newCachedThreadPool();+ ExecutorService service = new ThreadPoolExecutor(1, TestUtil.nextInt(random(), 2, 6), Long.MAX_VALUE, TimeUnit.MILLISECONDS,+ new LinkedBlockingQueue<>(),+ new NamedThreadFactory(getClass().getName()));List<Future<String>> futures = new LinkedList<>();for (Classifier<BytesRef> classifier : classifiers) {- testClassifier(reader, startTime, testReader, service, futures, classifier);+ testClassifier(reader, testReader, service, futures, classifier);}for (Future<String> f : futures) {System.out.println(f.get());@@ -232,7 +235,7 @@ public final class Test20NewsgroupsClassification extends LuceneTestCase {testReader.close();}- for (Classifier c : classifiers) {+ for (Classifier<BytesRef> c : classifiers) {if (c instanceof Closeable) {((Closeable) c).close();}@@ -240,15 +243,15 @@ public final class Test20NewsgroupsClassification extends LuceneTestCase {}}- private void testClassifier(final IndexReader ar, long startTime, IndexReader testReader, ExecutorService service, List<Future<String>> futures, Classifier<BytesRef> classifier) {+ private void testClassifier(final IndexReader ar, IndexReader testReader, ExecutorService service, List<Future<String>> futures, Classifier<BytesRef> classifier) {futures.add(service.submit(() -> {+ final long startTime = System.currentTimeMillis();ConfusionMatrixGenerator.ConfusionMatrix confusionMatrix;if (split) {confusionMatrix = ConfusionMatrixGenerator.getConfusionMatrix(testReader, classifier, CATEGORY_FIELD, BODY_FIELD, 60000 * 30);} else {confusionMatrix = ConfusionMatrixGenerator.getConfusionMatrix(ar, classifier, CATEGORY_FIELD, BODY_FIELD, 60000 * 30);}-final long endTime = System.currentTimeMillis();final int elapse = (int) (endTime - startTime) / 1000;@@ -274,31 +277,30 @@ public final class Test20NewsgroupsClassification extends LuceneTestCase {}-- int buildIndex(File indexDir, IndexWriter indexWriter)+ private int buildIndex(Path indexDir, IndexWriter indexWriter)throws IOException {- File[] groupsDir = indexDir.listFiles();int i = 0;- if (groupsDir != null) {- for (File group : groupsDir) {- String groupName = group.getName();- File[] posts = group.listFiles();- if (posts != null) {- for (File postFile : posts) {- String number = postFile.getName();- NewsPost post = parse(postFile, groupName, number);- if (post != null) {- Document d = new Document();- d.add(new StringField(CATEGORY_FIELD,- post.getGroup(), Field.Store.YES));- d.add(new SortedDocValuesField(CATEGORY_FIELD,- new BytesRef(post.getGroup())));- d.add(new TextField(SUBJECT_FIELD,- post.getSubject(), Field.Store.YES));- d.add(new TextField(BODY_FIELD,- post.getBody(), Field.Store.YES));- indexWriter.addDocument(d);- i++;+ try (DirectoryStream<Path> groupsStream = Files.newDirectoryStream(indexDir)) {+ for (Path groupsDir : groupsStream) {+ if (!Files.isHidden(groupsDir)) {+ try (DirectoryStream<Path> stream = Files.newDirectoryStream(groupsDir)) {+ for (Path p : stream) {+ if (!Files.isHidden(p)) {+ NewsPost post = parse(p, p.getParent().getFileName().toString(), p.getFileName().toString());+ if (post != null) {+ Document d = new Document();+ d.add(new StringField(CATEGORY_FIELD,+ post.getGroup(), Field.Store.YES));+ d.add(new SortedDocValuesField(CATEGORY_FIELD,+ new BytesRef(post.getGroup())));+ d.add(new TextField(SUBJECT_FIELD,+ post.getSubject(), Field.Store.YES));+ d.add(new TextField(BODY_FIELD,+ post.getBody(), Field.Store.YES));+ indexWriter.addDocument(d);+ i++;+ }+ }}}}@@ -308,11 +310,10 @@ public final class Test20NewsgroupsClassification extends LuceneTestCase {return i;}- private NewsPost parse(File postFile, String groupName, String number) throws IOException {+ private NewsPost parse(Path path, String groupName, String number) {StringBuilder body = new StringBuilder();String subject = "";boolean inBody = false;- Path path = postFile.toPath();try {if (Files.isReadable(path)) {for (String line : Files.readAllLines(path)) {