private String getFileCacheName(String name) throws IOException { return _dirName + "/" + name + ":" + fileModified(name); }
private int fetchBlock(long position, byte[] b, int off, int len) throws IOException { // read whole block into cache and then provide needed data long blockId = getBlock(position); int blockOffset = (int) getPosition(position); int lengthToReadInBlock = Math.min(len, _blockSize - blockOffset); if (checkCache(blockId, blockOffset, b, off, lengthToReadInBlock)) { return lengthToReadInBlock; } else { readIntoCacheAndResult(blockId, blockOffset, b, off, lengthToReadInBlock); } return lengthToReadInBlock; }
public IndexInput openInput(String name, int bufferSize) throws IOException { final IndexInput source = _directory.openInput(name, _blockSize); if (_blockCacheFileTypes == null || isCachableFile(name)) { return new CachedIndexInput(source, _blockSize, name, getFileCacheName(name), _cache, bufferSize); } return source; }
@Override public void close() throws IOException { String[] files = listAll(); for (String file : files) { _cache.delete(getFileCacheName(file)); } _directory.close(); }
@Before public void setUp() throws IOException { file = new File("./tmp"); rm(file); file.mkdirs(); FSDirectory dir = FSDirectory.open(new File(file, "base")); directory = new BlockDirectory("test", dir, getBasicCache()); seed = new Random().nextLong(); random = new Random(seed); }
@Override public IndexInput openInput(final String name) throws IOException { return openInput(name, _blockSize); }
public BlockDirectory(String dirName, Directory directory, Cache cache, Set<String> blockCacheFileTypes) throws IOException { _dirName = dirName; _directory = directory; _blockSize = BLOCK_SIZE; _cache = cache; if (blockCacheFileTypes == null || blockCacheFileTypes.isEmpty()) { _blockCacheFileTypes = null; } else { _blockCacheFileTypes = blockCacheFileTypes; } setLockFactory(directory.getLockFactory()); }
public void deleteFile(String name) throws IOException { _cache.delete(getFileCacheName(name)); _directory.deleteFile(name); }
private void readIntoCacheAndResult(long blockId, int blockOffset, byte[] b, int off, int lengthToReadInBlock) throws IOException { long position = getRealPosition(blockId, 0); int length = (int) Math.min(_blockSize, _fileLength - position); _source.seek(position); byte[] buf = BufferStore.takeBuffer(_blockSize); _source.readBytes(buf, 0, length); System.arraycopy(buf, blockOffset, b, off, lengthToReadInBlock); _cache.update(_cacheName, blockId, buf); BufferStore.putBuffer(buf); }
dir.setLockFactory(NoLockFactory.getNoLockFactory()); BlockDirectory directory = new BlockDirectory("test", dir, cache);
dir.setLockFactory(factory); BlockDirectory directory = new BlockDirectory("test", dir, cache);