Example usage for com.mongodb QueryBuilder get

List of usage examples for com.mongodb QueryBuilder get

Introduction

In this page you can find the example usage for com.mongodb QueryBuilder get.

Prototype

public DBObject get() 

Source Link

Document

Creates a DBObject query to be used for the driver's find operations

Usage

From source file:org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore.java

License:Apache License

@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();/*  ww w .  java2  s  .  c  o  m*/
    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) {
            // some documents may still have been updated
            // invalidate all documents affected by this update call
            for (String k : keys) {
                nodesCache.invalidate(k);
            }
            throw DocumentStoreException.convert(e);
        }
    } finally {
        stats.doneUpdate(watch.elapsed(TimeUnit.NANOSECONDS), collection, keys.size());
    }
}

From source file:org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore.java

License:Apache License

/**
 * 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
 *///from   w w  w.  ja  va 2 s  .c  o  m
@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;
}

From source file:org.apache.jackrabbit.oak.plugins.document.mongo.MongoMissingLastRevSeeker.java

License:Apache License

@Override
public boolean isRecoveryNeeded() {
    QueryBuilder query = start(ClusterNodeInfo.STATE).is(ClusterNodeInfo.ClusterNodeState.ACTIVE.name())
            .put(ClusterNodeInfo.LEASE_END_KEY).lessThan(clock.getTime());

    return getClusterNodeCollection().findOne(query.get()) != null;
}

From source file:org.apache.jackrabbit.oak.plugins.document.mongo.MongoVersionGCSupport.java

License:Apache License

private DBObject createQuery(Set<SplitDocType> gcTypes, long oldestRevTimeStamp) {
    //OR condition has to be first as we have a index for that
    //((type == DEFAULT_NO_CHILD || type == PROP_COMMIT_ONLY ..) && _sdMaxRevTime < oldestRevTimeStamp(in secs)
    QueryBuilder orClause = start();
    for (SplitDocType type : gcTypes) {
        orClause.or(start(NodeDocument.SD_TYPE).is(type.typeCode()).get());
    }/*from w  w  w.j a va 2  s.co  m*/
    return start().and(orClause.get(), start(NodeDocument.SD_MAX_REV_TIME_IN_SECS)
            .lessThan(NodeDocument.getModifiedInSecs(oldestRevTimeStamp)).get()).get();
}

From source file:org.apache.rya.indexing.geotemporal.mongo.GeoTemporalMongoDBStorageStrategy.java

License:Apache License

public DBObject getFilterQuery(final Collection<IndexingExpr> geoFilters,
        final Collection<IndexingExpr> temporalFilters) throws GeoTemporalIndexException {
    final QueryBuilder builder = QueryBuilder.start();

    if (!geoFilters.isEmpty()) {
        final DBObject[] geo = getGeoObjs(geoFilters);
        if (!temporalFilters.isEmpty()) {
            final DBObject[] temporal = getTemporalObjs(temporalFilters);
            builder.and(oneOrAnd(geo), oneOrAnd(temporal));
            return builder.get();
        } else {//from   ww  w . j  a  v  a2 s.c om
            return oneOrAnd(geo);
        }
    } else if (!temporalFilters.isEmpty()) {
        final DBObject[] temporal = getTemporalObjs(temporalFilters);
        return oneOrAnd(temporal);
    } else {
        return builder.get();
    }
}

From source file:org.apache.rya.indexing.mongodb.freetext.MongoFreeTextIndexer.java

License:Apache License

@Override
public CloseableIteration<Statement, QueryEvaluationException> queryText(final String query,
        final StatementConstraints constraints) throws IOException {
    final QueryBuilder qb = QueryBuilder.start().text(query);
    return withConstraints(constraints, qb.get());
}

From source file:org.apache.rya.indexing.mongodb.IndexingMongoDBStorageStrategy.java

License:Apache License

public DBObject getQuery(final StatementConstraints contraints) {
    final QueryBuilder queryBuilder = QueryBuilder.start();
    if (contraints.hasSubject()) {
        queryBuilder.and(new BasicDBObject(SUBJECT, contraints.getSubject().toString()));
    }//w  w  w  . j  a v a  2s .c  o  m

    if (contraints.hasPredicates()) {
        final Set<URI> predicates = contraints.getPredicates();
        if (predicates.size() > 1) {
            for (final URI pred : predicates) {
                final DBObject currentPred = new BasicDBObject(PREDICATE, pred.toString());
                queryBuilder.or(currentPred);
            }
        } else if (!predicates.isEmpty()) {
            queryBuilder.and(new BasicDBObject(PREDICATE, predicates.iterator().next().toString()));
        }
    }
    if (contraints.hasContext()) {
        queryBuilder.and(new BasicDBObject(CONTEXT, contraints.getContext().toString()));
    }
    return queryBuilder.get();
}

From source file:org.apache.rya.indexing.mongodb.temporal.MongoTemporalIndexer.java

License:Apache License

@Override
public CloseableIteration<Statement, QueryEvaluationException> queryInstantEqualsInstant(
        final TemporalInstant queryInstant, final StatementConstraints constraints)
        throws QueryEvaluationException {
    final QueryBuilder qb = QueryBuilder.start(INSTANT).is(queryInstant.getAsDateTime().toDate());
    return withConstraints(constraints, qb.get());
}

From source file:org.apache.rya.indexing.mongodb.temporal.MongoTemporalIndexer.java

License:Apache License

@Override
public CloseableIteration<Statement, QueryEvaluationException> queryInstantBeforeInstant(
        final TemporalInstant queryInstant, final StatementConstraints constraints)
        throws QueryEvaluationException {
    final QueryBuilder qb = QueryBuilder.start(INSTANT).lessThan(queryInstant.getAsDateTime().toDate());
    return withConstraints(constraints, qb.get());
}

From source file:org.apache.rya.indexing.mongodb.temporal.MongoTemporalIndexer.java

License:Apache License

@Override
public CloseableIteration<Statement, QueryEvaluationException> queryInstantAfterInstant(
        final TemporalInstant queryInstant, final StatementConstraints constraints)
        throws QueryEvaluationException {
    final QueryBuilder qb = QueryBuilder.start(INSTANT).greaterThan(queryInstant.getAsDateTime().toDate());
    return withConstraints(constraints, qb.get());
}