/** * Will hint the OS that this is will not be accessed again and should hence be dropped from the buffer cache. * @param file File to drop from buffer cache */ public void dropFileFromCache(File file) { try { dropFileFromCache(new FileInputStream(file).getFD()); } catch (FileNotFoundException e) { logger.fine("No point in dropping a non-existing file from the buffer cache: " + e.getMessage()); } catch (IOException e) { throw new RuntimeException(e); } }
public NativeIO() { if (!initialized) { logger.warning("native IO not possible due to " + getError().getMessage()); } }
/** * Will hint the OS that this is will not be accessed again and should hence be dropped from the buffer cache. * @param fd The file descriptor to drop from buffer cache. */ public void dropFileFromCache(FileDescriptor fd) { try { fd.sync(); } catch (SyncFailedException e) { logger.warning("Sync failed while dropping cache: " + e.getMessage()); } if (initialized) { posix_fadvise(getNativeFD(fd), 0, 0, POSIX_FADV_DONTNEED); } }
private void runCompression(File oldFile) { File gzippedFile = new File(oldFile.getPath() + ".gz"); try { GZIPOutputStream compressor = new GZIPOutputStream(new FileOutputStream(gzippedFile), 0x100000); FileInputStream inputStream = new FileInputStream(oldFile); byte [] buffer = new byte[0x100000]; for (int read = inputStream.read(buffer); read > 0; read = inputStream.read(buffer)) { compressor.write(buffer, 0, read); } inputStream.close(); compressor.finish(); compressor.flush(); compressor.close(); NativeIO nativeIO = new NativeIO(); nativeIO.dropFileFromCache(oldFile); // Drop from cache in case somebody else has a reference to it preventing from dying quickly. oldFile.delete(); nativeIO.dropFileFromCache(gzippedFile); } catch (IOException e) { logger.warning("Got '" + e + "' while compressing '" + oldFile.getPath() + "'."); } }
executor.execute(() -> runCompression(oldFile)); } else { NativeIO nativeIO = new NativeIO(); nativeIO.dropFileFromCache(oldFile);