module DistHT;
import * from ABS.StdLib;
type Key = Int;
type Data = Int;
def Int hash(Key k,Int n) = k%n;
//[k > 111][result() > 0]
def B mylookup(Map ms, A k) = // retrieve from the map
case ms {
InsertAssoc(Pair(k, y), _) => y;
// InsertAssoc(_, tm) => lookup(tm, k);
// Rewritten for aPET (to avoid having fresh vars in the nomatch not present in the match)
InsertAssoc(Pair(_, _), tm) => mylookup(tm, k);
};
def Map myput(Map ms, A k, B v) =
case ms {
EmptyMap => InsertAssoc(Pair(k, v),EmptyMap);
InsertAssoc(Pair(k, _), ts) => InsertAssoc(Pair(k, v), ts);
// InsertAssoc(p, ts) => InsertAssoc(p, myput(ts, k, v));
// Rewritten for aPET (to avoid having fresh vars in the nomatch not present in the match)
InsertAssoc(Pair(kp,vp), ts) => InsertAssoc(Pair(kp,vp), myput(ts, k, v));
};
interface DistHTInt{
Maybe getData(Key key);
List> getAllData(List keys);
Unit putData(Key key,Data d);
Unit putAllData(List> pairs);
Unit put2Data(Key key1,Data d1,Key key2,Data d2);
NodeInt lookupNode(Key key);
Set getAllKeys();
}
interface NodeInt{
Maybe getData(Key key);
Unit putData(Key key,Data d);
Set getKeys();
}
class DistHT(List nodes,Int nNodes) implements DistHTInt{
[nodes <= max(nodes)][nNodes <= max(nNodes)]
NodeInt lookupNode(Key key){
Int n = hash(key,nNodes);
NodeInt node = nth(nodes,n);
return node;
}
Maybe getData(Key key){
NodeInt node;
node = this.lookupNode(key);
Fut> f;
f = node!getData(key);
await f?;
Maybe d = f.get;
return d;
}
List> getAllData(List keys){
List> r = Nil;
Key firstK = 0; List tailKs = Nil;
NodeInt node;
Fut> fd; Fut>> fds;
Maybe d = Just(0); List> ds = Nil;
if (keys != Nil){
firstK = head(keys);
tailKs = tail(keys);
node = this.lookupNode(firstK);
fd = node!getData(firstK);
fds = this!getAllData(tailKs);
await fd? & fds?;
d = fd.get;
ds = fds.get;
r = Cons(d,ds);
}
return r;
}
[nodes <= max(nodes)]
Set getAllKeys(){
return this.getAllKeysAux(nodes);
}
Set getAllKeysAux(List ns){
Set r = EmptySet;
NodeInt firstNode; List tailNs = Nil;
Fut> fFirstKs; Fut> fRKs;
Set firstKs = EmptySet; Set rKs = EmptySet;
if (ns != Nil){
firstNode = head(ns);
tailNs = tail(ns);
fFirstKs = firstNode!getKeys();
fRKs = this!getAllKeysAux(tailNs);
await fFirstKs? & fRKs?;
firstKs = fFirstKs.get;
rKs = fRKs.get;
r = union(firstKs,rKs);
}
return r;
}
Unit putData(Key key,Data d){
NodeInt node;
node = this.lookupNode(key);
node!putData(key,d);
}
Unit putAllData(List> pairs){
while (isEmpty(pairs) == False){
this!putData(fst(head(pairs)),snd(head(pairs)));
pairs = tail(pairs);
}
}
Unit put2Data(Key key1,Data d1,Key key2,Data d2){
this!putData(key1,d1);
this!putData(key2,d2);
}
}
class Node(Map map) implements NodeInt{
[map <= max(map)]
Maybe getData(Key key){
return lookup(map,key);
}
[map <= max(map)]
Unit putData(Key key,Data d){
map = myput(map,key,d);
}
[map <= max(map)]
Set getKeys(){
return keys(map);
}
}
interface IMain {
Unit main(List nodes,Int nNodes, Int nData);
}
class Main () implements IMain {
Unit main(List nodes,Int nNodes, Int nData) {
Map m1 = EmptyMap;
Map m2 = EmptyMap;
NodeInt n1 = new cog Node(m1);
NodeInt n2 = new cog Node(m2);
DistHTInt dht = new cog DistHT(Cons(n1,Cons(n2,Nil)),nNodes);
dht!putData(nData,nData);
while(nData > 0) {
dht!putData(nData,nData);
nData = nData - 1;
}
dht!getAllData(Cons(2,Nil));
dht!getAllData(Cons(2,Cons(5,Cons(4,Nil))));
}
}
class C {
Unit m(List nodes,Int nNodes, Int nData) {
IMain main1 = new cog Main ();
IMain main2 = new cog Main ();
main1!main(nodes,nNodes, nData);
main2!main(nodes,nNodes, nData);
}
}
{
Map m1 = EmptyMap;
Map m2 = EmptyMap;
NodeInt n1 = new cog Node(m1);
NodeInt n2 = new cog Node(m2);
DistHTInt dht = new cog DistHT(Cons(n1,Cons(n2,Nil)),2);
//dht!getData(0);
dht!putData(0,10);
//dht!getData(2);
dht!putData(2,20);
//dht!putData(0,20);
dht!getAllData(Cons(0,Cons(2,Nil)));
/* dht.putData(2,1);
dht.putData(5,2);
dht.putData(7,3);
dht.putData(4,4);
//dht.getAllData(Cons(2,Nil));
dht.getAllData(Cons(2,Cons(5,Cons(4,Nil))));
dht.getAllKeys();*/
}