Fork me on GitHub
  • API

    Show / Hide Table of Contents

    Class MockDirectoryWrapper

    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 NewDirectory() are tracked to ensure they are disposed by the test.
    • When a MockDirectoryWrapper is disposed, 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 disposed, it runs Lucene.Net.Index.CheckIndex to test if the index was corrupted.
    • MockDirectoryWrapper simulates some "features" of Windows, such as refusing to write/delete to open files.
    Inheritance
    object
    Directory
    FilterDirectory
    BaseDirectoryWrapper
    MockDirectoryWrapper
    Implements
    IDisposable
    Inherited Members
    BaseDirectoryWrapper.CompareAndSetIsOpen(bool, bool)
    BaseDirectoryWrapper.IsOpen
    BaseDirectoryWrapper.CheckIndexOnDispose
    BaseDirectoryWrapper.CrossCheckTermVectorsOnDispose
    FilterDirectory.m_input
    FilterDirectory.Delegate
    FilterDirectory.ToString()
    Directory.OpenChecksumInput(string, IOContext)
    Directory.Dispose()
    Directory.EnsureOpen()
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    Namespace: Lucene.Net.Store
    Assembly: Lucene.Net.TestFramework.dll
    Syntax
    public class MockDirectoryWrapper : BaseDirectoryWrapper, IDisposable

    Constructors

    MockDirectoryWrapper(Random, 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 NewDirectory() are tracked to ensure they are disposed by the test.
    • When a MockDirectoryWrapper is disposed, 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 disposed, it runs Lucene.Net.Index.CheckIndex to test if the index was corrupted.
    • MockDirectoryWrapper simulates some "features" of Windows, such as refusing to write/delete to open files.
    Declaration
    public MockDirectoryWrapper(Random random, Directory @delegate)
    Parameters
    Type Name Description
    Random random
    Directory delegate

    Fields

    m_lockFactory

    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 NewDirectory() are tracked to ensure they are disposed by the test.
    • When a MockDirectoryWrapper is disposed, 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 disposed, it runs Lucene.Net.Index.CheckIndex to test if the index was corrupted.
    • MockDirectoryWrapper simulates some "features" of Windows, such as refusing to write/delete to open files.
    Declaration
    protected LockFactory m_lockFactory
    Field Value
    Type Description
    LockFactory

    Properties

    AllowRandomFileNotFoundException

    If set to true (the default), when we throw random IOException on OpenInput(string, IOContext) or CreateOutput(string, IOContext), we may sometimes throw FileNotFoundException.

    Declaration
    public virtual bool AllowRandomFileNotFoundException { get; set; }
    Property Value
    Type Description
    bool

    AllowReadingFilesStillOpenForWrite

    If set to true, you can open an inputstream on a file that is still open for writes.

    Declaration
    public virtual bool AllowReadingFilesStillOpenForWrite { get; set; }
    Property Value
    Type Description
    bool

    AssertNoDeleteOpenFile

    Trip a test assert if there is an attempt to delete an open file.

    Declaration
    public virtual bool AssertNoDeleteOpenFile { get; set; }
    Property Value
    Type Description
    bool

    AssertNoUnreferencedFilesOnDispose

    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 NewDirectory() are tracked to ensure they are disposed by the test.
    • When a MockDirectoryWrapper is disposed, 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 disposed, it runs Lucene.Net.Index.CheckIndex to test if the index was corrupted.
    • MockDirectoryWrapper simulates some "features" of Windows, such as refusing to write/delete to open files.
    Declaration
    public virtual bool AssertNoUnreferencedFilesOnDispose { get; set; }
    Property Value
    Type Description
    bool

    FailOnCreateOutput

    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 NewDirectory() are tracked to ensure they are disposed by the test.
    • When a MockDirectoryWrapper is disposed, 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 disposed, it runs Lucene.Net.Index.CheckIndex to test if the index was corrupted.
    • MockDirectoryWrapper simulates some "features" of Windows, such as refusing to write/delete to open files.
    Declaration
    public virtual bool FailOnCreateOutput { get; set; }
    Property Value
    Type Description
    bool

    FailOnOpenInput

    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 NewDirectory() are tracked to ensure they are disposed by the test.
    • When a MockDirectoryWrapper is disposed, 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 disposed, it runs Lucene.Net.Index.CheckIndex to test if the index was corrupted.
    • MockDirectoryWrapper simulates some "features" of Windows, such as refusing to write/delete to open files.
    Declaration
    public virtual bool FailOnOpenInput { get; set; }
    Property Value
    Type Description
    bool

    InputCloneCount

    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 NewDirectory() are tracked to ensure they are disposed by the test.
    • When a MockDirectoryWrapper is disposed, 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 disposed, it runs Lucene.Net.Index.CheckIndex to test if the index was corrupted.
    • MockDirectoryWrapper simulates some "features" of Windows, such as refusing to write/delete to open files.
    Declaration
    public virtual int InputCloneCount { get; }
    Property Value
    Type Description
    int

    LockFactory

    Get the Lucene.Net.Store.LockFactory that this Lucene.Net.Store.Directory instance is using for its locking implementation. Note that this may be null for Lucene.Net.Store.Directory implementations that provide their own locking implementation.

    Declaration
    public override LockFactory LockFactory { get; }
    Property Value
    Type Description
    LockFactory
    Overrides
    Lucene.Net.Store.FilterDirectory.LockFactory

    MaxSizeInBytes

    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 NewDirectory() are tracked to ensure they are disposed by the test.
    • When a MockDirectoryWrapper is disposed, 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 disposed, it runs Lucene.Net.Index.CheckIndex to test if the index was corrupted.
    • MockDirectoryWrapper simulates some "features" of Windows, such as refusing to write/delete to open files.
    Declaration
    public virtual long MaxSizeInBytes { get; set; }
    Property Value
    Type Description
    long

    MaxUsedSizeInBytes

    Returns the peek actual storage used (bytes) in this directory.

    Declaration
    public virtual long MaxUsedSizeInBytes { get; }
    Property Value
    Type Description
    long

    NoDeleteOpenFile

    Emulate Windows whereby deleting an open file is not allowed (raise IOException).

    Declaration
    public virtual bool NoDeleteOpenFile { get; set; }
    Property Value
    Type Description
    bool

    PreventDoubleWrite

    If set to true, we throw an IOException if the same file is opened by CreateOutput(string, IOContext), ever.

    Declaration
    public virtual bool PreventDoubleWrite { get; set; }
    Property Value
    Type Description
    bool

    RandomIOExceptionRate

    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 a Stream based on this probability.

    Declaration
    public virtual double RandomIOExceptionRate { get; set; }
    Property Value
    Type Description
    double

    RandomIOExceptionRateOnOpen

    If 0.0, no exceptions will be thrown during OpenInput(string, IOContext) and CreateOutput(string, IOContext). Else this should be a double 0.0 - 1.0 and we will randomly throw an IOException in OpenInput(string, IOContext) and CreateOutput(string, IOContext) with this probability.

    Declaration
    public virtual double RandomIOExceptionRateOnOpen { get; set; }
    Property Value
    Type Description
    double

    Throttling

    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 NewDirectory() are tracked to ensure they are disposed by the test.
    • When a MockDirectoryWrapper is disposed, 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 disposed, it runs Lucene.Net.Index.CheckIndex to test if the index was corrupted.
    • MockDirectoryWrapper simulates some "features" of Windows, such as refusing to write/delete to open files.
    Declaration
    public virtual Throttling Throttling { get; set; }
    Property Value
    Type Description
    Throttling

    TrackDiskUsage

    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 NewDirectory() are tracked to ensure they are disposed by the test.
    • When a MockDirectoryWrapper is disposed, 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 disposed, it runs Lucene.Net.Index.CheckIndex to test if the index was corrupted.
    • MockDirectoryWrapper simulates some "features" of Windows, such as refusing to write/delete to open files.
    Declaration
    public virtual bool TrackDiskUsage { get; set; }
    Property Value
    Type Description
    bool

    WrapLockFactory

    Set to false if you want to return the pure lockfactory and not wrap it with MockLockFactoryWrapper.

    Be careful if you turn this off: MockDirectoryWrapper might no longer be able to detect if you forget to close an Lucene.Net.Index.IndexWriter, and spit out horribly scary confusing exceptions instead of simply telling you that.
    Declaration
    public virtual bool WrapLockFactory { get; set; }
    Property Value
    Type Description
    bool

    Methods

    ClearCrash()

    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 NewDirectory() are tracked to ensure they are disposed by the test.
    • When a MockDirectoryWrapper is disposed, 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 disposed, it runs Lucene.Net.Index.CheckIndex to test if the index was corrupted.
    • MockDirectoryWrapper simulates some "features" of Windows, such as refusing to write/delete to open files.
    Declaration
    public virtual void ClearCrash()

    ClearLock(string)

    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.

    Declaration
    public override void ClearLock(string name)
    Parameters
    Type Name Description
    string name

    name of the lock to be cleared.

    Overrides
    FilterDirectory.ClearLock(string)

    Copy(Directory, string, string, IOContext)

    Copies the file src to Lucene.Net.Store.Directoryto 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
    foreach (string file in dir.ListAll()) {
        dir.Copy(to, file, newFile, IOContext.DEFAULT); // 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.

    Declaration
    public override void Copy(Directory to, string src, string dest, IOContext context)
    Parameters
    Type Name Description
    Directory to
    string src
    string dest
    IOContext context
    Overrides
    BaseDirectoryWrapper.Copy(Directory, string, string, IOContext)

    Crash()

    Simulates a crash of OS or machine by overwriting unsynced files.

    Declaration
    public virtual void Crash()

    CreateOutput(string, IOContext)

    Creates a new, empty file in the directory with the given name. Returns a stream writing this file.

    Declaration
    public override IndexOutput CreateOutput(string name, IOContext context)
    Parameters
    Type Name Description
    string name
    IOContext context
    Returns
    Type Description
    IndexOutput
    Overrides
    FilterDirectory.CreateOutput(string, IOContext)

    CreateSlicer(string, IOContext)

    Creates an Lucene.Net.Store.Directory.IndexInputSlicer for the given file name. Lucene.Net.Store.Directory.IndexInputSlicer allows other Lucene.Net.Store.Directory implementations to efficiently open one or more sliced Lucene.Net.Store.IndexInput instances from a single file handle. The underlying file handle is kept open until the Lucene.Net.Store.Directory.IndexInputSlicer is closed.

    Throws FileNotFoundException if the file does not exist.

    Note

    This API is for internal purposes only and might change in incompatible ways in the next release.

    Note

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

    Declaration
    public override Directory.IndexInputSlicer CreateSlicer(string name, IOContext context)
    Parameters
    Type Name Description
    string name
    IOContext context
    Returns
    Type Description
    Directory.IndexInputSlicer
    Overrides
    BaseDirectoryWrapper.CreateSlicer(string, IOContext)
    Exceptions
    Type Condition
    IOException

    if an IOException occurs

    DeleteFile(string)

    Removes an existing file in the directory.

    Declaration
    public override void DeleteFile(string name)
    Parameters
    Type Name Description
    string name
    Overrides
    FilterDirectory.DeleteFile(string)

    Dispose(bool)

    Disposes the store.

    Declaration
    protected override void Dispose(bool disposing)
    Parameters
    Type Name Description
    bool disposing
    Overrides
    BaseDirectoryWrapper.Dispose(bool)

    FailOn(Failure)

    Add a Failure object to the list of objects to be evaluated at every potential failure point.

    Declaration
    public virtual void FailOn(Failure fail)
    Parameters
    Type Name Description
    Failure fail

    FileExists(string)

    Returns true iff a file with the given name exists.

    Declaration
    [Obsolete("this method will be removed in 5.0")]
    public override bool FileExists(string name)
    Parameters
    Type Name Description
    string name
    Returns
    Type Description
    bool
    Overrides
    FilterDirectory.FileExists(string)

    FileLength(string)

    Returns the length of a file in the directory. this method follows the following contract:

    Declaration
    public override long FileLength(string name)
    Parameters
    Type Name Description
    string name

    the name of the file for which to return the length.

    Returns
    Type Description
    long
    Overrides
    FilterDirectory.FileLength(string)
    Exceptions
    Type Condition
    IOException

    if there was an IO error while retrieving the file's length.

    GetLockID()

    Return a string identifier that uniquely differentiates this Lucene.Net.Store.Directory instance from other Lucene.Net.Store.Directory instances. This ID should be the same if two Lucene.Net.Store.Directory instances (even in different AppDomains and/or on different machines) are considered "the same index". This is how locking "scopes" to the right index.

    Declaration
    public override string GetLockID()
    Returns
    Type Description
    string
    Overrides
    Lucene.Net.Store.FilterDirectory.GetLockID()

    GetOpenDeletedFiles()

    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 NewDirectory() are tracked to ensure they are disposed by the test.
    • When a MockDirectoryWrapper is disposed, 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 disposed, it runs Lucene.Net.Index.CheckIndex to test if the index was corrupted.
    • MockDirectoryWrapper simulates some "features" of Windows, such as refusing to write/delete to open files.
    Declaration
    public virtual ICollection<string> GetOpenDeletedFiles()
    Returns
    Type Description
    ICollection<string>

    GetRecomputedActualSizeInBytes()

    Like GetRecomputedSizeInBytes(), but, uses actual file lengths rather than buffer allocations (which are quantized up to nearest Lucene.Net.Store.RAMOutputStream.BUFFER_SIZE (now 1024) bytes.

    Declaration
    public long GetRecomputedActualSizeInBytes()
    Returns
    Type Description
    long

    GetRecomputedSizeInBytes()

    Provided for testing purposes. Use GetSizeInBytes() instead.

    Declaration
    public long GetRecomputedSizeInBytes()
    Returns
    Type Description
    long

    GetSizeInBytes()

    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 NewDirectory() are tracked to ensure they are disposed by the test.
    • When a MockDirectoryWrapper is disposed, 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 disposed, it runs Lucene.Net.Index.CheckIndex to test if the index was corrupted.
    • MockDirectoryWrapper simulates some "features" of Windows, such as refusing to write/delete to open files.
    Declaration
    public long GetSizeInBytes()
    Returns
    Type Description
    long

    ListAll()

    Returns an array of strings, one for each file in the directory.

    Declaration
    public override string[] ListAll()
    Returns
    Type Description
    string[]
    Overrides
    Lucene.Net.Store.FilterDirectory.ListAll()
    Exceptions
    Type Condition
    DirectoryNotFoundException

    if the directory is not prepared for any write operations (such as CreateOutput(string, IOContext)).

    IOException

    in case of other IO errors

    MakeLock(string)

    Construct a Lucene.Net.Store.Lock.

    Declaration
    public override Lock MakeLock(string name)
    Parameters
    Type Name Description
    string name

    the name of the lock file

    Returns
    Type Description
    Lock
    Overrides
    FilterDirectory.MakeLock(string)

    OpenInput(string, IOContext)

    Returns a stream reading an existing file, with the specified read buffer size. The particular Lucene.Net.Store.Directory implementation may ignore the buffer size. Currently the only Lucene.Net.Store.Directory implementations that respect this parameter are Lucene.Net.Store.FSDirectory and Lucene.Net.Store.CompoundFileDirectory.

    Throws FileNotFoundException if the file does not exist.
    Declaration
    public override IndexInput OpenInput(string name, IOContext context)
    Parameters
    Type Name Description
    string name
    IOContext context
    Returns
    Type Description
    IndexInput
    Overrides
    FilterDirectory.OpenInput(string, IOContext)

    RemoveIndexInput(IndexInput, string)

    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 NewDirectory() are tracked to ensure they are disposed by the test.
    • When a MockDirectoryWrapper is disposed, 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 disposed, it runs Lucene.Net.Index.CheckIndex to test if the index was corrupted.
    • MockDirectoryWrapper simulates some "features" of Windows, such as refusing to write/delete to open files.
    Declaration
    public virtual void RemoveIndexInput(IndexInput @in, string name)
    Parameters
    Type Name Description
    IndexInput in
    string name

    RemoveIndexOutput(IndexOutput, string)

    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 NewDirectory() are tracked to ensure they are disposed by the test.
    • When a MockDirectoryWrapper is disposed, 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 disposed, it runs Lucene.Net.Index.CheckIndex to test if the index was corrupted.
    • MockDirectoryWrapper simulates some "features" of Windows, such as refusing to write/delete to open files.
    Declaration
    public virtual void RemoveIndexOutput(IndexOutput @out, string name)
    Parameters
    Type Name Description
    IndexOutput out
    string name

    ResetMaxUsedSizeInBytes()

    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 NewDirectory() are tracked to ensure they are disposed by the test.
    • When a MockDirectoryWrapper is disposed, 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 disposed, it runs Lucene.Net.Index.CheckIndex to test if the index was corrupted.
    • MockDirectoryWrapper simulates some "features" of Windows, such as refusing to write/delete to open files.
    Declaration
    public virtual void ResetMaxUsedSizeInBytes()

    SetLockFactory(LockFactory)

    Set the Lucene.Net.Store.LockFactory that this Lucene.Net.Store.Directory instance should use for its locking implementation. Each * instance of Lucene.Net.Store.LockFactory should only be used for one directory (ie, do not share a single instance across multiple Directories).

    Declaration
    public override void SetLockFactory(LockFactory lockFactory)
    Parameters
    Type Name Description
    LockFactory lockFactory

    instance of Lucene.Net.Store.LockFactory.

    Overrides
    Lucene.Net.Store.FilterDirectory.SetLockFactory(Lucene.Net.Store.LockFactory)

    Sync(ICollection<string>)

    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.

    Declaration
    public override void Sync(ICollection<string> names)
    Parameters
    Type Name Description
    ICollection<string> names
    Overrides
    FilterDirectory.Sync(ICollection<string>)

    Implements

    IDisposable
    Back to top Copyright © 2024 The Apache Software Foundation, Licensed under the Apache License, Version 2.0
    Apache Lucene.Net, Lucene.Net, Apache, the Apache feather logo, and the Apache Lucene.Net project logo are trademarks of The Apache Software Foundation.
    All other marks mentioned may be trademarks or registered trademarks of their respective owners.