Example #1
0
 @Override
 public void accept(BsonReader reader) {
   reader.readStartArray();
   while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
     BsonSkipper.valueOf(reader.getCurrentBsonType().name()).accept(reader);
   }
   reader.readEndArray();
 }
 private List<Object> readList(final BsonReader reader, final DecoderContext decoderContext) {
   reader.readStartArray();
   List<Object> list = new ArrayList<Object>();
   while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
     list.add(readValue(reader, decoderContext));
   }
   reader.readEndArray();
   return list;
 }
 public void write(ComplexWriter writer, BsonReader reader) throws IOException {
   reader.readStartDocument();
   BsonType readBsonType = reader.getCurrentBsonType();
   switch (readBsonType) {
     case DOCUMENT:
       writeToListOrMap(reader, new MapOrListWriterImpl(writer.rootAsMap()), false, null);
       break;
     default:
       throw new DrillRuntimeException(
           "Root object must be DOCUMENT type. Found: " + readBsonType);
   }
 }
  @Override
  public Document decode(final BsonReader reader, final DecoderContext decoderContext) {
    Document document = new Document();

    reader.readStartDocument();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
      String fieldName = reader.readName();
      document.put(fieldName, readValue(reader, decoderContext));
    }

    reader.readEndDocument();

    return document;
  }
  @Override
  public BsonDocument decode(final BsonReader reader, final DecoderContext decoderContext) {
    List<BsonElement> keyValuePairs = new ArrayList<BsonElement>();

    reader.readStartDocument();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
      String fieldName = reader.readName();
      keyValuePairs.add(new BsonElement(fieldName, readValue(reader, decoderContext)));
    }

    reader.readEndDocument();

    return new BsonDocument(keyValuePairs);
  }
 private Object readValue(final BsonReader reader, final DecoderContext decoderContext) {
   BsonType bsonType = reader.getCurrentBsonType();
   if (bsonType == BsonType.NULL) {
     reader.readNull();
     return null;
   } else if (bsonType == BsonType.ARRAY) {
     return readList(reader, decoderContext);
   } else if (bsonType == BsonType.BINARY) {
     byte bsonSubType = reader.peekBinarySubType();
     if (bsonSubType == BsonBinarySubType.UUID_STANDARD.getValue()
         || bsonSubType == BsonBinarySubType.UUID_LEGACY.getValue()) {
       return registry.get(UUID.class).decode(reader, decoderContext);
     }
   }
   return valueTransformer.transform(
       registry.get(bsonTypeClassMap.get(bsonType)).decode(reader, decoderContext));
 }
 @Override
 protected Object readDateTime(BsonReader reader, DecoderContext ctx) {
   final JsonObject result = new JsonObject();
   result.put(
       DATE_FIELD,
       OffsetDateTime.ofInstant(Instant.ofEpochMilli(reader.readDateTime()), ZoneOffset.UTC)
           .format(ISO_OFFSET_DATE_TIME));
   return result;
 }
 private void writeBinary(
     BsonReader reader, final MapOrListWriterImpl writer, String fieldName, boolean isList) {
   final VarBinaryHolder vb = new VarBinaryHolder();
   BsonBinary readBinaryData = reader.readBinaryData();
   byte[] data = readBinaryData.getData();
   Byte type = (Byte) readBinaryData.getType();
   // Based on specified binary type, cast it accordingly
   switch (type.intValue()) {
     case 1:
       // Double 1
       writeDouble(ByteBuffer.wrap(data).getDouble(), writer, fieldName, isList);
       break;
     case 2:
       // String 2
       writeString(new String(data), writer, fieldName, isList);
       break;
     case 8:
       // Boolean 8
       boolean boolValue = (data == null || data.length == 0) ? false : data[0] != 0x00;
       writeBoolean(boolValue, writer, fieldName, isList);
       break;
     case 9:
       // Date 9
       writeDateTime(ByteBuffer.wrap(data).getLong(), writer, fieldName, isList);
       break;
     case 13:
       // JavaScript 13
       writeString(new String(data), writer, fieldName, isList);
       break;
     case 14:
       // Symbol 14
       writeString(new String(data), writer, fieldName, isList);
       break;
     case 15:
       // JavaScript (with scope) 15
       writeString(new String(data), writer, fieldName, isList);
       break;
     case 16:
       // 32-bit integer 16
       writeInt32(ByteBuffer.wrap(data).getInt(), writer, fieldName, isList);
       break;
     case 17:
       // Timestamp 17
       writeTimeStamp(ByteBuffer.wrap(data).getInt(), writer, fieldName, isList);
       break;
     case 18:
       // 64-bit integer 18
       writeInt64(ByteBuffer.wrap(data).getInt(), writer, fieldName, isList);
       break;
     default:
       // In case of Object(3)/Binary data (5)/Object id (7) or in other case
       // considering as VarBinary
       final byte[] bytes = readBinaryData.getData();
       writeBinary(writer, fieldName, isList, vb, bytes);
       break;
   }
 }
