Search in sources :

Example 26 with MongoClientURI

use of com.mongodb.MongoClientURI in project mongo-java-driver by mongodb.

the class QuickTourAdmin method main.

/**
     * Run this main method to see the output of this quick example.
     *
     * @param args takes an optional single argument for the connection string
     */
public static void main(final String[] args) {
    MongoClient mongoClient;
    if (args.length == 0) {
        // connect to the local database server
        mongoClient = new MongoClient();
    } else {
        mongoClient = new MongoClient(new MongoClientURI(args[0]));
    }
    // get handle to "mydb" database
    MongoDatabase database = mongoClient.getDatabase("mydb");
    database.drop();
    // get a handle to the "test" collection
    MongoCollection<Document> collection = database.getCollection("test");
    // drop all the data in it
    collection.drop();
    // getting a list of databases
    for (String name : mongoClient.listDatabaseNames()) {
        System.out.println(name);
    }
    // drop a database
    mongoClient.getDatabase("databaseToBeDropped").drop();
    // create a collection
    database.createCollection("cappedCollection", new CreateCollectionOptions().capped(true).sizeInBytes(0x100000));
    for (String name : database.listCollectionNames()) {
        System.out.println(name);
    }
    // drop a collection:
    collection.drop();
    // create an ascending index on the "i" field
    collection.createIndex(Indexes.ascending("i"));
    // list the indexes on the collection
    for (final Document index : collection.listIndexes()) {
        System.out.println(index.toJson());
    }
    // create a text index on the "content" field
    collection.createIndex(Indexes.text("content"));
    collection.insertOne(new Document("_id", 0).append("content", "textual content"));
    collection.insertOne(new Document("_id", 1).append("content", "additional content"));
    collection.insertOne(new Document("_id", 2).append("content", "irrelevant content"));
    // Find using the text index
    long matchCount = collection.count(text("textual content -irrelevant"));
    System.out.println("Text search matches: " + matchCount);
    // Find using the $language operator
    Bson textSearch = text("textual content -irrelevant", new TextSearchOptions().language("english"));
    matchCount = collection.count(textSearch);
    System.out.println("Text search matches (english): " + matchCount);
    // Find the highest scoring match
    Document projection = new Document("score", new Document("$meta", "textScore"));
    Document myDoc = collection.find(textSearch).projection(projection).first();
    System.out.println("Highest scoring document: " + myDoc.toJson());
    // Run a command
    Document buildInfo = database.runCommand(new Document("buildInfo", 1));
    System.out.println(buildInfo);
    // release resources
    database.drop();
    mongoClient.close();
}
Also used : MongoClient(com.mongodb.MongoClient) MongoClientURI(com.mongodb.MongoClientURI) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) TextSearchOptions(com.mongodb.client.model.TextSearchOptions) Document(org.bson.Document) MongoDatabase(com.mongodb.client.MongoDatabase) Bson(org.bson.conversions.Bson)

Example 27 with MongoClientURI

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

the class DocumentNodeStoreService method registerNodeStore.

