/** @return the underlying filesystem directory */ public Path getDirectory() { ensureOpen(); return directory; }
@Override public long fileLength(String name) throws IOException { ensureOpen(); if (pendingDeletes.contains(name)) { throw new NoSuchFileException("file \"" + name + "\" is pending delete"); } return Files.size(directory.resolve(name)); }
@Override public String[] listAll() throws IOException { ensureOpen(); return listAll(directory, pendingDeletes); }
@Override public void rename(String source, String dest) throws IOException { ensureOpen(); if (pendingDeletes.contains(source)) { throw new NoSuchFileException("file \"" + source + "\" is pending delete and cannot be moved"); } maybeDeletePendingFiles(); if (pendingDeletes.remove(dest)) { privateDeleteFile(dest, true); // try again to delete it - this is best effort pendingDeletes.remove(dest); // watch out if the delete fails it's back in here. } Files.move(directory.resolve(source), directory.resolve(dest), StandardCopyOption.ATOMIC_MOVE); }
@Override public IndexOutput createOutput(String name, IOContext context) throws IOException { ensureOpen(); maybeDeletePendingFiles(); // If this file was pending delete, we are now bringing it back to life: if (pendingDeletes.remove(name)) { privateDeleteFile(name, true); // try again to delete it - this is best effort pendingDeletes.remove(name); // watch out - if the delete fails it put } return new FSIndexOutput(name); }
@Override public void sync(Collection<String> names) throws IOException { ensureOpen(); for (String name : names) { fsync(name); } maybeDeletePendingFiles(); }
@Override public void syncMetaData() throws IOException { // TODO: to improve listCommits(), IndexFileDeleter could call this after deleting segments_Ns ensureOpen(); IOUtils.fsync(directory, true); maybeDeletePendingFiles(); }
@Override public IndexOutput createTempOutput(String prefix, String suffix, IOContext context) throws IOException { ensureOpen(); maybeDeletePendingFiles(); while (true) { try { String name = IndexFileNames.segmentFileName(prefix, suffix + "_" + Long.toString(nextTempFileCounter.getAndIncrement(), Character.MAX_RADIX), "tmp"); if (pendingDeletes.contains(name)) { continue; } return new FSIndexOutput(name, StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW); } catch (FileAlreadyExistsException faee) { // Retry with next incremented name } } }
/** @return the underlying filesystem directory */ public Path getDirectory() { ensureOpen(); return directory; }
/** Returns the time the named file was last modified. */ public long fileModified(String name) { ensureOpen(); File file = new File(directory, name); return file.lastModified(); }
/** Returns the length in bytes of a file in the directory. */ 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. */ @Override public long fileLength(String name) throws IOException { ensureOpen(); return Files.size(directory.resolve(name)); }
/** Removes an existing file in the directory. */ @Override public void deleteFile(String name) throws IOException { ensureOpen(); Files.delete(directory.resolve(name)); }
/** Set the modified time of an existing file to now. */ public void touchFile(String name) { ensureOpen(); File file = new File(directory, name); file.setLastModified(System.currentTimeMillis()); }
/** Removes an existing file in the directory. */ public void deleteFile(String name) throws IOException { ensureOpen(); File file = new File(directory, name); if (!file.delete()) throw new IOException("Cannot delete " + file); }
/** Set the modified time of an existing file to now. */ public void touchFile(String name) { ensureOpen(); File file = new File(directory, name); file.setLastModified(System.currentTimeMillis()); }
/** Returns an array of strings, one for each Lucene index file in the directory. */ public String[] list() { ensureOpen(); return directory.list(IndexFileNameFilter.getFilter()); }
/** 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 { 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); }
@Override public void sync(Collection<String> names) throws IOException { ensureOpen(); for (String name : names) { fsync(name); } }
/** Creates an IndexOutput for the file with the given name. */ @Override public IndexOutput createOutput(String name, IOContext context) throws IOException { ensureOpen(); return new FSIndexOutput(name); }