Example usage for com.mongodb Bytes QUERYOPTION_AWAITDATA

List of usage examples for com.mongodb Bytes QUERYOPTION_AWAITDATA

Introduction

In this page you can find the example usage for com.mongodb Bytes QUERYOPTION_AWAITDATA.

Prototype

int QUERYOPTION_AWAITDATA

To view the source code for com.mongodb Bytes QUERYOPTION_AWAITDATA.

Click Source Link

Document

Use with TailableCursor.

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;//from www .  jav a2  s.co 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:com.edgytech.umongo.MongoUtils.java

License:Apache License

public static String queryOptionsToString(int options) {
    String opt = "";
    if ((options & Bytes.QUERYOPTION_TAILABLE) != 0) {
        opt += "TAILABLE ";
    }//from  ww  w.  ja va  2 s. c  o  m
    if ((options & Bytes.QUERYOPTION_SLAVEOK) != 0) {
        opt += "SLAVEOK ";
    }
    if ((options & Bytes.QUERYOPTION_OPLOGREPLAY) != 0) {
        opt += "OPLOGREPLAY ";
    }
    if ((options & Bytes.QUERYOPTION_NOTIMEOUT) != 0) {
        opt += "NOTIMEOUT ";
    }
    if ((options & Bytes.QUERYOPTION_AWAITDATA) != 0) {
        opt += "AWAITDATA ";
    }
    if ((options & Bytes.QUERYOPTION_EXHAUST) != 0) {
        opt += "EXHAUST ";
    }
    return opt;
}

From source file:com.edgytech.umongo.OptionDialog.java

License:Apache License

void update(int options, WriteConcern wc, ReadPreference rp) {
    // reset/*  w  ww . j av a  2 s. co  m*/
    xmlLoadCheckpoint();

    setBooleanFieldValue(Item.tailable, (options & Bytes.QUERYOPTION_TAILABLE) != 0);
    setBooleanFieldValue(Item.slaveOk, (options & Bytes.QUERYOPTION_SLAVEOK) != 0);
    setBooleanFieldValue(Item.opLogReplay, (options & Bytes.QUERYOPTION_OPLOGREPLAY) != 0);
    setBooleanFieldValue(Item.noTimeout, (options & Bytes.QUERYOPTION_NOTIMEOUT) != 0);
    setBooleanFieldValue(Item.awaitData, (options & Bytes.QUERYOPTION_AWAITDATA) != 0);
    setBooleanFieldValue(Item.exhaust, (options & Bytes.QUERYOPTION_EXHAUST) != 0);
    setBooleanFieldValue(Item.partial, (options & Bytes.QUERYOPTION_PARTIAL) != 0);

    Object w = wc.getWObject();
    int wInt = (Integer) (w instanceof Integer ? w : 0);
    String wStr = (String) (w instanceof String ? w : "");
    setIntFieldValue(Item.writeFactor, wInt);
    setStringFieldValue(Item.writePolicy, wStr);
    setIntFieldValue(Item.writeTimeout, wc.getWtimeout());
    //        setBooleanFieldValue(Item.fsync, wc.fsync());

    DBObject rpObj = rp.toDBObject();
    ComboBox readBox = (ComboBox) getBoundUnit(Item.rpPreference);
    ReadPref rpEnm = ReadPref.primary;
    if (rp != null)
        rpEnm = ReadPref.valueOf(rp.getName());
    readBox.value = rpEnm.ordinal();
    if (rpObj.containsField("tags")) {
        List tags = (List) rpObj.get("tags");
        if (tags.size() > 0) {
            ((DocBuilderField) getBoundComponentUnit(Item.rpTag)).setDBObject((DBObject) tags.get(0));
        }
    }
}

From source file:com.edgytech.umongo.OptionDialog.java

License:Apache License

int getQueryOptions() {
    int options = 0;
    if (getBooleanFieldValue(Item.tailable))
        options |= Bytes.QUERYOPTION_TAILABLE;
    if (getBooleanFieldValue(Item.slaveOk))
        options |= Bytes.QUERYOPTION_SLAVEOK;
    if (getBooleanFieldValue(Item.opLogReplay))
        options |= Bytes.QUERYOPTION_OPLOGREPLAY;
    if (getBooleanFieldValue(Item.noTimeout))
        options |= Bytes.QUERYOPTION_NOTIMEOUT;
    if (getBooleanFieldValue(Item.awaitData))
        options |= Bytes.QUERYOPTION_AWAITDATA;
    if (getBooleanFieldValue(Item.exhaust))
        options |= Bytes.QUERYOPTION_EXHAUST;
    if (getBooleanFieldValue(Item.partial))
        options |= Bytes.QUERYOPTION_PARTIAL;
    return options;
}

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.
 * //ww  w. ja v a  2s  .  com
 * @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.tomtom.speedtools.tracer.mongo.MongoDBTraceFetcher.java

License:Apache License

/**
 * Move to a specific time location, or to the end of the event stream.
 *
 * @param time Move to start of this time stamp, or to end of stream if null.
 * @return Time moved to./*from   w w w.j  a va  2  s  .c o m*/
 */
