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;
}
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());
}
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());
}
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());
}
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);
}
}
Aggregations