Index: CHANGES.txt
===================================================================
--- CHANGES.txt	(revision 770782)
+++ CHANGES.txt	(working copy)
@@ -283,6 +283,12 @@
 22. LUCENE-1605: Added BitVector.subset().  (Jeremy Volkman via Mike
     McCandless)
 
+23. LUCENE-1313: Near realtime search enhancement.  IndexWriter
+    accepts a RAMDirectory that segments are flushed to (instead of 
+    to the real directory).  This can be faster than flushing and 
+    merging small segments using FSDirectory.
+    (Jason Rutherglen via Mike McCandless)
+
 Optimizations
 
  1. LUCENE-1427: Fixed QueryWrapperFilter to not waste time computing
Index: src/java/org/apache/lucene/index/DirectoryIndexReader.java
===================================================================
--- src/java/org/apache/lucene/index/DirectoryIndexReader.java	(revision 770782)
+++ src/java/org/apache/lucene/index/DirectoryIndexReader.java	(working copy)
@@ -400,7 +400,7 @@
         segmentInfos.setUserData(commitUserData);
         // Default deleter (for backwards compatibility) is
         // KeepOnlyLastCommitDeleter:
-        IndexFileDeleter deleter =  new IndexFileDeleter(directory,
+        IndexFileDeleter deleter =  new IndexFileDeleter(directory, null,
                                                          deletionPolicy == null ? new KeepOnlyLastCommitDeletionPolicy() : deletionPolicy,
                                                          segmentInfos, null, null);
 
Index: src/java/org/apache/lucene/index/SegmentInfo.java
===================================================================
--- src/java/org/apache/lucene/index/SegmentInfo.java	(revision 770782)
+++ src/java/org/apache/lucene/index/SegmentInfo.java	(working copy)
@@ -549,7 +549,7 @@
    * modify it.
    */
 
-  public List files() throws IOException {
+  public synchronized List files() throws IOException {
 
     if (files != null) {
       // Already cached:
@@ -644,7 +644,7 @@
 
   /* Called whenever any change is made that affects which
    * files this segment has. */
-  private void clearFiles() {
+  private synchronized void clearFiles() {
     files = null;
     sizeInBytes = -1;
   }
Index: src/java/org/apache/lucene/index/DocFieldProcessorPerThread.java
===================================================================
--- src/java/org/apache/lucene/index/DocFieldProcessorPerThread.java	(revision 770782)
+++ src/java/org/apache/lucene/index/DocFieldProcessorPerThread.java	(working copy)
@@ -23,6 +23,7 @@
 import java.io.IOException;
 import org.apache.lucene.document.Document;
 import org.apache.lucene.document.Fieldable;
+import org.apache.lucene.store.Directory;
 
 /**
  * Gathers all Fieldables for a document under the same
@@ -146,7 +147,7 @@
   }
 
   public DocumentsWriter.DocWriter processDocument() throws IOException {
-
+    Directory dir = this.docState.docWriter.writer.getFlushDirectory();
     consumer.startDocument();
     final Document doc = docState.doc;
 
Index: src/java/org/apache/lucene/index/MergePolicy.java
===================================================================
--- src/java/org/apache/lucene/index/MergePolicy.java	(revision 770782)
+++ src/java/org/apache/lucene/index/MergePolicy.java	(working copy)
@@ -85,6 +85,7 @@
     final boolean useCompoundFile;
     boolean aborted;
     Throwable error;
+    Directory directory;
 
     public OneMerge(SegmentInfos segments, boolean useCompoundFile) {
       if (0 == segments.size())
Index: src/java/org/apache/lucene/index/LogMergePolicy.java
===================================================================
--- src/java/org/apache/lucene/index/LogMergePolicy.java	(revision 770782)
+++ src/java/org/apache/lucene/index/LogMergePolicy.java	(working copy)
@@ -19,7 +19,9 @@
 
 import java.io.IOException;
 import java.util.Set;
+import java.util.List;
 
+import org.apache.lucene.index.MergePolicy.OneMerge;
 import org.apache.lucene.store.Directory;
 
 /** <p>This class implements a {@link MergePolicy} that tries
@@ -301,7 +303,7 @@
 
     return spec;
   }
-
+  
   /** Checks if any merges are now necessary and returns a
    *  {@link MergePolicy.MergeSpecification} if so.  A merge
    *  is necessary when there are more than {@link
@@ -309,8 +311,7 @@
    *  multiple levels have too many segments, this method
    *  will return multiple merges, allowing the {@link
    *  MergeScheduler} to use concurrency. */
-  public MergeSpecification findMerges(SegmentInfos infos, IndexWriter writer) throws IOException {
-
+  public MergeSpecification findMerges(SegmentInfos infos, IndexWriter writer) throws IOException {  
     final int numSegments = infos.size();
     this.writer = writer;
     if (verbose())
@@ -321,8 +322,6 @@
     float[] levels = new float[numSegments];
     final float norm = (float) Math.log(mergeFactor);
 
-    final Directory directory = writer.getDirectory();
-
     for(int i=0;i<numSegments;i++) {
       final SegmentInfo info = infos.info(i);
       long size = size(info);
Index: src/java/org/apache/lucene/index/StoredFieldsWriter.java
===================================================================
--- src/java/org/apache/lucene/index/StoredFieldsWriter.java	(revision 770782)
+++ src/java/org/apache/lucene/index/StoredFieldsWriter.java	(working copy)
@@ -20,6 +20,7 @@
 import java.util.Map;
 import java.io.IOException;
 import org.apache.lucene.store.RAMOutputStream;
+import org.apache.lucene.store.Directory;
 import org.apache.lucene.util.ArrayUtil;
 
 /** This is a DocFieldConsumer that writes stored fields. */
@@ -62,7 +63,8 @@
       final String docStoreSegment = docWriter.getDocStoreSegment();
       if (docStoreSegment != null) {
         assert docStoreSegment != null;
-        fieldsWriter = new FieldsWriter(docWriter.directory,
+        Directory directory = docWriter.writer.getFlushDirectory();
+        fieldsWriter = new FieldsWriter(directory,
                                         docStoreSegment,
                                         fieldInfos);
         docWriter.addOpenFile(docStoreSegment + "." + IndexFileNames.FIELDS_EXTENSION);
Index: src/java/org/apache/lucene/index/SegmentInfos.java
===================================================================
--- src/java/org/apache/lucene/index/SegmentInfos.java	(revision 770782)
+++ src/java/org/apache/lucene/index/SegmentInfos.java	(working copy)
@@ -818,6 +818,21 @@
     }
     return files;
   }
+  
+  public Collection files(Directory dir, Directory ramDir, boolean includeSegmentsFile) throws IOException {
+    HashSet files = new HashSet();
+    if (includeSegmentsFile) {
+      files.add(getCurrentSegmentFileName());
+    }
+    final int size = size();
+    for(int i=0;i<size;i++) {
+      final SegmentInfo info = info(i);
+      if (info.dir == dir || info.dir == ramDir) {
+        files.addAll(info(i).files());
+      }
+    }
+    return files;
+  }
 
   public final void finishCommit(Directory dir) throws IOException {
     if (pendingOutput == null)
@@ -918,10 +933,10 @@
   }
 
   // Used only for testing
-  boolean hasExternalSegments(Directory dir) {
+  boolean hasExternalSegments(Directory dir, Directory ramDir) {
     final int numSegments = size();
     for(int i=0;i<numSegments;i++)
-      if (info(i).dir != dir)
+      if (info(i).dir != dir && info(i).dir != ramDir)
         return true;
     return false;
   }
Index: src/java/org/apache/lucene/index/SegmentMerger.java
===================================================================
--- src/java/org/apache/lucene/index/SegmentMerger.java	(revision 770782)
+++ src/java/org/apache/lucene/index/SegmentMerger.java	(working copy)
@@ -78,7 +78,8 @@
   }
 
   SegmentMerger(IndexWriter writer, String name, MergePolicy.OneMerge merge) {
-    directory = writer.getDirectory();
+    if (merge != null) directory = merge.directory;
+    if (directory == null) directory = writer.getDirectory();
     segment = name;
     if (merge != null)
       checkAbort = new CheckAbort(merge, directory);
Index: src/java/org/apache/lucene/index/TermVectorsTermsWriter.java
===================================================================
--- src/java/org/apache/lucene/index/TermVectorsTermsWriter.java	(revision 770782)
+++ src/java/org/apache/lucene/index/TermVectorsTermsWriter.java	(working copy)
@@ -19,6 +19,7 @@
 
 import org.apache.lucene.store.IndexOutput;
 import org.apache.lucene.store.RAMOutputStream;
+import org.apache.lucene.store.Directory;
 import org.apache.lucene.util.ArrayUtil;
 
 import java.io.IOException;
@@ -152,9 +153,10 @@
       // vector output files, we must abort this segment
       // because those files will be in an unknown
       // state:
-      tvx = docWriter.directory.createOutput(docStoreSegment + "." + IndexFileNames.VECTORS_INDEX_EXTENSION);
-      tvd = docWriter.directory.createOutput(docStoreSegment +  "." + IndexFileNames.VECTORS_DOCUMENTS_EXTENSION);
-      tvf = docWriter.directory.createOutput(docStoreSegment +  "." + IndexFileNames.VECTORS_FIELDS_EXTENSION);
+      Directory dir = docWriter.writer.getFlushDirectory();
+      tvx = dir.createOutput(docStoreSegment + "." + IndexFileNames.VECTORS_INDEX_EXTENSION);
+      tvd = dir.createOutput(docStoreSegment +  "." + IndexFileNames.VECTORS_DOCUMENTS_EXTENSION);
+      tvf = dir.createOutput(docStoreSegment +  "." + IndexFileNames.VECTORS_FIELDS_EXTENSION);
       
       tvx.writeInt(TermVectorsReader.FORMAT_CURRENT);
       tvd.writeInt(TermVectorsReader.FORMAT_CURRENT);
Index: src/java/org/apache/lucene/index/DocumentsWriter.java
===================================================================
--- src/java/org/apache/lucene/index/DocumentsWriter.java	(revision 770782)
+++ src/java/org/apache/lucene/index/DocumentsWriter.java	(working copy)
@@ -25,6 +25,7 @@
 import org.apache.lucene.search.Scorer;
 import org.apache.lucene.search.Weight;
 import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.RAMDirectory;
 import org.apache.lucene.store.AlreadyClosedException;
 import org.apache.lucene.util.ArrayUtil;
 
@@ -110,7 +111,6 @@
 final class DocumentsWriter {
 
   IndexWriter writer;
-  Directory directory;
 
   String segment;                         // Current segment we are working on
   private String docStoreSegment;         // Current doc-store segment we are writing
@@ -155,7 +155,7 @@
       return docWriter.writer.testPoint(name);
     }
   }
-
+  
   /** Consumer returns this on each doc.  This holds any
    *  state that must be flushed synchronized "in docID
    *  order".  We gather these and flush them in order. */
@@ -259,8 +259,7 @@
 
   private boolean closed;
 
-  DocumentsWriter(Directory directory, IndexWriter writer, IndexingChain indexingChain) throws IOException {
-    this.directory = directory;
+  DocumentsWriter(IndexWriter writer, IndexingChain indexingChain) throws IOException {
     this.writer = writer;
     this.similarity = writer.getSimilarity();
     flushedDocCount = writer.maxDoc();
@@ -536,6 +535,7 @@
 
   synchronized private void initFlushState(boolean onlyDocStore) {
     initSegmentName(onlyDocStore);
+    Directory directory = writer.getFlushDirectory();
     flushState = new SegmentWriteState(this, directory, segment, docStoreSegment, numDocsInRAM, numDocsInStore, writer.getTermIndexInterval());
   }
 
@@ -549,7 +549,8 @@
     assert nextDocID == numDocsInRAM;
     assert waitQueue.numWaiting == 0;
     assert waitQueue.waitingBytes == 0;
-
+    
+    Directory directory = writer.getFlushDirectory();
     initFlushState(false);
 
     docStoreOffset = numDocsInStore;
@@ -574,7 +575,7 @@
       consumer.flush(threads, flushState);
 
       if (infoStream != null) {
-        final long newSegmentSize = segmentSize(flushState.segmentName);
+        final long newSegmentSize = segmentSize(flushState.segmentName, directory);
         String message = "  oldRAMSize=" + numBytesUsed +
           " newFlushedSize=" + newSegmentSize +
           " docs/MB=" + nf.format(numDocsInRAM/(newSegmentSize/1024./1024.)) +
@@ -600,7 +601,7 @@
   }
 
   /** Build compound file for the segment we just flushed */
-  void createCompoundFile(String segment) throws IOException {
+  void createCompoundFile(String segment, Directory directory) throws IOException {
     
     CompoundFileWriter cfsWriter = new CompoundFileWriter(directory, segment + "." + IndexFileNames.COMPOUND_FILE_EXTENSION);
     Iterator it = flushState.flushedFiles.iterator();
@@ -763,7 +764,6 @@
       // This call is not synchronized and does all the
       // work
       final DocWriter perDoc = state.consumer.processDocument();
-        
       // This call is synchronized but fast
       finishDocument(state, perDoc);
       success = true;
@@ -914,11 +914,13 @@
 
     int docStart = 0;
     boolean any = false;
+    Directory flushDir = writer.getFlushDirectory();
+    Directory directory = writer.getDirectory();
     for (int i = 0; i < infosEnd; i++) {
 
       // Make sure we never attempt to apply deletes to
       // segment in external dir
-      assert infos.info(i).dir == directory;
+      assert infos.info(i).dir == directory || infos.info(i).dir == flushDir;
 
       SegmentReader reader = writer.readerPool.get(infos.info(i), false);
       try {
@@ -1103,7 +1105,7 @@
   long getRAMUsed() {
     return numBytesUsed;
   }
-
+  
   long numBytesAlloc;
   long numBytesUsed;
 
@@ -1111,10 +1113,9 @@
 
   // TODO FI: this is not flexible -- we can't hardwire
   // extensions in here:
-  private long segmentSize(String segmentName) throws IOException {
+  private long segmentSize(String segmentName, Directory directory) throws IOException {
     // Used only when infoStream != null
     assert infoStream != null;
-    
     long size = directory.fileLength(segmentName + ".tii") +
       directory.fileLength(segmentName + ".tis") +
       directory.fileLength(segmentName + ".frq") +
@@ -1123,10 +1124,22 @@
     final String normFileName = segmentName + ".nrm";
     if (directory.fileExists(normFileName))
       size += directory.fileLength(normFileName);
-
+    
     return size;
   }
-
+  
+  public long fileLength(String name) throws IOException {
+    Directory dir = writer.getDirectory();
+    Directory dir2 = writer.getFlushDirectory();
+    return fileLength(name, dir, dir2);
+  }
+  
+  public static long fileLength(String name, Directory dir1, Directory dir2) throws IOException {
+    if (dir1.fileExists(name)) {
+      return dir1.fileLength(name);
+    } else return dir2.fileLength(name);
+  }
+  
   // Coarse estimates used to measure RAM usage of buffered deletes
   final static int OBJECT_HEADER_BYTES = 8;
   final static int POINTER_NUM_BYTE = 4;
Index: src/java/org/apache/lucene/index/MultiSegmentReader.java
===================================================================
--- src/java/org/apache/lucene/index/MultiSegmentReader.java	(revision 770782)
+++ src/java/org/apache/lucene/index/MultiSegmentReader.java	(working copy)
@@ -84,13 +84,16 @@
     final int numSegments = infos.size();
     SegmentReader[] readers = new SegmentReader[numSegments];
     final Directory dir = writer.getDirectory();
+    final Directory flushDir = writer.getFlushDirectory();
     int upto = 0;
 
     for (int i=0;i<numSegments;i++) {
       boolean success = false;
       try {
         final SegmentInfo info = infos.info(upto);
-        if (info.dir == dir) {
+        // accept the regular and ram dirs (not the external dir 
+        // from addIndexesNoOptimize)
+        if (info.dir == dir || info.dir == flushDir) {
           readers[upto++] = writer.readerPool.getReadOnlyClone(info, true);
         }
         success = true;
Index: src/java/org/apache/lucene/index/IndexWriter.java
===================================================================
--- src/java/org/apache/lucene/index/IndexWriter.java	(revision 770782)
+++ src/java/org/apache/lucene/index/IndexWriter.java	(working copy)
@@ -28,6 +28,8 @@
 import org.apache.lucene.store.LockObtainFailedException;
 import org.apache.lucene.store.AlreadyClosedException;
 import org.apache.lucene.store.BufferedIndexInput;
+import org.apache.lucene.store.RAMDirectory;
+import org.apache.lucene.store.FileSwitchDirectory;
 import org.apache.lucene.util.Constants;
 
 import java.io.File;
@@ -286,6 +288,18 @@
    * set (see {@link #setInfoStream}).
    */
   public final static int MAX_TERM_LENGTH = DocumentsWriter.MAX_TERM_LENGTH;
+  
+  /**
+   * File extensions for the FileSwitchDirectory used when NRT is on
+   */
+  public final static Set SWITCH_FILE_EXTS = new HashSet();
+  static {
+    SWITCH_FILE_EXTS.add("fdx");
+    SWITCH_FILE_EXTS.add("fdt");
+    SWITCH_FILE_EXTS.add("tvx");
+    SWITCH_FILE_EXTS.add("tvf");
+    SWITCH_FILE_EXTS.add("tvd");
+  }
 
   /**
    * Default for {@link #getMaxSyncPauseSeconds}.  On
@@ -353,6 +367,7 @@
   private HashSet mergingSegments = new HashSet();
 
   private MergePolicy mergePolicy = new LogByteSizeMergePolicy();
+  private MergePolicy ramMergePolicy = mergePolicy;
   private MergeScheduler mergeScheduler = new ConcurrentMergeScheduler();
   private LinkedList pendingMerges = new LinkedList();
   private Set runningMerges = new HashSet();
@@ -370,6 +385,7 @@
   private Thread writeThread;                     // non-null if any thread holds write lock
   final ReaderPool readerPool = new ReaderPool();
   private int upgradeCount;
+  private FileSwitchDirectory switchDirectory;
   
   // This is a "write once" variable (like the organic dye
   // on a DVD-R that may or may not be heated by a laser and
@@ -450,7 +466,7 @@
       return new ReadOnlyMultiSegmentReader(this, segmentInfos);
     }
   }
-
+  
   /** Holds shared SegmentReader instances. IndexWriter uses
    *  SegmentReaders for 1) applying deletes, 2) doing
    *  merges, 3) handing out a real-time reader.  This pool
@@ -730,6 +746,17 @@
     else
       throw new IllegalArgumentException("this method can only be called when the merge policy is the default LogMergePolicy");
   }
+  
+  /**
+   * Casts current mergePolicy to LogMergePolicy, and throws
+   * an exception if the mergePolicy is not a LogMergePolicy.
+   */
+  private LogMergePolicy getRamLogMergePolicy() {
+    if (ramMergePolicy instanceof LogMergePolicy)
+      return (LogMergePolicy) ramMergePolicy;
+    else
+      throw new IllegalArgumentException("this method can only be called when the merge policy is the default LogMergePolicy");
+  }
 
   /** <p>Get the current setting of whether newly flushed
    *  segments will use the compound file format.  Note that
@@ -847,7 +874,7 @@
    */
   public IndexWriter(String path, Analyzer a, boolean create, MaxFieldLength mfl)
        throws CorruptIndexException, LockObtainFailedException, IOException {
-    init(FSDirectory.getDirectory(path), a, create, true, null, false, mfl.getLimit(), null, null);
+    init(FSDirectory.getDirectory(path), null, a, create, true, null, false, mfl.getLimit(), null, null);
   }
 
   /**
@@ -876,7 +903,7 @@
    */
   public IndexWriter(String path, Analyzer a, boolean create)
        throws CorruptIndexException, LockObtainFailedException, IOException {
-    init(FSDirectory.getDirectory(path), a, create, true, null, true, DEFAULT_MAX_FIELD_LENGTH, null, null);
+    init(FSDirectory.getDirectory(path), null, a, create, true, null, true, DEFAULT_MAX_FIELD_LENGTH, null, null);
   }
 
   /**
@@ -907,7 +934,7 @@
    */
   public IndexWriter(File path, Analyzer a, boolean create, MaxFieldLength mfl)
        throws CorruptIndexException, LockObtainFailedException, IOException {
-    init(FSDirectory.getDirectory(path), a, create, true, null, false, mfl.getLimit(), null, null);
+    init(FSDirectory.getDirectory(path), null, a, create, true, null, false, mfl.getLimit(), null, null);
   }
 
   /**
@@ -936,7 +963,7 @@
    */
   public IndexWriter(File path, Analyzer a, boolean create)
        throws CorruptIndexException, LockObtainFailedException, IOException {
-    init(FSDirectory.getDirectory(path), a, create, true, null, true, DEFAULT_MAX_FIELD_LENGTH, null, null);
+    init(FSDirectory.getDirectory(path), null, a, create, true, null, true, DEFAULT_MAX_FIELD_LENGTH, null, null);
   }
 
   /**
@@ -967,7 +994,7 @@
    */
   public IndexWriter(Directory d, Analyzer a, boolean create, MaxFieldLength mfl)
        throws CorruptIndexException, LockObtainFailedException, IOException {
-    init(d, a, create, false, null, false, mfl.getLimit(), null, null);
+    init(d, null, a, create, false, null, false, mfl.getLimit(), null, null);
   }
 
   /**
@@ -995,7 +1022,7 @@
    */
   public IndexWriter(Directory d, Analyzer a, boolean create)
        throws CorruptIndexException, LockObtainFailedException, IOException {
-    init(d, a, create, false, null, true, DEFAULT_MAX_FIELD_LENGTH, null, null);
+    init(d, null, a, create, false, null, true, DEFAULT_MAX_FIELD_LENGTH, null, null);
   }
 
   /**
@@ -1126,6 +1153,34 @@
     throws CorruptIndexException, LockObtainFailedException, IOException {
     init(d, a, false, null, false, mfl.getLimit(), null, null);
   }
+  
+  /**
+   * Constructs an IndexWriter for the index in
+   * <code>d</code>, first creating it if it does not
+   * already exist.  Text will be analyzed with
+   * <code>a</code>.
+   *
+   * <p><b>NOTE</b>: autoCommit (see <a
+   * href="#autoCommit">above</a>) is set to false with this
+   * constructor.
+   *
+   * @param d the index directory
+   * @param ramDir ram directory
+   * @param a the analyzer to use
+   * @param mfl Maximum field length in number of terms/tokens: LIMITED, UNLIMITED, or user-specified
+   *   via the MaxFieldLength constructor.
+   * @throws CorruptIndexException if the index is corrupt
+   * @throws LockObtainFailedException if another writer
+   *  has this index open (<code>write.lock</code> could not
+   *  be obtained)
+   * @throws IOException if the directory cannot be
+   *  read/written to or if there is any other low-level
+   *  IO error
+   */
+  public IndexWriter(Directory d, Directory ramDir, Analyzer a, MaxFieldLength mfl)
+    throws CorruptIndexException, LockObtainFailedException, IOException {
+    init(d, ramDir, a, false, null, false, mfl.getLimit(), null, null);
+  }
 
   /**
    * Constructs an IndexWriter for the index in
@@ -1205,7 +1260,7 @@
    */
   public IndexWriter(Directory d, boolean autoCommit, Analyzer a, boolean create)
        throws CorruptIndexException, LockObtainFailedException, IOException {
-    init(d, a, create, false, null, autoCommit, DEFAULT_MAX_FIELD_LENGTH, null, null);
+    init(d, null, a, create, false, null, autoCommit, DEFAULT_MAX_FIELD_LENGTH, null, null);
   }
 
   /**
@@ -1292,7 +1347,7 @@
    */
   public IndexWriter(Directory d, Analyzer a, boolean create, IndexDeletionPolicy deletionPolicy, MaxFieldLength mfl)
        throws CorruptIndexException, LockObtainFailedException, IOException {
-    init(d, a, create, false, deletionPolicy, false, mfl.getLimit(), null, null);
+    init(d, null, a, create, false, deletionPolicy, false, mfl.getLimit(), null, null);
   }
   
   /**
@@ -1329,7 +1384,7 @@
    */
   IndexWriter(Directory d, Analyzer a, boolean create, IndexDeletionPolicy deletionPolicy, MaxFieldLength mfl, IndexingChain indexingChain, IndexCommit commit)
        throws CorruptIndexException, LockObtainFailedException, IOException {
-    init(d, a, create, false, deletionPolicy, false, mfl.getLimit(), indexingChain, commit);
+    init(d, null, a, create, false, deletionPolicy, false, mfl.getLimit(), indexingChain, commit);
   }
   
   /**
@@ -1362,9 +1417,9 @@
    */
   public IndexWriter(Directory d, boolean autoCommit, Analyzer a, boolean create, IndexDeletionPolicy deletionPolicy)
           throws CorruptIndexException, LockObtainFailedException, IOException {
-    init(d, a, create, false, deletionPolicy, autoCommit, DEFAULT_MAX_FIELD_LENGTH, null, null);
+    init(d, null, a, create, false, deletionPolicy, autoCommit, DEFAULT_MAX_FIELD_LENGTH, null, null);
   }
-
+  
   /**
    * Expert: constructs an IndexWriter on specific commit
    * point, with a custom {@link IndexDeletionPolicy}, for
@@ -1403,25 +1458,78 @@
    */
   public IndexWriter(Directory d, Analyzer a, IndexDeletionPolicy deletionPolicy, MaxFieldLength mfl, IndexCommit commit)
        throws CorruptIndexException, LockObtainFailedException, IOException {
-    init(d, a, false, false, deletionPolicy, false, mfl.getLimit(), null, commit);
+    init(d, null, a, false, false, deletionPolicy, false, mfl.getLimit(), null, commit);
   }
+  
+  /**
+   * Expert: constructs an IndexWriter on specific commit
+   * point, with a custom {@link IndexDeletionPolicy}, for
+   * the index in <code>d</code>.  Text will be analyzed
+   * with <code>a</code>.
+   *
+   * <p> This is only meaningful if you've used a {@link
+   * IndexDeletionPolicy} in that past that keeps more than
+   * just the last commit.
+   * 
+   * <p>This operation is similar to {@link #rollback()},
+   * except that method can only rollback what's been done
+   * with the current instance of IndexWriter since its last
+   * commit, whereas this method can rollback to an
+   * arbitrary commit point from the past, assuming the
+   * {@link IndexDeletionPolicy} has preserved past
+   * commits.
+   *
+   * <p><b>NOTE</b>: autoCommit (see <a
+   * href="#autoCommit">above</a>) is set to false with this
+   * constructor.
+   *
+   * @param d the index directory
+   * @param ramDirectory ram directory
+   * @param a the analyzer to use
+   * @param deletionPolicy see <a href="#deletionPolicy">above</a>
+   * @param mfl whether or not to limit field lengths, value is in number of terms/tokens.  See {@link org.apache.lucene.index.IndexWriter.MaxFieldLength}.
+   * @param commit which commit to open
+   * @throws CorruptIndexException if the index is corrupt
+   * @throws LockObtainFailedException if another writer
+   *  has this index open (<code>write.lock</code> could not
+   *  be obtained)
+   * @throws IOException if the directory cannot be read/written to, or
+   *  if it does not exist and <code>create</code> is
+   *  <code>false</code> or if there is any other low-level
+   *  IO error
+   */
+  public IndexWriter(Directory d, Directory ramDirectory, Analyzer a, IndexDeletionPolicy deletionPolicy, MaxFieldLength mfl, IndexCommit commit)
+       throws CorruptIndexException, LockObtainFailedException, IOException {
+    init(d, ramDirectory, a, false, false, deletionPolicy, false, mfl.getLimit(), null, commit);
+  }
 
   private void init(Directory d, Analyzer a, boolean closeDir, IndexDeletionPolicy deletionPolicy, 
+      boolean autoCommit, int maxFieldLength, IndexingChain indexingChain, IndexCommit commit)
+     throws CorruptIndexException, LockObtainFailedException, IOException {
+    init(d, null, a, closeDir, deletionPolicy, autoCommit, maxFieldLength, indexingChain, commit);
+  }
+  
+  private void init(Directory d, Directory ramDirectory, Analyzer a, boolean closeDir, IndexDeletionPolicy deletionPolicy, 
                     boolean autoCommit, int maxFieldLength, IndexingChain indexingChain, IndexCommit commit)
     throws CorruptIndexException, LockObtainFailedException, IOException {
     if (IndexReader.indexExists(d)) {
-      init(d, a, false, closeDir, deletionPolicy, autoCommit, maxFieldLength, indexingChain, commit);
+      init(d, ramDirectory, a, false, closeDir, deletionPolicy, autoCommit, maxFieldLength, indexingChain, commit);
     } else {
-      init(d, a, true, closeDir, deletionPolicy, autoCommit, maxFieldLength, indexingChain, commit);
+      init(d, ramDirectory, a, true, closeDir, deletionPolicy, autoCommit, maxFieldLength, indexingChain, commit);
     }
   }
 
-  private void init(Directory d, Analyzer a, final boolean create, boolean closeDir, 
+  private void init(Directory d, Directory ramDirectory, Analyzer a, final boolean create, boolean closeDir, 
                     IndexDeletionPolicy deletionPolicy, boolean autoCommit, int maxFieldLength,
                     IndexingChain indexingChain, IndexCommit commit)
     throws CorruptIndexException, LockObtainFailedException, IOException {
     this.closeDir = closeDir;
     directory = d;
+    if (ramDirectory != null) {
+      this.switchDirectory = new FileSwitchDirectory(SWITCH_FILE_EXTS, directory, ramDirectory, false);
+      getRamLogMergePolicy().setUseCompoundDocStore(false);
+      getRamLogMergePolicy().setUseCompoundFile(false);
+    }
     analyzer = a;
     setMessageID(defaultInfoStream);
     this.maxFieldLength = maxFieldLength;
@@ -1493,13 +1601,13 @@
       this.autoCommit = autoCommit;
       setRollbackSegmentInfos(segmentInfos);
 
-      docWriter = new DocumentsWriter(directory, this, indexingChain);
+      docWriter = new DocumentsWriter(this, indexingChain);
       docWriter.setInfoStream(infoStream);
       docWriter.setMaxFieldLength(maxFieldLength);
 
       // Default deleter (for backwards compatibility) is
       // KeepOnlyLastCommitDeleter:
-      deleter = new IndexFileDeleter(directory,
+      deleter = new IndexFileDeleter(directory, switchDirectory, 
                                      deletionPolicy == null ? new KeepOnlyLastCommitDeletionPolicy() : deletionPolicy,
                                      segmentInfos, infoStream, docWriter);
 
@@ -1523,16 +1631,42 @@
       throw e;
     }
   }
-
+  
+  public Directory getFlushDirectory() {
+    if (switchDirectory != null) {
+      return switchDirectory;
+    } else return directory;
+  }
+  
+  public Directory getRAMDirectory() {
+    return switchDirectory.getOtherDir();
+  }
+  
   private synchronized void setRollbackSegmentInfos(SegmentInfos infos) {
     rollbackSegmentInfos = (SegmentInfos) infos.clone();
-    assert !rollbackSegmentInfos.hasExternalSegments(directory);
+    assert !rollbackSegmentInfos.hasExternalSegments(directory, getFlushDirectory());
     rollbackSegments = new HashMap();
     final int size = rollbackSegmentInfos.size();
     for(int i=0;i<size;i++)
       rollbackSegments.put(rollbackSegmentInfos.info(i), new Integer(i));
   }
+  
+  /**
+   * Expert: set the ram directory merge policy used by this writer.
+   */
+  public void setRAMMergePolicy(MergePolicy mp) {
+    ensureOpen();
+    if (mp == null)
+      throw new NullPointerException("MergePolicy must be non-null");
 
+    if (ramMergePolicy != mp)
+      ramMergePolicy.close();
+    ramMergePolicy = mp;
+    pushMaxBufferedDocs();
+    if (infoStream != null)
+      message("setMergePolicy " + mp);
+  }
+  
   /**
    * Expert: set the merge policy used by this writer.
    */
@@ -2149,7 +2283,8 @@
       final String compoundFileName = docStoreSegment + "." + IndexFileNames.COMPOUND_FILE_STORE_EXTENSION;
 
       try {
-        CompoundFileWriter cfsWriter = new CompoundFileWriter(directory, compoundFileName);
+        Directory dir = getFlushDirectory();
+        CompoundFileWriter cfsWriter = new CompoundFileWriter(dir, compoundFileName);
         final Iterator it = docWriter.closedFiles().iterator();
         while(it.hasNext())
           cfsWriter.addFile((String) it.next());
@@ -2531,7 +2666,12 @@
       handleOOM(oom, "updateDocument");
     }
   }
-
+  
+  //for test purpose
+  final synchronized SegmentInfos getSegmentInfos() {
+    return segmentInfos;
+  }
+  
   // for test purpose
   final synchronized int getSegmentCount(){
     return segmentInfos.size();
@@ -2892,13 +3032,47 @@
   private final void maybeMerge(boolean optimize) throws CorruptIndexException, IOException {
     maybeMerge(1, optimize);
   }
-
+  
+  long getRamBufferSizeLong() {
+    return (long)(getRAMBufferSizeMB()*(double)1024*(double)1024);
+  }
+  
   private final void maybeMerge(int maxNumSegmentsOptimize, boolean optimize) throws CorruptIndexException, IOException {
-    updatePendingMerges(maxNumSegmentsOptimize, optimize);
+    updatePendingMerges(maxNumSegmentsOptimize, optimize, false);
     mergeScheduler.merge(this);
   }
-
-  private synchronized void updatePendingMerges(int maxNumSegmentsOptimize, boolean optimize)
+  
+  void updatePendingMerges(int maxNumSegmentsOptimize, boolean optimize, boolean forceToDir) throws IOException {
+    if (switchDirectory != null) {
+      SegmentInfos ramInfos = getRamDirSegmentInfos();
+      long ramSegSize = size(ramInfos);
+      long totalRamUsed = ramSegSize + docWriter.getRAMUsed();
+      //System.out.println("totalRamUsed:"+totalRamUsed+" rambufsize:"+getRamBufferSizeLong());
+      if (forceToDir || (totalRamUsed > getRamBufferSizeLong())) {
+        // call updatePendingMerges, optimize the ramInfos to directory
+        updatePendingMerges(1, true, mergePolicy, ramInfos, directory);
+        updatePendingMerges(maxNumSegmentsOptimize, optimize, mergePolicy, segmentInfos, directory);
+      } else {
+        updatePendingMerges(maxNumSegmentsOptimize, optimize, mergePolicy, segmentInfos, directory);
+        updatePendingMerges(maxNumSegmentsOptimize, optimize, ramMergePolicy, ramInfos, switchDirectory);
+      }
+    } else {
+      updatePendingMerges(maxNumSegmentsOptimize, optimize, mergePolicy, segmentInfos, directory);
+    }
+    if (forceToDir) { // this is kind of a hack
+      mergeScheduler.merge(this);
+    }
+  }
+  
+  static long size(SegmentInfos infos) throws IOException {
+    long size = 0;
+    for (int x=0; x < infos.size(); x++) {
+      size += infos.info(x).sizeInBytes();
+    }
+    return size;
+  }
+  
+  private synchronized void updatePendingMerges(int maxNumSegmentsOptimize, boolean optimize, MergePolicy mergePolicy, SegmentInfos segmentInfos, Directory directory)
     throws CorruptIndexException, IOException {
     assert !optimize || maxNumSegmentsOptimize > 0;
 
@@ -2928,8 +3102,11 @@
 
     if (spec != null) {
       final int numMerges = spec.merges.size();
-      for(int i=0;i<numMerges;i++)
-        registerMerge((MergePolicy.OneMerge) spec.merges.get(i));
+      for(int i=0;i<numMerges;i++) {
+        MergePolicy.OneMerge merge = (MergePolicy.OneMerge) spec.merges.get(i);
+        merge.directory = directory; // set the destination directory
+        registerMerge(merge);
+      }
     }
   }
 
@@ -3552,7 +3729,7 @@
   }
 
   private boolean hasExternalSegments() {
-    return segmentInfos.hasExternalSegments(directory);
+    return segmentInfos.hasExternalSegments(directory, getFlushDirectory());
   }
 
   /* If any of our segments are using a directory != ours
@@ -3584,7 +3761,7 @@
           if (info.dir != directory) {
             done = false;
             final MergePolicy.OneMerge newMerge = new MergePolicy.OneMerge(segmentInfos.range(i, 1+i), info.getUseCompoundFile());
-
+            newMerge.directory = directory; // do we want to use the flushDir here?
             // Returns true if no running merge conflicts
             // with this one (and, records this merge as
             // pending), ie, this segment is not currently
@@ -3983,9 +4160,26 @@
     if (infoStream != null)
       message("commit: done");
   }
-
+  
+  public static SegmentInfos getInfosByDir(SegmentInfos infos, Directory dir) throws IOException {
+    SegmentInfos dirInfos = new SegmentInfos();
+    for (int x=0; x < infos.size(); x++) {
+      SegmentInfo info = infos.info(x);
+      if (info.dir == dir) {
+        dirInfos.add(info);
+      }
+    }
+    return dirInfos;
+  }
+  
+  SegmentInfos getRamDirSegmentInfos() throws IOException {
+    if (this.switchDirectory != null) 
+      return getInfosByDir(segmentInfos, switchDirectory);
+    return null;
+  }
+  
   /**
-   * Flush all in-memory buffered udpates (adds and deletes)
+   * Flush all in-memory buffered updates (adds and deletes)
    * to the Directory.
    * @param triggerMerge if true, we may merge segments (if
    *  deletes or docs were flushed) if necessary
@@ -3995,7 +4189,6 @@
    *  be flushed
    */
   protected final void flush(boolean triggerMerge, boolean flushDocStores, boolean flushDeletes) throws CorruptIndexException, IOException {
-    // We can be called during close, when closing==true, so we must pass false to ensureOpen:
     ensureOpen(false);
     if (doFlush(flushDocStores, flushDeletes) && triggerMerge)
       maybeMerge();
@@ -4129,9 +4322,10 @@
         // Create new SegmentInfo, but do not add to our
         // segmentInfos until deletes are flushed
         // successfully.
+        Directory segDir = getFlushDirectory();
         newSegment = new SegmentInfo(segment,
                                      flushedDocCount,
-                                     directory, false, true,
+                                     segDir, false, true,
                                      docStoreOffset, docStoreSegment,
                                      docStoreIsCompoundFile,    
                                      docWriter.hasProx());
@@ -4156,7 +4350,7 @@
         // Now build compound file
         boolean success = false;
         try {
-          docWriter.createCompoundFile(segment);
+          docWriter.createCompoundFile(segment, getFlushDirectory());
           success = true;
         } finally {
           if (!success) {
@@ -4200,19 +4394,22 @@
 
     int first = segmentInfos.indexOf(merge.segments.info(0));
     if (first == -1)
-      throw new MergePolicy.MergeException("could not find segment " + merge.segments.info(0).name + " in current index " + segString(), directory);
+      throw new MergePolicy.MergeException("could not find segment " + merge.segments.info(0).name + " in current index " + segString(), merge.directory);
 
     final int numSegments = segmentInfos.size();
     
     final int numSegmentsToMerge = merge.segments.size();
     for(int i=0;i<numSegmentsToMerge;i++) {
       final SegmentInfo info = merge.segments.info(i);
-
-      if (first + i >= numSegments || !segmentInfos.info(first+i).equals(info)) {
+      
+      boolean n1 = first + i >= numSegments;
+      boolean n2 = !segmentInfos.info(first+i).equals(info);
+      if (n1 || n2) {
+      //if (first + i >= numSegments || !segmentInfos.info(first+i).equals(info)) {
         if (segmentInfos.indexOf(info) == -1)
-          throw new MergePolicy.MergeException("MergePolicy selected a segment (" + info.name + ") that is not in the current index " + segString(), directory);
+          throw new MergePolicy.MergeException("MergePolicy selected a segment (" + info.name + ") that is not in the current index " + segString(), merge.directory);
         else
-          throw new MergePolicy.MergeException("MergePolicy selected non-contiguous segments to merge (" + merge.segString(directory) + " vs " + segString() + "), which IndexWriter (currently) cannot handle",
+          throw new MergePolicy.MergeException("n1: "+n1+" n2:"+n2+" MergePolicy selected non-contiguous segments to merge (" + merge.segString(merge.directory) + " vs " + segString() + "), which IndexWriter (currently) cannot handle",
                                                directory);
       }
     }
@@ -4236,7 +4433,7 @@
     final SegmentInfos sourceSegments = merge.segments;
 
     if (infoStream != null)
-      message("commitMergeDeletes " + merge.segString(directory));
+      message("commitMergeDeletes " + merge.segString(merge.directory));
 
     // Carefully merge deletes that occurred after we
     // started merging:
@@ -4305,7 +4502,7 @@
     }
 
     if (infoStream != null)
-      message("commitMerge: " + merge.segString(directory) + " index=" + segString());
+      message("commitMerge: " + merge.segString(merge.directory) + " index=" + segString());
 
     assert merge.registerDone;
 
@@ -4317,7 +4514,7 @@
     // abort this merge
     if (merge.isAborted()) {
       if (infoStream != null)
-        message("commitMerge: skipping merge " + merge.segString(directory) + ": it was aborted");
+        message("commitMerge: skipping merge " + merge.segString(merge.directory) + ": it was aborted");
 
       deleter.refresh(merge.info.name);
       return false;
@@ -4437,8 +4634,9 @@
           // This merge (and, generally, any change to the
           // segments) may now enable new merges, so we call
           // merge policy & update pending merges.
-          if (success && !merge.isAborted() && !closed && !closing)
-            updatePendingMerges(merge.maxNumSegmentsOptimize, merge.optimize);
+          if (success && !merge.isAborted() && !closed && !closing) {
+            updatePendingMerges(merge.maxNumSegmentsOptimize, merge.optimize, false);
+          }
         }
       }
     } catch (OutOfMemoryError oom) {
@@ -4461,7 +4659,7 @@
       merge.abort();
       throw new MergePolicy.MergeAbortedException("merge is aborted: " + merge.segString(directory));
     }
-
+    
     final int count = merge.segments.size();
     boolean isExternal = false;
     for(int i=0;i<count;i++) {
@@ -4470,7 +4668,7 @@
         return false;
       if (segmentInfos.indexOf(info) == -1)
         return false;
-      if (info.dir != directory)
+      if (info.dir != directory && info.dir != switchDirectory)
         isExternal = true;
     }
 
@@ -4635,19 +4833,34 @@
     // ConcurrentMergePolicy we keep deterministic segment
     // names.
     merge.info = new SegmentInfo(newSegmentName(), 0,
-                                 directory, false, true,
+                                 (merge.directory != null ? merge.directory : directory), false, true,
                                  docStoreOffset,
                                  docStoreSegment,
                                  docStoreIsCompoundFile,
                                  false);
-
+  
     // Also enroll the merged segment into mergingSegments;
     // this prevents it from getting selected for a merge
     // after our merge is done but while we are building the
     // CFS:
     mergingSegments.add(merge.info);
   }
-
+  
+  static List getSegmentNames(SegmentInfos infos) {
+    List list = new ArrayList();
+    for (int x=0; x < infos.size(); x++) {
+      list.add(infos.info(x).name);
+    }
+    return list;
+  }
+  
+  private String printDir(Directory dir) {
+    if (dir == switchDirectory) return "switchdir";
+    if (dir == directory) return "realdir";
+    if (switchDirectory != null && dir == switchDirectory.getOtherDir()) return "ramdir";
+    return "unknown";
+  }
+  
   /** 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
Index: src/java/org/apache/lucene/index/IndexFileDeleter.java
===================================================================
--- src/java/org/apache/lucene/index/IndexFileDeleter.java	(revision 770782)
+++ src/java/org/apache/lucene/index/IndexFileDeleter.java	(working copy)
@@ -102,6 +102,7 @@
 
   private PrintStream infoStream;
   private Directory directory;
+  private Directory ramDirectory;
   private IndexDeletionPolicy policy;
   private DocumentsWriter docWriter;
 
@@ -120,7 +121,12 @@
   private void message(String message) {
     infoStream.println("IFD [" + Thread.currentThread().getName() + "]: " + message);
   }
-
+  
+  public IndexFileDeleter(Directory directory, IndexDeletionPolicy policy, SegmentInfos segmentInfos, PrintStream infoStream, DocumentsWriter docWriter)
+    throws CorruptIndexException, IOException {
+    this(directory, null, policy, segmentInfos, infoStream, docWriter);
+  }
+  
   /**
    * Initialize the deleter: find all previous commits in
    * the Directory, incref the files they reference, call
@@ -129,7 +135,7 @@
    * @throws CorruptIndexException if the index is corrupt
    * @throws IOException if there is a low-level IO error
    */
-  public IndexFileDeleter(Directory directory, IndexDeletionPolicy policy, SegmentInfos segmentInfos, PrintStream infoStream, DocumentsWriter docWriter)
+  public IndexFileDeleter(Directory directory, Directory ramDirectory, IndexDeletionPolicy policy, SegmentInfos segmentInfos, PrintStream infoStream, DocumentsWriter docWriter)
     throws CorruptIndexException, IOException {
 
     this.docWriter = docWriter;
@@ -140,6 +146,7 @@
 
     this.policy = policy;
     this.directory = directory;
+    this.ramDirectory = ramDirectory;
 
     // First pass: walk the files and initialize our ref
     // counts:
@@ -185,7 +192,7 @@
               sis = null;
             }
             if (sis != null) {
-              CommitPoint commitPoint = new CommitPoint(commitsToDelete, directory, sis);
+              CommitPoint commitPoint = new CommitPoint(commitsToDelete, directory, ramDirectory, sis);
               if (sis.getGeneration() == segmentInfos.getGeneration()) {
                 currentCommitPoint = commitPoint;
               }
@@ -213,7 +220,7 @@
       }
       if (infoStream != null)
         message("forced open of current segments file " + segmentInfos.getCurrentSegmentFileName());
-      currentCommitPoint = new CommitPoint(commitsToDelete, directory, sis);
+      currentCommitPoint = new CommitPoint(commitsToDelete, directory, ramDirectory, sis);
       commits.add(currentCommitPoint);
       incRef(sis, true);
     }
@@ -304,7 +311,21 @@
    * that segment.
    */
   public void refresh(String segmentName) throws IOException {
-    String[] files = directory.listAll();
+    String[] files = null;
+    if (ramDirectory == null) {
+      files = directory.listAll();
+    } else {
+      List list = new ArrayList();
+      String[] ramFiles = ramDirectory.listAll();
+      for (int x=0; x < ramFiles.length; x++) {
+        list.add(ramFiles[x]);
+      }
+      String[] dirFiles = directory.listAll();
+      for (int x=0; x < dirFiles.length; x++) {
+        list.add(dirFiles[x]);
+      }
+      files = (String[])list.toArray(new String[0]);
+    }
     IndexFileNameFilter filter = IndexFileNameFilter.getFilter();
     String segmentPrefix1;
     String segmentPrefix2;
@@ -395,7 +416,7 @@
 
     if (isCommit) {
       // Append to our commits list:
-      commits.add(new CommitPoint(commitsToDelete, directory, segmentInfos));
+      commits.add(new CommitPoint(commitsToDelete, directory, ramDirectory, segmentInfos));
 
       // Tell policy so it can remove commits:
       policy.onCommit(commits);
@@ -434,7 +455,7 @@
   void incRef(SegmentInfos segmentInfos, boolean isCommit) throws IOException {
      // If this is a commit point, also incRef the
      // segments_N file:
-    Iterator it = segmentInfos.files(directory, isCommit).iterator();
+    Iterator it = segmentInfos.files(directory, ramDirectory, isCommit).iterator();
     while(it.hasNext()) {
       incRef((String) it.next());
     }
@@ -476,7 +497,7 @@
   }
 
   void decRef(SegmentInfos segmentInfos) throws IOException {
-    Iterator it = segmentInfos.files(directory, false).iterator();
+    Iterator it = segmentInfos.files(directory, ramDirectory, false).iterator();
     while(it.hasNext()) {
       decRef((String) it.next());
     }
@@ -509,14 +530,23 @@
         deleteFile(fileName);
     }
   }
-
+  
+  private Directory getDirectoryForFile(String name) throws IOException {
+    if (ramDirectory != null) {
+      if (ramDirectory.fileExists(name)) {
+        return ramDirectory;
+      }
+    }
+    return directory;
+  }
+  
   void deleteFile(String fileName)
        throws IOException {
     try {
       if (infoStream != null) {
         message("delete \"" + fileName + "\"");
       }
-      directory.deleteFile(fileName);
+      getDirectoryForFile(fileName).deleteFile(fileName);
     } catch (IOException e) {			  // if delete fails
       if (directory.fileExists(fileName)) {
 
@@ -587,7 +617,7 @@
     final boolean isOptimized;
     final String userData;
 
-    public CommitPoint(Collection commitsToDelete, Directory directory, SegmentInfos segmentInfos) throws IOException {
+    public CommitPoint(Collection commitsToDelete, Directory directory, Directory ramDir, SegmentInfos segmentInfos) throws IOException {
       this.directory = directory;
       this.commitsToDelete = commitsToDelete;
       userData = segmentInfos.getUserData();
@@ -598,7 +628,7 @@
       gen = segmentInfos.getGeneration();
       isOptimized = segmentInfos.size() == 1 && !segmentInfos.info(0).hasDeletions();
 
-      assert !segmentInfos.hasExternalSegments(directory);
+      assert !segmentInfos.hasExternalSegments(directory, ramDir);
     }
 
     public boolean isOptimized() {
