Skip to content

Apache Lucene open-source search software

LUCENE-4055: remove SegmentInfo.reset method

Michael McCandlessbd0d03833a8f
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/LiveDocsFormat.java b/lucene/core/src/java/org/apache/lucene/codecs/LiveDocsFormat.java
index 6c8ee7bb9e7..e48aaf3b395 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/LiveDocsFormat.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/LiveDocsFormat.java
@@ -29,13 +29,21 @@ import org.apache.lucene.util.MutableBits;
/** Format for live/deleted documents
* @lucene.experimental */
public abstract class LiveDocsFormat {
- /** creates a new mutablebits, with all bits set, for the specified size */
+ /** Creates a new MutableBits, with all bits set, for the specified size. */
public abstract MutableBits newLiveDocs(int size) throws IOException;
- /** creates a new mutablebits of the same bits set and size of existing */
+
+ /** Creates a new mutablebits of the same bits set and size of existing. */
public abstract MutableBits newLiveDocs(Bits existing) throws IOException;
- /** reads bits from a file */
+
+ /** Read live docs bits. */
public abstract Bits readLiveDocs(Directory dir, SegmentInfo info, IOContext context) throws IOException;
- /** writes bits to a file */
- public abstract void writeLiveDocs(MutableBits bits, Directory dir, SegmentInfo info, IOContext context) throws IOException;
+
+ /** Persist live docs bits. Use {@link
+ * SegmentInfo#getNextDelGen} to determine the
+ * generation of the deletes file you should write to. */
+ public abstract void writeLiveDocs(MutableBits bits, Directory dir, SegmentInfo info, int newDelCount, IOContext context) throws IOException;
+
+ /** Records all files in use by this {@link SegmentInfo}
+ * into the files argument. */
public abstract void files(SegmentInfo info, Set<String> files) throws IOException;
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene3x/Lucene3xCodec.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene3x/Lucene3xCodec.java
index 96e64aa5e1a..e47cf97a303 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene3x/Lucene3xCodec.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene3x/Lucene3xCodec.java
@@ -68,7 +68,7 @@ public class Lucene3xCodec extends Codec {
// TODO: this should really be a different impl
private final LiveDocsFormat liveDocsFormat = new Lucene40LiveDocsFormat() {
@Override
- public void writeLiveDocs(MutableBits bits, Directory dir, SegmentInfo info, IOContext context) throws IOException {
+ public void writeLiveDocs(MutableBits bits, Directory dir, SegmentInfo info, int newDelCount, IOContext context) throws IOException {
throw new UnsupportedOperationException("this codec can only be used for reading");
}
};
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40LiveDocsFormat.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40LiveDocsFormat.java
index 46e257be20e..4907f018a0c 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40LiveDocsFormat.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40LiveDocsFormat.java
@@ -90,10 +90,10 @@ public class Lucene40LiveDocsFormat extends LiveDocsFormat {
}
@Override
- public void writeLiveDocs(MutableBits bits, Directory dir, SegmentInfo info, IOContext context) throws IOException {
- String filename = IndexFileNames.fileNameFromGeneration(info.name, DELETES_EXTENSION, info.getDelGen());
+ public void writeLiveDocs(MutableBits bits, Directory dir, SegmentInfo info, int newDelCount, IOContext context) throws IOException {
+ String filename = IndexFileNames.fileNameFromGeneration(info.name, DELETES_EXTENSION, info.getNextDelGen());
final BitVector liveDocs = (BitVector) bits;
- assert liveDocs.count() == info.docCount - info.getDelCount();
+ assert liveDocs.count() == info.docCount - info.getDelCount() - newDelCount;
assert liveDocs.length() == info.docCount;
liveDocs.write(dir, filename, context);
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/simpletext/SimpleTextLiveDocsFormat.java b/lucene/core/src/java/org/apache/lucene/codecs/simpletext/SimpleTextLiveDocsFormat.java
index fab1fa381f7..5eef0ee646a 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/simpletext/SimpleTextLiveDocsFormat.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/simpletext/SimpleTextLiveDocsFormat.java
@@ -105,12 +105,12 @@ public class SimpleTextLiveDocsFormat extends LiveDocsFormat {
}
@Override
- public void writeLiveDocs(MutableBits bits, Directory dir, SegmentInfo info, IOContext context) throws IOException {
+ public void writeLiveDocs(MutableBits bits, Directory dir, SegmentInfo info, int newDelCount, IOContext context) throws IOException {
BitSet set = ((SimpleTextBits) bits).bits;
int size = bits.length();
BytesRef scratch = new BytesRef();
- String fileName = IndexFileNames.fileNameFromGeneration(info.name, LIVEDOCS_EXTENSION, info.getDelGen());
+ String fileName = IndexFileNames.fileNameFromGeneration(info.name, LIVEDOCS_EXTENSION, info.getNextDelGen());
IndexOutput out = null;
boolean success = false;
try {
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 f02d4fe2a69..a205ec4f6e2 100644
--- a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java
+++ b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java
@@ -1996,8 +1996,6 @@ public class IndexWriter implements Closeable, TwoPhaseCommit {
if (flushedSegment.liveDocs != null) {
final int delCount = flushedSegment.delCount;
assert delCount > 0;
- newSegment.setDelCount(delCount);
- newSegment.advanceDelGen();
if (infoStream.isEnabled("IW")) {
infoStream.message("IW", "flush: write " + delCount + " deletes gen=" + flushedSegment.segmentInfo.getDelGen());
}
@@ -2010,7 +2008,9 @@ public class IndexWriter implements Closeable, TwoPhaseCommit {
SegmentInfo info = flushedSegment.segmentInfo;
Codec codec = info.getCodec();
- codec.liveDocsFormat().writeLiveDocs(flushedSegment.liveDocs, directory, info, context);
+ codec.liveDocsFormat().writeLiveDocs(flushedSegment.liveDocs, directory, info, delCount, context);
+ newSegment.setDelCount(delCount);
+ newSegment.advanceDelGen();
}
success = true;
diff --git a/lucene/core/src/java/org/apache/lucene/index/ReadersAndLiveDocs.java b/lucene/core/src/java/org/apache/lucene/index/ReadersAndLiveDocs.java
index cc194747702..bb383200358 100644
--- a/lucene/core/src/java/org/apache/lucene/index/ReadersAndLiveDocs.java
+++ b/lucene/core/src/java/org/apache/lucene/index/ReadersAndLiveDocs.java
@@ -272,23 +272,17 @@ class ReadersAndLiveDocs {
// We have new deletes
assert liveDocs.length() == info.docCount;
- // Save in case we need to rollback on failure:
- final SegmentInfo sav = info.clone();
- info.advanceDelGen();
- info.setDelCount(info.getDelCount() + pendingDeleteCount);
-
// We can write directly to the actual name (vs to a
// .tmp & renaming it) because the file is not live
// until segments file is written:
- boolean success = false;
- try {
- info.getCodec().liveDocsFormat().writeLiveDocs((MutableBits)liveDocs, dir, info, IOContext.DEFAULT);
- success = true;
- } finally {
- if (!success) {
- info.reset(sav);
- }
- }
+ info.getCodec().liveDocsFormat().writeLiveDocs((MutableBits)liveDocs, dir, info, pendingDeleteCount, IOContext.DEFAULT);
+
+ // If we hit an exc in the line above (eg disk full)
+ // then info remains pointing to the previous
+ // (successfully written) del docs:
+ info.advanceDelGen();
+ info.setDelCount(info.getDelCount() + pendingDeleteCount);
+
pendingDeleteCount = 0;
return true;
} else {
diff --git a/lucene/core/src/java/org/apache/lucene/index/SegmentInfo.java b/lucene/core/src/java/org/apache/lucene/index/SegmentInfo.java
index 7de2cd1ba35..44cf8e597df 100644
--- a/lucene/core/src/java/org/apache/lucene/index/SegmentInfo.java
+++ b/lucene/core/src/java/org/apache/lucene/index/SegmentInfo.java
@@ -127,35 +127,6 @@ public final class SegmentInfo implements Cloneable {
this.fieldInfos = fieldInfos;
}
- /**
- * Copy everything from src SegmentInfo into our instance.
- */
- void reset(SegmentInfo src) {
- clearFilesCache();
- version = src.version;
- name = src.name;
- docCount = src.docCount;
- dir = src.dir;
- delGen = src.delGen;
- docStoreOffset = src.docStoreOffset;
- docStoreSegment = src.docStoreSegment;
- docStoreIsCompoundFile = src.docStoreIsCompoundFile;
- hasVectors = src.hasVectors;
- hasProx = src.hasProx;
- fieldInfos = src.fieldInfos == null ? null : src.fieldInfos.clone();
- if (src.normGen == null) {
- normGen = null;
- } else {
- normGen = new HashMap<Integer, Long>(src.normGen.size());
- for (Entry<Integer,Long> entry : src.normGen.entrySet()) {
- normGen.put(entry.getKey(), entry.getValue());
- }
- }
- isCompoundFile = src.isCompoundFile;
- delCount = src.delCount;
- codec = src.codec;
- }
-
void setDiagnostics(Map<String, String> diagnostics) {
this.diagnostics = diagnostics;
}
@@ -246,6 +217,14 @@ public final class SegmentInfo implements Cloneable {
clearFilesCache();
}
+ public long getNextDelGen() {
+ if (delGen == NO) {
+ return YES;
+ } else {
+ return delGen + 1;
+ }
+ }
+
void clearDelGen() {
delGen = NO;
clearFilesCache();