Example usage for com.mongodb BasicDBObject getLong

List of usage examples for com.mongodb BasicDBObject getLong

Introduction

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

Prototype

public long getLong(final String key) 

Source Link

Document

Returns the value of a field as a long .

Usage

From source file:org.apache.chemistry.opencmis.mongodb.MongodbUtils.java

License:Apache License

public BasicDBList getNodeDescendents(BasicDBObject node, DBCollection collection) {
    BasicDBList descendantsArray = new BasicDBList();
    DBCursor descendants = collection/*w w w .j a v  a2  s . co  m*/
            .find(new BasicDBObject().append("left", new BasicDBObject().append("$gt", node.getLong("left")))
                    .append("right", new BasicDBObject().append("$lt", node.getLong("right"))))
            .sort(new BasicDBObject().append("left", 1));

    while (descendants.hasNext()) {
        descendantsArray.add(descendants.next());

    }
    return descendantsArray;
}

From source file:org.apache.chemistry.opencmis.mongodb.MongodbUtils.java

License:Apache License

private BasicDBList getNodeChildren(BasicDBObject node, DBCollection collection) {
    BasicDBList childrenArray = new BasicDBList();
    DBCursor children = collection//from  w w  w  .j a v a2 s  .  com
            .find(new BasicDBObject().append("left", new BasicDBObject().append("$gt", node.getLong("left")))
                    .append("right", new BasicDBObject().append("$lt", node.getLong("right")))
                    .append("level", node.getLong("level") + 1))
            .sort(new BasicDBObject().append("left", 1));

    while (children.hasNext()) {
        childrenArray.add(children.next());

    }
    return childrenArray;
}

From source file:org.apache.gora.mongodb.utils.BSONDecorator.java

License:Apache License

/**
 * Access field as a long./*  w w w .j  av  a 2  s . c  o m*/
 *
 * @param fieldName
 *          fully qualified name of the field to be accessed
 * @return value of the field as a double
 */
public Long getLong(String fieldName) {
    BasicDBObject parent = getFieldParent(fieldName);
    return parent.getLong(getLeafName(fieldName));
}

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

License:Open Source License

