Example usage for com.mongodb CommandResult getLong

List of usage examples for com.mongodb CommandResult getLong

Introduction

In this page you can find the example usage for com.mongodb CommandResult getLong.

Prototype

public long getLong(final String key) 

Source Link

Document

Returns the value of a field as a long .

Usage

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

License:Apache License

public void compareReplicas(ButtonBase button) {
    final String stat = getStringFieldValue(Item.crStat);
    new DbJob() {

        @Override/*from   w  w  w  .  j a  v a 2  s . c  o  m*/
        public Object doRun() {
            ReplSetNode node = getReplSetNode();
            if (!node.hasChildren())
                return null;

            ArrayList<MongoClient> svrs = new ArrayList<MongoClient>();
            for (XmlUnit unit : node.getChildren()) {
                ServerNode svr = (ServerNode) unit;
                MongoClient svrm = svr.getServerMongoClient();
                try {
                    svrm.getDatabaseNames();
                } catch (Exception e) {
                    continue;
                }
                svrs.add(svrm);
            }

            BasicDBObject res = new BasicDBObject();
            MongoClient m = getReplSetNode().getMongoClient();
            for (String dbname : m.getDatabaseNames()) {
                DB db = m.getDB(dbname);
                BasicDBObject dbres = new BasicDBObject();
                for (String colname : db.getCollectionNames()) {
                    DBCollection col = db.getCollection(colname);
                    BasicDBObject colres = new BasicDBObject();
                    BasicDBObject values = new BasicDBObject();
                    boolean same = true;
                    long ref = -1;
                    for (MongoClient svrm : svrs) {
                        DBCollection svrcol = svrm.getDB(dbname).getCollection(colname);
                        long value = 0;
                        if (stat.startsWith("Count")) {
                            value = svrcol.count();
                        } else if (stat.startsWith("Data Size")) {
                            CommandResult stats = svrcol.getStats();
                            value = stats.getLong("size");
                        }
                        values.append(svrm.getConnectPoint(), value);
                        if (ref < 0)
                            ref = value;
                        else if (ref != value)
                            same = false;
                    }
                    if (!same) {
                        colres.append("values", values);
                        dbres.append(colname, colres);
                    }
                }
                if (!dbres.isEmpty()) {
                    res.append(dbname, dbres);
                }
            }

            return res;
        }

        @Override
        public String getNS() {
            return "*";
        }

        @Override
        public String getShortName() {
            return "Compare Replicas";
        }
    }.addJob();
}

From source file:com.hangum.tadpole.mongodb.core.query.MongoDBQuery.java

License:Open Source License

/**
 * collection list//from  w w w.j a  v a2 s  .  c o  m
 * 
 * System collections(http://docs.mongodb.org/manual/reference/system-collections/)?  collection list .
 * 
 * 
 * @param userDB
 * @return
 * @throws Exception
 */
public static List<TableDAO> listCollection(UserDBDAO userDB) throws Exception {
    List<TableDAO> listReturn = new ArrayList<TableDAO>();

    DB mongoDB = MongoConnectionManager.getInstance(userDB);
    for (String col : mongoDB.getCollectionNames()) {
        if (!isSystemCollection(col)) {
            TableDAO dao = new TableDAO();
            dao.setName(col);

            CommandResult commandResult = mongoDB.getCollection(col).getStats();
            dao.setRows(commandResult.getLong("count"));
            dao.setSize(commandResult.getInt("size"));

            listReturn.add(dao);
        }
    }

    return filter(userDB, listReturn);
}

From source file:de.uni_koeln.spinfo.maalr.mongo.core.Database.java

License:Apache License

