Skip to content

Commit 38a0927

Browse files
remove cursor default batch size of 1000
1 parent 25de3df commit 38a0927

File tree

4 files changed

+26
-35
lines changed

4 files changed

+26
-35
lines changed

src/cursor/abstract_cursor.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ export abstract class AbstractCursor<
893893
): Promise<InitialCursorResponse>;
894894

895895
/** @internal */
896-
async getMore(batchSize: number): Promise<CursorResponse> {
896+
async getMore(): Promise<CursorResponse> {
897897
if (this.cursorId == null) {
898898
throw new MongoRuntimeError(
899899
'Unexpected null cursor id. A cursor creating command should have set this'
@@ -910,11 +910,10 @@ export abstract class AbstractCursor<
910910
'Unexpected null session. A cursor creating command should have set this'
911911
);
912912
}
913-
914913
const getMoreOptions = {
915914
...this.cursorOptions,
916915
session: this.cursorSession,
917-
batchSize
916+
batchSize: this.cursorOptions.batchSize
918917
};
919918

920919
const getMoreOperation = new GetMoreOperation(
@@ -987,14 +986,11 @@ export abstract class AbstractCursor<
987986
await this.cursorInit();
988987
// If the cursor died or returned documents, return
989988
if ((this.documents?.length ?? 0) !== 0 || this.isDead) return;
990-
// Otherwise, run a getMore
991989
}
992990

993-
// otherwise need to call getMore
994-
const batchSize = this.cursorOptions.batchSize || 1000;
995-
991+
// Otherwise, run a getMore
996992
try {
997-
const response = await this.getMore(batchSize);
993+
const response = await this.getMore();
998994
this.cursorId = response.id;
999995
this.documents = response;
1000996
} catch (error) {

src/cursor/change_stream_cursor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ export class ChangeStreamCursor<
158158
return { server, session, response };
159159
}
160160

161-
override async getMore(batchSize: number): Promise<CursorResponse> {
162-
const response = await super.getMore(batchSize);
161+
override async getMore(): Promise<CursorResponse> {
162+
const response = await super.getMore();
163163

164164
this.maxWireVersion = maxWireVersion(this.server);
165165
this._processBatch(response);

src/cursor/find_cursor.ts

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -98,34 +98,29 @@ export class FindCursor<TSchema = any> extends ExplainableCursor<TSchema> {
9898
}
9999

100100
/** @internal */
101-
override async getMore(batchSize: number): Promise<CursorResponse> {
101+
override async getMore(): Promise<CursorResponse> {
102102
const numReturned = this.numReturned;
103-
if (numReturned) {
104-
// TODO(DRIVERS-1448): Remove logic to enforce `limit` in the driver
105-
const limit = this.findOptions.limit;
106-
batchSize =
107-
limit && limit > 0 && numReturned + batchSize > limit ? limit - numReturned : batchSize;
108-
109-
if (batchSize <= 0) {
110-
try {
111-
await this.close();
112-
} catch (error) {
113-
squashError(error);
114-
// this is an optimization for the special case of a limit for a find command to avoid an
115-
// extra getMore when the limit has been reached and the limit is a multiple of the batchSize.
116-
// This is a consequence of the new query engine in 5.0 having no knowledge of the limit as it
117-
// produces results for the find command. Once a batch is filled up, it is returned and only
118-
// on the subsequent getMore will the query framework consider the limit, determine the cursor
119-
// is exhausted and return a cursorId of zero.
120-
// instead, if we determine there are no more documents to request from the server, we preemptively
121-
// close the cursor
122-
}
123-
return CursorResponse.emptyGetMore;
103+
const limit = this.findOptions.limit;
104+
105+
if (numReturned && limit && numReturned >= limit) {
106+
try {
107+
await this.close();
108+
} catch (error) {
109+
squashError(error);
110+
// this is an optimization for the special case of a limit for a find command to avoid an
111+
// extra getMore when the limit has been reached and the limit is a multiple of the batchSize.
112+
// This is a consequence of the new query engine in 5.0 having no knowledge of the limit as it
113+
// produces results for the find command. Once a batch is filled up, it is returned and only
114+
// on the subsequent getMore will the query framework consider the limit, determine the cursor
115+
// is exhausted and return a cursorId of zero.
116+
// instead, if we determine there are no more documents to request from the server, we preemptively
117+
// close the cursor
124118
}
119+
return CursorResponse.emptyGetMore;
125120
}
126121

127-
const response = await super.getMore(batchSize);
128-
// TODO: wrap this in some logic to prevent it from happening if we don't need this support
122+
const response = await super.getMore();
123+
129124
this.numReturned = this.numReturned + response.batchSize;
130125

131126
return response;

src/cursor/run_command_cursor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ export class RunCommandCursor extends AbstractCursor {
159159
}
160160

161161
/** @internal */
162-
override async getMore(_batchSize: number): Promise<CursorResponse> {
162+
override async getMore(): Promise<CursorResponse> {
163163
if (!this.session) {
164164
throw new MongoRuntimeError(
165165
'Unexpected null session. A cursor creating command should have set this'

0 commit comments

Comments
 (0)