Developing lightweight computation at the DSG edge
Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Roger Pueyo Centelles
antidote-java-tutorial
Commits
16f81e7e
Commit
16f81e7e
authored
Jul 11, 2017
by
deepthidevaki
Browse files
book store commands
parent
67538ddf
Changes
3
Hide whitespace changes
Inline
Side-by-side
bookstore/src/main/java/BookCommands.java
0 → 100644
View file @
16f81e7e
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"
);
}
}
bookstore/src/main/java/BookStore.java
View file @
16f81e7e
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
())
...
...
bookstore/src/main/java/DemoCommandsExecutor.java
View file @
16f81e7e
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
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment