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
This commit is contained in:
Michael McCandless 2012-02-10 19:57:07 +00:00
parent 590741dcfe
commit c74d48b857
14 changed files with 63 additions and 132 deletions

View File

@ -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

View File

@ -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("");

View File

@ -313,38 +313,6 @@ public abstract class DirectoryReader extends BaseMultiReader<AtomicReader> {
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<String, String> getCommitUserData(Directory directory) throws CorruptIndexException, IOException {
return SegmentInfos.readCurrentUserData(directory);
}
/**
* Returns <code>true</code> 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<AtomicReader> {
*/
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<String,String> getCommitUserData();
/**
* Check whether any new changes have occurred to the
* index since this reader was opened.

View File

@ -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();

View File

@ -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);

View File

@ -138,12 +138,12 @@ public final class SegmentInfos implements Cloneable, Iterable<SegmentInfo> {
}
/**
* 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<SegmentInfo> {
}
/**
* 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<SegmentInfo> {
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<String,String> 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<SegmentInfo> {
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<SegmentInfo> {
public Collection<String> files(Directory dir, boolean includeSegmentsFile) throws IOException {
HashSet<String> files = new HashSet<String>();
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<SegmentInfo> {
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) {

View File

@ -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<String,String> 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));

View File

@ -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<N+1;i++) {
try {
IndexReader reader = IndexReader.open(dir);
@ -684,7 +684,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:
long gen = SegmentInfos.getCurrentSegmentGeneration(dir);
long gen = SegmentInfos.getLastCommitGeneration(dir);
dir.deleteFile(IndexFileNames.SEGMENTS_GEN);
int expectedCount = 0;

View File

@ -582,7 +582,7 @@ public class TestIndexReader extends LuceneTestCase {
DirectoryReader r = DirectoryReader.open(d);
IndexCommit c = r.getIndexCommit();
assertEquals(sis.getCurrentSegmentFileName(), c.getSegmentsFileName());
assertEquals(sis.getSegmentsFileName(), c.getSegmentsFileName());
assertTrue(c.equals(r.getIndexCommit()));

View File

@ -620,11 +620,9 @@ public class TestIndexWriterCommit extends LuceneTestCase {
TestIndexWriter.addDoc(w);
w.close();
assertEquals(0, DirectoryReader.getCommitUserData(dir).size());
DirectoryReader r = IndexReader.open(dir);
// commit(Map) never called for this index
assertEquals(0, r.getCommitUserData().size());
assertEquals(0, r.getIndexCommit().getUserData().size());
r.close();
w = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMaxBufferedDocs(2));
@ -635,18 +633,14 @@ public class TestIndexWriterCommit extends LuceneTestCase {
w.commit(data);
w.close();
assertEquals("test1", DirectoryReader.getCommitUserData(dir).get("label"));
r = IndexReader.open(dir);
assertEquals("test1", r.getCommitUserData().get("label"));
assertEquals("test1", r.getIndexCommit().getUserData().get("label"));
r.close();
w = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
w.forceMerge(1);
w.close();
assertEquals("test1", DirectoryReader.getCommitUserData(dir).get("label"));
dir.close();
}
}

View File

@ -1045,10 +1045,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);
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);

View File

@ -242,7 +242,7 @@ public interface TaxonomyReader extends Closeable {
* Retrieve user committed data.
* @see TaxonomyWriter#commit(Map)
*/
public Map<String, String> getCommitUserData();
public Map<String, String> getCommitUserData() throws IOException;
/**
* Expert: increments the refCount of this TaxonomyReader instance.

View File

@ -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<String, String> getCommitUserData() {
public Map<String, String> getCommitUserData() throws IOException {
ensureOpen();
return indexReader.getCommitUserData();
return indexReader.getIndexCommit().getUserData();
}
private ChildrenArrays childrenArrays;

View File

@ -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 <String, String> readUserCommitData = r.getCommitUserData();
Map <String, String> readUserCommitData = r.getIndexCommit().getUserData();
assertTrue("wrong value extracted from commit data",
"1 2 3".equals(readUserCommitData.get("testing")));
r.close();