List of usage examples for com.mongodb DBCursor skip
public DBCursor skip(final int numberOfElements)
From source file:org.canedata.provider.mongodb.entity.MongoEntity.java
License:Apache License
public List<Fields> list(int offset, int count) { if (logger.isDebug()) logger.debug("Listing entities, Database is {0}, Collection is {1}, offset is {2}, count is {3}.", getSchema(), getName(), offset, count); List<Fields> rlt = new ArrayList<Fields>(); BasicDBObject options = new BasicDBObject(); DBCursor cursor = null; try {/*from w ww. ja v a2s. c o m*/ validateState(); MongoExpressionFactory expFactory = new MongoExpressionFactory.Impl(); BasicDBObject projection = new BasicDBObject(); Limiter limiter = new Limiter.Default(); BasicDBObject sorter = new BasicDBObject(); IntentParser.parse(getIntent(), expFactory, null, projection, limiter, sorter, options); if (!options.isEmpty()) prepareOptions(options); if (null != getCache()) {// cache cursor = getCollection().find(expFactory.toQuery(), new BasicDBObject().append("_id", 1)); } else {// no cache // projection if (projection.isEmpty()) cursor = getCollection().find(expFactory.toQuery()); else cursor = getCollection().find(expFactory.toQuery(), projection); } // sort if (!sorter.isEmpty()) cursor.sort(sorter); if (offset > 0) limiter.offset(offset); if (count > 0) limiter.count(count); if (limiter.offset() > 0) cursor.skip(limiter.offset()); if (limiter.count() > 0) cursor.limit(limiter.count()); if (null != getCache()) { Map<Object, MongoFields> missedCacheHits = new HashMap<Object, MongoFields>(); while (cursor.hasNext()) { BasicDBObject dbo = (BasicDBObject) cursor.next(); Object key = dbo.get("_id"); String cacheKey = getKey().concat("#").concat(key.toString()); MongoFields ele = null; if (getCache().isAlive(cacheKey)) {// load from cache MongoFields mf = (MongoFields) getCache().restore(cacheKey); if (null != mf) ele = mf.clone();// pooling } if (null != ele && !projection.isEmpty()) ele.project(projection.keySet()); if (null == ele) { ele = new MongoFields(this, getIntent()); missedCacheHits.put(key, ele); } rlt.add(ele); } // load missed cache hits. if (!missedCacheHits.isEmpty()) { loadForMissedCacheHits(missedCacheHits, projection.keySet()); missedCacheHits.clear(); } if (logger.isDebug()) logger.debug("Listed entities hit cache ..."); } else { while (cursor.hasNext()) { BasicDBObject dbo = (BasicDBObject) cursor.next(); rlt.add(new MongoFields(this, getIntent(), dbo)); } if (logger.isDebug()) logger.debug("Listed entities ..."); } return rlt; } catch (AnalyzeBehaviourException abe) { if (logger.isDebug()) logger.debug(abe, "Analyzing behaviour failure, cause by: {0}.", abe.getMessage()); throw new RuntimeException(abe); } finally { if (!options.getBoolean(Options.RETAIN)) getIntent().reset(); if (cursor != null) cursor.close(); } }
From source file:org.datanucleus.store.mongodb.MongoDBUtils.java
License:Open Source License
/** * Convenience method to return all objects of the candidate type (optionally allowing subclasses). * @param q Query// w w w . j a va 2 s .c om * @param db Mongo DB * @param filterObject Optional filter object * @param orderingObject Optional ordering object * @param options Set of options for controlling this query * @param skip Number of records to skip * @param limit Max number of records to return * @return List of all candidate objects (implements QueryResult) */ public static List getObjectsOfCandidateType(Query q, DB db, BasicDBObject filterObject, BasicDBObject orderingObject, Map<String, Object> options, Integer skip, Integer limit) { LazyLoadQueryResult qr = new LazyLoadQueryResult(q); // Find the DBCollections we need to query ExecutionContext ec = q.getExecutionContext(); StoreManager storeMgr = ec.getStoreManager(); ClassLoaderResolver clr = ec.getClassLoaderResolver(); List<AbstractClassMetaData> cmds = MetaDataUtils.getMetaDataForCandidates(q.getCandidateClass(), q.isSubclasses(), ec); Map<String, List<AbstractClassMetaData>> classesByCollectionName = new HashMap(); for (AbstractClassMetaData cmd : cmds) { if (cmd instanceof ClassMetaData && ((ClassMetaData) cmd).isAbstract()) { // Omit any classes that are not instantiable (e.g abstract) } else { String collectionName = storeMgr.getNamingFactory().getTableName(cmd); List<AbstractClassMetaData> cmdsForCollection = classesByCollectionName.get(collectionName); if (cmdsForCollection == null) { cmdsForCollection = new ArrayList(); classesByCollectionName.put(collectionName, cmdsForCollection); } cmdsForCollection.add(cmd); } } // Add a query for each DBCollection we need Iterator<Map.Entry<String, List<AbstractClassMetaData>>> iter = classesByCollectionName.entrySet() .iterator(); while (iter.hasNext()) { Map.Entry<String, List<AbstractClassMetaData>> entry = iter.next(); String collectionName = entry.getKey(); List<AbstractClassMetaData> cmdsForCollection = entry.getValue(); AbstractClassMetaData rootCmd = cmdsForCollection.get(0); int[] fpMembers = q.getFetchPlan().getFetchPlanForClass(rootCmd).getMemberNumbers(); BasicDBObject fieldsSelection = new BasicDBObject(); if (fpMembers != null && fpMembers.length > 0) { fieldsSelection = new BasicDBObject(); for (int i = 0; i < fpMembers.length; i++) { AbstractMemberMetaData mmd = rootCmd .getMetaDataForManagedMemberAtAbsolutePosition(fpMembers[i]); RelationType relationType = mmd.getRelationType(clr); if (mmd.isEmbedded() && RelationType.isRelationSingleValued(relationType)) { boolean nested = true; String nestedStr = mmd.getValueForExtension("nested"); if (nestedStr != null && nestedStr.equalsIgnoreCase("false")) { nested = false; } if (nested) { // Nested Embedded field, so include field String fieldName = storeMgr.getNamingFactory().getColumnName(mmd, ColumnType.COLUMN); fieldsSelection.append(fieldName, 1); } else { // Flat Embedded field, so add all fields of sub-objects selectAllFieldsOfEmbeddedObject(mmd, fieldsSelection, ec, clr); } } else { String fieldName = storeMgr.getNamingFactory().getColumnName(mmd, ColumnType.COLUMN); fieldsSelection.append(fieldName, 1); } } } if (rootCmd.getIdentityType() == IdentityType.DATASTORE) { fieldsSelection.append( storeMgr.getNamingFactory().getColumnName(rootCmd, ColumnType.DATASTOREID_COLUMN), 1); } if (rootCmd.isVersioned()) { VersionMetaData vermd = rootCmd.getVersionMetaDataForClass(); if (vermd.getFieldName() != null) { AbstractMemberMetaData verMmd = rootCmd.getMetaDataForMember(vermd.getFieldName()); String fieldName = storeMgr.getNamingFactory().getColumnName(verMmd, ColumnType.COLUMN); fieldsSelection.append(fieldName, 1); } else { fieldsSelection.append( storeMgr.getNamingFactory().getColumnName(rootCmd, ColumnType.VERSION_COLUMN), 1); } } if (rootCmd.hasDiscriminatorStrategy()) { fieldsSelection.append( storeMgr.getNamingFactory().getColumnName(rootCmd, ColumnType.DISCRIMINATOR_COLUMN), 1); } BasicDBObject query = new BasicDBObject(); if (filterObject != null) { Iterator<Map.Entry<String, Object>> filterEntryIter = filterObject.entrySet().iterator(); while (filterEntryIter.hasNext()) { Map.Entry<String, Object> filterEntry = filterEntryIter.next(); query.put(filterEntry.getKey(), filterEntry.getValue()); } } if (rootCmd.hasDiscriminatorStrategy() && cmdsForCollection.size() == 1) { // TODO Add this restriction on *all* possible cmds for this DBCollection // Discriminator present : Add restriction on the discriminator value for this class query.put(storeMgr.getNamingFactory().getColumnName(rootCmd, ColumnType.DISCRIMINATOR_COLUMN), rootCmd.getDiscriminatorValue()); } if (storeMgr.getStringProperty(PropertyNames.PROPERTY_TENANT_ID) != null) { // Multitenancy discriminator present : Add restriction for this tenant if ("true".equalsIgnoreCase(rootCmd.getValueForExtension("multitenancy-disable"))) { // Don't bother with multitenancy for this class } else { String fieldName = storeMgr.getNamingFactory().getColumnName(rootCmd, ColumnType.MULTITENANCY_COLUMN); String value = storeMgr.getStringProperty(PropertyNames.PROPERTY_TENANT_ID); query.put(fieldName, value); } } DBCollection dbColl = db.getCollection(collectionName); Object val = (options != null ? options.get("slave-ok") : Boolean.FALSE); if (val == Boolean.TRUE) { dbColl.setReadPreference(ReadPreference.secondaryPreferred()); } if (NucleusLogger.DATASTORE_NATIVE.isDebugEnabled()) { NucleusLogger.DATASTORE_NATIVE .debug("Performing find() using query on collection " + collectionName + " for fields=" + fieldsSelection + " with filter=" + query + " and ordering=" + orderingObject); } DBCursor curs = dbColl.find(query, fieldsSelection); if (ec.getStatistics() != null) { // Add to statistics ec.getStatistics().incrementNumReads(); } if (classesByCollectionName.size() == 1) { if (orderingObject != null) { curs = curs.sort(orderingObject); qr.setOrderProcessed(true); } // We have a single DBCursor so apply the range specification directly to this DBCursor if (skip != null && skip > 0) { curs = curs.skip(skip); qr.setRangeProcessed(true); } if (limit != null && limit > 0) { curs = curs.limit(limit); qr.setRangeProcessed(true); } } qr.addCandidateResult(rootCmd, curs, fpMembers); } return qr; }
From source file:org.eclipse.birt.data.oda.mongodb.internal.impl.MDbOperation.java
License:Open Source License
/** * Applies data set query properties and hints on DBCursor, except * for cursor limit.//from w ww . ja v a 2 s . c o m * @see #applyPropertiesToCursor(DBCursor,QueryProperties,boolean,boolean) */ static void applyPropertiesToCursor(DBCursor rowsCursor, QueryProperties queryProps, boolean includeSortExpr) { if (includeSortExpr) // normally done only when executing a query to get full result set { DBObject sortExprObj = null; try { sortExprObj = queryProps.getSortExprAsParsedObject(); } catch (OdaException ex) { // log warning and ignore DriverUtil.getLogger().log(Level.WARNING, Messages.bind("Unable to parse the user-defined Sort Expression: {0}", //$NON-NLS-1$ queryProps.getSortExpr()), ex); } if (sortExprObj != null) rowsCursor.sort(sortExprObj); } ReadPreference readPref = queryProps.getTaggableReadPreference(); if (readPref != null) rowsCursor.setReadPreference(readPref); if (queryProps.getBatchSize() > 0) rowsCursor.batchSize(queryProps.getBatchSize()); if (queryProps.getNumDocsToSkip() > 0) rowsCursor.skip(queryProps.getNumDocsToSkip()); DBObject hintObj = queryProps.getIndexHintsAsParsedObject(); if (hintObj != null) rowsCursor.hint(hintObj); else // try to pass the hint string value as is { String hintValue = queryProps.getIndexHints(); if (!hintValue.isEmpty()) rowsCursor.hint(hintValue); } if (queryProps.hasNoTimeOut()) rowsCursor.addOption(Bytes.QUERYOPTION_NOTIMEOUT); if (queryProps.isPartialResultsOk()) rowsCursor.addOption(Bytes.QUERYOPTION_PARTIAL); }
From source file:org.eclipselabs.mongoemf.streams.MongoInputStream.java
License:Open Source License
@Override public void loadResource(Resource resource) throws IOException { // We need to set up the XMLResource.URIHandler so that proxy URIs are handled properly. XMLResource.URIHandler uriHandler = (XMLResource.URIHandler) options.get(XMLResource.OPTION_URI_HANDLER); if (uriHandler == null) uriHandler = new org.eclipse.emf.ecore.xmi.impl.URIHandlerImpl(); if (resource.getURI().hasQuery()) uriHandler.setBaseURI(resource.getURI().trimSegments(1).appendSegment("-1")); else//from w w w . j a v a 2 s.c o m uriHandler.setBaseURI(resource.getURI()); boolean includeAttributesForProxyReferences = Boolean.TRUE .equals(options.get(Options.OPTION_PROXY_ATTRIBUTES)); EObjectBuilder builder = builderFactory.createObjectBuilder(converterService, uriHandler, includeAttributesForProxyReferences, eClassCache); // If the URI contains a query string, use it to locate a collection of objects from // MongoDB, otherwise simply get the object from MongoDB using the id. EList<EObject> contents = resource.getContents(); if (uri.query() != null) { if (queryEngine == null) throw new IOException("The query engine was not found"); MongoQuery mongoQuery = queryEngine.buildDBObjectQuery(uri); DBCursor resultCursor = null; if (mongoQuery.getProjection() == null) resultCursor = collection.find(mongoQuery.getFilter()); else resultCursor = collection.find(mongoQuery.getFilter(), mongoQuery.getProjection()); if (mongoQuery.getSkip() != null) resultCursor.skip(mongoQuery.getSkip()); if (mongoQuery.getSort() != null) resultCursor = resultCursor.sort(mongoQuery.getSort()); if (mongoQuery.getLimit() != null) resultCursor = resultCursor.limit(mongoQuery.getLimit()); boolean createCursor = Boolean.TRUE.equals(options.get(Options.OPTION_QUERY_CURSOR)); if (createCursor) { MongoCursor cursor = ModelFactory.eINSTANCE.createMongoCursor(); cursor.setDbCollection(collection); cursor.setDbCursor(resultCursor); cursor.setObjectBuilder(builder); contents.add(cursor); } else { EReferenceCollection eCollection = EmodelingFactory.eINSTANCE.createEReferenceCollection(); InternalEList<EObject> values = (InternalEList<EObject>) eCollection.getValues(); for (DBObject dbObject : resultCursor) values.addUnique(builder.buildEObject(collection, dbObject, resource, true)); contents.add(eCollection); } } else { DBObject dbObject = collection.findOne(new BasicDBObject(Keywords.ID_KEY, MongoUtils.getID(uri))); if (dbObject != null) { EObject eObject = builder.buildEObject(collection, dbObject, resource, false); if (eObject != null) contents.add(eObject); response.put(URIConverter.RESPONSE_TIME_STAMP_PROPERTY, dbObject.get(Keywords.TIME_STAMP_KEY)); } } }
From source file:org.envirocar.server.mongo.dao.MongoMeasurementDao.java
License:Open Source License
private Measurements query(DBObject query, Pagination p) { final Mapper mapper = this.mongoDB.getMapper(); final Datastore ds = this.mongoDB.getDatastore(); final DBCollection coll = ds.getCollection(MongoMeasurement.class); DBCursor cursor = coll.find(query, null); long count = 0; cursor.setDecoderFactory(ds.getDecoderFact()); if (p != null) { count = coll.count(query);/*from w w w . j av a 2 s. c om*/ if (p.getOffset() > 0) { cursor.skip(p.getOffset()); } if (p.getLimit() > 0) { cursor.limit(p.getLimit()); } } cursor.sort(QueryImpl.parseFieldsString(MongoMeasurement.TIME, MongoMeasurement.class, mapper, true)); Iterable<MongoMeasurement> i = new MorphiaIterator<MongoMeasurement, MongoMeasurement>(cursor, mapper, MongoMeasurement.class, coll.getName(), mapper.createEntityCache()); return createPaginatedIterable(i, p, count); }
From source file:org.fornax.cartridges.sculptor.framework.accessimpl.mongodb.MongoDbFindAllAccessImpl.java
License:Apache License
@Override public void performExecute() { List<T> foundResult = new ArrayList<T>(); DBCursor cur = getDBCollection().find(); if (orderBy != null) { cur.sort(new BasicDBObject(orderBy, orderByAsc ? 1 : -1)); }/*from ww w . ja v a2s. co m*/ if (firstResult >= 0) { cur.skip(firstResult); } if (maxResult >= 1) { cur.limit(maxResult); } for (DBObject each : cur) { T eachResult = getDataMapper().toDomain(each); foundResult.add(eachResult); } this.result = foundResult; }
From source file:org.fornax.cartridges.sculptor.framework.accessimpl.mongodb.MongoDbFindByConditionAccessImpl.java
License:Apache License
@Override public void performExecute() { DBObject query = createQuery();/*from w w w .j a va 2s .co m*/ DBCursor cur = getDBCollection().find(query); sort(cur); if (firstResult >= 0) { cur.skip(firstResult); } if (maxResult >= 1) { cur.limit(maxResult); } List<T> foundResult = new ArrayList<T>(); for (DBObject each : cur) { T eachResult = getDataMapper().toDomain(each); foundResult.add(eachResult); } this.result = foundResult; }
From source file:org.fracturedatlas.athena.apa.impl.MongoApaAdapter.java
License:Open Source License
private DBCursor setSkip(DBCursor recordsCursor, String start) { if (start != null) { try {//from w ww. ja v a 2 s .c om Integer st = Integer.parseInt(start); recordsCursor.skip(st); } catch (NumberFormatException nfe) { //ignored, no limit will be set } } return recordsCursor; }
From source file:org.grails.datastore.mapping.mongo.query.MongoQuery.java
License:Apache License
@SuppressWarnings("hiding") @Override/*w ww . j a v a2 s . co m*/ protected List executeQuery(final PersistentEntity entity, final Junction criteria) { final MongoTemplate template = mongoSession.getMongoTemplate(entity); return template.execute(new DbCallback<List>() { @SuppressWarnings("unchecked") 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 = collection.findOne(new BasicDBObject(MongoEntityPersister.MONGO_CLASS_FIELD, entity.getDiscriminator())); } } else { dbObject = collection.findOne(getMongoQuery()); } return wrapObjectResultInList(createObjectFromDBObject(dbObject)); } DBCursor cursor = null; DBObject query = createQueryObject(entity); final List<Projection> projectionList = projections().getProjectionList(); if (projectionList.isEmpty()) { cursor = executeQuery(entity, criteria, collection, query); return (List) new MongoResultList(cursor, mongoEntityPersister).clone(); } 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)); if (cursor == null) cursor = executeQuery(entity, criteria, collection, query); projectedResults.add(cursor.size()); } else if (projection instanceof MinProjection) { if (cursor == null) cursor = executeQuery(entity, criteria, collection, query); MinProjection mp = (MinProjection) projection; MongoResultList results = new MongoResultList(cursor, mongoEntityPersister); projectedResults.add(manualProjections.min((Collection) results.clone(), getPropertyName(entity, mp.getPropertyName()))); } else if (projection instanceof MaxProjection) { if (cursor == null) cursor = executeQuery(entity, criteria, collection, query); MaxProjection mp = (MaxProjection) projection; MongoResultList results = new MongoResultList(cursor, mongoEntityPersister); projectedResults.add(manualProjections.max((Collection) results.clone(), getPropertyName(entity, mp.getPropertyName()))); } else if (projection instanceof CountDistinctProjection) { if (cursor == null) cursor = executeQuery(entity, criteria, collection, query); CountDistinctProjection mp = (CountDistinctProjection) projection; MongoResultList results = new MongoResultList(cursor, mongoEntityPersister); projectedResults.add(manualProjections.countDistinct((Collection) results.clone(), getPropertyName(entity, 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 = getPropertyName(entity, persistentProperty.getName()); } if (persistentProperty != null) { populateMongoQuery(entity, query, criteria); List propertyResults = null; if (max > -1) { // if there is a limit then we have to do a manual projection since the MongoDB driver doesn't support limits and distinct together cursor = executeQueryAndApplyPagination(collection, query); propertyResults = manualProjections .property(new MongoResultList(cursor, mongoEntityPersister), propertyName); } else { 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; } 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); } if (queryArguments != null) { if (queryArguments.containsKey(HINT_ARGUMENT)) { Object hint = queryArguments.get(HINT_ARGUMENT); if (hint instanceof Map) { cursor.hint(new BasicDBObject((Map) hint)); } else if (hint != null) { cursor.hint(hint.toString()); } } } 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); } if (!orderBy.isEmpty()) { DBObject orderObject = new BasicDBObject(); for (Order order : orderBy) { String property = order.getProperty(); property = getPropertyName(entity, property); orderObject.put(property, order.getDirection() == Order.Direction.DESC ? -1 : 1); } cursor.sort(orderObject); } else { MongoCollection coll = (MongoCollection) entity.getMapping().getMappedForm(); if (coll != null && coll.getSort() != null) { DBObject orderObject = new BasicDBObject(); Order order = coll.getSort(); String property = order.getProperty(); property = getPropertyName(entity, property); orderObject.put(property, order.getDirection() == Order.Direction.DESC ? -1 : 1); cursor.sort(orderObject); } } return cursor; } }); }
From source file:org.hibernate.ogm.datastore.mongodb.MongoDBDialect.java
License:LGPL
private ClosableIterator<Tuple> doFind(MongoDBQueryDescriptor query, QueryParameters queryParameters, DBCollection collection, EntityKeyMetadata entityKeyMetadata) { DBCursor cursor = collection.find(query.getCriteria(), query.getProjection()); if (query.getOrderBy() != null) { cursor.sort(query.getOrderBy()); }/* w w w. ja va2 s . c om*/ // apply firstRow/maxRows if present if (queryParameters.getRowSelection().getFirstRow() != null) { cursor.skip(queryParameters.getRowSelection().getFirstRow()); } if (queryParameters.getRowSelection().getMaxRows() != null) { cursor.limit(queryParameters.getRowSelection().getMaxRows()); } return new MongoDBResultsCursor(cursor, entityKeyMetadata); }