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){ 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));
}
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");
} }
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){ 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. ArrayList<Tuple> vals = new ArrayList<Tuple>();
ArrayList<Tuple> vals = new ArrayList<Tuple>(); vals.add(prefix.addAll(Tuple.fromBytes(v.get())));
vals.add(prefix.addAll(Tuple.fromBytes(v.get()))); return fromTuples(vals);
return fromTuples(vals); } else {
} else { // Multiple items.
// Multiple items. ArrayList<Tuple> vals = new ArrayList<Tuple>();
ArrayList<Tuple> vals = new ArrayList<Tuple>(); for(KeyValue kv : tr.getRange(docSpace.range(Tuple.from(ID).addAll(prefix)))){
for(KeyValue kv : tr.getRange(docSpace.range(Tuple.from(ID).addAll(prefix)))){ vals.add(docSpace.unpack(kv.getKey()).popFront().addAll(Tuple.fromBytes(kv.getValue())));
vals.add(docSpace.unpack(kv.getKey()).popFront().addAll(Tuple.fromBytes(kv.getValue())));
}
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") boolean found = false;
public Integer apply(Transaction tr){ int newID;
boolean found = false; do {
int newID; newID = (int)(Math.random()*100000000);
do { found = true;
newID = (int)(Math.random()*100000000); for(KeyValue kv : tr.getRange(docSpace.range(Tuple.from(newID)))){
found = true; // If not empty, this is false.
for(KeyValue kv : tr.getRange(docSpace.range(Tuple.from(newID)))){ found = false;
// If not empty, this is false. break;
found = false; }
break; } while(!found);
} return newID;
} 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){ 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);
tr.mutate(MutationType.ADD, key, b.array());
tr.mutate(MutationType.ADD, key, b.array());
return null;
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, 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());
if(v.get() != null && getLong(v.get()) > 1l){
if(v.get() != null && getLong(v.get()) > 1l){ addHelp(tr, multi.subspace(Tuple.from(index,value)).getKey(), -1l);
addHelp(tr, multi.subspace(Tuple.from(index,value)).getKey(), -1l); } else {
} else { 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())){ vals.put(multi.unpack(kv.getKey()).get(1),
vals.put(multi.unpack(kv.getKey()).get(1), 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,32 +56,30 @@ 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()); while(keysToCheck){
while(keysToCheck){ keysToCheck = false;
keysToCheck = false; for(KeyValue kv : tr.getRange(n_begin, end, LIMIT)){
for(KeyValue kv : tr.getRange(n_begin, end, LIMIT)){ keysToCheck = true;
keysToCheck = true; Tuple t = Tuple.fromBytes(kv.getKey());
Tuple t = Tuple.fromBytes(kv.getKey()); if(keysFound.size() == 0
if(keysFound.size() == 0 || !t.equals(keysFound.get(keysFound.size()-1))){
|| !t.equals(keysFound.get(keysFound.size()-1))){ keysFound.add(t);
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>();
} }
} }
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) // 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,19 +55,17 @@ 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. if(/* there is a previous z */){
if(/* there is a previous z */){ tr.clear(labelZ.pack(Tuple.from(label,previous)));
tr.clear(labelZ.pack(Tuple.from(label,previous))); tr.clear(zLabel.pack(Tuple.from(previous,label)));
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;
} }
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(); 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(() -> {
.flatMap(new Function<Void,Future<DirectorySubspace>>() { // Replace the old directory with the new one.
public Future<DirectorySubspace> apply(Void arg0) { return dir.move(tr, PathUtil.from("new"), PathUtil.from("current"));
// 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, 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( rowIndex.subspace(Tuple.from(row)).range())){
rowIndex.subspace(Tuple.from(row)).range())){ cols.put(rowIndex.unpack(kv.getKey()).getString(1),
cols.put(rowIndex.unpack(kv.getKey()).getString(1), unpack(kv.getValue()));
unpack(kv.getValue()));
}
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( colIndex.subspace(Tuple.from(column)).range())){
colIndex.subspace(Tuple.from(column)).range())){ rows.put(colIndex.unpack(kv.getKey()).getString(1),
rows.put(colIndex.unpack(kv.getKey()).getString(1), unpack(kv.getValue()));
unpack(kv.getValue()));
}
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;
}
}); });
} }
} }