Search in sources :

Example 56 with MongoClientURI

use of com.mongodb.MongoClientURI in project jackrabbit-oak by apache.

the class MongoConnection method hasWriteConcern.

/**
     * Returns {@code true} if the given {@code uri} has a write concern set.
     * @param uri the URI to check.
     * @return {@code true} if the URI has a write concern set, {@code false}
     *      otherwise.
     */
public static boolean hasWriteConcern(@Nonnull String uri) {
    MongoClientOptions.Builder builder = MongoClientOptions.builder();
    builder.writeConcern(WC_UNKNOWN);
    WriteConcern wc = new MongoClientURI(checkNotNull(uri), builder).getOptions().getWriteConcern();
    return !WC_UNKNOWN.equals(wc);
}
Also used : MongoClientOptions(com.mongodb.MongoClientOptions) WriteConcern(com.mongodb.WriteConcern) MongoClientURI(com.mongodb.MongoClientURI)

Example 57 with MongoClientURI

use of com.mongodb.MongoClientURI in project jackrabbit-oak by apache.

the class NodeCollectionProvider method prepareClientForHostname.

private MongoClient prepareClientForHostname(String hostname) throws UnknownHostException {
    ServerAddress address;
    if (hostname.contains(":")) {
        String[] hostSplit = hostname.split(":");
        if (hostSplit.length != 2) {
            throw new IllegalArgumentException("Not a valid hostname: " + hostname);
        }
        address = new ServerAddress(hostSplit[0], Integer.parseInt(hostSplit[1]));
    } else {
        address = new ServerAddress(hostname);
    }
    MongoClientURI originalUri = new MongoClientURI(originalMongoUri);
    List<MongoCredential> credentialList = new ArrayList<MongoCredential>(1);
    if (originalUri.getCredentials() != null) {
        credentialList.add(originalUri.getCredentials());
    }
    return new MongoClient(address, credentialList, originalUri.getOptions());
}
Also used : MongoClient(com.mongodb.MongoClient) MongoCredential(com.mongodb.MongoCredential) MongoClientURI(com.mongodb.MongoClientURI) ServerAddress(com.mongodb.ServerAddress) ArrayList(java.util.ArrayList)

Example 58 with MongoClientURI

use of com.mongodb.MongoClientURI in project jackrabbit-oak by apache.

the class NodeCollectionProvider method get.

@SuppressWarnings("deprecation")
public DBCollection get(String hostname) throws UnknownHostException {
    if (collections.containsKey(hostname)) {
        return collections.get(hostname);
    }
    MongoClient client;
    if (originalMongoUri == null) {
        MongoClientURI uri = new MongoClientURI("mongodb://" + hostname);
        client = new MongoClient(uri);
    } else {
        client = prepareClientForHostname(hostname);
    }
    DB db = client.getDB(dbName);
    db.getMongo().slaveOk();
    DBCollection collection = db.getCollection(Collection.NODES.toString());
    collections.put(hostname, collection);
    return collection;
}
Also used : MongoClient(com.mongodb.MongoClient) DBCollection(com.mongodb.DBCollection) MongoClientURI(com.mongodb.MongoClientURI) DB(com.mongodb.DB)

Example 59 with MongoClientURI

use of com.mongodb.MongoClientURI in project jackrabbit-oak by apache.

the class DocumentMongoFixture method getDb.

protected DB getDb(String suffix) throws UnknownHostException {
    String dbName = new MongoClientURI(uri).getDatabase();
    MongoConnection connection = new MongoConnection(uri);
    return connection.getDB(dbName + "-" + suffix);
}
Also used : MongoClientURI(com.mongodb.MongoClientURI) MongoConnection(org.apache.jackrabbit.oak.plugins.document.util.MongoConnection)

Example 60 with MongoClientURI

use of com.mongodb.MongoClientURI in project jackrabbit-oak by apache.

the class MongoNodeStoreContainer method testMongoAvailability.

private static boolean testMongoAvailability() {
    Mongo mongo = null;
    try {
        MongoClientURI uri = new MongoClientURI(MONGO_URI + "?connectTimeoutMS=3000");
        mongo = new MongoClient(uri);
        mongo.getDatabaseNames();
        return true;
    } catch (Exception e) {
        return false;
    } finally {
        if (mongo != null) {
            mongo.close();
        }
    }
}
Also used : MongoClient(com.mongodb.MongoClient) Mongo(com.mongodb.Mongo) MongoClientURI(com.mongodb.MongoClientURI) IOException(java.io.IOException)

Aggregations

MongoClientURI (com.mongodb.MongoClientURI)64 MongoClient (com.mongodb.MongoClient)27 DBCollection (com.mongodb.DBCollection)12 BasicDBObject (com.mongodb.BasicDBObject)9 Test (org.junit.Test)9 MongoClientURIBuilder (com.mongodb.hadoop.util.MongoClientURIBuilder)8 Configuration (org.apache.hadoop.conf.Configuration)8 DBObject (com.mongodb.DBObject)7 ArrayList (java.util.ArrayList)7 List (java.util.List)7 InputSplit (org.apache.hadoop.mapreduce.InputSplit)6 DB (com.mongodb.DB)5 MongoDatabase (com.mongodb.client.MongoDatabase)5 MongoInputSplit (com.mongodb.hadoop.input.MongoInputSplit)5 IOException (java.io.IOException)5 MongoConnection (org.apache.jackrabbit.oak.plugins.document.util.MongoConnection)5 Document (org.bson.Document)5 OptionParser (joptsimple.OptionParser)4 OptionSet (joptsimple.OptionSet)4 DocumentNodeStore (org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore)4