Skip to content

Commit 6ad95f6

Browse files
author
Luis Sanchez
committed
[FAB-3305] java cc get query result
Change-Id: If3544c57a1b9c7442284ff6d06bc862cdd916ed0 Signed-off-by: Luis Sanchez <[email protected]>
1 parent 5858fbb commit 6ad95f6

File tree

3 files changed

+70
-38
lines changed

3 files changed

+70
-38
lines changed

core/chaincode/shim/java/src/main/java/org/hyperledger/fabric/shim/ChaincodeStub.java

+33-20
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public interface ChaincodeStub {
3333
* Returns the arguments corresponding to the call to
3434
* {@link Chaincode#init(ChaincodeStub)} or
3535
* {@link Chaincode#invoke(ChaincodeStub)}.
36-
*
36+
*
3737
* @return a list of arguments
3838
*/
3939
List<byte[]> getArgs();
@@ -42,17 +42,17 @@ public interface ChaincodeStub {
4242
* Returns the arguments corresponding to the call to
4343
* {@link Chaincode#init(ChaincodeStub)} or
4444
* {@link Chaincode#invoke(ChaincodeStub)}.
45-
*
45+
*
4646
* @return a list of arguments cast to UTF-8 strings
4747
*/
4848
List<String> getStringArgs();
4949

5050
/**
5151
* A convenience method that returns the first argument of the chaincode
5252
* invocation for use as a function name.
53-
*
53+
*
5454
* The bytes of the first argument are decoded as a UTF-8 string.
55-
*
55+
*
5656
* @return the function name
5757
*/
5858
String getFunction();
@@ -61,10 +61,10 @@ public interface ChaincodeStub {
6161
* A convenience method that returns all except the first argument of the
6262
* chaincode invocation for use as the parameters to the function returned
6363
* by #{@link ChaincodeStub#getFunction()}.
64-
*
64+
*
6565
* The bytes of the arguments are decoded as a UTF-8 strings and returned as
6666
* a list of string parameters..
67-
*
67+
*
6868
* @return a list of parameters
6969
*/
7070
List<String> getParameters();
@@ -78,7 +78,7 @@ public interface ChaincodeStub {
7878

7979
/**
8080
* Invoke another chaincode using the same transaction context.
81-
*
81+
*
8282
* @param chaincodeName
8383
* Name of chaincode to be invoked.
8484
* @param args
@@ -120,7 +120,7 @@ public interface ChaincodeStub {
120120
* Returns all existing keys, and their values, that are lexicographically
121121
* between <code>startkey</code> (inclusive) and the <code>endKey</code>
122122
* (exclusive).
123-
*
123+
*
124124
* @param startKey
125125
* @param endKey
126126
* @return an {@link Iterable} of {@link KeyValue}
@@ -130,10 +130,10 @@ public interface ChaincodeStub {
130130
/**
131131
* Returns all existing keys, and their values, that are prefixed by the
132132
* specified partial {@link CompositeKey}.
133-
*
133+
*
134134
* If a full composite key is specified, it will not match itself, resulting
135135
* in no keys being returned.
136-
*
136+
*
137137
* @param compositeKey
138138
* partial composite key
139139
* @return an {@link Iterable} of {@link KeyValue}
@@ -162,6 +162,19 @@ public interface ChaincodeStub {
162162
*/
163163
CompositeKey splitCompositeKey(String compositeKey);
164164

165+
/**
166+
* Perform a rich query against the state database.
167+
*
168+
* @param query
169+
* query string in a syntax supported by the underlying state
170+
* database
171+
* @return
172+
* @throws UnsupportedOperationException
173+
* if the underlying state database does not support rich
174+
* queries.
175+
*/
176+
QueryResultsIterator<KeyValue> getQueryResult(String query);
177+
165178
/**
166179
* Defines the CHAINCODE type event that will be posted to interested
167180
* clients when the chaincode's result is committed to the ledger.
@@ -175,7 +188,7 @@ public interface ChaincodeStub {
175188

176189
/**
177190
* Invoke another chaincode using the same transaction context.
178-
*
191+
*
179192
* @param chaincodeName
180193
* Name of chaincode to be invoked.
181194
* @param args
@@ -188,11 +201,11 @@ default Response invokeChaincode(String chaincodeName, List<byte[]> args) {
188201

189202
/**
190203
* Invoke another chaincode using the same transaction context.
191-
*
204+
*
192205
* This is a convenience version of
193206
* {@link #invokeChaincode(String, List, String)}. The string args will be
194207
* encoded into as UTF-8 bytes.
195-
*
208+
*
196209
* @param chaincodeName
197210
* Name of chaincode to be invoked.
198211
* @param args
@@ -207,11 +220,11 @@ default Response invokeChaincodeWithStringArgs(String chaincodeName, List<String
207220

208221
/**
209222
* Invoke another chaincode using the same transaction context.
210-
*
223+
*
211224
* This is a convenience version of {@link #invokeChaincode(String, List)}.
212225
* The string args will be encoded into as UTF-8 bytes.
213-
*
214-
*
226+
*
227+
*
215228
* @param chaincodeName
216229
* Name of chaincode to be invoked.
217230
* @param args
@@ -224,11 +237,11 @@ default Response invokeChaincodeWithStringArgs(String chaincodeName, List<String
224237

225238
/**
226239
* Invoke another chaincode using the same transaction context.
227-
*
240+
*
228241
* This is a convenience version of {@link #invokeChaincode(String, List)}.
229242
* The string args will be encoded into as UTF-8 bytes.
230-
*
231-
*
243+
*
244+
*
232245
* @param chaincodeName
233246
* Name of chaincode to be invoked.
234247
* @param args
@@ -266,7 +279,7 @@ default void putStringState(String key, String value) {
266279
/**
267280
* Returns the CHAINCODE type event that will be posted to interested
268281
* clients when the chaincode's result is committed to the ledger.
269-
*
282+
*
270283
* @return the chaincode event or null
271284
*/
272285
ChaincodeEvent getEvent();

core/chaincode/shim/java/src/main/java/org/hyperledger/fabric/shim/impl/ChaincodeStubImpl.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ public List<String> getStringArgs() {
7575
public String getFunction() {
7676
return getStringArgs().size() > 0 ? getStringArgs().get(0) : null;
7777
}
78-
78+
7979
/* (non-Javadoc)
8080
* @see org.hyperledger.fabric.shim.ChaincodeStub#getParameters()
8181
*/
8282
@Override
8383
public List<String> getParameters() {
8484
return getStringArgs().stream().skip(1).collect(toList());
8585
}
86-
86+
8787
/* (non-Javadoc)
8888
* @see org.hyperledger.fabric.shim.ChaincodeStub#setEvent(java.lang.String, byte[])
8989
*/
@@ -187,6 +187,17 @@ public CompositeKey splitCompositeKey(String compositeKey) {
187187
return CompositeKey.parseCompositeKey(compositeKey);
188188
}
189189

190+
/* (non-Javadoc)
191+
* @see org.hyperledger.fabric.shim.ChaincodeStub#getQueryResult(java.lang.String)
192+
*/
193+
@Override
194+
public QueryResultsIterator<KeyValue> getQueryResult(String query) {
195+
return new QueryResultsIteratorImpl<KeyValue>(this.handler, getTxId(),
196+
handler.handleGetQueryResult(getTxId(), query),
197+
queryResultBytesToKv.andThen(KeyValueImpl::new)
198+
);
199+
}
200+
190201
/* (non-Javadoc)
191202
* @see org.hyperledger.fabric.shim.ChaincodeStub#invokeChaincode(java.lang.String, java.util.List, java.lang.String)
192203
*/

core/chaincode/shim/java/src/main/java/org/hyperledger/fabric/shim/impl/Handler.java

+24-16
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.hyperledger.fabric.protos.peer.ChaincodeShim.ChaincodeMessage.Type.COMPLETED;
2020
import static org.hyperledger.fabric.protos.peer.ChaincodeShim.ChaincodeMessage.Type.DEL_STATE;
2121
import static org.hyperledger.fabric.protos.peer.ChaincodeShim.ChaincodeMessage.Type.ERROR;
22+
import static org.hyperledger.fabric.protos.peer.ChaincodeShim.ChaincodeMessage.Type.GET_QUERY_RESULT;
2223
import static org.hyperledger.fabric.protos.peer.ChaincodeShim.ChaincodeMessage.Type.GET_STATE;
2324
import static org.hyperledger.fabric.protos.peer.ChaincodeShim.ChaincodeMessage.Type.GET_STATE_BY_RANGE;
2425
import static org.hyperledger.fabric.protos.peer.ChaincodeShim.ChaincodeMessage.Type.INIT;
@@ -48,6 +49,7 @@
4849
import org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeSpec;
4950
import org.hyperledger.fabric.protos.peer.ChaincodeShim.ChaincodeMessage;
5051
import org.hyperledger.fabric.protos.peer.ChaincodeShim.ChaincodeMessage.Type;
52+
import org.hyperledger.fabric.protos.peer.ChaincodeShim.GetQueryResult;
5153
import org.hyperledger.fabric.protos.peer.ChaincodeShim.GetStateByRange;
5254
import org.hyperledger.fabric.protos.peer.ChaincodeShim.PutStateInfo;
5355
import org.hyperledger.fabric.protos.peer.ChaincodeShim.QueryResponse;
@@ -217,19 +219,19 @@ private void beforeRegistered(Event event) {
217219
private void handleInit(ChaincodeMessage message) {
218220
new Thread(() -> {
219221
try {
220-
222+
221223
// Get the function and args from Payload
222224
final ChaincodeInput input = ChaincodeInput.parseFrom(message.getPayload());
223-
225+
224226
// Mark as a transaction (allow put/del state)
225227
markIsTransaction(message.getTxid(), true);
226-
228+
227229
// Create the ChaincodeStub which the chaincode can use to callback
228230
final ChaincodeStub stub = new ChaincodeStubImpl(message.getTxid(), this, input.getArgsList());
229-
231+
230232
// Call chaincode's init
231233
final Response result = chaincode.init(stub);
232-
234+
233235
if(result.getStatus() == Status.SUCCESS_VALUE) {
234236
// Send COMPLETED with entire result as payload
235237
logger.debug(String.format(String.format("[%s]Init succeeded. Sending %s", shortID(message), COMPLETED)));
@@ -239,7 +241,7 @@ private void handleInit(ChaincodeMessage message) {
239241
logger.error(String.format("[%s]Init failed. Sending %s", shortID(message), ERROR));
240242
triggerNextState(newErrorEventMessage(message.getTxid(), result.getMessage(), stub.getEvent()), true);
241243
}
242-
244+
243245
} catch (InvalidProtocolBufferException | RuntimeException e) {
244246
logger.error(String.format("[%s]Init failed. Sending %s", shortID(message), ERROR), e);
245247
triggerNextState(ChaincodeMessage.newBuilder()
@@ -273,19 +275,19 @@ private void beforeInit(Event event) {
273275
private void handleTransaction(ChaincodeMessage message) {
274276
new Thread(() -> {
275277
try {
276-
278+
277279
// Get the function and args from Payload
278280
final ChaincodeInput input = ChaincodeInput.parseFrom(message.getPayload());
279-
281+
280282
// Mark as a transaction (allow put/del state)
281283
markIsTransaction(message.getTxid(), true);
282-
284+
283285
// Create the ChaincodeStub which the chaincode can use to callback
284286
final ChaincodeStub stub = new ChaincodeStubImpl(message.getTxid(), this, input.getArgsList());
285-
287+
286288
// Call chaincode's invoke
287289
final Response result = chaincode.invoke(stub);
288-
290+
289291
if(result.getStatus() == Status.SUCCESS_VALUE) {
290292
// Send COMPLETED with entire result as payload
291293
logger.debug(String.format(String.format("[%s]Invoke succeeded. Sending %s", shortID(message), COMPLETED)));
@@ -295,7 +297,7 @@ private void handleTransaction(ChaincodeMessage message) {
295297
logger.error(String.format("[%s]Invoke failed. Sending %s", shortID(message), ERROR));
296298
triggerNextState(newErrorEventMessage(message.getTxid(), result.getMessage(), stub.getEvent()), true);
297299
}
298-
300+
299301
} catch (InvalidProtocolBufferException | RuntimeException e) {
300302
logger.error(String.format("[%s]Invoke failed. Sending %s", shortID(message), ERROR), e);
301303
triggerNextState(ChaincodeMessage.newBuilder()
@@ -591,6 +593,12 @@ void queryStateClose(String txId, String queryId) {
591593
.build().toByteString());
592594
}
593595

596+
QueryResponse handleGetQueryResult(String txId, String query) {
597+
return invokeQueryResponseMessage(txId, GET_QUERY_RESULT, GetQueryResult.newBuilder()
598+
.setQuery(query)
599+
.build().toByteString());
600+
}
601+
594602
private QueryResponse invokeQueryResponseMessage(String txId, ChaincodeMessage.Type type, ByteString payload) {
595603
try {
596604
return QueryResponse.parseFrom(invokeChaincodeSupport(txId, type, payload)
@@ -672,19 +680,19 @@ Response handleInvokeChaincode(String chaincodeName, List<byte[]> args, String t
672680
try {
673681
// create the channel on which to communicate the response from validating peer
674682
final Channel<ChaincodeMessage> responseChannel = createChannel(txid);
675-
683+
676684
// Send INVOKE_CHAINCODE message to validator chaincode support
677685
final ChaincodeMessage message = HandlerHelper.newInvokeChaincodeMessage(txid, invocationSpec.toByteString());
678686
logger.debug(String.format("[%s]Sending %s", shortID(message), INVOKE_CHAINCODE));
679687
serialSend(message);
680688

681689
// wait for response chaincode message
682690
final ChaincodeMessage outerResponseMessage = receiveChannel(responseChannel);
683-
691+
684692
if(outerResponseMessage == null) {
685693
return ChaincodeHelper.newInternalServerErrorResponse("chaincode invoke returned null");
686694
}
687-
695+
688696
logger.debug(String.format("[%s]Received %s.", shortID(outerResponseMessage.getTxid()), outerResponseMessage.getType()));
689697

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

0 commit comments

Comments
 (0)