private void registerNodeStore() throws IOException {
    String uri = PropertiesUtil.toString(prop(PROP_URI, FWK_PROP_URI), DEFAULT_URI);
    String db = PropertiesUtil.toString(prop(PROP_DB, FWK_PROP_DB), DEFAULT_DB);
    boolean soKeepAlive = PropertiesUtil.toBoolean(prop(PROP_SO_KEEP_ALIVE, FWK_PROP_SO_KEEP_ALIVE), DEFAULT_SO_KEEP_ALIVE);
    int cacheSize = toInteger(prop(PROP_CACHE), DEFAULT_CACHE);
    int nodeCachePercentage = toInteger(prop(PROP_NODE_CACHE_PERCENTAGE), DEFAULT_NODE_CACHE_PERCENTAGE);
    int prevDocCachePercentage = toInteger(prop(PROP_PREV_DOC_CACHE_PERCENTAGE), DEFAULT_NODE_CACHE_PERCENTAGE);
    int childrenCachePercentage = toInteger(prop(PROP_CHILDREN_CACHE_PERCENTAGE), DEFAULT_CHILDREN_CACHE_PERCENTAGE);
    int diffCachePercentage = toInteger(prop(PROP_DIFF_CACHE_PERCENTAGE), DEFAULT_DIFF_CACHE_PERCENTAGE);
    int blobCacheSize = toInteger(prop(PROP_BLOB_CACHE_SIZE), DEFAULT_BLOB_CACHE_SIZE);
    String persistentCache = getPath(PROP_PERSISTENT_CACHE, DEFAULT_PERSISTENT_CACHE);
    String journalCache = getPath(PROP_JOURNAL_CACHE, DEFAULT_JOURNAL_CACHE);
    int cacheSegmentCount = toInteger(prop(PROP_CACHE_SEGMENT_COUNT), DEFAULT_CACHE_SEGMENT_COUNT);
    int cacheStackMoveDistance = toInteger(prop(PROP_CACHE_STACK_MOVE_DISTANCE), DEFAULT_CACHE_STACK_MOVE_DISTANCE);
    boolean bundlingDisabled = toBoolean(prop(PROP_BUNDLING_DISABLED), DEFAULT_BUNDLING_DISABLED);
    boolean prefetchExternalChanges = toBoolean(prop(PROP_PREFETCH_EXTERNAL_CHANGES), false);
    int updateLimit = toInteger(prop(PROP_UPDATE_LIMIT), DocumentMK.UPDATE_LIMIT);
    DocumentMK.Builder mkBuilder = new DocumentMK.Builder().setStatisticsProvider(statisticsProvider).memoryCacheSize(cacheSize * MB).memoryCacheDistribution(nodeCachePercentage, prevDocCachePercentage, childrenCachePercentage, diffCachePercentage).setCacheSegmentCount(cacheSegmentCount).setCacheStackMoveDistance(cacheStackMoveDistance).setBundlingDisabled(bundlingDisabled).setJournalPropertyHandlerFactory(journalPropertyHandlerFactory).setLeaseCheck(!ClusterNodeInfo.DEFAULT_LEASE_CHECK_DISABLED).setLeaseFailureHandler(new LeaseFailureHandler() {

        @Override
        public void handleLeaseFailure() {
            try {
                // plan A: try stopping oak-core
                log.error("handleLeaseFailure: stopping oak-core...");
                Bundle bundle = context.getBundleContext().getBundle();
                bundle.stop(Bundle.STOP_TRANSIENT);
                log.error("handleLeaseFailure: stopped oak-core.");
            // plan A worked, perfect!
            } catch (BundleException e) {
                log.error("handleLeaseFailure: exception while stopping oak-core: " + e, e);
                // plan B: stop only DocumentNodeStoreService (to stop the background threads)
                log.error("handleLeaseFailure: stopping DocumentNodeStoreService...");
                context.disableComponent(DocumentNodeStoreService.class.getName());
                log.error("handleLeaseFailure: stopped DocumentNodeStoreService");
            // plan B succeeded.
            }
        }
    }).setPrefetchExternalChanges(prefetchExternalChanges).setUpdateLimit(updateLimit);
    if (!Strings.isNullOrEmpty(persistentCache)) {
        mkBuilder.setPersistentCache(persistentCache);
    }
    if (!Strings.isNullOrEmpty(journalCache)) {
        mkBuilder.setJournalCache(journalCache);
    }
    boolean wrappingCustomBlobStore = customBlobStore && blobStore instanceof BlobStoreWrapper;
    //Set blobstore before setting the DB
    if (customBlobStore && !wrappingCustomBlobStore) {
        checkNotNull(blobStore, "Use of custom BlobStore enabled via  [%s] but blobStore reference not " + "initialized", CUSTOM_BLOB_STORE);
        mkBuilder.setBlobStore(blobStore);
    }
    if (documentStoreType == DocumentStoreType.RDB) {
        checkNotNull(dataSource, "DataStore type set [%s] but DataSource reference not initialized", PROP_DS_TYPE);
        if (!customBlobStore) {
            checkNotNull(blobDataSource, "DataStore type set [%s] but BlobDataSource reference not initialized", PROP_DS_TYPE);
            mkBuilder.setRDBConnection(dataSource, blobDataSource);
            log.info("Connected to datasources {} {}", dataSource, blobDataSource);
        } else {
            if (blobDataSource != null && blobDataSource != dataSource) {
                log.info("Ignoring blobDataSource {} as custom blob store takes precedence.", blobDataSource);
            }
            mkBuilder.setRDBConnection(dataSource);
            log.info("Connected to datasource {}", dataSource);
        }
    } else {
        MongoClientURI mongoURI = new MongoClientURI(uri);
        if (log.isInfoEnabled()) {
            // Take care around not logging the uri directly as it
            // might contain passwords
            log.info("Starting DocumentNodeStore with host={}, db={}, cache size (MB)={}, persistentCache={}, " + "journalCache={}, blobCacheSize (MB)={}, maxReplicationLagInSecs={}", mongoURI.getHosts(), db, cacheSize, persistentCache, journalCache, blobCacheSize, maxReplicationLagInSecs);
            log.info("Mongo Connection details {}", MongoConnection.toString(mongoURI.getOptions()));
        }
        mkBuilder.setMaxReplicationLag(maxReplicationLagInSecs, TimeUnit.SECONDS);
        mkBuilder.setSocketKeepAlive(soKeepAlive);
        mkBuilder.setMongoDB(uri, db, blobCacheSize);
        log.info("Connected to database '{}'", db);
    }
    if (!customBlobStore) {
        defaultBlobStore = mkBuilder.getBlobStore();
        log.info("Registering the BlobStore with ServiceRegistry");
        blobStoreReg = context.getBundleContext().registerService(BlobStore.class.getName(), defaultBlobStore, null);
    }
    //Set wrapping blob store after setting the DB
    if (wrappingCustomBlobStore) {
        ((BlobStoreWrapper) blobStore).setBlobStore(mkBuilder.getBlobStore());
        mkBuilder.setBlobStore(blobStore);
    }
    mkBuilder.setExecutor(executor);
    // attach GCMonitor
    final GCMonitorTracker gcMonitor = new GCMonitorTracker();
    gcMonitor.start(whiteboard);
    closer.register(asCloseable(gcMonitor));
    mkBuilder.setGCMonitor(gcMonitor);
    nodeStore = mkBuilder.getNodeStore();
    // ensure a clusterId is initialized 
    // and expose it as 'oak.clusterid' repository descriptor
    GenericDescriptors clusterIdDesc = new GenericDescriptors();
    clusterIdDesc.put(ClusterRepositoryInfo.OAK_CLUSTERID_REPOSITORY_DESCRIPTOR_KEY, new SimpleValueFactory().createValue(ClusterRepositoryInfo.getOrCreateId(nodeStore)), true, false);
    whiteboard.register(Descriptors.class, clusterIdDesc, Collections.emptyMap());
    // If a shared data store register the repo id in the data store
    if (SharedDataStoreUtils.isShared(blobStore)) {
        String repoId = null;
        try {
            repoId = ClusterRepositoryInfo.getOrCreateId(nodeStore);
            ((SharedDataStore) blobStore).addMetadataRecord(new ByteArrayInputStream(new byte[0]), SharedDataStoreUtils.SharedStoreRecordType.REPOSITORY.getNameFromId(repoId));
        } catch (Exception e) {
            throw new IOException("Could not register a unique repositoryId", e);
        }
        if (blobStore instanceof BlobTrackingStore) {
            final long trackSnapshotInterval = toLong(prop(PROP_BLOB_SNAPSHOT_INTERVAL), DEFAULT_BLOB_SNAPSHOT_INTERVAL);
            String root = getRepositoryHome();
            BlobTrackingStore trackingStore = (BlobTrackingStore) blobStore;
            if (trackingStore.getTracker() != null) {
                trackingStore.getTracker().close();
            }
            ((BlobTrackingStore) blobStore).addTracker(new BlobIdTracker(root, repoId, trackSnapshotInterval, (SharedDataStore) blobStore));
        }
    }
    registerJMXBeans(nodeStore, mkBuilder);
    registerLastRevRecoveryJob(nodeStore);
    registerJournalGC(nodeStore);
    if (!isNodeStoreProvider()) {
        observerTracker = new ObserverTracker(nodeStore);
        observerTracker.start(context.getBundleContext());
    }
    journalPropertyHandlerFactory.start(whiteboard);
    DocumentStore ds = nodeStore.getDocumentStore();
    // OAK-2682: time difference detection applied at startup with a default
    // max time diff of 2000 millis (2sec)
    final long maxDiff = Long.parseLong(System.getProperty("oak.documentMK.maxServerTimeDiffMillis", "2000"));
    try {
        if (maxDiff >= 0) {
            final long timeDiff = ds.determineServerTimeDifferenceMillis();
            log.info("registerNodeStore: server time difference: {}ms (max allowed: {}ms)", timeDiff, maxDiff);
            if (Math.abs(timeDiff) > maxDiff) {
                throw new AssertionError("Server clock seems off (" + timeDiff + "ms) by more than configured amount (" + maxDiff + "ms)");
            }
        }
    } catch (RuntimeException e) {
        // no checked exception
        // in case of a RuntimeException, just log but continue
        log.warn("registerNodeStore: got RuntimeException while trying to determine time difference to server: " + e, e);
    }
    String[] serviceClasses;
    if (isNodeStoreProvider()) {
        registerNodeStoreProvider(nodeStore);
        serviceClasses = new String[] { DocumentNodeStore.class.getName(), Clusterable.class.getName() };
    } else {
        serviceClasses = new String[] { NodeStore.class.getName(), DocumentNodeStore.class.getName(), Clusterable.class.getName() };
    }
    Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put(Constants.SERVICE_PID, DocumentNodeStore.class.getName());
    props.put(DESCRIPTION, getMetadata(ds));
    // OAK-2844: in order to allow DocumentDiscoveryLiteService to directly
    // require a service DocumentNodeStore (instead of having to do an 'instanceof')
    // the registration is now done for both NodeStore and DocumentNodeStore here.
    nodeStoreReg = context.getBundleContext().registerService(serviceClasses, nodeStore, props);
}
Also used : ObserverTracker(org.apache.jackrabbit.oak.spi.commit.ObserverTracker) MongoClientURI(com.mongodb.MongoClientURI) Clusterable(org.apache.jackrabbit.oak.spi.state.Clusterable) NodeStore(org.apache.jackrabbit.oak.spi.state.NodeStore) BlobTrackingStore(org.apache.jackrabbit.oak.plugins.blob.BlobTrackingStore) BundleException(org.osgi.framework.BundleException) SharedDataStore(org.apache.jackrabbit.oak.plugins.blob.SharedDataStore) GCMonitorTracker(org.apache.jackrabbit.oak.spi.gc.GCMonitorTracker) GenericDescriptors(org.apache.jackrabbit.oak.spi.descriptors.GenericDescriptors) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) IOException(java.io.IOException) SimpleValueFactory(org.apache.jackrabbit.commons.SimpleValueFactory) BundleException(org.osgi.framework.BundleException) IOException(java.io.IOException) BlobIdTracker(org.apache.jackrabbit.oak.plugins.blob.datastore.BlobIdTracker) ByteArrayInputStream(java.io.ByteArrayInputStream) BlobStoreWrapper(org.apache.jackrabbit.oak.spi.blob.BlobStoreWrapper)

