Example usage for com.mongodb Bytes QUERYOPTION_SLAVEOK

List of usage examples for com.mongodb Bytes QUERYOPTION_SLAVEOK

Introduction

In this page you can find the example usage for com.mongodb Bytes QUERYOPTION_SLAVEOK.

Prototype

int QUERYOPTION_SLAVEOK

To view the source code for com.mongodb Bytes QUERYOPTION_SLAVEOK.

Click Source Link

Document

When turned on, read queries will be directed to slave servers instead of the primary server.

Usage

From source file:com.edgytech.umongo.MongoUtils.java

License:Apache License

public static String queryOptionsToString(int options) {
    String opt = "";
    if ((options & Bytes.QUERYOPTION_TAILABLE) != 0) {
        opt += "TAILABLE ";
    }//from w  ww  . ja  va  2s. c  o m
    if ((options & Bytes.QUERYOPTION_SLAVEOK) != 0) {
        opt += "SLAVEOK ";
    }
    if ((options & Bytes.QUERYOPTION_OPLOGREPLAY) != 0) {
        opt += "OPLOGREPLAY ";
    }
    if ((options & Bytes.QUERYOPTION_NOTIMEOUT) != 0) {
        opt += "NOTIMEOUT ";
    }
    if ((options & Bytes.QUERYOPTION_AWAITDATA) != 0) {
        opt += "AWAITDATA ";
    }
    if ((options & Bytes.QUERYOPTION_EXHAUST) != 0) {
        opt += "EXHAUST ";
    }
    return opt;
}

From source file:com.edgytech.umongo.OptionDialog.java

License:Apache License

void update(int options, WriteConcern wc, ReadPreference rp) {
    // reset//  ww w.  ja  v  a2  s  .com
    xmlLoadCheckpoint();

    setBooleanFieldValue(Item.tailable, (options & Bytes.QUERYOPTION_TAILABLE) != 0);
    setBooleanFieldValue(Item.slaveOk, (options & Bytes.QUERYOPTION_SLAVEOK) != 0);
    setBooleanFieldValue(Item.opLogReplay, (options & Bytes.QUERYOPTION_OPLOGREPLAY) != 0);
    setBooleanFieldValue(Item.noTimeout, (options & Bytes.QUERYOPTION_NOTIMEOUT) != 0);
    setBooleanFieldValue(Item.awaitData, (options & Bytes.QUERYOPTION_AWAITDATA) != 0);
    setBooleanFieldValue(Item.exhaust, (options & Bytes.QUERYOPTION_EXHAUST) != 0);
    setBooleanFieldValue(Item.partial, (options & Bytes.QUERYOPTION_PARTIAL) != 0);

    Object w = wc.getWObject();
    int wInt = (Integer) (w instanceof Integer ? w : 0);
    String wStr = (String) (w instanceof String ? w : "");
    setIntFieldValue(Item.writeFactor, wInt);
    setStringFieldValue(Item.writePolicy, wStr);
    setIntFieldValue(Item.writeTimeout, wc.getWtimeout());
    //        setBooleanFieldValue(Item.fsync, wc.fsync());

    DBObject rpObj = rp.toDBObject();
    ComboBox readBox = (ComboBox) getBoundUnit(Item.rpPreference);
    ReadPref rpEnm = ReadPref.primary;
    if (rp != null)
        rpEnm = ReadPref.valueOf(rp.getName());
    readBox.value = rpEnm.ordinal();
    if (rpObj.containsField("tags")) {
        List tags = (List) rpObj.get("tags");
        if (tags.size() > 0) {
            ((DocBuilderField) getBoundComponentUnit(Item.rpTag)).setDBObject((DBObject) tags.get(0));
        }
    }
}

From source file:com.edgytech.umongo.OptionDialog.java

License:Apache License

int getQueryOptions() {
    int options = 0;
    if (getBooleanFieldValue(Item.tailable))
        options |= Bytes.QUERYOPTION_TAILABLE;
    if (getBooleanFieldValue(Item.slaveOk))
        options |= Bytes.QUERYOPTION_SLAVEOK;
    if (getBooleanFieldValue(Item.opLogReplay))
        options |= Bytes.QUERYOPTION_OPLOGREPLAY;
    if (getBooleanFieldValue(Item.noTimeout))
        options |= Bytes.QUERYOPTION_NOTIMEOUT;
    if (getBooleanFieldValue(Item.awaitData))
        options |= Bytes.QUERYOPTION_AWAITDATA;
    if (getBooleanFieldValue(Item.exhaust))
        options |= Bytes.QUERYOPTION_EXHAUST;
    if (getBooleanFieldValue(Item.partial))
        options |= Bytes.QUERYOPTION_PARTIAL;
    return options;
}

From source file:com.edgytech.umongo.ServerNode.java

License:Apache License

