## org.apache.lucene.store
Class NRTCachingDirectory

    java.lang.Object
      org.apache.lucene.store.Directory
          org.apache.lucene.store.NRTCachingDirectory

All Implemented Interfaces:
  Closeable

    public class NRTCachingDirectory

extends org.apache.lucene.store.Directory

Wraps a `RAMDirectory` around any provided delegate directory, to be used during NRT search. Make sure you pull the merge scheduler using `getMergeScheduler()` and pass that to your `IndexWriter`; this class uses that to keep track of which merges are being done by which threads, to decide when to cache each written file.

This class is likely only useful in a near-real-time context, where indexing rate is lowish but reopen rate is highish, resulting in many tiny files being written. This directory keeps such segments (as well as the segments produced by merging them, as long as they are small enough), in RAM.

This is safe to use: when your app calls {IndexWriter#commit}, all cached files will be flushed from the cached and sync'd.

NOTE: this class is somewhat sneaky in its approach for spying on merges to determine the size of a merge: it records which threads are running which merges by watching ConcurrentMergeScheduler's doMerge method. While this works correctly, likely future versions of this class will take a more general approach.

Here's a simple example usage:

       Directory fsDir = FSDirectory.open(new File("/path/to/index"));
       NRTCachingDirectory cachedFSDir = new NRTCachingDirectory(fsDir, 5.0, 60.0);
       IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_32, analyzer);
       conf.setMergeScheduler(cachedFSDir.getMergeScheduler());
       IndexWriter writer = new IndexWriter(cachedFSDir, conf);

This will cache all newly flushed segments, all merges whose expected segment size is <= 5 MB, unless the net cached bytes exceeds 60 MB at which point all writes will not be cached (until the net bytes falls below 60 MB).

WARNING: This API is experimental and might change in incompatible ways in the next release.

Field Summary

Fields inherited from class org.apache.lucene.store.Directory
`isOpen, lockFactory`

Constructor Summary
`NRTCachingDirectory(org.apache.lucene.store.Directory delegate, double maxMergeSizeMB, double maxCachedMB)` We will cache a newly created output if 1) it's a flush or a merge and the estimated size of the merged segment is <= maxMergeSizeMB, and 2) the total cached bytes is <= maxCachedMB

Method Summary
`void` `clearLock(String name)`
`void` `close()` Close thius directory, which flushes any cached files to the delegate and then closes the delegate.
`org.apache.lucene.store.IndexOutput` `createOutput(String name)`
`void` `deleteFile(String name)`
`protected boolean` `doCacheWrite(String name)` Subclass can override this to customize logic; return true if this file should be written to the RAMDirectory.
`boolean` `fileExists(String name)`
`long` `fileLength(String name)`
`long` `fileModified(String name)`
`org.apache.lucene.store.LockFactory` `getLockFactory()`
`String` `getLockID()`
`org.apache.lucene.index.MergeScheduler` `getMergeScheduler()`
`String[]` `listAll()`
`String[]` `listCachedFiles()`
`org.apache.lucene.store.Lock` `makeLock(String name)`
`org.apache.lucene.store.IndexInput` `openInput(String name)`
`org.apache.lucene.store.IndexInput` `openInput(String name, int bufferSize)`
`void` `setLockFactory(org.apache.lucene.store.LockFactory lf)`
`long` `sizeInBytes()` Returns how many bytes are being used by the RAMDirectory cache
`void` `sync(Collection<String> fileNames)`
`String` `toString()`
`void` `touchFile(String name)` Deprecated.

Methods inherited from class org.apache.lucene.store.Directory
`copy, copy, ensureOpen, sync`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

Constructor Detail

### NRTCachingDirectory

    public NRTCachingDirectory(org.apache.lucene.store.Directory delegate,
                               double maxMergeSizeMB,
                               double maxCachedMB)

  We will cache a newly created output if 1) it's a flush or a merge and the estimated size of the merged segment is <= maxMergeSizeMB, and 2) the total cached bytes is <= maxCachedMB

