Example usage for com.mongodb.client MongoCollection find

List of usage examples for com.mongodb.client MongoCollection find

Introduction

In this page you can find the example usage for com.mongodb.client MongoCollection find.

Prototype

FindIterable<TDocument> find(ClientSession clientSession);

Source Link

Document

Finds all documents in the collection.

Usage

From source file:org.restheart.db.DatabaseImpl.java

License:Open Source License

/**
 *
 * @param cs the client session//from  ww  w.  ja  va  2 s  .c om
 * @param dbName
 * @param newContent
 * @param requestEtag
 * @param patching
 * @return
 */
@Override
@SuppressWarnings("unchecked")
public OperationResult upsertDB(final ClientSession cs, final String dbName, final BsonDocument newContent,
        final String requestEtag, final boolean updating, final boolean patching, final boolean checkEtag) {

    if (patching && !updating) {
        return new OperationResult(HttpStatus.SC_NOT_FOUND);
    }

    ObjectId newEtag = new ObjectId();

    final BsonDocument content = DAOUtils.validContent(newContent);

    content.put("_etag", new BsonObjectId(newEtag));
    content.remove("_id"); // make sure we don't change this field

    MongoDatabase mdb = client.getDatabase(dbName);
    MongoCollection<BsonDocument> mcoll = mdb.getCollection(META_COLLNAME, BsonDocument.class);

    if (checkEtag && updating) {
        BsonDocument oldProperties = cs == null
                ? mcoll.find(eq("_id", DB_META_DOCID)).projection(FIELDS_TO_RETURN).first()
                : mcoll.find(cs, eq("_id", DB_META_DOCID)).projection(FIELDS_TO_RETURN).first();

        if (oldProperties != null) {
            BsonValue oldEtag = oldProperties.get("_etag");

            if (oldEtag != null && requestEtag == null) {
                return new OperationResult(HttpStatus.SC_CONFLICT, oldEtag);
            }

            BsonValue _requestEtag;

            if (ObjectId.isValid(requestEtag)) {
                _requestEtag = new BsonObjectId(new ObjectId(requestEtag));
            } else {
                // restheart generates ObjectId etags, but here we support
                // strings as well
                _requestEtag = new BsonString(requestEtag);
            }

            if (Objects.equals(_requestEtag, oldEtag)) {
                return doDbPropsUpdate(cs, patching, updating, mcoll, content, newEtag);
            } else {
                return new OperationResult(HttpStatus.SC_PRECONDITION_FAILED, oldEtag);
            }
        } else {
            // this is the case when the db does not have properties
            // e.g. it has not been created by restheart
            return doDbPropsUpdate(cs, patching, updating, mcoll, content, newEtag);
        }
    } else {
        return doDbPropsUpdate(cs, patching, updating, mcoll, content, newEtag);
    }
}

From source file:org.restheart.db.DatabaseImpl.java

License:Open Source License

/**
 *
 * @param cs the client session/*from ww  w .j ava 2s .c  o  m*/
 * @param dbName
 * @param requestEtag
 * @return
 */
@Override
public OperationResult deleteDatabase(final ClientSession cs, final String dbName, final String requestEtag,
        final boolean checkEtag) {
    MongoDatabase mdb = client.getDatabase(dbName);
    MongoCollection<Document> mcoll = mdb.getCollection(META_COLLNAME);

    if (checkEtag) {
        var query = eq("_id", DB_META_DOCID);
        Document properties = cs == null ? mcoll.find(query).projection(FIELDS_TO_RETURN).first()
                : mcoll.find(cs, query).projection(FIELDS_TO_RETURN).first();

        if (properties != null) {
            Object oldEtag = properties.get("_etag");

            if (oldEtag != null) {
                if (requestEtag == null) {
                    return new OperationResult(HttpStatus.SC_CONFLICT, oldEtag);
                } else if (!Objects.equals(oldEtag.toString(), requestEtag)) {
                    return new OperationResult(HttpStatus.SC_PRECONDITION_FAILED, oldEtag);
                }
            }
        }
    }

    if (cs == null) {
        mdb.drop();
    } else {
        mdb.drop(cs);
    }

    return new OperationResult(HttpStatus.SC_NO_CONTENT);
}

From source file:org.restheart.db.DbsDAO.java

License:Open Source License

/**
 * @param dbName//from   w w  w  .j  a  v a2  s.c  o m
 * @return the db props
 *
 */
