You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ja...@apache.org on 2020/10/04 16:02:45 UTC

[camel] branch master updated: CAMEL-15604 support allowDiskUse for MongoDB find operations

This is an automated email from the ASF dual-hosted git repository.

janbednar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new af7e383  CAMEL-15604 support allowDiskUse for MongoDB find operations
af7e383 is described below

commit af7e383e9fc1d25ce9665f74d33672cc5b507952
Author: Jan Bednář <ma...@janbednar.eu>
AuthorDate: Sun Oct 4 17:45:16 2020 +0200

    CAMEL-15604 support allowDiskUse for MongoDB find operations
---
 .../camel/catalog/docs/mongodb-component.adoc       |  2 ++
 .../src/main/docs/mongodb-component.adoc            |  2 ++
 .../camel/component/mongodb/MongoDbProducer.java    | 21 ++++++++++++++-------
 .../camel/component/mongodb/MongoDbContainer.java   |  6 ++----
 .../component/mongodb/MongoDbFindOperationTest.java | 21 +++++++++++++++++++++
 .../modules/ROOT/pages/mongodb-component.adoc       |  2 ++
 6 files changed, 43 insertions(+), 11 deletions(-)

diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/mongodb-component.adoc b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/mongodb-component.adoc
index fcbeea5..d96c6fd 100644
--- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/mongodb-component.adoc
+++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/mongodb-component.adoc
@@ -311,6 +311,8 @@ documents must fit within a maximum size, and it removes the need to
 send a request to close the cursor server-side. The batch size can be
 changed even after a cursor is iterated, in which case the setting will
 apply on the next batch retrieval. |int/Integer
+|`CamelMongoDbAllowDiskUse` |`MongoDbConstants.ALLOW_DISK_USE` | Sets allowDiskUse MongoDB flag.
+This is supported since MongoDB Server 4.3.1. Using this header with older MongoDB Server version can cause query to fail. |boolean/Boolean
 |=======================================================================
 
 Example with option outputType=MongoIterable and batch size :
diff --git a/components/camel-mongodb/src/main/docs/mongodb-component.adoc b/components/camel-mongodb/src/main/docs/mongodb-component.adoc
index fcbeea5..d96c6fd 100644
--- a/components/camel-mongodb/src/main/docs/mongodb-component.adoc
+++ b/components/camel-mongodb/src/main/docs/mongodb-component.adoc
@@ -311,6 +311,8 @@ documents must fit within a maximum size, and it removes the need to
 send a request to close the cursor server-side. The batch size can be
 changed even after a cursor is iterated, in which case the setting will
 apply on the next batch retrieval. |int/Integer
+|`CamelMongoDbAllowDiskUse` |`MongoDbConstants.ALLOW_DISK_USE` | Sets allowDiskUse MongoDB flag.
+This is supported since MongoDB Server 4.3.1. Using this header with older MongoDB Server version can cause query to fail. |boolean/Boolean
 |=======================================================================
 
 Example with option outputType=MongoIterable and batch size :
diff --git a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbProducer.java b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbProducer.java
index 6d7f48d..53a844e 100644
--- a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbProducer.java
+++ b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbProducer.java
@@ -298,7 +298,6 @@ public class MongoDbProducer extends DefaultProducer {
 
                 Bson sortBy = exchange.getIn().getHeader(SORT_BY, Bson.class);
                 Bson fieldFilter = exchange.getIn().getHeader(FIELDS_PROJECTION, Bson.class);
-
                 if (fieldFilter == null) {
                     fieldFilter = new Document();
                 }
@@ -307,7 +306,13 @@ public class MongoDbProducer extends DefaultProducer {
                     sortBy = new Document();
                 }
 
-                Document ret = dbCol.find(query).projection(fieldFilter).sort(sortBy).first();
+                Document ret = dbCol
+                        .find(query)
+                        .projection(fieldFilter)
+                        .sort(sortBy)
+                        .allowDiskUse(exchange.getIn().getHeader(MongoDbConstants.ALLOW_DISK_USE, Boolean.class))
+                        .first();
+
                 exchange.getMessage().setHeader(RESULT_TOTAL_SIZE, ret == null ? 0 : 1);
                 return ret;
             } catch (InvalidPayloadException e) {
@@ -344,7 +349,6 @@ public class MongoDbProducer extends DefaultProducer {
             } else {
                 ret = dbCol.distinct(distinctFieldName, String.class);
             }
-
             try {
                 ret.iterator().forEachRemaining(result::add);
                 exchange.getMessage().setHeader(MongoDbConstants.RESULT_PAGE_SIZE, result.size());
@@ -401,6 +405,7 @@ public class MongoDbProducer extends DefaultProducer {
                 ret.limit(limit);
             }
 
+            ret.allowDiskUse(exchange.getIn().getHeader(MongoDbConstants.ALLOW_DISK_USE, Boolean.class));
             if (!MongoDbOutputType.MongoIterable.equals(endpoint.getOutputType())) {
                 try {
                     result = new ArrayList<>();
@@ -543,9 +548,7 @@ public class MongoDbProducer extends DefaultProducer {
                     aggregationResult.batchSize(batchSize);
                 }
 
-                Boolean allowDiskUse
-                        = exchange.getIn().getHeader(MongoDbConstants.ALLOW_DISK_USE, Boolean.FALSE, Boolean.class);
-                aggregationResult.allowDiskUse(allowDiskUse);
+                aggregationResult.allowDiskUse(exchange.getIn().getHeader(MongoDbConstants.ALLOW_DISK_USE, Boolean.class));
 
                 Iterable<Document> result;
                 if (!MongoDbOutputType.MongoIterable.equals(endpoint.getOutputType())) {
@@ -595,7 +598,11 @@ public class MongoDbProducer extends DefaultProducer {
                 if (fieldFilter == null) {
                     fieldFilter = new Document();
                 }
-                ret = dbCol.find(o).projection(fieldFilter).first();
+                ret = dbCol
+                        .find(o)
+                        .projection(fieldFilter)
+                        .allowDiskUse(exchange.getIn().getHeader(MongoDbConstants.ALLOW_DISK_USE, Boolean.class))
+                        .first();
                 exchange.getMessage().setHeader(RESULT_TOTAL_SIZE, ret == null ? 0 : 1);
                 return ret;
             } catch (InvalidPayloadException e) {
diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbContainer.java b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbContainer.java
index 58a7e61..d88a252 100644
--- a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbContainer.java
+++ b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbContainer.java
@@ -29,7 +29,7 @@ public class MongoDbContainer extends GenericContainer {
     private static final Logger LOGGER = LoggerFactory.getLogger(MongoDbContainer.class);
     private static final String CONTAINER_NAME = "mongo";
     private static final int MONGODB_PORT = 27017;
-    private static final String MONGO_IMAGE = "mongo:4.0";
+    private static final String MONGO_IMAGE = "mongo:4.4";
 
     public MongoDbContainer() {
         super(MONGO_IMAGE);
@@ -43,9 +43,7 @@ public class MongoDbContainer extends GenericContainer {
                 "--replSet", "replicationName",
                 "--oplogSize", "5000",
                 "--syncdelay", "0",
-                "--noauth",
-                "--noprealloc",
-                "--smallfiles");
+                "--noauth");
     }
 
     @Override
diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbFindOperationTest.java b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbFindOperationTest.java
index 379d2cd..88cec2d 100644
--- a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbFindOperationTest.java
+++ b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbFindOperationTest.java
@@ -70,6 +70,27 @@ public class MongoDbFindOperationTest extends AbstractMongoDbTest {
     }
 
     @Test
+    public void testFindAllAllowDiskUse() throws Exception {
+        // Test that the collection has 0 documents in it
+        assertEquals(0, testCollection.countDocuments());
+        pumpDataIntoTestCollection();
+
+        Object result
+                = template.requestBodyAndHeader("direct:findAll", ObjectUtils.NULL, MongoDbConstants.ALLOW_DISK_USE, true);
+        assertTrue(result instanceof List, "Result (allowDiskUse=true) is not of type List");
+        assertListSize("Result (allowDiskUse=true) does not contain all entries in collection", (List<Document>) result, 1000);
+
+        result = template.requestBodyAndHeader("direct:findAll", ObjectUtils.NULL, MongoDbConstants.ALLOW_DISK_USE, false);
+        assertTrue(result instanceof List, "Result (allowDiskUse=false) is not of type List");
+        assertListSize("Result (allowDiskUse=false) does not contain all entries in collection", (List<Document>) result,
+                1000);
+
+        result = template.requestBodyAndHeader("direct:findAll", ObjectUtils.NULL, MongoDbConstants.ALLOW_DISK_USE, null);
+        assertTrue(result instanceof List, "Result (allowDiskUse=null) is not of type List");
+        assertListSize("Result (allowDiskUse=null) does not contain all entries in collection", (List<Document>) result, 1000);
+    }
+
+    @Test
     public void testFindAllWithQueryAndNoFIlter() throws Exception {
         // Test that the collection has 0 documents in it
         assertEquals(0, testCollection.countDocuments());
diff --git a/docs/components/modules/ROOT/pages/mongodb-component.adoc b/docs/components/modules/ROOT/pages/mongodb-component.adoc
index dd589e8..1227420 100644
--- a/docs/components/modules/ROOT/pages/mongodb-component.adoc
+++ b/docs/components/modules/ROOT/pages/mongodb-component.adoc
@@ -313,6 +313,8 @@ documents must fit within a maximum size, and it removes the need to
 send a request to close the cursor server-side. The batch size can be
 changed even after a cursor is iterated, in which case the setting will
 apply on the next batch retrieval. |int/Integer
+|`CamelMongoDbAllowDiskUse` |`MongoDbConstants.ALLOW_DISK_USE` | Sets allowDiskUse MongoDB flag.
+This is supported since MongoDB Server 4.3.1. Using this header with older MongoDB Server version can cause query to fail. |boolean/Boolean
 |=======================================================================
 
 Example with option outputType=MongoIterable and batch size :