Leon-WTF commented on a change in pull request #2384:
URL: https://github.com/apache/drill/pull/2384#discussion_r768746854



##########
File path: 
metastore/mongo-metastore/src/main/java/org/apache/drill/metastore/mongo/MongoMetastore.java
##########
@@ -0,0 +1,136 @@
+/*
+ * 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.drill.metastore.mongo;
+
+import com.mongodb.BasicDBObject;
+import com.mongodb.ConnectionString;
+import com.mongodb.client.FindIterable;
+import com.mongodb.client.MongoClient;
+import com.mongodb.client.MongoClients;
+import org.apache.drill.common.config.DrillConfig;
+import org.apache.drill.metastore.Metastore;
+import org.apache.drill.metastore.components.tables.Tables;
+import org.apache.drill.metastore.components.views.Views;
+import org.apache.drill.metastore.mongo.components.tables.MongoTables;
+import org.apache.drill.metastore.mongo.config.MongoConfigConstants;
+import org.apache.drill.metastore.mongo.exception.MongoMetastoreException;
+import org.bson.Document;
+
+/**
+ * Mongo Drill Metastore implementation.
+ */
+public class MongoMetastore implements Metastore {
+
+  private final MongoClient client;
+  private final String database;
+  private final String tableCollection;
+
+  public MongoMetastore(DrillConfig config) {
+    this.client = MongoClients.create(
+      new ConnectionString(config.getString(MongoConfigConstants.CONNECTION)));
+    if(config.hasPath(MongoConfigConstants.DATABASE)) {
+      this.database = config.getString(MongoConfigConstants.DATABASE);
+    } else {
+      this.database = MongoConfigConstants.DEFAULT_DATABASE;
+    }
+    if(config.hasPath(MongoConfigConstants.TABLE_COLLECTION)) {
+      this.tableCollection = 
config.getString(MongoConfigConstants.TABLE_COLLECTION);
+    } else {
+      this.tableCollection = MongoConfigConstants.DEFAULT_TABLE_COLLECTION;
+    }
+    if (config.hasPath(MongoConfigConstants.IS_SHARDED) &&
+      config.getBoolean(MongoConfigConstants.IS_SHARDED)) {
+      initShardedCollection();
+    }
+  }
+
+  @Override
+  public Tables tables() {
+    return new MongoTables(
+      client.getDatabase(database).getCollection(tableCollection), client);
+  }
+
+  @Override
+  public Views views() {
+    throw new UnsupportedOperationException("Views metadata support is not 
implemented");
+  }
+
+  private void initShardedCollection() {
+    FindIterable<Document> dbs = this.client.getDatabase("config")
+      .getCollection("databases")
+      .find(new Document()
+        .append(MongoConfigConstants.ID, database));
+    if (!dbs.cursor().hasNext()) {
+      shardDatabase();
+      shardCollection();
+    } else {
+      Document doc = dbs.cursor().next();
+      if (!doc.getBoolean("partitioned")) {
+        shardDatabase();
+      }
+      FindIterable<Document> collections = this.client.getDatabase("config")
+        .getCollection("collections")
+        .find(new Document()
+          .append(MongoConfigConstants.ID, database+"."+tableCollection)
+          .append("distributionMode", "sharded"));
+      if (!collections.cursor().hasNext()) {
+        shardCollection();
+      } else {
+        Document shardKey = (Document) dbs.cursor().next().get("key");
+        if (shardKey.size() != 1 || 
!shardKey.containsKey(MongoConfigConstants.ID)
+          || !shardKey.containsValue("hashed")) {
+          throw new MongoMetastoreException(
+            String.format("%s.%s(with shard key %s) is not hash sharded and " +
+                "only hash sharded by %s", database, tableCollection,
+              shardKey.toString(), MongoConfigConstants.ID));
+        }
+      }
+    }
+  }
+
+  private void shardDatabase() {

Review comment:
       Yes, you are right, I will choose the first one and document the 
requirements of database and collections.




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to