Search in sources :

Example 21 with BsonInt64

use of org.bson.BsonInt64 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 22 with BsonInt64

use of org.bson.BsonInt64 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());
}
Also used : BsonInt64(org.bson.BsonInt64) CommandSucceededEvent(com.mongodb.event.CommandSucceededEvent) BsonDocument(org.bson.BsonDocument) CommandStartedEvent(com.mongodb.event.CommandStartedEvent) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) MongoNamespace(com.mongodb.MongoNamespace) Test(org.junit.Test) ClusterFixture.isDataLakeTest(com.mongodb.ClusterFixture.isDataLakeTest)

Example 23 with BsonInt64

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

the class AbstractRetryableReadsTest method processFiles.

private List<BsonDocument> processFiles(final BsonArray bsonArray, final List<BsonDocument> documents) {
    for (BsonValue rawDocument : bsonArray.getValues()) {
        if (rawDocument.isDocument()) {
            BsonDocument document = rawDocument.asDocument();
            if (document.get("length").isInt32()) {
                document.put("length", new BsonInt64(document.getInt32("length").getValue()));
            }
            if (document.containsKey("metadata") && document.getDocument("metadata").isEmpty()) {
                document.remove("metadata");
            }
            if (document.containsKey("aliases") && document.getArray("aliases").getValues().size() == 0) {
                document.remove("aliases");
            }
            if (document.containsKey("contentType") && document.getString("contentType").getValue().length() == 0) {
                document.remove("contentType");
            }
            documents.add(document);
        }
    }
    return documents;
}
Also used : BsonInt64(org.bson.BsonInt64) BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue)

Example 24 with BsonInt64

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

the class QueryProtocol method asFindCommandResponseDocument.

private BsonDocument asFindCommandResponseDocument(final ResponseBuffers responseBuffers, final QueryResult<T> queryResult, final boolean isExplain) {
    List<ByteBufBsonDocument> rawResultDocuments = Collections.emptyList();
    if (responseBuffers.getReplyHeader().getNumberReturned() > 0) {
        responseBuffers.reset();
        rawResultDocuments = ByteBufBsonDocument.createList(responseBuffers);
    }
    if (isExplain) {
        BsonDocument explainCommandResponseDocument = new BsonDocument("ok", new BsonDouble(1));
        explainCommandResponseDocument.putAll(rawResultDocuments.get(0));
        return explainCommandResponseDocument;
    } else {
        BsonDocument cursorDocument = new BsonDocument("id", queryResult.getCursor() == null ? new BsonInt64(0) : new BsonInt64(queryResult.getCursor().getId())).append("ns", new BsonString(namespace.getFullName())).append("firstBatch", new BsonArray(rawResultDocuments));
        return new BsonDocument("cursor", cursorDocument).append("ok", new BsonDouble(1));
    }
}
Also used : BsonInt64(org.bson.BsonInt64) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) BsonDouble(org.bson.BsonDouble)

Example 25 with BsonInt64

use of org.bson.BsonInt64 in project immutables by immutables.

the class BsonReaderTest method bsonToGson.

/**
 * Reading from BSON to GSON
 */
@Test
public void bsonToGson() throws Exception {
    BsonDocument document = new BsonDocument();
    document.append("boolean", new BsonBoolean(true));
    document.append("int32", new BsonInt32(32));
    document.append("int64", new BsonInt64(64));
    document.append("double", new BsonDouble(42.42D));
    document.append("string", new BsonString("foo"));
    document.append("null", new BsonNull());
    document.append("array", new BsonArray());
    document.append("object", new BsonDocument());
    JsonElement element = TypeAdapters.JSON_ELEMENT.read(new BsonReader(new BsonDocumentReader(document)));
    check(element.isJsonObject());
    check(element.getAsJsonObject().get("boolean").getAsJsonPrimitive().isBoolean());
    check(element.getAsJsonObject().get("boolean").getAsJsonPrimitive().getAsBoolean());
    check(element.getAsJsonObject().get("int32").getAsJsonPrimitive().isNumber());
    check(element.getAsJsonObject().get("int32").getAsJsonPrimitive().getAsNumber().intValue()).is(32);
    check(element.getAsJsonObject().get("int64").getAsJsonPrimitive().isNumber());
    check(element.getAsJsonObject().get("int64").getAsJsonPrimitive().getAsNumber().longValue()).is(64L);
    check(element.getAsJsonObject().get("double").getAsJsonPrimitive().isNumber());
    check(element.getAsJsonObject().get("double").getAsJsonPrimitive().getAsNumber().doubleValue()).is(42.42D);
    check(element.getAsJsonObject().get("string").getAsJsonPrimitive().isString());
    check(element.getAsJsonObject().get("string").getAsJsonPrimitive().getAsString()).is("foo");
    check(element.getAsJsonObject().get("null").isJsonNull());
    check(element.getAsJsonObject().get("array").isJsonArray());
    check(element.getAsJsonObject().get("object").isJsonObject());
}
Also used : BsonNull(org.bson.BsonNull) BsonInt64(org.bson.BsonInt64) BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) JsonElement(com.google.gson.JsonElement) BsonArray(org.bson.BsonArray) BsonDouble(org.bson.BsonDouble) BsonDocumentReader(org.bson.BsonDocumentReader) BsonBoolean(org.bson.BsonBoolean) Test(org.junit.Test)

Aggregations

BsonInt64 (org.bson.BsonInt64)44 BsonDocument (org.bson.BsonDocument)36 BsonString (org.bson.BsonString)30 BsonInt32 (org.bson.BsonInt32)20 BsonArray (org.bson.BsonArray)16 BsonDouble (org.bson.BsonDouble)13 BsonValue (org.bson.BsonValue)11 Test (org.junit.Test)7 MongoNamespace (com.mongodb.MongoNamespace)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 CommandSucceededEvent (com.mongodb.event.CommandSucceededEvent)3 BsonDocumentReader (org.bson.BsonDocumentReader)3 BsonRegularExpression (org.bson.BsonRegularExpression)3 ObjectId (org.bson.types.ObjectId)3 Test (org.junit.jupiter.api.Test)3 SingleMapReaderImpl (org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl)2 BsonBoolean (org.bson.BsonBoolean)2 BsonDateTime (org.bson.BsonDateTime)2 BsonNull (org.bson.BsonNull)2