/** * Closes the underlying Directory. After it's closed, no other invocations are expected on this Adapter; we don't check explicitly for it * as the Directory instance takes care of it. */ protected void close() { try { directory.close(); } catch (IOException e) { //log but continue execution: we might want to try closing more instance log.errorOnFSDirectoryClose(e); } }
/** * ContainsKey implementation for chunk elements */ protected Boolean containsKeyIntern(final FileCacheKey fileCacheKey) throws IOException { return Boolean.valueOf(directory.fileExists(fileCacheKey.getFileName())); }
/** * Guess in how many chunks we should split this file. Should return the same value consistently * for the same file (segments are immutable) so that a full segment can be rebuilt from the upper * layers without anyone actually specifying the chunks numbers. */ private int figureChunksNumber(String fileName) throws IOException { long fileLength = directory.fileLength(fileName); return figureChunksNumber(fileName, fileLength, autoChunkSize); }
/** * Load implementation for FileCacheKey: must return the metadata of the * requested file. */ private FileMetadata loadIntern(final FileCacheKey key) throws IOException { final String fileName = key.getFileName(); final long fileModified = directory.fileModified(fileName); final long fileLength = directory.fileLength(fileName); // We're forcing the buffer size of a to-be-read segment to the full file size: final int bufferSize = (int) Math.min(fileLength, (long)autoChunkSize); final FileMetadata meta = new FileMetadata(bufferSize); meta.setLastModified(fileModified); meta.setSize(fileLength); return meta; }
/** * Load implementation for FileListCacheKey; must return a * ConcurrentHashSet containing the names of all files in this Directory. */ private Object loadIntern(final FileListCacheKey key) throws IOException { final String[] listAll = directory.listAll(); final ConcurrentHashSet<String> fileNames = new ConcurrentHashSet<String>(); for (String filename : listAll) { fileNames.add(filename); } return fileNames; }
/** * Loads the actual byte array from a segment, in the range of a specific chunkSize. * Not that while the chunkSize is specified in this case, it's likely derived * from the invocations of other loading methods. */ private byte[] loadIntern(final ChunkCacheKey key) throws IOException { final String fileName = key.getFileName(); final long chunkId = key.getChunkId(); //needs to be long to upcast following operations int bufferSize = key.getBufferSize(); final long seekTo = chunkId * bufferSize; final byte[] buffer; final IndexInput input = directory.openInput(fileName); final long length = input.length(); try { if (seekTo != 0) { input.seek(seekTo); } bufferSize = (int) Math.min(length - seekTo, (long)bufferSize); buffer = new byte[bufferSize]; input.readBytes(buffer, 0, bufferSize); } finally { input.close(); } return buffer; }
String[] listAll = directory.listAll(); for (String fileName : listAll) { if (collectedKeys >= maxElements) return;
/** * ContainsKey implementation for chunk elements */ private Boolean containsKeyIntern(final ChunkCacheKey chunkCacheKey) throws IOException { try { final long length = directory.fileLength(chunkCacheKey.getFileName()); final int bufferSize = chunkCacheKey.getBufferSize(); final int chunkId = chunkCacheKey.getChunkId(); return Boolean.valueOf((chunkId * bufferSize) < (length + bufferSize)); } catch (FileNotFoundException nfne) { //Ok, we might check for file existence first.. but it's reasonable to be //optimistic. return Boolean.FALSE; } }