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,8 +179,7 @@ Heres a basic implementation of the recipe.
} }
public static Object insertDoc(TransactionContext tcx, final Map<Object,Object> doc){ public static Object insertDoc(TransactionContext tcx, final Map<Object,Object> doc){
return tcx.run(new Function<Transaction,Object>() { return tcx.run(tr -> {
public Object apply(Transaction tr){
if(!doc.containsKey("doc_id")){ if(!doc.containsKey("doc_id")){
doc.put("doc_id", getNewID(tr)); doc.put("doc_id", getNewID(tr));
} }
@ -189,7 +188,6 @@ Heres a basic implementation of the recipe.
Tuple.from(t.get(t.size() - 1)).pack()); Tuple.from(t.get(t.size() - 1)).pack());
} }
return doc.get("doc_id"); return doc.get("doc_id");
}
}); });
} }
@ -198,8 +196,7 @@ Heres a basic implementation of the recipe.
} }
public static Object getDoc(TransactionContext tcx, final Object ID, final Tuple prefix){ public static Object getDoc(TransactionContext tcx, final Object ID, final Tuple prefix){
return tcx.run(new Function<Transaction,Object>() { return tcx.run(tr -> {
public Object apply(Transaction tr){
Future<byte[]> v = tr.get(docSpace.pack(Tuple.from(ID).addAll(prefix))); Future<byte[]> v = tr.get(docSpace.pack(Tuple.from(ID).addAll(prefix)));
if(v.get() != null){ if(v.get() != null){
// One single item. // One single item.
@ -214,14 +211,11 @@ Heres a basic implementation of the recipe.
} }
return fromTuples(vals); return fromTuples(vals);
} }
}
}); });
} }
private static int getNewID(TransactionContext tcx){ private static int getNewID(TransactionContext tcx){
return tcx.run(new Function<Transaction,Integer>() { return tcx.run(tr -> {
@SuppressWarnings("unused")
public Integer apply(Transaction tr){
boolean found = false; boolean found = false;
int newID; int newID;
do { do {
@ -234,7 +228,6 @@ Heres a basic implementation of the recipe.
} }
} while(!found); } while(!found);
return newID; return newID;
}
}); });
} }
} }

View File

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

View File

@ -56,8 +56,7 @@ Heres a basic function that successively reads sub-ranges of a size determine
.. code-block:: java .. code-block:: java
public static void getRangeLimited(TransactionContext tcx, final KeySelector begin, final KeySelector end){ public static void getRangeLimited(TransactionContext tcx, final KeySelector begin, final KeySelector end){
tcx.run(new Function<Transaction,Void>() { tcx.run(tr -> {
public Void apply(Transaction tr){
boolean keysToCheck = true; boolean keysToCheck = true;
ArrayList<Tuple> keysFound = new ArrayList<Tuple>(); ArrayList<Tuple> keysFound = new ArrayList<Tuple>();
KeySelector n_begin = new KeySelector(begin.getKey(),true,begin.getOffset()); KeySelector n_begin = new KeySelector(begin.getKey(),true,begin.getOffset());
@ -82,6 +81,5 @@ Heres a basic function that successively reads sub-ranges of a size determine
} }
} }
return null; 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) // TODO These three methods (setUser, getUser, and getUserIDsInRegion)
// are all in the recipe book. // are all in the recipe book.
public static void setUser(TransactionContext tcx, final String ID, final String name, final String zipcode){ public static void setUser(TransactionContext tcx, final String ID, final String name, final String zipcode){
tcx.run(new Function<Transaction,Void>() { tcx.run(tr ->
public Void apply(Transaction tr){
tr.set(main.pack(Tuple.from(ID,zipcode)), Tuple.from(name).pack()); tr.set(main.pack(Tuple.from(ID,zipcode)), Tuple.from(name).pack());
tr.set(index.pack(Tuple.from(zipcode,ID)), Tuple.from().pack()); tr.set(index.pack(Tuple.from(zipcode,ID)), Tuple.from().pack());
return null; return null;
}
}); });
} }
// Normal lookup. // Normal lookup.
public static String getUser(TransactionContext tcx, final String ID){ public static String getUser(TransactionContext tcx, final String ID){
return tcx.run(new Function<Transaction,String>() { return tcx.run(tr -> {
public String apply(Transaction tr){
for(KeyValue kv : tr.getRange(main.subspace(Tuple.from(ID)).range(), 1)){ for(KeyValue kv : tr.getRange(main.subspace(Tuple.from(ID)).range(), 1)){
// Return user with correct ID (if exists). // Return user with correct ID (if exists).
return Tuple.fromBytes(kv.getValue()).getString(0); return Tuple.fromBytes(kv.getValue()).getString(0);
} }
return ""; return "";
}
}); });
} }
// Index lookup. // Index lookup.
public static ArrayList<String> getUserIDsInRegion(TransactionContext tcx, final String zipcode){ public static ArrayList<String> getUserIDsInRegion(TransactionContext tcx, final String zipcode){
return tcx.run(new Function<Transaction,ArrayList<String>>() { return tcx.run(tr -> {
public ArrayList<String> apply(Transaction tr){
ArrayList<String> IDs = new ArrayList<String>(); ArrayList<String> IDs = new ArrayList<String>();
for(KeyValue kv : tr.getRange(index.subspace(Tuple.from(zipcode)).range())){ for(KeyValue kv : tr.getRange(index.subspace(Tuple.from(zipcode)).range())){
IDs.add(index.unpack(kv.getKey()).getString(1)); IDs.add(index.unpack(kv.getKey()).getString(1));
} }
return IDs; return IDs;
}
}); });
} }
} }

View File

@ -55,8 +55,7 @@ The spatial index will use a pair of subspaces: one, ``z_label``, to give us eff
.. code-block:: java .. code-block:: java
public void setLocation(TransactionContext tcx, final String label, final long[] pos){ public void setLocation(TransactionContext tcx, final String label, final long[] pos){
tcx.run(new Function<Transaction,Void>() { tcx.run(tr -> {
public Void apply(Transaction tr){
long z = xyToZ(pos); long z = xyToZ(pos);
long previous; long previous;
// Read labelZ.subspace(Tuple.from(label)) to find previous z. // Read labelZ.subspace(Tuple.from(label)) to find previous z.
@ -67,7 +66,6 @@ The spatial index will use a pair of subspaces: one, ``z_label``, to give us eff
tr.set(labelZ.pack(Tuple.from(label,z)),Tuple.from().pack()); tr.set(labelZ.pack(Tuple.from(label,z)),Tuple.from().pack());
tr.set(zLabel.pack(Tuple.from(z,label)),Tuple.from().pack()); tr.set(zLabel.pack(Tuple.from(z,label)),Tuple.from().pack());
return null; 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(); final DirectorySubspace newspace = workspace.getNew().get();
try { try {
clearSubspace(db, newspace); clearSubspace(db, newspace);
db.run(new Function<Transaction,Void>() { db.run(tr -> {
public Void apply(Transaction tr){
tr.set(newspace.pack(Tuple.from(3)),Tuple.from("c").pack()); tr.set(newspace.pack(Tuple.from(3)),Tuple.from("c").pack());
tr.set(newspace.pack(Tuple.from(4)), Tuple.from("d").pack()); tr.set(newspace.pack(Tuple.from(4)), Tuple.from("d").pack());
return null; return null;
}
}); });
} finally { } finally {
// Asynchronous operation--wait until result is reached. // 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")); return dir.createOrOpen(this.db, PathUtil.from("new"));
} }
public Future<DirectorySubspace> replaceWithNew() { public Future<DirectorySubspace> replaceWithNew() {
return this.db.runAsync(new Function<Transaction,Future<DirectorySubspace>>() { return this.db.runAsync(tr -> {
public Future<DirectorySubspace> apply(final Transaction tr){
return dir.remove(tr, PathUtil.from("current")) // Clear the old current. return dir.remove(tr, PathUtil.from("current")) // Clear the old current.
.flatMap(new Function<Void,Future<DirectorySubspace>>() { .flatMap(() -> {
public Future<DirectorySubspace> apply(Void arg0) {
// Replace the old directory with the new one. // Replace the old directory with the new one.
return dir.move(tr, PathUtil.from("new"), PathUtil.from("current")); return dir.move(tr, PathUtil.from("new"), PathUtil.from("current"));
}
}); });
}
}); });
} }
} }

View File

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

View File

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