FileDocCategorySizeDatePackage
RAMDirectory.javaAPI DocApache Lucene 2.2.07822Sat Jun 16 22:20:36 BST 2007org.apache.lucene.store

RAMDirectory

public class RAMDirectory extends Directory implements Serializable
A memory-resident {@link Directory} implementation. Locking implementation is by default the {@link SingleInstanceLockFactory} but can be changed with {@link #setLockFactory}.
version
$Id: RAMDirectory.java 542561 2007-05-29 15:14:07Z mikemccand $

Fields Summary
private static final long
serialVersionUID
HashMap
fileMap
long
sizeInBytes
Constructors Summary
public RAMDirectory()
Constructs an empty {@link Directory}.

  
  // *****
  // Lock acquisition sequence:  RAMDirectory, then RAMFile
  // *****

        
    
    setLockFactory(new SingleInstanceLockFactory());
  
public RAMDirectory(Directory dir)
Creates a new RAMDirectory instance from a different Directory implementation. This can be used to load a disk-based index into memory.

This should be used only with indices that can fit into memory.

Note that the resulting RAMDirectory instance is fully independent from the original Directory (it is a complete copy). Any subsequent changes to the original Directory will not be visible in the RAMDirectory instance.

param
dir a Directory value
exception
IOException if an error occurs

    this(dir, false);
  
private RAMDirectory(Directory dir, boolean closeDir)

    this();
    Directory.copy(dir, this, closeDir);
  
public RAMDirectory(File dir)
Creates a new RAMDirectory instance from the {@link FSDirectory}.

param
dir a File specifying the index directory
see
#RAMDirectory(Directory)

    this(FSDirectory.getDirectory(dir), true);
  
public RAMDirectory(String dir)
Creates a new RAMDirectory instance from the {@link FSDirectory}.

param
dir a String specifying the full index directory path
see
#RAMDirectory(Directory)

    this(FSDirectory.getDirectory(dir), true);
  
Methods Summary
public voidclose()
Closes the store to future operations, releasing associated memory.

    fileMap = null;
  
public org.apache.lucene.store.IndexOutputcreateOutput(java.lang.String name)
Creates a new, empty file in the directory with the given name. Returns a stream writing this file.

    ensureOpen();
    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 RAMOutputStream(file);
  
public synchronized voiddeleteFile(java.lang.String name)
Removes an existing file in the directory.

throws
IOException if the file does not exist

    ensureOpen();
    RAMFile file = (RAMFile)fileMap.get(name);
    if (file!=null) {
        fileMap.remove(name);
        file.directory = null;
        sizeInBytes -= file.sizeInBytes;       // updates to RAMFile.sizeInBytes synchronized on directory
    } else
      throw new FileNotFoundException(name);
  
protected final voidensureOpen()

throws
AlreadyClosedException if this IndexReader is closed

    if (fileMap == null) {
      throw new AlreadyClosedException("this RAMDirectory is closed");
    }
  
public final booleanfileExists(java.lang.String name)
Returns true iff the named file exists in this directory.

    ensureOpen();
    RAMFile file;
    synchronized (this) {
      file = (RAMFile)fileMap.get(name);
    }
    return file != null;
  
public final longfileLength(java.lang.String name)
Returns the length in bytes of a file in the directory.

throws
IOException if the file does not exist

    ensureOpen();
    RAMFile file;
    synchronized (this) {
      file = (RAMFile)fileMap.get(name);
    }
    if (file==null)
      throw new FileNotFoundException(name);
    return file.getLength();
  
public final longfileModified(java.lang.String name)
Returns the time the named file was last modified.

throws
IOException if the file does not exist

    ensureOpen();
    RAMFile file;
    synchronized (this) {
      file = (RAMFile)fileMap.get(name);
    }
    if (file==null)
      throw new FileNotFoundException(name);
    return file.getLastModified();
  
public final synchronized java.lang.String[]list()
Returns an array of strings, one for each file in the directory.

    ensureOpen();
    Set fileNames = fileMap.keySet();
    String[] result = new String[fileNames.size()];
    int i = 0;
    Iterator it = fileNames.iterator();
    while (it.hasNext())
      result[i++] = (String)it.next();
    return result;
  
public org.apache.lucene.store.IndexInputopenInput(java.lang.String name)
Returns a stream reading an existing file.

    ensureOpen();
    RAMFile file;
    synchronized (this) {
      file = (RAMFile)fileMap.get(name);
    }
    if (file == null)
      throw new FileNotFoundException(name);
    return new RAMInputStream(file);
  
public final synchronized voidrenameFile(java.lang.String from, java.lang.String to)
Renames an existing file in the directory.

throws
FileNotFoundException if from does not exist
deprecated

    ensureOpen();
    RAMFile fromFile = (RAMFile)fileMap.get(from);
    if (fromFile==null)
      throw new FileNotFoundException(from);
    RAMFile toFile = (RAMFile)fileMap.get(to);
    if (toFile!=null) {
      sizeInBytes -= toFile.sizeInBytes;       // updates to RAMFile.sizeInBytes synchronized on directory
      toFile.directory = null;
    }
    fileMap.remove(from);
    fileMap.put(to, fromFile);
  
public final synchronized longsizeInBytes()
Return total size in bytes of all files in this directory. This is currently quantized to RAMOutputStream.BUFFER_SIZE.

    ensureOpen();
    return sizeInBytes;
  
public voidtouchFile(java.lang.String name)
Set the modified time of an existing file to now.

throws
IOException if the file does not exist

    ensureOpen();
    RAMFile file;
    synchronized (this) {
      file = (RAMFile)fileMap.get(name);
    }
    if (file==null)
      throw new FileNotFoundException(name);
    
    long ts2, ts1 = System.currentTimeMillis();
    do {
      try {
        Thread.sleep(0, 1);
      } catch (InterruptedException e) {}
      ts2 = System.currentTimeMillis();
    } while(ts1 == ts2);
    
    file.setLastModified(ts2);