Re: anyone able to iterate over records using getStateByRange?


Matthew White
 

Hello;
 
 
Will need to double check, but as a workaround for an unbounded range try this as the end key
 
const MAX_UNICODE_RUNE_VALUE = '\u{10ffff}';
 
Though such an unbounded range could potentially give you a lot data; worth looking at the pagination variants especially if there's any doubt about the size of data that might come back 
 
Regards, Matthew.
Matthew B White  IBM Blockchain Solutions Architect
 
Email me at WHITEMAT@...
Find me on StackOverflow, and generally at  calanais.me.uk
 
Note: restricted availability for meetings 14:30 to 17:00 UK Tuesday 
IBM United Kingdom Limited, Hursley Park, Winchester, Hampshire, SO21 2JN

"The wrong answers are the ones you go looking for when the right answers stare you in the face"
 
 
 
----- Original message -----
From: "Siddharth Jain" <siddjain@...>
Sent by: fabric@...
To: "fabric@..." <fabric@...>
Cc:
Subject: [EXTERNAL] [Hyperledger Fabric] anyone able to iterate over records using getStateByRange?
Date: Wed, Jan 29, 2020 9:24 PM
 
this method:
 
Note that startKey and endKey can be empty string, which implies unbounded range query on start or end.
 
is supposed to enable enumerating records in the database but when we tried it, it did not work. e.g.:
 
public async getAssets(ctx: Context): Promise<Asset[]> {
        const assets: Asset[] = [];
        const results = await ctx.stub.getStateByRange('', '');
        while (true) {
            let item = await results.next();
            if (item.done) {
                break;
            } else {
                let buffer = item.value.getValue();
                let asset = JSON.parse(buffer.buffer.toString()) as Asset;     // buffer.buffer returns many records not just once and the JSON.parse will fail           
                assets.push(asset);                
            }
        }
        await results.close();
        return assets;
    }
 
buffer.toString()
"ByteBufferNB(offset=22,markedOffset=-1,limit=386,capacity=1981)"
 
buffer.buffer.toString()
"
�
�
mycc00000�{"createdBy":"","id":"00000","lastModifiedBy":"","metadata":"","owner":"org2MSP"}
�
�
mycc00001�{"createdBy":"","id":"00001","lastModifiedBy":"","metadata":"","owner":"org1MSP"}
�
�
mycc00002�{"createdBy":"","id":"00002","lastModifiedBy":"","metadata":"","owner":"org1MSP"}
�
�
mycc00003�{"createdBy":"","id":"00003","lastModifiedBy":"","metadata":"","owner":"org1MSP"}
�
�
mycc00004�{"createdBy":"","id":"00004","lastModifiedBy":"","metadata":"","owner":"org1MSP"}$efc8d188-16ae-41e8-a024-7880a8d0bf31"
 
 
JSON.parse(buffer.buffer.toString())
SyntaxError: Unexpected token � in JSON at position 1
 
buffer.buffer.buffer.toString()
"[object ArrayBuffer]"
has anyone been able to use this method successfully?
 
Unless stated otherwise above:
IBM United Kingdom Limited - Registered in England and Wales with number 741598.
Registered office: PO Box 41, North Harbour, Portsmouth, Hampshire PO6 3AU

Join fabric@lists.hyperledger.org to automatically receive all group messages.