Example usage for com.mongodb DBCursor getCursorId

List of usage examples for com.mongodb DBCursor getCursorId

Introduction

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

Prototype

@Override
    public long getCursorId() 

Source Link

Usage

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

License:Apache License

public void find(final ButtonBase button) {
    final DBCollection col = getCollectionNode().getCollection();
    final DBObject query = ((DocBuilderField) getBoundUnit(Item.findQuery)).getDBObject();
    final DBObject fields = ((DocBuilderField) getBoundUnit(Item.findFields)).getDBObject();
    final DBObject sort = ((DocBuilderField) getBoundUnit(Item.findSort)).getDBObject();
    final DBObject hint = ((DocBuilderField) getBoundUnit(Item.findHint)).getDBObject();
    final int skip = getIntFieldValue(Item.findSkip);
    final int limit = getIntFieldValue(Item.findLimit);
    final int batchSize = getIntFieldValue(Item.findBatchSize);
    final boolean explain = getBooleanFieldValue(Item.findExplain);
    final boolean export = getBooleanFieldValue(Item.findExport);

    if (export) {
        exportToFile(col, query, fields, sort, skip, limit, batchSize);
    } else {//w ww  .  ja va 2  s .c o m
        new DbJob() {
            @Override
            public Object doRun() {
                // this does not actually block, may not need dbjob
                DBCursor cur = col.find(query, fields, skip, batchSize);
                if (sort != null) {
                    cur.sort(sort);
                }
                if (limit > 0) {
                    cur.limit(limit);
                }
                if (hint != null) {
                    cur.hint(hint);
                }
                if (explain) {
                    return cur.explain();
                }

                // force cursor to start
                cur.hasNext();
                return cur;
            }

            @Override
            public String getNS() {
                return col.getFullName();
            }

            @Override
            public String getShortName() {
                return "Find";
            }

            @Override
            public DBObject getRoot(Object result) {
                if (result == null || !(result instanceof DBCursor)) {
                    return null;
                }
                DBCursor res = (DBCursor) result;
                BasicDBObject obj = new BasicDBObject("cursorId", res.getCursorId());
                obj.put("server", res.getServerAddress().toString());
                obj.put("query", res.getQuery());
                obj.put("fields", res.getKeysWanted());
                obj.put("options", res.getOptions());
                obj.put("readPreference", res.getReadPreference().toDBObject());
                obj.put("numSeen", res.numSeen());
                obj.put("numGetMores", res.numGetMores());
                // want skip, limit, batchsize
                return obj;
            }

            @Override
            public ButtonBase getButton() {
                return button;
            }
        }.addJob();

    }
}

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

License:Apache License

static void doFind(final DBCollection col, final DBObject query, final DBObject fields, final DBObject sort,
        final int skip, final int limit, final int batchSize, final boolean explain, final DBObject hint,
        final int options) {
    new DbJob() {
        @Override/* w w w. j  a  v a  2s.  co m*/
        public Object doRun() {
            // this does not actually block, may not need dbjob
            DBCursor cur = col.find(query, fields).skip(skip).batchSize(batchSize).addOption(options);
            if (sort != null) {
                cur.sort(sort);
            }
            if (limit > 0) {
                cur.limit(limit);
            }
            if (hint != null) {
                cur.hint(hint);
            }
            if (explain) {
                return cur.explain();
            }

            // force cursor to start
            cur.hasNext();
            return cur;
        }

        @Override
        public String getNS() {
            return col.getFullName();
        }

        @Override
        public String getShortName() {
            return "Find";
        }

        @Override
        public DBObject getRoot(Object result) {
            if (result == null || !(result instanceof DBCursor)) {
                return null;
            }
            DBCursor res = (DBCursor) result;
            BasicDBObject obj = new BasicDBObject("cursorId", res.getCursorId());
            obj.put("query", res.getQuery());
            obj.put("fields", res.getKeysWanted());
            obj.put("options", res.getOptions());
            obj.put("readPreference", res.getReadPreference().toDBObject());
            obj.put("numSeen", res.numSeen());
            obj.put("numGetMores", res.numGetMores());
            // want skip, limit, batchsize
            return obj;
        }
    }.addJob();
}

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();/*  w  w  w  .  j a  v  a 2 s  . c o  m*/
            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:org.apache.camel.component.gridfs.GridFsConsumer.java

License:Apache License

@Override
public void run() {
    DBCursor c = null;
    java.util.Date fromDate = null;

    QueryStrategy s = endpoint.getQueryStrategy();
    boolean usesTimestamp = (s != QueryStrategy.FileAttribute);
    boolean persistsTimestamp = (s == QueryStrategy.PersistentTimestamp
            || s == QueryStrategy.PersistentTimestampAndFileAttribute);
    boolean usesAttribute = (s == QueryStrategy.FileAttribute || s == QueryStrategy.TimeStampAndFileAttribute
            || s == QueryStrategy.PersistentTimestampAndFileAttribute);

    DBCollection ptsCollection = null;//from   w  w  w.j ava2  s  . c om
    DBObject persistentTimestamp = null;
    if (persistsTimestamp) {
        ptsCollection = endpoint.getDB().getCollection(endpoint.getPersistentTSCollection());
        // ensure standard indexes as long as collections are small
        try {
            if (ptsCollection.count() < 1000) {
                ptsCollection.createIndex(new BasicDBObject("id", 1));
            }
        } catch (MongoException e) {
            //TODO: Logging
        }
        persistentTimestamp = ptsCollection.findOne(new BasicDBObject("id", endpoint.getPersistentTSObject()));
        if (persistentTimestamp == null) {
            persistentTimestamp = new BasicDBObject("id", endpoint.getPersistentTSObject());
            fromDate = new java.util.Date();
            persistentTimestamp.put("timestamp", fromDate);
            ptsCollection.save(persistentTimestamp);
        }
        fromDate = (java.util.Date) persistentTimestamp.get("timestamp");
    } else if (usesTimestamp) {
        fromDate = new java.util.Date();
    }
    try {
        Thread.sleep(endpoint.getInitialDelay());
        while (isStarted()) {
            if (c == null || c.getCursorId() == 0) {
                if (c != null) {
                    c.close();
                }
                String queryString = endpoint.getQuery();
                DBObject query;
                if (queryString == null) {
                    query = new BasicDBObject();
                } else {
                    query = (DBObject) JSON.parse(queryString);
                }
                if (usesTimestamp) {
                    query.put("uploadDate", new BasicDBObject("$gt", fromDate));
                }
                if (usesAttribute) {
                    query.put(endpoint.getFileAttributeName(), null);
                }
                c = endpoint.getFilesCollection().find(query);
            }
            boolean dateModified = false;
            while (c.hasNext() && isStarted()) {
                GridFSDBFile file = (GridFSDBFile) c.next();
                GridFSDBFile forig = file;
                if (usesAttribute) {
                    file.put(endpoint.getFileAttributeName(), "processing");
                    DBObject q = BasicDBObjectBuilder.start("_id", file.getId()).append("camel-processed", null)
                            .get();
                    forig = (GridFSDBFile) endpoint.getFilesCollection().findAndModify(q, null, null, false,
                            file, true, false);
                }
                if (forig != null) {
                    file = endpoint.getGridFs().findOne(new BasicDBObject("_id", file.getId()));

                    Exchange exchange = endpoint.createExchange();
                    exchange.getIn().setHeader(GridFsEndpoint.GRIDFS_METADATA,
                            JSON.serialize(file.getMetaData()));
                    exchange.getIn().setHeader(Exchange.FILE_CONTENT_TYPE, file.getContentType());
                    exchange.getIn().setHeader(Exchange.FILE_LENGTH, file.getLength());
                    exchange.getIn().setHeader(Exchange.FILE_LAST_MODIFIED, file.getUploadDate());
                    exchange.getIn().setBody(file.getInputStream(), InputStream.class);
                    try {
                        getProcessor().process(exchange);
                        //System.out.println("Processing " + file.getFilename());
                        if (usesAttribute) {
                            forig.put(endpoint.getFileAttributeName(), "done");
                            endpoint.getFilesCollection().save(forig);
                        }
                        if (usesTimestamp) {
                            if (file.getUploadDate().compareTo(fromDate) > 0) {
                                fromDate = file.getUploadDate();
                                dateModified = true;
                            }
                        }
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            if (persistsTimestamp && dateModified) {
                persistentTimestamp.put("timestamp", fromDate);
                ptsCollection.save(persistentTimestamp);
            }
            Thread.sleep(endpoint.getDelay());
        }
    } catch (Throwable e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    if (c != null) {
        c.close();
    }
}