Permalink
Browse files

minor refactoring

  • Loading branch information...
1 parent 1c2c157 commit 6d59b1d6a890f43177fcd3897bb7a912bb95a944 Maysam Yabandeh committed May 25, 2012
@@ -25,18 +25,18 @@
public class Elders {
//set is efficient for membership checking
- protected HashSet<Elder> setofelders;
+ HashSet<Elder> setOfElders;
// The list of the failed elders: the elders that did not reincarnte in a timely manner
- protected TreeSet<Elder> failedElders;
+ TreeSet<Elder> failedElders;
//the eldest of elders: the elder with min ts
- protected Elder eldest = null;
- protected boolean eldestChangedSinceLastProbe = false;
+ Elder eldest = null;
+ boolean eldestChangedSinceLastProbe = false;
//heap is efficient for advancing largestDeletedTimestamp
//heap.peek is always valid but the other members might be stale
- protected PriorityQueue<Elder> heapofelders;
+ PriorityQueue<Elder> heapOfElders;
public Elders() {
- setofelders = new HashSet<Elder>();
- heapofelders = new PriorityQueue<Elder>();
+ setOfElders = new HashSet<Elder>();
+ heapOfElders = new PriorityQueue<Elder>();
failedElders = new TreeSet<Elder>();
}
@@ -58,7 +58,7 @@ public boolean isEldestChangedSinceLastProbe() {
}
//check if the eldest is still eldest
- protected void updateEldest(Elder newElder) {
+ void updateEldest(Elder newElder) {
assert(newElder != null);
Elder oldEldest = eldest;
if (eldest == null || eldest.getId() > newElder.getId())
@@ -68,7 +68,7 @@ protected void updateEldest(Elder newElder) {
}
//the eldest is removed, elect new eldest
- protected void setEldest() {
+ void setEldest() {
Elder oldEldest = eldest;
//a failed elder is elder than a non-failed elder
if (failedElders.size() > 0) {
@@ -77,12 +77,12 @@ protected void setEldest() {
}
//then select eldest among normal elders
//GC invalid peeks of the heap
- while (heapofelders.size() > 0 && !setofelders.contains(heapofelders.peek()))
- heapofelders.poll();
- if (heapofelders.size() == 0)
+ while (heapOfElders.size() > 0 && !setOfElders.contains(heapOfElders.peek()))
+ heapOfElders.poll();
+ if (heapOfElders.size() == 0)
eldest = null;
else
- eldest = heapofelders.peek();
+ eldest = heapOfElders.peek();
if (eldest != oldEldest)
eldestChangedSinceLastProbe = true;
}
@@ -91,8 +91,8 @@ public void addElder(long ts, long tc, RowKey[] wwRows) {
synchronized (this) {
Elder e = new Elder(ts, tc);
//TODO: store the rest as well
- heapofelders.offer(e);
- setofelders.add(e);
+ heapOfElders.offer(e);
+ setOfElders.add(e);
updateEldest(e);
//System.out.println("WWWWWW " + ts);
}
@@ -103,7 +103,7 @@ public boolean reincarnateElder(long id) {
synchronized (this) {
assert(eldest == null || eldest.getId() <= id);
Elder e = new Elder(id);
- boolean isStillElder = setofelders.remove(e);
+ boolean isStillElder = setOfElders.remove(e);
itWasFailed = false;
if (!isStillElder)//then it is a failed elder
itWasFailed = failedElders.remove(e);
@@ -117,9 +117,9 @@ public boolean reincarnateElder(long id) {
public Set<Elder> raiseLargestDeletedTransaction(long id) {
Set<Elder> failed = null;
synchronized (this) {
- while (heapofelders.size() > 0 && heapofelders.peek().getId() < id) {
- Elder e = heapofelders.poll();
- boolean isStillElder = setofelders.remove(e);
+ while (heapOfElders.size() > 0 && heapOfElders.peek().getId() < id) {
+ Elder e = heapOfElders.poll();
+ boolean isStillElder = setOfElders.remove(e);
if (isStillElder) {
if (failed == null)
failed = new TreeSet<Elder>();
@@ -25,103 +25,81 @@
import org.jboss.netty.buffer.ChannelBuffer;
public class RowKey implements Comparable<RowKey> {
- private byte[] rowId;
- private byte[] tableId;
- private int hash = 0;
- //these are not to be serialized, just used for lock-based impl
- public int index = -1;//the index on hashmap, it is used to sort the rowkeys
+ private byte[] rowId;
+ private byte[] tableId;
+ private int hash = 0;
+ //these are not to be serialized, just used for lock-based impl
+ public int index = -1;//the index on hashmap, it is used to sort the rowkeys
- public RowKey() {
- rowId = new byte[0];
- tableId = new byte[0];
- }
+ public RowKey() {
+ rowId = new byte[0];
+ tableId = new byte[0];
+ }
- public RowKey(byte[] r, byte[] t) {
- rowId = r;
- tableId = t;
- }
- public byte[] getTable() {
- return tableId;
- }
+ public RowKey(byte[] r, byte[] t) {
+ rowId = r;
+ tableId = t;
+ }
+ public byte[] getTable() {
+ return tableId;
+ }
- public byte[] getRow() {
- return rowId;
- }
+ public byte[] getRow() {
+ return rowId;
+ }
- public String toString() {
- return new String(tableId) + ":" + new String(rowId);
- }
+ public String toString() {
+ return new String(tableId) + ":" + new String(rowId);
+ }
- public static RowKey readObject(ChannelBuffer aInputStream)
- throws IOException {
- //int index = aInputStream.readInt();
- int hash = aInputStream.readInt();
- short len = aInputStream.readByte();
-// byte[] rowId = RowKeyBuffer.nextRowKey(len);
- byte[] rowId = new byte[len];
- aInputStream.readBytes(rowId, 0, len);
- len = aInputStream.readByte();
-// byte[] tableId = RowKeyBuffer.nextRowKey(len);
- byte[] tableId = new byte[len];
- aInputStream.readBytes(tableId, 0, len);
- RowKey rk = new RowKey(rowId, tableId);
- rk.hash = hash;
- //rk.index = index;
- return rk;
- }
+ public static RowKey readObject(ChannelBuffer aInputStream)
+ throws IOException {
+ int hash = aInputStream.readInt();
+ short len = aInputStream.readByte();
+ byte[] rowId = new byte[len];
+ aInputStream.readBytes(rowId, 0, len);
+ len = aInputStream.readByte();
+ byte[] tableId = new byte[len];
+ aInputStream.readBytes(tableId, 0, len);
+ RowKey rk = new RowKey(rowId, tableId);
+ rk.hash = hash;
+ return rk;
+ }
- public void writeObject(DataOutputStream aOutputStream)
- throws IOException {
- hashCode();
- //assert(index != -1);
- //aOutputStream.writeInt(index);
- aOutputStream.writeInt(hash);
- aOutputStream.writeByte(rowId.length);
- aOutputStream.write(rowId,0,rowId.length);
- aOutputStream.writeByte(tableId.length);
- aOutputStream.write(tableId,0,tableId.length);
- }
+ public void writeObject(DataOutputStream aOutputStream)
+ throws IOException {
+ hashCode();
+ aOutputStream.writeInt(hash);
+ aOutputStream.writeByte(rowId.length);
+ aOutputStream.write(rowId,0,rowId.length);
+ aOutputStream.writeByte(tableId.length);
+ aOutputStream.write(tableId,0,tableId.length);
+ }
- public boolean equals(Object obj) {
- if (obj instanceof RowKey) {
- RowKey other = (RowKey)obj;
-
- return Bytes.equals(other.rowId, rowId)
- && Bytes.equals(other.tableId, tableId);
- }
- return false;
- }
+ public boolean equals(Object obj) {
+ if (obj instanceof RowKey) {
+ RowKey other = (RowKey)obj;
- public int hashCode() {
- if (hash != 0) {
- return hash;
- }
- //hash is the xor or row and table id
- /*int h = 0;
- for(int i =0; i < Math.min(8, rowId.length); i++){
- h <<= 8;
- h ^= (int)rowId[i] & 0xFF;
- }
- hash = h;
- h = 0;
- for(int i =0; i < Math.min(8,tableId.length); i++){
- h <<= 8;
- h ^= (int)tableId[i] & 0xFF;
- }
- hash ^= h;
- return hash;*/
- byte[] key = Arrays.copyOf(tableId, tableId.length + rowId.length);
- System.arraycopy(rowId, 0, key, tableId.length, rowId.length);
- hash = MurmurHash.getInstance().hash(key, 0, key.length, 0xdeadbeef);
- //return MurmurHash3.MurmurHash3_x64_32(rowId, 0xDEADBEEF);
- // return (31*Arrays.hashCode(tableId)) + Arrays.hashCode(rowId);
- return hash;
- }
+ return Bytes.equals(other.rowId, rowId)
+ && Bytes.equals(other.tableId, tableId);
+ }
+ return false;
+ }
- //assume: index must be assigned before using the RowKey in a collection
- //This is used to sort the RowKeys
- public int compareTo(RowKey rk) {
- return index - rk.index;
- }
+ public int hashCode() {
+ if (hash != 0) {
+ return hash;
+ }
+ byte[] key = Arrays.copyOf(tableId, tableId.length + rowId.length);
+ System.arraycopy(rowId, 0, key, tableId.length, rowId.length);
+ hash = MurmurHash.getInstance().hash(key, 0, key.length, 0xdeadbeef);
+ return hash;
+ }
+
+ //assume: index must be assigned before using the RowKey in a collection
+ //This is used to sort the RowKeys
+ public int compareTo(RowKey rk) {
+ return index - rk.index;
+ }
}
@@ -115,8 +115,6 @@
* @param channelGroup
*/
public TSOHandler(ChannelGroup channelGroup, TSOState state) {
- //System.out.println("This is rwcimbo with elders - no filter is installed");
- //System.out.println("This is buggy rwcimbo");
this.channelGroup = channelGroup;
this.timestampOracle = state.getSO();
this.sharedState = state;
@@ -37,7 +37,6 @@ public void testCommit() throws Exception {
clientHandler.sendMessage(new CommitRequest(tr1.timestamp));
CommitResponse cr1 = clientHandler.receiveMessage(CommitResponse.class);
assertTrue(cr1.committed);
- //Changed by Maysam Yabandeh
//For read-only transactions, Tc=Ts is valid
assertTrue(cr1.commitTimestamp >= tr1.timestamp);
//assertTrue(cr1.commitTimestamp > tr1.timestamp);
@@ -38,7 +38,6 @@ public void testCommitReport() throws Exception {
clientHandler.sendMessage(new CommitRequest(tr1.timestamp));
CommitResponse cr1 = clientHandler.receiveMessage(CommitResponse.class);
assertTrue(cr1.committed);
- //Changed by Maysam Yabandeh
//For read-only transactions, Tc=Ts is valid
assertTrue(cr1.commitTimestamp >= tr1.timestamp);
//assertTrue(cr1.commitTimestamp > tr1.timestamp);
@@ -51,7 +50,6 @@ public void testCommitReport() throws Exception {
secondClientHandler.sendMessage(new CommitRequest(tr2.timestamp));
CommitResponse cr2 = secondClientHandler.receiveMessage(CommitResponse.class);
assertTrue(cr2.committed);
- //Changed by Maysam Yabandeh
//For read-only transactions, Tc=Ts is valid
assertTrue(cr2.commitTimestamp >= tr2.timestamp);
//assertTrue(cr2.commitTimestamp > tr2.timestamp);

0 comments on commit 6d59b1d

Please sign in to comment.