public DatabaseStatistics getStatistics() {
    DatabaseStatistics stats = new DatabaseStatistics();
    stats.setNumberOfEntries((int) entryCollection.getCount());
    Map<Verification, Integer> count = new HashMap<Verification, Integer>();
    DBCursor cursor = entryCollection.find();
    int entryCount = 0, lemmaCount = 0;
    Verification[] values = Verification.values();
    for (Verification verification : values) {
        count.put(verification, 0);//from  ww w.j  a  v a2 s  .  co  m
    }
    count.put(null, 0);
    while (cursor.hasNext()) {
        LexEntry entry = Converter.convertToLexEntry(cursor.next());
        List<LemmaVersion> history = entry.getVersionHistory();
        entryCount++;
        lemmaCount += history.size();
        for (LemmaVersion lemma : history) {
            Integer old = count.get(lemma.getVerification());
            count.put(lemma.getVerification(), old + 1);
        }
    }
    stats.setNumberOfApproved(count.get(Verification.ACCEPTED));
    stats.setNumberOfSuggestions(count.get(Verification.UNVERIFIED));
    stats.setNumberOfDeleted(count.get(Verification.REJECTED));
    stats.setNumberOfOutdated(count.get(Verification.OUTDATED));
    stats.setNumberOfUndefined(count.get(null));
    stats.setNumberOfEntries(entryCount);
    stats.setNumberOfLemmata(lemmaCount);
    /*
     * TODO: - Create a separate "System/Diagnostics"-Tab, containing a)
     * general information about - RAM - Free Disk Space - CPU - Network b)
     * DB-Statistics - general - entries - users
     * 
     * Check if this will work with sigar:
     * 
     * 
     * 
     * - Add green and red 'lamps' to each entry, each sub-statistic
     * (entries, users, general) - Add one lamp to the navigation bar, to
     * summarize the server state: should always be green.
     */

    CommandResult dbStats = entryCollection.getDB().getStats();

    NumberFormat nf = NumberFormat.getInstance();
    stats.addDBProperty("Server", dbStats.getString("serverUsed"));

    Double serverStatus = dbStats.getDouble("ok");
    if (Double.compare(1D, serverStatus) == 0) {
        stats.addDBProperty("Server Status", "ok");
    } else {
        stats.addDBProperty("Server Status", "not ok: " + serverStatus, Category.ERROR);
    }

    long collections = dbStats.getLong("collections");
    if (collections == 0) {
        stats.addDBProperty("Number of Collections", nf.format(dbStats.getLong("collections")), Category.ERROR);
    } else {
        stats.addDBProperty("Number of Collections", nf.format(dbStats.getLong("collections")));
    }
    stats.addDBProperty("Number of Indexes", nf.format(dbStats.getLong("indexes")));
    stats.addDBProperty("Average Object Size", nf.format(dbStats.getDouble("avgObjSize") / 1024) + " KB");
    stats.addDBProperty("Data Size", nf.format(dbStats.getLong("dataSize") / (1024 * 1024)) + " MB");
    stats.addDBProperty("Storage Size", nf.format(dbStats.getLong("storageSize") / (1024 * 1024)) + " MB");
    stats.addDBProperty("Index Size", nf.format(dbStats.getLong("indexSize") / (1024 * 1024)) + " MB");
    stats.addDBProperty("File Size", nf.format(dbStats.getLong("fileSize") / (1024 * 1024)) + " MB");

    BasicDBObject query;
    BasicDBList attributes;
    query = new BasicDBObject();
    attributes = new BasicDBList();
    DBObject lemmata = new BasicDBObject();
    lemmata.put(QUERY_VERSION_TIMESTAMP, new BasicDBObject("$gt", -1));
    attributes.add(lemmata);
    query.append("$and", attributes);
    // stats.setNumberOfLemmata((int) entryCollection.count(query));

    return stats;
}

From source file:org.graylog2.system.stats.mongo.MongoProbe.java

License:Open Source License

private BuildInfo createBuildInfo() {
    final BuildInfo buildInfo;
    final CommandResult buildInfoResult = adminDb.command("buildInfo");
    if (buildInfoResult.ok()) {
        buildInfo = BuildInfo.create(buildInfoResult.getString("version"),
                buildInfoResult.getString("gitVersion"), buildInfoResult.getString("sysInfo"),
                buildInfoResult.getString("loaderFlags"), buildInfoResult.getString("compilerFlags"),
                buildInfoResult.getString("allocator"), (List<Integer>) buildInfoResult.get("versionArray"),
                buildInfoResult.getString("javascriptEngine"), buildInfoResult.getInt("bits"),
                buildInfoResult.getBoolean("debug"), buildInfoResult.getLong("maxBsonObjectSize")

        );/*from  w w w.  j  ava  2 s .  co  m*/
    } else {
        buildInfo = null;
    }

    return buildInfo;
}