@Override
public BsonDocument getDatabaseProperties(final String dbName) {
    MongoCollection<BsonDocument> propsColl = collectionDAO.getCollection(dbName, "_properties");

    BsonDocument props = propsColl.find(PROPS_QUERY).limit(1).first();

    if (props != null) {
        props.append("_id", new BsonString(dbName));
    } else if (doesDbExist(dbName)) {
        return new BsonDocument("_id", new BsonString(dbName));
    }

    return props;
}

From source file:org.restheart.db.DbsDAO.java

License:Open Source License

/**
 *
 * @param dbName/*from ww w  . j  a  v a  2s .  com*/
 * @param newContent
 * @param requestEtag
 * @param patching
 * @return
 */
@Override
@SuppressWarnings("unchecked")
public OperationResult upsertDB(final String dbName, final BsonDocument newContent, final String requestEtag,
        final boolean updating, final boolean patching, final boolean checkEtag) {

    if (patching && !updating) {
        return new OperationResult(HttpStatus.SC_NOT_FOUND);
    }

    ObjectId newEtag = new ObjectId();

    final BsonDocument content = DAOUtils.validContent(newContent);

    content.put("_etag", new BsonObjectId(newEtag));
    content.remove("_id"); // make sure we don't change this field

    MongoDatabase mdb = client.getDatabase(dbName);
    MongoCollection<BsonDocument> mcoll = mdb.getCollection("_properties", BsonDocument.class);

    if (checkEtag && updating) {
        BsonDocument oldProperties = mcoll.find(eq("_id", "_properties")).projection(FIELDS_TO_RETURN).first();

        if (oldProperties != null) {
            BsonValue oldEtag = oldProperties.get("_etag");

            if (oldEtag != null && requestEtag == null) {
                return new OperationResult(HttpStatus.SC_CONFLICT, oldEtag);
            }

            BsonValue _requestEtag;

            if (ObjectId.isValid(requestEtag)) {
                _requestEtag = new BsonObjectId(new ObjectId(requestEtag));
            } else {
                // restheart generates ObjectId etags, but here we support
                // strings as well
                _requestEtag = new BsonString(requestEtag);
            }

            if (Objects.equals(_requestEtag, oldEtag)) {
                return doDbPropsUpdate(patching, updating, mcoll, content, newEtag);
            } else {
                return new OperationResult(HttpStatus.SC_PRECONDITION_FAILED, oldEtag);
            }
        } else {
            // this is the case when the db does not have properties
            // e.g. it has not been created by restheart
            return doDbPropsUpdate(patching, updating, mcoll, content, newEtag);
        }
    } else {
        return doDbPropsUpdate(patching, updating, mcoll, content, newEtag);
    }
}

From source file:org.restheart.db.DbsDAO.java

License:Open Source License

/**
 *
 * @param dbName//from   w w w .ja va  2  s .c om
 * @param requestEtag
 * @return
 */
@Override
public OperationResult deleteDatabase(final String dbName, final String requestEtag, final boolean checkEtag) {
    MongoDatabase mdb = client.getDatabase(dbName);
    MongoCollection<Document> mcoll = mdb.getCollection("_properties");

    if (checkEtag) {
        Document properties = mcoll.find(eq("_id", "_properties")).projection(FIELDS_TO_RETURN).first();

        if (properties != null) {
            Object oldEtag = properties.get("_etag");

            if (oldEtag != null) {
                if (requestEtag == null) {
                    return new OperationResult(HttpStatus.SC_CONFLICT, oldEtag);
                } else if (!Objects.equals(oldEtag.toString(), requestEtag)) {
                    return new OperationResult(HttpStatus.SC_PRECONDITION_FAILED, oldEtag);
                }
            }
        }
    }

    mdb.drop();
    return new OperationResult(HttpStatus.SC_NO_CONTENT);
}

From source file:org.restheart.db.DocumentDAO.java

License:Open Source License

@Override
public Document getDocumentEtag(final String dbName, final String collName, final Object documentId) {
    MongoDatabase mdb = client.getDatabase(dbName);
    MongoCollection<Document> mcoll = mdb.getCollection(collName);

    FindIterable<Document> documents = mcoll.find(eq("_id", documentId)).projection(new Document("_etag", 1));

    return documents == null ? null : documents.iterator().tryNext();
}

From source file:org.restheart.db.DocumentDAO.java

License:Open Source License

/**
 * @param dbName//from w  ww .  j a  v  a  2s  .c om
 * @param collName
 * @param documentId
 * @param shardKeys
 * @param newContent
 * @param requestEtag
 * @param patching
 * @param checkEtag
 * @return the HttpStatus code
 */
