FrankKanBear commented on code in PR #10:
URL:
https://github.com/apache/ignite-nodejs-thin-client/pull/10#discussion_r3372391171
##########
src/Cursor.ts:
##########
@@ -74,8 +73,13 @@ export abstract class BaseCursor<T> {
* @return {boolean} - true if more cache entries are available, false
otherwise.
*/
hasMore(): boolean {
+ // _buffer is set on a freshly-opened cursor and holds the first page
of
+ // results before _getValues() has been called for the first time.
+ // Without this check, hasMore() incorrectly returns false on a new
cursor
+ // because _hasNext starts as false and _values starts as null.
return this._hasNext ||
- this._values && this._valueIndex < this._values.length;
+ (this._values != null && this._valueIndex < this._values.length) ||
+ this._buffer != null;
Review Comment:
Good catch — this is correct. With || this._buffer != null, a zero-row first
page (rowCount = 0, trailing hasNext = false) makes hasMore() return true while
the next getValue() returns null, so a while (cursor.hasMore()) { … } loop runs
one spurious iteration on any empty result.
Peeking the buffered page's rowCount (and the trailing hasNext when rowCount
=== 0) without consuming it fixes it — MessageBuffer exposes position get/set,
so save → peek → restore is safe and stays synchronous:
if (this._hasNext) {
return true;
}
if (this._values != null && this._valueIndex < this._values.length) {
return true;
}
if (this._buffer != null) {
// Peek the buffered first page without consuming it. A page is
laid out as
// [rowCount:int][rows...][hasNext:bool]; rowCount === 0 with a
trailing
// hasNext === false is an empty result, so hasMore() must be
false here.
const savedPosition = this._buffer.position;
const rowCount = this._buffer.readInteger();
const more = rowCount > 0 || this._buffer.readBoolean();
this._buffer.position = savedPosition;
return more;
}
return false;
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]