List of usage examples for com.mongodb QueryBuilder and
@SuppressWarnings("unchecked") public QueryBuilder and(final DBObject... ands)
From source file:org.alfresco.bm.test.mongo.MongoTestDAO.java
License:Open Source License
/** * Get registered test drivers/*from w ww . j av a 2 s . c om*/ * * @param release * the release name of the test or <tt>null</tt> for all releases * @param schema * the schema number of the driver or <tt>null</tt> for all schemas * @param liveOnly * <tt>true</tt> to retrieve only live instances */ public DBCursor getDrivers(String release, Integer schema, boolean active) { QueryBuilder queryBuilder = QueryBuilder.start(); if (release != null) { queryBuilder.and(FIELD_RELEASE).is(release); } if (schema != null) { queryBuilder.and(FIELD_SCHEMA).is(schema); } if (active) { queryBuilder.and(FIELD_PING + "." + FIELD_EXPIRES).greaterThan(new Date()); } DBObject queryObj = queryBuilder.get(); DBObject sortObj = BasicDBObjectBuilder.start().add(FIELD_RELEASE, 1).add(FIELD_SCHEMA, 1).get(); DBCursor cursor = testDrivers.find(queryObj).sort(sortObj); // Done if (logger.isDebugEnabled()) { logger.debug("Retrieved test driver: \n" + " Release: " + release + "\n" + " Schema: " + schema + "\n" + " active: " + active + "\n" + " Results: " + cursor.count()); } return cursor; }
From source file:org.alfresco.bm.test.mongo.MongoTestDAO.java
License:Open Source License
/** * Count registered drivers/*w w w . ja va2 s . c o m*/ * * @param release * the release name of the test or <tt>null</tt> for all releases * @param schema * the schema number of the driver or <tt>null</tt> for all schemas * @param liveOnly * <tt>true</tt> to retrieve only live instances * @return a count of the number of drivers matching the criteria */ public long countDrivers(String release, Integer schema, boolean active) { QueryBuilder queryBuilder = QueryBuilder.start(); if (release != null) { queryBuilder.and(FIELD_RELEASE).is(release); } if (schema != null) { queryBuilder.and(FIELD_SCHEMA).is(schema); } if (active) { queryBuilder.and(FIELD_PING + "." + FIELD_EXPIRES).greaterThan(new Date()); } DBObject queryObj = queryBuilder.get(); long count = testDrivers.count(queryObj); // Done if (logger.isDebugEnabled()) { logger.debug("Retrieved test driver: \n" + " Release: " + release + "\n" + " Schema: " + schema + "\n" + " active: " + active + "\n" + " Results: " + count); } return count; }
From source file:org.alfresco.contentstore.dao.mongo.MongoContentDAO.java
License:Open Source License
@Override public void updateNode(NodeInfo nodeInfo) { String cacheServerId = serverIdentity.getId(); String nodeId = nodeInfo.getNode().getNodeId(); // String versionLabel = nodeInfo.getNode().getVersionLabel(); Long nodeInternalId = nodeInfo.getNode().getNodeInternalId(); Long nodeVersion = nodeInfo.getNode().getNodeVersion(); MimeType mimeType = nodeInfo.getMimeType(); Long size = nodeInfo.getSize(); String nodePath = nodeInfo.getNode().getNodePath(); String contentPath = nodeInfo.getContentPath(); boolean isPrimary = nodeInfo.isPrimary(); QueryBuilder queryBuilder = QueryBuilder.start("e").is(cacheServerId).and("n").is(nodeId); if (nodeVersion != null) { queryBuilder.and("v").is(nodeVersion); }/* ww w . j a v a 2s . com*/ if (nodeInternalId != null) { queryBuilder.and("ni").is(nodeInternalId); } DBObject query = queryBuilder.get(); BasicDBObjectBuilder builder = BasicDBObjectBuilder.start("$set", BasicDBObjectBuilder.start("m", mimeType.getMimetype()).add("s", size).add("ni", nodeInternalId) .add("c", contentPath).add("p", nodePath).add("pri", isPrimary).get()); DBObject update = builder.get(); WriteResult result = contentData.update(query, update, true, false); checkResult(result); }
From source file:org.alfresco.semantics.MongoSemantics.java
License:Open Source License
public List<Relation> relationsFrom(String fromId, Set<String> categories, int skip, int maxItems) { List<Relation> ret = new LinkedList<>(); QueryBuilder queryBuilder = QueryBuilder.start("f").is(fromId); if (categories != null && categories.size() > 0) { queryBuilder.and("c").in(categories); }// ww w . j ava2 s .c o m DBObject query = queryBuilder.get(); BasicDBObjectBuilder orderByBuilder = BasicDBObjectBuilder.start("t", 1).add("w", 1); DBObject orderBy = orderByBuilder.get(); DBCursor cursor = relations.find(query).sort(orderBy).skip(skip).limit(maxItems); try { for (DBObject dbObject : cursor) { String toId = (String) dbObject.get("t"); double weight = (Double) dbObject.get("w"); Relation r = new Relation(fromId, toId, weight); ret.add(r); } } finally { if (cursor != null) { cursor.close(); } } return ret; }
From source file:org.alfresco.semantics.MongoSemantics.java
License:Open Source License
public List<Relation> relationsTo(String toId, Set<String> categories, int skip, int maxItems) { List<Relation> ret = new LinkedList<>(); QueryBuilder queryBuilder = QueryBuilder.start("t").is(toId); if (categories != null && categories.size() > 0) { queryBuilder.and("c").in(categories); }/*from www .ja v a2s . c om*/ DBObject query = queryBuilder.get(); BasicDBObjectBuilder orderByBuilder = BasicDBObjectBuilder.start("f", 1).add("w", 1); DBObject orderBy = orderByBuilder.get(); DBCursor cursor = relations.find(query).sort(orderBy).skip(skip).limit(maxItems); try { for (DBObject dbObject : cursor) { String fromId = (String) dbObject.get("f"); double weight = (Double) dbObject.get("w"); Relation r = new Relation(fromId, toId, weight); ret.add(r); } } finally { if (cursor != null) { cursor.close(); } } return ret; }
From source file:org.apache.jackrabbit.oak.plugins.document.mongo.MongoBlobStore.java
License:Apache License
private static DBObject getBlobQuery(String id, long lastMod) { QueryBuilder queryBuilder = new QueryBuilder(); if (id != null) { queryBuilder = queryBuilder.and(MongoBlob.KEY_ID).is(id); }/*from ww w.j av a 2 s . c o m*/ if (lastMod > 0) { queryBuilder = queryBuilder.and(MongoBlob.KEY_LAST_MOD).lessThan(lastMod); } return queryBuilder.get(); }
From source file:org.apache.jackrabbit.oak.plugins.document.mongo.MongoBlobStore.java
License:Apache License
@Override public long countDeleteChunks(List<String> chunkIds, long maxLastModifiedTime) throws Exception { DBCollection collection = getBlobCollection(); QueryBuilder queryBuilder = new QueryBuilder(); if (chunkIds != null) { queryBuilder = queryBuilder.and(MongoBlob.KEY_ID).in(chunkIds.toArray(new String[0])); if (maxLastModifiedTime > 0) { queryBuilder = queryBuilder.and(MongoBlob.KEY_LAST_MOD).lessThan(maxLastModifiedTime); }//from w ww. j a v a 2 s .com } WriteResult result = collection.remove(queryBuilder.get()); return result.getN(); }
From source file:org.apache.jackrabbit.oak.plugins.document.mongo.MongoBlobStore.java
License:Apache License
@Override public Iterator<String> getAllChunkIds(long maxLastModifiedTime) throws Exception { DBCollection collection = getBlobCollection(); DBObject fields = new BasicDBObject(); fields.put(MongoBlob.KEY_ID, 1);// w w w .ja v a 2 s . co m QueryBuilder builder = new QueryBuilder(); if (maxLastModifiedTime != 0 && maxLastModifiedTime != -1) { builder.and(MongoBlob.KEY_LAST_MOD).lessThanEquals(maxLastModifiedTime); } final DBCursor cur = collection.find(builder.get(), fields).hint(fields) .addOption(Bytes.QUERYOPTION_SLAVEOK); //TODO The cursor needs to be closed return new AbstractIterator<String>() { @Override protected String computeNext() { if (cur.hasNext()) { MongoBlob blob = (MongoBlob) cur.next(); if (blob != null) { return blob.getId(); } } return endOfData(); } }; }
From source file:org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore.java
License:Apache License
@SuppressWarnings("unchecked") @Nonnull/*from w w w .j a v a 2 s . co m*/ <T extends Document> List<T> queryInternal(Collection<T> collection, String fromKey, String toKey, String indexedProperty, long startValue, int limit, long maxQueryTime) { log("query", fromKey, toKey, indexedProperty, startValue, limit); DBCollection dbCollection = getDBCollection(collection); QueryBuilder queryBuilder = QueryBuilder.start(Document.ID); queryBuilder.greaterThan(fromKey); queryBuilder.lessThan(toKey); DBObject hint = new BasicDBObject(NodeDocument.ID, 1); if (indexedProperty != null) { if (NodeDocument.DELETED_ONCE.equals(indexedProperty)) { if (startValue != 1) { throw new DocumentStoreException("unsupported value for property " + NodeDocument.DELETED_ONCE); } queryBuilder.and(indexedProperty); queryBuilder.is(true); } else { queryBuilder.and(indexedProperty); queryBuilder.greaterThanEquals(startValue); if (NodeDocument.MODIFIED_IN_SECS.equals(indexedProperty) && canUseModifiedTimeIdx(startValue)) { hint = new BasicDBObject(NodeDocument.MODIFIED_IN_SECS, -1); } } } DBObject query = queryBuilder.get(); String parentId = Utils.getParentIdFromLowerLimit(fromKey); long lockTime = -1; final Stopwatch watch = startWatch(); boolean isSlaveOk = false; int resultSize = 0; CacheChangesTracker cacheChangesTracker = null; if (parentId != null && collection == Collection.NODES) { cacheChangesTracker = nodesCache.registerTracker(fromKey, toKey); } try { DBCursor cursor = dbCollection.find(query).sort(BY_ID_ASC); if (!disableIndexHint && !hasModifiedIdCompoundIndex) { cursor.hint(hint); } if (maxQueryTime > 0) { // OAK-2614: set maxTime if maxQueryTimeMS > 0 cursor.maxTime(maxQueryTime, TimeUnit.MILLISECONDS); } ReadPreference readPreference = getMongoReadPreference(collection, parentId, null, getDefaultReadPreference(collection)); if (readPreference.isSlaveOk()) { isSlaveOk = true; LOG.trace("Routing call to secondary for fetching children from [{}] to [{}]", fromKey, toKey); } cursor.setReadPreference(readPreference); List<T> list; try { list = new ArrayList<T>(); for (int i = 0; i < limit && cursor.hasNext(); i++) { DBObject o = cursor.next(); T doc = convertFromDBObject(collection, o); list.add(doc); } resultSize = list.size(); } finally { cursor.close(); } if (cacheChangesTracker != null) { nodesCache.putNonConflictingDocs(cacheChangesTracker, (List<NodeDocument>) list); } return list; } finally { if (cacheChangesTracker != null) { cacheChangesTracker.close(); } stats.doneQuery(watch.elapsed(TimeUnit.NANOSECONDS), collection, fromKey, toKey, indexedProperty != null, resultSize, lockTime, isSlaveOk); } }
From source file:org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore.java
License:Apache License
@SuppressWarnings("unchecked") @CheckForNull/*www. j a v a 2s .c o m*/ private <T extends Document> T findAndModify(Collection<T> collection, UpdateOp updateOp, boolean upsert, boolean checkConditions) { DBCollection dbCollection = getDBCollection(collection); // make sure we don't modify the original updateOp updateOp = updateOp.copy(); DBObject update = createUpdate(updateOp, false); Lock lock = null; if (collection == Collection.NODES) { lock = nodeLocks.acquire(updateOp.getId()); } final Stopwatch watch = startWatch(); boolean newEntry = false; try { // get modCount of cached document Long modCount = null; T cachedDoc = null; if (collection == Collection.NODES) { cachedDoc = (T) nodesCache.getIfPresent(updateOp.getId()); if (cachedDoc != null) { modCount = cachedDoc.getModCount(); } } // perform a conditional update with limited result // if we have a matching modCount if (modCount != null) { QueryBuilder query = createQueryForUpdate(updateOp.getId(), updateOp.getConditions()); query.and(Document.MOD_COUNT).is(modCount); WriteResult result = dbCollection.update(query.get(), update); if (result.getN() > 0) { // success, update cached document if (collection == Collection.NODES) { NodeDocument newDoc = (NodeDocument) applyChanges(collection, cachedDoc, updateOp); nodesCache.put(newDoc); } // return previously cached document return cachedDoc; } } // conditional update failed or not possible // perform operation and get complete document QueryBuilder query = createQueryForUpdate(updateOp.getId(), updateOp.getConditions()); DBObject oldNode = dbCollection.findAndModify(query.get(), null, null /*sort*/, false /*remove*/, update, false /*returnNew*/, upsert); if (oldNode == null) { newEntry = true; } if (checkConditions && oldNode == null) { return null; } T oldDoc = convertFromDBObject(collection, oldNode); if (oldDoc != null) { if (collection == Collection.NODES) { NodeDocument newDoc = (NodeDocument) applyChanges(collection, oldDoc, updateOp); nodesCache.put(newDoc); updateLocalChanges(newDoc); } oldDoc.seal(); } else if (upsert) { if (collection == Collection.NODES) { NodeDocument doc = (NodeDocument) collection.newDocument(this); UpdateUtils.applyChanges(doc, updateOp); nodesCache.putIfAbsent(doc); updateLocalChanges(doc); } } else { // updateOp without conditions and not an upsert // this means the document does not exist } return oldDoc; } catch (Exception e) { throw DocumentStoreException.convert(e); } finally { if (lock != null) { lock.unlock(); } stats.doneFindAndModify(watch.elapsed(TimeUnit.NANOSECONDS), collection, updateOp.getId(), newEntry, true, 0); } }