From c74d48b857cb3e3d9e8e2f07eb8bf3b44ebc3334 Mon Sep 17 00:00:00 2001 From: Michael McCandless Date: Fri, 10 Feb 2012 19:57:07 +0000 Subject: [PATCH] LUCENE-3760: clean up DirectoryReader/SegmentInfos methods git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1242903 13f79535-47bb-0310-9956-ffa450edef68 --- lucene/CHANGES.txt | 5 ++ .../org/apache/lucene/index/CheckIndex.java | 4 +- .../apache/lucene/index/DirectoryReader.java | 40 ----------- .../apache/lucene/index/IndexFileDeleter.java | 6 +- .../org/apache/lucene/index/IndexWriter.java | 4 +- .../org/apache/lucene/index/SegmentInfos.java | 68 ++++++------------- .../lucene/index/StandardDirectoryReader.java | 22 +++--- .../lucene/index/TestDeletionPolicy.java | 8 +-- .../apache/lucene/index/TestIndexReader.java | 2 +- .../lucene/index/TestIndexWriterCommit.java | 10 +-- .../index/TestIndexWriterExceptions.java | 14 ++-- .../lucene/facet/taxonomy/TaxonomyReader.java | 2 +- .../directory/DirectoryTaxonomyReader.java | 8 +-- .../TestDirectoryTaxonomyWriter.java | 2 +- 14 files changed, 63 insertions(+), 132 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 0d21f950c01..7c2d22b77ff 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -748,6 +748,11 @@ Changes in backwards compatibility policy * LUCENE-3672: Deprecate Directory.fileModified and IndexCommit.getTimestamp and .getVersion. (Andrzej Bialecki, Robert Muir, Mike McCandless) + +* LUCENE-3760: In IndexReader/DirectoryReader, deprecate static + methods getCurrentVersion and getCommitUserData, and non-static + method getCommitUserData (use getIndexCommit().getUserData() + instead). (Ryan McKinley, Robert Muir, Mike McCandless) Security fixes diff --git a/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java b/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java index a933a0a5fc2..de2857e7bb7 100644 --- a/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java +++ b/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java @@ -385,7 +385,7 @@ public class CheckIndex { } final int numSegments = sis.size(); - final String segmentsFileName = sis.getCurrentSegmentFileName(); + final String segmentsFileName = sis.getSegmentsFileName(); // note: we only read the format byte (required preamble) here! IndexInput input = null; try { @@ -1773,7 +1773,7 @@ public class CheckIndex { System.out.println("Writing..."); checker.fixIndex(result, codec); System.out.println("OK"); - System.out.println("Wrote new segments file \"" + result.newSegments.getCurrentSegmentFileName() + "\""); + System.out.println("Wrote new segments file \"" + result.newSegments.getSegmentsFileName() + "\""); } } System.out.println(""); diff --git a/lucene/core/src/java/org/apache/lucene/index/DirectoryReader.java b/lucene/core/src/java/org/apache/lucene/index/DirectoryReader.java index 8b7763be6c8..6cb1c37632f 100644 --- a/lucene/core/src/java/org/apache/lucene/index/DirectoryReader.java +++ b/lucene/core/src/java/org/apache/lucene/index/DirectoryReader.java @@ -313,38 +313,6 @@ public abstract class DirectoryReader extends BaseMultiReader { return commits; } - /** - * 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. - * @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) throws CorruptIndexException, IOException { - return SegmentInfos.readCurrentVersion(directory); - } - - /** - * 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. - * @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 getCommitUserData(Directory directory) throws CorruptIndexException, IOException { - return SegmentInfos.readCurrentUserData(directory); - } - /** * Returns true if an index exists at the specified directory. * @param directory the directory to check for an index @@ -414,14 +382,6 @@ public abstract class DirectoryReader extends BaseMultiReader { */ public abstract long getVersion(); - /** - * Retrieve the String userData optionally passed to - * IndexWriter#commit. This will return null if {@link - * IndexWriter#commit(Map)} has never been called for - * this index. - */ - public abstract Map getCommitUserData(); - /** * Check whether any new changes have occurred to the * index since this reader was opened. diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexFileDeleter.java b/lucene/core/src/java/org/apache/lucene/index/IndexFileDeleter.java index ef8c2ae1678..4b9e3f7c0e8 100644 --- a/lucene/core/src/java/org/apache/lucene/index/IndexFileDeleter.java +++ b/lucene/core/src/java/org/apache/lucene/index/IndexFileDeleter.java @@ -126,7 +126,7 @@ final class IndexFileDeleter { this.infoStream = infoStream; this.writer = writer; - final String currentSegmentsFile = segmentInfos.getCurrentSegmentFileName(); + final String currentSegmentsFile = segmentInfos.getSegmentsFileName(); if (infoStream.isEnabled("IFD")) { infoStream.message("IFD", "init: current segments file is \"" + currentSegmentsFile + "\"; deletionPolicy=" + policy); @@ -243,7 +243,7 @@ final class IndexFileDeleter { throw new CorruptIndexException("failed to locate current segments_N file"); } if (infoStream.isEnabled("IFD")) { - infoStream.message("IFD", "forced open of current segments file " + segmentInfos.getCurrentSegmentFileName()); + infoStream.message("IFD", "forced open of current segments file " + segmentInfos.getSegmentsFileName()); } currentCommitPoint = new CommitPoint(commitsToDelete, directory, sis); commits.add(currentCommitPoint); @@ -663,7 +663,7 @@ final class IndexFileDeleter { this.directory = directory; this.commitsToDelete = commitsToDelete; userData = segmentInfos.getUserData(); - segmentsFileName = segmentInfos.getCurrentSegmentFileName(); + segmentsFileName = segmentInfos.getSegmentsFileName(); generation = segmentInfos.getGeneration(); files = Collections.unmodifiableCollection(segmentInfos.files(directory, true)); segmentCount = segmentInfos.size(); diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java index 197bb66f42e..6b3160cee7b 100644 --- a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java +++ b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java @@ -2630,7 +2630,7 @@ public class IndexWriter implements Closeable, TwoPhaseCommit { * @param commitUserData Opaque Map (String->String) * that's recorded into the segments file in the index, * and retrievable by {@link - * DirectoryReader#getCommitUserData}. Note that when + * IndexCommit#getUserData}. Note that when * IndexWriter commits itself during {@link #close}, the * commitUserData is unchanged (just carried over from * the prior commit). If this is null then the previous @@ -2821,7 +2821,7 @@ public class IndexWriter implements Closeable, TwoPhaseCommit { } pendingCommit.finishCommit(directory, codec); if (infoStream.isEnabled("IW")) { - infoStream.message("IW", "commit: wrote segments file \"" + pendingCommit.getCurrentSegmentFileName() + "\""); + infoStream.message("IW", "commit: wrote segments file \"" + pendingCommit.getSegmentsFileName() + "\""); } lastCommitChangeCount = pendingCommitChangeCount; segmentInfos.updateGeneration(pendingCommit); diff --git a/lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java b/lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java index 9b7b70c12b4..3c89b7ca1c7 100644 --- a/lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java +++ b/lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java @@ -138,12 +138,12 @@ public final class SegmentInfos implements Cloneable, Iterable { } /** - * Get the generation (N) of the current segments_N file - * from a list of files. + * Get the generation of the most recent commit to the + * list of index files (N in the segments_N file). * * @param files -- array of file names to check */ - public static long getCurrentSegmentGeneration(String[] files) { + public static long getLastCommitGeneration(String[] files) { if (files == null) { return -1; } @@ -160,48 +160,48 @@ public final class SegmentInfos implements Cloneable, Iterable { } /** - * Get the generation (N) of the current segments_N file - * in the directory. + * Get the generation of the most recent commit to the + * index in this directory (N in the segments_N file). * * @param directory -- directory to search for the latest segments_N file */ - public static long getCurrentSegmentGeneration(Directory directory) throws IOException { + public static long getLastCommitGeneration(Directory directory) throws IOException { try { - return getCurrentSegmentGeneration(directory.listAll()); + return getLastCommitGeneration(directory.listAll()); } catch (NoSuchDirectoryException nsde) { return -1; } } /** - * Get the filename of the current segments_N file - * from a list of files. + * Get the filename of the segments_N file for the most + * recent commit in the list of index files. * * @param files -- array of file names to check */ - public static String getCurrentSegmentFileName(String[] files) throws IOException { + public static String getLastCommitSegmentsFileName(String[] files) throws IOException { return IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS, "", - getCurrentSegmentGeneration(files)); + getLastCommitGeneration(files)); } /** - * Get the filename of the current segments_N file - * in the directory. + * Get the filename of the segments_N file for the most + * recent commit to the index in this Directory. * * @param directory -- directory to search for the latest segments_N file */ - public static String getCurrentSegmentFileName(Directory directory) throws IOException { + public static String getLastCommitSegmentsFileName(Directory directory) throws IOException { return IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS, "", - getCurrentSegmentGeneration(directory)); + getLastCommitGeneration(directory)); } /** * Get the segments_N filename in use by this segment infos. */ - public String getCurrentSegmentFileName() { + public String getSegmentsFileName() { return IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS, "", lastGeneration); @@ -407,36 +407,6 @@ public final class SegmentInfos implements Cloneable, Iterable { return lastGeneration; } - /** - * Current version number from segments file. - * @throws CorruptIndexException if the index is corrupt - * @throws IOException if there is a low-level IO error - */ - public static long readCurrentVersion(Directory directory) - throws CorruptIndexException, IOException { - - // Fully read the segments file: this ensures that it's - // completely written so that if - // 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); - return sis.version; - } - - /** - * Returns userData from latest segments file - * @throws CorruptIndexException if the index is corrupt - * @throws IOException if there is a low-level IO error - */ - public static Map readCurrentUserData(Directory directory) - throws CorruptIndexException, IOException { - SegmentInfos sis = new SegmentInfos(); - sis.read(directory); - return sis.getUserData(); - } - /** If non-null, information about retries when loading * the segments file will be printed to this. */ @@ -555,7 +525,7 @@ public final class SegmentInfos implements Cloneable, Iterable { files = directory.listAll(); if (files != null) { - genA = getCurrentSegmentGeneration(files); + genA = getLastCommitGeneration(files); } if (infoStream != null) { @@ -770,7 +740,7 @@ public final class SegmentInfos implements Cloneable, Iterable { public Collection files(Directory dir, boolean includeSegmentsFile) throws IOException { HashSet files = new HashSet(); if (includeSegmentsFile) { - final String segmentFileName = getCurrentSegmentFileName(); + final String segmentFileName = getSegmentsFileName(); if (segmentFileName != null) { /* * TODO: if lastGen == -1 we get might get null here it seems wrong to @@ -869,7 +839,7 @@ public final class SegmentInfos implements Cloneable, Iterable { public String toString(Directory directory) { StringBuilder buffer = new StringBuilder(); - buffer.append(getCurrentSegmentFileName()).append(": "); + buffer.append(getSegmentsFileName()).append(": "); final int count = size(); for(int i = 0; i < count; i++) { if (i > 0) { diff --git a/lucene/core/src/java/org/apache/lucene/index/StandardDirectoryReader.java b/lucene/core/src/java/org/apache/lucene/index/StandardDirectoryReader.java index 5539d0aea10..c7c8b70b60e 100644 --- a/lucene/core/src/java/org/apache/lucene/index/StandardDirectoryReader.java +++ b/lucene/core/src/java/org/apache/lucene/index/StandardDirectoryReader.java @@ -203,7 +203,7 @@ final class StandardDirectoryReader extends DirectoryReader { final StringBuilder buffer = new StringBuilder(); buffer.append(getClass().getSimpleName()); buffer.append('('); - final String segmentsFile = segmentInfos.getCurrentSegmentFileName(); + final String segmentsFile = segmentInfos.getSegmentsFileName(); if (segmentsFile != null) { buffer.append(segmentsFile).append(":").append(segmentInfos.getVersion()); } @@ -276,7 +276,7 @@ final class StandardDirectoryReader extends DirectoryReader { if (directory != commit.getDirectory()) { throw new IOException("the specified commit does not match the specified Directory"); } - if (segmentInfos != null && commit.getSegmentsFileName().equals(segmentInfos.getCurrentSegmentFileName())) { + if (segmentInfos != null && commit.getSegmentsFileName().equals(segmentInfos.getSegmentsFileName())) { return null; } } @@ -301,18 +301,20 @@ final class StandardDirectoryReader extends DirectoryReader { return segmentInfos.getVersion(); } - @Override - public Map getCommitUserData() { - ensureOpen(); - return segmentInfos.getUserData(); - } - @Override public boolean isCurrent() throws CorruptIndexException, IOException { ensureOpen(); if (writer == null || writer.isClosed()) { + // Fully read the segments file: this ensures that it's + // completely written so that if + // 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); + // we loaded SegmentInfos from the directory - return SegmentInfos.readCurrentVersion(directory) == segmentInfos.getVersion(); + return sis.getVersion() == segmentInfos.getVersion(); } else { return writer.nrtIsCurrent(segmentInfos); } @@ -355,7 +357,7 @@ final class StandardDirectoryReader extends DirectoryReader { private final int segmentCount; ReaderCommit(SegmentInfos infos, Directory dir) throws IOException { - segmentsFileName = infos.getCurrentSegmentFileName(); + segmentsFileName = infos.getSegmentsFileName(); this.dir = dir; userData = infos.getUserData(); files = Collections.unmodifiableCollection(infos.files(dir, true)); diff --git a/lucene/core/src/test/org/apache/lucene/index/TestDeletionPolicy.java b/lucene/core/src/test/org/apache/lucene/index/TestDeletionPolicy.java index 8369ffa4c50..8a27176d60f 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestDeletionPolicy.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestDeletionPolicy.java @@ -252,7 +252,7 @@ public class TestDeletionPolicy extends LuceneTestCase { // segments_N's that still exist are in fact within SECONDS // seconds of the last one's mod time, and, that I can // open a reader on each: - long gen = SegmentInfos.getCurrentSegmentGeneration(dir); + long gen = SegmentInfos.getLastCommitGeneration(dir); String fileName = IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS, "", @@ -364,7 +364,7 @@ public class TestDeletionPolicy extends LuceneTestCase { // Simplistic check: just verify all segments_N's still // exist, and, I can open a reader on each: dir.deleteFile(IndexFileNames.SEGMENTS_GEN); - long gen = SegmentInfos.getCurrentSegmentGeneration(dir); + long gen = SegmentInfos.getLastCommitGeneration(dir); while(gen > 0) { IndexReader reader = IndexReader.open(dir); reader.close(); @@ -586,7 +586,7 @@ public class TestDeletionPolicy extends LuceneTestCase { // Simplistic check: just verify only the past N segments_N's still // exist, and, I can open a reader on each: dir.deleteFile(IndexFileNames.SEGMENTS_GEN); - long gen = SegmentInfos.getCurrentSegmentGeneration(dir); + long gen = SegmentInfos.getLastCommitGeneration(dir); for(int i=0;i 0 but got " + gen, gen > 0); - final String segmentsFileName = SegmentInfos.getCurrentSegmentFileName(dir); + final String segmentsFileName = SegmentInfos.getLastCommitSegmentsFileName(dir); IndexInput in = dir.openInput(segmentsFileName, newIOContext(random)); IndexOutput out = dir.createOutput(IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS, "", 1+gen), newIOContext(random)); out.copyBytes(in, in.length()-1); @@ -1087,10 +1087,10 @@ public class TestIndexWriterExceptions extends LuceneTestCase { // close writer.close(); - long gen = SegmentInfos.getCurrentSegmentGeneration(dir); + long gen = SegmentInfos.getLastCommitGeneration(dir); assertTrue("segment generation should be > 0 but got " + gen, gen > 0); - String fileNameIn = SegmentInfos.getCurrentSegmentFileName(dir); + String fileNameIn = SegmentInfos.getLastCommitSegmentsFileName(dir); String fileNameOut = IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS, "", 1+gen); @@ -1139,7 +1139,7 @@ public class TestIndexWriterExceptions extends LuceneTestCase { // close writer.close(); - long gen = SegmentInfos.getCurrentSegmentGeneration(dir); + long gen = SegmentInfos.getLastCommitGeneration(dir); assertTrue("segment generation should be > 0 but got " + gen, gen > 0); String[] files = dir.listAll(); @@ -1185,13 +1185,13 @@ public class TestIndexWriterExceptions extends LuceneTestCase { // close writer.close(); - long gen = SegmentInfos.getCurrentSegmentGeneration(dir); + long gen = SegmentInfos.getLastCommitGeneration(dir); assertTrue("segment generation should be > 0 but got " + gen, gen > 0); // Make the next segments file, with last byte // missing, to simulate a writer that crashed while // writing segments file: - String fileNameIn = SegmentInfos.getCurrentSegmentFileName(dir); + String fileNameIn = SegmentInfos.getLastCommitSegmentsFileName(dir); String fileNameOut = IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS, "", 1+gen); diff --git a/modules/facet/src/java/org/apache/lucene/facet/taxonomy/TaxonomyReader.java b/modules/facet/src/java/org/apache/lucene/facet/taxonomy/TaxonomyReader.java index e17de23ec0c..5d5fce61832 100644 --- a/modules/facet/src/java/org/apache/lucene/facet/taxonomy/TaxonomyReader.java +++ b/modules/facet/src/java/org/apache/lucene/facet/taxonomy/TaxonomyReader.java @@ -242,7 +242,7 @@ public interface TaxonomyReader extends Closeable { * Retrieve user committed data. * @see TaxonomyWriter#commit(Map) */ - public Map getCommitUserData(); + public Map getCommitUserData() throws IOException; /** * Expert: increments the refCount of this TaxonomyReader instance. diff --git a/modules/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyReader.java b/modules/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyReader.java index 8f868786ab5..f66621daf7a 100644 --- a/modules/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyReader.java +++ b/modules/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyReader.java @@ -361,8 +361,8 @@ public class DirectoryTaxonomyReader implements TaxonomyReader { // validate that a refresh is valid at this point, i.e. that the taxonomy // was not recreated since this reader was last opened or refresshed. - String t1 = indexReader.getCommitUserData().get(DirectoryTaxonomyWriter.INDEX_CREATE_TIME); - String t2 = r2.getCommitUserData().get(DirectoryTaxonomyWriter.INDEX_CREATE_TIME); + String t1 = indexReader.getIndexCommit().getUserData().get(DirectoryTaxonomyWriter.INDEX_CREATE_TIME); + String t2 = r2.getIndexCommit().getUserData().get(DirectoryTaxonomyWriter.INDEX_CREATE_TIME); if (t1==null) { if (t2!=null) { r2.close(); @@ -450,9 +450,9 @@ public class DirectoryTaxonomyReader implements TaxonomyReader { } } - public Map getCommitUserData() { + public Map getCommitUserData() throws IOException { ensureOpen(); - return indexReader.getCommitUserData(); + return indexReader.getIndexCommit().getUserData(); } private ChildrenArrays childrenArrays; diff --git a/modules/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.java b/modules/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.java index 2fec3dcf8ba..b0ff41fc793 100644 --- a/modules/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.java +++ b/modules/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.java @@ -81,7 +81,7 @@ public class TestDirectoryTaxonomyWriter extends LuceneTestCase { ltw.close(); DirectoryReader r = IndexReader.open(dir); assertEquals("2 categories plus root should have been committed to the underlying directory", 3, r.numDocs()); - Map readUserCommitData = r.getCommitUserData(); + Map readUserCommitData = r.getIndexCommit().getUserData(); assertTrue("wrong value extracted from commit data", "1 2 3".equals(readUserCommitData.get("testing"))); r.close();