LUCENE-4055: remove SegmentInfo.reset method

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene4055@1339328 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2012-05-16 19:54:20 +00:00
parent 4554a4ff2f
commit bd0d03833a
7 changed files with 38 additions and 57 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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