Example usage for com.mongodb DBCursor close

List of usage examples for com.mongodb DBCursor close

Introduction

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

Prototype

@Override
    public void close() 

Source Link

Usage

From source file:mini_mirc_server.Mini_mirc_server.java

public static void cleaner() {
    try {/*w w w  .j  ava  2 s. c o m*/
        boolean hard_clean = false;
        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll = db.getCollection("activeUser");

        DBCursor cursor = coll.find();
        try {
            Date now = new Date();
            long timestamp_now = now.getTime();
            long treshold = timestamp_now - (1000 * 60 * 5); //5 minutes
            while (cursor.hasNext()) {
                hard_clean = true;
                BasicDBObject temp = (BasicDBObject) cursor.next();
                Date time_temp = (Date) temp.get("timestamp");
                long timestamp_temp = time_temp.getTime();
                if (timestamp_temp < treshold) {
                    String target = temp.getString("username");
                    handler.SoftDelete(target);
                }
            }
            HardClean();
        } finally {
            cursor.close();
        }

    } catch (UnknownHostException ex) {
        Logger.getLogger(Mini_mirc_server.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:mini_mirc_server.Mini_mirc_server.java

public static void HardClean() {
    try {// w w  w .j  ava  2s .  c o m
        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll[] = new DBCollection[4];
        coll[0] = db.getCollection("channelCollection");
        coll[1] = db.getCollection("inbox");
        coll[2] = db.getCollection("activeUser");
        coll[3] = db.getCollection("passiveUser");

        DBCursor cursor = coll[3].find();

        try {
            while (cursor.hasNext()) {
                BasicDBObject temp = (BasicDBObject) cursor.next();
                String username = temp.getString("username");
                BasicDBObject query = new BasicDBObject("username", username);
                System.out.println("cleaning " + username);
                for (int i = 0; i < 4; i++) {
                    DBCursor cursor2 = coll[i].find(query);

                    try {
                        while (cursor2.hasNext()) {
                            DBObject temp2 = cursor2.next();
                            coll[i].remove(temp2);
                        }
                    } finally {
                        cursor2.close();
                    }
                }
            }
        } finally {
            cursor.close();
        }

    } catch (UnknownHostException ex) {
        Logger.getLogger(Mini_mirc_server.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:Model.DAO_Etudiant.java

public boolean archive(String uneClasse) {
    // Vider la base archive a chaque fois.
    if (db.collectionExists("etudiant_save")) {
        collection = db.getCollection("etudiant_save");
        collection.drop();//from w  w  w  . j ava 2 s. c om
    } else {
        DBObject options = BasicDBObjectBuilder.start().add("capped", false).add("size", 2000000000l).get();
        collection = db.createCollection("etudiant_save", options);
    }
    collection = db.getCollection("etudiant");
    DBCursor cursor = collection.find();
    try {
        while (cursor.hasNext()) {
            collection = db.getCollection("etudiant_save");
            collection.insert(cursor.next());
        }
        collection = db.getCollection("etudiant");
        BasicDBObject query = new BasicDBObject();
        query.append("classe", uneClasse);
        collection.remove(query);

    } finally {
        cursor.close();
    }

    return true;
}

From source file:models.datasource.SingletonDataSource.java

public static List<User> findAll() {
    DBCollection collection = connectDB("mongo.usersCollection");
    DBCursor cursor = collection.find();
    List<User> users = new ArrayList<>();
    try {//from w ww. ja va 2s . c  o  m
        while (cursor.hasNext()) {
            User user = new User();
            String userStr = cursor.next().toString();

            //Get user object from JSON
            user = new Gson().fromJson(userStr, User.class);

            // Deserialize Current Situation object
            user.currentSituation = new Gson().fromJson(
                    cursor.curr().get(Constants.USER_CURRENT_SITUATION).toString(), CurrentSituation.class);

            // Deserialize ArrayList of Skills
            user.skill = new Gson().fromJson(cursor.curr().get(Constants.USER_SKILLS_LIST).toString(),
                    new TypeToken<List<Skill>>() {
                    }.getType());

            //Add to USER the completedOrientationSteps object stored in JSON
            user.completedOrientationSteps = new Gson().fromJson(
                    cursor.curr().get(Constants.USER_ORIENTATION_STEPS).toString(),
                    User.CompletedOrientationSteps.class);
            // Deserialize ArrayList of InterviewSchedule Objects
            user.interviewScheduleList = new Gson().fromJson(
                    cursor.curr().get(Constants.USER_NEXT_INTERVIEWS_LIST).toString(),
                    new TypeToken<List<InterviewSchedule>>() {
                    }.getType());

            // Deserialize ArrayList of Courses
            user.courses = new Gson().fromJson(cursor.curr().get(Constants.USER_COURSES).toString(),
                    new TypeToken<List<Course>>() {
                    }.getType());

            //Deserialize ArrayList of Languages
            user.languages = new Gson().fromJson(cursor.curr().get(Constants.USER_LANGUAGES).toString(),
                    new TypeToken<List<Language>>() {
                    }.getType());

            //Deserialize ArrayList of Software
            user.softwareList = new Gson().fromJson(cursor.curr().get(Constants.USER_SOFTWARE).toString(),
                    new TypeToken<List<Software>>() {
                    }.getType());

            user.id = cursor.curr().get("_id").toString();

            users.add(user);
        }
    } finally {
        cursor.close();
    }

    mongoClient.close();

    return users;
}

From source file:Modules.Client.Model.DAO.DAO_Mongo_client.java

public static void load_Clients() {
    DBCollection table = Singleton_app.collection;
    DBCursor cursor = null;
    Client_class client = new Client_class();

    try {/*from   w  w  w  .j  av a 2  s .co m*/
        cursor = table.find();
        if (cursor.count() != 0) {
            while (cursor.hasNext()) {
                BasicDBObject document = (BasicDBObject) cursor.next();
                Singleton_client.cli.add(client.Client_from_DB(document));
            }
        } else {
            System.out.println("NO DATA");
        }

    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

}

From source file:Modules.Client.Model.DAO.DAO_Mongo_client.java

public static Client_class load_client_dni(String dni) {
    DBCollection table = Singleton_app.collection;
    DBCursor cursor = null;
    Client_class c = new Client_class();
    //        System.out.println("Modules.Client.Model.DAO.DAO_Mongo_client.load_client_dni()");
    try {/*from   w  ww .ja v  a2  s.c o m*/
        BasicDBObject query = new BasicDBObject();
        query.put("dni", dni);

        cursor = table.find(query);
        if (cursor.count() != 0) {
            while (cursor.hasNext()) {
                BasicDBObject document = (BasicDBObject) cursor.next();
                c = c.Client_from_DB(document);

            }
        } else {
            //                System.out.println("NO DATA");
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return c;

}

From source file:modules.Sign_In.model.DAO_SignIn.DAO_SignIn.java

/**
 * funcion para buscar un cliente por DNI en MongoDB
 *//*from w  w w . j a  v a 2 s  . c  o m*/
public static void searchClientDAO() {
    DBCursor cursor = null;
    singleton.userclient.clear();
    client c = new client();
    try {

        BasicDBObject searchClient = new BasicDBObject();
        c = new client(Sign_In.dniField.getText());
        searchClient.append("dni", c.getDni());
        cursor = singletonMongo.collection.find(searchClient);
        if (cursor.count() != 0) {
            while (cursor.hasNext()) {
                BasicDBObject document = (BasicDBObject) cursor.next();
                singleton.client = c.Mongo_to_client(document);
            }
        } else {
            System.out.println("NOT DATA");
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:modules.users.client.model.DAO.DAO_Mongo.java

/**
 * lista los clientes que hay guardados en MongoDB
 *//*from  w w  w.j  av a2  s .c  om*/
public static void viewTableClient() {
    DBCursor cursor = null;
    singleton.userclient.clear();
    client c = new client();
    try {
        cursor = singletonMongo.collection.find();
        if (cursor.count() != 0) {
            while (cursor.hasNext()) {
                BasicDBObject document = (BasicDBObject) cursor.next();
                singleton.client = c.Mongo_to_client(document);
                singleton.userclient.add(singleton.client);

            }
        } else {
            System.out.println("NOT DATA");
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:mongoDB.MongoDbClientAllImagesAsByteArrays.java

License:Open Source License

@Override
public int listFriends(int requesterID, int profileOwnerID, Set<String> fields,
        Vector<HashMap<String, ByteIterator>> result, boolean insertImage, boolean testMode) {
    int retVal = 0;
    if (requesterID < 0 || profileOwnerID < 0)
        return -1;

    // first get all confirmed friendids for profileOwnerID
    com.mongodb.DB db = null;//  ww  w .  j  a v  a2 s  .  c  om
    try {
        db = mongo.getDB(database);
        db.requestStart();
        DBCollection collection = db.getCollection("users");
        DBObject q = new BasicDBObject().append("_id", profileOwnerID);
        q.removeField("pic");
        BasicDBObject attrs = new BasicDBObject("pic", 0);

        DBObject queryResult = null;
        queryResult = collection.findOne(q, attrs);

        String x = queryResult.get("ConfFriends").toString();

        if (!x.equals("")) {
            x = x.substring(2, x.length() - 1);
            if (!x.equals("")) {
                String friendIds[] = x.split(",");
                BasicDBObject query = new BasicDBObject();
                if (!friendListReq) {
                    List<Integer> list = new ArrayList<Integer>();
                    for (int i = 0; i < friendIds.length; i++) {
                        // add to list
                        list.add(Integer.parseInt(friendIds[i].trim()));
                        int cnt = 0;
                        if (i == friendIds.length - 1 || ((i + 1) % 10) == 0) {
                            // query
                            query.put("_id", new BasicDBObject("$in", list));
                            query.removeField("pic");
                            // DBCursor cursor = collection.find(query,
                            // fieldsObj);
                            DBCursor cursor = collection.find(query, attrs);
                            while (cursor.hasNext()) {
                                cnt++;
                                // System.out.println(cursor.next());
                                HashMap<String, ByteIterator> vals = new HashMap<String, ByteIterator>();
                                DBObject curs = cursor.next();
                                if (curs.get("tpic") != null) {
                                    byte[] myPic = (byte[]) curs.get("tpic");
                                    vals.put("tpic", new ObjectByteIterator(myPic));
                                    if (testMode) {
                                        // Save loaded image from database
                                        // into new image file
                                        FileOutputStream fos = new FileOutputStream(
                                                profileOwnerID + "-" + cnt + "-mthumbimage.bmp");
                                        fos.write(myPic);
                                        fos.close();
                                    }
                                    curs.removeField("tpic");
                                }
                                vals.putAll(curs.toMap());
                                vals.remove("ConfFriends");
                                vals.remove("PendFriends");
                                vals.remove("wallResources");
                                // needed to do this so the coreworkload
                                // will not need to know the datastore typr
                                if (vals.get("_id") != null) {
                                    String tmp = vals.get("_id") + "";
                                    vals.remove("_id");
                                    vals.put("userid", new ObjectByteIterator(tmp.getBytes()));
                                }
                                result.add(vals);
                            }
                            cursor.close();
                            // empty list
                            list.clear();
                        }
                    }
                } else if (friendListReq) {// retrive one list
                    List<Integer> list = new ArrayList<Integer>();
                    for (int i = 0; i < friendIds.length; i++) {
                        // put all in one list and retrieve instead of
                        // retrieving one by one
                        list.add(Integer.parseInt(friendIds[i].trim()));
                    }
                    query.put("_id", new BasicDBObject("$in", list));
                    query.removeField("pic");
                    // DBCursor cursor = collection.find(query, fieldsObj);
                    DBCursor cursor = collection.find(query, attrs);

                    int cnt = 0;
                    while (cursor.hasNext()) {
                        cnt++;
                        HashMap<String, ByteIterator> vals = new HashMap<String, ByteIterator>();
                        DBObject curs = cursor.next();
                        if (curs.get("tpic") != null) {
                            byte[] myPic = (byte[]) curs.get("tpic");
                            vals.put("tpic", new ObjectByteIterator(myPic));
                            if (testMode) {
                                // Save loaded image from database into new
                                // image file
                                FileOutputStream fos = new FileOutputStream(
                                        profileOwnerID + "-" + cnt + "-mthumbimage.bmp");
                                fos.write(myPic);
                                fos.close();
                            }
                            curs.removeField("tpic");
                        }

                        vals.putAll(curs.toMap());
                        vals.remove("ConfFriends");
                        vals.remove("PendFriends");
                        vals.remove("wallResurces");
                        // needed to do this so the coreworkload will not
                        // need to know the datastore typr
                        if (vals.get("_id") != null) {
                            String tmp = vals.get("_id") + "";
                            vals.remove("_id");
                            vals.put("userid", new ObjectByteIterator(tmp.getBytes()));
                        }
                        result.add(vals);
                    }
                    cursor.close();
                }
            }
        }

    } catch (Exception e) {
        System.out.println(e.toString());
        retVal = -1;
    } finally {
        if (db != null) {
            db.requestDone();
        }
    }
    return retVal;

}

From source file:mongoDB.MongoDbClientAllImagesAsByteArrays.java

License:Open Source License

@Override
public int viewFriendReq(int profileOwnerID, Vector<HashMap<String, ByteIterator>> values, boolean insertImage,
        boolean testMode) {
    int retVal = 0;
    if (profileOwnerID < 0)
        return -1;

    // first get all pending friendids for profileOwnerID
    com.mongodb.DB db = null;/*w  w  w  .j  a  v a 2  s . c  o m*/

    try {
        db = mongo.getDB(database);
        db.requestStart();
        DBCollection collection = db.getCollection("users");
        DBObject q = new BasicDBObject().append("_id", profileOwnerID);
        q.removeField("pic");
        BasicDBObject fields = new BasicDBObject("pic", 0);

        DBObject queryResult = null;
        queryResult = collection.findOne(q, fields);

        String x = queryResult.get("PendFriends").toString();
        if (!x.equals("")) {
            x = x.substring(2, x.length() - 1);
            if (!x.equals("")) {
                String friendIds[] = x.split(",");
                BasicDBObject query = new BasicDBObject();

                List<Integer> list = new ArrayList<Integer>();
                if (!friendListReq) {
                    int cnt = 0;
                    for (int i = 0; i < friendIds.length; i++) {
                        cnt++;
                        HashMap<String, ByteIterator> vals = new HashMap<String, ByteIterator>();
                        DBObject frnd = new BasicDBObject().append("_id",
                                Integer.parseInt(friendIds[i].trim()));
                        frnd.removeField("pic");
                        DBObject frndQueryResult = null;
                        frndQueryResult = collection.findOne(frnd, fields);
                        if (frndQueryResult.get("tpic") != null) {
                            byte[] myPic = (byte[]) frndQueryResult.get("tpic");
                            vals.put("tpic", new ObjectByteIterator(myPic));
                            if (testMode) {
                                // Save loaded image from database into new
                                // image file
                                FileOutputStream fos = new FileOutputStream(
                                        profileOwnerID + "-" + i + "-mthumbimage.bmp");
                                fos.write(myPic);
                                fos.close();
                            }
                            frndQueryResult.removeField("tpic");
                        }
                        if (frndQueryResult != null) {
                            frndQueryResult.removeField("ConfFriends");
                            frndQueryResult.removeField("PendFriends");
                            frndQueryResult.removeField("wallResources");
                            vals.putAll(frndQueryResult.toMap());
                        }
                        if (vals.get("_id") != null) {
                            String tmp = vals.get("_id") + "";
                            vals.remove("_id");
                            vals.put("userid", new ObjectByteIterator(tmp.getBytes()));
                        }
                        values.add(vals);
                    }
                } else if (friendListReq) {// retrive one list
                    for (int i = 0; i < friendIds.length; i++) {
                        // put all in one list and retrieve instead of
                        // retrieving one by one
                        list.add(Integer.parseInt(friendIds[i].trim()));
                    }
                    query.put("_id", new BasicDBObject("$in", list));
                    query.removeField("pic");
                    DBCursor cursor = collection.find(query, fields);
                    int cnt = 0;
                    while (cursor.hasNext()) {
                        cnt++;
                        // System.out.println(cursor.next());
                        HashMap<String, ByteIterator> vals = new HashMap<String, ByteIterator>();
                        DBObject curs = cursor.next();
                        if (curs.get("tpic") != null) {
                            byte[] myPic = (byte[]) curs.get("tpic");
                            vals.put("tpic", new ObjectByteIterator(myPic));
                            if (testMode) {
                                // Save loaded image from database into new
                                // image file
                                FileOutputStream fos = new FileOutputStream(
                                        profileOwnerID + "-" + cnt + "-mthumbimage.bmp");
                                fos.write(myPic);
                                fos.close();
                            }
                            curs.removeField("tpic");
                        }
                        vals.putAll(curs.toMap());
                        vals.remove("PendFriends");
                        vals.remove("ConfFriends");
                        vals.remove("wallResources");
                        // needed to do this so the coreworkload will not
                        // need to know the datastore typr
                        if (vals.get("_id") != null) {
                            String tmp = vals.get("_id") + "";
                            vals.remove("_id");
                            vals.put("userid", new ObjectByteIterator(tmp.getBytes()));
                        }
                        values.add(vals);
                    }
                    cursor.close();
                }

            }

        }

    } catch (Exception e) {
        System.out.println(e.toString());
        retVal = -1;
    } finally {
        if (db != null) {
            db.requestDone();
        }
    }
    return retVal;

}