Skip to content

Commit 7e80bfe

Browse files
mbroadstdaprahamian
authored andcommitted
refactor(cursor): move all wire protocol calls to Server
The primitive wire protocol methods are not meant to be used directly outside of the `Server` class(es). The fact that the cursors are doing so makes it difficult to provide consistent error handling behavior required by SDAM/sessions/etc. This patch moves these methods to the `Server`, with an eye towards eventually moving them to `Connection`. NODE-2066
1 parent 95a7d05 commit 7e80bfe

File tree

3 files changed

+94
-19
lines changed

3 files changed

+94
-19
lines changed

lib/core/cursor.js

+4-19
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const MongoNetworkError = require('./error').MongoNetworkError;
77
const mongoErrorContextSymbol = require('./error').mongoErrorContextSymbol;
88
const f = require('util').format;
99
const collationNotSupported = require('./utils').collationNotSupported;
10-
const wireProtocol = require('./wireprotocol');
1110
const BSON = retrieveBSON();
1211
const Long = BSON.Long;
1312

@@ -195,7 +194,7 @@ Cursor.prototype._getMore = function(callback) {
195194
batchSize = this.cursorState.limit - this.cursorState.currentLimit;
196195
}
197196

198-
wireProtocol.getMore(this.server, this.ns, this.cursorState, batchSize, this.options, callback);
197+
this.server.getMore(this.ns, this.cursorState, batchSize, this.options, callback);
199198
};
200199

201200
/**
@@ -304,7 +303,7 @@ Cursor.prototype.kill = function(callback) {
304303
return;
305304
}
306305

307-
wireProtocol.killCursors(this.server, this.ns, this.cursorState, callback);
306+
this.server.killCursors(this.ns, this.cursorState, callback);
308307
};
309308

310309
/**
@@ -716,25 +715,11 @@ Cursor.prototype._initializeCursor = function(callback) {
716715
}
717716

718717
if (cursor.cmd.find != null) {
719-
wireProtocol.query(
720-
cursor.server,
721-
cursor.ns,
722-
cursor.cmd,
723-
cursor.cursorState,
724-
cursor.options,
725-
queryCallback
726-
);
727-
718+
server.query(cursor.ns, cursor.cmd, cursor.cursorState, cursor.options, queryCallback);
728719
return;
729720
}
730721

731-
cursor.query = wireProtocol.command(
732-
cursor.server,
733-
cursor.ns,
734-
cursor.cmd,
735-
cursor.options,
736-
queryCallback
737-
);
722+
server.command(cursor.ns, cursor.cmd, cursor.options, queryCallback);
738723
});
739724
};
740725

lib/core/sdam/server.js

+55
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,61 @@ class Server extends EventEmitter {
248248
});
249249
}
250250

251+
/**
252+
* Execute a query against the server
253+
*
254+
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
255+
* @param {object} cmd The command document for the query
256+
* @param {object} options Optional settings
257+
* @param {function} callback
258+
*/
259+
query(ns, cmd, cursorState, options, callback) {
260+
wireProtocol.query(this, ns, cmd, cursorState, options, (err, result) => {
261+
if (err && isSDAMUnrecoverableError(err)) {
262+
this.emit('error', err);
263+
}
264+
265+
callback(err, result);
266+
});
267+
}
268+
269+
/**
270+
* Execute a `getMore` against the server
271+
*
272+
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
273+
* @param {object} cursorState State data associated with the cursor calling this method
274+
* @param {object} options Optional settings
275+
* @param {function} callback
276+
*/
277+
getMore(ns, cursorState, batchSize, options, callback) {
278+
wireProtocol.getMore(this, ns, cursorState, batchSize, options, (err, result) => {
279+
if (err && isSDAMUnrecoverableError(err)) {
280+
this.emit('error', err);
281+
}
282+
283+
callback(err, result);
284+
});
285+
}
286+
287+
/**
288+
* Execute a `killCursors` command against the server
289+
*
290+
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
291+
* @param {object} cursorState State data associated with the cursor calling this method
292+
* @param {function} callback
293+
*/
294+
killCursors(ns, cursorState, callback) {
295+
wireProtocol.killCursors(this, ns, cursorState, (err, result) => {
296+
if (err && isSDAMUnrecoverableError(err)) {
297+
this.emit('error', err);
298+
}
299+
300+
if (typeof callback === 'function') {
301+
callback(err, result);
302+
}
303+
});
304+
}
305+
251306
/**
252307
* Insert one or more documents
253308
* @method

lib/core/topologies/server.js

+35
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,41 @@ Server.prototype.command = function(ns, cmd, options, callback) {
628628
wireProtocol.command(self, ns, cmd, options, callback);
629629
};
630630

631+
/**
632+
* Execute a query against the server
633+
*
634+
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
635+
* @param {object} cmd The command document for the query
636+
* @param {object} options Optional settings
637+
* @param {function} callback
638+
*/
639+
Server.prototype.query = function(ns, cmd, cursorState, options, callback) {
640+
wireProtocol.query(this, ns, cmd, cursorState, options, callback);
641+
};
642+
643+
/**
644+
* Execute a `getMore` against the server
645+
*
646+
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
647+
* @param {object} cursorState State data associated with the cursor calling this method
648+
* @param {object} options Optional settings
649+
* @param {function} callback
650+
*/
651+
Server.prototype.getMore = function(ns, cursorState, batchSize, options, callback) {
652+
wireProtocol.getMore(this, ns, cursorState, batchSize, options, callback);
653+
};
654+
655+
/**
656+
* Execute a `killCursors` command against the server
657+
*
658+
* @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
659+
* @param {object} cursorState State data associated with the cursor calling this method
660+
* @param {function} callback
661+
*/
662+
Server.prototype.killCursors = function(ns, cursorState, callback) {
663+
wireProtocol.killCursors(this, ns, cursorState, callback);
664+
};
665+
631666
/**
632667
* Insert one or more documents
633668
* @method

0 commit comments

Comments
 (0)