Example usage for com.mongodb DBCursor size

List of usage examples for com.mongodb DBCursor size

Introduction

In this page you can find the example usage for com.mongodb DBCursor size.

Prototype

public int size() 

Source Link

Document

Counts the number of objects matching the query this does take limit/skip into consideration

Usage

From source file:org.opencb.opencga.storage.hadoop.variant.VariantHbaseDBAdaptor.java

License:Apache License

@Override
public QueryResult getVariantsHistogramByRegion(Region region, String sourceId, boolean histogramLogarithm,
        int histogramMax) {
    QueryResult<ObjectMap> queryResult = new QueryResult<>(
            String.format("%s:%d-%d", region.getChromosome(), region.getStart(), region.getEnd()));
    List<ObjectMap> data = new ArrayList<>();
    String startRow = buildRowkey(region.getChromosome(), Long.toString(region.getStart()));
    String stopRow = buildRowkey(region.getChromosome(), Long.toString(region.getEnd()));

    long startTime = System.currentTimeMillis();

    long startDbTime = System.currentTimeMillis();

    BasicDBObject query = new BasicDBObject("position",
            new BasicDBObject("$gte", startRow).append("$lte", stopRow)).append("studies.studyId", sourceId);
    DBCollection collection = db.getCollection("variants");
    DBCursor queryResults = collection.find(query);
    queryResult.setDbTime(System.currentTimeMillis() - startDbTime);

    int resultSize = queryResults.size();

    if (resultSize > histogramMax) { // Need to group results to fit maximum size of the histogram
        int sumChunkSize = resultSize / histogramMax;
        int i = 0, j = 0;
        int featuresCount = 0;
        ObjectMap item = null;// w  ww . j  a  va2s .  com

        for (DBObject result : queryResults) {
            //                featuresCount += result.getInt("features_count");
            //                if (i == 0) {
            //                    item = new ObjectMap("chromosome", result.getString("chromosome"));
            //                    item.put("chunkId", result.getInt("chunk_id"));
            //                    item.put("start", result.getInt("start"));
            //                } else if (i == sumChunkSize - 1 || j == resultSize - 1) {
            //                    if (histogramLogarithm) {
            //                        item.put("featuresCount", (featuresCount > 0) ? Math.log(featuresCount) : 0);
            //                    } else {
            //                        item.put("featuresCount", featuresCount);
            //                    }
            //                    item.put("end", result.getInt("end"));
            //                    data.add(item);
            //                    i = -1;
            //                    featuresCount = 0;
            //                }
            //                j++;
            //                i++;
        }
    } else {
        for (DBObject result : queryResults) {
            //                ObjectMap item = new ObjectMap("chromosome", result.getString("chromosome"));
            //                item.put("chunkId", result.getInt("chunk_id"));
            //                item.put("start", result.getInt("start"));
            //                if (histogramLogarithm) {
            //                    int features_count = result.getInt("features_count");
            //                    result.put("featuresCount", (features_count > 0) ? Math.log(features_count) : 0);
            //                } else {
            //                    item.put("featuresCount", result.getInt("features_count"));
            //                }
            //                item.put("end", result.getInt("end"));
            //                data.add(item);
        }
    }

    queryResult.setResult(data);
    queryResult.setNumResults(data.size());
    queryResult.setTime(System.currentTimeMillis() - startTime);

    return queryResult;
}

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

public List<TapPolicy> getAllTapPolicies() {

    DBCollection tpTable = database.getCollection(DatabaseNames.getTapPolicyTableName());
    DBCursor cursor = tpTable.find();

    List<TapPolicy> tapPolicyList = new ArrayList<TapPolicy>(cursor.size());

    try {//from w w w .  j  a va 2s.c  om
        while (cursor.hasNext()) {
            // Get the object from the database
            DBObject tapPolicyObj = cursor.next();

            // Construct a new tap policy from the DBobject and add it to list
            tapPolicyList.add(new TapPolicy(tapPolicyObj));
        }
    } catch (NotFoundException e) {
        logger.error("Object referenced by Tap Policy cannot be found", e);
    } finally {
        cursor.close();
    }

    return tapPolicyList;
}

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

public List<MatchCriteria> getAllMatchCriteria() {

    DBCollection mcTable = database.getCollection(DatabaseNames.getMatchCriteriaTableName());
    DBCursor cursor = mcTable.find();

    List<MatchCriteria> matchCriteriaList = new ArrayList<MatchCriteria>(cursor.size());

    try {//from   w w  w  . j  a  v  a  2s.c om
        while (cursor.hasNext()) {
            DBObject matchCriteriaObj = cursor.next();
            MatchCriteria matchCriteria = new MatchCriteria(matchCriteriaObj);

            matchCriteriaList.add(matchCriteria);

        }
    } finally {
        cursor.close();
    }

    return matchCriteriaList;
}

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

public List<SwitchEntry> getAllSwitchEntries() {

    DBCollection seTable = database.getCollection(DatabaseNames.getSwitchEntryTableName());
    DBCursor cursor = seTable.find();

    List<SwitchEntry> switchEntryList = new ArrayList<SwitchEntry>(cursor.size());

    try {//from ww w. j  a  v  a  2  s. co m
        while (cursor.hasNext()) {
            DBObject switchEntryObj = cursor.next();

            SwitchEntry switchEntry = new SwitchEntry(switchEntryObj, this);

            // Add the new SwitchEntry to the list
            switchEntryList.add(switchEntry);
        }
    } finally {
        cursor.close();
    }

    return switchEntryList;
}

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

