Example usage for com.mongodb MongoClient getReadPreference

List of usage examples for com.mongodb MongoClient getReadPreference

Introduction

In this page you can find the example usage for com.mongodb MongoClient getReadPreference.

Prototype

public ReadPreference getReadPreference() 

Source Link

Document

Gets the default read preference

Usage

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

License:Apache License

@Override
protected void updateComponentCustom(JPanel old) {
    try {/* w  w w.  ja v  a2s  . co m*/
        MongoClient mongo = getMongoNode().getMongoClient();
        setStringFieldValue(Item.version, mongo.getVersion());

        ServerAddress master = mongo.getAddress();
        if (master != null) {
            setStringFieldValue(Item.masterServer, getServerAddressString(master));
        }
        List<ServerAddress> addrs = mongo.getAllAddress();
        String html = "<html>";
        for (ServerAddress addr : addrs) {
            html += getServerAddressString(addr) + "<br/>";
        }
        html += "</html>";
        setStringFieldValue(Item.seedServers, html);

        addrs = mongo.getServerAddressList();
        html = "<html>";
        for (ServerAddress addr : addrs) {
            //                String ports = MongoUtils.makeInfoString("used", mongo.getConnector().getDBPortPool(addr).inUse(),
            //                        "available", mongo.getConnector().getDBPortPool(addr).available(),
            //                        "created", mongo.getConnector().getDBPortPool(addr).everCreated());
            String txt = getServerAddressString(addr);
            html += txt + "<br/>";
        }
        setStringFieldValue(Item.activeServers, html);

        setStringFieldValue(Item.queryOptions, MongoUtils.queryOptionsToString(mongo.getOptions()));
        ((DocField) getBoundUnit(Item.writeConcern)).setDoc(mongo.getWriteConcern().getCommand());
        ((DocField) getBoundUnit(Item.readPreference)).setDoc(mongo.getReadPreference().toDBObject());
        setStringFieldValue(Item.maxObjectSize, String.valueOf(mongo.getMaxBsonObjectSize()));
    } catch (Exception e) {
        UMongo.instance.showError(this.getClass().getSimpleName() + " update", e);
    }
}

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

License:Apache License

public void readWriteOptions(ButtonBase button) {
    MongoClient mongo = getMongoNode().getMongoClient();
    OptionDialog od = UMongo.instance.getGlobalStore().getOptionDialog();
    od.update(mongo.getOptions(), mongo.getWriteConcern(), mongo.getReadPreference());
    if (!od.show()) {
        return;//  w w w.j a  v  a 2 s  .c  om
    }
    mongo.setOptions(od.getQueryOptions());
    mongo.setWriteConcern(od.getWriteConcern());
    mongo.setReadPreference(od.getReadPreference());
    refresh();
}

From source file:org.apache.drill.exec.store.mongo.MongoGroupScan.java

License:Apache License

@SuppressWarnings("unchecked")
private Set<ServerAddress> getPreferredHosts(MongoClient client, List<String> hosts) {
    Set<ServerAddress> addressList = Sets.newHashSet();
    MongoDatabase db = client.getDatabase(scanSpec.getDbName());
    ReadPreference readPreference = client.getReadPreference();
    Document command = db.runCommand(new Document("isMaster", 1));

    final String primaryHost = command.getString("primary");
    final List<String> hostsList = (List<String>) command.get("hosts");

    switch (readPreference.getName().toUpperCase()) {
    case "PRIMARY":
    case "PRIMARYPREFERRED":
        if (primaryHost == null) {
            return null;
        }/*  ww  w  .j av  a2  s  . com*/
        addressList.add(new ServerAddress(primaryHost));
        return addressList;
    case "SECONDARY":
    case "SECONDARYPREFERRED":
        if (primaryHost == null || hostsList == null) {
            return null;
        }
        hostsList.remove(primaryHost);
        for (String host : hostsList) {
            addressList.add(new ServerAddress(host));
        }
        return addressList;
    case "NEAREST":
        if (hostsList == null) {
            return null;
        }
        for (String host : hostsList) {
            addressList.add(new ServerAddress(host));
        }
        return addressList;
    default:
        return null;
    }
}