@@ -277,6 +277,8 @@ class ChaincodeMessageHandler {
277
277
constructor ( stream , chaincode ) {
278
278
this . _stream = stream ;
279
279
this . chaincode = chaincode ;
280
+ this . usePeerGetMultipleKeys = false ;
281
+ this . maxSizeGetMultipleKeys = 0 ;
280
282
}
281
283
282
284
// this is a long-running method that does not return until
@@ -388,6 +390,65 @@ class ChaincodeMessageHandler {
388
390
return await this . _askPeerAndListen ( msg , 'GetState' ) ;
389
391
}
390
392
393
+ async handleGetMultipleStates ( collection , keys , channel_id , txId ) {
394
+ if ( keys . length === 0 ) {
395
+ return [ ] ;
396
+ }
397
+
398
+ const responses = [ ] ;
399
+
400
+ if ( ! this . usePeerGetMultipleKeys ) {
401
+ for ( const key of keys ) {
402
+ const resp = await this . handleGetState ( collection , key , channel_id , txId ) ;
403
+ responses . push ( resp ) ;
404
+ }
405
+ return responses ;
406
+ }
407
+
408
+ let remainingKeys = [ ...keys ] ;
409
+ while ( remainingKeys . length > this . maxSizeGetMultipleKeys ) {
410
+ const batch = remainingKeys . slice ( 0 , this . maxSizeGetMultipleKeys ) ;
411
+ const resp = await this . handleOneSendGetMultipleStates ( collection , batch , channel_id , txId ) ;
412
+ responses . push ( ...resp ) ;
413
+ remainingKeys = remainingKeys . slice ( this . maxSizeGetMultipleKeys ) ;
414
+ }
415
+
416
+ if ( remainingKeys . length > 0 ) {
417
+ const resp = await this . handleOneSendGetMultipleStates ( collection , remainingKeys , channel_id , txId ) ;
418
+ responses . push ( ...resp ) ;
419
+ }
420
+
421
+ return responses . map ( r => ( r . length === 0 ? null : r ) ) ;
422
+ }
423
+
424
+ async handleOneSendGetMultipleStates ( collection , keys , channel_id , txId ) {
425
+ const msgPb = new peer . GetStateMultiple ( ) ;
426
+ msgPb . setCollection ( collection ) ;
427
+ msgPb . setKeysList ( keys ) ;
428
+
429
+ const msg = mapToChaincodeMessage ( {
430
+ type : peer . ChaincodeMessage . Type . GET_STATE_MULTIPLE ,
431
+ payload : msgPb . serializeBinary ( ) ,
432
+ txid : txId ,
433
+ channel_id : channel_id
434
+ } ) ;
435
+
436
+ logger . debug ( 'handleOneSendGetMultipleStates - keys:' , keys ) ;
437
+
438
+ const responseMsg = await this . _askPeerAndListen ( msg , 'GetMultipleStates' ) ;
439
+
440
+ if ( responseMsg . getType ( ) === peer . ChaincodeMessage . Type . RESPONSE ) {
441
+ const result = peer . GetStateMultipleResult . deserializeBinary ( responseMsg . getPayload ( ) ) ;
442
+ return result . getValuesList ( ) ;
443
+ }
444
+
445
+ if ( responseMsg . getType ( ) === peer . ChaincodeMessage . Type . ERROR ) {
446
+ throw new Error ( Buffer . from ( responseMsg . getPayload ( ) ) . toString ( ) ) ;
447
+ }
448
+
449
+ throw new Error ( `Unexpected message type ${ responseMsg . getType ( ) } received` ) ;
450
+ }
451
+
391
452
async handlePutState ( collection , key , value , channel_id , txId ) {
392
453
const msgPb = new peer . PutState ( ) ;
393
454
msgPb . setKey ( key ) ;
0 commit comments