public List<NextHopSwitch> getAllNextHopSwitches() {
    DBCollection nhsTable = database.getCollection(DatabaseNames.getNextHopSwitchTableName());
    DBCursor cursor = nhsTable.find();

    List<NextHopSwitch> nextHopSwitchList = new ArrayList<NextHopSwitch>(cursor.size());

    try {//from   w w w .j  a va 2s  .  c  o  m
        while (cursor.hasNext()) {
            DBObject nhsObj = cursor.next();

            NextHopSwitch nhs = new NextHopSwitch(nhsObj);

            // Add the new SwitchEntry to the list
            nextHopSwitchList.add(nhs);
        }
    } finally {
        cursor.close();
    }

    return nextHopSwitchList;
}

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

public List<PortChain> getAllPortChains() {
    DBCollection pcTable = database.getCollection(DatabaseNames.getPortChainTableName());
    DBCursor cursor = pcTable.find();

    List<PortChain> portChainList = new ArrayList<PortChain>(cursor.size());

    try {/*w w w.j a v a2  s  .co m*/
        while (cursor.hasNext()) {
            DBObject portChainObj = cursor.next();

            // Construct a new port chain from the DB object
            PortChain portChain = new PortChain(portChainObj);

            // Add the new port chain to the list
            portChainList.add(portChain);
        }
    } finally {
        cursor.close();
    }

    return portChainList;
}

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

public List<CaptureDev> getAllCaptureDevices() {
    DBCollection pcTable = database.getCollection(DatabaseNames.getCaptureDevTableName());
    DBCursor cursor = pcTable.find();

    List<CaptureDev> captureDevList = new ArrayList<CaptureDev>(cursor.size());

    while (cursor.hasNext()) {
        DBObject captureDevObj = cursor.next();
        // Construct a new CaptureDev from the document
        CaptureDev captureDev = new CaptureDev(captureDevObj);

        // Add it to the list
        captureDevList.add(captureDev);/*  w  ww.j a  v  a 2s. co  m*/
    }

    return captureDevList;
}

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

private List<LoggedMessage> processLogMessageCursor(DBCursor cursor) {
    List<LoggedMessage> loggedMessageList = new ArrayList<LoggedMessage>(cursor.size());

    while (cursor.hasNext()) {
        DBObject logObj = cursor.next();
        LoggedMessage logMsg = new LoggedMessage();

        ObjectId oid = (ObjectId) logObj.get("_id");
        logMsg.setObjectId(oid.toString());
        logMsg.setTimestamp((Date) logObj.get("timestamp"));
        logMsg.setLogLevel(LoggingLevelEnum.getEnum((String) logObj.get("level")));
        logMsg.setThreadId((String) logObj.get("thread"));
        logMsg.setMessage((String) logObj.get("message"));
        logMsg.setFileName((String) logObj.get("fileName"));
        logMsg.setMethodName((String) logObj.get("method"));
        logMsg.setLineNumber(Integer.parseInt((String) logObj.get("lineNumber")));

        // get these fields from the "host" sub-document
        DBObject hostObj = (DBObject) logObj.get("host");
        logMsg.setHostname((String) hostObj.get("name"));
        logMsg.setHostIP((String) hostObj.get("ip"));

        // Add the message to the list
        loggedMessageList.add(logMsg);/*from   w w w  . j  a v  a2 s.  com*/
    }
    return loggedMessageList;
}

From source file:org.sipfoundry.sipxconfig.admin.commserver.RegistrationContextImpl.java

License:Contributor Agreement License

/**
 * @see org.sipfoundry.sipxconfig.admin.commserver.RegistrationContext#getRegistrations()
 *//*from w w w.  jav a  2s . c  om*/
public List<RegistrationItem> getRegistrations() {
    try {
        DB datasetDb = m_nodedb.getDb();
        DBCollection registrarCollection = datasetDb.getCollection(DB_COLLECTION_NAME);
        DBCursor cursor = registrarCollection.find();
        List<RegistrationItem> items = new ArrayList<RegistrationItem>(cursor.size());
        while (cursor.hasNext()) {
            DBObject registration = cursor.next();
            if ((Boolean) registration.get("expired")) {
                continue;
            }
            RegistrationItem item = new RegistrationItem();
            item.setContact((String) registration.get("contact"));
            item.setPrimary((String) registration.get("primary"));
            item.setExpires((Integer) registration.get("expirationTime"));
            item.setUri((String) registration.get("uri"));
            item.setInstrument((String) registration.get("instrument"));
            items.add(item);
        }
        return items;
    } catch (Exception e) {
        // we are handling this separately - server returns FileNotFound even if everything is
        // OK but we have no registrations present
        LOG.warn("Cannot retrieve registrations.", e);
        return Collections.emptyList();
    }
}

From source file:org.sipfoundry.sipxconfig.registrar.RegistrationContextImpl.java

License:Contributor Agreement License

private List<RegistrationItem> getItems(DBCursor cursor) {
    List<RegistrationItem> items = new ArrayList<RegistrationItem>(cursor.size());
    while (cursor.hasNext()) {
        DBObject registration = cursor.next();
        RegistrationItem item = new RegistrationItem();
        item.setContact((String) registration.get("contact"));
        item.setPrimary((String) registration.get("primary"));
        item.setExpires((Integer) registration.get("expirationTime"));
        item.setUri((String) registration.get(URI));
        item.setInstrument((String) registration.get("instrument"));
        item.setRegCallId((String) registration.get("callId"));
        item.setIdentity((String) registration.get(IDENTITY));
        items.add(item);//from   w ww.  ja v a2 s.co  m
    }
    return items;
}