use of com.mongodb.QueryBuilder in project gora by apache.
the class DefaultFactory method transformMapFilter.
protected DBObject transformMapFilter(final MapFieldValueFilter<K, T> mapFilter, final MongoStore<K, T> store) {
MongoMapping mapping = store.getMapping();
String dbFieldName = mapping.getDocumentField(mapFilter.getFieldName()) + "." + store.encodeFieldKey(mapFilter.getMapKey().toString());
FilterOp filterOp = mapFilter.getFilterOp();
List<Object> operands = mapFilter.getOperands();
QueryBuilder builder = QueryBuilder.start(dbFieldName);
builder = appendToBuilder(builder, filterOp, operands);
if (!mapFilter.isFilterIfMissing()) {
// If false, the find query will pass if the column is not found.
DBObject notExist = QueryBuilder.start(dbFieldName).exists(false).get();
builder = QueryBuilder.start().or(notExist, builder.get());
}
return builder.get();
}
use of com.mongodb.QueryBuilder in project jackrabbit-oak by apache.
the class MongoDocumentStore method createQueryForUpdate.
@Nonnull
private static QueryBuilder createQueryForUpdate(String key, Map<Key, Condition> conditions) {
QueryBuilder query = getByKeyQuery(key);
for (Entry<Key, Condition> entry : conditions.entrySet()) {
Key k = entry.getKey();
Condition c = entry.getValue();
switch(c.type) {
case EXISTS:
query.and(k.toString()).exists(c.value);
break;
case EQUALS:
query.and(k.toString()).is(c.value);
break;
case NOTEQUALS:
query.and(k.toString()).notEquals(c.value);
break;
}
}
return query;
}
use of com.mongodb.QueryBuilder in project jackrabbit-oak by apache.
the class MongoDocumentStore method update.
@Override
public <T extends Document> void update(Collection<T> collection, List<String> keys, UpdateOp updateOp) {
log("update", keys, updateOp);
UpdateUtils.assertUnconditional(updateOp);
DBCollection dbCollection = getDBCollection(collection);
QueryBuilder query = QueryBuilder.start(Document.ID).in(keys);
// make sure we don't modify the original updateOp
updateOp = updateOp.copy();
DBObject update = createUpdate(updateOp, false);
final Stopwatch watch = startWatch();
try {
Map<String, NodeDocument> cachedDocs = Collections.emptyMap();
if (collection == Collection.NODES) {
cachedDocs = Maps.newHashMap();
for (String key : keys) {
cachedDocs.put(key, nodesCache.getIfPresent(key));
}
}
try {
dbCollection.update(query.get(), update, false, true);
if (collection == Collection.NODES) {
Map<String, ModificationStamp> modCounts = getModStamps(filterValues(cachedDocs, notNull()).keySet());
// update cache
for (Entry<String, NodeDocument> entry : cachedDocs.entrySet()) {
// the cachedDocs is not empty, so the collection = NODES
Lock lock = nodeLocks.acquire(entry.getKey());
try {
ModificationStamp postUpdateModStamp = modCounts.get(entry.getKey());
if (postUpdateModStamp != null && entry.getValue() != null && entry.getValue() != NodeDocument.NULL && Long.valueOf(postUpdateModStamp.modCount - 1).equals(entry.getValue().getModCount())) {
// post update modCount is one higher than
// what we currently see in the cache. we can
// replace the cached document
NodeDocument newDoc = applyChanges(Collection.NODES, entry.getValue(), updateOp.shallowCopy(entry.getKey()));
nodesCache.replaceCachedDocument(entry.getValue(), newDoc);
} else {
// make sure concurrently loaded document is
// invalidated
nodesCache.invalidate(entry.getKey());
}
} finally {
lock.unlock();
}
}
}
} catch (MongoException e) {
throw handleException(e, collection, keys);
}
} finally {
stats.doneUpdate(watch.elapsed(TimeUnit.NANOSECONDS), collection, keys.size());
}
}
use of com.mongodb.QueryBuilder in project jackrabbit-oak by apache.
the class MongoDocumentStore method queryInternal.
@SuppressWarnings("unchecked")
@Nonnull
<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);
}
}
use of com.mongodb.QueryBuilder in project jackrabbit-oak by apache.
the class MongoDocumentStore method getModStamps.
/**
* Returns the {@link Document#MOD_COUNT} and
* {@link NodeDocument#MODIFIED_IN_SECS} values of the documents with the
* given {@code keys}. The returned map will only contain entries for
* existing documents. The default value is -1 if the document does not have
* a modCount field. The same applies to the modified field.
*
* @param keys the keys of the documents.
* @return map with key to modification stamp mapping.
* @throws MongoException if the call fails
*/
@Nonnull
private Map<String, ModificationStamp> getModStamps(Iterable<String> keys) throws MongoException {
QueryBuilder query = QueryBuilder.start(Document.ID).in(keys);
// Fetch only the modCount and id
final BasicDBObject fields = new BasicDBObject(Document.ID, 1);
fields.put(Document.MOD_COUNT, 1);
fields.put(NodeDocument.MODIFIED_IN_SECS, 1);
DBCursor cursor = nodes.find(query.get(), fields);
cursor.setReadPreference(ReadPreference.primary());
Map<String, ModificationStamp> modCounts = Maps.newHashMap();
for (DBObject obj : cursor) {
String id = (String) obj.get(Document.ID);
Long modCount = Utils.asLong((Number) obj.get(Document.MOD_COUNT));
if (modCount == null) {
modCount = -1L;
}
Long modified = Utils.asLong((Number) obj.get(NodeDocument.MODIFIED_IN_SECS));
if (modified == null) {
modified = -1L;
}
modCounts.put(id, new ModificationStamp(modCount, modified));
}
return modCounts;
}
Aggregations