Search in sources :

Example 31 with MongoClientURI

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

the class DataStoreCheckCommand method execute.

@Override
public void execute(String... args) throws Exception {
    OptionParser parser = new OptionParser();
    parser.allowsUnrecognizedOptions();
    String helpStr = "datastorecheck [--id] [--ref] [--consistency] [--store <path>|<mongo_uri>] " + "[--s3ds <s3ds_config>|--fds <fds_config>|--azureblobds <azureblobds_config>] [--dump <path>]";
    Closer closer = Closer.create();
    try {
        // Options for operations requested
        OptionSpecBuilder idOp = parser.accepts("id", "Get ids");
        OptionSpecBuilder refOp = parser.accepts("ref", "Get references");
        OptionSpecBuilder consistencyOp = parser.accepts("consistency", "Check consistency");
        // Node Store - needed for --ref, --consistency
        ArgumentAcceptingOptionSpec<String> store = parser.accepts("store", "Node Store").requiredIf(refOp, consistencyOp).withRequiredArg().ofType(String.class);
        // Optional argument to specify the dump path
        ArgumentAcceptingOptionSpec<String> dump = parser.accepts("dump", "Dump Path").withRequiredArg().ofType(String.class);
        // Optional argument to specify tracking
        ArgumentAcceptingOptionSpec<String> track = parser.accepts("track", "Local repository home folder").withRequiredArg().ofType(String.class);
        OptionSpec<?> help = parser.acceptsAll(asList("h", "?", "help"), "show help").forHelp();
        // Required rules (any one of --id, --ref, --consistency)
        idOp.requiredUnless(refOp, consistencyOp);
        refOp.requiredUnless(idOp, consistencyOp);
        consistencyOp.requiredUnless(idOp, refOp);
        OptionSet options = null;
        try {
            options = parser.parse(args);
        } catch (Exception e) {
            System.err.println(e);
            parser.printHelpOn(System.err);
            return;
        }
        if (options.has(help)) {
            parser.printHelpOn(System.out);
            return;
        }
        String dumpPath = JAVA_IO_TMPDIR.value();
        if (options.has(dump)) {
            dumpPath = options.valueOf(dump);
        }
        GarbageCollectableBlobStore blobStore = null;
        BlobReferenceRetriever marker = null;
        if (options.has(store)) {
            String source = options.valueOf(store);
            if (source.startsWith(MongoURI.MONGODB_PREFIX)) {
                MongoClientURI uri = new MongoClientURI(source);
                MongoClient client = new MongoClient(uri);
                DocumentNodeStore nodeStore = new DocumentMK.Builder().setMongoDB(client.getDB(uri.getDatabase())).getNodeStore();
                closer.register(Utils.asCloseable(nodeStore));
                blobStore = (GarbageCollectableBlobStore) nodeStore.getBlobStore();
                marker = new DocumentBlobReferenceRetriever(nodeStore);
            } else {
                marker = SegmentTarUtils.newBlobReferenceRetriever(source, closer);
            }
        }
        // Initialize S3/FileDataStore if configured
        GarbageCollectableBlobStore dataStore = Utils.bootstrapDataStore(args, closer);
        if (dataStore != null) {
            blobStore = dataStore;
        }
        // blob store still not initialized means configuration not supported
        if (blobStore == null) {
            System.err.println("Operation not defined for SegmentNodeStore without external datastore");
            parser.printHelpOn(System.err);
            return;
        }
        FileRegister register = new FileRegister(options);
        closer.register(register);
        if (options.has(idOp) || options.has(consistencyOp)) {
            File dumpFile = register.createFile(idOp, dumpPath);
            retrieveBlobIds(blobStore, dumpFile);
            // If track path specified copy the file to the location
            if (options.has(track)) {
                String trackPath = options.valueOf(track);
                File trackingFileParent = new File(FilenameUtils.concat(trackPath, "blobids"));
                File trackingFile = new File(trackingFileParent, "blob-" + String.valueOf(System.currentTimeMillis()) + ".gen");
                FileUtils.copyFile(dumpFile, trackingFile);
            }
        }
        if (options.has(refOp) || options.has(consistencyOp)) {
            retrieveBlobReferences(blobStore, marker, register.createFile(refOp, dumpPath));
        }
        if (options.has(consistencyOp)) {
            checkConsistency(register.get(idOp), register.get(refOp), register.createFile(consistencyOp, dumpPath));
        }
    } catch (Throwable t) {
        t.printStackTrace();
    } finally {
        closer.close();
    }
}
Also used : Closer(com.google.common.io.Closer) MongoClientURI(com.mongodb.MongoClientURI) OptionSpecBuilder(joptsimple.OptionSpecBuilder) DocumentNodeStore(org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore) OptionParser(joptsimple.OptionParser) IOException(java.io.IOException) MongoClient(com.mongodb.MongoClient) OptionSpecBuilder(joptsimple.OptionSpecBuilder) DocumentBlobReferenceRetriever(org.apache.jackrabbit.oak.plugins.document.DocumentBlobReferenceRetriever) BlobReferenceRetriever(org.apache.jackrabbit.oak.plugins.blob.BlobReferenceRetriever) GarbageCollectableBlobStore(org.apache.jackrabbit.oak.spi.blob.GarbageCollectableBlobStore) OptionSet(joptsimple.OptionSet) DocumentBlobReferenceRetriever(org.apache.jackrabbit.oak.plugins.document.DocumentBlobReferenceRetriever) File(java.io.File)

