Search in sources :

Example 6 with BsonElement

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

the class BsonDocumentCodec method decode.

@Override
public BsonDocument decode(final BsonReader reader, final DecoderContext decoderContext) {
    List<BsonElement> keyValuePairs = new ArrayList<BsonElement>();
    reader.readStartDocument();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        String fieldName = reader.readName();
        keyValuePairs.add(new BsonElement(fieldName, readValue(reader, decoderContext)));
    }
    reader.readEndDocument();
    return new BsonDocument(keyValuePairs);
}
Also used : BsonElement(org.bson.BsonElement) BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList)

Example 7 with BsonElement

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

the class CommandMessage method getExtraElements.

private List<BsonElement> getExtraElements(final SessionContext sessionContext) {
    List<BsonElement> extraElements = new ArrayList<BsonElement>();
    extraElements.add(new BsonElement("$db", new BsonString(new MongoNamespace(getCollectionName()).getDatabaseName())));
    if (sessionContext.getClusterTime() != null) {
        extraElements.add(new BsonElement("$clusterTime", sessionContext.getClusterTime()));
    }
    if (sessionContext.hasSession() && responseExpected) {
        extraElements.add(new BsonElement("lsid", sessionContext.getSessionId()));
    }
    boolean firstMessageInTransaction = sessionContext.notifyMessageSent();
    assertFalse(sessionContext.hasActiveTransaction() && sessionContext.isSnapshot());
    if (sessionContext.hasActiveTransaction()) {
        checkServerVersionForTransactionSupport();
        extraElements.add(new BsonElement("txnNumber", new BsonInt64(sessionContext.getTransactionNumber())));
        if (firstMessageInTransaction) {
            extraElements.add(new BsonElement("startTransaction", BsonBoolean.TRUE));
            addReadConcernDocument(extraElements, sessionContext);
        }
        extraElements.add(new BsonElement("autocommit", BsonBoolean.FALSE));
    } else if (sessionContext.isSnapshot()) {
        addReadConcernDocument(extraElements, sessionContext);
    }
    if (serverApi != null) {
        addServerApiElements(extraElements);
    }
    if (readPreference != null) {
        if (!readPreference.equals(primary())) {
            extraElements.add(new BsonElement("$readPreference", readPreference.toDocument()));
        } else if (isDirectConnectionToReplicaSetMember()) {
            extraElements.add(new BsonElement("$readPreference", primaryPreferred().toDocument()));
        }
    }
    return extraElements;
}
Also used : BsonElement(org.bson.BsonElement) BsonInt64(org.bson.BsonInt64) BsonString(org.bson.BsonString) ArrayList(java.util.ArrayList) MongoNamespace(com.mongodb.MongoNamespace)

Example 8 with BsonElement

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

the class CommandMessage method encodeMessageBodyWithMetadata.

@Override
protected EncodingMetadata encodeMessageBodyWithMetadata(final BsonOutput bsonOutput, final SessionContext sessionContext) {
    int messageStartPosition = bsonOutput.getPosition() - MESSAGE_PROLOGUE_LENGTH;
    int commandStartPosition;
    if (useOpMsg()) {
        int flagPosition = bsonOutput.getPosition();
        // flag bits
        bsonOutput.writeInt32(0);
        // payload type
        bsonOutput.writeByte(0);
        commandStartPosition = bsonOutput.getPosition();
        addDocument(getCommandToEncode(), bsonOutput, commandFieldNameValidator, getExtraElements(sessionContext));
        if (payload != null) {
            // payload type
            bsonOutput.writeByte(1);
            int payloadBsonOutputStartPosition = bsonOutput.getPosition();
            // size
            bsonOutput.writeInt32(0);
            bsonOutput.writeCString(payload.getPayloadName());
            writePayload(new BsonBinaryWriter(bsonOutput, payloadFieldNameValidator), bsonOutput, getSettings(), messageStartPosition, payload, getSettings().getMaxDocumentSize());
            int payloadBsonOutputLength = bsonOutput.getPosition() - payloadBsonOutputStartPosition;
            bsonOutput.writeInt32(payloadBsonOutputStartPosition, payloadBsonOutputLength);
        }
        // Write the flag bits
        bsonOutput.writeInt32(flagPosition, getOpMsgFlagBits());
    } else {
        bsonOutput.writeInt32(getOpQueryFlagBits());
        bsonOutput.writeCString(namespace.getFullName());
        bsonOutput.writeInt32(0);
        bsonOutput.writeInt32(-1);
        commandStartPosition = bsonOutput.getPosition();
        if (payload == null) {
            List<BsonElement> elements = null;
            if (serverApi != null) {
                elements = new ArrayList<>(3);
                addServerApiElements(elements);
            }
            addDocument(getCommandToEncode(), bsonOutput, commandFieldNameValidator, elements);
        } else {
            // We're not concerned with adding ServerApi elements here.  The only reason we do it for OP_QUERY-based commands is that
            // OP_QUERY is always used for the handshake, and we have to pass ServerApi elements in the handshake.  Other than that,
            // all servers that support ServerApi also support OP_MSG, so this code path should never be hit.
            addDocumentWithPayload(bsonOutput, messageStartPosition);
        }
    }
    return new EncodingMetadata(commandStartPosition);
}
Also used : BsonElement(org.bson.BsonElement) BsonBinaryWriter(org.bson.BsonBinaryWriter)

Example 9 with BsonElement

use of org.bson.BsonElement in project brave by openzipkin.

the class TraceMongoCommandListenerTest method getCollectionName_notAllowListedCommandAndCollectionField.

@Test
public void getCollectionName_notAllowListedCommandAndCollectionField() {
    BsonDocument command = new BsonDocument(Arrays.asList(new BsonElement("collection", new BsonString("coll")), new BsonElement("cmd", new BsonString("bar"))));
    assertThat(listener.getCollectionName(command, "cmd")).isEqualTo(// collection field wins
    "coll");
}
Also used : BsonElement(org.bson.BsonElement) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) TraceMongoCommandListener.getNonEmptyBsonString(brave.mongodb.TraceMongoCommandListener.getNonEmptyBsonString) Test(org.junit.Test)

Aggregations

BsonElement (org.bson.BsonElement)9 BsonDocument (org.bson.BsonDocument)6 BsonString (org.bson.BsonString)6 ArrayList (java.util.ArrayList)5 TraceMongoCommandListener.getNonEmptyBsonString (brave.mongodb.TraceMongoCommandListener.getNonEmptyBsonString)2 BsonInt32 (org.bson.BsonInt32)2 Test (org.junit.Test)2 MongoNamespace (com.mongodb.MongoNamespace)1 Accumulators (com.mongodb.client.model.Accumulators)1 Aggregates (com.mongodb.client.model.Aggregates)1 BsonField (com.mongodb.client.model.BsonField)1 Arrays (java.util.Arrays)1 List (java.util.List)1 BiFunction (java.util.function.BiFunction)1 Collectors (java.util.stream.Collectors)1 StreamSupport (java.util.stream.StreamSupport)1 Aggregate (org.apache.calcite.rel.core.Aggregate)1 AggregateCall (org.apache.calcite.rel.core.AggregateCall)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1 RexNode (org.apache.calcite.rex.RexNode)1