Example 28 with MongoClientURI

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

the class MongoDocumentStore method setReadWriteMode.

@Override
public void setReadWriteMode(String readWriteMode) {
    if (readWriteMode == null || readWriteMode.equals(lastReadWriteMode)) {
        return;
    }
    lastReadWriteMode = readWriteMode;
    try {
        String rwModeUri = readWriteMode;
        if (!readWriteMode.startsWith("mongodb://")) {
            rwModeUri = String.format("mongodb://localhost/?%s", readWriteMode);
        }
        MongoClientURI uri = new MongoClientURI(rwModeUri);
        ReadPreference readPref = uri.getOptions().getReadPreference();
        if (!readPref.equals(nodes.getReadPreference())) {
            nodes.setReadPreference(readPref);
            LOG.info("Using ReadPreference {} ", readPref);
        }
        WriteConcern writeConcern = uri.getOptions().getWriteConcern();
        if (!writeConcern.equals(nodes.getWriteConcern())) {
            nodes.setWriteConcern(writeConcern);
            LOG.info("Using WriteConcern " + writeConcern);
        }
    } catch (Exception e) {
        LOG.error("Error setting readWriteMode " + readWriteMode, e);
    }
}
Also used : ReadPreference(com.mongodb.ReadPreference) WriteConcern(com.mongodb.WriteConcern) MongoClientURI(com.mongodb.MongoClientURI) MongoException(com.mongodb.MongoException) DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) BulkWriteException(com.mongodb.BulkWriteException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 29 with MongoClientURI

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

the class TextExtractorMain method bootStrapNodeStore.

private static NodeStore bootStrapNodeStore(String src, BlobStore blobStore, Closer closer) throws IOException {
    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));
        DocumentNodeStore store = new DocumentMK.Builder().setBlobStore(blobStore).setMongoDB(mongo.getDB()).getNodeStore();
        closer.register(asCloseable(store));
        return store;
    }
    return SegmentTarUtils.bootstrap(src, blobStore, closer);
}
Also used : MongoClientURI(com.mongodb.MongoClientURI) DocumentMK(org.apache.jackrabbit.oak.plugins.document.DocumentMK) DocumentNodeStore(org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore) MongoConnection(org.apache.jackrabbit.oak.plugins.document.util.MongoConnection)

