Example usage for com.mongodb DBCursor count

List of usage examples for com.mongodb DBCursor count

Introduction

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

Prototype

public int count() 

Source Link

Document

Counts the number of objects matching the query.

Usage

From source file:org.mephi.griffin.actorcloud.storage.StorageActor.java

License:Apache License

@Override
public void preStart() {
    logger.entering("StorageActor", "preStart");
    String errors = "";
    Config config = getContext().system().settings().config();
    try {/*w  w w  .j ava  2 s  .  c om*/
        String host;
        int port;
        String login = null;
        String pass = null;
        try {
            host = config.getString("actorcloud.storage.host");
        } catch (Missing ex) {
            host = "localhost";
        }
        try {
            port = config.getInt("actorcloud.storage.port");
        } catch (Missing ex) {
            port = 27017;
        }
        try {
            login = config.getString("actorcloud.storage.login");
            pass = config.getString("actorcloud.storage.pass");
        } catch (Missing ex) {
            if (login != null)
                throw ex;
            login = "";
            pass = "";
        }
        try {
            dbName = config.getString("actorcloud.storage.db");
        } catch (Missing ex) {
            dbName = "actorcloud";
        }
        if (!login.equals(""))
            client = new MongoClient(new ServerAddress(host, port), Arrays
                    .asList(MongoCredential.createPlainCredential(login, "actorcloud", pass.toCharArray())));
        else
            client = new MongoClient(new ServerAddress(host, port));
        logger.logp(Level.FINER, "StorageActor", "preStart", "Connected to " + host + ":" + port);
        if (client != null) {
            db = client.getDB(dbName);
            DBCursor cursor = db.getCollection("clients").find();
            boolean adminFound = false;
            if (cursor.count() != 0) {
                while (cursor.hasNext()) {
                    DBObject doc = cursor.next();
                    if (doc.get("name") != null && doc.get("name").equals("admin")) {
                        adminFound = true;
                        break;
                    }
                }
            }
            if (!adminFound) {
                MessageDigest md = MessageDigest.getInstance("SHA-512");
                BasicDBObject doc = new BasicDBObject();
                byte[] hash = md.digest(("admin").getBytes());
                doc.append("name", "admin");
                doc.append("hash", hash);
                doc.append("maxSessions", 1);
                doc.append("maxChilds", 0);
                doc.append("messageHandlers", new ArrayList<>());
                doc.append("childHandlers", new ArrayList<>());
                db.getCollection("clients").insert(doc);
            }
            InitSuccess msg = new InitSuccess(InitSuccess.STORAGE, null, null, 0);
            logger.logp(Level.FINER, "StorageActor", "preStart", "InitSuccess -> Manager: " + msg);
            nodeManager.tell(msg, getSelf());
            logger.logp(Level.INFO, "StorageActor", "preStart", "Storage started");
        } else {
            InitFail msg = new InitFail(InitFail.STORAGE, null, null, 0, errors);
            logger.logp(Level.FINER, "StorageActor", "preStart", "InitFail -> Manager: " + msg);
            nodeManager.tell(msg, getSelf());
            logger.logp(Level.WARNING, "StorageActor", "preStart", "Failed to connect to DB:\n" + errors);
            getContext().stop(getSelf());
        }
    } catch (MongoException | UnknownHostException | NoSuchAlgorithmException e) {
        logger.throwing("StorageActor", "preStart", e);
        errors += e.getMessage() + "\n";
        InitFail msg = new InitFail(InitFail.STORAGE, null, null, 0, errors);
        logger.logp(Level.FINER, "StorageActor", "preStart", "InitFail -> Manager: " + msg);
        nodeManager.tell(msg, getSelf());
        logger.logp(Level.WARNING, "StorageActor", "preStart", "Failed to connect to DB:\n" + errors);
        getContext().stop(getSelf());
    }
    logger.exiting("StorageActor", "preStart");
}

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

License:Apache License

