<commit_before>package de.idealo.mongodb.perf;

import com.google.common.collect.Lists;
import com.mongodb.*;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.UnknownHostException;

/**
 * Created by kay.agahd on 23.11.16.
 */
public class MongoDbAccessor {

    private static final Logger LOG = LoggerFactory.getLogger(MongoDbAccessor.class);

    private final ServerAddress[] serverAddress;
    private final int socketTimeOut;
    private final String user;
    private final String pw;
    private final String authDb;
    private final boolean ssl;
    private MongoClient mongo;



    private MongoDbAccessor(){
        this(-1, null, null, null, false, null);
    };

    public MongoDbAccessor(String user, String pw, String authDb, boolean ssl, ServerAddress ... serverAddress){
        this(-1, user, pw, authDb, ssl, serverAddress);
    }

    public MongoDbAccessor(int socketTimeOut, String user, String pw, String authDb, boolean ssl, ServerAddress ... serverAddress){
        this.serverAddress = serverAddress;
        this.socketTimeOut = socketTimeOut;
        this.user = user;
        this.pw = pw;
        this.authDb = authDb!=null&&!authDb.isEmpty()?authDb:"admin";
        this.ssl = ssl;
        init();
    }

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


    private void init() {
        LOG.info(">>> init {}", serverAddress);
        try {
            MongoClientOptions options = MongoClientOptions.builder().
                    connectTimeout(1000*2).//fail fast, so we know this node is unavailable
                    socketTimeout(socketTimeOut==-1?1000*10:socketTimeOut).//default 10 seconds
                    readPreference(ReadPreference.secondaryPreferred()).
                    connectionsPerHost(5000).
                    threadsAllowedToBlockForConnectionMultiplier(10).
                    writeConcern(WriteConcern.ACKNOWLEDGED).
                    sslEnabled(ssl).
                    sslInvalidHostNameAllowed(true).
                    build();

            if(user != null && !user.isEmpty() && pw!= null && !pw.isEmpty()) {
                MongoCredential mc = MongoCredential.createCredential(user, authDb, pw.toCharArray());
                if(serverAddress.length == 1) {
                    mongo = new MongoClient(serverAddress[0], Lists.newArrayList(mc), options);
                }else {
                    mongo = new MongoClient(Lists.newArrayList(serverAddress), Lists.newArrayList(mc), options);
                }
            }else{<commit_msg>perhaps, fixed error that would occur on longer running tests<commit_after>package de.idealo.mongodb.perf;

import com.google.common.collect.Lists;
import com.mongodb.*;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.UnknownHostException;

/**
 * Created by kay.agahd on 23.11.16.
 */
public class MongoDbAccessor {

    private static final Logger LOG = LoggerFactory.getLogger(MongoDbAccessor.class);

    private final ServerAddress[] serverAddress;
    private final int socketTimeOut;
    private final String user;
    private final String pw;
    private final String authDb;
    private final boolean ssl;
    private MongoClient mongo;



    private MongoDbAccessor(){
        this(-1, null, null, null, false, null);
    };

    public MongoDbAccessor(String user, String pw, String authDb, boolean ssl, ServerAddress ... serverAddress){
        this(-1, user, pw, authDb, ssl, serverAddress);
    }

    public MongoDbAccessor(int socketTimeOut, String user, String pw, String authDb, boolean ssl, ServerAddress ... serverAddress){
        this.serverAddress = serverAddress;
        this.socketTimeOut = socketTimeOut;
        this.user = user;
        this.pw = pw;
        this.authDb = authDb!=null&&!authDb.isEmpty()?authDb:"admin";
        this.ssl = ssl;
        init();
    }

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


    private void init() {
        LOG.info(">>> init {}", serverAddress);
        try {
            MongoClientOptions options = MongoClientOptions.builder().
                    connectTimeout(1000*2).//fail fast, so we know this node is unavailable
                    maxConnectionIdleTime(1000 * 60).
                    maxConnectionLifeTime(1000 * 60).
                    socketTimeout(socketTimeOut==-1?1000*10:socketTimeOut).//default 10 seconds
                    readPreference(ReadPreference.secondaryPreferred()).
                    connectionsPerHost(5000).
                    threadsAllowedToBlockForConnectionMultiplier(10).
                    writeConcern(WriteConcern.ACKNOWLEDGED).
                    sslEnabled(ssl).
                    sslInvalidHostNameAllowed(true).
                    build();

            if(user != null && !user.isEmpty() && pw!= null && !pw.isEmpty()) {
                MongoCredential mc = MongoCredential.createCredential(user, authDb, pw.toCharArray());
                if(serverAddress.length == 1) {
                    mongo = new MongoClient(serverAddress[0], Lists.newArrayList(mc), options);
                }else {
                    mongo = new MongoClient(Lists.newArrayList(serverAddress), Lists.newArrayList(mc), options);
                }
            }else{
{'fraction_non_alphanumeric': 0.10145184135977336, 'fraction_numerical': 0.013456090651558074, 'mean_word_length': 2.487037037037037, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6538223', 'n_tokens_mistral': 1641, 'n_tokens_neox': 1503, 'n_words': 356}