Search in sources :

Example 16 with BsonInt64

use of org.bson.BsonInt64 in project mongo-java-driver by mongodb.

the class CommandMonitoringTestHelper method massageActualCommandSucceededEvent.

private static CommandSucceededEvent massageActualCommandSucceededEvent(final CommandSucceededEvent actual) {
    BsonDocument response = getWritableCloneOfCommand(actual.getResponse());
    // massage numbers that are the wrong BSON type
    response.put("ok", new BsonDouble(response.getNumber("ok").doubleValue()));
    if (response.containsKey("n")) {
        response.put("n", new BsonInt32(response.getNumber("n").intValue()));
    }
    if (actual.getCommandName().equals("find") || actual.getCommandName().equals("getMore")) {
        if (response.containsKey("cursor")) {
            if (response.getDocument("cursor").containsKey("id") && !response.getDocument("cursor").getInt64("id").equals(new BsonInt64(0))) {
                response.getDocument("cursor").put("id", new BsonInt64(42));
            }
        }
    } else if (actual.getCommandName().equals("killCursors")) {
        response.getArray("cursorsUnknown").set(0, new BsonInt64(42));
    } else if (isWriteCommand(actual.getCommandName())) {
        if (response.containsKey("writeErrors")) {
            for (BsonValue bsonValue : response.getArray("writeErrors")) {
                BsonDocument cur = bsonValue.asDocument();
                BsonDocument newWriteErrorDocument = new BsonDocument().append("index", cur.get("index")).append("code", new BsonInt32(42)).append("errmsg", new BsonString(""));
                cur.clear();
                cur.putAll(newWriteErrorDocument);
            }
        }
        if (actual.getCommandName().equals("update")) {
            response.remove("nModified");
        }
    }
    return new CommandSucceededEvent(actual.getRequestId(), actual.getConnectionDescription(), actual.getCommandName(), response, actual.getElapsedTime(TimeUnit.NANOSECONDS));
}
Also used : BsonInt64(org.bson.BsonInt64) BsonInt32(org.bson.BsonInt32) CommandSucceededEvent(com.mongodb.event.CommandSucceededEvent) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonDouble(org.bson.BsonDouble) BsonValue(org.bson.BsonValue)

Example 17 with BsonInt64

use of org.bson.BsonInt64 in project mongo-java-driver by mongodb.

the class OperationHelper method cursorDocumentToQueryResult.

private static <T> QueryResult<T> cursorDocumentToQueryResult(final BsonDocument cursorDocument, final ServerAddress serverAddress, final String fieldNameContainingBatch) {
    long cursorId = ((BsonInt64) cursorDocument.get("id")).getValue();
    MongoNamespace queryResultNamespace = new MongoNamespace(cursorDocument.getString("ns").getValue());
    return new QueryResult<T>(queryResultNamespace, BsonDocumentWrapperHelper.<T>toList(cursorDocument, fieldNameContainingBatch), cursorId, serverAddress);
}
Also used : BsonInt64(org.bson.BsonInt64) QueryResult(com.mongodb.internal.connection.QueryResult) MongoNamespace(com.mongodb.MongoNamespace)

Example 18 with BsonInt64

use of org.bson.BsonInt64 in project mongo-java-driver by mongodb.

the class UnifiedCrudHelper method executeEstimatedDocumentCount.

public OperationResult executeEstimatedDocumentCount(final BsonDocument operation) {
    MongoCollection<BsonDocument> collection = entities.getCollection(operation.getString("object").getValue());
    BsonDocument arguments = operation.getDocument("arguments", new BsonDocument());
    EstimatedDocumentCountOptions options = new EstimatedDocumentCountOptions();
    for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
        // noinspection SwitchStatementWithTooFewBranches
        switch(cur.getKey()) {
            case "maxTimeMS":
                options.maxTime(cur.getValue().asNumber().intValue(), TimeUnit.MILLISECONDS);
                break;
            default:
                throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
        }
    }
    return resultOf(() -> new BsonInt64(collection.estimatedDocumentCount(options)));
}
Also used : BsonInt64(org.bson.BsonInt64) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) EstimatedDocumentCountOptions(com.mongodb.client.model.EstimatedDocumentCountOptions) Map(java.util.Map) BsonValue(org.bson.BsonValue)

Example 19 with BsonInt64

use of org.bson.BsonInt64 in project mongo-java-driver by mongodb.

the class GetMoreProtocol method asGetMoreCommandResponseDocument.

private BsonDocument asGetMoreCommandResponseDocument(final QueryResult<T> queryResult, final ResponseBuffers responseBuffers) {
    List<ByteBufBsonDocument> rawResultDocuments = Collections.emptyList();
    if (responseBuffers.getReplyHeader().getNumberReturned() != 0) {
        responseBuffers.reset();
        rawResultDocuments = ByteBufBsonDocument.createList(responseBuffers);
    }
    BsonDocument cursorDocument = new BsonDocument("id", queryResult.getCursor() == null ? new BsonInt64(0) : new BsonInt64(queryResult.getCursor().getId())).append("ns", new BsonString(namespace.getFullName())).append("nextBatch", new BsonArray(rawResultDocuments));
    return new BsonDocument("cursor", cursorDocument).append("ok", new BsonDouble(1));
}
Also used : BsonInt64(org.bson.BsonInt64) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) BsonDouble(org.bson.BsonDouble)

Example 20 with BsonInt64

use of org.bson.BsonInt64 in project mongo-java-driver by mongodb.

the class IdHoldingBsonWriter method writeInt64.

@Override
public void writeInt64(final String name, final long value) {
    setCurrentFieldName(name);
    addBsonValue(() -> new BsonInt64(value), () -> getIdBsonWriter().writeInt64(name, value));
    super.writeInt64(name, value);
}
Also used : BsonInt64(org.bson.BsonInt64)

Aggregations

BsonInt64 (org.bson.BsonInt64)44 BsonDocument (org.bson.BsonDocument)36 BsonString (org.bson.BsonString)30 BsonInt32 (org.bson.BsonInt32)20 BsonArray (org.bson.BsonArray)16 BsonDouble (org.bson.BsonDouble)13 BsonValue (org.bson.BsonValue)11 Test (org.junit.Test)7 MongoNamespace (com.mongodb.MongoNamespace)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 CommandSucceededEvent (com.mongodb.event.CommandSucceededEvent)3 BsonDocumentReader (org.bson.BsonDocumentReader)3 BsonRegularExpression (org.bson.BsonRegularExpression)3 ObjectId (org.bson.types.ObjectId)3 Test (org.junit.jupiter.api.Test)3 SingleMapReaderImpl (org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl)2 BsonBoolean (org.bson.BsonBoolean)2 BsonDateTime (org.bson.BsonDateTime)2 BsonNull (org.bson.BsonNull)2