Search in sources :

Example 61 with MongoClientURI

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

the class ResetClusterIdCommand method execute.

@Override
public void execute(String... args) throws Exception {
    OptionParser parser = new OptionParser();
    OptionSet options = parser.parse(args);
    if (options.nonOptionArguments().isEmpty()) {
        System.out.println("usage: resetclusterid {<path>|<mongo-uri>}");
        System.exit(1);
    }
    String source = options.nonOptionArguments().get(0).toString();
    Closer closer = Closer.create();
    try {
        NodeStore store;
        if (args[0].startsWith(MongoURI.MONGODB_PREFIX)) {
            MongoClientURI uri = new MongoClientURI(source);
            MongoClient client = new MongoClient(uri);
            final DocumentNodeStore dns = new DocumentMK.Builder().setMongoDB(client.getDB(uri.getDatabase())).getNodeStore();
            closer.register(Utils.asCloseable(dns));
            store = dns;
        } else {
            store = SegmentTarUtils.bootstrapNodeStore(source, closer);
        }
        deleteClusterId(store);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}
Also used : Closer(com.google.common.io.Closer) MongoClient(com.mongodb.MongoClient) DocumentNodeStore(org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore) NodeStore(org.apache.jackrabbit.oak.spi.state.NodeStore) MongoClientURI(com.mongodb.MongoClientURI) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) DocumentNodeStore(org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore) OptionSet(joptsimple.OptionSet) OptionParser(joptsimple.OptionParser)

Example 62 with MongoClientURI

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

the class Utils method createDocumentMKBuilder.

@CheckForNull
static DocumentMK.Builder createDocumentMKBuilder(NodeStoreOptions options, Closer closer) throws IOException {
    String src = options.getStoreArg();
    if (src == null || src.length() == 0) {
        options.printHelpOn(System.err);
        System.exit(1);
    }
    DocumentMK.Builder builder = new DocumentMK.Builder();
    if (src.startsWith(MongoURI.MONGODB_PREFIX)) {
        MongoClientURI uri = new MongoClientURI(src);
        if (uri.getDatabase() == null) {
            System.err.println("Database missing in MongoDB URI: " + uri.getURI());
            System.exit(1);
        }
        MongoConnection mongo = new MongoConnection(uri.getURI());
        closer.register(asCloseable(mongo));
        builder.setMongoDB(mongo.getDB());
    } else if (src.startsWith("jdbc")) {
        DataSource ds = RDBDataSourceFactory.forJdbcUrl(src, options.getRDBJDBCUser(), options.getRDBJDBCPassword());
        builder.setRDBConnection(ds);
    } else {
        return null;
    }
    builder.setLeaseCheck(false).setClusterId(options.getClusterId());
    if (options.disableBranchesSpec()) {
        builder.disableBranches();
    }
    int cacheSize = options.getCacheSize();
    if (cacheSize != 0) {
        builder.memoryCacheSize(cacheSize * MB);
    }
    return builder;
}
Also used : DocumentMK(org.apache.jackrabbit.oak.plugins.document.DocumentMK) MongoClientURI(com.mongodb.MongoClientURI) MongoConnection(org.apache.jackrabbit.oak.plugins.document.util.MongoConnection) DataSource(javax.sql.DataSource) CheckForNull(javax.annotation.CheckForNull)

Example 63 with MongoClientURI

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

the class CheckpointsCommand method execute.