public List<TapPolicy> getTapPoliciesForSwitch(SwitchEntry switchEntry) {

    DBCollection table = database.getCollection(DatabaseNames.getTapPolicyTableName());

    List<TapPolicy> tapPoliciesForSwitch = new ArrayList<TapPolicy>();

    logger.info("Searching for Tap Policies for switch with ID " + switchEntry.getObjectId());
    BasicDBObject searchQuery = new BasicDBObject();
    ObjectId switchId = new ObjectId(switchEntry.getObjectId());
    searchQuery.put("switchAndPort.switchEntryId", switchId);
    DBCursor cursor = table.find(searchQuery);

    logger.info("Query result set contains " + cursor.count() + "Tap Policies");

    try {//w w w . j  a  va 2s. c om
        while (cursor.hasNext()) {
            TapPolicy tapPolicy;
            try {
                tapPolicy = new TapPolicy(cursor.next());
                tapPoliciesForSwitch.add(tapPolicy);
                logger.info("Tap Policy  " + tapPolicy);
            } catch (NotFoundException e) {
                logger.warn("Cannot find tap policy");
            }
        }
    } finally {
        cursor.close();
    }

    return tapPoliciesForSwitch;
}

From source file:org.springframework.xd.test.fixtures.HdfsMongoDbJob.java

License:Apache License

/**
 * Returns a single object from the collection.
 * /*w ww .j  a va 2  s  .co  m*/
 * @param collectionName the name of the collection to query
 * @return A Map with the content of the result
 * @throws IllegalStateException if the number of objects in the collection is not one
 */
@SuppressWarnings("unchecked")
public Map<String, String> getSingleObject(String collectionName) {
    DBCursor cursor = mongoTemplate.getCollection(collectionName).find();
    if (cursor.count() != 1) {
        throw new IllegalStateException("Expected only one result but received " + cursor.count() + " entries");
    }
    DBObject dbObject = mongoTemplate.getCollection(collectionName).findOne();
    Map<String, String> result = null;
    if (dbObject != null) {
        result = dbObject.toMap();
    }
    return result;
}

From source file:org.wikidata.couchbase.WikidataIterator.java

License:Open Source License

/**
 * /*  w ww .j  a  v a  2  s . c o m*/
 */
private void logDbStatus() {
    BasicDBObject doc = new BasicDBObject("property", property);
    DBCursor cursor = persistService.find(doc);
    LOG.info("Number of " + property + " properties in DB: " + cursor.count());
}

From source file:Rpackage.MongoFixPostCode.java