private HostInfo createHostInfo() {
    final HostInfo hostInfo;
    final CommandResult hostInfoResult = adminDb.command("hostInfo");
    if (hostInfoResult.ok()) {
        final BasicDBObject systemMap = (BasicDBObject) hostInfoResult.get("system");
        final HostInfo.System system = HostInfo.System.create(new DateTime(systemMap.getDate("currentTime")),
                systemMap.getString("hostname"), systemMap.getInt("cpuAddrSize"),
                systemMap.getLong("memSizeMB"), systemMap.getInt("numCores"), systemMap.getString("cpuArch"),
                systemMap.getBoolean("numaEnabled"));
        final BasicDBObject osMap = (BasicDBObject) hostInfoResult.get("os");
        final HostInfo.Os os = HostInfo.Os.create(osMap.getString("type"), osMap.getString("name"),
                osMap.getString("version"));

        final BasicDBObject extraMap = (BasicDBObject) hostInfoResult.get("extra");
        final HostInfo.Extra extra = HostInfo.Extra.create(extraMap.getString("versionString"),
                extraMap.getString("libcVersion"), extraMap.getString("kernelVersion"),
                extraMap.getString("cpuFrequencyMHz"), extraMap.getString("cpuFeatures"),
                extraMap.getString("scheduler"), extraMap.getLong("pageSize", -1l),
                extraMap.getLong("numPages", -1l), extraMap.getLong("maxOpenFiles", -1l));

        hostInfo = HostInfo.create(system, os, extra);
    } else {/* w w  w .  j a  v  a 2s .  c om*/
        hostInfo = null;
    }

    return hostInfo;
}

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()));
    }/*  ww  w  .  jav  a2  s . co  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);
}

From source file:org.jivesoftware.openfire.roster.DefaultRosterItemProvider.java

License:Open Source License

public Iterator<RosterItem> getItems(String username) {
    LinkedList<RosterItem> itemList = new LinkedList<RosterItem>();
    try {//from ww w  .  j  a  v  a2 s  . c  o  m
        init();

        DBCollection coll = db.getCollection("gUser");
        BasicDBObject doc = new BasicDBObject("friends", 1);
        BasicDBObject q = new BasicDBObject("himId", username);
        DBCursor res = coll.find(q, doc);
        Iterator<DBObject> iter = res.iterator();
        while (iter.hasNext()) {
            BasicDBList o = (BasicDBList) iter.next().get("friends");
            for (int i = 0; i < o.size(); i++) {
                BasicDBObject dbo = (BasicDBObject) o.get(i);
                // information

                // SELECT jid, rosterID, sub, ask, recv, nick,version FROM
                // ofRoster WHERE username=?
                RosterItem item = new RosterItem(i + 1, new JID(dbo.getString("jid")),
                        RosterItem.SubType.getTypeFromInt(dbo.getInt("sub")),
                        RosterItem.AskType.getTypeFromInt(dbo.getInt("ask")),
                        RosterItem.RecvType.getTypeFromInt(dbo.getInt("recv")), dbo.getString("name"), null);
                item.setCurrVersion(dbo.getLong("ver"));
                // Add the loaded RosterItem (ie. user contact) to the
                // result
                itemList.add(item);

            }
            System.out.println("sdsd");
        }

    } catch (Exception e) {
        Log.warn("Error trying to get rows in ofRoster", e);

    }

    return itemList.iterator();
}

From source file:org.mongoste.core.impl.mongodb.MongoStatsEngine.java

License:Open Source License

private List<StatAction> getTargetStats(DBObject query) throws StatsEngineException {
    List<StatAction> result = new ArrayList<StatAction>();
    DBCursor dbc = null;/*  w  ww  . ja  v a 2 s .  c  o  m*/
    try {
        log.debug("Querying targets");
        DBCollection targets = getTargetCollection();
        long t = System.currentTimeMillis();
        DBObject fields = MongoUtil.createDoc(EVENT_ACTION, 1, FIELD_COUNT, 1, EVENT_DATE, 1);
        dbc = targets.find(query, fields);
        t = System.currentTimeMillis() - t;
        if (t > 1000) {
            log.warn("getTargetStats query: {}\n took {}s", debugTrim(query), t / 1000.0);
        }
        BasicDBObject resultDoc;
        Map<String, StatAction> actions = new HashMap<String, StatAction>();
        Map<String, Map<DateTime, StatCounter>> actionsDate = new HashMap<String, Map<DateTime, StatCounter>>();
        Map<DateTime, StatCounter> dateCount;
        StatAction action;
        StatCounter dateCounter;
        String actionName;
        Long count;
        MutableDateTime dateTime = DateUtil.getDateTimeUTC(true).toMutableDateTime();
        DateTime date;
        Date eventYearMonthTargetDate;
        int processed = 0;
        t = System.currentTimeMillis();
        while (dbc.hasNext()) {
            resultDoc = (BasicDBObject) dbc.next();
            actionName = resultDoc.getString(EVENT_ACTION);
            count = resultDoc.getLong(FIELD_COUNT);
            eventYearMonthTargetDate = (Date) resultDoc.get(EVENT_DATE);
            dateTime.setDate(eventYearMonthTargetDate.getTime());
            date = dateTime.toDateTime();
            action = actions.get(actionName);
            if (action == null) {
                actions.put(actionName, action = new StatAction(actionName, 0));
            }
            action.add(count);
            dateCount = actionsDate.get(actionName);
            if (dateCount == null) {
                dateCount = new TreeMap<DateTime, StatCounter>();
                actionsDate.put(actionName, dateCount);
            }
            dateCounter = dateCount.get(date);
            if (dateCounter == null) {
                dateCount.put(date, dateCounter = new StatCounter(actionName, 0, date.toDate()));
            }
            dateCounter.add(count);
            processed++;
        }
        //Build result list
        for (Entry<String, StatAction> entry : actions.entrySet()) {
            action = entry.getValue();
            dateCount = actionsDate.get(action.getName());
            List<StatCounter> targetList = action.getTargets();
            for (Entry<DateTime, StatCounter> entryDate : dateCount.entrySet()) {
                StatCounter counter = entryDate.getValue();
                targetList.add(counter);
            }
            result.add(action);
        }
        t = System.currentTimeMillis() - t;
        //TODO add warning level to X ms:
        if (t > 1000) {
            log.warn("getTargetStats query fetch: {}\n took {}s", debugTrim(query), t / 1000.0);
        } else {
            log.info("getTargetStats processed {} results in {}ms", processed, t);
        }
    } catch (Exception ex) {
        log.error("getTargetStats", ex);
        if (ex instanceof StatsEngineException) {
            throw (StatsEngineException) ex;
        }
        throw new StatsEngineException("getTargetStats", ex);
    } finally {
        MongoUtil.close(dbc);
    }
    return result;
}

