summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFranklin Wei <git@fwei.tk>2019-07-16 15:59:16 -0400
committerFranklin Wei <git@fwei.tk>2019-07-16 15:59:16 -0400
commitde2cd3c7afe244b1439944289e6adb1fbc22b269 (patch)
treeaadf6ef9cb32cae00a0e82cdb721235c05f59fd8
parent56b26148d09574429c232bd9c31738fe7fdbfbef (diff)
downloaddcr-de2cd3c7afe244b1439944289e6adb1fbc22b269.zip
dcr-de2cd3c7afe244b1439944289e6adb1fbc22b269.tar.gz
dcr-de2cd3c7afe244b1439944289e6adb1fbc22b269.tar.bz2
dcr-de2cd3c7afe244b1439944289e6adb1fbc22b269.tar.xz
iomt: Work on encloser query. Almost complete.
Implemented the main condition of enclosure (not tested).
-rw-r--r--java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/DCRChaincode.java2
-rw-r--r--java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/FabricIOMT.java120
2 files changed, 101 insertions, 21 deletions
diff --git a/java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/DCRChaincode.java b/java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/DCRChaincode.java
index ef3f0cc..de0ebad 100644
--- a/java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/DCRChaincode.java
+++ b/java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/DCRChaincode.java
@@ -153,7 +153,7 @@ public class DCRChaincode extends ChaincodeBase {
try {
FabricIOMT iomt = new FabricIOMT(chaincodeStub, name);
- return newSuccessResponse("found IOMT " + name + " with height=" + iomt.getHeight());
+ return newSuccessResponse("found IOMT " + name + " with JSON " + iomt.toJSON().toString());
} catch(Exception e) {
return newErrorResponse(e.getMessage());
}
diff --git a/java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/FabricIOMT.java b/java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/FabricIOMT.java
index e38d256..f58504e 100644
--- a/java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/FabricIOMT.java
+++ b/java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/FabricIOMT.java
@@ -5,6 +5,7 @@ import org.hyperledger.fabric.shim.ChaincodeStub;
import org.hyperledger.fabric.shim.ledger.CompositeKey;
import org.hyperledger.fabric.shim.ledger.KeyValue;
import org.hyperledger.fabric.shim.ledger.QueryResultsIterator;
+import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Iterator;
@@ -18,6 +19,10 @@ import java.util.Iterator;
public class FabricIOMT extends IOMT {
private String name;
+
+ // we keep track of these for fast encloser query
+ private int minVirtIdx, maxVirtIdx;
+
private ChaincodeStub chaincodeStub;
/**
@@ -40,6 +45,10 @@ public class FabricIOMT extends IOMT {
if (json.isEmpty()) {
height = pref_height;
+ // no leaves to start
+ minVirtIdx = Integer.MAX_VALUE;
+ maxVirtIdx = Integer.MIN_VALUE;
+
/* Doesn't exist. Insert new into state. */
JSONObject jsonObject = toJSON();
@@ -47,7 +56,9 @@ public class FabricIOMT extends IOMT {
} else {
JSONObject jso = new JSONObject(json);
- height = Integer.valueOf(jso.getString("height"));
+ minVirtIdx = jso.getInt("minIdx");
+ maxVirtIdx = jso.getInt("maxIdx");
+ height = jso.getInt("height");
assert (jso.getString("name").equals(iomt_name));
}
}
@@ -135,7 +146,10 @@ public class FabricIOMT extends IOMT {
JSONObject obj = new JSONObject();
try {
- obj.put("height", Integer.toString(height));
+ obj.put("height", height);
+ obj.put("minIdx", minVirtIdx);
+ obj.put("maxIdx", maxVirtIdx);
+
obj.put("name", name);
} catch (JSONException e) {
throw new RuntimeException("toJSON");
@@ -254,27 +268,45 @@ public class FabricIOMT extends IOMT {
chaincodeStub.putStringState(buildLeafKey(leafId).toString(),
leaf.toString());
- }
-
- public ImmutablePair<IOMTLeaf, Integer> getLeafAndPhysIdxVirtual(int virtIdx) {
- // TODO: couchdb query
- // build query string (see CouchDB docs)
+ // update min/max if necessary
+ boolean updateHeader = false;
+ int idx = val.getIdx();
- JSONObject jsonQuery = new JSONObject();
- JSONObject selector = new JSONObject();
- QueryResultsIterator results;
- try {
- selector.put("docType", "iomtleaf");
- selector.put("parentIOMT", this.name);
- selector.put("idx", virtIdx);
+ // first leaf inserted
+ if(maxVirtIdx == Integer.MIN_VALUE && minVirtIdx == Integer.MAX_VALUE)
+ {
+ minVirtIdx = maxVirtIdx = idx;
+ updateHeader = true;
+ }
+ else if(idx < minVirtIdx)
+ {
+ minVirtIdx = idx;
+ updateHeader = true;
+ }
+ else if(idx > maxVirtIdx)
+ {
+ maxVirtIdx = idx;
+ updateHeader = true;
+ }
- jsonQuery.put("selector", selector);
- results = chaincodeStub.getQueryResult(jsonQuery.toString());
- } catch (JSONException e) {
- throw new RuntimeException("json error");
+ if(updateHeader) {
+ try {
+ chaincodeStub.putStringState(buildTreeKey().toString(), toJSON().toString());
+ } catch (Exception e) {
+ throw new RuntimeException(e.getMessage());
+ }
}
+ }
+ /**
+ * Extract a JSON IOMT leaf and its physical index from a QueryResultsIterator. Will only
+ * process the first leaf pointed to by the iterator.
+ *
+ * @param results iterator
+ * @return pair of IOMT leaf and physical index
+ */
+ private ImmutablePair<IOMTLeaf, Integer> extractLeafFromQueryResults(QueryResultsIterator results) {
Iterator<KeyValue> iterator = results.iterator();
if (!iterator.hasNext())
@@ -295,8 +327,56 @@ public class FabricIOMT extends IOMT {
return new ImmutablePair<IOMTLeaf, Integer>(leaf, physIdx);
}
- public ImmutablePair<IOMTLeaf, Integer> getEncloser(int virtIdx) {
- return new ImmutablePair(new IOMTLeaf(), 0);
+ public ImmutablePair<IOMTLeaf, Integer> getLeafAndPhysIdxVirtual(int virtIdx) {
+ // build query string (see CouchDB docs)
+
+ JSONObject jsonQuery = new JSONObject();
+ JSONObject selector = new JSONObject();
+ QueryResultsIterator results;
+ try {
+ selector.put("docType", "iomtleaf");
+ selector.put("parentIOMT", this.name);
+ selector.put("idx", virtIdx);
+
+ jsonQuery.put("selector", selector);
+ results = chaincodeStub.getQueryResult(jsonQuery.toString());
+ } catch (JSONException e) {
+ throw new RuntimeException("json error");
+ }
+
+ return extractLeafFromQueryResults(results);
+ }
+
+ public ImmutablePair<IOMTLeaf, Integer> getEncloser(int virtIdx)
+ {
+ // first the low-hanging fruit: a < i_min or a > i_max
+ // TODO
+
+ // see encloser-query.json for where this comes from
+ JSONObject jsonQuery = new JSONObject();
+ JSONObject selector = new JSONObject();
+ QueryResultsIterator results;
+
+ try {
+ /* most common case: b < a < b' */
+ selector.put("docType", "iomtleaf");
+ selector.put("parentIOMT", this.name);
+
+ JSONArray conditions = new JSONArray();
+ conditions.put(new JSONObject()
+ .put("nextIdx", new JSONObject()
+ .put("$gt", virtIdx))
+ .put("idx", new JSONObject()
+ .put("$lt", virtIdx)));
+ selector.put("$and", conditions);
+
+ jsonQuery.put("selector", selector);
+ results = chaincodeStub.getQueryResult(jsonQuery.toString());
+ } catch (Exception e) {
+ throw new IndexOutOfBoundsException("no encloser found");
+ }
+
+ return extractLeafFromQueryResults(results);
}
public ImmutablePair<IOMTLeaf, Integer> getLeafOrEncloser(int virtIdx) {