public static void main(String[] args) {

    YelpAPI yelpApi = new YelpAPI();
    JSONParser parser = new JSONParser();
    try {/*  ww  w .ja v a  2  s.com*/

        // Connect to mongodb
        MongoClient mongo = new MongoClient("10.33.2.142", 27017);

        // get database
        // if database doesn't exists, mongodb will create it for you
        DB db = mongo.getDB("test");

        // get collection
        // if collection doesn't exists, mongodb will create it for you
        DBCollection collection = db.getCollection("twitter_Stream");

        DBCursor cursor;
        BasicDBObject query;
        //------------------------------------
        // ( 1 ) collection.find() --> get all document

        DBObject clause1 = new BasicDBObject("Latitude", new BasicDBObject("$exists", true));
        DBObject clause2 = new BasicDBObject("tweet_Country_Code", new BasicDBObject("$exists", true));
        DBObject clause3 = new BasicDBObject("R_Post_Code", new BasicDBObject("$exists", false));
        /*{
          "$and":[
            {
              "tweet_Country_Code":{
                "$exists":true
              }
            },
            {
              "Latitude":{
                "$exists":false
              }
            },
            {
              "tweet_Country_Code":"AU"
            }
          ]
        }                
        */
        BasicDBList or = new BasicDBList();
        or.add(clause1);
        or.add(clause2);
        or.add(clause3);
        query = new BasicDBObject("$and", or);

        //{  "$and":[    {      "Latitude":{        "$exists":true      }    },    {      "R_Post_Code":{        "$exists":false      }    }  ]}            
        cursor = collection.find(query).addOption(Bytes.QUERYOPTION_NOTIMEOUT) //
                .addOption(Bytes.QUERYOPTION_AWAITDATA);

        System.out.println("( 1 ) .get the user id within  latitide");
        System.out.println("results --> " + cursor.count());
        FoursquareAPI_backup qui4squreApi = new FoursquareAPI_backup();
        try {
            while (cursor.hasNext()) {
                DBObject data = cursor.next();
                String v_user_Name = (String) data.get("user_name");
                Long v_twitte_id = (Long) data.get("tweet_ID");
                String v_twitte_text = (String) data.get("tweet_text");
                Long v_user_Id = (Long) data.get("user_Id");
                Double v_Latitude = (Double) data.get("Latitude");
                Double v_Longtitude = (Double) data.get("Longitude");
                String v_tweet_Place_Name = (String) data.get("tweet_Place_Name");
                String v_tweet_Country_Code = (String) data.get("tweet_Country_Code");

                if (v_user_Id == null) {
                    /*                        System.out.print("update:" +v_user_Name+"/status/"+ v_twitte_id);
                     try{
                     Status status = twitter.showStatus(v_twitte_id);
                     BasicDBObject jo = GetMongoRecord(status);
                     System.out.println( "-->" + status.getId() + " : " + jo.getString("Re_user_screenName") + ":" + jo.getString("tweet_text"));
                     collection.update(new BasicDBObject("tweet_ID", v_twitte_id), jo); // set the document in the DB to the new document for Jo                     
                     }catch (TwitterException ex){
                     if (ex.getStatusCode()==144) continue;
                     }
                     */
                    continue;
                }
                JSONObject businesses = yelpApi.search4Yelp("city", v_Latitude, v_Longtitude);//-27.497835,153.017472);                      
                boolean searchAgain = false;

                if (businesses == null)
                    searchAgain = true;
                else if (businesses.size() < 1)
                    searchAgain = true;

                if (searchAgain) {
                    System.out.println("La:" + v_Latitude + "\tLo:" + v_Longtitude);
                    businesses = qui4squreApi.search4Square("city", v_Latitude, v_Longtitude);
                    searchAgain = false;
                }
                if (businesses == null)
                    searchAgain = true;
                else if (businesses.size() < 1)
                    searchAgain = true;

                if (searchAgain) {
                    businesses = qui4squreApi.searchGoogleMap("city", v_Latitude, v_Longtitude);
                    if (businesses == null) {
                        System.out.println("\t" + v_tweet_Country_Code + "\t:" + v_tweet_Place_Name);
                        continue;
                    } else if (businesses.size() < 1) {
                        System.out.println("\t" + v_tweet_Country_Code + "\t:" + v_tweet_Place_Name);
                        continue;
                    }
                }

                String country_code = (String) businesses.get("country_code");
                String city_code = (String) businesses.get("city_name");
                String state_code = (String) businesses.get("state_code");
                String post_code = (String) businesses.get("post_code");

                BasicDBObject setNewFieldQuery = new BasicDBObject().append("$set",
                        new BasicDBObject().append("R_Country_Code", country_code)
                                .append("R_State_Code", state_code).append("R_City", city_code)
                                .append("R_Post_Code", post_code));

                collection.update(new BasicDBObject().append("tweet_ID", v_twitte_id), setNewFieldQuery); // set the document in the DB to the new document for Jo                                             

                setNewFieldQuery.clear();
                setNewFieldQuery = null;
            }
        } finally {
            cursor.close();
        }

        System.out.println("---------------------------------");

    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (MongoException e) {
        e.printStackTrace();
    }

}

From source file:se.inera.axel.shs.broker.messagestore.internal.MongoMessageStoreServiceIT.java

License:Open Source License

@Test(groups = "largeTests", enabled = true)
public void deletingNonExistingFileShouldBeANoOp() {
    messageStore.delete(make(a(ShsMessageEntryMaker.ShsMessageEntry)));

    mongoOperations.execute(new DbCallback<Object>() {
        @Override//from ww w  .  j  a  va  2 s . co m
        public Object doInDB(DB db) throws MongoException, DataAccessException {
            GridFS gridFs = new GridFS(db);
            DBCursor dbCursor = gridFs.getFileList();

            assertThat(dbCursor.count(), is(0));

            return null;
        }
    });
}

From source file:tango.gui.DataManager.java

License:Open Source License

private void extractData() {
    HashMap<MultiKey, TreeSet<String>> newC2CKeys = new HashMap<MultiKey, TreeSet<String>>();
    TreeSet<String> NucKeysToAdd = new TreeSet<String>();
    DBCursor cur = mc.getXPNuclei(xp.getName());
    cur.sort(new BasicDBObject("field_id", 1).append("idx", 1));
    int nbNuc = cur.count();
    IJ.log("extract data nb nuc:" + nbNuc);
    objectMes = new HashMap<Integer, TreeMap<MultiKey3D, String>>(ojectKeys.size());
    nbObjects = new TreeMap<MultiKey2D, int[]>();
    nucTags = new TreeMap<MultiKey2D, Integer>();
    nucIds = new TreeMap<MultiKey2D, String>();
    for (int i : ojectKeys.keySet()) {
        if (i < 0) {
            continue;
        }//w w  w.j  a v a2 s . c  om
        objectMes.put(i, new TreeMap<MultiKey3D, String>());
    }
    if (!ojectKeys.containsKey(0)) {
        objectMes.put(0, new TreeMap<MultiKey3D, String>());
        ojectKeys.put(0, new TreeSet<String>());
    }
    o2oMes = new HashMap<MultiKey, TreeMap<MultiKey4D, String>>();
    for (MultiKey dk : c2cKeys.keySet()) {
        o2oMes.put(dk, new TreeMap<MultiKey4D, String>());
        newC2CKeys.put(dk, new TreeSet<String>());
    }
    while (cur.hasNext()) {
        BasicDBObject nuc = (BasicDBObject) cur.next();
        if (nuc.getInt("tag", 0) < 0) {
            continue; // exclude negative tags
        }
        ObjectId nucId = (ObjectId) nuc.get("_id");
        int nucIdx = nuc.getInt("idx");
        String fieldName = mc.getField((ObjectId) nuc.get("field_id")).getString("name");
        int[] nbPart = new int[channelNames.length];
        //mesure objects
        for (int i = 0; i < channelNames.length; i++) {
            TreeMap<MultiKey3D, String> omes = objectMes.get(i);
            TreeSet<String> keys = ojectKeys.get(i);
            DBCursor cursor = mc.getObjectsCursor(nucId, i);
            cursor.sort(new BasicDBObject("idx", 1));
            nbPart[i] = cursor.count();
            if (keys != null && !keys.isEmpty()) {
                while (cursor.hasNext()) {
                    BasicDBObject o = (BasicDBObject) cursor.next();
                    //IJ.log("o="+o);
                    //IJ.log("omes="+omes);
                    //IJ.log("f="+fieldName+" "+nucIdx+" "+o.getInt("idx")+" "+keys);
                    for (String k : keys) {
                        //IJ.log("k="+k+" "+o.getString(k));
                        if (o.getString(k) != null) {
                            omes.put(new MultiKey3D(fieldName, nucIdx, o.getInt("idx"), k),
                                    o.get(k).toString());
                        }
                    }
                }
            }
            cursor.close();
        }
        String s = "";
        for (int i : nbPart) {
            s += i + ";";
        }
        //IJ.log("nb objects:" + s);
        MultiKey2D k2D = new MultiKey2D(fieldName, nucIdx, "nbParts");
        nbObjects.put(k2D, nbPart);
        nucTags.put(k2D, nuc.getInt("tag", 0));
        nucIds.put(k2D, nuc.getString("_id"));
        //C2C
        TreeMap<MultiKey3D, String> nucMes = objectMes.get(0);
        for (MultiKey dk : c2cKeys.keySet()) {
            if (dk.getKey(0) < 0) {
                continue;
            }
            int size = (dk.getKey(0) != dk.getKey(1)) ? nbPart[dk.getKey(0)] * nbPart[dk.getKey(1)]
                    : nbPart[dk.getKey(0)] * (nbPart[dk.getKey(0)] - 1) / 2;
            BasicDBObject mes = mc.getMeasurementStructure(nucId, dk.getKeys(), true);
            //IJ.log("get mes:" + dk + " mes");
            TreeMap<MultiKey4D, String> o2oMesDk = o2oMes.get(dk);
            TreeSet<String> keys = c2cKeys.get(dk);
            TreeSet<String> newKeys = newC2CKeys.get(dk);

            for (String k : keys) {
                Object o = mes.get(k);
                if (o instanceof BasicDBList) {
                    BasicDBList list = ((BasicDBList) o);
                    if (list.size() == size) {
                        int count = 0;
                        if (dk.getKey(0) != dk.getKey(1)) {
                            for (int p1 = 1; p1 <= nbPart[dk.getKey(0)]; p1++) {
                                for (int p2 = 1; p2 <= nbPart[dk.getKey(1)]; p2++) {
                                    o2oMesDk.put(new MultiKey4D(fieldName, nucIdx, p1, p2, k),
                                            list.get(count).toString());
                                    count++;
                                }
                            }
                        } else {
                            for (int p1 = 1; p1 < nbPart[dk.getKey(0)]; p1++) {
                                for (int p2 = p1 + 1; p2 <= nbPart[dk.getKey(1)]; p2++) {
                                    o2oMesDk.put(new MultiKey4D(fieldName, nucIdx, p1, p2, k),
                                            list.get(count).toString());
                                    count++;

                                }
                            }
                        }
                        newKeys.add(k);
                    }
                } else if (o instanceof Number || o instanceof String) {
                    String newKey = channelNames[dk.getKey(0)] + "." + channelNames[dk.getKey(1)] + "." + k;
                    nucMes.put(new MultiKey3D(fieldName, nucIdx, 1, newKey), o.toString());
                    NucKeysToAdd.add(newKey);
                }
            }
        }
    }
    cur.close();
    this.ojectKeys.get(0).addAll(NucKeysToAdd);
    this.c2cKeys = newC2CKeys;
}

From source file:tango.gui.DataManager.java

License:Open Source License

private void writeC2CMisc(File output, MultiKey dk, String key, String delimiter) {
    try {/*from ww w.  j av  a  2s. c o m*/
        DBCursor cur = mc.getXPNuclei(xp.getName());
        cur.sort(new BasicDBObject("field_id", 1).append("idx", 1));
        int nbNuc = cur.count();
        FileWriter fstream = new FileWriter(output);
        BufferedWriter out = new BufferedWriter(fstream);
        String headers = "nucId" + delimiter + "field" + delimiter + "nuc.idx" + delimiter + "idx" + delimiter
                + key;
        out.write(headers);
        out.newLine();
        while (cur.hasNext()) {
            BasicDBObject nuc = (BasicDBObject) cur.next();
            ObjectId nucId = (ObjectId) nuc.get("_id");
            int nucIdx = nuc.getInt("idx");
            String fieldName = mc.getField((ObjectId) nuc.get("field_id")).getString("name");
            String line = nucId + delimiter + fieldName + delimiter + nucIdx + delimiter;
            //C2C
            BasicDBObject mes = mc.getMeasurementStructure(nucId, dk.getKeys(), true);
            Object o = mes.get(key);
            if (o instanceof BasicDBList) {
                BasicDBList list = ((BasicDBList) o);
                for (int i = 0; i < list.size(); i++) {
                    out.write(line + i + delimiter + list.get(i));
                    out.newLine();
                }
            } else if (o instanceof Number || o instanceof String) {
                out.write(line + "1" + delimiter + o.toString());
                out.newLine();
            }
        }
        out.close();
        cur.close();
    } catch (Exception e) {
        exceptionPrinter.print(e, "extract key: " + key, Core.GUIMode);
    }
}

From source file:tango.mongo.MongoConnector.java

License:Open Source License

public synchronized void removeExperiment(String name) {
    BasicDBObject xp = this.getExperiment(name);
    BasicDBObject queryXP = new BasicDBObject("experiment_id", (ObjectId) xp.get("_id"));
    DBCursor cursor = field.find(queryXP);
    if (Core.GUIMode) {
        ij.IJ.log("Deleting XP:" + name + "...");
    }//from   w  w w .ja  v a  2  s.  c  om
    if (Core.GUIMode) {
        ij.IJ.log("Number of fields" + cursor.count());
    }
    while (cursor.hasNext()) {
        DBObject obj = cursor.next();
        removeField((ObjectId) obj.get("_id"));
    }
    cursor.close();
    selection.remove(queryXP);
    experiment.remove(new BasicDBObject("name", name));
    if (Core.GUIMode) {
        IJ.log("xp removed:" + name);
    }
}

From source file:tango.mongo.MongoConnector.java

License:Open Source License

public synchronized ArrayList<String> getNucSettings() {
    DBCursor cur = nucleusSettings.find().sort(new BasicDBObject("name", 1));
    ArrayList<String> res = new ArrayList<String>(cur.count());
    while (cur.hasNext()) {
        DBObject o = cur.next();/*from  ww w.j  ava 2 s .c om*/
        if (o.containsField("name")) {
            res.add(o.get("name").toString());
        }
    }
    cur.close();
    return res;
}