class IndexWriter {
/** This is called after merging a segment and before
* building its CFS. Return true if the files should be
* sync'd. If you return false, then the source segment
* files that were merged cannot be deleted until the CFS
* file is built & sync'd. So, returning false consumes
* more transient disk space, but saves performance of
* not having to sync files which will shortly be deleted
* anyway.
* @deprecated -- this will be removed in 3.0 when
* autoCommit is hardwired to false */
private synchronized boolean doCommitBeforeMergeCFS(MergePolicy.OneMerge merge) throws IOException {
long freeableBytes = 0;
final int size = merge.segments.size();
for(int i=0;i<size;i++) {
final SegmentInfo info = merge.segments.info(i);
// It's only important to sync if the most recent
// commit actually references this segment, because if
// it doesn't, even without syncing we will free up
// the disk space:
Integer loc = (Integer) rollbackSegments.get(info);
if (loc != null) {
final SegmentInfo oldInfo = rollbackSegmentInfos.info(loc.intValue());
if (oldInfo.getUseCompoundFile() != info.getUseCompoundFile())
freeableBytes += info.sizeInBytes();
}
}
// If we would free up more than 1/3rd of the index by
// committing now, then do so:
long totalBytes = 0;
final int numSegments = segmentInfos.size();
for(int i=0;i<numSegments;i++)
totalBytes += segmentInfos.info(i).sizeInBytes();
if (3*freeableBytes > totalBytes)
return true;
else
return false;
}
}
class IndexWriter {
// Apply buffered deletes to all segments.
private final synchronized boolean applyDeletes() throws CorruptIndexException, IOException {
assert testPoint("startApplyDeletes");
SegmentInfos rollback = (SegmentInfos) segmentInfos.clone();
boolean success = false;
boolean changed;
try {
changed = docWriter.applyDeletes(segmentInfos);
success = true;
} finally {
if (!success) {
if (infoStream != null)
message("hit exception flushing deletes");
// Carefully remove any partially written .del
// files
final int size = rollback.size();
for(int i=0;i<size;i++) {
final String newDelFileName = segmentInfos.info(i).getDelFileName();
final String delFileName = rollback.info(i).getDelFileName();
if (newDelFileName != null && !newDelFileName.equals(delFileName))
deleter.deleteFile(newDelFileName);
}
// Fully replace the segmentInfos since flushed
// deletes could have changed any of the
// SegmentInfo instances:
segmentInfos.clear();
segmentInfos.addAll(rollback);
}
}
if (changed)
checkpoint();
return changed;
}
}
class IndexWriter {
// utility routines for tests
SegmentInfo newestSegment() {
return segmentInfos.info(segmentInfos.size()-1);
}
}
|