<commit_before>/**
 * MongoDB client binding for YCSB.
 *
 * Submitted by <NAME> on 5/11/2010.
 *
 * https://gist.github.com/000a66b8db2caf42467b#file_mongo_db.java
 *
 */

package com.yahoo.ycsb.db;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
import java.util.Map;
import java.util.Vector;

import com.mongodb.BasicDBObject;
import com.mongodb.DBAddress;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.WriteConcern;
import com.mongodb.WriteResult;

import com.yahoo.ycsb.DB;
import com.yahoo.ycsb.DBException;
import com.yahoo.ycsb.ByteIterator;
import com.yahoo.ycsb.StringByteIterator;

/**
 * MongoDB client for YCSB framework.
 *
 * Properties to set:
 *
 * mongodb.url=mongodb://localhost:27017
 * mongodb.database=ycsb
 * mongodb.writeConcern=normal
 *
 * @author ypai
 *
 */
public class MongoDbClient extends DB {

    private Mongo mongo;
    private WriteConcern writeConcern;
    private String database;

    /**
     * Initialize any state for this DB. Called once per DB instance; there is
     * one DB instance per client thread.
     */
    public void init() throws DBException {
        // initialize MongoDb driver
        Properties props = getProperties();
        String url = props.getProperty("mongodb.url", "mongodb://localhost:27017");
        database = props.getProperty("mongodb.database", "ycsb");
        String writeConcernType = props.getProperty("mongodb.writeConcern", "safe").toLowerCase();

        if ("none".equals(writeConcernType)) {
            writeConcern = WriteConcern.NONE;
        } else if ("safe".equals(writeConcernType)) {
            writeConcern = WriteConcern.SAFE;
        } else if ("normal".equals(writeConcernType)) {
            writeConcern = WriteConcern.NORMAL;
        } else if ("fsync_safe".equals(writeConcernType)) {
            writeConcern = WriteConcern.FSYNC_SAFE;
        } else if ("replicas_safe".equals(writeConcernType)) {
            writeConcern = WriteConcern.REPLICAS_SAFE;
        } else {
            System.err.println("ERROR: Invalid writeConcern: '" + writeConcernType + "'. " +
                "Must be [ none | safe | normal | fsync_safe | replicas_safe ]");
            System.exit(1);
        }

        try {
            // strip out prefix since Java driver doesn't currently support
            // standard connection format URL yet
            // http://www.mongodb.org/display/DOCS/Connections
            if (url.startsWith("mongodb://")) {
                url = url.substring(10);
            }

            // need to append db to url.
            url += "/"+database;
            System.out.println("new database url = "+url);
            mongo = new Mongo(new DBAddress(url));
            System.out.println("mongo connection created with "+url);
        } catch (Exception e1) {
            System.err.println(
                    "Could not initialize MongoDB connection pool for Loader: "
                            + e1.toString());
            e1.printStackTrace();
            return;
        }

    }

    @Override
    /**
     * Delete a record from the database.
     *<commit_msg>Added close() for MongoDbClient<commit_after>/**
 * MongoDB client binding for YCSB.
 *
 * Submitted by <NAME> on 5/11/2010.
 *
 * https://gist.github.com/000a66b8db2caf42467b#file_mongo_db.java
 *
 */

package com.yahoo.ycsb.db;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
import java.util.Map;
import java.util.Vector;

import com.mongodb.BasicDBObject;
import com.mongodb.DBAddress;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.WriteConcern;
import com.mongodb.WriteResult;

import com.yahoo.ycsb.DB;
import com.yahoo.ycsb.DBException;
import com.yahoo.ycsb.ByteIterator;
import com.yahoo.ycsb.StringByteIterator;

/**
 * MongoDB client for YCSB framework.
 *
 * Properties to set:
 *
 * mongodb.url=mongodb://localhost:27017
 * mongodb.database=ycsb
 * mongodb.writeConcern=normal
 *
 * @author ypai
 *
 */
public class MongoDbClient extends DB {

    private Mongo mongo;
    private WriteConcern writeConcern;
    private String database;

    @Override
    /**
     * Initialize any state for this DB.
     * Called once per DB instance; there is one DB instance per client thread.
     */
    public void init() throws DBException {
        // initialize MongoDb driver
        Properties props = getProperties();
        String url = props.getProperty("mongodb.url", "mongodb://localhost:27017");
        database = props.getProperty("mongodb.database", "ycsb");
        String writeConcernType = props.getProperty("mongodb.writeConcern", "safe").toLowerCase();

        if ("none".equals(writeConcernType)) {
            writeConcern = WriteConcern.NONE;
        } else if ("safe".equals(writeConcernType)) {
            writeConcern = WriteConcern.SAFE;
        } else if ("normal".equals(writeConcernType)) {
            writeConcern = WriteConcern.NORMAL;
        } else if ("fsync_safe".equals(writeConcernType)) {
            writeConcern = WriteConcern.FSYNC_SAFE;
        } else if ("replicas_safe".equals(writeConcernType)) {
            writeConcern = WriteConcern.REPLICAS_SAFE;
        } else {
            System.err.println("ERROR: Invalid writeConcern: '" + writeConcernType + "'. " +
                "Must be [ none | safe | normal | fsync_safe | replicas_safe ]");
            System.exit(1);
        }

        try {
            // strip out prefix since Java driver doesn't currently support
            // standard connection format URL yet
            // http://www.mongodb.org/display/DOCS/Connections
            if (url.startsWith("mongodb://")) {
                url = url.substring(10);
            }

            // need to append db to url.
            url += "/"+database;
            System.out.println("new database url = "+url);
            mongo = new Mongo(new DBAddress(url));
            System.out.println("mongo connection created with "+url);
        } catch (Exception e1) {
            System.err.println(
                    "Could not initialize MongoDB connection pool for Loader: "
                            + e1.toString());
            e1.printStackTrace();
            return;
        }

    }
    
    @Override
	/**
	 * Cleanup any state for this DB.
	 * Called once per DB instance; there is one DB instance per client thread.
	 */
	public void cleanup() throws DBException
	{
        try {
        	mongo.close();
        } catch (Exception e1) {
        	System.err.println(
                    "Could not close MongoDB connection pool: "
                            + e1.toString());
            e1.printStackTrace();
            return;
        }
	}

    @Override
    /**
     * Delete a record from the database.
     *
{'fraction_non_alphanumeric': 0.11184304740083059, 'fraction_numerical': 0.010453959616210798, 'mean_word_length': 2.333651551312649, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '2557982', 'n_tokens_mistral': 2173, 'n_tokens_neox': 2015, 'n_words': 480}