mnemonic-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ga...@apache.org
Subject incubator-mnemonic git commit: MNEMONIC-70: Support two new durable types i.e. Buffer and Chunk for durable object model
Date Mon, 27 Jun 2016 23:44:59 GMT
Repository: incubator-mnemonic
Updated Branches:
  refs/heads/master 27ae59962 -> 3251359dc


MNEMONIC-70: Support two new durable types i.e. Buffer and Chunk for durable object model


Project: http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/commit/3251359d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/tree/3251359d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/diff/3251359d

Branch: refs/heads/master
Commit: 3251359dc04cee87f92b30a55b1b0f7d80f237fc
Parents: 27ae599
Author: Wang, Gang(Gary) <gang1.wang@intel.com>
Authored: Mon Jun 27 16:34:04 2016 -0700
Committer: Wang, Gang(Gary) <gang1.wang@intel.com>
Committed: Mon Jun 27 16:34:04 2016 -0700

----------------------------------------------------------------------
 .../mnemonic/AnnotatedDurableEntityClass.java   | 54 ++++++++++++--
 .../java/org/apache/mnemonic/DurableType.java   |  4 +-
 .../java/org/apache/mnemonic/GenericField.java  | 75 ++++++++++++++++++++
 .../org/apache/mnemonic/GenericTypeError.java   | 30 ++++++++
 .../apache/mnemonic/IllegalAllocatorError.java  | 30 ++++++++
 5 files changed, 187 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/3251359d/mnemonic-core/src/main/java/org/apache/mnemonic/AnnotatedDurableEntityClass.java
----------------------------------------------------------------------
diff --git a/mnemonic-core/src/main/java/org/apache/mnemonic/AnnotatedDurableEntityClass.java
b/mnemonic-core/src/main/java/org/apache/mnemonic/AnnotatedDurableEntityClass.java
index 66a240f..1876551 100644
--- a/mnemonic-core/src/main/java/org/apache/mnemonic/AnnotatedDurableEntityClass.java
+++ b/mnemonic-core/src/main/java/org/apache/mnemonic/AnnotatedDurableEntityClass.java
@@ -58,7 +58,7 @@ import com.squareup.javapoet.TypeVariableName;
 import sun.misc.Unsafe;
 
 /**
- * this class managed to generate generic non-volatile concrete object and their
+ * this class managed to generate generic durable concrete object and their
  * corresponding factory.
  *
  */