Example #9
0
 @Override
 public void accept(BsonReader reader) {
   reader.readDBPointer();
 }
Example #10
0
 @Override
 public void accept(BsonReader reader) {
   reader.readRegularExpression();
 }
Example #11
0
 @Override
 public void accept(BsonReader reader) {
   reader.readNull();
 }
Example #12
0
 @Override
 public void accept(BsonReader reader) {
   reader.readDateTime();
 }
Example #13
0
 @Override
 public void accept(BsonReader reader) {
   reader.readBoolean();
 }
Example #14
0
 @Override
 public void accept(BsonReader reader) {
   reader.readObjectId();
 }
Example #15
0
 @Override
 public void accept(BsonReader reader) {
   reader.readUndefined();
 }
Example #16
0
 @Override
 public void accept(BsonReader reader) {
   reader.readDouble();
 }
Example #17
0
 @Override
 public void accept(BsonReader reader) {
   reader.readString();
 }
Example #18
0
 @Override
 public void accept(BsonReader reader) {
   reader.readSymbol();
 }
Example #19
0
 @Override
 public void accept(BsonReader reader) {
   reader.readBinaryData();
 }
Example #20
0
 @Override
 public void accept(BsonReader reader) {
   reader.readJavaScriptWithScope();
 }
 /**
  * This method may be overridden to change the behavior of reading the current value from the
  * given {@code BsonReader}. It is required that the value be fully consumed before returning.
  *
  * @param reader the read to read the value from
  * @param decoderContext the context
  * @return the non-null value read from the reader
  */
 protected BsonValue readValue(final BsonReader reader, final DecoderContext decoderContext) {
   return codecRegistry
       .get(BsonValueCodecProvider.getClassForBsonType(reader.getCurrentBsonType()))
       .decode(reader, decoderContext);
 }
Example #22
0
 @Override
 public void accept(BsonReader reader) {
   reader.readTimestamp();
 }
 private void writeObjectId(
     BsonReader reader, final MapOrListWriterImpl writer, String fieldName, boolean isList) {
   final VarBinaryHolder vObj = new VarBinaryHolder();
   final byte[] objBytes = reader.readObjectId().toByteArray();
   writeBinary(writer, fieldName, isList, vObj, objBytes);
 }
