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.
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
long len = is.length();
long readCount = 0;
while (readCount < len) {
int toRead = readCount + BufferedIndexOutput.BUFFER_SIZE > len ? (int)(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}.
this(FSDirectory.getDirectory(dir, false), true);
|
public RAMDirectory(String dir)Creates a new RAMDirectory instance from the {@link FSDirectory}.
this(FSDirectory.getDirectory(dir, false), true);
|
Methods Summary |
---|
public final void | close()Closes the store to future operations.
|
public final org.apache.lucene.store.IndexOutput | createOutput(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 void | deleteFile(java.lang.String name)Removes an existing file in the directory.
files.remove(name);
|
public final boolean | fileExists(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 long | fileLength(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 long | fileModified(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.Lock | makeLock(java.lang.String name)Construct a {@link Lock}.
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.IndexInput | openInput(java.lang.String name)Returns a stream reading an existing file.
RAMFile file = (RAMFile)files.get(name);
return new RAMInputStream(file);
|
public final void | renameFile(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 void | touchFile(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);
|