From 198d6682e954b94d7f4d72b222753f4bce667ed8 Mon Sep 17 00:00:00 2001 From: John Brownlee Date: Thu, 15 Mar 2018 13:04:28 -0700 Subject: [PATCH] Makes use of lambda syntax in more of the javadocs. --- .../source/hierarchical-documents-java.rst | 75 +++++++--------- .../sphinx/source/multimaps-java.rst | 84 ++++++++--------- .../source/segmented-range-reads-java.rst | 46 +++++----- .../sphinx/source/simple-indexes-java.rst | 34 +++---- .../sphinx/source/spatial-indexing-java.rst | 22 +++-- .../source/subspace-indirection-java.rst | 26 +++--- documentation/sphinx/source/tables-java.rst | 90 ++++++++----------- documentation/sphinx/source/vector-java.rst | 16 ++-- 8 files changed, 171 insertions(+), 222 deletions(-) diff --git a/documentation/sphinx/source/hierarchical-documents-java.rst b/documentation/sphinx/source/hierarchical-documents-java.rst index 780cba579d..78dfd05ad4 100644 --- a/documentation/sphinx/source/hierarchical-documents-java.rst +++ b/documentation/sphinx/source/hierarchical-documents-java.rst @@ -179,17 +179,15 @@ Here’s a basic implementation of the recipe. } public static Object insertDoc(TransactionContext tcx, final Map doc){ - return tcx.run(new Function() { - 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 @@ Here’s a basic implementation of the recipe. } public static Object getDoc(TransactionContext tcx, final Object ID, final Tuple prefix){ - return tcx.run(new Function() { - public Object apply(Transaction tr){ - Future v = tr.get(docSpace.pack(Tuple.from(ID).addAll(prefix))); - if(v.get() != null){ - // One single item. - ArrayList vals = new ArrayList(); - vals.add(prefix.addAll(Tuple.fromBytes(v.get()))); - return fromTuples(vals); - } else { - // Multiple items. - ArrayList vals = new ArrayList(); - 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 v = tr.get(docSpace.pack(Tuple.from(ID).addAll(prefix))); + if(v.get() != null){ + // One single item. + ArrayList vals = new ArrayList(); + vals.add(prefix.addAll(Tuple.fromBytes(v.get()))); + return fromTuples(vals); + } else { + // Multiple items. + ArrayList vals = new ArrayList(); + 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() { - @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; }); } } diff --git a/documentation/sphinx/source/multimaps-java.rst b/documentation/sphinx/source/multimaps-java.rst index 2bc2078335..6de36c2364 100644 --- a/documentation/sphinx/source/multimaps-java.rst +++ b/documentation/sphinx/source/multimaps-java.rst @@ -80,16 +80,14 @@ Here’s 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() { - 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 @@ Here’s 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() { - 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() { - public Void apply(Transaction tr){ - Future 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 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 get(TransactionContext tcx, final String index){ - return tcx.run(new Function >() { - public ArrayList apply(Transaction tr){ - ArrayList vals = new ArrayList(); - 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 vals = new ArrayList(); + for(KeyValue kv : tr.getRange(multi.subspace( + Tuple.from(index)).range())){ + vals.add(multi.unpack(kv.getKey()).get(1)); } + return vals; }); } public static HashMap getCounts(TransactionContext tcx, final String index){ - return tcx.run(new Function >() { - public HashMap apply(Transaction tr){ - HashMap vals = new HashMap(); - 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 vals = new HashMap(); + 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() { - 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; }); } } diff --git a/documentation/sphinx/source/segmented-range-reads-java.rst b/documentation/sphinx/source/segmented-range-reads-java.rst index 6561c84ef0..ff73fe720a 100644 --- a/documentation/sphinx/source/segmented-range-reads-java.rst +++ b/documentation/sphinx/source/segmented-range-reads-java.rst @@ -56,32 +56,30 @@ Here’s 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() { - public Void apply(Transaction tr){ - boolean keysToCheck = true; - ArrayList keysFound = new ArrayList(); - 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 readableFound = new ArrayList(); - for(Tuple t : keysFound){ - readableFound.add(t.get(1)); - } - System.out.println(readableFound); - keysFound = new ArrayList(); + tcx.run(tr -> { + boolean keysToCheck = true; + ArrayList keysFound = new ArrayList(); + 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 readableFound = new ArrayList(); + for(Tuple t : keysFound){ + readableFound.add(t.get(1)); + } + System.out.println(readableFound); + keysFound = new ArrayList(); + } } + return null; }); } diff --git a/documentation/sphinx/source/simple-indexes-java.rst b/documentation/sphinx/source/simple-indexes-java.rst index 58566fb731..5859003be8 100644 --- a/documentation/sphinx/source/simple-indexes-java.rst +++ b/documentation/sphinx/source/simple-indexes-java.rst @@ -96,38 +96,32 @@ In this example, we’re 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() { - 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() { - 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 getUserIDsInRegion(TransactionContext tcx, final String zipcode){ - return tcx.run(new Function>() { - public ArrayList apply(Transaction tr){ - ArrayList IDs = new ArrayList(); - 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 IDs = new ArrayList(); + for(KeyValue kv : tr.getRange(index.subspace(Tuple.from(zipcode)).range())){ + IDs.add(index.unpack(kv.getKey()).getString(1)); } + return IDs; }); } } diff --git a/documentation/sphinx/source/spatial-indexing-java.rst b/documentation/sphinx/source/spatial-indexing-java.rst index 9b31139c48..939018057b 100644 --- a/documentation/sphinx/source/spatial-indexing-java.rst +++ b/documentation/sphinx/source/spatial-indexing-java.rst @@ -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() { - 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; }); } diff --git a/documentation/sphinx/source/subspace-indirection-java.rst b/documentation/sphinx/source/subspace-indirection-java.rst index 6fb49768c7..b31c2cbd12 100644 --- a/documentation/sphinx/source/subspace-indirection-java.rst +++ b/documentation/sphinx/source/subspace-indirection-java.rst @@ -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() { - 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 replaceWithNew() { - return this.db.runAsync(new Function>() { - public Future apply(final Transaction tr){ - return dir.remove(tr, PathUtil.from("current")) // Clear the old current. - .flatMap(new Function>() { - public Future 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")); + }); }); } } diff --git a/documentation/sphinx/source/tables-java.rst b/documentation/sphinx/source/tables-java.rst index 6604a2da3b..d1d8774bb4 100644 --- a/documentation/sphinx/source/tables-java.rst +++ b/documentation/sphinx/source/tables-java.rst @@ -80,87 +80,75 @@ Here’s 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() { - 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() { - 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 cols){ - tcx.run(new Function() { - public Void apply(Transaction tr){ - tr.clear(rowIndex.subspace(Tuple.from(row)).range()); - - for(Map.Entry 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 cv : cols.entrySet()){ + setCell(tr, row, cv.getKey(), cv.getValue()); } + return null; }); } public static void setColumn(TransactionContext tcx, final String column, final Map rows){ - tcx.run(new Function() { - public Void apply(Transaction tr){ - tr.clear(colIndex.subspace(Tuple.from(column)).range()); - for(Map.Entry 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 rv : rows.entrySet()){ + setCell(tr, rv.getKey(), column, rv.getValue()); } + return null; }); } public static TreeMap getRow(TransactionContext tcx, final String row){ - return tcx.run(new Function >() { - public TreeMap apply(Transaction tr){ - TreeMap cols = new TreeMap(); - - 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 cols = new TreeMap(); + + 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 getColumn(TransactionContext tcx, final String column){ - return tcx.run(new Function >() { - public TreeMap apply(Transaction tr){ - TreeMap rows = new TreeMap(); - - 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 rows = new TreeMap(); + + for(KeyValue kv : tr.getRange( + colIndex.subspace(Tuple.from(column)).range())){ + rows.put(colIndex.unpack(kv.getKey()).getString(1), + unpack(kv.getValue())); } + + return rows; }); } } diff --git a/documentation/sphinx/source/vector-java.rst b/documentation/sphinx/source/vector-java.rst index 088d986e82..075af3ba81 100644 --- a/documentation/sphinx/source/vector-java.rst +++ b/documentation/sphinx/source/vector-java.rst @@ -83,20 +83,16 @@ Here’s the basic pattern: } public static Object get(TransactionContext tcx, final long index){ - return tcx.run(new Function() { - 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() { - 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; }); } }