@@ -483,7 +483,27 @@ public class AnnotatedDurableEntityClass {
         code.addStatement(codefmt, unsafename, holdername, dynfieldinfo.fieldoff,
             transTypeToUnsafeMethod(ftname, true));
       } else {
-        if (methodinfo.rettype.toString().equals(String.class.getCanonicalName())) {
+        if (methodinfo.rettype.toString().equals(MemChunkHolder.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.retrieveChunk(phandler, $3N)",
+              dynfieldinfo.name, allocname, autoreclaimname);
+          code.endControlFlow();
+          code.endControlFlow();
+          code.addStatement("return $1N", dynfieldinfo.name);
+        }  else if (methodinfo.rettype.toString().equals(MemBufferHolder.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.endControlFlow();
+          code.endControlFlow();
+          code.addStatement("return $1N", dynfieldinfo.name);
+        } 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);
@@ -580,7 +600,31 @@ public class AnnotatedDurableEntityClass {
           throw new AnnotationProcessingException(null, "%s's setter has no second parameters
for non primitive type.",
               name);
         }
-        if (valtname.toString().equals(String.class.getCanonicalName())) {
+        if (valtname.toString().equals(MemChunkHolder.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 ($1N.getAllocator() != $2N)", dynfieldinfo.name, allocname);
+          code.addStatement("throw new IllegalAllocatorError(\"This chunk is allocated by
another allocator!\")");
+          code.endControlFlow();
+          code.addStatement("$1N = $2L", dynfieldinfo.name, arg0);
+          code.addStatement("$1N.putLong($2N.get() + $3L, null == $4N ? 0L : $5N.getChunkHandler($4N))",
+              unsafename, holdername, dynfieldinfo.fieldoff, dynfieldinfo.name, allocname);
+        } else if (valtname.toString().equals(MemBufferHolder.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 ($1N.getAllocator() != $2N)", dynfieldinfo.name, allocname);
+          code.addStatement("throw new IllegalAllocatorError(\"This buffer is allocated by
another allocator!\")");
+          code.endControlFlow();
+          code.addStatement("$1N = $2L", dynfieldinfo.name, arg0);
+          code.addStatement("$1N.putLong($2N.get() + $3L, null == $4N ? 0L : $5N.getBufferHandler($4N))",
+              unsafename, holdername, dynfieldinfo.fieldoff, dynfieldinfo.name, allocname);
+        } else 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);
@@ -592,7 +636,7 @@ public class AnnotatedDurableEntityClass {
           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 OutOfHybridMemory(\"Create Non-Volatile String Error!\")");
+          code.addStatement("throw new OutOfHybridMemory(\"Create Durable 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,
@@ -732,7 +776,7 @@ public class AnnotatedDurableEntityClass {
         code.addStatement("initializeDurableEntity($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 OutOfHybridMemory(\"Create Non-Volatile Entity Error!\")");
+        code.addStatement("throw new OutOfHybridMemory(\"Create Durable Entity Error!\")");
         code.endControlFlow();
         // code.beginControlFlow("try");
         // for (String fname : m_dynfieldsinfo.keySet()) {

http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/3251359d/mnemonic-core/src/main/java/org/apache/mnemonic/DurableType.java
----------------------------------------------------------------------
diff --git a/mnemonic-core/src/main/java/org/apache/mnemonic/DurableType.java b/mnemonic-core/src/main/java/org/apache/mnemonic/DurableType.java
index 4ec22f0..94e2cdf 100644
--- a/mnemonic-core/src/main/java/org/apache/mnemonic/DurableType.java
+++ b/mnemonic-core/src/main/java/org/apache/mnemonic/DurableType.java
@@ -32,7 +32,9 @@ public enum DurableType {
   FLOAT(7),
   DOUBLE(8),
   STRING(9),
-  DURABLE(10);
+  DURABLE(10),
+  BUFFER(11),
+  CHUNK(12);
 
   private int value;
 

http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/3251359d/mnemonic-core/src/main/java/org/apache/mnemonic/GenericField.java
----------------------------------------------------------------------
diff --git a/mnemonic-core/src/main/java/org/apache/mnemonic/GenericField.java b/mnemonic-core/src/main/java/org/apache/mnemonic/GenericField.java
index 01f8b58..d46923d 100644
--- a/mnemonic-core/src/main/java/org/apache/mnemonic/GenericField.java
+++ b/mnemonic-core/src/main/java/org/apache/mnemonic/GenericField.java
@@ -32,6 +32,8 @@ public class GenericField<A extends RestorableAllocator<A>, E>
implements Durabl
   private DurableType m_dgftype = null;
   private Durable m_field = null;
   private MemBufferHolder<A> m_strfield = null;
+  private MemChunkHolder<A> m_chunkfield = null;
+  private MemBufferHolder<A> m_bufferfield = null;
   private A m_allocator;
   private boolean m_autoreclaim;
   private EntityFactoryProxy m_defproxy = null;
@@ -88,6 +90,7 @@ public class GenericField<A extends RestorableAllocator<A>, E>
implements Durabl
    * @param destroy
    *          specify true if want to destroy the original value
    */
+  @SuppressWarnings("unchecked")
   public void set(E e, boolean destroy) {
     boolean isnull = null == e;
     switch (m_dgftype) {
@@ -142,6 +145,36 @@ public class GenericField<A extends RestorableAllocator<A>,
E> implements Durabl
       m_field = (Durable) e;
       m_unsafe.putAddress(m_fpos, null == m_field ? 0L : m_field.getHandler());
       break;
+    case CHUNK:
+      if (destroy && null != get()) {
+        m_chunkfield.destroy();
+        m_chunkfield = null;
+        m_unsafe.putAddress(m_fpos, 0L);
+      }
+      if (e instanceof MemChunkHolder<?> == false) {
+        throw new GenericTypeError("generic type is not mapped to a chunk type!");
+      }
+      if (((MemChunkHolder<A>) e).getAllocator() != m_allocator) {
+        throw new IllegalAllocatorError("This generic chunk is allocated by another allocator!");
+      }
+      m_chunkfield = (MemChunkHolder<A>) e;
+      m_unsafe.putAddress(m_fpos, null == m_chunkfield ? 0L : m_allocator.getChunkHandler(m_chunkfield));
+      break;
+    case BUFFER :
+      if (destroy && null != get()) {
+        m_bufferfield.destroy();
+        m_bufferfield = null;
+        m_unsafe.putAddress(m_fpos, 0L);
+      }
+      if (e instanceof MemBufferHolder<?> == false) {
+        throw new GenericTypeError("generic type is not mapped to a buffer type!");
+      }
+      if (((MemBufferHolder<A>) e).getAllocator() != m_allocator) {
+        throw new IllegalAllocatorError("This generic buffer is allocated by another allocator!");
+      }
+      m_bufferfield = (MemBufferHolder<A>) e;
+      m_unsafe.putAddress(m_fpos, null == m_bufferfield ? 0L : m_allocator.getBufferHandler(m_bufferfield));
+      break;
     }
 
   }
@@ -203,6 +236,30 @@ public class GenericField<A extends RestorableAllocator<A>,
E> implements Durabl
       }
       ret = (E) m_field;
       break;
+    case CHUNK:
+      if (null == m_chunkfield) {
+        long phandler = m_unsafe.getAddress(m_fpos);
+        if (0L != phandler) {
+          m_chunkfield = m_allocator.retrieveChunk(phandler, m_autoreclaim);
+          if (null == m_chunkfield) {
+            throw new RetrieveDurableEntityError("Retrieve Chunk Failure.");
+          }
+        }
+      }
+      ret = (E) m_chunkfield;
+      break;
+    case BUFFER:
+      if (null == m_bufferfield) {
+        long phandler = m_unsafe.getAddress(m_fpos);
+        if (0L != phandler) {
+          m_bufferfield = m_allocator.retrieveBuffer(phandler, m_autoreclaim);
+          if (null == m_bufferfield) {
+            throw new RetrieveDurableEntityError("Retrieve Buffer Failure.");
+          }
+        }
+      }
+      ret = (E) m_bufferfield;
+      break;
     }
     return ret;
   }
@@ -234,6 +291,12 @@ public class GenericField<A extends RestorableAllocator<A>,
E> implements Durabl
     if (null != m_strfield) {
       m_strfield.cancelAutoReclaim();
     }
+    if (null != m_chunkfield) {
+      m_chunkfield.cancelAutoReclaim();
+    }
+    if (null != m_bufferfield) {
+      m_bufferfield.cancelAutoReclaim();
+    }
     m_autoreclaim = false;
   }
 
@@ -248,6 +311,12 @@ public class GenericField<A extends RestorableAllocator<A>,
E> implements Durabl
     if (null != m_strfield) {
       m_strfield.registerAutoReclaim();
     }
+    if (null != m_chunkfield) {
+      m_chunkfield.registerAutoReclaim();
+    }
+    if (null != m_bufferfield) {
+      m_bufferfield.registerAutoReclaim();
+    }
     m_autoreclaim = true;
   }
 
@@ -278,6 +347,12 @@ public class GenericField<A extends RestorableAllocator<A>,
E> implements Durabl
     if (null != m_strfield) {
       m_strfield.destroy();
     }
+    if (null != m_chunkfield) {
+      m_chunkfield.destroy();
+    }
+    if (null != m_bufferfield) {
+      m_bufferfield.destroy();
+    }
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/3251359d/mnemonic-core/src/main/java/org/apache/mnemonic/GenericTypeError.java
----------------------------------------------------------------------
diff --git a/mnemonic-core/src/main/java/org/apache/mnemonic/GenericTypeError.java b/mnemonic-core/src/main/java/org/apache/mnemonic/GenericTypeError.java
new file mode 100644
index 0000000..2f1821a
--- /dev/null
+++ b/mnemonic-core/src/main/java/org/apache/mnemonic/GenericTypeError.java
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.mnemonic;
+
+public class GenericTypeError extends RuntimeException {
+
+  /**
+   *
+   */
+  private static final long serialVersionUID = 1L;
+
+  public GenericTypeError(String s) {
+    super(s);
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/3251359d/mnemonic-core/src/main/java/org/apache/mnemonic/IllegalAllocatorError.java
----------------------------------------------------------------------
diff --git a/mnemonic-core/src/main/java/org/apache/mnemonic/IllegalAllocatorError.java b/mnemonic-core/src/main/java/org/apache/mnemonic/IllegalAllocatorError.java
new file mode 100644
index 0000000..e5aab55
--- /dev/null
+++ b/mnemonic-core/src/main/java/org/apache/mnemonic/IllegalAllocatorError.java
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.mnemonic;
+
+public class IllegalAllocatorError extends RuntimeException {
+
+  /**
+   *
+   */
+  private static final long serialVersionUID = 1L;
+
+  public IllegalAllocatorError(String s) {
+    super(s);
+  }
+}


Mime
View raw message