@Nonnull
public DateTime moveTo(@Nullable final DateTime time) {

    // Bail-out if traces disabled.
    if (collection == null) {
        LOG.trace("getTraces: reading traces disabled, cannot move to {}", time);
        return (time == null) ? UTCTime.now() : time;
    }

    final DateTime fromTime;
    if (time == null) {
        final DateTime now = UTCTime.now();
        while (now.equals(UTCTime.now())) {

            // Move past 'now'.
            try {
                //noinspection BusyWait
                Thread.sleep(1);
            } catch (final InterruptedException ignored) {
                // Ignored.
            }
        }
        fromTime = UTCTime.now();
    } else {
        fromTime = time;
    }

    /**
     * Select all events after 'fromTime'.
     *
     * Important: The query will not be tailable if the first query does not return
     * a record, so we MUST add a dummy record. The fetcher must forget (i.e. not handle())
     * this one if it encounters it.
     */
    final DBObject query = new BasicDBObject("time", new BasicDBObject("$gte", fromTime.toDate()));
    if (collection.findOne(query) == null) {

        // No object was, we need to insert at least one.
        @Nonnull
        final MongoDBTrace trace = new MongoDBTrace(fromTime, // Insert time stamp.
                TAILABLE_QUERY_DUMMY_EVENT, // Owner class.
                TAILABLE_QUERY_DUMMY_EVENT, // Tracer interface.
                TAILABLE_QUERY_DUMMY_EVENT, // Method name.
                new Object[] {}, 0); // Dummy object and serial number.

        // Catch exceptions from MongoDB here.
        try {
            final Object dbTrace = SimpleMongoDBSerializer.getInstance().serialize(trace);
            if (dbTrace instanceof DBObject) {
                collection.insert((DBObject) dbTrace);
            }
        } catch (final Exception e) {
            LOG.error("moveTo: unexpected exception=" + e.getMessage(), e);

            /**
             *  Continue execution, because errors during tracing should NOT disturb execution.
             *  Do log this as en error, because we're not expecting this to happen.
             */
        }
        LOG.debug("moveTo: inserted dummy trace event, fromTime={}", fromTime);
    }

    // Execute actual tailing query.
    final DBCursor cursor = collection.find(query).addOption(Bytes.QUERYOPTION_TAILABLE)
            .addOption(Bytes.QUERYOPTION_AWAITDATA);

    // Start a new fetch
    currentFetch.set(new CurrentFetch(cursor, new ConcurrentLinkedQueue<>()));
    return fromTime;
}

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

License:Open Source License

public void run() {
    running = true;//  w  w  w  .  ja va 2 s .c  o  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:de.otto.mongodb.profiler.op.OpProfileDataFetcher.java

License:Apache License

private DBCursor getCursor() {

    synchronized (cursorMutex) {

        // Close stale cursor
        if (cursor != null && cursor.getCursorId() == 0L) {
            cursor.close();/*from   w w w  . j  a  v  a  2s  .  com*/
            cursor = null;
        }

        // Create new cursor
        if (cursor == null && db.collectionExists(COLLECTION)) {

            if (lastTs == null) {
                lastTs = DateTime.now(DateTimeZone.UTC);
            }

            final DBCollection collection = db.getCollection(COLLECTION);
            final DBObject query = QueryBuilder.start()
                    .and(QueryBuilder.start("ns").notEquals(collection.getFullName()).get(),
                            QueryBuilder.start("ts").greaterThan(lastTs.toDate()).get())
                    .get();
            final DBObject sortBy = new BasicDBObject("$natural", 1);
            final DBCursor cursor = collection.find(query).sort(sortBy).batchSize(100)
                    .addOption(Bytes.QUERYOPTION_TAILABLE).addOption(Bytes.QUERYOPTION_AWAITDATA);
            this.cursor = cursor;
        }
    }

    return cursor;
}

From source file:essex.bigessexnew.OplogListener.java

public void listen(boolean processHistory, String... logFields) {
    BasicDBObject query = prepareOplogListenQuery(processHistory);
    BasicDBObject fields = prepareOplogListenFields(logFields);

    DBCursor cur = collection.find(query, fields).sort((BasicDBObjectBuilder.start("$natural", 1)).get())
            .addOption(Bytes.QUERYOPTION_TAILABLE | Bytes.QUERYOPTION_AWAITDATA | Bytes.QUERYOPTION_NOTIMEOUT);

    performListenTask(cur);/*from w w w  .j  av  a 2s.co  m*/
}

From source file:HAL.libraries.blackboard_client.OplogMonitorThread.java

License:Open Source License

/**
 * Constructor of OplogMonitorThread./*w ww. j av a 2s.  co  m*/
 * @param mongo The Mongo database connection that should be used.
 * @param oplogDBName The database in which the oplog collection resides.
 * @param oplogCollectionName The name of the oplog collection.
 * @param query The query that will be used in the tailed cursor.
 **/
public OplogMonitorThread(Mongo mongo, String oplogDBName, String oplogCollectionName, DBObject query) {
    database = mongo.getDB(oplogDBName);
    DBCollection collection = database.getCollection(oplogCollectionName);

    tailedCursor = collection.find(query);
    tailedCursor.addOption(Bytes.QUERYOPTION_TAILABLE);
    tailedCursor.addOption(Bytes.QUERYOPTION_AWAITDATA);
    tailedCursor.skip(tailedCursor.size());
    callbackThread = new OplogCallbackThread();
}