<commit_before>package eu.socialsensor.framework.client.mongo;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoException;
import com.mongodb.WriteConcern;
import com.mongodb.WriteResult;
import com.mongodb.util.JSON;

import eu.socialsensor.framework.common.domain.JSONable;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 *
 * @author etzoannos
 *
 */
public class MongoHandler {
	
    DBCollection collection;
    private DBObject sortField = new BasicDBObject("_id", -1);
    private DBObject publicationTimeField = new BasicDBObject("publicationTime", -1);
    public static int ASC = 1;
    public static int DESC = -1;
    private DB db = null;
    private static Map<String, MongoClient> connections = new HashMap<String, MongoClient>();
    private static Map<String, DB> databases = new HashMap<String, DB>();

    private static MongoClientOptions options = MongoClientOptions.builder()
            .writeConcern(WriteConcern.UNACKNOWLEDGED).build();
    
    public MongoHandler(String host, String dbName, String collectionName, List<String> indexes) throws Exception {
        this(host, dbName);
        collection = db.getCollection(collectionName);
        
      
        if (indexes != null) {
            for (String index : indexes) {
                collection.ensureIndex(index);
            }
        }
    }

    public MongoHandler(String hostname, String dbName) throws Exception {
        String connectionKey = hostname + "#" + dbName;
        db = databases.get(connectionKey);
        if (db == null) {
            MongoClient mongo = connections.get(hostname);
            
            if (mongo == null) {
                mongo = new MongoClient(hostname, options);
                connections.put(hostname, mongo);
            }
            db = mongo.getDB(dbName);
            databases.put(connectionKey, db);
            try{
            	mongo.getConnector().getDBPortPool(mongo.getAddress()).get().ensureOpen();
            
            }
            catch(Exception e){
            	System.out.println("Mongo DB at " + hostname +" is closed");
            	throw e;
            }
            
        }
    }
    
    public boolean checkConnection(String hostname) {
    	
    	MongoClient mongo = connections.get(hostname);
    	
    	if(mongo == null)
    		return false;
    	
//    	 try{<commit_msg>comment code for connections check<commit_after>package eu.socialsensor.framework.client.mongo;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoException;
import com.mongodb.WriteConcern;
import com.mongodb.WriteResult;
import com.mongodb.util.JSON;

import eu.socialsensor.framework.common.domain.JSONable;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 *
 * @author etzoannos
 *
 */
public class MongoHandler {
	
    DBCollection collection;
    private DBObject sortField = new BasicDBObject("_id", -1);
    private DBObject publicationTimeField = new BasicDBObject("publicationTime", -1);
    public static int ASC = 1;
    public static int DESC = -1;
    private DB db = null;
    private static Map<String, MongoClient> connections = new HashMap<String, MongoClient>();
    private static Map<String, DB> databases = new HashMap<String, DB>();

    private static MongoClientOptions options = MongoClientOptions.builder()
            .writeConcern(WriteConcern.UNACKNOWLEDGED).build();
    
    public MongoHandler(String host, String dbName, String collectionName, List<String> indexes) throws Exception {
        this(host, dbName);
        collection = db.getCollection(collectionName);
      
        if (indexes != null) {
            for (String index : indexes) {
                collection.ensureIndex(index);
            }
        }
    }

    public MongoHandler(String hostname, String dbName) throws Exception {
        String connectionKey = hostname + "#" + dbName;
        db = databases.get(connectionKey);
        if (db == null) {
            MongoClient mongo = connections.get(hostname);
            
            if (mongo == null) {
                mongo = new MongoClient(hostname, options);
                connections.put(hostname, mongo);
            }
            db = mongo.getDB(dbName);
            databases.put(connectionKey, db);
            
            /*
            try{
            	mongo.getConnector().getDBPortPool(mongo.getAddress()).get().ensureOpen();
            
            }
            catch(Exception e){
            	System.out.println("Mongo DB at " + hostname +" is closed");
            	throw e;
            }
            */
        }
    }
    
    public boolean checkConnection(String hostname) {
    	
    	MongoClient mongo = connections.get(hostname);
    	
    	if(mongo == null)
    		return false;
    	
//    	 try{
{'fraction_non_alphanumeric': 0.1012518409425626, 'fraction_numerical': 0.0014727540500736377, 'mean_word_length': 2.514230271668823, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '4671335', 'n_tokens_mistral': 1591, 'n_tokens_neox': 1530, 'n_words': 351}