@Override
@SuppressWarnings("unchecked")
public OperationResult upsertDocument(final String dbName, final String collName, final Object documentId,
        final BsonDocument shardKeys, final BsonDocument newContent, final String requestEtag,
        final boolean patching, final boolean checkEtag) {
    MongoDatabase mdb = client.getDatabase(dbName);
    MongoCollection<BsonDocument> mcoll = mdb.getCollection(collName, BsonDocument.class);

    // genereate new etag
    ObjectId newEtag = new ObjectId();

    final BsonDocument content = DAOUtils.validContent(newContent);

    content.put("_etag", new BsonObjectId(newEtag));

    OperationResult updateResult = DAOUtils.updateDocument(mcoll, documentId, shardKeys, content, !patching);

    BsonDocument oldDocument = updateResult.getOldData();

    if (patching) {
        if (oldDocument == null) {
            return new OperationResult(HttpStatus.SC_NOT_FOUND);
        } else if (checkEtag) {
            // check the old etag (in case restore the old document version)
            return optimisticCheckEtag(mcoll, shardKeys, oldDocument, newEtag, requestEtag, HttpStatus.SC_OK,
                    false);
        } else {
            BsonDocument newDocument = mcoll.find(eq("_id", documentId)).first();

            return new OperationResult(HttpStatus.SC_OK, newEtag, oldDocument, newDocument);
        }
    } else if (oldDocument != null && checkEtag) { // upsertDocument
        // check the old etag (in case restore the old document)
        return optimisticCheckEtag(mcoll, shardKeys, oldDocument, newEtag, requestEtag, HttpStatus.SC_OK,
                false);
    } else if (oldDocument != null) { // insert
        BsonDocument newDocument = mcoll.find(eq("_id", documentId)).first();

        return new OperationResult(HttpStatus.SC_OK, newEtag, oldDocument, newDocument);
    } else {
        BsonDocument newDocument = mcoll.find(eq("_id", documentId)).first();

        return new OperationResult(HttpStatus.SC_CREATED, newEtag, null, newDocument);
    }
}

From source file:org.restheart.db.DocumentDAO.java

License:Open Source License

private OperationResult optimisticCheckEtag(final MongoCollection<BsonDocument> coll, BsonDocument shardKeys,
        final BsonDocument oldDocument, final Object newEtag, final String requestEtag,
        final int httpStatusIfOk, final boolean deleting) {

    BsonValue oldEtag = oldDocument.get("_etag");

    if (oldEtag != null && requestEtag == null) {
        // oopps, we need to restore old document
        // they call it optimistic lock strategy
        if (deleting) {
            DAOUtils.updateDocument(coll, oldDocument.get("_id"), shardKeys, oldDocument, true);
        } else {/*  www.j a  v  a 2 s  .c  o  m*/
            DAOUtils.restoreDocument(coll, oldDocument.get("_id"), shardKeys, oldDocument, newEtag);
        }

        return new OperationResult(HttpStatus.SC_CONFLICT, oldEtag, oldDocument, null);
    }

    BsonValue _requestEtag;

    if (ObjectId.isValid(requestEtag)) {
        _requestEtag = new BsonObjectId(new ObjectId(requestEtag));
    } else {
        // restheart generates ObjectId etags, but here we support
        // strings as well
        _requestEtag = new BsonString(requestEtag);
    }

    if (Objects.equals(_requestEtag, oldEtag)) {
        BsonDocument newDocument = coll.find(eq("_id", oldDocument.get("_id"))).first();

        return new OperationResult(httpStatusIfOk, newEtag, oldDocument, newDocument);
    } else {
        // oopps, we need to restore old document
        // they call it optimistic lock strategy
        if (deleting) {
            DAOUtils.updateDocument(coll, oldDocument.get("_id"), shardKeys, oldDocument, true);
        } else {
            DAOUtils.restoreDocument(coll, oldDocument.get("_id"), shardKeys, oldDocument, newEtag);
        }

        return new OperationResult(HttpStatus.SC_PRECONDITION_FAILED, oldEtag, oldDocument, null);
    }
}

From source file:org.restheart.db.FileMetadataDAO.java

License:Open Source License

/**
 *
 * @param cs the client session/*w w w  .  j a v  a2 s.c o m*/
 * @param dbName
 * @param collName
 * @param documentId
 * @param filter
 * @param shardKeys
 * @param newContent
 * @param requestEtag
 * @param patching
 * @param checkEtag
 * @return
 */
