package com.openparts.utils.mongodb;

import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.client.MongoDatabase;
import org.apache.commons.lang.StringUtils;
import java.util.Properties;
import com.cnpc.framework.utils.PropertiesUtil;

public class MongoDBDriver {

    private MongoClient mongo = null;

    private Integer connectionsPerHost = 32;
    private Integer threadsAllowedToBlockForConnectionMultiplier = 5;
    private Integer maxWaitTime = 30000;
    private Integer connectTimeout = 30000;
    private Integer socketTimeout = 30000;

    private Integer maxConnectionIdle = 6000;


    private MongoDBConfig configuration;

    public MongoDBDriver(MongoDBConfig configuration) throws Exception {
        this.configuration = configuration;

        connectionsPerHost = PropertiesUtil.getIntValue("mongodb.driver.connectionsPerHost", 32);
        threadsAllowedToBlockForConnectionMultiplier = PropertiesUtil.getIntValue("mongodb.driver.maxWaitTime", 30000);
        connectionsPerHost = PropertiesUtil.getIntValue("mongodb.driver.connectTimeout", 30000);
        socketTimeout = PropertiesUtil.getIntValue("mongodb.driver.socketTimeout", 30000);
        maxConnectionIdle = PropertiesUtil.getIntValue("mongodb.driver.maxConnectionIdle", 30000);

        ////init,db check and connected.
        MongoClientOptions.Builder builder = MongoClientOptions.builder();

        builder.connectionsPerHost(this.connectionsPerHost);
        builder.threadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier);
        builder.socketKeepAlive(true);
        builder.maxWaitTime(this.maxWaitTime);
        builder.connectTimeout(this.connectTimeout);
        builder.socketTimeout(this.socketTimeout);
        builder.maxConnectionIdleTime(maxConnectionIdle);

        MongoClientOptions options = builder.build();

        this.mongo = new MongoClient(configuration.buildAddresses(), configuration.buildCredentials(), options);
    }


    public void close() {
        mongo.close();
    }

    public Mongo getMongo() {
        return mongo;
    }

    public MongoDatabase getDatabase(String dbName) {
        return mongo.getDatabase(dbName);
    }

    /**
     * old api
     * @param dbName
     * @return
     */
    public DB getDB(String dbName) {
        return mongo.getDB(dbName);
    }

}

@@ -626,17 +626,16 @@
  30000;%0A
-%0A
     priv
@@ -1344,49 +1344,8 @@
 );%0A%0A
-        ////init,db check and connected.%0A
     
@@ -1830,24 +1830,249 @@
 tionIdle);%0A%0A
+        /**%0A         * %E5%9C%A8options%E9%87%8C%E6%B7%BB%E5%8A%A0readPreference=secondaryPreferred%E5%8D%B3%E5%8F%AF%E5%AE%9E%E7%8E%B0%EF%BC%8C%E8%AF%BB%E8%AF%B7%E6%B1%82%E4%BC%98%E5%85%88%E5%88%B0Secondary%E8%8A%82%E7%82%B9%EF%BC%8C%E4%BB%8E%E8%80%8C%E5%AE%9E%E7%8E%B0%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB%E7%9A%84%E5%8A%9F%E8%83%BD%0A         * %E5%9C%A8options%E9%87%8C%E6%B7%BB%E5%8A%A0maxPoolSize=xx%E5%8D%B3%E5%8F%AF%E5%B0%86%E5%AE%A2%E6%88%B7%E7%AB%AF%E8%BF%9E%E6%8E%A5%E6%B1%A0%E9%99%90%E5%88%B6%E5%9C%A8xx%E4%BB%A5%E5%86%85%E3%80%82%0A         * %E5%9C%A8options%E9%87%8C%E6%B7%BB%E5%8A%A0w= majority%E5%8D%B3%E5%8F%AF%E4%BF%9D%E8%AF%81%E5%86%99%E8%AF%B7%E6%B1%82%E6%88%90%E5%8A%9F%E5%86%99%E5%85%A5%E5%A4%A7%E5%A4%9A%E6%95%B0%E8%8A%82%E7%82%B9%E6%89%8D%E5%90%91%E5%AE%A2%E6%88%B7%E7%AB%AF%E7%A1%AE%E8%AE%A4%0A         */%0A
         Mong
@@ -2457,75 +2457,8 @@
  %7D%0A%0A
-    /**%0A     * old api%0A     * @param dbName%0A     * @return%0A     */%0A
     

add some comments for MongoDBDriver