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 2017/07/13 06:43:46 UTC

incubator-mnemonic git commit: MNEMONIC-95: Durable Set Add/Remove/Contains implementation

Repository: incubator-mnemonic
Updated Branches:
  refs/heads/master 2c82bdb53 -> 854cb5c3b


MNEMONIC-95: Durable Set Add/Remove/Contains implementation


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

Branch: refs/heads/master
Commit: 854cb5c3b520139ceb6e56f799a0b0bb5da1d27c
Parents: 2c82bdb
Author: Johnu George <jo...@apache.org>
Authored: Wed Jul 12 17:27:08 2017 -0700
Committer: Johnu George <jo...@apache.org>
Committed: Wed Jul 12 17:44:54 2017 -0700

----------------------------------------------------------------------
 bin/test.conf                                   |   3 +
 .../mnemonic/collections/DurableHashSet.java    |  11 +-
 .../collections/DurableHashSetImpl.java         |  61 +++++--
 .../collections/DurableHashSetNGTest.java       | 180 +++++++++++++++++--
 4 files changed, 225 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/854cb5c3/bin/test.conf
----------------------------------------------------------------------
diff --git a/bin/test.conf b/bin/test.conf
index bc5aa75..20a5a91 100644
--- a/bin/test.conf
+++ b/bin/test.conf
@@ -39,6 +39,9 @@ mvn -Dtest=DurablePersonNGTest  test -pl mnemonic-collections -DskipTests=false
 mvn -Dtest=DurableHashMapNGTest  test -pl mnemonic-collections -DskipTests=false
 
 # a testcase for module "mnemonic-collection" that requires 'pmalloc' memory service to pass
+mvn -Dtest=DurableHashSetNGTest  test -pl mnemonic-collections -DskipTests=false
+
+# a testcase for module "mnemonic-collection" that requires 'pmalloc' memory service to pass
 mvn -Dtest=DurableArrayNGTest  test -pl mnemonic-collections -DskipTests=false
 
 # a testcase for module "mnemonic-collection" that requires 'pmalloc' memory service to pass

http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/854cb5c3/mnemonic-collections/src/main/java/org/apache/mnemonic/collections/DurableHashSet.java
----------------------------------------------------------------------
diff --git a/mnemonic-collections/src/main/java/org/apache/mnemonic/collections/DurableHashSet.java b/mnemonic-collections/src/main/java/org/apache/mnemonic/collections/DurableHashSet.java
index a30bf57..8b470d2 100644
--- a/mnemonic-collections/src/main/java/org/apache/mnemonic/collections/DurableHashSet.java
+++ b/mnemonic-collections/src/main/java/org/apache/mnemonic/collections/DurableHashSet.java
@@ -69,6 +69,9 @@ public abstract class DurableHashSet<E> implements Durable, Iterable<E> {
   /**
    * checks if set contains the specified element
    *
+   * @param item
+   *          the item to be searched
+   *
    * @return true if set contains the element
    */
   public abstract boolean contains(E item);
@@ -76,6 +79,9 @@ public abstract class DurableHashSet<E> implements Durable, Iterable<E> {
   /**
    * adds a specific element to the set
    *
+   * @param item
+   *          the item to be added
+   *
    * @return true if set did not already contain the element
    */
   public abstract boolean add(E item);
@@ -83,9 +89,12 @@ public abstract class DurableHashSet<E> implements Durable, Iterable<E> {
   /**
    * removes a specific element from the set
    *
+   * @param item
+   *          the item to be removed
+   *
    * @return true if set contained the element
    */
-  public abstract boolean remove(E value);
+  public abstract boolean remove(E item);
 
   /**
    * Get the number of elements in the set

http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/854cb5c3/mnemonic-collections/src/main/java/org/apache/mnemonic/collections/DurableHashSetImpl.java
----------------------------------------------------------------------
diff --git a/mnemonic-collections/src/main/java/org/apache/mnemonic/collections/DurableHashSetImpl.java b/mnemonic-collections/src/main/java/org/apache/mnemonic/collections/DurableHashSetImpl.java
index 032480b..fa0bec5 100644
--- a/mnemonic-collections/src/main/java/org/apache/mnemonic/collections/DurableHashSetImpl.java
+++ b/mnemonic-collections/src/main/java/org/apache/mnemonic/collections/DurableHashSetImpl.java
@@ -26,6 +26,7 @@ import org.apache.mnemonic.RestoreDurableEntityError;
 import org.apache.mnemonic.RetrieveDurableEntityError;
 import org.apache.mnemonic.Utils;
 
+import org.apache.commons.lang3.ArrayUtils;
 import sun.misc.Unsafe;
 import java.util.Iterator;
 
@@ -38,9 +39,10 @@ public class DurableHashSetImpl<A extends RestorableAllocator<A>, E>
   private EntityFactoryProxy[] factoryProxy;
   private DurableType[] genericType;
   private volatile boolean autoReclaim;
-  private DurableHashMap<E, Object> map;
+  private DurableHashMap<E, Boolean> map;
   private A allocator;
   private long initialCapacity;
+  private static final Boolean DEFAULT = Boolean.TRUE;
 
   public void setCapacityHint(long capacity) {
     initialCapacity = capacity;
@@ -53,31 +55,37 @@ public class DurableHashSetImpl<A extends RestorableAllocator<A>, E>
   /**
    * adds a specific element to the set
    *
+   * @param item
+   *          the item to be added
+   *
    * @return true if set did not already contain the element
    */
   public boolean add(E item) {
-    //add logic for adding item
-    return true;
+    return map.put(item, DEFAULT) == null;
   }
 
   /**
    * removes a specific element from the set
    *
+   * @param item
+   *          the item to be removed
+   *
    * @return true if set contained the element
    */
-  public boolean remove(E value) {
-    //add logic for removing item
-    return true;
+  public boolean remove(E item) {
+    return map.remove(item) == DEFAULT;
   }
 
   /**
    * checks if set contains the specified element
    *
+   * @param item
+   *          the item to be searched
+   *
    * @return true if set contains the element
    */
   public boolean contains(E item) {
-    //add logic to check if set contains the item
-    return true;
+    return map.get(item) != null;
   }
 
   @Override
@@ -143,7 +151,7 @@ public class DurableHashSetImpl<A extends RestorableAllocator<A>, E>
     if (0L == phandler) {
       throw new RestoreDurableEntityError("Input handler is null on restoreDurableEntity.");
     }
-    map = DurableHashMapFactory.restore(allocator, factoryProxy, gType, phandler, autoReclaim);
+    map = DurableHashMapFactory.restore(allocator, this.factoryProxy, this.genericType, phandler, autoReclaim);
     if (null == map) {
       throw new RestoreDurableEntityError("Retrieve Entity Failure!");
     }
@@ -155,9 +163,10 @@ public class DurableHashSetImpl<A extends RestorableAllocator<A>, E>
   public void initializeDurableEntity(A allocator, EntityFactoryProxy[] factoryProxy,
               DurableType[] gType, boolean autoReclaim) {
     this.allocator = allocator;
-    this.factoryProxy = factoryProxy;
-    this.genericType = gType;
     this.autoReclaim = autoReclaim;
+    DurableType gftypes[] = {DurableType.BOOLEAN};
+    this.genericType = ArrayUtils.addAll(gType, gftypes);
+    this.factoryProxy = ArrayUtils.addAll(factoryProxy, null);
     try {
       this.unsafe = Utils.getUnsafe();
     } catch (Exception e) {
@@ -169,12 +178,38 @@ public class DurableHashSetImpl<A extends RestorableAllocator<A>, E>
   public void createDurableEntity(A allocator, EntityFactoryProxy[] factoryProxy,
               DurableType[] gType, boolean autoReclaim) throws OutOfHybridMemory {
     initializeDurableEntity(allocator, factoryProxy, gType, autoReclaim);
-    map = DurableHashMapFactory.create(allocator, factoryProxy, gType, initialCapacity, autoReclaim); 
+    map = DurableHashMapFactory.create(allocator, this.factoryProxy, this.genericType, initialCapacity, autoReclaim);
     initializeAfterCreate();
   }
 
   @Override
   public Iterator<E> iterator() {
-    return null;
+    return new HashSetItr(map.iterator());
+  }
+
+  private class HashSetItr implements Iterator<E> {
+
+    Iterator<MapEntry<E, Boolean>> mapItr;
+
+    HashSetItr(Iterator<MapEntry<E, Boolean>> itr) {
+      this.mapItr = itr;
+    }
+
+    @Override
+    public boolean hasNext() {
+      return mapItr.hasNext();
+    }
+
+    @Override
+    public E next() {
+      return mapItr.next().getKey();
+    }
+
+    @Override
+    public void remove() {
+      mapItr.remove();
+    }
+
+
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/854cb5c3/mnemonic-collections/src/test/java/org/apache/mnemonic/collections/DurableHashSetNGTest.java
----------------------------------------------------------------------
diff --git a/mnemonic-collections/src/test/java/org/apache/mnemonic/collections/DurableHashSetNGTest.java b/mnemonic-collections/src/test/java/org/apache/mnemonic/collections/DurableHashSetNGTest.java
index 49eb086..5a35174 100644
--- a/mnemonic-collections/src/test/java/org/apache/mnemonic/collections/DurableHashSetNGTest.java
+++ b/mnemonic-collections/src/test/java/org/apache/mnemonic/collections/DurableHashSetNGTest.java
@@ -17,27 +17,26 @@
 
 package org.apache.mnemonic.collections;
 
+import java.util.Iterator;
 import java.nio.ByteBuffer;
 import java.util.Random;
 import java.util.zip.Checksum;
-//import java.util.zip.CRC32;
-//import org.apache.commons.lang3.tuple.Pair;
-//import org.apache.commons.lang3.ArrayUtils;
 
 import org.apache.mnemonic.Utils;
 import org.apache.mnemonic.NonVolatileMemAllocator;
-//import org.apache.mnemonic.RestorableAllocator;
+import org.apache.mnemonic.RestorableAllocator;
+import org.apache.mnemonic.ParameterHolder;
 import org.apache.mnemonic.OutOfHybridMemory;
 import org.apache.mnemonic.DurableBuffer;
 import org.apache.mnemonic.DurableChunk;
 import org.apache.mnemonic.DurableType;
-//import org.apache.mnemonic.Durable;
-//import org.apache.mnemonic.EntityFactoryProxy;
+import org.apache.mnemonic.EntityFactoryProxy;
 import org.apache.mnemonic.Reclaim;
 import org.apache.commons.lang3.RandomUtils;
 import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
+import org.testng.AssertJUnit;
 import org.testng.Assert;
 
 import sun.misc.Unsafe;
@@ -126,33 +125,182 @@ public class DurableHashSetNGTest {
   }
 
   @Test(enabled = true)
-  public void testAddRemoveSetPrimitives() {
+  public void testAddRemoveSetIntegers() {
     DurableType gtypes[] = {DurableType.INTEGER};
     DurableHashSet<Integer> set = DurableHashSetFactory.create(m_act, null, gtypes, initialCapacity, false);
 
     Long handler = set.getHandler();
     boolean val;
-    /*for (int i = 0; i < 100; i++) {
+    for (int i = 0; i < 10; i++) {
       val = set.add(i);
-      Assert.assertFalse(val)
+      Assert.assertTrue(val);
     }
 
-    for (int i = 0; i < 100; i++) {
+    for (int i = 0; i < 10; i++) {
       val = set.contains(i);
-      Assert.assertTrue(val)
+      Assert.assertTrue(val);
     }
 
-    for (int i = 0; i < 100; i++) {
+    for (int i = 0; i < 10; i++) {
       val = set.remove(i);
-      Assert.assertTrue(val)
+      Assert.assertTrue(val);
     }
 
-    for (int i = 0; i < 100; i++) {
+    for (int i = 0; i < 10; i++) {
       val = set.contains(i);
-      Assert.assertFalse(val)
-    }*/
+      Assert.assertFalse(val);
+    }
+
+    set.destroy();
+  }
+
+  @Test(enabled = true)
+  public void testAddRemoveSetStrings() {
+    DurableType gtypes[] = {DurableType.STRING};
+    DurableHashSet<String> set = DurableHashSetFactory.create(m_act, null, gtypes, initialCapacity, false);
+
+    Long handler = set.getHandler();
+    boolean val;
+    for (int i = 0; i < 10; i++) {
+      val = set.add("str" + i);
+      Assert.assertTrue(val);
+    }
+
+    for (int i = 0; i < 10; i++) {
+      val = set.contains("str" + i);
+      Assert.assertTrue(val);
+    }
+
+    for (int i = 0; i < 10; i++) {
+      val = set.remove("str" + i);
+      Assert.assertTrue(val);
+    }
+
+    for (int i = 0; i < 10; i++) {
+      val = set.contains("str" + i);
+      Assert.assertFalse(val);
+    }
+
+    set.destroy();
+  }
+
+  @Test(enabled = true)
+  public void testAddRemoveDurable() {
+    DurableType gtypes[] = {DurableType.DURABLE};
+    EntityFactoryProxy efproxies[] = {new EntityFactoryProxy() {
+      @Override
+      public <A extends RestorableAllocator<A>> Person<Long> restore(
+          A allocator, EntityFactoryProxy[] factoryproxys,
+          DurableType[] gfields, long phandler, boolean autoreclaim) {
+        return PersonFactory.restore(allocator, factoryproxys, gfields, phandler, autoreclaim);
+      }
+      @Override
+      public <A extends RestorableAllocator<A>> Person<Long> restore(ParameterHolder<A> ph) {
+        return PersonFactory.restore(ph.getAllocator(),
+                ph.getEntityFactoryProxies(), ph.getGenericTypes(), ph.getHandler(), ph.getAutoReclaim());
+      }
+      @Override
+      public <A extends RestorableAllocator<A>> Person<Long> create(
+          A allocator, EntityFactoryProxy[] factoryproxys,
+          DurableType[] gfields, boolean autoreclaim) {
+        return PersonFactory.create(allocator, factoryproxys, gfields, autoreclaim);
+      }
+      @Override
+      public <A extends RestorableAllocator<A>> Person<Long> create(ParameterHolder<A> ph) {
+        return PersonFactory.create(ph.getAllocator(),
+                ph.getEntityFactoryProxies(), ph.getGenericTypes(), ph.getAutoReclaim());
+      }
+    } };
+
+    Person<Long> person =  (Person<Long>) efproxies[0].create(m_act, null, null, false);
+    person.setAge((short) 31);
+    person.setName("Bob", true);
+
+    Person<Long> anotherPerson =  (Person<Long>) efproxies[0].create(m_act, null, null, false);
+    anotherPerson.setAge((short) 30);
+    anotherPerson.setName("Alice", true);
+
+    DurableHashSet<Person<Long>> set = DurableHashSetFactory.create(m_act, efproxies, gtypes, initialCapacity, false);
+    boolean val = set.add(person);
+    AssertJUnit.assertTrue(val);
+    val = set.contains(person);
+    AssertJUnit.assertTrue(val);
+    val = set.contains(anotherPerson);
+    AssertJUnit.assertFalse(val);
+    val = set.add(anotherPerson);
+    AssertJUnit.assertTrue(val);
+    val = set.contains(anotherPerson);
+    AssertJUnit.assertTrue(val);
+
+    val = set.remove(person);
+    AssertJUnit.assertTrue(val);
+    val = set.contains(person);
+    AssertJUnit.assertFalse(val);
+    val = set.contains(anotherPerson);
+    AssertJUnit.assertTrue(val);
+
+    val = set.remove(anotherPerson);
+    AssertJUnit.assertTrue(val);
+    val = set.contains(anotherPerson);
+    AssertJUnit.assertFalse(val);
 
     set.destroy();
   }
+
+  @Test(enabled = true)
+  public void testSetIterator() {
+    DurableType gtypes[] = {DurableType.STRING};
+    DurableHashSet<String> set = DurableHashSetFactory.create(m_act, null, gtypes, initialCapacity, false);
+
+    Long handler = set.getHandler();
+    set.add("hello");
+    set.add("world");
+    AssertJUnit.assertEquals(set.getSize(), 2);
+
+    Iterator<String> iter = set.iterator();
+    int count = 0;
+    String entry = "";
+    while (iter.hasNext()) {
+      entry = iter.next();
+      count++;
+      if (entry.equals("world")) {
+        iter.remove();
+      }
+    }
+    AssertJUnit.assertEquals(count, 2);
+    AssertJUnit.assertEquals(set.getSize(), 1);
+    iter = set.iterator();
+    count = 0;
+    while (iter.hasNext()) {
+      entry = iter.next();
+      iter.remove();
+      count++;
+    }
+    AssertJUnit.assertEquals(count, 1);
+    AssertJUnit.assertEquals(set.getSize(), 0);
+    AssertJUnit.assertEquals(entry, "hello");
+
+    iter = set.iterator();
+    count = 0;
+    while (iter.hasNext()) {
+      entry = iter.next();
+      count++;
+    }
+    AssertJUnit.assertEquals(count, 0);
+    set.add("hello");
+    set.add("world");
+    AssertJUnit.assertEquals(set.getSize(), 2);
+
+    iter = set.iterator();
+    count = 0;
+    while (iter.hasNext()) {
+      entry = iter.next();
+      count++;
+    }
+    AssertJUnit.assertEquals(count, 2);
+
+    set.destroy();
+  }
+
 }