Example 32 with MongoClientURI

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

the class MongoNodeStoreContainer method clean.

@Override
public void clean() throws IOException {
    MongoClientURI uri = new MongoClientURI(mongoUri);
    MongoClient client = new MongoClient(uri);
    client.dropDatabase(uri.getDatabase());
    blob.clean();
}
Also used : MongoClient(com.mongodb.MongoClient) MongoClientURI(com.mongodb.MongoClientURI)

Example 33 with MongoClientURI

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

the class NodeStoreFixtureProvider method configureDocumentMk.

private static NodeStore configureDocumentMk(Options options, BlobStore blobStore, StatisticsProvider statisticsProvider, Closer closer, Whiteboard wb, boolean readOnly) throws UnknownHostException {
    DocumentMK.Builder builder = new DocumentMK.Builder();
    if (blobStore != null) {
        builder.setBlobStore(blobStore);
    }
    DocumentNodeStoreOptions docStoreOpts = options.getOptionBean(DocumentNodeStoreOptions.class);
    builder.setClusterId(docStoreOpts.getClusterId());
    builder.setStatisticsProvider(statisticsProvider);
    if (readOnly) {
        builder.setReadOnlyMode();
    }
    int cacheSize = docStoreOpts.getCacheSize();
    if (cacheSize != 0) {
        builder.memoryCacheSize(cacheSize * MB);
    }
    if (docStoreOpts.disableBranchesSpec()) {
        builder.disableBranches();
    }
    CommonOptions commonOpts = options.getOptionBean(CommonOptions.class);
    if (docStoreOpts.isCacheDistributionDefined()) {
        builder.memoryCacheDistribution(docStoreOpts.getNodeCachePercentage(), docStoreOpts.getPrevDocCachePercentage(), docStoreOpts.getChildrenCachePercentage(), docStoreOpts.getDiffCachePercentage());
    }
    if (commonOpts.isMongo()) {
        MongoClientURI uri = new MongoClientURI(commonOpts.getStoreArg());
        if (uri.getDatabase() == null) {
            System.err.println("Database missing in MongoDB URI: " + uri.getURI());
            System.exit(1);
        }
        MongoConnection mongo = new MongoConnection(uri.getURI());
        wb.register(MongoConnection.class, mongo, Collections.emptyMap());
        closer.register(mongo::close);
        builder.setMongoDB(mongo.getDB());
    } else if (commonOpts.isRDB()) {
        RDBStoreOptions rdbOpts = options.getOptionBean(RDBStoreOptions.class);
        DataSource ds = RDBDataSourceFactory.forJdbcUrl(commonOpts.getStoreArg(), rdbOpts.getUser(), rdbOpts.getPassword());
        wb.register(DataSource.class, ds, Collections.emptyMap());
        builder.setRDBConnection(ds);
    }
    return builder.getNodeStore();
}
Also used : FileStoreBuilder.fileStoreBuilder(org.apache.jackrabbit.oak.segment.file.FileStoreBuilder.fileStoreBuilder) FileStoreBuilder(org.apache.jackrabbit.oak.segment.file.FileStoreBuilder) 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)

Example 34 with MongoClientURI

use of com.mongodb.MongoClientURI in project cas by apereo.

the class MongoAuthenticationHandler method getAuthenticator.

@Override
protected Authenticator<UsernamePasswordCredentials> getAuthenticator(final Credential credential) {
    final MongoClientURI uri = new MongoClientURI(this.mongoHostUri);
    final MongoClient client = new MongoClient(uri);
    LOGGER.info("Connected to MongoDb instance @ [{}] using database [{}]", uri.getHosts(), uri.getDatabase());
    final MongoAuthenticator mongoAuthenticator = new MongoAuthenticator(client, this.attributes);
    mongoAuthenticator.setUsersCollection(this.collectionName);
    mongoAuthenticator.setUsersDatabase(uri.getDatabase());
    mongoAuthenticator.setUsernameAttribute(this.usernameAttribute);
    mongoAuthenticator.setPasswordAttribute(this.passwordAttribute);
    mongoAuthenticator.setPasswordEncoder(this.mongoPasswordEncoder);
    return mongoAuthenticator;
}
Also used : MongoClient(com.mongodb.MongoClient) MongoClientURI(com.mongodb.MongoClientURI) MongoAuthenticator(org.pac4j.mongo.credentials.authenticator.MongoAuthenticator)

Example 35 with MongoClientURI

use of com.mongodb.MongoClientURI in project android-uploader by nightscout.

the class MongoUploaderTest method setUp.

@Before
public void setUp() throws Exception {
    mockCollection = mock(DBCollection.class);
    preferences = new TestPreferences();
    mongoUploader = new MongoUploader(preferences, new MongoClientURI("mongodb://localhost"), "collection", "dsCollection");
    mongoUploader.setCollection(mockCollection);
    mongoUploader.setDeviceStatusCollection(mockCollection);
    setUpUpsertCapture();
}
Also used : DBCollection(com.mongodb.DBCollection) MongoClientURI(com.mongodb.MongoClientURI) TestPreferences(com.nightscout.core.preferences.TestPreferences) Before(org.junit.Before)

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