Makes use of lambda syntax in more of the javadocs.

This commit is contained in:
John Brownlee 2018-03-15 13:04:28 -07:00
parent ab8028383d
commit 198d6682e9
8 changed files with 171 additions and 222 deletions

View File

@ -179,17 +179,15 @@ Heres a basic implementation of the recipe.
}
public static Object insertDoc(TransactionContext tcx, final Map<Object,Object> doc){
return tcx.run(new Function<Transaction,Object>() {
public Object apply(Transaction tr){
if(!doc.containsKey("doc_id")){
doc.put("doc_id", getNewID(tr));
}
for(Tuple t : toTuples(doc)){
tr.set(docSpace.pack(Tuple.from(doc.get("doc_id")).addAll(t.popBack())),
Tuple.from(t.get(t.size() - 1)).pack());
}
return doc.get("doc_id");
return tcx.run(tr -> {
if(!doc.containsKey("doc_id")){
doc.put("doc_id", getNewID(tr));
}
for(Tuple t : toTuples(doc)){
tr.set(docSpace.pack(Tuple.from(doc.get("doc_id")).addAll(t.popBack())),
Tuple.from(t.get(t.size() - 1)).pack());
}
return doc.get("doc_id");
});
}
@ -198,43 +196,38 @@ Heres a basic implementation of the recipe.
}
public static Object getDoc(TransactionContext tcx, final Object ID, final Tuple prefix){
return tcx.run(new Function<Transaction,Object>() {
public Object apply(Transaction tr){
Future<byte[]> v = tr.get(docSpace.pack(Tuple.from(ID).addAll(prefix)));
if(v.get() != null){
// One single item.
ArrayList<Tuple> vals = new ArrayList<Tuple>();
vals.add(prefix.addAll(Tuple.fromBytes(v.get())));
return fromTuples(vals);
} else {
// Multiple items.
ArrayList<Tuple> vals = new ArrayList<Tuple>();
for(KeyValue kv : tr.getRange(docSpace.range(Tuple.from(ID).addAll(prefix)))){
vals.add(docSpace.unpack(kv.getKey()).popFront().addAll(Tuple.fromBytes(kv.getValue())));
}
return fromTuples(vals);
return tcx.run(tr -> {
Future<byte[]> v = tr.get(docSpace.pack(Tuple.from(ID).addAll(prefix)));
if(v.get() != null){
// One single item.
ArrayList<Tuple> vals = new ArrayList<Tuple>();
vals.add(prefix.addAll(Tuple.fromBytes(v.get())));
return fromTuples(vals);
} else {
// Multiple items.
ArrayList<Tuple> vals = new ArrayList<Tuple>();
for(KeyValue kv : tr.getRange(docSpace.range(Tuple.from(ID).addAll(prefix)))){
vals.add(docSpace.unpack(kv.getKey()).popFront().addAll(Tuple.fromBytes(kv.getValue())));
}
return fromTuples(vals);
}
});
}
private static int getNewID(TransactionContext tcx){
return tcx.run(new Function<Transaction,Integer>() {
@SuppressWarnings("unused")
public Integer apply(Transaction tr){
boolean found = false;
int newID;
do {
newID = (int)(Math.random()*100000000);
found = true;
for(KeyValue kv : tr.getRange(docSpace.range(Tuple.from(newID)))){
// If not empty, this is false.
found = false;
break;
}
} while(!found);
return newID;
}
return tcx.run(tr -> {
boolean found = false;
int newID;
do {
newID = (int)(Math.random()*100000000);
found = true;
for(KeyValue kv : tr.getRange(docSpace.range(Tuple.from(newID)))){
// If not empty, this is false.
found = false;
break;
}
} while(!found);
return newID;
});
}
}

View File

@ -80,16 +80,14 @@ Heres a simple implementation of multimaps with multisets as described:
}
private static void addHelp(TransactionContext tcx, final byte[] key, final long amount){
tcx.run(new Function<Transaction,Void>() {
public Void apply(Transaction tr){
ByteBuffer b = ByteBuffer.allocate(8);
b.order(ByteOrder.LITTLE_ENDIAN);
b.putLong(amount);
tr.mutate(MutationType.ADD, key, b.array());
return null;
}
tcx.run(tr -> {
ByteBuffer b = ByteBuffer.allocate(8);
b.order(ByteOrder.LITTLE_ENDIAN);
b.putLong(amount);
tr.mutate(MutationType.ADD, key, b.array());
return null;
});
}
@ -102,66 +100,56 @@ Heres a simple implementation of multimaps with multisets as described:
public static void add(TransactionContext tcx, final String index,
final Object value){
tcx.run(new Function<Transaction,Void>() {
public Void apply(Transaction tr){
addHelp(tr, multi.subspace(Tuple.from(index,value)).getKey(),1l);
return null;
}
tcx.run(tr -> {
addHelp(tr, multi.subspace(Tuple.from(index,value)).getKey(),1l);
return null;
});
}
public static void subtract(TransactionContext tcx, final String index,
final Object value){
tcx.run(new Function<Transaction,Void>() {
public Void apply(Transaction tr){
Future<byte[]> v = tr.get(multi.subspace(
Tuple.from(index,value)).getKey());
if(v.get() != null && getLong(v.get()) > 1l){
addHelp(tr, multi.subspace(Tuple.from(index,value)).getKey(), -1l);
} else {
tr.clear(multi.subspace(Tuple.from(index,value)).getKey());
}
return null;
tcx.run(tr -> {
Future<byte[]> v = tr.get(multi.subspace(
Tuple.from(index,value)).getKey());
if(v.get() != null && getLong(v.get()) > 1l){
addHelp(tr, multi.subspace(Tuple.from(index,value)).getKey(), -1l);
} else {
tr.clear(multi.subspace(Tuple.from(index,value)).getKey());
}
return null;
});
}
public static ArrayList<Object> get(TransactionContext tcx, final String index){
return tcx.run(new Function<Transaction,ArrayList<Object> >() {
public ArrayList<Object> apply(Transaction tr){
ArrayList<Object> vals = new ArrayList<Object>();
for(KeyValue kv : tr.getRange(multi.subspace(
Tuple.from(index)).range())){
vals.add(multi.unpack(kv.getKey()).get(1));
}
return vals;
return tcx.run(tr -> {
ArrayList<Object> vals = new ArrayList<Object>();
for(KeyValue kv : tr.getRange(multi.subspace(
Tuple.from(index)).range())){
vals.add(multi.unpack(kv.getKey()).get(1));
}
return vals;
});
}
public static HashMap<Object,Long> getCounts(TransactionContext tcx,
final String index){
return tcx.run(new Function<Transaction,HashMap<Object,Long> >() {
public HashMap<Object,Long> apply(Transaction tr){
HashMap<Object,Long> vals = new HashMap<Object,Long>();
for(KeyValue kv : tr.getRange(multi.subspace(
Tuple.from(index)).range())){
vals.put(multi.unpack(kv.getKey()).get(1),
getLong(kv.getValue()));
}
return vals;
return tcx.run(tr -> {
HashMap<Object,Long> vals = new HashMap<Object,Long>();
for(KeyValue kv : tr.getRange(multi.subspace(
Tuple.from(index)).range())){
vals.put(multi.unpack(kv.getKey()).get(1),
getLong(kv.getValue()));
}
return vals;
});
}
public static boolean isElement(TransactionContext tcx, final String index,
final Object value){
return tcx.run(new Function<Transaction,Boolean>() {
public Boolean apply(Transaction tr){
return tr.get(multi.subspace(
Tuple.from(index, value)).getKey()).get() != null;
}
return tcx.run(tr -> {
return tr.get(multi.subspace(
Tuple.from(index, value)).getKey()).get() != null;
});
}
}

View File

@ -56,32 +56,30 @@ Heres a basic function that successively reads sub-ranges of a size determine
.. code-block:: java
public static void getRangeLimited(TransactionContext tcx, final KeySelector begin, final KeySelector end){
tcx.run(new Function<Transaction,Void>() {
public Void apply(Transaction tr){
boolean keysToCheck = true;
ArrayList<Tuple> keysFound = new ArrayList<Tuple>();
KeySelector n_begin = new KeySelector(begin.getKey(),true,begin.getOffset());
while(keysToCheck){
keysToCheck = false;
for(KeyValue kv : tr.getRange(n_begin, end, LIMIT)){
keysToCheck = true;
Tuple t = Tuple.fromBytes(kv.getKey());
if(keysFound.size() == 0
|| !t.equals(keysFound.get(keysFound.size()-1))){
keysFound.add(t);
}
}
if(keysToCheck){
n_begin = KeySelector.firstGreaterThan(keysFound.get(keysFound.size()-1).pack());
ArrayList<Object> readableFound = new ArrayList<Object>();
for(Tuple t : keysFound){
readableFound.add(t.get(1));
}
System.out.println(readableFound);
keysFound = new ArrayList<Tuple>();
tcx.run(tr -> {
boolean keysToCheck = true;
ArrayList<Tuple> keysFound = new ArrayList<Tuple>();
KeySelector n_begin = new KeySelector(begin.getKey(),true,begin.getOffset());
while(keysToCheck){
keysToCheck = false;
for(KeyValue kv : tr.getRange(n_begin, end, LIMIT)){
keysToCheck = true;
Tuple t = Tuple.fromBytes(kv.getKey());
if(keysFound.size() == 0
|| !t.equals(keysFound.get(keysFound.size()-1))){
keysFound.add(t);
}
}
return null;
if(keysToCheck){
n_begin = KeySelector.firstGreaterThan(keysFound.get(keysFound.size()-1).pack());
ArrayList<Object> readableFound = new ArrayList<Object>();
for(Tuple t : keysFound){
readableFound.add(t.get(1));
}
System.out.println(readableFound);
keysFound = new ArrayList<Tuple>();
}
}
return null;
});
}

View File

@ -96,38 +96,32 @@ In this example, were storing user data based on user ID but sometimes need t
// TODO These three methods (setUser, getUser, and getUserIDsInRegion)
// are all in the recipe book.
public static void setUser(TransactionContext tcx, final String ID, final String name, final String zipcode){
tcx.run(new Function<Transaction,Void>() {
public Void apply(Transaction tr){
tr.set(main.pack(Tuple.from(ID,zipcode)), Tuple.from(name).pack());
tr.set(index.pack(Tuple.from(zipcode,ID)), Tuple.from().pack());
return null;
}
tcx.run(tr ->
tr.set(main.pack(Tuple.from(ID,zipcode)), Tuple.from(name).pack());
tr.set(index.pack(Tuple.from(zipcode,ID)), Tuple.from().pack());
return null;
});
}
// Normal lookup.
public static String getUser(TransactionContext tcx, final String ID){
return tcx.run(new Function<Transaction,String>() {
public String apply(Transaction tr){
for(KeyValue kv : tr.getRange(main.subspace(Tuple.from(ID)).range(), 1)){
// Return user with correct ID (if exists).
return Tuple.fromBytes(kv.getValue()).getString(0);
}
return "";
return tcx.run(tr -> {
for(KeyValue kv : tr.getRange(main.subspace(Tuple.from(ID)).range(), 1)){
// Return user with correct ID (if exists).
return Tuple.fromBytes(kv.getValue()).getString(0);
}
return "";
});
}
// Index lookup.
public static ArrayList<String> getUserIDsInRegion(TransactionContext tcx, final String zipcode){
return tcx.run(new Function<Transaction,ArrayList<String>>() {
public ArrayList<String> apply(Transaction tr){
ArrayList<String> IDs = new ArrayList<String>();
for(KeyValue kv : tr.getRange(index.subspace(Tuple.from(zipcode)).range())){
IDs.add(index.unpack(kv.getKey()).getString(1));
}
return IDs;
return tcx.run(tr -> {
ArrayList<String> IDs = new ArrayList<String>();
for(KeyValue kv : tr.getRange(index.subspace(Tuple.from(zipcode)).range())){
IDs.add(index.unpack(kv.getKey()).getString(1));
}
return IDs;
});
}
}

View File

@ -55,19 +55,17 @@ The spatial index will use a pair of subspaces: one, ``z_label``, to give us eff
.. code-block:: java
public void setLocation(TransactionContext tcx, final String label, final long[] pos){
tcx.run(new Function<Transaction,Void>() {
public Void apply(Transaction tr){
long z = xyToZ(pos);
long previous;
// Read labelZ.subspace(Tuple.from(label)) to find previous z.
if(/* there is a previous z */){
tr.clear(labelZ.pack(Tuple.from(label,previous)));
tr.clear(zLabel.pack(Tuple.from(previous,label)));
}
tr.set(labelZ.pack(Tuple.from(label,z)),Tuple.from().pack());
tr.set(zLabel.pack(Tuple.from(z,label)),Tuple.from().pack());
return null;
tcx.run(tr -> {
long z = xyToZ(pos);
long previous;
// Read labelZ.subspace(Tuple.from(label)) to find previous z.
if(/* there is a previous z */){
tr.clear(labelZ.pack(Tuple.from(label,previous)));
tr.clear(zLabel.pack(Tuple.from(previous,label)));
}
tr.set(labelZ.pack(Tuple.from(label,z)),Tuple.from().pack());
tr.set(zLabel.pack(Tuple.from(z,label)),Tuple.from().pack());
return null;
});
}

View File

@ -45,12 +45,10 @@ The client performs transactions on data in the subspace current in the usual ma
final DirectorySubspace newspace = workspace.getNew().get();
try {
clearSubspace(db, newspace);
db.run(new Function<Transaction,Void>() {
public Void apply(Transaction tr){
tr.set(newspace.pack(Tuple.from(3)),Tuple.from("c").pack());
tr.set(newspace.pack(Tuple.from(4)), Tuple.from("d").pack());
return null;
}
db.run(tr -> {
tr.set(newspace.pack(Tuple.from(3)),Tuple.from("c").pack());
tr.set(newspace.pack(Tuple.from(4)), Tuple.from("d").pack());
return null;
});
} finally {
// Asynchronous operation--wait until result is reached.
@ -87,16 +85,12 @@ Here's a simple Workspace class for swapping in a new workspace supporting the b
return dir.createOrOpen(this.db, PathUtil.from("new"));
}
public Future<DirectorySubspace> replaceWithNew() {
return this.db.runAsync(new Function<Transaction,Future<DirectorySubspace>>() {
public Future<DirectorySubspace> apply(final Transaction tr){
return dir.remove(tr, PathUtil.from("current")) // Clear the old current.
.flatMap(new Function<Void,Future<DirectorySubspace>>() {
public Future<DirectorySubspace> apply(Void arg0) {
// Replace the old directory with the new one.
return dir.move(tr, PathUtil.from("new"), PathUtil.from("current"));
}
});
}
return this.db.runAsync(tr -> {
return dir.remove(tr, PathUtil.from("current")) // Clear the old current.
.flatMap(() -> {
// Replace the old directory with the new one.
return dir.move(tr, PathUtil.from("new"), PathUtil.from("current"));
});
});
}
}

View File

@ -80,87 +80,75 @@ Heres a simple implementation of the basic table pattern:
public static void setCell(TransactionContext tcx, final String row,
final String column, final Object value){
tcx.run(new Function<Transaction, Void>() {
public Void apply(Transaction tr){
tr.set(rowIndex.subspace(Tuple.from(row, column)).getKey(),
pack(value));
tr.set(colIndex.subspace(Tuple.from(column,row)).getKey(),
pack(value));
return null;
}
tcx.run(tr -> {
tr.set(rowIndex.subspace(Tuple.from(row, column)).getKey(),
pack(value));
tr.set(colIndex.subspace(Tuple.from(column,row)).getKey(),
pack(value));
return null;
});
}
public static Object getCell(TransactionContext tcx, final String row,
final String column){
return tcx.run(new Function<Transaction, Object>() {
public Object apply(Transaction tr){
return unpack(tr.get(rowIndex.subspace(
Tuple.from(row,column)).getKey()).get());
}
return tcx.run(tr -> {
return unpack(tr.get(rowIndex.subspace(
Tuple.from(row,column)).getKey()).get());
});
}
public static void setRow(TransactionContext tcx, final String row,
final Map<String,Object> cols){
tcx.run(new Function<Transaction, Void>() {
public Void apply(Transaction tr){
tr.clear(rowIndex.subspace(Tuple.from(row)).range());
for(Map.Entry<String,Object> cv : cols.entrySet()){
setCell(tr, row, cv.getKey(), cv.getValue());
}
return null;
tcx.run(tr -> {
tr.clear(rowIndex.subspace(Tuple.from(row)).range());
for(Map.Entry<String,Object> cv : cols.entrySet()){
setCell(tr, row, cv.getKey(), cv.getValue());
}
return null;
});
}
public static void setColumn(TransactionContext tcx, final String column,
final Map<String,Object> rows){
tcx.run(new Function<Transaction,Void>() {
public Void apply(Transaction tr){
tr.clear(colIndex.subspace(Tuple.from(column)).range());
for(Map.Entry<String,Object> rv : rows.entrySet()){
setCell(tr, rv.getKey(), column, rv.getValue());
}
return null;
tcx.run(tr -> {
tr.clear(colIndex.subspace(Tuple.from(column)).range());
for(Map.Entry<String,Object> rv : rows.entrySet()){
setCell(tr, rv.getKey(), column, rv.getValue());
}
return null;
});
}
public static TreeMap<String,Object> getRow(TransactionContext tcx,
final String row){
return tcx.run(new Function<Transaction,TreeMap<String,Object> >() {
public TreeMap<String,Object> apply(Transaction tr){
TreeMap<String,Object> cols = new TreeMap<String,Object>();
for(KeyValue kv : tr.getRange(
rowIndex.subspace(Tuple.from(row)).range())){
cols.put(rowIndex.unpack(kv.getKey()).getString(1),
unpack(kv.getValue()));
}
return cols;
return tcx.run(tr -> {
TreeMap<String,Object> cols = new TreeMap<String,Object>();
for(KeyValue kv : tr.getRange(
rowIndex.subspace(Tuple.from(row)).range())){
cols.put(rowIndex.unpack(kv.getKey()).getString(1),
unpack(kv.getValue()));
}
return cols;
});
}
public static TreeMap<String,Object> getColumn(TransactionContext tcx,
final String column){
return tcx.run(new Function<Transaction,TreeMap<String,Object> >() {
public TreeMap<String,Object> apply(Transaction tr){
TreeMap<String,Object> rows = new TreeMap<String,Object>();
for(KeyValue kv : tr.getRange(
colIndex.subspace(Tuple.from(column)).range())){
rows.put(colIndex.unpack(kv.getKey()).getString(1),
unpack(kv.getValue()));
}
return rows;
return tcx.run(tr -> {
TreeMap<String,Object> rows = new TreeMap<String,Object>();
for(KeyValue kv : tr.getRange(
colIndex.subspace(Tuple.from(column)).range())){
rows.put(colIndex.unpack(kv.getKey()).getString(1),
unpack(kv.getValue()));
}
return rows;
});
}
}

View File

@ -83,20 +83,16 @@ Heres the basic pattern:
}
public static Object get(TransactionContext tcx, final long index){
return tcx.run(new Function<Transaction,Object>() {
public Object apply(Transaction tr){
return Tuple.fromBytes(tr.get(vector.pack(
Tuple.from(index))).get()).get(0);
}
return tcx.run(tr -> {
return Tuple.fromBytes(tr.get(vector.pack(
Tuple.from(index))).get()).get(0);
});
}
public static void set(TransactionContext tcx, final long index, final Object value){
tcx.run(new Function<Transaction,Void>() {
public Void apply(Transaction tr){
tr.set(vector.pack(Tuple.from(index)), Tuple.from(value).pack());
return null;
}
tcx.run(tr -> {
tr.set(vector.pack(Tuple.from(index)), Tuple.from(value).pack());
return null;
});
}
}