initial checkin
authoradash <adash>
Fri, 30 Oct 2009 00:30:17 +0000 (00:30 +0000)
committeradash <adash>
Fri, 30 Oct 2009 00:30:17 +0000 (00:30 +0000)
Robust/src/Benchmarks/Distributed/SpamFilter/DistributedHashMap.java [new file with mode: 0644]
Robust/src/Benchmarks/Distributed/SpamFilter/HashEntry.java [new file with mode: 0644]
Robust/src/Benchmarks/Distributed/SpamFilter/HashStat.java [new file with mode: 0644]

diff --git a/Robust/src/Benchmarks/Distributed/SpamFilter/DistributedHashMap.java b/Robust/src/Benchmarks/Distributed/SpamFilter/DistributedHashMap.java
new file mode 100644 (file)
index 0000000..f860111
--- /dev/null
@@ -0,0 +1,169 @@
+public class DistributedHashMap {
+  DistributedHashEntry[] table;
+  float loadFactor;
+  int secondcapacity;
+
+  public DistributedHashMap(int initialCapacity, int secondcapacity, float loadFactor) {
+    init(initialCapacity, secondcapacity, loadFactor);
+  }
+
+  private void init(int initialCapacity, int secondcapacity, float loadFactor) {
+    table=global new DistributedHashEntry[initialCapacity];
+    this.loadFactor=loadFactor;
+    this.secondcapacity=secondcapacity;
+  }
+
+  private static int hash1(int hashcode, int length) {
+    int value=hashcode%length;
+    if (value<0)
+      return -value;
+    else
+      return value;
+  }
+
+  private static int hash2(int hashcode, int length1, int length2) {
+    int value=(hashcode*31)%length2;
+    if (value<0)
+      return -value;
+    else
+      return value;
+  }
+
+  void resize(int index) {
+    DHashEntry[] oldtable=table[index].array;
+    int newCapacity=oldtable.length*2+1;
+    DHashEntry [] newtable=global new DHashEntry[newCapacity];
+    table[index].array=newtable;
+
+    for(int i=0; i<oldtable.length; i++) {
+      DHashEntry e=oldtable[i];
+      while(e!=null) {
+       DHashEntry next=e.next;
+       int bin=hash2(e.hashval, table.length, newCapacity);
+       e.next=newtable[bin];
+       newtable[bin]=e;
+       e=next;
+      }
+    }
+  }
+
+  Object remove(Object key) {
+    int hashcode=key.hashCode();
+    int index1=hash1(hashcode, table.length);
+    DistributedHashEntry dhe=table[index1];
+    if (dhe==null)
+      return null;
+    int index2=hash2(hashcode, table.length, dhe.array.length);
+    DHashEntry ptr=dhe.array[index2];
+
+    if (ptr!=null) {
+      if (ptr.hashval==hashcode&&ptr.key.equals(key)) {
+       dhe.array[index2]=ptr.next;
+       dhe.count--;
+       return ptr.value;
+      }
+      while(ptr.next!=null) {
+       if (ptr.hashval==hashcode&&ptr.next.key.equals(key)) {
+         Object oldvalue=ptr.value;
+         ptr.next=ptr.next.next;
+         dhe.count--;
+         return oldvalue;
+       }
+       ptr=ptr.next;
+      }
+    }
+    return null;
+  }
+
+  Object get(Object key) {
+    int hashcode=key.hashCode();
+    int index1=hash1(hashcode, table.length);
+    DistributedHashEntry dhe=table[index1];
+    if (dhe==null)
+      return null;
+
+    int index2=hash2(hashcode, table.length, dhe.array.length);
+    DHashEntry ptr=dhe.array[index2];
+
+    while(ptr!=null) {
+      if (ptr.hashval==hashcode
+          &&ptr.key.equals(key)) {
+       return ptr.value;
+      }
+      ptr=ptr.next;
+    }
+    return null;
+  }
+
+  boolean containsKey(Object key) {
+    int hashcode=key.hashCode();
+    int index1=hash1(hashcode, table.length);
+    DistributedHashEntry dhe=table[index1];
+    if (dhe==null)
+      return false;
+    int index2=hash2(hashcode, table.length, dhe.array.length);
+    DHashEntry ptr=dhe.array[index2];
+
+    while(ptr!=null) {
+      if (ptr.hashval==hashcode
+          &&ptr.key.equals(key)) {
+       return true;
+      }
+      ptr=ptr.next;
+    }
+    return false;
+  }
+
+  Object put(Object key, Object value) {
+    int hashcode=key.hashCode();
+    int index1=hash1(hashcode, table.length);
+    DistributedHashEntry dhe=table[index1];
+    if (dhe==null) {
+      dhe=global new DistributedHashEntry(secondcapacity);
+      table[index1]=dhe;
+    }
+    int index2=hash2(hashcode, table.length, dhe.array.length);
+    DHashEntry ptr=dhe.array[index2];
+
+    while(ptr!=null) {
+      if (ptr.hashval==hashcode&&ptr.key.equals(key)) {
+       Object oldvalue=ptr.value;
+       ptr.value=value;
+       return oldvalue;
+      }
+      ptr=ptr.next;
+    }
+
+    DHashEntry he=global new DHashEntry();
+    he.value=value;
+    he.key=key;
+    he.hashval=hashcode;
+    he.next=dhe.array[index2];
+    dhe.array[index2]=he;
+
+    dhe.count++;
+    if (dhe.count>(loadFactor*dhe.array.length)) {
+      //Resize the table
+      resize(index1);
+    }
+    return null;
+  }
+}
+
+
+class DistributedHashEntry {
+  public DistributedHashEntry(int capacity) {
+    array=global new DHashEntry[capacity];
+  }
+  int count;
+  DHashEntry[] array;
+}
+
+class DHashEntry {
+  int hashval;
+  Object key;
+  Object value;
+  DHashEntry next;
+  public DHashEntry() {
+  }
+}
diff --git a/Robust/src/Benchmarks/Distributed/SpamFilter/HashEntry.java b/Robust/src/Benchmarks/Distributed/SpamFilter/HashEntry.java
new file mode 100644 (file)
index 0000000..31d60aa
--- /dev/null
@@ -0,0 +1,45 @@
+public class HashEntry {
+  String engine;
+  String signature;
+  HashStat stats;
+  public HashEntry() {
+  }
+
+  /**
+   * hashCode that combines two strings using xor.
+   * @return a hash code value on the entire object.
+   */
+  public int hashCode() {
+    int result=0;
+    // this will not work well if some of the strings are equal.
+    result = engine.hashCode();
+    result ^= signature.hashCode();
+    result ^= stats.hashCode();
+    return result;
+  }
+
+  public String getEngine() {
+    return engine;
+  }
+
+  public String getSignature() {
+    return signature;
+  }
+
+  public Stat getStats() {
+    return stats;
+  }
+
+  public boolean equals(Object o) {
+    if(o.getType()!=getType())
+      return false;
+    HashEntry he = (HashEntry)o;
+    if(!(he.getEngine().equals(Engine)))
+      return false;
+    if(!(he.getSignature().equals(Signature)))
+      return false;
+    if(!(he.getStats().equals(stats)))
+      return false;
+    return true;
+  }
+}
diff --git a/Robust/src/Benchmarks/Distributed/SpamFilter/HashStat.java b/Robust/src/Benchmarks/Distributed/SpamFilter/HashStat.java
new file mode 100644 (file)
index 0000000..68b3ebf
--- /dev/null
@@ -0,0 +1,27 @@
+public class HashStat {
+  int[] userid;
+  FilterStatistic[] userstat; 
+  public HashStat() {
+    userid = new int[8]; //max users for our system=8
+    userstat = new FilterStatistic[8];
+    for(int i=0; i<8; i++) {
+      userstat[i] = new FilterStatistic();
+    }
+  }
+
+  public int getuser(int id) {
+    return userid[id];
+  }
+
+  public int getspamcount(int userid) {
+    return userstat[userid].getSpam();
+  }
+
+  public int gethamcount(int userid) {
+    return userstat[userid].getham();
+  }
+
+  public int getunknowncount(int userid) {
+    return userstat[userid].getUnknown();
+  }
+}