Search in sources :

Example 1 with MongoClientConfiguration

use of com.allanbank.mongodb.MongoClientConfiguration in project YCSB by brianfrankcooper.

the class AsyncMongoDbClient method init.

/**
   * Initialize any state for this DB. Called once per DB instance; there is one
   * DB instance per client thread.
   */
@Override
public final void init() throws DBException {
    final int count = INIT_COUNT.incrementAndGet();
    synchronized (AsyncMongoDbClient.class) {
        final Properties props = getProperties();
        if (mongoClient != null) {
            database = mongoClient.getDatabase(databaseName);
            // the connections occupied.
            if (count > mongoClient.getConfig().getMaxConnectionCount()) {
                mongoClient.getConfig().setLockType(LockType.MUTEX);
            }
            return;
        }
        // Set insert batchsize, default 1 - to be YCSB-original equivalent
        batchSize = Integer.parseInt(props.getProperty("mongodb.batchsize", "1"));
        // Set is inserts are done as upserts. Defaults to false.
        useUpsert = Boolean.parseBoolean(props.getProperty("mongodb.upsert", "false"));
        // Just use the standard connection format URL
        // http://docs.mongodb.org/manual/reference/connection-string/
        // to configure the client.
        String url = props.getProperty("mongodb.url", "mongodb://localhost:27017/ycsb?w=1");
        if (!url.startsWith("mongodb://")) {
            System.err.println("ERROR: Invalid URL: '" + url + "'. Must be of the form " + "'mongodb://<host1>:<port1>,<host2>:<port2>/database?" + "options'. See " + "http://docs.mongodb.org/manual/reference/connection-string/.");
            System.exit(1);
        }
        MongoDbUri uri = new MongoDbUri(url);
        try {
            databaseName = uri.getDatabase();
            if ((databaseName == null) || databaseName.isEmpty()) {
                // Default database is "ycsb" if database is not
                // specified in URL
                databaseName = "ycsb";
            }
            mongoClient = MongoFactory.createClient(uri);
            MongoClientConfiguration config = mongoClient.getConfig();
            if (!url.toLowerCase().contains("locktype=")) {
                // assumed...
                config.setLockType(LockType.LOW_LATENCY_SPIN);
            }
            readPreference = config.getDefaultReadPreference();
            writeConcern = config.getDefaultDurability();
            database = mongoClient.getDatabase(databaseName);
            System.out.println("mongo connection created with " + url);
        } catch (final Exception e1) {
            System.err.println("Could not initialize MongoDB connection pool for Loader: " + e1.toString());
            e1.printStackTrace();
            return;
        }
    }
}
Also used : MongoDbUri(com.allanbank.mongodb.MongoDbUri) Properties(java.util.Properties) MongoClientConfiguration(com.allanbank.mongodb.MongoClientConfiguration) DBException(com.yahoo.ycsb.DBException)

Aggregations

MongoClientConfiguration (com.allanbank.mongodb.MongoClientConfiguration)1 MongoDbUri (com.allanbank.mongodb.MongoDbUri)1 DBException (com.yahoo.ycsb.DBException)1 Properties (java.util.Properties)1