Skip to content

Commit

Permalink
Add missing segments file to the lucene file listing
Browse files Browse the repository at this point in the history
Relates to elastic#9761
  • Loading branch information
s1monw committed Feb 20, 2015
1 parent f71beac commit 50b9a8d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/elasticsearch/common/lucene/Lucene.java
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.common.lucene;

import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import org.apache.lucene.analysis.core.KeywordAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.codecs.CodecUtil;
Expand Down Expand Up @@ -120,6 +121,7 @@ public static SegmentInfos readSegmentInfos(Directory directory) throws IOExcept
*/
public static Iterable<String> files(SegmentInfos infos) throws IOException {
final List<Collection<String>> list = new ArrayList<>();
list.add(Collections.singleton(infos.getSegmentsFileName()));
for (SegmentCommitInfo info : infos) {
list.add(info.files());
}
Expand Down
53 changes: 53 additions & 0 deletions src/test/java/org/elasticsearch/common/lucene/LuceneTest.java
Expand Up @@ -17,6 +17,7 @@
* under the License.
*/
package org.elasticsearch.common.lucene;
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
Expand All @@ -29,6 +30,9 @@
import org.junit.Test;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

/**
*
*/
Expand Down Expand Up @@ -100,4 +104,53 @@ public void testPruneUnreferencedFiles() throws IOException {
dir.close();

}

public void testFiles() throws IOException {
MockDirectoryWrapper dir = newMockDirectory();
dir.setEnableVirusScanner(false);
IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
iwc.setMergePolicy(NoMergePolicy.INSTANCE);
iwc.setMaxBufferedDocs(2);
iwc.setUseCompoundFile(true);
IndexWriter writer = new IndexWriter(dir, iwc);
Document doc = new Document();
doc.add(new TextField("id", "1", random().nextBoolean() ? Field.Store.YES : Field.Store.NO));
writer.addDocument(doc);
writer.commit();
Set<String> files = new HashSet<>();
for (String f : Lucene.files(Lucene.readSegmentInfos(dir))) {
files.add(f);
}

assertTrue(files.toString(), files.contains("segments_1"));
assertTrue(files.toString(), files.contains("_0.cfs"));
assertTrue(files.toString(), files.contains("_0.cfe"));
assertTrue(files.toString(), files.contains("_0.si"));

doc = new Document();
doc.add(new TextField("id", "2", random().nextBoolean() ? Field.Store.YES : Field.Store.NO));
writer.addDocument(doc);

doc = new Document();
doc.add(new TextField("id", "3", random().nextBoolean() ? Field.Store.YES : Field.Store.NO));
writer.addDocument(doc);
writer.commit();

files.clear();
for (String f : Lucene.files(Lucene.readSegmentInfos(dir))) {
files.add(f);
}
assertFalse(files.toString(), files.contains("segments_1"));
assertTrue(files.toString(), files.contains("segments_2"));
assertTrue(files.toString(), files.contains("_0.cfs"));
assertTrue(files.toString(), files.contains("_0.cfe"));
assertTrue(files.toString(), files.contains("_0.si"));

assertTrue(files.toString(), files.contains("_1.cfs"));
assertTrue(files.toString(), files.contains("_1.cfe"));
assertTrue(files.toString(), files.contains("_1.si"));
writer.close();
dir.close();

}
}

0 comments on commit 50b9a8d

Please sign in to comment.