Skip to content

Commit

Permalink
second pass implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
emadum committed Mar 26, 2021
1 parent e10849e commit b76612f
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 983 deletions.
3 changes: 1 addition & 2 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,6 @@ export class Collection {
callback?: Callback<number>
): Promise<number> | void {
if (typeof options === 'function') (callback = options), (options = {});

return executeOperation(
getTopology(this),
new EstimatedDocumentCountOperation(this, resolveOptions(this, options)),
Expand Down Expand Up @@ -1430,7 +1429,7 @@ export class Collection {
query = query || {};
return executeOperation(
getTopology(this),
new EstimatedDocumentCountOperation(this, query, resolveOptions(this, options)),
new CountDocumentsOperation(this, query, resolveOptions(this, options)),
callback
);
}
Expand Down
89 changes: 31 additions & 58 deletions src/operations/estimated_document_count.ts
Original file line number Diff line number Diff line change
@@ -1,90 +1,63 @@
import { Aspect, defineAspects, Hint } from './operation';
import { Aspect, defineAspects } from './operation';
import { CommandOperation, CommandOperationOptions } from './command';
import { Callback, maxWireVersion } from '../utils';
import type { Document } from '../bson';
import type { Server } from '../sdam/server';
import type { Collection } from '../collection';
import type { ClientSession } from '../sessions';

/**
* All supported options, including legacy options
* @public
*/
export interface EstimatedDocumentCountOptions extends EstimatedDocumentCountOptionsV1 {
skip?: number;
limit?: number;
hint?: Hint;
}

/**
* Options supported by Server API Version 1
* @public
*/
export interface EstimatedDocumentCountOptionsV1 extends CommandOperationOptions {
/** specifies a cumulative time limit in milliseconds for processing operations on the cursor. MongoDB interrupts the operation at the earliest following interrupt point. */
import type { MongoError } from '../error';

/** @public */
export interface EstimatedDocumentCountOptions extends CommandOperationOptions {
/**
* The maximum amount of time to allow the operation to run.
*
* This option is sent only if the caller explicitly provides a value. The default is to not send a value.
*/
maxTimeMS?: number;
}

/** @internal */
export class EstimatedDocumentCountOperation extends CommandOperation<number> {
options: EstimatedDocumentCountOptions;
collectionName: string;
query?: Document;

constructor(collection: Collection, options: EstimatedDocumentCountOptions);
constructor(collection: Collection, query: Document, options: EstimatedDocumentCountOptions);
constructor(
collection: Collection,
query?: Document | EstimatedDocumentCountOptions,
options?: EstimatedDocumentCountOptions
) {
constructor(collection: Collection, options?: EstimatedDocumentCountOptions) {
if (typeof options === 'undefined') {
options = query as EstimatedDocumentCountOptions;
query = undefined;
options = {} as EstimatedDocumentCountOptions;
}

super(collection, options);
this.options = options;
this.collectionName = collection.collectionName;
if (query) {
this.query = query;
}
}

execute(server: Server, session: ClientSession, callback: Callback<number>): void {
const options = this.options;

let cmd: Document;

if (maxWireVersion(server) > 11) {
const pipeline = [
{ $collStats: { count: {} } },
{ $group: { _id: 1, n: { $sum: '$count' } } }
];
if (maxWireVersion(server) < 12) {
return this.executeLegacy(server, session, callback);
}
const pipeline = [{ $collStats: { count: {} } }, { $group: { _id: 1, n: { $sum: '$count' } } }];

cmd = { aggregate: this.collectionName, pipeline, cursor: {} };
const cmd: Document = { aggregate: this.collectionName, pipeline, cursor: {} };

if (typeof options.maxTimeMS === 'number') {
cmd.maxTimeMS = options.maxTimeMS;
}
} else {
cmd = { count: this.collectionName };
if (typeof this.options.maxTimeMS === 'number') {
cmd.maxTimeMS = this.options.maxTimeMS;
}

if (this.query) {
cmd.query = this.query;
super.executeCommand(server, session, cmd, (err, response) => {
if (err && (err as MongoError).code !== 26) {
callback(err);
return;
}

if (typeof options.skip === 'number') {
cmd.skip = options.skip;
}
callback(undefined, response?.cursor?.firstBatch[0]?.n || 0);
});
}

if (typeof options.limit === 'number') {
cmd.limit = options.limit;
}
executeLegacy(server: Server, session: ClientSession, callback: Callback<number>): void {
const cmd: Document = { count: this.collectionName };

if (options.hint) {
cmd.hint = options.hint;
}
if (typeof this.options.maxTimeMS === 'number') {
cmd.maxTimeMS = this.options.maxTimeMS;
}

super.executeCommand(server, session, cmd, (err, response) => {
Expand Down
Loading

0 comments on commit b76612f

Please sign in to comment.