Recovery
[IRC.git] / Robust / src / Benchmarks / Recovery / FileSystem / DistributedHashMap.java
1 public class DistributedHashMap {
2   DistributedHashEntry[] table;
3   float loadFactor;
4   int secondcapacity;
5
6   public DistributedHashMap(int initialCapacity, int secondcapacity, float loadFactor) {
7     init(initialCapacity, secondcapacity, loadFactor);
8   }
9
10   private void init(int initialCapacity, int secondcapacity, float loadFactor) {
11     table = global new DistributedHashEntry[initialCapacity];
12     this.loadFactor=loadFactor;
13     this.secondcapacity=secondcapacity;
14   }
15
16   private static int hash1(int hashcode, int length) {
17     int value=hashcode%length;
18     if (value<0)
19       return -value;
20     else
21       return value;
22   }
23
24   private static int hash2(int hashcode, int length1, int length2) {
25     int value=(hashcode*31)%length2;
26     if (value<0)
27       return -value;
28     else
29       return value;
30   }
31
32   void resize(int index) {
33     DHashEntry[] oldtable=table[index].array;
34     int newCapacity=oldtable.length*2+1;
35     DHashEntry [] newtable=global new DHashEntry[newCapacity];
36     table[index].array=newtable;
37
38     for(int i=0; i<oldtable.length; i++) {
39       DHashEntry e=oldtable[i];
40       while(e!=null) {
41         DHashEntry next=e.next;
42         int bin=hash2(e.hashval, table.length, newCapacity);
43         e.next=newtable[bin];
44         newtable[bin]=e;
45         e=next;
46       }
47     }
48   }
49
50   Object remove(Object key) {
51     int hashcode=key.hashCode();
52     int index1=hash1(hashcode, table.length);
53     DistributedHashEntry dhe=table[index1];
54     if (dhe==null)
55       return null;
56     int index2=hash2(hashcode, table.length, dhe.array.length);
57     DHashEntry ptr=dhe.array[index2];
58
59     if (ptr!=null) {
60       if (ptr.hashval==hashcode&&ptr.key.equals(key)) {
61         dhe.array[index2]=ptr.next;
62         dhe.count--;
63         return ptr.value;
64       }
65       while(ptr.next!=null) {
66         if (ptr.hashval==hashcode&&ptr.next.key.equals(key)) {
67           Object oldvalue=ptr.value;
68           ptr.next=ptr.next.next;
69           dhe.count--;
70           return oldvalue;
71         }
72         ptr=ptr.next;
73       }
74     }
75     return null;
76   }
77
78   Object get(Object key) {
79     int hashcode=key.hashCode();
80     int index1=hash1(hashcode, table.length);
81     
82     DistributedHashEntry dhe=table[index1];
83     if (dhe==null)
84       return null;
85
86     int index2=hash2(hashcode, table.length, dhe.array.length);
87     
88     DHashEntry ptr=dhe.array[index2];
89
90     while(ptr!=null) {
91       if (ptr.hashval==hashcode
92           &&ptr.key.equals(key)) {
93         return ptr.value;
94       }
95       ptr=ptr.next;
96     }
97     return null;
98   }
99
100   boolean containsKey(Object key) {
101     int hashcode=key.hashCode();
102     int index1=hash1(hashcode, table.length);
103     DistributedHashEntry dhe=table[index1];
104     if (dhe==null)
105       return false;
106     int index2=hash2(hashcode, table.length, dhe.array.length);
107     DHashEntry ptr=dhe.array[index2];
108
109     while(ptr!=null) {
110       if (ptr.hashval==hashcode
111           &&ptr.key.equals(key)) {
112         return true;
113       }
114       ptr=ptr.next;
115     }
116     return false;
117   }
118
119   Object put(Object key, Object value) {
120     int hashcode=key.hashCode();
121     int index1=hash1(hashcode, table.length);
122     DistributedHashEntry dhe=table[index1];
123     if (dhe==null) {
124       dhe=global new DistributedHashEntry(secondcapacity);
125       table[index1]=dhe;
126     }
127     int index2=hash2(hashcode, table.length, dhe.array.length);
128     DHashEntry ptr=dhe.array[index2];
129
130     while(ptr!=null) {
131       if (ptr.hashval==hashcode&&ptr.key.equals(key)) {
132         Object oldvalue=ptr.value;
133         ptr.value=value;
134         return oldvalue;
135       }
136       ptr=ptr.next;
137     }
138
139     DHashEntry he=global new DHashEntry();
140     he.value=value;
141     he.key=key;
142     he.hashval=hashcode;
143     he.next=dhe.array[index2];
144     dhe.array[index2]=he;
145
146     dhe.count++;
147     if (dhe.count>(loadFactor*dhe.array.length)) {
148       //Resize the table
149       resize(index1);
150     }
151     return null;
152   }
153 }
154
155
156 class DistributedHashEntry {
157   public DistributedHashEntry(int capacity) {
158     array=global new DHashEntry[capacity];
159   }
160   int count;
161   DHashEntry[] array;
162 }
163
164
165 class DHashEntry {
166   public DHashEntry() {
167   }
168   int hashval;
169   Object key;
170   Object value;
171   DHashEntry next;
172 }