FileDocCategorySizeDatePackage
MockRAMDirectory.javaAPI DocApache Lucene 2.1.03884Wed Feb 14 10:46:34 GMT 2007org.apache.lucene.store

MockRAMDirectory

public class MockRAMDirectory extends RAMDirectory
This is a subclass of RAMDirectory that adds methods intented to be used only by unit tests.
version
$Id: RAMDirectory.java 437897 2006-08-29 01:13:10Z yonik $

Fields Summary
long
maxSize
long
maxUsedSize
double
randomIOExceptionRate
Random
randomState
Constructors Summary
public MockRAMDirectory()

    super();
  
public MockRAMDirectory(String dir)

    super(dir);
  
public MockRAMDirectory(Directory dir)

    super(dir);
  
public MockRAMDirectory(File dir)

    super(dir);
  
Methods Summary
public org.apache.lucene.store.IndexOutputcreateOutput(java.lang.String name)

    RAMFile file = new RAMFile(this);
    synchronized (this) {
      RAMFile existing = (RAMFile)fileMap.get(name);
      if (existing!=null) {
        sizeInBytes -= existing.sizeInBytes;
        existing.directory = null;
      }
      fileMap.put(name, file);
    }

    return new MockRAMOutputStream(this, file);
  
public longgetMaxSizeInBytes()

    return this.maxSize;
  
public longgetMaxUsedSizeInBytes()
Returns the peek actual storage used (bytes) in this directory.

    return this.maxUsedSize;
  
public doublegetRandomIOExceptionRate()

    return randomIOExceptionRate;
  
final longgetRecomputedActualSizeInBytes()
Like getRecomputedSizeInBytes(), but, uses actual file lengths rather than buffer allocations (which are quantized up to nearest BufferedIndexOutput.BUFFER_SIZE (now 1024) bytes.

    long size = 0;
    Iterator it = fileMap.values().iterator();
    while (it.hasNext())
      size += ((RAMFile) it.next()).length;
    return size;
  
public final synchronized longgetRecomputedSizeInBytes()
Provided for testing purposes. Use sizeInBytes() instead.

    long size = 0;
    Iterator it = fileMap.values().iterator();
    while (it.hasNext())
      size += ((RAMFile) it.next()).getSizeInBytes();
    return size;
  
voidmaybeThrowIOException()

    if (randomIOExceptionRate > 0.0) {
      int number = Math.abs(randomState.nextInt() % 1000);
      if (number < randomIOExceptionRate*1000) {
        throw new IOException("a random IOException");
      }
    }
  
public voidresetMaxUsedSizeInBytes()

    this.maxUsedSize = getRecomputedActualSizeInBytes();
  
public voidsetMaxSizeInBytes(long maxSize)

    this.maxSize = maxSize;
  
public voidsetRandomIOExceptionRate(double rate, long seed)
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.

    randomIOExceptionRate = rate;
    // seed so we have deterministic behaviour:
    randomState = new Random(seed);