You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mnemonic.apache.org by ga...@apache.org on 2016/04/09 00:00:04 UTC
[2/7] incubator-mnemonic git commit: format codestyle & add Apache
License
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/551c8247/core/src/main/java/org/apache/mnemonic/PMAddressTranslator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/PMAddressTranslator.java b/core/src/main/java/org/apache/mnemonic/PMAddressTranslator.java
index 1b1028d..b7a1600 100644
--- a/core/src/main/java/org/apache/mnemonic/PMAddressTranslator.java
+++ b/core/src/main/java/org/apache/mnemonic/PMAddressTranslator.java
@@ -1,5 +1,21 @@
-package org.apache.mnemonic;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.mnemonic;
/**
* translate persistent memory address for allocator
@@ -7,39 +23,39 @@ package org.apache.mnemonic;
*/
public interface PMAddressTranslator {
- /**
- * calculate the portable address
- *
- * @param addr
- * the address to be calculated
- *
- * @return the portable address
- */
- public long getPortableAddress(long addr);
+ /**
+ * calculate the portable address
+ *
+ * @param addr
+ * the address to be calculated
+ *
+ * @return the portable address
+ */
+ public long getPortableAddress(long addr);
+
+ /**
+ * calculate the effective address
+ *
+ * @param addr
+ * the address to be calculated
+ *
+ * @return the effective address
+ */
+ public long getEffectiveAddress(long addr);
+
+ /**
+ * get the base address
+ *
+ * @return the base address
+ */
+ public long getBaseAddress();
- /**
- * calculate the effective address
- *
- * @param addr
- * the address to be calculated
- *
- * @return the effective address
- */
- public long getEffectiveAddress(long addr);
-
- /**
- * get the base address
- *
- * @return the base address
- */
- public long getBaseAddress();
-
- /**
- * set the base address for calculation
- *
- * @param addr
- * the base address
- *
- */
- public long setBaseAddress(long addr);
+ /**
+ * set the base address for calculation
+ *
+ * @param addr
+ * the base address
+ *
+ */
+ public long setBaseAddress(long addr);
}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/551c8247/core/src/main/java/org/apache/mnemonic/Reclaim.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/Reclaim.java b/core/src/main/java/org/apache/mnemonic/Reclaim.java
index 7a43cb3..80d923f 100644
--- a/core/src/main/java/org/apache/mnemonic/Reclaim.java
+++ b/core/src/main/java/org/apache/mnemonic/Reclaim.java
@@ -1,3 +1,20 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package org.apache.mnemonic;
/**
@@ -6,18 +23,18 @@ package org.apache.mnemonic;
*/
public interface Reclaim<MRES> {
- /**
- * reclaim specified resources.
- *
- * @param mres
- * a resource to be reclaimed
- *
- * @param size
- * the size of resource, it will be set as null if unknown
- *
- * @return <tt>true</tt> if this resource has been reclaimed by this
- * callback. <tt>false</tt> if this resource needs to be claimed by
- * default reclaimer
- */
- public boolean reclaim(MRES mres, Long size);
+ /**
+ * reclaim specified resources.
+ *
+ * @param mres
+ * a resource to be reclaimed
+ *
+ * @param size
+ * the size of resource, it will be set as null if unknown
+ *
+ * @return <tt>true</tt> if this resource has been reclaimed by this callback.
+ * <tt>false</tt> if this resource needs to be claimed by default
+ * reclaimer
+ */
+ public boolean reclaim(MRES mres, Long size);
}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/551c8247/core/src/main/java/org/apache/mnemonic/RetrieveNonVolatileEntityError.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/RetrieveNonVolatileEntityError.java b/core/src/main/java/org/apache/mnemonic/RetrieveNonVolatileEntityError.java
index 22a0811..b38af5e 100644
--- a/core/src/main/java/org/apache/mnemonic/RetrieveNonVolatileEntityError.java
+++ b/core/src/main/java/org/apache/mnemonic/RetrieveNonVolatileEntityError.java
@@ -1,3 +1,20 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package org.apache.mnemonic;
/**
@@ -5,15 +22,14 @@ package org.apache.mnemonic;
*
*/
-
public class RetrieveNonVolatileEntityError extends RuntimeException {
- /**
- *
- */
- private static final long serialVersionUID = 3985459401986971131L;
+ /**
+ *
+ */
+ private static final long serialVersionUID = 3985459401986971131L;
- public RetrieveNonVolatileEntityError(String s) {
- super(s);
- }
+ public RetrieveNonVolatileEntityError(String s) {
+ super(s);
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/551c8247/core/src/main/java/org/apache/mnemonic/SysMemAllocator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/SysMemAllocator.java b/core/src/main/java/org/apache/mnemonic/SysMemAllocator.java
index 8889d48..36346ee 100644
--- a/core/src/main/java/org/apache/mnemonic/SysMemAllocator.java
+++ b/core/src/main/java/org/apache/mnemonic/SysMemAllocator.java
@@ -1,3 +1,20 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package org.apache.mnemonic;
import java.lang.reflect.*;
@@ -18,276 +35,268 @@ import sun.misc.Cleaner;
@SuppressWarnings("restriction")
public class SysMemAllocator extends CommonAllocator<SysMemAllocator> {
- private boolean m_activegc = true;
- private long m_gctimeout = 100;
- private Unsafe m_unsafe = null;
- private AtomicLong currentMemory = new AtomicLong(0L);
- private long maxStoreCapacity = 0L;
- private Map<Long, Long> m_chunksize = new ConcurrentHashMap<Long, Long>();
-
+ private boolean m_activegc = true;
+ private long m_gctimeout = 100;
+ private Unsafe m_unsafe = null;
+ private AtomicLong currentMemory = new AtomicLong(0L);
+ private long maxStoreCapacity = 0L;
+ private Map<Long, Long> m_chunksize = new ConcurrentHashMap<Long, Long>();
+ /**
+ * Constructor, it initialize and allocate a memory pool from Java off-heap
+ * with specified capacity.
+ *
+ * @param capacity
+ * specify the capacity of a system memory pool
+ *
+ * @param isnew
+ * a place holder, always specify it as true
+ *
+ * @throws Exception
+ * fail to retrieve Unsafe object
+ *
+ */
+ public SysMemAllocator(long capacity, boolean isnew) throws Exception {
+ m_unsafe = Utils.getUnsafe();
+ maxStoreCapacity = capacity;
/**
- * Constructor, it initialize and allocate a memory pool from Java off-heap
- * with specified capacity.
- *
- * @param capacity
- * specify the capacity of a system memory pool
- *
- * @param isnew
- * a place holder, always specify it as true
- *
- * @throws Exception
- * fail to retrieve Unsafe object
- *
+ * create a resource collector to release specified bytebuffer that backed
+ * by Java off-heap.
*/
- public SysMemAllocator(long capacity, boolean isnew) throws Exception {
- m_unsafe = Utils.getUnsafe();
- maxStoreCapacity = capacity;
- /**
- * create a resource collector to release specified bytebuffer that backed
- * by Java off-heap.
- */
- m_bufcollector = new ResCollector<MemBufferHolder<SysMemAllocator>, ByteBuffer>(
- new ResReclaim<ByteBuffer>() {
- @Override
- public synchronized void reclaim(ByteBuffer mres) {
- Long sz = Long.valueOf(mres.capacity());
- boolean cb_reclaimed = false;
- if (null != m_bufferreclaimer) {
- cb_reclaimed = m_bufferreclaimer.reclaim(mres, sz);
- }
- if (!cb_reclaimed) {
- try {
- Field cleanerField;
- cleanerField = mres.getClass().getDeclaredField(
- "cleaner");
- cleanerField.setAccessible(true);
- Cleaner cleaner = (Cleaner) cleanerField.get(mres);
- cleaner.clean();
- } catch (NoSuchFieldException | SecurityException
- | IllegalArgumentException | IllegalAccessException e) {
- e.printStackTrace();
- }
- mres = null;
- }
- currentMemory.addAndGet(-sz);
- }
- });
-
- /**
- * create a resource collector to release specified chunk that backed by
- * Java off-heap.
- */
- m_chunkcollector = new ResCollector<MemChunkHolder<SysMemAllocator>, Long>(
- new ResReclaim<Long>() {
- @Override
- public synchronized void reclaim(Long mres) {
- // System.out.println(String.format("Reclaim: %X ...", mres));
- Long sz = m_chunksize.remove(mres);
- boolean cb_reclaimed = false;
- if (null != m_chunkreclaimer) {
- cb_reclaimed = m_chunkreclaimer.reclaim(mres, sz);
- }
- if (!cb_reclaimed) {
- m_unsafe.freeMemory(mres);
- mres = null;
- }
- if (null != sz) {
- currentMemory.addAndGet(-sz);
- }
- }
- });
- }
+ m_bufcollector = new ResCollector<MemBufferHolder<SysMemAllocator>, ByteBuffer>(new ResReclaim<ByteBuffer>() {
+ @Override
+ public synchronized void reclaim(ByteBuffer mres) {
+ Long sz = Long.valueOf(mres.capacity());
+ boolean cb_reclaimed = false;
+ if (null != m_bufferreclaimer) {
+ cb_reclaimed = m_bufferreclaimer.reclaim(mres, sz);
+ }
+ if (!cb_reclaimed) {
+ try {
+ Field cleanerField;
+ cleanerField = mres.getClass().getDeclaredField("cleaner");
+ cleanerField.setAccessible(true);
+ Cleaner cleaner = (Cleaner) cleanerField.get(mres);
+ cleaner.clean();
+ } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
+ e.printStackTrace();
+ }
+ mres = null;
+ }
+ currentMemory.addAndGet(-sz);
+ }
+ });
/**
- * enable active garbage collection. the GC will be forced to collect garbages when
- * there is no more space for current allocation request.
- *
- * @param timeout
- * the timeout is used to yield for GC performing
- *
- * @return this allocator
+ * create a resource collector to release specified chunk that backed by
+ * Java off-heap.
*/
- @Override
- public SysMemAllocator enableActiveGC(long timeout) {
- m_activegc = true;
- m_gctimeout = timeout;
- return this;
- }
+ m_chunkcollector = new ResCollector<MemChunkHolder<SysMemAllocator>, Long>(new ResReclaim<Long>() {
+ @Override
+ public synchronized void reclaim(Long mres) {
+ // System.out.println(String.format("Reclaim: %X ...", mres));
+ Long sz = m_chunksize.remove(mres);
+ boolean cb_reclaimed = false;
+ if (null != m_chunkreclaimer) {
+ cb_reclaimed = m_chunkreclaimer.reclaim(mres, sz);
+ }
+ if (!cb_reclaimed) {
+ m_unsafe.freeMemory(mres);
+ mres = null;
+ }
+ if (null != sz) {
+ currentMemory.addAndGet(-sz);
+ }
+ }
+ });
+ }
- /**
- * disable active garbage collection.
- *
- * @return this allocator
- */
- @Override
- public SysMemAllocator disableActiveGC() {
- m_activegc = false;
- return this;
- }
+ /**
+ * enable active garbage collection. the GC will be forced to collect garbages
+ * when there is no more space for current allocation request.
+ *
+ * @param timeout
+ * the timeout is used to yield for GC performing
+ *
+ * @return this allocator
+ */
+ @Override
+ public SysMemAllocator enableActiveGC(long timeout) {
+ m_activegc = true;
+ m_gctimeout = timeout;
+ return this;
+ }
- /**
- * release the memory pool and close it.
- *
- */
- @Override
- public void close() {
- super.close();
+ /**
+ * disable active garbage collection.
+ *
+ * @return this allocator
+ */
+ @Override
+ public SysMemAllocator disableActiveGC() {
+ m_activegc = false;
+ return this;
+ }
+
+ /**
+ * release the memory pool and close it.
+ *
+ */
+ @Override
+ public void close() {
+ super.close();
+ }
+
+ /**
+ * force to synchronize uncommitted data to backed memory pool (this is a
+ * placeholder).
+ *
+ */
+ @Override
+ public void sync() {
+ }
+
+ /**
+ * re-size a specified chunk on its backed memory pool.
+ *
+ * @param mholder
+ * the holder of memory chunk. it can be null.
+ *
+ * @param size
+ * specify a new size of memory chunk
+ *
+ * @return the resized memory chunk handler
+ */
+ @Override
+ public MemChunkHolder<SysMemAllocator> resizeChunk(MemChunkHolder<SysMemAllocator> mholder, long size) {
+ MemChunkHolder<SysMemAllocator> ret = null;
+ boolean ac = null != mholder.getRefId();
+ if (size > 0) {
+ if (currentMemory.get() + size > maxStoreCapacity) {
+ if (m_activegc) {
+ m_chunkcollector.waitReclaimCoolDown(m_gctimeout);
+ }
+ }
+ if (currentMemory.get() + size <= maxStoreCapacity) {
+ Long addr = m_unsafe.reallocateMemory(mholder.get(), size);
+ if (0 != addr) {
+ mholder.clear();
+ mholder.destroy();
+ ret = new MemChunkHolder<SysMemAllocator>(this, addr, size);
+ if (ac) {
+ m_chunkcollector.register(ret);
+ }
+ }
+ }
}
+ return ret;
+ }
- /**
- * force to synchronize uncommitted data to backed memory pool
- * (this is a placeholder).
- *
- */
- @Override
- public void sync() {
+ /**
+ * resize a specified buffer on its backed memory pool.
+ *
+ * @param mholder
+ * the holder of memory buffer. it can be null.
+ *
+ * @param size
+ * specify a new size of memory chunk
+ *
+ * @return the resized memory buffer handler
+ *
+ */
+ @Override
+ public MemBufferHolder<SysMemAllocator> resizeBuffer(MemBufferHolder<SysMemAllocator> mholder, long size) {
+ MemBufferHolder<SysMemAllocator> ret = null;
+ boolean ac = null != mholder.getRefId();
+ if (size > 0) {
+ int bufpos = mholder.get().position();
+ int buflimit = mholder.get().limit();
+ if (currentMemory.get() + size > maxStoreCapacity) {
+ if (m_activegc) {
+ m_bufcollector.waitReclaimCoolDown(m_gctimeout);
+ }
+ }
+ if (currentMemory.get() + size <= maxStoreCapacity) {
+ ByteBuffer buf = Utils.resizeByteBuffer(mholder.get(), size);
+ if (null != buf) {
+ mholder.clear();
+ mholder.destroy();
+ buf.position(bufpos <= size ? bufpos : 0);
+ buf.limit(buflimit <= size ? buflimit : (int) size);
+ ret = new MemBufferHolder<SysMemAllocator>(this, buf);
+ if (ac) {
+ m_bufcollector.register(ret);
+ }
+ }
+ }
}
+ return ret;
+ }
- /**
- * re-size a specified chunk on its backed memory pool.
- *
- * @param mholder
- * the holder of memory chunk. it can be
- * null.
- *
- * @param size
- * specify a new size of memory chunk
- *
- * @return the resized memory chunk handler
- */
- @Override
- public MemChunkHolder<SysMemAllocator> resizeChunk(MemChunkHolder<SysMemAllocator> mholder, long size){
- MemChunkHolder<SysMemAllocator> ret = null;
- boolean ac = null != mholder.getRefId();
- if (size > 0) {
- if (currentMemory.get() + size > maxStoreCapacity) {
- if (m_activegc) {
- m_chunkcollector.waitReclaimCoolDown(m_gctimeout);
- }
- }
- if (currentMemory.get() + size <= maxStoreCapacity) {
- Long addr = m_unsafe.reallocateMemory(mholder.get(), size);
- if (0 != addr) {
- mholder.clear();
- mholder.destroy();
- ret = new MemChunkHolder<SysMemAllocator>(this, addr, size);
- if (ac) {
- m_chunkcollector.register(ret);
- }
- }
- }
- }
- return ret;
+ /**
+ * create a memory chunk that is managed by its holder.
+ *
+ * @param size
+ * specify the size of memory chunk
+ *
+ * @param autoreclaim
+ * specify whether or not to reclaim this chunk automatically
+ *
+ * @return a holder contains a memory chunk
+ */
+ @Override
+ public MemChunkHolder<SysMemAllocator> createChunk(long size, boolean autoreclaim) {
+ MemChunkHolder<SysMemAllocator> ret = null;
+ Long addr = null;
+ if (currentMemory.get() + size > maxStoreCapacity) {
+ if (m_activegc) {
+ m_chunkcollector.waitReclaimCoolDown(m_gctimeout);
+ }
}
-
- /**
- * resize a specified buffer on its backed memory pool.
- *
- * @param mholder
- * the holder of memory buffer. it can be
- * null.
- *
- * @param size
- * specify a new size of memory chunk
- *
- * @return the resized memory buffer handler
- *
- */
- @Override
- public MemBufferHolder<SysMemAllocator> resizeBuffer(MemBufferHolder<SysMemAllocator> mholder, long size) {
- MemBufferHolder<SysMemAllocator> ret = null;
- boolean ac = null != mholder.getRefId();
- if (size > 0) {
- int bufpos = mholder.get().position();
- int buflimit = mholder.get().limit();
- if (currentMemory.get() + size > maxStoreCapacity) {
- if (m_activegc) {
- m_bufcollector.waitReclaimCoolDown(m_gctimeout);
- }
- }
- if (currentMemory.get() + size <= maxStoreCapacity) {
- ByteBuffer buf = Utils.resizeByteBuffer(mholder.get(), size);
- if (null != buf) {
- mholder.clear();
- mholder.destroy();
- buf.position(bufpos <= size ? bufpos : 0);
- buf.limit(buflimit <= size ? buflimit : (int)size);
- ret = new MemBufferHolder<SysMemAllocator>(this, buf);
- if (ac) {
- m_bufcollector.register(ret);
- }
- }
- }
- }
- return ret;
+ if (currentMemory.get() + size <= maxStoreCapacity) {
+ addr = m_unsafe.allocateMemory(size);
}
-
- /**
- * create a memory chunk that is managed by its holder.
- *
- * @param size
- * specify the size of memory chunk
- *
- * @param autoreclaim
- * specify whether or not to reclaim this
- * chunk automatically
- *
- * @return a holder contains a memory chunk
- */
- @Override
- public MemChunkHolder<SysMemAllocator> createChunk(long size, boolean autoreclaim) {
- MemChunkHolder<SysMemAllocator> ret = null;
- Long addr = null;
- if (currentMemory.get() + size > maxStoreCapacity) {
- if (m_activegc) {
- m_chunkcollector.waitReclaimCoolDown(m_gctimeout);
- }
- }
- if (currentMemory.get() + size <= maxStoreCapacity) {
- addr = m_unsafe.allocateMemory(size);
- }
- if (null != addr && 0 != addr) {
- ret = new MemChunkHolder<SysMemAllocator>(this, addr, size);
- ret.setCollector(m_chunkcollector);
- if (autoreclaim) {
- m_chunkcollector.register(ret);
- }
- m_chunksize.put(addr, size);
- currentMemory.getAndAdd(size);
- }
- return ret;
+ if (null != addr && 0 != addr) {
+ ret = new MemChunkHolder<SysMemAllocator>(this, addr, size);
+ ret.setCollector(m_chunkcollector);
+ if (autoreclaim) {
+ m_chunkcollector.register(ret);
+ }
+ m_chunksize.put(addr, size);
+ currentMemory.getAndAdd(size);
}
+ return ret;
+ }
- /**
- * create a memory buffer that is managed by its holder.
- *
- * @param size
- * specify the size of memory buffer
- *
- * @return a holder contains a memory buffer
- */
- @Override
- public MemBufferHolder<SysMemAllocator> createBuffer(long size, boolean autoreclaim) {
- MemBufferHolder<SysMemAllocator> ret = null;
- ByteBuffer bb = null;
- if (currentMemory.get() + size > maxStoreCapacity) {
- if (m_activegc) {
- m_bufcollector.waitReclaimCoolDown(m_gctimeout);
- }
- }
- if (currentMemory.get() + size <= maxStoreCapacity) {
- bb = ByteBuffer.allocateDirect((int) size);
- }
- if (null != bb) {
- ret = new MemBufferHolder<SysMemAllocator>(this, bb);
- ret.setCollector(m_bufcollector);
- if (autoreclaim) {
- m_bufcollector.register(ret);
- }
- currentMemory.getAndAdd(size);
- }
- return ret;
+ /**
+ * create a memory buffer that is managed by its holder.
+ *
+ * @param size
+ * specify the size of memory buffer
+ *
+ * @return a holder contains a memory buffer
+ */
+ @Override
+ public MemBufferHolder<SysMemAllocator> createBuffer(long size, boolean autoreclaim) {
+ MemBufferHolder<SysMemAllocator> ret = null;
+ ByteBuffer bb = null;
+ if (currentMemory.get() + size > maxStoreCapacity) {
+ if (m_activegc) {
+ m_bufcollector.waitReclaimCoolDown(m_gctimeout);
+ }
+ }
+ if (currentMemory.get() + size <= maxStoreCapacity) {
+ bb = ByteBuffer.allocateDirect((int) size);
+ }
+ if (null != bb) {
+ ret = new MemBufferHolder<SysMemAllocator>(this, bb);
+ ret.setCollector(m_bufcollector);
+ if (autoreclaim) {
+ m_bufcollector.register(ret);
+ }
+ currentMemory.getAndAdd(size);
}
+ return ret;
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/551c8247/core/src/main/java/org/apache/mnemonic/Utils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/Utils.java b/core/src/main/java/org/apache/mnemonic/Utils.java
index 73f7a82..9425b98 100644
--- a/core/src/main/java/org/apache/mnemonic/Utils.java
+++ b/core/src/main/java/org/apache/mnemonic/Utils.java
@@ -1,3 +1,20 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package org.apache.mnemonic;
import java.lang.reflect.Field;
@@ -25,360 +42,361 @@ import org.apache.mnemonic.service.allocatorservice.NonVolatileMemoryAllocatorSe
*/
@SuppressWarnings("restriction")
public class Utils {
- private static long fSLEEP_INTERVAL = 100;
+ private static long fSLEEP_INTERVAL = 100;
+
+ public static final String ANSI_RESET = "\u001B[0m";
+ public static final String ANSI_BLACK = "\u001B[30m";
+ public static final String ANSI_RED = "\u001B[31m";
+ public static final String ANSI_GREEN = "\u001B[32m";
+ public static final String ANSI_YELLOW = "\u001B[33m";
+ public static final String ANSI_BLUE = "\u001B[34m";
+ public static final String ANSI_PURPLE = "\u001B[35m";
+ public static final String ANSI_CYAN = "\u001B[36m";
+ public static final String ANSI_WHITE = "\u001B[37m";
- public static final String ANSI_RESET = "\u001B[0m";
- public static final String ANSI_BLACK = "\u001B[30m";
- public static final String ANSI_RED = "\u001B[31m";
- public static final String ANSI_GREEN = "\u001B[32m";
- public static final String ANSI_YELLOW = "\u001B[33m";
- public static final String ANSI_BLUE = "\u001B[34m";
- public static final String ANSI_PURPLE = "\u001B[35m";
- public static final String ANSI_CYAN = "\u001B[36m";
- public static final String ANSI_WHITE = "\u001B[37m";
-
- private static Unsafe m_unsafe = null;
+ private static Unsafe m_unsafe = null;
- private static ServiceLoader<VolatileMemoryAllocatorService> m_vmasvcloader = null;
- private static ServiceLoader<NonVolatileMemoryAllocatorService> m_nvmasvcloader = null;
+ private static ServiceLoader<VolatileMemoryAllocatorService> m_vmasvcloader = null;
+ private static ServiceLoader<NonVolatileMemoryAllocatorService> m_nvmasvcloader = null;
- /**
- * retrieve a volatile memory allocator service
- *
- * @param id
- * specify a name of allocator to retrieve
- *
- * @return the volatile memory allocator service instance
- */
- public static VolatileMemoryAllocatorService getVolatileMemoryAllocatorService(String id) {
- return getVolatileMemoryAllocatorService(id, true);
+ /**
+ * retrieve a volatile memory allocator service
+ *
+ * @param id
+ * specify a name of allocator to retrieve
+ *
+ * @return the volatile memory allocator service instance
+ */
+ public static VolatileMemoryAllocatorService getVolatileMemoryAllocatorService(String id) {
+ return getVolatileMemoryAllocatorService(id, true);
+ }
+
+ /**
+ * retrieve a volatile memory allocator service
+ *
+ * @param id
+ * specify a name of allocator to retrieve
+ *
+ * @param allownvmsvc
+ * specify whether allow to treat non-volatile memory allocator as
+ * volatile one during searching
+ *
+ * @return the volatile memory allocator service instance
+ */
+ public static VolatileMemoryAllocatorService getVolatileMemoryAllocatorService(String id, boolean allownvmsvc) {
+ VolatileMemoryAllocatorService ret = null;
+ if (null == m_vmasvcloader) {
+ m_vmasvcloader = ServiceLoader.load(VolatileMemoryAllocatorService.class);
}
-
- /**
- * retrieve a volatile memory allocator service
- *
- * @param id
- * specify a name of allocator to retrieve
- *
- * @param allownvmsvc
- * specify whether allow to treat non-volatile memory allocator as volatile one during searching
- *
- * @return the volatile memory allocator service instance
- */
- public static VolatileMemoryAllocatorService getVolatileMemoryAllocatorService(String id, boolean allownvmsvc) {
- VolatileMemoryAllocatorService ret = null;
- if (null == m_vmasvcloader) {
- m_vmasvcloader = ServiceLoader.load(VolatileMemoryAllocatorService.class);
- }
- Iterator<VolatileMemoryAllocatorService> svcit = m_vmasvcloader.iterator();
- VolatileMemoryAllocatorService svc = null;
- while (null == ret && svcit.hasNext()) {
- svc = svcit.next();
- if (svc.getServiceId().equals(id)) {
- ret = svc;
- }
- }
- if (null == ret && allownvmsvc) {
- ret = getNonVolatileMemoryAllocatorService(id);
- }
- assert null != ret : "VolatileMemoryAllocatorService \'" + id + "\' not found!";
- return ret;
+ Iterator<VolatileMemoryAllocatorService> svcit = m_vmasvcloader.iterator();
+ VolatileMemoryAllocatorService svc = null;
+ while (null == ret && svcit.hasNext()) {
+ svc = svcit.next();
+ if (svc.getServiceId().equals(id)) {
+ ret = svc;
+ }
}
-
- /**
- * retrieve a non-volatile memory allocator service
- *
- * @param id
- * specify a name of allocator to retrieve
- *
- * @return the non-volatile memory allocator service instance
- */
- public static NonVolatileMemoryAllocatorService getNonVolatileMemoryAllocatorService(String id) {
- NonVolatileMemoryAllocatorService ret = null;
- if (null == m_nvmasvcloader) {
- m_nvmasvcloader = ServiceLoader.load(NonVolatileMemoryAllocatorService.class);
- }
- Iterator<NonVolatileMemoryAllocatorService> svcit = m_nvmasvcloader.iterator();
- NonVolatileMemoryAllocatorService svc = null;
- while (null == ret && svcit.hasNext()) {
- svc = svcit.next();
- if (svc.getServiceId().equals(id)) {
- ret = svc;
- }
- }
- assert null != ret : "NonVolatileMemoryAllocatorService \'" + id + "\' not found!";
- return ret;
+ if (null == ret && allownvmsvc) {
+ ret = getNonVolatileMemoryAllocatorService(id);
}
+ assert null != ret : "VolatileMemoryAllocatorService \'" + id + "\' not found!";
+ return ret;
+ }
- /**
- * Generates a unique name that contains current timestamp.
- *
- * @param format
- * the template that is used to generate unique name.
- *
- * @return unique path name.
- */
- public static String genUniquePathname(String format) {
- String ret = null;
- if (null != format && !format.isEmpty()) {
- ret = String.format(format, (new SimpleDateFormat(
- "ddMMyy-hhmmss.SSS").format(new Date())));
- }
- return ret;
+ /**
+ * retrieve a non-volatile memory allocator service
+ *
+ * @param id
+ * specify a name of allocator to retrieve
+ *
+ * @return the non-volatile memory allocator service instance
+ */
+ public static NonVolatileMemoryAllocatorService getNonVolatileMemoryAllocatorService(String id) {
+ NonVolatileMemoryAllocatorService ret = null;
+ if (null == m_nvmasvcloader) {
+ m_nvmasvcloader = ServiceLoader.load(NonVolatileMemoryAllocatorService.class);
}
-
- /**
- * retrieve the usage of memory.
- *
- * @return the size of memory has been occupied
- */
- public static long getMemoryUse() {
- putOutTheGarbage();
- long totalMemory = Runtime.getRuntime().totalMemory();
- putOutTheGarbage();
- long freeMemory = Runtime.getRuntime().freeMemory();
- return (totalMemory - freeMemory);
+ Iterator<NonVolatileMemoryAllocatorService> svcit = m_nvmasvcloader.iterator();
+ NonVolatileMemoryAllocatorService svc = null;
+ while (null == ret && svcit.hasNext()) {
+ svc = svcit.next();
+ if (svc.getServiceId().equals(id)) {
+ ret = svc;
+ }
}
+ assert null != ret : "NonVolatileMemoryAllocatorService \'" + id + "\' not found!";
+ return ret;
+ }
- /**
- * run garbage collections.
- */
- private static void putOutTheGarbage() {
- collectGarbage();
- collectGarbage();
+ /**
+ * Generates a unique name that contains current timestamp.
+ *
+ * @param format
+ * the template that is used to generate unique name.
+ *
+ * @return unique path name.
+ */
+ public static String genUniquePathname(String format) {
+ String ret = null;
+ if (null != format && !format.isEmpty()) {
+ ret = String.format(format, (new SimpleDateFormat("ddMMyy-hhmmss.SSS").format(new Date())));
}
+ return ret;
+ }
- /**
- * run a garbage collection.
- */
- public static void collectGarbage() {
- try {
- System.gc();
- Thread.sleep(fSLEEP_INTERVAL);
- System.runFinalization();
- Thread.sleep(fSLEEP_INTERVAL);
- } catch (InterruptedException ex) {
- ex.printStackTrace();
- }
- }
+ /**
+ * retrieve the usage of memory.
+ *
+ * @return the size of memory has been occupied
+ */
+ public static long getMemoryUse() {
+ putOutTheGarbage();
+ long totalMemory = Runtime.getRuntime().totalMemory();
+ putOutTheGarbage();
+ long freeMemory = Runtime.getRuntime().freeMemory();
+ return (totalMemory - freeMemory);
+ }
- /**
- * Retrieve an Unsafe object.
- *
- * @throws Exception
- * Error when get Unsafe object from runtime
- *
- * @return an unsafe object
- */
- public static Unsafe getUnsafe() throws Exception {
- if (null == m_unsafe) {
- Field field = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
- field.setAccessible(true);
- m_unsafe = (sun.misc.Unsafe) field.get(null);
- }
- return m_unsafe;
- }
+ /**
+ * run garbage collections.
+ */
+ private static void putOutTheGarbage() {
+ collectGarbage();
+ collectGarbage();
+ }
- /**
- * resize a bytebuffer with a new instance
- *
- * @param buf
- * specify a buf to resize
- *
- * @param size
- * specify the size for resizing
- *
- * @return the resized bytebuffer instance
- */
- public static ByteBuffer resizeByteBuffer(ByteBuffer buf, long size) {
- ByteBuffer ret = ByteBuffer.allocateDirect((int) size);
- if (ret != null) {
- if (null != buf) {
- ret.put(buf);
- ret.flip();
- }
- }
- return ret;
+ /**
+ * run a garbage collection.
+ */
+ public static void collectGarbage() {
+ try {
+ System.gc();
+ Thread.sleep(fSLEEP_INTERVAL);
+ System.runFinalization();
+ Thread.sleep(fSLEEP_INTERVAL);
+ } catch (InterruptedException ex) {
+ ex.printStackTrace();
}
+ }
- /**
- * create a new instance of Random using default fixed seed
- *
- * @return the instance of Random
- */
- public static Random createRandom() {
- return createRandom(0L);
+ /**
+ * Retrieve an Unsafe object.
+ *
+ * @throws Exception
+ * Error when get Unsafe object from runtime
+ *
+ * @return an unsafe object
+ */
+ public static Unsafe getUnsafe() throws Exception {
+ if (null == m_unsafe) {
+ Field field = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
+ field.setAccessible(true);
+ m_unsafe = (sun.misc.Unsafe) field.get(null);
}
+ return m_unsafe;
+ }
- /**
- * create a new instance of Random
- *
- * @param rgenseed
- * specify a random seed
- *
- * @return the instance of Random
- */
- public static Random createRandom(long rgenseed) {
- Random ret = new Random();
- if (0L == rgenseed) {
- rgenseed = System.currentTimeMillis();
- System.out.println("Random number generator seed is " + rgenseed);
- } else {
- System.out.println("Fixed Random number generator seed is " + rgenseed);
- }
- ret.setSeed(rgenseed);
- return ret;
+ /**
+ * resize a bytebuffer with a new instance
+ *
+ * @param buf
+ * specify a buf to resize
+ *
+ * @param size
+ * specify the size for resizing
+ *
+ * @return the resized bytebuffer instance
+ */
+ public static ByteBuffer resizeByteBuffer(ByteBuffer buf, long size) {
+ ByteBuffer ret = ByteBuffer.allocateDirect((int) size);
+ if (ret != null) {
+ if (null != buf) {
+ ret.put(buf);
+ ret.flip();
+ }
}
+ return ret;
+ }
- /**
- * generate a random string with fixed length
- *
- * @return the random string
- */
- public static String genRandomString() {
- return genRandomString(6);
- }
+ /**
+ * create a new instance of Random using default fixed seed
+ *
+ * @return the instance of Random
+ */
+ public static Random createRandom() {
+ return createRandom(0L);
+ }
- /**
- * generate a random string
- *
- * @param len
- * specify the length of this random string
- *
- * @return the random string
- */
- public static String genRandomString(int len) {
- return UUID.randomUUID().toString().replaceAll("-", "").toUpperCase().substring(0, len);
+ /**
+ * create a new instance of Random
+ *
+ * @param rgenseed
+ * specify a random seed
+ *
+ * @return the instance of Random
+ */
+ public static Random createRandom(long rgenseed) {
+ Random ret = new Random();
+ if (0L == rgenseed) {
+ rgenseed = System.currentTimeMillis();
+ System.out.println("Random number generator seed is " + rgenseed);
+ } else {
+ System.out.println("Fixed Random number generator seed is " + rgenseed);
}
+ ret.setSeed(rgenseed);
+ return ret;
+ }
- /**
- * assert the equality of two generic objects using compareTo() operator
- *
- * @param <T> the type of comparable objects
- *
- * @param actual
- * specify a object to be compared
- *
- * @param expected
- * specify a object to be expected
- *
- * @return true if equal according to compareTo()
- */
- public static <T extends Comparable<T>>boolean assertComparison(T actual, T expected) {
- boolean ret = false;
- if ((expected == null) && (actual == null)) {
- ret = true;
- } else if (expected != null) {
- ret = expected.compareTo(actual) == 0;
- }
- return ret;
- }
+ /**
+ * generate a random string with fixed length
+ *
+ * @return the random string
+ */
+ public static String genRandomString() {
+ return genRandomString(6);
+ }
+
+ /**
+ * generate a random string
+ *
+ * @param len
+ * specify the length of this random string
+ *
+ * @return the random string
+ */
+ public static String genRandomString(int len) {
+ return UUID.randomUUID().toString().replaceAll("-", "").toUpperCase().substring(0, len);
+ }
- /**
- * convert a long array to a initializer literal string.
- *
- * @param larr
- * specify a long array to be converted
- *
- * @return a literal string represent the initializer
- */
- public static String toInitLiteral(long[] larr) {
- return Arrays.toString(larr).replaceAll("\\[", "{").replaceAll("\\]", "}");
+ /**
+ * assert the equality of two generic objects using compareTo() operator
+ *
+ * @param <T>
+ * the type of comparable objects
+ *
+ * @param actual
+ * specify a object to be compared
+ *
+ * @param expected
+ * specify a object to be expected
+ *
+ * @return true if equal according to compareTo()
+ */
+ public static <T extends Comparable<T>> boolean assertComparison(T actual, T expected) {
+ boolean ret = false;
+ if ((expected == null) && (actual == null)) {
+ ret = true;
+ } else if (expected != null) {
+ ret = expected.compareTo(actual) == 0;
}
+ return ret;
+ }
- /**
- * convert a list of long array to a initializer literal string.
- *
- * @param llarr
- * specify a list of long array to be converted
- *
- * @return a literal string represent the initializer
- */
- public static String toInitLiteral(List<long[]> llarr) {
- List<String> slist = new ArrayList<String>();
- for (long[] larr : llarr) {
- slist.add(toInitLiteral(larr));
- }
- return "{" + String.join(",", slist) + "}";
+ /**
+ * convert a long array to a initializer literal string.
+ *
+ * @param larr
+ * specify a long array to be converted
+ *
+ * @return a literal string represent the initializer
+ */
+ public static String toInitLiteral(long[] larr) {
+ return Arrays.toString(larr).replaceAll("\\[", "{").replaceAll("\\]", "}");
+ }
+
+ /**
+ * convert a list of long array to a initializer literal string.
+ *
+ * @param llarr
+ * specify a list of long array to be converted
+ *
+ * @return a literal string represent the initializer
+ */
+ public static String toInitLiteral(List<long[]> llarr) {
+ List<String> slist = new ArrayList<String>();
+ for (long[] larr : llarr) {
+ slist.add(toInitLiteral(larr));
}
+ return "{" + String.join(",", slist) + "}";
+ }
- /**
- * retrieve a set of native field info from a list of object field info according to the field
- * id info. it forms a value info stack for native code to use as one standardized parameter
- *
- * @param objstack
- * a stack of object info retrieved from Durable.getNativeFieldInfo(), order matters
- *
- * @param fidinfostack
- * a stack of field id in the form of (next_fid, next_level_fid) order follows objstack
- * the last next_level_fid specifies the value's fid.
- * the last item of next_fid could be null if there is no next node
- * if it is null that means the last item is a object instead of node
- *
- * @return the stack of native field info
- *
- */
- public static List<long[]> getNativeParamForm(List<long[][]> objstack, long[][] fidinfostack) {
- List<long[]> ret = new ArrayList<long[]>();
- if (null == objstack ||
- null == fidinfostack ||
- fidinfostack.length != objstack.size()) {
- throw new IllegalArgumentException("Not the same depth");
- }
- for (int idx = 0; idx < fidinfostack.length; ++idx) {
- ret.add(genNativeStackItem(objstack.get(idx), fidinfostack[idx],
- idx == fidinfostack.length - 1));
- }
- return ret;
+ /**
+ * retrieve a set of native field info from a list of object field info
+ * according to the field id info. it forms a value info stack for native code
+ * to use as one standardized parameter
+ *
+ * @param objstack
+ * a stack of object info retrieved from
+ * Durable.getNativeFieldInfo(), order matters
+ *
+ * @param fidinfostack
+ * a stack of field id in the form of (next_fid, next_level_fid)
+ * order follows objstack the last next_level_fid specifies the
+ * value's fid. the last item of next_fid could be null if there is
+ * no next node if it is null that means the last item is a object
+ * instead of node
+ *
+ * @return the stack of native field info
+ *
+ */
+ public static List<long[]> getNativeParamForm(List<long[][]> objstack, long[][] fidinfostack) {
+ List<long[]> ret = new ArrayList<long[]>();
+ if (null == objstack || null == fidinfostack || fidinfostack.length != objstack.size()) {
+ throw new IllegalArgumentException("Not the same depth");
+ }
+ for (int idx = 0; idx < fidinfostack.length; ++idx) {
+ ret.add(genNativeStackItem(objstack.get(idx), fidinfostack[idx], idx == fidinfostack.length - 1));
}
+ return ret;
+ }
- /**
- * generate an item of native stack.
- *
- * @param oinfo
- * a object field info
- *
- * @param fidinfo
- * a pair of field id info
- *
- * @param allowfidnull
- * allow the first field id is null
- *
- * @return the native item
- */
- public static long[] genNativeStackItem(long[][] oinfo, long[] fidinfo, boolean allowfidnull) {
- long[] ret = new long[4];
- long fid;
- boolean found;
- if (fidinfo.length != 2) {
- throw new IllegalArgumentException("the length of field id array is not exactly 2");
+ /**
+ * generate an item of native stack.
+ *
+ * @param oinfo
+ * a object field info
+ *
+ * @param fidinfo
+ * a pair of field id info
+ *
+ * @param allowfidnull
+ * allow the first field id is null
+ *
+ * @return the native item
+ */
+ public static long[] genNativeStackItem(long[][] oinfo, long[] fidinfo, boolean allowfidnull) {
+ long[] ret = new long[4];
+ long fid;
+ boolean found;
+ if (fidinfo.length != 2) {
+ throw new IllegalArgumentException("the length of field id array is not exactly 2");
+ }
+ for (int idx = 0; idx < fidinfo.length; ++idx) {
+ ret[idx * 2] = 0;
+ ret[idx * 2 + 1] = 0;
+ fid = fidinfo[idx];
+ if (fid <= 0) {
+ if (allowfidnull && 0 == idx) {
+ continue;
+ } else {
+ throw new IllegalArgumentException("the field id is not greater than 0");
+ }
+ }
+ found = false;
+ for (long[] finfo : oinfo) {
+ if (finfo.length != 3) {
+ throw new IllegalArgumentException("the length of field array is not exactly 3");
}
- for (int idx = 0; idx < fidinfo.length; ++idx) {
- ret[idx*2] = 0;
- ret[idx*2 + 1] = 0;
- fid = fidinfo[idx];
- if (fid <= 0) {
- if (allowfidnull && 0 == idx) {
- continue;
- } else {
- throw new IllegalArgumentException("the field id is not greater than 0");
- }
- }
- found = false;
- for (long[] finfo : oinfo) {
- if (finfo.length != 3) {
- throw new IllegalArgumentException("the length of field array is not exactly 3");
- }
- if (fid == finfo[0]) {
- ret[idx*2] = finfo[1];
- ret[idx*2 + 1] = finfo[2];
- found = true;
- }
- }
- if (!found) {
- throw new IllegalArgumentException("field id not found");
- }
+ if (fid == finfo[0]) {
+ ret[idx * 2] = finfo[1];
+ ret[idx * 2 + 1] = finfo[2];
+ found = true;
}
- return ret;
+ }
+ if (!found) {
+ throw new IllegalArgumentException("field id not found");
+ }
}
+ return ret;
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/551c8247/core/src/main/java/org/apache/mnemonic/service/allocatorservice/NonVolatileMemoryAllocatorService.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/service/allocatorservice/NonVolatileMemoryAllocatorService.java b/core/src/main/java/org/apache/mnemonic/service/allocatorservice/NonVolatileMemoryAllocatorService.java
index 6549273..1007708 100644
--- a/core/src/main/java/org/apache/mnemonic/service/allocatorservice/NonVolatileMemoryAllocatorService.java
+++ b/core/src/main/java/org/apache/mnemonic/service/allocatorservice/NonVolatileMemoryAllocatorService.java
@@ -1,3 +1,20 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package org.apache.mnemonic.service.allocatorservice;
import org.flowcomputing.commons.primitives.*;
@@ -5,93 +22,93 @@ import java.nio.ByteBuffer;
public interface NonVolatileMemoryAllocatorService extends VolatileMemoryAllocatorService {
- /**
- * retrieve a bytebuffer from its handler
- *
- * @param id
- * the identifier of backed memory pool
- *
- * @param handler
- * the handler of a nonvolatile bytebuffer
- *
- * @return the nonvolatile bytebuffer
- *
- */
- public ByteBuffer retrieveByteBuffer(long id, long handler);
+ /**
+ * retrieve a bytebuffer from its handler
+ *
+ * @param id
+ * the identifier of backed memory pool
+ *
+ * @param handler
+ * the handler of a nonvolatile bytebuffer
+ *
+ * @return the nonvolatile bytebuffer
+ *
+ */
+ public ByteBuffer retrieveByteBuffer(long id, long handler);
+
+ /**
+ * retrieve the size of a nonvolatile memory object
+ *
+ * @param id
+ * the identifier of backed memory pool
+ *
+ * @param handler
+ * the handler of a nonvolatile object
+ *
+ * @return the size of nonvolatile object
+ *
+ */
+ public long retrieveSize(long id, long handler);
+
+ /**
+ * get the handler of a nonvolatile bytebuffer
+ *
+ * @param id
+ * the identifier of backed memory pool
+ *
+ * @param buf
+ * the nonvolatile bytebuffer
+ *
+ * @return the handler of this specified nonvolatile bytebuffer
+ *
+ */
+ public long getByteBufferHandler(long id, ByteBuffer buf);
+
+ /**
+ * set a handler to a key.
+ *
+ * @param id
+ * the identifier of backed memory pool
+ *
+ * @param key
+ * the key to set this handler
+ *
+ * @param handler
+ * the handler
+ */
+ public void setHandler(long id, long key, long handler);
+
+ /**
+ * get a handler from specified key.
+ *
+ * @param id
+ * the identifier of backed memory pool
+ *
+ * @param key
+ * the key to get its handler
+ *
+ * @return the handler of the specified key
+ */
+ public long getHandler(long id, long key);
- /**
- * retrieve the size of a nonvolatile memory object
- *
- * @param id
- * the identifier of backed memory pool
- *
- * @param handler
- * the handler of a nonvolatile object
- *
- * @return the size of nonvolatile object
- *
- */
- public long retrieveSize(long id, long handler);
-
- /**
- * get the handler of a nonvolatile bytebuffer
- *
- * @param id
- * the identifier of backed memory pool
- *
- * @param buf
- * the nonvolatile bytebuffer
- *
- * @return the handler of this specified nonvolatile bytebuffer
- *
- */
- public long getByteBufferHandler(long id, ByteBuffer buf);
+ /**
+ * return the number of available keys to use.
+ *
+ * @param id
+ * the identifier of backed memory pool
+ *
+ * @return the number of keys
+ */
+ public long handlerCapacity(long id);
- /**
- * set a handler to a key.
- *
- * @param id
- * the identifier of backed memory pool
- *
- * @param key
- * the key to set this handler
- *
- * @param handler
- * the handler
- */
- public void setHandler(long id, long key, long handler);
-
- /**
- * get a handler from specified key.
- *
- * @param id
- * the identifier of backed memory pool
- *
- * @param key
- * the key to get its handler
- *
- * @return the handler of the specified key
- */
- public long getHandler(long id, long key);
-
- /**
- * return the number of available keys to use.
- *
- * @param id
- * the identifier of backed memory pool
- *
- * @return the number of keys
- */
- public long handlerCapacity(long id);
-
- /**
- * return the base address of this persistent memory pool.
- *
- * @param id
- * the identifier of backed memory pool
- *
- * @return the base address of this pmem pool
- */
- public long getBaseAddress(long id);
+ /**
+ * return the base address of this persistent memory pool.
+ *
+ * @param id
+ * the identifier of backed memory pool
+ *
+ * @return the base address of this pmem pool
+ */
+ public long getBaseAddress(long id);
}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/551c8247/core/src/main/java/org/apache/mnemonic/service/allocatorservice/VolatileMemoryAllocatorService.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/service/allocatorservice/VolatileMemoryAllocatorService.java b/core/src/main/java/org/apache/mnemonic/service/allocatorservice/VolatileMemoryAllocatorService.java
index 5d506c8..32f7c33 100644
--- a/core/src/main/java/org/apache/mnemonic/service/allocatorservice/VolatileMemoryAllocatorService.java
+++ b/core/src/main/java/org/apache/mnemonic/service/allocatorservice/VolatileMemoryAllocatorService.java
@@ -1,3 +1,20 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package org.apache.mnemonic.service.allocatorservice;
import org.flowcomputing.commons.primitives.*;
@@ -5,126 +22,123 @@ import java.nio.ByteBuffer;
public interface VolatileMemoryAllocatorService {
- /**
- * Provide the service identifier for this allocator
- *
- * @return the service identifer of this allocator
- */
- public String getServiceId();
+ /**
+ * Provide the service identifier for this allocator
+ *
+ * @return the service identifer of this allocator
+ */
+ public String getServiceId();
- /**
- * Initialize a memory pool through native interface backed by native
- * library.
- *
- * @param capacity
- * the capacity of memory pool
- *
- * @param uri
- * the location of memory pool will be created
- *
- * @param isnew
- * a place holder, always specify it as true
- *
- * @return the identifier of created memory pool
- */
- public long init(long capacity, String uri, boolean isnew);
+ /**
+ * Initialize a memory pool through native interface backed by native library.
+ *
+ * @param capacity
+ * the capacity of memory pool
+ *
+ * @param uri
+ * the location of memory pool will be created
+ *
+ * @param isnew
+ * a place holder, always specify it as true
+ *
+ * @return the identifier of created memory pool
+ */
+ public long init(long capacity, String uri, boolean isnew);
- /**
- * close the memory pool through native interface.
- *
- */
- public void close(long id);
+ /**
+ * close the memory pool through native interface.
+ *
+ */
+ public void close(long id);
+ /**
+ * force to synchronize uncommitted data to backed memory pool through native
+ * interface.
+ */
+ public void sync(long id);
- /**
- * force to synchronize uncommitted data to backed memory pool through
- * native interface.
- */
- public void sync(long id);
+ /**
+ * allocate specified size of memory block from backed memory pool.
+ *
+ * @param id
+ * the identifier of backed memory pool
+ *
+ * @param size
+ * specify size of memory block to be allocated
+ *
+ * @return the address of allocated memory block from native memory pool
+ */
+ public long allocate(long id, long size, boolean initzero);
- /**
- * allocate specified size of memory block from backed memory pool.
- *
- * @param id
- * the identifier of backed memory pool
- *
- * @param size
- * specify size of memory block to be allocated
- *
- * @return the address of allocated memory block from native memory pool
- */
- public long allocate(long id, long size, boolean initzero);
+ /**
+ * reallocate a specified size of memory block from backed memory pool.
+ *
+ * @param id
+ * the identifier of backed memory pool
+ *
+ * @param address
+ * the address of previous allocated memory block. it can be null.
+ *
+ * @param size
+ * specify new size of memory block to be reallocated
+ *
+ * @return the address of reallocated memory block from native memory pool
+ */
+ public long reallocate(long id, long address, long size, boolean initzero);
- /**
- * reallocate a specified size of memory block from backed memory pool.
- *
- * @param id
- * the identifier of backed memory pool
- *
- * @param address
- * the address of previous allocated memory block. it can be
- * null.
- *
- * @param size
- * specify new size of memory block to be reallocated
- *
- * @return the address of reallocated memory block from native memory pool
- */
- public long reallocate(long id, long address, long size, boolean initzero);
+ /**
+ * free a memory block by specify its address into backed memory pool.
+ *
+ * @param id
+ * the identifier of backed memory pool
+ *
+ * @param address
+ * the address of allocated memory block.
+ */
+ public void free(long id, long address);
- /**
- * free a memory block by specify its address into backed memory pool.
- *
- * @param id
- * the identifier of backed memory pool
- *
- * @param address
- * the address of allocated memory block.
- */
- public void free(long id, long address);
+ /**
+ * create a ByteBuffer object which backed buffer is coming from backed native
+ * memory pool.
+ *
+ * @param id
+ * the identifier of backed memory pool
+ *
+ * @param size
+ * the size of backed buffer that is managed by created ByteBuffer
+ * object.
+ *
+ * @return a created ByteBuffer object with a backed native memory block
+ */
+ public ByteBuffer createByteBuffer(long id, long size);
- /**
- * create a ByteBuffer object which backed buffer is coming from backed
- * native memory pool.
- *
- * @param id
- * the identifier of backed memory pool
- *
- * @param size
- * the size of backed buffer that is managed by created
- * ByteBuffer object.
- *
- * @return a created ByteBuffer object with a backed native memory block
- */
- public ByteBuffer createByteBuffer(long id, long size);
+ /**
+ * resize a ByteBuffer object which backed buffer is coming from backed native
+ * memory pool. NOTE: the ByteBuffer object will be renewed and lost metadata
+ * e.g. position, mark and etc.
+ *
+ * @param id
+ * the identifier of backed memory pool
+ *
+ * @param bytebuf
+ * the specified ByteBuffer object to be destroyed
+ *
+ * @param size
+ * the new size of backed buffer that is managed by created
+ * ByteBuffer object.
+ *
+ * @return a created ByteBuffer object with a backed native memory block
+ */
+ public ByteBuffer resizeByteBuffer(long id, ByteBuffer bytebuf, long size);
- /**
- * resize a ByteBuffer object which backed buffer is coming from backed
- * native memory pool.
- * NOTE: the ByteBuffer object will be renewed and lost metadata e.g. position, mark and etc.
- *
- * @param id
- * the identifier of backed memory pool
- *
- * @param bytebuf
- * the specified ByteBuffer object to be destroyed
- *
- * @param size
- * the new size of backed buffer that is managed by created
- * ByteBuffer object.
- *
- * @return a created ByteBuffer object with a backed native memory block
- */
- public ByteBuffer resizeByteBuffer(long id, ByteBuffer bytebuf, long size);
-
- /**
- * destroy a native memory block backed ByteBuffer object.
- *
- * @param id
- * the identifier of backed memory pool
- *
- * @param bytebuf
- * the specified ByteBuffer object to be destroyed
- */
- public void destroyByteBuffer(long id, ByteBuffer bytebuf);
+ /**
+ * destroy a native memory block backed ByteBuffer object.
+ *
+ * @param id
+ * the identifier of backed memory pool
+ *
+ * @param bytebuf
+ * the specified ByteBuffer object to be destroyed
+ */
+ public void destroyByteBuffer(long id, ByteBuffer bytebuf);
}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/551c8247/core/src/test/java/org/apache/mnemonic/BigDataMemAllocatorNGTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/mnemonic/BigDataMemAllocatorNGTest.java b/core/src/test/java/org/apache/mnemonic/BigDataMemAllocatorNGTest.java
index 6497879..61ea2ed 100644
--- a/core/src/test/java/org/apache/mnemonic/BigDataMemAllocatorNGTest.java
+++ b/core/src/test/java/org/apache/mnemonic/BigDataMemAllocatorNGTest.java
@@ -1,3 +1,19 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package org.apache.mnemonic;
@@ -13,46 +29,45 @@ import java.util.Random;
/**
* test the functionality of BigMemAllocator class.
*
- * @author Wang, Gang(Gary) {@literal <ga...@intel.com>}
*/
public class BigDataMemAllocatorNGTest {
- /**
- * test to allocate MemBufferHolder objects and then verify them.
- */
- @Test
- public void testMemByteBuffer() {
- Random randomGenerator = new Random();
- Allocator<BigDataMemAllocator> act = new BigDataMemAllocator(Utils.getVolatileMemoryAllocatorService("vmem"), 1024 * 1024 * 1024, ".", true);
- MemBufferHolder<?> mbh;
- for (int idx = 1; idx <= 5; ++idx) {
- int size = randomGenerator.nextInt(1024 * 1024) + 1024 * 1024;
- mbh = act.createBuffer(size);
- for (int i = 0; i < size; i++) {
- mbh.get().put((byte) randomGenerator.nextInt(255));
- }
- // if (bb.hasArray()) randomGenerator.nextBytes(bb.array());
- System.out.println(String.format("[Seq.%d] size %d - %d, (%s)",
- idx, size, mbh.get().capacity(), size == mbh.get()
- .capacity() ? "Correct" : "Failed!!!"));
- // mbh.destroy();
- }
- }
+ /**
+ * test to allocate MemBufferHolder objects and then verify them.
+ */
+ @Test
+ public void testMemByteBuffer() {
+ Random randomGenerator = new Random();
+ Allocator<BigDataMemAllocator> act = new BigDataMemAllocator(Utils.getVolatileMemoryAllocatorService("vmem"),
+ 1024 * 1024 * 1024, ".", true);
+ MemBufferHolder<?> mbh;
+ for (int idx = 1; idx <= 5; ++idx) {
+ int size = randomGenerator.nextInt(1024 * 1024) + 1024 * 1024;
+ mbh = act.createBuffer(size);
+ for (int i = 0; i < size; i++) {
+ mbh.get().put((byte) randomGenerator.nextInt(255));
+ }
+ // if (bb.hasArray()) randomGenerator.nextBytes(bb.array());
+ System.out.println(String.format("[Seq.%d] size %d - %d, (%s)", idx, size, mbh.get().capacity(),
+ size == mbh.get().capacity() ? "Correct" : "Failed!!!"));
+ // mbh.destroy();
+ }
+ }
- /**
- * test to allocate MemChunkHolder objects and then verify them.
- */
- @Test
- public void testMemChunk() {
- Random randomGenerator = new Random();
- Allocator<BigDataMemAllocator> act = new BigDataMemAllocator(Utils.getVolatileMemoryAllocatorService("vmem"), 1024 * 1024 * 1024, ".", true);
- MemChunkHolder<?> mch;
- for (int idx = 1; idx <= 5; ++idx) {
- int size = randomGenerator.nextInt(1024 * 1024) + 1024 * 1024;
- mch = act.createChunk(size);
- System.out.println(String.format("[Seq.%d] addr : %X", idx, size,
- mch.get()));
- mch.destroy();
- }
- }
+ /**
+ * test to allocate MemChunkHolder objects and then verify them.
+ */
+ @Test
+ public void testMemChunk() {
+ Random randomGenerator = new Random();
+ Allocator<BigDataMemAllocator> act = new BigDataMemAllocator(Utils.getVolatileMemoryAllocatorService("vmem"),
+ 1024 * 1024 * 1024, ".", true);
+ MemChunkHolder<?> mch;
+ for (int idx = 1; idx <= 5; ++idx) {
+ int size = randomGenerator.nextInt(1024 * 1024) + 1024 * 1024;
+ mch = act.createChunk(size);
+ System.out.println(String.format("[Seq.%d] addr : %X", idx, size, mch.get()));
+ mch.destroy();
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/551c8247/core/src/test/java/org/apache/mnemonic/BigDataPMemAllocatorNGTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/mnemonic/BigDataPMemAllocatorNGTest.java b/core/src/test/java/org/apache/mnemonic/BigDataPMemAllocatorNGTest.java
index b515ef0..ee8b0c6 100644
--- a/core/src/test/java/org/apache/mnemonic/BigDataPMemAllocatorNGTest.java
+++ b/core/src/test/java/org/apache/mnemonic/BigDataPMemAllocatorNGTest.java
@@ -1,3 +1,20 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package org.apache.mnemonic;
import java.nio.ByteBuffer;
@@ -9,116 +26,109 @@ import org.testng.annotations.Test;
/**
*
- * @author Wang, Gang {@literal <ga...@intel.com>}
- *
*/
public class BigDataPMemAllocatorNGTest {
- @Test
- public void testPMemByteBuffer() {
- Random randomGenerator = new Random();
- BigDataPMemAllocator act = new BigDataPMemAllocator(Utils.getNonVolatileMemoryAllocatorService("pmalloc"), 1024 * 1024 * 1024, "./pmtest.dat", true);
- act.setBufferReclaimer(new Reclaim<ByteBuffer>() {
- @Override
- public boolean reclaim(ByteBuffer mres, Long sz) {
- System.out.println(String.format(
- "Reclaim Memory Buffer: %X Size: %s", System
- .identityHashCode(mres),
- null == sz ? "NULL" : sz.toString()));
- return false;
- }
- });
- MemBufferHolder<?> mbh;
- for (int idx = 1; idx <= 500; ++idx) {
- int size = randomGenerator.nextInt(1024 * 1024) + 1024 * 1024;
- mbh = act.createBuffer(size);
- Assert.assertNotNull(mbh);
- for (int i = 0; i < size; i++) {
- mbh.get().put((byte) randomGenerator.nextInt(255));
- }
- // if (bb.hasArray()) randomGenerator.nextBytes(bb.array());
- Assert.assertEquals(size, mbh.get().capacity());
- System.out.println(String.format("[Seq.%d] size %d - %d, (%s)",
- idx, size, mbh.get().capacity(), size == mbh.get()
- .capacity() ? "Correct" : "Failed!!!"));
- // mbh.destroy();
- }
- act.close();
- }
-
- @Test
- public void testGetBufferAddress() {
- BigDataPMemAllocator act = new BigDataPMemAllocator(Utils.getNonVolatileMemoryAllocatorService("pmalloc"), 1024 * 1024 * 1024, "./pmtest_buffer.dat", true);
- MemBufferHolder<BigDataPMemAllocator> mbh;
- mbh = act.createBuffer(20000);
- long phandler = act.getBufferHandler(mbh);
- System.out.println(String.format("**** 0x%X", phandler));
- act.close();
- }
-
- @Test
- public void testGenPMemByteBufferWithKey() {
- Random randomGenerator = Utils.createRandom();
- BigDataPMemAllocator act = new BigDataPMemAllocator(Utils.getNonVolatileMemoryAllocatorService("pmalloc"), 1024 * 1024 * 1024, "./pmtest_key.dat", true);
- act.setBufferReclaimer(new Reclaim<ByteBuffer>() {
- @Override
- public boolean reclaim(ByteBuffer mres, Long sz) {
- System.out.println(String.format(
- "Reclaim Memory Buffer: %X Size: %s", System
- .identityHashCode(mres),
- null == sz ? "NULL" : sz.toString()));
- return false;
- }
- });
- MemBufferHolder<BigDataPMemAllocator> mbh;
- Long phandler;
- long keycount = act.handlerCapacity();
- for (int idx = 0; idx < keycount; ++idx) {
- int size = randomGenerator.nextInt(1024 * 1024) + 1024 * 1024;
- mbh = act.createBuffer(size);
- if (6 == idx) {
- size += 2000;
- mbh = mbh.resize(size);
- }
- Assert.assertNotNull(mbh);
- mbh.get().putInt(size);
- Assert.assertEquals(size, mbh.get().capacity());
- System.out.println(String.format("Generating PKey Value [Seq.%d] size %d - %d, (%s)",
- idx, size, mbh.get().capacity(), size == mbh.get()
- .capacity() ? "Correct" : "Failed!!!"));
- phandler = act.getBufferHandler(mbh);
- System.out.println(String.format("---- 0x%X", phandler));
- act.setHandler(idx, phandler);
- mbh.cancelAutoReclaim();
- }
- act.close();
- }
-
- @Test(dependsOnMethods = {"testGenPMemByteBufferWithKey"})
- public void testCheckPMemByteBufferWithKey() {
- BigDataPMemAllocator act = new BigDataPMemAllocator(Utils.getNonVolatileMemoryAllocatorService("pmalloc"), 1024 * 1024 * 1024, "./pmtest_key.dat", true);
- act.setBufferReclaimer(new Reclaim<ByteBuffer>() {
- @Override
- public boolean reclaim(ByteBuffer mres, Long sz) {
- System.out.println(String.format(
- "Reclaim Memory Buffer: %X Size: %s", System
- .identityHashCode(mres),
- null == sz ? "NULL" : sz.toString()));
- return false;
- }
- });
- MemBufferHolder<BigDataPMemAllocator> mbh;
- for (int idx = 0; idx < act.handlerCapacity(); ++idx) {
- long phandler = act.getHandler(idx);
- mbh = act.retrieveBuffer(phandler);
- Assert.assertNotNull(mbh);
- int val = mbh.get().getInt();
- Assert.assertEquals(val, mbh.get().capacity());
- System.out.println(String.format("Checking PKey Value [Seq.%d] size %d - %d, (%s)",
- idx, val, mbh.get().capacity(), val == mbh.get()
- .capacity() ? "Correct" : "Failed!!!"));
- }
- act.close();
- }
-
+ @Test
+ public void testPMemByteBuffer() {
+ Random randomGenerator = new Random();
+ BigDataPMemAllocator act = new BigDataPMemAllocator(Utils.getNonVolatileMemoryAllocatorService("pmalloc"),
+ 1024 * 1024 * 1024, "./pmtest.dat", true);
+ act.setBufferReclaimer(new Reclaim<ByteBuffer>() {
+ @Override
+ public boolean reclaim(ByteBuffer mres, Long sz) {
+ System.out.println(String.format("Reclaim Memory Buffer: %X Size: %s", System.identityHashCode(mres),
+ null == sz ? "NULL" : sz.toString()));
+ return false;
+ }
+ });
+ MemBufferHolder<?> mbh;
+ for (int idx = 1; idx <= 500; ++idx) {
+ int size = randomGenerator.nextInt(1024 * 1024) + 1024 * 1024;
+ mbh = act.createBuffer(size);
+ Assert.assertNotNull(mbh);
+ for (int i = 0; i < size; i++) {
+ mbh.get().put((byte) randomGenerator.nextInt(255));
+ }
+ // if (bb.hasArray()) randomGenerator.nextBytes(bb.array());
+ Assert.assertEquals(size, mbh.get().capacity());
+ System.out.println(String.format("[Seq.%d] size %d - %d, (%s)", idx, size, mbh.get().capacity(),
+ size == mbh.get().capacity() ? "Correct" : "Failed!!!"));
+ // mbh.destroy();
+ }
+ act.close();
+ }
+
+ @Test
+ public void testGetBufferAddress() {
+ BigDataPMemAllocator act = new BigDataPMemAllocator(Utils.getNonVolatileMemoryAllocatorService("pmalloc"),
+ 1024 * 1024 * 1024, "./pmtest_buffer.dat", true);
+ MemBufferHolder<BigDataPMemAllocator> mbh;
+ mbh = act.createBuffer(20000);
+ long phandler = act.getBufferHandler(mbh);
+ System.out.println(String.format("**** 0x%X", phandler));
+ act.close();
+ }
+
+ @Test
+ public void testGenPMemByteBufferWithKey() {
+ Random randomGenerator = Utils.createRandom();
+ BigDataPMemAllocator act = new BigDataPMemAllocator(Utils.getNonVolatileMemoryAllocatorService("pmalloc"),
+ 1024 * 1024 * 1024, "./pmtest_key.dat", true);
+ act.setBufferReclaimer(new Reclaim<ByteBuffer>() {
+ @Override
+ public boolean reclaim(ByteBuffer mres, Long sz) {
+ System.out.println(String.format("Reclaim Memory Buffer: %X Size: %s", System.identityHashCode(mres),
+ null == sz ? "NULL" : sz.toString()));
+ return false;
+ }
+ });
+ MemBufferHolder<BigDataPMemAllocator> mbh;
+ Long phandler;
+ long keycount = act.handlerCapacity();
+ for (int idx = 0; idx < keycount; ++idx) {
+ int size = randomGenerator.nextInt(1024 * 1024) + 1024 * 1024;
+ mbh = act.createBuffer(size);
+ if (6 == idx) {
+ size += 2000;
+ mbh = mbh.resize(size);
+ }
+ Assert.assertNotNull(mbh);
+ mbh.get().putInt(size);
+ Assert.assertEquals(size, mbh.get().capacity());
+ System.out.println(String.format("Generating PKey Value [Seq.%d] size %d - %d, (%s)", idx, size,
+ mbh.get().capacity(), size == mbh.get().capacity() ? "Correct" : "Failed!!!"));
+ phandler = act.getBufferHandler(mbh);
+ System.out.println(String.format("---- 0x%X", phandler));
+ act.setHandler(idx, phandler);
+ mbh.cancelAutoReclaim();
+ }
+ act.close();
+ }
+
+ @Test(dependsOnMethods = { "testGenPMemByteBufferWithKey" })
+ public void testCheckPMemByteBufferWithKey() {
+ BigDataPMemAllocator act = new BigDataPMemAllocator(Utils.getNonVolatileMemoryAllocatorService("pmalloc"),
+ 1024 * 1024 * 1024, "./pmtest_key.dat", true);
+ act.setBufferReclaimer(new Reclaim<ByteBuffer>() {
+ @Override
+ public boolean reclaim(ByteBuffer mres, Long sz) {
+ System.out.println(String.format("Reclaim Memory Buffer: %X Size: %s", System.identityHashCode(mres),
+ null == sz ? "NULL" : sz.toString()));
+ return false;
+ }
+ });
+ MemBufferHolder<BigDataPMemAllocator> mbh;
+ for (int idx = 0; idx < act.handlerCapacity(); ++idx) {
+ long phandler = act.getHandler(idx);
+ mbh = act.retrieveBuffer(phandler);
+ Assert.assertNotNull(mbh);
+ int val = mbh.get().getInt();
+ Assert.assertEquals(val, mbh.get().capacity());
+ System.out.println(String.format("Checking PKey Value [Seq.%d] size %d - %d, (%s)", idx, val,
+ mbh.get().capacity(), val == mbh.get().capacity() ? "Correct" : "Failed!!!"));
+ }
+ act.close();
+ }
+
}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/551c8247/core/src/test/java/org/apache/mnemonic/ByteBufferSerializerNGTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/mnemonic/ByteBufferSerializerNGTest.java b/core/src/test/java/org/apache/mnemonic/ByteBufferSerializerNGTest.java
index 6150efe..883a00e 100644
--- a/core/src/test/java/org/apache/mnemonic/ByteBufferSerializerNGTest.java
+++ b/core/src/test/java/org/apache/mnemonic/ByteBufferSerializerNGTest.java
@@ -1,3 +1,19 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package org.apache.mnemonic;
@@ -16,50 +32,43 @@ import java.io.IOException;
/**
* test the functionalities of ByteBufferSerializer class
*
- * @author Wang, Gang(Gary) {@literal <ga...@intel.com>}
*/
public class ByteBufferSerializerNGTest {
- /**
- * test to convert any serializable object from/to ByteBuffer object that is
- * backed by Java heap.
- */
- @Test
- public void testToFromByteBuffer() throws IOException,
- ClassNotFoundException {
- Random randomGenerator = new Random();
- for (int idx = 0; idx < 100; idx++) {
- Payload pl = new Payload(randomGenerator.nextInt(1024 * 1024),
- String.format("Str is %d",
- randomGenerator.nextInt(1024 * 1024)),
- randomGenerator.nextDouble());
- ByteBuffer bb = ByteBufferSerializer.toByteBuffer(pl);
- Payload rpl = ByteBufferSerializer.toObject(bb);
- assertTrue(pl.compareTo(rpl) == 0);
- }
- }
+ /**
+ * test to convert any serializable object from/to ByteBuffer object that is
+ * backed by Java heap.
+ */
+ @Test
+ public void testToFromByteBuffer() throws IOException, ClassNotFoundException {
+ Random randomGenerator = new Random();
+ for (int idx = 0; idx < 100; idx++) {
+ Payload pl = new Payload(randomGenerator.nextInt(1024 * 1024),
+ String.format("Str is %d", randomGenerator.nextInt(1024 * 1024)), randomGenerator.nextDouble());
+ ByteBuffer bb = ByteBufferSerializer.toByteBuffer(pl);
+ Payload rpl = ByteBufferSerializer.toObject(bb);
+ assertTrue(pl.compareTo(rpl) == 0);
+ }
+ }
- /**
- * test to convert any serializable object from/to MemBufferHolder object
- * that is backed by native memory pool.
- */
- @Test
- public void testToFromMemBufferHolder() throws IOException,
- ClassNotFoundException {
- BigDataMemAllocator act = new BigDataMemAllocator(Utils.getVolatileMemoryAllocatorService("vmem"), 1024 * 1024 * 1024, ".", true);
+ /**
+ * test to convert any serializable object from/to MemBufferHolder object that
+ * is backed by native memory pool.
+ */
+ @Test
+ public void testToFromMemBufferHolder() throws IOException, ClassNotFoundException {
+ BigDataMemAllocator act = new BigDataMemAllocator(Utils.getVolatileMemoryAllocatorService("vmem"),
+ 1024 * 1024 * 1024, ".", true);
- Random randomGenerator = new Random();
- for (int idx = 0; idx < 100; idx++) {
- Payload pl = new Payload(randomGenerator.nextInt(1024 * 1024),
- String.format("Str is %d",
- randomGenerator.nextInt(1024 * 1024)),
- randomGenerator.nextDouble());
- MemBufferHolder<BigDataMemAllocator> mbh =
- ByteBufferSerializer.toMemBufferHolder(act, pl);
- Payload rpl = ByteBufferSerializer.fromMemBufferHolder(mbh);
- mbh.destroy();
- assertTrue(pl.compareTo(rpl) == 0);
- }
+ Random randomGenerator = new Random();
+ for (int idx = 0; idx < 100; idx++) {
+ Payload pl = new Payload(randomGenerator.nextInt(1024 * 1024),
+ String.format("Str is %d", randomGenerator.nextInt(1024 * 1024)), randomGenerator.nextDouble());
+ MemBufferHolder<BigDataMemAllocator> mbh = ByteBufferSerializer.toMemBufferHolder(act, pl);
+ Payload rpl = ByteBufferSerializer.fromMemBufferHolder(mbh);
+ mbh.destroy();
+ assertTrue(pl.compareTo(rpl) == 0);
+ }
- }
+ }
}