import { ExecuteStatementResult , FetchPageResult , IonBinary , Page , ValueHolder } from "aws-sdk/clients/qldbsession" ; import { dom } from "ion-js" ; import { Communicator } from "./Communicator" ; import { ClientError } from "./errors/Errors" ; import { ResultReadable } from "./ResultReadable" ; import { IOUsage } from "./stats/IOUsage" ; import { TimingInformation } from "./stats/TimingInformation" ; /**
 * A class representing a fully buffered set of results returned from QLDB.
 */ export class Result { private _resultList : dom . Value [ ] ; private _ioUsage : IOUsage ; private _timingInformation : TimingInformation ; param resultList param ioUsage param timingInformation private constructor ( resultList , ioUsage , timingInformation ) { this . _resultList = resultList ; this . _ioUsage = ioUsage ; this . _timingInformation = timingInformation ; } param txnId param executeResult param communicator returns internal static async create ( txnId , executeResult , communicator ) { const result = await Result . _fetchResultPages ( txnId , executeResult , communicator ) ; return result ; } param resultReadable returns static async bufferResultReadable ( resultReadable ) { const resultList = await Result . _readResultReadable ( resultReadable ) ; return new Result ( resultList , resultReadable . getConsumedIOs ( ) , resultReadable . getTimingInformation ( ) ) ; } returns getResultList ( ) { return this . _resultList . slice ( ) ; } returns getConsumedIOs ( ) { return this . _ioUsage ; } returns getTimingInformation ( ) { return this . _timingInformation ; } param ionBinary returns throws internal static _handleBlob ( ionBinary ) { if ( ionBinary instanceof Buffer ) { return < Buffer > ionBinary ; } if ( ionBinary instanceof Uint8Array ) { return < Uint8Array > ionBinary ; } if ( typeof ionBinary === "string" ) { return < string > ionBinary ; } throw new ClientError ( "Unexpected Blob returned from QLDB." ) ; } param txnId param executeResult param communicator returns private static async _fetchResultPages ( txnId , executeResult , communicator ) { let currentPage = executeResult . FirstPage ; let readIO = executeResult . ConsumedIOs != null ? executeResult . ConsumedIOs . ReadIOs : null ; let processingTime = executeResult . TimingInformation != null ? executeResult . TimingInformation . ProcessingTimeMilliseconds : null ; const pageValuesArray = [ ] ; if ( currentPage . Values && currentPage . Values . length > 0 ) { pageValuesArray . push ( currentPage . Values ) ; } while ( currentPage . NextPageToken ) { const fetchPageResult = await communicator . fetchPage ( txnId , currentPage . NextPageToken ) ; currentPage = fetchPageResult . Page ; if ( currentPage . Values && currentPage . Values . length > 0 ) { pageValuesArray . push ( currentPage . Values ) ; } if ( fetchPageResult . ConsumedIOs != null ) { readIO += fetchPageResult . ConsumedIOs . ReadIOs ; } if ( fetchPageResult . TimingInformation != null ) { processingTime += fetchPageResult . TimingInformation . ProcessingTimeMilliseconds ; } } const ionValues = [ ] ; pageValuesArray . forEach ( ( valueHolders ) => { valueHolders . forEach ( ( valueHolder ) => { ionValues . push ( dom . load ( Result . _handleBlob ( valueHolder . IonBinary ) ) ) ; } ) ; } ) ; const ioUsage = readIO != null ? new IOUsage ( readIO ) : null ; const timingInformation = processingTime != null ? new TimingInformation ( processingTime ) : null ; return new Result ( ionValues , ioUsage , timingInformation ) ; } param resultReadable returns private static async _readResultReadable ( resultReadable ) { return new Promise ( res => { let ionValues = [ ] ; resultReadable . on ( "data" , function ( value ) { ionValues . push ( value ) ; } ) . on ( "end" , function ( ) { res ( ionValues ) ; } ) ; } ) ; } }