Example #24
0
 @Override
 public void accept(BsonReader reader) {
   reader.readInt64();
 }
 private void writeToListOrMap(
     BsonReader reader, final MapOrListWriterImpl writer, boolean isList, String fieldName) {
   writer.start();
   // If isList is true, then filedName can be null as it is not required while
   // writing
   while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
     if (!isList) {
       fieldName = reader.readName();
     }
     BsonType currentBsonType = reader.getCurrentBsonType();
     switch (currentBsonType) {
       case INT32:
         int readInt32 = reader.readInt32();
         if (readNumbersAsDouble) {
           writeDouble(readInt32, writer, fieldName, isList);
         } else {
           writeInt32(readInt32, writer, fieldName, isList);
         }
         atLeastOneWrite = true;
         break;
       case INT64:
         long readInt64 = reader.readInt64();
         if (readNumbersAsDouble) {
           writeDouble(readInt64, writer, fieldName, isList);
         } else {
           writeInt64(readInt64, writer, fieldName, isList);
         }
         atLeastOneWrite = true;
         break;
       case ARRAY:
         reader.readStartArray();
         writeToListOrMap(reader, (MapOrListWriterImpl) writer.list(fieldName), true, fieldName);
         atLeastOneWrite = true;
         break;
       case BINARY:
         // handle types
         writeBinary(reader, writer, fieldName, isList);
         atLeastOneWrite = true;
         break;
       case BOOLEAN:
         boolean readBoolean = reader.readBoolean();
         writeBoolean(readBoolean, writer, fieldName, isList);
         atLeastOneWrite = true;
         break;
       case DATE_TIME:
         long readDateTime = reader.readDateTime();
         writeDateTime(readDateTime, writer, fieldName, isList);
         atLeastOneWrite = true;
         break;
       case DOCUMENT:
         reader.readStartDocument();
         // To handle nested Documents.
         MapOrListWriterImpl _writer = writer;
         if (!isList) {
           _writer = (MapOrListWriterImpl) writer.map(fieldName);
         } else {
           _writer = (MapOrListWriterImpl) writer.listoftmap(fieldName);
         }
         writeToListOrMap(reader, _writer, false, fieldName);
         atLeastOneWrite = true;
         break;
       case DOUBLE:
         double readDouble = reader.readDouble();
         writeDouble(readDouble, writer, fieldName, isList);
         atLeastOneWrite = true;
         break;
       case JAVASCRIPT:
         final String readJavaScript = reader.readJavaScript();
         writeString(readJavaScript, writer, fieldName, isList);
         atLeastOneWrite = true;
         break;
       case JAVASCRIPT_WITH_SCOPE:
         final String readJavaScriptWithScopeString = reader.readJavaScriptWithScope();
         writeString(readJavaScriptWithScopeString, writer, fieldName, isList);
         atLeastOneWrite = true;
         break;
       case NULL:
         // just read and ignore.
         reader.readNull();
         break;
       case OBJECT_ID:
         writeObjectId(reader, writer, fieldName, isList);
         atLeastOneWrite = true;
         break;
       case STRING:
         final String readString = reader.readString();
         writeString(readString, writer, fieldName, isList);
         atLeastOneWrite = true;
         break;
       case SYMBOL:
         final String readSymbol = reader.readSymbol();
         writeString(readSymbol, writer, fieldName, isList);
         atLeastOneWrite = true;
         break;
       case TIMESTAMP:
         int time = reader.readTimestamp().getTime();
         writeTimeStamp(time, writer, fieldName, isList);
         atLeastOneWrite = true;
         break;
       default:
         // Didn't handled REGULAR_EXPRESSION and DB_POINTER types
         throw new DrillRuntimeException("UnSupported Bson type: " + currentBsonType);
     }
   }
   if (!isList) {
     reader.readEndDocument();
   } else {
     reader.readEndArray();
   }
 }
Example #26
0
 @Override
 public void accept(BsonReader reader) {
   reader.readMaxKey();
 }
Example #27
0
 public static void skipValue(BsonReader reader) {
   BsonSkipper.valueOf(reader.getCurrentBsonType().name()).accept(reader);
 }
Example #28
0
 @Override
 public void accept(BsonReader reader) {
   // ??
   reader.readEndDocument(); // ??
 }
Example #29
0
  @Override
  public Ra decode(BsonReader reader, DecoderContext dc) {
    reader.readStartDocument();
    reader.readName();
    String name = reader.readString();
    reader.readName();
    Double pointsTaken = reader.readDouble();
    reader.readName();
    Codec<LocalDate> dateCodec = codecRegistry.get(LocalDate.class);
    LocalDate previousDutyDay = dateCodec.decode(reader, dc);

    // Read Blackout Date Array
    reader.readName();
    ArrayList<LocalDate> blackoutDates = new ArrayList<>();
    reader.readStartArray();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
      blackoutDates.add(dateCodec.decode(reader, dc));
    }
    reader.readEndArray();

    // Read Duty Date Array
    reader.readName();
    ArrayList<LocalDate> dutyDays = new ArrayList<>();
    reader.readStartArray();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
      dutyDays.add(dateCodec.decode(reader, dc));
    }
    reader.readEndArray();
    reader.readEndDocument();

    return new Ra(name, pointsTaken, previousDutyDay, blackoutDates, dutyDays);
  }
 @Override
 protected Object readObjectId(BsonReader reader, DecoderContext ctx) {
   return reader.readObjectId().toHexString();
 }