public ServerNode(ServerAddress serverAddress, MongoClientOptions opts, boolean isReplica, boolean isConfig) {
    setLabel(serverAddress.toString());//from w ww.jav a  2 s  . c o  m
    this.serverAddress = serverAddress;
    serverMongo = new MongoClient(serverAddress, opts);
    serverMongo.addOption(Bytes.QUERYOPTION_SLAVEOK);
    this.isReplica = isReplica;
    this.isConfig = isConfig;

    try {
        xmlLoad(Resource.getXmlDir(), Resource.File.serverNode, null);
    } catch (Exception ex) {
        getLogger().log(Level.SEVERE, null, ex);
    }

    markStructured();
}

From source file:com.edgytech.umongo.ServerNode.java

License:Apache License

public ServerNode(String host, MongoClientOptions opts, boolean isReplica, boolean isConfig)
        throws UnknownHostException {
    setLabel(host);//w  w w.jav a 2 s  . c om
    this.host = host;
    this.serverAddress = new ServerAddress(host);
    serverMongo = new MongoClient(serverAddress, opts);
    serverMongo.addOption(Bytes.QUERYOPTION_SLAVEOK);
    this.isReplica = isReplica;
    this.isConfig = isConfig;

    try {
        xmlLoad(Resource.getXmlDir(), Resource.File.serverNode, null);
    } catch (Exception ex) {
        getLogger().log(Level.SEVERE, null, ex);
    }

    markStructured();
}

From source file:org.apache.jackrabbit.oak.plugins.document.mongo.MongoBlobStore.java

License:Apache License

@Override
public Iterator<String> getAllChunkIds(long maxLastModifiedTime) throws Exception {
    DBCollection collection = getBlobCollection();

    DBObject fields = new BasicDBObject();
    fields.put(MongoBlob.KEY_ID, 1);//from   w w w.  j  a  v  a2s .  c om

    QueryBuilder builder = new QueryBuilder();
    if (maxLastModifiedTime != 0 && maxLastModifiedTime != -1) {
        builder.and(MongoBlob.KEY_LAST_MOD).lessThanEquals(maxLastModifiedTime);
    }

    final DBCursor cur = collection.find(builder.get(), fields).hint(fields)
            .addOption(Bytes.QUERYOPTION_SLAVEOK);

    //TODO The cursor needs to be closed
    return new AbstractIterator<String>() {
        @Override
        protected String computeNext() {
            if (cur.hasNext()) {
                MongoBlob blob = (MongoBlob) cur.next();
                if (blob != null) {
                    return blob.getId();
                }
            }
            return endOfData();
        }
    };
}

From source file:org.mule.module.mongo.tools.MongoDump.java

License:Open Source License

public void dump(final String outputDirectory, final String database, String outputName, final int threads)
        throws IOException {
    Validate.notNull(outputDirectory);//from   w w  w.  ja  v a2 s .  c  o m
    Validate.notNull(outputName);
    Validate.notNull(database);

    String opName = outputName;
    opName += appendTimestamp();

    initOplog(database);

    final Collection<String> collections = mongoClient.listCollections();
    if (collections != null) {
        final ExecutorService executor = Executors.newFixedThreadPool(threads);
        final DumpWriter dumpWriter = new BsonDumpWriter(outputDirectory, opName);
        for (final String collectionName : collections) {
            final DBCollection dbCollection = mongoClient.getCollection(collectionName);
            final MongoDumpCollection dumpCollection = new MongoDumpCollection(dbCollection);
            dumpCollection.setDumpWriter(dumpWriter);

            final Future<Void> future = executor.submit(dumpCollection);
            propagateException(future);
        }

        executor.shutdown();
        try {
            if (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
                executor.shutdownNow();
            }

            if (oplog) {
                final ExecutorService singleExecutor = Executors.newSingleThreadExecutor();
                final MongoDumpCollection dumpCollection = new MongoDumpCollection(oplogCollection);
                dumpCollection.setName(BackupConstants.OPLOG);
                dumpCollection.addOption(Bytes.QUERYOPTION_OPLOGREPLAY);
                dumpCollection.addOption(Bytes.QUERYOPTION_SLAVEOK);
                final DBObject query = new BasicDBObject();
                query.put(BackupConstants.TIMESTAMP_FIELD, new BasicDBObject("$gt", oplogStart));
                // Filter only oplogs for given database
                query.put(BackupConstants.NAMESPACE_FIELD, BackupUtils.getNamespacePattern(database));
                dumpCollection.setQuery(query);
                dumpCollection.setDumpWriter(dumpWriter);
                final Future<Void> future = singleExecutor.submit(dumpCollection);
                propagateException(future);
            }

            if (zip) {
                final String dbDumpPath = outputDirectory + File.separator + opName;
                ZipUtils.zipDirectory(dbDumpPath);
                FileUtils.deleteDirectory(new File(dbDumpPath));
            }
        } catch (final InterruptedException ie) {
            executor.shutdownNow();
            Thread.currentThread().interrupt();
        }
    }
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

License:Apache License

@Deprecated
public CommandResult executeCommand(final DBObject command, final int options) {
    return executeCommand(command,
            (options & Bytes.QUERYOPTION_SLAVEOK) != 0 ? ReadPreference.secondaryPreferred()
                    : ReadPreference.primary());
}