Apache Lucene open-source search software
LUCENE-2737: Codec is not consistently passed to internal API
Simon Willnauer154105635ded
diff --git a/lucene/contrib/misc/src/java/org/apache/lucene/index/IndexSplitter.java b/lucene/contrib/misc/src/java/org/apache/lucene/index/IndexSplitter.javaindex 5641a74761c..e2f3ecf52e3 100644--- a/lucene/contrib/misc/src/java/org/apache/lucene/index/IndexSplitter.java+++ b/lucene/contrib/misc/src/java/org/apache/lucene/index/IndexSplitter.java@@ -26,6 +26,8 @@ import java.text.DecimalFormat;import java.util.ArrayList;import java.util.List;+import org.apache.lucene.index.codecs.Codec;+import org.apache.lucene.index.codecs.CodecProvider;import org.apache.lucene.store.FSDirectory;/**@@ -47,6 +49,8 @@ import org.apache.lucene.store.FSDirectory;*/public class IndexSplitter {public SegmentInfos infos;++ private final CodecProvider codecs;FSDirectory fsDir;@@ -89,10 +93,15 @@ public class IndexSplitter {}public IndexSplitter(File dir) throws IOException {+ this(dir, CodecProvider.getDefault());+ }++ public IndexSplitter(File dir, CodecProvider codecs) throws IOException {this.dir = dir;+ this.codecs = codecs;fsDir = FSDirectory.open(dir);- infos = new SegmentInfos();- infos.read(fsDir);+ infos = new SegmentInfos(codecs);+ infos.read(fsDir, codecs);}public void listSegments() throws IOException {@@ -131,7 +140,7 @@ public class IndexSplitter {public void split(File destDir, String[] segs) throws IOException {destDir.mkdirs();FSDirectory destFSDir = FSDirectory.open(destDir);- SegmentInfos destInfos = new SegmentInfos();+ SegmentInfos destInfos = new SegmentInfos(codecs);for (String n : segs) {SegmentInfo info = getInfo(n);destInfos.add(info);diff --git a/lucene/src/java/org/apache/lucene/index/CheckIndex.java b/lucene/src/java/org/apache/lucene/index/CheckIndex.javaindex b0ecfd753b4..01274beaf34 100644--- a/lucene/src/java/org/apache/lucene/index/CheckIndex.java+++ b/lucene/src/java/org/apache/lucene/index/CheckIndex.java@@ -304,7 +304,7 @@ public class CheckIndex {* writer. */public Status checkIndex(List<String> onlySegments, CodecProvider codecs) throws IOException {NumberFormat nf = NumberFormat.getInstance();- SegmentInfos sis = new SegmentInfos();+ SegmentInfos sis = new SegmentInfos(codecs);Status result = new Status();result.dir = dir;try {diff --git a/lucene/src/java/org/apache/lucene/index/DirectoryReader.java b/lucene/src/java/org/apache/lucene/index/DirectoryReader.javaindex 523fb30e447..befda89bd60 100644--- a/lucene/src/java/org/apache/lucene/index/DirectoryReader.java+++ b/lucene/src/java/org/apache/lucene/index/DirectoryReader.java@@ -82,18 +82,14 @@ class DirectoryReader extends IndexReader implements Cloneable {static IndexReader open(final Directory directory, final IndexDeletionPolicy deletionPolicy, final IndexCommit commit, final boolean readOnly,final int termInfosIndexDivisor, CodecProvider codecs) throws CorruptIndexException, IOException {- final CodecProvider codecs2;- if (codecs == null) {- codecs2 = CodecProvider.getDefault();- } else {- codecs2 = codecs;- }+ final CodecProvider codecProvider = codecs == null ? CodecProvider.getDefault()+ : codecs;return (IndexReader) new SegmentInfos.FindSegmentsFile(directory) {@Overrideprotected Object doBody(String segmentFileName) throws CorruptIndexException, IOException {- SegmentInfos infos = new SegmentInfos();- infos.read(directory, segmentFileName, codecs2);- return new DirectoryReader(directory, infos, deletionPolicy, readOnly, termInfosIndexDivisor, codecs2);+ SegmentInfos infos = new SegmentInfos(codecProvider);+ infos.read(directory, segmentFileName, codecProvider);+ return new DirectoryReader(directory, infos, deletionPolicy, readOnly, termInfosIndexDivisor, codecProvider);}}.run(commit);}@@ -491,7 +487,7 @@ class DirectoryReader extends IndexReader implements Cloneable {return (IndexReader) new SegmentInfos.FindSegmentsFile(directory) {@Overrideprotected Object doBody(String segmentFileName) throws CorruptIndexException, IOException {- SegmentInfos infos = new SegmentInfos();+ final SegmentInfos infos = new SegmentInfos(codecs);infos.read(directory, segmentFileName, codecs);return doReopen(infos, false, openReadOnly);}@@ -500,7 +496,7 @@ class DirectoryReader extends IndexReader implements Cloneable {private synchronized DirectoryReader doReopen(SegmentInfos infos, boolean doClone, boolean openReadOnly) throws CorruptIndexException, IOException {DirectoryReader reader;- reader = new DirectoryReader(directory, infos, subReaders, starts, normsCache, openReadOnly, doClone, termInfosIndexDivisor, null);+ reader = new DirectoryReader(directory, infos, subReaders, starts, normsCache, openReadOnly, doClone, termInfosIndexDivisor, codecs);return reader;}@@ -953,7 +949,7 @@ class DirectoryReader extends IndexReader implements Cloneable {Collection<IndexCommit> commits = new ArrayList<IndexCommit>();- SegmentInfos latest = new SegmentInfos();+ SegmentInfos latest = new SegmentInfos(codecs);latest.read(dir, codecs);final long currentGen = latest.getGeneration();@@ -967,7 +963,7 @@ class DirectoryReader extends IndexReader implements Cloneable {!fileName.equals(IndexFileNames.SEGMENTS_GEN) &&SegmentInfos.generationFromSegmentsFileName(fileName) < currentGen) {- SegmentInfos sis = new SegmentInfos();+ SegmentInfos sis = new SegmentInfos(codecs);try {// IOException allowed to throw there, in case// segments_N is corruptdiff --git a/lucene/src/java/org/apache/lucene/index/IndexFileDeleter.java b/lucene/src/java/org/apache/lucene/index/IndexFileDeleter.javaindex 1dd2f44c9b2..a4e5b56b68a 100644--- a/lucene/src/java/org/apache/lucene/index/IndexFileDeleter.java+++ b/lucene/src/java/org/apache/lucene/index/IndexFileDeleter.java@@ -173,7 +173,7 @@ final class IndexFileDeleter {if (infoStream != null) {message("init: load commit \"" + fileName + "\"");}- SegmentInfos sis = new SegmentInfos();+ SegmentInfos sis = new SegmentInfos(codecs);try {sis.read(directory, fileName, codecs);} catch (FileNotFoundException e) {@@ -222,7 +222,7 @@ final class IndexFileDeleter {// listing was stale (eg when index accessed via NFS// client with stale directory listing cache). So we// try now to explicitly open this commit point:- SegmentInfos sis = new SegmentInfos();+ SegmentInfos sis = new SegmentInfos(codecs);try {sis.read(directory, currentSegmentsFile, codecs);} catch (IOException e) {diff --git a/lucene/src/java/org/apache/lucene/index/IndexReader.java b/lucene/src/java/org/apache/lucene/index/IndexReader.javaindex 14498527844..da781345af9 100644--- a/lucene/src/java/org/apache/lucene/index/IndexReader.java+++ b/lucene/src/java/org/apache/lucene/index/IndexReader.java@@ -20,6 +20,7 @@ package org.apache.lucene.index;import org.apache.lucene.document.Document;import org.apache.lucene.document.FieldSelector;import org.apache.lucene.search.Similarity;+import org.apache.lucene.index.codecs.Codec;import org.apache.lucene.index.codecs.CodecProvider;import org.apache.lucene.store.*;import org.apache.lucene.util.ArrayUtil;@@ -639,7 +640,22 @@ public abstract class IndexReader implements Cloneable,Closeable {* @throws IOException if there is a low-level IO error*/public static long getCurrentVersion(Directory directory) throws CorruptIndexException, IOException {- return SegmentInfos.readCurrentVersion(directory, CodecProvider.getDefault());+ return getCurrentVersion(directory, CodecProvider.getDefault());+ }++ /**+ * Reads version number from segments files. The version number is+ * initialized with a timestamp and then increased by one for each change of+ * the index.+ *+ * @param directory where the index resides.+ * @param codecs the {@link CodecProvider} holding all {@link Codec}s required to open the index+ * @return version number.+ * @throws CorruptIndexException if the index is corrupt+ * @throws IOException if there is a low-level IO error+ */+ public static long getCurrentVersion(Directory directory, CodecProvider codecs) throws CorruptIndexException, IOException {+ return SegmentInfos.readCurrentVersion(directory, codecs);}/**@@ -657,7 +673,27 @@ public abstract class IndexReader implements Cloneable,Closeable {* @see #getCommitUserData()*/public static Map<String,String> getCommitUserData(Directory directory) throws CorruptIndexException, IOException {- return SegmentInfos.readCurrentUserData(directory, CodecProvider.getDefault());+ return getCommitUserData(directory, CodecProvider.getDefault());+ }+++ /**+ * Reads commitUserData, previously passed to {@link+ * IndexWriter#commit(Map)}, from current index+ * segments file. This will return null if {@link+ * IndexWriter#commit(Map)} has never been called for+ * this index.+ *+ * @param directory where the index resides.+ * @param codecs the {@link CodecProvider} provider holding all {@link Codec}s required to open the index+ * @return commit userData.+ * @throws CorruptIndexException if the index is corrupt+ * @throws IOException if there is a low-level IO error+ *+ * @see #getCommitUserData()+ */+ public static Map<String, String> getCommitUserData(Directory directory, CodecProvider codecs) throws CorruptIndexException, IOException {+ return SegmentInfos.readCurrentUserData(directory, codecs);}/**diff --git a/lucene/src/java/org/apache/lucene/index/IndexWriter.java b/lucene/src/java/org/apache/lucene/index/IndexWriter.javaindex 0a90a1889b0..78dad85ad56 100644--- a/lucene/src/java/org/apache/lucene/index/IndexWriter.java+++ b/lucene/src/java/org/apache/lucene/index/IndexWriter.java@@ -279,7 +279,7 @@ public class IndexWriter implements Closeable {volatile SegmentInfos pendingCommit; // set when a commit is pending (after prepareCommit() & before commit())volatile long pendingCommitChangeCount;- private SegmentInfos segmentInfos = new SegmentInfos(); // the segments+ private final SegmentInfos segmentInfos; // the segmentsprivate DocumentsWriter docWriter;private IndexFileDeleter deleter;@@ -1069,7 +1069,7 @@ public class IndexWriter implements Closeable {// instead of later when merge, applyDeletes, getReader// is attempted. I think to do this we should store the// oldest segment's version in segments_N.-+ segmentInfos = new SegmentInfos(codecs);try {if (create) {// Try to read first. This is to allow create@@ -1098,7 +1098,7 @@ public class IndexWriter implements Closeable {// points.if (commit.getDirectory() != directory)throw new IllegalArgumentException("IndexCommit's directory doesn't match my directory");- SegmentInfos oldInfos = new SegmentInfos();+ SegmentInfos oldInfos = new SegmentInfos(codecs);oldInfos.read(directory, commit.getSegmentsFileName(), codecs);segmentInfos.replace(oldInfos);changeCount++;@@ -1117,7 +1117,7 @@ public class IndexWriter implements Closeable {// KeepOnlyLastCommitDeleter:deleter = new IndexFileDeleter(directory,conf.getIndexDeletionPolicy(),- segmentInfos, infoStream, docWriter, this.codecs);+ segmentInfos, infoStream, docWriter, codecs);if (deleter.startingCommitDeleted)// Deletion policy deleted the "head" commit point.@@ -2900,8 +2900,8 @@ public class IndexWriter implements Closeable {if (infoStream != null) {message("process directory " + dir);}- SegmentInfos sis = new SegmentInfos(); // read infos from dir- sis.read(dir);+ SegmentInfos sis = new SegmentInfos(codecs); // read infos from dir+ sis.read(dir, codecs);Map<String, String> dsNames = new HashMap<String, String>();for (SegmentInfo info : sis) {assert !infos.contains(info): "dup info dir=" + info.dir + " name=" + info.name;diff --git a/lucene/src/java/org/apache/lucene/index/SegmentInfos.java b/lucene/src/java/org/apache/lucene/index/SegmentInfos.javaindex ecd4631c817..900e8245a0c 100644--- a/lucene/src/java/org/apache/lucene/index/SegmentInfos.java+++ b/lucene/src/java/org/apache/lucene/index/SegmentInfos.java@@ -341,8 +341,8 @@ public final class SegmentInfos extends Vector<SegmentInfo> {// IndexWriter.prepareCommit has been called (but not// yet commit), then the reader will still see itself as// current:- SegmentInfos sis = new SegmentInfos();- sis.read(directory);+ SegmentInfos sis = new SegmentInfos(codecs);+ sis.read(directory, codecs);return sis.version;}@@ -353,7 +353,7 @@ public final class SegmentInfos extends Vector<SegmentInfo> {*/public static Map<String,String> readCurrentUserData(Directory directory, CodecProvider codecs)throws CorruptIndexException, IOException {- SegmentInfos sis = new SegmentInfos();+ SegmentInfos sis = new SegmentInfos(codecs);sis.read(directory, codecs);return sis.getUserData();}@@ -694,7 +694,7 @@ public final class SegmentInfos extends Vector<SegmentInfo> {* is last-first.*/public SegmentInfos range(int first, int last) {- SegmentInfos infos = new SegmentInfos();+ SegmentInfos infos = new SegmentInfos(codecs);infos.addAll(super.subList(first, last));return infos;}diff --git a/lucene/src/test/org/apache/lucene/util/_TestUtil.java b/lucene/src/test/org/apache/lucene/util/_TestUtil.javaindex 380e321f02d..76cc0b98112 100644--- a/lucene/src/test/org/apache/lucene/util/_TestUtil.java+++ b/lucene/src/test/org/apache/lucene/util/_TestUtil.java@@ -69,11 +69,17 @@ public class _TestUtil {* issues are hit, a RuntimeException is thrown; else,* true is returned. */public static CheckIndex.Status checkIndex(Directory dir) throws IOException {+ return checkIndex(dir, CodecProvider.getDefault());+ }++ /** This runs the CheckIndex tool on the index in. If any+ * issues are hit, a RuntimeException is thrown; else,+ * true is returned. */+ public static CheckIndex.Status checkIndex(Directory dir, CodecProvider codecs) throws IOException {ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);-CheckIndex checker = new CheckIndex(dir);checker.setInfoStream(new PrintStream(bos));- CheckIndex.Status indexStatus = checker.checkIndex();+ CheckIndex.Status indexStatus = checker.checkIndex(null, codecs);if (indexStatus == null || indexStatus.clean == false) {System.out.println("CheckIndex failed");System.out.println(bos.toString());