Search in sources :

Example 91 with MongoClient

use of com.mongodb.MongoClient 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 92 with MongoClient

use of com.mongodb.MongoClient 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 93 with MongoClient

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

the class BlobThroughPutTest method performBenchMark.

@Ignore
@Test
public void performBenchMark() throws UnknownHostException, InterruptedException {
    MongoClient local = new MongoClient(new DBAddress(localServer));
    MongoClient remote = new MongoClient(new DBAddress(remoteServer));
    run(local, false, false);
    run(local, true, false);
    run(remote, false, true);
    run(remote, true, true);
    dumpResult();
}
Also used : MongoClient(com.mongodb.MongoClient) DBAddress(com.mongodb.DBAddress) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 94 with MongoClient

use of com.mongodb.MongoClient in project qi4j-sdk by Qi4j.

the class MongoMapEntityStoreMixin method activateService.

@Override
public void activateService() throws Exception {
    loadConfiguration();
    // Create Mongo driver and open the database
    if (username.isEmpty()) {
        mongo = new MongoClient(serverAddresses);
    } else {
        MongoCredential credential = MongoCredential.createMongoCRCredential(username, databaseName, password);
        mongo = new MongoClient(serverAddresses, Arrays.asList(credential));
    }
    db = mongo.getDB(databaseName);
    // Create index if needed
    db.requestStart();
    DBCollection entities = db.getCollection(collectionName);
    if (entities.getIndexInfo().isEmpty()) {
        entities.createIndex(new BasicDBObject(IDENTITY_COLUMN, 1));
    }
    db.requestDone();
}
Also used : MongoClient(com.mongodb.MongoClient) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) MongoCredential(com.mongodb.MongoCredential)

Example 95 with MongoClient

use of com.mongodb.MongoClient in project sling by apache.

the class IndexCreationIT method setUp.

@Before
public void setUp() throws Exception {
    String connectionString = System.getProperty("connectionString", "localhost:27017");
    database = System.getProperty("database", "sling") + "_indextest";
    collection = System.getProperty("collection", "resources");
    mongoClient = new MongoClient(connectionString);
    underTest = new MongoDBNoSqlAdapter(mongoClient, database, collection);
    underTest.checkConnection();
    underTest.createIndexDefinitions();
}
Also used : MongoClient(com.mongodb.MongoClient) MongoDBNoSqlAdapter(org.apache.sling.nosql.mongodb.resourceprovider.impl.MongoDBNoSqlAdapter) Before(org.junit.Before)

Aggregations

MongoClient (com.mongodb.MongoClient)126 Test (org.junit.Test)31 MongoClientURI (com.mongodb.MongoClientURI)29 Document (org.bson.Document)26 ServerAddress (com.mongodb.ServerAddress)21 MongoDatabase (com.mongodb.client.MongoDatabase)21 Before (org.junit.Before)20 BasicDBObject (com.mongodb.BasicDBObject)11 ArrayList (java.util.ArrayList)11 MongoCredential (com.mongodb.MongoCredential)9 MongoException (com.mongodb.MongoException)8 DB (com.mongodb.DB)7 DBCollection (com.mongodb.DBCollection)7 UnknownHostException (java.net.UnknownHostException)7 MongoTemplate (org.springframework.data.mongodb.core.MongoTemplate)7 DBObject (com.mongodb.DBObject)6 MongoClientOptions (com.mongodb.MongoClientOptions)6 IOException (java.io.IOException)5 List (java.util.List)5 MongoDbFactory (org.springframework.data.mongodb.MongoDbFactory)5