FileDocCategorySizeDatePackage
RAMDirectory.javaAPI DocApache Lucene 1.4.35713Sun May 09 14:41:48 BST 2004org.apache.lucene.store

RAMDirectory

public final class RAMDirectory extends Directory
A memory-resident {@link Directory} implementation.
version
$Id: RAMDirectory.java,v 1.15 2004/05/09 12:41:47 ehatcher Exp $

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();
    for (int i = 0; i < files.length; i++) {
      // make place on ram disk
      OutputStream os = createFile(files[i]);
      // read current file
      InputStream is = dir.openFile(files[i]);
      // and copy to ram disk
      int len = (int) is.length();
      byte[] buf = new byte[len];
      is.readBytes(buf, 0, len);
      os.writeBytes(buf, len);
      // 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.OutputStreamcreateFile(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)) {
            createFile(name).close();
            return true;
          }
          return false;
        }
      }
      public void release() {
        deleteFile(name);
      }
      public boolean isLocked() {
        return fileExists(name);
      }
    };
  
public final org.apache.lucene.store.InputStreamopenFile(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);