List of usage examples for com.mongodb DBCursor skip
public DBCursor skip(final int numberOfElements)
From source file:org.iternine.jeppetto.dao.mongodb.MongoDBQueryModelDAO.java
License:Apache License
public Iterable<T> findUsingQueryModel(QueryModel queryModel) { MongoDBCommand command = buildCommand(queryModel); DBCursor dbCursor = command.cursor(dbCollection); if (queryModel.getSorts() != null) { dbCursor.sort(processSorts(queryModel.getSorts())); }//from w ww . ja v a 2 s. c om if (queryModel.getFirstResult() > 0) { dbCursor = dbCursor.skip(queryModel.getFirstResult()); // dbCursor is zero-indexed, firstResult is one-indexed } if (queryModel.getMaxResults() > 0) { dbCursor = dbCursor.limit(queryModel.getMaxResults()); } final DBCursor finalDbCursor = dbCursor; return new Iterable<T>() { @Override public Iterator<T> iterator() { return new Iterator<T>() { @Override public boolean hasNext() { return finalDbCursor.hasNext(); } @Override @SuppressWarnings({ "unchecked" }) public T next() { DBObject result = finalDbCursor.next(); ((DirtyableDBObject) result).markPersisted(); if (MongoDBSession.isActive()) { MongoDBSession.trackForSave(MongoDBQueryModelDAO.this, buildIdentifyingQuery(result), (T) result, createIdentifyingQueries(result)); } return (T) result; } @Override public void remove() { finalDbCursor.remove(); } }; } }; }
From source file:org.jeo.mongo.MongoDataset.java
License:Open Source License
@Override public Cursor<Feature> cursor(Query q) throws IOException { if (q.getMode() == Mode.APPEND) { return new MongoCursor(q.getMode(), null, this); }/*from w w w . j a v a2 s . co m*/ QueryPlan qp = new QueryPlan(q); //TODO: sorting DBCursor dbCursor = !Envelopes.isNull(q.getBounds()) ? dbcol.find(encodeBboxQuery(q.getBounds())) : dbcol.find(); qp.bounded(); Integer offset = q.getOffset(); if (offset != null) { dbCursor.skip(offset); qp.offsetted(); } Integer limit = q.getLimit(); if (limit != null) { dbCursor.limit(limit); qp.limited(); } return qp.apply(new MongoCursor(q.getMode(), dbCursor, this)); }
From source file:org.jongo.Find.java
License:Apache License
public Find skip(final int skip) { this.modifiers.add(new QueryModifier() { public void modify(DBCursor cursor) { cursor.skip(skip); }/* w w w. ja v a 2 s. c o m*/ }); return this; }
From source file:org.mule.module.mongo.api.MongoClientImpl.java
License:Open Source License
@Override public Iterable<DBObject> findObjects(@NotNull final String collection, final DBObject query, final List<String> fields, final Integer numToSkip, final Integer limit, DBObject sortBy) { Validate.notNull(collection);/*from w w w .j av a2 s . com*/ DBCursor dbCursor = db.getCollection(collection).find(query, FieldsSet.from(fields)); if (numToSkip != null) { dbCursor = dbCursor.skip(numToSkip); } if (limit != null) { dbCursor = dbCursor.limit(limit); } if (sortBy != null) { dbCursor.sort(sortBy); } return bug5588Workaround(dbCursor); }
From source file:org.netbeans.modules.mongodb.ui.windows.collectionview.CollectionQueryResult.java
License:Open Source License
private DBCursor getPageCursor(DBCursor queryCursor) { if (pageSize > 0) { final int toSkip = (page - 1) * pageSize; return queryCursor.skip(toSkip).limit(pageSize); }/*from www . j a v a 2 s .co m*/ return queryCursor; }
From source file:org.restheart.db.DBCursorPool.java
License:Open Source License
private void populateCacheLinear(DBCursorPoolEntryKey key) { if (key.getSkipped() < SKIP_SLICE_LINEAR_WIDTH) { return;/* w ww. j a v a 2 s .c om*/ } int firstSlice = key.getSkipped() / SKIP_SLICE_LINEAR_WIDTH; executor.submit(() -> { int slice = firstSlice; for (int tohave : SKIP_SLICES_HEIGHTS) { int sliceSkips = slice * SKIP_SLICE_LINEAR_WIDTH - SKIP_SLICE_LINEAR_DELTA; DBCursorPoolEntryKey sliceKey = new DBCursorPoolEntryKey(key.getCollection(), key.getSort(), key.getFilter(), sliceSkips, -1); long existing = getSliceHeight(sliceKey); for (long cont = tohave - existing; cont > 0; cont--) { DBCursor cursor = dbsDAO.getCollectionDBCursor(key.getCollection(), key.getSort(), key.getFilter()); cursor.skip(sliceSkips); DBCursorPoolEntryKey newkey = new DBCursorPoolEntryKey(key.getCollection(), key.getSort(), key.getFilter(), sliceSkips, System.nanoTime()); cache.put(newkey, cursor); LOGGER.debug("created new cursor in pool: {}", newkey); } slice++; } }); }
From source file:org.restheart.db.DBCursorPool.java
License:Open Source License
private void populateCacheRandom(DBCursorPoolEntryKey key) { executor.submit(() -> {/*from w w w. ja va 2s .com*/ Long size = collSizes.getLoading(key).get(); int sliceWidht; int slices = 0; int totalSlices = size.intValue() / SKIP_SLICE_RND_MIN_WIDTH + 1; if (totalSlices <= SKIP_SLICE_RND_MAX_CURSORS) { slices = totalSlices; sliceWidht = SKIP_SLICE_RND_MIN_WIDTH; } else { slices = SKIP_SLICE_RND_MAX_CURSORS; sliceWidht = size.intValue() / slices; } for (int slice = 1; slice < slices; slice++) { int sliceSkips = (int) slice * sliceWidht; DBCursorPoolEntryKey sliceKey = new DBCursorPoolEntryKey(key.getCollection(), key.getSort(), key.getFilter(), sliceSkips, -1); long existing = getSliceHeight(sliceKey); for (long cont = 1 - existing; cont > 0; cont--) { DBCursor cursor = dbsDAO.getCollectionDBCursor(key.getCollection(), key.getSort(), key.getFilter()); cursor.skip(sliceSkips); DBCursorPoolEntryKey newkey = new DBCursorPoolEntryKey(key.getCollection(), key.getSort(), key.getFilter(), sliceSkips, System.nanoTime()); cache.put(newkey, cursor); LOGGER.debug("created new cursor in pool: {}", newkey); } } }); }
From source file:org.s1.mongodb.MongoDBQueryHelper.java
License:Apache License
/** * * @param c/*www .ja va 2 s . c o m*/ * @param search * @param sort * @param fields * @param skip * @param max * @return */ public static List<Map<String, Object>> list(CollectionId c, Map<String, Object> search, Map<String, Object> sort, Map<String, Object> fields, int skip, int max) { List<Map<String, Object>> res = Objects.newArrayList(); DBCollection coll = MongoDBConnectionHelper.getCollection(c); if (search == null) search = Objects.newHashMap(); DBCursor cur = coll.find(MongoDBFormat.fromMap(search), MongoDBFormat.fromMap(fields)); if (max > 0) cur.limit(max); if (skip >= 0) cur.skip(skip); if (sort != null) { cur = cur.sort(MongoDBFormat.fromMap(sort)); } while (cur.hasNext()) { DBObject obj = cur.next(); Map<String, Object> m = MongoDBFormat.toMap(obj); res.add(m); } if (LOG.isDebugEnabled()) LOG.debug("MongoDB list result (" + c + ", search:" + search + ", sort:" + sort + ", fields:" + fields + ", max:" + max + ", skip:" + skip + "\n\t> " + res); return res; }
From source file:org.springframework.data.document.mongodb.MongoTemplate.java
License:Apache License
public <T> List<T> find(String collectionName, final Query query, Class<T> targetClass) { CursorPreparer cursorPreparer = null; if (query.getSkip() > 0 || query.getLimit() > 0 || query.getSortObject() != null) { cursorPreparer = new CursorPreparer() { public DBCursor prepare(DBCursor cursor) { DBCursor cursorToUse = cursor; try { if (query.getSkip() > 0) { cursorToUse = cursorToUse.skip(query.getSkip()); }// w ww .ja v a 2s .c o m if (query.getLimit() > 0) { cursorToUse = cursorToUse.limit(query.getLimit()); } if (query.getSortObject() != null) { cursorToUse = cursorToUse.sort(query.getSortObject()); } } catch (RuntimeException e) { throw potentiallyConvertRuntimeException(e); } return cursorToUse; } }; } return doFind(collectionName, query.getQueryObject(), query.getFieldsObject(), targetClass, cursorPreparer); }
From source file:org.springframework.datastore.mapping.mongo.query.MongoQuery.java
License:Apache License
@Override protected List executeQuery(final PersistentEntity entity, final Junction criteria) { final MongoTemplate template = mongoSession.getMongoTemplate(entity); return template.execute(new DbCallback<List>() { @Override/*from w ww . j ava 2s . c o m*/ public List doInDB(DB db) throws MongoException, DataAccessException { final DBCollection collection = db.getCollection(mongoEntityPersister.getCollectionName(entity)); if (uniqueResult) { final DBObject dbObject; if (criteria.isEmpty()) { if (entity.isRoot()) { dbObject = collection.findOne(); } else { DBObject query = new BasicDBObject(MongoEntityPersister.MONGO_CLASS_FIELD, entity.getDiscriminator()); dbObject = collection.findOne(query); } } else { DBObject query = getMongoQuery(); dbObject = collection.findOne(query); } final Object object = createObjectFromDBObject(dbObject); return wrapObjectResultInList(object); } else { DBCursor cursor; DBObject query = createQueryObject(entity); final List<Projection> projectionList = projections().getProjectionList(); if (projectionList.isEmpty()) { cursor = executeQuery(entity, criteria, collection, query); return new MongoResultList(cursor, mongoEntityPersister); } else { List projectedResults = new ArrayList(); for (Projection projection : projectionList) { if (projection instanceof CountProjection) { // For some reason the below doesn't return the expected result whilst executing the query and returning the cursor does //projectedResults.add(collection.getCount(query)); cursor = executeQuery(entity, criteria, collection, query); projectedResults.add(cursor.size()); } else if (projection instanceof MinProjection) { cursor = executeQuery(entity, criteria, collection, query); MinProjection mp = (MinProjection) projection; MongoResultList results = new MongoResultList(cursor, mongoEntityPersister); projectedResults.add( manualProjections.min((Collection) results.clone(), mp.getPropertyName())); } else if (projection instanceof MaxProjection) { cursor = executeQuery(entity, criteria, collection, query); MaxProjection mp = (MaxProjection) projection; MongoResultList results = new MongoResultList(cursor, mongoEntityPersister); projectedResults.add( manualProjections.max((Collection) results.clone(), mp.getPropertyName())); } else if ((projection instanceof PropertyProjection) || (projection instanceof IdProjection)) { final PersistentProperty persistentProperty; final String propertyName; if (projection instanceof IdProjection) { persistentProperty = entity.getIdentity(); propertyName = MongoEntityPersister.MONGO_ID_FIELD; } else { PropertyProjection pp = (PropertyProjection) projection; persistentProperty = entity.getPropertyByName(pp.getPropertyName()); propertyName = pp.getPropertyName(); } if (persistentProperty != null) { populateMongoQuery(entity, query, criteria); List propertyResults = collection.distinct(propertyName, query); if (persistentProperty instanceof ToOne) { Association a = (Association) persistentProperty; propertyResults = session.retrieveAll( a.getAssociatedEntity().getJavaClass(), propertyResults); } if (projectedResults.size() == 0 && projectionList.size() == 1) { return propertyResults; } else { projectedResults.add(propertyResults); } } else { throw new InvalidDataAccessResourceUsageException( "Cannot use [" + projection.getClass().getSimpleName() + "] projection on non-existent property: " + propertyName); } } } return projectedResults; } } } protected DBCursor executeQuery(final PersistentEntity entity, final Junction criteria, final DBCollection collection, DBObject query) { final DBCursor cursor; if (criteria.isEmpty()) { cursor = executeQueryAndApplyPagination(collection, query); } else { populateMongoQuery(entity, query, criteria); cursor = executeQueryAndApplyPagination(collection, query); } return cursor; } protected DBCursor executeQueryAndApplyPagination(final DBCollection collection, DBObject query) { final DBCursor cursor; cursor = collection.find(query); if (offset > 0) { cursor.skip(offset); } if (max > -1) { cursor.limit(max); } for (Order order : orderBy) { DBObject orderObject = new BasicDBObject(); orderObject.put(order.getProperty(), order.getDirection() == Order.Direction.DESC ? -1 : 1); cursor.sort(orderObject); } return cursor; } }); }