@Override
public void execute(String... args) throws Exception {
    OptionParser parser = new OptionParser();
    OptionSet options = parser.parse(args);
    if (options.nonOptionArguments().isEmpty()) {
        System.out.println("usage: checkpoints {<path>|<mongo-uri>} [list|rm-all|rm-unreferenced|rm <checkpoint>|info <checkpoint>|set <checkpoint> <name> [<value>]] [--segment]");
        System.exit(1);
    }
    boolean success = false;
    Checkpoints cps;
    Closer closer = Closer.create();
    try {
        String op = "list";
        if (options.nonOptionArguments().size() >= 2) {
            op = options.nonOptionArguments().get(1).toString();
            if (!"list".equals(op) && !"rm-all".equals(op) && !"rm-unreferenced".equals(op) && !"rm".equals(op) && !"info".equals(op) && !"set".equals(op)) {
                failWith("Unknown command.");
            }
        }
        String connection = options.nonOptionArguments().get(0).toString();
        if (connection.startsWith(MongoURI.MONGODB_PREFIX)) {
            MongoClientURI uri = new MongoClientURI(connection);
            MongoClient client = new MongoClient(uri);
            final DocumentNodeStore store = new DocumentMK.Builder().setMongoDB(client.getDB(uri.getDatabase())).getNodeStore();
            closer.register(Utils.asCloseable(store));
            cps = Checkpoints.onDocumentMK(store);
        } else {
            cps = Checkpoints.onSegmentTar(new File(connection), closer);
        }
        System.out.println("Checkpoints " + connection);
        if ("list".equals(op)) {
            int cnt = 0;
            for (Checkpoints.CP cp : cps.list()) {
                System.out.printf("- %s created %s expires %s%n", cp.id, new Timestamp(cp.created), new Timestamp(cp.expires));
                cnt++;
            }
            System.out.println("Found " + cnt + " checkpoints");
        } else if ("rm-all".equals(op)) {
            long time = System.currentTimeMillis();
            long cnt = cps.removeAll();
            time = System.currentTimeMillis() - time;
            if (cnt != -1) {
                System.out.println("Removed " + cnt + " checkpoints in " + time + "ms.");
            } else {
                failWith("Failed to remove all checkpoints.");
            }
        } else if ("rm-unreferenced".equals(op)) {
            long time = System.currentTimeMillis();
            long cnt = cps.removeUnreferenced();
            time = System.currentTimeMillis() - time;
            if (cnt != -1) {
                System.out.println("Removed " + cnt + " checkpoints in " + time + "ms.");
            } else {
                failWith("Failed to remove unreferenced checkpoints.");
            }
        } else if ("rm".equals(op)) {
            if (options.nonOptionArguments().size() < 3) {
                failWith("Missing checkpoint id");
            } else {
                String cp = options.nonOptionArguments().get(2).toString();
                long time = System.currentTimeMillis();
                int cnt = cps.remove(cp);
                time = System.currentTimeMillis() - time;
                if (cnt != 0) {
                    if (cnt == 1) {
                        System.out.println("Removed checkpoint " + cp + " in " + time + "ms.");
                    } else {
                        failWith("Failed to remove checkpoint " + cp);
                    }
                } else {
                    failWith("Checkpoint '" + cp + "' not found.");
                }
            }
        } else if ("info".equals(op)) {
            if (options.nonOptionArguments().size() < 3) {
                failWith("Missing checkpoint id");
            } else {
                String cp = options.nonOptionArguments().get(2).toString();
                Map<String, String> info = cps.getInfo(cp);
                if (info != null) {
                    for (Map.Entry<String, String> e : info.entrySet()) {
                        System.out.println(e.getKey() + '\t' + e.getValue());
                    }
                } else {
                    failWith("Checkpoint '" + cp + "' not found.");
                }
            }
        } else if ("set".equals(op)) {
            if (options.nonOptionArguments().size() < 4) {
                failWith("Missing checkpoint id");
            } else {
                List<?> l = options.nonOptionArguments();
                String cp = l.get(2).toString();
                String name = l.get(3).toString();
                String value = null;
                if (l.size() >= 5) {
                    value = l.get(4).toString();
                }
                long time = System.currentTimeMillis();
                int cnt = cps.setInfoProperty(cp, name, value);
                time = System.currentTimeMillis() - time;
                if (cnt != 0) {
                    if (cnt == 1) {
                        System.out.println("Updated checkpoint " + cp + " in " + time + "ms.");
                    } else {
                        failWith("Failed to remove checkpoint " + cp);
                    }
                } else {
                    failWith("Checkpoint '" + cp + "' not found.");
                }
            }
        }
        success = true;
    } catch (Throwable t) {
        System.err.println(t.getMessage());
    } finally {
        closer.close();
    }
    if (!success) {
        System.exit(1);
    }
}
Also used : Closer(com.google.common.io.Closer) Checkpoints(org.apache.jackrabbit.oak.checkpoint.Checkpoints) MongoClientURI(com.mongodb.MongoClientURI) DocumentNodeStore(org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore) OptionParser(joptsimple.OptionParser) Timestamp(java.sql.Timestamp) MongoClient(com.mongodb.MongoClient) List(java.util.List) OptionSet(joptsimple.OptionSet) File(java.io.File) Map(java.util.Map)

Example 64 with MongoClientURI

use of com.mongodb.MongoClientURI in project GeoGig by boundlessgeo.

the class MongoGraphDatabaseTest method createDatabase.

@Override
protected MongoGraphDatabase createDatabase(Platform platform) throws Exception {
    final IniMongoProperties properties = new IniMongoProperties();
    final String uri = properties.get("mongodb.uri", String.class).or("mongodb://localhost:27017/");
    final String database = properties.get("mongodb.database", String.class).or("geogig");
    MongoClient client = new MongoClient(new MongoClientURI(uri));
    DB db = client.getDB(database);
    db.dropDatabase();
    MongoConnectionManager manager = new MongoConnectionManager();
    ConfigDatabase config = new TestConfigDatabase(platform);
    MongoGraphDatabase mongoGraphDatabase = new MongoGraphDatabase(manager, config);
    return mongoGraphDatabase;
}
Also used : MongoClient(com.mongodb.MongoClient) MongoGraphDatabase(org.locationtech.geogig.storage.mongo.MongoGraphDatabase) ConfigDatabase(org.locationtech.geogig.storage.ConfigDatabase) MongoClientURI(com.mongodb.MongoClientURI) MongoConnectionManager(org.locationtech.geogig.storage.mongo.MongoConnectionManager) DB(com.mongodb.DB)

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