Developing lightweight computation at the DSG edge

Commit 16f81e7e authored by deepthidevaki's avatar deepthidevaki
Browse files

book store commands

parent 67538ddf
public class BookCommands {
public String addUser(String username, String userEmail){
not_implemented();
return null;
}
public String addOwnedBooks(String username, String book){
not_implemented();
return null;
}
public String[] getOwnedBooks(String username){
not_implemented();
return null;
}
public String removeOwnedBook(String username, String book){
not_implemented();
return null;
}
public String borrowBook(String fromUser, String byUser, String book){
not_implemented();
return null;
}
void not_implemented(){
System.out.println("Not implemented");
}
}
import java.io.IOException;
import java.util.List;
import eu.antidotedb.client.AntidoteClient;
import eu.antidotedb.client.Host;
......@@ -12,7 +13,7 @@ public class BookStore {
/* *** Demo Commands *** */
@Command // One,
@Command
public String hello() {
return "Hello, World!";
}
......@@ -22,6 +23,11 @@ public class BookStore {
return DemoCommandsExecutor.incCounter(currentSession, bucket, key);
}
@Command
public List<String> addtoset(String bucket, String key, String item){
return DemoCommandsExecutor.addToSet(currentSession, bucket, key, item);
}
@Command //connect antidote
public String connect(String host, int port){
currentSession = new AntidoteClient(new Host(host, port));
......@@ -35,7 +41,31 @@ public class BookStore {
}
/* *** BookStore commands *** */
// TODO
@Command
public String adduser(String username, String userEmail){
return (new BookCommands().addUser(username, userEmail));
}
@Command
public String ownbook(String username, String book){
return (new BookCommands().addOwnedBooks(username, book));
}
@Command
public String[] getownedbook(String username){
return (new BookCommands().getOwnedBooks(username));
}
@Command
public String removeownedbook(String username, String book){
return (new BookCommands().removeOwnedBook(username, book));
}
@Command
public String borrowbook(String fromUser, String byUser, String book){
return (new BookCommands().borrowBook(fromUser, byUser, book));
}
public static void main(String[] args) throws IOException {
ShellFactory.createConsoleShell("antidotebookstore", "", new BookStore())
......
import java.util.List;
import eu.antidotedb.client.AntidoteClient;
import eu.antidotedb.client.AntidoteStaticTransaction;
import eu.antidotedb.client.BatchRead;
import eu.antidotedb.client.BatchReadResult;
import eu.antidotedb.client.Bucket;
import eu.antidotedb.client.CounterRef;
import eu.antidotedb.client.InteractiveTransaction;
import eu.antidotedb.client.SetRef;
public class DemoCommandsExecutor {
//increment a counter without wrapping it in a transaction
public static int incCounter(AntidoteClient client, String cbucket, String key){
Bucket<String> bucket = Bucket.create(cbucket);
CounterRef cnt = bucket.counter(key);
......@@ -12,4 +20,50 @@ public class DemoCommandsExecutor {
return cnt.read(client.noTransaction());
}
//update a set without wrapping it in a transaction
public static List<String> addToSet(AntidoteClient client, String cbucket, String key, String elem){
Bucket<String> bucket = Bucket.create(cbucket);
SetRef<String> set = bucket.set(key);
set.add(client.noTransaction(),elem);
return set.read(client.noTransaction());
}
//update and read with in a transaction
public static String addToSetTxn(AntidoteClient client){
Bucket<String> bucket = Bucket.create("testbucket");
CounterRef cnt = bucket.counter("testcounter");
SetRef<String> set = bucket.set("testset");
try (InteractiveTransaction tx = client.startTransaction()) {
cnt.increment(tx);
int i = cnt.read(tx);
set.add(tx, "i:" + i);
tx.commitTransaction();
}
return "ok";
}
//batch updates and batch reads
public static int batchUpdates(AntidoteClient client){
Bucket<String> bucket = Bucket.create("testbucket");
CounterRef a = bucket.counter("batchcounter1");
CounterRef b = bucket.counter("batchcounter2");
CounterRef c = bucket.counter("batchcounter3");
//increment counters in a batch transaction.
AntidoteStaticTransaction tx = client.createStaticTransaction();
a.increment(tx);
b.increment(tx);
c.increment(tx);
tx.commitTransaction();
//Read multiple keys in batch read
BatchRead batchRead = client.newBatchRead();
BatchReadResult<Integer> c1val = a.read(batchRead);
BatchReadResult<Integer> c2val = b.read(batchRead);
BatchReadResult<Integer> c3val = c.read(batchRead);
batchRead.commit();
int sum = c1val.get() + c2val.get() + c3val.get();
return sum;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment