Example usage for com.mongodb CursorType Tailable

List of usage examples for com.mongodb CursorType Tailable

Introduction

In this page you can find the example usage for com.mongodb CursorType Tailable.

Prototype

CursorType Tailable

To view the source code for com.mongodb CursorType Tailable.

Click Source Link

Document

Tailable means the cursor is not closed when the last data is retrieved.

Usage

From source file:com.eightkdata.mongowp.client.wrapper.MongoConnectionWrapper.java

License:Open Source License

private CursorType toCursorType(QueryOptions queryOptions) {
    if (!queryOptions.isTailable()) {
        return CursorType.NonTailable;
    }/*from w  w  w  . j av a  2s  . co m*/
    if (queryOptions.isAwaitData()) {
        return CursorType.TailableAwait;
    }
    return CursorType.Tailable;
}

From source file:com.streamsets.pipeline.stage.origin.mongodb.oplog.MongoDBOplogSource.java

License:Apache License

private void prepareCursor(int timestampSeconds, int ordinal, List<OplogOpType> filterOplogTypes,
        int batchSize) {
    LOG.debug("Getting new cursor with offset - TimeStampInSeconds:'{}', Ordinal : '{}' and Batch Size : '{}'",
            timestampSeconds, ordinal, batchSize);
    FindIterable<Document> mongoCursorIterable = mongoCollection.find()
            //As the collection is a capped collection we use Tailable cursor which will return results in natural order in this case
            //based on ts timestamp field.
            //Tailable Await does not return and blocks, so we are using tailable.
            .cursorType(CursorType.Tailable).batchSize(batchSize);

    List<Bson> andFilters = new ArrayList<>();
    //Only filter if we already have saved/initial offset specified or else both time_t and ordinal will not be -1.
    if (timestampSeconds > 0 && ordinal >= 0) {
        andFilters.add(Filters.gt(TIMESTAMP_FIELD, new BsonTimestamp(timestampSeconds, ordinal)));
    }/*from  ww w  .j  a  v  a  2s  .co m*/

    if (!filterOplogTypes.isEmpty()) {
        List<Bson> oplogOptypeFilters = new ArrayList<>();
        Set<OplogOpType> oplogOpTypesSet = new HashSet<>();
        for (OplogOpType filterOplogopType : filterOplogTypes) {
            if (oplogOpTypesSet.add(filterOplogopType)) {
                oplogOptypeFilters.add(Filters.eq(OP_TYPE_FIELD, filterOplogopType.getOp()));
            }
        }
        //Add an or filter for filtered Or Types
        andFilters.add(Filters.or(oplogOptypeFilters));
    }
    //Finally and timestamp with oplog filters
    if (!andFilters.isEmpty()) {
        mongoCursorIterable = mongoCursorIterable.filter(Filters.and(andFilters));
    }
    cursor = mongoCursorIterable.iterator();
}