From source file:org.sdsai.dsds.mongo.MongoUtils.java

License:Open Source License

public static Object getPrimitive(final BasicDBObject dbo, final String fieldName, final Class<?> returnType)
        throws Exception {
    final boolean contained = dbo.containsField(fieldName);

    if (Boolean.TYPE.equals(returnType))
        return contained ? dbo.getBoolean(fieldName) : Boolean.FALSE;

    if (Short.TYPE.equals(returnType) || Byte.TYPE.equals(returnType) || Integer.TYPE.equals(returnType))
        return (Integer) (contained ? dbo.getInt(fieldName) : 0);

    if (Character.TYPE.equals(returnType))
        return (Character) ((dbo.get(fieldName) + "").charAt(0));

    if (Long.TYPE.equals(returnType))
        return (Long) (contained ? dbo.getLong(fieldName) : 0L);

    if (Float.TYPE.equals(returnType))
        return (Float) (contained ? Float.valueOf(dbo.get(fieldName) + "") : 0F);

    if (Double.TYPE.equals(returnType))
        return (Double) (contained ? dbo.getDouble(fieldName) : 0D);

    return null;//from  w ww .j a v a 2 s .c o m
}

From source file:pl.nask.hsn2.os.MongoConnector.java

License:Open Source License

public long getNextObjId() {
    DBCollection counters = db.getCollection("counters");
    BasicDBObject result = (BasicDBObject) counters.findAndModify(counterQuery, new BasicDBObject(),
            new BasicDBObject(), false, counterUpdate, true, true);
    return result.getLong("counter");
}

From source file:pl.nask.hsn2.os.ObjectStore.java

License:Open Source License

private Builder getBuilder(String key, BasicDBObject objectFound) {
    Object value = objectFound.get(key);
    Builder builder = Attribute.newBuilder().setName(key);

    if (value instanceof Integer || key.equalsIgnoreCase("depth")) {
        builder.setType(Type.INT).setDataInt(objectFound.getInt(key));
    } else if (value instanceof Long) {
        builder.setType(Type.OBJECT).setDataObject(objectFound.getLong(key));
    } else if (value instanceof BasicDBObject) {

        BasicDBObject mongoObj = (BasicDBObject) value;
        if (mongoObj.get("type").equals("TIME")) {
            builder.setType(Type.TIME).setDataTime(mongoObj.getLong("time"));
        } else if (mongoObj.get("type").equals("BYTES")) {
            builder.setType(Type.BYTES).setDataBytes(Reference.newBuilder().setKey(mongoObj.getLong("key"))
                    .setStore(mongoObj.getInt("store")).build());
        }//  w  w w  .j  a v a  2  s. co  m
    } else if (value instanceof Boolean) {
        builder.setType(Type.BOOL).setDataBool(objectFound.getBoolean(key));
    } else {
        builder.setType(Type.STRING).setDataString(objectFound.getString(key));
    }
    return builder;
}