@Override
public OperationResult updateMetadata(final ClientSession cs, final String dbName, final String collName,
        final Object documentId, final BsonDocument filter, final BsonDocument shardKeys,
        final BsonDocument newContent, final String requestEtag, final boolean patching,
        final boolean checkEtag) {
    MongoDatabase mdb = client.getDatabase(dbName);
    MongoCollection<BsonDocument> mcoll = mdb.getCollection(collName, BsonDocument.class);

    // genereate new etag
    ObjectId newEtag = new ObjectId();

    final BsonDocument content = DAOUtils.validContent(newContent);
    content.get("metadata", new BsonDocument()).asDocument().put("_etag", new BsonObjectId(newEtag));

    OperationResult updateResult = DAOUtils.updateMetadata(cs, mcoll, documentId, filter, shardKeys, content,
            patching);

    BsonDocument oldDocument = updateResult.getOldData();

    if (patching) {
        if (oldDocument == null) { // Attempted an insert of a new doc.
            return new OperationResult(
                    updateResult.getHttpCode() > 0 ? updateResult.getHttpCode() : HttpStatus.SC_CONFLICT,
                    newEtag, null, updateResult.getNewData());
        } else if (checkEtag) {
            // check the old etag (in case restore the old document version)
            return optimisticCheckEtag(cs, mcoll, shardKeys, oldDocument, newEtag, requestEtag,
                    HttpStatus.SC_OK);
        } else {
            var query = eq("_id", documentId);

            BsonDocument newDocument = cs == null ? mcoll.find(query).first() : mcoll.find(cs, query).first();

            return new OperationResult(
                    updateResult.getHttpCode() > 0 ? updateResult.getHttpCode() : HttpStatus.SC_OK, newEtag,
                    oldDocument, newDocument);
        }
    } else if (oldDocument != null && checkEtag) { // update
        // check the old etag (in case restore the old document)
        return optimisticCheckEtag(cs, mcoll, shardKeys, oldDocument, newEtag, requestEtag, HttpStatus.SC_OK);
    } else if (oldDocument != null) { // update
        var query = eq("_id", documentId);
        BsonDocument newDocument = cs == null ? mcoll.find(query).first() : mcoll.find(cs, query).first();

        return new OperationResult(
                updateResult.getHttpCode() > 0 ? updateResult.getHttpCode() : HttpStatus.SC_OK, newEtag,
                oldDocument, newDocument);
    } else { // Attempted an insert of a new doc.
        return new OperationResult(
                updateResult.getHttpCode() > 0 ? updateResult.getHttpCode() : HttpStatus.SC_CONFLICT, newEtag,
                null, updateResult.getNewData());
    }
}

From source file:org.restheart.db.FileMetadataDAO.java

License:Open Source License

private OperationResult optimisticCheckEtag(final ClientSession cs, final MongoCollection<BsonDocument> coll,
        final BsonDocument shardKeys, final BsonDocument oldDocument, final Object newEtag,
        final String requestEtag, final int httpStatusIfOk) {

    BsonValue oldEtag = oldDocument.get("metadata", new BsonDocument()).asDocument().get("_etag");

    if (oldEtag != null && requestEtag == null) {
        // oops, we need to restore old document
        DAOUtils.restoreDocument(cs, coll, oldDocument.get("_id"), shardKeys, oldDocument, newEtag,
                "metadata._etag");

        return new OperationResult(HttpStatus.SC_CONFLICT, oldEtag, oldDocument, null);
    }/*from w ww .j ava  2 s .  c o  m*/

    BsonValue _requestEtag;

    if (ObjectId.isValid(requestEtag)) {
        _requestEtag = new BsonObjectId(new ObjectId(requestEtag));
    } else {
        // restheart generates ObjectId etags, but here we support
        // strings as well
        _requestEtag = new BsonString(requestEtag);
    }

    if (Objects.equals(_requestEtag, oldEtag)) {
        var query = eq("_id", oldDocument.get("_id"));
        BsonDocument newDocument = cs == null ? coll.find(query).first() : coll.find(cs, query).first();

        return new OperationResult(httpStatusIfOk, newEtag, oldDocument, newDocument);
    } else {
        // oops, we need to restore old document
        DAOUtils.restoreDocument(cs, coll, oldDocument.get("_id"), shardKeys, oldDocument, newEtag,
                "metadata._etag");

        return new OperationResult(HttpStatus.SC_PRECONDITION_FAILED, oldEtag, oldDocument, null);
    }
}