package org.apache.lucene.codecs.lucene40;

import java.io.IOException;
import java.util.Set;

import org.apache.lucene.codecs.LiveDocsFormat;
import org.apache.lucene.index.IndexFileNames;
import org.apache.lucene.index.SegmentInfo;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.MutableBits;

public class Lucene40LiveDocsFormat extends LiveDocsFormat {

  /** Extension of deletes */
  static final String DELETES_EXTENSION = "del";
  
  @Override
  public MutableBits newLiveDocs(int size) throws IOException {
    BitVector bitVector = new BitVector(size);
    bitVector.invertAll();
    return bitVector;
  }

  @Override
  public Bits readLiveDocs(Directory dir, SegmentInfo info, IOContext context) throws IOException {
    String filename = IndexFileNames.fileNameFromGeneration(info.name, DELETES_EXTENSION, info.getDelGen());
    return new BitVector(dir, filename, context);
  }

  @Override
  public void writeLiveDocs(MutableBits bits, Directory dir, SegmentInfo info, IOContext context) throws IOException {
    // nocommit: this api is ugly...
    String filename = IndexFileNames.fileNameFromGeneration(info.name, DELETES_EXTENSION, info.getDelGen());
    
    // nocommit: is it somehow cleaner to still have IW do this try/finally/delete stuff and add abort() instead?
    boolean success = false;
    try {
      ((BitVector)bits).write(dir, filename, context);
      success = true;
    } finally {
      if (!success) {
        try {
          dir.deleteFile(filename);
        } catch (Throwable t) {
          // suppress this so we keep throwing the
          // original exception
        }
      }
    }
  }

  @Override
  public void separateFiles(Directory dir, SegmentInfo info, Set<String> files) throws IOException {
    if (info.hasDeletions()) {
      files.add(IndexFileNames.fileNameFromGeneration(info.name, DELETES_EXTENSION, info.getDelGen()));
    }
  }
}

@@ -347,16 +347,55 @@
 l.Bits;%0A
+import org.apache.lucene.util.IOUtils;%0A
 import o
@@ -1330,104 +1330,35 @@
 it: 
-is it somehow cleaner to still have IW do this try/finally/delete stuff and add abort() instead?
+test if we really need this
 %0A   
@@ -1520,174 +1520,61 @@
     
-try %7B%0A          dir.deleteFile(filename);%0A        %7D catch (Throwable t) %7B%0A          // suppress this so we keep throwing the%0A          // original exception%0A        %7D
+IOUtils.deleteFilesIgnoringExceptions(dir, filename);
 %0A   

clean this up to use IOUtils method