Skip to content

Commit c4d636d

Browse files
committed
Updated ListDatabaseOperation to support new 3.6 features
Added support for filters and the nameOnly flag JAVA-2597
1 parent 7addf8d commit c4d636d

20 files changed

+220
-93
lines changed

driver-async/src/main/com/mongodb/async/client/FindIterableImpl.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import com.mongodb.operation.AsyncReadOperation;
2929
import com.mongodb.operation.FindOperation;
3030
import com.mongodb.session.ClientSession;
31-
import org.bson.BsonDocument;
3231
import org.bson.codecs.configuration.CodecRegistry;
3332
import org.bson.conversions.Bson;
3433

@@ -238,9 +237,9 @@ private FindOperation<TResult> createFindOperation() {
238237
.limit(findOptions.getLimit())
239238
.maxTime(findOptions.getMaxTime(MILLISECONDS), MILLISECONDS)
240239
.maxAwaitTime(findOptions.getMaxAwaitTime(MILLISECONDS), MILLISECONDS)
241-
.modifiers(toBsonDocument(findOptions.getModifiers()))
242-
.projection(toBsonDocument(findOptions.getProjection()))
243-
.sort(toBsonDocument(findOptions.getSort()))
240+
.modifiers(toBsonDocumentOrNull(findOptions.getModifiers(), documentClass, codecRegistry))
241+
.projection(toBsonDocumentOrNull(findOptions.getProjection(), documentClass, codecRegistry))
242+
.sort(toBsonDocumentOrNull(findOptions.getSort(), documentClass, codecRegistry))
244243
.cursorType(findOptions.getCursorType())
245244
.noCursorTimeout(findOptions.isNoCursorTimeout())
246245
.oplogReplay(findOptions.isOplogReplay())
@@ -249,17 +248,12 @@ private FindOperation<TResult> createFindOperation() {
249248
.readConcern(getReadConcern())
250249
.collation(findOptions.getCollation())
251250
.comment(findOptions.getComment())
252-
.hint(toBsonDocument(findOptions.getHint()))
253-
.min(toBsonDocument(findOptions.getMin()))
254-
.max(toBsonDocument(findOptions.getMax()))
251+
.hint(toBsonDocumentOrNull(findOptions.getHint(), documentClass, codecRegistry))
252+
.min(toBsonDocumentOrNull(findOptions.getMin(), documentClass, codecRegistry))
253+
.max(toBsonDocumentOrNull(findOptions.getMax(), documentClass, codecRegistry))
255254
.maxScan(findOptions.getMaxScan())
256255
.returnKey(findOptions.isReturnKey())
257256
.showRecordId(findOptions.isShowRecordId())
258257
.snapshot(findOptions.isSnapshot());
259258
}
260-
261-
private BsonDocument toBsonDocument(final Bson document) {
262-
return document == null ? null : document.toBsonDocument(documentClass, codecRegistry);
263-
}
264-
265259
}

driver-async/src/main/com/mongodb/async/client/ListCollectionsIterableImpl.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.mongodb.operation.AsyncReadOperation;
2424
import com.mongodb.operation.ListCollectionsOperation;
2525
import com.mongodb.session.ClientSession;
26-
import org.bson.BsonDocument;
2726
import org.bson.codecs.configuration.CodecRegistry;
2827
import org.bson.conversions.Bson;
2928

@@ -72,7 +71,7 @@ public ListCollectionsIterable<TResult> batchSize(final int batchSize) {
7271
@Override
7372
AsyncReadOperation<AsyncBatchCursor<TResult>> asAsyncReadOperation() {
7473
ListCollectionsOperation<TResult> operation = new ListCollectionsOperation<TResult>(databaseName, codecRegistry.get(resultClass))
75-
.filter(toBsonDocument(filter))
74+
.filter(toBsonDocumentOrNull(filter, codecRegistry))
7675
.maxTime(maxTimeMS, MILLISECONDS);
7776

7877
if (getBatchSize() != null) {
@@ -82,7 +81,4 @@ AsyncReadOperation<AsyncBatchCursor<TResult>> asAsyncReadOperation() {
8281
return operation;
8382
}
8483

85-
private BsonDocument toBsonDocument(final Bson document) {
86-
return document == null ? null : document.toBsonDocument(BsonDocument.class, codecRegistry);
87-
}
8884
}

driver-async/src/main/com/mongodb/async/client/ListDatabasesIterable.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.mongodb.async.client;
1818

19+
import org.bson.conversions.Bson;
20+
1921
import java.util.concurrent.TimeUnit;
2022

2123
/**
@@ -43,4 +45,25 @@ public interface ListDatabasesIterable<T> extends MongoIterable<T> {
4345
* @mongodb.driver.manual reference/method/cursor.batchSize/#cursor.batchSize Batch Size
4446
*/
4547
ListDatabasesIterable<T> batchSize(int batchSize);
48+
49+
/**
50+
* Sets the query filter to apply to the returned database names.
51+
*
52+
* @param filter the filter, which may be null.
53+
* @return this
54+
* @since 3.6
55+
* @mongodb.server.release 3.4.2
56+
*/
57+
ListDatabasesIterable<T> filter(Bson filter);
58+
59+
/**
60+
* Sets the nameOnly flag that indicates whether the command should return just the database names or return the database names and
61+
* size information.
62+
*
63+
* @param nameOnly the nameOnly flag, which may be null
64+
* @return this
65+
* @since 3.6
66+
* @mongodb.server.release 3.4.3
67+
*/
68+
ListDatabasesIterable<T> nameOnly(Boolean nameOnly);
4669
}

driver-async/src/main/com/mongodb/async/client/ListDatabasesIterableImpl.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.mongodb.operation.ListDatabasesOperation;
2525
import com.mongodb.session.ClientSession;
2626
import org.bson.codecs.configuration.CodecRegistry;
27+
import org.bson.conversions.Bson;
2728

2829
import java.util.concurrent.TimeUnit;
2930

@@ -35,6 +36,8 @@ final class ListDatabasesIterableImpl<TResult> extends MongoIterableImpl<TResult
3536
private final CodecRegistry codecRegistry;
3637

3738
private long maxTimeMS;
39+
private Bson filter;
40+
private Boolean nameOnly;
3841

3942
ListDatabasesIterableImpl(final ClientSession clientSession, final Class<TResult> resultClass, final CodecRegistry codecRegistry,
4043
final ReadPreference readPreference, final AsyncOperationExecutor executor) {
@@ -57,8 +60,21 @@ public ListDatabasesIterable<TResult> batchSize(final int batchSize) {
5760
return this;
5861
}
5962

63+
@Override
64+
public ListDatabasesIterable<TResult> filter(final Bson filter) {
65+
this.filter = filter;
66+
return this;
67+
}
68+
69+
@Override
70+
public ListDatabasesIterable<TResult> nameOnly(final Boolean nameOnly) {
71+
this.nameOnly = nameOnly;
72+
return this;
73+
}
74+
6075
@Override
6176
AsyncReadOperation<AsyncBatchCursor<TResult>> asAsyncReadOperation() {
62-
return new ListDatabasesOperation<TResult>(codecRegistry.get(resultClass)).maxTime(maxTimeMS, MILLISECONDS);
77+
return new ListDatabasesOperation<TResult>(codecRegistry.get(resultClass)).maxTime(maxTimeMS, MILLISECONDS)
78+
.filter(toBsonDocumentOrNull(filter, codecRegistry)).nameOnly(nameOnly);
6379
}
6480
}

driver-async/src/main/com/mongodb/async/client/MapReduceIterableImpl.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import com.mongodb.operation.MapReduceToCollectionOperation;
3636
import com.mongodb.operation.MapReduceWithInlineResultsOperation;
3737
import com.mongodb.session.ClientSession;
38-
import org.bson.BsonDocument;
3938
import org.bson.BsonJavaScript;
4039
import org.bson.codecs.configuration.CodecRegistry;
4140
import org.bson.conversions.Bson;
@@ -214,12 +213,12 @@ AsyncReadOperation<AsyncBatchCursor<TResult>> asAsyncReadOperation() {
214213
private WrappedMapReduceReadOperation<TResult> createMapReduceInlineOperation() {
215214
final MapReduceWithInlineResultsOperation<TResult> operation = new MapReduceWithInlineResultsOperation<TResult>(namespace,
216215
new BsonJavaScript(mapFunction), new BsonJavaScript(reduceFunction), codecRegistry.get(resultClass))
217-
.filter(toBsonDocument(filter))
216+
.filter(toBsonDocumentOrNull(filter, documentClass, codecRegistry))
218217
.limit(limit)
219218
.maxTime(maxTimeMS, MILLISECONDS)
220219
.jsMode(jsMode)
221-
.scope(toBsonDocument(scope))
222-
.sort(toBsonDocument(sort))
220+
.scope(toBsonDocumentOrNull(scope, documentClass, codecRegistry))
221+
.sort(toBsonDocumentOrNull(sort, documentClass, codecRegistry))
223222
.verbose(verbose)
224223
.readConcern(getReadConcern())
225224
.collation(collation);
@@ -232,12 +231,12 @@ private WrappedMapReduceReadOperation<TResult> createMapReduceInlineOperation()
232231
private WrappedMapReduceWriteOperation createMapReduceToCollectionOperation() {
233232
MapReduceToCollectionOperation operation = new MapReduceToCollectionOperation(namespace, new BsonJavaScript(mapFunction),
234233
new BsonJavaScript(reduceFunction), collectionName, writeConcern)
235-
.filter(toBsonDocument(filter))
234+
.filter(toBsonDocumentOrNull(filter, documentClass, codecRegistry))
236235
.limit(limit)
237236
.maxTime(maxTimeMS, MILLISECONDS)
238237
.jsMode(jsMode)
239-
.scope(toBsonDocument(scope))
240-
.sort(toBsonDocument(sort))
238+
.scope(toBsonDocumentOrNull(scope, documentClass, codecRegistry))
239+
.sort(toBsonDocumentOrNull(sort, documentClass, codecRegistry))
241240
.verbose(verbose)
242241
.action(action.getValue())
243242
.nonAtomic(nonAtomic)
@@ -259,10 +258,6 @@ private FindOperation<TResult> createFindOperation() {
259258
.batchSize(getBatchSize() == null ? 0 : getBatchSize());
260259
}
261260

262-
private BsonDocument toBsonDocument(final Bson document) {
263-
return document == null ? null : document.toBsonDocument(documentClass, codecRegistry);
264-
}
265-
266261
// this could be inlined, but giving it a name so that it's unit-testable
267262
static class WrappedMapReduceReadOperation<TResult> implements AsyncReadOperation<AsyncBatchCursor<TResult>> {
268263
private final AsyncReadOperation<MapReduceAsyncBatchCursor<TResult>> operation;

driver-async/src/main/com/mongodb/async/client/MongoClientImpl.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,17 @@ public MongoClientSettings getSettings() {
113113

114114
@Override
115115
public MongoIterable<String> listDatabaseNames() {
116-
return executeListDatabaseNames(null);
116+
return createListDatabaseNamesIterable(null);
117117
}
118118

119119
@Override
120120
public MongoIterable<String> listDatabaseNames(final ClientSession clientSession) {
121121
notNull("clientSession", clientSession);
122-
return executeListDatabaseNames(clientSession);
122+
return createListDatabaseNamesIterable(clientSession);
123123
}
124124

125-
private MongoIterable<String> executeListDatabaseNames(final ClientSession clientSession) {
126-
return executeListDatabases(clientSession, BsonDocument.class).map(new Function<BsonDocument, String>() {
125+
private MongoIterable<String> createListDatabaseNamesIterable(final ClientSession clientSession) {
126+
return createListDatabasesIterable(clientSession, BsonDocument.class).nameOnly(true).map(new Function<BsonDocument, String>() {
127127
@Override
128128
public String apply(final BsonDocument result) {
129129
return result.getString("name").getValue();
@@ -133,7 +133,7 @@ public String apply(final BsonDocument result) {
133133

134134
@Override
135135
public ListDatabasesIterable<Document> listDatabases() {
136-
return executeListDatabases(null, Document.class);
136+
return createListDatabasesIterable(null, Document.class);
137137
}
138138

139139
@Override
@@ -143,16 +143,16 @@ public ListDatabasesIterable<Document> listDatabases(final ClientSession clientS
143143

144144
@Override
145145
public <T> ListDatabasesIterable<T> listDatabases(final Class<T> resultClass) {
146-
return executeListDatabases(null, resultClass);
146+
return createListDatabasesIterable(null, resultClass);
147147
}
148148

149149
@Override
150150
public <TResult> ListDatabasesIterable<TResult> listDatabases(final ClientSession clientSession, final Class<TResult> resultClass) {
151151
notNull("clientSession", clientSession);
152-
return executeListDatabases(clientSession, resultClass);
152+
return createListDatabasesIterable(clientSession, resultClass);
153153
}
154154

155-
private <T> ListDatabasesIterable<T> executeListDatabases(final ClientSession clientSession, final Class<T> clazz) {
155+
private <T> ListDatabasesIterable<T> createListDatabasesIterable(final ClientSession clientSession, final Class<T> clazz) {
156156
return new ListDatabasesIterableImpl<T>(clientSession, clazz, settings.getCodecRegistry(),
157157
ReadPreference.primary(), executor);
158158
}

driver-async/src/main/com/mongodb/async/client/MongoIterableImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
import com.mongodb.operation.AsyncOperationExecutor;
2626
import com.mongodb.operation.AsyncReadOperation;
2727
import com.mongodb.session.ClientSession;
28+
import org.bson.BsonDocument;
2829
import org.bson.assertions.Assertions;
30+
import org.bson.codecs.configuration.CodecRegistry;
31+
import org.bson.conversions.Bson;
2932

3033
import java.util.Collection;
3134
import java.util.List;
@@ -161,6 +164,14 @@ public void batchCursor(final SingleResultCallback<AsyncBatchCursor<TResult>> ca
161164
executor.execute(asAsyncReadOperation(), readPreference, clientSession, callback);
162165
}
163166

167+
BsonDocument toBsonDocumentOrNull(final Bson document, final CodecRegistry codecRegistry) {
168+
return toBsonDocumentOrNull(document, BsonDocument.class, codecRegistry);
169+
}
170+
171+
<T> BsonDocument toBsonDocumentOrNull(final Bson document, final Class<T> documentClass, final CodecRegistry codecRegistry) {
172+
return document == null ? null : document.toBsonDocument(documentClass, codecRegistry);
173+
}
174+
164175
private void loopCursor(final AsyncBatchCursor<TResult> batchCursor, final Block<? super TResult> block,
165176
final SingleResultCallback<Void> callback) {
166177
batchCursor.next(new SingleResultCallback<List<TResult>>() {

driver-async/src/test/unit/com/mongodb/async/client/ListDatabasesIterableSpecification.groovy

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.mongodb.async.FutureResultCallback
2323
import com.mongodb.async.SingleResultCallback
2424
import com.mongodb.operation.AsyncOperationExecutor
2525
import com.mongodb.operation.ListDatabasesOperation
26+
import org.bson.BsonDocument
2627
import org.bson.Document
2728
import org.bson.codecs.BsonValueCodecProvider
2829
import org.bson.codecs.DocumentCodec
@@ -64,12 +65,13 @@ class ListDatabasesIterableSpecification extends Specification {
6465
readPreference == secondary()
6566

6667
when: 'overriding initial options'
67-
listDatabasesIterable.maxTime(999, MILLISECONDS).into([]) { result, t -> }
68+
listDatabasesIterable.maxTime(999, MILLISECONDS).filter(Document.parse('{a: 1}')).nameOnly(true).into([]) { result, t -> }
6869

6970
operation = executor.getReadOperation() as ListDatabasesOperation<Document>
7071

7172
then: 'should use the overrides'
72-
expect operation, isTheSameAs(new ListDatabasesOperation<Document>(new DocumentCodec()).maxTime(999, MILLISECONDS))
73+
expect operation, isTheSameAs(new ListDatabasesOperation<Document>(new DocumentCodec()).maxTime(999, MILLISECONDS)
74+
.filter(BsonDocument.parse('{a: 1}')).nameOnly(true))
7375
}
7476

7577
def 'should follow the MongoIterable interface as expected'() {

driver-async/src/test/unit/com/mongodb/async/client/MongoClientSpecification.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class MongoClientSpecification extends Specification {
6363
then:
6464
// listDatabaseNamesIterable is an instance of a MappingIterable, so have to get the mapped iterable inside it
6565
expect listDatabaseNamesIterable.getMapped(), isTheSameAs(new ListDatabasesIterableImpl<BsonDocument>(session, BsonDocument,
66-
getDefaultCodecRegistry(), primary(), executor))
66+
getDefaultCodecRegistry(), primary(), executor).nameOnly(true))
6767

6868
cleanup:
6969
client?.close()

driver-core/src/main/com/mongodb/operation/ListDatabasesOperation.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.mongodb.connection.ConnectionDescription;
2929
import com.mongodb.connection.QueryResult;
3030
import com.mongodb.operation.CommandOperationHelper.CommandTransformer;
31+
import org.bson.BsonBoolean;
3132
import org.bson.BsonDocument;
3233
import org.bson.BsonInt32;
3334
import org.bson.BsonInt64;
@@ -55,6 +56,8 @@ public class ListDatabasesOperation<T> implements AsyncReadOperation<AsyncBatchC
5556
private final Decoder<T> decoder;
5657

5758
private long maxTimeMS;
59+
private BsonDocument filter;
60+
private Boolean nameOnly;
5861

5962
/**
6063
* Construct a new instance.
@@ -91,6 +94,56 @@ public ListDatabasesOperation<T> maxTime(final long maxTime, final TimeUnit time
9194
return this;
9295
}
9396

97+
/**
98+
* Sets the query filter to apply to the returned database names.
99+
*
100+
* @param filter the filter, which may be null.
101+
* @return this
102+
* @since 3.6
103+
* @mongodb.server.release 3.6
104+
*/
105+
public ListDatabasesOperation<T> filter(final BsonDocument filter) {
106+
this.filter = filter;
107+
return this;
108+
}
109+
110+
/**
111+
* Gets the query filter to apply to the returned database names.
112+
*
113+
* @return this
114+
* @since 3.6
115+
* @mongodb.server.release 3.6
116+
*/
117+
public BsonDocument getFilter() {
118+
return filter;
119+
}
120+
121+
/**
122+
* Sets the nameOnly flag that indicates whether the command should return just the database names or return the database names and
123+
* size information.
124+
*
125+
* @param nameOnly the nameOnly flag, which may be null
126+
* @return this
127+
* @since 3.6
128+
* @mongodb.server.release 3.6
129+
*/
130+
public ListDatabasesOperation<T> nameOnly(final Boolean nameOnly) {
131+
this.nameOnly = nameOnly;
132+
return this;
133+
}
134+
135+
/**
136+
* Gets the nameOnly flag that indicates whether the command should return just the database names or return the database names and
137+
* size information.
138+
*
139+
* @return this
140+
* @since 3.6
141+
* @mongodb.server.release 3.6
142+
*/
143+
public Boolean getNameOnly() {
144+
return nameOnly;
145+
}
146+
94147
/**
95148
* Executing this will return a list of all the databases names in the MongoDB instance.
96149
*
@@ -156,6 +209,12 @@ private BsonDocument getCommand() {
156209
if (maxTimeMS > 0) {
157210
command.put("maxTimeMS", new BsonInt64(maxTimeMS));
158211
}
212+
if (filter != null) {
213+
command.put("filter", filter);
214+
}
215+
if (nameOnly != null) {
216+
command.put("nameOnly", new BsonBoolean(nameOnly));
217+
}
159218
return command;
160219
}
161220
}

0 commit comments

Comments
 (0)