public Table(String baseurl, String password, long _localmachineid) {
localmachineid=_localmachineid;
buffer = new SlotBuffer();
+ numslots = buffer.capacity();
sequencenumber = 1;
initCloud(baseurl, password);
}
+ public Table(CloudComm _cloud, long _localmachineid) {
+ localmachineid=_localmachineid;
+ buffer = new SlotBuffer();
+ sequencenumber = 1;
+ cloud=_cloud;
+ }
+
private void initCloud(String baseurl, String password) {
try {
SecretKeySpec secret=getKey(password);
return null;
}
+ public void initTable() {
+ Slot s=new Slot(1, localmachineid);
+ TableStatus status=new TableStatus(s, numslots);
+ s.addEntry(status);
+ Slot[] array=cloud.putSlot(s, numslots);
+ if (array == null) {
+ array = new Slot[] {s};
+ validateandupdate(array); // update data structure
+ } else {
+ throw new Error("Error on initialization");
+ }
+ }
+
public IoTString put(IoTString key, IoTString value) {
while(true) {
KeyValue oldvalue=table.get(key);
- if (tryput(key, value)) {
+ if (tryput(key, value, false)) {
return oldvalue.getValue();
}
}
}
- private boolean tryput(IoTString key, IoTString value) {
- Slot s=new Slot(sequencenumber+1, localmachineid, buffer.getSlot(sequencenumber).getHMAC());
- boolean forcedresize = false;
-
+ private boolean tryput(IoTString key, IoTString value, boolean forcedresize) {
+ Slot s=new Slot(sequencenumber+1, localmachineid, buffer.getSlot(sequencenumber).getHMAC());
long seqn = buffer.getOldestSeqNum();
-
+
+ if (forcedresize) {
+ TableStatus status=new TableStatus(s, FORCED_RESIZE_INCREMENT + numslots);
+ s.addEntry(status);
+ }
+
if ((numslots - buffer.size()) < FREE_SLOTS) {
//have to check whether we have enough free slots
seqn = buffer.getNewestSeqNum() + 1 - numslots;
else if (i==0) {
if (s.canFit(liveentry))
s.addEntry(liveentry);
- else
- forcedresize = true;
- }
+ else if (!forcedresize) {
+ return tryput(key, value, true);
+ }
+ }
}
}
}
--- /dev/null
+package iotcloud;
+
+public class Test {
+ public static void main(String[] args) {
+ TestCloudComm cc=new TestCloudComm();
+ Table t1=new Table(cc, 6513);
+ t1.initTable();
+ Table t2=new Table(cc, 6512);
+ t2.update();
+ for(int i=0;i<100;i++) {
+ String a="STR"+i;
+ String b="ABR"+i;
+ IoTString ia=new IoTString(a);
+ IoTString ib=new IoTString(b);
+ t1.put(ia, ia);
+ t2.put(ib, ib);
+ System.out.println(ib+"->"+t1.get(ib));
+ System.out.println(ia+"->"+t2.get(ia));
+ }
+ }
+}
--- /dev/null
+package iotcloud;
+import java.io.*;
+import java.net.*;
+import java.util.Arrays;
+import javax.crypto.*;
+
+class TestCloudComm extends CloudComm {
+ SlotBuffer buffer;
+
+ TestCloudComm() {
+ buffer = new SlotBuffer();
+ }
+
+ public synchronized Slot[] putSlot(Slot slot, int max) {
+ if (buffer.getNewestSeqNum()+1 == slot.getSequenceNumber()) {
+ if (max!=0)
+ buffer.resize(max);
+ buffer.putSlot(slot);
+ return null;
+ } else
+ return getSlots(slot.getSequenceNumber());
+ }
+
+ public synchronized Slot[] getSlots(long sequencenumber) {
+ long newestseqnum=buffer.getNewestSeqNum();
+ long oldestseqnum=buffer.getOldestSeqNum();
+ if (sequencenumber < oldestseqnum)
+ sequencenumber=oldestseqnum;
+ int numslots=(int)((newestseqnum - sequencenumber)+1);
+ Slot[] slots=new Slot[numslots];
+ for(int i=0;i<numslots;i++,sequencenumber++)
+ slots[i]=buffer.getSlot(sequencenumber);
+ return slots;
+ }
+}