Skip to content

Commit 44200b1

Browse files
committed
Changed FindOperation to only use the $query meta-operator if there other meta-operators that are being used.
This is necessary to make the check for whether the server is fsync-locked work, as that particular usage of OP_QUERY does not handle the $query meta-operator and simply closed the connection. JAVA-1886
1 parent 9802eb3 commit 44200b1

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,8 +537,8 @@ private int getNumberToReturn() {
537537
}
538538

539539
private BsonDocument asDocument(final ConnectionDescription connectionDescription, final ReadPreference readPreference) {
540-
BsonDocument document = modifiers != null ? modifiers : new BsonDocument();
541-
document.put("$query", filter != null ? filter : new BsonDocument());
540+
BsonDocument document = new BsonDocument();
541+
542542
if (sort != null) {
543543
document.put("$orderby", sort);
544544
}
@@ -551,6 +551,16 @@ private BsonDocument asDocument(final ConnectionDescription connectionDescriptio
551551
document.put("$readPreference", readPreference.toDocument());
552552
}
553553

554+
if (modifiers != null) {
555+
document.putAll(modifiers);
556+
}
557+
558+
if (document.isEmpty()) {
559+
document = filter != null ? filter : new BsonDocument();
560+
} else {
561+
document.put("$query", filter != null ? filter : new BsonDocument());
562+
}
563+
554564
return document;
555565
}
556566

driver-core/src/test/functional/com/mongodb/operation/FindOperationSpecification.groovy

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,33 @@ class FindOperationSpecification extends OperationFunctionalSpecification {
293293
count == 500
294294
}
295295

296+
def 'should call query on Connection with no $query when there are no other meta operators'() {
297+
given:
298+
def findOperation = new FindOperation<Document>(getNamespace(), new DocumentCodec())
299+
.projection(new BsonDocument('x', new BsonInt32(1)))
300+
.filter(new BsonDocument('z', new BsonString('val')))
301+
def binding = Stub(ReadBinding)
302+
def source = Stub(ConnectionSource)
303+
def connection = Mock(Connection)
304+
binding.readPreference >> ReadPreference.primary()
305+
binding.readConnectionSource >> source
306+
source.connection >> connection
307+
source.retain() >> source
308+
309+
when:
310+
findOperation.execute(binding)
311+
312+
then:
313+
_ * connection.description >> new ConnectionDescription(new ConnectionId(new ServerId(new ClusterId(), new ServerAddress())),
314+
new ServerVersion(2, 6), ServerType.STANDALONE, 1000, 100000, 100000)
315+
316+
1 * connection.query(getNamespace(), findOperation.filter,
317+
findOperation.projection, 0, 0, false, false, false, false, false, false, _) >>
318+
new QueryResult(getNamespace(), [new BsonDocument('n', new BsonInt32(1))], 0, new ServerAddress())
319+
320+
1 * connection.release()
321+
}
322+
296323
def 'should call query on Connection with correct arguments for an explain'() {
297324
given:
298325
def findOperation = new FindOperation<Document>(getNamespace(), new DocumentCodec())

driver/src/test/functional/com/mongodb/MongoMethodsTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import java.net.UnknownHostException;
2222

2323
import static org.hamcrest.CoreMatchers.hasItems;
24+
import static org.junit.Assert.assertFalse;
2425
import static org.junit.Assert.assertThat;
26+
import static org.junit.Assert.assertTrue;
2527

2628
public class MongoMethodsTest extends DatabaseTestCase {
2729
@Test
@@ -37,4 +39,15 @@ public void shouldGetDatabaseNames() throws UnknownHostException {
3739
getClient().dropDatabase("test2");
3840
}
3941
}
42+
43+
@Test
44+
public void shouldLockAndUnlock() {
45+
assertFalse(getClient().isLocked());
46+
47+
getClient().fsyncAndLock();
48+
assertTrue(getClient().isLocked());
49+
50+
getClient().unlock();
51+
assertFalse(getClient().isLocked());
52+
}
4053
}

0 commit comments

Comments
 (0)