use of com.mongodb.MongoNamespace in project mongo-java-driver by mongodb.
the class AggregatePublisherImpl method getOutNamespace.
@Nullable
private MongoNamespace getOutNamespace() {
BsonDocument lastPipelineStage = getLastPipelineStage();
if (lastPipelineStage == null) {
return null;
}
String databaseName = getNamespace().getDatabaseName();
if (lastPipelineStage.containsKey("$out")) {
if (lastPipelineStage.get("$out").isString()) {
return new MongoNamespace(databaseName, lastPipelineStage.getString("$out").getValue());
} else if (lastPipelineStage.get("$out").isDocument()) {
BsonDocument outDocument = lastPipelineStage.getDocument("$out");
if (!outDocument.containsKey("db") || !outDocument.containsKey("coll")) {
throw new IllegalStateException("Cannot return a cursor when the value for $out stage is not a namespace document");
}
return new MongoNamespace(outDocument.getString("db").getValue(), outDocument.getString("coll").getValue());
} else {
throw new IllegalStateException("Cannot return a cursor when the value for $out stage " + "is not a string or namespace document");
}
} else if (lastPipelineStage.containsKey("$merge")) {
if (lastPipelineStage.isString("$merge")) {
return new MongoNamespace(databaseName, lastPipelineStage.getString("$merge").getValue());
} else if (lastPipelineStage.isDocument("$merge")) {
BsonDocument mergeDocument = lastPipelineStage.getDocument("$merge");
if (mergeDocument.isDocument("into")) {
BsonDocument intoDocument = mergeDocument.getDocument("into");
return new MongoNamespace(intoDocument.getString("db", new BsonString(databaseName)).getValue(), intoDocument.getString("coll").getValue());
} else if (mergeDocument.isString("into")) {
return new MongoNamespace(databaseName, mergeDocument.getString("into").getValue());
}
} else {
throw new IllegalStateException("Cannot return a cursor when the value for $merge stage is not a string or a document");
}
}
return null;
}
use of com.mongodb.MongoNamespace in project mongo-java-driver by mongodb.
the class Crypts method createCrypt.
public static Crypt createCrypt(final MongoClientImpl client, final AutoEncryptionSettings options) {
MongoClient internalClient = null;
MongoClientSettings keyVaultMongoClientSettings = options.getKeyVaultMongoClientSettings();
if (keyVaultMongoClientSettings == null || !options.isBypassAutoEncryption()) {
MongoClientSettings settings = MongoClientSettings.builder(client.getSettings()).applyToConnectionPoolSettings(builder -> builder.minSize(0)).autoEncryptionSettings(null).build();
internalClient = MongoClients.create(settings);
}
MongoClient collectionInfoRetrieverClient = internalClient;
MongoClient keyVaultClient = keyVaultMongoClientSettings == null ? internalClient : MongoClients.create(keyVaultMongoClientSettings);
return new Crypt(MongoCrypts.create(createMongoCryptOptions(options.getKmsProviders(), options.getSchemaMap())), options.isBypassAutoEncryption() ? null : new CollectionInfoRetriever(collectionInfoRetrieverClient), new CommandMarker(options.isBypassAutoEncryption(), options.getExtraOptions()), new KeyRetriever(keyVaultClient, new MongoNamespace(options.getKeyVaultNamespace())), createKeyManagementService(options.getKmsProviderSslContextMap()), options.isBypassAutoEncryption(), internalClient);
}
use of com.mongodb.MongoNamespace in project mongo-java-driver by mongodb.
the class AbstractUnifiedTest method setUp.
@Before
public void setUp() {
assumeFalse(skipTest);
assumeTrue("Skipping test: " + definition.getString("skipReason", new BsonString("")).getValue(), !definition.containsKey("skipReason"));
assumeFalse("Skipping test of count", filename.equals("count.json"));
collectionHelper = new CollectionHelper<Document>(new DocumentCodec(), new MongoNamespace(databaseName, collectionName));
collectionHelper.killAllSessions();
if (!isDataLakeTest()) {
collectionHelper.create(collectionName, new CreateCollectionOptions(), WriteConcern.MAJORITY);
}
if (!data.isEmpty()) {
List<BsonDocument> documents = new ArrayList<BsonDocument>();
for (BsonValue document : data) {
documents.add(document.asDocument());
}
collectionHelper.insertDocuments(documents, WriteConcern.MAJORITY);
}
if (definition.containsKey("failPoint")) {
collectionHelper.runAdminCommand(definition.getDocument("failPoint"));
}
final BsonDocument clientOptions = definition.getDocument("clientOptions", new BsonDocument());
connectionString = getConnectionString();
useMultipleMongoses = definition.getBoolean("useMultipleMongoses", BsonBoolean.FALSE).getValue();
if (useMultipleMongoses) {
assumeTrue(isSharded());
connectionString = getMultiMongosConnectionString();
assumeTrue("The system property org.mongodb.test.transaction.uri is not set.", connectionString != null);
}
MongoClientSettings.Builder builder = getMongoClientSettingsBuilder().applyConnectionString(connectionString).addCommandListener(commandListener).applyToClusterSettings(clusterSettingsBuilder -> {
if (clientOptions.containsKey("serverSelectionTimeoutMS")) {
clusterSettingsBuilder.serverSelectionTimeout(clientOptions.getNumber("serverSelectionTimeoutMS").longValue(), MILLISECONDS);
}
if (clientOptions.containsKey("directConnection")) {
setDirectConnection(clusterSettingsBuilder);
}
}).applyToSocketSettings(new Block<SocketSettings.Builder>() {
@Override
public void apply(final SocketSettings.Builder builder) {
builder.readTimeout(clientOptions.getInt32("socketTimeoutMS", new BsonInt32(toIntExact(SECONDS.toMillis(5)))).getValue(), MILLISECONDS);
if (clientOptions.containsKey("connectTimeoutMS")) {
builder.connectTimeout(clientOptions.getNumber("connectTimeoutMS").intValue(), MILLISECONDS);
}
}
}).writeConcern(getWriteConcern(clientOptions)).readConcern(getReadConcern(clientOptions)).readPreference(getReadPreference(clientOptions)).retryWrites(clientOptions.getBoolean("retryWrites", BsonBoolean.FALSE).getValue()).retryReads(false).applyToConnectionPoolSettings(poolSettingsBuilder -> {
poolSettingsBuilder.addConnectionPoolListener(connectionPoolListener);
if (clientOptions.containsKey("minPoolSize")) {
poolSettingsBuilder.minSize(clientOptions.getInt32("minPoolSize").getValue());
}
}).applyToServerSettings(new Block<ServerSettings.Builder>() {
@Override
public void apply(final ServerSettings.Builder builder) {
builder.heartbeatFrequency(50, MILLISECONDS);
builder.minHeartbeatFrequency(MIN_HEARTBEAT_FREQUENCY_MS, MILLISECONDS);
builder.addServerListener(serverListener);
}
});
if (clientOptions.containsKey("heartbeatFrequencyMS")) {
builder.applyToServerSettings(new Block<ServerSettings.Builder>() {
@Override
public void apply(final ServerSettings.Builder builder) {
builder.heartbeatFrequency(clientOptions.getInt32("heartbeatFrequencyMS").intValue(), MILLISECONDS);
}
});
}
if (clientOptions.containsKey("appname")) {
builder.applicationName(clientOptions.getString("appname").getValue());
}
if (clientOptions.containsKey("w")) {
if (clientOptions.isString("w")) {
builder.writeConcern(new WriteConcern(clientOptions.getString("w").getValue()));
} else if (clientOptions.isNumber("w")) {
builder.writeConcern(new WriteConcern(clientOptions.getNumber("w").intValue()));
}
}
StreamFactoryFactory streamFactoryFactory = getStreamFactoryFactory();
if (streamFactoryFactory != null) {
builder.streamFactoryFactory(streamFactoryFactory);
}
mongoClient = createMongoClient(builder.build());
database = mongoClient.getDatabase(databaseName);
if (useMultipleMongoses) {
// non-transactional distinct operation to avoid StaleDbVersion error
runDistinctOnEachNode();
}
helper = new JsonPoweredCrudTestHelper(description, database, database.getCollection(collectionName, BsonDocument.class), null, mongoClient);
sessionsMap = new HashMap<>();
lsidMap = new HashMap<>();
if (createSessions && serverVersionAtLeast(3, 6)) {
ClientSession sessionZero = createSession("session0");
ClientSession sessionOne = createSession("session1");
sessionsMap.put("session0", sessionZero);
sessionsMap.put("session1", sessionOne);
lsidMap.put("session0", sessionZero.getServerSession().getIdentifier());
lsidMap.put("session1", sessionOne.getServerSession().getIdentifier());
}
}
use of com.mongodb.MongoNamespace in project mongo-java-driver by mongodb.
the class AtlasDataLakeKillCursorsProseTest method testKillCursorsOnAtlasDataLake.
@Test
public void testKillCursorsOnAtlasDataLake() {
// Initiate find command
MongoCursor<Document> cursor = client.getDatabase(DATABASE_NAME).getCollection(COLLECTION_NAME).find().batchSize(2).cursor();
CommandSucceededEvent findCommandSucceededEvent = commandListener.getCommandSucceededEvent("find");
BsonDocument findCommandResponse = findCommandSucceededEvent.getResponse();
MongoNamespace cursorNamespace = new MongoNamespace(findCommandResponse.getDocument("cursor").getString("ns").getValue());
// Initiate killCursors command
cursor.close();
CommandStartedEvent killCursorsCommandStartedEvent = commandListener.getCommandStartedEvent("killCursors");
CommandSucceededEvent killCursorsCommandSucceededEvent = commandListener.getCommandSucceededEvent("killCursors");
BsonDocument killCursorsCommand = killCursorsCommandStartedEvent.getCommand();
assertEquals(cursorNamespace.getDatabaseName(), killCursorsCommandStartedEvent.getDatabaseName());
assertEquals(cursorNamespace.getCollectionName(), killCursorsCommand.getString("killCursors").getValue());
BsonInt64 cursorId = findCommandResponse.getDocument("cursor").getInt64("id");
assertEquals(cursorId, killCursorsCommand.getArray("cursors").getValues().get(0).asInt64());
assertEquals(cursorId, killCursorsCommandSucceededEvent.getResponse().getArray("cursorsKilled").get(0).asInt64());
}
use of com.mongodb.MongoNamespace in project mongo-java-driver by mongodb.
the class AbstractRetryableReadsTest method setUp.
@Before
public void setUp() {
assumeFalse(skipTest);
assumeTrue("Skipping test: " + definition.getString("skipReason", new BsonString("")).getValue(), !definition.containsKey("skipReason"));
assumeFalse("Skipping count tests", filename.startsWith("count.") || filename.startsWith("count-"));
assumeFalse("Skipping list index names tests", filename.startsWith("listIndexNames"));
collectionHelper = new CollectionHelper<Document>(new DocumentCodec(), new MongoNamespace(databaseName, collectionName));
final BsonDocument clientOptions = definition.getDocument("clientOptions", new BsonDocument());
ConnectionString connectionString = getConnectionString();
useMultipleMongoses = definition.getBoolean("useMultipleMongoses", BsonBoolean.FALSE).getValue();
if (useMultipleMongoses) {
assumeTrue(isSharded());
connectionString = getMultiMongosConnectionString();
assumeTrue("The system property org.mongodb.test.multi.mongos.uri is not set.", connectionString != null);
}
MongoClientSettings settings = getMongoClientSettingsBuilder().applyConnectionString(connectionString).addCommandListener(commandListener).applyToSocketSettings(new Block<SocketSettings.Builder>() {
@Override
public void apply(final SocketSettings.Builder builder) {
builder.readTimeout(5, TimeUnit.SECONDS);
}
}).applyToServerSettings(new Block<ServerSettings.Builder>() {
@Override
public void apply(final ServerSettings.Builder builder) {
builder.heartbeatFrequency(5, TimeUnit.MILLISECONDS);
}
}).writeConcern(getWriteConcern(clientOptions)).readConcern(getReadConcern(clientOptions)).readPreference(getReadPreference(clientOptions)).retryWrites(clientOptions.getBoolean("retryWrites", BsonBoolean.FALSE).getValue()).retryReads(clientOptions.getBoolean("retryReads", BsonBoolean.TRUE).getValue()).build();
mongoClient = createMongoClient(settings);
if (data != null) {
List<BsonDocument> documents = new ArrayList<BsonDocument>();
for (BsonValue document : data) {
documents.add(document.asDocument());
}
collectionHelper.drop();
if (documents.size() > 0) {
collectionHelper.insertDocuments(documents);
}
}
MongoDatabase database = mongoClient.getDatabase(databaseName);
if (gridFSBucketName != null) {
setupGridFSBuckets(database);
commandListener.reset();
}
collection = database.getCollection(collectionName, BsonDocument.class);
helper = new JsonPoweredCrudTestHelper(description, database, collection, gridFSBucket, mongoClient);
if (definition.containsKey("failPoint")) {
collectionHelper.runAdminCommand(definition.getDocument("failPoint"));
}
}
Aggregations