Skip to content

Apache Lucene open-source search software

add isCurrent() and initialise the segments version number with System.currentTimeMillis()

Daniel Naberc636897dc4a8
diff --git a/src/java/org/apache/lucene/index/IndexReader.java b/src/java/org/apache/lucene/index/IndexReader.java
index ceda0d00c3f..026b297739e 100644
--- a/src/java/org/apache/lucene/index/IndexReader.java
+++ b/src/java/org/apache/lucene/index/IndexReader.java
@@ -224,6 +224,7 @@ public abstract class IndexReader {
* @param directory where the index resides.
* @return version number.
* @throws IOException if segments file cannot be read
+ * @deprecated use {@link #isCurrent()} instead
*/
public static long getCurrentVersion(String directory) throws IOException {
return getCurrentVersion(new File(directory));
@@ -236,6 +237,7 @@ public abstract class IndexReader {
* @param directory where the index resides.
* @return version number.
* @throws IOException if segments file cannot be read
+ * @deprecated use {@link #isCurrent()} instead
*/
public static long getCurrentVersion(File directory) throws IOException {
Directory dir = FSDirectory.getDirectory(directory, false);
@@ -251,11 +253,26 @@ public abstract class IndexReader {
* @param directory where the index resides.
* @return version number.
* @throws IOException if segments file cannot be read.
+ * @deprecated use {@link #isCurrent()} instead
*/
public static long getCurrentVersion(Directory directory) throws IOException {
return SegmentInfos.readCurrentVersion(directory);
}
+ /**
+ * Check whether this IndexReader still works on a current version of the index.
+ * If this is not the case you will need to re-open the IndexReader to
+ * make sure you see the latest changes made to the index.
+ *
+ * @throws IOException
+ */
+ public boolean isCurrent() throws IOException {
+ if (SegmentInfos.readCurrentVersion(directory) != segmentInfos.getVersion()) {
+ return false;
+ }
+ return true;
+ }
+
/**
* Return an array of term frequency vectors for the specified document.
* The array contains a vector for each vectorized field in the document.
diff --git a/src/java/org/apache/lucene/index/SegmentInfos.java b/src/java/org/apache/lucene/index/SegmentInfos.java
index 52de7c55fd4..418c6333b56 100644
--- a/src/java/org/apache/lucene/index/SegmentInfos.java
+++ b/src/java/org/apache/lucene/index/SegmentInfos.java
@@ -29,7 +29,11 @@ final class SegmentInfos extends Vector {
public static final int FORMAT = -1;
public int counter = 0; // used to name new segments
- private long version = 0; //counts how often the index has been changed by adding or deleting docs
+ /**
+ * counts how often the index has been changed by adding or deleting docs.
+ * starting with the current time in milliseconds forces to create unique version numbers.
+ */
+ private long version = System.currentTimeMillis();
public final SegmentInfo info(int i) {
return (SegmentInfo) elementAt(i);
@@ -59,7 +63,7 @@ final class SegmentInfos extends Vector {
if(format >= 0){ // in old format the version number may be at the end of the file
if (input.getFilePointer() >= input.length())
- version = 0; // old file format without version number
+ version = System.currentTimeMillis(); // old file format without version number
else
version = input.readLong(); // read version
}
diff --git a/src/test/org/apache/lucene/index/TestIndexReader.java b/src/test/org/apache/lucene/index/TestIndexReader.java
index be5efa45c9b..e4c6c4431a5 100644
--- a/src/test/org/apache/lucene/index/TestIndexReader.java
+++ b/src/test/org/apache/lucene/index/TestIndexReader.java
@@ -48,6 +48,27 @@ public class TestIndexReader extends TestCase
super(name);
}
+ public void testIsCurrent() throws Exception
+ {
+ RAMDirectory d = new RAMDirectory();
+ IndexWriter writer = new IndexWriter(d, new StandardAnalyzer(), true);
+ addDocumentWithFields(writer);
+ writer.close();
+ // set up reader:
+ IndexReader reader = IndexReader.open(d);
+ assertTrue(reader.isCurrent());
+ // modify index by adding another document:
+ writer = new IndexWriter(d, new StandardAnalyzer(), false);
+ addDocumentWithFields(writer);
+ writer.close();
+ assertFalse(reader.isCurrent());
+ // re-create index:
+ writer = new IndexWriter(d, new StandardAnalyzer(), true);
+ addDocumentWithFields(writer);
+ writer.close();
+ assertFalse(reader.isCurrent());
+ reader.close();
+ }
/**
* Tests the IndexReader.getFieldNames implementation