8
8
9
9
const logger = require ( './logger' ) . getLogger ( 'lib/iterators.js' ) ;
10
10
11
- const { ledger} = require ( '@hyperledger/fabric-protos' ) ;
11
+ const { ledger } = require ( '@hyperledger/fabric-protos' ) ;
12
12
13
13
/**
14
14
* CommonIterator allows a chaincode to check whether any more result(s)
@@ -20,16 +20,16 @@ const {ledger} = require('@hyperledger/fabric-protos');
20
20
class CommonIterator {
21
21
22
22
/**
23
- * constructor
23
+ * constructor
24
24
*
25
25
* Note that the decoded payload will be a protobuf of type
26
26
* fabprotos.protos.QueryResponse
27
27
*
28
- * @param {ChaincodeSupportClient } handler client handler
29
- * @param {string } channel_id channel id
30
- * @param {string } txID transaction id
31
- * @param {object } response decoded payload
32
- */
28
+ * @param {ChaincodeSupportClient } handler client handler
29
+ * @param {string } channel_id channel id
30
+ * @param {string } txID transaction id
31
+ * @param {object } response decoded payload
32
+ */
33
33
constructor ( handler , channel_id , txID , response , type ) {
34
34
this . type = type ;
35
35
this . handler = handler ;
@@ -42,58 +42,57 @@ class CommonIterator {
42
42
}
43
43
44
44
/**
45
- * close the iterator.
46
- * @async
47
- * @return {promise } A promise that is resolved with the close payload or rejected
48
- * if there is a problem
49
- */
45
+ * close the iterator.
46
+ * @async
47
+ * @return {promise } A promise that is resolved with the close payload or rejected
48
+ * if there is a problem
49
+ */
50
50
async close ( ) {
51
51
logger . debug ( 'close called on %s iterator for txid: %s' , this . type , this . txID ) ;
52
52
return await this . handler . handleQueryStateClose ( this . response . getId ( ) , this . channel_id , this . txID ) ;
53
53
}
54
54
55
55
/*
56
- * decode the payload depending on the type of iterator.
57
- * @param {object } bytes
58
- */
59
- _getResultFromBytes ( bytes ) {
60
- if ( this . type === 'QUERY' ) {
61
- return ledger . queryresult . KV . deserializeBinary ( bytes . getResultbytes ( ) ) ;
62
- } else if ( this . type === 'HISTORY' ) {
63
- return ledger . queryresult . KeyModification . deserializeBinary ( bytes . getResultbytes ( ) ) ;
64
- }
65
- throw new Error ( 'Iterator constructed with unknown type: ' + this . type ) ;
66
- }
67
-
68
-
69
- /*
70
- * creates a return value
71
- */
56
+ * creates a return value
57
+ */
72
58
_createAndEmitResult ( ) {
73
- const queryResult = { } ;
74
59
const resultsList = this . response . getResultsList ( ) ;
60
+ let queryResult ;
75
61
76
- const queryResultPb = this . _getResultFromBytes ( resultsList [ this . currentLoc ] ) ;
77
- queryResult . value = { value :Buffer . from ( queryResultPb . getValue ( ) ) } ;
78
- /* istanbul ignore else*/
79
- if ( 'getKey' in queryResultPb ) {
80
- queryResult . value . key = Buffer . from ( queryResultPb . getKey ( ) ) . toString ( ) ;
62
+ // established external API has a very specific structure here
63
+ // so need to 'fluff' up this structure to match
64
+ // Not all queryResults have the same methods
65
+ if ( this . type === 'QUERY' ) {
66
+ const queryResultPb = ledger . queryresult . KV . deserializeBinary ( ( resultsList [ this . currentLoc ] ) . getResultbytes ( ) ) ;
67
+ queryResult = {
68
+ key : queryResultPb . getKey ( ) ,
69
+ value : Buffer . from ( queryResultPb . getValue ( ) )
70
+ } ;
71
+ } else if ( this . type === 'HISTORY' ) {
72
+ const queryResultPb = ledger . queryresult . KeyModification . deserializeBinary ( ( resultsList [ this . currentLoc ] ) . getResultbytes ( ) ) ;
73
+ queryResult = {
74
+ txId : queryResultPb . getTxId ( ) ,
75
+ value : Buffer . from ( queryResultPb . getValue ( ) ) ,
76
+ isDelete : queryResultPb . getIsDelete ( ) ,
77
+ timestamp : queryResultPb . getTimestamp ( ) . toObject ( )
78
+
79
+ } ;
80
+ } else {
81
+ throw new Error ( 'Iterator constructed with unknown type: ' + this . type ) ;
81
82
}
82
83
83
-
84
84
this . currentLoc ++ ;
85
85
86
- queryResult . done = false ;
87
- return queryResult ;
86
+ return { value : queryResult , done : false } ;
88
87
}
89
88
90
89
/**
91
- * Get the next value and return it through a promise.
92
- * @async
93
- * @return {promise } a promise that is fulfilled with an object { value: (next value) },
94
- * is fulfilled with an object { done: true } if there is no more value,
95
- * or is rejected if any error occurs.
96
- */
90
+ * Get the next value and return it through a promise.
91
+ * @async
92
+ * @return {promise } a promise that is fulfilled with an object { value: (next value) },
93
+ * is fulfilled with an object { done: true } if there is no more value,
94
+ * or is rejected if any error occurs.
95
+ */
97
96
async next ( ) {
98
97
// check to see if there are some results left in the current result set
99
98
const resultsList = this . response . getResultsList ( ) ;
@@ -112,7 +111,7 @@ class CommonIterator {
112
111
throw err ;
113
112
}
114
113
}
115
- return { done : true } ;
114
+ return { done : true } ;
116
115
}
117
116
118
117
}
0 commit comments