Package org.apache.lucene.store
Class MockDirectoryWrapper
- java.lang.Object
-
- org.apache.lucene.store.Directory
-
- org.apache.lucene.store.MockDirectoryWrapper
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
public class MockDirectoryWrapper extends Directory
This is a Directory Wrapper that adds methods intended to be used only by unit tests. It also adds a number of features useful for testing:- Instances created by
LuceneTestCase.newDirectory()
are tracked to ensure they are closed by the test. - When a MockDirectoryWrapper is closed, it will throw an exception if it has any open files against it (with a stacktrace indicating where they were opened from).
- When a MockDirectoryWrapper is closed, it runs CheckIndex to test if the index was corrupted.
- MockDirectoryWrapper simulates some "features" of Windows, such as refusing to write/delete to open files.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
MockDirectoryWrapper.Failure
Objects that represent fail-able conditions.static class
MockDirectoryWrapper.Throttling
-
Field Summary
-
Fields inherited from class org.apache.lucene.store.Directory
isOpen, lockFactory
-
-
Constructor Summary
Constructors Constructor Description MockDirectoryWrapper(Random random, Directory delegate)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
clearCrash()
void
clearLock(String name)
Attempt to clear (forcefully unlock and remove) the specified lock.void
close()
Closes the store.void
copy(Directory to, String src, String dest)
Copies the file src toDirectory
to under the new file name dest.void
crash()
Simulates a crash of OS or machine by overwriting unsynced files.IndexOutput
createOutput(String name)
Creates a new, empty file in the directory with the given name.void
deleteFile(String name)
Removes an existing file in the directory.void
failOn(MockDirectoryWrapper.Failure fail)
add a Failure object to the list of objects to be evaluated at every potential failure pointboolean
fileExists(String name)
Returns true iff a file with the given name exists.long
fileLength(String name)
Returns the length of a file in the directory.long
fileModified(String name)
Returns the time the named file was last modified.boolean
getCheckIndexOnClose()
int
getInputCloneCount()
LockFactory
getLockFactory()
Get the LockFactory that this Directory instance is using for its locking implementation.String
getLockID()
Return a string identifier that uniquely differentiates this Directory instance from other Directory instances.long
getMaxSizeInBytes()
long
getMaxUsedSizeInBytes()
Returns the peek actual storage used (bytes) in this directory.boolean
getNoDeleteOpenFile()
Set<String>
getOpenDeletedFiles()
double
getRandomIOExceptionRate()
long
getRecomputedActualSizeInBytes()
Like getRecomputedSizeInBytes(), but, uses actual file lengths rather than buffer allocations (which are quantized up to nearest RAMOutputStream.BUFFER_SIZE (now 1024) bytes.long
getRecomputedSizeInBytes()
Provided for testing purposes.boolean
isOpen()
String[]
listAll()
Returns an array of strings, one for each file in the directory.Lock
makeLock(String name)
Construct aLock
.IndexInput
openInput(String name)
Returns a stream reading an existing file.void
removeIndexInput(IndexInput in, String name)
void
removeIndexOutput(IndexOutput out, String name)
void
resetMaxUsedSizeInBytes()
void
setAssertNoUnrefencedFilesOnClose(boolean v)
void
setCheckIndexOnClose(boolean value)
Set whether or not checkindex should be run on closevoid
setFailOnCreateOutput(boolean v)
void
setFailOnOpenInput(boolean v)
void
setLockFactory(LockFactory lockFactory)
Set the LockFactory that this Directory instance should use for its locking implementation.void
setMaxSizeInBytes(long maxSize)
void
setNoDeleteOpenFile(boolean value)
Emulate windows whereby deleting an open file is not allowed (raise IOException).void
setPreventDoubleWrite(boolean value)
If set to true, we throw an IOException if the same file is opened by createOutput, ever.void
setRandomIOExceptionRate(double rate)
If 0.0, no exceptions will be thrown.void
setThrottling(MockDirectoryWrapper.Throttling throttling)
void
setTrackDiskUsage(boolean v)
long
sizeInBytes()
void
sync(String name)
Deprecated.void
sync(Collection<String> names)
Ensure that any writes to these files are moved to stable storage.String
toString()
void
touchFile(String name)
Deprecated.-
Methods inherited from class org.apache.lucene.store.Directory
copy, ensureOpen, openInput
-
-
-
-
Method Detail
-
getInputCloneCount
public int getInputCloneCount()
-
setTrackDiskUsage
public void setTrackDiskUsage(boolean v)
-
setPreventDoubleWrite
public void setPreventDoubleWrite(boolean value)
If set to true, we throw an IOException if the same file is opened by createOutput, ever.
-
sync
@Deprecated public void sync(String name) throws IOException
Deprecated.Description copied from class:Directory
Ensure that any writes to this file are moved to stable storage. Lucene uses this to properly commit changes to the index, to prevent a machine/OS crash from corrupting the index.- Overrides:
sync
in classDirectory
- Throws:
IOException
-
setThrottling
public void setThrottling(MockDirectoryWrapper.Throttling throttling)
-
sync
public void sync(Collection<String> names) throws IOException
Description copied from class:Directory
Ensure that any writes to these files are moved to stable storage. Lucene uses this to properly commit changes to the index, to prevent a machine/OS crash from corrupting the index.
NOTE: Clients may call this method for same files over and over again, so some impls might optimize for that. For other impls the operation can be a noop, for various reasons.- Overrides:
sync
in classDirectory
- Throws:
IOException
-
sizeInBytes
public final long sizeInBytes() throws IOException
- Throws:
IOException
-
crash
public void crash() throws IOException
Simulates a crash of OS or machine by overwriting unsynced files.- Throws:
IOException
-
clearCrash
public void clearCrash() throws IOException
- Throws:
IOException
-
setMaxSizeInBytes
public void setMaxSizeInBytes(long maxSize)
-
getMaxSizeInBytes
public long getMaxSizeInBytes()
-
getMaxUsedSizeInBytes
public long getMaxUsedSizeInBytes()
Returns the peek actual storage used (bytes) in this directory.
-
resetMaxUsedSizeInBytes
public void resetMaxUsedSizeInBytes() throws IOException
- Throws:
IOException
-
setNoDeleteOpenFile
public void setNoDeleteOpenFile(boolean value)
Emulate windows whereby deleting an open file is not allowed (raise IOException).
-
getNoDeleteOpenFile
public boolean getNoDeleteOpenFile()
-
setCheckIndexOnClose
public void setCheckIndexOnClose(boolean value)
Set whether or not checkindex should be run on close
-
getCheckIndexOnClose
public boolean getCheckIndexOnClose()
-
setRandomIOExceptionRate
public void setRandomIOExceptionRate(double rate)
If 0.0, no exceptions will be thrown. Else this should be a double 0.0 - 1.0. We will randomly throw an IOException on the first write to an OutputStream based on this probability.
-
getRandomIOExceptionRate
public double getRandomIOExceptionRate()
-
deleteFile
public void deleteFile(String name) throws IOException
Description copied from class:Directory
Removes an existing file in the directory.- Specified by:
deleteFile
in classDirectory
- Throws:
IOException
-
setFailOnCreateOutput
public void setFailOnCreateOutput(boolean v)
-
createOutput
public IndexOutput createOutput(String name) throws IOException
Description copied from class:Directory
Creates a new, empty file in the directory with the given name. Returns a stream writing this file.- Specified by:
createOutput
in classDirectory
- Throws:
IOException
-
setFailOnOpenInput
public void setFailOnOpenInput(boolean v)
-
openInput
public IndexInput openInput(String name) throws IOException
Description copied from class:Directory
Returns a stream reading an existing file.- Specified by:
openInput
in classDirectory
- Throws:
IOException
-
getRecomputedSizeInBytes
public final long getRecomputedSizeInBytes() throws IOException
Provided for testing purposes. Use sizeInBytes() instead.- Throws:
IOException
-
getRecomputedActualSizeInBytes
public final long getRecomputedActualSizeInBytes() throws IOException
Like getRecomputedSizeInBytes(), but, uses actual file lengths rather than buffer allocations (which are quantized up to nearest RAMOutputStream.BUFFER_SIZE (now 1024) bytes.- Throws:
IOException
-
setAssertNoUnrefencedFilesOnClose
public void setAssertNoUnrefencedFilesOnClose(boolean v)
-
close
public void close() throws IOException
Description copied from class:Directory
Closes the store.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in classDirectory
- Throws:
IOException
-
removeIndexOutput
public void removeIndexOutput(IndexOutput out, String name)
-
removeIndexInput
public void removeIndexInput(IndexInput in, String name)
-
isOpen
public boolean isOpen()
-
failOn
public void failOn(MockDirectoryWrapper.Failure fail)
add a Failure object to the list of objects to be evaluated at every potential failure point
-
listAll
public String[] listAll() throws IOException
Description copied from class:Directory
Returns an array of strings, one for each file in the directory.- Specified by:
listAll
in classDirectory
- Throws:
NoSuchDirectoryException
- if the directory is not prepared for any write operations (such asDirectory.createOutput(String)
).IOException
- in case of other IO errors
-
fileExists
public boolean fileExists(String name) throws IOException
Description copied from class:Directory
Returns true iff a file with the given name exists.- Specified by:
fileExists
in classDirectory
- Throws:
IOException
-
fileModified
public long fileModified(String name) throws IOException
Description copied from class:Directory
Returns the time the named file was last modified.- Specified by:
fileModified
in classDirectory
- Throws:
IOException
-
touchFile
@Deprecated public void touchFile(String name) throws IOException
Deprecated.Description copied from class:Directory
Set the modified time of an existing file to now.- Specified by:
touchFile
in classDirectory
- Throws:
IOException
-
fileLength
public long fileLength(String name) throws IOException
Description copied from class:Directory
Returns the length of a file in the directory. This method follows the following contract:- Throws
FileNotFoundException
if the file does not exist - Returns a value ≥0 if the file exists, which specifies its length.
- Specified by:
fileLength
in classDirectory
- Parameters:
name
- the name of the file for which to return the length.- Throws:
FileNotFoundException
- if the file does not exist.IOException
- if there was an IO error while retrieving the file's length.
- Throws
-
makeLock
public Lock makeLock(String name)
Description copied from class:Directory
Construct aLock
.
-
clearLock
public void clearLock(String name) throws IOException
Description copied from class:Directory
Attempt to clear (forcefully unlock and remove) the specified lock. Only call this at a time when you are certain this lock is no longer in use.- Overrides:
clearLock
in classDirectory
- Parameters:
name
- name of the lock to be cleared.- Throws:
IOException
-
setLockFactory
public void setLockFactory(LockFactory lockFactory) throws IOException
Description copied from class:Directory
Set the LockFactory that this Directory instance should use for its locking implementation. Each * instance of LockFactory should only be used for one directory (ie, do not share a single instance across multiple Directories).- Overrides:
setLockFactory
in classDirectory
- Parameters:
lockFactory
- instance ofLockFactory
.- Throws:
IOException
-
getLockFactory
public LockFactory getLockFactory()
Description copied from class:Directory
Get the LockFactory that this Directory instance is using for its locking implementation. Note that this may be null for Directory implementations that provide their own locking implementation.- Overrides:
getLockFactory
in classDirectory
-
getLockID
public String getLockID()
Description copied from class:Directory
Return a string identifier that uniquely differentiates this Directory instance from other Directory instances. This ID should be the same if two Directory instances (even in different JVMs and/or on different machines) are considered "the same index". This is how locking "scopes" to the right index.
-
copy
public void copy(Directory to, String src, String dest) throws IOException
Description copied from class:Directory
Copies the file src toDirectory
to under the new file name dest.If you want to copy the entire source directory to the destination one, you can do so like this:
Directory to; // the directory to copy to for (String file : dir.listAll()) { dir.copy(to, file, newFile); // newFile can be either file, or a new name }
NOTE: this method does not check whether dest exist and will overwrite it if it does.
- Overrides:
copy
in classDirectory
- Throws:
IOException
-
-