use of org.bson.BsonString 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 org.bson.BsonString in project mongo-java-driver by mongodb.
the class CommandResultTest method shouldNotBeOkWhenOkFieldTypeIsNotBooleanOrNumber.
@Test
public void shouldNotBeOkWhenOkFieldTypeIsNotBooleanOrNumber() throws UnknownHostException {
CommandResult commandResult = new CommandResult(new BsonDocument("ok", new BsonString("1")), DECODER);
assertFalse(commandResult.ok());
}
use of org.bson.BsonString in project mongo-java-driver by mongodb.
the class GridFSFileCodec method encode.
@Override
public void encode(final BsonWriter writer, final GridFSFile value, final EncoderContext encoderContext) {
BsonDocument bsonDocument = new BsonDocument();
bsonDocument.put("_id", value.getId());
bsonDocument.put("filename", new BsonString(value.getFilename()));
bsonDocument.put("length", new BsonInt64(value.getLength()));
bsonDocument.put("chunkSize", new BsonInt32(value.getChunkSize()));
bsonDocument.put("uploadDate", new BsonDateTime(value.getUploadDate().getTime()));
Document metadata = value.getMetadata();
if (metadata != null) {
bsonDocument.put("metadata", new BsonDocumentWrapper<Document>(metadata, documentCodec));
}
bsonDocumentCodec.encode(writer, bsonDocument, encoderContext);
}
use of org.bson.BsonString in project mongo-java-driver by mongodb.
the class GridFSFileCodec method decode.
@Override
public GridFSFile decode(final BsonReader reader, final DecoderContext decoderContext) {
BsonDocument bsonDocument = bsonDocumentCodec.decode(reader, decoderContext);
BsonValue id = bsonDocument.get("_id");
String filename = bsonDocument.get("filename", new BsonString("")).asString().getValue();
long length = bsonDocument.getNumber("length").longValue();
int chunkSize = bsonDocument.getNumber("chunkSize").intValue();
Date uploadDate = new Date(bsonDocument.getDateTime("uploadDate").getValue());
BsonDocument metadataBsonDocument = bsonDocument.getDocument("metadata", new BsonDocument());
Document optionalMetadata = asDocumentOrNull(metadataBsonDocument);
return new GridFSFile(id, filename, length, chunkSize, uploadDate, optionalMetadata);
}
use of org.bson.BsonString in project mongo-java-driver by mongodb.
the class AbstractConnectionPoolTest method setUp.
@Before
public void setUp() {
assumeFalse(skipTest);
ConnectionPoolSettings.Builder settingsBuilder = ConnectionPoolSettings.builder();
BsonDocument poolOptions = definition.getDocument("poolOptions", new BsonDocument());
if (poolOptions.containsKey("maxPoolSize")) {
settingsBuilder.maxSize(poolOptions.getNumber("maxPoolSize").intValue());
}
if (poolOptions.containsKey("minPoolSize")) {
settingsBuilder.minSize(poolOptions.getNumber("minPoolSize").intValue());
}
if (poolOptions.containsKey("maxIdleTimeMS")) {
settingsBuilder.maxConnectionIdleTime(poolOptions.getNumber("maxIdleTimeMS").intValue(), TimeUnit.MILLISECONDS);
}
if (poolOptions.containsKey("waitQueueTimeoutMS")) {
settingsBuilder.maxWaitTime(poolOptions.getNumber("waitQueueTimeoutMS").intValue(), TimeUnit.MILLISECONDS);
}
if (poolOptions.containsKey("backgroundThreadIntervalMS")) {
long intervalMillis = poolOptions.getNumber("backgroundThreadIntervalMS").longValue();
assertFalse(intervalMillis == 0);
if (intervalMillis < 0) {
settingsBuilder.maintenanceInitialDelay(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
} else {
/* Using frequency/period instead of an interval as required by the specification is incorrect, for example,
* because it opens up a possibility to run the background thread non-stop if runs are as long as or longer than the period.
* Nevertheless, I am reusing what we already have in the driver instead of clogging up the implementation. */
settingsBuilder.maintenanceFrequency(poolOptions.getNumber("backgroundThreadIntervalMS").longValue(), TimeUnit.MILLISECONDS);
}
}
if (poolOptions.containsKey("maxConnecting")) {
settingsBuilder.maxConnecting(poolOptions.getInt32("maxConnecting").intValue());
}
listener = new TestConnectionPoolListener();
settingsBuilder.addConnectionPoolListener(listener);
settings = settingsBuilder.build();
InternalConnectionPoolSettings internalSettings = InternalConnectionPoolSettings.builder().prestartAsyncWorkManager(PRESTART_POOL_ASYNC_WORK_MANAGER_FILE_NAMES.contains(fileName)).build();
Style style = Style.of(definition.getString("style").getValue());
switch(style) {
case UNIT:
{
ServerId serverId = new ServerId(new ClusterId(), new ServerAddress("host1"));
pool = new DefaultConnectionPool(serverId, new TestInternalConnectionFactory(), settings, internalSettings, SameObjectProvider.initialized(mock(SdamServerDescriptionManager.class)));
break;
}
case INTEGRATION:
{
ServerId serverId = new ServerId(new ClusterId(), ClusterFixture.getPrimary());
ClusterConnectionMode connectionMode = ClusterConnectionMode.MULTIPLE;
SameObjectProvider<SdamServerDescriptionManager> sdamProvider = SameObjectProvider.uninitialized();
pool = new ConnectionIdAdjustingConnectionPool(new DefaultConnectionPool(serverId, new InternalStreamConnectionFactory(connectionMode, createStreamFactory(SocketSettings.builder().build(), ClusterFixture.getSslSettings()), ClusterFixture.getCredentialWithCache(), poolOptions.getString("appName", new BsonString(fileName + ": " + description)).getValue(), MongoDriverInformation.builder().build(), Collections.emptyList(), new TestCommandListener(), ClusterFixture.getServerApi()), settings, internalSettings, sdamProvider));
sdamProvider.initialize(new DefaultSdamServerDescriptionManager(mockedCluster(), serverId, mock(ServerListener.class), mock(ServerMonitor.class), pool, connectionMode));
setFailPoint();
break;
}
default:
{
throw new AssertionError(format("Style %s is not implemented", style));
}
}
if (internalSettings.isPrestartAsyncWorkManager()) {
waitForPoolAsyncWorkManagerStart();
}
}
Aggregations