Search in sources :

Example 61 with BsonString

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;
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonString(org.bson.BsonString) MongoNamespace(com.mongodb.MongoNamespace) Nullable(com.mongodb.lang.Nullable)

Example 62 with BsonString

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());
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) Test(org.junit.Test)

Example 63 with BsonString

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);
}
Also used : BsonInt64(org.bson.BsonInt64) BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonDateTime(org.bson.BsonDateTime) BsonString(org.bson.BsonString) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument)

Example 64 with BsonString

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);
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) BsonString(org.bson.BsonString) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Date(java.util.Date) BsonValue(org.bson.BsonValue)

Example 65 with BsonString

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();
    }
}
Also used : ClusterId(com.mongodb.connection.ClusterId) ServerAddress(com.mongodb.ServerAddress) SameObjectProvider(com.mongodb.internal.inject.SameObjectProvider) ClusterConnectionMode(com.mongodb.connection.ClusterConnectionMode) ServerId(com.mongodb.connection.ServerId) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) ConnectionPoolSettings(com.mongodb.connection.ConnectionPoolSettings) ServerListener(com.mongodb.event.ServerListener) Before(org.junit.Before)

Aggregations

BsonString (org.bson.BsonString)178 BsonDocument (org.bson.BsonDocument)153 BsonInt32 (org.bson.BsonInt32)55 BsonArray (org.bson.BsonArray)48 Test (org.junit.Test)36 Document (org.bson.Document)32 BsonValue (org.bson.BsonValue)31 BsonInt64 (org.bson.BsonInt64)28 ArrayList (java.util.ArrayList)23 Test (org.junit.jupiter.api.Test)18 Map (java.util.Map)14 BsonDouble (org.bson.BsonDouble)14 MongoClientSettings (com.mongodb.MongoClientSettings)13 BsonBinary (org.bson.BsonBinary)13 EncryptOptions (com.mongodb.client.model.vault.EncryptOptions)12 HashMap (java.util.HashMap)12 MongoNamespace (com.mongodb.MongoNamespace)11 List (java.util.List)10 Before (org.junit.Before)10 DataKeyOptions (com.mongodb.client.model.vault.DataKeyOptions)9