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/05 21:51:49 UTC
[1/7] incubator-mnemonic git commit: MNEMONIC-5 MNEMONIC-6 #resolve
refactored package names
Repository: incubator-mnemonic
Updated Branches:
refs/heads/master b53b24172 -> 8e83efe5b
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/test/java/org/apache/mnemonic/NonVolatilePersonNGTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/mnemonic/NonVolatilePersonNGTest.java b/core/src/test/java/org/apache/mnemonic/NonVolatilePersonNGTest.java
new file mode 100644
index 0000000..2edf6ff
--- /dev/null
+++ b/core/src/test/java/org/apache/mnemonic/NonVolatilePersonNGTest.java
@@ -0,0 +1,136 @@
+package org.apache.mnemonic;
+
+/**
+ *
+ *
+ */
+
+
+import java.nio.ByteBuffer;
+import java.util.List;
+import java.util.Random;
+import java.util.UUID;
+import org.apache.mnemonic.Utils;
+
+import org.testng.annotations.Test;
+
+public class NonVolatilePersonNGTest {
+ private long KEYCAPACITY;
+
+ @Test(expectedExceptions = { OutOfPersistentMemory.class })
+ public void testGenPeople() throws OutOfPersistentMemory, RetrieveNonVolatileEntityError {
+ Random rand = Utils.createRandom();
+ BigDataPMemAllocator act = new BigDataPMemAllocator(Utils.getNonVolatileMemoryAllocatorService("pmalloc"), 1024 * 1024 * 8, "./pobj_person.dat", true);
+ KEYCAPACITY = act.handlerCapacity();
+ 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;
+ }
+ });
+ act.setChunkReclaimer(new Reclaim<Long>() {
+ @Override
+ public boolean reclaim(Long mres, Long sz) {
+ System.out.println(String.format(
+ "Reclaim Memory Chunk: %X Size: %s", System
+ .identityHashCode(mres),
+ null == sz ? "NULL" : sz.toString()));
+ return false;
+ }
+ });
+
+ for (long i = 0; i < KEYCAPACITY; ++i) {
+ act.setHandler(i, 0L);
+ }
+
+ Person<Integer> mother;
+ Person<Integer> person;
+
+ long keyidx = 0;
+ long val;
+
+ try {
+ while(true) {
+ //if (keyidx >= KEYCAPACITY) break;
+
+ keyidx %= KEYCAPACITY;
+
+ System.out.printf("************ Generating People on Key %d ***********\n", keyidx);
+
+ val = act.getHandler(keyidx);
+ if (0L != val) {
+ PersonFactory.restore(act, val, true);
+ }
+
+ person = PersonFactory.create(act);
+ person.setAge((short)rand.nextInt(50));
+ person.setName(String.format("Name: [%s]", UUID.randomUUID().toString()), true);
+ person.setName(String.format("Name: [%s]", UUID.randomUUID().toString()), true);
+ person.setName(String.format("Name: [%s]", UUID.randomUUID().toString()), true);
+ person.setName(String.format("Name: [%s]", UUID.randomUUID().toString()), true);
+
+ act.setHandler(keyidx, person.getNonVolatileHandler());
+
+ for (int deep = 0; deep < rand.nextInt(100); ++deep) {
+
+ mother = PersonFactory.create(act);
+ mother.setAge((short)(50 + rand.nextInt(50)));
+ mother.setName(String.format("Name: [%s]", UUID.randomUUID().toString()), true);
+
+ person.setMother(mother, true);
+
+ person = mother;
+
+ }
+ ++keyidx;
+ }
+ }finally {
+ act.close();
+ }
+ }
+
+ @Test(dependsOnMethods = {"testGenPeople"})
+ public void testCheckPeople() throws RetrieveNonVolatileEntityError {
+ BigDataPMemAllocator act = new BigDataPMemAllocator(Utils.getNonVolatileMemoryAllocatorService("pmalloc"), 1024 * 1024 * 8, "./pobj_person.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;
+ }
+ });
+ act.setChunkReclaimer(new Reclaim<Long>() {
+ @Override
+ public boolean reclaim(Long mres, Long sz) {
+ System.out.println(String.format(
+ "Reclaim Memory Chunk: %X Size: %s", System
+ .identityHashCode(mres),
+ null == sz ? "NULL" : sz.toString()));
+ return false;
+ }
+ });
+
+ long val;
+ for (long i = 0; i < KEYCAPACITY; ++i) {
+ System.out.printf("----------Key %d--------------\n", i);
+ val = act.getHandler(i);
+ if (0L == val) {
+ break;
+ }
+ Person<Integer> person = PersonFactory.restore(act, val, true);
+ while (null != person) {
+ person.testOutput();
+ person = person.getMother();
+ }
+ }
+
+ act.close();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/test/java/org/apache/mnemonic/Payload.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/mnemonic/Payload.java b/core/src/test/java/org/apache/mnemonic/Payload.java
new file mode 100644
index 0000000..9f9468a
--- /dev/null
+++ b/core/src/test/java/org/apache/mnemonic/Payload.java
@@ -0,0 +1,29 @@
+
+package org.apache.mnemonic;
+
+/**
+ * a dummy object that is used for other test cases.
+ *
+ *
+ *
+ */
+public class Payload implements java.io.Serializable, Comparable<Payload> {
+
+ private static final long serialVersionUID = 187397440699436500L;
+
+ public Payload(int iv, String strv, double dv) {
+ ival = iv;
+ strval = strv;
+ dval = dv;
+ }
+
+ public int ival;
+ public String strval;
+ public double dval;
+
+ @Override
+ public int compareTo(Payload pl) {
+ return ival == pl.ival && strval.equals(pl.strval) && dval == pl.dval ? 0
+ : 1;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/test/java/org/apache/mnemonic/Person.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/mnemonic/Person.java b/core/src/test/java/org/apache/mnemonic/Person.java
new file mode 100644
index 0000000..f43001f
--- /dev/null
+++ b/core/src/test/java/org/apache/mnemonic/Person.java
@@ -0,0 +1,61 @@
+package org.apache.mnemonic;
+
+import java.util.List;
+
+/**
+ *
+ *
+ */
+
+@NonVolatileEntity
+public abstract class Person<E> implements Durable, Comparable<Person<E>> {
+ E element;
+
+ @Override
+ public void initializeAfterCreate() {
+ System.out.println("Initializing After Created");
+ }
+
+ @Override
+ public void initializeAfterRestore() {
+ System.out.println("Initializing After Restored");
+ }
+
+ @Override
+ public void setupGenericInfo(EntityFactoryProxy[] efproxies, GenericField.GType[] gftypes) {
+
+ }
+
+ public void testOutput() throws RetrieveNonVolatileEntityError {
+ System.out.printf("Person %s, Age: %d ( %s ) \n", getName(), getAge(),
+ null == getMother()? "No Recorded Mother" : "Has Recorded Mother");
+ }
+
+ public int compareTo(Person<E> anotherPerson) {
+ int ret = 0;
+ if (0 == ret) ret = getAge().compareTo(anotherPerson.getAge());
+ if (0 == ret) ret = getName().compareTo(anotherPerson.getName());
+ return ret;
+ }
+
+ @NonVolatileGetter
+ abstract public Short getAge();
+ @NonVolatileSetter
+ abstract public void setAge(Short age);
+
+ @NonVolatileGetter
+ abstract public String getName() throws RetrieveNonVolatileEntityError;
+ @NonVolatileSetter
+ abstract public void setName(String name, boolean destroy) throws OutOfPersistentMemory, RetrieveNonVolatileEntityError;
+
+ @NonVolatileGetter
+ abstract public Person<E> getMother() throws RetrieveNonVolatileEntityError;
+ @NonVolatileSetter
+ abstract public void setMother(Person<E> mother, boolean destroy) throws RetrieveNonVolatileEntityError;
+
+ @NonVolatileGetter
+ abstract public Person<E> getFather() throws RetrieveNonVolatileEntityError;
+ @NonVolatileSetter
+ abstract public void setFather(Person<E> mother, boolean destroy) throws RetrieveNonVolatileEntityError;
+}
+
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/test/resources/testng.xml
----------------------------------------------------------------------
diff --git a/core/src/test/resources/testng.xml b/core/src/test/resources/testng.xml
index 32ed9ab..2eda957 100644
--- a/core/src/test/resources/testng.xml
+++ b/core/src/test/resources/testng.xml
@@ -3,18 +3,18 @@
<suite name="Mnenomic Core test Suite" verbose="1" parallel="tests" thread-count="1">
<test name="Memory Clustering test" >
<classes>
- <class name="com.intel.bigdatamem.NonVolatileListNGTest" />
+ <class name="org.apache.mnemonic.NonVolatileListNGTest" />
</classes>
</test>
</suite>
<!--
- <class name="com.intel.bigdatamem.MemBufferHolderCachePoolNGTest" />
- <class name="com.intel.bigdatamem.BigDataMemAllocatorNGTest" />
- <class name="com.intel.bigdatamem.ByteBufferSerializerNGTest" />
- <class name="com.intel.bigdatamem.MemClusteringNGTest" />
- <class name="com.intel.bigdatamem.BigDataPMemAllocatorNGTest" />
- <class name="com.intel.bigdatamem.NonVolatilePersonNGTest" />
- <class name="com.intel.bigdatamem.NonVolatileListNGTest" />
+ <class name="org.apache.mnemonic.MemBufferHolderCachePoolNGTest" />
+ <class name="org.apache.mnemonic.BigDataMemAllocatorNGTest" />
+ <class name="org.apache.mnemonic.ByteBufferSerializerNGTest" />
+ <class name="org.apache.mnemonic.MemClusteringNGTest" />
+ <class name="org.apache.mnemonic.BigDataPMemAllocatorNGTest" />
+ <class name="org.apache.mnemonic.NonVolatilePersonNGTest" />
+ <class name="org.apache.mnemonic.NonVolatileListNGTest" />
-->
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/examples/pom.xml
----------------------------------------------------------------------
diff --git a/examples/pom.xml b/examples/pom.xml
index cb9803e..16dce06 100644
--- a/examples/pom.xml
+++ b/examples/pom.xml
@@ -2,20 +2,20 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
- <groupId>com.intel.mnemonic</groupId>
+ <groupId>org.apache.mnemonic</groupId>
<artifactId>mnemonic-parent</artifactId>
<version>2.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
- <groupId>com.intel.bigdatamem.example</groupId>
+ <groupId>org.apache.mnemonic.example</groupId>
<artifactId>mnemonic-examples</artifactId>
<packaging>jar</packaging>
<name>Mnemonic Project Examples</name>
<dependencies>
<dependency>
- <groupId>com.intel.bigdatamem</groupId>
+ <groupId>org.apache.mnemonic</groupId>
<artifactId>mnemonic-core</artifactId>
<version>${project.version}</version>
</dependency>
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/examples/src/main/java/com/intel/bigdatamem/example/Main.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/com/intel/bigdatamem/example/Main.java b/examples/src/main/java/com/intel/bigdatamem/example/Main.java
deleted file mode 100644
index 4370930..0000000
--- a/examples/src/main/java/com/intel/bigdatamem/example/Main.java
+++ /dev/null
@@ -1,189 +0,0 @@
-package com.intel.bigdatamem.example;
-
-import java.nio.ByteBuffer;
-import java.util.*;
-import sun.misc.Unsafe;
-import com.intel.bigdatamem.*;
-
-/**
- * Main is the class of example.
- *
- */
-@SuppressWarnings("restriction")
-public class Main {
-
- /**
- * Run a example code to demonstrate some basic functionalities.
- *
- * @param argv
- * array of commandline parameters
- */
- public static void main(String[] argv) throws Exception {
- Random randomGenerator = new Random();
-
- /* Generate configuration for each node */
- /*
- * Currently only one node is supported due to the external native code 'vmem'
- * returns null for multiple 'vmem' allocator instantialization
- */
- MemClustering.NodeConfig ncs[] = new MemClustering.NodeConfig[] {
- new MemClustering.NodeConfig(new SysMemAllocator(
- 1024 * 1024 * 20, true).disableActiveGC(),
- MemClustering.PerformanceLevel.FASTEST),
-
- // new MemClustering.NodeConfig(new
- // BigDataMemAllocator(1024*1024*20, ".",
- // true).disableActiveGC(),
- // MemClustering.PerformanceLevel.FAST),
- // new MemClustering.NodeConfig(new
- // BigDataMemAllocator(1024*1024*20, ".",
- // true).disableActiveGC(),
- // MemClustering.PerformanceLevel.NORMAL),
- new MemClustering.NodeConfig(new BigDataMemAllocator(
- Utils.getVolatileMemoryAllocatorService("vmem"),
- 1024 * 1024 * 20, ".", true),
- //Utils.getVolatileMemoryAllocatorService("pmalloc"),
- //1024 * 1024 * 20, "./example.dat", true),
- MemClustering.PerformanceLevel.SLOW),
- };
-
- for (MemClustering.NodeConfig nc : ncs) {
- /**
- * set a reclaimer for memory buffers
- */
- nc.getAllocator().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;
- }
- });
- /**
- * set a reclaimer for memory chunks
- */
- nc.getAllocator().setChunkReclaimer(new Reclaim<Long>() {
- @Override
- public boolean reclaim(Long mres, Long sz) {
- System.out.println(String.format(
- "Reclaim Memory Chunk: %X Size: %s", mres,
- null == sz ? "NULL" : sz.toString()));
- return false;
- }
- });
- }
-
- /* Deploy Memory Clustering */
- MemClustering mclst = new MemClustering(ncs);
-
- /**
- * Set event callback for allocator changing. this callback is used to
- * trace the event of spilling out of allocation when previous allocator
- * is unable to meet the allocation requirement so trying to switch to
- * next allocator.
- */
- mclst.setAllocatorChange(new MemClustering.AllocatorChange() {
- @Override
- public void changed(MemClustering.PerformanceLevel lvl,
- CommonAllocator prevallocator, CommonAllocator tgtallocator) {
- System.out.println(String.format(
- "AllocatorChanged: %s, %X -> %X", lvl.name(),
- System.identityHashCode(prevallocator),
- System.identityHashCode(tgtallocator)));
- }
- });
-
- /**
- * Set event callback for performance level changing. this callback is
- * used to trace the event of downgrading to low level performance allocation.
- */
- mclst.setPerformanceLevelChange(new MemClustering.PerformanceLevelChange() {
- @Override
- public void changed(MemClustering.PerformanceLevel prevlvl,
- MemClustering.PerformanceLevel lvl) {
- System.out.println(String.format("PerfLevelChanged: %s -> %s",
- null == prevlvl ? "NULL" : prevlvl.name(), lvl.name()));
- }
- });
-
- /**
- * Start to create a piece of memory resource backed ByteBuffer and then automatically
- * release it or manually release it every six objects.
- */
- System.out.println(Utils.ANSI_GREEN + "[[Demo Allocation, Auto Destruction "
- + "and Manual Destruction of Big Memory ByteBuffer.]]" + Utils.ANSI_RESET);
- MemBufferHolder<?> mbh;
- for (int idx = 1; idx <= 50; ++idx) {
- int size = randomGenerator.nextInt(1024 * 1024) + 1024 * 1024;
- mbh = mclst.createBuffer(size);
- if (null == mbh) {
- throw new OutOfMemoryError("Memory Cluster out of memory!");
- }
- for (int i = 0; i < size; i++) {
- /**
- * Manipulate the ByteBuffer backed by external memory resource.
- * Note: Do not assigned internal ByteBuffer object to any
- * variable, only use function get() to access it all time.
- */
- mbh.get().put((byte) randomGenerator.nextInt(255));
- }
- System.out.println(String.format("[Seq.%d] size %d - %d, (%s)",
- idx, size, mbh.get().capacity(), size == mbh.get()
- .capacity() ? "Correct" : "Failed!!!"));
- if (idx % 6 == 0) {
- /**
- * Force to release memory resource of specified ByteBuffer
- * object immediately.
- */
- System.out.println(String.format("Manually destroy Buffer at %X.",
- System.identityHashCode(mbh.get())));
- mbh.destroy();
- }
- System.out.println("-------------------");
- }
- //Utils.collectGarbage();
-
- /**
- * Start to create a piece of memory resource backed chunk and then automatically
- * release it or manually release it every six chunks.
- */
- System.out.println(Utils.ANSI_GREEN + "[[Demo Allocation, Auto Destruction "
- + "and Manual Destruction of Big Memory Chunk.]]" + Utils.ANSI_RESET);
- Unsafe unsafe = Utils.getUnsafe();
- MemChunkHolder<?> mch;
- for (int idx = 1; idx <= 50; ++idx) {
- int size = randomGenerator.nextInt(1024 * 1024) + 1024 * 1024;
- mch = mclst.createChunk(size);
- if (null == mch) {
- throw new OutOfMemoryError("Memory Cluster out of memory!");
- }
- //mch.cancelAutoReclaim();
- mch.resize(size - 10);
- System.out.printf("chunk size is %d \n", mch.getSize());
- for (int i = 0; i < mch.getSize(); i++) {
- /**
- * Manipulate the chunk data. Note: Do not
- * assigned the internal memory space to any variable, please always
- * use function get() to access it at all time.
- */
- unsafe.putByte(mch.get() + i,
- (byte) randomGenerator.nextInt(255));
- }
- System.out.println(String.format("[Seq.%d] size %d - %d at %X.",
- idx, size, mch.getSize(), mch.get()));
- if (idx % 6 == 0) {
- /**
- * Force to release memory resource of specified Chunk object
- * Immediately.
- */
- System.out.println(String.format("Manually destroy Chunk at %X.",
- mch.get()));
- mch.destroy();
- }
- System.out.println("-------------------");
- }
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/examples/src/main/java/org/apache/mnemonic/example/Main.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/mnemonic/example/Main.java b/examples/src/main/java/org/apache/mnemonic/example/Main.java
new file mode 100644
index 0000000..39dba17
--- /dev/null
+++ b/examples/src/main/java/org/apache/mnemonic/example/Main.java
@@ -0,0 +1,191 @@
+package org.apache.mnemonic.example;
+
+import java.nio.ByteBuffer;
+import java.util.*;
+
+import org.apache.mnemonic.*;
+
+import sun.misc.Unsafe;
+
+/**
+ * Main is the class of example.
+ *
+ */
+@SuppressWarnings("restriction")
+public class Main {
+
+ /**
+ * Run a example code to demonstrate some basic functionalities.
+ *
+ * @param argv
+ * array of commandline parameters
+ */
+ public static void main(String[] argv) throws Exception {
+ Random randomGenerator = new Random();
+
+ /* Generate configuration for each node */
+ /*
+ * Currently only one node is supported due to the external native code 'vmem'
+ * returns null for multiple 'vmem' allocator instantialization
+ */
+ MemClustering.NodeConfig ncs[] = new MemClustering.NodeConfig[] {
+ new MemClustering.NodeConfig(new SysMemAllocator(
+ 1024 * 1024 * 20, true).disableActiveGC(),
+ MemClustering.PerformanceLevel.FASTEST),
+
+ // new MemClustering.NodeConfig(new
+ // BigDataMemAllocator(1024*1024*20, ".",
+ // true).disableActiveGC(),
+ // MemClustering.PerformanceLevel.FAST),
+ // new MemClustering.NodeConfig(new
+ // BigDataMemAllocator(1024*1024*20, ".",
+ // true).disableActiveGC(),
+ // MemClustering.PerformanceLevel.NORMAL),
+ new MemClustering.NodeConfig(new BigDataMemAllocator(
+ Utils.getVolatileMemoryAllocatorService("vmem"),
+ 1024 * 1024 * 20, ".", true),
+ //Utils.getVolatileMemoryAllocatorService("pmalloc"),
+ //1024 * 1024 * 20, "./example.dat", true),
+ MemClustering.PerformanceLevel.SLOW),
+ };
+
+ for (MemClustering.NodeConfig nc : ncs) {
+ /**
+ * set a reclaimer for memory buffers
+ */
+ nc.getAllocator().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;
+ }
+ });
+ /**
+ * set a reclaimer for memory chunks
+ */
+ nc.getAllocator().setChunkReclaimer(new Reclaim<Long>() {
+ @Override
+ public boolean reclaim(Long mres, Long sz) {
+ System.out.println(String.format(
+ "Reclaim Memory Chunk: %X Size: %s", mres,
+ null == sz ? "NULL" : sz.toString()));
+ return false;
+ }
+ });
+ }
+
+ /* Deploy Memory Clustering */
+ MemClustering mclst = new MemClustering(ncs);
+
+ /**
+ * Set event callback for allocator changing. this callback is used to
+ * trace the event of spilling out of allocation when previous allocator
+ * is unable to meet the allocation requirement so trying to switch to
+ * next allocator.
+ */
+ mclst.setAllocatorChange(new MemClustering.AllocatorChange() {
+ @Override
+ public void changed(MemClustering.PerformanceLevel lvl,
+ CommonAllocator prevallocator, CommonAllocator tgtallocator) {
+ System.out.println(String.format(
+ "AllocatorChanged: %s, %X -> %X", lvl.name(),
+ System.identityHashCode(prevallocator),
+ System.identityHashCode(tgtallocator)));
+ }
+ });
+
+ /**
+ * Set event callback for performance level changing. this callback is
+ * used to trace the event of downgrading to low level performance allocation.
+ */
+ mclst.setPerformanceLevelChange(new MemClustering.PerformanceLevelChange() {
+ @Override
+ public void changed(MemClustering.PerformanceLevel prevlvl,
+ MemClustering.PerformanceLevel lvl) {
+ System.out.println(String.format("PerfLevelChanged: %s -> %s",
+ null == prevlvl ? "NULL" : prevlvl.name(), lvl.name()));
+ }
+ });
+
+ /**
+ * Start to create a piece of memory resource backed ByteBuffer and then automatically
+ * release it or manually release it every six objects.
+ */
+ System.out.println(Utils.ANSI_GREEN + "[[Demo Allocation, Auto Destruction "
+ + "and Manual Destruction of Big Memory ByteBuffer.]]" + Utils.ANSI_RESET);
+ MemBufferHolder<?> mbh;
+ for (int idx = 1; idx <= 50; ++idx) {
+ int size = randomGenerator.nextInt(1024 * 1024) + 1024 * 1024;
+ mbh = mclst.createBuffer(size);
+ if (null == mbh) {
+ throw new OutOfMemoryError("Memory Cluster out of memory!");
+ }
+ for (int i = 0; i < size; i++) {
+ /**
+ * Manipulate the ByteBuffer backed by external memory resource.
+ * Note: Do not assigned internal ByteBuffer object to any
+ * variable, only use function get() to access it all time.
+ */
+ mbh.get().put((byte) randomGenerator.nextInt(255));
+ }
+ System.out.println(String.format("[Seq.%d] size %d - %d, (%s)",
+ idx, size, mbh.get().capacity(), size == mbh.get()
+ .capacity() ? "Correct" : "Failed!!!"));
+ if (idx % 6 == 0) {
+ /**
+ * Force to release memory resource of specified ByteBuffer
+ * object immediately.
+ */
+ System.out.println(String.format("Manually destroy Buffer at %X.",
+ System.identityHashCode(mbh.get())));
+ mbh.destroy();
+ }
+ System.out.println("-------------------");
+ }
+ //Utils.collectGarbage();
+
+ /**
+ * Start to create a piece of memory resource backed chunk and then automatically
+ * release it or manually release it every six chunks.
+ */
+ System.out.println(Utils.ANSI_GREEN + "[[Demo Allocation, Auto Destruction "
+ + "and Manual Destruction of Big Memory Chunk.]]" + Utils.ANSI_RESET);
+ Unsafe unsafe = Utils.getUnsafe();
+ MemChunkHolder<?> mch;
+ for (int idx = 1; idx <= 50; ++idx) {
+ int size = randomGenerator.nextInt(1024 * 1024) + 1024 * 1024;
+ mch = mclst.createChunk(size);
+ if (null == mch) {
+ throw new OutOfMemoryError("Memory Cluster out of memory!");
+ }
+ //mch.cancelAutoReclaim();
+ mch.resize(size - 10);
+ System.out.printf("chunk size is %d \n", mch.getSize());
+ for (int i = 0; i < mch.getSize(); i++) {
+ /**
+ * Manipulate the chunk data. Note: Do not
+ * assigned the internal memory space to any variable, please always
+ * use function get() to access it at all time.
+ */
+ unsafe.putByte(mch.get() + i,
+ (byte) randomGenerator.nextInt(255));
+ }
+ System.out.println(String.format("[Seq.%d] size %d - %d at %X.",
+ idx, size, mch.getSize(), mch.get()));
+ if (idx % 6 == 0) {
+ /**
+ * Force to release memory resource of specified Chunk object
+ * Immediately.
+ */
+ System.out.println(String.format("Manually destroy Chunk at %X.",
+ mch.get()));
+ mch.destroy();
+ }
+ System.out.println("-------------------");
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 3f8a161..37bc5df 100644
--- a/pom.xml
+++ b/pom.xml
@@ -3,7 +3,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
- <groupId>com.intel.mnemonic</groupId>
+ <groupId>org.apache.mnemonic</groupId>
<artifactId>mnemonic-parent</artifactId>
<version>2.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
[2/7] incubator-mnemonic git commit: MNEMONIC-5 MNEMONIC-6 #resolve
refactored package names
Posted by ga...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/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
new file mode 100644
index 0000000..1b1028d
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/PMAddressTranslator.java
@@ -0,0 +1,45 @@
+package org.apache.mnemonic;
+
+
+/**
+ * translate persistent memory address for allocator
+ *
+ */
+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 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);
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/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
new file mode 100644
index 0000000..7a43cb3
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/Reclaim.java
@@ -0,0 +1,23 @@
+package org.apache.mnemonic;
+
+/**
+ * an interface to reclaim its memory resource.
+ *
+ */
+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);
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/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
new file mode 100644
index 0000000..22a0811
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/RetrieveNonVolatileEntityError.java
@@ -0,0 +1,19 @@
+package org.apache.mnemonic;
+
+/**
+ *
+ *
+ */
+
+
+public class RetrieveNonVolatileEntityError extends RuntimeException {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = 3985459401986971131L;
+
+ public RetrieveNonVolatileEntityError(String s) {
+ super(s);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/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
new file mode 100644
index 0000000..a693556
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/SysMemAllocator.java
@@ -0,0 +1,305 @@
+package org.apache.mnemonic;
+
+import java.lang.reflect.*;
+import java.nio.ByteBuffer;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.flowcomputing.commons.resgc.*;
+
+import sun.misc.Unsafe;
+import sun.misc.Cleaner;
+
+/**
+ * manage a system memory pool as a internal volatile allocator
+ *
+ */
+@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>();
+
+
+ /**
+ * 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;
+ /**
+ * 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);
+ }
+ }
+ });
+ }
+
+ /**
+ * 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;
+ }
+
+ /**
+ * 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) {
+ forceGC();
+ }
+ }
+ 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;
+ }
+
+ /**
+ * 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) {
+ forceGC();
+ }
+ }
+ 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;
+ }
+
+ /**
+ * 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) {
+ forceGC();
+ }
+ }
+ 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;
+ }
+
+ /**
+ * 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) {
+ forceGC();
+ }
+ }
+ 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;
+ }
+
+ /**
+ * force to perform GC that is used to release unused backed memory
+ * resources.
+ */
+ private void forceGC() {
+ System.gc();
+ try {
+ Thread.sleep(m_gctimeout);
+ } catch (Exception ex) {
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/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
new file mode 100644
index 0000000..73f7a82
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/Utils.java
@@ -0,0 +1,384 @@
+package org.apache.mnemonic;
+
+import java.lang.reflect.Field;
+import java.nio.ByteBuffer;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.Random;
+import java.util.UUID;
+
+import sun.misc.Unsafe;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ServiceConfigurationError;
+import java.util.ServiceLoader;
+import org.apache.mnemonic.service.allocatorservice.VolatileMemoryAllocatorService;
+import org.apache.mnemonic.service.allocatorservice.NonVolatileMemoryAllocatorService;
+
+/**
+ * <p>
+ * Utilities for project.
+ * </p>
+ *
+ */
+@SuppressWarnings("restriction")
+public class Utils {
+ 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";
+
+ private static Unsafe m_unsafe = 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
+ *
+ * @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;
+ }
+
+ /**
+ * 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;
+ }
+
+ /**
+ * 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 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);
+ }
+
+ /**
+ * run garbage collections.
+ */
+ private static void putOutTheGarbage() {
+ collectGarbage();
+ collectGarbage();
+ }
+
+ /**
+ * 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 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;
+ }
+
+ /**
+ * 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;
+ }
+
+ /**
+ * create a new instance of Random using default fixed seed
+ *
+ * @return the instance of Random
+ */
+ public static Random createRandom() {
+ return createRandom(0L);
+ }
+
+ /**
+ * 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;
+ }
+
+ /**
+ * 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);
+ }
+
+ /**
+ * 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 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;
+ }
+
+ /**
+ * 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");
+ }
+ 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");
+ }
+ }
+ return ret;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/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
new file mode 100644
index 0000000..6549273
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/service/allocatorservice/NonVolatileMemoryAllocatorService.java
@@ -0,0 +1,97 @@
+package org.apache.mnemonic.service.allocatorservice;
+
+import org.flowcomputing.commons.primitives.*;
+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 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);
+
+ /**
+ * 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);
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/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
new file mode 100644
index 0000000..5d506c8
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/service/allocatorservice/VolatileMemoryAllocatorService.java
@@ -0,0 +1,130 @@
+package org.apache.mnemonic.service.allocatorservice;
+
+import org.flowcomputing.commons.primitives.*;
+import java.nio.ByteBuffer;
+
+public interface VolatileMemoryAllocatorService {
+
+ /**
+ * 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);
+
+ /**
+ * 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);
+
+ /**
+ * 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);
+
+ /**
+ * 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);
+
+ /**
+ * 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);
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/resources/META-INFO/services/javax.annotation.processing.Processor
----------------------------------------------------------------------
diff --git a/core/src/main/resources/META-INFO/services/javax.annotation.processing.Processor b/core/src/main/resources/META-INFO/services/javax.annotation.processing.Processor
index 61d60fd..6981d38 100644
--- a/core/src/main/resources/META-INFO/services/javax.annotation.processing.Processor
+++ b/core/src/main/resources/META-INFO/services/javax.annotation.processing.Processor
@@ -1 +1 @@
-com.intel.bigdatamem.NonVolatileEntityProcessor
+org.apache.mnemonic.NonVolatileEntityProcessor
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/test/java/com/intel/bigdatamem/BigDataMemAllocatorNGTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/com/intel/bigdatamem/BigDataMemAllocatorNGTest.java b/core/src/test/java/com/intel/bigdatamem/BigDataMemAllocatorNGTest.java
deleted file mode 100644
index 56f6319..0000000
--- a/core/src/test/java/com/intel/bigdatamem/BigDataMemAllocatorNGTest.java
+++ /dev/null
@@ -1,59 +0,0 @@
-
-package com.intel.bigdatamem;
-
-import org.testng.annotations.Test;
-
-import com.intel.bigdatamem.Allocator;
-import com.intel.bigdatamem.BigDataMemAllocator;
-import com.intel.bigdatamem.MemBufferHolder;
-import com.intel.bigdatamem.MemChunkHolder;
-import com.intel.bigdatamem.Utils;
-
-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 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/8e83efe5/core/src/test/java/com/intel/bigdatamem/BigDataPMemAllocatorNGTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/com/intel/bigdatamem/BigDataPMemAllocatorNGTest.java b/core/src/test/java/com/intel/bigdatamem/BigDataPMemAllocatorNGTest.java
deleted file mode 100644
index 6b3c9c4..0000000
--- a/core/src/test/java/com/intel/bigdatamem/BigDataPMemAllocatorNGTest.java
+++ /dev/null
@@ -1,124 +0,0 @@
-package com.intel.bigdatamem;
-
-import java.nio.ByteBuffer;
-import java.util.Random;
-
-import org.testng.Assert;
-import org.testng.annotations.Test;
-import com.intel.bigdatamem.Utils;
-
-/**
- *
- * @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();
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/test/java/com/intel/bigdatamem/ByteBufferSerializerNGTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/com/intel/bigdatamem/ByteBufferSerializerNGTest.java b/core/src/test/java/com/intel/bigdatamem/ByteBufferSerializerNGTest.java
deleted file mode 100644
index 40bb541..0000000
--- a/core/src/test/java/com/intel/bigdatamem/ByteBufferSerializerNGTest.java
+++ /dev/null
@@ -1,65 +0,0 @@
-
-package com.intel.bigdatamem;
-
-import static org.testng.Assert.*;
-import org.testng.annotations.Test;
-
-import com.intel.bigdatamem.BigDataMemAllocator;
-import com.intel.bigdatamem.ByteBufferSerializer;
-import com.intel.bigdatamem.MemBufferHolder;
-import com.intel.bigdatamem.Utils;
-
-import java.nio.ByteBuffer;
-import java.util.Random;
-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 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);
- }
-
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/test/java/com/intel/bigdatamem/MemBufferHolderCachePoolNGTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/com/intel/bigdatamem/MemBufferHolderCachePoolNGTest.java b/core/src/test/java/com/intel/bigdatamem/MemBufferHolderCachePoolNGTest.java
deleted file mode 100644
index fd17c15..0000000
--- a/core/src/test/java/com/intel/bigdatamem/MemBufferHolderCachePoolNGTest.java
+++ /dev/null
@@ -1,150 +0,0 @@
-package com.intel.bigdatamem;
-
-import static org.testng.Assert.*;
-import org.testng.annotations.Test;
-
-import com.intel.bigdatamem.BigDataMemAllocator;
-import com.intel.bigdatamem.CachePool;
-import com.intel.bigdatamem.ContainerOverflowException;
-import com.intel.bigdatamem.EvictFilter;
-import com.intel.bigdatamem.DropEvent;
-import com.intel.bigdatamem.MemBufferHolder;
-import com.intel.bigdatamem.MemBufferHolderCachePool;
-import com.intel.bigdatamem.MemClustering;
-import com.intel.bigdatamem.Utils;
-
-import java.util.Random;
-
-/**
- * test the functionalities of MemBufferHolderCachePool class
- *
- * @author Wang, Gang(Gary) {@literal <ga...@intel.com>}
- */
-public class MemBufferHolderCachePoolNGTest {
-
- /**
- * test to aggressively allow any MemBufferHolder objects in pool to be able
- * to drop at will that prevents this cache pool from overflowing.
- */
- @Test
- public void testMemBufferHolderCachePoolWithoutOverflow() {
-
- MemBufferHolderCachePool<Integer> mbhcpool = new MemBufferHolderCachePool<Integer>(
- 1024 * 1024 * 10);
- Random randomGenerator = new Random();
- MemClustering.NodeConfig<?> ncs[] = new MemClustering.NodeConfig<?>[] {
- new MemClustering.NodeConfig<BigDataMemAllocator>(
- new BigDataMemAllocator(Utils.getVolatileMemoryAllocatorService("vmem"), 1024 * 1024 * 200, ".", true)
- .disableActiveGC(),
- MemClustering.PerformanceLevel.FAST),
- // new MemClustering.NodeConfig(new BigMemAllocator(1024*1024*20, ".",
- // true).disableActiveGC(), MemClustering.PerformanceLevel.NORMAL),
- // new MemClustering.NodeConfig(new BigMemAllocator(1024*1024*20, ".",
- // true).disableActiveGC(), MemClustering.PerformanceLevel.SLOW),
- };
- MemClustering mclst = new MemClustering(ncs);
- MemBufferHolder<?> mbh;
-
- DropEvent<Integer, MemBufferHolder<?>> dropevt =
- new DropEvent<Integer, MemBufferHolder<?>>() {
- @Override
- public void drop(CachePool<Integer, MemBufferHolder<?>> pool,
- Integer k, MemBufferHolder<?> v) {
- System.out.println(String.format("dropping idx: %d", k));
- v.destroy();
- }
- };
-
- EvictFilter<Integer, MemBufferHolder<?>> dfilter = new EvictFilter<Integer, MemBufferHolder<?>>() {
- @Override
- public boolean validate(CachePool<Integer, MemBufferHolder<?>> pool,
- Integer k, MemBufferHolder<?> v) {
- System.out.println(String.format("validating idx: %d", k));
- return true;
- }
- };
-
- for (int idx = 1; idx <= 100; ++idx) {
- int size = randomGenerator.nextInt(1024 * 1024) + 1024 * 1024;
- mbh = mclst.createBuffer(size);
- System.out.println(String.format(
- "\nallocating idx: %d - size: %d, FreeCap: %d", idx, size,
- mbhcpool.freeCapacity()));
- assertNotNull(mbh);
- assertNotNull(mbh.get());
- for (int i = 0; i < size; i++) {
- mbh.get().put((byte) randomGenerator.nextInt(255));
- }
-
- mbh.get().flip();
-
- assertEquals(size, mbh.get().capacity());
-
- mbhcpool.put(idx, mbh, dropevt, dfilter);
-
- }
- }
-
- /**
- * test to overflow a cache pool of MemBufferHolder objects that is caused
- * by preventing any objected in pool from dropping.
- */
- @Test(expectedExceptions = ContainerOverflowException.class)
- public void testMemBufferHolderCachePoolWithOverflow() {
-
- MemBufferHolderCachePool<Integer> mbhcpool = new MemBufferHolderCachePool<Integer>(
- 1024 * 1024 * 10);
- Random randomGenerator = new Random();
- MemClustering.NodeConfig<?> ncs[] = new MemClustering.NodeConfig<?>[] {
- new MemClustering.NodeConfig<BigDataMemAllocator>(
- new BigDataMemAllocator(Utils.getVolatileMemoryAllocatorService("vmem"), 1024 * 1024 * 200, ".", true)
- .disableActiveGC(),
- MemClustering.PerformanceLevel.FAST),
- // new MemClustering.NodeConfig(new BigMemAllocator(1024*1024*20, ".",
- // true).disableActiveGC(), MemClustering.PerformanceLevel.NORMAL),
- // new MemClustering.NodeConfig(new BigMemAllocator(1024*1024*20, ".",
- // true).disableActiveGC(), MemClustering.PerformanceLevel.SLOW),
- };
- MemClustering mclst = new MemClustering(ncs);
- MemBufferHolder<?> mbh;
-
- DropEvent<Integer, MemBufferHolder<?>> dropevt = new DropEvent<Integer, MemBufferHolder<?>>() {
- @Override
- public void drop(CachePool<Integer, MemBufferHolder<?>> pool,
- Integer k, MemBufferHolder<?> v) {
- System.out.println(String.format("dropping idx: %d", k));
- v.destroy();
- }
- };
-
- EvictFilter<Integer, MemBufferHolder<?>> dfilter = new EvictFilter<Integer, MemBufferHolder<?>>() {
- @Override
- public boolean validate(CachePool<Integer, MemBufferHolder<?>> pool,
- Integer k, MemBufferHolder<?> v) {
- System.out.println(String.format("validating idx: %d", k));
- return false;
- }
- };
-
- for (int idx = 1; idx <= 100; ++idx) {
- int size = randomGenerator.nextInt(1024 * 1024) + 1024 * 1024;
- mbh = mclst.createBuffer(size);
- System.out.println(String.format(
- "\nallocating idx: %d - size: %d, FreeCap: %d", idx, size,
- mbhcpool.freeCapacity()));
- assertNotNull(mbh);
- assertNotNull(mbh.get());
- for (int i = 0; i < size; i++) {
- mbh.get().put((byte) randomGenerator.nextInt(255));
- }
-
- mbh.get().flip();
-
- assertEquals(size, mbh.get().capacity());
-
- mbhcpool.put(idx, mbh, dropevt, dfilter);
-
- }
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/test/java/com/intel/bigdatamem/MemClusteringNGTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/com/intel/bigdatamem/MemClusteringNGTest.java b/core/src/test/java/com/intel/bigdatamem/MemClusteringNGTest.java
deleted file mode 100644
index 201d76f..0000000
--- a/core/src/test/java/com/intel/bigdatamem/MemClusteringNGTest.java
+++ /dev/null
@@ -1,190 +0,0 @@
-
-package com.intel.bigdatamem;
-
-import static org.testng.Assert.*;
-
-import org.testng.annotations.Test;
-
-import com.intel.bigdatamem.BigDataMemAllocator;
-import com.intel.bigdatamem.CommonAllocator;
-import com.intel.bigdatamem.MemBufferHolder;
-import com.intel.bigdatamem.MemClustering;
-import com.intel.bigdatamem.SysMemAllocator;
-import com.intel.bigdatamem.Utils;
-
-import java.util.Random;
-
-/**
- * test the functionalities of MemClustering class
- *
- * @author Wang, Gang(Gary) {@literal <ga...@intel.com>}
- */
-public class MemClusteringNGTest {
-
- /**
- * test to fill up memory pool without reclaim unused memory blocks that
- * will cause additional operations of allocation will be failed.
- *
- * @throws Exception
- */
- @Test(expectedExceptions = NullPointerException.class)
- public void testMemByteBufferWithoutActiveGC() throws Exception {
- Random randomGenerator = new Random();
- MemClustering.NodeConfig<?> ncs[] = new MemClustering.NodeConfig<?>[] {
- new MemClustering.NodeConfig<SysMemAllocator>(new SysMemAllocator(
- 1024 * 1024 * 20, true).disableActiveGC(),
- MemClustering.PerformanceLevel.FASTEST),
- new MemClustering.NodeConfig<BigDataMemAllocator>(new BigDataMemAllocator(Utils.getVolatileMemoryAllocatorService("vmem"),
- 1024 * 1024 * 20, ".", true).disableActiveGC(),
- MemClustering.PerformanceLevel.FAST),
- // new MemClustering.NodeConfig(new
- // BigMemAllocator(1024*1024*20, ".", true).disableActiveGC(),
- // MemClustering.PerformanceLevel.NORMAL),
- new MemClustering.NodeConfig<BigDataMemAllocator>(new BigDataMemAllocator(Utils.getVolatileMemoryAllocatorService("vmem"),
- 1024 * 1024 * 20, ".", true).disableActiveGC(),
- MemClustering.PerformanceLevel.SLOW), };
- MemClustering mclst = new MemClustering(ncs);
- MemBufferHolder<?> mbh;
- for (int idx = 1; idx <= 200; ++idx) {
- int size = randomGenerator.nextInt(1024 * 1024) + 1024 * 1024;
- mbh = mclst.createBuffer(size);
- for (int i = 0; i < size; i++) {
- mbh.get().put((byte) randomGenerator.nextInt(255));
- }
- assertEquals(size, mbh.get().capacity());
- }
- }
-
- /**
- * test to try to fill up memory pool and reclaim unused memory block as
- * required.
- */
- @Test
- public void testMemByteBufferWithActiveGC() {
- Random randomGenerator = new Random();
- MemClustering.NodeConfig<?> ncs[] = new MemClustering.NodeConfig<?>[] { new MemClustering.NodeConfig<BigDataMemAllocator>(
- new BigDataMemAllocator(Utils.getVolatileMemoryAllocatorService("vmem"), 1024 * 1024 * 20, ".", true),
- MemClustering.PerformanceLevel.NORMAL),
- // new MemClustering.NodeConfig(new BigMemAllocator(1024*1024*20, ".",
- // true), MemClustering.PerformanceLevel.SLOW),
- };
- MemClustering mclst = new MemClustering(ncs);
- MemBufferHolder<?> mbh;
- for (int idx = 1; idx <= 200; ++idx) {
- int size = randomGenerator.nextInt(1024 * 1024) + 1024 * 1024;
- mbh = mclst.createBuffer(size);
- for (int i = 0; i < size; i++) {
- mbh.get().put((byte) randomGenerator.nextInt(255));
- }
- assertEquals(size, mbh.get().capacity());
- }
- }
-
- /**
- * test to manually release memory resource once that is intended to be
- * unused.
- */
- @Test
- public void testMemByteBufferManualRelease() {
- Random randomGenerator = new Random();
- MemClustering.NodeConfig<?> ncs[] = new MemClustering.NodeConfig<?>[] {
- new MemClustering.NodeConfig<BigDataMemAllocator>(new BigDataMemAllocator(
- Utils.getVolatileMemoryAllocatorService("vmem"),
- 1024 * 1024 * 20, ".", true).disableActiveGC(),
- MemClustering.PerformanceLevel.FAST),
- // new MemClustering.NodeConfig(new
- // BigMemAllocator(1024*1024*20, ".", true).disableActiveGC(),
- // MemClustering.PerformanceLevel.NORMAL),
- new MemClustering.NodeConfig<BigDataMemAllocator>(new BigDataMemAllocator(
- Utils.getVolatileMemoryAllocatorService("vmem"),
- 1024 * 1024 * 20, ".", true),
- MemClustering.PerformanceLevel.SLOW), };
- MemClustering mclst = new MemClustering(ncs);
- MemBufferHolder<?> mbh;
- for (int idx = 1; idx <= 200; ++idx) {
- int size = randomGenerator.nextInt(1024 * 1024) + 1024 * 1024;
- mbh = mclst.createBuffer(size);
- for (int i = 0; i < size; i++) {
- mbh.get().put((byte) randomGenerator.nextInt(255));
- }
- assertEquals(size, mbh.get().capacity());
- // System.out.println("testMemByteBufferManualRelease");
- mbh.destroy();
- }
- }
-
- public boolean actriggered = false, pctriggered = false;
-
- /**
- * test change events that will be triggered by memory pool downgrading or
- * memory pool switching caused by fill up.
- *
- * @throws Exception
- */
- @Test
- public void testMemByteBufferWithChange() throws Exception {
- Random randomGenerator = new Random();
- MemClustering.NodeConfig<?> ncs[] = new MemClustering.NodeConfig<?>[] {
- new MemClustering.NodeConfig<SysMemAllocator>(new SysMemAllocator(
- 1024 * 1024 * 20, true).disableActiveGC(),
- MemClustering.PerformanceLevel.FASTEST),
- new MemClustering.NodeConfig<BigDataMemAllocator>(new BigDataMemAllocator(
- Utils.getVolatileMemoryAllocatorService("vmem"),
- 1024 * 1024 * 20, ".", true).disableActiveGC(),
- MemClustering.PerformanceLevel.FAST),
- // new MemClustering.NodeConfig(new
- // BigMemAllocator(1024*1024*20, ".", true).disableActiveGC(),
- // MemClustering.PerformanceLevel.NORMAL),
- new MemClustering.NodeConfig<BigDataMemAllocator>(new BigDataMemAllocator(
- Utils.getVolatileMemoryAllocatorService("vmem"),
- 1024 * 1024 * 20, ".", true),
- MemClustering.PerformanceLevel.SLOW), };
- MemClustering mclst = new MemClustering(ncs){};
-
- actriggered = false;
- pctriggered = false;
- mclst.setAllocatorChange(new MemClustering.AllocatorChange() {
- @Override
- public void changed(MemClustering.PerformanceLevel lvl,
- CommonAllocator<?> prevallocator, CommonAllocator<?> tgtallocator) {
- System.out.println(String.format(
- "Allocator Changed: %s, %X -> %X", lvl.name(),
- System.identityHashCode(prevallocator),
- System.identityHashCode(tgtallocator)));
- actriggered = true;
- }
- });
- mclst.setPerformanceLevelChange(new MemClustering.PerformanceLevelChange() {
- @Override
- public void changed(MemClustering.PerformanceLevel prevlvl,
- MemClustering.PerformanceLevel lvl) {
- System.out.println(String.format(
- "Perf.Level Changed: %s -> %s",
- null == prevlvl ? "NULL" : prevlvl.name(), lvl.name()));
- pctriggered = true;
- }
- });
-
- MemBufferHolder<?> mbh;
- for (int idx = 1; idx <= 100; ++idx) {
- int size = randomGenerator.nextInt(1024 * 1024) + 1024 * 1024;
- mbh = mclst.createBuffer(size);
- for (int i = 0; i < size; i++) {
- mbh.get().put((byte) randomGenerator.nextInt(255));
- }
- //mbh.destroy();
- }
- assertTrue(actriggered && pctriggered);
-
- }
-
- /*
- * @Test public void testMemChunk() { Random randomGenerator = new Random();
- * Allocator act = new BigMemAllocator(1024*1024*1024, "/home/wg/bm", true);
- * MemChunkHolder mch; for (int idx = 1; idx <= 50000; ++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/8e83efe5/core/src/test/java/com/intel/bigdatamem/NonVolatilePersonNGTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/com/intel/bigdatamem/NonVolatilePersonNGTest.java b/core/src/test/java/com/intel/bigdatamem/NonVolatilePersonNGTest.java
deleted file mode 100644
index 12cd5f7..0000000
--- a/core/src/test/java/com/intel/bigdatamem/NonVolatilePersonNGTest.java
+++ /dev/null
@@ -1,136 +0,0 @@
-package com.intel.bigdatamem;
-
-/**
- *
- *
- */
-
-
-import java.nio.ByteBuffer;
-import java.util.List;
-import java.util.Random;
-import java.util.UUID;
-import com.intel.bigdatamem.Utils;
-
-import org.testng.annotations.Test;
-
-public class NonVolatilePersonNGTest {
- private long KEYCAPACITY;
-
- @Test(expectedExceptions = { OutOfPersistentMemory.class })
- public void testGenPeople() throws OutOfPersistentMemory, RetrieveNonVolatileEntityError {
- Random rand = Utils.createRandom();
- BigDataPMemAllocator act = new BigDataPMemAllocator(Utils.getNonVolatileMemoryAllocatorService("pmalloc"), 1024 * 1024 * 8, "./pobj_person.dat", true);
- KEYCAPACITY = act.handlerCapacity();
- 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;
- }
- });
- act.setChunkReclaimer(new Reclaim<Long>() {
- @Override
- public boolean reclaim(Long mres, Long sz) {
- System.out.println(String.format(
- "Reclaim Memory Chunk: %X Size: %s", System
- .identityHashCode(mres),
- null == sz ? "NULL" : sz.toString()));
- return false;
- }
- });
-
- for (long i = 0; i < KEYCAPACITY; ++i) {
- act.setHandler(i, 0L);
- }
-
- Person<Integer> mother;
- Person<Integer> person;
-
- long keyidx = 0;
- long val;
-
- try {
- while(true) {
- //if (keyidx >= KEYCAPACITY) break;
-
- keyidx %= KEYCAPACITY;
-
- System.out.printf("************ Generating People on Key %d ***********\n", keyidx);
-
- val = act.getHandler(keyidx);
- if (0L != val) {
- PersonFactory.restore(act, val, true);
- }
-
- person = PersonFactory.create(act);
- person.setAge((short)rand.nextInt(50));
- person.setName(String.format("Name: [%s]", UUID.randomUUID().toString()), true);
- person.setName(String.format("Name: [%s]", UUID.randomUUID().toString()), true);
- person.setName(String.format("Name: [%s]", UUID.randomUUID().toString()), true);
- person.setName(String.format("Name: [%s]", UUID.randomUUID().toString()), true);
-
- act.setHandler(keyidx, person.getNonVolatileHandler());
-
- for (int deep = 0; deep < rand.nextInt(100); ++deep) {
-
- mother = PersonFactory.create(act);
- mother.setAge((short)(50 + rand.nextInt(50)));
- mother.setName(String.format("Name: [%s]", UUID.randomUUID().toString()), true);
-
- person.setMother(mother, true);
-
- person = mother;
-
- }
- ++keyidx;
- }
- }finally {
- act.close();
- }
- }
-
- @Test(dependsOnMethods = {"testGenPeople"})
- public void testCheckPeople() throws RetrieveNonVolatileEntityError {
- BigDataPMemAllocator act = new BigDataPMemAllocator(Utils.getNonVolatileMemoryAllocatorService("pmalloc"), 1024 * 1024 * 8, "./pobj_person.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;
- }
- });
- act.setChunkReclaimer(new Reclaim<Long>() {
- @Override
- public boolean reclaim(Long mres, Long sz) {
- System.out.println(String.format(
- "Reclaim Memory Chunk: %X Size: %s", System
- .identityHashCode(mres),
- null == sz ? "NULL" : sz.toString()));
- return false;
- }
- });
-
- long val;
- for (long i = 0; i < KEYCAPACITY; ++i) {
- System.out.printf("----------Key %d--------------\n", i);
- val = act.getHandler(i);
- if (0L == val) {
- break;
- }
- Person<Integer> person = PersonFactory.restore(act, val, true);
- while (null != person) {
- person.testOutput();
- person = person.getMother();
- }
- }
-
- act.close();
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/test/java/com/intel/bigdatamem/Payload.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/com/intel/bigdatamem/Payload.java b/core/src/test/java/com/intel/bigdatamem/Payload.java
deleted file mode 100644
index 4c5d5b3..0000000
--- a/core/src/test/java/com/intel/bigdatamem/Payload.java
+++ /dev/null
@@ -1,29 +0,0 @@
-
-package com.intel.bigdatamem;
-
-/**
- * a dummy object that is used for other test cases.
- *
- *
- *
- */
-public class Payload implements java.io.Serializable, Comparable<Payload> {
-
- private static final long serialVersionUID = 187397440699436500L;
-
- public Payload(int iv, String strv, double dv) {
- ival = iv;
- strval = strv;
- dval = dv;
- }
-
- public int ival;
- public String strval;
- public double dval;
-
- @Override
- public int compareTo(Payload pl) {
- return ival == pl.ival && strval.equals(pl.strval) && dval == pl.dval ? 0
- : 1;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/test/java/com/intel/bigdatamem/Person.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/com/intel/bigdatamem/Person.java b/core/src/test/java/com/intel/bigdatamem/Person.java
deleted file mode 100644
index 86919af..0000000
--- a/core/src/test/java/com/intel/bigdatamem/Person.java
+++ /dev/null
@@ -1,61 +0,0 @@
-package com.intel.bigdatamem;
-
-import java.util.List;
-
-/**
- *
- *
- */
-
-@NonVolatileEntity
-public abstract class Person<E> implements Durable, Comparable<Person<E>> {
- E element;
-
- @Override
- public void initializeAfterCreate() {
- System.out.println("Initializing After Created");
- }
-
- @Override
- public void initializeAfterRestore() {
- System.out.println("Initializing After Restored");
- }
-
- @Override
- public void setupGenericInfo(EntityFactoryProxy[] efproxies, GenericField.GType[] gftypes) {
-
- }
-
- public void testOutput() throws RetrieveNonVolatileEntityError {
- System.out.printf("Person %s, Age: %d ( %s ) \n", getName(), getAge(),
- null == getMother()? "No Recorded Mother" : "Has Recorded Mother");
- }
-
- public int compareTo(Person<E> anotherPerson) {
- int ret = 0;
- if (0 == ret) ret = getAge().compareTo(anotherPerson.getAge());
- if (0 == ret) ret = getName().compareTo(anotherPerson.getName());
- return ret;
- }
-
- @NonVolatileGetter
- abstract public Short getAge();
- @NonVolatileSetter
- abstract public void setAge(Short age);
-
- @NonVolatileGetter
- abstract public String getName() throws RetrieveNonVolatileEntityError;
- @NonVolatileSetter
- abstract public void setName(String name, boolean destroy) throws OutOfPersistentMemory, RetrieveNonVolatileEntityError;
-
- @NonVolatileGetter
- abstract public Person<E> getMother() throws RetrieveNonVolatileEntityError;
- @NonVolatileSetter
- abstract public void setMother(Person<E> mother, boolean destroy) throws RetrieveNonVolatileEntityError;
-
- @NonVolatileGetter
- abstract public Person<E> getFather() throws RetrieveNonVolatileEntityError;
- @NonVolatileSetter
- abstract public void setFather(Person<E> mother, boolean destroy) throws RetrieveNonVolatileEntityError;
-}
-
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/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
new file mode 100644
index 0000000..6497879
--- /dev/null
+++ b/core/src/test/java/org/apache/mnemonic/BigDataMemAllocatorNGTest.java
@@ -0,0 +1,58 @@
+
+package org.apache.mnemonic;
+
+import org.apache.mnemonic.Allocator;
+import org.apache.mnemonic.BigDataMemAllocator;
+import org.apache.mnemonic.MemBufferHolder;
+import org.apache.mnemonic.MemChunkHolder;
+import org.apache.mnemonic.Utils;
+import org.testng.annotations.Test;
+
+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 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/8e83efe5/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
new file mode 100644
index 0000000..b515ef0
--- /dev/null
+++ b/core/src/test/java/org/apache/mnemonic/BigDataPMemAllocatorNGTest.java
@@ -0,0 +1,124 @@
+package org.apache.mnemonic;
+
+import java.nio.ByteBuffer;
+import java.util.Random;
+
+import org.apache.mnemonic.Utils;
+import org.testng.Assert;
+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();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/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
new file mode 100644
index 0000000..6150efe
--- /dev/null
+++ b/core/src/test/java/org/apache/mnemonic/ByteBufferSerializerNGTest.java
@@ -0,0 +1,65 @@
+
+package org.apache.mnemonic;
+
+import static org.testng.Assert.*;
+
+import org.apache.mnemonic.BigDataMemAllocator;
+import org.apache.mnemonic.ByteBufferSerializer;
+import org.apache.mnemonic.MemBufferHolder;
+import org.apache.mnemonic.Utils;
+import org.testng.annotations.Test;
+
+import java.nio.ByteBuffer;
+import java.util.Random;
+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 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);
+ }
+
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/test/java/org/apache/mnemonic/MemBufferHolderCachePoolNGTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/mnemonic/MemBufferHolderCachePoolNGTest.java b/core/src/test/java/org/apache/mnemonic/MemBufferHolderCachePoolNGTest.java
new file mode 100644
index 0000000..b58c3c8
--- /dev/null
+++ b/core/src/test/java/org/apache/mnemonic/MemBufferHolderCachePoolNGTest.java
@@ -0,0 +1,150 @@
+package org.apache.mnemonic;
+
+import static org.testng.Assert.*;
+
+import org.apache.mnemonic.BigDataMemAllocator;
+import org.apache.mnemonic.CachePool;
+import org.apache.mnemonic.ContainerOverflowException;
+import org.apache.mnemonic.DropEvent;
+import org.apache.mnemonic.EvictFilter;
+import org.apache.mnemonic.MemBufferHolder;
+import org.apache.mnemonic.MemBufferHolderCachePool;
+import org.apache.mnemonic.MemClustering;
+import org.apache.mnemonic.Utils;
+import org.testng.annotations.Test;
+
+import java.util.Random;
+
+/**
+ * test the functionalities of MemBufferHolderCachePool class
+ *
+ * @author Wang, Gang(Gary) {@literal <ga...@intel.com>}
+ */
+public class MemBufferHolderCachePoolNGTest {
+
+ /**
+ * test to aggressively allow any MemBufferHolder objects in pool to be able
+ * to drop at will that prevents this cache pool from overflowing.
+ */
+ @Test
+ public void testMemBufferHolderCachePoolWithoutOverflow() {
+
+ MemBufferHolderCachePool<Integer> mbhcpool = new MemBufferHolderCachePool<Integer>(
+ 1024 * 1024 * 10);
+ Random randomGenerator = new Random();
+ MemClustering.NodeConfig<?> ncs[] = new MemClustering.NodeConfig<?>[] {
+ new MemClustering.NodeConfig<BigDataMemAllocator>(
+ new BigDataMemAllocator(Utils.getVolatileMemoryAllocatorService("vmem"), 1024 * 1024 * 200, ".", true)
+ .disableActiveGC(),
+ MemClustering.PerformanceLevel.FAST),
+ // new MemClustering.NodeConfig(new BigMemAllocator(1024*1024*20, ".",
+ // true).disableActiveGC(), MemClustering.PerformanceLevel.NORMAL),
+ // new MemClustering.NodeConfig(new BigMemAllocator(1024*1024*20, ".",
+ // true).disableActiveGC(), MemClustering.PerformanceLevel.SLOW),
+ };
+ MemClustering mclst = new MemClustering(ncs);
+ MemBufferHolder<?> mbh;
+
+ DropEvent<Integer, MemBufferHolder<?>> dropevt =
+ new DropEvent<Integer, MemBufferHolder<?>>() {
+ @Override
+ public void drop(CachePool<Integer, MemBufferHolder<?>> pool,
+ Integer k, MemBufferHolder<?> v) {
+ System.out.println(String.format("dropping idx: %d", k));
+ v.destroy();
+ }
+ };
+
+ EvictFilter<Integer, MemBufferHolder<?>> dfilter = new EvictFilter<Integer, MemBufferHolder<?>>() {
+ @Override
+ public boolean validate(CachePool<Integer, MemBufferHolder<?>> pool,
+ Integer k, MemBufferHolder<?> v) {
+ System.out.println(String.format("validating idx: %d", k));
+ return true;
+ }
+ };
+
+ for (int idx = 1; idx <= 100; ++idx) {
+ int size = randomGenerator.nextInt(1024 * 1024) + 1024 * 1024;
+ mbh = mclst.createBuffer(size);
+ System.out.println(String.format(
+ "\nallocating idx: %d - size: %d, FreeCap: %d", idx, size,
+ mbhcpool.freeCapacity()));
+ assertNotNull(mbh);
+ assertNotNull(mbh.get());
+ for (int i = 0; i < size; i++) {
+ mbh.get().put((byte) randomGenerator.nextInt(255));
+ }
+
+ mbh.get().flip();
+
+ assertEquals(size, mbh.get().capacity());
+
+ mbhcpool.put(idx, mbh, dropevt, dfilter);
+
+ }
+ }
+
+ /**
+ * test to overflow a cache pool of MemBufferHolder objects that is caused
+ * by preventing any objected in pool from dropping.
+ */
+ @Test(expectedExceptions = ContainerOverflowException.class)
+ public void testMemBufferHolderCachePoolWithOverflow() {
+
+ MemBufferHolderCachePool<Integer> mbhcpool = new MemBufferHolderCachePool<Integer>(
+ 1024 * 1024 * 10);
+ Random randomGenerator = new Random();
+ MemClustering.NodeConfig<?> ncs[] = new MemClustering.NodeConfig<?>[] {
+ new MemClustering.NodeConfig<BigDataMemAllocator>(
+ new BigDataMemAllocator(Utils.getVolatileMemoryAllocatorService("vmem"), 1024 * 1024 * 200, ".", true)
+ .disableActiveGC(),
+ MemClustering.PerformanceLevel.FAST),
+ // new MemClustering.NodeConfig(new BigMemAllocator(1024*1024*20, ".",
+ // true).disableActiveGC(), MemClustering.PerformanceLevel.NORMAL),
+ // new MemClustering.NodeConfig(new BigMemAllocator(1024*1024*20, ".",
+ // true).disableActiveGC(), MemClustering.PerformanceLevel.SLOW),
+ };
+ MemClustering mclst = new MemClustering(ncs);
+ MemBufferHolder<?> mbh;
+
+ DropEvent<Integer, MemBufferHolder<?>> dropevt = new DropEvent<Integer, MemBufferHolder<?>>() {
+ @Override
+ public void drop(CachePool<Integer, MemBufferHolder<?>> pool,
+ Integer k, MemBufferHolder<?> v) {
+ System.out.println(String.format("dropping idx: %d", k));
+ v.destroy();
+ }
+ };
+
+ EvictFilter<Integer, MemBufferHolder<?>> dfilter = new EvictFilter<Integer, MemBufferHolder<?>>() {
+ @Override
+ public boolean validate(CachePool<Integer, MemBufferHolder<?>> pool,
+ Integer k, MemBufferHolder<?> v) {
+ System.out.println(String.format("validating idx: %d", k));
+ return false;
+ }
+ };
+
+ for (int idx = 1; idx <= 100; ++idx) {
+ int size = randomGenerator.nextInt(1024 * 1024) + 1024 * 1024;
+ mbh = mclst.createBuffer(size);
+ System.out.println(String.format(
+ "\nallocating idx: %d - size: %d, FreeCap: %d", idx, size,
+ mbhcpool.freeCapacity()));
+ assertNotNull(mbh);
+ assertNotNull(mbh.get());
+ for (int i = 0; i < size; i++) {
+ mbh.get().put((byte) randomGenerator.nextInt(255));
+ }
+
+ mbh.get().flip();
+
+ assertEquals(size, mbh.get().capacity());
+
+ mbhcpool.put(idx, mbh, dropevt, dfilter);
+
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/test/java/org/apache/mnemonic/MemClusteringNGTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/mnemonic/MemClusteringNGTest.java b/core/src/test/java/org/apache/mnemonic/MemClusteringNGTest.java
new file mode 100644
index 0000000..9e88979
--- /dev/null
+++ b/core/src/test/java/org/apache/mnemonic/MemClusteringNGTest.java
@@ -0,0 +1,189 @@
+
+package org.apache.mnemonic;
+
+import static org.testng.Assert.*;
+
+import org.apache.mnemonic.BigDataMemAllocator;
+import org.apache.mnemonic.CommonAllocator;
+import org.apache.mnemonic.MemBufferHolder;
+import org.apache.mnemonic.MemClustering;
+import org.apache.mnemonic.SysMemAllocator;
+import org.apache.mnemonic.Utils;
+import org.testng.annotations.Test;
+
+import java.util.Random;
+
+/**
+ * test the functionalities of MemClustering class
+ *
+ * @author Wang, Gang(Gary) {@literal <ga...@intel.com>}
+ */
+public class MemClusteringNGTest {
+
+ /**
+ * test to fill up memory pool without reclaim unused memory blocks that
+ * will cause additional operations of allocation will be failed.
+ *
+ * @throws Exception
+ */
+ @Test(expectedExceptions = NullPointerException.class)
+ public void testMemByteBufferWithoutActiveGC() throws Exception {
+ Random randomGenerator = new Random();
+ MemClustering.NodeConfig<?> ncs[] = new MemClustering.NodeConfig<?>[] {
+ new MemClustering.NodeConfig<SysMemAllocator>(new SysMemAllocator(
+ 1024 * 1024 * 20, true).disableActiveGC(),
+ MemClustering.PerformanceLevel.FASTEST),
+ new MemClustering.NodeConfig<BigDataMemAllocator>(new BigDataMemAllocator(Utils.getVolatileMemoryAllocatorService("vmem"),
+ 1024 * 1024 * 20, ".", true).disableActiveGC(),
+ MemClustering.PerformanceLevel.FAST),
+ // new MemClustering.NodeConfig(new
+ // BigMemAllocator(1024*1024*20, ".", true).disableActiveGC(),
+ // MemClustering.PerformanceLevel.NORMAL),
+ new MemClustering.NodeConfig<BigDataMemAllocator>(new BigDataMemAllocator(Utils.getVolatileMemoryAllocatorService("vmem"),
+ 1024 * 1024 * 20, ".", true).disableActiveGC(),
+ MemClustering.PerformanceLevel.SLOW), };
+ MemClustering mclst = new MemClustering(ncs);
+ MemBufferHolder<?> mbh;
+ for (int idx = 1; idx <= 200; ++idx) {
+ int size = randomGenerator.nextInt(1024 * 1024) + 1024 * 1024;
+ mbh = mclst.createBuffer(size);
+ for (int i = 0; i < size; i++) {
+ mbh.get().put((byte) randomGenerator.nextInt(255));
+ }
+ assertEquals(size, mbh.get().capacity());
+ }
+ }
+
+ /**
+ * test to try to fill up memory pool and reclaim unused memory block as
+ * required.
+ */
+ @Test
+ public void testMemByteBufferWithActiveGC() {
+ Random randomGenerator = new Random();
+ MemClustering.NodeConfig<?> ncs[] = new MemClustering.NodeConfig<?>[] { new MemClustering.NodeConfig<BigDataMemAllocator>(
+ new BigDataMemAllocator(Utils.getVolatileMemoryAllocatorService("vmem"), 1024 * 1024 * 20, ".", true),
+ MemClustering.PerformanceLevel.NORMAL),
+ // new MemClustering.NodeConfig(new BigMemAllocator(1024*1024*20, ".",
+ // true), MemClustering.PerformanceLevel.SLOW),
+ };
+ MemClustering mclst = new MemClustering(ncs);
+ MemBufferHolder<?> mbh;
+ for (int idx = 1; idx <= 200; ++idx) {
+ int size = randomGenerator.nextInt(1024 * 1024) + 1024 * 1024;
+ mbh = mclst.createBuffer(size);
+ for (int i = 0; i < size; i++) {
+ mbh.get().put((byte) randomGenerator.nextInt(255));
+ }
+ assertEquals(size, mbh.get().capacity());
+ }
+ }
+
+ /**
+ * test to manually release memory resource once that is intended to be
+ * unused.
+ */
+ @Test
+ public void testMemByteBufferManualRelease() {
+ Random randomGenerator = new Random();
+ MemClustering.NodeConfig<?> ncs[] = new MemClustering.NodeConfig<?>[] {
+ new MemClustering.NodeConfig<BigDataMemAllocator>(new BigDataMemAllocator(
+ Utils.getVolatileMemoryAllocatorService("vmem"),
+ 1024 * 1024 * 20, ".", true).disableActiveGC(),
+ MemClustering.PerformanceLevel.FAST),
+ // new MemClustering.NodeConfig(new
+ // BigMemAllocator(1024*1024*20, ".", true).disableActiveGC(),
+ // MemClustering.PerformanceLevel.NORMAL),
+ new MemClustering.NodeConfig<BigDataMemAllocator>(new BigDataMemAllocator(
+ Utils.getVolatileMemoryAllocatorService("vmem"),
+ 1024 * 1024 * 20, ".", true),
+ MemClustering.PerformanceLevel.SLOW), };
+ MemClustering mclst = new MemClustering(ncs);
+ MemBufferHolder<?> mbh;
+ for (int idx = 1; idx <= 200; ++idx) {
+ int size = randomGenerator.nextInt(1024 * 1024) + 1024 * 1024;
+ mbh = mclst.createBuffer(size);
+ for (int i = 0; i < size; i++) {
+ mbh.get().put((byte) randomGenerator.nextInt(255));
+ }
+ assertEquals(size, mbh.get().capacity());
+ // System.out.println("testMemByteBufferManualRelease");
+ mbh.destroy();
+ }
+ }
+
+ public boolean actriggered = false, pctriggered = false;
+
+ /**
+ * test change events that will be triggered by memory pool downgrading or
+ * memory pool switching caused by fill up.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testMemByteBufferWithChange() throws Exception {
+ Random randomGenerator = new Random();
+ MemClustering.NodeConfig<?> ncs[] = new MemClustering.NodeConfig<?>[] {
+ new MemClustering.NodeConfig<SysMemAllocator>(new SysMemAllocator(
+ 1024 * 1024 * 20, true).disableActiveGC(),
+ MemClustering.PerformanceLevel.FASTEST),
+ new MemClustering.NodeConfig<BigDataMemAllocator>(new BigDataMemAllocator(
+ Utils.getVolatileMemoryAllocatorService("vmem"),
+ 1024 * 1024 * 20, ".", true).disableActiveGC(),
+ MemClustering.PerformanceLevel.FAST),
+ // new MemClustering.NodeConfig(new
+ // BigMemAllocator(1024*1024*20, ".", true).disableActiveGC(),
+ // MemClustering.PerformanceLevel.NORMAL),
+ new MemClustering.NodeConfig<BigDataMemAllocator>(new BigDataMemAllocator(
+ Utils.getVolatileMemoryAllocatorService("vmem"),
+ 1024 * 1024 * 20, ".", true),
+ MemClustering.PerformanceLevel.SLOW), };
+ MemClustering mclst = new MemClustering(ncs){};
+
+ actriggered = false;
+ pctriggered = false;
+ mclst.setAllocatorChange(new MemClustering.AllocatorChange() {
+ @Override
+ public void changed(MemClustering.PerformanceLevel lvl,
+ CommonAllocator<?> prevallocator, CommonAllocator<?> tgtallocator) {
+ System.out.println(String.format(
+ "Allocator Changed: %s, %X -> %X", lvl.name(),
+ System.identityHashCode(prevallocator),
+ System.identityHashCode(tgtallocator)));
+ actriggered = true;
+ }
+ });
+ mclst.setPerformanceLevelChange(new MemClustering.PerformanceLevelChange() {
+ @Override
+ public void changed(MemClustering.PerformanceLevel prevlvl,
+ MemClustering.PerformanceLevel lvl) {
+ System.out.println(String.format(
+ "Perf.Level Changed: %s -> %s",
+ null == prevlvl ? "NULL" : prevlvl.name(), lvl.name()));
+ pctriggered = true;
+ }
+ });
+
+ MemBufferHolder<?> mbh;
+ for (int idx = 1; idx <= 100; ++idx) {
+ int size = randomGenerator.nextInt(1024 * 1024) + 1024 * 1024;
+ mbh = mclst.createBuffer(size);
+ for (int i = 0; i < size; i++) {
+ mbh.get().put((byte) randomGenerator.nextInt(255));
+ }
+ //mbh.destroy();
+ }
+ assertTrue(actriggered && pctriggered);
+
+ }
+
+ /*
+ * @Test public void testMemChunk() { Random randomGenerator = new Random();
+ * Allocator act = new BigMemAllocator(1024*1024*1024, "/home/wg/bm", true);
+ * MemChunkHolder mch; for (int idx = 1; idx <= 50000; ++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(); } }
+ */
+}
[5/7] incubator-mnemonic git commit: MNEMONIC-5 MNEMONIC-6 #resolve
refactored package names
Posted by ga...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/BigDataMemAllocator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/BigDataMemAllocator.java b/core/src/main/java/com/intel/bigdatamem/BigDataMemAllocator.java
deleted file mode 100644
index ccf05fa..0000000
--- a/core/src/main/java/com/intel/bigdatamem/BigDataMemAllocator.java
+++ /dev/null
@@ -1,278 +0,0 @@
-package com.intel.bigdatamem;
-
-import java.nio.ByteBuffer;
-
-import org.flowcomputing.commons.resgc.*;
-import org.flowcomputing.commons.primitives.*;
-import com.intel.mnemonic.service.allocatorservice.VolatileMemoryAllocatorService;
-
-/**
- * manage a big native memory pool through libvmem.so that is provied by Intel nvml library.
- *
- *
- */
-public class BigDataMemAllocator extends CommonAllocator<BigDataMemAllocator> {
-
- private boolean m_activegc = true;
- private long m_gctimeout = 100;
- private long m_nid = -1;
- private VolatileMemoryAllocatorService m_vmasvc = null;
-
- /**
- * Constructor, it initializes and allocate a memory pool from specified uri
- * location with specified capacity and an allocator service instance.
- * usually, the uri points to a mounted
- * memory device or a location of file system.
- *
- * @param vmasvc
- * the volatile memory allocation service instance
- *
- * @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
- */
- public BigDataMemAllocator(VolatileMemoryAllocatorService vmasvc, long capacity, String uri, boolean isnew) {
- assert null != vmasvc : "VolatileMemoryAllocatorService object is null";
- if (capacity <= 0) {
- throw new IllegalArgumentException("BigDataMemAllocator cannot be initialized with capacity <= 0.");
- }
-
- m_vmasvc = vmasvc;
- m_nid = m_vmasvc.init(capacity, uri, isnew);
-
- /**
- * create a resource collector to release specified bytebuffer that backed
- * by underlying big memory pool.
- */
- m_bufcollector = new ResCollector<MemBufferHolder<BigDataMemAllocator>, ByteBuffer>(
- new ResReclaim<ByteBuffer>() {
- @Override
- public void reclaim(ByteBuffer mres) {
- boolean cb_reclaimed = false;
- if (null != m_bufferreclaimer) {
- cb_reclaimed = m_bufferreclaimer.reclaim(mres, Long.valueOf(mres.capacity()));
- }
- if (!cb_reclaimed) {
- m_vmasvc.destroyByteBuffer(m_nid, mres);
- mres = null;
- }
- }
- });
-
- /**
- * create a resource collector to release specified chunk that backed by
- * underlying big memory pool.
- */
- m_chunkcollector = new ResCollector<MemChunkHolder<BigDataMemAllocator>, Long>(
- new ResReclaim<Long>() {
- @Override
- public void reclaim(Long mres) {
- // System.out.println(String.format("Reclaim: %X", mres));
- boolean cb_reclaimed = false;
- if (null != m_chunkreclaimer) {
- cb_reclaimed = m_chunkreclaimer.reclaim(mres, null);
- }
- if (!cb_reclaimed) {
- m_vmasvc.free(m_nid, mres);
- mres = null;
- }
- }
- });
- }
-
- /**
- * 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 BigDataMemAllocator enableActiveGC(long timeout) {
- m_activegc = true;
- m_gctimeout = timeout;
- return this;
- }
-
- /**
- * disable active garbage collection.
- *
- * @return this allocator
- */
- @Override
- public BigDataMemAllocator 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<BigDataMemAllocator> resizeChunk(MemChunkHolder<BigDataMemAllocator> mholder, long size){
- MemChunkHolder<BigDataMemAllocator> ret = null;
- boolean ac = null != mholder.getRefId();
- if (size > 0) {
- Long addr = m_vmasvc.reallocate(m_nid, mholder.get(), size, true);
- if (0 == addr && m_activegc) {
- forceGC();
- addr = m_vmasvc.reallocate(m_nid, mholder.get(), size, true);
- }
- if (0 != addr) {
- mholder.clear();
- mholder.destroy();
- ret = new MemChunkHolder<BigDataMemAllocator>(this, addr, size);
- if (ac) {
- m_chunkcollector.register(ret);
- }
- }
- }
- return ret;
- }
-
- /**
- * 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<BigDataMemAllocator> resizeBuffer(MemBufferHolder<BigDataMemAllocator> mholder, long size) {
- MemBufferHolder<BigDataMemAllocator> ret = null;
- boolean ac = null != mholder.getRefId();
- if (size > 0) {
- int bufpos = mholder.get().position();
- int buflimit = mholder.get().limit();
- ByteBuffer buf = m_vmasvc.resizeByteBuffer(m_nid, mholder.get(), size);
- if (null == buf && m_activegc) {
- forceGC();
- buf = m_vmasvc.resizeByteBuffer(m_nid, 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<BigDataMemAllocator>(this, buf);
- if (ac) {
- m_bufcollector.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<BigDataMemAllocator> createChunk(long size, boolean autoreclaim) {
- MemChunkHolder<BigDataMemAllocator> ret = null;
- Long addr = m_vmasvc.allocate(m_nid, size, true);
- if (0 == addr && m_activegc) {
- forceGC();
- addr = m_vmasvc.allocate(m_nid, size, true);
- }
- if (0 != addr) {
- ret = new MemChunkHolder<BigDataMemAllocator>(this, addr, size);
- ret.setCollector(m_chunkcollector);
- if (autoreclaim) {
- m_chunkcollector.register(ret);
- }
- }
- return ret;
- }
-
- /**
- * create a memory buffer that is managed by its holder.
- *
- * @param size
- * specify the size of memory buffer
- *
- * @param autoreclaim
- * specify whether or not to reclaim this
- * buffer automatically
- *
- * @return a holder contains a memory buffer
- */
- @Override
- public MemBufferHolder<BigDataMemAllocator> createBuffer(long size, boolean autoreclaim) {
- MemBufferHolder<BigDataMemAllocator> ret = null;
- ByteBuffer bb = m_vmasvc.createByteBuffer(m_nid, size);
- if (null == bb && m_activegc) {
- forceGC();
- bb = m_vmasvc.createByteBuffer(m_nid, size);
- }
- if (null != bb) {
- ret = new MemBufferHolder<BigDataMemAllocator>(this, bb);
- ret.setCollector(m_bufcollector);
- if (autoreclaim) {
- m_bufcollector.register(ret);
- }
- }
- return ret;
- }
-
- /**
- * force to perform GC that is used to release unused backed memory
- * resources.
- */
- private void forceGC() {
- System.gc();
- try {
- Thread.sleep(m_gctimeout);
- } catch (Exception ex) {
- }
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/BigDataPMemAllocator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/BigDataPMemAllocator.java b/core/src/main/java/com/intel/bigdatamem/BigDataPMemAllocator.java
deleted file mode 100644
index d7cf6a3..0000000
--- a/core/src/main/java/com/intel/bigdatamem/BigDataPMemAllocator.java
+++ /dev/null
@@ -1,469 +0,0 @@
-package com.intel.bigdatamem;
-
-import java.nio.ByteBuffer;
-
-import org.flowcomputing.commons.resgc.*;
-import org.flowcomputing.commons.primitives.*;
-import com.intel.mnemonic.service.allocatorservice.NonVolatileMemoryAllocatorService;
-
-/**
- * manage a big native persistent memory pool through libpmalloc.so provided by pmalloc project.
- *
- *
- */
-public class BigDataPMemAllocator extends CommonPersistAllocator<BigDataPMemAllocator> implements PMAddressTranslator{
-
- private boolean m_activegc = true;
- private long m_gctimeout = 100;
- private long m_nid = -1;
- private long b_addr = 0;
- private NonVolatileMemoryAllocatorService m_nvmasvc = null;
-
- /**
- * Constructor, it initializes and allocate a memory pool from specified uri
- * location with specified capacity and an allocator service instance.
- * usually, the uri points to a mounted
- * non-volatile memory device or a location of file system.
- *
- * @param nvmasvc
- * the non-volatile memory allocation service instance
- *
- * @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
- */
- public BigDataPMemAllocator(NonVolatileMemoryAllocatorService nvmasvc, long capacity, String uri, boolean isnew) {
- assert null != nvmasvc : "NonVolatileMemoryAllocatorService object is null";
- if (capacity <= 0) {
- throw new IllegalArgumentException("BigDataPMemAllocator cannot be initialized with capacity <= 0.");
- }
-
- m_nvmasvc = nvmasvc;
-
- m_nid = m_nvmasvc.init(capacity, uri, isnew);
- b_addr = m_nvmasvc.getBaseAddress(m_nid);
-
- /**
- * create a resource collector to release specified chunk that backed by
- * underlying big memory pool.
- */
- m_chunkcollector = new ResCollector<MemChunkHolder<BigDataPMemAllocator>, Long>(new ResReclaim<Long>() {
- @Override
- public void reclaim(Long mres) {
- // System.out.println(String.format("Reclaim: %X", mres));
- boolean cb_reclaimed = false;
- if (null != m_chunkreclaimer) {
- cb_reclaimed = m_chunkreclaimer.reclaim(mres, null);
- }
- if (!cb_reclaimed) {
- m_nvmasvc.free(m_nid, mres);
- mres = null;
- }
- }
- });
-
- /**
- * create a resource collector to release specified bytebuffer that backed
- * by underlying big memory pool.
- */
- m_bufcollector = new ResCollector<MemBufferHolder<BigDataPMemAllocator>, ByteBuffer>(
- new ResReclaim<ByteBuffer>() {
- @Override
- public void reclaim(ByteBuffer mres) {
- boolean cb_reclaimed = false;
- if (null != m_bufferreclaimer) {
- cb_reclaimed = m_bufferreclaimer.reclaim(mres, Long.valueOf(mres.capacity()));
- }
- if (!cb_reclaimed) {
- m_nvmasvc.destroyByteBuffer(m_nid, mres);
- mres = null;
- }
- }
- });
- }
-
- /**
- * 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
- */
- @Override
- public BigDataPMemAllocator enableActiveGC(long timeout) {
- m_activegc = true;
- m_gctimeout = timeout;
- return this;
- }
-
- /**
- * disable active garbage collection.
- *
- */
- @Override
- public BigDataPMemAllocator disableActiveGC() {
- m_activegc = false;
- return this;
- }
-
- /**
- * Release the memory pool and close it.
- *
- */
- @Override
- public void close() {
- forceGC();
- super.close();
- m_nvmasvc.close(m_nid);
- }
-
- /**
- * 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<BigDataPMemAllocator> resizeChunk(MemChunkHolder<BigDataPMemAllocator> mholder, long size){
- MemChunkHolder<BigDataPMemAllocator> ret = null;
- boolean ac = null != mholder.getRefId();
- if (size > 0) {
- Long addr = m_nvmasvc.reallocate(m_nid, mholder.get(), size, true);
- if (0 == addr && m_activegc) {
- forceGC();
- addr = m_nvmasvc.reallocate(m_nid, mholder.get(), size, true);
- }
- if (0 != addr) {
- mholder.clear();
- mholder.destroy();
- ret = new MemChunkHolder<BigDataPMemAllocator>(this, addr, size);
- if (ac) {
- m_chunkcollector.register(ret);
- }
- }
- }
- return ret;
- }
-
- /**
- * 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<BigDataPMemAllocator> resizeBuffer(MemBufferHolder<BigDataPMemAllocator> mholder, long size) {
- MemBufferHolder<BigDataPMemAllocator> ret = null;
- boolean ac = null != mholder.getRefId();
- if (size > 0) {
- int bufpos = mholder.get().position();
- int buflimit = mholder.get().limit();
- ByteBuffer buf = m_nvmasvc.resizeByteBuffer(m_nid, mholder.get(), size);
- if (null == buf && m_activegc) {
- forceGC();
- buf = m_nvmasvc.resizeByteBuffer(m_nid, 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<BigDataPMemAllocator>(this, buf);
- if (ac) {
- m_bufcollector.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<BigDataPMemAllocator> createChunk(long size, boolean autoreclaim) {
- MemChunkHolder<BigDataPMemAllocator> ret = null;
- Long addr = m_nvmasvc.allocate(m_nid, size, true);
- if ((null == addr || 0 == addr) && m_activegc) {
- forceGC();
- addr = m_nvmasvc.allocate(m_nid, size, true);
- }
- if (null != addr && 0 != addr) {
- ret = new MemChunkHolder<BigDataPMemAllocator>(this, addr, size);
- ret.setCollector(m_chunkcollector);
- if (autoreclaim) {
- m_chunkcollector.register(ret);
- }
- }
- return ret;
- }
-
- /**
- * create a memory buffer that is managed by its holder.
- *
- * @param size
- * specify the size of memory buffer
- *
- * @param autoreclaim
- * specify whether or not to reclaim this
- * buffer automatically
- *
- * @return a holder contains a memory buffer
- */
- @Override
- public MemBufferHolder<BigDataPMemAllocator> createBuffer(long size, boolean autoreclaim) {
- MemBufferHolder<BigDataPMemAllocator> ret = null;
- ByteBuffer bb = m_nvmasvc.createByteBuffer(m_nid, size);
- if (null == bb && m_activegc) {
- forceGC();
- bb = m_nvmasvc.createByteBuffer(m_nid, size);
- }
- if (null != bb) {
- ret = new MemBufferHolder<BigDataPMemAllocator>(this, bb);
- ret.setCollector(m_bufcollector);
- if (autoreclaim) {
- m_bufcollector.register(ret);
- }
- }
- return ret;
- }
-
- /**
- * retrieve a memory buffer from its backed memory allocator.
- *
- * @param phandler
- * specify the handler of memory buffer to retrieve
- *
- * @param autoreclaim
- * specify whether this retrieved memory buffer can be reclaimed automatically or not
- *
- * @return a holder contains the retrieved memory buffer
- */
- @Override
- public MemBufferHolder<BigDataPMemAllocator> retrieveBuffer(long phandler, boolean autoreclaim) {
- MemBufferHolder<BigDataPMemAllocator> ret = null;
- ByteBuffer bb = m_nvmasvc.retrieveByteBuffer(m_nid, getEffectiveAddress(phandler));
- if (null != bb) {
- ret = new MemBufferHolder<BigDataPMemAllocator>(this, bb);
- if (autoreclaim) {
- m_bufcollector.register(ret);
- }
- }
- return ret;
- }
-
- /**
- * retrieve a memory chunk from its backed memory allocator.
- *
- * @param phandler
- * specify the handler of memory chunk to retrieve
- *
- * @param autoreclaim
- * specify whether this retrieved memory chunk can be reclaimed automatically or not
- *
- * @return a holder contains the retrieved memory chunk
- */
- @Override
- public MemChunkHolder<BigDataPMemAllocator> retrieveChunk(long phandler, boolean autoreclaim) {
- MemChunkHolder<BigDataPMemAllocator> ret = null;
- long eaddr = getEffectiveAddress(phandler);
- long sz = m_nvmasvc.retrieveSize(m_nid, eaddr);
- if (sz > 0L) {
- ret = new MemChunkHolder<BigDataPMemAllocator>(this, eaddr, sz);
- if (autoreclaim) {
- m_chunkcollector.register(ret);
- }
- }
- return ret;
- }
-
- /**
- * get the handler from a memory buffer holder.
- *
- * @param mbuf
- * specify the memory buffer holder
- *
- * @return a handler that could be used to retrieve its memory buffer
- */
- @Override
- public long getBufferHandler(MemBufferHolder<BigDataPMemAllocator> mbuf) {
- return getPortableAddress(m_nvmasvc.getByteBufferHandler(m_nid, mbuf.get()));
- }
-
- /**
- * get the handler from a memory chunk holder.
- *
- * @param mchunk
- * specify the memory chunk holder
- *
- * @return a handler that could be used to retrieve its memory chunk
- */
- @Override
- public long getChunkHandler(MemChunkHolder<BigDataPMemAllocator> mchunk) {
- return getPortableAddress(mchunk.get());
- }
-
- /**
- * determine whether this allocator supports to store non-volatile handler or not.
- * (it is a placeholder)
- *
- * @return true if there is
- */
- @Override
- public boolean hasNonVolatileHandlerStore() {
- return true;
- }
-
- /**
- * start a application level transaction on this allocator.
- * (it is a place holder)
- *
- */
- @Override
- public void beginTransaction() {
- throw new UnsupportedOperationException("Transaction Unsupported.");
- }
-
- /**
- * end a application level transaction on this allocator.
- * (it is a place holder)
- *
- */
- @Override
- public void endTransaction() {
- throw new UnsupportedOperationException("Transaction Unsupported.");
- }
-
- /**
- * set a handler on key.
- *
- * @param key
- * the key to set its value
- *
- * @param handler
- * the handler
- */
- public void setHandler(long key, long handler) {
- m_nvmasvc.setHandler(m_nid, key, handler);
- }
-
- /**
- * get a handler value.
- *
- * @param key
- * the key to set its value
- *
- * @return the value of handler
- */
- public long getHandler(long key) {
- return m_nvmasvc.getHandler(m_nid, key);
- }
-
- /**
- * return the capacity of non-volatile handler store.
- *
- * @return the capacity of handler store
- *
- */
- public long handlerCapacity() {
- return m_nvmasvc.handlerCapacity(m_nid);
- }
-
- /**
- * force to perform GC that is used to re-claim garbages objects
- * as well as memory resources managed by this allocator.
- *
- */
- private void forceGC() {
- System.gc();
- try {
- Thread.sleep(m_gctimeout);
- } catch (Exception ex) {
- }
- }
-
- /**
- * calculate the portable address
- *
- * @param addr
- * the address to be calculated
- *
- * @return the portable address
- */
- @Override
- public long getPortableAddress(long addr) {
- return addr - b_addr;
- }
-
- /**
- * calculate the effective address
- *
- * @param addr
- * the address to be calculated
- *
- * @return the effective address
- */
- @Override
- public long getEffectiveAddress(long addr) {
- return addr + b_addr;
- }
-
- /**
- * get the base address
- *
- * @return the base address
- */
- @Override
- public long getBaseAddress() {
- return b_addr;
- }
-
- /**
- * set the base address for calculation
- *
- * @param addr
- * the base address
- *
- */
- @Override
- public long setBaseAddress(long addr) {
- return b_addr = addr;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/ByteBufferBackedInputStream.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/ByteBufferBackedInputStream.java b/core/src/main/java/com/intel/bigdatamem/ByteBufferBackedInputStream.java
deleted file mode 100644
index bacf532..0000000
--- a/core/src/main/java/com/intel/bigdatamem/ByteBufferBackedInputStream.java
+++ /dev/null
@@ -1,61 +0,0 @@
-package com.intel.bigdatamem;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.nio.ByteBuffer;
-
-/**
- * an input Stream that is backed by a in-memory ByteBuffer.
- *
- *
- */
-public class ByteBufferBackedInputStream extends InputStream {
-
- private ByteBuffer buf;
-
- /**
- * accept a ByteBuffer as backed object for inputStream.
- *
- * @param buf
- * specify a bytebuffer that is where any data is from
- */
- public ByteBufferBackedInputStream(ByteBuffer buf) {
- this.buf = buf;
- }
-
- /**
- * read an integer value from backed ByteBuffer.
- *
- * @return a integer value from stream input
- */
- public int read() throws IOException {
- if (!buf.hasRemaining()) {
- return -1;
- }
- return buf.get() & 0xFF;
- }
-
- /**
- * read a specified range of byte array from backed ByteBuffer.
- *
- * @param bytes
- * specify a output byte array to store data
- *
- * @param off
- * specify the offset from ByteBuffer to read
- *
- * @param len
- * specify the length of bytes to read
- *
- * @return the number of bytes has been read
- */
- public int read(byte[] bytes, int off, int len) throws IOException {
- if (!buf.hasRemaining()) {
- return -1;
- }
-
- len = Math.min(len, buf.remaining());
- buf.get(bytes, off, len);
- return len;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/ByteBufferBackedOutputStream.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/ByteBufferBackedOutputStream.java b/core/src/main/java/com/intel/bigdatamem/ByteBufferBackedOutputStream.java
deleted file mode 100644
index 9cd1b98..0000000
--- a/core/src/main/java/com/intel/bigdatamem/ByteBufferBackedOutputStream.java
+++ /dev/null
@@ -1,56 +0,0 @@
-package com.intel.bigdatamem;
-
-import java.io.IOException;
-import java.io.OutputStream;
-import java.nio.ByteBuffer;
-
-/**
- * an output Stream that is backed by a in-memory ByteBuffer.
- *
- *
- */
-public class ByteBufferBackedOutputStream extends OutputStream {
-
- private ByteBuffer buf;
-
- /**
- * accept a ByteBuffer to store external data, the capacity of it could be
- * extended at will.
- *
- * @param buf
- * specify a ByteBuffer object that is used to store external
- * data to its backed buffer
- *
- */
- public ByteBufferBackedOutputStream(ByteBuffer buf) {
- this.buf = buf;
- }
-
- /**
- * write an integer value to backed buffer.
- *
- * @param b
- * specify an integer value to be written
- */
- public void write(int b) throws IOException {
- buf.put((byte) b);
- }
-
- /**
- * write an array of bytes to a specified range of backed buffer
- *
- * @param bytes
- * specify a byte array to write
- *
- * @param off
- * specify the offset of backed buffer where is start point to be
- * written
- *
- * @param len
- * specify the length of bytes to be written
- */
- public void write(byte[] bytes, int off, int len) throws IOException {
- buf.put(bytes, off, len);
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/ByteBufferSerializer.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/ByteBufferSerializer.java b/core/src/main/java/com/intel/bigdatamem/ByteBufferSerializer.java
deleted file mode 100644
index eec66ce..0000000
--- a/core/src/main/java/com/intel/bigdatamem/ByteBufferSerializer.java
+++ /dev/null
@@ -1,149 +0,0 @@
-package com.intel.bigdatamem;
-
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.nio.ByteBuffer;
-import java.io.InputStream;
-
-/**
- * a serializer class that manages to serialize any serilizable objects from/to
- * ByteBuffer.
- *
- */
-
-public class ByteBufferSerializer {
-
- /**
- * serialize object to a ByteBuffer object.
- *
- * @param <ValueT>
- * the type of object
- *
- * @param obj
- * specify a object that is serializable
- *
- * @throws IOException
- * the exception of serialization
- *
- * @return a ByteBuffer object contained serialized object
- */
- public static <ValueT> ByteBuffer toByteBuffer(ValueT obj)
- throws IOException {
- byte[] bytes = null;
- ByteArrayOutputStream bos = null;
- ObjectOutputStream oos = null;
- try {
- bos = new ByteArrayOutputStream();
- oos = new ObjectOutputStream(bos);
- oos.writeObject(obj);
- oos.flush();
- bytes = bos.toByteArray();
- } finally {
- if (oos != null) {
- oos.close();
- }
- if (bos != null) {
- bos.close();
- }
- }
- return ByteBuffer.wrap(bytes);
- }
-
- /**
- * de-serialize an object from a ByteBuffer object.
- *
- * @param <ValueT>
- * the type of object
- *
- * @param bf
- * specify an ByteBuffer contains data that can be de-serialized
- *
- * @throws IOException
- * the exception of deserialization
- *
- * @throws ClassNotFoundException
- * Not found class of de-serialized object
- *
- * @return a de-serialized object
- */
- @SuppressWarnings("unchecked")
- public static <ValueT> ValueT toObject(ByteBuffer bf) throws IOException,
- ClassNotFoundException {
- Object obj = null;
- InputStream is = null;
- ObjectInputStream ois = null;
- try {
- is = new ByteBufferBackedInputStream(bf);
- ois = new ObjectInputStream(is);
- obj = ois.readObject();
- } finally {
- if (is != null) {
- is.close();
- }
- if (ois != null) {
- ois.close();
- }
- }
- return (ValueT) obj;
- }
-
- /**
- * serialize object to a MemBufferHolder object.
- *
- * @param <A> the type of bound allocator
- *
- * @param <ValueT>
- * the type of object
- *
- * @param ar
- * specify an Allocator that is used to generate MemBufferHolder
- * which is backed by a native memory block
- *
- * @param obj
- * specify a object that is serializable
- *
- * @throws IOException
- * the exception of serialization
- *
- * @return a MemBufferHolder object contained serialized object
- */
- public static <A extends CommonAllocator<A>, ValueT> MemBufferHolder<A>
- toMemBufferHolder(A ar, ValueT obj) throws IOException {
- MemBufferHolder<A> ret = null;
- ByteBuffer bb = toByteBuffer(obj);
- if (null != bb && bb.remaining() > 0) {
- ret = ar.createBuffer(bb.remaining());
- ret.get().put(bb);
- ret.get().flip();
- }
- return ret;
- }
-
- /**
- * de-serialize an object from a MemBufferHolder object.
- *
- * @param <A> the type of bound allocator
- *
- * @param <ValueT>
- * the type of object
- *
- * @param mbh
- * specify an MemBufferHolder who contains data that can be
- * de-serialized
- *
- * @throws IOException
- * the exception of deserialization
- *
- * @throws ClassNotFoundException
- * Not found class of de-serialized object
- *
- * @return a de-serialized object
- */
- public static <A extends CommonAllocator<A>, ValueT> ValueT
- fromMemBufferHolder(MemBufferHolder<A> mbh)
- throws IOException, ClassNotFoundException {
- return toObject(mbh.get());
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/CachePool.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/CachePool.java b/core/src/main/java/com/intel/bigdatamem/CachePool.java
deleted file mode 100644
index 0180e45..0000000
--- a/core/src/main/java/com/intel/bigdatamem/CachePool.java
+++ /dev/null
@@ -1,144 +0,0 @@
-package com.intel.bigdatamem;
-
-import java.util.Set;
-import java.util.LinkedHashMap;
-import java.util.Map;
-
-/**
- * This is a abstract CachePool class that is inherited from LinedHashMap class,
- * it extends functionalities of its parent class to support some new features
- * that is usually requried in data caching usage scenario.
- *
- *
- */
-public abstract class CachePool<KeyT, ValueT> extends
- LinkedHashMap<KeyT, ValueT> {
-
- /**
- *
- */
- private static final long serialVersionUID = 1L;
-
- public CachePool(int initialCapacity, float loadFactor) {
- super(initialCapacity, loadFactor);
- }
-
- /**
- * Return available capacity for new entries.
- *
- * @return available capacity in this cache pool
- */
- public abstract long freeCapacity();
-
- /**
- * Return the used capacity of cached entries.
- *
- * @return the used size of this cache pool
- */
- public abstract long usedCapacity();
-
- /**
- * Put an entry in this cache pool and evict eldest entries if necessary
- * that will free enough space to hold new entry, which entry could be
- * evicted that can be customized by ({@link EvictFilter}), regarding how
- * to post-process the eldest entry that can be customized by (
- * {@link DropEvent}). If this cache pool previously contained an entry for
- * the key, the old value will be replaced by the specified value
- *
- * @param k
- * the key whoes associated value is to be put
- *
- * @param v
- * the value to be put
- *
- * @param fsop
- * the customized operations to free space to hold new entry
- *
- * @param dfilter
- * the filter of entries for deletion
- *
- * @return <tt>true</tt> if the entry has been put into this container
- *
- */
- public abstract ValueT put(KeyT k, ValueT v, DropEvent<KeyT, ValueT> fsop,
- EvictFilter<KeyT, ValueT> dfilter);
-
- /**
- * Put all entries into this cache pool and evict eldes entries if
- * necessary.
- *
- * @param m
- * the Map object that contains entries to be put
- *
- * @param fsop
- * the customized operations to free space to hold new entry
- *
- * @param dfilter
- * the filter of entries for deletion
- *
- */
- public abstract void putAll(
- Map<? extends KeyT, ? extends ValueT> m,
- DropEvent<KeyT, ValueT> fsop,
- EvictFilter<KeyT, ValueT> dfilter);
-
- /**
- * Returns a new {@link Set} view of the keys of this cache pool, It
- * contains the most recently visited keys
- *
- * @param n
- * the number of keys to retrieve
- *
- * @return a set of hot keys
- */
- public abstract Set<KeyT> hotKeySet(int n);
-
- /**
- * Ensure the free capacity is greater than the specified size
- *
- * @param freesz
- * the size of free capacity that needs to be secured
- *
- * @return <tt>true</tt> if the size of free capacity is greater than the
- * specified size after evacuation
- *
- * @see #ensureFreeSpace(long, DropEvent, EvictFilter)
- */
- public abstract boolean ensureFreeSpace(long freesz);
-
- /**
- * Removes a first qualified entry in this cache pool
- *
- * @param fsop
- * the customized callback to post-process its evicted entry
- *
- * @param dfilter
- * the filter for entry deletion
- *
- * @return <tt>true</tt> if there is one qualified entry that has been
- * dropped
- */
- public abstract boolean removeFirstEntry(DropEvent<KeyT, ValueT> fsop,
- EvictFilter<KeyT, ValueT> dfilter);
-
- /**
- * Ensure the size of free capacity is greater than the specified size, the
- * entries will be filtered by {@link EvictFilter} before dropping, the
- * {@link DropEvent} is used for post-processing
- *
- * @param freesz
- * the size of free capacity that needs to be secured
- *
- * @param fsop
- * the customized operations to free space to hold new entry
- *
- * @param dfilter
- * the filter of entries for deletion
- *
- * @return <tt>true</tt> if the size of free capacity is greater than the
- * specified size after evication if necessary
- */
- public abstract boolean ensureFreeSpace(long freesz,
- DropEvent<KeyT, ValueT> fsop, EvictFilter<KeyT, ValueT> dfilter);
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/CommonAllocator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/CommonAllocator.java b/core/src/main/java/com/intel/bigdatamem/CommonAllocator.java
deleted file mode 100644
index c12e9b6..0000000
--- a/core/src/main/java/com/intel/bigdatamem/CommonAllocator.java
+++ /dev/null
@@ -1,104 +0,0 @@
-package com.intel.bigdatamem;
-
-import java.nio.ByteBuffer;
-
-import org.flowcomputing.commons.resgc.ResCollector;
-
-/**
- * an abstract common class for memory allocator to provide common
- * functionalities.
- *
- */
-public abstract class CommonAllocator<A extends CommonAllocator<A>> implements Allocator<A> {
-
- protected Reclaim<Long> m_chunkreclaimer = null;
- protected Reclaim<ByteBuffer> m_bufferreclaimer = null;
-
- protected ResCollector<MemChunkHolder<A>, Long> m_chunkcollector = null;
- protected ResCollector<MemBufferHolder<A>, ByteBuffer> m_bufcollector = null;
-
- /**
- * set a reclaimer to reclaim memory buffer
- *
- * @param reclaimer
- * specify a reclaimer to accept reclaim request
- */
- public void setBufferReclaimer(Reclaim<ByteBuffer> reclaimer) {
- m_bufferreclaimer = reclaimer;
- }
-
- /**
- * set a reclaimer to reclaim memory chunk
- *
- * @param reclaimer
- * specify a reclaimer to accept reclaim request
- */
- public void setChunkReclaimer(Reclaim<Long> reclaimer) {
- m_chunkreclaimer = reclaimer;
- }
-
- /**
- * create a memory chunk that is managed by its holder.
- *
- * @param size
- * specify the size of memory chunk
- *
- * @return a holder contains a memory chunk
- */
- @Override
- public MemChunkHolder<A> createChunk(long size) {
- return createChunk(size, true);
- }
-
- /**
- * 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<A> createBuffer(long size) {
- return createBuffer(size, true);
- }
-
- /**
- * register a memory chunk for auto-reclaim
- *
- * @param mholder
- * specify a chunk holder to register
- */
- @Override
- public void registerChunkAutoReclaim(MemChunkHolder<A> mholder) {
- m_chunkcollector.register(mholder);
- }
-
- /**
- * register a memory buffer for auto-reclaim
- *
- * @param mholder
- * specify a buffer holder to register
- */
- @Override
- public void registerBufferAutoReclaim(MemBufferHolder<A> mholder) {
- m_bufcollector.register(mholder);
- }
-
- /**
- * close both of resource collectors for this allocator
- *
- */
- @Override
- public void close() {
- if (null != m_chunkcollector) {
- m_chunkcollector.close();
- m_chunkcollector = null;
- }
- if (null != m_bufcollector) {
- m_bufcollector.close();
- m_bufcollector = null;
- }
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/CommonPersistAllocator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/CommonPersistAllocator.java b/core/src/main/java/com/intel/bigdatamem/CommonPersistAllocator.java
deleted file mode 100644
index 514fd01..0000000
--- a/core/src/main/java/com/intel/bigdatamem/CommonPersistAllocator.java
+++ /dev/null
@@ -1,120 +0,0 @@
-package com.intel.bigdatamem;
-
-/**
- * an abstract common class for persistent memory allocator to provide common
- * functionalities.
- *
- */
-public abstract class CommonPersistAllocator<A extends CommonAllocator<A>> extends CommonAllocator<A> {
-
- /**
- * determine whether the allocator supports transaction feature or not
- *
- * @return true if supported
- */
- public boolean supportTransaction() {
- return false;
- }
-
- /**
- * determine whether the allocator does atomic operations on memory pool
- *
- * @return true if it is
- *
- */
- public boolean isAtomicOperation() {
- return false;
- }
-
- /**
- * determine whether this allocator supports to store non-volatile handler or not
- *
- * @return true if there is
- */
- public boolean hasNonVolatileHandlerStore() {
- return false;
- }
-
- /**
- * retrieve a memory buffer from its backed memory allocator.
- *
- * @param phandler
- * specify the handler of memory buffer to retrieve
- *
- * @return a holder contains the retrieved memory buffer
- */
- public MemBufferHolder<A> retrieveBuffer(long phandler) {
- return retrieveBuffer(phandler, true);
- }
-
- /**
- * retrieve a memory chunk from its backed memory allocator.
- *
- * @param phandler
- * specify the handler of memory chunk to retrieve
- *
- * @return a holder contains the retrieved memory chunk
- */
- public MemChunkHolder<A> retrieveChunk(long phandler) {
- return retrieveChunk(phandler, true);
- }
-
- /**
- * retrieve a memory buffer from its backed memory allocator.
- *
- * @param phandler
- * specify the handler of memory buffer to retrieve
- *
- * @param autoreclaim
- * specify whether this retrieved memory buffer can be reclaimed automatically or not
- *
- * @return a holder contains the retrieved memory buffer
- */
- abstract public MemBufferHolder<A> retrieveBuffer(long phandler, boolean autoreclaim);
-
- /**
- * retrieve a memory chunk from its backed memory allocator.
- *
- * @param phandler
- * specify the handler of memory chunk to retrieve
- *
- * @param autoreclaim
- * specify whether this retrieved memory chunk can be reclaimed automatically or not
- *
- * @return a holder contains the retrieved memory chunk
- */
- abstract public MemChunkHolder<A> retrieveChunk(long phandler, boolean autoreclaim);
-
- /**
- * get the handler from a memory buffer holder.
- *
- * @param mbuf
- * specify the memory buffer holder
- *
- * @return a handler that could be used to retrieve its memory buffer
- */
- abstract public long getBufferHandler(MemBufferHolder<A> mbuf);
-
- /**
- * get the handler from a memory chunk holder.
- *
- * @param mchunk
- * specify the memory chunk holder
- *
- * @return a handler that could be used to retrieve its memory chunk
- */
- abstract public long getChunkHandler(MemChunkHolder<A> mchunk);
-
- /**
- * start a application level transaction on this allocator.
- *
- */
- abstract public void beginTransaction();
-
- /**
- * end a application level transaction on this allocator.
- *
- */
- abstract public void endTransaction();
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/ContainerOverflowException.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/ContainerOverflowException.java b/core/src/main/java/com/intel/bigdatamem/ContainerOverflowException.java
deleted file mode 100644
index c591f80..0000000
--- a/core/src/main/java/com/intel/bigdatamem/ContainerOverflowException.java
+++ /dev/null
@@ -1,20 +0,0 @@
-package com.intel.bigdatamem;
-
-/**
- * this Exception will be thrown once cache pool has been out of space.
- *
- */
-public class ContainerOverflowException extends RuntimeException {
-
- private static final long serialVersionUID = -8515518456414990004L;
-
- /**
- * accept a exception message to describe specific condition.
- *
- * @param message
- * exception message
- */
- public ContainerOverflowException(String message) {
- super(message);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/DropEvent.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/DropEvent.java b/core/src/main/java/com/intel/bigdatamem/DropEvent.java
deleted file mode 100644
index 31d473e..0000000
--- a/core/src/main/java/com/intel/bigdatamem/DropEvent.java
+++ /dev/null
@@ -1,24 +0,0 @@
-package com.intel.bigdatamem;
-
-/**
- * A event listener to monitor and post-process an entry's evacuation.
- *
- *
- */
-public interface DropEvent<KeyT, ValueT> {
- /**
- * A call-back actor when an entry has been evicted. a customized drop
- * action can be implemented on this interface's method e.g. spill this
- * entry to disk or release associated resources etc.
- *
- * @param pool
- * the pool which an entry has been evicted from
- *
- * @param k
- * the key of an entry that has been evicted
- *
- * @param v
- * the value of an entry that has been evicted
- */
- public void drop(CachePool<KeyT, ValueT> pool, KeyT k, ValueT v);
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/Durable.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/Durable.java b/core/src/main/java/com/intel/bigdatamem/Durable.java
deleted file mode 100644
index 5021daa..0000000
--- a/core/src/main/java/com/intel/bigdatamem/Durable.java
+++ /dev/null
@@ -1,74 +0,0 @@
-package com.intel.bigdatamem;
-
-/**
- * this interface defines the interactive functionalities with Mnenomic core part.
- *
- */
-
-
-public interface Durable {
-
- /**
- * this function will be invoked after this non-volatile object is created brandly new
- *
- */
- public void initializeAfterCreate();
-
- /**
- * this function will be invoked after this non-volatile object is restored from its allocator
- *
- */
- public void initializeAfterRestore();
-
-
- /**
- * this function will be invoked by its factory to setup generic related info to avoid expensive operations from reflection
- *
- * @param efproxies
- * specify a array of factory to proxy the restoring of its generic field objects
- *
- * @param gftypes
- * specify a array of types corresponding to efproxies
- */
- public void setupGenericInfo(EntityFactoryProxy[] efproxies, GenericField.GType[] gftypes);
-
- /**
- * this function could be called by user code to disable auto-reclaim for this non-volatile object
- *
- */
- public void cancelAutoReclaim();
-
- /**
- * this function could be called by user code to register this object for auto-reclaim
- *
- */
- public void registerAutoReclaim();
-
- /**
- * this function returns its bound handler for this object
- *
- * @return the handler of this object
- */
- public long getNonVolatileHandler();
-
- /**
- * return the setting for auto-reclaim
- *
- * @return the status of the auto-reclaim setting
- */
- public boolean autoReclaim();
-
- /**
- * manually destroy this object and release its memory resource
- *
- */
- public void destroy() throws RetrieveNonVolatileEntityError;
-
- /**
- * return the native field map info for native processing.
- *
- * @return the native field map info
- *
- */
- public long[][] getNativeFieldInfo();
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/EntityFactoryProxy.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/EntityFactoryProxy.java b/core/src/main/java/com/intel/bigdatamem/EntityFactoryProxy.java
deleted file mode 100644
index 57c52ae..0000000
--- a/core/src/main/java/com/intel/bigdatamem/EntityFactoryProxy.java
+++ /dev/null
@@ -1,35 +0,0 @@
-package com.intel.bigdatamem;
-
-/**
- * proxy the restoring of generic non-volatile object fields
- *
- */
-
-
-public interface EntityFactoryProxy {
-
- /**
- * restore a durable object from persistent allocator using a handler of non-volatile object
- *
- * @param allocator
- * specify a persistent allocator instance
- *
- * @param factoryproxys
- * specify an array of factory proxies for its restored non-volatile object
- *
- * @param gfields
- * specify an array of generic types of its generic fields corresponding to factoryproxys
- *
- * @param phandler
- * specify a non-volatile handler to restore
- *
- * @param autoreclaim
- * specify auto-reclaim for this restored non-volatile object
- *
- * @return the restored non-volatile object from this factory proxy
- *
- */
- public abstract <A extends CommonPersistAllocator<A>> Durable restore(A allocator,
- EntityFactoryProxy[] factoryproxys, GenericField.GType[] gfields, long phandler,
- boolean autoreclaim);
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/EvictFilter.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/EvictFilter.java b/core/src/main/java/com/intel/bigdatamem/EvictFilter.java
deleted file mode 100644
index a9e5f10..0000000
--- a/core/src/main/java/com/intel/bigdatamem/EvictFilter.java
+++ /dev/null
@@ -1,24 +0,0 @@
-package com.intel.bigdatamem;
-
-/**
- * A listener callback to validate its accepted key-value pair for evacuation.
- *
- */
-public interface EvictFilter<KeyT, ValueT> {
- /**
- * A call-back validator when an entry will be evicted.
- *
- * @param pool
- * the pool which an entry has been evicted from
- *
- * @param k
- * the entry's key
- *
- * @param v
- * the entry's value
- *
- * @return <tt>true</tt> if the entry is allowed to be dropped from its
- * cache pool.
- */
- public boolean validate(CachePool<KeyT, ValueT> pool, KeyT k, ValueT v);
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/GenericField.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/GenericField.java b/core/src/main/java/com/intel/bigdatamem/GenericField.java
deleted file mode 100644
index 95885dd..0000000
--- a/core/src/main/java/com/intel/bigdatamem/GenericField.java
+++ /dev/null
@@ -1,288 +0,0 @@
-package com.intel.bigdatamem;
-
-/**
- * this class defines generic field for non-volatile entity
- *
- */
-
-import sun.misc.Unsafe;
-
-@SuppressWarnings("restriction")
-public class GenericField<A extends CommonPersistAllocator<A>, E> implements Durable {
-
- /**
- * defines the types of generic field
- *
- */
- public enum GType {
- BOOLEAN, CHARACTER, BYTE, SHORT, INTEGER, LONG, FLOAT, DOUBLE, STRING, DURABLE
- };
-
- private Unsafe m_unsafe;
- private long m_fpos;
- private GType m_dgftype = null;
- private Durable m_field = null;
- private MemBufferHolder<A> m_strfield = null;
- private A m_allocator;
- private boolean m_autoreclaim;
- private EntityFactoryProxy m_defproxy = null;
- private EntityFactoryProxy[] m_efproxies;
- private GType[] m_gftypes;
-
- /**
- * Constructor: initialize this generic field
- *
- * @param defproxy
- * specify its entity factory proxy
- *
- * @param dgftype
- * specify its type
- *
- * @param efproxies
- * specify an array of containing entity factory proxies
- *
- * @param gftypes
- * specify an array of containing types corresponding to dfproxies
- *
- * @param allocator
- * specify the allocator this field sit on
- *
- * @param unsafe
- * specify the unsafe instance
- *
- * @param autoreclaim
- * specify true if ask for auto-reclaim for this field
- *
- * @param fpos
- * specify the field position
- *
- */
- public GenericField(EntityFactoryProxy defproxy, GType dgftype, EntityFactoryProxy[] efproxies, GType[] gftypes,
- A allocator, Unsafe unsafe, boolean autoreclaim, Long fpos) {
- m_unsafe = unsafe;
- m_fpos = fpos;
- m_allocator = allocator;
- m_autoreclaim = autoreclaim;
- m_efproxies = efproxies;
- m_gftypes = gftypes;
- m_defproxy = defproxy;
- m_dgftype = dgftype;
- }
-
- /**
- * set a value to this field
- *
- * @param e
- * specify a value to set
- *
- * @param destroy
- * specify true if want to destroy the original value
- */
- public void set(E e, boolean destroy) {
- boolean isnull = null == e;
- switch (m_dgftype) {
- case BYTE:
- m_unsafe.putByte(m_fpos, isnull ? (byte)0 : (Byte) e);
- break;
- case BOOLEAN:
- m_unsafe.putByte(m_fpos, isnull ? (byte)0 : ((Boolean) e ? (byte)1 : (byte)0));
- break;
- case CHARACTER:
- m_unsafe.putChar(m_fpos, isnull ? (char)0 : (Character) e);
- break;
- case SHORT:
- m_unsafe.putShort(m_fpos, isnull ? (short)0 : (Short) e);
- break;
- case INTEGER:
- m_unsafe.putInt(m_fpos, isnull ? (int)0 : (Integer) e);
- break;
- case LONG:
- m_unsafe.putLong(m_fpos, isnull ? (long)0 : (Long) e);
- break;
- case FLOAT:
- m_unsafe.putFloat(m_fpos, isnull ? (float)0 : (Float) e);
- break;
- case DOUBLE:
- m_unsafe.putDouble(m_fpos, isnull ? (double)0 : (Double) e);
- break;
- case STRING:
- String str = (String) e;
- if (destroy && null != get()) {
- m_strfield.destroy();
- m_strfield = null;
- m_unsafe.putAddress(m_fpos, 0L);
- }
- if (null == str) {
- m_unsafe.putAddress(m_fpos, 0L);
- } else {
- m_strfield = m_allocator.createBuffer(str.length() * 2, m_autoreclaim);
- if (null == m_strfield) {
- throw new OutOfPersistentMemory("Create Persistent String Error!");
- }
- m_strfield.get().asCharBuffer().put(str);
- m_unsafe.putAddress(m_fpos, m_allocator.getBufferHandler(m_strfield));
- }
- break;
- case DURABLE:
- if (destroy && null != get()) {
- m_field.destroy();
- m_field = null;
- m_unsafe.putAddress(m_fpos, 0L);
- }
- m_field = (Durable) e;
- m_unsafe.putAddress(m_fpos, null == m_field ? 0L : m_field.getNonVolatileHandler());
- break;
- }
-
- }
-
- /**
- * get the value of this field
- *
- * @return the field value
- */
- @SuppressWarnings("unchecked")
- public E get() {
- E ret = null;
- switch (m_dgftype) {
- case BYTE:
- ret = (E) Byte.valueOf(m_unsafe.getByte(m_fpos));
- break;
- case BOOLEAN:
- ret = (E) Boolean.valueOf(1 == m_unsafe.getByte(m_fpos));
- break;
- case CHARACTER:
- ret = (E) Character.valueOf(m_unsafe.getChar(m_fpos));
- break;
- case SHORT:
- ret = (E) Short.valueOf(m_unsafe.getShort(m_fpos));
- break;
- case INTEGER:
- ret = (E) Integer.valueOf(m_unsafe.getInt(m_fpos));
- break;
- case LONG:
- ret = (E) Long.valueOf(m_unsafe.getLong(m_fpos));
- break;
- case FLOAT:
- ret = (E) Float.valueOf(m_unsafe.getFloat(m_fpos));
- break;
- case DOUBLE:
- ret = (E) Double.valueOf(m_unsafe.getDouble(m_fpos));
- break;
- case STRING:
- if (null == m_strfield) {
- long phandler = m_unsafe.getAddress(m_fpos);
- if (0L != phandler) {
- m_strfield = m_allocator.retrieveBuffer(phandler, m_autoreclaim);
- if (null == m_strfield) {
- throw new RetrieveNonVolatileEntityError("Retrieve String Buffer Failure.");
- }
- }
- }
- ret = (E) (null == m_strfield ? null : m_strfield.get().asCharBuffer().toString());
- break;
- case DURABLE:
- if (null == m_field) {
- long phandler = m_unsafe.getAddress(m_fpos);
- if (0L != phandler) {
- if (null == m_defproxy) {
- throw new RetrieveNonVolatileEntityError("Proxy not specified for Non-Volatile Generic entity.");
- }
- m_field = m_defproxy.restore(m_allocator, m_efproxies, m_gftypes, phandler, m_autoreclaim);
- }
- }
- ret = (E) m_field;
- break;
- }
- return ret;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void initializeAfterCreate() {
- throw new UnsupportedOperationException("GenericField.initializeAfterCreate()");
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void initializeAfterRestore() {
- throw new UnsupportedOperationException("GenericField.initializeAfterRestore()");
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void cancelAutoReclaim() {
- if (null != m_field) {
- m_field.cancelAutoReclaim();
- }
- if (null != m_strfield) {
- m_strfield.cancelAutoReclaim();
- }
- m_autoreclaim = false;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void registerAutoReclaim() {
- if (null != m_field) {
- m_field.registerAutoReclaim();
- }
- if (null != m_strfield) {
- m_strfield.registerAutoReclaim();
- }
- m_autoreclaim = true;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public long getNonVolatileHandler() {
- throw new UnsupportedOperationException("GenericField.getNonVolatileHandler()");
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean autoReclaim() {
- return m_autoreclaim;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void destroy() throws RetrieveNonVolatileEntityError {
- if (null != m_field) {
- m_field.destroy();
- }
- if (null != m_strfield) {
- m_strfield.destroy();
- }
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void setupGenericInfo(EntityFactoryProxy[] efproxies, GType[] gftypes) {
- throw new UnsupportedOperationException("GenericField.setupGenericInfo()");
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public long[][] getNativeFieldInfo() {
- throw new UnsupportedOperationException("getNativeFieldInfo() on Generic Field.");
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/MemBufferHolder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/MemBufferHolder.java b/core/src/main/java/com/intel/bigdatamem/MemBufferHolder.java
deleted file mode 100644
index 408046b..0000000
--- a/core/src/main/java/com/intel/bigdatamem/MemBufferHolder.java
+++ /dev/null
@@ -1,56 +0,0 @@
-package com.intel.bigdatamem;
-
-import java.nio.ByteBuffer;
-
-/**
- * holder for a ByteBuffer instance.
- *
- */
-public class MemBufferHolder<A extends CommonAllocator<A>> extends MemHolder<A, ByteBuffer, MemBufferHolder<A>> {
-
-
- /**
- * Constructor: initialize with a bytebuffer.
- *
- * @param ar
- * specify an Allocator for this holder
- *
- * @param mres
- * specify a chunk to be holden
- *
- */
- public MemBufferHolder(A ar, ByteBuffer mres) {
- super(mres, ar);
- }
-
- /**
- * get the size of its held bytebuffer
- *
- * @return the size
- */
- @Override
- public long getSize() {
- return m_mres.capacity();
- }
-
- /**
- * resize its held buffer
- *
- * @param size
- * specify the new size for its held buffer
- */
- @Override
- public MemBufferHolder<A> resize(long size) {
- return m_allocator.resizeBuffer(this, size);
- }
-
- /**
- * register its held buffer for auto-reclaim
- *
- */
- @Override
- public void registerAutoReclaim() {
- m_allocator.registerBufferAutoReclaim(this);
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/MemBufferHolderCachePool.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/MemBufferHolderCachePool.java b/core/src/main/java/com/intel/bigdatamem/MemBufferHolderCachePool.java
deleted file mode 100644
index 93d6875..0000000
--- a/core/src/main/java/com/intel/bigdatamem/MemBufferHolderCachePool.java
+++ /dev/null
@@ -1,236 +0,0 @@
-
-package com.intel.bigdatamem;
-
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.Set;
-import java.util.Map;
-import java.util.concurrent.atomic.AtomicLong;
-
-/**
- *
- * This class inherited from abstract CachePool class that implemented all
- * inherited abstract methods. it specialized to cache MemBufferHolder objects
- * that is backed by native memory pool.
- *
- */
-public class MemBufferHolderCachePool<KeyT> extends
- CachePool<KeyT, MemBufferHolder<?>> {
-
- private static final long serialVersionUID = 684275993324558070L;
-
- private AtomicLong currentMemory = new AtomicLong(0L);
-
- private long maxStoreCapacity = 0L;
-
- /**
- * Constructs an empty cache pool with specified capacity.
- *
- * @param maxCapacity
- * the capacity of this cache pool
- *
- */
- public MemBufferHolderCachePool(long maxCapacity) {
- super(512, 0.75f);
- maxStoreCapacity = maxCapacity;
- }
-
- /**
- * @see CachePool#freeCapacity()
- */
- @Override
- public long freeCapacity() {
- return maxStoreCapacity - currentMemory.get();
- }
-
- /**
- * @see CachePool#usedCapacity()
- */
- @Override
- public long usedCapacity() {
- return currentMemory.get();
- }
-
- /**
- * @see Map#remove(Object)
- */
- @Override
- public synchronized MemBufferHolder<?> remove(Object k) {
- MemBufferHolder<?> ret = super.remove(k);
- if (null != ret) {
- currentMemory.getAndAdd(-ret.getSize());
- }
- return ret;
- }
-
- /**
- * Associates the specified value with the specified key in this map
- * (optional operation). If the map previously contained a mapping for the
- * key, the old value is replaced by the specified value. (A map m is said
- * to contain a mapping for a key k if and only if m.containsKey(k) would
- * return true.)
- *
- * @param k
- * key with which the specified value is to be associated
- *
- * @param v
- * MemBufferHolder value to be associated with the specified key
- *
- * @return the previous value associated with key, or null if there was no
- * mapping for key. (A null return can also indicate that the map
- * previously associated null with key, if the implementation
- * supports null values.)
- */
- @Override
- public MemBufferHolder<?> put(KeyT k, MemBufferHolder<?> v) {
- return put(k, v, null, null);
- }
-
- /**
- * @see CachePool#put(Object, Object, DropEvent, EvictFilter)
- */
- @Override
- public MemBufferHolder<?> put(KeyT k, MemBufferHolder<?> v,
- DropEvent<KeyT, MemBufferHolder<?>> fsop,
- EvictFilter<KeyT, MemBufferHolder<?>> dfilter) {
- MemBufferHolder<?> ret = null;
- long sz = v.getSize();
- if (containsKey(k)) {
- sz -= get(k).getSize();
- }
- if (sz <= maxStoreCapacity && ensureFreeSpace(sz, fsop, dfilter)) {
- currentMemory.addAndGet(sz);
- ret = super.put(k, v);
- } else {
- throw new ContainerOverflowException(
- "Out of capacity of MemBufferHolderCachePool.");
- }
- return ret;
- }
-
- /**
- * @see Map#putAll(Map)
- */
- @Override
- public void putAll(
- Map<? extends KeyT, ? extends MemBufferHolder<?>> m) {
- putAll(m, null, null);
- }
-
- /**
- * @see CachePool#putAll(Map, DropEvent, EvictFilter)
- */
- @Override
- public void putAll(
- Map<? extends KeyT, ? extends MemBufferHolder<?>> m,
- DropEvent<KeyT, MemBufferHolder<?>> fsop,
- EvictFilter<KeyT, MemBufferHolder<?>> dfilter) {
-
- long reqsz = 0;
- for (KeyT k : m.keySet()) {
- reqsz += m.get(k).getSize();
- }
-
- if (reqsz <= maxStoreCapacity && ensureFreeSpace(reqsz, fsop, dfilter)) {
- currentMemory.addAndGet(reqsz);
- super.putAll(m);
- } else {
- throw new ContainerOverflowException(
- "Out of capacity of MemBufferHolderCachePool.");
- }
- }
-
- /**
- * @see CachePool#hotKeySet(int)
- */
- @Override
- public Set<KeyT> hotKeySet(int n) {
- Set<KeyT> ret = new HashSet<KeyT>();
- ArrayList<KeyT> keys = new ArrayList<KeyT>(keySet());
- int endindex = keys.size() > n ? keys.size() - n : 0;
- for (int i = keys.size(); i > endindex; i--) {
- ret.add(keys.get(i - 1));
- }
- return ret;
- }
-
- /**
- * @see CachePool#ensureFreeSpace(long)
- */
- @Override
- public boolean ensureFreeSpace(long freesz) {
- return ensureFreeSpace(freesz, null, null);
- }
-
- /**
- * @see CachePool#removeFirstEntry(DropEvent, EvictFilter)
- */
- @Override
- public boolean removeFirstEntry(
- DropEvent<KeyT, MemBufferHolder<?>> fsop,
- EvictFilter<KeyT, MemBufferHolder<?>> dfilter) {
- boolean ret = false;
- boolean delible = true;
- for (Map.Entry<KeyT, MemBufferHolder<?>> entry : entrySet()) {
- if (null != dfilter) {
- delible = dfilter.validate(this, entry.getKey(),
- entry.getValue());
- }
- if (delible) {
- KeyT k = entry.getKey();
- MemBufferHolder<?> v = remove(k);
- if (null != fsop) {
- fsop.drop(this, k, v);
- }
- ret = true;
- break;
- }
- }
- return ret;
- }
-
- /**
- * @see CachePool#ensureFreeSpace(long, DropEvent, EvictFilter)
- */
- @Override
- public boolean ensureFreeSpace(long freesz,
- DropEvent<KeyT, MemBufferHolder<?>> fsop,
- EvictFilter<KeyT, MemBufferHolder<?>> dfilter) {
- boolean ret = false;
-
- if (freesz <= freeCapacity())
- return true;
-
- if (freesz > maxStoreCapacity)
- return false;
-
- long selectedMemory = 0L;
- Set<KeyT> selected = new HashSet<KeyT>();
-
- boolean delible = true;
- for (Map.Entry<KeyT, MemBufferHolder<?>> entry : entrySet()) {
- if (null != dfilter) {
- delible = dfilter.validate(this, entry.getKey(),
- entry.getValue());
- }
- if (delible) {
- selectedMemory += entry.getValue().getSize();
- selected.add(entry.getKey());
- if (freesz <= freeCapacity() + selectedMemory) {
- ret = true;
- break;
- }
- }
- }
- if (ret) {
- for (KeyT k : selected) {
- MemBufferHolder<?> mbh = remove(k);
- if (null != fsop) {
- fsop.drop(this, k, mbh);
- }
- }
- }
- return ret;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/MemChunkHolder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/MemChunkHolder.java b/core/src/main/java/com/intel/bigdatamem/MemChunkHolder.java
deleted file mode 100644
index cca079f..0000000
--- a/core/src/main/java/com/intel/bigdatamem/MemChunkHolder.java
+++ /dev/null
@@ -1,58 +0,0 @@
-package com.intel.bigdatamem;
-
-/**
- * holder for a memory chunk.
- *
- */
-public class MemChunkHolder<A extends CommonAllocator<A>> extends MemHolder<A, Long, MemChunkHolder<A>> {
-
- protected long m_size;
-
- /**
- * Constructor: initialize with a memory chunk.
- *
- * @param ar
- * specify an Allocator for this holder
- *
- * @param mres
- * specify a chunk to be holden
- *
- * @param size
- * specify the size of this memory chunk
- */
- public MemChunkHolder(A ar, Long mres, long size) {
- super(mres, ar);
- m_size = size;
- }
-
- /**
- * get the size of its held memory chunk
- *
- * @return the size
- */
- @Override
- public long getSize() {
- return m_size;
- }
-
- /**
- * resize its held chunk
- *
- * @param size
- * specify the new size for its held chunk
- */
- @Override
- public MemChunkHolder<A> resize(long size) {
- return m_allocator.resizeChunk(this, size);
- }
-
- /**
- * register its held chunk for auto-reclaim
- *
- */
- @Override
- public void registerAutoReclaim() {
- m_allocator.registerChunkAutoReclaim(this);
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/MemClustering.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/MemClustering.java b/core/src/main/java/com/intel/bigdatamem/MemClustering.java
deleted file mode 100644
index f452db2..0000000
--- a/core/src/main/java/com/intel/bigdatamem/MemClustering.java
+++ /dev/null
@@ -1,297 +0,0 @@
-package com.intel.bigdatamem;
-
-import java.util.*;
-
-/**
- * clustering different kind of memory-like media and combine them as a larger
- * memory pool for allocation. it will notify user when the underlying memory
- * storage has been switched or downgraded for the request of allocation
- *
- */
-public class MemClustering {
-
- /**
- * an interface of event for performance level change.
- *
- */
- public interface PerformanceLevelChange {
-
- /**
- * callback if performance level changed
- *
- * @param prevlvl
- * the perf. level before change
- *
- * @param tgtlvl
- * the perf. level after change
- *
- */
- public void changed(PerformanceLevel prevlvl, PerformanceLevel tgtlvl);
- }
-
- /**
- * an interface of event for CommonAllocator change.
- *
- */
- public interface AllocatorChange {
-
- /**
- * callback if allocator changed
- *
- * @param lvl
- * the perf. level after changed
- *
- * @param prevallocator
- * the allocator before change
- *
- * @param tgtallocator
- * the allocator after change
- */
- public void changed(PerformanceLevel lvl, CommonAllocator<?> prevallocator,
- CommonAllocator<?> tgtallocator);
- }
-
- /**
- * an interface to assist the creation of memory holder.
- *
- * @param <H>
- * the holder type of memory resource
- *
- * @param bma
- * specify an allocator for this holder
- *
- * @param size
- * specify the size of this memory resource
- *
- * @return the holder created
- */
- private interface MemCreate<H extends MemHolder<? extends CommonAllocator<?>, ?, ?>> {
- public H create(CommonAllocator<?> bma, long size);
- }
-
-
- private MemCreate<MemChunkHolder<?>> m_memchunkcreate = new MemCreate<MemChunkHolder<?>>() {
- @Override
- public MemChunkHolder<?> create(CommonAllocator<?> bma, long size) {
- return bma.createChunk(size);
- }
- };
-
- private MemCreate<MemBufferHolder<?>> m_membuffercreate = new MemCreate<MemBufferHolder<?>>() {
- @Override
- public MemBufferHolder<?> create(CommonAllocator<?> bma, long size) {
- return bma.createBuffer(size);
- }
- };
-
- /**
- * performance level categories.
- *
- */
- public static enum PerformanceLevel {
- FASTEST, FAST, NORMAL, SLOW, SLOWEST
- }
-
- /**
- * configuration for each allocator node.
- *
- */
- public static class NodeConfig<A extends CommonAllocator<A>> {
- private A m_allocator;
- private PerformanceLevel m_level;
-
- /**
- * Constructor: initialize this instance with specified allocator and perf. level.
- *
- * @param a
- * specify an allocator for this node
- *
- * @param l
- * specify a performance level
- */
- public NodeConfig(A a, PerformanceLevel l) {
- m_allocator = a;
- m_level = l;
- }
-
- /**
- * retrieve the allocator of this node.
- *
- * @return allocator of this node
- */
- public A getAllocator() {
- return m_allocator;
- }
-
- /**
- * retrieve the performance level of this node.
- *
- * @return level of this node
- */
- public PerformanceLevel getPerformanceLevel() {
- return m_level;
- }
- }
-
- private PerformanceLevelChange m_bwlvlchange = null;
- private AllocatorChange m_allocatorChange = null;
- private PerformanceLevel m_prevbwlevel = null;
- private CommonAllocator<?> m_prevallocator = null;
-
-
- private Map<PerformanceLevel, List<CommonAllocator<?>>> m_info;
-
- /**
- * Constructor: initialize a memory clustering instance.
- *
- * @param ncs
- * specify a set of node with specified configuration respectively
- */
- public MemClustering(NodeConfig<? extends CommonAllocator<?>>[] ncs) {
- m_info = new EnumMap<PerformanceLevel, List<CommonAllocator<?>>>(
- PerformanceLevel.class);
- for (PerformanceLevel lvl : PerformanceLevel.values()) {
- m_info.put(lvl, new ArrayList<CommonAllocator<?>>());
- }
-
- for (NodeConfig<? extends CommonAllocator<?>> nc : ncs) {
- m_info.get(nc.getPerformanceLevel()).add(nc.getAllocator());
- }
-
- }
-
- /**
- * set a callback of event for performance level change.
- *
- * @param bwlvlchange
- * specify a callback object for perf. level change
- */
- public void setPerformanceLevelChange(PerformanceLevelChange bwlvlchange) {
- m_bwlvlchange = bwlvlchange;
- }
-
- /**
- * set a callback of event for allocator change.
- *
- * @param allocatorChange
- * specify a callback object for allocator change
- */
- public <A extends CommonAllocator<A>> void setAllocatorChange(AllocatorChange allocatorChange) {
- m_allocatorChange = allocatorChange;
- }
-
- /**
- * a factory to create memory resource.
- *
- * @param <H> the type of holder
- *
- * @param creator
- * specify a creator to delegate concrete holder creation
- *
- * @param startlevel
- * specify a perf. level that is the start level for memory resource
- * allocation
- *
- * @param size
- * specify a size to request memory resource
- *
- * @return a new created holder held an allocated memory resource
- */
- protected <H extends MemHolder<? extends CommonAllocator<?>, ?, ?>> H
- create(MemCreate<H> creator, PerformanceLevel startlevel, long size) {
- H ret = null;
- boolean eligible = false;
- for (PerformanceLevel lvl : m_info.keySet()) {
- if (!eligible && startlevel == lvl) {
- eligible = true;
- }
- if (eligible) {
- int distance = 0;
- List<CommonAllocator<?>> bmas = m_info.get(lvl);
- for (CommonAllocator<?> bma : bmas) {
- ret = creator.create(bma, size);
- if (null == ret) {
- distance++;
- } else {
- if (null != m_bwlvlchange && m_prevbwlevel != lvl) {
- m_bwlvlchange.changed(m_prevbwlevel, lvl);
- m_prevbwlevel = lvl;
- }
- if (null != m_allocatorChange && m_prevallocator != bma) {
- m_allocatorChange.changed(lvl, m_prevallocator, bma);
- m_prevallocator = bma;
- }
- break;
- }
- }
- Collections.rotate(bmas, distance);
- }
- if (null != ret) {
- break;
- }
- }
- return ret;
- }
-
- /**
- * create a chunk from this clustering.
- *
- * @param size
- * specify the size of memory chunk
- *
- * @return a holder with a created chunk
- *
- */
- //@Override
- public MemChunkHolder<?> createChunk(long size) {
- return createChunk(PerformanceLevel.FASTEST, size);
- }
-
- /**
- * create a chunk from this clustering
- *
- * @param startlevel
- * specify the perf. level from which to search qualified
- * node.
- *
- * @param size
- * specify the size of memory chunk
- *
- * @return a holder with a created chunk
- */
- public MemChunkHolder<?> createChunk(PerformanceLevel startlevel, long size) {
- return create(m_memchunkcreate, startlevel, size);
- }
-
- /**
- * create a buffer from this clustering.
- *
- * @param size
- * specify the size of memory buffer
- *
- * @return a holder with a created buffer
- *
- */
- //@Override
- public MemBufferHolder<?> createBuffer(long size) {
- return createBuffer(PerformanceLevel.FASTEST, size);
- }
-
- /**
- * create a buffer from this clustering
- *
- * @param startlevel
- * specify the perf. level from which to search qualified
- * node.
- *
- * @param size
- * specify the size of memory buffer
- *
- * @return a holder with a created buffer
- */
- public MemBufferHolder<?> createBuffer(PerformanceLevel startlevel,
- long size) {
- return create(m_membuffercreate, startlevel, size);
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/MemFile.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/MemFile.java b/core/src/main/java/com/intel/bigdatamem/MemFile.java
deleted file mode 100644
index f907604..0000000
--- a/core/src/main/java/com/intel/bigdatamem/MemFile.java
+++ /dev/null
@@ -1,51 +0,0 @@
-
-package com.intel.bigdatamem;
-
-import java.io.File;
-
-/**
- * a memory file that manages its data on native memory storage. Note: this
- * class depends on PMFS, we suggest that NVM library to support this feature in
- * native layer. In addition, the permission of /mnt/pmfs will be set properly.
- *
- *
- */
-public class MemFile extends File {
-
- private static final long serialVersionUID = 6579668848729471173L;
- private String uri, id;
-
- /**
- * initialize the memory file.
- *
- * @param uri
- * specify the location of memory file
- *
- * @param id
- * specify the id of memory file
- */
- public MemFile(String uri, String id) {
- super(uri, id);
- this.uri = uri;
- this.id = id;
- }
-
- /**
- * retrieve the uri of this memory file.
- *
- * @return the uri of memory file
- */
- public String getUri() {
- return this.uri;
- }
-
- /**
- * retrieve the id of this memory file.
- *
- * @return the id of memory file
- */
- public String getId() {
- return this.id;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/MemHolder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/MemHolder.java b/core/src/main/java/com/intel/bigdatamem/MemHolder.java
deleted file mode 100644
index 8b662ea..0000000
--- a/core/src/main/java/com/intel/bigdatamem/MemHolder.java
+++ /dev/null
@@ -1,58 +0,0 @@
-package com.intel.bigdatamem;
-
-import org.flowcomputing.commons.resgc.*;
-
-/**
- * hold a memory kind of resource.
- *
- */
-public abstract class MemHolder<A extends CommonAllocator<A>, T, H extends MemHolder<A, T, H>> extends ResHolder<T, H> {
-
- protected A m_allocator;
-
- /**
- * Constructor: initialize with resource.
- *
- * @param mres
- * specify a resource to be holden
- *
- * @param ar
- * specify an Allocator for this holder
- *
- */
- public MemHolder(T mres, A ar) {
- super(mres);
- m_allocator = ar;
- }
-
- /**
- * get its allocator
- *
- * @return the allocator
- */
- public A getAllocator() {
- return m_allocator;
- }
-
- /**
- * resize its held resource
- *
- * @param size
- * specify the new size for its held resource
- */
- abstract public MemHolder<A, T, H> resize(long size);
-
- /**
- * get the size of its held memory resource.
- *
- * @return the size
- */
- abstract public long getSize();
-
- /**
- * register its held resource for auto-reclaim
- *
- */
- abstract public void registerAutoReclaim();
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/MemoryNonVolatileEntity.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/MemoryNonVolatileEntity.java b/core/src/main/java/com/intel/bigdatamem/MemoryNonVolatileEntity.java
deleted file mode 100644
index 4a2a7c0..0000000
--- a/core/src/main/java/com/intel/bigdatamem/MemoryNonVolatileEntity.java
+++ /dev/null
@@ -1,19 +0,0 @@
-package com.intel.bigdatamem;
-
-/**
- *
- *
- */
-
-
-public interface MemoryNonVolatileEntity<ALLOC_PMem3C93D24F59 extends CommonPersistAllocator<ALLOC_PMem3C93D24F59>> {
-
- public void initializeNonVolatileEntity(ALLOC_PMem3C93D24F59 allocator, EntityFactoryProxy[] efproxys, GenericField.GType[] gfields, boolean autoreclaim);
-
- public void createNonVolatileEntity(ALLOC_PMem3C93D24F59 allocator, EntityFactoryProxy[] efproxys, GenericField.GType[] gfields, boolean autoreclaim)
- throws OutOfPersistentMemory;
-
- public void restoreNonVolatileEntity(ALLOC_PMem3C93D24F59 allocator, EntityFactoryProxy[] efproxys, GenericField.GType[] gfields, long phandler, boolean autoreclaim)
- throws RetrieveNonVolatileEntityError;
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/NonVolatileEntity.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/NonVolatileEntity.java b/core/src/main/java/com/intel/bigdatamem/NonVolatileEntity.java
deleted file mode 100644
index 1ab7fc4..0000000
--- a/core/src/main/java/com/intel/bigdatamem/NonVolatileEntity.java
+++ /dev/null
@@ -1,13 +0,0 @@
-package com.intel.bigdatamem;
-
-/**
- * this class defines a annotation for non-volatile entity
- *
- */
-
-import java.lang.annotation.*;
-
-@Target(ElementType.TYPE) @Retention(RetentionPolicy.CLASS)
-public @interface NonVolatileEntity {
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/NonVolatileEntityProcessor.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/NonVolatileEntityProcessor.java b/core/src/main/java/com/intel/bigdatamem/NonVolatileEntityProcessor.java
deleted file mode 100644
index 7acc172..0000000
--- a/core/src/main/java/com/intel/bigdatamem/NonVolatileEntityProcessor.java
+++ /dev/null
@@ -1,134 +0,0 @@
-package com.intel.bigdatamem;
-
-/**
- * a non-volatile annotation processor
- *
- */
-
-import java.io.IOException;
-import java.util.LinkedHashSet;
-import java.util.Set;
-
-import javax.annotation.processing.*;
-import javax.lang.model.SourceVersion;
-import javax.lang.model.element.Element;
-import javax.lang.model.element.ElementKind;
-import javax.lang.model.element.ExecutableElement;
-import javax.lang.model.element.TypeElement;
-import javax.lang.model.element.VariableElement;
-import javax.lang.model.type.DeclaredType;
-import javax.lang.model.type.TypeMirror;
-import javax.lang.model.util.ElementFilter;
-import javax.lang.model.util.Elements;
-import javax.lang.model.util.Types;
-
-import javax.tools.Diagnostic;
-
-import com.squareup.javapoet.MethodSpec;
-
-public class NonVolatileEntityProcessor extends AbstractProcessor {
- private Types typeUtils;
- private Elements elementUtils;
- private Filer filer;
- private Messager messager;
- // private Map<String, FactoryGroupedClasses> factoryClasses = new
- // LinkedHashMap<String, FactoryGroupedClasses>();
-
- /**
- * {@inheritDoc}
- */
- @Override
- public synchronized void init(ProcessingEnvironment processingEnv) {
- super.init(processingEnv);
- typeUtils = processingEnv.getTypeUtils();
- elementUtils = processingEnv.getElementUtils();
- filer = processingEnv.getFiler();
- messager = processingEnv.getMessager();
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public Set<String> getSupportedAnnotationTypes() {
- Set<String> annotataions = new LinkedHashSet<String>();
- annotataions.add(NonVolatileEntity.class.getCanonicalName());
- return annotataions;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public SourceVersion getSupportedSourceVersion() {
- return SourceVersion.latestSupported();
- }
-
- /**
- * triggered if an error issued during processing
- *
- * @param e
- * the element in question
- *
- * @param msg
- * the message issued
- */
- public void error(Element e, String msg) {
- messager.printMessage(Diagnostic.Kind.ERROR, msg, e);
- }
-
- /**
- * triggered if a note issued during processing
- *
- * @param e
- * the element in question
- *
- * @param msg
- * the message issued
- */
- public void note(Element e, String msg) {
- messager.printMessage(Diagnostic.Kind.NOTE, msg, e);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
-
- try {
-
- for (Element annotatedElement : roundEnv.getElementsAnnotatedWith(NonVolatileEntity.class)) {
-
- String outputstr = String.format("++++++++++%s+++++++++++", annotatedElement.getSimpleName());
- note(annotatedElement, outputstr);
- // System.err.println(outputstr);
-
- if (annotatedElement.getKind() != ElementKind.CLASS) {
- throw new AnnotationProcessingException(
- annotatedElement, "Only classes can be annotated with @%s",
- NonVolatileEntity.class.getSimpleName());
- }
-
- // We can cast it, because we know that it of ElementKind.CLASS
- TypeElement typeelem = (TypeElement) annotatedElement;
-
- AnnotatedNonVolatileEntityClass annotatedClass =
- new AnnotatedNonVolatileEntityClass(typeelem, typeUtils, elementUtils, messager);
-
- annotatedClass.prepareProcessing();
-
- annotatedClass.generateCode(filer);
-
- }
-
- } catch (AnnotationProcessingException e) {
- error(e.getElement(), e.getMessage());
- } catch (IOException e) {
- error(null, e.getMessage());
- }
-
- return true;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/NonVolatileGetter.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/NonVolatileGetter.java b/core/src/main/java/com/intel/bigdatamem/NonVolatileGetter.java
deleted file mode 100644
index e4ad7f7..0000000
--- a/core/src/main/java/com/intel/bigdatamem/NonVolatileGetter.java
+++ /dev/null
@@ -1,18 +0,0 @@
-package com.intel.bigdatamem;
-
-/**
- * this class defines an annotation for getter methods of non-volatile entity
- *
- */
-
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
-
-@Target(ElementType.METHOD) @Retention(RetentionPolicy.CLASS)
-public @interface NonVolatileGetter {
- String EntityFactoryProxies() default "null";
- String GenericFieldTypes() default "null";
- long Id() default -1L;
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/NonVolatileSetter.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/NonVolatileSetter.java b/core/src/main/java/com/intel/bigdatamem/NonVolatileSetter.java
deleted file mode 100644
index 4d9cea7..0000000
--- a/core/src/main/java/com/intel/bigdatamem/NonVolatileSetter.java
+++ /dev/null
@@ -1,16 +0,0 @@
-package com.intel.bigdatamem;
-
-/**
- * this class defines an annotation for setter methods of non-volatile entity
- *
- */
-
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
-
-@Target(ElementType.METHOD) @Retention(RetentionPolicy.CLASS)
-public @interface NonVolatileSetter {
-
-}
[3/7] incubator-mnemonic git commit: MNEMONIC-5 MNEMONIC-6 #resolve
refactored package names
Posted by ga...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/BigDataMemAllocator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/BigDataMemAllocator.java b/core/src/main/java/org/apache/mnemonic/BigDataMemAllocator.java
new file mode 100644
index 0000000..e663540
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/BigDataMemAllocator.java
@@ -0,0 +1,278 @@
+package org.apache.mnemonic;
+
+import java.nio.ByteBuffer;
+
+import org.flowcomputing.commons.resgc.*;
+import org.flowcomputing.commons.primitives.*;
+import org.apache.mnemonic.service.allocatorservice.VolatileMemoryAllocatorService;
+
+/**
+ * manage a big native memory pool through libvmem.so that is provied by Intel nvml library.
+ *
+ *
+ */
+public class BigDataMemAllocator extends CommonAllocator<BigDataMemAllocator> {
+
+ private boolean m_activegc = true;
+ private long m_gctimeout = 100;
+ private long m_nid = -1;
+ private VolatileMemoryAllocatorService m_vmasvc = null;
+
+ /**
+ * Constructor, it initializes and allocate a memory pool from specified uri
+ * location with specified capacity and an allocator service instance.
+ * usually, the uri points to a mounted
+ * memory device or a location of file system.
+ *
+ * @param vmasvc
+ * the volatile memory allocation service instance
+ *
+ * @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
+ */
+ public BigDataMemAllocator(VolatileMemoryAllocatorService vmasvc, long capacity, String uri, boolean isnew) {
+ assert null != vmasvc : "VolatileMemoryAllocatorService object is null";
+ if (capacity <= 0) {
+ throw new IllegalArgumentException("BigDataMemAllocator cannot be initialized with capacity <= 0.");
+ }
+
+ m_vmasvc = vmasvc;
+ m_nid = m_vmasvc.init(capacity, uri, isnew);
+
+ /**
+ * create a resource collector to release specified bytebuffer that backed
+ * by underlying big memory pool.
+ */
+ m_bufcollector = new ResCollector<MemBufferHolder<BigDataMemAllocator>, ByteBuffer>(
+ new ResReclaim<ByteBuffer>() {
+ @Override
+ public void reclaim(ByteBuffer mres) {
+ boolean cb_reclaimed = false;
+ if (null != m_bufferreclaimer) {
+ cb_reclaimed = m_bufferreclaimer.reclaim(mres, Long.valueOf(mres.capacity()));
+ }
+ if (!cb_reclaimed) {
+ m_vmasvc.destroyByteBuffer(m_nid, mres);
+ mres = null;
+ }
+ }
+ });
+
+ /**
+ * create a resource collector to release specified chunk that backed by
+ * underlying big memory pool.
+ */
+ m_chunkcollector = new ResCollector<MemChunkHolder<BigDataMemAllocator>, Long>(
+ new ResReclaim<Long>() {
+ @Override
+ public void reclaim(Long mres) {
+ // System.out.println(String.format("Reclaim: %X", mres));
+ boolean cb_reclaimed = false;
+ if (null != m_chunkreclaimer) {
+ cb_reclaimed = m_chunkreclaimer.reclaim(mres, null);
+ }
+ if (!cb_reclaimed) {
+ m_vmasvc.free(m_nid, mres);
+ mres = null;
+ }
+ }
+ });
+ }
+
+ /**
+ * 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 BigDataMemAllocator enableActiveGC(long timeout) {
+ m_activegc = true;
+ m_gctimeout = timeout;
+ return this;
+ }
+
+ /**
+ * disable active garbage collection.
+ *
+ * @return this allocator
+ */
+ @Override
+ public BigDataMemAllocator 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<BigDataMemAllocator> resizeChunk(MemChunkHolder<BigDataMemAllocator> mholder, long size){
+ MemChunkHolder<BigDataMemAllocator> ret = null;
+ boolean ac = null != mholder.getRefId();
+ if (size > 0) {
+ Long addr = m_vmasvc.reallocate(m_nid, mholder.get(), size, true);
+ if (0 == addr && m_activegc) {
+ forceGC();
+ addr = m_vmasvc.reallocate(m_nid, mholder.get(), size, true);
+ }
+ if (0 != addr) {
+ mholder.clear();
+ mholder.destroy();
+ ret = new MemChunkHolder<BigDataMemAllocator>(this, addr, size);
+ if (ac) {
+ m_chunkcollector.register(ret);
+ }
+ }
+ }
+ return ret;
+ }
+
+ /**
+ * 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<BigDataMemAllocator> resizeBuffer(MemBufferHolder<BigDataMemAllocator> mholder, long size) {
+ MemBufferHolder<BigDataMemAllocator> ret = null;
+ boolean ac = null != mholder.getRefId();
+ if (size > 0) {
+ int bufpos = mholder.get().position();
+ int buflimit = mholder.get().limit();
+ ByteBuffer buf = m_vmasvc.resizeByteBuffer(m_nid, mholder.get(), size);
+ if (null == buf && m_activegc) {
+ forceGC();
+ buf = m_vmasvc.resizeByteBuffer(m_nid, 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<BigDataMemAllocator>(this, buf);
+ if (ac) {
+ m_bufcollector.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<BigDataMemAllocator> createChunk(long size, boolean autoreclaim) {
+ MemChunkHolder<BigDataMemAllocator> ret = null;
+ Long addr = m_vmasvc.allocate(m_nid, size, true);
+ if (0 == addr && m_activegc) {
+ forceGC();
+ addr = m_vmasvc.allocate(m_nid, size, true);
+ }
+ if (0 != addr) {
+ ret = new MemChunkHolder<BigDataMemAllocator>(this, addr, size);
+ ret.setCollector(m_chunkcollector);
+ if (autoreclaim) {
+ m_chunkcollector.register(ret);
+ }
+ }
+ return ret;
+ }
+
+ /**
+ * create a memory buffer that is managed by its holder.
+ *
+ * @param size
+ * specify the size of memory buffer
+ *
+ * @param autoreclaim
+ * specify whether or not to reclaim this
+ * buffer automatically
+ *
+ * @return a holder contains a memory buffer
+ */
+ @Override
+ public MemBufferHolder<BigDataMemAllocator> createBuffer(long size, boolean autoreclaim) {
+ MemBufferHolder<BigDataMemAllocator> ret = null;
+ ByteBuffer bb = m_vmasvc.createByteBuffer(m_nid, size);
+ if (null == bb && m_activegc) {
+ forceGC();
+ bb = m_vmasvc.createByteBuffer(m_nid, size);
+ }
+ if (null != bb) {
+ ret = new MemBufferHolder<BigDataMemAllocator>(this, bb);
+ ret.setCollector(m_bufcollector);
+ if (autoreclaim) {
+ m_bufcollector.register(ret);
+ }
+ }
+ return ret;
+ }
+
+ /**
+ * force to perform GC that is used to release unused backed memory
+ * resources.
+ */
+ private void forceGC() {
+ System.gc();
+ try {
+ Thread.sleep(m_gctimeout);
+ } catch (Exception ex) {
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/BigDataPMemAllocator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/BigDataPMemAllocator.java b/core/src/main/java/org/apache/mnemonic/BigDataPMemAllocator.java
new file mode 100644
index 0000000..aac30e3
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/BigDataPMemAllocator.java
@@ -0,0 +1,469 @@
+package org.apache.mnemonic;
+
+import java.nio.ByteBuffer;
+
+import org.flowcomputing.commons.resgc.*;
+import org.flowcomputing.commons.primitives.*;
+import org.apache.mnemonic.service.allocatorservice.NonVolatileMemoryAllocatorService;
+
+/**
+ * manage a big native persistent memory pool through libpmalloc.so provided by pmalloc project.
+ *
+ *
+ */
+public class BigDataPMemAllocator extends CommonPersistAllocator<BigDataPMemAllocator> implements PMAddressTranslator{
+
+ private boolean m_activegc = true;
+ private long m_gctimeout = 100;
+ private long m_nid = -1;
+ private long b_addr = 0;
+ private NonVolatileMemoryAllocatorService m_nvmasvc = null;
+
+ /**
+ * Constructor, it initializes and allocate a memory pool from specified uri
+ * location with specified capacity and an allocator service instance.
+ * usually, the uri points to a mounted
+ * non-volatile memory device or a location of file system.
+ *
+ * @param nvmasvc
+ * the non-volatile memory allocation service instance
+ *
+ * @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
+ */
+ public BigDataPMemAllocator(NonVolatileMemoryAllocatorService nvmasvc, long capacity, String uri, boolean isnew) {
+ assert null != nvmasvc : "NonVolatileMemoryAllocatorService object is null";
+ if (capacity <= 0) {
+ throw new IllegalArgumentException("BigDataPMemAllocator cannot be initialized with capacity <= 0.");
+ }
+
+ m_nvmasvc = nvmasvc;
+
+ m_nid = m_nvmasvc.init(capacity, uri, isnew);
+ b_addr = m_nvmasvc.getBaseAddress(m_nid);
+
+ /**
+ * create a resource collector to release specified chunk that backed by
+ * underlying big memory pool.
+ */
+ m_chunkcollector = new ResCollector<MemChunkHolder<BigDataPMemAllocator>, Long>(new ResReclaim<Long>() {
+ @Override
+ public void reclaim(Long mres) {
+ // System.out.println(String.format("Reclaim: %X", mres));
+ boolean cb_reclaimed = false;
+ if (null != m_chunkreclaimer) {
+ cb_reclaimed = m_chunkreclaimer.reclaim(mres, null);
+ }
+ if (!cb_reclaimed) {
+ m_nvmasvc.free(m_nid, mres);
+ mres = null;
+ }
+ }
+ });
+
+ /**
+ * create a resource collector to release specified bytebuffer that backed
+ * by underlying big memory pool.
+ */
+ m_bufcollector = new ResCollector<MemBufferHolder<BigDataPMemAllocator>, ByteBuffer>(
+ new ResReclaim<ByteBuffer>() {
+ @Override
+ public void reclaim(ByteBuffer mres) {
+ boolean cb_reclaimed = false;
+ if (null != m_bufferreclaimer) {
+ cb_reclaimed = m_bufferreclaimer.reclaim(mres, Long.valueOf(mres.capacity()));
+ }
+ if (!cb_reclaimed) {
+ m_nvmasvc.destroyByteBuffer(m_nid, mres);
+ mres = null;
+ }
+ }
+ });
+ }
+
+ /**
+ * 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
+ */
+ @Override
+ public BigDataPMemAllocator enableActiveGC(long timeout) {
+ m_activegc = true;
+ m_gctimeout = timeout;
+ return this;
+ }
+
+ /**
+ * disable active garbage collection.
+ *
+ */
+ @Override
+ public BigDataPMemAllocator disableActiveGC() {
+ m_activegc = false;
+ return this;
+ }
+
+ /**
+ * Release the memory pool and close it.
+ *
+ */
+ @Override
+ public void close() {
+ forceGC();
+ super.close();
+ m_nvmasvc.close(m_nid);
+ }
+
+ /**
+ * 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<BigDataPMemAllocator> resizeChunk(MemChunkHolder<BigDataPMemAllocator> mholder, long size){
+ MemChunkHolder<BigDataPMemAllocator> ret = null;
+ boolean ac = null != mholder.getRefId();
+ if (size > 0) {
+ Long addr = m_nvmasvc.reallocate(m_nid, mholder.get(), size, true);
+ if (0 == addr && m_activegc) {
+ forceGC();
+ addr = m_nvmasvc.reallocate(m_nid, mholder.get(), size, true);
+ }
+ if (0 != addr) {
+ mholder.clear();
+ mholder.destroy();
+ ret = new MemChunkHolder<BigDataPMemAllocator>(this, addr, size);
+ if (ac) {
+ m_chunkcollector.register(ret);
+ }
+ }
+ }
+ return ret;
+ }
+
+ /**
+ * 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<BigDataPMemAllocator> resizeBuffer(MemBufferHolder<BigDataPMemAllocator> mholder, long size) {
+ MemBufferHolder<BigDataPMemAllocator> ret = null;
+ boolean ac = null != mholder.getRefId();
+ if (size > 0) {
+ int bufpos = mholder.get().position();
+ int buflimit = mholder.get().limit();
+ ByteBuffer buf = m_nvmasvc.resizeByteBuffer(m_nid, mholder.get(), size);
+ if (null == buf && m_activegc) {
+ forceGC();
+ buf = m_nvmasvc.resizeByteBuffer(m_nid, 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<BigDataPMemAllocator>(this, buf);
+ if (ac) {
+ m_bufcollector.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<BigDataPMemAllocator> createChunk(long size, boolean autoreclaim) {
+ MemChunkHolder<BigDataPMemAllocator> ret = null;
+ Long addr = m_nvmasvc.allocate(m_nid, size, true);
+ if ((null == addr || 0 == addr) && m_activegc) {
+ forceGC();
+ addr = m_nvmasvc.allocate(m_nid, size, true);
+ }
+ if (null != addr && 0 != addr) {
+ ret = new MemChunkHolder<BigDataPMemAllocator>(this, addr, size);
+ ret.setCollector(m_chunkcollector);
+ if (autoreclaim) {
+ m_chunkcollector.register(ret);
+ }
+ }
+ return ret;
+ }
+
+ /**
+ * create a memory buffer that is managed by its holder.
+ *
+ * @param size
+ * specify the size of memory buffer
+ *
+ * @param autoreclaim
+ * specify whether or not to reclaim this
+ * buffer automatically
+ *
+ * @return a holder contains a memory buffer
+ */
+ @Override
+ public MemBufferHolder<BigDataPMemAllocator> createBuffer(long size, boolean autoreclaim) {
+ MemBufferHolder<BigDataPMemAllocator> ret = null;
+ ByteBuffer bb = m_nvmasvc.createByteBuffer(m_nid, size);
+ if (null == bb && m_activegc) {
+ forceGC();
+ bb = m_nvmasvc.createByteBuffer(m_nid, size);
+ }
+ if (null != bb) {
+ ret = new MemBufferHolder<BigDataPMemAllocator>(this, bb);
+ ret.setCollector(m_bufcollector);
+ if (autoreclaim) {
+ m_bufcollector.register(ret);
+ }
+ }
+ return ret;
+ }
+
+ /**
+ * retrieve a memory buffer from its backed memory allocator.
+ *
+ * @param phandler
+ * specify the handler of memory buffer to retrieve
+ *
+ * @param autoreclaim
+ * specify whether this retrieved memory buffer can be reclaimed automatically or not
+ *
+ * @return a holder contains the retrieved memory buffer
+ */
+ @Override
+ public MemBufferHolder<BigDataPMemAllocator> retrieveBuffer(long phandler, boolean autoreclaim) {
+ MemBufferHolder<BigDataPMemAllocator> ret = null;
+ ByteBuffer bb = m_nvmasvc.retrieveByteBuffer(m_nid, getEffectiveAddress(phandler));
+ if (null != bb) {
+ ret = new MemBufferHolder<BigDataPMemAllocator>(this, bb);
+ if (autoreclaim) {
+ m_bufcollector.register(ret);
+ }
+ }
+ return ret;
+ }
+
+ /**
+ * retrieve a memory chunk from its backed memory allocator.
+ *
+ * @param phandler
+ * specify the handler of memory chunk to retrieve
+ *
+ * @param autoreclaim
+ * specify whether this retrieved memory chunk can be reclaimed automatically or not
+ *
+ * @return a holder contains the retrieved memory chunk
+ */
+ @Override
+ public MemChunkHolder<BigDataPMemAllocator> retrieveChunk(long phandler, boolean autoreclaim) {
+ MemChunkHolder<BigDataPMemAllocator> ret = null;
+ long eaddr = getEffectiveAddress(phandler);
+ long sz = m_nvmasvc.retrieveSize(m_nid, eaddr);
+ if (sz > 0L) {
+ ret = new MemChunkHolder<BigDataPMemAllocator>(this, eaddr, sz);
+ if (autoreclaim) {
+ m_chunkcollector.register(ret);
+ }
+ }
+ return ret;
+ }
+
+ /**
+ * get the handler from a memory buffer holder.
+ *
+ * @param mbuf
+ * specify the memory buffer holder
+ *
+ * @return a handler that could be used to retrieve its memory buffer
+ */
+ @Override
+ public long getBufferHandler(MemBufferHolder<BigDataPMemAllocator> mbuf) {
+ return getPortableAddress(m_nvmasvc.getByteBufferHandler(m_nid, mbuf.get()));
+ }
+
+ /**
+ * get the handler from a memory chunk holder.
+ *
+ * @param mchunk
+ * specify the memory chunk holder
+ *
+ * @return a handler that could be used to retrieve its memory chunk
+ */
+ @Override
+ public long getChunkHandler(MemChunkHolder<BigDataPMemAllocator> mchunk) {
+ return getPortableAddress(mchunk.get());
+ }
+
+ /**
+ * determine whether this allocator supports to store non-volatile handler or not.
+ * (it is a placeholder)
+ *
+ * @return true if there is
+ */
+ @Override
+ public boolean hasNonVolatileHandlerStore() {
+ return true;
+ }
+
+ /**
+ * start a application level transaction on this allocator.
+ * (it is a place holder)
+ *
+ */
+ @Override
+ public void beginTransaction() {
+ throw new UnsupportedOperationException("Transaction Unsupported.");
+ }
+
+ /**
+ * end a application level transaction on this allocator.
+ * (it is a place holder)
+ *
+ */
+ @Override
+ public void endTransaction() {
+ throw new UnsupportedOperationException("Transaction Unsupported.");
+ }
+
+ /**
+ * set a handler on key.
+ *
+ * @param key
+ * the key to set its value
+ *
+ * @param handler
+ * the handler
+ */
+ public void setHandler(long key, long handler) {
+ m_nvmasvc.setHandler(m_nid, key, handler);
+ }
+
+ /**
+ * get a handler value.
+ *
+ * @param key
+ * the key to set its value
+ *
+ * @return the value of handler
+ */
+ public long getHandler(long key) {
+ return m_nvmasvc.getHandler(m_nid, key);
+ }
+
+ /**
+ * return the capacity of non-volatile handler store.
+ *
+ * @return the capacity of handler store
+ *
+ */
+ public long handlerCapacity() {
+ return m_nvmasvc.handlerCapacity(m_nid);
+ }
+
+ /**
+ * force to perform GC that is used to re-claim garbages objects
+ * as well as memory resources managed by this allocator.
+ *
+ */
+ private void forceGC() {
+ System.gc();
+ try {
+ Thread.sleep(m_gctimeout);
+ } catch (Exception ex) {
+ }
+ }
+
+ /**
+ * calculate the portable address
+ *
+ * @param addr
+ * the address to be calculated
+ *
+ * @return the portable address
+ */
+ @Override
+ public long getPortableAddress(long addr) {
+ return addr - b_addr;
+ }
+
+ /**
+ * calculate the effective address
+ *
+ * @param addr
+ * the address to be calculated
+ *
+ * @return the effective address
+ */
+ @Override
+ public long getEffectiveAddress(long addr) {
+ return addr + b_addr;
+ }
+
+ /**
+ * get the base address
+ *
+ * @return the base address
+ */
+ @Override
+ public long getBaseAddress() {
+ return b_addr;
+ }
+
+ /**
+ * set the base address for calculation
+ *
+ * @param addr
+ * the base address
+ *
+ */
+ @Override
+ public long setBaseAddress(long addr) {
+ return b_addr = addr;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/ByteBufferBackedInputStream.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/ByteBufferBackedInputStream.java b/core/src/main/java/org/apache/mnemonic/ByteBufferBackedInputStream.java
new file mode 100644
index 0000000..046472b
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/ByteBufferBackedInputStream.java
@@ -0,0 +1,61 @@
+package org.apache.mnemonic;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+
+/**
+ * an input Stream that is backed by a in-memory ByteBuffer.
+ *
+ *
+ */
+public class ByteBufferBackedInputStream extends InputStream {
+
+ private ByteBuffer buf;
+
+ /**
+ * accept a ByteBuffer as backed object for inputStream.
+ *
+ * @param buf
+ * specify a bytebuffer that is where any data is from
+ */
+ public ByteBufferBackedInputStream(ByteBuffer buf) {
+ this.buf = buf;
+ }
+
+ /**
+ * read an integer value from backed ByteBuffer.
+ *
+ * @return a integer value from stream input
+ */
+ public int read() throws IOException {
+ if (!buf.hasRemaining()) {
+ return -1;
+ }
+ return buf.get() & 0xFF;
+ }
+
+ /**
+ * read a specified range of byte array from backed ByteBuffer.
+ *
+ * @param bytes
+ * specify a output byte array to store data
+ *
+ * @param off
+ * specify the offset from ByteBuffer to read
+ *
+ * @param len
+ * specify the length of bytes to read
+ *
+ * @return the number of bytes has been read
+ */
+ public int read(byte[] bytes, int off, int len) throws IOException {
+ if (!buf.hasRemaining()) {
+ return -1;
+ }
+
+ len = Math.min(len, buf.remaining());
+ buf.get(bytes, off, len);
+ return len;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/ByteBufferBackedOutputStream.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/ByteBufferBackedOutputStream.java b/core/src/main/java/org/apache/mnemonic/ByteBufferBackedOutputStream.java
new file mode 100644
index 0000000..6755444
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/ByteBufferBackedOutputStream.java
@@ -0,0 +1,56 @@
+package org.apache.mnemonic;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+
+/**
+ * an output Stream that is backed by a in-memory ByteBuffer.
+ *
+ *
+ */
+public class ByteBufferBackedOutputStream extends OutputStream {
+
+ private ByteBuffer buf;
+
+ /**
+ * accept a ByteBuffer to store external data, the capacity of it could be
+ * extended at will.
+ *
+ * @param buf
+ * specify a ByteBuffer object that is used to store external
+ * data to its backed buffer
+ *
+ */
+ public ByteBufferBackedOutputStream(ByteBuffer buf) {
+ this.buf = buf;
+ }
+
+ /**
+ * write an integer value to backed buffer.
+ *
+ * @param b
+ * specify an integer value to be written
+ */
+ public void write(int b) throws IOException {
+ buf.put((byte) b);
+ }
+
+ /**
+ * write an array of bytes to a specified range of backed buffer
+ *
+ * @param bytes
+ * specify a byte array to write
+ *
+ * @param off
+ * specify the offset of backed buffer where is start point to be
+ * written
+ *
+ * @param len
+ * specify the length of bytes to be written
+ */
+ public void write(byte[] bytes, int off, int len) throws IOException {
+ buf.put(bytes, off, len);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/ByteBufferSerializer.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/ByteBufferSerializer.java b/core/src/main/java/org/apache/mnemonic/ByteBufferSerializer.java
new file mode 100644
index 0000000..7ed9f05
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/ByteBufferSerializer.java
@@ -0,0 +1,149 @@
+package org.apache.mnemonic;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.nio.ByteBuffer;
+import java.io.InputStream;
+
+/**
+ * a serializer class that manages to serialize any serilizable objects from/to
+ * ByteBuffer.
+ *
+ */
+
+public class ByteBufferSerializer {
+
+ /**
+ * serialize object to a ByteBuffer object.
+ *
+ * @param <ValueT>
+ * the type of object
+ *
+ * @param obj
+ * specify a object that is serializable
+ *
+ * @throws IOException
+ * the exception of serialization
+ *
+ * @return a ByteBuffer object contained serialized object
+ */
+ public static <ValueT> ByteBuffer toByteBuffer(ValueT obj)
+ throws IOException {
+ byte[] bytes = null;
+ ByteArrayOutputStream bos = null;
+ ObjectOutputStream oos = null;
+ try {
+ bos = new ByteArrayOutputStream();
+ oos = new ObjectOutputStream(bos);
+ oos.writeObject(obj);
+ oos.flush();
+ bytes = bos.toByteArray();
+ } finally {
+ if (oos != null) {
+ oos.close();
+ }
+ if (bos != null) {
+ bos.close();
+ }
+ }
+ return ByteBuffer.wrap(bytes);
+ }
+
+ /**
+ * de-serialize an object from a ByteBuffer object.
+ *
+ * @param <ValueT>
+ * the type of object
+ *
+ * @param bf
+ * specify an ByteBuffer contains data that can be de-serialized
+ *
+ * @throws IOException
+ * the exception of deserialization
+ *
+ * @throws ClassNotFoundException
+ * Not found class of de-serialized object
+ *
+ * @return a de-serialized object
+ */
+ @SuppressWarnings("unchecked")
+ public static <ValueT> ValueT toObject(ByteBuffer bf) throws IOException,
+ ClassNotFoundException {
+ Object obj = null;
+ InputStream is = null;
+ ObjectInputStream ois = null;
+ try {
+ is = new ByteBufferBackedInputStream(bf);
+ ois = new ObjectInputStream(is);
+ obj = ois.readObject();
+ } finally {
+ if (is != null) {
+ is.close();
+ }
+ if (ois != null) {
+ ois.close();
+ }
+ }
+ return (ValueT) obj;
+ }
+
+ /**
+ * serialize object to a MemBufferHolder object.
+ *
+ * @param <A> the type of bound allocator
+ *
+ * @param <ValueT>
+ * the type of object
+ *
+ * @param ar
+ * specify an Allocator that is used to generate MemBufferHolder
+ * which is backed by a native memory block
+ *
+ * @param obj
+ * specify a object that is serializable
+ *
+ * @throws IOException
+ * the exception of serialization
+ *
+ * @return a MemBufferHolder object contained serialized object
+ */
+ public static <A extends CommonAllocator<A>, ValueT> MemBufferHolder<A>
+ toMemBufferHolder(A ar, ValueT obj) throws IOException {
+ MemBufferHolder<A> ret = null;
+ ByteBuffer bb = toByteBuffer(obj);
+ if (null != bb && bb.remaining() > 0) {
+ ret = ar.createBuffer(bb.remaining());
+ ret.get().put(bb);
+ ret.get().flip();
+ }
+ return ret;
+ }
+
+ /**
+ * de-serialize an object from a MemBufferHolder object.
+ *
+ * @param <A> the type of bound allocator
+ *
+ * @param <ValueT>
+ * the type of object
+ *
+ * @param mbh
+ * specify an MemBufferHolder who contains data that can be
+ * de-serialized
+ *
+ * @throws IOException
+ * the exception of deserialization
+ *
+ * @throws ClassNotFoundException
+ * Not found class of de-serialized object
+ *
+ * @return a de-serialized object
+ */
+ public static <A extends CommonAllocator<A>, ValueT> ValueT
+ fromMemBufferHolder(MemBufferHolder<A> mbh)
+ throws IOException, ClassNotFoundException {
+ return toObject(mbh.get());
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/CachePool.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/CachePool.java b/core/src/main/java/org/apache/mnemonic/CachePool.java
new file mode 100644
index 0000000..8a9af75
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/CachePool.java
@@ -0,0 +1,144 @@
+package org.apache.mnemonic;
+
+import java.util.Set;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * This is a abstract CachePool class that is inherited from LinedHashMap class,
+ * it extends functionalities of its parent class to support some new features
+ * that is usually requried in data caching usage scenario.
+ *
+ *
+ */
+public abstract class CachePool<KeyT, ValueT> extends
+ LinkedHashMap<KeyT, ValueT> {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = 1L;
+
+ public CachePool(int initialCapacity, float loadFactor) {
+ super(initialCapacity, loadFactor);
+ }
+
+ /**
+ * Return available capacity for new entries.
+ *
+ * @return available capacity in this cache pool
+ */
+ public abstract long freeCapacity();
+
+ /**
+ * Return the used capacity of cached entries.
+ *
+ * @return the used size of this cache pool
+ */
+ public abstract long usedCapacity();
+
+ /**
+ * Put an entry in this cache pool and evict eldest entries if necessary
+ * that will free enough space to hold new entry, which entry could be
+ * evicted that can be customized by ({@link EvictFilter}), regarding how
+ * to post-process the eldest entry that can be customized by (
+ * {@link DropEvent}). If this cache pool previously contained an entry for
+ * the key, the old value will be replaced by the specified value
+ *
+ * @param k
+ * the key whoes associated value is to be put
+ *
+ * @param v
+ * the value to be put
+ *
+ * @param fsop
+ * the customized operations to free space to hold new entry
+ *
+ * @param dfilter
+ * the filter of entries for deletion
+ *
+ * @return <tt>true</tt> if the entry has been put into this container
+ *
+ */
+ public abstract ValueT put(KeyT k, ValueT v, DropEvent<KeyT, ValueT> fsop,
+ EvictFilter<KeyT, ValueT> dfilter);
+
+ /**
+ * Put all entries into this cache pool and evict eldes entries if
+ * necessary.
+ *
+ * @param m
+ * the Map object that contains entries to be put
+ *
+ * @param fsop
+ * the customized operations to free space to hold new entry
+ *
+ * @param dfilter
+ * the filter of entries for deletion
+ *
+ */
+ public abstract void putAll(
+ Map<? extends KeyT, ? extends ValueT> m,
+ DropEvent<KeyT, ValueT> fsop,
+ EvictFilter<KeyT, ValueT> dfilter);
+
+ /**
+ * Returns a new {@link Set} view of the keys of this cache pool, It
+ * contains the most recently visited keys
+ *
+ * @param n
+ * the number of keys to retrieve
+ *
+ * @return a set of hot keys
+ */
+ public abstract Set<KeyT> hotKeySet(int n);
+
+ /**
+ * Ensure the free capacity is greater than the specified size
+ *
+ * @param freesz
+ * the size of free capacity that needs to be secured
+ *
+ * @return <tt>true</tt> if the size of free capacity is greater than the
+ * specified size after evacuation
+ *
+ * @see #ensureFreeSpace(long, DropEvent, EvictFilter)
+ */
+ public abstract boolean ensureFreeSpace(long freesz);
+
+ /**
+ * Removes a first qualified entry in this cache pool
+ *
+ * @param fsop
+ * the customized callback to post-process its evicted entry
+ *
+ * @param dfilter
+ * the filter for entry deletion
+ *
+ * @return <tt>true</tt> if there is one qualified entry that has been
+ * dropped
+ */
+ public abstract boolean removeFirstEntry(DropEvent<KeyT, ValueT> fsop,
+ EvictFilter<KeyT, ValueT> dfilter);
+
+ /**
+ * Ensure the size of free capacity is greater than the specified size, the
+ * entries will be filtered by {@link EvictFilter} before dropping, the
+ * {@link DropEvent} is used for post-processing
+ *
+ * @param freesz
+ * the size of free capacity that needs to be secured
+ *
+ * @param fsop
+ * the customized operations to free space to hold new entry
+ *
+ * @param dfilter
+ * the filter of entries for deletion
+ *
+ * @return <tt>true</tt> if the size of free capacity is greater than the
+ * specified size after evication if necessary
+ */
+ public abstract boolean ensureFreeSpace(long freesz,
+ DropEvent<KeyT, ValueT> fsop, EvictFilter<KeyT, ValueT> dfilter);
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/CommonAllocator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/CommonAllocator.java b/core/src/main/java/org/apache/mnemonic/CommonAllocator.java
new file mode 100644
index 0000000..20a584b
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/CommonAllocator.java
@@ -0,0 +1,104 @@
+package org.apache.mnemonic;
+
+import java.nio.ByteBuffer;
+
+import org.flowcomputing.commons.resgc.ResCollector;
+
+/**
+ * an abstract common class for memory allocator to provide common
+ * functionalities.
+ *
+ */
+public abstract class CommonAllocator<A extends CommonAllocator<A>> implements Allocator<A> {
+
+ protected Reclaim<Long> m_chunkreclaimer = null;
+ protected Reclaim<ByteBuffer> m_bufferreclaimer = null;
+
+ protected ResCollector<MemChunkHolder<A>, Long> m_chunkcollector = null;
+ protected ResCollector<MemBufferHolder<A>, ByteBuffer> m_bufcollector = null;
+
+ /**
+ * set a reclaimer to reclaim memory buffer
+ *
+ * @param reclaimer
+ * specify a reclaimer to accept reclaim request
+ */
+ public void setBufferReclaimer(Reclaim<ByteBuffer> reclaimer) {
+ m_bufferreclaimer = reclaimer;
+ }
+
+ /**
+ * set a reclaimer to reclaim memory chunk
+ *
+ * @param reclaimer
+ * specify a reclaimer to accept reclaim request
+ */
+ public void setChunkReclaimer(Reclaim<Long> reclaimer) {
+ m_chunkreclaimer = reclaimer;
+ }
+
+ /**
+ * create a memory chunk that is managed by its holder.
+ *
+ * @param size
+ * specify the size of memory chunk
+ *
+ * @return a holder contains a memory chunk
+ */
+ @Override
+ public MemChunkHolder<A> createChunk(long size) {
+ return createChunk(size, true);
+ }
+
+ /**
+ * 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<A> createBuffer(long size) {
+ return createBuffer(size, true);
+ }
+
+ /**
+ * register a memory chunk for auto-reclaim
+ *
+ * @param mholder
+ * specify a chunk holder to register
+ */
+ @Override
+ public void registerChunkAutoReclaim(MemChunkHolder<A> mholder) {
+ m_chunkcollector.register(mholder);
+ }
+
+ /**
+ * register a memory buffer for auto-reclaim
+ *
+ * @param mholder
+ * specify a buffer holder to register
+ */
+ @Override
+ public void registerBufferAutoReclaim(MemBufferHolder<A> mholder) {
+ m_bufcollector.register(mholder);
+ }
+
+ /**
+ * close both of resource collectors for this allocator
+ *
+ */
+ @Override
+ public void close() {
+ if (null != m_chunkcollector) {
+ m_chunkcollector.close();
+ m_chunkcollector = null;
+ }
+ if (null != m_bufcollector) {
+ m_bufcollector.close();
+ m_bufcollector = null;
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/CommonPersistAllocator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/CommonPersistAllocator.java b/core/src/main/java/org/apache/mnemonic/CommonPersistAllocator.java
new file mode 100644
index 0000000..6e0374d
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/CommonPersistAllocator.java
@@ -0,0 +1,120 @@
+package org.apache.mnemonic;
+
+/**
+ * an abstract common class for persistent memory allocator to provide common
+ * functionalities.
+ *
+ */
+public abstract class CommonPersistAllocator<A extends CommonAllocator<A>> extends CommonAllocator<A> {
+
+ /**
+ * determine whether the allocator supports transaction feature or not
+ *
+ * @return true if supported
+ */
+ public boolean supportTransaction() {
+ return false;
+ }
+
+ /**
+ * determine whether the allocator does atomic operations on memory pool
+ *
+ * @return true if it is
+ *
+ */
+ public boolean isAtomicOperation() {
+ return false;
+ }
+
+ /**
+ * determine whether this allocator supports to store non-volatile handler or not
+ *
+ * @return true if there is
+ */
+ public boolean hasNonVolatileHandlerStore() {
+ return false;
+ }
+
+ /**
+ * retrieve a memory buffer from its backed memory allocator.
+ *
+ * @param phandler
+ * specify the handler of memory buffer to retrieve
+ *
+ * @return a holder contains the retrieved memory buffer
+ */
+ public MemBufferHolder<A> retrieveBuffer(long phandler) {
+ return retrieveBuffer(phandler, true);
+ }
+
+ /**
+ * retrieve a memory chunk from its backed memory allocator.
+ *
+ * @param phandler
+ * specify the handler of memory chunk to retrieve
+ *
+ * @return a holder contains the retrieved memory chunk
+ */
+ public MemChunkHolder<A> retrieveChunk(long phandler) {
+ return retrieveChunk(phandler, true);
+ }
+
+ /**
+ * retrieve a memory buffer from its backed memory allocator.
+ *
+ * @param phandler
+ * specify the handler of memory buffer to retrieve
+ *
+ * @param autoreclaim
+ * specify whether this retrieved memory buffer can be reclaimed automatically or not
+ *
+ * @return a holder contains the retrieved memory buffer
+ */
+ abstract public MemBufferHolder<A> retrieveBuffer(long phandler, boolean autoreclaim);
+
+ /**
+ * retrieve a memory chunk from its backed memory allocator.
+ *
+ * @param phandler
+ * specify the handler of memory chunk to retrieve
+ *
+ * @param autoreclaim
+ * specify whether this retrieved memory chunk can be reclaimed automatically or not
+ *
+ * @return a holder contains the retrieved memory chunk
+ */
+ abstract public MemChunkHolder<A> retrieveChunk(long phandler, boolean autoreclaim);
+
+ /**
+ * get the handler from a memory buffer holder.
+ *
+ * @param mbuf
+ * specify the memory buffer holder
+ *
+ * @return a handler that could be used to retrieve its memory buffer
+ */
+ abstract public long getBufferHandler(MemBufferHolder<A> mbuf);
+
+ /**
+ * get the handler from a memory chunk holder.
+ *
+ * @param mchunk
+ * specify the memory chunk holder
+ *
+ * @return a handler that could be used to retrieve its memory chunk
+ */
+ abstract public long getChunkHandler(MemChunkHolder<A> mchunk);
+
+ /**
+ * start a application level transaction on this allocator.
+ *
+ */
+ abstract public void beginTransaction();
+
+ /**
+ * end a application level transaction on this allocator.
+ *
+ */
+ abstract public void endTransaction();
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/ContainerOverflowException.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/ContainerOverflowException.java b/core/src/main/java/org/apache/mnemonic/ContainerOverflowException.java
new file mode 100644
index 0000000..d6c053a
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/ContainerOverflowException.java
@@ -0,0 +1,20 @@
+package org.apache.mnemonic;
+
+/**
+ * this Exception will be thrown once cache pool has been out of space.
+ *
+ */
+public class ContainerOverflowException extends RuntimeException {
+
+ private static final long serialVersionUID = -8515518456414990004L;
+
+ /**
+ * accept a exception message to describe specific condition.
+ *
+ * @param message
+ * exception message
+ */
+ public ContainerOverflowException(String message) {
+ super(message);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/DropEvent.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/DropEvent.java b/core/src/main/java/org/apache/mnemonic/DropEvent.java
new file mode 100644
index 0000000..b3ca93f
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/DropEvent.java
@@ -0,0 +1,24 @@
+package org.apache.mnemonic;
+
+/**
+ * A event listener to monitor and post-process an entry's evacuation.
+ *
+ *
+ */
+public interface DropEvent<KeyT, ValueT> {
+ /**
+ * A call-back actor when an entry has been evicted. a customized drop
+ * action can be implemented on this interface's method e.g. spill this
+ * entry to disk or release associated resources etc.
+ *
+ * @param pool
+ * the pool which an entry has been evicted from
+ *
+ * @param k
+ * the key of an entry that has been evicted
+ *
+ * @param v
+ * the value of an entry that has been evicted
+ */
+ public void drop(CachePool<KeyT, ValueT> pool, KeyT k, ValueT v);
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/Durable.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/Durable.java b/core/src/main/java/org/apache/mnemonic/Durable.java
new file mode 100644
index 0000000..0649dda
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/Durable.java
@@ -0,0 +1,74 @@
+package org.apache.mnemonic;
+
+/**
+ * this interface defines the interactive functionalities with Mnenomic core part.
+ *
+ */
+
+
+public interface Durable {
+
+ /**
+ * this function will be invoked after this non-volatile object is created brandly new
+ *
+ */
+ public void initializeAfterCreate();
+
+ /**
+ * this function will be invoked after this non-volatile object is restored from its allocator
+ *
+ */
+ public void initializeAfterRestore();
+
+
+ /**
+ * this function will be invoked by its factory to setup generic related info to avoid expensive operations from reflection
+ *
+ * @param efproxies
+ * specify a array of factory to proxy the restoring of its generic field objects
+ *
+ * @param gftypes
+ * specify a array of types corresponding to efproxies
+ */
+ public void setupGenericInfo(EntityFactoryProxy[] efproxies, GenericField.GType[] gftypes);
+
+ /**
+ * this function could be called by user code to disable auto-reclaim for this non-volatile object
+ *
+ */
+ public void cancelAutoReclaim();
+
+ /**
+ * this function could be called by user code to register this object for auto-reclaim
+ *
+ */
+ public void registerAutoReclaim();
+
+ /**
+ * this function returns its bound handler for this object
+ *
+ * @return the handler of this object
+ */
+ public long getNonVolatileHandler();
+
+ /**
+ * return the setting for auto-reclaim
+ *
+ * @return the status of the auto-reclaim setting
+ */
+ public boolean autoReclaim();
+
+ /**
+ * manually destroy this object and release its memory resource
+ *
+ */
+ public void destroy() throws RetrieveNonVolatileEntityError;
+
+ /**
+ * return the native field map info for native processing.
+ *
+ * @return the native field map info
+ *
+ */
+ public long[][] getNativeFieldInfo();
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/EntityFactoryProxy.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/EntityFactoryProxy.java b/core/src/main/java/org/apache/mnemonic/EntityFactoryProxy.java
new file mode 100644
index 0000000..89e3ca9
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/EntityFactoryProxy.java
@@ -0,0 +1,35 @@
+package org.apache.mnemonic;
+
+/**
+ * proxy the restoring of generic non-volatile object fields
+ *
+ */
+
+
+public interface EntityFactoryProxy {
+
+ /**
+ * restore a durable object from persistent allocator using a handler of non-volatile object
+ *
+ * @param allocator
+ * specify a persistent allocator instance
+ *
+ * @param factoryproxys
+ * specify an array of factory proxies for its restored non-volatile object
+ *
+ * @param gfields
+ * specify an array of generic types of its generic fields corresponding to factoryproxys
+ *
+ * @param phandler
+ * specify a non-volatile handler to restore
+ *
+ * @param autoreclaim
+ * specify auto-reclaim for this restored non-volatile object
+ *
+ * @return the restored non-volatile object from this factory proxy
+ *
+ */
+ public abstract <A extends CommonPersistAllocator<A>> Durable restore(A allocator,
+ EntityFactoryProxy[] factoryproxys, GenericField.GType[] gfields, long phandler,
+ boolean autoreclaim);
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/EvictFilter.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/EvictFilter.java b/core/src/main/java/org/apache/mnemonic/EvictFilter.java
new file mode 100644
index 0000000..f3a65dd
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/EvictFilter.java
@@ -0,0 +1,24 @@
+package org.apache.mnemonic;
+
+/**
+ * A listener callback to validate its accepted key-value pair for evacuation.
+ *
+ */
+public interface EvictFilter<KeyT, ValueT> {
+ /**
+ * A call-back validator when an entry will be evicted.
+ *
+ * @param pool
+ * the pool which an entry has been evicted from
+ *
+ * @param k
+ * the entry's key
+ *
+ * @param v
+ * the entry's value
+ *
+ * @return <tt>true</tt> if the entry is allowed to be dropped from its
+ * cache pool.
+ */
+ public boolean validate(CachePool<KeyT, ValueT> pool, KeyT k, ValueT v);
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/GenericField.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/GenericField.java b/core/src/main/java/org/apache/mnemonic/GenericField.java
new file mode 100644
index 0000000..0f674a8
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/GenericField.java
@@ -0,0 +1,288 @@
+package org.apache.mnemonic;
+
+/**
+ * this class defines generic field for non-volatile entity
+ *
+ */
+
+import sun.misc.Unsafe;
+
+@SuppressWarnings("restriction")
+public class GenericField<A extends CommonPersistAllocator<A>, E> implements Durable {
+
+ /**
+ * defines the types of generic field
+ *
+ */
+ public enum GType {
+ BOOLEAN, CHARACTER, BYTE, SHORT, INTEGER, LONG, FLOAT, DOUBLE, STRING, DURABLE
+ };
+
+ private Unsafe m_unsafe;
+ private long m_fpos;
+ private GType m_dgftype = null;
+ private Durable m_field = null;
+ private MemBufferHolder<A> m_strfield = null;
+ private A m_allocator;
+ private boolean m_autoreclaim;
+ private EntityFactoryProxy m_defproxy = null;
+ private EntityFactoryProxy[] m_efproxies;
+ private GType[] m_gftypes;
+
+ /**
+ * Constructor: initialize this generic field
+ *
+ * @param defproxy
+ * specify its entity factory proxy
+ *
+ * @param dgftype
+ * specify its type
+ *
+ * @param efproxies
+ * specify an array of containing entity factory proxies
+ *
+ * @param gftypes
+ * specify an array of containing types corresponding to dfproxies
+ *
+ * @param allocator
+ * specify the allocator this field sit on
+ *
+ * @param unsafe
+ * specify the unsafe instance
+ *
+ * @param autoreclaim
+ * specify true if ask for auto-reclaim for this field
+ *
+ * @param fpos
+ * specify the field position
+ *
+ */
+ public GenericField(EntityFactoryProxy defproxy, GType dgftype, EntityFactoryProxy[] efproxies, GType[] gftypes,
+ A allocator, Unsafe unsafe, boolean autoreclaim, Long fpos) {
+ m_unsafe = unsafe;
+ m_fpos = fpos;
+ m_allocator = allocator;
+ m_autoreclaim = autoreclaim;
+ m_efproxies = efproxies;
+ m_gftypes = gftypes;
+ m_defproxy = defproxy;
+ m_dgftype = dgftype;
+ }
+
+ /**
+ * set a value to this field
+ *
+ * @param e
+ * specify a value to set
+ *
+ * @param destroy
+ * specify true if want to destroy the original value
+ */
+ public void set(E e, boolean destroy) {
+ boolean isnull = null == e;
+ switch (m_dgftype) {
+ case BYTE:
+ m_unsafe.putByte(m_fpos, isnull ? (byte)0 : (Byte) e);
+ break;
+ case BOOLEAN:
+ m_unsafe.putByte(m_fpos, isnull ? (byte)0 : ((Boolean) e ? (byte)1 : (byte)0));
+ break;
+ case CHARACTER:
+ m_unsafe.putChar(m_fpos, isnull ? (char)0 : (Character) e);
+ break;
+ case SHORT:
+ m_unsafe.putShort(m_fpos, isnull ? (short)0 : (Short) e);
+ break;
+ case INTEGER:
+ m_unsafe.putInt(m_fpos, isnull ? (int)0 : (Integer) e);
+ break;
+ case LONG:
+ m_unsafe.putLong(m_fpos, isnull ? (long)0 : (Long) e);
+ break;
+ case FLOAT:
+ m_unsafe.putFloat(m_fpos, isnull ? (float)0 : (Float) e);
+ break;
+ case DOUBLE:
+ m_unsafe.putDouble(m_fpos, isnull ? (double)0 : (Double) e);
+ break;
+ case STRING:
+ String str = (String) e;
+ if (destroy && null != get()) {
+ m_strfield.destroy();
+ m_strfield = null;
+ m_unsafe.putAddress(m_fpos, 0L);
+ }
+ if (null == str) {
+ m_unsafe.putAddress(m_fpos, 0L);
+ } else {
+ m_strfield = m_allocator.createBuffer(str.length() * 2, m_autoreclaim);
+ if (null == m_strfield) {
+ throw new OutOfPersistentMemory("Create Persistent String Error!");
+ }
+ m_strfield.get().asCharBuffer().put(str);
+ m_unsafe.putAddress(m_fpos, m_allocator.getBufferHandler(m_strfield));
+ }
+ break;
+ case DURABLE:
+ if (destroy && null != get()) {
+ m_field.destroy();
+ m_field = null;
+ m_unsafe.putAddress(m_fpos, 0L);
+ }
+ m_field = (Durable) e;
+ m_unsafe.putAddress(m_fpos, null == m_field ? 0L : m_field.getNonVolatileHandler());
+ break;
+ }
+
+ }
+
+ /**
+ * get the value of this field
+ *
+ * @return the field value
+ */
+ @SuppressWarnings("unchecked")
+ public E get() {
+ E ret = null;
+ switch (m_dgftype) {
+ case BYTE:
+ ret = (E) Byte.valueOf(m_unsafe.getByte(m_fpos));
+ break;
+ case BOOLEAN:
+ ret = (E) Boolean.valueOf(1 == m_unsafe.getByte(m_fpos));
+ break;
+ case CHARACTER:
+ ret = (E) Character.valueOf(m_unsafe.getChar(m_fpos));
+ break;
+ case SHORT:
+ ret = (E) Short.valueOf(m_unsafe.getShort(m_fpos));
+ break;
+ case INTEGER:
+ ret = (E) Integer.valueOf(m_unsafe.getInt(m_fpos));
+ break;
+ case LONG:
+ ret = (E) Long.valueOf(m_unsafe.getLong(m_fpos));
+ break;
+ case FLOAT:
+ ret = (E) Float.valueOf(m_unsafe.getFloat(m_fpos));
+ break;
+ case DOUBLE:
+ ret = (E) Double.valueOf(m_unsafe.getDouble(m_fpos));
+ break;
+ case STRING:
+ if (null == m_strfield) {
+ long phandler = m_unsafe.getAddress(m_fpos);
+ if (0L != phandler) {
+ m_strfield = m_allocator.retrieveBuffer(phandler, m_autoreclaim);
+ if (null == m_strfield) {
+ throw new RetrieveNonVolatileEntityError("Retrieve String Buffer Failure.");
+ }
+ }
+ }
+ ret = (E) (null == m_strfield ? null : m_strfield.get().asCharBuffer().toString());
+ break;
+ case DURABLE:
+ if (null == m_field) {
+ long phandler = m_unsafe.getAddress(m_fpos);
+ if (0L != phandler) {
+ if (null == m_defproxy) {
+ throw new RetrieveNonVolatileEntityError("Proxy not specified for Non-Volatile Generic entity.");
+ }
+ m_field = m_defproxy.restore(m_allocator, m_efproxies, m_gftypes, phandler, m_autoreclaim);
+ }
+ }
+ ret = (E) m_field;
+ break;
+ }
+ return ret;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void initializeAfterCreate() {
+ throw new UnsupportedOperationException("GenericField.initializeAfterCreate()");
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void initializeAfterRestore() {
+ throw new UnsupportedOperationException("GenericField.initializeAfterRestore()");
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void cancelAutoReclaim() {
+ if (null != m_field) {
+ m_field.cancelAutoReclaim();
+ }
+ if (null != m_strfield) {
+ m_strfield.cancelAutoReclaim();
+ }
+ m_autoreclaim = false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void registerAutoReclaim() {
+ if (null != m_field) {
+ m_field.registerAutoReclaim();
+ }
+ if (null != m_strfield) {
+ m_strfield.registerAutoReclaim();
+ }
+ m_autoreclaim = true;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public long getNonVolatileHandler() {
+ throw new UnsupportedOperationException("GenericField.getNonVolatileHandler()");
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean autoReclaim() {
+ return m_autoreclaim;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void destroy() throws RetrieveNonVolatileEntityError {
+ if (null != m_field) {
+ m_field.destroy();
+ }
+ if (null != m_strfield) {
+ m_strfield.destroy();
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void setupGenericInfo(EntityFactoryProxy[] efproxies, GType[] gftypes) {
+ throw new UnsupportedOperationException("GenericField.setupGenericInfo()");
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public long[][] getNativeFieldInfo() {
+ throw new UnsupportedOperationException("getNativeFieldInfo() on Generic Field.");
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/MemBufferHolder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/MemBufferHolder.java b/core/src/main/java/org/apache/mnemonic/MemBufferHolder.java
new file mode 100644
index 0000000..66ee0bf
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/MemBufferHolder.java
@@ -0,0 +1,56 @@
+package org.apache.mnemonic;
+
+import java.nio.ByteBuffer;
+
+/**
+ * holder for a ByteBuffer instance.
+ *
+ */
+public class MemBufferHolder<A extends CommonAllocator<A>> extends MemHolder<A, ByteBuffer, MemBufferHolder<A>> {
+
+
+ /**
+ * Constructor: initialize with a bytebuffer.
+ *
+ * @param ar
+ * specify an Allocator for this holder
+ *
+ * @param mres
+ * specify a chunk to be holden
+ *
+ */
+ public MemBufferHolder(A ar, ByteBuffer mres) {
+ super(mres, ar);
+ }
+
+ /**
+ * get the size of its held bytebuffer
+ *
+ * @return the size
+ */
+ @Override
+ public long getSize() {
+ return m_mres.capacity();
+ }
+
+ /**
+ * resize its held buffer
+ *
+ * @param size
+ * specify the new size for its held buffer
+ */
+ @Override
+ public MemBufferHolder<A> resize(long size) {
+ return m_allocator.resizeBuffer(this, size);
+ }
+
+ /**
+ * register its held buffer for auto-reclaim
+ *
+ */
+ @Override
+ public void registerAutoReclaim() {
+ m_allocator.registerBufferAutoReclaim(this);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/MemBufferHolderCachePool.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/MemBufferHolderCachePool.java b/core/src/main/java/org/apache/mnemonic/MemBufferHolderCachePool.java
new file mode 100644
index 0000000..7634206
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/MemBufferHolderCachePool.java
@@ -0,0 +1,236 @@
+
+package org.apache.mnemonic;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ *
+ * This class inherited from abstract CachePool class that implemented all
+ * inherited abstract methods. it specialized to cache MemBufferHolder objects
+ * that is backed by native memory pool.
+ *
+ */
+public class MemBufferHolderCachePool<KeyT> extends
+ CachePool<KeyT, MemBufferHolder<?>> {
+
+ private static final long serialVersionUID = 684275993324558070L;
+
+ private AtomicLong currentMemory = new AtomicLong(0L);
+
+ private long maxStoreCapacity = 0L;
+
+ /**
+ * Constructs an empty cache pool with specified capacity.
+ *
+ * @param maxCapacity
+ * the capacity of this cache pool
+ *
+ */
+ public MemBufferHolderCachePool(long maxCapacity) {
+ super(512, 0.75f);
+ maxStoreCapacity = maxCapacity;
+ }
+
+ /**
+ * @see CachePool#freeCapacity()
+ */
+ @Override
+ public long freeCapacity() {
+ return maxStoreCapacity - currentMemory.get();
+ }
+
+ /**
+ * @see CachePool#usedCapacity()
+ */
+ @Override
+ public long usedCapacity() {
+ return currentMemory.get();
+ }
+
+ /**
+ * @see Map#remove(Object)
+ */
+ @Override
+ public synchronized MemBufferHolder<?> remove(Object k) {
+ MemBufferHolder<?> ret = super.remove(k);
+ if (null != ret) {
+ currentMemory.getAndAdd(-ret.getSize());
+ }
+ return ret;
+ }
+
+ /**
+ * Associates the specified value with the specified key in this map
+ * (optional operation). If the map previously contained a mapping for the
+ * key, the old value is replaced by the specified value. (A map m is said
+ * to contain a mapping for a key k if and only if m.containsKey(k) would
+ * return true.)
+ *
+ * @param k
+ * key with which the specified value is to be associated
+ *
+ * @param v
+ * MemBufferHolder value to be associated with the specified key
+ *
+ * @return the previous value associated with key, or null if there was no
+ * mapping for key. (A null return can also indicate that the map
+ * previously associated null with key, if the implementation
+ * supports null values.)
+ */
+ @Override
+ public MemBufferHolder<?> put(KeyT k, MemBufferHolder<?> v) {
+ return put(k, v, null, null);
+ }
+
+ /**
+ * @see CachePool#put(Object, Object, DropEvent, EvictFilter)
+ */
+ @Override
+ public MemBufferHolder<?> put(KeyT k, MemBufferHolder<?> v,
+ DropEvent<KeyT, MemBufferHolder<?>> fsop,
+ EvictFilter<KeyT, MemBufferHolder<?>> dfilter) {
+ MemBufferHolder<?> ret = null;
+ long sz = v.getSize();
+ if (containsKey(k)) {
+ sz -= get(k).getSize();
+ }
+ if (sz <= maxStoreCapacity && ensureFreeSpace(sz, fsop, dfilter)) {
+ currentMemory.addAndGet(sz);
+ ret = super.put(k, v);
+ } else {
+ throw new ContainerOverflowException(
+ "Out of capacity of MemBufferHolderCachePool.");
+ }
+ return ret;
+ }
+
+ /**
+ * @see Map#putAll(Map)
+ */
+ @Override
+ public void putAll(
+ Map<? extends KeyT, ? extends MemBufferHolder<?>> m) {
+ putAll(m, null, null);
+ }
+
+ /**
+ * @see CachePool#putAll(Map, DropEvent, EvictFilter)
+ */
+ @Override
+ public void putAll(
+ Map<? extends KeyT, ? extends MemBufferHolder<?>> m,
+ DropEvent<KeyT, MemBufferHolder<?>> fsop,
+ EvictFilter<KeyT, MemBufferHolder<?>> dfilter) {
+
+ long reqsz = 0;
+ for (KeyT k : m.keySet()) {
+ reqsz += m.get(k).getSize();
+ }
+
+ if (reqsz <= maxStoreCapacity && ensureFreeSpace(reqsz, fsop, dfilter)) {
+ currentMemory.addAndGet(reqsz);
+ super.putAll(m);
+ } else {
+ throw new ContainerOverflowException(
+ "Out of capacity of MemBufferHolderCachePool.");
+ }
+ }
+
+ /**
+ * @see CachePool#hotKeySet(int)
+ */
+ @Override
+ public Set<KeyT> hotKeySet(int n) {
+ Set<KeyT> ret = new HashSet<KeyT>();
+ ArrayList<KeyT> keys = new ArrayList<KeyT>(keySet());
+ int endindex = keys.size() > n ? keys.size() - n : 0;
+ for (int i = keys.size(); i > endindex; i--) {
+ ret.add(keys.get(i - 1));
+ }
+ return ret;
+ }
+
+ /**
+ * @see CachePool#ensureFreeSpace(long)
+ */
+ @Override
+ public boolean ensureFreeSpace(long freesz) {
+ return ensureFreeSpace(freesz, null, null);
+ }
+
+ /**
+ * @see CachePool#removeFirstEntry(DropEvent, EvictFilter)
+ */
+ @Override
+ public boolean removeFirstEntry(
+ DropEvent<KeyT, MemBufferHolder<?>> fsop,
+ EvictFilter<KeyT, MemBufferHolder<?>> dfilter) {
+ boolean ret = false;
+ boolean delible = true;
+ for (Map.Entry<KeyT, MemBufferHolder<?>> entry : entrySet()) {
+ if (null != dfilter) {
+ delible = dfilter.validate(this, entry.getKey(),
+ entry.getValue());
+ }
+ if (delible) {
+ KeyT k = entry.getKey();
+ MemBufferHolder<?> v = remove(k);
+ if (null != fsop) {
+ fsop.drop(this, k, v);
+ }
+ ret = true;
+ break;
+ }
+ }
+ return ret;
+ }
+
+ /**
+ * @see CachePool#ensureFreeSpace(long, DropEvent, EvictFilter)
+ */
+ @Override
+ public boolean ensureFreeSpace(long freesz,
+ DropEvent<KeyT, MemBufferHolder<?>> fsop,
+ EvictFilter<KeyT, MemBufferHolder<?>> dfilter) {
+ boolean ret = false;
+
+ if (freesz <= freeCapacity())
+ return true;
+
+ if (freesz > maxStoreCapacity)
+ return false;
+
+ long selectedMemory = 0L;
+ Set<KeyT> selected = new HashSet<KeyT>();
+
+ boolean delible = true;
+ for (Map.Entry<KeyT, MemBufferHolder<?>> entry : entrySet()) {
+ if (null != dfilter) {
+ delible = dfilter.validate(this, entry.getKey(),
+ entry.getValue());
+ }
+ if (delible) {
+ selectedMemory += entry.getValue().getSize();
+ selected.add(entry.getKey());
+ if (freesz <= freeCapacity() + selectedMemory) {
+ ret = true;
+ break;
+ }
+ }
+ }
+ if (ret) {
+ for (KeyT k : selected) {
+ MemBufferHolder<?> mbh = remove(k);
+ if (null != fsop) {
+ fsop.drop(this, k, mbh);
+ }
+ }
+ }
+ return ret;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/MemChunkHolder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/MemChunkHolder.java b/core/src/main/java/org/apache/mnemonic/MemChunkHolder.java
new file mode 100644
index 0000000..28dd639
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/MemChunkHolder.java
@@ -0,0 +1,58 @@
+package org.apache.mnemonic;
+
+/**
+ * holder for a memory chunk.
+ *
+ */
+public class MemChunkHolder<A extends CommonAllocator<A>> extends MemHolder<A, Long, MemChunkHolder<A>> {
+
+ protected long m_size;
+
+ /**
+ * Constructor: initialize with a memory chunk.
+ *
+ * @param ar
+ * specify an Allocator for this holder
+ *
+ * @param mres
+ * specify a chunk to be holden
+ *
+ * @param size
+ * specify the size of this memory chunk
+ */
+ public MemChunkHolder(A ar, Long mres, long size) {
+ super(mres, ar);
+ m_size = size;
+ }
+
+ /**
+ * get the size of its held memory chunk
+ *
+ * @return the size
+ */
+ @Override
+ public long getSize() {
+ return m_size;
+ }
+
+ /**
+ * resize its held chunk
+ *
+ * @param size
+ * specify the new size for its held chunk
+ */
+ @Override
+ public MemChunkHolder<A> resize(long size) {
+ return m_allocator.resizeChunk(this, size);
+ }
+
+ /**
+ * register its held chunk for auto-reclaim
+ *
+ */
+ @Override
+ public void registerAutoReclaim() {
+ m_allocator.registerChunkAutoReclaim(this);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/MemClustering.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/MemClustering.java b/core/src/main/java/org/apache/mnemonic/MemClustering.java
new file mode 100644
index 0000000..8494cbb
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/MemClustering.java
@@ -0,0 +1,297 @@
+package org.apache.mnemonic;
+
+import java.util.*;
+
+/**
+ * clustering different kind of memory-like media and combine them as a larger
+ * memory pool for allocation. it will notify user when the underlying memory
+ * storage has been switched or downgraded for the request of allocation
+ *
+ */
+public class MemClustering {
+
+ /**
+ * an interface of event for performance level change.
+ *
+ */
+ public interface PerformanceLevelChange {
+
+ /**
+ * callback if performance level changed
+ *
+ * @param prevlvl
+ * the perf. level before change
+ *
+ * @param tgtlvl
+ * the perf. level after change
+ *
+ */
+ public void changed(PerformanceLevel prevlvl, PerformanceLevel tgtlvl);
+ }
+
+ /**
+ * an interface of event for CommonAllocator change.
+ *
+ */
+ public interface AllocatorChange {
+
+ /**
+ * callback if allocator changed
+ *
+ * @param lvl
+ * the perf. level after changed
+ *
+ * @param prevallocator
+ * the allocator before change
+ *
+ * @param tgtallocator
+ * the allocator after change
+ */
+ public void changed(PerformanceLevel lvl, CommonAllocator<?> prevallocator,
+ CommonAllocator<?> tgtallocator);
+ }
+
+ /**
+ * an interface to assist the creation of memory holder.
+ *
+ * @param <H>
+ * the holder type of memory resource
+ *
+ * @param bma
+ * specify an allocator for this holder
+ *
+ * @param size
+ * specify the size of this memory resource
+ *
+ * @return the holder created
+ */
+ private interface MemCreate<H extends MemHolder<? extends CommonAllocator<?>, ?, ?>> {
+ public H create(CommonAllocator<?> bma, long size);
+ }
+
+
+ private MemCreate<MemChunkHolder<?>> m_memchunkcreate = new MemCreate<MemChunkHolder<?>>() {
+ @Override
+ public MemChunkHolder<?> create(CommonAllocator<?> bma, long size) {
+ return bma.createChunk(size);
+ }
+ };
+
+ private MemCreate<MemBufferHolder<?>> m_membuffercreate = new MemCreate<MemBufferHolder<?>>() {
+ @Override
+ public MemBufferHolder<?> create(CommonAllocator<?> bma, long size) {
+ return bma.createBuffer(size);
+ }
+ };
+
+ /**
+ * performance level categories.
+ *
+ */
+ public static enum PerformanceLevel {
+ FASTEST, FAST, NORMAL, SLOW, SLOWEST
+ }
+
+ /**
+ * configuration for each allocator node.
+ *
+ */
+ public static class NodeConfig<A extends CommonAllocator<A>> {
+ private A m_allocator;
+ private PerformanceLevel m_level;
+
+ /**
+ * Constructor: initialize this instance with specified allocator and perf. level.
+ *
+ * @param a
+ * specify an allocator for this node
+ *
+ * @param l
+ * specify a performance level
+ */
+ public NodeConfig(A a, PerformanceLevel l) {
+ m_allocator = a;
+ m_level = l;
+ }
+
+ /**
+ * retrieve the allocator of this node.
+ *
+ * @return allocator of this node
+ */
+ public A getAllocator() {
+ return m_allocator;
+ }
+
+ /**
+ * retrieve the performance level of this node.
+ *
+ * @return level of this node
+ */
+ public PerformanceLevel getPerformanceLevel() {
+ return m_level;
+ }
+ }
+
+ private PerformanceLevelChange m_bwlvlchange = null;
+ private AllocatorChange m_allocatorChange = null;
+ private PerformanceLevel m_prevbwlevel = null;
+ private CommonAllocator<?> m_prevallocator = null;
+
+
+ private Map<PerformanceLevel, List<CommonAllocator<?>>> m_info;
+
+ /**
+ * Constructor: initialize a memory clustering instance.
+ *
+ * @param ncs
+ * specify a set of node with specified configuration respectively
+ */
+ public MemClustering(NodeConfig<? extends CommonAllocator<?>>[] ncs) {
+ m_info = new EnumMap<PerformanceLevel, List<CommonAllocator<?>>>(
+ PerformanceLevel.class);
+ for (PerformanceLevel lvl : PerformanceLevel.values()) {
+ m_info.put(lvl, new ArrayList<CommonAllocator<?>>());
+ }
+
+ for (NodeConfig<? extends CommonAllocator<?>> nc : ncs) {
+ m_info.get(nc.getPerformanceLevel()).add(nc.getAllocator());
+ }
+
+ }
+
+ /**
+ * set a callback of event for performance level change.
+ *
+ * @param bwlvlchange
+ * specify a callback object for perf. level change
+ */
+ public void setPerformanceLevelChange(PerformanceLevelChange bwlvlchange) {
+ m_bwlvlchange = bwlvlchange;
+ }
+
+ /**
+ * set a callback of event for allocator change.
+ *
+ * @param allocatorChange
+ * specify a callback object for allocator change
+ */
+ public <A extends CommonAllocator<A>> void setAllocatorChange(AllocatorChange allocatorChange) {
+ m_allocatorChange = allocatorChange;
+ }
+
+ /**
+ * a factory to create memory resource.
+ *
+ * @param <H> the type of holder
+ *
+ * @param creator
+ * specify a creator to delegate concrete holder creation
+ *
+ * @param startlevel
+ * specify a perf. level that is the start level for memory resource
+ * allocation
+ *
+ * @param size
+ * specify a size to request memory resource
+ *
+ * @return a new created holder held an allocated memory resource
+ */
+ protected <H extends MemHolder<? extends CommonAllocator<?>, ?, ?>> H
+ create(MemCreate<H> creator, PerformanceLevel startlevel, long size) {
+ H ret = null;
+ boolean eligible = false;
+ for (PerformanceLevel lvl : m_info.keySet()) {
+ if (!eligible && startlevel == lvl) {
+ eligible = true;
+ }
+ if (eligible) {
+ int distance = 0;
+ List<CommonAllocator<?>> bmas = m_info.get(lvl);
+ for (CommonAllocator<?> bma : bmas) {
+ ret = creator.create(bma, size);
+ if (null == ret) {
+ distance++;
+ } else {
+ if (null != m_bwlvlchange && m_prevbwlevel != lvl) {
+ m_bwlvlchange.changed(m_prevbwlevel, lvl);
+ m_prevbwlevel = lvl;
+ }
+ if (null != m_allocatorChange && m_prevallocator != bma) {
+ m_allocatorChange.changed(lvl, m_prevallocator, bma);
+ m_prevallocator = bma;
+ }
+ break;
+ }
+ }
+ Collections.rotate(bmas, distance);
+ }
+ if (null != ret) {
+ break;
+ }
+ }
+ return ret;
+ }
+
+ /**
+ * create a chunk from this clustering.
+ *
+ * @param size
+ * specify the size of memory chunk
+ *
+ * @return a holder with a created chunk
+ *
+ */
+ //@Override
+ public MemChunkHolder<?> createChunk(long size) {
+ return createChunk(PerformanceLevel.FASTEST, size);
+ }
+
+ /**
+ * create a chunk from this clustering
+ *
+ * @param startlevel
+ * specify the perf. level from which to search qualified
+ * node.
+ *
+ * @param size
+ * specify the size of memory chunk
+ *
+ * @return a holder with a created chunk
+ */
+ public MemChunkHolder<?> createChunk(PerformanceLevel startlevel, long size) {
+ return create(m_memchunkcreate, startlevel, size);
+ }
+
+ /**
+ * create a buffer from this clustering.
+ *
+ * @param size
+ * specify the size of memory buffer
+ *
+ * @return a holder with a created buffer
+ *
+ */
+ //@Override
+ public MemBufferHolder<?> createBuffer(long size) {
+ return createBuffer(PerformanceLevel.FASTEST, size);
+ }
+
+ /**
+ * create a buffer from this clustering
+ *
+ * @param startlevel
+ * specify the perf. level from which to search qualified
+ * node.
+ *
+ * @param size
+ * specify the size of memory buffer
+ *
+ * @return a holder with a created buffer
+ */
+ public MemBufferHolder<?> createBuffer(PerformanceLevel startlevel,
+ long size) {
+ return create(m_membuffercreate, startlevel, size);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/MemFile.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/MemFile.java b/core/src/main/java/org/apache/mnemonic/MemFile.java
new file mode 100644
index 0000000..08fb0d7
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/MemFile.java
@@ -0,0 +1,51 @@
+
+package org.apache.mnemonic;
+
+import java.io.File;
+
+/**
+ * a memory file that manages its data on native memory storage. Note: this
+ * class depends on PMFS, we suggest that NVM library to support this feature in
+ * native layer. In addition, the permission of /mnt/pmfs will be set properly.
+ *
+ *
+ */
+public class MemFile extends File {
+
+ private static final long serialVersionUID = 6579668848729471173L;
+ private String uri, id;
+
+ /**
+ * initialize the memory file.
+ *
+ * @param uri
+ * specify the location of memory file
+ *
+ * @param id
+ * specify the id of memory file
+ */
+ public MemFile(String uri, String id) {
+ super(uri, id);
+ this.uri = uri;
+ this.id = id;
+ }
+
+ /**
+ * retrieve the uri of this memory file.
+ *
+ * @return the uri of memory file
+ */
+ public String getUri() {
+ return this.uri;
+ }
+
+ /**
+ * retrieve the id of this memory file.
+ *
+ * @return the id of memory file
+ */
+ public String getId() {
+ return this.id;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/MemHolder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/MemHolder.java b/core/src/main/java/org/apache/mnemonic/MemHolder.java
new file mode 100644
index 0000000..b52e50a
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/MemHolder.java
@@ -0,0 +1,58 @@
+package org.apache.mnemonic;
+
+import org.flowcomputing.commons.resgc.*;
+
+/**
+ * hold a memory kind of resource.
+ *
+ */
+public abstract class MemHolder<A extends CommonAllocator<A>, T, H extends MemHolder<A, T, H>> extends ResHolder<T, H> {
+
+ protected A m_allocator;
+
+ /**
+ * Constructor: initialize with resource.
+ *
+ * @param mres
+ * specify a resource to be holden
+ *
+ * @param ar
+ * specify an Allocator for this holder
+ *
+ */
+ public MemHolder(T mres, A ar) {
+ super(mres);
+ m_allocator = ar;
+ }
+
+ /**
+ * get its allocator
+ *
+ * @return the allocator
+ */
+ public A getAllocator() {
+ return m_allocator;
+ }
+
+ /**
+ * resize its held resource
+ *
+ * @param size
+ * specify the new size for its held resource
+ */
+ abstract public MemHolder<A, T, H> resize(long size);
+
+ /**
+ * get the size of its held memory resource.
+ *
+ * @return the size
+ */
+ abstract public long getSize();
+
+ /**
+ * register its held resource for auto-reclaim
+ *
+ */
+ abstract public void registerAutoReclaim();
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/MemoryNonVolatileEntity.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/MemoryNonVolatileEntity.java b/core/src/main/java/org/apache/mnemonic/MemoryNonVolatileEntity.java
new file mode 100644
index 0000000..87010d5
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/MemoryNonVolatileEntity.java
@@ -0,0 +1,19 @@
+package org.apache.mnemonic;
+
+/**
+ *
+ *
+ */
+
+
+public interface MemoryNonVolatileEntity<ALLOC_PMem3C93D24F59 extends CommonPersistAllocator<ALLOC_PMem3C93D24F59>> {
+
+ public void initializeNonVolatileEntity(ALLOC_PMem3C93D24F59 allocator, EntityFactoryProxy[] efproxys, GenericField.GType[] gfields, boolean autoreclaim);
+
+ public void createNonVolatileEntity(ALLOC_PMem3C93D24F59 allocator, EntityFactoryProxy[] efproxys, GenericField.GType[] gfields, boolean autoreclaim)
+ throws OutOfPersistentMemory;
+
+ public void restoreNonVolatileEntity(ALLOC_PMem3C93D24F59 allocator, EntityFactoryProxy[] efproxys, GenericField.GType[] gfields, long phandler, boolean autoreclaim)
+ throws RetrieveNonVolatileEntityError;
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/NonVolatileEntity.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/NonVolatileEntity.java b/core/src/main/java/org/apache/mnemonic/NonVolatileEntity.java
new file mode 100644
index 0000000..6c2aabb
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/NonVolatileEntity.java
@@ -0,0 +1,13 @@
+package org.apache.mnemonic;
+
+/**
+ * this class defines a annotation for non-volatile entity
+ *
+ */
+
+import java.lang.annotation.*;
+
+@Target(ElementType.TYPE) @Retention(RetentionPolicy.CLASS)
+public @interface NonVolatileEntity {
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/NonVolatileEntityProcessor.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/NonVolatileEntityProcessor.java b/core/src/main/java/org/apache/mnemonic/NonVolatileEntityProcessor.java
new file mode 100644
index 0000000..b9e83c6
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/NonVolatileEntityProcessor.java
@@ -0,0 +1,134 @@
+package org.apache.mnemonic;
+
+/**
+ * a non-volatile annotation processor
+ *
+ */
+
+import java.io.IOException;
+import java.util.LinkedHashSet;
+import java.util.Set;
+
+import javax.annotation.processing.*;
+import javax.lang.model.SourceVersion;
+import javax.lang.model.element.Element;
+import javax.lang.model.element.ElementKind;
+import javax.lang.model.element.ExecutableElement;
+import javax.lang.model.element.TypeElement;
+import javax.lang.model.element.VariableElement;
+import javax.lang.model.type.DeclaredType;
+import javax.lang.model.type.TypeMirror;
+import javax.lang.model.util.ElementFilter;
+import javax.lang.model.util.Elements;
+import javax.lang.model.util.Types;
+
+import javax.tools.Diagnostic;
+
+import com.squareup.javapoet.MethodSpec;
+
+public class NonVolatileEntityProcessor extends AbstractProcessor {
+ private Types typeUtils;
+ private Elements elementUtils;
+ private Filer filer;
+ private Messager messager;
+ // private Map<String, FactoryGroupedClasses> factoryClasses = new
+ // LinkedHashMap<String, FactoryGroupedClasses>();
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public synchronized void init(ProcessingEnvironment processingEnv) {
+ super.init(processingEnv);
+ typeUtils = processingEnv.getTypeUtils();
+ elementUtils = processingEnv.getElementUtils();
+ filer = processingEnv.getFiler();
+ messager = processingEnv.getMessager();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Set<String> getSupportedAnnotationTypes() {
+ Set<String> annotataions = new LinkedHashSet<String>();
+ annotataions.add(NonVolatileEntity.class.getCanonicalName());
+ return annotataions;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public SourceVersion getSupportedSourceVersion() {
+ return SourceVersion.latestSupported();
+ }
+
+ /**
+ * triggered if an error issued during processing
+ *
+ * @param e
+ * the element in question
+ *
+ * @param msg
+ * the message issued
+ */
+ public void error(Element e, String msg) {
+ messager.printMessage(Diagnostic.Kind.ERROR, msg, e);
+ }
+
+ /**
+ * triggered if a note issued during processing
+ *
+ * @param e
+ * the element in question
+ *
+ * @param msg
+ * the message issued
+ */
+ public void note(Element e, String msg) {
+ messager.printMessage(Diagnostic.Kind.NOTE, msg, e);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
+
+ try {
+
+ for (Element annotatedElement : roundEnv.getElementsAnnotatedWith(NonVolatileEntity.class)) {
+
+ String outputstr = String.format("++++++++++%s+++++++++++", annotatedElement.getSimpleName());
+ note(annotatedElement, outputstr);
+ // System.err.println(outputstr);
+
+ if (annotatedElement.getKind() != ElementKind.CLASS) {
+ throw new AnnotationProcessingException(
+ annotatedElement, "Only classes can be annotated with @%s",
+ NonVolatileEntity.class.getSimpleName());
+ }
+
+ // We can cast it, because we know that it of ElementKind.CLASS
+ TypeElement typeelem = (TypeElement) annotatedElement;
+
+ AnnotatedNonVolatileEntityClass annotatedClass =
+ new AnnotatedNonVolatileEntityClass(typeelem, typeUtils, elementUtils, messager);
+
+ annotatedClass.prepareProcessing();
+
+ annotatedClass.generateCode(filer);
+
+ }
+
+ } catch (AnnotationProcessingException e) {
+ error(e.getElement(), e.getMessage());
+ } catch (IOException e) {
+ error(null, e.getMessage());
+ }
+
+ return true;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/NonVolatileGetter.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/NonVolatileGetter.java b/core/src/main/java/org/apache/mnemonic/NonVolatileGetter.java
new file mode 100644
index 0000000..be88be8
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/NonVolatileGetter.java
@@ -0,0 +1,18 @@
+package org.apache.mnemonic;
+
+/**
+ * this class defines an annotation for getter methods of non-volatile entity
+ *
+ */
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target(ElementType.METHOD) @Retention(RetentionPolicy.CLASS)
+public @interface NonVolatileGetter {
+ String EntityFactoryProxies() default "null";
+ String GenericFieldTypes() default "null";
+ long Id() default -1L;
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/NonVolatileSetter.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/NonVolatileSetter.java b/core/src/main/java/org/apache/mnemonic/NonVolatileSetter.java
new file mode 100644
index 0000000..11dfba2
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/NonVolatileSetter.java
@@ -0,0 +1,16 @@
+package org.apache.mnemonic;
+
+/**
+ * this class defines an annotation for setter methods of non-volatile entity
+ *
+ */
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target(ElementType.METHOD) @Retention(RetentionPolicy.CLASS)
+public @interface NonVolatileSetter {
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/OutOfPersistentMemory.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/OutOfPersistentMemory.java b/core/src/main/java/org/apache/mnemonic/OutOfPersistentMemory.java
new file mode 100644
index 0000000..bb1d042
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/OutOfPersistentMemory.java
@@ -0,0 +1,15 @@
+package org.apache.mnemonic;
+
+/**
+ * this is an exception that should be throw once out of persistent memory
+ *
+ */
+
+public class OutOfPersistentMemory extends RuntimeException {
+
+ private static final long serialVersionUID = -6315943783592441148L;
+
+ public OutOfPersistentMemory(String s) {
+ super(s);
+ }
+}
[7/7] incubator-mnemonic git commit: MNEMONIC-5 MNEMONIC-6 #resolve
refactored package names
Posted by ga...@apache.org.
MNEMONIC-5 MNEMONIC-6 #resolve refactored package names
Project: http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/commit/8e83efe5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/tree/8e83efe5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/diff/8e83efe5
Branch: refs/heads/master
Commit: 8e83efe5be11b9875fd7cb71d65122602c96619b
Parents: b53b241
Author: Wang, Gang(Gary) <ga...@intel.com>
Authored: Tue Apr 5 11:13:46 2016 -0700
Committer: Wang, Gang(Gary) <ga...@intel.com>
Committed: Tue Apr 5 11:13:46 2016 -0700
----------------------------------------------------------------------
allocator-services/nvml-vmem-service/pom.xml | 4 +-
.../internal/VMemServiceImpl.java | 94 --
.../internal/VMemServiceImpl.java | 94 ++
.../src/main/native/CMakeLists.txt | 2 +-
..._allocatorservice_internal_VMemServiceImpl.c | 185 ----
..._allocatorservice_internal_VMemServiceImpl.h | 31 -
..._allocatorservice_internal_VMemServiceImpl.c | 185 ++++
..._allocatorservice_internal_VMemServiceImpl.h | 31 +
...ocatorservice.VolatileMemoryAllocatorService | 1 -
...ocatorservice.VolatileMemoryAllocatorService | 1 +
allocator-services/pmalloc-service/pom.xml | 4 +-
.../internal/PMallocServiceImpl.java | 150 ---
.../internal/PMallocServiceImpl.java | 150 +++
.../src/main/native/CMakeLists.txt | 2 +-
...locatorservice_internal_PMallocServiceImpl.c | 319 ------
...locatorservice_internal_PMallocServiceImpl.h | 31 -
...locatorservice_internal_PMallocServiceImpl.c | 319 ++++++
...locatorservice_internal_PMallocServiceImpl.h | 31 +
...torservice.NonVolatileMemoryAllocatorService | 1 -
...torservice.NonVolatileMemoryAllocatorService | 1 +
allocator-services/pom.xml | 6 +-
collections/pom.xml | 10 +-
.../collections/NonVolatileNodeValue.java | 150 ---
.../collections/NonVolatileNodeValue.java | 150 +++
.../collections/NonVolatileNodeValueNGTest.java | 256 -----
.../collections/NonVolatilePersonNGTest.java | 136 ---
.../intel/bigdatamem/collections/Payload.java | 29 -
.../intel/bigdatamem/collections/Person.java | 65 --
.../collections/NonVolatileNodeValueNGTest.java | 255 +++++
.../collections/NonVolatilePersonNGTest.java | 134 +++
.../apache/mnemonic/collections/Payload.java | 29 +
.../org/apache/mnemonic/collections/Person.java | 64 ++
collections/src/test/resources/testng.xml | 10 +-
core/pom.xml | 4 +-
.../java/com/intel/bigdatamem/Allocatable.java | 102 --
.../java/com/intel/bigdatamem/Allocator.java | 39 -
.../AnnotatedNonVolatileEntityClass.java | 977 -------------------
.../AnnotationProcessingException.java | 23 -
.../intel/bigdatamem/BigDataMemAllocator.java | 278 ------
.../intel/bigdatamem/BigDataPMemAllocator.java | 469 ---------
.../bigdatamem/ByteBufferBackedInputStream.java | 61 --
.../ByteBufferBackedOutputStream.java | 56 --
.../intel/bigdatamem/ByteBufferSerializer.java | 149 ---
.../java/com/intel/bigdatamem/CachePool.java | 144 ---
.../com/intel/bigdatamem/CommonAllocator.java | 104 --
.../bigdatamem/CommonPersistAllocator.java | 120 ---
.../bigdatamem/ContainerOverflowException.java | 20 -
.../java/com/intel/bigdatamem/DropEvent.java | 24 -
.../main/java/com/intel/bigdatamem/Durable.java | 74 --
.../intel/bigdatamem/EntityFactoryProxy.java | 35 -
.../java/com/intel/bigdatamem/EvictFilter.java | 24 -
.../java/com/intel/bigdatamem/GenericField.java | 288 ------
.../com/intel/bigdatamem/MemBufferHolder.java | 56 --
.../bigdatamem/MemBufferHolderCachePool.java | 236 -----
.../com/intel/bigdatamem/MemChunkHolder.java | 58 --
.../com/intel/bigdatamem/MemClustering.java | 297 ------
.../main/java/com/intel/bigdatamem/MemFile.java | 51 -
.../java/com/intel/bigdatamem/MemHolder.java | 58 --
.../bigdatamem/MemoryNonVolatileEntity.java | 19 -
.../com/intel/bigdatamem/NonVolatileEntity.java | 13 -
.../bigdatamem/NonVolatileEntityProcessor.java | 134 ---
.../com/intel/bigdatamem/NonVolatileGetter.java | 18 -
.../com/intel/bigdatamem/NonVolatileSetter.java | 16 -
.../intel/bigdatamem/OutOfPersistentMemory.java | 15 -
.../intel/bigdatamem/PMAddressTranslator.java | 45 -
.../main/java/com/intel/bigdatamem/Reclaim.java | 23 -
.../RetrieveNonVolatileEntityError.java | 19 -
.../com/intel/bigdatamem/SysMemAllocator.java | 305 ------
.../main/java/com/intel/bigdatamem/Utils.java | 384 --------
.../NonVolatileMemoryAllocatorService.java | 97 --
.../VolatileMemoryAllocatorService.java | 130 ---
.../java/org/apache/mnemonic/Allocatable.java | 102 ++
.../java/org/apache/mnemonic/Allocator.java | 39 +
.../AnnotatedNonVolatileEntityClass.java | 977 +++++++++++++++++++
.../mnemonic/AnnotationProcessingException.java | 23 +
.../apache/mnemonic/BigDataMemAllocator.java | 278 ++++++
.../apache/mnemonic/BigDataPMemAllocator.java | 469 +++++++++
.../mnemonic/ByteBufferBackedInputStream.java | 61 ++
.../mnemonic/ByteBufferBackedOutputStream.java | 56 ++
.../apache/mnemonic/ByteBufferSerializer.java | 149 +++
.../java/org/apache/mnemonic/CachePool.java | 144 +++
.../org/apache/mnemonic/CommonAllocator.java | 104 ++
.../apache/mnemonic/CommonPersistAllocator.java | 120 +++
.../mnemonic/ContainerOverflowException.java | 20 +
.../java/org/apache/mnemonic/DropEvent.java | 24 +
.../main/java/org/apache/mnemonic/Durable.java | 74 ++
.../org/apache/mnemonic/EntityFactoryProxy.java | 35 +
.../java/org/apache/mnemonic/EvictFilter.java | 24 +
.../java/org/apache/mnemonic/GenericField.java | 288 ++++++
.../org/apache/mnemonic/MemBufferHolder.java | 56 ++
.../mnemonic/MemBufferHolderCachePool.java | 236 +++++
.../org/apache/mnemonic/MemChunkHolder.java | 58 ++
.../java/org/apache/mnemonic/MemClustering.java | 297 ++++++
.../main/java/org/apache/mnemonic/MemFile.java | 51 +
.../java/org/apache/mnemonic/MemHolder.java | 58 ++
.../mnemonic/MemoryNonVolatileEntity.java | 19 +
.../org/apache/mnemonic/NonVolatileEntity.java | 13 +
.../mnemonic/NonVolatileEntityProcessor.java | 134 +++
.../org/apache/mnemonic/NonVolatileGetter.java | 18 +
.../org/apache/mnemonic/NonVolatileSetter.java | 16 +
.../apache/mnemonic/OutOfPersistentMemory.java | 15 +
.../apache/mnemonic/PMAddressTranslator.java | 45 +
.../main/java/org/apache/mnemonic/Reclaim.java | 23 +
.../RetrieveNonVolatileEntityError.java | 19 +
.../org/apache/mnemonic/SysMemAllocator.java | 305 ++++++
.../main/java/org/apache/mnemonic/Utils.java | 384 ++++++++
.../NonVolatileMemoryAllocatorService.java | 97 ++
.../VolatileMemoryAllocatorService.java | 130 +++
.../javax.annotation.processing.Processor | 2 +-
.../bigdatamem/BigDataMemAllocatorNGTest.java | 59 --
.../bigdatamem/BigDataPMemAllocatorNGTest.java | 124 ---
.../bigdatamem/ByteBufferSerializerNGTest.java | 65 --
.../MemBufferHolderCachePoolNGTest.java | 150 ---
.../intel/bigdatamem/MemClusteringNGTest.java | 190 ----
.../bigdatamem/NonVolatilePersonNGTest.java | 136 ---
.../test/java/com/intel/bigdatamem/Payload.java | 29 -
.../test/java/com/intel/bigdatamem/Person.java | 61 --
.../mnemonic/BigDataMemAllocatorNGTest.java | 58 ++
.../mnemonic/BigDataPMemAllocatorNGTest.java | 124 +++
.../mnemonic/ByteBufferSerializerNGTest.java | 65 ++
.../MemBufferHolderCachePoolNGTest.java | 150 +++
.../apache/mnemonic/MemClusteringNGTest.java | 189 ++++
.../mnemonic/NonVolatilePersonNGTest.java | 136 +++
.../test/java/org/apache/mnemonic/Payload.java | 29 +
.../test/java/org/apache/mnemonic/Person.java | 61 ++
core/src/test/resources/testng.xml | 16 +-
examples/pom.xml | 6 +-
.../java/com/intel/bigdatamem/example/Main.java | 189 ----
.../java/org/apache/mnemonic/example/Main.java | 191 ++++
pom.xml | 2 +-
130 files changed, 7442 insertions(+), 7446 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/allocator-services/nvml-vmem-service/pom.xml
----------------------------------------------------------------------
diff --git a/allocator-services/nvml-vmem-service/pom.xml b/allocator-services/nvml-vmem-service/pom.xml
index 7259033..7104a82 100644
--- a/allocator-services/nvml-vmem-service/pom.xml
+++ b/allocator-services/nvml-vmem-service/pom.xml
@@ -3,11 +3,11 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
- <groupId>com.intel.mnemonic.service.allocatorservice</groupId>
+ <groupId>org.apache.mnemonic.service.allocatorservice</groupId>
<artifactId>allocator-services-parent-project</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
- <groupId>com.intel.mnemonic.service.allocatorservice</groupId>
+ <groupId>org.apache.mnemonic.service.allocatorservice</groupId>
<artifactId>nvml-vmem-service</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Mnemonic Project NVML VMem Allocator</name>
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/allocator-services/nvml-vmem-service/src/main/java/com/intel/mnemonic/service/allocatorservice/internal/VMemServiceImpl.java
----------------------------------------------------------------------
diff --git a/allocator-services/nvml-vmem-service/src/main/java/com/intel/mnemonic/service/allocatorservice/internal/VMemServiceImpl.java b/allocator-services/nvml-vmem-service/src/main/java/com/intel/mnemonic/service/allocatorservice/internal/VMemServiceImpl.java
deleted file mode 100644
index 5c9b2f2..0000000
--- a/allocator-services/nvml-vmem-service/src/main/java/com/intel/mnemonic/service/allocatorservice/internal/VMemServiceImpl.java
+++ /dev/null
@@ -1,94 +0,0 @@
-package com.intel.mnemonic.service.allocatorservice.internal;
-
-import com.intel.mnemonic.service.allocatorservice.VolatileMemoryAllocatorService;
-import org.flowcomputing.commons.primitives.*;
-import java.nio.ByteBuffer;
-
-
-public class VMemServiceImpl implements VolatileMemoryAllocatorService {
- static {
- try {
- NativeLibraryLoader.loadFromJar("vmemallocator");
- } catch (Exception e) {
- throw new Error(e);
- }
- }
-
- @Override
- public String getServiceId() {
- return "vmem";
- }
-
- @Override
- public long init(long capacity, String uri, boolean isnew) {
- return ninit(capacity, uri, isnew);
- }
-
- @Override
- public void close(long id) {
- nclose(id);
- }
-
- @Override
- public void sync(long id) {
- nsync(id);
- }
-
- @Override
- public long allocate(long id, long size, boolean initzero) {
- return nallocate(id, size, initzero);
- }
-
- @Override
- public long reallocate(long id, long address, long size, boolean initzero) {
- return nreallocate(id, address, size, initzero);
- }
-
- @Override
- public void free(long id, long address) {
- nfree(id, address);
- }
-
- @Override
- public ByteBuffer createByteBuffer(long id, long size) {
- return ncreateByteBuffer(id, size);
- }
-
- @Override
- public ByteBuffer resizeByteBuffer(long id, ByteBuffer bytebuf, long size) {
- return nresizeByteBuffer(id, bytebuf, size);
- }
-
- @Override
- public void destroyByteBuffer(long id, ByteBuffer bytebuf) {
- ndestroyByteBuffer(id, bytebuf);
- }
-
-
- protected native long ninit(long capacity, String uri, boolean isnew);
-
-
- protected native void nclose(long id);
-
-
- protected native void nsync(long id);
-
-
- protected native long nallocate(long id, long size, boolean initzero);
-
-
- protected native long nreallocate(long id, long address, long size, boolean initzero);
-
-
- protected native void nfree(long id, long address);
-
-
- protected native ByteBuffer ncreateByteBuffer(long id, long size);
-
-
- protected native ByteBuffer nresizeByteBuffer(long id, ByteBuffer bytebuf, long size);
-
-
- protected native void ndestroyByteBuffer(long id, ByteBuffer bytebuf);
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/allocator-services/nvml-vmem-service/src/main/java/org/apache/mnemonic/service/allocatorservice/internal/VMemServiceImpl.java
----------------------------------------------------------------------
diff --git a/allocator-services/nvml-vmem-service/src/main/java/org/apache/mnemonic/service/allocatorservice/internal/VMemServiceImpl.java b/allocator-services/nvml-vmem-service/src/main/java/org/apache/mnemonic/service/allocatorservice/internal/VMemServiceImpl.java
new file mode 100644
index 0000000..6e4558d
--- /dev/null
+++ b/allocator-services/nvml-vmem-service/src/main/java/org/apache/mnemonic/service/allocatorservice/internal/VMemServiceImpl.java
@@ -0,0 +1,94 @@
+package org.apache.mnemonic.service.allocatorservice.internal;
+
+import org.apache.mnemonic.service.allocatorservice.VolatileMemoryAllocatorService;
+import org.flowcomputing.commons.primitives.*;
+import java.nio.ByteBuffer;
+
+
+public class VMemServiceImpl implements VolatileMemoryAllocatorService {
+ static {
+ try {
+ NativeLibraryLoader.loadFromJar("vmemallocator");
+ } catch (Exception e) {
+ throw new Error(e);
+ }
+ }
+
+ @Override
+ public String getServiceId() {
+ return "vmem";
+ }
+
+ @Override
+ public long init(long capacity, String uri, boolean isnew) {
+ return ninit(capacity, uri, isnew);
+ }
+
+ @Override
+ public void close(long id) {
+ nclose(id);
+ }
+
+ @Override
+ public void sync(long id) {
+ nsync(id);
+ }
+
+ @Override
+ public long allocate(long id, long size, boolean initzero) {
+ return nallocate(id, size, initzero);
+ }
+
+ @Override
+ public long reallocate(long id, long address, long size, boolean initzero) {
+ return nreallocate(id, address, size, initzero);
+ }
+
+ @Override
+ public void free(long id, long address) {
+ nfree(id, address);
+ }
+
+ @Override
+ public ByteBuffer createByteBuffer(long id, long size) {
+ return ncreateByteBuffer(id, size);
+ }
+
+ @Override
+ public ByteBuffer resizeByteBuffer(long id, ByteBuffer bytebuf, long size) {
+ return nresizeByteBuffer(id, bytebuf, size);
+ }
+
+ @Override
+ public void destroyByteBuffer(long id, ByteBuffer bytebuf) {
+ ndestroyByteBuffer(id, bytebuf);
+ }
+
+
+ protected native long ninit(long capacity, String uri, boolean isnew);
+
+
+ protected native void nclose(long id);
+
+
+ protected native void nsync(long id);
+
+
+ protected native long nallocate(long id, long size, boolean initzero);
+
+
+ protected native long nreallocate(long id, long address, long size, boolean initzero);
+
+
+ protected native void nfree(long id, long address);
+
+
+ protected native ByteBuffer ncreateByteBuffer(long id, long size);
+
+
+ protected native ByteBuffer nresizeByteBuffer(long id, ByteBuffer bytebuf, long size);
+
+
+ protected native void ndestroyByteBuffer(long id, ByteBuffer bytebuf);
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/allocator-services/nvml-vmem-service/src/main/native/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/allocator-services/nvml-vmem-service/src/main/native/CMakeLists.txt b/allocator-services/nvml-vmem-service/src/main/native/CMakeLists.txt
index e3b39a6..537508f 100644
--- a/allocator-services/nvml-vmem-service/src/main/native/CMakeLists.txt
+++ b/allocator-services/nvml-vmem-service/src/main/native/CMakeLists.txt
@@ -26,7 +26,7 @@ if (NOT LIBVMEM_LIBRARIES)
message(FATAL_ERROR "not found vmem library")
endif (NOT LIBVMEM_LIBRARIES)
-add_library(vmemallocator SHARED common.c com_intel_mnemonic_service_allocatorservice_internal_VMemServiceImpl.c)
+add_library(vmemallocator SHARED common.c org_apache_mnemonic_service_allocatorservice_internal_VMemServiceImpl.c)
target_include_directories(vmemallocator PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(vmemallocator vmem)
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/allocator-services/nvml-vmem-service/src/main/native/com_intel_mnemonic_service_allocatorservice_internal_VMemServiceImpl.c
----------------------------------------------------------------------
diff --git a/allocator-services/nvml-vmem-service/src/main/native/com_intel_mnemonic_service_allocatorservice_internal_VMemServiceImpl.c b/allocator-services/nvml-vmem-service/src/main/native/com_intel_mnemonic_service_allocatorservice_internal_VMemServiceImpl.c
deleted file mode 100644
index ba8630f..0000000
--- a/allocator-services/nvml-vmem-service/src/main/native/com_intel_mnemonic_service_allocatorservice_internal_VMemServiceImpl.c
+++ /dev/null
@@ -1,185 +0,0 @@
-/****************************************************************************************
-Revise Date: 20 Apr. 2014
-*****************************************************************************************/
-
-#include "com_intel_mnemonic_service_allocatorservice_internal_VMemServiceImpl.h"
-
-#include <libvmem.h>
-
-static VMEM **g_vmp_ptr = NULL;
-static size_t g_vmp_count = 0;
-
-static pthread_mutex_t *g_vmem_mutex_ptr = NULL;
-
-static pthread_rwlock_t g_vmem_rwlock = PTHREAD_RWLOCK_INITIALIZER;
-
-/******************************************************************************
- ** JNI implementations
- *****************************************************************************/
-
-JNIEXPORT
-jlong JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_VMemServiceImpl_nallocate(
- JNIEnv* env,
- jobject this, jlong id,
- jlong size, jboolean initzero)
-{
- pthread_rwlock_rdlock(&g_vmem_rwlock);
- pthread_mutex_lock(g_vmem_mutex_ptr + id);
- void* nativebuf = initzero ? vmem_calloc(*(g_vmp_ptr + id), 1, size) : vmem_malloc(*(g_vmp_ptr + id), size);
- pthread_mutex_unlock(g_vmem_mutex_ptr + id);
- pthread_rwlock_unlock(&g_vmem_rwlock);
- return addr_to_java(nativebuf);
-}
-
-JNIEXPORT
-jlong JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_VMemServiceImpl_nreallocate(
- JNIEnv* env,
- jobject this, jlong id,
- jlong address,
- jlong size, jboolean initzero)
-{
- pthread_rwlock_rdlock(&g_vmem_rwlock);
- pthread_mutex_lock(g_vmem_mutex_ptr + id);
-
- void* p = addr_from_java(address);
-
- void* nativebuf = vmem_realloc(*(g_vmp_ptr + id), p, size);
-
- pthread_mutex_unlock(g_vmem_mutex_ptr + id);
- pthread_rwlock_unlock(&g_vmem_rwlock);
- return addr_to_java(nativebuf);
-}
-
-JNIEXPORT
-void JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_VMemServiceImpl_nfree(
- JNIEnv* env,
- jobject this, jlong id,
- jlong address)
-{
- pthread_rwlock_rdlock(&g_vmem_rwlock);
- pthread_mutex_lock(g_vmem_mutex_ptr + id);
- void* nativebuf = addr_from_java(address);
- if (nativebuf != NULL)
- vmem_free(*(g_vmp_ptr + id), nativebuf);
- pthread_mutex_unlock(g_vmem_mutex_ptr + id);
- pthread_rwlock_unlock(&g_vmem_rwlock);
-}
-
-JNIEXPORT
-void JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_VMemServiceImpl_nsync(
- JNIEnv* env,
- jobject this, jlong id)
-{
-}
-
-JNIEXPORT
-jobject JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_VMemServiceImpl_ncreateByteBuffer(
- JNIEnv *env, jobject this, jlong id, jlong size)
-{
- pthread_rwlock_rdlock(&g_vmem_rwlock);
- pthread_mutex_lock(g_vmem_mutex_ptr + id);
- jobject ret = NULL;
- void* nativebuf = vmem_malloc(*(g_vmp_ptr + id), size);
- ret = NULL != nativebuf ? (*env)->NewDirectByteBuffer(env, nativebuf, size) : NULL;
- pthread_mutex_unlock(g_vmem_mutex_ptr + id);
- pthread_rwlock_unlock(&g_vmem_rwlock);
- return ret;
-}
-
-JNIEXPORT
-jobject JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_VMemServiceImpl_nresizeByteBuffer(
- JNIEnv *env, jobject this, jlong id, jobject bytebuf, jlong size)
-{
- pthread_rwlock_rdlock(&g_vmem_rwlock);
- pthread_mutex_lock(g_vmem_mutex_ptr + id);
- jobject ret = NULL;
- if (NULL != bytebuf) {
- void* nativebuf = (void*)(*env)->GetDirectBufferAddress(env, bytebuf);
- if (nativebuf != NULL) {
- nativebuf = vmem_realloc(*(g_vmp_ptr + id), nativebuf, size);
- ret = NULL != nativebuf ? (*env)->NewDirectByteBuffer(env, nativebuf, size) : NULL;
- }
- }
- pthread_mutex_unlock(g_vmem_mutex_ptr + id);
- pthread_rwlock_unlock(&g_vmem_rwlock);
- return ret;
-}
-
-JNIEXPORT
-void JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_VMemServiceImpl_ndestroyByteBuffer(
- JNIEnv *env, jobject this, jlong id, jobject bytebuf)
-{
- pthread_rwlock_rdlock(&g_vmem_rwlock);
- pthread_mutex_lock(g_vmem_mutex_ptr + id);
- if (NULL != bytebuf) {
- void* nativebuf = (void*)(*env)->GetDirectBufferAddress(env, bytebuf);
- if (nativebuf != NULL) {
- vmem_free(*(g_vmp_ptr + id), nativebuf);
- }
- }
- pthread_mutex_unlock(g_vmem_mutex_ptr + id);
- pthread_rwlock_unlock(&g_vmem_rwlock);
-}
-
-JNIEXPORT
-jlong JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_VMemServiceImpl_ninit
- (JNIEnv *env, jclass this, jlong capacity, jstring pathname, jboolean isnew)
-{
- pthread_rwlock_wrlock(&g_vmem_rwlock);
- size_t ret = -1;
- VMEM *vmp = NULL;
- const char* mpathname = (*env)->GetStringUTFChars(env, pathname, NULL);
- if (NULL == mpathname) {
- pthread_rwlock_unlock(&g_vmem_rwlock);
- throw(env, "Big memory path not specified!");
- }
- if ((vmp = vmem_create(mpathname, capacity)) == NULL) {
- pthread_rwlock_unlock(&g_vmem_rwlock);
- throw(env, "Big memory init failure!");
- }
- g_vmp_ptr = realloc(g_vmp_ptr, (g_vmp_count + 1) * sizeof(VMEM*));
- g_vmem_mutex_ptr =
- realloc(g_vmem_mutex_ptr, (g_vmp_count + 1) * sizeof(pthread_mutex_t));
- if (NULL != g_vmp_ptr && NULL != g_vmem_mutex_ptr) {
- g_vmp_ptr[g_vmp_count] = vmp;
- pthread_mutex_init(g_vmem_mutex_ptr + g_vmp_count, NULL);
- ret = g_vmp_count;
- g_vmp_count++;
- } else {
- pthread_rwlock_unlock(&g_vmem_rwlock);
- throw(env, "Big memory init Out of memory!");
- }
- pthread_rwlock_unlock(&g_vmem_rwlock);
- return ret;
-}
-
-JNIEXPORT
-void JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_VMemServiceImpl_nclose
- (JNIEnv *env, jobject this, jlong id)
-{
- pthread_rwlock_rdlock(&g_vmem_rwlock);
- pthread_mutex_lock(g_vmem_mutex_ptr + id);
-
- pthread_mutex_unlock(g_vmem_mutex_ptr + id);
- pthread_rwlock_unlock(&g_vmem_rwlock);
-}
-
-
-__attribute__((destructor)) void fini(void)
-{
- int i;
- if (NULL != g_vmp_ptr) {
- for (i = 0; i < g_vmp_count; ++i) {
- if (NULL != *(g_vmp_ptr + i)){
- /* vmem_close(*(g_vmp_ptr + i)); undefined function */
- *(g_vmp_ptr + i) = NULL;
- pthread_mutex_destroy(g_vmem_mutex_ptr + i);
- }
- }
- free(g_vmp_ptr);
- g_vmp_ptr = NULL;
- free(g_vmem_mutex_ptr);
- g_vmem_mutex_ptr = NULL;
- g_vmp_count = 0;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/allocator-services/nvml-vmem-service/src/main/native/com_intel_mnemonic_service_allocatorservice_internal_VMemServiceImpl.h
----------------------------------------------------------------------
diff --git a/allocator-services/nvml-vmem-service/src/main/native/com_intel_mnemonic_service_allocatorservice_internal_VMemServiceImpl.h b/allocator-services/nvml-vmem-service/src/main/native/com_intel_mnemonic_service_allocatorservice_internal_VMemServiceImpl.h
deleted file mode 100644
index 6403d46..0000000
--- a/allocator-services/nvml-vmem-service/src/main/native/com_intel_mnemonic_service_allocatorservice_internal_VMemServiceImpl.h
+++ /dev/null
@@ -1,31 +0,0 @@
-/* DO NOT EDIT THIS FILE - it is machine generated */
-#include <jni.h>
-/* Header for class com_intel_mnemonic_service_allocatorservice_internal_VMemServiceImpl */
-
-#include "common.h"
-
-#ifndef _Included_com_intel_mnemonic_service_allocatorservice_internal_VMemServiceImpl
-#define _Included_com_intel_mnemonic_service_allocatorservice_internal_VMemServiceImpl
-#ifdef __cplusplus
-extern "C" {
-#endif
-/*
- * Class: com_intel_mnemonic_service_allocatorservice_internal_VMemServiceImpl
- * Method: jniInit
- * Signature: (II)V
- */
-JNIEXPORT void JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_VMemServiceImpl_jniInit
- (JNIEnv *, jobject, jint, jint);
-
-/*
- * Class: com_intel_mnemonic_service_allocatorservice_internal_VMemServiceImpl
- * Method: jniTerm
- * Signature: ()V
- */
-JNIEXPORT void JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_VMemServiceImpl_jniTerm
- (JNIEnv *, jobject);
-
-#ifdef __cplusplus
-}
-#endif
-#endif
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/allocator-services/nvml-vmem-service/src/main/native/org_apache_mnemonic_service_allocatorservice_internal_VMemServiceImpl.c
----------------------------------------------------------------------
diff --git a/allocator-services/nvml-vmem-service/src/main/native/org_apache_mnemonic_service_allocatorservice_internal_VMemServiceImpl.c b/allocator-services/nvml-vmem-service/src/main/native/org_apache_mnemonic_service_allocatorservice_internal_VMemServiceImpl.c
new file mode 100644
index 0000000..824b703
--- /dev/null
+++ b/allocator-services/nvml-vmem-service/src/main/native/org_apache_mnemonic_service_allocatorservice_internal_VMemServiceImpl.c
@@ -0,0 +1,185 @@
+/****************************************************************************************
+Revise Date: 20 Apr. 2014
+*****************************************************************************************/
+
+#include "org_apache_mnemonic_service_allocatorservice_internal_VMemServiceImpl.h"
+
+#include <libvmem.h>
+
+static VMEM **g_vmp_ptr = NULL;
+static size_t g_vmp_count = 0;
+
+static pthread_mutex_t *g_vmem_mutex_ptr = NULL;
+
+static pthread_rwlock_t g_vmem_rwlock = PTHREAD_RWLOCK_INITIALIZER;
+
+/******************************************************************************
+ ** JNI implementations
+ *****************************************************************************/
+
+JNIEXPORT
+jlong JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_VMemServiceImpl_nallocate(
+ JNIEnv* env,
+ jobject this, jlong id,
+ jlong size, jboolean initzero)
+{
+ pthread_rwlock_rdlock(&g_vmem_rwlock);
+ pthread_mutex_lock(g_vmem_mutex_ptr + id);
+ void* nativebuf = initzero ? vmem_calloc(*(g_vmp_ptr + id), 1, size) : vmem_malloc(*(g_vmp_ptr + id), size);
+ pthread_mutex_unlock(g_vmem_mutex_ptr + id);
+ pthread_rwlock_unlock(&g_vmem_rwlock);
+ return addr_to_java(nativebuf);
+}
+
+JNIEXPORT
+jlong JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_VMemServiceImpl_nreallocate(
+ JNIEnv* env,
+ jobject this, jlong id,
+ jlong address,
+ jlong size, jboolean initzero)
+{
+ pthread_rwlock_rdlock(&g_vmem_rwlock);
+ pthread_mutex_lock(g_vmem_mutex_ptr + id);
+
+ void* p = addr_from_java(address);
+
+ void* nativebuf = vmem_realloc(*(g_vmp_ptr + id), p, size);
+
+ pthread_mutex_unlock(g_vmem_mutex_ptr + id);
+ pthread_rwlock_unlock(&g_vmem_rwlock);
+ return addr_to_java(nativebuf);
+}
+
+JNIEXPORT
+void JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_VMemServiceImpl_nfree(
+ JNIEnv* env,
+ jobject this, jlong id,
+ jlong address)
+{
+ pthread_rwlock_rdlock(&g_vmem_rwlock);
+ pthread_mutex_lock(g_vmem_mutex_ptr + id);
+ void* nativebuf = addr_from_java(address);
+ if (nativebuf != NULL)
+ vmem_free(*(g_vmp_ptr + id), nativebuf);
+ pthread_mutex_unlock(g_vmem_mutex_ptr + id);
+ pthread_rwlock_unlock(&g_vmem_rwlock);
+}
+
+JNIEXPORT
+void JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_VMemServiceImpl_nsync(
+ JNIEnv* env,
+ jobject this, jlong id)
+{
+}
+
+JNIEXPORT
+jobject JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_VMemServiceImpl_ncreateByteBuffer(
+ JNIEnv *env, jobject this, jlong id, jlong size)
+{
+ pthread_rwlock_rdlock(&g_vmem_rwlock);
+ pthread_mutex_lock(g_vmem_mutex_ptr + id);
+ jobject ret = NULL;
+ void* nativebuf = vmem_malloc(*(g_vmp_ptr + id), size);
+ ret = NULL != nativebuf ? (*env)->NewDirectByteBuffer(env, nativebuf, size) : NULL;
+ pthread_mutex_unlock(g_vmem_mutex_ptr + id);
+ pthread_rwlock_unlock(&g_vmem_rwlock);
+ return ret;
+}
+
+JNIEXPORT
+jobject JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_VMemServiceImpl_nresizeByteBuffer(
+ JNIEnv *env, jobject this, jlong id, jobject bytebuf, jlong size)
+{
+ pthread_rwlock_rdlock(&g_vmem_rwlock);
+ pthread_mutex_lock(g_vmem_mutex_ptr + id);
+ jobject ret = NULL;
+ if (NULL != bytebuf) {
+ void* nativebuf = (void*)(*env)->GetDirectBufferAddress(env, bytebuf);
+ if (nativebuf != NULL) {
+ nativebuf = vmem_realloc(*(g_vmp_ptr + id), nativebuf, size);
+ ret = NULL != nativebuf ? (*env)->NewDirectByteBuffer(env, nativebuf, size) : NULL;
+ }
+ }
+ pthread_mutex_unlock(g_vmem_mutex_ptr + id);
+ pthread_rwlock_unlock(&g_vmem_rwlock);
+ return ret;
+}
+
+JNIEXPORT
+void JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_VMemServiceImpl_ndestroyByteBuffer(
+ JNIEnv *env, jobject this, jlong id, jobject bytebuf)
+{
+ pthread_rwlock_rdlock(&g_vmem_rwlock);
+ pthread_mutex_lock(g_vmem_mutex_ptr + id);
+ if (NULL != bytebuf) {
+ void* nativebuf = (void*)(*env)->GetDirectBufferAddress(env, bytebuf);
+ if (nativebuf != NULL) {
+ vmem_free(*(g_vmp_ptr + id), nativebuf);
+ }
+ }
+ pthread_mutex_unlock(g_vmem_mutex_ptr + id);
+ pthread_rwlock_unlock(&g_vmem_rwlock);
+}
+
+JNIEXPORT
+jlong JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_VMemServiceImpl_ninit
+ (JNIEnv *env, jclass this, jlong capacity, jstring pathname, jboolean isnew)
+{
+ pthread_rwlock_wrlock(&g_vmem_rwlock);
+ size_t ret = -1;
+ VMEM *vmp = NULL;
+ const char* mpathname = (*env)->GetStringUTFChars(env, pathname, NULL);
+ if (NULL == mpathname) {
+ pthread_rwlock_unlock(&g_vmem_rwlock);
+ throw(env, "Big memory path not specified!");
+ }
+ if ((vmp = vmem_create(mpathname, capacity)) == NULL) {
+ pthread_rwlock_unlock(&g_vmem_rwlock);
+ throw(env, "Big memory init failure!");
+ }
+ g_vmp_ptr = realloc(g_vmp_ptr, (g_vmp_count + 1) * sizeof(VMEM*));
+ g_vmem_mutex_ptr =
+ realloc(g_vmem_mutex_ptr, (g_vmp_count + 1) * sizeof(pthread_mutex_t));
+ if (NULL != g_vmp_ptr && NULL != g_vmem_mutex_ptr) {
+ g_vmp_ptr[g_vmp_count] = vmp;
+ pthread_mutex_init(g_vmem_mutex_ptr + g_vmp_count, NULL);
+ ret = g_vmp_count;
+ g_vmp_count++;
+ } else {
+ pthread_rwlock_unlock(&g_vmem_rwlock);
+ throw(env, "Big memory init Out of memory!");
+ }
+ pthread_rwlock_unlock(&g_vmem_rwlock);
+ return ret;
+}
+
+JNIEXPORT
+void JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_VMemServiceImpl_nclose
+ (JNIEnv *env, jobject this, jlong id)
+{
+ pthread_rwlock_rdlock(&g_vmem_rwlock);
+ pthread_mutex_lock(g_vmem_mutex_ptr + id);
+
+ pthread_mutex_unlock(g_vmem_mutex_ptr + id);
+ pthread_rwlock_unlock(&g_vmem_rwlock);
+}
+
+
+__attribute__((destructor)) void fini(void)
+{
+ int i;
+ if (NULL != g_vmp_ptr) {
+ for (i = 0; i < g_vmp_count; ++i) {
+ if (NULL != *(g_vmp_ptr + i)){
+ /* vmem_close(*(g_vmp_ptr + i)); undefined function */
+ *(g_vmp_ptr + i) = NULL;
+ pthread_mutex_destroy(g_vmem_mutex_ptr + i);
+ }
+ }
+ free(g_vmp_ptr);
+ g_vmp_ptr = NULL;
+ free(g_vmem_mutex_ptr);
+ g_vmem_mutex_ptr = NULL;
+ g_vmp_count = 0;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/allocator-services/nvml-vmem-service/src/main/native/org_apache_mnemonic_service_allocatorservice_internal_VMemServiceImpl.h
----------------------------------------------------------------------
diff --git a/allocator-services/nvml-vmem-service/src/main/native/org_apache_mnemonic_service_allocatorservice_internal_VMemServiceImpl.h b/allocator-services/nvml-vmem-service/src/main/native/org_apache_mnemonic_service_allocatorservice_internal_VMemServiceImpl.h
new file mode 100644
index 0000000..9a2e73a
--- /dev/null
+++ b/allocator-services/nvml-vmem-service/src/main/native/org_apache_mnemonic_service_allocatorservice_internal_VMemServiceImpl.h
@@ -0,0 +1,31 @@
+/* DO NOT EDIT THIS FILE - it is machine generated */
+#include <jni.h>
+/* Header for class org_apache_mnemonic_service_allocatorservice_internal_VMemServiceImpl */
+
+#include "common.h"
+
+#ifndef _Included_org_apache_mnemonic_service_allocatorservice_internal_VMemServiceImpl
+#define _Included_org_apache_mnemonic_service_allocatorservice_internal_VMemServiceImpl
+#ifdef __cplusplus
+extern "C" {
+#endif
+/*
+ * Class: org_apache_mnemonic_service_allocatorservice_internal_VMemServiceImpl
+ * Method: jniInit
+ * Signature: (II)V
+ */
+JNIEXPORT void JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_VMemServiceImpl_jniInit
+ (JNIEnv *, jobject, jint, jint);
+
+/*
+ * Class: org_apache_mnemonic_service_allocatorservice_internal_VMemServiceImpl
+ * Method: jniTerm
+ * Signature: ()V
+ */
+JNIEXPORT void JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_VMemServiceImpl_jniTerm
+ (JNIEnv *, jobject);
+
+#ifdef __cplusplus
+}
+#endif
+#endif
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/allocator-services/nvml-vmem-service/src/main/resources/META-INF/services/com.intel.mnemonic.service.allocatorservice.VolatileMemoryAllocatorService
----------------------------------------------------------------------
diff --git a/allocator-services/nvml-vmem-service/src/main/resources/META-INF/services/com.intel.mnemonic.service.allocatorservice.VolatileMemoryAllocatorService b/allocator-services/nvml-vmem-service/src/main/resources/META-INF/services/com.intel.mnemonic.service.allocatorservice.VolatileMemoryAllocatorService
deleted file mode 100644
index 439c8bd..0000000
--- a/allocator-services/nvml-vmem-service/src/main/resources/META-INF/services/com.intel.mnemonic.service.allocatorservice.VolatileMemoryAllocatorService
+++ /dev/null
@@ -1 +0,0 @@
-com.intel.mnemonic.service.allocatorservice.internal.VMemServiceImpl
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/allocator-services/nvml-vmem-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.allocatorservice.VolatileMemoryAllocatorService
----------------------------------------------------------------------
diff --git a/allocator-services/nvml-vmem-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.allocatorservice.VolatileMemoryAllocatorService b/allocator-services/nvml-vmem-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.allocatorservice.VolatileMemoryAllocatorService
new file mode 100644
index 0000000..4aacb0d
--- /dev/null
+++ b/allocator-services/nvml-vmem-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.allocatorservice.VolatileMemoryAllocatorService
@@ -0,0 +1 @@
+org.apache.mnemonic.service.allocatorservice.internal.VMemServiceImpl
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/allocator-services/pmalloc-service/pom.xml
----------------------------------------------------------------------
diff --git a/allocator-services/pmalloc-service/pom.xml b/allocator-services/pmalloc-service/pom.xml
index e9044b0..9f1bfb8 100644
--- a/allocator-services/pmalloc-service/pom.xml
+++ b/allocator-services/pmalloc-service/pom.xml
@@ -3,11 +3,11 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
- <groupId>com.intel.mnemonic.service.allocatorservice</groupId>
+ <groupId>org.apache.mnemonic.service.allocatorservice</groupId>
<artifactId>allocator-services-parent-project</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
- <groupId>com.intel.mnemonic.service.allocatorservice</groupId>
+ <groupId>org.apache.mnemonic.service.allocatorservice</groupId>
<artifactId>pmalloc-service</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Mnemonic Project PMalloc Allocator</name>
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/allocator-services/pmalloc-service/src/main/java/com/intel/mnemonic/service/allocatorservice/internal/PMallocServiceImpl.java
----------------------------------------------------------------------
diff --git a/allocator-services/pmalloc-service/src/main/java/com/intel/mnemonic/service/allocatorservice/internal/PMallocServiceImpl.java b/allocator-services/pmalloc-service/src/main/java/com/intel/mnemonic/service/allocatorservice/internal/PMallocServiceImpl.java
deleted file mode 100644
index 735762e..0000000
--- a/allocator-services/pmalloc-service/src/main/java/com/intel/mnemonic/service/allocatorservice/internal/PMallocServiceImpl.java
+++ /dev/null
@@ -1,150 +0,0 @@
-package com.intel.mnemonic.service.allocatorservice.internal;
-
-import com.intel.mnemonic.service.allocatorservice.NonVolatileMemoryAllocatorService;
-import org.flowcomputing.commons.primitives.*;
-import java.nio.ByteBuffer;
-
-
-public class PMallocServiceImpl implements NonVolatileMemoryAllocatorService {
- static {
- try {
- NativeLibraryLoader.loadFromJar("pmallocallocator");
- } catch (Exception e) {
- throw new Error(e);
- }
- }
-
- @Override
- public String getServiceId() {
- return "pmalloc";
- }
-
- @Override
- public long init(long capacity, String uri, boolean isnew) {
- return ninit(capacity, uri, isnew);
- }
-
- @Override
- public void close(long id) {
- nclose(id);
- }
-
- @Override
- public void sync(long id) {
- nsync(id);
- }
-
- @Override
- public long allocate(long id, long size, boolean initzero) {
- return nallocate(id, size, initzero);
- }
-
- @Override
- public long reallocate(long id, long address, long size, boolean initzero) {
- return nreallocate(id, address, size, initzero);
- }
-
- @Override
- public void free(long id, long address) {
- nfree(id, address);
- }
-
- @Override
- public ByteBuffer createByteBuffer(long id, long size) {
- return ncreateByteBuffer(id, size);
- }
-
- @Override
- public ByteBuffer resizeByteBuffer(long id, ByteBuffer bytebuf, long size) {
- return nresizeByteBuffer(id, bytebuf, size);
- }
-
- @Override
- public void destroyByteBuffer(long id, ByteBuffer bytebuf) {
- ndestroyByteBuffer(id, bytebuf);
- }
-
- @Override
- public ByteBuffer retrieveByteBuffer(long id, long handler) {
- return nretrieveByteBuffer(id, handler);
- }
-
- @Override
- public long retrieveSize(long id, long handler) {
- return nretrieveSize(id, handler);
- }
-
- @Override
- public long getByteBufferHandler(long id, ByteBuffer buf) {
- return ngetByteBufferHandler(id, buf);
- }
-
- @Override
- public void setHandler(long id, long key, long handler) {
- nsetHandler(id, key, handler);
- }
-
- @Override
- public long getHandler(long id, long key) {
- return ngetHandler(id, key);
- }
-
- @Override
- public long handlerCapacity(long id) {
- return nhandlerCapacity(id);
- }
-
- @Override
- public long getBaseAddress(long id) {
- return ngetBaseAddress(id);
- }
-
- protected native long ninit(long capacity, String uri, boolean isnew);
-
-
- protected native void nclose(long id);
-
-
- protected native void nsync(long id);
-
-
- protected native long nallocate(long id, long size, boolean initzero);
-
-
- protected native long nreallocate(long id, long address, long size, boolean initzero);
-
-
- protected native void nfree(long id, long address);
-
-
- protected native ByteBuffer ncreateByteBuffer(long id, long size);
-
-
- protected native ByteBuffer nresizeByteBuffer(long id, ByteBuffer bytebuf, long size);
-
-
- protected native void ndestroyByteBuffer(long id, ByteBuffer bytebuf);
-
-
- protected native ByteBuffer nretrieveByteBuffer(long id, long handler);
-
-
- protected native long nretrieveSize(long id, long handler);
-
-
- protected native long ngetByteBufferHandler(long id, ByteBuffer buf);
-
-
- protected native void nsetHandler(long id, long key, long handler);
-
-
- protected native long ngetHandler(long id, long key);
-
-
- protected native long nhandlerCapacity(long id);
-
-
- protected native long ngetBaseAddress(long id);
-
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/allocator-services/pmalloc-service/src/main/java/org/apache/mnemonic/service/allocatorservice/internal/PMallocServiceImpl.java
----------------------------------------------------------------------
diff --git a/allocator-services/pmalloc-service/src/main/java/org/apache/mnemonic/service/allocatorservice/internal/PMallocServiceImpl.java b/allocator-services/pmalloc-service/src/main/java/org/apache/mnemonic/service/allocatorservice/internal/PMallocServiceImpl.java
new file mode 100644
index 0000000..f96d3b3
--- /dev/null
+++ b/allocator-services/pmalloc-service/src/main/java/org/apache/mnemonic/service/allocatorservice/internal/PMallocServiceImpl.java
@@ -0,0 +1,150 @@
+package org.apache.mnemonic.service.allocatorservice.internal;
+
+import org.apache.mnemonic.service.allocatorservice.NonVolatileMemoryAllocatorService;
+import org.flowcomputing.commons.primitives.*;
+import java.nio.ByteBuffer;
+
+
+public class PMallocServiceImpl implements NonVolatileMemoryAllocatorService {
+ static {
+ try {
+ NativeLibraryLoader.loadFromJar("pmallocallocator");
+ } catch (Exception e) {
+ throw new Error(e);
+ }
+ }
+
+ @Override
+ public String getServiceId() {
+ return "pmalloc";
+ }
+
+ @Override
+ public long init(long capacity, String uri, boolean isnew) {
+ return ninit(capacity, uri, isnew);
+ }
+
+ @Override
+ public void close(long id) {
+ nclose(id);
+ }
+
+ @Override
+ public void sync(long id) {
+ nsync(id);
+ }
+
+ @Override
+ public long allocate(long id, long size, boolean initzero) {
+ return nallocate(id, size, initzero);
+ }
+
+ @Override
+ public long reallocate(long id, long address, long size, boolean initzero) {
+ return nreallocate(id, address, size, initzero);
+ }
+
+ @Override
+ public void free(long id, long address) {
+ nfree(id, address);
+ }
+
+ @Override
+ public ByteBuffer createByteBuffer(long id, long size) {
+ return ncreateByteBuffer(id, size);
+ }
+
+ @Override
+ public ByteBuffer resizeByteBuffer(long id, ByteBuffer bytebuf, long size) {
+ return nresizeByteBuffer(id, bytebuf, size);
+ }
+
+ @Override
+ public void destroyByteBuffer(long id, ByteBuffer bytebuf) {
+ ndestroyByteBuffer(id, bytebuf);
+ }
+
+ @Override
+ public ByteBuffer retrieveByteBuffer(long id, long handler) {
+ return nretrieveByteBuffer(id, handler);
+ }
+
+ @Override
+ public long retrieveSize(long id, long handler) {
+ return nretrieveSize(id, handler);
+ }
+
+ @Override
+ public long getByteBufferHandler(long id, ByteBuffer buf) {
+ return ngetByteBufferHandler(id, buf);
+ }
+
+ @Override
+ public void setHandler(long id, long key, long handler) {
+ nsetHandler(id, key, handler);
+ }
+
+ @Override
+ public long getHandler(long id, long key) {
+ return ngetHandler(id, key);
+ }
+
+ @Override
+ public long handlerCapacity(long id) {
+ return nhandlerCapacity(id);
+ }
+
+ @Override
+ public long getBaseAddress(long id) {
+ return ngetBaseAddress(id);
+ }
+
+ protected native long ninit(long capacity, String uri, boolean isnew);
+
+
+ protected native void nclose(long id);
+
+
+ protected native void nsync(long id);
+
+
+ protected native long nallocate(long id, long size, boolean initzero);
+
+
+ protected native long nreallocate(long id, long address, long size, boolean initzero);
+
+
+ protected native void nfree(long id, long address);
+
+
+ protected native ByteBuffer ncreateByteBuffer(long id, long size);
+
+
+ protected native ByteBuffer nresizeByteBuffer(long id, ByteBuffer bytebuf, long size);
+
+
+ protected native void ndestroyByteBuffer(long id, ByteBuffer bytebuf);
+
+
+ protected native ByteBuffer nretrieveByteBuffer(long id, long handler);
+
+
+ protected native long nretrieveSize(long id, long handler);
+
+
+ protected native long ngetByteBufferHandler(long id, ByteBuffer buf);
+
+
+ protected native void nsetHandler(long id, long key, long handler);
+
+
+ protected native long ngetHandler(long id, long key);
+
+
+ protected native long nhandlerCapacity(long id);
+
+
+ protected native long ngetBaseAddress(long id);
+
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/allocator-services/pmalloc-service/src/main/native/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/allocator-services/pmalloc-service/src/main/native/CMakeLists.txt b/allocator-services/pmalloc-service/src/main/native/CMakeLists.txt
index 2715d73..39985de 100644
--- a/allocator-services/pmalloc-service/src/main/native/CMakeLists.txt
+++ b/allocator-services/pmalloc-service/src/main/native/CMakeLists.txt
@@ -26,7 +26,7 @@ if (NOT LIBPMALLOC_LIBRARIES)
message(FATAL_ERROR "not found pmalloc library")
endif (NOT LIBPMALLOC_LIBRARIES)
-add_library(pmallocallocator SHARED common.c com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl.c)
+add_library(pmallocallocator SHARED common.c org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl.c)
target_include_directories(pmallocallocator PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(pmallocallocator pmalloc)
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/allocator-services/pmalloc-service/src/main/native/com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl.c
----------------------------------------------------------------------
diff --git a/allocator-services/pmalloc-service/src/main/native/com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl.c b/allocator-services/pmalloc-service/src/main/native/com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl.c
deleted file mode 100644
index 25d5a30..0000000
--- a/allocator-services/pmalloc-service/src/main/native/com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl.c
+++ /dev/null
@@ -1,319 +0,0 @@
-/****************************************************************************************
-Revise Date: 20 Apr. 2014
-*****************************************************************************************/
-
-#include "com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl.h"
-
-#include <pmalloc.h>
-
-typedef struct {
- //size_t size;
- jlong size;
-} PMBHeader;
-
-#define PMBHSZ (sizeof(PMBHeader))
-
-static void **g_pmp_ptr = NULL;
-static size_t g_pmp_count = 0;
-
-static pthread_mutex_t *g_pmalloc_mutex_ptr = NULL;
-
-static pthread_rwlock_t g_pmp_rwlock = PTHREAD_RWLOCK_INITIALIZER;
-
-/******************************************************************************
- ** JNI implementations
- *****************************************************************************/
-
-JNIEXPORT
-jlong JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_nallocate(
- JNIEnv* env,
- jobject this, jlong id,
- jlong size, jboolean initzero)
-{
- pthread_rwlock_rdlock(&g_pmp_rwlock);
- pthread_mutex_lock(g_pmalloc_mutex_ptr + id);
- jlong ret = 0L;
- void *md = *(g_pmp_ptr + id);
- void* nativebuf = initzero ? pmcalloc(md, 1, size + PMBHSZ) : pmalloc(md, size + PMBHSZ);
- if (NULL != nativebuf) {
- ((PMBHeader *)nativebuf)->size = size + PMBHSZ;
- ret = addr_to_java(nativebuf + PMBHSZ);
-// fprintf(stderr, "### nallocate size: %lld, %X, header size: %ld ### \n",
-// ((PMBHeader *)nativebuf)->size, nativebuf-b_addr(*(g_pmp_ptr + id)), PMBHSZ);
- }
- pthread_mutex_unlock(g_pmalloc_mutex_ptr + id);
- pthread_rwlock_unlock(&g_pmp_rwlock);
- return ret;
-}
-
-JNIEXPORT
-jlong JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_nreallocate(
- JNIEnv* env,
- jobject this, jlong id,
- jlong address,
- jlong size, jboolean initzero)
-{
- pthread_rwlock_rdlock(&g_pmp_rwlock);
- pthread_mutex_lock(g_pmalloc_mutex_ptr + id);
- jlong ret = 0L;
- void *md = *(g_pmp_ptr + id);
- void* nativebuf = NULL;
- void* p = addr_from_java(address);
- if (NULL != p) {
- nativebuf = pmrealloc(md, p - PMBHSZ, size + PMBHSZ);
- } else {
- nativebuf = initzero ? pmcalloc(md, 1, size + PMBHSZ) : pmalloc(md, size + PMBHSZ);
- }
- if (nativebuf != NULL) {
- ((PMBHeader *)nativebuf)->size = size + PMBHSZ;
- ret = addr_to_java(nativebuf + PMBHSZ);
- }
- pthread_mutex_unlock(g_pmalloc_mutex_ptr + id);
- pthread_rwlock_unlock(&g_pmp_rwlock);
- return ret;
-}
-
-JNIEXPORT
-void JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_nfree(
- JNIEnv* env,
- jobject this, jlong id,
- jlong address)
-{
- pthread_rwlock_rdlock(&g_pmp_rwlock);
- pthread_mutex_lock(g_pmalloc_mutex_ptr + id);
- //fprintf(stderr, "nfree Get Called %ld, %X\n", id, address);
- void *md = *(g_pmp_ptr + id);
- void* nativebuf = addr_from_java(address);
- if (nativebuf != NULL) {
-// fprintf(stderr, "### nfree size: %lld, %X ###, header size: %ld \n",
-// ((PMBHeader *)(nativebuf - PMBHSZ))->size, nativebuf - PMBHSZ-b_addr(*(g_pmp_ptr + id)), PMBHSZ);
- pmfree(md, nativebuf - PMBHSZ);
- }
- pthread_mutex_unlock(g_pmalloc_mutex_ptr + id);
- pthread_rwlock_unlock(&g_pmp_rwlock);
-}
-
-JNIEXPORT
-void JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_nsync(
- JNIEnv* env,
- jobject this, jlong id)
-{
-}
-
-JNIEXPORT
-jobject JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_ncreateByteBuffer(
- JNIEnv *env, jobject this, jlong id, jlong size)
-{
- pthread_rwlock_rdlock(&g_pmp_rwlock);
- pthread_mutex_lock(g_pmalloc_mutex_ptr + id);
- jobject ret = NULL;
- void *md = *(g_pmp_ptr + id);
- void* nativebuf = pmalloc(md, size + PMBHSZ);
- if (NULL != nativebuf) {
- ((PMBHeader *)nativebuf)->size = size + PMBHSZ;
- ret = (*env)->NewDirectByteBuffer(env, nativebuf + PMBHSZ, size);
-// fprintf(stderr, "### ncreateByteBuffer size: %lld, %X ###, header size: %ld \n",
-// ((PMBHeader *)nativebuf)->size, nativebuf-b_addr(*(g_pmp_ptr + id)), PMBHSZ);
- }
- pthread_mutex_unlock(g_pmalloc_mutex_ptr + id);
- pthread_rwlock_unlock(&g_pmp_rwlock);
- return ret;
-}
-
-JNIEXPORT
-jobject JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_nretrieveByteBuffer(
- JNIEnv *env, jobject this, jlong id, jlong e_addr)
-{
- jobject ret = NULL;
- void* p = addr_from_java(e_addr);
- if (NULL != p) {
- void* nativebuf = p - PMBHSZ;
- ret = (*env)->NewDirectByteBuffer(env, p, ((PMBHeader *)nativebuf)->size - PMBHSZ);
- }
- return ret;
-}
-
-JNIEXPORT
-jlong JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_nretrieveSize(
- JNIEnv *env, jobject this, jlong id, jlong e_addr)
-{
- jlong ret = 0L;
- void* p = addr_from_java(e_addr);
- if (NULL != p) {
- void* nativebuf = p - PMBHSZ;
- ret = ((PMBHeader *)nativebuf)->size - PMBHSZ;
-// fprintf(stderr, "### nretrieveSize size: %lld, %X ###, header size: %ld \n",
-// ((PMBHeader *)nativebuf)->size, nativebuf-b_addr(*(g_pmp_ptr + id)), PMBHSZ);
- }
- return ret;
-}
-
-JNIEXPORT
-jlong JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_ngetByteBufferHandler(
- JNIEnv *env, jobject this, jlong id, jobject bytebuf)
-{
-// fprintf(stderr, "ngetByteBufferAddress Get Called %X, %X\n", env, bytebuf);
- jlong ret = 0L;
- if (NULL != bytebuf) {
- void* nativebuf = (*env)->GetDirectBufferAddress(env, bytebuf);
-// fprintf(stderr, "ngetByteBufferAddress Get Native address %X\n", nativebuf);
- ret = addr_to_java(nativebuf);
- }
-// fprintf(stderr, "ngetByteBufferAddress returned address %016lx\n", ret);
- return ret;
-}
-
-JNIEXPORT
-jobject JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_nresizeByteBuffer(
- JNIEnv *env, jobject this, jlong id, jobject bytebuf, jlong size)
-{
- pthread_rwlock_rdlock(&g_pmp_rwlock);
- pthread_mutex_lock(g_pmalloc_mutex_ptr + id);
- jobject ret = NULL;
- void *md = *(g_pmp_ptr + id);
- if (NULL != bytebuf) {
- void* nativebuf = (*env)->GetDirectBufferAddress(env, bytebuf);
- if (nativebuf != NULL) {
- nativebuf = pmrealloc(md, nativebuf - PMBHSZ, size + PMBHSZ);
- if (NULL != nativebuf) {
- ((PMBHeader *)nativebuf)->size = size + PMBHSZ;
- ret = (*env)->NewDirectByteBuffer(env, nativebuf + PMBHSZ, size);
- }
- }
- }
- pthread_mutex_unlock(g_pmalloc_mutex_ptr + id);
- pthread_rwlock_unlock(&g_pmp_rwlock);
- return ret;
-}
-
-JNIEXPORT
-void JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_ndestroyByteBuffer(
- JNIEnv *env, jobject this, jlong id, jobject bytebuf)
-{
- pthread_rwlock_rdlock(&g_pmp_rwlock);
- pthread_mutex_lock(g_pmalloc_mutex_ptr + id);
- void *md = *(g_pmp_ptr + id);
- if (NULL != bytebuf) {
- void* nativebuf = (*env)->GetDirectBufferAddress(env, bytebuf);
- if (nativebuf != NULL) {
-// fprintf(stderr, "### ndestroyByteBuffer size: %lld, %X, header size: %ld ### \n",
-// ((PMBHeader *)(nativebuf - PMBHSZ))->size, nativebuf - PMBHSZ -b_addr(*(g_pmp_ptr + id)), PMBHSZ);
- pmfree(md, nativebuf - PMBHSZ);
- }
- }
- pthread_mutex_unlock(g_pmalloc_mutex_ptr + id);
- pthread_rwlock_unlock(&g_pmp_rwlock);
-}
-
-JNIEXPORT
-void JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_nsetHandler(
- JNIEnv *env, jobject this, jlong id, jlong key, jlong value)
-{
- pthread_rwlock_rdlock(&g_pmp_rwlock);
- pthread_mutex_lock(g_pmalloc_mutex_ptr + id);
- void *md = *(g_pmp_ptr + id);
- if (id < PMALLOC_KEYS && id >= 0) {
- pmalloc_setkey(md, key, (void*)value);
- }
- pthread_mutex_unlock(g_pmalloc_mutex_ptr + id);
- pthread_rwlock_unlock(&g_pmp_rwlock);
-}
-
-JNIEXPORT
-jlong JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_ngetHandler(
- JNIEnv *env, jobject this, jlong id, jlong key)
-{
- pthread_rwlock_rdlock(&g_pmp_rwlock);
- pthread_mutex_lock(g_pmalloc_mutex_ptr + id);
- void *md = *(g_pmp_ptr + id);
- jlong ret = (id < PMALLOC_KEYS && id >= 0) ? (long)pmalloc_getkey(md, key) : 0;
- pthread_mutex_unlock(g_pmalloc_mutex_ptr + id);
- pthread_rwlock_unlock(&g_pmp_rwlock);
- return ret;
-}
-
-JNIEXPORT
-jlong JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_nhandlerCapacity(
- JNIEnv *env, jobject this)
-{
- return PMALLOC_KEYS;
-}
-
-JNIEXPORT
-jlong JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_ngetBaseAddress(
- JNIEnv *env, jobject this, jlong id)
-{
- pthread_rwlock_rdlock(&g_pmp_rwlock);
- void *md = *(g_pmp_ptr + id);
- jlong ret = (long)b_addr(md);
- pthread_rwlock_unlock(&g_pmp_rwlock);
- return ret;
-}
-
-JNIEXPORT
-jlong JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_ninit
- (JNIEnv *env, jclass this, jlong capacity, jstring pathname, jboolean isnew)
-{
- pthread_rwlock_wrlock(&g_pmp_rwlock);
- size_t ret = -1;
- void *md = NULL;
- const char* mpathname = (*env)->GetStringUTFChars(env, pathname, NULL);
- if (NULL == mpathname) {
- pthread_rwlock_unlock(&g_pmp_rwlock);
- throw(env, "Big memory path not specified!");
- }
- if ((md = pmopen(mpathname, NULL, capacity)) == NULL) {
- pthread_rwlock_unlock(&g_pmp_rwlock);
- throw(env, "Big memory init failure!");
- }
- (*env)->ReleaseStringUTFChars(env, pathname, mpathname);
- g_pmp_ptr = realloc(g_pmp_ptr, (g_pmp_count + 1) * sizeof(void*));
- g_pmalloc_mutex_ptr =
- realloc(g_pmalloc_mutex_ptr, (g_pmp_count + 1) * sizeof(pthread_mutex_t));
- if (NULL != g_pmp_ptr && NULL != g_pmalloc_mutex_ptr) {
- *(g_pmp_ptr + g_pmp_count) = md;
- pthread_mutex_init(g_pmalloc_mutex_ptr + g_pmp_count, NULL);
- ret = g_pmp_count;
- ++g_pmp_count;
- } else {
- pthread_rwlock_unlock(&g_pmp_rwlock);
- throw(env, "Big memory init Out of memory!");
- }
- pthread_rwlock_unlock(&g_pmp_rwlock);
- return ret;
-}
-
-JNIEXPORT
-void JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_nclose
- (JNIEnv *env, jobject this, jlong id)
-{
- pthread_rwlock_rdlock(&g_pmp_rwlock);
- pthread_mutex_lock(g_pmalloc_mutex_ptr + id);
- void *md = *(g_pmp_ptr + id);
- pmclose(md);
- *(g_pmp_ptr + id) = NULL;
- pthread_mutex_unlock(g_pmalloc_mutex_ptr + id);
- pthread_mutex_destroy(g_pmalloc_mutex_ptr + id);
- pthread_rwlock_unlock(&g_pmp_rwlock);
-}
-
-
-__attribute__((destructor)) void fini(void)
-{
- int i;
- if (NULL != g_pmp_ptr) {
- for (i = 0; i < g_pmp_count; ++i) {
- if (NULL != *(g_pmp_ptr + i)){
- pmclose(*(g_pmp_ptr + i));
- *(g_pmp_ptr + i) = NULL;
- pthread_mutex_destroy(g_pmalloc_mutex_ptr + i);
- }
- }
- free(g_pmp_ptr);
- g_pmp_ptr = NULL;
- free(g_pmalloc_mutex_ptr);
- g_pmalloc_mutex_ptr = NULL;
- g_pmp_count = 0;
- }
- pthread_rwlock_destroy(&g_pmp_rwlock);
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/allocator-services/pmalloc-service/src/main/native/com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl.h
----------------------------------------------------------------------
diff --git a/allocator-services/pmalloc-service/src/main/native/com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl.h b/allocator-services/pmalloc-service/src/main/native/com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl.h
deleted file mode 100644
index edc59fc..0000000
--- a/allocator-services/pmalloc-service/src/main/native/com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl.h
+++ /dev/null
@@ -1,31 +0,0 @@
-/* DO NOT EDIT THIS FILE - it is machine generated */
-#include <jni.h>
-/* Header for class PMallocServiceImpl */
-
-#include "common.h"
-
-#ifndef _Included_com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl
-#define _Included_com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl
-#ifdef __cplusplus
-extern "C" {
-#endif
-/*
- * Class: com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl
- * Method: jniInit
- * Signature: (II)V
- */
-JNIEXPORT void JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_jniInit
- (JNIEnv *, jobject, jint, jint);
-
-/*
- * Class: com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl
- * Method: jniTerm
- * Signature: ()V
- */
-JNIEXPORT void JNICALL Java_com_intel_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_jniTerm
- (JNIEnv *, jobject);
-
-#ifdef __cplusplus
-}
-#endif
-#endif
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/allocator-services/pmalloc-service/src/main/native/org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl.c
----------------------------------------------------------------------
diff --git a/allocator-services/pmalloc-service/src/main/native/org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl.c b/allocator-services/pmalloc-service/src/main/native/org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl.c
new file mode 100644
index 0000000..c377e82
--- /dev/null
+++ b/allocator-services/pmalloc-service/src/main/native/org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl.c
@@ -0,0 +1,319 @@
+/****************************************************************************************
+Revise Date: 20 Apr. 2014
+*****************************************************************************************/
+
+#include "org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl.h"
+
+#include <pmalloc.h>
+
+typedef struct {
+ //size_t size;
+ jlong size;
+} PMBHeader;
+
+#define PMBHSZ (sizeof(PMBHeader))
+
+static void **g_pmp_ptr = NULL;
+static size_t g_pmp_count = 0;
+
+static pthread_mutex_t *g_pmalloc_mutex_ptr = NULL;
+
+static pthread_rwlock_t g_pmp_rwlock = PTHREAD_RWLOCK_INITIALIZER;
+
+/******************************************************************************
+ ** JNI implementations
+ *****************************************************************************/
+
+JNIEXPORT
+jlong JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_nallocate(
+ JNIEnv* env,
+ jobject this, jlong id,
+ jlong size, jboolean initzero)
+{
+ pthread_rwlock_rdlock(&g_pmp_rwlock);
+ pthread_mutex_lock(g_pmalloc_mutex_ptr + id);
+ jlong ret = 0L;
+ void *md = *(g_pmp_ptr + id);
+ void* nativebuf = initzero ? pmcalloc(md, 1, size + PMBHSZ) : pmalloc(md, size + PMBHSZ);
+ if (NULL != nativebuf) {
+ ((PMBHeader *)nativebuf)->size = size + PMBHSZ;
+ ret = addr_to_java(nativebuf + PMBHSZ);
+// fprintf(stderr, "### nallocate size: %lld, %X, header size: %ld ### \n",
+// ((PMBHeader *)nativebuf)->size, nativebuf-b_addr(*(g_pmp_ptr + id)), PMBHSZ);
+ }
+ pthread_mutex_unlock(g_pmalloc_mutex_ptr + id);
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+ return ret;
+}
+
+JNIEXPORT
+jlong JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_nreallocate(
+ JNIEnv* env,
+ jobject this, jlong id,
+ jlong address,
+ jlong size, jboolean initzero)
+{
+ pthread_rwlock_rdlock(&g_pmp_rwlock);
+ pthread_mutex_lock(g_pmalloc_mutex_ptr + id);
+ jlong ret = 0L;
+ void *md = *(g_pmp_ptr + id);
+ void* nativebuf = NULL;
+ void* p = addr_from_java(address);
+ if (NULL != p) {
+ nativebuf = pmrealloc(md, p - PMBHSZ, size + PMBHSZ);
+ } else {
+ nativebuf = initzero ? pmcalloc(md, 1, size + PMBHSZ) : pmalloc(md, size + PMBHSZ);
+ }
+ if (nativebuf != NULL) {
+ ((PMBHeader *)nativebuf)->size = size + PMBHSZ;
+ ret = addr_to_java(nativebuf + PMBHSZ);
+ }
+ pthread_mutex_unlock(g_pmalloc_mutex_ptr + id);
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+ return ret;
+}
+
+JNIEXPORT
+void JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_nfree(
+ JNIEnv* env,
+ jobject this, jlong id,
+ jlong address)
+{
+ pthread_rwlock_rdlock(&g_pmp_rwlock);
+ pthread_mutex_lock(g_pmalloc_mutex_ptr + id);
+ //fprintf(stderr, "nfree Get Called %ld, %X\n", id, address);
+ void *md = *(g_pmp_ptr + id);
+ void* nativebuf = addr_from_java(address);
+ if (nativebuf != NULL) {
+// fprintf(stderr, "### nfree size: %lld, %X ###, header size: %ld \n",
+// ((PMBHeader *)(nativebuf - PMBHSZ))->size, nativebuf - PMBHSZ-b_addr(*(g_pmp_ptr + id)), PMBHSZ);
+ pmfree(md, nativebuf - PMBHSZ);
+ }
+ pthread_mutex_unlock(g_pmalloc_mutex_ptr + id);
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+}
+
+JNIEXPORT
+void JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_nsync(
+ JNIEnv* env,
+ jobject this, jlong id)
+{
+}
+
+JNIEXPORT
+jobject JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_ncreateByteBuffer(
+ JNIEnv *env, jobject this, jlong id, jlong size)
+{
+ pthread_rwlock_rdlock(&g_pmp_rwlock);
+ pthread_mutex_lock(g_pmalloc_mutex_ptr + id);
+ jobject ret = NULL;
+ void *md = *(g_pmp_ptr + id);
+ void* nativebuf = pmalloc(md, size + PMBHSZ);
+ if (NULL != nativebuf) {
+ ((PMBHeader *)nativebuf)->size = size + PMBHSZ;
+ ret = (*env)->NewDirectByteBuffer(env, nativebuf + PMBHSZ, size);
+// fprintf(stderr, "### ncreateByteBuffer size: %lld, %X ###, header size: %ld \n",
+// ((PMBHeader *)nativebuf)->size, nativebuf-b_addr(*(g_pmp_ptr + id)), PMBHSZ);
+ }
+ pthread_mutex_unlock(g_pmalloc_mutex_ptr + id);
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+ return ret;
+}
+
+JNIEXPORT
+jobject JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_nretrieveByteBuffer(
+ JNIEnv *env, jobject this, jlong id, jlong e_addr)
+{
+ jobject ret = NULL;
+ void* p = addr_from_java(e_addr);
+ if (NULL != p) {
+ void* nativebuf = p - PMBHSZ;
+ ret = (*env)->NewDirectByteBuffer(env, p, ((PMBHeader *)nativebuf)->size - PMBHSZ);
+ }
+ return ret;
+}
+
+JNIEXPORT
+jlong JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_nretrieveSize(
+ JNIEnv *env, jobject this, jlong id, jlong e_addr)
+{
+ jlong ret = 0L;
+ void* p = addr_from_java(e_addr);
+ if (NULL != p) {
+ void* nativebuf = p - PMBHSZ;
+ ret = ((PMBHeader *)nativebuf)->size - PMBHSZ;
+// fprintf(stderr, "### nretrieveSize size: %lld, %X ###, header size: %ld \n",
+// ((PMBHeader *)nativebuf)->size, nativebuf-b_addr(*(g_pmp_ptr + id)), PMBHSZ);
+ }
+ return ret;
+}
+
+JNIEXPORT
+jlong JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_ngetByteBufferHandler(
+ JNIEnv *env, jobject this, jlong id, jobject bytebuf)
+{
+// fprintf(stderr, "ngetByteBufferAddress Get Called %X, %X\n", env, bytebuf);
+ jlong ret = 0L;
+ if (NULL != bytebuf) {
+ void* nativebuf = (*env)->GetDirectBufferAddress(env, bytebuf);
+// fprintf(stderr, "ngetByteBufferAddress Get Native address %X\n", nativebuf);
+ ret = addr_to_java(nativebuf);
+ }
+// fprintf(stderr, "ngetByteBufferAddress returned address %016lx\n", ret);
+ return ret;
+}
+
+JNIEXPORT
+jobject JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_nresizeByteBuffer(
+ JNIEnv *env, jobject this, jlong id, jobject bytebuf, jlong size)
+{
+ pthread_rwlock_rdlock(&g_pmp_rwlock);
+ pthread_mutex_lock(g_pmalloc_mutex_ptr + id);
+ jobject ret = NULL;
+ void *md = *(g_pmp_ptr + id);
+ if (NULL != bytebuf) {
+ void* nativebuf = (*env)->GetDirectBufferAddress(env, bytebuf);
+ if (nativebuf != NULL) {
+ nativebuf = pmrealloc(md, nativebuf - PMBHSZ, size + PMBHSZ);
+ if (NULL != nativebuf) {
+ ((PMBHeader *)nativebuf)->size = size + PMBHSZ;
+ ret = (*env)->NewDirectByteBuffer(env, nativebuf + PMBHSZ, size);
+ }
+ }
+ }
+ pthread_mutex_unlock(g_pmalloc_mutex_ptr + id);
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+ return ret;
+}
+
+JNIEXPORT
+void JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_ndestroyByteBuffer(
+ JNIEnv *env, jobject this, jlong id, jobject bytebuf)
+{
+ pthread_rwlock_rdlock(&g_pmp_rwlock);
+ pthread_mutex_lock(g_pmalloc_mutex_ptr + id);
+ void *md = *(g_pmp_ptr + id);
+ if (NULL != bytebuf) {
+ void* nativebuf = (*env)->GetDirectBufferAddress(env, bytebuf);
+ if (nativebuf != NULL) {
+// fprintf(stderr, "### ndestroyByteBuffer size: %lld, %X, header size: %ld ### \n",
+// ((PMBHeader *)(nativebuf - PMBHSZ))->size, nativebuf - PMBHSZ -b_addr(*(g_pmp_ptr + id)), PMBHSZ);
+ pmfree(md, nativebuf - PMBHSZ);
+ }
+ }
+ pthread_mutex_unlock(g_pmalloc_mutex_ptr + id);
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+}
+
+JNIEXPORT
+void JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_nsetHandler(
+ JNIEnv *env, jobject this, jlong id, jlong key, jlong value)
+{
+ pthread_rwlock_rdlock(&g_pmp_rwlock);
+ pthread_mutex_lock(g_pmalloc_mutex_ptr + id);
+ void *md = *(g_pmp_ptr + id);
+ if (id < PMALLOC_KEYS && id >= 0) {
+ pmalloc_setkey(md, key, (void*)value);
+ }
+ pthread_mutex_unlock(g_pmalloc_mutex_ptr + id);
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+}
+
+JNIEXPORT
+jlong JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_ngetHandler(
+ JNIEnv *env, jobject this, jlong id, jlong key)
+{
+ pthread_rwlock_rdlock(&g_pmp_rwlock);
+ pthread_mutex_lock(g_pmalloc_mutex_ptr + id);
+ void *md = *(g_pmp_ptr + id);
+ jlong ret = (id < PMALLOC_KEYS && id >= 0) ? (long)pmalloc_getkey(md, key) : 0;
+ pthread_mutex_unlock(g_pmalloc_mutex_ptr + id);
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+ return ret;
+}
+
+JNIEXPORT
+jlong JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_nhandlerCapacity(
+ JNIEnv *env, jobject this)
+{
+ return PMALLOC_KEYS;
+}
+
+JNIEXPORT
+jlong JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_ngetBaseAddress(
+ JNIEnv *env, jobject this, jlong id)
+{
+ pthread_rwlock_rdlock(&g_pmp_rwlock);
+ void *md = *(g_pmp_ptr + id);
+ jlong ret = (long)b_addr(md);
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+ return ret;
+}
+
+JNIEXPORT
+jlong JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_ninit
+ (JNIEnv *env, jclass this, jlong capacity, jstring pathname, jboolean isnew)
+{
+ pthread_rwlock_wrlock(&g_pmp_rwlock);
+ size_t ret = -1;
+ void *md = NULL;
+ const char* mpathname = (*env)->GetStringUTFChars(env, pathname, NULL);
+ if (NULL == mpathname) {
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+ throw(env, "Big memory path not specified!");
+ }
+ if ((md = pmopen(mpathname, NULL, capacity)) == NULL) {
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+ throw(env, "Big memory init failure!");
+ }
+ (*env)->ReleaseStringUTFChars(env, pathname, mpathname);
+ g_pmp_ptr = realloc(g_pmp_ptr, (g_pmp_count + 1) * sizeof(void*));
+ g_pmalloc_mutex_ptr =
+ realloc(g_pmalloc_mutex_ptr, (g_pmp_count + 1) * sizeof(pthread_mutex_t));
+ if (NULL != g_pmp_ptr && NULL != g_pmalloc_mutex_ptr) {
+ *(g_pmp_ptr + g_pmp_count) = md;
+ pthread_mutex_init(g_pmalloc_mutex_ptr + g_pmp_count, NULL);
+ ret = g_pmp_count;
+ ++g_pmp_count;
+ } else {
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+ throw(env, "Big memory init Out of memory!");
+ }
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+ return ret;
+}
+
+JNIEXPORT
+void JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_nclose
+ (JNIEnv *env, jobject this, jlong id)
+{
+ pthread_rwlock_rdlock(&g_pmp_rwlock);
+ pthread_mutex_lock(g_pmalloc_mutex_ptr + id);
+ void *md = *(g_pmp_ptr + id);
+ pmclose(md);
+ *(g_pmp_ptr + id) = NULL;
+ pthread_mutex_unlock(g_pmalloc_mutex_ptr + id);
+ pthread_mutex_destroy(g_pmalloc_mutex_ptr + id);
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+}
+
+
+__attribute__((destructor)) void fini(void)
+{
+ int i;
+ if (NULL != g_pmp_ptr) {
+ for (i = 0; i < g_pmp_count; ++i) {
+ if (NULL != *(g_pmp_ptr + i)){
+ pmclose(*(g_pmp_ptr + i));
+ *(g_pmp_ptr + i) = NULL;
+ pthread_mutex_destroy(g_pmalloc_mutex_ptr + i);
+ }
+ }
+ free(g_pmp_ptr);
+ g_pmp_ptr = NULL;
+ free(g_pmalloc_mutex_ptr);
+ g_pmalloc_mutex_ptr = NULL;
+ g_pmp_count = 0;
+ }
+ pthread_rwlock_destroy(&g_pmp_rwlock);
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/allocator-services/pmalloc-service/src/main/native/org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl.h
----------------------------------------------------------------------
diff --git a/allocator-services/pmalloc-service/src/main/native/org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl.h b/allocator-services/pmalloc-service/src/main/native/org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl.h
new file mode 100644
index 0000000..4b119c7
--- /dev/null
+++ b/allocator-services/pmalloc-service/src/main/native/org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl.h
@@ -0,0 +1,31 @@
+/* DO NOT EDIT THIS FILE - it is machine generated */
+#include <jni.h>
+/* Header for class PMallocServiceImpl */
+
+#include "common.h"
+
+#ifndef _Included_org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl
+#define _Included_org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl
+#ifdef __cplusplus
+extern "C" {
+#endif
+/*
+ * Class: org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl
+ * Method: jniInit
+ * Signature: (II)V
+ */
+JNIEXPORT void JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_jniInit
+ (JNIEnv *, jobject, jint, jint);
+
+/*
+ * Class: org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl
+ * Method: jniTerm
+ * Signature: ()V
+ */
+JNIEXPORT void JNICALL Java_org_apache_mnemonic_service_allocatorservice_internal_PMallocServiceImpl_jniTerm
+ (JNIEnv *, jobject);
+
+#ifdef __cplusplus
+}
+#endif
+#endif
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/allocator-services/pmalloc-service/src/main/resources/META-INF/services/com.intel.mnemonic.service.allocatorservice.NonVolatileMemoryAllocatorService
----------------------------------------------------------------------
diff --git a/allocator-services/pmalloc-service/src/main/resources/META-INF/services/com.intel.mnemonic.service.allocatorservice.NonVolatileMemoryAllocatorService b/allocator-services/pmalloc-service/src/main/resources/META-INF/services/com.intel.mnemonic.service.allocatorservice.NonVolatileMemoryAllocatorService
deleted file mode 100644
index 0b31d2a..0000000
--- a/allocator-services/pmalloc-service/src/main/resources/META-INF/services/com.intel.mnemonic.service.allocatorservice.NonVolatileMemoryAllocatorService
+++ /dev/null
@@ -1 +0,0 @@
-com.intel.mnemonic.service.allocatorservice.internal.PMallocServiceImpl
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/allocator-services/pmalloc-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.allocatorservice.NonVolatileMemoryAllocatorService
----------------------------------------------------------------------
diff --git a/allocator-services/pmalloc-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.allocatorservice.NonVolatileMemoryAllocatorService b/allocator-services/pmalloc-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.allocatorservice.NonVolatileMemoryAllocatorService
new file mode 100644
index 0000000..4511285
--- /dev/null
+++ b/allocator-services/pmalloc-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.allocatorservice.NonVolatileMemoryAllocatorService
@@ -0,0 +1 @@
+org.apache.mnemonic.service.allocatorservice.internal.PMallocServiceImpl
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/allocator-services/pom.xml
----------------------------------------------------------------------
diff --git a/allocator-services/pom.xml b/allocator-services/pom.xml
index 4432f0e..5615ec4 100644
--- a/allocator-services/pom.xml
+++ b/allocator-services/pom.xml
@@ -2,13 +2,13 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
- <groupId>com.intel.mnemonic</groupId>
+ <groupId>org.apache.mnemonic</groupId>
<artifactId>mnemonic-parent</artifactId>
<version>2.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
- <groupId>com.intel.mnemonic.service.allocatorservice</groupId>
+ <groupId>org.apache.mnemonic.service.allocatorservice</groupId>
<artifactId>allocator-services-parent-project</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
@@ -27,7 +27,7 @@
<dependencies>
<dependency>
- <groupId>com.intel.bigdatamem</groupId>
+ <groupId>org.apache.mnemonic</groupId>
<artifactId>mnemonic-core</artifactId>
<version>${mnemonic.core.version}</version>
<scope>compile</scope>
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/collections/pom.xml
----------------------------------------------------------------------
diff --git a/collections/pom.xml b/collections/pom.xml
index 900df84..cc5bd0b 100644
--- a/collections/pom.xml
+++ b/collections/pom.xml
@@ -3,21 +3,21 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
- <groupId>com.intel.mnemonic</groupId>
+ <groupId>org.apache.mnemonic</groupId>
<artifactId>mnemonic-parent</artifactId>
<version>2.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
- <groupId>com.intel.bigdatamem</groupId>
+ <groupId>org.apache.mnemonic.collections</groupId>
<artifactId>mnemonic-collections</artifactId>
<packaging>jar</packaging>
<name>Mnemonic Project Collections</name>
<dependencies>
<dependency>
- <groupId>com.intel.bigdatamem</groupId>
+ <groupId>org.apache.mnemonic</groupId>
<artifactId>mnemonic-core</artifactId>
<version>${project.version}</version>
</dependency>
@@ -80,7 +80,7 @@
<configuration>
<compilerArguments>-XDenableSunApiLintControl</compilerArguments>
<processors>
- <processor>${project.groupId}.NonVolatileEntityProcessor</processor>
+ <processor>${project.parent.groupId}.NonVolatileEntityProcessor</processor>
</processors>
</configuration>
</execution>
@@ -91,7 +91,7 @@
<configuration>
<compilerArguments>-XDenableSunApiLintControl</compilerArguments>
<processors>
- <processor>${project.groupId}.NonVolatileEntityProcessor</processor>
+ <processor>${project.parent.groupId}.NonVolatileEntityProcessor</processor>
</processors>
</configuration>
</execution>
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/collections/src/main/java/com/intel/bigdatamem/collections/NonVolatileNodeValue.java
----------------------------------------------------------------------
diff --git a/collections/src/main/java/com/intel/bigdatamem/collections/NonVolatileNodeValue.java b/collections/src/main/java/com/intel/bigdatamem/collections/NonVolatileNodeValue.java
deleted file mode 100644
index 14797d3..0000000
--- a/collections/src/main/java/com/intel/bigdatamem/collections/NonVolatileNodeValue.java
+++ /dev/null
@@ -1,150 +0,0 @@
-package com.intel.bigdatamem.collections;
-
-import java.util.Arrays;
-import java.util.Iterator;
-import java.util.NoSuchElementException;
-
-import com.intel.bigdatamem.*;
-
-/**
- * this class defines a non-volatile node for a generic value to form a unidirectional link
- *
- */
-@NonVolatileEntity
-public abstract class NonVolatileNodeValue<E>
- implements Durable, Iterable<E> {
- protected transient EntityFactoryProxy[] m_node_efproxies;
- protected transient GenericField.GType[] m_node_gftypes;
-
- /**
- * creation callback for initialization
- *
- */
- @Override
- public void initializeAfterCreate() {
- // System.out.println("Initializing After Created");
- }
-
- /**
- * restore callback for initialization
- *
- */
- @Override
- public void initializeAfterRestore() {
- // System.out.println("Initializing After Restored");
- }
-
- /**
- * this function will be invoked by its factory to setup generic related info to avoid expensive operations from reflection
- *
- * @param efproxies
- * specify a array of factory to proxy the restoring of its generic field objects
- *
- * @param gftypes
- * specify a array of types corresponding to efproxies
- */
- @Override
- public void setupGenericInfo(EntityFactoryProxy[] efproxies, GenericField.GType[] gftypes) {
- m_node_efproxies = efproxies;
- m_node_gftypes = gftypes;
- }
-
- /**
- * get the item value of this node
- *
- * @return the item value of this node
- */
- @NonVolatileGetter(Id = 1L, EntityFactoryProxies = "m_node_efproxies", GenericFieldTypes = "m_node_gftypes")
- abstract public E getItem();
-
- /**
- * set a value to this node item
- *
- * @param value
- * the value to be set
- *
- * @param destroy
- * true if want to destroy exist one
- *
- */
- @NonVolatileSetter
- abstract public void setItem(E value, boolean destroy);
-
- /**
- * get next node
- *
- * @return the next node
- *
- */
- @NonVolatileGetter(Id = 2L, EntityFactoryProxies = "m_node_efproxies", GenericFieldTypes = "m_node_gftypes")
- abstract public NonVolatileNodeValue<E> getNext();
-
- /**
- * set next node
- *
- * @param next
- * specify the next node
- *
- * @param destroy
- * true if want to destroy the exist node
- */
- @NonVolatileSetter
- abstract public void setNext(NonVolatileNodeValue<E> next, boolean destroy);
-
-
- /**
- * get an iterator instance of this list
- *
- * @return an iterator of this list
- */
- @Override
- public Iterator<E> iterator() {
- return new Intr(this);
- }
-
- /**
- * this class defines a iterator for this non-volatile list
- *
- */
- private class Intr implements Iterator<E> {
-
- protected NonVolatileNodeValue<E> next = null;
-
- /**
- * Constructor
- *
- * @param head
- * the start point for this iterator
- *
- */
- Intr(NonVolatileNodeValue<E> head) {
- next = head;
- }
-
- /**
- * determine the existing of next
- *
- * @return true if there is a next node
- *
- */
- @Override
- public boolean hasNext() {
- return null != next;
- }
-
- /**
- * get next node
- *
- * @return the next node
- */
- @Override
- public E next() {
- if (null == next) {
- new NoSuchElementException();
- }
- E ret = next.getItem();
- next = next.getNext();
- return ret;
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/collections/src/main/java/org/apache/mnemonic/collections/NonVolatileNodeValue.java
----------------------------------------------------------------------
diff --git a/collections/src/main/java/org/apache/mnemonic/collections/NonVolatileNodeValue.java b/collections/src/main/java/org/apache/mnemonic/collections/NonVolatileNodeValue.java
new file mode 100644
index 0000000..d7f45e4
--- /dev/null
+++ b/collections/src/main/java/org/apache/mnemonic/collections/NonVolatileNodeValue.java
@@ -0,0 +1,150 @@
+package org.apache.mnemonic.collections;
+
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+import org.apache.mnemonic.*;
+
+/**
+ * this class defines a non-volatile node for a generic value to form a unidirectional link
+ *
+ */
+@NonVolatileEntity
+public abstract class NonVolatileNodeValue<E>
+ implements Durable, Iterable<E> {
+ protected transient EntityFactoryProxy[] m_node_efproxies;
+ protected transient GenericField.GType[] m_node_gftypes;
+
+ /**
+ * creation callback for initialization
+ *
+ */
+ @Override
+ public void initializeAfterCreate() {
+ // System.out.println("Initializing After Created");
+ }
+
+ /**
+ * restore callback for initialization
+ *
+ */
+ @Override
+ public void initializeAfterRestore() {
+ // System.out.println("Initializing After Restored");
+ }
+
+ /**
+ * this function will be invoked by its factory to setup generic related info to avoid expensive operations from reflection
+ *
+ * @param efproxies
+ * specify a array of factory to proxy the restoring of its generic field objects
+ *
+ * @param gftypes
+ * specify a array of types corresponding to efproxies
+ */
+ @Override
+ public void setupGenericInfo(EntityFactoryProxy[] efproxies, GenericField.GType[] gftypes) {
+ m_node_efproxies = efproxies;
+ m_node_gftypes = gftypes;
+ }
+
+ /**
+ * get the item value of this node
+ *
+ * @return the item value of this node
+ */
+ @NonVolatileGetter(Id = 1L, EntityFactoryProxies = "m_node_efproxies", GenericFieldTypes = "m_node_gftypes")
+ abstract public E getItem();
+
+ /**
+ * set a value to this node item
+ *
+ * @param value
+ * the value to be set
+ *
+ * @param destroy
+ * true if want to destroy exist one
+ *
+ */
+ @NonVolatileSetter
+ abstract public void setItem(E value, boolean destroy);
+
+ /**
+ * get next node
+ *
+ * @return the next node
+ *
+ */
+ @NonVolatileGetter(Id = 2L, EntityFactoryProxies = "m_node_efproxies", GenericFieldTypes = "m_node_gftypes")
+ abstract public NonVolatileNodeValue<E> getNext();
+
+ /**
+ * set next node
+ *
+ * @param next
+ * specify the next node
+ *
+ * @param destroy
+ * true if want to destroy the exist node
+ */
+ @NonVolatileSetter
+ abstract public void setNext(NonVolatileNodeValue<E> next, boolean destroy);
+
+
+ /**
+ * get an iterator instance of this list
+ *
+ * @return an iterator of this list
+ */
+ @Override
+ public Iterator<E> iterator() {
+ return new Intr(this);
+ }
+
+ /**
+ * this class defines a iterator for this non-volatile list
+ *
+ */
+ private class Intr implements Iterator<E> {
+
+ protected NonVolatileNodeValue<E> next = null;
+
+ /**
+ * Constructor
+ *
+ * @param head
+ * the start point for this iterator
+ *
+ */
+ Intr(NonVolatileNodeValue<E> head) {
+ next = head;
+ }
+
+ /**
+ * determine the existing of next
+ *
+ * @return true if there is a next node
+ *
+ */
+ @Override
+ public boolean hasNext() {
+ return null != next;
+ }
+
+ /**
+ * get next node
+ *
+ * @return the next node
+ */
+ @Override
+ public E next() {
+ if (null == next) {
+ new NoSuchElementException();
+ }
+ E ret = next.getItem();
+ next = next.getNext();
+ return ret;
+ }
+ }
+}
[6/7] incubator-mnemonic git commit: MNEMONIC-5 MNEMONIC-6 #resolve
refactored package names
Posted by ga...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/collections/src/test/java/com/intel/bigdatamem/collections/NonVolatileNodeValueNGTest.java
----------------------------------------------------------------------
diff --git a/collections/src/test/java/com/intel/bigdatamem/collections/NonVolatileNodeValueNGTest.java b/collections/src/test/java/com/intel/bigdatamem/collections/NonVolatileNodeValueNGTest.java
deleted file mode 100644
index b344ee8..0000000
--- a/collections/src/test/java/com/intel/bigdatamem/collections/NonVolatileNodeValueNGTest.java
+++ /dev/null
@@ -1,256 +0,0 @@
-package com.intel.bigdatamem.collections;
-
-import java.nio.ByteBuffer;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Random;
-
-import org.testng.AssertJUnit;
-import org.testng.annotations.AfterClass;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-
-import com.intel.bigdatamem.BigDataPMemAllocator;
-import com.intel.bigdatamem.CommonPersistAllocator;
-import com.intel.bigdatamem.Durable;
-import com.intel.bigdatamem.EntityFactoryProxy;
-import com.intel.bigdatamem.GenericField;
-import com.intel.bigdatamem.Reclaim;
-import com.intel.bigdatamem.Utils;
-
-/**
- *
- *
- */
-
-
-public class NonVolatileNodeValueNGTest {
- private long KEYCAPACITY;
- private Random m_rand;
- private BigDataPMemAllocator m_act;
-
- @BeforeClass
- public void setUp() {
- m_rand = Utils.createRandom();
- m_act = new BigDataPMemAllocator(Utils.getNonVolatileMemoryAllocatorService("pmalloc"), 1024 * 1024 * 1024, "./pobj_NodeValue.dat", true);
- KEYCAPACITY = m_act.handlerCapacity();
- m_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;
- }
- });
- m_act.setChunkReclaimer(new Reclaim<Long>() {
- @Override
- public boolean reclaim(Long mres, Long sz) {
- System.out.println(String.format("Reclaim Memory Chunk: %X Size: %s", System.identityHashCode(mres),
- null == sz ? "NULL" : sz.toString()));
- return false;
- }
- });
-
- for (long i = 0; i < KEYCAPACITY; ++i) {
- m_act.setHandler(i, 0L);
- }
- }
-
- @AfterClass
- public void tearDown() {
- m_act.close();
- }
-
- @Test(enabled = false)
- public void testSingleNodeValueWithInteger() {
- int val = m_rand.nextInt();
- GenericField.GType gtypes[] = {GenericField.GType.INTEGER};
- NonVolatileNodeValue<Integer> plln = NonVolatileNodeValueFactory.create(m_act, null, gtypes, false);
- plln.setItem(val, false);
- Long handler = plln.getNonVolatileHandler();
- System.err.println("-------------Start to Restore Integer -----------");
- NonVolatileNodeValue<Integer> plln2 = NonVolatileNodeValueFactory.restore(m_act, null, gtypes, handler, false);
- AssertJUnit.assertEquals(val, (int)plln2.getItem());
- }
-
- @Test(enabled = false)
- public void testNodeValueWithString() {
- String val = Utils.genRandomString();
- GenericField.GType gtypes[] = {GenericField.GType.STRING};
- NonVolatileNodeValue<String> plln = NonVolatileNodeValueFactory.create(m_act, null, gtypes, false);
- plln.setItem(val, false);
- Long handler = plln.getNonVolatileHandler();
- System.err.println("-------------Start to Restore String-----------");
- NonVolatileNodeValue<String> plln2 = NonVolatileNodeValueFactory.restore(m_act, null, gtypes, handler, false);
- AssertJUnit.assertEquals(val, plln2.getItem());
- }
-
- @Test(enabled = false)
- public void testNodeValueWithPerson() {
-
- Person<Long> person = PersonFactory.create(m_act);
- person.setAge((short)31);
-
- GenericField.GType gtypes[] = {GenericField.GType.DURABLE};
- EntityFactoryProxy efproxies[] = {new EntityFactoryProxy(){
- @Override
- public <A extends CommonPersistAllocator<A>> Durable restore(A allocator,
- EntityFactoryProxy[] factoryproxys, GenericField.GType[] gfields, long phandler, boolean autoreclaim) {
- return PersonFactory.restore(allocator, factoryproxys, gfields, phandler, autoreclaim);
- }
- }
- };
-
- NonVolatileNodeValue<Person<Long>> plln = NonVolatileNodeValueFactory.create(m_act, efproxies, gtypes, false);
- plln.setItem(person, false);
- Long handler = plln.getNonVolatileHandler();
-
- NonVolatileNodeValue<Person<Long>> plln2 = NonVolatileNodeValueFactory.restore(m_act, efproxies, gtypes, handler, false);
- AssertJUnit.assertEquals(31, (int)plln2.getItem().getAge());
-
- }
- @Test(enabled = false)
- public void testLinkedNodeValueWithPerson() {
-
- int elem_count = 10;
- List<Long> referlist = new ArrayList();
-
- GenericField.GType listgftypes[] = {GenericField.GType.DURABLE};
- EntityFactoryProxy listefproxies[] = {
- new EntityFactoryProxy(){
- @Override
- public <A extends CommonPersistAllocator<A>> Durable restore(A allocator,
- EntityFactoryProxy[] factoryproxys, GenericField.GType[] gfields, long phandler, boolean autoreclaim) {
- return PersonFactory.restore(allocator, factoryproxys, gfields, phandler, autoreclaim);
- }
- }
- };
-
- NonVolatileNodeValue<Person<Long>> firstnv = NonVolatileNodeValueFactory.create(m_act, listefproxies, listgftypes, false);
-
- NonVolatileNodeValue<Person<Long>> nextnv = firstnv;
-
- Person<Long> person;
- long val;
- NonVolatileNodeValue<Person<Long>> newnv;
- for (int i = 0; i < elem_count; ++i) {
- person = PersonFactory.create(m_act);
- person.setAge((short)m_rand.nextInt(50));
- person.setName(String.format("Name: [%s]", Utils.genRandomString()), true);
- nextnv.setItem(person, false);
- newnv = NonVolatileNodeValueFactory.create(m_act, listefproxies, listgftypes, false);
- nextnv.setNext(newnv, false);
- nextnv = newnv;
- }
-
- Person<Long> eval;
- NonVolatileNodeValue<Person<Long>> iternv = firstnv;
- while(null != iternv) {
- System.out.printf(" Stage 1 --->\n");
- eval = iternv.getItem();
- if (null != eval)
- eval.testOutput();
- iternv = iternv.getNext();
- }
-
- long handler = firstnv.getNonVolatileHandler();
-
- NonVolatileNodeValue<Person<Long>> firstnv2 = NonVolatileNodeValueFactory.restore(m_act, listefproxies, listgftypes, handler, false);
-
- for (Person<Long> eval2 : firstnv2) {
- System.out.printf(" Stage 2 ---> \n");
- if (null != eval2)
- eval2.testOutput();
- }
-
- //Assert.assert, expected);(plist, plist2);
-
- }
-
- @Test(enabled = true)
- public void testLinkedNodeValueWithLinkedNodeValue() {
-
- int elem_count = 10;
- long slotKeyId = 10;
-
- GenericField.GType[] elem_gftypes = {GenericField.GType.DOUBLE};
- EntityFactoryProxy[] elem_efproxies = null;
-
- GenericField.GType linkedgftypes[] = {GenericField.GType.DURABLE, GenericField.GType.DOUBLE};
- EntityFactoryProxy linkedefproxies[] = {
- new EntityFactoryProxy(){
- @Override
- public <A extends CommonPersistAllocator<A>> Durable restore(A allocator,
- EntityFactoryProxy[] factoryproxys, GenericField.GType[] gfields, long phandler, boolean autoreclaim) {
- EntityFactoryProxy[] val_efproxies = null;
- GenericField.GType[] val_gftypes = null;
- if ( null != factoryproxys && factoryproxys.length >= 2 ) {
- val_efproxies = Arrays.copyOfRange(factoryproxys, 1, factoryproxys.length);
- }
- if ( null != gfields && gfields.length >= 2 ) {
- val_gftypes = Arrays.copyOfRange(gfields, 1, gfields.length);
- }
- return NonVolatileNodeValueFactory.restore(allocator, val_efproxies, val_gftypes, phandler, autoreclaim);
- }
- }
- };
-
- NonVolatileNodeValue<NonVolatileNodeValue<Double>> nextnv = null, pre_nextnv = null;
- NonVolatileNodeValue<Double> elem = null, pre_elem = null, first_elem = null;
-
- Long linkhandler = 0L;
-
- System.out.printf(" Stage 1 -testLinkedNodeValueWithLinkedNodeValue--> \n");
-
- pre_nextnv = null;
- Double val;
- for (int i=0; i< elem_count; ++i) {
- first_elem = null;
- pre_elem = null;
- for (int v=0; v<3 ; ++v) {
- elem = NonVolatileNodeValueFactory.create(m_act, elem_efproxies, elem_gftypes, false);
- val = m_rand.nextDouble();
- elem.setItem(val, false);
- if (null == pre_elem) {
- first_elem = elem;
- } else {
- pre_elem.setNext(elem, false);
- }
- pre_elem = elem;
- System.out.printf("%f ", val);
- }
-
- nextnv = NonVolatileNodeValueFactory.create(m_act, linkedefproxies, linkedgftypes, false);
- nextnv.setItem(first_elem, false);
- if (null == pre_nextnv) {
- linkhandler = nextnv.getNonVolatileHandler();
- } else {
- pre_nextnv.setNext(nextnv, false);
- }
- pre_nextnv = nextnv;
- System.out.printf(" generated an item... \n");
- }
- m_act.setHandler(slotKeyId, linkhandler);
-
- long handler = m_act.getHandler(slotKeyId);
-
- NonVolatileNodeValue<NonVolatileNodeValue<Double>> linkedvals = NonVolatileNodeValueFactory.restore(m_act, linkedefproxies, linkedgftypes, handler, false);
- Iterator<NonVolatileNodeValue<Double>> iter = linkedvals.iterator();
- Iterator<Double> elemiter = null;
-
- System.out.printf(" Stage 2 -testLinkedNodeValueWithLinkedNodeValue--> \n");
- while(iter.hasNext()) {
- elemiter = iter.next().iterator();
- while(elemiter.hasNext()) {
- System.out.printf("%f ", elemiter.next());
- }
- System.out.printf(" Fetched an item... \n");
- }
-
- //Assert.assert, expected);(plist, plist2);
-
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/collections/src/test/java/com/intel/bigdatamem/collections/NonVolatilePersonNGTest.java
----------------------------------------------------------------------
diff --git a/collections/src/test/java/com/intel/bigdatamem/collections/NonVolatilePersonNGTest.java b/collections/src/test/java/com/intel/bigdatamem/collections/NonVolatilePersonNGTest.java
deleted file mode 100644
index 8423cfe..0000000
--- a/collections/src/test/java/com/intel/bigdatamem/collections/NonVolatilePersonNGTest.java
+++ /dev/null
@@ -1,136 +0,0 @@
-package com.intel.bigdatamem.collections;
-
-import org.testng.annotations.Test;
-import java.nio.ByteBuffer;
-import java.util.List;
-import java.util.Random;
-import java.util.UUID;
-import com.intel.bigdatamem.Utils;
-
-import com.intel.bigdatamem.*;
-
-/**
- *
- *
- */
-
-public class NonVolatilePersonNGTest {
- private long KEYCAPACITY;
-
- @Test(expectedExceptions = { OutOfPersistentMemory.class })
- public void testGenPeople() throws OutOfPersistentMemory, RetrieveNonVolatileEntityError {
- Random rand = Utils.createRandom();
- BigDataPMemAllocator act = new BigDataPMemAllocator(Utils.getNonVolatileMemoryAllocatorService("pmalloc"), 1024 * 1024 * 8, "./pobj_person.dat", true);
- KEYCAPACITY = act.handlerCapacity();
- 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;
- }
- });
- act.setChunkReclaimer(new Reclaim<Long>() {
- @Override
- public boolean reclaim(Long mres, Long sz) {
- System.out.println(String.format(
- "Reclaim Memory Chunk: %X Size: %s", System
- .identityHashCode(mres),
- null == sz ? "NULL" : sz.toString()));
- return false;
- }
- });
-
- for (long i = 0; i < KEYCAPACITY; ++i) {
- act.setHandler(i, 0L);
- }
-
- Person<Integer> mother;
- Person<Integer> person;
-
- long keyidx = 0;
- long val;
-
- try {
- while(true) {
- //if (keyidx >= KEYCAPACITY) break;
-
- keyidx %= KEYCAPACITY;
-
- System.out.printf("************ Generating People on Key %d ***********\n", keyidx);
-
- val = act.getHandler(keyidx);
- if (0L != val) {
- PersonFactory.restore(act, val, true);
- }
-
- person = PersonFactory.create(act);
- person.setAge((short)rand.nextInt(50));
- person.setName(String.format("Name: [%s]", UUID.randomUUID().toString()), true);
- person.setName(String.format("Name: [%s]", UUID.randomUUID().toString()), true);
- person.setName(String.format("Name: [%s]", UUID.randomUUID().toString()), true);
- person.setName(String.format("Name: [%s]", UUID.randomUUID().toString()), true);
-
- act.setHandler(keyidx, person.getNonVolatileHandler());
-
- for (int deep = 0; deep < rand.nextInt(100); ++deep) {
-
- mother = PersonFactory.create(act);
- mother.setAge((short)(50 + rand.nextInt(50)));
- mother.setName(String.format("Name: [%s]", UUID.randomUUID().toString()), true);
-
- person.setMother(mother, true);
-
- person = mother;
-
- }
- ++keyidx;
- }
- }finally {
- act.close();
- }
- }
-
- @Test(dependsOnMethods = {"testGenPeople"})
- public void testCheckPeople() throws RetrieveNonVolatileEntityError {
- BigDataPMemAllocator act = new BigDataPMemAllocator(Utils.getNonVolatileMemoryAllocatorService("pmalloc"), 1024 * 1024 * 8, "./pobj_person.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;
- }
- });
- act.setChunkReclaimer(new Reclaim<Long>() {
- @Override
- public boolean reclaim(Long mres, Long sz) {
- System.out.println(String.format(
- "Reclaim Memory Chunk: %X Size: %s", System
- .identityHashCode(mres),
- null == sz ? "NULL" : sz.toString()));
- return false;
- }
- });
-
- long val;
- for (long i = 0; i < KEYCAPACITY; ++i) {
- System.out.printf("----------Key %d--------------\n", i);
- val = act.getHandler(i);
- if (0L == val) {
- break;
- }
- Person<Integer> person = PersonFactory.restore(act, val, true);
- while (null != person) {
- person.testOutput();
- person = person.getMother();
- }
- }
-
- act.close();
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/collections/src/test/java/com/intel/bigdatamem/collections/Payload.java
----------------------------------------------------------------------
diff --git a/collections/src/test/java/com/intel/bigdatamem/collections/Payload.java b/collections/src/test/java/com/intel/bigdatamem/collections/Payload.java
deleted file mode 100644
index c220bfc..0000000
--- a/collections/src/test/java/com/intel/bigdatamem/collections/Payload.java
+++ /dev/null
@@ -1,29 +0,0 @@
-
-package com.intel.bigdatamem.collections;
-
-/**
- * a dummy object that is used for other test cases.
- *
- *
- *
- */
-public class Payload implements java.io.Serializable, Comparable<Payload> {
-
- private static final long serialVersionUID = 187397440699436500L;
-
- public Payload(int iv, String strv, double dv) {
- ival = iv;
- strval = strv;
- dval = dv;
- }
-
- public int ival;
- public String strval;
- public double dval;
-
- @Override
- public int compareTo(Payload pl) {
- return ival == pl.ival && strval.equals(pl.strval) && dval == pl.dval ? 0
- : 1;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/collections/src/test/java/com/intel/bigdatamem/collections/Person.java
----------------------------------------------------------------------
diff --git a/collections/src/test/java/com/intel/bigdatamem/collections/Person.java b/collections/src/test/java/com/intel/bigdatamem/collections/Person.java
deleted file mode 100644
index 3c2cc28..0000000
--- a/collections/src/test/java/com/intel/bigdatamem/collections/Person.java
+++ /dev/null
@@ -1,65 +0,0 @@
-package com.intel.bigdatamem.collections;
-
-import org.testng.annotations.Test;
-import java.util.List;
-
-import com.intel.bigdatamem.*;
-
-/**
- *
- *
- */
-
-@NonVolatileEntity
-public abstract class Person<E> implements Durable, Comparable<Person<E>> {
- E element;
-
- @Override
- public void initializeAfterCreate() {
- System.out.println("Initializing After Created");
- }
-
- @Override
- public void initializeAfterRestore() {
- System.out.println("Initializing After Restored");
- }
-
- @Override
- public void setupGenericInfo(EntityFactoryProxy[] efproxies, GenericField.GType[] gftypes) {
-
- }
-
- @Test
- public void testOutput() throws RetrieveNonVolatileEntityError {
- System.out.printf("Person %s, Age: %d ( %s ) \n", getName(), getAge(),
- null == getMother()? "No Recorded Mother" : "Has Recorded Mother");
- }
-
- public int compareTo(Person<E> anotherPerson) {
- int ret = 0;
- if (0 == ret) ret = getAge().compareTo(anotherPerson.getAge());
- if (0 == ret) ret = getName().compareTo(anotherPerson.getName());
- return ret;
- }
-
- @NonVolatileGetter(Id = 1L)
- abstract public Short getAge();
- @NonVolatileSetter
- abstract public void setAge(Short age);
-
- @NonVolatileGetter(Id = 2L)
- abstract public String getName() throws RetrieveNonVolatileEntityError;
- @NonVolatileSetter
- abstract public void setName(String name, boolean destroy) throws OutOfPersistentMemory, RetrieveNonVolatileEntityError;
-
- @NonVolatileGetter(Id = 3L)
- abstract public Person<E> getMother() throws RetrieveNonVolatileEntityError;
- @NonVolatileSetter
- abstract public void setMother(Person<E> mother, boolean destroy) throws RetrieveNonVolatileEntityError;
-
- @NonVolatileGetter(Id = 4L)
- abstract public Person<E> getFather() throws RetrieveNonVolatileEntityError;
- @NonVolatileSetter
- abstract public void setFather(Person<E> mother, boolean destroy) throws RetrieveNonVolatileEntityError;
-}
-
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/collections/src/test/java/org/apache/mnemonic/collections/NonVolatileNodeValueNGTest.java
----------------------------------------------------------------------
diff --git a/collections/src/test/java/org/apache/mnemonic/collections/NonVolatileNodeValueNGTest.java b/collections/src/test/java/org/apache/mnemonic/collections/NonVolatileNodeValueNGTest.java
new file mode 100644
index 0000000..82829ef
--- /dev/null
+++ b/collections/src/test/java/org/apache/mnemonic/collections/NonVolatileNodeValueNGTest.java
@@ -0,0 +1,255 @@
+package org.apache.mnemonic.collections;
+
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Random;
+
+import org.apache.mnemonic.BigDataPMemAllocator;
+import org.apache.mnemonic.CommonPersistAllocator;
+import org.apache.mnemonic.Durable;
+import org.apache.mnemonic.EntityFactoryProxy;
+import org.apache.mnemonic.GenericField;
+import org.apache.mnemonic.Reclaim;
+import org.apache.mnemonic.Utils;
+import org.testng.AssertJUnit;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+/**
+ *
+ *
+ */
+
+
+public class NonVolatileNodeValueNGTest {
+ private long KEYCAPACITY;
+ private Random m_rand;
+ private BigDataPMemAllocator m_act;
+
+ @BeforeClass
+ public void setUp() {
+ m_rand = Utils.createRandom();
+ m_act = new BigDataPMemAllocator(Utils.getNonVolatileMemoryAllocatorService("pmalloc"), 1024 * 1024 * 1024, "./pobj_NodeValue.dat", true);
+ KEYCAPACITY = m_act.handlerCapacity();
+ m_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;
+ }
+ });
+ m_act.setChunkReclaimer(new Reclaim<Long>() {
+ @Override
+ public boolean reclaim(Long mres, Long sz) {
+ System.out.println(String.format("Reclaim Memory Chunk: %X Size: %s", System.identityHashCode(mres),
+ null == sz ? "NULL" : sz.toString()));
+ return false;
+ }
+ });
+
+ for (long i = 0; i < KEYCAPACITY; ++i) {
+ m_act.setHandler(i, 0L);
+ }
+ }
+
+ @AfterClass
+ public void tearDown() {
+ m_act.close();
+ }
+
+ @Test(enabled = false)
+ public void testSingleNodeValueWithInteger() {
+ int val = m_rand.nextInt();
+ GenericField.GType gtypes[] = {GenericField.GType.INTEGER};
+ NonVolatileNodeValue<Integer> plln = NonVolatileNodeValueFactory.create(m_act, null, gtypes, false);
+ plln.setItem(val, false);
+ Long handler = plln.getNonVolatileHandler();
+ System.err.println("-------------Start to Restore Integer -----------");
+ NonVolatileNodeValue<Integer> plln2 = NonVolatileNodeValueFactory.restore(m_act, null, gtypes, handler, false);
+ AssertJUnit.assertEquals(val, (int)plln2.getItem());
+ }
+
+ @Test(enabled = false)
+ public void testNodeValueWithString() {
+ String val = Utils.genRandomString();
+ GenericField.GType gtypes[] = {GenericField.GType.STRING};
+ NonVolatileNodeValue<String> plln = NonVolatileNodeValueFactory.create(m_act, null, gtypes, false);
+ plln.setItem(val, false);
+ Long handler = plln.getNonVolatileHandler();
+ System.err.println("-------------Start to Restore String-----------");
+ NonVolatileNodeValue<String> plln2 = NonVolatileNodeValueFactory.restore(m_act, null, gtypes, handler, false);
+ AssertJUnit.assertEquals(val, plln2.getItem());
+ }
+
+ @Test(enabled = false)
+ public void testNodeValueWithPerson() {
+
+ Person<Long> person = PersonFactory.create(m_act);
+ person.setAge((short)31);
+
+ GenericField.GType gtypes[] = {GenericField.GType.DURABLE};
+ EntityFactoryProxy efproxies[] = {new EntityFactoryProxy(){
+ @Override
+ public <A extends CommonPersistAllocator<A>> Durable restore(A allocator,
+ EntityFactoryProxy[] factoryproxys, GenericField.GType[] gfields, long phandler, boolean autoreclaim) {
+ return PersonFactory.restore(allocator, factoryproxys, gfields, phandler, autoreclaim);
+ }
+ }
+ };
+
+ NonVolatileNodeValue<Person<Long>> plln = NonVolatileNodeValueFactory.create(m_act, efproxies, gtypes, false);
+ plln.setItem(person, false);
+ Long handler = plln.getNonVolatileHandler();
+
+ NonVolatileNodeValue<Person<Long>> plln2 = NonVolatileNodeValueFactory.restore(m_act, efproxies, gtypes, handler, false);
+ AssertJUnit.assertEquals(31, (int)plln2.getItem().getAge());
+
+ }
+ @Test(enabled = false)
+ public void testLinkedNodeValueWithPerson() {
+
+ int elem_count = 10;
+ List<Long> referlist = new ArrayList();
+
+ GenericField.GType listgftypes[] = {GenericField.GType.DURABLE};
+ EntityFactoryProxy listefproxies[] = {
+ new EntityFactoryProxy(){
+ @Override
+ public <A extends CommonPersistAllocator<A>> Durable restore(A allocator,
+ EntityFactoryProxy[] factoryproxys, GenericField.GType[] gfields, long phandler, boolean autoreclaim) {
+ return PersonFactory.restore(allocator, factoryproxys, gfields, phandler, autoreclaim);
+ }
+ }
+ };
+
+ NonVolatileNodeValue<Person<Long>> firstnv = NonVolatileNodeValueFactory.create(m_act, listefproxies, listgftypes, false);
+
+ NonVolatileNodeValue<Person<Long>> nextnv = firstnv;
+
+ Person<Long> person;
+ long val;
+ NonVolatileNodeValue<Person<Long>> newnv;
+ for (int i = 0; i < elem_count; ++i) {
+ person = PersonFactory.create(m_act);
+ person.setAge((short)m_rand.nextInt(50));
+ person.setName(String.format("Name: [%s]", Utils.genRandomString()), true);
+ nextnv.setItem(person, false);
+ newnv = NonVolatileNodeValueFactory.create(m_act, listefproxies, listgftypes, false);
+ nextnv.setNext(newnv, false);
+ nextnv = newnv;
+ }
+
+ Person<Long> eval;
+ NonVolatileNodeValue<Person<Long>> iternv = firstnv;
+ while(null != iternv) {
+ System.out.printf(" Stage 1 --->\n");
+ eval = iternv.getItem();
+ if (null != eval)
+ eval.testOutput();
+ iternv = iternv.getNext();
+ }
+
+ long handler = firstnv.getNonVolatileHandler();
+
+ NonVolatileNodeValue<Person<Long>> firstnv2 = NonVolatileNodeValueFactory.restore(m_act, listefproxies, listgftypes, handler, false);
+
+ for (Person<Long> eval2 : firstnv2) {
+ System.out.printf(" Stage 2 ---> \n");
+ if (null != eval2)
+ eval2.testOutput();
+ }
+
+ //Assert.assert, expected);(plist, plist2);
+
+ }
+
+ @Test(enabled = true)
+ public void testLinkedNodeValueWithLinkedNodeValue() {
+
+ int elem_count = 10;
+ long slotKeyId = 10;
+
+ GenericField.GType[] elem_gftypes = {GenericField.GType.DOUBLE};
+ EntityFactoryProxy[] elem_efproxies = null;
+
+ GenericField.GType linkedgftypes[] = {GenericField.GType.DURABLE, GenericField.GType.DOUBLE};
+ EntityFactoryProxy linkedefproxies[] = {
+ new EntityFactoryProxy(){
+ @Override
+ public <A extends CommonPersistAllocator<A>> Durable restore(A allocator,
+ EntityFactoryProxy[] factoryproxys, GenericField.GType[] gfields, long phandler, boolean autoreclaim) {
+ EntityFactoryProxy[] val_efproxies = null;
+ GenericField.GType[] val_gftypes = null;
+ if ( null != factoryproxys && factoryproxys.length >= 2 ) {
+ val_efproxies = Arrays.copyOfRange(factoryproxys, 1, factoryproxys.length);
+ }
+ if ( null != gfields && gfields.length >= 2 ) {
+ val_gftypes = Arrays.copyOfRange(gfields, 1, gfields.length);
+ }
+ return NonVolatileNodeValueFactory.restore(allocator, val_efproxies, val_gftypes, phandler, autoreclaim);
+ }
+ }
+ };
+
+ NonVolatileNodeValue<NonVolatileNodeValue<Double>> nextnv = null, pre_nextnv = null;
+ NonVolatileNodeValue<Double> elem = null, pre_elem = null, first_elem = null;
+
+ Long linkhandler = 0L;
+
+ System.out.printf(" Stage 1 -testLinkedNodeValueWithLinkedNodeValue--> \n");
+
+ pre_nextnv = null;
+ Double val;
+ for (int i=0; i< elem_count; ++i) {
+ first_elem = null;
+ pre_elem = null;
+ for (int v=0; v<3 ; ++v) {
+ elem = NonVolatileNodeValueFactory.create(m_act, elem_efproxies, elem_gftypes, false);
+ val = m_rand.nextDouble();
+ elem.setItem(val, false);
+ if (null == pre_elem) {
+ first_elem = elem;
+ } else {
+ pre_elem.setNext(elem, false);
+ }
+ pre_elem = elem;
+ System.out.printf("%f ", val);
+ }
+
+ nextnv = NonVolatileNodeValueFactory.create(m_act, linkedefproxies, linkedgftypes, false);
+ nextnv.setItem(first_elem, false);
+ if (null == pre_nextnv) {
+ linkhandler = nextnv.getNonVolatileHandler();
+ } else {
+ pre_nextnv.setNext(nextnv, false);
+ }
+ pre_nextnv = nextnv;
+ System.out.printf(" generated an item... \n");
+ }
+ m_act.setHandler(slotKeyId, linkhandler);
+
+ long handler = m_act.getHandler(slotKeyId);
+
+ NonVolatileNodeValue<NonVolatileNodeValue<Double>> linkedvals = NonVolatileNodeValueFactory.restore(m_act, linkedefproxies, linkedgftypes, handler, false);
+ Iterator<NonVolatileNodeValue<Double>> iter = linkedvals.iterator();
+ Iterator<Double> elemiter = null;
+
+ System.out.printf(" Stage 2 -testLinkedNodeValueWithLinkedNodeValue--> \n");
+ while(iter.hasNext()) {
+ elemiter = iter.next().iterator();
+ while(elemiter.hasNext()) {
+ System.out.printf("%f ", elemiter.next());
+ }
+ System.out.printf(" Fetched an item... \n");
+ }
+
+ //Assert.assert, expected);(plist, plist2);
+
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/collections/src/test/java/org/apache/mnemonic/collections/NonVolatilePersonNGTest.java
----------------------------------------------------------------------
diff --git a/collections/src/test/java/org/apache/mnemonic/collections/NonVolatilePersonNGTest.java b/collections/src/test/java/org/apache/mnemonic/collections/NonVolatilePersonNGTest.java
new file mode 100644
index 0000000..fb75eac
--- /dev/null
+++ b/collections/src/test/java/org/apache/mnemonic/collections/NonVolatilePersonNGTest.java
@@ -0,0 +1,134 @@
+package org.apache.mnemonic.collections;
+
+import org.apache.mnemonic.*;
+import org.testng.annotations.Test;
+import java.nio.ByteBuffer;
+import java.util.List;
+import java.util.Random;
+import java.util.UUID;
+
+/**
+ *
+ *
+ */
+
+public class NonVolatilePersonNGTest {
+ private long KEYCAPACITY;
+
+ @Test(expectedExceptions = { OutOfPersistentMemory.class })
+ public void testGenPeople() throws OutOfPersistentMemory, RetrieveNonVolatileEntityError {
+ Random rand = Utils.createRandom();
+ BigDataPMemAllocator act = new BigDataPMemAllocator(Utils.getNonVolatileMemoryAllocatorService("pmalloc"), 1024 * 1024 * 8, "./pobj_person.dat", true);
+ KEYCAPACITY = act.handlerCapacity();
+ 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;
+ }
+ });
+ act.setChunkReclaimer(new Reclaim<Long>() {
+ @Override
+ public boolean reclaim(Long mres, Long sz) {
+ System.out.println(String.format(
+ "Reclaim Memory Chunk: %X Size: %s", System
+ .identityHashCode(mres),
+ null == sz ? "NULL" : sz.toString()));
+ return false;
+ }
+ });
+
+ for (long i = 0; i < KEYCAPACITY; ++i) {
+ act.setHandler(i, 0L);
+ }
+
+ Person<Integer> mother;
+ Person<Integer> person;
+
+ long keyidx = 0;
+ long val;
+
+ try {
+ while(true) {
+ //if (keyidx >= KEYCAPACITY) break;
+
+ keyidx %= KEYCAPACITY;
+
+ System.out.printf("************ Generating People on Key %d ***********\n", keyidx);
+
+ val = act.getHandler(keyidx);
+ if (0L != val) {
+ PersonFactory.restore(act, val, true);
+ }
+
+ person = PersonFactory.create(act);
+ person.setAge((short)rand.nextInt(50));
+ person.setName(String.format("Name: [%s]", UUID.randomUUID().toString()), true);
+ person.setName(String.format("Name: [%s]", UUID.randomUUID().toString()), true);
+ person.setName(String.format("Name: [%s]", UUID.randomUUID().toString()), true);
+ person.setName(String.format("Name: [%s]", UUID.randomUUID().toString()), true);
+
+ act.setHandler(keyidx, person.getNonVolatileHandler());
+
+ for (int deep = 0; deep < rand.nextInt(100); ++deep) {
+
+ mother = PersonFactory.create(act);
+ mother.setAge((short)(50 + rand.nextInt(50)));
+ mother.setName(String.format("Name: [%s]", UUID.randomUUID().toString()), true);
+
+ person.setMother(mother, true);
+
+ person = mother;
+
+ }
+ ++keyidx;
+ }
+ }finally {
+ act.close();
+ }
+ }
+
+ @Test(dependsOnMethods = {"testGenPeople"})
+ public void testCheckPeople() throws RetrieveNonVolatileEntityError {
+ BigDataPMemAllocator act = new BigDataPMemAllocator(Utils.getNonVolatileMemoryAllocatorService("pmalloc"), 1024 * 1024 * 8, "./pobj_person.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;
+ }
+ });
+ act.setChunkReclaimer(new Reclaim<Long>() {
+ @Override
+ public boolean reclaim(Long mres, Long sz) {
+ System.out.println(String.format(
+ "Reclaim Memory Chunk: %X Size: %s", System
+ .identityHashCode(mres),
+ null == sz ? "NULL" : sz.toString()));
+ return false;
+ }
+ });
+
+ long val;
+ for (long i = 0; i < KEYCAPACITY; ++i) {
+ System.out.printf("----------Key %d--------------\n", i);
+ val = act.getHandler(i);
+ if (0L == val) {
+ break;
+ }
+ Person<Integer> person = PersonFactory.restore(act, val, true);
+ while (null != person) {
+ person.testOutput();
+ person = person.getMother();
+ }
+ }
+
+ act.close();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/collections/src/test/java/org/apache/mnemonic/collections/Payload.java
----------------------------------------------------------------------
diff --git a/collections/src/test/java/org/apache/mnemonic/collections/Payload.java b/collections/src/test/java/org/apache/mnemonic/collections/Payload.java
new file mode 100644
index 0000000..31945bd
--- /dev/null
+++ b/collections/src/test/java/org/apache/mnemonic/collections/Payload.java
@@ -0,0 +1,29 @@
+
+package org.apache.mnemonic.collections;
+
+/**
+ * a dummy object that is used for other test cases.
+ *
+ *
+ *
+ */
+public class Payload implements java.io.Serializable, Comparable<Payload> {
+
+ private static final long serialVersionUID = 187397440699436500L;
+
+ public Payload(int iv, String strv, double dv) {
+ ival = iv;
+ strval = strv;
+ dval = dv;
+ }
+
+ public int ival;
+ public String strval;
+ public double dval;
+
+ @Override
+ public int compareTo(Payload pl) {
+ return ival == pl.ival && strval.equals(pl.strval) && dval == pl.dval ? 0
+ : 1;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/collections/src/test/java/org/apache/mnemonic/collections/Person.java
----------------------------------------------------------------------
diff --git a/collections/src/test/java/org/apache/mnemonic/collections/Person.java b/collections/src/test/java/org/apache/mnemonic/collections/Person.java
new file mode 100644
index 0000000..c7afa64
--- /dev/null
+++ b/collections/src/test/java/org/apache/mnemonic/collections/Person.java
@@ -0,0 +1,64 @@
+package org.apache.mnemonic.collections;
+
+import org.apache.mnemonic.*;
+import org.testng.annotations.Test;
+import java.util.List;
+
+/**
+ *
+ *
+ */
+
+@NonVolatileEntity
+public abstract class Person<E> implements Durable, Comparable<Person<E>> {
+ E element;
+
+ @Override
+ public void initializeAfterCreate() {
+ System.out.println("Initializing After Created");
+ }
+
+ @Override
+ public void initializeAfterRestore() {
+ System.out.println("Initializing After Restored");
+ }
+
+ @Override
+ public void setupGenericInfo(EntityFactoryProxy[] efproxies, GenericField.GType[] gftypes) {
+
+ }
+
+ @Test
+ public void testOutput() throws RetrieveNonVolatileEntityError {
+ System.out.printf("Person %s, Age: %d ( %s ) \n", getName(), getAge(),
+ null == getMother()? "No Recorded Mother" : "Has Recorded Mother");
+ }
+
+ public int compareTo(Person<E> anotherPerson) {
+ int ret = 0;
+ if (0 == ret) ret = getAge().compareTo(anotherPerson.getAge());
+ if (0 == ret) ret = getName().compareTo(anotherPerson.getName());
+ return ret;
+ }
+
+ @NonVolatileGetter(Id = 1L)
+ abstract public Short getAge();
+ @NonVolatileSetter
+ abstract public void setAge(Short age);
+
+ @NonVolatileGetter(Id = 2L)
+ abstract public String getName() throws RetrieveNonVolatileEntityError;
+ @NonVolatileSetter
+ abstract public void setName(String name, boolean destroy) throws OutOfPersistentMemory, RetrieveNonVolatileEntityError;
+
+ @NonVolatileGetter(Id = 3L)
+ abstract public Person<E> getMother() throws RetrieveNonVolatileEntityError;
+ @NonVolatileSetter
+ abstract public void setMother(Person<E> mother, boolean destroy) throws RetrieveNonVolatileEntityError;
+
+ @NonVolatileGetter(Id = 4L)
+ abstract public Person<E> getFather() throws RetrieveNonVolatileEntityError;
+ @NonVolatileSetter
+ abstract public void setFather(Person<E> mother, boolean destroy) throws RetrieveNonVolatileEntityError;
+}
+
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/collections/src/test/resources/testng.xml
----------------------------------------------------------------------
diff --git a/collections/src/test/resources/testng.xml b/collections/src/test/resources/testng.xml
index f21f4a7..eeacf58 100644
--- a/collections/src/test/resources/testng.xml
+++ b/collections/src/test/resources/testng.xml
@@ -3,15 +3,15 @@
<suite name="Suite" verbose="1" parallel="tests" thread-count="1">
<test name="Test">
<classes>
- <class name="com.intel.bigdatamem.collections.NonVolatileNodeValueNGTest"/>
+ <class name="org.apache.mnemonic.collections.NonVolatileNodeValueNGTest"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
<!--
- <class name="com.intel.bigdatamem.collections.Person"/>
- <class name="com.intel.bigdatamem.collections.NonVolatilePersonNGTest"/>
- <class name="com.intel.bigdatamem.collections.NonVolatileListNGTest"/>
- <class name="com.intel.bigdatamem.collections.NonVolatileNodeValueNGTest"/>
+ <class name="org.apache.mnemonic.collections.Person"/>
+ <class name="org.apache.mnemonic.collections.NonVolatilePersonNGTest"/>
+ <class name="org.apache.mnemonic.collections.NonVolatileListNGTest"/>
+ <class name="org.apache.mnemonic.collections.NonVolatileNodeValueNGTest"/>
-->
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/pom.xml
----------------------------------------------------------------------
diff --git a/core/pom.xml b/core/pom.xml
index 7589785..91751f7 100644
--- a/core/pom.xml
+++ b/core/pom.xml
@@ -3,13 +3,13 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
- <groupId>com.intel.mnemonic</groupId>
+ <groupId>org.apache.mnemonic</groupId>
<artifactId>mnemonic-parent</artifactId>
<version>2.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
- <groupId>com.intel.bigdatamem</groupId>
+ <groupId>org.apache.mnemonic</groupId>
<artifactId>mnemonic-core</artifactId>
<packaging>jar</packaging>
<name>Mnemonic Project Core</name>
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/Allocatable.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/Allocatable.java b/core/src/main/java/com/intel/bigdatamem/Allocatable.java
deleted file mode 100644
index feaf1c7..0000000
--- a/core/src/main/java/com/intel/bigdatamem/Allocatable.java
+++ /dev/null
@@ -1,102 +0,0 @@
-package com.intel.bigdatamem;
-
-/**
- * an interface to allocate memory resources from any underlying memory kind
- * of storage.
- *
- */
-public interface Allocatable<A extends CommonAllocator<A>> {
-
- /**
- * 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
- */
- public MemChunkHolder<A> createChunk(long size, boolean autoreclaim);
-
- /**
- * create a memory chunk that is managed by its holder.
- *
- * @param size
- * specify the size of memory chunk
- *
- * @return a holder contains a memory chunk
- */
- public MemChunkHolder<A> createChunk(long size);
-
- /**
- * create a memory buffer that is managed by its holder.
- *
- * @param size
- * specify the size of memory buffer
- *
- * @param autoreclaim
- * specify whether or not to reclaim this
- * buffer automatically
- *
- * @return a holder contains a memory buffer
- */
- public MemBufferHolder<A> createBuffer(long size, boolean autoreclaim);
-
- /**
- * 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
- */
- public MemBufferHolder<A> createBuffer(long size);
-
- /**
- * register a memory chunk for auto-reclaim
- *
- * @param mholder
- * specify a chunk holder to register
- */
- public void registerChunkAutoReclaim(MemChunkHolder<A> mholder);
-
- /**
- * register a memory buffer for auto-reclaim
- *
- * @param mholder
- * specify a buffer holder to register
- */
- public void registerBufferAutoReclaim(MemBufferHolder<A> mholder);
-
- /**
- * resize a memory chunk.
- *
- * @param mholder
- * specify a chunk holder for resizing
- *
- * @param size
- * specify a new size of this memory chunk
- *
- * @return the resized memory chunk holder
- *
- */
- public MemChunkHolder<A> resizeChunk(MemChunkHolder<A> mholder, long size);
-
- /**
- * resize a memory buffer.
- *
- * @param mholder
- * specify a buffer holder for resizing
- *
- * @param size
- * specify a new size of this memory buffer
- *
- * @return the resized memory buffer holder
- *
- */
- public MemBufferHolder<A> resizeBuffer(MemBufferHolder<A> mholder, long size);
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/Allocator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/Allocator.java b/core/src/main/java/com/intel/bigdatamem/Allocator.java
deleted file mode 100644
index 5aac318..0000000
--- a/core/src/main/java/com/intel/bigdatamem/Allocator.java
+++ /dev/null
@@ -1,39 +0,0 @@
-package com.intel.bigdatamem;
-
-/**
- * an interface to manage the lifecycle of memory allocator
- *
- */
-public interface Allocator<A extends CommonAllocator<A>> extends Allocatable<A> {
-
- /**
- * release the underlying memory pool and close it.
- *
- */
- public void close();
-
- /**
- * sync. dirty data to underlying memory-like device
- *
- */
- public void sync();
-
- /**
- * 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
- */
- public A enableActiveGC(long timeout);
-
- /**
- * disable active garbage collection.
- *
- * @return this allocator
- */
- public A disableActiveGC();
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/AnnotatedNonVolatileEntityClass.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/AnnotatedNonVolatileEntityClass.java b/core/src/main/java/com/intel/bigdatamem/AnnotatedNonVolatileEntityClass.java
deleted file mode 100644
index 5c80645..0000000
--- a/core/src/main/java/com/intel/bigdatamem/AnnotatedNonVolatileEntityClass.java
+++ /dev/null
@@ -1,977 +0,0 @@
-package com.intel.bigdatamem;
-
-import java.io.IOException;
-import java.lang.reflect.Array;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.LinkedHashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import javax.annotation.processing.*;
-import javax.lang.model.element.AnnotationMirror;
-import javax.lang.model.element.Element;
-import javax.lang.model.element.ElementKind;
-import javax.lang.model.element.ExecutableElement;
-import javax.lang.model.element.Modifier;
-import javax.lang.model.element.TypeElement;
-import javax.lang.model.element.TypeParameterElement;
-import javax.lang.model.element.VariableElement;
-import javax.lang.model.type.DeclaredType;
-import javax.lang.model.type.TypeKind;
-import javax.lang.model.type.TypeMirror;
-import javax.lang.model.type.TypeVariable;
-import javax.lang.model.util.Elements;
-import javax.lang.model.util.Types;
-import sun.misc.Unsafe;
-
-import com.squareup.javapoet.*;
-import com.squareup.javapoet.MethodSpec.Builder;
-
-/**
- * this class managed to generate generic non-volatile concrete object and their corresponding factory.
- *
- */
-public class AnnotatedNonVolatileEntityClass {
- protected class MethodInfo {
- public ExecutableElement elem;
- public MethodSpec.Builder specbuilder;
- public TypeName rettype;
- }
- protected class FieldInfo {
- public FieldSpec.Builder specbuilder;
- public TypeName type;
- public long id;
- public String name;
- public String efproxiesname;
- public String gftypesname;
- public long fieldoff;
- public long fieldsize;
- }
-
- protected final String FACTORYNAMESUFFIX = "Factory";
- protected final String PMEMNAMEPREFIX = "NonVolatile_";
- protected final String FIELDNAMESUFFIX = String.format("_field_%s", Utils.genRandomString());
- protected final String ALLOCATORFIELDNAME = String.format("alloc_%s", Utils.genRandomString());
- protected final String AUTORECLAIMFIELDNAME = String.format("autoreclaim_%s", Utils.genRandomString());
- protected final String UNSAFEFIELDNAME = String.format("unsafe_%s", Utils.genRandomString());
- protected final String HOLDERFIELDNAME = String.format("holder_%s", Utils.genRandomString());
- protected final String ALLOCTYPENAME = String.format("ALLOC_PMem3C93D24F59");
-
- private Types m_typeutils;
- private Elements m_elemutils;
- private Messager m_msgr;
- private TypeElement m_elem;
-
- private String m_factoryname;
- private String m_entityname;
-
- private long m_holdersize;
-
- private String m_packagename;
-
- private TypeName m_alloctypename = TypeVariableName.get(ALLOCTYPENAME);
- private TypeName m_factoryproxystypename = TypeName.get(EntityFactoryProxy[].class);
- private TypeName m_gfieldstypename = TypeName.get(GenericField.GType[].class);
- private TypeVariableName m_alloctypevarname = TypeVariableName.get(ALLOCTYPENAME,
- ParameterizedTypeName.get(ClassName.get(CommonPersistAllocator.class), TypeVariableName.get(ALLOCTYPENAME)));
-
- private Map<String, MethodInfo> m_gettersinfo = new HashMap<String, MethodInfo>();
- private Map<String, MethodInfo> m_settersinfo = new HashMap<String, MethodInfo>();
- private Map<String, FieldInfo> m_dynfieldsinfo = new HashMap<String, FieldInfo>();
- private Map<String, FieldInfo> m_fieldsinfo = new HashMap<String, FieldInfo>();
-
- private Map<String, MethodInfo> m_durablemtdinfo = new HashMap<String, MethodInfo>();
- private Map<String, MethodInfo> m_entitymtdinfo = new HashMap<String, MethodInfo>();
-
- private long computeTypeSize(TypeMirror type) {
- long ret;
- switch (type.getKind()) {
- case BYTE:
- ret = 1L;
- break;
- case BOOLEAN:
- ret = 1L;
- break;
- case CHAR:
- ret = 2L;
- break;
- case DOUBLE:
- ret = 8L;
- break;
- case FLOAT:
- ret = 4L;
- break;
- case SHORT:
- ret = 2L;
- break;
- case INT:
- ret = 4L;
- break;
- case LONG:
- ret = 8L;
- break;
- default:
- ret = 8L;
- }
- return ret;
- }
-
- private boolean isUnboxPrimitive(TypeName tn) {
- TypeName n = tn;
- try {
- n = tn.unbox();
- } catch(UnsupportedOperationException ex) {}
- return n.isPrimitive();
- }
-
- private TypeName unboxTypeName(TypeName tn) {
- TypeName n = tn;
- try {
- n = tn.unbox();
- } catch(UnsupportedOperationException ex) {}
- return n;
- }
-
- public AnnotatedNonVolatileEntityClass(TypeElement classElement, Types typeUtils, Elements elementUtils,
- Messager messager) {
- m_elem = classElement;
- m_typeutils = typeUtils;
- m_elemutils = elementUtils;
- m_msgr = messager;
-
- m_packagename = m_elemutils.getPackageOf(m_elem).getQualifiedName().toString();
-
- m_factoryname = String.format("%s%s", m_elem.getSimpleName(), FACTORYNAMESUFFIX);
- m_entityname = String.format("%s%s_%s", PMEMNAMEPREFIX, m_elem.getSimpleName(), Utils.genRandomString());
-
- m_durablemtdinfo.put("cancelAutoReclaim", new MethodInfo());
- m_durablemtdinfo.put("registerAutoReclaim", new MethodInfo());
- m_durablemtdinfo.put("getNonVolatileHandler", new MethodInfo());
- m_durablemtdinfo.put("autoReclaim", new MethodInfo());
- m_durablemtdinfo.put("destroy", new MethodInfo());
- m_durablemtdinfo.put("getNativeFieldInfo", new MethodInfo());
-
- m_entitymtdinfo.put("initializeNonVolatileEntity", new MethodInfo());
- m_entitymtdinfo.put("createNonVolatileEntity", new MethodInfo());
- m_entitymtdinfo.put("restoreNonVolatileEntity", new MethodInfo());
-
- }
-
- public void prepareProcessing() throws AnnotationProcessingException {
- MethodInfo methodinfo = null;
- FieldInfo fieldinfo;
- String methodname;
- long fieldoff = 0;
- TypeElement intf_durable = m_elemutils.getTypeElement(Durable.class.getCanonicalName());
- TypeElement intf_entity = m_elemutils.getTypeElement(MemoryNonVolatileEntity.class.getCanonicalName());
- // System.err.printf("<><><><><> %s ======\n", intf_entity.toString());
-
- boolean valid = false;
- for (TypeMirror tm : m_elem.getInterfaces()) {
- if (tm.toString().equals(Durable.class.getCanonicalName())) {
- valid = true;
- break;
- }
- }
- if (!valid) {
- throw new AnnotationProcessingException(m_elem, "Not implemented Durable Interface by %s.",
- m_elem.getSimpleName().toString());
- }
-
- fieldinfo = new FieldInfo();
- fieldinfo.name = String.format("m_unsafe_%s",
- Utils.genRandomString());
- fieldinfo.type = TypeName.get(m_elemutils.getTypeElement(Unsafe.class.getCanonicalName()).asType());
- fieldinfo.specbuilder = FieldSpec.builder(fieldinfo.type,
- fieldinfo.name, Modifier.PRIVATE);
- m_fieldsinfo.put("unsafe", fieldinfo);
-
- fieldinfo = new FieldInfo();
- fieldinfo.name = String.format("m_holder_%s",
- Utils.genRandomString());
- fieldinfo.type = ParameterizedTypeName.get(ClassName.get(MemChunkHolder.class), m_alloctypename);
- fieldinfo.specbuilder = FieldSpec.builder(fieldinfo.type,
- fieldinfo.name, Modifier.PRIVATE);
- m_fieldsinfo.put("holder", fieldinfo);
-
- fieldinfo = new FieldInfo();
- fieldinfo.name = String.format("m_autoreclaim_%s",
- Utils.genRandomString());
- fieldinfo.type = TypeName.get(m_typeutils.getPrimitiveType(TypeKind.BOOLEAN));
- fieldinfo.specbuilder = FieldSpec.builder(fieldinfo.type,
- fieldinfo.name, Modifier.PRIVATE, Modifier.VOLATILE);
- m_fieldsinfo.put("autoreclaim", fieldinfo);
-
- fieldinfo = new FieldInfo();
- fieldinfo.name = String.format("m_allocator_%s",
- Utils.genRandomString());
- fieldinfo.type = m_alloctypename;
- fieldinfo.specbuilder = FieldSpec.builder(fieldinfo.type,
- fieldinfo.name, Modifier.PRIVATE);
- m_fieldsinfo.put("allocator", fieldinfo);
-
- fieldinfo = new FieldInfo();
- fieldinfo.name = String.format("m_factoryproxy_%s",
- Utils.genRandomString());
- fieldinfo.type = m_factoryproxystypename;
- fieldinfo.specbuilder = FieldSpec.builder(fieldinfo.type,
- fieldinfo.name, Modifier.PRIVATE);
- m_fieldsinfo.put("factoryproxy", fieldinfo);
-
- fieldinfo = new FieldInfo();
- fieldinfo.name = String.format("m_genericfield_%s",
- Utils.genRandomString());
- fieldinfo.type = m_gfieldstypename;
- fieldinfo.specbuilder = FieldSpec.builder(fieldinfo.type,
- fieldinfo.name, Modifier.PRIVATE);
- m_fieldsinfo.put("genericfield", fieldinfo);
-
- for (Element elem : m_elem.getEnclosedElements()) {
- if (elem.getKind() == ElementKind.METHOD) {
- methodname = elem.getSimpleName().toString();
- // System.err.printf("=========== %s ======\n", methodname);
- NonVolatileGetter pgetter = elem.getAnnotation(NonVolatileGetter.class);
- if (pgetter != null) {
- if (!elem.getModifiers().contains(Modifier.ABSTRACT)) {
- throw new AnnotationProcessingException(elem,
- "%s annotated with NonVolatileGetter is not abstract.", methodname);
- }
- if (null != elem.getAnnotation(NonVolatileSetter.class)) {
- throw new AnnotationProcessingException(elem, "%s is annotated with NonVolatileSetter as well.",
- methodname);
- }
- if (!methodname.startsWith("get")) {
- throw new AnnotationProcessingException(elem, "%s does not comply name convention of getter.",
- methodname);
- }
- methodinfo = new MethodInfo();
- methodinfo.elem = (ExecutableElement) elem;
- methodinfo.specbuilder = MethodSpec.overriding(methodinfo.elem);
- methodinfo.rettype = TypeName.get(methodinfo.elem.getReturnType());
- m_gettersinfo.put(methodname.substring(3), methodinfo);
- fieldinfo = new FieldInfo();
- fieldinfo.type = methodinfo.rettype;
- if (fieldinfo.type.toString().equals(String.class.getCanonicalName())) {
- fieldinfo.type = ParameterizedTypeName.get(ClassName.get(MemBufferHolder.class), m_alloctypename);
- }
- if (fieldinfo.type instanceof TypeVariableName) {
- fieldinfo.type = ParameterizedTypeName.get(ClassName.get(GenericField.class), m_alloctypename, fieldinfo.type);
- }
- fieldinfo.name = String.format("m_%s_%s",
- methodname.substring(3).toLowerCase(),
- Utils.genRandomString());
- fieldinfo.specbuilder = FieldSpec.builder(fieldinfo.type,
- fieldinfo.name, Modifier.PRIVATE);
- fieldinfo.fieldsize = computeTypeSize(methodinfo.elem.getReturnType());
- fieldinfo.fieldoff = fieldoff;
- fieldoff += fieldinfo.fieldsize;
- fieldinfo.efproxiesname = pgetter.EntityFactoryProxies();
- fieldinfo.gftypesname = pgetter.GenericFieldTypes();
- fieldinfo.id = pgetter.Id();
- m_dynfieldsinfo.put(methodname.substring(3), fieldinfo);
-
- }
- if (null != elem.getAnnotation(NonVolatileSetter.class)) {
- if (!elem.getModifiers().contains(Modifier.ABSTRACT)) {
- throw new AnnotationProcessingException(elem,
- "%s annotated with NonVolatileSetter is not abstract.", methodname);
- }
- if (!methodname.startsWith("set")) {
- throw new AnnotationProcessingException(elem, "%s does not comply name convention of setter.",
- methodname);
- }
- methodinfo = new MethodInfo();
- methodinfo.elem = (ExecutableElement) elem;
- methodinfo.specbuilder = MethodSpec.overriding(methodinfo.elem);
- m_settersinfo.put(methodname.substring(3), methodinfo);
- }
- // if (!methodinfo.elem.getThrownTypes().contains(m_elemutils.getTypeElement(RetrieveNonVolatileEntityError.class.getCanonicalName()).asType())) {
- // throw new AnnotationProcessingException(methodinfo.elem, "%s must throw out %s.",
- // methodname, RetrieveNonVolatileEntityError.class.getName());
- // }
- }
- }
-
- m_holdersize = fieldoff;
-
- // MethodInfo minfo = null;
- for (String name : m_settersinfo.keySet()) {
- if (!m_gettersinfo.containsKey(name)) {
- throw new AnnotationProcessingException(null, "%s has no getter.", name);
- }
- // if (m_dynfieldsinfo.get(name).type.toString().equals(String.class.getCanonicalName())) {
- // minfo = m_settersinfo.get(name);
- // if (!minfo.elem.getThrownTypes().contains(m_elemutils.getTypeElement(OutOfPersistentMemory.class.getCanonicalName()).asType())) {
- // throw new AnnotationProcessingException(minfo.elem, "%s must throw out %s.",
- // gsetterName(name, false), OutOfPersistentMemory.class.getName());
- // }
- // }
- }
-
- // for (String name : m_dynfieldsinfo.keySet()) {
- // if (!isUnboxPrimitive(m_dynfieldsinfo.get(name).type)) {
- // if (m_gettersinfo.containsKey(name)) {
- // minfo = m_gettersinfo.get(name);
- // if (!minfo.elem.getThrownTypes().contains(m_elemutils.getTypeElement(RetrieveNonVolatileEntityError.class.getCanonicalName()).asType())) {
- // throw new AnnotationProcessingException(minfo.elem, "%s must throw out %s.",
- // gsetterName(name, true), RetrieveNonVolatileEntityError.class.getName());
- // }
- // }
- // if (m_settersinfo.containsKey(name)) {
- // minfo = m_settersinfo.get(name);
- // if (!minfo.elem.getThrownTypes().contains(m_elemutils.getTypeElement(RetrieveNonVolatileEntityError.class.getCanonicalName()).asType())) {
- // throw new AnnotationProcessingException(minfo.elem, "%s must throw out %s.",
- // gsetterName(name, false), RetrieveNonVolatileEntityError.class.getName());
- // }
- // }
- // }
- // }
-
- for (Element elem : intf_durable.getEnclosedElements()) {
- if (elem.getKind() == ElementKind.METHOD) {
- methodname = elem.getSimpleName().toString();
- if (m_durablemtdinfo.containsKey(methodname)) {
- // System.err.printf("**++++++++++ %s ======\n", methodname);
- methodinfo = m_durablemtdinfo.get(methodname);
- methodinfo.elem = (ExecutableElement) elem;
- methodinfo.specbuilder = MethodSpec.overriding(methodinfo.elem);
- }
- }
- }
-
- for (Element elem : intf_entity.getEnclosedElements()) {
- if (elem.getKind() == ElementKind.METHOD) {
- methodname = elem.getSimpleName().toString();
- if (m_entitymtdinfo.containsKey(methodname)) {
- // System.err.printf("**------- %s ======\n", elem.toString());
- methodinfo = m_entitymtdinfo.get(methodname);
- methodinfo.elem = (ExecutableElement) elem;
- methodinfo.specbuilder = overriding(methodinfo.elem, ALLOCTYPENAME);
-
- }
- }
- }
- genNFieldInfo();
- }
-
- protected String transTypeToUnsafeMethod(TypeName tname, boolean isget) throws AnnotationProcessingException {
- String ret = null;
- if (isUnboxPrimitive(tname)) {
- TypeName tn = unboxTypeName(tname);
- if (tn.equals(TypeName.BOOLEAN)) ret = isget ? "getByte" : "putByte";
- if (tn.equals(TypeName.BYTE)) ret = isget ? "getByte" : "putByte";
- if (tn.equals(TypeName.CHAR)) ret = isget ? "getChar" : "putChar";
- if (tn.equals(TypeName.DOUBLE)) ret = isget ? "getDouble" : "putDouble";
- if (tn.equals(TypeName.FLOAT)) ret = isget ? "getFloat" : "putFloat";
- if (tn.equals(TypeName.INT)) ret = isget ? "getInt" : "putInt";
- if (tn.equals(TypeName.LONG)) ret = isget ? "getLong" : "putLong";
- if (tn.equals(TypeName.SHORT)) ret = isget ? "getShort" : "putShort";
- } else {
- ret = isget ? "getAddress" : "putAddress";
- }
- if (null == ret) {
- throw new AnnotationProcessingException(null, "%s is not supported by getters or setters.",
- tname.toString());
- }
- return ret;
- }
-
- protected String getIntialValueLiteral(TypeName tname) throws AnnotationProcessingException {
- String ret = null;
- if (isUnboxPrimitive(tname)) {
- TypeName tn = unboxTypeName(tname);
- if (tn.equals(TypeName.BOOLEAN)) ret = "false";
- if (tn.equals(TypeName.BYTE)) ret = "(byte)0";
- if (tn.equals(TypeName.CHAR)) ret = "(char)0";
- if (tn.equals(TypeName.DOUBLE)) ret = "(double)0.0";
- if (tn.equals(TypeName.FLOAT)) ret = "(float)0.0";
- if (tn.equals(TypeName.INT)) ret = "(int)0";
- if (tn.equals(TypeName.LONG)) ret = "(long)0";
- if (tn.equals(TypeName.SHORT)) ret = "(short)0";
- } else {
- ret = null;
- }
- if (null == ret) {
- throw new AnnotationProcessingException(null, "%s is not supported to determine the inital value.",
- tname.toString());
- }
- return ret;
- }
-
- private int getFactoryProxyIndex(TypeName gtname) throws AnnotationProcessingException {
- int ret = -1;
- boolean found = false;
- if (gtname instanceof TypeVariableName) {
- for (TypeParameterElement tpe : m_elem.getTypeParameters()) {
- ++ret;
- if (tpe.toString().equals(gtname.toString())) {
- found = true;
- break;
- }
- }
- if (!found) {
- throw new AnnotationProcessingException(null, "%s type is not found during factory proxy query.",
- gtname.toString());
- }
- } else {
- throw new AnnotationProcessingException(null, "%s type is not generic type for factory proxy query.",
- gtname.toString());
- }
- return ret;
- }
-
- protected void genNFieldInfo() {
- FieldInfo dynfieldinfo, fieldinfo;
- List<long[]> finfo = new ArrayList<long[]>();
- for (String name : m_gettersinfo.keySet()) {
- dynfieldinfo = m_dynfieldsinfo.get(name);
- if (dynfieldinfo.id > 0) {
- finfo.add(new long[]{dynfieldinfo.id, dynfieldinfo.fieldoff, dynfieldinfo.fieldsize});
- }
- }
-
- fieldinfo = new FieldInfo();
- fieldinfo.name = String.format("m_nfieldinfo_%s",
- Utils.genRandomString());
- fieldinfo.type = ArrayTypeName.of(ArrayTypeName.of(TypeName.LONG));
- String initlstr = Utils.toInitLiteral(finfo);
- fieldinfo.specbuilder = FieldSpec.builder(fieldinfo.type,
- fieldinfo.name, Modifier.PRIVATE, Modifier.STATIC).initializer("$1L", initlstr);
- m_fieldsinfo.put("nfieldinfo", fieldinfo);
- }
-
- protected void buildGettersSpecs(TypeSpec.Builder typespecbuilder) throws AnnotationProcessingException {
- MethodInfo methodinfo;
- TypeName ftname;
- String unsafename = m_fieldsinfo.get("unsafe").name;
- String holdername = m_fieldsinfo.get("holder").name;
- String allocname = m_fieldsinfo.get("allocator").name;
- String autoreclaimname = m_fieldsinfo.get("autoreclaim").name;
- String factoryproxyname = m_fieldsinfo.get("factoryproxy").name;
- String genericfieldname = m_fieldsinfo.get("genericfield").name;
- FieldInfo dynfieldinfo;
- CodeBlock.Builder code;
- String codefmt;
- for (String name : m_gettersinfo.keySet()) {
- code = CodeBlock.builder();
- methodinfo = m_gettersinfo.get(name);
- dynfieldinfo = m_dynfieldsinfo.get(name);
- ftname = m_dynfieldsinfo.get(name).type;
- if (isUnboxPrimitive(ftname)) {
- if (unboxTypeName(ftname).equals(TypeName.BOOLEAN)) {
- codefmt = "return 1 == $1N.$4L($2N.get() + $3L)";
- } else {
- codefmt = "return $1N.$4L($2N.get() + $3L)";
- }
- code.addStatement(codefmt, unsafename, holdername, dynfieldinfo.fieldoff, transTypeToUnsafeMethod(ftname, true));
- } else {
- if(methodinfo.rettype.toString().equals(String.class.getCanonicalName())) {
- code.beginControlFlow("if (null == $1N)", dynfieldinfo.name);
- code.addStatement("long phandler = $1N.getAddress($2N.get() + $3L)",
- unsafename, holdername, dynfieldinfo.fieldoff);
- code.beginControlFlow("if (0L != phandler)");
- code.addStatement("$1N = $2N.retrieveBuffer(phandler, $3N)",
- dynfieldinfo.name, allocname, autoreclaimname);
- code.beginControlFlow("if (null == $1N)", dynfieldinfo.name);
- code.addStatement("throw new RetrieveNonVolatileEntityError(\"Retrieve String Buffer Failure.\")");
- code.endControlFlow();
- code.endControlFlow();
- code.endControlFlow();
- code.addStatement("return null == $1N ? null : $1N.get().asCharBuffer().toString()",
- dynfieldinfo.name);
- } else if (dynfieldinfo.type.toString().startsWith(GenericField.class.getCanonicalName())) {
- code.beginControlFlow("if (null == $1N)", dynfieldinfo.name);
- code.addStatement("$1T proxy = null", TypeName.get(EntityFactoryProxy.class));
- code.addStatement("$1T gftype = null", TypeName.get(GenericField.GType.class));
- code.addStatement("int gfpidx = $1L", getFactoryProxyIndex(methodinfo.rettype));
- code.beginControlFlow("if (null != $1N && $1N.length > gfpidx)", factoryproxyname);
- code.addStatement("proxy = $1L[gfpidx]", factoryproxyname);
- code.endControlFlow();
- code.beginControlFlow("if (null != $1N && $1N.length > gfpidx)", genericfieldname);
- code.addStatement("gftype = $1L[gfpidx]", genericfieldname);
- code.nextControlFlow("else");
- code.addStatement("throw new RetrieveNonVolatileEntityError(\"No Generic Field Type Info.\")");
- code.endControlFlow();
- code.addStatement("$1N = new $2T(proxy, gftype, $8L, $9L, $3N, $4N, $5N, $6N.get() + $7L)",
- dynfieldinfo.name, dynfieldinfo.type,
- allocname, unsafename, autoreclaimname, holdername, dynfieldinfo.fieldoff,
- dynfieldinfo.efproxiesname, dynfieldinfo.gftypesname);
- code.endControlFlow();
- code.addStatement("return $1N.get()", dynfieldinfo.name);
- } else {
- code.beginControlFlow("if (null == $1N)", dynfieldinfo.name);
- code.addStatement("long phandler = $1N.getAddress($2N.get() + $3L)",
- unsafename, holdername, dynfieldinfo.fieldoff);
- code.beginControlFlow("if (0L != phandler)");
- code.addStatement("$1N = $4N.restore($2N, $5L, $6L, phandler, $3N)",
- dynfieldinfo.name, allocname, autoreclaimname,
- String.format("%s%s", m_typeutils.asElement(methodinfo.elem.getReturnType()).getSimpleName(), FACTORYNAMESUFFIX),
- dynfieldinfo.efproxiesname, dynfieldinfo.gftypesname);
- code.endControlFlow();
- code.endControlFlow();
- code.addStatement("return $1N", dynfieldinfo.name);
- }
- }
- typespecbuilder.addMethod(methodinfo.specbuilder.addCode(code.build()).build());
- }
- }
-
- protected String gsetterName(String name, boolean isget) {
- return String.format("%s%s", isget ? "get" : "set", name); //Character.toUpperCase(name.charAt(0)) + name.substring(1));
- }
-
- protected void buildSettersSpecs(TypeSpec.Builder typespecbuilder) throws AnnotationProcessingException {
- MethodInfo methodinfo;
- TypeName ftname, valtname;
- String unsafename = m_fieldsinfo.get("unsafe").name;
- String holdername = m_fieldsinfo.get("holder").name;
- String allocname = m_fieldsinfo.get("allocator").name;
- String autoreclaimname = m_fieldsinfo.get("autoreclaim").name;
- String factoryproxyname = m_fieldsinfo.get("factoryproxy").name;
- String genericfieldname = m_fieldsinfo.get("genericfield").name;
- FieldInfo dynfieldinfo;
- CodeBlock.Builder code;
- VariableElement arg0;
- VariableElement arg1;
- String codefmt;
- for (String name : m_settersinfo.keySet()) {
- code = CodeBlock.builder();
- methodinfo = m_settersinfo.get(name);
- dynfieldinfo = m_dynfieldsinfo.get(name);
- ftname = m_dynfieldsinfo.get(name).type;
- valtname = m_gettersinfo.get(name).rettype;
- arg0 = methodinfo.elem.getParameters().get(0);
- if (!TypeName.get(arg0.asType()).equals(valtname)) {
- throw new AnnotationProcessingException(null, "%s has inconsistent value type with its getter/setter.",
- name);
- }
- if (isUnboxPrimitive(ftname)) {
- if (unboxTypeName(ftname).equals(TypeName.BOOLEAN)) {
- codefmt = "$1N.$4L($2N.get() + $3L, $5L?1:0)";
- } else {
- codefmt = "$1N.$4L($2N.get() + $3L, $5L)";
- }
- code.addStatement(codefmt, unsafename, holdername, dynfieldinfo.fieldoff, transTypeToUnsafeMethod(ftname, false), arg0);
- } else {
- try {
- arg1 = methodinfo.elem.getParameters().get(1);
- if (!TypeName.BOOLEAN.equals(TypeName.get(arg1.asType()))) {
- throw new AnnotationProcessingException(null, "the second parameter of %s's setter is not boolean type.",
- name);
- }
- } catch (IndexOutOfBoundsException ex) {
- throw new AnnotationProcessingException(null, "%s's setter has no second parameters for non primitive type.",
- name);
- }
- if(valtname.toString().equals(String.class.getCanonicalName())) {
- code.beginControlFlow("if ($1L && null != $2L())", arg1, gsetterName(name, true));
- code.addStatement("$1N.destroy()", dynfieldinfo.name);
- code.addStatement("$1N = null", dynfieldinfo.name);
- code.addStatement("$1N.putAddress($2N.get() + $3L, 0L)",
- unsafename, holdername, dynfieldinfo.fieldoff);
- code.endControlFlow();
- code.beginControlFlow("if (null == $1L)", arg0);
- code.addStatement("$1N.putLong($2N.get() + $3L, 0L)",
- unsafename, holdername, dynfieldinfo.fieldoff);
- code.nextControlFlow("else");
- code.addStatement("$1N = $2N.createBuffer($3L.length() * 2, $4N)",
- dynfieldinfo.name, allocname, arg0, autoreclaimname);
- code.beginControlFlow("if (null == $1N)", dynfieldinfo.name);
- code.addStatement("throw new OutOfPersistentMemory(\"Create Non-Volatile String Error!\")");
- code.endControlFlow();
- code.addStatement("$1N.get().asCharBuffer().put($2L)", dynfieldinfo.name, arg0);
- code.addStatement("$1N.putLong($2N.get() + $3L, $4N.getBufferHandler($5N))",
- unsafename, holdername, dynfieldinfo.fieldoff, allocname, dynfieldinfo.name);
- code.endControlFlow();
- } else if (dynfieldinfo.type.toString().startsWith(GenericField.class.getCanonicalName())) {
- code.beginControlFlow("if (null == $1N)", dynfieldinfo.name);
- code.addStatement("$1T proxy = null", TypeName.get(EntityFactoryProxy.class));
- code.addStatement("$1T gftype = null", TypeName.get(GenericField.GType.class));
- code.addStatement("int gfpidx = $1L", getFactoryProxyIndex(valtname));
- code.beginControlFlow("if (null != $1N && $1N.length > gfpidx)", factoryproxyname);
- code.addStatement("proxy = $1L[gfpidx]", factoryproxyname);
- code.endControlFlow();
- code.beginControlFlow("if (null != $1N && $1N.length > gfpidx)", genericfieldname);
- code.addStatement("gftype = $1L[gfpidx]", genericfieldname);
- code.nextControlFlow("else");
- code.addStatement("throw new RetrieveNonVolatileEntityError(\"No Generic Field Type Info.\")");
- code.endControlFlow();
- code.addStatement("$1N = new $2T(proxy, gftype, $8L, $9L, $3N, $4N, $5N, $6N.get() + $7L)",
- dynfieldinfo.name, dynfieldinfo.type,
- allocname, unsafename, autoreclaimname, holdername, dynfieldinfo.fieldoff,
- dynfieldinfo.efproxiesname, dynfieldinfo.gftypesname);
- code.endControlFlow();
- code.beginControlFlow("if (null != $1L)", dynfieldinfo.name);
- code.addStatement("$1N.set($2L, $3L)", dynfieldinfo.name, arg0, arg1);
- code.nextControlFlow("else");
- code.addStatement("throw new RetrieveNonVolatileEntityError(\"GenericField is null!\")");
- code.endControlFlow();
- } else {
- code.beginControlFlow("if ($1L && null != $2L())", arg1, gsetterName(name, true));
- code.addStatement("$1N.destroy()", dynfieldinfo.name);
- code.addStatement("$1N = null", dynfieldinfo.name);
- code.addStatement("$1N.putAddress($2N.get() + $3L, 0L)",
- unsafename, holdername, dynfieldinfo.fieldoff);
- code.endControlFlow();
- code.addStatement("$1N = $2L", dynfieldinfo.name, arg0);
- code.addStatement("$1N.putLong($2N.get() + $3L, null == $4N ? 0L : $4N.getNonVolatileHandler())",
- unsafename, holdername, dynfieldinfo.fieldoff, dynfieldinfo.name);
- }
- }
- typespecbuilder.addMethod(methodinfo.specbuilder.addCode(code.build()).build());
- }
- }
-
- protected void buildDurableMethodSpecs(TypeSpec.Builder typespecbuilder) throws AnnotationProcessingException {
- MethodInfo methodinfo;
- CodeBlock.Builder code;
- FieldInfo dynfieldinfo;
- String holdername = m_fieldsinfo.get("holder").name;
- String allocname = m_fieldsinfo.get("allocator").name;
- String autoreclaimname = m_fieldsinfo.get("autoreclaim").name;
- for (String name : m_durablemtdinfo.keySet()) {
- methodinfo = m_durablemtdinfo.get(name);
- code = CodeBlock.builder();
- switch(name) {
- case "cancelAutoReclaim" :
- code.addStatement("$1N.cancelAutoReclaim()", holdername);
- for (String fname : m_dynfieldsinfo.keySet()) {
- dynfieldinfo = m_dynfieldsinfo.get(fname);
- if (!isUnboxPrimitive(dynfieldinfo.type)) {
- code.beginControlFlow("if (null != $1N)", dynfieldinfo.name);
- code.addStatement("$1N.cancelAutoReclaim()", dynfieldinfo.name);
- code.endControlFlow();
- }
- }
- code.addStatement("$1N = false", autoreclaimname);
- break;
- case "registerAutoReclaim" :
- code.addStatement("$1N.registerAutoReclaim()", holdername);
- for (String fname : m_dynfieldsinfo.keySet()) {
- dynfieldinfo = m_dynfieldsinfo.get(fname);
- if (!isUnboxPrimitive(dynfieldinfo.type)) {
- code.beginControlFlow("if (null != $1N)", dynfieldinfo.name);
- code.addStatement("$1N.registerAutoReclaim()", dynfieldinfo.name);
- code.endControlFlow();
- }
- }
- code.addStatement("$1N = true", autoreclaimname);
- break;
- case "getNonVolatileHandler" :
- code.addStatement("return $1N.getChunkHandler($2N)", allocname, holdername);
- break;
- case "autoReclaim" :
- code.addStatement("return $1N", autoreclaimname);
- break;
- case "destroy" :
- code.addStatement("$1N.destroy()", holdername);
- for (String fname : m_dynfieldsinfo.keySet()) {
- dynfieldinfo = m_dynfieldsinfo.get(fname);
- if (!isUnboxPrimitive(dynfieldinfo.type)) {
- code.beginControlFlow("if (null != $1N)", dynfieldinfo.name);
- code.addStatement("$1N.destroy()", dynfieldinfo.name);
- code.addStatement("$1N = null", dynfieldinfo.name);
- code.endControlFlow();
- }
- }
- break;
- case "getNativeFieldInfo" :
- code.addStatement("return $1N", m_fieldsinfo.get("nfieldinfo").name);
- break;
- default:
- throw new AnnotationProcessingException(null, "Method %s is not supported.",
- name);
- }
- typespecbuilder.addMethod(methodinfo.specbuilder.addCode(code.build()).build());
- }
- }
-
- protected void buildEntityMethodSpecs(TypeSpec.Builder typespecbuilder) throws AnnotationProcessingException {
- MethodInfo methodinfo;
- CodeBlock.Builder code;
- FieldInfo dynfieldinfo;
- VariableElement arg0, arg1, arg2, arg3, arg4;
- String unsafename = m_fieldsinfo.get("unsafe").name;
- String holdername = m_fieldsinfo.get("holder").name;
- String allocname = m_fieldsinfo.get("allocator").name;
- String autoreclaimname = m_fieldsinfo.get("autoreclaim").name;
- String factoryproxyname = m_fieldsinfo.get("factoryproxy").name;
- String genericfieldname = m_fieldsinfo.get("genericfield").name;
- for (String name : m_entitymtdinfo.keySet()) {
- methodinfo = m_entitymtdinfo.get(name);
- code = CodeBlock.builder();
- arg0 = methodinfo.elem.getParameters().get(0);
- arg1 = methodinfo.elem.getParameters().get(1);
- arg2 = methodinfo.elem.getParameters().get(2);
- switch(name) {
- case "initializeNonVolatileEntity" :
- arg3 = methodinfo.elem.getParameters().get(3);
- code.addStatement("$1N = $2L", allocname, arg0);
- code.addStatement("$1N = $2L", factoryproxyname, arg1);
- code.addStatement("$1N = $2L", genericfieldname, arg2);
- code.addStatement("$1N = $2L", autoreclaimname, arg3);
- code.beginControlFlow("try");
- code.addStatement("$1N = $2T.getUnsafe()", unsafename, Utils.class);
- code.nextControlFlow("catch (Exception e)");
- code.addStatement("e.printStackTrace()");
- code.endControlFlow();
- break;
- case "createNonVolatileEntity" :
- arg3 = methodinfo.elem.getParameters().get(3);
- code.addStatement("initializeNonVolatileEntity($1L, $2L, $3L, $4L)", arg0, arg1, arg2, arg3);
- code.addStatement("$1N = $2N.createChunk($3L, $4N)",
- holdername, allocname, m_holdersize, autoreclaimname);
- code.beginControlFlow("if (null == $1N)", holdername);
- code.addStatement("throw new OutOfPersistentMemory(\"Create Non-Volatile Entity Error!\")");
- code.endControlFlow();
- // code.beginControlFlow("try");
- // for (String fname : m_dynfieldsinfo.keySet()) {
- // dynfieldinfo = m_dynfieldsinfo.get(fname);
- // if (isUnboxPrimitive(dynfieldinfo.type)) {
- // code.addStatement("$1N($2L)", gsetterName(fname, false), getIntialValueLiteral(dynfieldinfo.type));
- // } else {
- // code.addStatement("$1N(null, false)", gsetterName(fname, false));
- // }
- // }
- // code.nextControlFlow("catch(RetrieveNonVolatileEntityError ex)");
- // code.endControlFlow();
- code.addStatement("initializeAfterCreate()");
- break;
- case "restoreNonVolatileEntity" :
- arg3 = methodinfo.elem.getParameters().get(3);
- arg4 = methodinfo.elem.getParameters().get(4);
- code.addStatement("initializeNonVolatileEntity($1L, $2L, $3L, $4L)", arg0, arg1, arg2, arg4);
- code.beginControlFlow("if (0L == $1L)", arg3);
- code.addStatement("throw new RetrieveNonVolatileEntityError(\"Input handler is null on $1N.\")", name);
- code.endControlFlow();
- code.addStatement("$1N = $2N.retrieveChunk($3L, $4N)",
- holdername, allocname, arg3, autoreclaimname);
- code.beginControlFlow("if (null == $1N)", holdername);
- code.addStatement("throw new RetrieveNonVolatileEntityError(\"Retrieve Entity Failure!\")");
- code.endControlFlow();
- code.addStatement("initializeAfterRestore()");
- break;
- default:
- throw new AnnotationProcessingException(null, "Method %s is not supported.",
- name);
- }
- typespecbuilder.addMethod(methodinfo.specbuilder.addCode(code.build()).build());
- }
- }
-
- protected void buildFieldSpecs(TypeSpec.Builder typespecbuilder, Map<String, FieldInfo> fieldinfos) {
- FieldInfo fieldinfo;
- for (String name : fieldinfos.keySet()) {
- fieldinfo = fieldinfos.get(name);
- if (null != fieldinfo.specbuilder) {
- typespecbuilder.addField(fieldinfo.specbuilder.build());
- }
- }
- }
-
- protected void buildFactoryMethodSpecs(TypeSpec entityspec, TypeSpec.Builder typespecbuilder) throws AnnotationProcessingException {
- MethodSpec methodspec;
- CodeBlock code;
-
- TypeName entitytn= ParameterizedTypeName.get(ClassName.get(m_packagename, m_entityname), entityspec.typeVariables.toArray(new TypeVariableName[0]));
-
- ParameterSpec allocparam = ParameterSpec.builder(m_alloctypename, "allocator").build();
- code = CodeBlock.builder()
- .addStatement("return create($1L, false)", allocparam.name)
- .build();
- methodspec = MethodSpec.methodBuilder("create")
- .addTypeVariables(entityspec.typeVariables)
- .addException(OutOfPersistentMemory.class)
- .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
- .returns(TypeName.get(m_elem.asType()))
- .addParameter(allocparam)
- .addCode(code).build();
- typespecbuilder.addMethod(methodspec);
-
- ParameterSpec autoreclaimparam = ParameterSpec.builder(TypeName.BOOLEAN, "autoreclaim").build();
- code = CodeBlock.builder()
- .addStatement("return create($1L, null, null, $2L)", allocparam.name, autoreclaimparam.name)
- .build();
- methodspec = MethodSpec.methodBuilder("create")
- .addTypeVariables(entityspec.typeVariables)
- .addException(OutOfPersistentMemory.class)
- .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
- .returns(TypeName.get(m_elem.asType()))
- .addParameter(allocparam)
- .addParameter(autoreclaimparam)
- .addCode(code).build();
- typespecbuilder.addMethod(methodspec);
-
- ParameterSpec factoryproxysparam = ParameterSpec.builder(m_factoryproxystypename, "factoryproxys").build();
- ParameterSpec gfieldsparam = ParameterSpec.builder(m_gfieldstypename, "gfields").build();
- code = CodeBlock.builder()
- .addStatement("$1T entity = new $1T()", entitytn)
- .addStatement("entity.setupGenericInfo($1N, $2N)", factoryproxysparam.name, gfieldsparam.name)
- .addStatement("entity.createNonVolatileEntity($1L, $2L, $3L, $4L)",
- allocparam.name, factoryproxysparam.name, gfieldsparam.name, autoreclaimparam.name)
- .addStatement("return entity")
- .build();
- methodspec = MethodSpec.methodBuilder("create")
- .addTypeVariables(entityspec.typeVariables)
- .addException(OutOfPersistentMemory.class)
- .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
- .returns(TypeName.get(m_elem.asType()))
- .addParameter(allocparam)
- .addParameter(factoryproxysparam)
- .addParameter(gfieldsparam)
- .addParameter(autoreclaimparam)
- .addCode(code).build();
- typespecbuilder.addMethod(methodspec);
-
- ParameterSpec phandlerparam = ParameterSpec.builder(TypeName.LONG, "phandler").build();
- code = CodeBlock.builder()
- .addStatement("return restore($1L, $2L, false)", allocparam.name, phandlerparam.name)
- .build();
- methodspec = MethodSpec.methodBuilder("restore")
- .addTypeVariables(entityspec.typeVariables)
- .addException(RetrieveNonVolatileEntityError.class)
- .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
- .returns(TypeName.get(m_elem.asType()))
- .addParameter(allocparam)
- .addParameter(phandlerparam)
- .addCode(code).build();
- typespecbuilder.addMethod(methodspec);
-
- code = CodeBlock.builder()
- .addStatement("return restore($1L, null, null, $2L, $3L)", allocparam.name, phandlerparam.name, autoreclaimparam.name)
- .build();
- methodspec = MethodSpec.methodBuilder("restore")
- .addTypeVariables(entityspec.typeVariables)
- .addException(RetrieveNonVolatileEntityError.class)
- .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
- .returns(TypeName.get(m_elem.asType()))
- .addParameter(allocparam)
- .addParameter(phandlerparam)
- .addParameter(autoreclaimparam)
- .addCode(code).build();
- typespecbuilder.addMethod(methodspec);
-
- code = CodeBlock.builder()
- .addStatement("$1T entity = new $1T()", entitytn)
- .addStatement("entity.setupGenericInfo($1N, $2N)", factoryproxysparam.name, gfieldsparam.name)
- .addStatement("entity.restoreNonVolatileEntity($1L, $2L, $3L, $4L, $5L)",
- allocparam.name, factoryproxysparam.name, gfieldsparam.name, phandlerparam.name, autoreclaimparam.name)
- .addStatement("return entity")
- .build();
- methodspec = MethodSpec.methodBuilder("restore")
- .addTypeVariables(entityspec.typeVariables)
- .addException(RetrieveNonVolatileEntityError.class)
- .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
- .returns(TypeName.get(m_elem.asType()))
- .addParameter(allocparam)
- .addParameter(factoryproxysparam)
- .addParameter(gfieldsparam)
- .addParameter(phandlerparam)
- .addParameter(autoreclaimparam)
- .addCode(code).build();
- typespecbuilder.addMethod(methodspec);
- }
-
- public void generateCode(Filer filer) throws IOException, AnnotationProcessingException {
- AnnotationSpec classannotation = AnnotationSpec.builder(SuppressWarnings.class)
- .addMember("value", "$S", "restriction").build();
-
- TypeSpec.Builder entitybuilder = TypeSpec.classBuilder(m_entityname)
- .superclass(TypeName.get(m_elem.asType()))
- .addModifiers(Modifier.PUBLIC).addAnnotation(classannotation)
- .addSuperinterface(
- ParameterizedTypeName.get(ClassName.get(MemoryNonVolatileEntity.class), m_alloctypevarname))
- .addTypeVariable(m_alloctypevarname);
-
- for (TypeParameterElement tpe : m_elem.getTypeParameters()) {
- entitybuilder.addTypeVariable(TypeVariableName.get(tpe));
- }
-
- buildFieldSpecs(entitybuilder, m_dynfieldsinfo);
- buildFieldSpecs(entitybuilder, m_fieldsinfo);
-
- buildGettersSpecs(entitybuilder);
- buildSettersSpecs(entitybuilder);
-
- buildDurableMethodSpecs(entitybuilder);
- buildEntityMethodSpecs(entitybuilder);
-
- TypeSpec entityspec = entitybuilder.build();
-
- JavaFile entityFile = JavaFile
- .builder(m_packagename, entityspec).build();
-
- entityFile.writeTo(filer);
-
- TypeSpec.Builder factorybuilder = TypeSpec.classBuilder(m_factoryname)
- .addModifiers(Modifier.PUBLIC);
-
- buildFactoryMethodSpecs(entityspec, factorybuilder);
-
- JavaFile factoryFile = JavaFile
- .builder(m_packagename, factorybuilder.build()).build();
-
- factoryFile.writeTo(filer);
-
- }
-
- public static Builder overriding(ExecutableElement method, String varname) {
-
- Set<Modifier> modifiers = method.getModifiers();
- if (modifiers.contains(Modifier.PRIVATE) || modifiers.contains(Modifier.FINAL)
- || modifiers.contains(Modifier.STATIC)) {
- throw new IllegalArgumentException("cannot override method with modifiers: " + modifiers);
- }
-
- String methodName = method.getSimpleName().toString();
- MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(methodName);
-
- methodBuilder.addAnnotation(Override.class);
- for (AnnotationMirror mirror : method.getAnnotationMirrors()) {
- AnnotationSpec annotationSpec = AnnotationSpec.get(mirror);
- if (annotationSpec.type.equals(Override.class))
- continue;
- methodBuilder.addAnnotation(annotationSpec);
- }
-
- modifiers = new LinkedHashSet<>(modifiers);
- modifiers.remove(Modifier.ABSTRACT);
- methodBuilder.addModifiers(modifiers);
-
- for (TypeParameterElement typeParameterElement : method.getTypeParameters()) {
- TypeVariable var = (TypeVariable) typeParameterElement.asType();
- methodBuilder.addTypeVariable(TypeVariableName.get(var));
- }
-
- methodBuilder.returns(TypeName.get(method.getReturnType()));
-
- List<? extends VariableElement> parameters = method.getParameters();
- TypeName type;
- for (VariableElement parameter : parameters) {
- if (parameter.asType().getKind() == TypeKind.TYPEVAR && parameter.asType().toString().equals(varname)) {
- type = TypeVariableName.get(varname);
- } else {
- type = TypeName.get(parameter.asType());
- }
-
- String name = parameter.getSimpleName().toString();
- Set<Modifier> parameterModifiers = parameter.getModifiers();
- ParameterSpec.Builder parameterBuilder = ParameterSpec.builder(type, name)
- .addModifiers(parameterModifiers.toArray(new Modifier[parameterModifiers.size()]));
- for (AnnotationMirror mirror : parameter.getAnnotationMirrors()) {
- parameterBuilder.addAnnotation(AnnotationSpec.get(mirror));
- }
- methodBuilder.addParameter(parameterBuilder.build());
- }
- methodBuilder.varargs(method.isVarArgs());
-
- for (TypeMirror thrownType : method.getThrownTypes()) {
- methodBuilder.addException(TypeName.get(thrownType));
- }
-
- return methodBuilder;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/AnnotationProcessingException.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/AnnotationProcessingException.java b/core/src/main/java/com/intel/bigdatamem/AnnotationProcessingException.java
deleted file mode 100644
index 561e071..0000000
--- a/core/src/main/java/com/intel/bigdatamem/AnnotationProcessingException.java
+++ /dev/null
@@ -1,23 +0,0 @@
-package com.intel.bigdatamem;
-
-import javax.lang.model.element.Element;
-
-/**
- * this exception is used for non-volatile annotation processing
- *
- */
-public class AnnotationProcessingException extends Exception {
-
- private static final long serialVersionUID = 6911141027622831646L;
-
- private Element element;
-
- public AnnotationProcessingException(Element element, String msg, Object... args) {
- super(String.format(msg, args));
- this.element = element;
- }
-
- public Element getElement() {
- return element;
- }
-}
[4/7] incubator-mnemonic git commit: MNEMONIC-5 MNEMONIC-6 #resolve
refactored package names
Posted by ga...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/OutOfPersistentMemory.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/OutOfPersistentMemory.java b/core/src/main/java/com/intel/bigdatamem/OutOfPersistentMemory.java
deleted file mode 100644
index 08ef84a..0000000
--- a/core/src/main/java/com/intel/bigdatamem/OutOfPersistentMemory.java
+++ /dev/null
@@ -1,15 +0,0 @@
-package com.intel.bigdatamem;
-
-/**
- * this is an exception that should be throw once out of persistent memory
- *
- */
-
-public class OutOfPersistentMemory extends RuntimeException {
-
- private static final long serialVersionUID = -6315943783592441148L;
-
- public OutOfPersistentMemory(String s) {
- super(s);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/PMAddressTranslator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/PMAddressTranslator.java b/core/src/main/java/com/intel/bigdatamem/PMAddressTranslator.java
deleted file mode 100644
index 60337f9..0000000
--- a/core/src/main/java/com/intel/bigdatamem/PMAddressTranslator.java
+++ /dev/null
@@ -1,45 +0,0 @@
-package com.intel.bigdatamem;
-
-
-/**
- * translate persistent memory address for allocator
- *
- */
-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 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);
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/Reclaim.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/Reclaim.java b/core/src/main/java/com/intel/bigdatamem/Reclaim.java
deleted file mode 100644
index 33b978f..0000000
--- a/core/src/main/java/com/intel/bigdatamem/Reclaim.java
+++ /dev/null
@@ -1,23 +0,0 @@
-package com.intel.bigdatamem;
-
-/**
- * an interface to reclaim its memory resource.
- *
- */
-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);
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/RetrieveNonVolatileEntityError.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/RetrieveNonVolatileEntityError.java b/core/src/main/java/com/intel/bigdatamem/RetrieveNonVolatileEntityError.java
deleted file mode 100644
index 3e8b0c0..0000000
--- a/core/src/main/java/com/intel/bigdatamem/RetrieveNonVolatileEntityError.java
+++ /dev/null
@@ -1,19 +0,0 @@
-package com.intel.bigdatamem;
-
-/**
- *
- *
- */
-
-
-public class RetrieveNonVolatileEntityError extends RuntimeException {
-
- /**
- *
- */
- private static final long serialVersionUID = 3985459401986971131L;
-
- public RetrieveNonVolatileEntityError(String s) {
- super(s);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/SysMemAllocator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/SysMemAllocator.java b/core/src/main/java/com/intel/bigdatamem/SysMemAllocator.java
deleted file mode 100644
index ab85917..0000000
--- a/core/src/main/java/com/intel/bigdatamem/SysMemAllocator.java
+++ /dev/null
@@ -1,305 +0,0 @@
-package com.intel.bigdatamem;
-
-import java.lang.reflect.*;
-import java.nio.ByteBuffer;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.atomic.AtomicLong;
-
-import org.flowcomputing.commons.resgc.*;
-
-import sun.misc.Unsafe;
-import sun.misc.Cleaner;
-
-/**
- * manage a system memory pool as a internal volatile allocator
- *
- */
-@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>();
-
-
- /**
- * 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;
- /**
- * 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);
- }
- }
- });
- }
-
- /**
- * 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;
- }
-
- /**
- * 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) {
- forceGC();
- }
- }
- 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;
- }
-
- /**
- * 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) {
- forceGC();
- }
- }
- 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;
- }
-
- /**
- * 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) {
- forceGC();
- }
- }
- 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;
- }
-
- /**
- * 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) {
- forceGC();
- }
- }
- 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;
- }
-
- /**
- * force to perform GC that is used to release unused backed memory
- * resources.
- */
- private void forceGC() {
- System.gc();
- try {
- Thread.sleep(m_gctimeout);
- } catch (Exception ex) {
- }
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/bigdatamem/Utils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/bigdatamem/Utils.java b/core/src/main/java/com/intel/bigdatamem/Utils.java
deleted file mode 100644
index 14a895a..0000000
--- a/core/src/main/java/com/intel/bigdatamem/Utils.java
+++ /dev/null
@@ -1,384 +0,0 @@
-package com.intel.bigdatamem;
-
-import java.lang.reflect.Field;
-import java.nio.ByteBuffer;
-import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Date;
-import java.util.Random;
-import java.util.UUID;
-
-import sun.misc.Unsafe;
-import java.util.Iterator;
-import java.util.List;
-import java.util.ServiceConfigurationError;
-import java.util.ServiceLoader;
-import com.intel.mnemonic.service.allocatorservice.VolatileMemoryAllocatorService;
-import com.intel.mnemonic.service.allocatorservice.NonVolatileMemoryAllocatorService;
-
-/**
- * <p>
- * Utilities for project.
- * </p>
- *
- */
-@SuppressWarnings("restriction")
-public class Utils {
- 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";
-
- private static Unsafe m_unsafe = 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
- *
- * @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;
- }
-
- /**
- * 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;
- }
-
- /**
- * 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 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);
- }
-
- /**
- * run garbage collections.
- */
- private static void putOutTheGarbage() {
- collectGarbage();
- collectGarbage();
- }
-
- /**
- * 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 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;
- }
-
- /**
- * 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;
- }
-
- /**
- * create a new instance of Random using default fixed seed
- *
- * @return the instance of Random
- */
- public static Random createRandom() {
- return createRandom(0L);
- }
-
- /**
- * 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;
- }
-
- /**
- * 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);
- }
-
- /**
- * 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 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;
- }
-
- /**
- * 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");
- }
- 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");
- }
- }
- return ret;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/mnemonic/service/allocatorservice/NonVolatileMemoryAllocatorService.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/mnemonic/service/allocatorservice/NonVolatileMemoryAllocatorService.java b/core/src/main/java/com/intel/mnemonic/service/allocatorservice/NonVolatileMemoryAllocatorService.java
deleted file mode 100644
index 81cc3c8..0000000
--- a/core/src/main/java/com/intel/mnemonic/service/allocatorservice/NonVolatileMemoryAllocatorService.java
+++ /dev/null
@@ -1,97 +0,0 @@
-package com.intel.mnemonic.service.allocatorservice;
-
-import org.flowcomputing.commons.primitives.*;
-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 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);
-
- /**
- * 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);
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/com/intel/mnemonic/service/allocatorservice/VolatileMemoryAllocatorService.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/intel/mnemonic/service/allocatorservice/VolatileMemoryAllocatorService.java b/core/src/main/java/com/intel/mnemonic/service/allocatorservice/VolatileMemoryAllocatorService.java
deleted file mode 100644
index e800872..0000000
--- a/core/src/main/java/com/intel/mnemonic/service/allocatorservice/VolatileMemoryAllocatorService.java
+++ /dev/null
@@ -1,130 +0,0 @@
-package com.intel.mnemonic.service.allocatorservice;
-
-import org.flowcomputing.commons.primitives.*;
-import java.nio.ByteBuffer;
-
-public interface VolatileMemoryAllocatorService {
-
- /**
- * 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);
-
- /**
- * 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);
-
- /**
- * 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);
-
- /**
- * 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);
-
- /**
- * 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);
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/Allocatable.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/Allocatable.java b/core/src/main/java/org/apache/mnemonic/Allocatable.java
new file mode 100644
index 0000000..76ec38d
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/Allocatable.java
@@ -0,0 +1,102 @@
+package org.apache.mnemonic;
+
+/**
+ * an interface to allocate memory resources from any underlying memory kind
+ * of storage.
+ *
+ */
+public interface Allocatable<A extends CommonAllocator<A>> {
+
+ /**
+ * 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
+ */
+ public MemChunkHolder<A> createChunk(long size, boolean autoreclaim);
+
+ /**
+ * create a memory chunk that is managed by its holder.
+ *
+ * @param size
+ * specify the size of memory chunk
+ *
+ * @return a holder contains a memory chunk
+ */
+ public MemChunkHolder<A> createChunk(long size);
+
+ /**
+ * create a memory buffer that is managed by its holder.
+ *
+ * @param size
+ * specify the size of memory buffer
+ *
+ * @param autoreclaim
+ * specify whether or not to reclaim this
+ * buffer automatically
+ *
+ * @return a holder contains a memory buffer
+ */
+ public MemBufferHolder<A> createBuffer(long size, boolean autoreclaim);
+
+ /**
+ * 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
+ */
+ public MemBufferHolder<A> createBuffer(long size);
+
+ /**
+ * register a memory chunk for auto-reclaim
+ *
+ * @param mholder
+ * specify a chunk holder to register
+ */
+ public void registerChunkAutoReclaim(MemChunkHolder<A> mholder);
+
+ /**
+ * register a memory buffer for auto-reclaim
+ *
+ * @param mholder
+ * specify a buffer holder to register
+ */
+ public void registerBufferAutoReclaim(MemBufferHolder<A> mholder);
+
+ /**
+ * resize a memory chunk.
+ *
+ * @param mholder
+ * specify a chunk holder for resizing
+ *
+ * @param size
+ * specify a new size of this memory chunk
+ *
+ * @return the resized memory chunk holder
+ *
+ */
+ public MemChunkHolder<A> resizeChunk(MemChunkHolder<A> mholder, long size);
+
+ /**
+ * resize a memory buffer.
+ *
+ * @param mholder
+ * specify a buffer holder for resizing
+ *
+ * @param size
+ * specify a new size of this memory buffer
+ *
+ * @return the resized memory buffer holder
+ *
+ */
+ public MemBufferHolder<A> resizeBuffer(MemBufferHolder<A> mholder, long size);
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/Allocator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/Allocator.java b/core/src/main/java/org/apache/mnemonic/Allocator.java
new file mode 100644
index 0000000..c887b07
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/Allocator.java
@@ -0,0 +1,39 @@
+package org.apache.mnemonic;
+
+/**
+ * an interface to manage the lifecycle of memory allocator
+ *
+ */
+public interface Allocator<A extends CommonAllocator<A>> extends Allocatable<A> {
+
+ /**
+ * release the underlying memory pool and close it.
+ *
+ */
+ public void close();
+
+ /**
+ * sync. dirty data to underlying memory-like device
+ *
+ */
+ public void sync();
+
+ /**
+ * 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
+ */
+ public A enableActiveGC(long timeout);
+
+ /**
+ * disable active garbage collection.
+ *
+ * @return this allocator
+ */
+ public A disableActiveGC();
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/AnnotatedNonVolatileEntityClass.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/AnnotatedNonVolatileEntityClass.java b/core/src/main/java/org/apache/mnemonic/AnnotatedNonVolatileEntityClass.java
new file mode 100644
index 0000000..68a7a3d
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/AnnotatedNonVolatileEntityClass.java
@@ -0,0 +1,977 @@
+package org.apache.mnemonic;
+
+import java.io.IOException;
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.annotation.processing.*;
+import javax.lang.model.element.AnnotationMirror;
+import javax.lang.model.element.Element;
+import javax.lang.model.element.ElementKind;
+import javax.lang.model.element.ExecutableElement;
+import javax.lang.model.element.Modifier;
+import javax.lang.model.element.TypeElement;
+import javax.lang.model.element.TypeParameterElement;
+import javax.lang.model.element.VariableElement;
+import javax.lang.model.type.DeclaredType;
+import javax.lang.model.type.TypeKind;
+import javax.lang.model.type.TypeMirror;
+import javax.lang.model.type.TypeVariable;
+import javax.lang.model.util.Elements;
+import javax.lang.model.util.Types;
+import sun.misc.Unsafe;
+
+import com.squareup.javapoet.*;
+import com.squareup.javapoet.MethodSpec.Builder;
+
+/**
+ * this class managed to generate generic non-volatile concrete object and their corresponding factory.
+ *
+ */
+public class AnnotatedNonVolatileEntityClass {
+ protected class MethodInfo {
+ public ExecutableElement elem;
+ public MethodSpec.Builder specbuilder;
+ public TypeName rettype;
+ }
+ protected class FieldInfo {
+ public FieldSpec.Builder specbuilder;
+ public TypeName type;
+ public long id;
+ public String name;
+ public String efproxiesname;
+ public String gftypesname;
+ public long fieldoff;
+ public long fieldsize;
+ }
+
+ protected final String FACTORYNAMESUFFIX = "Factory";
+ protected final String PMEMNAMEPREFIX = "NonVolatile_";
+ protected final String FIELDNAMESUFFIX = String.format("_field_%s", Utils.genRandomString());
+ protected final String ALLOCATORFIELDNAME = String.format("alloc_%s", Utils.genRandomString());
+ protected final String AUTORECLAIMFIELDNAME = String.format("autoreclaim_%s", Utils.genRandomString());
+ protected final String UNSAFEFIELDNAME = String.format("unsafe_%s", Utils.genRandomString());
+ protected final String HOLDERFIELDNAME = String.format("holder_%s", Utils.genRandomString());
+ protected final String ALLOCTYPENAME = String.format("ALLOC_PMem3C93D24F59");
+
+ private Types m_typeutils;
+ private Elements m_elemutils;
+ private Messager m_msgr;
+ private TypeElement m_elem;
+
+ private String m_factoryname;
+ private String m_entityname;
+
+ private long m_holdersize;
+
+ private String m_packagename;
+
+ private TypeName m_alloctypename = TypeVariableName.get(ALLOCTYPENAME);
+ private TypeName m_factoryproxystypename = TypeName.get(EntityFactoryProxy[].class);
+ private TypeName m_gfieldstypename = TypeName.get(GenericField.GType[].class);
+ private TypeVariableName m_alloctypevarname = TypeVariableName.get(ALLOCTYPENAME,
+ ParameterizedTypeName.get(ClassName.get(CommonPersistAllocator.class), TypeVariableName.get(ALLOCTYPENAME)));
+
+ private Map<String, MethodInfo> m_gettersinfo = new HashMap<String, MethodInfo>();
+ private Map<String, MethodInfo> m_settersinfo = new HashMap<String, MethodInfo>();
+ private Map<String, FieldInfo> m_dynfieldsinfo = new HashMap<String, FieldInfo>();
+ private Map<String, FieldInfo> m_fieldsinfo = new HashMap<String, FieldInfo>();
+
+ private Map<String, MethodInfo> m_durablemtdinfo = new HashMap<String, MethodInfo>();
+ private Map<String, MethodInfo> m_entitymtdinfo = new HashMap<String, MethodInfo>();
+
+ private long computeTypeSize(TypeMirror type) {
+ long ret;
+ switch (type.getKind()) {
+ case BYTE:
+ ret = 1L;
+ break;
+ case BOOLEAN:
+ ret = 1L;
+ break;
+ case CHAR:
+ ret = 2L;
+ break;
+ case DOUBLE:
+ ret = 8L;
+ break;
+ case FLOAT:
+ ret = 4L;
+ break;
+ case SHORT:
+ ret = 2L;
+ break;
+ case INT:
+ ret = 4L;
+ break;
+ case LONG:
+ ret = 8L;
+ break;
+ default:
+ ret = 8L;
+ }
+ return ret;
+ }
+
+ private boolean isUnboxPrimitive(TypeName tn) {
+ TypeName n = tn;
+ try {
+ n = tn.unbox();
+ } catch(UnsupportedOperationException ex) {}
+ return n.isPrimitive();
+ }
+
+ private TypeName unboxTypeName(TypeName tn) {
+ TypeName n = tn;
+ try {
+ n = tn.unbox();
+ } catch(UnsupportedOperationException ex) {}
+ return n;
+ }
+
+ public AnnotatedNonVolatileEntityClass(TypeElement classElement, Types typeUtils, Elements elementUtils,
+ Messager messager) {
+ m_elem = classElement;
+ m_typeutils = typeUtils;
+ m_elemutils = elementUtils;
+ m_msgr = messager;
+
+ m_packagename = m_elemutils.getPackageOf(m_elem).getQualifiedName().toString();
+
+ m_factoryname = String.format("%s%s", m_elem.getSimpleName(), FACTORYNAMESUFFIX);
+ m_entityname = String.format("%s%s_%s", PMEMNAMEPREFIX, m_elem.getSimpleName(), Utils.genRandomString());
+
+ m_durablemtdinfo.put("cancelAutoReclaim", new MethodInfo());
+ m_durablemtdinfo.put("registerAutoReclaim", new MethodInfo());
+ m_durablemtdinfo.put("getNonVolatileHandler", new MethodInfo());
+ m_durablemtdinfo.put("autoReclaim", new MethodInfo());
+ m_durablemtdinfo.put("destroy", new MethodInfo());
+ m_durablemtdinfo.put("getNativeFieldInfo", new MethodInfo());
+
+ m_entitymtdinfo.put("initializeNonVolatileEntity", new MethodInfo());
+ m_entitymtdinfo.put("createNonVolatileEntity", new MethodInfo());
+ m_entitymtdinfo.put("restoreNonVolatileEntity", new MethodInfo());
+
+ }
+
+ public void prepareProcessing() throws AnnotationProcessingException {
+ MethodInfo methodinfo = null;
+ FieldInfo fieldinfo;
+ String methodname;
+ long fieldoff = 0;
+ TypeElement intf_durable = m_elemutils.getTypeElement(Durable.class.getCanonicalName());
+ TypeElement intf_entity = m_elemutils.getTypeElement(MemoryNonVolatileEntity.class.getCanonicalName());
+ // System.err.printf("<><><><><> %s ======\n", intf_entity.toString());
+
+ boolean valid = false;
+ for (TypeMirror tm : m_elem.getInterfaces()) {
+ if (tm.toString().equals(Durable.class.getCanonicalName())) {
+ valid = true;
+ break;
+ }
+ }
+ if (!valid) {
+ throw new AnnotationProcessingException(m_elem, "Not implemented Durable Interface by %s.",
+ m_elem.getSimpleName().toString());
+ }
+
+ fieldinfo = new FieldInfo();
+ fieldinfo.name = String.format("m_unsafe_%s",
+ Utils.genRandomString());
+ fieldinfo.type = TypeName.get(m_elemutils.getTypeElement(Unsafe.class.getCanonicalName()).asType());
+ fieldinfo.specbuilder = FieldSpec.builder(fieldinfo.type,
+ fieldinfo.name, Modifier.PRIVATE);
+ m_fieldsinfo.put("unsafe", fieldinfo);
+
+ fieldinfo = new FieldInfo();
+ fieldinfo.name = String.format("m_holder_%s",
+ Utils.genRandomString());
+ fieldinfo.type = ParameterizedTypeName.get(ClassName.get(MemChunkHolder.class), m_alloctypename);
+ fieldinfo.specbuilder = FieldSpec.builder(fieldinfo.type,
+ fieldinfo.name, Modifier.PRIVATE);
+ m_fieldsinfo.put("holder", fieldinfo);
+
+ fieldinfo = new FieldInfo();
+ fieldinfo.name = String.format("m_autoreclaim_%s",
+ Utils.genRandomString());
+ fieldinfo.type = TypeName.get(m_typeutils.getPrimitiveType(TypeKind.BOOLEAN));
+ fieldinfo.specbuilder = FieldSpec.builder(fieldinfo.type,
+ fieldinfo.name, Modifier.PRIVATE, Modifier.VOLATILE);
+ m_fieldsinfo.put("autoreclaim", fieldinfo);
+
+ fieldinfo = new FieldInfo();
+ fieldinfo.name = String.format("m_allocator_%s",
+ Utils.genRandomString());
+ fieldinfo.type = m_alloctypename;
+ fieldinfo.specbuilder = FieldSpec.builder(fieldinfo.type,
+ fieldinfo.name, Modifier.PRIVATE);
+ m_fieldsinfo.put("allocator", fieldinfo);
+
+ fieldinfo = new FieldInfo();
+ fieldinfo.name = String.format("m_factoryproxy_%s",
+ Utils.genRandomString());
+ fieldinfo.type = m_factoryproxystypename;
+ fieldinfo.specbuilder = FieldSpec.builder(fieldinfo.type,
+ fieldinfo.name, Modifier.PRIVATE);
+ m_fieldsinfo.put("factoryproxy", fieldinfo);
+
+ fieldinfo = new FieldInfo();
+ fieldinfo.name = String.format("m_genericfield_%s",
+ Utils.genRandomString());
+ fieldinfo.type = m_gfieldstypename;
+ fieldinfo.specbuilder = FieldSpec.builder(fieldinfo.type,
+ fieldinfo.name, Modifier.PRIVATE);
+ m_fieldsinfo.put("genericfield", fieldinfo);
+
+ for (Element elem : m_elem.getEnclosedElements()) {
+ if (elem.getKind() == ElementKind.METHOD) {
+ methodname = elem.getSimpleName().toString();
+ // System.err.printf("=========== %s ======\n", methodname);
+ NonVolatileGetter pgetter = elem.getAnnotation(NonVolatileGetter.class);
+ if (pgetter != null) {
+ if (!elem.getModifiers().contains(Modifier.ABSTRACT)) {
+ throw new AnnotationProcessingException(elem,
+ "%s annotated with NonVolatileGetter is not abstract.", methodname);
+ }
+ if (null != elem.getAnnotation(NonVolatileSetter.class)) {
+ throw new AnnotationProcessingException(elem, "%s is annotated with NonVolatileSetter as well.",
+ methodname);
+ }
+ if (!methodname.startsWith("get")) {
+ throw new AnnotationProcessingException(elem, "%s does not comply name convention of getter.",
+ methodname);
+ }
+ methodinfo = new MethodInfo();
+ methodinfo.elem = (ExecutableElement) elem;
+ methodinfo.specbuilder = MethodSpec.overriding(methodinfo.elem);
+ methodinfo.rettype = TypeName.get(methodinfo.elem.getReturnType());
+ m_gettersinfo.put(methodname.substring(3), methodinfo);
+ fieldinfo = new FieldInfo();
+ fieldinfo.type = methodinfo.rettype;
+ if (fieldinfo.type.toString().equals(String.class.getCanonicalName())) {
+ fieldinfo.type = ParameterizedTypeName.get(ClassName.get(MemBufferHolder.class), m_alloctypename);
+ }
+ if (fieldinfo.type instanceof TypeVariableName) {
+ fieldinfo.type = ParameterizedTypeName.get(ClassName.get(GenericField.class), m_alloctypename, fieldinfo.type);
+ }
+ fieldinfo.name = String.format("m_%s_%s",
+ methodname.substring(3).toLowerCase(),
+ Utils.genRandomString());
+ fieldinfo.specbuilder = FieldSpec.builder(fieldinfo.type,
+ fieldinfo.name, Modifier.PRIVATE);
+ fieldinfo.fieldsize = computeTypeSize(methodinfo.elem.getReturnType());
+ fieldinfo.fieldoff = fieldoff;
+ fieldoff += fieldinfo.fieldsize;
+ fieldinfo.efproxiesname = pgetter.EntityFactoryProxies();
+ fieldinfo.gftypesname = pgetter.GenericFieldTypes();
+ fieldinfo.id = pgetter.Id();
+ m_dynfieldsinfo.put(methodname.substring(3), fieldinfo);
+
+ }
+ if (null != elem.getAnnotation(NonVolatileSetter.class)) {
+ if (!elem.getModifiers().contains(Modifier.ABSTRACT)) {
+ throw new AnnotationProcessingException(elem,
+ "%s annotated with NonVolatileSetter is not abstract.", methodname);
+ }
+ if (!methodname.startsWith("set")) {
+ throw new AnnotationProcessingException(elem, "%s does not comply name convention of setter.",
+ methodname);
+ }
+ methodinfo = new MethodInfo();
+ methodinfo.elem = (ExecutableElement) elem;
+ methodinfo.specbuilder = MethodSpec.overriding(methodinfo.elem);
+ m_settersinfo.put(methodname.substring(3), methodinfo);
+ }
+ // if (!methodinfo.elem.getThrownTypes().contains(m_elemutils.getTypeElement(RetrieveNonVolatileEntityError.class.getCanonicalName()).asType())) {
+ // throw new AnnotationProcessingException(methodinfo.elem, "%s must throw out %s.",
+ // methodname, RetrieveNonVolatileEntityError.class.getName());
+ // }
+ }
+ }
+
+ m_holdersize = fieldoff;
+
+ // MethodInfo minfo = null;
+ for (String name : m_settersinfo.keySet()) {
+ if (!m_gettersinfo.containsKey(name)) {
+ throw new AnnotationProcessingException(null, "%s has no getter.", name);
+ }
+ // if (m_dynfieldsinfo.get(name).type.toString().equals(String.class.getCanonicalName())) {
+ // minfo = m_settersinfo.get(name);
+ // if (!minfo.elem.getThrownTypes().contains(m_elemutils.getTypeElement(OutOfPersistentMemory.class.getCanonicalName()).asType())) {
+ // throw new AnnotationProcessingException(minfo.elem, "%s must throw out %s.",
+ // gsetterName(name, false), OutOfPersistentMemory.class.getName());
+ // }
+ // }
+ }
+
+ // for (String name : m_dynfieldsinfo.keySet()) {
+ // if (!isUnboxPrimitive(m_dynfieldsinfo.get(name).type)) {
+ // if (m_gettersinfo.containsKey(name)) {
+ // minfo = m_gettersinfo.get(name);
+ // if (!minfo.elem.getThrownTypes().contains(m_elemutils.getTypeElement(RetrieveNonVolatileEntityError.class.getCanonicalName()).asType())) {
+ // throw new AnnotationProcessingException(minfo.elem, "%s must throw out %s.",
+ // gsetterName(name, true), RetrieveNonVolatileEntityError.class.getName());
+ // }
+ // }
+ // if (m_settersinfo.containsKey(name)) {
+ // minfo = m_settersinfo.get(name);
+ // if (!minfo.elem.getThrownTypes().contains(m_elemutils.getTypeElement(RetrieveNonVolatileEntityError.class.getCanonicalName()).asType())) {
+ // throw new AnnotationProcessingException(minfo.elem, "%s must throw out %s.",
+ // gsetterName(name, false), RetrieveNonVolatileEntityError.class.getName());
+ // }
+ // }
+ // }
+ // }
+
+ for (Element elem : intf_durable.getEnclosedElements()) {
+ if (elem.getKind() == ElementKind.METHOD) {
+ methodname = elem.getSimpleName().toString();
+ if (m_durablemtdinfo.containsKey(methodname)) {
+ // System.err.printf("**++++++++++ %s ======\n", methodname);
+ methodinfo = m_durablemtdinfo.get(methodname);
+ methodinfo.elem = (ExecutableElement) elem;
+ methodinfo.specbuilder = MethodSpec.overriding(methodinfo.elem);
+ }
+ }
+ }
+
+ for (Element elem : intf_entity.getEnclosedElements()) {
+ if (elem.getKind() == ElementKind.METHOD) {
+ methodname = elem.getSimpleName().toString();
+ if (m_entitymtdinfo.containsKey(methodname)) {
+ // System.err.printf("**------- %s ======\n", elem.toString());
+ methodinfo = m_entitymtdinfo.get(methodname);
+ methodinfo.elem = (ExecutableElement) elem;
+ methodinfo.specbuilder = overriding(methodinfo.elem, ALLOCTYPENAME);
+
+ }
+ }
+ }
+ genNFieldInfo();
+ }
+
+ protected String transTypeToUnsafeMethod(TypeName tname, boolean isget) throws AnnotationProcessingException {
+ String ret = null;
+ if (isUnboxPrimitive(tname)) {
+ TypeName tn = unboxTypeName(tname);
+ if (tn.equals(TypeName.BOOLEAN)) ret = isget ? "getByte" : "putByte";
+ if (tn.equals(TypeName.BYTE)) ret = isget ? "getByte" : "putByte";
+ if (tn.equals(TypeName.CHAR)) ret = isget ? "getChar" : "putChar";
+ if (tn.equals(TypeName.DOUBLE)) ret = isget ? "getDouble" : "putDouble";
+ if (tn.equals(TypeName.FLOAT)) ret = isget ? "getFloat" : "putFloat";
+ if (tn.equals(TypeName.INT)) ret = isget ? "getInt" : "putInt";
+ if (tn.equals(TypeName.LONG)) ret = isget ? "getLong" : "putLong";
+ if (tn.equals(TypeName.SHORT)) ret = isget ? "getShort" : "putShort";
+ } else {
+ ret = isget ? "getAddress" : "putAddress";
+ }
+ if (null == ret) {
+ throw new AnnotationProcessingException(null, "%s is not supported by getters or setters.",
+ tname.toString());
+ }
+ return ret;
+ }
+
+ protected String getIntialValueLiteral(TypeName tname) throws AnnotationProcessingException {
+ String ret = null;
+ if (isUnboxPrimitive(tname)) {
+ TypeName tn = unboxTypeName(tname);
+ if (tn.equals(TypeName.BOOLEAN)) ret = "false";
+ if (tn.equals(TypeName.BYTE)) ret = "(byte)0";
+ if (tn.equals(TypeName.CHAR)) ret = "(char)0";
+ if (tn.equals(TypeName.DOUBLE)) ret = "(double)0.0";
+ if (tn.equals(TypeName.FLOAT)) ret = "(float)0.0";
+ if (tn.equals(TypeName.INT)) ret = "(int)0";
+ if (tn.equals(TypeName.LONG)) ret = "(long)0";
+ if (tn.equals(TypeName.SHORT)) ret = "(short)0";
+ } else {
+ ret = null;
+ }
+ if (null == ret) {
+ throw new AnnotationProcessingException(null, "%s is not supported to determine the inital value.",
+ tname.toString());
+ }
+ return ret;
+ }
+
+ private int getFactoryProxyIndex(TypeName gtname) throws AnnotationProcessingException {
+ int ret = -1;
+ boolean found = false;
+ if (gtname instanceof TypeVariableName) {
+ for (TypeParameterElement tpe : m_elem.getTypeParameters()) {
+ ++ret;
+ if (tpe.toString().equals(gtname.toString())) {
+ found = true;
+ break;
+ }
+ }
+ if (!found) {
+ throw new AnnotationProcessingException(null, "%s type is not found during factory proxy query.",
+ gtname.toString());
+ }
+ } else {
+ throw new AnnotationProcessingException(null, "%s type is not generic type for factory proxy query.",
+ gtname.toString());
+ }
+ return ret;
+ }
+
+ protected void genNFieldInfo() {
+ FieldInfo dynfieldinfo, fieldinfo;
+ List<long[]> finfo = new ArrayList<long[]>();
+ for (String name : m_gettersinfo.keySet()) {
+ dynfieldinfo = m_dynfieldsinfo.get(name);
+ if (dynfieldinfo.id > 0) {
+ finfo.add(new long[]{dynfieldinfo.id, dynfieldinfo.fieldoff, dynfieldinfo.fieldsize});
+ }
+ }
+
+ fieldinfo = new FieldInfo();
+ fieldinfo.name = String.format("m_nfieldinfo_%s",
+ Utils.genRandomString());
+ fieldinfo.type = ArrayTypeName.of(ArrayTypeName.of(TypeName.LONG));
+ String initlstr = Utils.toInitLiteral(finfo);
+ fieldinfo.specbuilder = FieldSpec.builder(fieldinfo.type,
+ fieldinfo.name, Modifier.PRIVATE, Modifier.STATIC).initializer("$1L", initlstr);
+ m_fieldsinfo.put("nfieldinfo", fieldinfo);
+ }
+
+ protected void buildGettersSpecs(TypeSpec.Builder typespecbuilder) throws AnnotationProcessingException {
+ MethodInfo methodinfo;
+ TypeName ftname;
+ String unsafename = m_fieldsinfo.get("unsafe").name;
+ String holdername = m_fieldsinfo.get("holder").name;
+ String allocname = m_fieldsinfo.get("allocator").name;
+ String autoreclaimname = m_fieldsinfo.get("autoreclaim").name;
+ String factoryproxyname = m_fieldsinfo.get("factoryproxy").name;
+ String genericfieldname = m_fieldsinfo.get("genericfield").name;
+ FieldInfo dynfieldinfo;
+ CodeBlock.Builder code;
+ String codefmt;
+ for (String name : m_gettersinfo.keySet()) {
+ code = CodeBlock.builder();
+ methodinfo = m_gettersinfo.get(name);
+ dynfieldinfo = m_dynfieldsinfo.get(name);
+ ftname = m_dynfieldsinfo.get(name).type;
+ if (isUnboxPrimitive(ftname)) {
+ if (unboxTypeName(ftname).equals(TypeName.BOOLEAN)) {
+ codefmt = "return 1 == $1N.$4L($2N.get() + $3L)";
+ } else {
+ codefmt = "return $1N.$4L($2N.get() + $3L)";
+ }
+ code.addStatement(codefmt, unsafename, holdername, dynfieldinfo.fieldoff, transTypeToUnsafeMethod(ftname, true));
+ } else {
+ if(methodinfo.rettype.toString().equals(String.class.getCanonicalName())) {
+ code.beginControlFlow("if (null == $1N)", dynfieldinfo.name);
+ code.addStatement("long phandler = $1N.getAddress($2N.get() + $3L)",
+ unsafename, holdername, dynfieldinfo.fieldoff);
+ code.beginControlFlow("if (0L != phandler)");
+ code.addStatement("$1N = $2N.retrieveBuffer(phandler, $3N)",
+ dynfieldinfo.name, allocname, autoreclaimname);
+ code.beginControlFlow("if (null == $1N)", dynfieldinfo.name);
+ code.addStatement("throw new RetrieveNonVolatileEntityError(\"Retrieve String Buffer Failure.\")");
+ code.endControlFlow();
+ code.endControlFlow();
+ code.endControlFlow();
+ code.addStatement("return null == $1N ? null : $1N.get().asCharBuffer().toString()",
+ dynfieldinfo.name);
+ } else if (dynfieldinfo.type.toString().startsWith(GenericField.class.getCanonicalName())) {
+ code.beginControlFlow("if (null == $1N)", dynfieldinfo.name);
+ code.addStatement("$1T proxy = null", TypeName.get(EntityFactoryProxy.class));
+ code.addStatement("$1T gftype = null", TypeName.get(GenericField.GType.class));
+ code.addStatement("int gfpidx = $1L", getFactoryProxyIndex(methodinfo.rettype));
+ code.beginControlFlow("if (null != $1N && $1N.length > gfpidx)", factoryproxyname);
+ code.addStatement("proxy = $1L[gfpidx]", factoryproxyname);
+ code.endControlFlow();
+ code.beginControlFlow("if (null != $1N && $1N.length > gfpidx)", genericfieldname);
+ code.addStatement("gftype = $1L[gfpidx]", genericfieldname);
+ code.nextControlFlow("else");
+ code.addStatement("throw new RetrieveNonVolatileEntityError(\"No Generic Field Type Info.\")");
+ code.endControlFlow();
+ code.addStatement("$1N = new $2T(proxy, gftype, $8L, $9L, $3N, $4N, $5N, $6N.get() + $7L)",
+ dynfieldinfo.name, dynfieldinfo.type,
+ allocname, unsafename, autoreclaimname, holdername, dynfieldinfo.fieldoff,
+ dynfieldinfo.efproxiesname, dynfieldinfo.gftypesname);
+ code.endControlFlow();
+ code.addStatement("return $1N.get()", dynfieldinfo.name);
+ } else {
+ code.beginControlFlow("if (null == $1N)", dynfieldinfo.name);
+ code.addStatement("long phandler = $1N.getAddress($2N.get() + $3L)",
+ unsafename, holdername, dynfieldinfo.fieldoff);
+ code.beginControlFlow("if (0L != phandler)");
+ code.addStatement("$1N = $4N.restore($2N, $5L, $6L, phandler, $3N)",
+ dynfieldinfo.name, allocname, autoreclaimname,
+ String.format("%s%s", m_typeutils.asElement(methodinfo.elem.getReturnType()).getSimpleName(), FACTORYNAMESUFFIX),
+ dynfieldinfo.efproxiesname, dynfieldinfo.gftypesname);
+ code.endControlFlow();
+ code.endControlFlow();
+ code.addStatement("return $1N", dynfieldinfo.name);
+ }
+ }
+ typespecbuilder.addMethod(methodinfo.specbuilder.addCode(code.build()).build());
+ }
+ }
+
+ protected String gsetterName(String name, boolean isget) {
+ return String.format("%s%s", isget ? "get" : "set", name); //Character.toUpperCase(name.charAt(0)) + name.substring(1));
+ }
+
+ protected void buildSettersSpecs(TypeSpec.Builder typespecbuilder) throws AnnotationProcessingException {
+ MethodInfo methodinfo;
+ TypeName ftname, valtname;
+ String unsafename = m_fieldsinfo.get("unsafe").name;
+ String holdername = m_fieldsinfo.get("holder").name;
+ String allocname = m_fieldsinfo.get("allocator").name;
+ String autoreclaimname = m_fieldsinfo.get("autoreclaim").name;
+ String factoryproxyname = m_fieldsinfo.get("factoryproxy").name;
+ String genericfieldname = m_fieldsinfo.get("genericfield").name;
+ FieldInfo dynfieldinfo;
+ CodeBlock.Builder code;
+ VariableElement arg0;
+ VariableElement arg1;
+ String codefmt;
+ for (String name : m_settersinfo.keySet()) {
+ code = CodeBlock.builder();
+ methodinfo = m_settersinfo.get(name);
+ dynfieldinfo = m_dynfieldsinfo.get(name);
+ ftname = m_dynfieldsinfo.get(name).type;
+ valtname = m_gettersinfo.get(name).rettype;
+ arg0 = methodinfo.elem.getParameters().get(0);
+ if (!TypeName.get(arg0.asType()).equals(valtname)) {
+ throw new AnnotationProcessingException(null, "%s has inconsistent value type with its getter/setter.",
+ name);
+ }
+ if (isUnboxPrimitive(ftname)) {
+ if (unboxTypeName(ftname).equals(TypeName.BOOLEAN)) {
+ codefmt = "$1N.$4L($2N.get() + $3L, $5L?1:0)";
+ } else {
+ codefmt = "$1N.$4L($2N.get() + $3L, $5L)";
+ }
+ code.addStatement(codefmt, unsafename, holdername, dynfieldinfo.fieldoff, transTypeToUnsafeMethod(ftname, false), arg0);
+ } else {
+ try {
+ arg1 = methodinfo.elem.getParameters().get(1);
+ if (!TypeName.BOOLEAN.equals(TypeName.get(arg1.asType()))) {
+ throw new AnnotationProcessingException(null, "the second parameter of %s's setter is not boolean type.",
+ name);
+ }
+ } catch (IndexOutOfBoundsException ex) {
+ throw new AnnotationProcessingException(null, "%s's setter has no second parameters for non primitive type.",
+ name);
+ }
+ if(valtname.toString().equals(String.class.getCanonicalName())) {
+ code.beginControlFlow("if ($1L && null != $2L())", arg1, gsetterName(name, true));
+ code.addStatement("$1N.destroy()", dynfieldinfo.name);
+ code.addStatement("$1N = null", dynfieldinfo.name);
+ code.addStatement("$1N.putAddress($2N.get() + $3L, 0L)",
+ unsafename, holdername, dynfieldinfo.fieldoff);
+ code.endControlFlow();
+ code.beginControlFlow("if (null == $1L)", arg0);
+ code.addStatement("$1N.putLong($2N.get() + $3L, 0L)",
+ unsafename, holdername, dynfieldinfo.fieldoff);
+ code.nextControlFlow("else");
+ code.addStatement("$1N = $2N.createBuffer($3L.length() * 2, $4N)",
+ dynfieldinfo.name, allocname, arg0, autoreclaimname);
+ code.beginControlFlow("if (null == $1N)", dynfieldinfo.name);
+ code.addStatement("throw new OutOfPersistentMemory(\"Create Non-Volatile String Error!\")");
+ code.endControlFlow();
+ code.addStatement("$1N.get().asCharBuffer().put($2L)", dynfieldinfo.name, arg0);
+ code.addStatement("$1N.putLong($2N.get() + $3L, $4N.getBufferHandler($5N))",
+ unsafename, holdername, dynfieldinfo.fieldoff, allocname, dynfieldinfo.name);
+ code.endControlFlow();
+ } else if (dynfieldinfo.type.toString().startsWith(GenericField.class.getCanonicalName())) {
+ code.beginControlFlow("if (null == $1N)", dynfieldinfo.name);
+ code.addStatement("$1T proxy = null", TypeName.get(EntityFactoryProxy.class));
+ code.addStatement("$1T gftype = null", TypeName.get(GenericField.GType.class));
+ code.addStatement("int gfpidx = $1L", getFactoryProxyIndex(valtname));
+ code.beginControlFlow("if (null != $1N && $1N.length > gfpidx)", factoryproxyname);
+ code.addStatement("proxy = $1L[gfpidx]", factoryproxyname);
+ code.endControlFlow();
+ code.beginControlFlow("if (null != $1N && $1N.length > gfpidx)", genericfieldname);
+ code.addStatement("gftype = $1L[gfpidx]", genericfieldname);
+ code.nextControlFlow("else");
+ code.addStatement("throw new RetrieveNonVolatileEntityError(\"No Generic Field Type Info.\")");
+ code.endControlFlow();
+ code.addStatement("$1N = new $2T(proxy, gftype, $8L, $9L, $3N, $4N, $5N, $6N.get() + $7L)",
+ dynfieldinfo.name, dynfieldinfo.type,
+ allocname, unsafename, autoreclaimname, holdername, dynfieldinfo.fieldoff,
+ dynfieldinfo.efproxiesname, dynfieldinfo.gftypesname);
+ code.endControlFlow();
+ code.beginControlFlow("if (null != $1L)", dynfieldinfo.name);
+ code.addStatement("$1N.set($2L, $3L)", dynfieldinfo.name, arg0, arg1);
+ code.nextControlFlow("else");
+ code.addStatement("throw new RetrieveNonVolatileEntityError(\"GenericField is null!\")");
+ code.endControlFlow();
+ } else {
+ code.beginControlFlow("if ($1L && null != $2L())", arg1, gsetterName(name, true));
+ code.addStatement("$1N.destroy()", dynfieldinfo.name);
+ code.addStatement("$1N = null", dynfieldinfo.name);
+ code.addStatement("$1N.putAddress($2N.get() + $3L, 0L)",
+ unsafename, holdername, dynfieldinfo.fieldoff);
+ code.endControlFlow();
+ code.addStatement("$1N = $2L", dynfieldinfo.name, arg0);
+ code.addStatement("$1N.putLong($2N.get() + $3L, null == $4N ? 0L : $4N.getNonVolatileHandler())",
+ unsafename, holdername, dynfieldinfo.fieldoff, dynfieldinfo.name);
+ }
+ }
+ typespecbuilder.addMethod(methodinfo.specbuilder.addCode(code.build()).build());
+ }
+ }
+
+ protected void buildDurableMethodSpecs(TypeSpec.Builder typespecbuilder) throws AnnotationProcessingException {
+ MethodInfo methodinfo;
+ CodeBlock.Builder code;
+ FieldInfo dynfieldinfo;
+ String holdername = m_fieldsinfo.get("holder").name;
+ String allocname = m_fieldsinfo.get("allocator").name;
+ String autoreclaimname = m_fieldsinfo.get("autoreclaim").name;
+ for (String name : m_durablemtdinfo.keySet()) {
+ methodinfo = m_durablemtdinfo.get(name);
+ code = CodeBlock.builder();
+ switch(name) {
+ case "cancelAutoReclaim" :
+ code.addStatement("$1N.cancelAutoReclaim()", holdername);
+ for (String fname : m_dynfieldsinfo.keySet()) {
+ dynfieldinfo = m_dynfieldsinfo.get(fname);
+ if (!isUnboxPrimitive(dynfieldinfo.type)) {
+ code.beginControlFlow("if (null != $1N)", dynfieldinfo.name);
+ code.addStatement("$1N.cancelAutoReclaim()", dynfieldinfo.name);
+ code.endControlFlow();
+ }
+ }
+ code.addStatement("$1N = false", autoreclaimname);
+ break;
+ case "registerAutoReclaim" :
+ code.addStatement("$1N.registerAutoReclaim()", holdername);
+ for (String fname : m_dynfieldsinfo.keySet()) {
+ dynfieldinfo = m_dynfieldsinfo.get(fname);
+ if (!isUnboxPrimitive(dynfieldinfo.type)) {
+ code.beginControlFlow("if (null != $1N)", dynfieldinfo.name);
+ code.addStatement("$1N.registerAutoReclaim()", dynfieldinfo.name);
+ code.endControlFlow();
+ }
+ }
+ code.addStatement("$1N = true", autoreclaimname);
+ break;
+ case "getNonVolatileHandler" :
+ code.addStatement("return $1N.getChunkHandler($2N)", allocname, holdername);
+ break;
+ case "autoReclaim" :
+ code.addStatement("return $1N", autoreclaimname);
+ break;
+ case "destroy" :
+ code.addStatement("$1N.destroy()", holdername);
+ for (String fname : m_dynfieldsinfo.keySet()) {
+ dynfieldinfo = m_dynfieldsinfo.get(fname);
+ if (!isUnboxPrimitive(dynfieldinfo.type)) {
+ code.beginControlFlow("if (null != $1N)", dynfieldinfo.name);
+ code.addStatement("$1N.destroy()", dynfieldinfo.name);
+ code.addStatement("$1N = null", dynfieldinfo.name);
+ code.endControlFlow();
+ }
+ }
+ break;
+ case "getNativeFieldInfo" :
+ code.addStatement("return $1N", m_fieldsinfo.get("nfieldinfo").name);
+ break;
+ default:
+ throw new AnnotationProcessingException(null, "Method %s is not supported.",
+ name);
+ }
+ typespecbuilder.addMethod(methodinfo.specbuilder.addCode(code.build()).build());
+ }
+ }
+
+ protected void buildEntityMethodSpecs(TypeSpec.Builder typespecbuilder) throws AnnotationProcessingException {
+ MethodInfo methodinfo;
+ CodeBlock.Builder code;
+ FieldInfo dynfieldinfo;
+ VariableElement arg0, arg1, arg2, arg3, arg4;
+ String unsafename = m_fieldsinfo.get("unsafe").name;
+ String holdername = m_fieldsinfo.get("holder").name;
+ String allocname = m_fieldsinfo.get("allocator").name;
+ String autoreclaimname = m_fieldsinfo.get("autoreclaim").name;
+ String factoryproxyname = m_fieldsinfo.get("factoryproxy").name;
+ String genericfieldname = m_fieldsinfo.get("genericfield").name;
+ for (String name : m_entitymtdinfo.keySet()) {
+ methodinfo = m_entitymtdinfo.get(name);
+ code = CodeBlock.builder();
+ arg0 = methodinfo.elem.getParameters().get(0);
+ arg1 = methodinfo.elem.getParameters().get(1);
+ arg2 = methodinfo.elem.getParameters().get(2);
+ switch(name) {
+ case "initializeNonVolatileEntity" :
+ arg3 = methodinfo.elem.getParameters().get(3);
+ code.addStatement("$1N = $2L", allocname, arg0);
+ code.addStatement("$1N = $2L", factoryproxyname, arg1);
+ code.addStatement("$1N = $2L", genericfieldname, arg2);
+ code.addStatement("$1N = $2L", autoreclaimname, arg3);
+ code.beginControlFlow("try");
+ code.addStatement("$1N = $2T.getUnsafe()", unsafename, Utils.class);
+ code.nextControlFlow("catch (Exception e)");
+ code.addStatement("e.printStackTrace()");
+ code.endControlFlow();
+ break;
+ case "createNonVolatileEntity" :
+ arg3 = methodinfo.elem.getParameters().get(3);
+ code.addStatement("initializeNonVolatileEntity($1L, $2L, $3L, $4L)", arg0, arg1, arg2, arg3);
+ code.addStatement("$1N = $2N.createChunk($3L, $4N)",
+ holdername, allocname, m_holdersize, autoreclaimname);
+ code.beginControlFlow("if (null == $1N)", holdername);
+ code.addStatement("throw new OutOfPersistentMemory(\"Create Non-Volatile Entity Error!\")");
+ code.endControlFlow();
+ // code.beginControlFlow("try");
+ // for (String fname : m_dynfieldsinfo.keySet()) {
+ // dynfieldinfo = m_dynfieldsinfo.get(fname);
+ // if (isUnboxPrimitive(dynfieldinfo.type)) {
+ // code.addStatement("$1N($2L)", gsetterName(fname, false), getIntialValueLiteral(dynfieldinfo.type));
+ // } else {
+ // code.addStatement("$1N(null, false)", gsetterName(fname, false));
+ // }
+ // }
+ // code.nextControlFlow("catch(RetrieveNonVolatileEntityError ex)");
+ // code.endControlFlow();
+ code.addStatement("initializeAfterCreate()");
+ break;
+ case "restoreNonVolatileEntity" :
+ arg3 = methodinfo.elem.getParameters().get(3);
+ arg4 = methodinfo.elem.getParameters().get(4);
+ code.addStatement("initializeNonVolatileEntity($1L, $2L, $3L, $4L)", arg0, arg1, arg2, arg4);
+ code.beginControlFlow("if (0L == $1L)", arg3);
+ code.addStatement("throw new RetrieveNonVolatileEntityError(\"Input handler is null on $1N.\")", name);
+ code.endControlFlow();
+ code.addStatement("$1N = $2N.retrieveChunk($3L, $4N)",
+ holdername, allocname, arg3, autoreclaimname);
+ code.beginControlFlow("if (null == $1N)", holdername);
+ code.addStatement("throw new RetrieveNonVolatileEntityError(\"Retrieve Entity Failure!\")");
+ code.endControlFlow();
+ code.addStatement("initializeAfterRestore()");
+ break;
+ default:
+ throw new AnnotationProcessingException(null, "Method %s is not supported.",
+ name);
+ }
+ typespecbuilder.addMethod(methodinfo.specbuilder.addCode(code.build()).build());
+ }
+ }
+
+ protected void buildFieldSpecs(TypeSpec.Builder typespecbuilder, Map<String, FieldInfo> fieldinfos) {
+ FieldInfo fieldinfo;
+ for (String name : fieldinfos.keySet()) {
+ fieldinfo = fieldinfos.get(name);
+ if (null != fieldinfo.specbuilder) {
+ typespecbuilder.addField(fieldinfo.specbuilder.build());
+ }
+ }
+ }
+
+ protected void buildFactoryMethodSpecs(TypeSpec entityspec, TypeSpec.Builder typespecbuilder) throws AnnotationProcessingException {
+ MethodSpec methodspec;
+ CodeBlock code;
+
+ TypeName entitytn= ParameterizedTypeName.get(ClassName.get(m_packagename, m_entityname), entityspec.typeVariables.toArray(new TypeVariableName[0]));
+
+ ParameterSpec allocparam = ParameterSpec.builder(m_alloctypename, "allocator").build();
+ code = CodeBlock.builder()
+ .addStatement("return create($1L, false)", allocparam.name)
+ .build();
+ methodspec = MethodSpec.methodBuilder("create")
+ .addTypeVariables(entityspec.typeVariables)
+ .addException(OutOfPersistentMemory.class)
+ .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
+ .returns(TypeName.get(m_elem.asType()))
+ .addParameter(allocparam)
+ .addCode(code).build();
+ typespecbuilder.addMethod(methodspec);
+
+ ParameterSpec autoreclaimparam = ParameterSpec.builder(TypeName.BOOLEAN, "autoreclaim").build();
+ code = CodeBlock.builder()
+ .addStatement("return create($1L, null, null, $2L)", allocparam.name, autoreclaimparam.name)
+ .build();
+ methodspec = MethodSpec.methodBuilder("create")
+ .addTypeVariables(entityspec.typeVariables)
+ .addException(OutOfPersistentMemory.class)
+ .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
+ .returns(TypeName.get(m_elem.asType()))
+ .addParameter(allocparam)
+ .addParameter(autoreclaimparam)
+ .addCode(code).build();
+ typespecbuilder.addMethod(methodspec);
+
+ ParameterSpec factoryproxysparam = ParameterSpec.builder(m_factoryproxystypename, "factoryproxys").build();
+ ParameterSpec gfieldsparam = ParameterSpec.builder(m_gfieldstypename, "gfields").build();
+ code = CodeBlock.builder()
+ .addStatement("$1T entity = new $1T()", entitytn)
+ .addStatement("entity.setupGenericInfo($1N, $2N)", factoryproxysparam.name, gfieldsparam.name)
+ .addStatement("entity.createNonVolatileEntity($1L, $2L, $3L, $4L)",
+ allocparam.name, factoryproxysparam.name, gfieldsparam.name, autoreclaimparam.name)
+ .addStatement("return entity")
+ .build();
+ methodspec = MethodSpec.methodBuilder("create")
+ .addTypeVariables(entityspec.typeVariables)
+ .addException(OutOfPersistentMemory.class)
+ .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
+ .returns(TypeName.get(m_elem.asType()))
+ .addParameter(allocparam)
+ .addParameter(factoryproxysparam)
+ .addParameter(gfieldsparam)
+ .addParameter(autoreclaimparam)
+ .addCode(code).build();
+ typespecbuilder.addMethod(methodspec);
+
+ ParameterSpec phandlerparam = ParameterSpec.builder(TypeName.LONG, "phandler").build();
+ code = CodeBlock.builder()
+ .addStatement("return restore($1L, $2L, false)", allocparam.name, phandlerparam.name)
+ .build();
+ methodspec = MethodSpec.methodBuilder("restore")
+ .addTypeVariables(entityspec.typeVariables)
+ .addException(RetrieveNonVolatileEntityError.class)
+ .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
+ .returns(TypeName.get(m_elem.asType()))
+ .addParameter(allocparam)
+ .addParameter(phandlerparam)
+ .addCode(code).build();
+ typespecbuilder.addMethod(methodspec);
+
+ code = CodeBlock.builder()
+ .addStatement("return restore($1L, null, null, $2L, $3L)", allocparam.name, phandlerparam.name, autoreclaimparam.name)
+ .build();
+ methodspec = MethodSpec.methodBuilder("restore")
+ .addTypeVariables(entityspec.typeVariables)
+ .addException(RetrieveNonVolatileEntityError.class)
+ .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
+ .returns(TypeName.get(m_elem.asType()))
+ .addParameter(allocparam)
+ .addParameter(phandlerparam)
+ .addParameter(autoreclaimparam)
+ .addCode(code).build();
+ typespecbuilder.addMethod(methodspec);
+
+ code = CodeBlock.builder()
+ .addStatement("$1T entity = new $1T()", entitytn)
+ .addStatement("entity.setupGenericInfo($1N, $2N)", factoryproxysparam.name, gfieldsparam.name)
+ .addStatement("entity.restoreNonVolatileEntity($1L, $2L, $3L, $4L, $5L)",
+ allocparam.name, factoryproxysparam.name, gfieldsparam.name, phandlerparam.name, autoreclaimparam.name)
+ .addStatement("return entity")
+ .build();
+ methodspec = MethodSpec.methodBuilder("restore")
+ .addTypeVariables(entityspec.typeVariables)
+ .addException(RetrieveNonVolatileEntityError.class)
+ .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
+ .returns(TypeName.get(m_elem.asType()))
+ .addParameter(allocparam)
+ .addParameter(factoryproxysparam)
+ .addParameter(gfieldsparam)
+ .addParameter(phandlerparam)
+ .addParameter(autoreclaimparam)
+ .addCode(code).build();
+ typespecbuilder.addMethod(methodspec);
+ }
+
+ public void generateCode(Filer filer) throws IOException, AnnotationProcessingException {
+ AnnotationSpec classannotation = AnnotationSpec.builder(SuppressWarnings.class)
+ .addMember("value", "$S", "restriction").build();
+
+ TypeSpec.Builder entitybuilder = TypeSpec.classBuilder(m_entityname)
+ .superclass(TypeName.get(m_elem.asType()))
+ .addModifiers(Modifier.PUBLIC).addAnnotation(classannotation)
+ .addSuperinterface(
+ ParameterizedTypeName.get(ClassName.get(MemoryNonVolatileEntity.class), m_alloctypevarname))
+ .addTypeVariable(m_alloctypevarname);
+
+ for (TypeParameterElement tpe : m_elem.getTypeParameters()) {
+ entitybuilder.addTypeVariable(TypeVariableName.get(tpe));
+ }
+
+ buildFieldSpecs(entitybuilder, m_dynfieldsinfo);
+ buildFieldSpecs(entitybuilder, m_fieldsinfo);
+
+ buildGettersSpecs(entitybuilder);
+ buildSettersSpecs(entitybuilder);
+
+ buildDurableMethodSpecs(entitybuilder);
+ buildEntityMethodSpecs(entitybuilder);
+
+ TypeSpec entityspec = entitybuilder.build();
+
+ JavaFile entityFile = JavaFile
+ .builder(m_packagename, entityspec).build();
+
+ entityFile.writeTo(filer);
+
+ TypeSpec.Builder factorybuilder = TypeSpec.classBuilder(m_factoryname)
+ .addModifiers(Modifier.PUBLIC);
+
+ buildFactoryMethodSpecs(entityspec, factorybuilder);
+
+ JavaFile factoryFile = JavaFile
+ .builder(m_packagename, factorybuilder.build()).build();
+
+ factoryFile.writeTo(filer);
+
+ }
+
+ public static Builder overriding(ExecutableElement method, String varname) {
+
+ Set<Modifier> modifiers = method.getModifiers();
+ if (modifiers.contains(Modifier.PRIVATE) || modifiers.contains(Modifier.FINAL)
+ || modifiers.contains(Modifier.STATIC)) {
+ throw new IllegalArgumentException("cannot override method with modifiers: " + modifiers);
+ }
+
+ String methodName = method.getSimpleName().toString();
+ MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(methodName);
+
+ methodBuilder.addAnnotation(Override.class);
+ for (AnnotationMirror mirror : method.getAnnotationMirrors()) {
+ AnnotationSpec annotationSpec = AnnotationSpec.get(mirror);
+ if (annotationSpec.type.equals(Override.class))
+ continue;
+ methodBuilder.addAnnotation(annotationSpec);
+ }
+
+ modifiers = new LinkedHashSet<>(modifiers);
+ modifiers.remove(Modifier.ABSTRACT);
+ methodBuilder.addModifiers(modifiers);
+
+ for (TypeParameterElement typeParameterElement : method.getTypeParameters()) {
+ TypeVariable var = (TypeVariable) typeParameterElement.asType();
+ methodBuilder.addTypeVariable(TypeVariableName.get(var));
+ }
+
+ methodBuilder.returns(TypeName.get(method.getReturnType()));
+
+ List<? extends VariableElement> parameters = method.getParameters();
+ TypeName type;
+ for (VariableElement parameter : parameters) {
+ if (parameter.asType().getKind() == TypeKind.TYPEVAR && parameter.asType().toString().equals(varname)) {
+ type = TypeVariableName.get(varname);
+ } else {
+ type = TypeName.get(parameter.asType());
+ }
+
+ String name = parameter.getSimpleName().toString();
+ Set<Modifier> parameterModifiers = parameter.getModifiers();
+ ParameterSpec.Builder parameterBuilder = ParameterSpec.builder(type, name)
+ .addModifiers(parameterModifiers.toArray(new Modifier[parameterModifiers.size()]));
+ for (AnnotationMirror mirror : parameter.getAnnotationMirrors()) {
+ parameterBuilder.addAnnotation(AnnotationSpec.get(mirror));
+ }
+ methodBuilder.addParameter(parameterBuilder.build());
+ }
+ methodBuilder.varargs(method.isVarArgs());
+
+ for (TypeMirror thrownType : method.getThrownTypes()) {
+ methodBuilder.addException(TypeName.get(thrownType));
+ }
+
+ return methodBuilder;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/8e83efe5/core/src/main/java/org/apache/mnemonic/AnnotationProcessingException.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/AnnotationProcessingException.java b/core/src/main/java/org/apache/mnemonic/AnnotationProcessingException.java
new file mode 100644
index 0000000..83b42fe
--- /dev/null
+++ b/core/src/main/java/org/apache/mnemonic/AnnotationProcessingException.java
@@ -0,0 +1,23 @@
+package org.apache.mnemonic;
+
+import javax.lang.model.element.Element;
+
+/**
+ * this exception is used for non-volatile annotation processing
+ *
+ */
+public class AnnotationProcessingException extends Exception {
+
+ private static final long serialVersionUID = 6911141027622831646L;
+
+ private Element element;
+
+ public AnnotationProcessingException(Element element, String msg, Object... args) {
+ super(String.format(msg, args));
+ this.element = element;
+ }
+
+ public Element getElement() {
+ return element;
+ }
+}