Skip to content

Commit 8c40920

Browse files
committed
MNEMONIC-293:Provide a way to persist the Bytebuffers created out of a durable chunk
1 parent 9385e71 commit 8c40920

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

mnemonic-core/src/main/java/org/apache/mnemonic/ChunkBuffer.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public class ChunkBuffer<A extends RetrievableAllocator<A>> {
2929

3030
protected DurableChunk<A> m_dchunk = null;
3131
protected ByteBuffer m_buffer = null;
32+
protected long m_offset;
33+
protected int m_size;
3234

3335
public ChunkBuffer(DurableChunk<A> dchunk, long offset, int size) {
3436
Field address, capacity;
@@ -45,6 +47,8 @@ public ChunkBuffer(DurableChunk<A> dchunk, long offset, int size) {
4547
capacity.setInt(bb, size);
4648
bb.limit(size);
4749
m_buffer = bb;
50+
m_offset = offset;
51+
m_size = size;
4852
} catch (NoSuchFieldException e) {
4953
throw new ConfigurationException("Buffer fields not found.");
5054
} catch (IllegalAccessException e) {
@@ -62,5 +66,28 @@ public ByteBuffer get() {
6266
public DurableChunk<A> getChunk() {
6367
return m_dchunk;
6468
}
69+
70+
public void persist() {
71+
if (m_dchunk.getAllocator() instanceof NonVolatileMemAllocator) {
72+
NonVolatileMemAllocator alloc = (NonVolatileMemAllocator) m_dchunk.getAllocator();
73+
alloc.persist(m_dchunk.get() + m_offset, m_size, false);
74+
} else {
75+
throw new UnsupportedOperationException("The ChunkBuffer does not backed by a non-volatile allocator");
76+
}
77+
}
78+
79+
public void sync() {
80+
m_dchunk.getAllocator().sync(m_dchunk.get() + m_offset, m_size, false);
81+
}
82+
83+
public void flush() {
84+
if (m_dchunk.getAllocator() instanceof NonVolatileMemAllocator) {
85+
NonVolatileMemAllocator alloc = (NonVolatileMemAllocator) m_dchunk.getAllocator();
86+
alloc.flush(m_dchunk.get() + m_offset, m_size, false);
87+
} else {
88+
throw new UnsupportedOperationException("The ChunkBuffer does not backed by a non-volatile allocator");
89+
}
90+
}
91+
6592
}
6693

0 commit comments

Comments
 (0)