FileDocCategorySizeDatePackage
RAMDirectory.javaAPI DocApache Lucene 1.95928Mon Feb 20 09:20:16 GMT 2006org.apache.lucene.store

RAMDirectory

public final class RAMDirectory extends Directory
A memory-resident {@link Directory} implementation.
version
$Id: RAMDirectory.java 351779 2005-12-02 17:37:50Z bmesser $

Fields Summary
Hashtable
files
Constructors Summary
public RAMDirectory()
Constructs an empty {@link Directory}.


        
    
  
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.

param
dir a Directory value
exception
IOException if an error occurs

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

    final String[] files = dir.list();
    byte[] buf = new byte[BufferedIndexOutput.BUFFER_SIZE];
    for (int i = 0; i < files.length; i++) {
      // make place on ram disk
      IndexOutput os = createOutput(files[i]);
      // read current file
      IndexInput is = dir.openInput(files[i]);
      // and copy to ram disk
      int len = (int) is.length();
      int readCount = 0;
      while (readCount < len) {
        int toRead = readCount + BufferedIndexOutput.BUFFER_SIZE > len ? len - readCount : BufferedIndexOutput.BUFFER_SIZE;
        is.readBytes(buf, 0, toRead);
        os.writeBytes(buf, toRead);
        readCount += toRead;
      }

      // graceful cleanup
      is.close();
      os.close();
    }
    if(closeDir)
      dir.close();
  
public RAMDirectory(File dir)
Creates a new RAMDirectory instance from the {@link FSDirectory}.

param
dir a File specifying the index directory

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

param
dir a String specifying the full index directory path

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

  
public final 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();
    files.put(name, file);
    return new RAMOutputStream(file);
  
public final voiddeleteFile(java.lang.String name)
Removes an existing file in the directory.

    files.remove(name);
  
public final booleanfileExists(java.lang.String name)
Returns true iff the named file exists in this directory.

    RAMFile file = (RAMFile)files.get(name);
    return file != null;
  
public final longfileLength(java.lang.String name)
Returns the length in bytes of a file in the directory.

    RAMFile file = (RAMFile)files.get(name);
    return file.length;
  
public final longfileModified(java.lang.String name)
Returns the time the named file was last modified.

    RAMFile file = (RAMFile)files.get(name);
    return file.lastModified;
  
public final java.lang.String[]list()
Returns an array of strings, one for each file in the directory.

    String[] result = new String[files.size()];
    int i = 0;
    Enumeration names = files.keys();
    while (names.hasMoreElements())
      result[i++] = (String)names.nextElement();
    return result;
  
public final org.apache.lucene.store.LockmakeLock(java.lang.String name)
Construct a {@link Lock}.

param
name the name of the lock file

    return new Lock() {
      public boolean obtain() throws IOException {
        synchronized (files) {
          if (!fileExists(name)) {
            createOutput(name).close();
            return true;
          }
          return false;
        }
      }
      public void release() {
        deleteFile(name);
      }
      public boolean isLocked() {
        return fileExists(name);
      }
    };
  
public final org.apache.lucene.store.IndexInputopenInput(java.lang.String name)
Returns a stream reading an existing file.

    RAMFile file = (RAMFile)files.get(name);
    return new RAMInputStream(file);
  
public final voidrenameFile(java.lang.String from, java.lang.String to)
Removes an existing file in the directory.

    RAMFile file = (RAMFile)files.get(from);
    files.remove(from);
    files.put(to, file);
  
public voidtouchFile(java.lang.String name)
Set the modified time of an existing file to now.

//     final boolean MONITOR = false;
    
    RAMFile file = (RAMFile)files.get(name);
    long ts2, ts1 = System.currentTimeMillis();
    do {
      try {
        Thread.sleep(0, 1);
      } catch (InterruptedException e) {}
      ts2 = System.currentTimeMillis();
//       if (MONITOR) {
//         count++;
//       }
    } while(ts1 == ts2);

    file.lastModified = ts2;

//     if (MONITOR)
//         System.out.println("SLEEP COUNT: " + count);