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);
}
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;
}
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);
}
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");
}
Aggregations