Skip to content

Commit 0afe736

Browse files
mbroadstdaprahamian
authored andcommitted
refactor: use new command base for estimatedDocumentCount
1 parent 26e0bfa commit 0afe736

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

lib/operations/estimated_document_count.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,44 @@
22

33
const Aspect = require('./operation').Aspect;
44
const defineAspects = require('./operation').defineAspects;
5-
const CommandOperation = require('./command');
6-
const buildCountCommand = require('./common_functions').buildCountCommand;
7-
const handleCallback = require('../utils').handleCallback;
5+
const CommandOperationV2 = require('./command_v2');
86

9-
class EstimatedDocumentCountOperation extends CommandOperation {
7+
class EstimatedDocumentCountOperation extends CommandOperationV2 {
108
constructor(collection, options) {
11-
super(collection.s.db, options, collection);
9+
super(collection, options);
10+
this.collectionName = collection.s.namespace.collection;
1211
}
1312

14-
_buildCommand() {
15-
const coll = this.collection;
16-
let options = this.options;
13+
execute(server, callback) {
14+
const options = this.options;
15+
const cmd = { count: this.collectionName };
16+
if (typeof options.skip === 'number') {
17+
cmd.skip = options.skip;
18+
}
1719

18-
options.collectionName = coll.collectionName;
20+
if (typeof options.limit === 'number') {
21+
cmd.limit = options.limit;
22+
}
1923

20-
return buildCountCommand(coll, null, options);
21-
}
24+
if (options.hint) {
25+
cmd.hint = options.hint;
26+
}
27+
28+
super.executeCommand(server, cmd, (err, response) => {
29+
if (err) {
30+
callback(err);
31+
return;
32+
}
2233

23-
execute(callback) {
24-
super.execute((err, result) => {
25-
if (err) return handleCallback(callback, err);
26-
handleCallback(callback, null, result.n);
34+
callback(null, response.n);
2735
});
2836
}
2937
}
3038

31-
defineAspects(EstimatedDocumentCountOperation, Aspect.READ_OPERATION);
39+
defineAspects(EstimatedDocumentCountOperation, [
40+
Aspect.READ_OPERATION,
41+
Aspect.RETRYABLE,
42+
Aspect.EXECUTE_WITH_SELECTION
43+
]);
3244

3345
module.exports = EstimatedDocumentCountOperation;

test/functional/retryable_reads_tests.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ describe('Retryable Reads', function() {
2323
spec.description.match(/listDatabases/i) ||
2424
spec.description.match(/listDatabaseNames/i) ||
2525
spec.description.match(/listCollections/i) ||
26-
spec.description.match(/listCollectionNames/i)
26+
spec.description.match(/listCollectionNames/i) ||
27+
spec.description.match(/estimatedDocumentCount/i)
2728
);
2829
});
2930
});

0 commit comments

Comments
 (0)