Example usage for com.mongodb DBCursor addOption

List of usage examples for com.mongodb DBCursor addOption

Introduction

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

Prototype

@Deprecated
public DBCursor addOption(final int option) 

Source Link

Document

Adds a query option.

Usage

From source file:ReadOplog.java

License:Apache License

public static void main(String[] args) throws Exception {

    MongoClient mongoClient = new MongoClient();
    DB local = mongoClient.getDB("local");

    DBCollection oplog = local.getCollection("oplog.$main");

    DBCursor lastCursor = oplog.find().sort(new BasicDBObject("$natural", -1)).limit(1);
    if (!lastCursor.hasNext()) {
        System.out.println("no oplog!");
        return;/* ww  w. ja v a2s .c o m*/
    }
    DBObject last = lastCursor.next();

    BSONTimestamp ts = (BSONTimestamp) last.get("ts");
    System.out.println("starting point: " + ts);

    while (true) {
        System.out.println("starting at ts: " + ts);
        DBCursor cursor = oplog.find(new BasicDBObject("ts", new BasicDBObject("$gt", ts)));
        cursor.addOption(Bytes.QUERYOPTION_TAILABLE);
        cursor.addOption(Bytes.QUERYOPTION_AWAITDATA);
        while (cursor.hasNext()) {
            DBObject x = cursor.next();
            ts = (BSONTimestamp) x.get("ts");
            System.out.println("\t" + x);
        }

        Thread.sleep(1000);
    }
}

From source file:act.server.MongoDB.java

License:Open Source License

public DBIterator getDbIteratorOverSeq(BasicDBObject matchCriterion, BasicDBObject keys) {
    if (keys == null) {
        keys = new BasicDBObject();
    }// ww w  .jav a  2 s  .  c om

    DBCursor cursor = this.dbSeq.find(matchCriterion, keys);
    cursor = cursor.addOption(Bytes.QUERYOPTION_NOTIMEOUT);
    return new DBIterator(cursor);
}

From source file:act.server.MongoDB.java

License:Open Source License

public DBIterator getIteratorOverChemicals(BasicDBObject matchCriterion, BasicDBObject keys) {
    if (keys == null) {
        keys = new BasicDBObject();
    }/*from   w  w  w  .  ja  va2s . com*/
    DBCursor cursor = this.dbChemicals.find(matchCriterion, keys);
    cursor = cursor.addOption(Bytes.QUERYOPTION_NOTIMEOUT);

    return new DBIterator(cursor); // DBIterator is just a wrapper class
}

From source file:act.server.MongoDB.java

License:Open Source License

public DBIterator getIteratorOverReactions(BasicDBObject matchCriterion, BasicDBObject keys) {

    if (keys == null) {
        keys = new BasicDBObject();
    }/*  w  ww.  ja v a 2s .  co m*/

    DBCursor cursor = this.dbReactions.find(matchCriterion, keys);
    cursor = cursor.addOption(Bytes.QUERYOPTION_NOTIMEOUT);
    return new DBIterator(cursor); // DBIterator is just a wrapper classs
}

From source file:act.server.MongoDB.java

License:Open Source License

public DBIterator getDbIteratorOverOrgs(BasicDBObject matchCriterion, BasicDBObject keys) {
    if (keys == null) {
        keys = new BasicDBObject();
    }/*from www  . ja v  a  2s .c  o  m*/

    DBCursor cursor = this.dbOrganismNames.find(matchCriterion, keys);
    cursor = cursor.addOption(Bytes.QUERYOPTION_NOTIMEOUT);
    return new DBIterator(cursor);
}

From source file:com.jive.myco.seyren.mongo.MongoStore.java

License:Apache License

private void addTargetHashToAlerts() {
    LOGGER.info("Adding targetHash field to any alerts which don't have it");
    DBCursor alerts = getAlertsCollection()
            .find(new BasicDBObject("targetHash", new BasicDBObject("$exists", false)));
    alerts.addOption(Bytes.QUERYOPTION_NOTIMEOUT);
    int alertCount = alerts.count();
    if (alertCount > 0) {
        LOGGER.info("Found {} alert(s) which need updating", alertCount);
    }/*w  w w .  j  av a  2 s .  c o m*/
    while (alerts.hasNext()) {
        DBObject alertObject = alerts.next();
        Alert alert = mapper.alertFrom(alertObject);
        getAlertsCollection().save(mapper.alertToDBObject(alert));
    }
}

From source file:com.sfelf.connectors.mongoOplogCursorConnector.java

License:Open Source License

