📄 fsdirectory.java
字号:
/** Returns an array of strings, one for each Lucene index file in the directory. */ public String[] list() { return directory.list(IndexFileNameFilter.getFilter()); } /** Returns true iff a file with the given name exists. */ public boolean fileExists(String name) { File file = new File(directory, name); return file.exists(); } /** Returns the time the named file was last modified. */ public long fileModified(String name) { 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(); } /** Set the modified time of an existing file to now. */ public void touchFile(String name) { File file = new File(directory, name); file.setLastModified(System.currentTimeMillis()); } /** Returns the length in bytes of a file in the directory. */ public long fileLength(String name) { File file = new File(directory, name); return file.length(); } /** Removes an existing file in the directory. */ public void deleteFile(String name) throws IOException { File file = new File(directory, name); if (!file.delete()) throw new IOException("Cannot delete " + file); } /** Renames an existing file in the directory. * Warning: This is not atomic. * @deprecated */ public synchronized void renameFile(String from, String to) throws IOException { 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); } } } } } } /** Creates a new, empty file in the directory with the given name. Returns a stream writing this file. */ public IndexOutput createOutput(String name) throws IOException { 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); } // Inherit javadoc public IndexInput openInput(String name) throws IOException { return openInput(name, BufferedIndexInput.BUFFER_SIZE); } // Inherit javadoc public IndexInput openInput(String name, int bufferSize) throws IOException { return new FSIndexInput(new File(directory, name), bufferSize); } /** * So we can do some byte-to-hexchar conversion below */ private static final char[] HEX_DIGITS = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; public String getLockID() { 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(); } /** Closes the store to future operations. */ public synchronized void close() { if (--refCount <= 0) { synchronized (DIRECTORIES) { DIRECTORIES.remove(directory); } } } public File getFile() { return directory; } /** For debug output. */ public String toString() { return this.getClass().getName() + "@" + directory; } protected static class FSIndexInput extends BufferedIndexInput { private static class Descriptor extends RandomAccessFile { // remember if the file is open, so that we don't try to close it // more than once private boolean isOpen; long position; final long length; public Descriptor(File file, String mode) throws IOException { super(file, mode); isOpen=true; length=length(); } public void close() throws IOException { if (isOpen) { isOpen=false; super.close(); } } protected void finalize() throws Throwable { try { close(); } finally { super.finalize(); } } } private final Descriptor file; boolean isClone; public FSIndexInput(File path) throws IOException { this(path, BufferedIndexInput.BUFFER_SIZE); } public FSIndexInput(File path, int bufferSize) throws IOException { super(bufferSize); file = new Descriptor(path, "r"); } /** IndexInput methods */ protected void readInternal(byte[] b, int offset, int len) throws IOException { synchronized (file) { long position = getFilePointer(); if (position != file.position) { file.seek(position); file.position = position; } int total = 0; do { int i = file.read(b, offset+total, len-total); if (i == -1) throw new IOException("read past EOF"); file.position += i; total += i; } while (total < len); } } public void close() throws IOException { // only close the file if this is not a clone if (!isClone) file.close(); } protected void seekInternal(long position) { } public long length() { return file.length; } public Object clone() { FSIndexInput clone = (FSIndexInput)super.clone(); clone.isClone = true; return clone; } /** Method used for testing. Returns true if the underlying * file descriptor is valid. */ boolean isFDValid() throws IOException { return file.getFD().valid(); } } protected static class FSIndexOutput extends BufferedIndexOutput { RandomAccessFile file = null; // remember if the file is open, so that we don't try to close it // more than once private boolean isOpen; public FSIndexOutput(File path) throws IOException { file = new RandomAccessFile(path, "rw"); isOpen = true; } /** output methods: */ public void flushBuffer(byte[] b, int offset, int size) throws IOException { file.write(b, offset, size); } public void close() throws IOException { // only close the file if it has not been closed yet if (isOpen) { boolean success = false; try { super.close(); success = true; } finally { isOpen = false; if (!success) { try { file.close(); } catch (Throwable t) { // Suppress so we don't mask original exception } } else file.close(); } } } /** Random-access methods */ public void seek(long pos) throws IOException { super.seek(pos); file.seek(pos); } public long length() throws IOException { return file.length(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -