Jiabao-Sun commented on code in PR #1:
URL: 
https://github.com/apache/flink-connector-mongodb/pull/1#discussion_r1035106483


##########
flink-connector-mongodb/src/main/java/org/apache/flink/connector/mongodb/source/enumerator/splitter/MongoSplitContext.java:
##########
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.connector.mongodb.source.enumerator.splitter;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.connector.mongodb.source.config.MongoReadOptions;
+
+import com.mongodb.MongoNamespace;
+import com.mongodb.client.MongoClient;
+import com.mongodb.client.MongoCollection;
+import org.bson.BsonBoolean;
+import org.bson.BsonDocument;
+import org.bson.BsonInt64;
+
+import static 
org.apache.flink.connector.mongodb.common.utils.MongoConstants.AVG_OBJ_SIZE_FIELD;
+import static 
org.apache.flink.connector.mongodb.common.utils.MongoConstants.COUNT_FIELD;
+import static 
org.apache.flink.connector.mongodb.common.utils.MongoConstants.SHARDED_FIELD;
+import static 
org.apache.flink.connector.mongodb.common.utils.MongoConstants.SIZE_FIELD;
+
+/**
+ * The split context used by {@link MongoSplitters.MongoSplitter} to split 
collection into a set of
+ * chunks for MongoDB data source.
+ */
+@Internal
+public class MongoSplitContext {
+
+    /** Read options of MongoDB. */
+    private final MongoReadOptions readOptions;
+
+    /** Client of MongoDB. */
+    private final MongoClient mongoClient;
+
+    /** Namespace of MongoDB, eg. db.coll. */
+    private final MongoNamespace namespace;
+
+    /** Is a sharded collection. */
+    private final boolean sharded;
+
+    /** The number of objects or documents in this collection. */
+    private final long count;
+
+    /** The total uncompressed size(bytes) in memory of all records in a 
collection. */
+    private final long size;
+
+    /** The average size(bytes) of an object in the collection. */
+    private final long avgObjSize;
+
+    public MongoSplitContext(
+            MongoReadOptions readOptions,
+            MongoClient mongoClient,
+            MongoNamespace namespace,
+            boolean sharded,
+            long count,
+            long size,
+            long avgObjSize) {
+        this.readOptions = readOptions;
+        this.mongoClient = mongoClient;
+        this.namespace = namespace;
+        this.sharded = sharded;
+        this.count = count;
+        this.size = size;
+        this.avgObjSize = avgObjSize;
+    }
+
+    public static MongoSplitContext of(
+            MongoReadOptions readOptions,
+            MongoClient mongoClient,
+            MongoNamespace namespace,
+            BsonDocument collStats) {
+        return new MongoSplitContext(
+                readOptions,
+                mongoClient,
+                namespace,
+                collStats.getBoolean(SHARDED_FIELD, 
BsonBoolean.FALSE).getValue(),
+                collStats.getNumber(COUNT_FIELD, new BsonInt64(0)).longValue(),
+                collStats.getNumber(SIZE_FIELD, new BsonInt64(0)).longValue(),
+                collStats.getNumber(AVG_OBJ_SIZE_FIELD, new 
BsonInt64(0)).longValue());

Review Comment:
   When the collection is empty, `avgObjSize` field will not be included in 
`collStats`. 
   We should add some validation to avoid division by zero in the sample 
partitioner.
   
   ```json
   {
       "ns": "test_source.empty_coll",
       "size": 0, 
       "count": 0, 
       "storageSize": 4096,
       "capped": false
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to