From source file:org.graylog2.system.stats.mongo.MongoProbe.java

License:Open Source License

public MongoStats mongoStats() {
    final List<ServerAddress> serverAddresses = mongoClient.getServerAddressList();
    final List<HostAndPort> servers = Lists.newArrayListWithCapacity(serverAddresses.size());
    for (ServerAddress serverAddress : serverAddresses) {
        servers.add(HostAndPort.fromParts(serverAddress.getHost(), serverAddress.getPort()));
    }/*  w w  w .  ja v a 2 s  . c o  m*/

    final DatabaseStats dbStats;
    final CommandResult dbStatsResult = db.command("dbStats");
    if (dbStatsResult.ok()) {
        final BasicDBObject extentFreeListMap = (BasicDBObject) dbStatsResult.get("extentFreeList");
        final DatabaseStats.ExtentFreeList extentFreeList = DatabaseStats.ExtentFreeList
                .create(extentFreeListMap.getInt("num"), extentFreeListMap.getInt("totalSize"));

        final BasicDBObject dataFileVersionMap = (BasicDBObject) dbStatsResult.get("dataFileVersion");
        final DatabaseStats.DataFileVersion dataFileVersion = DatabaseStats.DataFileVersion
                .create(dataFileVersionMap.getInt("major"), dataFileVersionMap.getInt("minor"));

        dbStats = DatabaseStats.create(dbStatsResult.getString("db"), dbStatsResult.getLong("collections"),
                dbStatsResult.getLong("objects"), dbStatsResult.getDouble("avgObjSize"),
                dbStatsResult.getLong("dataSize"), dbStatsResult.getLong("storageSize"),
                dbStatsResult.getLong("numExtents"), dbStatsResult.getLong("indexes"),
                dbStatsResult.getLong("indexSize"), dbStatsResult.getLong("fileSize"),
                dbStatsResult.getLong("nsSizeMB"), extentFreeList, dataFileVersion);
    } else {
        dbStats = null;
    }

    final ServerStatus serverStatus;
    final CommandResult serverStatusResult = adminDb.command("serverStatus");
    if (serverStatusResult.ok()) {
        final BasicDBObject connectionsMap = (BasicDBObject) serverStatusResult.get("connections");
        final ServerStatus.Connections connections = ServerStatus.Connections.create(
                connectionsMap.getInt("current"), connectionsMap.getInt("available"),
                connectionsMap.getLong("totalCreated"));

        final BasicDBObject networkMap = (BasicDBObject) serverStatusResult.get("network");
        final ServerStatus.Network network = ServerStatus.Network.create(networkMap.getInt("bytesIn"),
                networkMap.getInt("bytesOut"), networkMap.getInt("numRequests"));

        final BasicDBObject memoryMap = (BasicDBObject) serverStatusResult.get("mem");
        final ServerStatus.Memory memory = ServerStatus.Memory.create(memoryMap.getInt("bits"),
                memoryMap.getInt("resident"), memoryMap.getInt("virtual"), memoryMap.getBoolean("supported"),
                memoryMap.getInt("mapped"), memoryMap.getInt("mappedWithJournal"));

        serverStatus = ServerStatus.create(serverStatusResult.getString("host"),
                serverStatusResult.getString("version"), serverStatusResult.getString("process"),
                serverStatusResult.getLong("pid"), serverStatusResult.getInt("uptime"),
                serverStatusResult.getLong("uptimeMillis"), serverStatusResult.getInt("uptimeEstimate"),
                new DateTime(serverStatusResult.getDate("localTime")), connections, network, memory);
    } else {
        serverStatus = null;
    }

    // TODO Collection stats? http://docs.mongodb.org/manual/reference/command/collStats/

    return MongoStats.create(servers, buildInfo, hostInfo, serverStatus, dbStats);
}