Method Detail

### getLockFactory

    public org.apache.lucene.store.LockFactory getLockFactory()

  Overrides:
    `getLockFactory` in class `org.apache.lucene.store.Directory`

### setLockFactory

    public void setLockFactory(org.apache.lucene.store.LockFactory lf)
                        throws IOException

  Overrides:
    `setLockFactory` in class `org.apache.lucene.store.Directory`
  Throws:
    `IOException`

### getLockID

    public String getLockID()

  Overrides:
    `getLockID` in class `org.apache.lucene.store.Directory`

### makeLock

    public org.apache.lucene.store.Lock makeLock(String name)

  Overrides:
    `makeLock` in class `org.apache.lucene.store.Directory`

### clearLock

    public void clearLock(String name)
                   throws IOException

  Overrides:
    `clearLock` in class `org.apache.lucene.store.Directory`
  Throws:
    `IOException`

### toString

    public String toString()

  Overrides:
    `toString` in class `org.apache.lucene.store.Directory`

### listAll

    public String[] listAll()
                     throws IOException

  Specified by:
    `listAll` in class `org.apache.lucene.store.Directory`
  Throws:
    `IOException`

### sizeInBytes

    public long sizeInBytes()

  Returns how many bytes are being used by the RAMDirectory cache

### fileExists

    public boolean fileExists(String name)
                       throws IOException

  Specified by:
    `fileExists` in class `org.apache.lucene.store.Directory`
  Throws:
    `IOException`

### fileModified

    public long fileModified(String name)
                      throws IOException

  Specified by:
    `fileModified` in class `org.apache.lucene.store.Directory`
  Throws:
    `IOException`

### touchFile

    @Deprecated
    public void touchFile(String name)
                   throws IOException

  Deprecated.

  Specified by:
    `touchFile` in class `org.apache.lucene.store.Directory`
  Throws:
    `IOException`

### deleteFile

    public void deleteFile(String name)
                    throws IOException

  Specified by:
    `deleteFile` in class `org.apache.lucene.store.Directory`
  Throws:
    `IOException`

### fileLength

    public long fileLength(String name)
                    throws IOException

  Specified by:
    `fileLength` in class `org.apache.lucene.store.Directory`
  Throws:
    `IOException`

### listCachedFiles

    public String[] listCachedFiles()

### createOutput

    public org.apache.lucene.store.IndexOutput createOutput(String name)
                                                     throws IOException

  Specified by:
    `createOutput` in class `org.apache.lucene.store.Directory`
  Throws:
    `IOException`

### sync

    public void sync(Collection<String> fileNames)
              throws IOException

  Overrides:
    `sync` in class `org.apache.lucene.store.Directory`
  Throws:
    `IOException`

### openInput

    public org.apache.lucene.store.IndexInput openInput(String name)
                                                 throws IOException

  Specified by:
    `openInput` in class `org.apache.lucene.store.Directory`
  Throws:
    `IOException`

### openInput

    public org.apache.lucene.store.IndexInput openInput(String name,
                                                        int bufferSize)
                                                 throws IOException

  Overrides:
    `openInput` in class `org.apache.lucene.store.Directory`
  Throws:
    `IOException`

### close

    public void close()
               throws IOException

  Close thius directory, which flushes any cached files to the delegate and then closes the delegate.

  Specified by:
    `close` in interface `Closeable`
  Specified by:
    `close` in class `org.apache.lucene.store.Directory`
  Throws:
    `IOException`

### getMergeScheduler

    public org.apache.lucene.index.MergeScheduler getMergeScheduler()

### doCacheWrite

    protected boolean doCacheWrite(String name)

  Subclass can override this to customize logic; return true if this file should be written to the RAMDirectory.

Copyright © 2000-2011 Apache Software Foundation. All Rights Reserved.