Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | 2x 2x 18x 18x 18x 18x 18x 18x 1x 1x 3x 3x 1x 1x 2x 1x 1x 1x 2x 2x 5x 5x 1x 4x 2x 2x 1x 1x 1x 1x 1x 2x 6x 2x 2x 2x | /*
# Copyright Zhao Chaoyi. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const logger = require('./logger').getLogger('lib/iterators.js');
const { ledger } = require('@hyperledger/fabric-protos');
/**
* CommonIterator allows a chaincode to check whether any more result(s)
* need to be fetched from an iterator and close it when done.
*
* @class
* @memberof fabric-shim
*/
class CommonIterator {
/**
* constructor
*
* Note that the decoded payload will be a protobuf of type
* fabprotos.protos.QueryResponse
*
* @param {ChaincodeSupportClient} handler client handler
* @param {string} channel_id channel id
* @param {string} txID transaction id
* @param {object} response decoded payload
*/
constructor(handler, channel_id, txID, response, type) {
this.type = type;
this.handler = handler;
this.channel_id = channel_id;
this.txID = txID;
// this response is a peer.QueryResponse object
this.response = response;
this.currentLoc = 0;
}
/**
* close the iterator.
* @async
* @return {promise} A promise that is resolved with the close payload or rejected
* if there is a problem
*/
async close() {
logger.debug('close called on %s iterator for txid: %s', this.type, this.txID);
return await this.handler.handleQueryStateClose(this.response.getId(), this.channel_id, this.txID);
}
/*
* creates a return value
*/
_createAndEmitResult() {
const resultsList = this.response.getResultsList();
let queryResult;
// established external API has a very specific structure here
// so need to 'fluff' up this structure to match
// Not all queryResults have the same methods
if (this.type === 'QUERY') {
const queryResultPb = ledger.queryresult.KV.deserializeBinary((resultsList[this.currentLoc]).getResultbytes());
queryResult = {
key: queryResultPb.getKey(),
value: Buffer.from(queryResultPb.getValue())
};
} else if (this.type === 'HISTORY') {
const queryResultPb = ledger.queryresult.KeyModification.deserializeBinary((resultsList[this.currentLoc]).getResultbytes());
queryResult = {
txId: queryResultPb.getTxId(),
value: Buffer.from(queryResultPb.getValue()),
isDelete: queryResultPb.getIsDelete(),
timestamp: queryResultPb.getTimestamp().toObject()
};
} else {
throw new Error('Iterator constructed with unknown type: ' + this.type);
}
this.currentLoc++;
return {value: queryResult, done: false};
}
/**
* Get the next value and return it through a promise.
* @async
* @return {promise} a promise that is fulfilled with an object { value: (next value) },
* is fulfilled with an object { done: true } if there is no more value,
* or is rejected if any error occurs.
*/
async next() {
// check to see if there are some results left in the current result set
const resultsList = this.response.getResultsList();
if (this.currentLoc < resultsList.length) {
return this._createAndEmitResult();
} else {
// check to see if there is more and go fetch it
if (this.response.getHasMore()) {
try {
const response = await this.handler.handleQueryStateNext(this.response.getId(), this.channel_id, this.txID);
this.currentLoc = 0;
this.response = response;
return this._createAndEmitResult();
} catch (err) {
logger.error('unexpected error received getting next value: %s', err.message);
throw err;
}
}
return { done: true };
}
}
}
/**
* A State Query iterator allows a chaincode to iterate over a
* set of key/value pairs returned by range and execute queries
*
* @extends CommonIterator
* @memberof fabric-shim
*/
class StateQueryIterator extends CommonIterator {
constructor(handler, channel_id, txID, response) {
super(handler, channel_id, txID, response, 'QUERY');
}
}
/**
* A History Query iterator allows a chaincode to iterate over a
* set of key/value pairs returned by a history query
*
* @extends CommonIterator
* @memberof fabric-shim
*/
class HistoryQueryIterator extends CommonIterator {
constructor(handler, channel_id, txID, response) {
super(handler, channel_id, txID, response, 'HISTORY');
}
}
module.exports.StateQueryIterator = StateQueryIterator;
module.exports.HistoryQueryIterator = HistoryQueryIterator;
|