| Method from org.apache.lucene.store.FSDirectory Detail: |
public synchronized void close() {
if (isOpen && --refCount < = 0) {
isOpen = false;
synchronized (DIRECTORIES) {
DIRECTORIES.remove(directory);
}
}
}
Closes the store to future operations. |
public IndexOutput createOutput(String name) throws IOException {
ensureOpen();
File file = new File(directory, name);
if (file.exists() && !file.delete()) // delete existing, if any
throw new IOException("Cannot overwrite: " + file);
return new FSIndexOutput(file);
}
Creates a new, empty file in the directory with the given name.
Returns a stream writing this file. |
public void deleteFile(String name) throws IOException {
ensureOpen();
File file = new File(directory, name);
if (!file.delete())
throw new IOException("Cannot delete " + file);
}
Removes an existing file in the directory. |
public boolean fileExists(String name) {
ensureOpen();
File file = new File(directory, name);
return file.exists();
}
Returns true iff a file with the given name exists. |
public long fileLength(String name) {
ensureOpen();
File file = new File(directory, name);
return file.length();
}
Returns the length in bytes of a file in the directory. |
public long fileModified(String name) {
ensureOpen();
File file = new File(directory, name);
return file.lastModified();
}
Returns the time the named file was last modified. |
public static long fileModified(File directory,
String name) {
File file = new File(directory, name);
return file.lastModified();
}
Returns the time the named file was last modified. |
public static FSDirectory getDirectory(String path) throws IOException {
return getDirectory(new File(path), null);
}
Returns the directory instance for the named location. |
public static FSDirectory getDirectory(File file) throws IOException {
return getDirectory(file, null);
}
Returns the directory instance for the named location. |
public static FSDirectory getDirectory(String path,
LockFactory lockFactory) throws IOException {
return getDirectory(new File(path), lockFactory);
}
Returns the directory instance for the named location. |
public static FSDirectory getDirectory(File file,
LockFactory lockFactory) throws IOException {
file = new File(file.getCanonicalPath());
if (file.exists() && !file.isDirectory())
throw new IOException(file + " not a directory");
if (!file.exists())
if (!file.mkdirs())
throw new IOException("Cannot create directory: " + file);
FSDirectory dir;
synchronized (DIRECTORIES) {
dir = (FSDirectory)DIRECTORIES.get(file);
if (dir == null) {
try {
dir = (FSDirectory)IMPL.newInstance();
} catch (Exception e) {
throw new RuntimeException("cannot load FSDirectory class: " + e.toString(), e);
}
dir.init(file, lockFactory);
DIRECTORIES.put(file, dir);
} else {
// Catch the case where a Directory is pulled from the cache, but has a
// different LockFactory instance.
if (lockFactory != null && lockFactory != dir.getLockFactory()) {
throw new IOException("Directory was previously created with a different LockFactory instance; please pass null as the lockFactory instance and use setLockFactory to change it");
}
}
}
synchronized (dir) {
dir.refCount++;
}
return dir;
}
Returns the directory instance for the named location. |
public static FSDirectory getDirectory(String path,
boolean create) throws IOException {
return getDirectory(new File(path), create);
} Deprecated! Use - IndexWriter's create flag, instead, to
create a new index.
Returns the directory instance for the named location. |
public static FSDirectory getDirectory(File file,
boolean create) throws IOException {
FSDirectory dir = getDirectory(file, null);
// This is now deprecated (creation should only be done
// by IndexWriter):
if (create) {
dir.create();
}
return dir;
} Deprecated! Use - IndexWriter's create flag, instead, to
create a new index.
Returns the directory instance for the named location. |
public static boolean getDisableLocks() {
return FSDirectory.disableLocks;
}
Returns whether Lucene's use of lock files is disabled. |
public File getFile() {
ensureOpen();
return directory;
}
|
public String getLockID() {
ensureOpen();
String dirName; // name to be hashed
try {
dirName = directory.getCanonicalPath();
} catch (IOException e) {
throw new RuntimeException(e.toString(), e);
}
byte digest[];
synchronized (DIGESTER) {
digest = DIGESTER.digest(dirName.getBytes());
}
StringBuffer buf = new StringBuffer();
buf.append("lucene-");
for (int i = 0; i < digest.length; i++) {
int b = digest[i];
buf.append(HEX_DIGITS[(b > > 4) & 0xf]);
buf.append(HEX_DIGITS[b & 0xf]);
}
return buf.toString();
}
|
public String[] list() {
ensureOpen();
return directory.list(IndexFileNameFilter.getFilter());
}
Returns an array of strings, one for each Lucene index file in the directory. |
public IndexInput openInput(String name) throws IOException {
ensureOpen();
return openInput(name, BufferedIndexInput.BUFFER_SIZE);
}
|
public IndexInput openInput(String name,
int bufferSize) throws IOException {
ensureOpen();
return new FSIndexInput(new File(directory, name), bufferSize);
}
|
public synchronized void renameFile(String from,
String to) throws IOException {
ensureOpen();
File old = new File(directory, from);
File nu = new File(directory, to);
/* This is not atomic. If the program crashes between the call to
delete() and the call to renameTo() then we're screwed, but I've
been unable to figure out how else to do this... */
if (nu.exists())
if (!nu.delete())
throw new IOException("Cannot delete " + nu);
// Rename the old file to the new one. Unfortunately, the renameTo()
// method does not work reliably under some JVMs. Therefore, if the
// rename fails, we manually rename by copying the old file to the new one
if (!old.renameTo(nu)) {
java.io.InputStream in = null;
java.io.OutputStream out = null;
try {
in = new FileInputStream(old);
out = new FileOutputStream(nu);
// see if the buffer needs to be initialized. Initialization is
// only done on-demand since many VM's will never run into the renameTo
// bug and hence shouldn't waste 1K of mem for no reason.
if (buffer == null) {
buffer = new byte[1024];
}
int len;
while ((len = in.read(buffer)) >= 0) {
out.write(buffer, 0, len);
}
// delete the old file.
old.delete();
}
catch (IOException ioe) {
IOException newExc = new IOException("Cannot rename " + old + " to " + nu);
newExc.initCause(ioe);
throw newExc;
}
finally {
try {
if (in != null) {
try {
in.close();
} catch (IOException e) {
throw new RuntimeException("Cannot close input stream: " + e.toString(), e);
}
}
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
throw new RuntimeException("Cannot close output stream: " + e.toString(), e);
}
}
}
}
}
} Deprecated!
Renames an existing file in the directory.
Warning: This is not atomic. |
public static void setDisableLocks(boolean doDisableLocks) {
// TODO: should this move up to the Directory base class? Also: should we
// make a per-instance (in addition to the static "default") version?
FSDirectory.disableLocks = doDisableLocks;
}
Set whether Lucene's use of lock files is disabled. By default,
lock files are enabled. They should only be disabled if the index
is on a read-only medium like a CD-ROM. |
public void sync(String name) throws IOException {
ensureOpen();
File fullFile = new File(directory, name);
boolean success = false;
int retryCount = 0;
IOException exc = null;
while(!success && retryCount < 5) {
retryCount++;
RandomAccessFile file = null;
try {
try {
file = new RandomAccessFile(fullFile, "rw");
file.getFD().sync();
success = true;
} finally {
if (file != null)
file.close();
}
} catch (IOException ioe) {
if (exc == null)
exc = ioe;
try {
// Pause 5 msec
Thread.sleep(5);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
}
}
if (!success)
// Throw original exception
throw exc;
}
|
public String toString() {
return this.getClass().getName() + "@" + directory;
}
|
public void touchFile(String name) {
ensureOpen();
File file = new File(directory, name);
file.setLastModified(System.currentTimeMillis());
}
Set the modified time of an existing file to now. |