FileDocCategorySizeDatePackage
RAMDirectory.javaAPI DocApache Lucene 2.1.07406Wed Feb 14 10:46:40 GMT 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 503911 2007-02-05 22:49:42Z dnaber $

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 final 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.

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

throws
IOException if the file does not exist

    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);
  
public final booleanfileExists(java.lang.String name)
Returns true iff the named file exists in this directory.

    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

    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

    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.

    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 final org.apache.lucene.store.IndexInputopenInput(java.lang.String name)
Returns a stream reading an existing file.

    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

    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 BufferedIndexOutput.BUFFER_SIZE.

    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

    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);