Search in sources :

Example 1 with BsonWriter

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

the class DocumentCodec method getDocumentId.

@Override
public BsonValue getDocumentId(final Document document) {
    if (!documentHasId(document)) {
        throw new IllegalStateException("The document does not contain an _id");
    }
    Object id = document.get(ID_FIELD_NAME);
    if (id instanceof BsonValue) {
        return (BsonValue) id;
    }
    BsonDocument idHoldingDocument = new BsonDocument();
    BsonWriter writer = new BsonDocumentWriter(idHoldingDocument);
    writer.writeStartDocument();
    writer.writeName(ID_FIELD_NAME);
    writeValue(writer, EncoderContext.builder().build(), id);
    writer.writeEndDocument();
    return idHoldingDocument.get(ID_FIELD_NAME);
}
Also used : BsonDocument(org.bson.BsonDocument) BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonWriter(org.bson.BsonWriter) BsonValue(org.bson.BsonValue)

Example 2 with BsonWriter

use of org.bson.BsonWriter in project drill by apache.

the class TestBsonRecordReader method testArrayOfDocumentType.

@Test
public void testArrayOfDocumentType() throws IOException {
    BsonDocument bsonDoc = new BsonDocument();
    BsonWriter bw = new BsonDocumentWriter(bsonDoc);
    bw.writeStartDocument();
    bw.writeName("a");
    bw.writeString("MongoDB");
    bw.writeName("b");
    bw.writeStartArray();
    bw.writeStartDocument();
    bw.writeName("c");
    bw.writeInt32(1);
    bw.writeEndDocument();
    bw.writeEndArray();
    bw.writeEndDocument();
    bw.flush();
    writer.reset();
    bsonReader.write(writer, new BsonDocumentReader(bsonDoc));
    FieldReader reader = writer.getMapVector().getReader();
    SingleMapReaderImpl mapReader = (SingleMapReaderImpl) reader;
    FieldReader reader3 = mapReader.reader("b");
    assertEquals("MongoDB", mapReader.reader("a").readText().toString());
}
Also used : BsonDocument(org.bson.BsonDocument) BsonDocumentWriter(org.bson.BsonDocumentWriter) SingleMapReaderImpl(org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl) BsonWriter(org.bson.BsonWriter) BsonDocumentReader(org.bson.BsonDocumentReader) FieldReader(org.apache.drill.exec.vector.complex.reader.FieldReader) Test(org.junit.Test)

Example 3 with BsonWriter

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

the class DBObjectCodec method getDocumentId.

@Override
public BsonValue getDocumentId(final DBObject document) {
    if (!documentHasId(document)) {
        throw new IllegalStateException("The document does not contain an _id");
    }
    Object id = document.get(ID_FIELD_NAME);
    if (id instanceof BsonValue) {
        return (BsonValue) id;
    }
    BsonDocument idHoldingDocument = new BsonDocument();
    BsonWriter writer = new BsonDocumentWriter(idHoldingDocument);
    writer.writeStartDocument();
    writer.writeName(ID_FIELD_NAME);
    writeValue(writer, EncoderContext.builder().build(), id);
    writer.writeEndDocument();
    return idHoldingDocument.get(ID_FIELD_NAME);
}
Also used : BsonDocument(org.bson.BsonDocument) BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonWriter(org.bson.BsonWriter) BSONObject(org.bson.BSONObject) BsonValue(org.bson.BsonValue)

Example 4 with BsonWriter

use of org.bson.BsonWriter in project drill by apache.

the class TestBsonRecordReader method testRecursiveDocuments.

@Test
public void testRecursiveDocuments() throws IOException {
    BsonDocument topDoc = new BsonDocument();
    final int count = 3;
    for (int i = 0; i < count; ++i) {
        BsonDocument bsonDoc = new BsonDocument();
        BsonWriter bw = new BsonDocumentWriter(bsonDoc);
        bw.writeStartDocument();
        bw.writeName("k1" + i);
        bw.writeString("drillMongo1" + i);
        bw.writeName("k2" + i);
        bw.writeString("drillMongo2" + i);
        bw.writeEndDocument();
        bw.flush();
        topDoc.append("doc" + i, bsonDoc);
    }
    writer.reset();
    bsonReader.write(writer, new BsonDocumentReader(topDoc));
    SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader();
    for (int i = 0; i < count; ++i) {
        SingleMapReaderImpl reader = (SingleMapReaderImpl) mapReader.reader("doc" + i);
        assertEquals("drillMongo1" + i, reader.reader("k1" + i).readText().toString());
        assertEquals("drillMongo2" + i, reader.reader("k2" + i).readText().toString());
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonDocumentWriter(org.bson.BsonDocumentWriter) SingleMapReaderImpl(org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl) BsonWriter(org.bson.BsonWriter) BsonDocumentReader(org.bson.BsonDocumentReader) Test(org.junit.Test)

Example 5 with BsonWriter

use of org.bson.BsonWriter in project drill by apache.

the class TestBsonRecordReader method testArrayType.

@Test
public void testArrayType() throws IOException {
    BsonDocument bsonDoc = new BsonDocument();
    BsonWriter bw = new BsonDocumentWriter(bsonDoc);
    bw.writeStartDocument();
    bw.writeName("arrayKey");
    bw.writeStartArray();
    bw.writeInt32(1);
    bw.writeInt32(2);
    bw.writeInt32(3);
    bw.writeEndArray();
    bw.writeEndDocument();
    bw.flush();
    bsonReader.write(writer, new BsonDocumentReader(bsonDoc));
    SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader();
    FieldReader reader = mapReader.reader("arrayKey");
    assertEquals(3, reader.size());
}
Also used : BsonDocument(org.bson.BsonDocument) BsonDocumentWriter(org.bson.BsonDocumentWriter) SingleMapReaderImpl(org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl) BsonWriter(org.bson.BsonWriter) BsonDocumentReader(org.bson.BsonDocumentReader) FieldReader(org.apache.drill.exec.vector.complex.reader.FieldReader) Test(org.junit.Test)

Aggregations

BsonDocument (org.bson.BsonDocument)5 BsonDocumentWriter (org.bson.BsonDocumentWriter)5 BsonWriter (org.bson.BsonWriter)5 SingleMapReaderImpl (org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl)3 BsonDocumentReader (org.bson.BsonDocumentReader)3 Test (org.junit.Test)3 FieldReader (org.apache.drill.exec.vector.complex.reader.FieldReader)2 BsonValue (org.bson.BsonValue)2 BSONObject (org.bson.BSONObject)1