/**
 * Returns {@link DBCursor} for the specified oplog collection with the specified query applied. If fullDocument
 * is false then only the fields specified by {@link #getOplogFields() getOplogFields()} will be returned.
 * //from www .  j a  v  a2s.c o  m
 * @param oplog       {@link DBCollection} specifying the collection to use when creating the cursor
 * @param query       {@link DBObject} specifying the query to use when creating the cursor
 * @param fullDocument    {@link Boolean} specifying if the full document should be returned or the fields specified by {@link #getOplogFields() getOplogFields()}
 * @return {@link DBCursor}
 */
private DBCursor createCursor(DBCollection oplog, DBObject query, Boolean fullDocument) {
    DBCursor cursor;

    if (fullDocument) {
        cursor = oplog.find(query);
    } else {
        DBObject fields = getOplogFields();
        cursor = oplog.find(query, fields);
    }
    return cursor.addOption(Bytes.QUERYOPTION_TAILABLE).addOption(Bytes.QUERYOPTION_AWAITDATA);
}

From source file:com.wordnik.system.mongodb.OplogTailThread.java

License:Open Source License

public void run() {
    running = true;//from w w  w . ja  v  a 2s . co  m
    BSONTimestamp lastTimestamp = null;
    try {
        lastTimestamp = getLastTimestamp();

        long lastWrite = 0;
        long startTime = System.currentTimeMillis();
        long lastOutput = System.currentTimeMillis();
        while (true) {
            try {
                if (killMe) {
                    System.out.println("exiting thread");
                    return;
                }
                DBCursor cursor = null;
                oplog.setDBDecoderFactory(OplogDecoder.FACTORY);
                if (lastTimestamp != null) {
                    cursor = oplog.find(new BasicDBObject("ts", new BasicDBObject("$gt", lastTimestamp)));
                    cursor.addOption(Bytes.QUERYOPTION_OPLOGREPLAY);
                } else {
                    cursor = oplog.find();
                }
                cursor.addOption(Bytes.QUERYOPTION_TAILABLE);
                cursor.addOption(Bytes.QUERYOPTION_AWAITDATA);
                long count = 0;
                long skips = 0;

                while (!killMe && cursor.hasNext()) {
                    DBObject x = cursor.next();
                    if (!killMe) {
                        lastTimestamp = (BSONTimestamp) x.get("ts");
                        if (shouldWrite(x)) {
                            processor.processRecord((BasicDBObject) x);
                            count++;
                        } else {
                            skips++;
                        }
                        if (System.currentTimeMillis() - lastWrite > 1000) {
                            writeLastTimestamp(lastTimestamp);
                            lastWrite = System.currentTimeMillis();
                        }
                        long duration = System.currentTimeMillis() - lastOutput;
                        if (duration > reportInterval) {
                            report(this.getName(), count, skips, System.currentTimeMillis() - startTime);
                            lastOutput = System.currentTimeMillis();
                        }
                    }
                }
            } catch (com.mongodb.MongoException.CursorNotFound ex) {
                writeLastTimestamp(lastTimestamp);
                System.out.println("Cursor not found, waiting");
                Thread.sleep(2000);
            } catch (com.mongodb.MongoInternalException ex) {
                System.out.println("Cursor not found, waiting");
                writeLastTimestamp(lastTimestamp);
                ex.printStackTrace();
            } catch (com.mongodb.MongoException ex) {
                writeLastTimestamp(lastTimestamp);
                //   System.out.println("Internal exception, waiting");
                ex.printStackTrace();
                Thread.sleep(2000);
            } catch (Exception ex) {
                killMe = true;
                writeLastTimestamp(lastTimestamp);
                ex.printStackTrace();
                break;
            }
        }
        Thread.sleep(1000);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        writeLastTimestamp(lastTimestamp);
        try {
            processor.close("oplog");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    running = false;
}

From source file:eu.artist.postmigration.nfrvt.strategy.benchmark.AvailabilityCalculator.java

License:Open Source License

public static MultipleDCResults calculateAvailability(int DefinedQuantumofTimeInSeconds, int startYear,
        int startMonth, int startDay, int stopYear, int stopMonth, int stopDay) {// (Date thisDate,

    double OVERALL_MONTH_INTERVAL_SECONDS = 0;

    ///*  w  w w.  j  av  a  2 s  .  c  om*/
    try {
        Properties propertiesFile = BenchmarkConstants.getProperties();
        String databaseIP = propertiesFile.getProperty("3alibIP");
        MultipleDCResults response = new MultipleDCResults();
        Mongo mongoClient;

        System.out.println("DB NoSQL:" + databaseIP);
        mongoClient = new Mongo(databaseIP);
        DB db = mongoClient.getDB("3alib");
        System.out.println("Host address:" + databaseIP);
        DBCollection coll = db.getCollection("log_samples");

        Date date = new Date();
        Calendar calendarFrom = Calendar.getInstance();
        calendarFrom.setTime(date);
        calendarFrom.set(startYear, startMonth - 1, startDay, 0, 0, 0);

        Date dateFrom = calendarFrom.getTime();

        Calendar calendarTo = Calendar.getInstance();
        calendarTo.setTime(date);
        calendarTo.set(stopYear, stopMonth - 1, stopDay, 23, 59, 59);

        Date dateTo = calendarTo.getTime();

        System.out.println("Date beginning:" + dateFrom.toString());
        System.out.println("Date ending:" + dateTo.toString());
        ObjectId from = new ObjectId(dateFrom);
        ObjectId to = new ObjectId(dateTo);

        List<?> distinctTemplates = coll.distinct("location.parent.id");//distinct("imageId");

        for (int i = 0; i < distinctTemplates.size(); i++) {

            String index = "-1";

            System.out.println("Distinct Region IDs:" + distinctTemplates.get(i).toString());

            // query based on date to filter needed month

            BasicDBObject query = new BasicDBObject("_id", new BasicDBObject("$gte", from).append("$lte", to))
                    .append("location.parent.id", distinctTemplates.get(i).toString());

            DBCursor cursor = coll.find(query);
            cursor.addOption(com.mongodb.Bytes.QUERYOPTION_NOTIMEOUT);
            cursor.batchSize(100);

            try {
                long startID = 0;
                long stopID = 0;
                long diffSeconds = 0;
                double PREDEFINED_LOGICAL_SAMPLE_INTERVAL_IN_SECONDS = 500;//interval in which we would logically
                //have at least one sample if the daemon is running

                DBObject thisObject = cursor.next();
                System.out.println("First object:" + thisObject.toString());
                int cursorCount = cursor.count();
                System.out.println("Cursor count:" + cursor.count());
                DBObject previousObject = thisObject;
                int k = 0;
                while (k < (cursorCount + 1)) {

                    if ((k % 1000) == 0) {
                        System.out.println("Progress:" + k + " from " + cursorCount + " overall records");
                    }

                    if (((thisObject.get("reachability")).equals("UNREACHABLE")) && index.equals("-1")) { //if it is the first unavailable sample
                        System.out.println("Changing index to 1...");
                        startID = ((ObjectId) thisObject.get("_id")).getTime();//this line's id
                        System.out.println("StartID is: " + startID);
                        index = "1";
                    }

                    if (((thisObject.get("reachability")).equals("UNREACHABLE")) && (!(index.equals("-1")))) {
                        long gapstopID = ((ObjectId) thisObject.get("_id")).getTime();
                        long gapstartID = ((ObjectId) previousObject.get("_id")).getTime();
                        long GapdiffSeconds = (gapstopID - gapstartID) / 1000; // 60;
                        if (GapdiffSeconds > PREDEFINED_LOGICAL_SAMPLE_INTERVAL_IN_SECONDS) {
                            System.out.println("Found gap...");

                            stopID = ((ObjectId) previousObject.get("_id")).getTime();//this line's id to end interval
                            System.out.println("StopID is previous: " + stopID);
                            diffSeconds = (stopID - startID) / 1000;

                            if (diffSeconds > DefinedQuantumofTimeInSeconds) {
                                OVERALL_MONTH_INTERVAL_SECONDS = OVERALL_MONTH_INTERVAL_SECONDS + diffSeconds;
                                System.out.println("Overall month interval in seconds now:"
                                        + OVERALL_MONTH_INTERVAL_SECONDS);
                            }
                            startID = ((ObjectId) thisObject.get("_id")).getTime();//this line's id

                        } else {
                            //standard logic to cover generic case of consecutive unavailable samples
                        }

                    }

                    if (((((thisObject.get("reachability")).equals("REACHABLE")) || (!(cursor.hasNext()))))
                            && (!(index.equals("-1")))) {
                        if (!(cursor.hasNext())) {
                            System.out.println("FINAL ELEMENT REACHED");
                        }
                        stopID = ((ObjectId) previousObject.get("_id")).getTime();
                        diffSeconds = (stopID - startID) / 1000; // 60;

                        if (diffSeconds > DefinedQuantumofTimeInSeconds) {
                            OVERALL_MONTH_INTERVAL_SECONDS = OVERALL_MONTH_INTERVAL_SECONDS + diffSeconds;
                            System.out.println(
                                    "Overall month interval in seconds now:" + OVERALL_MONTH_INTERVAL_SECONDS);
                        }
                        System.out.println("Resetting index to -1...");
                        index = "-1";
                    }

                    if ((cursor.hasNext())) {
                        previousObject = thisObject;
                        thisObject = cursor.next();
                    }
                    k++;
                }

                System.out.println("Final Overall month unavailable interval in seconds now:"
                        + OVERALL_MONTH_INTERVAL_SECONDS);
                double OverallUnavailableIntervalInMinutes = OVERALL_MONTH_INTERVAL_SECONDS / 60;
                System.out
                        .println("OverallUnavailableIntervalInMinutes:" + OverallUnavailableIntervalInMinutes);
                double OverallIntervalInSeconds = (dateTo.getTime() - dateFrom.getTime()) / 1000;
                double OverallIntervalInMinutes = OverallIntervalInSeconds / 60;
                double finalAvailabilityPercentage = 100.0
                        * ((OverallIntervalInMinutes - OverallUnavailableIntervalInMinutes)
                                / OverallIntervalInMinutes);
                double downtimeInPercent = 100.0 - finalAvailabilityPercentage;
                response.DC.add(distinctTemplates.get(i).toString());
                response.availability.add((Double) downtimeInPercent);
                System.out.println(
                        "Final percentage of availability based on provider definition in the given interval:"
                                + finalAvailabilityPercentage);
            } catch (NoSuchElementException e2) {

                System.out.println("No available data for this period...");

            } catch (Exception e1) {

                e1.printStackTrace();

            } finally {
                cursor.close();
            }

        }
        return response;

    } catch (UnknownHostException e) {
        e.printStackTrace();
        return null;
    } catch (MongoException e) {
        System.out.println("No available data for this period...");
        return null;
    }
}

From source file:eu.artist.postmigration.nfrvt.strategy.benchmark.AvailabilityCalculator.java

License:Open Source License

public static MultipleDCResults calculateCloudSleuthAvailability(int startYear, int startMonth, int startDay,
        int stopYear, int stopMonth, int stopDay) {

    try {//from ww w  .jav  a 2 s .  c o m
        Properties propertiesFile = BenchmarkConstants.getProperties();
        String databaseIP = propertiesFile.getProperty("3alibIP");
        MultipleDCResults response = new MultipleDCResults();
        double CloudSleuthAvailability = 0;
        double CloudSleuthDowntime = 0;
        Mongo mongoClient;
        mongoClient = new Mongo(databaseIP);
        DB db = mongoClient.getDB("3alib");
        System.out.println("Host address:" + databaseIP);
        DBCollection coll = db.getCollection("log_samples");
        Date date = new Date();
        Calendar calendarFrom = Calendar.getInstance();
        calendarFrom.setTime(date);
        calendarFrom.set(startYear, startMonth - 1, startDay, 0, 0, 0);
        Date dateFrom = calendarFrom.getTime();
        Calendar calendarTo = Calendar.getInstance();
        calendarTo.setTime(date);
        calendarTo.set(stopYear, stopMonth - 1, stopDay, 23, 59, 59);
        Date dateTo = calendarTo.getTime();
        System.out.println("Date beginning:" + dateFrom.toString());
        System.out.println("Date ending:" + dateTo.toString());
        ObjectId from = new ObjectId(dateFrom);
        ObjectId to = new ObjectId(dateTo);
        List<?> distinctTemplates = coll.distinct("location.parent.id");

        for (int i = 0; i < distinctTemplates.size(); i++) {
            System.out.println("Region ID:" + distinctTemplates.get(i).toString());
            BasicDBObject queryPerRegionOverall = new BasicDBObject("_id",
                    new BasicDBObject("$gte", from).append("$lte", to)).append("location.parent.id",
                            distinctTemplates.get(i).toString());

            DBCursor cursorOverall;
            cursorOverall = coll.find(queryPerRegionOverall);
            cursorOverall.addOption(com.mongodb.Bytes.QUERYOPTION_NOTIMEOUT);

            System.out.println("Overall records:" + cursorOverall.length());

            BasicDBObject queryUnavailable = new BasicDBObject("_id",
                    new BasicDBObject("$gte", from).append("$lte", to))
                            .append("location.parent.id", distinctTemplates.get(i).toString())
                            .append("reachability", "UNREACHABLE");

            DBCursor cursorUnavailable = coll.find(queryUnavailable);
            cursorUnavailable.addOption(com.mongodb.Bytes.QUERYOPTION_NOTIMEOUT);
            System.out.println("Unavailable records:" + cursorUnavailable.length());

            CloudSleuthAvailability = 100
                    * (((double) cursorOverall.length() - (double) cursorUnavailable.length())
                            / cursorOverall.length());
            System.out.println("Cloudsleuth based availability:" + CloudSleuthAvailability);
            CloudSleuthDowntime = 100.0 - CloudSleuthAvailability;
            response.DC.add(distinctTemplates.get(i).toString());
            response.availability.add((Double) CloudSleuthDowntime);
        }
        return response;

    } catch (UnknownHostException e) {
        e.printStackTrace();
        return null;
    } catch (NoSuchElementException e) {
        System.out.println("No available data for this period...");
        return null;
    } catch (MongoException e) {
        System.out.println("No available data for this period...");
        return null;
    }
}