Example 30 with MongoClientURI

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

the class UnlockUpgradeCommand method execute.

@Override
public void execute(String... args) throws Exception {
    OptionParser parser = new OptionParser();
    // RDB specific options
    OptionSpec<String> rdbjdbcuser = parser.accepts("rdbjdbcuser", "RDB JDBC user").withOptionalArg().defaultsTo("");
    OptionSpec<String> rdbjdbcpasswd = parser.accepts("rdbjdbcpasswd", "RDB JDBC password").withOptionalArg().defaultsTo("");
    OptionSpec<String> nonOption = parser.nonOptions("unlockUpgrade {<jdbc-uri> | <mongodb-uri>}");
    OptionSpec help = parser.acceptsAll(asList("h", "?", "help"), "show help").forHelp();
    OptionSet options = parser.parse(args);
    List<String> nonOptions = nonOption.values(options);
    if (options.has(help)) {
        parser.printHelpOn(System.out);
        return;
    }
    if (nonOptions.isEmpty()) {
        parser.printHelpOn(System.err);
        return;
    }
    DocumentStore store = null;
    try {
        String uri = nonOptions.get(0);
        if (uri.startsWith(MONGODB_PREFIX)) {
            MongoClientURI clientURI = new MongoClientURI(uri);
            if (clientURI.getDatabase() == null) {
                System.err.println("Database missing in MongoDB URI: " + clientURI.getURI());
            } else {
                MongoConnection mongo = new MongoConnection(clientURI.getURI());
                store = new MongoDocumentStore(mongo.getDB(), new DocumentMK.Builder());
            }
        } else if (uri.startsWith("jdbc")) {
            DataSource ds = RDBDataSourceFactory.forJdbcUrl(uri, rdbjdbcuser.value(options), rdbjdbcpasswd.value(options));
            store = new RDBDocumentStore(ds, new DocumentMK.Builder());
        } else {
            System.err.println("Unrecognized URI: " + uri);
        }
        if (store != null && VERSION.writeTo(store)) {
            System.out.println("Format version set to " + VERSION);
        }
    } catch (DocumentStoreException e) {
        System.err.println(e.getMessage());
    } finally {
        if (store != null) {
            store.dispose();
        }
    }
}
Also used : OptionSpec(joptsimple.OptionSpec) DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) MongoClientURI(com.mongodb.MongoClientURI) DocumentMK(org.apache.jackrabbit.oak.plugins.document.DocumentMK) OptionParser(joptsimple.OptionParser) MongoDocumentStore(org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore) DataSource(javax.sql.DataSource) DocumentStore(org.apache.jackrabbit.oak.plugins.document.DocumentStore) MongoDocumentStore(org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore) RDBDocumentStore(org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore) RDBDocumentStore(org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore) OptionSet(joptsimple.OptionSet) MongoConnection(org.apache.jackrabbit.oak.plugins.document.util.MongoConnection)

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