@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()); }
@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()); } }
@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()); }
/** * Reads the first value in the given {@link BsonValue}, assuming that it is a * {@link BsonDocument}. * * @param bsonValue the BsonDocument to read * @return the first value */ private Object readFirstValue(final BsonValue bsonValue) { if (bsonValue == null) { throw new ConversionException("Expected a BsonDocument but the given value was a null"); } else if (!bsonValue.isDocument()) { throw new ConversionException( "Expected a BsonDocument but the given value was a " + bsonValue.getBsonType().name()); } try (final BsonDocumentReader reader = new BsonDocumentReader((BsonDocument) bsonValue)) { reader.readStartDocument(); reader.readName(); final Object value = decodeValue(readBsonValue(reader, DecoderContext.builder().build()), null); return value; } }
/** * Writes a logical operation of the following form: * * <pre> * { $operator: [ { <operand1> }, { <operand2> } , ... , {<operandN> } ] } * </pre> * * @param operator the operator to write * @param operands the operands to write */ private void writeLogicalOperation(final MongoOperator operator, final List<Expression> operands) { this.writer.writeStartArray(operator.getLiteral()); for (Expression operand : operands) { final BsonDocument operandDocument = new BsonDocument(); try (final BsonDocumentWriter operandBsonWriter = new BsonDocumentWriter(operandDocument);) { try ( final BsonDocumentReader operandBsonReader = new BsonDocumentReader(operandDocument);) { final FilterExpressionEncoder operandEncoder = new FilterExpressionEncoder(this.queryMetadataClass, this.queryMetadataVarName, operandBsonWriter, this.encoderContext); operand.accept(operandEncoder); this.writer.pipe(operandBsonReader); } } } this.writer.writeEndArray(); }
@Nullable private JsonObject convertUpsertId(@Nullable BsonValue upsertId) { JsonObject jsonUpsertId; if (upsertId != null) { JsonObjectCodec jsonObjectCodec = new JsonObjectCodec(new JsonObject()); BsonDocument upsertIdDocument = new BsonDocument(); upsertIdDocument.append(ID_FIELD, upsertId); BsonDocumentReader bsonDocumentReader = new BsonDocumentReader(upsertIdDocument); jsonUpsertId = jsonObjectCodec.decode(bsonDocumentReader, DecoderContext.builder().build()); } else { jsonUpsertId = null; } return jsonUpsertId; }
/** * 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()); }
@Test public void readFrom1() throws Exception { BsonDocument bsonDocument = getBsonDocument(); BsonDocumentConverter bsonDocumentConverter = BsonValueConverterRepertory.getBsonDocumentConverter(); long start = System.nanoTime(); BsonDocumentReader bsonDocumentReader = new BsonDocumentReader(bsonDocument); BsonTest bsonTest = bsonDocumentConverter.decode(bsonDocumentReader, BsonTest.class, BsonMapperConfig.DEFALUT); System.out.println(System.nanoTime() - start); long start2 = System.nanoTime(); BsonTest bsonTest1 = bsonDocumentConverter.decode(bsonDocument, BsonTest.class, BsonMapperConfig.DEFALUT); System.out.println(System.nanoTime() - start2); System.out.println(bsonTest); System.out.println(bsonTest1); }
protected final <T> T deserializeWithCodec(Codec<T> codec, BsonValue value) { checkArgument(codec != null, "Cannot find a valid codec to deserialize: " + value); final BsonDocument document = new BsonDocument("result", value); final BsonReader reader = new BsonDocumentReader(document); reader.readStartDocument(); reader.readName(); return codec.decode(reader, DecoderContext.builder().build()); }
@Test public void testIntType() throws IOException { BsonDocument bsonDoc = new BsonDocument(); bsonDoc.append("seqNo", new BsonInt64(10)); writer.reset(); bsonReader.write(writer, new BsonDocumentReader(bsonDoc)); SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader(); assertEquals(10l, mapReader.reader("seqNo").readLong().longValue()); }
@Test public void testTimeStampType() throws IOException { BsonDocument bsonDoc = new BsonDocument(); bsonDoc.append("ts", new BsonTimestamp(1000, 10)); writer.reset(); bsonReader.write(writer, new BsonDocumentReader(bsonDoc)); SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader(); assertEquals(1000l, mapReader.reader("ts").readDateTime().getMillis()); }
@Test public void testSymbolType() throws IOException { BsonDocument bsonDoc = new BsonDocument(); bsonDoc.append("symbolKey", new BsonSymbol("test_symbol")); writer.reset(); bsonReader.write(writer, new BsonDocumentReader(bsonDoc)); SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader(); assertEquals("test_symbol", mapReader.reader("symbolKey").readText().toString()); }
@Test public void testStringType() throws IOException { BsonDocument bsonDoc = new BsonDocument(); bsonDoc.append("stringKey", new BsonString("test_string")); writer.reset(); bsonReader.write(writer, new BsonDocumentReader(bsonDoc)); SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader(); assertEquals("test_string", mapReader.reader("stringKey").readText().toString()); }
@Test public void testSpecialCharStringType() throws IOException { BsonDocument bsonDoc = new BsonDocument(); bsonDoc.append("stringKey", new BsonString("§§§§§§§§§1")); writer.reset(); bsonReader.write(writer, new BsonDocumentReader(bsonDoc)); SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader(); assertEquals("§§§§§§§§§1", mapReader.reader("stringKey").readText().toString()); }
@Test public void testObjectIdType() throws IOException { BsonDocument bsonDoc = new BsonDocument(); BsonObjectId value = new BsonObjectId(new ObjectId()); bsonDoc.append("_idKey", value); writer.reset(); bsonReader.write(writer, new BsonDocumentReader(bsonDoc)); SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader(); byte[] readByteArray = mapReader.reader("_idKey").readByteArray(); assertTrue(Arrays.equals(value.getValue().toByteArray(), readByteArray)); }
@Test public void testNullType() throws IOException { BsonDocument bsonDoc = new BsonDocument(); bsonDoc.append("nullKey", new BsonNull()); writer.reset(); bsonReader.write(writer, new BsonDocumentReader(bsonDoc)); SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader(); assertEquals(null, mapReader.reader("nullKey").readObject()); }
@Test public void testDoubleType() throws IOException { BsonDocument bsonDoc = new BsonDocument(); bsonDoc.append("doubleKey", new BsonDouble(12.35)); writer.reset(); bsonReader.write(writer, new BsonDocumentReader(bsonDoc)); SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader(); assertEquals(12.35d, mapReader.reader("doubleKey").readDouble().doubleValue(), 0.00001); }
@Test public void testDateTimeType() throws IOException { BsonDocument bsonDoc = new BsonDocument(); bsonDoc.append("dateTimeKey", new BsonDateTime(5262729712L)); writer.reset(); bsonReader.write(writer, new BsonDocumentReader(bsonDoc)); SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader(); assertEquals(5262729712L, mapReader.reader("dateTimeKey").readDateTime().getMillis()); }
@Test public void testBooleanType() throws IOException { BsonDocument bsonDoc = new BsonDocument(); bsonDoc.append("booleanKey", new BsonBoolean(true)); writer.reset(); bsonReader.write(writer, new BsonDocumentReader(bsonDoc)); SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader(); assertTrue(mapReader.reader("booleanKey").readBoolean()); }
private void shouldDecodeDocument(boolean loggerEnabled) { // given final BsonReader bsonReader = new BsonDocumentReader(expectation); final DecoderContext decoderContext = DecoderContext.builder().build(); // when final Foo actual = new DocumentCodec<Foo>(Foo.class, DEFAULT_CODEC_REGISTRY).decode(bsonReader, decoderContext); // then assertEquals(javaObject, actual); }
@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); } }
private ProcessingProtectedRegion read(BsonDocument document) { return processingProtectedRegionCodec.decode(new BsonDocumentReader(document), DecoderContext.builder().build()); }