From 590e4b7eb9c5db2e65c15fe47778a002a4af8fa1 Mon Sep 17 00:00:00 2001 From: emadum Date: Tue, 15 Dec 2020 18:32:17 -0500 Subject: [PATCH 1/5] feat(FindCursor): fluent builder for allowDiskUse option --- src/cursor/find_cursor.ts | 12 +++++++++++ src/operations/find.ts | 2 +- test/functional/cursor.test.js | 37 +++++++++++++++++++++++++++++++++- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/cursor/find_cursor.ts b/src/cursor/find_cursor.ts index d6dbf0567c..36d261465d 100644 --- a/src/cursor/find_cursor.ts +++ b/src/cursor/find_cursor.ts @@ -353,6 +353,18 @@ export class FindCursor extends AbstractCursor { return this; } + /** + * Allows disk use for blocking sort operations exceeding 100MB memory. + */ + allowDiskUse(): this { + assertUninitialized(this); + if (!this[kBuiltOptions].sort) { + throw new MongoError('allowDiskUse must follow sort'); + } + this[kBuiltOptions].allowDiskUse = true; + return this; + } + /** * Set the collation options for the cursor. * diff --git a/src/operations/find.ts b/src/operations/find.ts index 292ccf51f4..3588ceaeff 100644 --- a/src/operations/find.ts +++ b/src/operations/find.ts @@ -52,7 +52,7 @@ export interface FindOptions extends CommandOperationOptions { noCursorTimeout?: boolean; /** Specify collation (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields). */ collation?: CollationOptions; - /** Enables writing to temporary files on the server. */ + /** Allows disk use for blocking sort operations exceeding 100MB memory. */ allowDiskUse?: boolean; /** Determines whether to close the cursor after the first batch. Defaults to false. */ singleBatch?: boolean; diff --git a/test/functional/cursor.test.js b/test/functional/cursor.test.js index d40d7b8339..52bf2a795a 100644 --- a/test/functional/cursor.test.js +++ b/test/functional/cursor.test.js @@ -1,5 +1,5 @@ 'use strict'; -const { assert: test, filterForCommands, withMonitoredClient } = require('./shared'); +const { assert: test, filterForCommands, withClient, withMonitoredClient } = require('./shared'); const { setupDatabase } = require('./shared'); const fs = require('fs'); const { expect } = require('chai'); @@ -4089,5 +4089,40 @@ describe('Cursor', function () { beta: 1 }); }); + + it( + 'should use allowDiskUse option on sort', + withMonitoredClient('find', function (client, events, done) { + const db = client.db('test'); + db.collection('test_sort_allow_disk_use', (err, collection) => { + expect(err).to.not.exist; + const cursor = collection.find({}).sort(['alpha', 1]).allowDiskUse(); + cursor.next(err => { + expect(err).to.not.exist; + const { command } = events.shift(); + expect(command.sort).to.deep.equal({ alpha: 1 }); + expect(command.allowDiskUse).to.be.true; + cursor.close(done); + }); + }); + }) + ); + + it( + 'should error if allowDiskUse option used without sort', + withClient(function (client, done) { + const db = client.db('test'); + db.collection('test_sort_allow_disk_use', (err, collection) => { + expect(err).to.not.exist; + try { + collection.find({}).allowDiskUse(); + } catch (err) { + expect(err).to.exist; + expect(err.message).to.equal('allowDiskUse must follow sort'); + done(); + } + }); + }) + ); }); }); From 9dfe11195ded5d3875679bd478275173d44a43db Mon Sep 17 00:00:00 2001 From: emadum Date: Tue, 15 Dec 2020 19:06:37 -0500 Subject: [PATCH 2/5] skip allowDiskUse tests before mongo 4.4 --- package.json | 2 +- test/functional/cursor.test.js | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index a98d1f0521..750e997896 100644 --- a/package.json +++ b/package.json @@ -100,7 +100,7 @@ "check:test": "mocha --recursive test/functional test/unit", "check:ts": "tsc -v && tsc --noEmit", "check:atlas": "mocha --config \"test/manual/mocharc.json\" test/manual/atlas_connectivity.test.js", - "check:adl": "mocha test/manual/data_lake.test.js", + "check:adl": "mocha --exit test/manual/data_lake.test.js", "check:ocsp": "mocha --config \"test/manual/mocharc.json\" test/manual/ocsp_support.test.js", "check:kerberos": "mocha --config \"test/manual/mocharc.json\" test/manual/kerberos.test.js", "check:tls": "mocha --config \"test/manual/mocharc.json\" test/manual/tls_support.test.js", diff --git a/test/functional/cursor.test.js b/test/functional/cursor.test.js index 52bf2a795a..ae43c7fe77 100644 --- a/test/functional/cursor.test.js +++ b/test/functional/cursor.test.js @@ -4090,9 +4090,9 @@ describe('Cursor', function () { }); }); - it( - 'should use allowDiskUse option on sort', - withMonitoredClient('find', function (client, events, done) { + it('should use allowDiskUse option on sort', { + metadata: { requires: { mongodb: '>=4.4' } }, + test: withMonitoredClient('find', function (client, events, done) { const db = client.db('test'); db.collection('test_sort_allow_disk_use', (err, collection) => { expect(err).to.not.exist; @@ -4106,11 +4106,11 @@ describe('Cursor', function () { }); }); }) - ); + }); - it( - 'should error if allowDiskUse option used without sort', - withClient(function (client, done) { + it('should error if allowDiskUse option used without sort', { + metadata: { requires: { mongodb: '>=4.4' } }, + test: withClient(function (client, done) { const db = client.db('test'); db.collection('test_sort_allow_disk_use', (err, collection) => { expect(err).to.not.exist; @@ -4123,6 +4123,6 @@ describe('Cursor', function () { } }); }) - ); + }); }); }); From 3caf3be52d63093fb6d137f8a448eb1095063dff Mon Sep 17 00:00:00 2001 From: emadum Date: Thu, 17 Dec 2020 13:08:35 -0500 Subject: [PATCH 3/5] review feedback --- src/cursor/find_cursor.ts | 7 +++++-- src/operations/find.ts | 2 +- test/functional/cursor.test.js | 11 ++++------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/cursor/find_cursor.ts b/src/cursor/find_cursor.ts index 36d261465d..b760deccbf 100644 --- a/src/cursor/find_cursor.ts +++ b/src/cursor/find_cursor.ts @@ -354,12 +354,15 @@ export class FindCursor extends AbstractCursor { } /** - * Allows disk use for blocking sort operations exceeding 100MB memory. + * Allows disk use for blocking sort operations exceeding 100MB memory. (MongoDB 3.2 or higher) + * + * @remarks + * {@link https://docs.mongodb.com/manual/reference/command/find/#find-cmd-allowdiskuse | find command allowDiskUse documentation} */ allowDiskUse(): this { assertUninitialized(this); if (!this[kBuiltOptions].sort) { - throw new MongoError('allowDiskUse must follow sort'); + throw new MongoError('allowDiskUse requires a sort specification'); } this[kBuiltOptions].allowDiskUse = true; return this; diff --git a/src/operations/find.ts b/src/operations/find.ts index 3588ceaeff..643ba6b3b7 100644 --- a/src/operations/find.ts +++ b/src/operations/find.ts @@ -52,7 +52,7 @@ export interface FindOptions extends CommandOperationOptions { noCursorTimeout?: boolean; /** Specify collation (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields). */ collation?: CollationOptions; - /** Allows disk use for blocking sort operations exceeding 100MB memory. */ + /** Allows disk use for blocking sort operations exceeding 100MB memory. (MongoDB 3.2 or higher) */ allowDiskUse?: boolean; /** Determines whether to close the cursor after the first batch. Defaults to false. */ singleBatch?: boolean; diff --git a/test/functional/cursor.test.js b/test/functional/cursor.test.js index ae43c7fe77..765ca150a2 100644 --- a/test/functional/cursor.test.js +++ b/test/functional/cursor.test.js @@ -4114,13 +4114,10 @@ describe('Cursor', function () { const db = client.db('test'); db.collection('test_sort_allow_disk_use', (err, collection) => { expect(err).to.not.exist; - try { - collection.find({}).allowDiskUse(); - } catch (err) { - expect(err).to.exist; - expect(err.message).to.equal('allowDiskUse must follow sort'); - done(); - } + expect(collection.find({}).allowDiskUse()).to.throw( + /allowDiskUse requires a sort specification/ + ); + done(); }); }) }); From 1fb0aae594d30befa4d93405aba784012f936e43 Mon Sep 17 00:00:00 2001 From: emadum Date: Thu, 17 Dec 2020 13:28:37 -0500 Subject: [PATCH 4/5] fix --- test/functional/cursor.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/functional/cursor.test.js b/test/functional/cursor.test.js index 6758e1ba65..07955eb896 100644 --- a/test/functional/cursor.test.js +++ b/test/functional/cursor.test.js @@ -4114,7 +4114,7 @@ describe('Cursor', function () { const db = client.db('test'); db.collection('test_sort_allow_disk_use', (err, collection) => { expect(err).to.not.exist; - expect(collection.find({}).allowDiskUse()).to.throw( + expect(() => collection.find({}).allowDiskUse()).to.throw( /allowDiskUse requires a sort specification/ ); done(); From cd52f7088e17e1bd112bcbe32df0a3fa5107faff Mon Sep 17 00:00:00 2001 From: emadum Date: Thu, 17 Dec 2020 14:44:31 -0500 Subject: [PATCH 5/5] remove --exit from adl test --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 102d259386..96a72e4d04 100644 --- a/package.json +++ b/package.json @@ -98,7 +98,7 @@ "check:test": "mocha --recursive test/functional test/unit", "check:ts": "tsc -v && tsc --noEmit", "check:atlas": "mocha --config \"test/manual/mocharc.json\" test/manual/atlas_connectivity.test.js", - "check:adl": "mocha --exit test/manual/data_lake.test.js", + "check:adl": "mocha test/manual/data_lake.test.js", "check:ocsp": "mocha --config \"test/manual/mocharc.json\" test/manual/ocsp_support.test.js", "check:kerberos": "mocha --config \"test/manual/mocharc.json\" test/manual/kerberos.test.js", "check:tls": "mocha --config \"test/manual/mocharc.json\" test/manual/tls_support.test.js",