Package org.apache.lucene.store
Class NRTCachingDirectory
- java.lang.Object
-
- org.apache.lucene.store.Directory
-
- org.apache.lucene.store.FilterDirectory
-
- org.apache.lucene.store.NRTCachingDirectory
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,Accountable
public class NRTCachingDirectory extends FilterDirectory implements Accountable
Wraps aRAMDirectoryaround any provided delegate directory, to be used during NRT search.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.
Here's a simple example usage:
Directory fsDir = FSDirectory.open(new File("/path/to/index").toPath()); NRTCachingDirectory cachedFSDir = new NRTCachingDirectory(fsDir, 5.0, 60.0); IndexWriterConfig conf = new IndexWriterConfig(analyzer); 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).
-
-
Field Summary
Fields Modifier and Type Field Description private RAMDirectorycacheprivate longmaxCachedBytesprivate longmaxMergeSizeBytesprivate java.lang.ObjectuncacheLockprivate static booleanVERBOSE-
Fields inherited from class org.apache.lucene.store.FilterDirectory
in
-
Fields inherited from interface org.apache.lucene.util.Accountable
NULL_ACCOUNTABLE
-
-
Constructor Summary
Constructors Constructor Description NRTCachingDirectory(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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Close this directory, which flushes any cached files to the delegate and then closes the delegate.IndexOutputcreateOutput(java.lang.String name, IOContext context)Creates a new, empty file in the directory and returns anIndexOutputinstance for appending data to this file.IndexOutputcreateTempOutput(java.lang.String prefix, java.lang.String suffix, IOContext context)Creates a new, empty, temporary file in the directory and returns anIndexOutputinstance for appending data to this file.voiddeleteFile(java.lang.String name)Removes an existing file in the directory.protected booleandoCacheWrite(java.lang.String name, IOContext context)Subclass can override this to customize logic; return true if this file should be written to the RAMDirectory.longfileLength(java.lang.String name)Returns the byte length of a file in the directory.java.util.Collection<Accountable>getChildResources()Returns nested resources of this class.java.lang.String[]listAll()Returns names of all files stored in this directory.java.lang.String[]listCachedFiles()IndexInputopenInput(java.lang.String name, IOContext context)Opens a stream for reading an existing file.longramBytesUsed()Return the memory usage of this object in bytes.voidrename(java.lang.String source, java.lang.String dest)Renamessourcefile todestfile wheredestmust not already exist in the directory.(package private) static booleanslowFileExists(Directory dir, java.lang.String fileName)Returns true if the file exists (can be opened), false if it cannot be opened, and (unlike Java's File.exists) throws IOException if there's some unexpected error.voidsync(java.util.Collection<java.lang.String> fileNames)Ensures that any writes to these files are moved to stable storage (made durable).java.lang.StringtoString()private voidunCache(java.lang.String fileName)-
Methods inherited from class org.apache.lucene.store.FilterDirectory
getDelegate, getPendingDeletions, obtainLock, syncMetaData, unwrap
-
Methods inherited from class org.apache.lucene.store.Directory
copyFrom, ensureOpen, getTempFileName, openChecksumInput
-
-
-
-
Field Detail
-
cache
private final RAMDirectory cache
-
maxMergeSizeBytes
private final long maxMergeSizeBytes
-
maxCachedBytes
private final long maxCachedBytes
-
VERBOSE
private static final boolean VERBOSE
- See Also:
- Constant Field Values
-
uncacheLock
private final java.lang.Object uncacheLock
-
-
Constructor Detail
-
NRTCachingDirectory
public NRTCachingDirectory(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
-
toString
public java.lang.String toString()
- Overrides:
toStringin classFilterDirectory
-
listAll
public java.lang.String[] listAll() throws java.io.IOExceptionDescription copied from class:DirectoryReturns names of all files stored in this directory. The output must be in sorted (UTF-16, java'sString.compareTo(java.lang.String)) order.- Overrides:
listAllin classFilterDirectory- Throws:
java.io.IOException- in case of I/O error
-
deleteFile
public void deleteFile(java.lang.String name) throws java.io.IOExceptionDescription copied from class:DirectoryRemoves an existing file in the directory. This method must throw eitherNoSuchFileExceptionorFileNotFoundExceptionifnamepoints to a non-existing file.- Overrides:
deleteFilein classFilterDirectory- Parameters:
name- the name of an existing file.- Throws:
java.io.IOException- in case of I/O error
-
fileLength
public long fileLength(java.lang.String name) throws java.io.IOExceptionDescription copied from class:DirectoryReturns the byte length of a file in the directory. This method must throw eitherNoSuchFileExceptionorFileNotFoundExceptionifnamepoints to a non-existing file.- Overrides:
fileLengthin classFilterDirectory- Parameters:
name- the name of an existing file.- Throws:
java.io.IOException- in case of I/O error
-
listCachedFiles
public java.lang.String[] listCachedFiles()
-
createOutput
public IndexOutput createOutput(java.lang.String name, IOContext context) throws java.io.IOException
Description copied from class:DirectoryCreates a new, empty file in the directory and returns anIndexOutputinstance for appending data to this file. This method must throwFileAlreadyExistsExceptionif the file already exists.- Overrides:
createOutputin classFilterDirectory- Parameters:
name- the name of the file to create.- Throws:
java.io.IOException- in case of I/O error
-
sync
public void sync(java.util.Collection<java.lang.String> fileNames) throws java.io.IOExceptionDescription copied from class:DirectoryEnsures that any writes to these files are moved to stable storage (made durable). Lucene uses this to properly commit changes to the index, to prevent a machine/OS crash from corrupting the index.- Overrides:
syncin classFilterDirectory- Throws:
java.io.IOException- See Also:
Directory.syncMetaData()
-
rename
public void rename(java.lang.String source, java.lang.String dest) throws java.io.IOExceptionDescription copied from class:DirectoryRenamessourcefile todestfile wheredestmust not already exist in the directory. It is permitted for this operation to not be truly atomic, for example bothsourceanddestcan be visible temporarily inDirectory.listAll(). However, the implementation of this method must ensure the content ofdestappears as the entiresourceatomically. So oncedestis visible for readers, the entire content of previoussourceis visible. This method is used by IndexWriter to publish commits.- Overrides:
renamein classFilterDirectory- Throws:
java.io.IOException
-
openInput
public IndexInput openInput(java.lang.String name, IOContext context) throws java.io.IOException
Description copied from class:DirectoryOpens a stream for reading an existing file. This method must throw eitherNoSuchFileExceptionorFileNotFoundExceptionifnamepoints to a non-existing file.- Overrides:
openInputin classFilterDirectory- Parameters:
name- the name of an existing file.- Throws:
java.io.IOException- in case of I/O error
-
close
public void close() throws java.io.IOExceptionClose this directory, which flushes any cached files to the delegate and then closes the delegate.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classFilterDirectory- Throws:
java.io.IOException
-
doCacheWrite
protected boolean doCacheWrite(java.lang.String name, IOContext context)Subclass can override this to customize logic; return true if this file should be written to the RAMDirectory.
-
createTempOutput
public IndexOutput createTempOutput(java.lang.String prefix, java.lang.String suffix, IOContext context) throws java.io.IOException
Description copied from class:DirectoryCreates a new, empty, temporary file in the directory and returns anIndexOutputinstance for appending data to this file. The temporary file name (accessible viaIndexOutput.getName()) will start withprefix, end withsuffixand have a reserved file extension.tmp.- Overrides:
createTempOutputin classFilterDirectory- Throws:
java.io.IOException
-
slowFileExists
static boolean slowFileExists(Directory dir, java.lang.String fileName) throws java.io.IOException
Returns true if the file exists (can be opened), false if it cannot be opened, and (unlike Java's File.exists) throws IOException if there's some unexpected error.- Throws:
java.io.IOException
-
unCache
private void unCache(java.lang.String fileName) throws java.io.IOException- Throws:
java.io.IOException
-
ramBytesUsed
public long ramBytesUsed()
Description copied from interface:AccountableReturn the memory usage of this object in bytes. Negative values are illegal.- Specified by:
ramBytesUsedin interfaceAccountable
-
getChildResources
public java.util.Collection<Accountable> getChildResources()
Description copied from interface:AccountableReturns nested resources of this class. The result should be a point-in-time snapshot (to avoid race conditions).- Specified by:
getChildResourcesin interfaceAccountable- See Also:
Accountables
-
-