Skip to content

Commit

Permalink
[FAB-3305] java cc get query result
Browse files Browse the repository at this point in the history
Change-Id: If3544c57a1b9c7442284ff6d06bc862cdd916ed0
Signed-off-by: Luis Sanchez <sanchezl@us.ibm.com>
  • Loading branch information
Luis Sanchez committed Apr 26, 2017
1 parent 5858fbb commit 6ad95f6
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface ChaincodeStub {
* Returns the arguments corresponding to the call to
* {@link Chaincode#init(ChaincodeStub)} or
* {@link Chaincode#invoke(ChaincodeStub)}.
*
*
* @return a list of arguments
*/
List<byte[]> getArgs();
Expand All @@ -42,17 +42,17 @@ public interface ChaincodeStub {
* Returns the arguments corresponding to the call to
* {@link Chaincode#init(ChaincodeStub)} or
* {@link Chaincode#invoke(ChaincodeStub)}.
*
*
* @return a list of arguments cast to UTF-8 strings
*/
List<String> getStringArgs();

/**
* A convenience method that returns the first argument of the chaincode
* invocation for use as a function name.
*
*
* The bytes of the first argument are decoded as a UTF-8 string.
*
*
* @return the function name
*/
String getFunction();
Expand All @@ -61,10 +61,10 @@ public interface ChaincodeStub {
* A convenience method that returns all except the first argument of the
* chaincode invocation for use as the parameters to the function returned
* by #{@link ChaincodeStub#getFunction()}.
*
*
* The bytes of the arguments are decoded as a UTF-8 strings and returned as
* a list of string parameters..
*
*
* @return a list of parameters
*/
List<String> getParameters();
Expand All @@ -78,7 +78,7 @@ public interface ChaincodeStub {

/**
* Invoke another chaincode using the same transaction context.
*
*
* @param chaincodeName
* Name of chaincode to be invoked.
* @param args
Expand Down Expand Up @@ -120,7 +120,7 @@ public interface ChaincodeStub {
* Returns all existing keys, and their values, that are lexicographically
* between <code>startkey</code> (inclusive) and the <code>endKey</code>
* (exclusive).
*
*
* @param startKey
* @param endKey
* @return an {@link Iterable} of {@link KeyValue}
Expand All @@ -130,10 +130,10 @@ public interface ChaincodeStub {
/**
* Returns all existing keys, and their values, that are prefixed by the
* specified partial {@link CompositeKey}.
*
*
* If a full composite key is specified, it will not match itself, resulting
* in no keys being returned.
*
*
* @param compositeKey
* partial composite key
* @return an {@link Iterable} of {@link KeyValue}
Expand Down Expand Up @@ -162,6 +162,19 @@ public interface ChaincodeStub {
*/
CompositeKey splitCompositeKey(String compositeKey);

/**
* Perform a rich query against the state database.
*
* @param query
* query string in a syntax supported by the underlying state
* database
* @return
* @throws UnsupportedOperationException
* if the underlying state database does not support rich
* queries.
*/
QueryResultsIterator<KeyValue> getQueryResult(String query);

/**
* Defines the CHAINCODE type event that will be posted to interested
* clients when the chaincode's result is committed to the ledger.
Expand All @@ -175,7 +188,7 @@ public interface ChaincodeStub {

/**
* Invoke another chaincode using the same transaction context.
*
*
* @param chaincodeName
* Name of chaincode to be invoked.
* @param args
Expand All @@ -188,11 +201,11 @@ default Response invokeChaincode(String chaincodeName, List<byte[]> args) {

/**
* Invoke another chaincode using the same transaction context.
*
*
* This is a convenience version of
* {@link #invokeChaincode(String, List, String)}. The string args will be
* encoded into as UTF-8 bytes.
*
*
* @param chaincodeName
* Name of chaincode to be invoked.
* @param args
Expand All @@ -207,11 +220,11 @@ default Response invokeChaincodeWithStringArgs(String chaincodeName, List<String

/**
* Invoke another chaincode using the same transaction context.
*
*
* This is a convenience version of {@link #invokeChaincode(String, List)}.
* The string args will be encoded into as UTF-8 bytes.
*
*
*
*
* @param chaincodeName
* Name of chaincode to be invoked.
* @param args
Expand All @@ -224,11 +237,11 @@ default Response invokeChaincodeWithStringArgs(String chaincodeName, List<String

/**
* Invoke another chaincode using the same transaction context.
*
*
* This is a convenience version of {@link #invokeChaincode(String, List)}.
* The string args will be encoded into as UTF-8 bytes.
*
*
*
*
* @param chaincodeName
* Name of chaincode to be invoked.
* @param args
Expand Down Expand Up @@ -266,7 +279,7 @@ default void putStringState(String key, String value) {
/**
* Returns the CHAINCODE type event that will be posted to interested
* clients when the chaincode's result is committed to the ledger.
*
*
* @return the chaincode event or null
*/
ChaincodeEvent getEvent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ public List<String> getStringArgs() {
public String getFunction() {
return getStringArgs().size() > 0 ? getStringArgs().get(0) : null;
}

/* (non-Javadoc)
* @see org.hyperledger.fabric.shim.ChaincodeStub#getParameters()
*/
@Override
public List<String> getParameters() {
return getStringArgs().stream().skip(1).collect(toList());
}

/* (non-Javadoc)
* @see org.hyperledger.fabric.shim.ChaincodeStub#setEvent(java.lang.String, byte[])
*/
Expand Down Expand Up @@ -187,6 +187,17 @@ public CompositeKey splitCompositeKey(String compositeKey) {
return CompositeKey.parseCompositeKey(compositeKey);
}

/* (non-Javadoc)
* @see org.hyperledger.fabric.shim.ChaincodeStub#getQueryResult(java.lang.String)
*/
@Override
public QueryResultsIterator<KeyValue> getQueryResult(String query) {
return new QueryResultsIteratorImpl<KeyValue>(this.handler, getTxId(),
handler.handleGetQueryResult(getTxId(), query),
queryResultBytesToKv.andThen(KeyValueImpl::new)
);
}

/* (non-Javadoc)
* @see org.hyperledger.fabric.shim.ChaincodeStub#invokeChaincode(java.lang.String, java.util.List, java.lang.String)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.hyperledger.fabric.protos.peer.ChaincodeShim.ChaincodeMessage.Type.COMPLETED;
import static org.hyperledger.fabric.protos.peer.ChaincodeShim.ChaincodeMessage.Type.DEL_STATE;
import static org.hyperledger.fabric.protos.peer.ChaincodeShim.ChaincodeMessage.Type.ERROR;
import static org.hyperledger.fabric.protos.peer.ChaincodeShim.ChaincodeMessage.Type.GET_QUERY_RESULT;
import static org.hyperledger.fabric.protos.peer.ChaincodeShim.ChaincodeMessage.Type.GET_STATE;
import static org.hyperledger.fabric.protos.peer.ChaincodeShim.ChaincodeMessage.Type.GET_STATE_BY_RANGE;
import static org.hyperledger.fabric.protos.peer.ChaincodeShim.ChaincodeMessage.Type.INIT;
Expand Down Expand Up @@ -48,6 +49,7 @@
import org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeSpec;
import org.hyperledger.fabric.protos.peer.ChaincodeShim.ChaincodeMessage;
import org.hyperledger.fabric.protos.peer.ChaincodeShim.ChaincodeMessage.Type;
import org.hyperledger.fabric.protos.peer.ChaincodeShim.GetQueryResult;
import org.hyperledger.fabric.protos.peer.ChaincodeShim.GetStateByRange;
import org.hyperledger.fabric.protos.peer.ChaincodeShim.PutStateInfo;
import org.hyperledger.fabric.protos.peer.ChaincodeShim.QueryResponse;
Expand Down Expand Up @@ -217,19 +219,19 @@ private void beforeRegistered(Event event) {
private void handleInit(ChaincodeMessage message) {
new Thread(() -> {
try {

// Get the function and args from Payload
final ChaincodeInput input = ChaincodeInput.parseFrom(message.getPayload());

// Mark as a transaction (allow put/del state)
markIsTransaction(message.getTxid(), true);

// Create the ChaincodeStub which the chaincode can use to callback
final ChaincodeStub stub = new ChaincodeStubImpl(message.getTxid(), this, input.getArgsList());

// Call chaincode's init
final Response result = chaincode.init(stub);

if(result.getStatus() == Status.SUCCESS_VALUE) {
// Send COMPLETED with entire result as payload
logger.debug(String.format(String.format("[%s]Init succeeded. Sending %s", shortID(message), COMPLETED)));
Expand All @@ -239,7 +241,7 @@ private void handleInit(ChaincodeMessage message) {
logger.error(String.format("[%s]Init failed. Sending %s", shortID(message), ERROR));
triggerNextState(newErrorEventMessage(message.getTxid(), result.getMessage(), stub.getEvent()), true);
}

} catch (InvalidProtocolBufferException | RuntimeException e) {
logger.error(String.format("[%s]Init failed. Sending %s", shortID(message), ERROR), e);
triggerNextState(ChaincodeMessage.newBuilder()
Expand Down Expand Up @@ -273,19 +275,19 @@ private void beforeInit(Event event) {
private void handleTransaction(ChaincodeMessage message) {
new Thread(() -> {
try {

// Get the function and args from Payload
final ChaincodeInput input = ChaincodeInput.parseFrom(message.getPayload());

// Mark as a transaction (allow put/del state)
markIsTransaction(message.getTxid(), true);

// Create the ChaincodeStub which the chaincode can use to callback
final ChaincodeStub stub = new ChaincodeStubImpl(message.getTxid(), this, input.getArgsList());

// Call chaincode's invoke
final Response result = chaincode.invoke(stub);

if(result.getStatus() == Status.SUCCESS_VALUE) {
// Send COMPLETED with entire result as payload
logger.debug(String.format(String.format("[%s]Invoke succeeded. Sending %s", shortID(message), COMPLETED)));
Expand All @@ -295,7 +297,7 @@ private void handleTransaction(ChaincodeMessage message) {
logger.error(String.format("[%s]Invoke failed. Sending %s", shortID(message), ERROR));
triggerNextState(newErrorEventMessage(message.getTxid(), result.getMessage(), stub.getEvent()), true);
}

} catch (InvalidProtocolBufferException | RuntimeException e) {
logger.error(String.format("[%s]Invoke failed. Sending %s", shortID(message), ERROR), e);
triggerNextState(ChaincodeMessage.newBuilder()
Expand Down Expand Up @@ -591,6 +593,12 @@ void queryStateClose(String txId, String queryId) {
.build().toByteString());
}

QueryResponse handleGetQueryResult(String txId, String query) {
return invokeQueryResponseMessage(txId, GET_QUERY_RESULT, GetQueryResult.newBuilder()
.setQuery(query)
.build().toByteString());
}

private QueryResponse invokeQueryResponseMessage(String txId, ChaincodeMessage.Type type, ByteString payload) {
try {
return QueryResponse.parseFrom(invokeChaincodeSupport(txId, type, payload)
Expand Down Expand Up @@ -672,19 +680,19 @@ Response handleInvokeChaincode(String chaincodeName, List<byte[]> args, String t
try {
// create the channel on which to communicate the response from validating peer
final Channel<ChaincodeMessage> responseChannel = createChannel(txid);

// Send INVOKE_CHAINCODE message to validator chaincode support
final ChaincodeMessage message = HandlerHelper.newInvokeChaincodeMessage(txid, invocationSpec.toByteString());
logger.debug(String.format("[%s]Sending %s", shortID(message), INVOKE_CHAINCODE));
serialSend(message);

// wait for response chaincode message
final ChaincodeMessage outerResponseMessage = receiveChannel(responseChannel);

if(outerResponseMessage == null) {
return ChaincodeHelper.newInternalServerErrorResponse("chaincode invoke returned null");
}

logger.debug(String.format("[%s]Received %s.", shortID(outerResponseMessage.getTxid()), outerResponseMessage.getType()));

switch (outerResponseMessage.getType()) {
Expand All @@ -695,7 +703,7 @@ Response handleInvokeChaincode(String chaincodeName, List<byte[]> args, String t
logger.debug(String.format("[%s]Received %s.", shortID(responseMessage.getTxid()), responseMessage.getType()));
if(responseMessage.getType() == COMPLETED) {
// success
return Response.parseFrom(responseMessage.getPayload());
return Response.parseFrom(responseMessage.getPayload());
} else {
// error
return ChaincodeHelper.newInternalServerErrorResponse(responseMessage.getPayload().toByteArray());
Expand Down

0 comments on commit 6ad95f6

Please sign in to comment.