/* (non-Javadoc) * @see com.ongraphdb.store.DiskSore1#shutdown() */ public void shutdown() throws IOException { mappedDataBuffer.force(); mappedDataBuffer.clear(); dataLock.release(); dataChannel.close(); unmap(mappedDataBuffer); Runtime.getRuntime().removeShutdownHook(shutdownHookThread); }
public synchronized void close() { try { if (activateMemoryMappedBuffers) { buffer.force(); } storedFile.close(); closed = true; } catch (IOException e) { throw new RuntimeException(e); } }
public void flush() { synchronized (this) { if (this.closed) return; if (this.dirty) { MappedByteBuffer srcBuf = (MappedByteBuffer) this.threadLocalBuffer.getSourceBuffer(); srcBuf.force(); this.dirty = false; if (logger.isDebugEnabled()) logger.debug("Mapped page for " + this.pageFile + " was just flushed."); } } }
/** @tests {@link java.nio.MappedByteBuffer#force()} */ public void test_force() throws IOException { // buffer was not mapped in read/write mode FileInputStream fileInputStream = new FileInputStream(tmpFile); FileChannel fileChannelRead = fileInputStream.getChannel(); MappedByteBuffer mmbRead = fileChannelRead.map(MapMode.READ_ONLY, 0, fileChannelRead.size()); mmbRead.force(); FileInputStream inputStream = new FileInputStream(tmpFile); FileChannel fileChannelR = inputStream.getChannel(); MappedByteBuffer resultRead = fileChannelR.map(MapMode.READ_ONLY, 0, fileChannelR.size()); // If this buffer was not mapped in read/write mode, then invoking this method has no effect. assertEquals( "Invoking force() should have no effect when this buffer was not mapped in read/write mode", mmbRead, resultRead); // Buffer was mapped in read/write mode RandomAccessFile randomFile = new RandomAccessFile(tmpFile, "rw"); FileChannel fileChannelReadWrite = randomFile.getChannel(); MappedByteBuffer mmbReadWrite = fileChannelReadWrite.map(FileChannel.MapMode.READ_WRITE, 0, fileChannelReadWrite.size()); mmbReadWrite.put((byte) 'o'); mmbReadWrite.force(); RandomAccessFile random = new RandomAccessFile(tmpFile, "rw"); FileChannel fileChannelRW = random.getChannel(); MappedByteBuffer resultReadWrite = fileChannelRW.map(FileChannel.MapMode.READ_WRITE, 0, fileChannelRW.size()); // Invoking force() will change the buffer assertFalse(mmbReadWrite.equals(resultReadWrite)); fileChannelRead.close(); fileChannelR.close(); fileChannelReadWrite.close(); fileChannelRW.close(); }
@Override public void close() throws BlockStoreException { try { buffer.force(); if (System.getProperty("os.name").toLowerCase().contains("win")) { log.info("Windows mmap hack: Forcing buffer cleaning"); WindowsMMapHack.forceRelease(buffer); } buffer = null; // Allow it to be GCd and the underlying file mapping to go away. randomAccessFile.close(); } catch (IOException e) { throw new BlockStoreException(e); } }
public synchronized void flush(List<Update> updates) { Assert.state(storedFile != null, "The FileStorage is not initialized"); try { for (Update update : updates) { ensureCapacity(update.index * BYTES_IN_LONG + BYTES_IN_LONG); if (activateMemoryMappedBuffers) { buffer.putLong(update.index * BYTES_IN_LONG, update.value); } else { storedFile.seek(update.index * BYTES_IN_LONG); storedFile.writeLong(update.value); } } if (activateMemoryMappedBuffers) { buffer.force(); } storedFile.getFD().sync(); } catch (Exception e) { throw new RuntimeException(e); } }
@Override public void flush() { ((MappedByteBuffer) bb).force(); }
public static void main(String[] argv) throws Exception { // Create a temp file and get a channel connected to it File tempFile = File.createTempFile("mmaptest", null); RandomAccessFile file = new RandomAccessFile(tempFile, "rw"); FileChannel channel = file.getChannel(); // write strings to file by channel ByteBuffer temp = ByteBuffer.allocate(100); temp.put("This is the file content".getBytes()); temp.flip(); channel.write(temp, 0); temp.clear(); temp.put("This is more file content".getBytes()); temp.flip(); channel.write(temp, 8192); // readonly MappedByteBuffer ro = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); // read and write MappedByteBuffer rw = channel.map(FileChannel.MapMode.READ_WRITE, 0, channel.size()); // copy on write MappedByteBuffer cow = channel.map(FileChannel.MapMode.PRIVATE, 0, channel.size()); System.out.println("Begin"); // ro、rw、cow 相同 showBuffers(ro, rw, cow); // Modify the copy-on-write buffer,copy-on-write不会影响数据源 cow.position(8); cow.put("COW".getBytes()); System.out.println("Change to COW buffer"); showBuffers(ro, rw, cow); // Modify the read/write buffer,read/write会影响数据源 rw.position(9); rw.put(" R/W ".getBytes()); rw.position(8194); rw.put(" R/W ".getBytes()); rw.force(); System.out.println("Change to R/W buffer"); showBuffers(ro, rw, cow); // Write to the file through the channel; hit both pages temp.clear(); temp.put("Channel write ".getBytes()); temp.flip(); channel.write(temp, 0); temp.rewind(); channel.write(temp, 8202); System.out.println("Write on channel"); showBuffers(ro, rw, cow); // Modify the copy-on-write buffer again cow.position(8207); cow.put(" COW2 ".getBytes()); System.out.println("Second change to COW buffer"); showBuffers(ro, rw, cow); // Modify the read/write buffer rw.position(0); rw.put(" R/W2 ".getBytes()); rw.position(8210); rw.put(" R/W2 ".getBytes()); rw.force(); System.out.println("Second change to R/W buffer"); showBuffers(ro, rw, cow); }