Skip to content

Commit 3f74929

Browse files
authored
fix(shell-api): configurable batch size option applies only to display MONGOSH-877 (#992)
1 parent 5a07823 commit 3f74929

21 files changed

+101
-102
lines changed

packages/cli-repl/src/cli-repl.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ describe('CliRepl', () => {
191191

192192
it('returns the list of available config options when asked to', () => {
193193
expect(cliRepl.listConfigOptions()).to.deep.equal([
194-
'batchSize',
194+
'displayBatchSize',
195195
'enableTelemetry',
196196
'snippetIndexSourceURLs',
197197
'snippetRegistryURL',

packages/i18n/src/locales/en_US.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ const translations: Catalog = {
251251
description: 'Deprecated. The shell provides auto-formatting so this method is no longer useful'
252252
},
253253
batchSize: {
254-
description: 'Specifies the number of documents that mongosh displays at once.',
254+
description: 'Specifies the number of documents to return in each batch of the response from the MongoDB instance.',
255255
example: 'db.collection.aggregate(pipeline, options).batchSize(10)'
256256
},
257257
projection: {
@@ -711,10 +711,6 @@ const translations: Catalog = {
711711
},
712712
isExhausted: {
713713
description: 'This method is deprecated because because after closing a cursor, the remaining documents in the batch are no longer accessible. If you want to see if the cursor is closed use cursor.isClosed. If you want to see if there are documents left in the batch, use cursor.tryNext. This is a breaking change'
714-
},
715-
batchSize: {
716-
description: 'Specifies the number of documents that mongosh displays at once.',
717-
example: 'db.collection.aggregate(pipeline, options).batchSize(10)'
718714
}
719715
}
720716
}
@@ -744,7 +740,7 @@ const translations: Catalog = {
744740
},
745741
batchSize: {
746742
link: 'https://docs.mongodb.com/manual/reference/method/cursor.batchSize',
747-
description: 'Specifies the number of documents to return in each batch of the response from the MongoDB instance. In most cases, modifying the batch size will not affect the user or the application, as the mongo shell and most drivers return results as if MongoDB returned a single batch. This also specifies how many entries mongosh displays at once.',
743+
description: 'Specifies the number of documents to return in each batch of the response from the MongoDB instance. In most cases, modifying the batch size will not affect the user or the application, as the mongo shell and most drivers return results as if MongoDB returned a single batch.',
748744
example: 'db.collection.find(query, projection).batchSize(10)'
749745
},
750746
close: {
@@ -1296,7 +1292,7 @@ const translations: Catalog = {
12961292
},
12971293
DBQuery: {
12981294
help: {
1299-
description: 'Deprecated -- use cursor.batchSize(value) or config.set("batchSize", value) instead of DBQuery.batchSize = value',
1295+
description: 'Deprecated -- use config.set("displayBatchSize", value) instead of DBQuery.shellBatchSize = value',
13001296
}
13011297
},
13021298
Explainable: {

packages/node-runtime-worker-thread/src/worker-runtime.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ describe('worker', () => {
496496
getConfig() {},
497497
setConfig() {},
498498
validateConfig() {},
499-
listConfigOptions() { return ['batchSize']; },
499+
listConfigOptions() { return ['displayBatchSize']; },
500500
onRunInterruptible() {}
501501
};
502502

@@ -576,9 +576,9 @@ describe('worker', () => {
576576

577577
await init('mongodb://nodb/', {}, { nodb: true });
578578

579-
await evaluate('config.set("batchSize", 200)');
580-
expect(evalListener.validateConfig).to.have.been.calledWith('batchSize', 200);
581-
expect(evalListener.setConfig).to.have.been.calledWith('batchSize', 200);
579+
await evaluate('config.set("displayBatchSize", 200)');
580+
expect(evalListener.validateConfig).to.have.been.calledWith('displayBatchSize', 200);
581+
expect(evalListener.setConfig).to.have.been.calledWith('displayBatchSize', 200);
582582
});
583583
});
584584

packages/shell-api/src/abstract-cursor.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@ import { CursorIterationResult } from './result';
1818
import { iterate, validateExplainableVerbosity, markAsExplainOutput } from './helpers';
1919

2020
@shellApiClassNoHelp
21-
export abstract class AbstractCursor extends ShellApiWithMongoClass {
21+
export abstract class AbstractCursor<CursorType extends ServiceProviderAggregationCursor | ServiceProviderCursor> extends ShellApiWithMongoClass {
2222
_mongo: Mongo;
23-
abstract _cursor: ServiceProviderAggregationCursor | ServiceProviderCursor;
23+
_cursor: CursorType;
24+
2425
_currentIterationResult: CursorIterationResult | null = null;
25-
_batchSize: number | null = null;
2626
_mapError: Error | null = null;
2727

28-
constructor(mongo: Mongo) {
28+
constructor(mongo: Mongo, cursor: CursorType) {
2929
super();
3030
this._mongo = mongo;
31+
this._cursor = cursor;
3132
}
3233

3334
// Wrap a function with checks before and after that verify whether a .map()
@@ -63,14 +64,14 @@ export abstract class AbstractCursor extends ShellApiWithMongoClass {
6364

6465
async _it(): Promise<CursorIterationResult> {
6566
const results = this._currentIterationResult = new CursorIterationResult();
66-
await iterate(results, this, this._batchSize ?? await this._mongo._batchSize());
67+
await iterate(results, this, await this._mongo._displayBatchSize());
6768
results.cursorHasMore = !this.isExhausted();
6869
return results;
6970
}
7071

7172
@returnType('this')
7273
batchSize(size: number): this {
73-
this._batchSize = size;
74+
this._cursor.batchSize(size);
7475
return this;
7576
}
7677

packages/shell-api/src/aggregation-cursor.spec.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe('AggregationCursor', () => {
4545
};
4646
cursor = new AggregationCursor({
4747
_serviceProvider: { platform: ReplPlatform.CLI },
48-
_batchSize: () => 20
48+
_displayBatchSize: () => 20
4949
} as any, wrappee);
5050
});
5151

@@ -75,7 +75,7 @@ describe('AggregationCursor', () => {
7575

7676
describe('Cursor Internals', () => {
7777
const mongo = {
78-
_batchSize: () => 20
78+
_displayBatchSize: () => 20
7979
} as any;
8080
describe('#close', () => {
8181
let spCursor: StubbedInstance<SPAggregationCursor>;
@@ -212,8 +212,9 @@ describe('AggregationCursor', () => {
212212
});
213213

214214
describe('toShellResult', () => {
215-
let shellApiCursor;
216-
let i;
215+
let shellApiCursor: AggregationCursor;
216+
let i: number;
217+
let batchSize: number | undefined;
217218

218219
beforeEach(() => {
219220
i = 0;
@@ -226,6 +227,9 @@ describe('AggregationCursor', () => {
226227
if (prop === 'tryNext') {
227228
return async() => ({ key: i++ });
228229
}
230+
if (prop === 'batchSize') {
231+
return (size: number) => { batchSize = size; };
232+
}
229233
return (target as any)[prop];
230234
}
231235
});
@@ -243,11 +247,12 @@ describe('AggregationCursor', () => {
243247
expect(i).to.equal(40);
244248
});
245249

246-
it('lets .batchSize() control the output length', async() => {
250+
it('.batchSize() does not control the output length', async() => {
247251
shellApiCursor.batchSize(10);
248252
const result = (await toShellResult(shellApiCursor)).printable;
249-
expect(i).to.equal(10);
250-
expect(result).to.have.nested.property('documents.length', 10);
253+
expect(i).to.equal(20);
254+
expect(batchSize).to.equal(10);
255+
expect(result).to.have.nested.property('documents.length', 20);
251256
});
252257
});
253258
});

packages/shell-api/src/aggregation-cursor.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@ import type {
88
import { AbstractCursor } from './abstract-cursor';
99

1010
@shellApiClassDefault
11-
export default class AggregationCursor extends AbstractCursor {
12-
_cursor: ServiceProviderAggregationCursor;
13-
11+
export default class AggregationCursor extends AbstractCursor<ServiceProviderAggregationCursor> {
1412
constructor(mongo: Mongo, cursor: ServiceProviderAggregationCursor) {
15-
super(mongo);
16-
this._cursor = cursor;
13+
super(mongo, cursor);
1714
}
1815
}

packages/shell-api/src/change-stream-cursor.spec.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,6 @@ describe('ChangeStreamCursor', () => {
9898
expect(spCursor.next.calledWith()).to.equal(true);
9999
expect(warnSpy.calledOnce).to.equal(true);
100100
});
101-
it('lets .batchSize() control iteration batch size', async() => {
102-
const cursor2 = new ChangeStreamCursor({
103-
tryNext: sinon.stub().resolves({ doc: 1 })
104-
} as any, 'source', {
105-
_internalState: {}
106-
} as Mongo);
107-
cursor2.batchSize(3);
108-
const results = await cursor2._it();
109-
expect(results.documents).to.deep.equal([{ doc: 1 }, { doc: 1 }, { doc: 1 }]);
110-
});
111101
});
112102
describe('integration', () => {
113103
const [ srv0 ] = startTestCluster(['--replicaset'] );

packages/shell-api/src/change-stream-cursor.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export default class ChangeStreamCursor extends ShellApiWithMongoClass {
2727
_cursor: ChangeStream<Document>;
2828
_currentIterationResult: CursorIterationResult | null = null;
2929
_on: string;
30-
_batchSize: number | null = null;
3130

3231
constructor(cursor: ChangeStream<Document>, on: string, mongo: Mongo) {
3332
super();
@@ -41,7 +40,7 @@ export default class ChangeStreamCursor extends ShellApiWithMongoClass {
4140
throw new MongoshRuntimeError('ChangeStreamCursor is closed');
4241
}
4342
const result = this._currentIterationResult = new CursorIterationResult();
44-
return iterate(result, this, this._batchSize ?? await this._mongo._batchSize());
43+
return iterate(result, this, await this._mongo._displayBatchSize());
4544
}
4645

4746
/**
@@ -130,10 +129,4 @@ export default class ChangeStreamCursor extends ShellApiWithMongoClass {
130129
pretty(): ChangeStreamCursor {
131130
return this;
132131
}
133-
134-
@returnType('ChangeStreamCursor')
135-
batchSize(size: number): ChangeStreamCursor {
136-
this._batchSize = size;
137-
return this;
138-
}
139132
}

packages/shell-api/src/collection.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ describe('Collection', () => {
137137
expect(serviceProvider.aggregate).to.have.been.calledWith(
138138
collection._database._name,
139139
collection._name,
140-
[{ $piplelineStage: {} }],
140+
[{ $piplelineStage: {} } ],
141141
{}
142142
);
143143
});
@@ -169,13 +169,13 @@ describe('Collection', () => {
169169
it('calls serviceProvider.aggregate with pipleline and options', async() => {
170170
await collection.aggregate(
171171
[{ $piplelineStage: {} }],
172-
{ options: true });
172+
{ options: true, batchSize: 10 });
173173

174174
expect(serviceProvider.aggregate).to.have.been.calledWith(
175175
collection._database._name,
176176
collection._name,
177177
[{ $piplelineStage: {} }],
178-
{ options: true }
178+
{ options: true, batchSize: 10 }
179179
);
180180
});
181181

packages/shell-api/src/collection.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,12 @@ export default class Collection extends ShellApiWithMongoClass {
414414
this._emitCollectionApiCall('find', { query, options });
415415
const cursor = new Cursor(
416416
this._mongo,
417-
this._mongo._serviceProvider.find(this._database._name, this._name, query, { ...this._database._baseOptions, ...options })
417+
this._mongo._serviceProvider.find(
418+
this._database._name,
419+
this._name,
420+
query,
421+
{ ...this._database._baseOptions, ...options }
422+
)
418423
);
419424

420425
this._mongo._internalState.currentCursor = cursor;
@@ -470,7 +475,12 @@ export default class Collection extends ShellApiWithMongoClass {
470475
this._emitCollectionApiCall('findOne', { query, options });
471476
return new Cursor(
472477
this._mongo,
473-
this._mongo._serviceProvider.find(this._database._name, this._name, query, { ...this._database._baseOptions, ...options })
478+
this._mongo._serviceProvider.find(
479+
this._database._name,
480+
this._name,
481+
query,
482+
{ ...this._database._baseOptions, ...options }
483+
)
474484
).limit(1).tryNext();
475485
}
476486

0 commit comments

Comments
 (0)