Search in sources :

Example 21 with BsonDocumentReader

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

the class ListCollectionsOperation method projectFromFullNamespaceToCollectionName.

private List<T> projectFromFullNamespaceToCollectionName(final List<BsonDocument> unstripped) {
    if (unstripped == null) {
        return null;
    }
    List<T> stripped = new ArrayList<T>(unstripped.size());
    String prefix = databaseName + ".";
    for (BsonDocument cur : unstripped) {
        String name = cur.getString("name").getValue();
        String collectionName = name.substring(prefix.length());
        cur.put("name", new BsonString(collectionName));
        stripped.add(decoder.decode(new BsonDocumentReader(cur), DecoderContext.builder().build()));
    }
    return stripped;
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) ArrayList(java.util.ArrayList) BsonDocumentReader(org.bson.BsonDocumentReader) BsonString(org.bson.BsonString)

Example 22 with BsonDocumentReader

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

the class CodecTestCase method getDecodedValue.

<T> T getDecodedValue(final BsonValue bsonValue, final Decoder<T> decoder) {
    BsonDocument document = new BsonDocument("val", bsonValue);
    BsonDocumentReader reader = new BsonDocumentReader(document);
    reader.readStartDocument();
    reader.readName("val");
    return decoder.decode(reader, DecoderContext.builder().build());
}
Also used : BsonDocument(org.bson.BsonDocument) BsonDocumentReader(org.bson.BsonDocumentReader)

Example 23 with BsonDocumentReader

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

the class TupleCodecProviderTest method optionalAttribute_nickname.

/**
 * Projection of an optional attribute
 */
@Test
@SuppressWarnings("unchecked")
public void optionalAttribute_nickname() {
    Query query = Query.of(Person.class).addProjections(Matchers.toExpression(PersonCriteria.person.nickName));
    Path idPath = Visitors.toPath(KeyExtractor.defaultFactory().create(Person.class).metadata().keys().get(0));
    TupleCodecProvider provider = new TupleCodecProvider(query, new MongoPathNaming(idPath, PathNaming.defaultNaming()).toExpression());
    Codec<ProjectedTuple> codec = provider.get(ProjectedTuple.class, registry);
    ProjectedTuple tuple1 = codec.decode(new BsonDocumentReader(new BsonDocument("nickName", new BsonString("aaa"))), DecoderContext.builder().build());
    check(tuple1.values()).hasSize(1);
    check((Optional<String>) tuple1.values().get(0)).is(Optional.of("aaa"));
    ProjectedTuple tuple2 = codec.decode(new BsonDocumentReader(new BsonDocument()), DecoderContext.builder().build());
    check(tuple2.values()).hasSize(1);
    check((Optional<String>) tuple2.values().get(0)).is(Optional.empty());
}
Also used : Path(org.immutables.criteria.expression.Path) Query(org.immutables.criteria.expression.Query) BsonDocument(org.bson.BsonDocument) Optional(java.util.Optional) BsonString(org.bson.BsonString) BsonDocumentReader(org.bson.BsonDocumentReader) ProjectedTuple(org.immutables.criteria.backend.ProjectedTuple) Person(org.immutables.criteria.personmodel.Person) Test(org.junit.jupiter.api.Test)

Example 24 with BsonDocumentReader

use of org.bson.BsonDocumentReader 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)

Example 25 with BsonDocumentReader

use of org.bson.BsonDocumentReader in project drill by axbaretto.

the class MongoRecordReader method next.

@Override
public int next() {
    if (cursor == null) {
        logger.info("Filters Applied : " + filters);
        logger.info("Fields Selected :" + fields);
        cursor = collection.find(filters).projection(fields).batchSize(100).iterator();
    }
    writer.allocate();
    writer.reset();
    int docCount = 0;
    Stopwatch watch = Stopwatch.createStarted();
    try {
        while (docCount < BaseValueVector.INITIAL_VALUE_ALLOCATION && cursor.hasNext()) {
            writer.setPosition(docCount);
            if (isBsonRecordReader) {
                BsonDocument bsonDocument = cursor.next();
                bsonReader.write(writer, new BsonDocumentReader(bsonDocument));
            } else {
                String doc = cursor.next().toJson();
                jsonReader.setSource(doc.getBytes(Charsets.UTF_8));
                jsonReader.write(writer);
            }
            docCount++;
        }
        if (isBsonRecordReader) {
            bsonReader.ensureAtLeastOneField(writer);
        } else {
            jsonReader.ensureAtLeastOneField(writer);
        }
        writer.setValueCount(docCount);
        logger.debug("Took {} ms to get {} records", watch.elapsed(TimeUnit.MILLISECONDS), docCount);
        return docCount;
    } catch (IOException e) {
        String msg = "Failure while reading document. - Parser was at record: " + (docCount + 1);
        logger.error(msg, e);
        throw new DrillRuntimeException(msg, e);
    }
}
Also used : BsonDocument(org.bson.BsonDocument) Stopwatch(com.google.common.base.Stopwatch) BsonDocumentReader(org.bson.BsonDocumentReader) IOException(java.io.IOException) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException)

Aggregations

BsonDocumentReader (org.bson.BsonDocumentReader)43 BsonDocument (org.bson.BsonDocument)42 Test (org.junit.Test)30 SingleMapReaderImpl (org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl)29 BaseTest (org.apache.drill.test.BaseTest)15 BsonString (org.bson.BsonString)9 FieldReader (org.apache.drill.exec.vector.complex.reader.FieldReader)6 BsonDocumentWriter (org.bson.BsonDocumentWriter)6 BsonWriter (org.bson.BsonWriter)6 Test (org.junit.jupiter.api.Test)5 ArrayList (java.util.ArrayList)3 BsonBoolean (org.bson.BsonBoolean)3 BsonDateTime (org.bson.BsonDateTime)3 BsonDouble (org.bson.BsonDouble)3 BsonInt32 (org.bson.BsonInt32)3 BsonInt64 (org.bson.BsonInt64)3 BsonNull (org.bson.BsonNull)3 ProjectedTuple (org.immutables.criteria.backend.ProjectedTuple)3 Path (org.immutables.criteria.expression.Path)3 Query (org.immutables.criteria.expression.Query)3