org.bson.BsonBinary Java Examples

The following examples show how to use org.bson.BsonBinary. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MongoDB.java    From aion with MIT License 7 votes vote down vote up
/**
 * Adds a new edit to the batch
 *
 * @param key the key to write
 * @param value the value to write. Null indicates we should delete this key
 * @return this
 */
public WriteBatch addEdit(byte[] key, byte[] value) {
    if (value == null) {
        DeleteOneModel deleteModel =
                new DeleteOneModel<>(eq(MongoConstants.ID_FIELD_NAME, new BsonBinary(key)));

        edits.add(deleteModel);
    } else {
        UpdateOneModel updateModel =
                new UpdateOneModel<>(
                        eq(MongoConstants.ID_FIELD_NAME, new BsonBinary(key)),
                        Updates.set(MongoConstants.VALUE_FIELD_NAME, new BsonBinary(value)),
                        new UpdateOptions().upsert(true));

        edits.add(updateModel);
    }

    return this;
}
 
Example #2
Source File: BytesFieldConverter.java    From mongo-kafka with Apache License 2.0 6 votes vote down vote up
@Override
public BsonValue toBson(final Object data) {

  // obviously SinkRecords may contain different types
  // to represent byte arrays
  if (data instanceof ByteBuffer) {
    return new BsonBinary(((ByteBuffer) data).array());
  }

  if (data instanceof byte[]) {
    return new BsonBinary((byte[]) data);
  }

  throw new DataException(
      "Error: bytes field conversion failed due to unexpected object type "
          + data.getClass().getName());
}
 
Example #3
Source File: GridFSTest.java    From mongo-java-driver-rx with Apache License 2.0 5 votes vote down vote up
private BsonDocument parseHexDocument(final BsonDocument document, final String hexDocument) {
    if (document.containsKey(hexDocument) && document.get(hexDocument).isDocument()) {
        byte[] bytes = DatatypeConverter.parseHexBinary(document.getDocument(hexDocument).getString("$hex").getValue());
        document.put(hexDocument, new BsonBinary(bytes));
    }
    return document;
}
 
Example #4
Source File: GridFSTest.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
private BsonDocument parseHexDocument(final BsonDocument document, final String hexDocument) {
    if (document.containsKey(hexDocument) && document.get(hexDocument).isDocument()) {
        byte[] bytes = DatatypeConverter.parseHexBinary(document.getDocument(hexDocument).getString("$hex").getValue());
        document.put(hexDocument, new BsonBinary(bytes));
    }
    return document;
}
 
Example #5
Source File: ClientEncryptionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<BsonValue> decrypt(final BsonBinary value) {
    return new ObservableToPublisher<BsonValue>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<BsonValue>>(){
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<BsonValue> callback) {
                    wrapped.decrypt(value, callback);
                }
            }));
}
 
Example #6
Source File: ClientEncryptionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<BsonBinary> encrypt(final BsonValue value, final EncryptOptions options) {
    return new ObservableToPublisher<BsonBinary>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<BsonBinary>>(){
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<BsonBinary> callback) {
                    wrapped.encrypt(value, options, callback);
                }
            }));
}
 
Example #7
Source File: ClientEncryptionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
@Override
public Publisher<BsonBinary> createDataKey(final String kmsProvider, final DataKeyOptions dataKeyOptions) {
    return new ObservableToPublisher<BsonBinary>(com.mongodb.async.client.Observables.observe(
            new Block<com.mongodb.async.SingleResultCallback<BsonBinary>>(){
                @Override
                public void apply(final com.mongodb.async.SingleResultCallback<BsonBinary> callback) {
                    wrapped.createDataKey(kmsProvider, dataKeyOptions, callback);
                }
            }));
}
 
Example #8
Source File: TypeConversionTest.java    From immutables with Apache License 2.0 5 votes vote down vote up
@Test
void binary() throws IOException {
  byte[] data = {1, 2, 3};
  JsonParser parser = Parsers.parserAt(new BsonBinary(data));
  check(parser.getCurrentToken()).is(JsonToken.VALUE_EMBEDDED_OBJECT);
  check(parser.getBinaryValue()).isOf((byte) 1, (byte) 2, (byte) 3);
  check(parser.getBinaryValue()).isOf((byte) 1, (byte) 2, (byte) 3);

  Assertions.assertThrows(JsonParseException.class, parser::getNumberType);
  Assertions.assertThrows(JsonParseException.class, parser::getBooleanValue);
}
 
Example #9
Source File: CoreDocumentSynchronizationConfig.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
BsonDocument toBsonDocument() {
  docLock.readLock().lock();
  try {
    final BsonDocument asDoc = new BsonDocument();
    asDoc.put(ConfigCodec.Fields.DOCUMENT_ID_FIELD, getDocumentId());
    asDoc.put(ConfigCodec.Fields.SCHEMA_VERSION_FIELD, new BsonInt32(1));
    asDoc.put(ConfigCodec.Fields.NAMESPACE_FIELD, new BsonString(getNamespace().toString()));
    asDoc.put(ConfigCodec.Fields.LAST_RESOLUTION_FIELD, new BsonInt64(getLastResolution()));
    if (getLastKnownRemoteVersion() != null) {
      asDoc.put(ConfigCodec.Fields.LAST_KNOWN_REMOTE_VERSION_FIELD, getLastKnownRemoteVersion());
    }
    asDoc.put(ConfigCodec.Fields.LAST_KNOWN_HASH_FIELD, new BsonInt64(lastKnownHash));

    if (lastUncommittedChangeEvent != null) {
      final BsonDocument ceDoc = lastUncommittedChangeEvent.toBsonDocument();
      final OutputBuffer outputBuffer = new BasicOutputBuffer();
      final BsonWriter innerWriter = new BsonBinaryWriter(outputBuffer);
      bsonDocumentCodec.encode(innerWriter, ceDoc, EncoderContext.builder().build());
      final BsonBinary encoded = new BsonBinary(outputBuffer.toByteArray());
      // TODO: This may put the doc above the 16MiB but ignore for now.
      asDoc.put(ConfigCodec.Fields.LAST_UNCOMMITTED_CHANGE_EVENT, encoded);
    }
    asDoc.put(ConfigCodec.Fields.IS_STALE, new BsonBoolean(isStale));
    asDoc.put(ConfigCodec.Fields.IS_PAUSED, new BsonBoolean(isPaused));
    return asDoc;
  } finally {
    docLock.readLock().unlock();
  }
}
 
Example #10
Source File: BytesFieldConverter.java    From kafka-connect-mongodb with Apache License 2.0 5 votes vote down vote up
@Override
public BsonValue toBson(Object data) {

    //obviously SinkRecords may contain different types
    //to represent byte arrays
    if(data instanceof ByteBuffer)
        return new BsonBinary(((ByteBuffer) data).array());

    if(data instanceof byte[])
        return new BsonBinary((byte[])data);

    throw new DataException("error: bytes field conversion failed to due"
            + " unexpected object type "+ data.getClass().getName());

}
 
Example #11
Source File: MongoDB.java    From aion with MIT License 5 votes vote down vote up
@Override
protected byte[] getInternal(byte[] k) {
    BsonDocument document =
            this.collection
                    .find(
                            this.clientSession,
                            eq(MongoConstants.ID_FIELD_NAME, new BsonBinary(k)))
                    .first();
    if (document == null) {
        return null;
    } else {
        return document.getBinary(MongoConstants.VALUE_FIELD_NAME).getData();
    }
}
 
Example #12
Source File: SinkFieldConverterTest.java    From mongo-kafka with Apache License 2.0 5 votes vote down vote up
@TestFactory
@DisplayName("tests for bytes field conversions based on byte[]")
List<DynamicTest> testBytesFieldConverterByteArray() {
  SinkFieldConverter converter = new BytesFieldConverter();
  List<DynamicTest> tests = new ArrayList<>();
  asList(new byte[] {-128, -127, 0}, new byte[] {}, new byte[] {0, 126, 127})
      .forEach(
          el ->
              tests.add(
                  dynamicTest(
                      "conversion with "
                          + converter.getClass().getSimpleName()
                          + " for "
                          + Arrays.toString(el),
                      () -> assertEquals(el, ((BsonBinary) converter.toBson(el)).getData()))));

  tests.add(
      dynamicTest(
          "optional type conversions",
          () -> {
            Schema valueOptionalDefault =
                SchemaBuilder.bytes().optional().defaultValue(new byte[] {});
            assertAll(
                "checks",
                () ->
                    assertThrows(
                        DataException.class, () -> converter.toBson(null, Schema.BYTES_SCHEMA)),
                () ->
                    assertEquals(
                        new BsonNull(), converter.toBson(null, Schema.OPTIONAL_BYTES_SCHEMA)),
                () ->
                    assertEquals(
                        valueOptionalDefault.defaultValue(),
                        ((BsonBinary) converter.toBson(null, valueOptionalDefault)).getData()));
          }));

  return tests;
}
 
Example #13
Source File: SinkDocumentTest.java    From mongo-kafka with Apache License 2.0 5 votes vote down vote up
@BeforeAll
static void initBsonDocs() {

  flatStructKey = new BsonDocument();
  flatStructKey.put("_id", new BsonObjectId(ObjectId.get()));
  flatStructKey.put("myBoolean", new BsonBoolean(true));
  flatStructKey.put("myInt", new BsonInt32(42));
  flatStructKey.put("myBytes", new BsonBinary(new byte[] {65, 66, 67}));
  BsonArray ba1 = new BsonArray();
  ba1.addAll(asList(new BsonInt32(1), new BsonInt32(2), new BsonInt32(3)));
  flatStructKey.put("myArray", ba1);

  flatStructValue = new BsonDocument();
  flatStructValue.put("myLong", new BsonInt64(42L));
  flatStructValue.put("myDouble", new BsonDouble(23.23d));
  flatStructValue.put("myString", new BsonString("BSON"));
  flatStructValue.put("myBytes", new BsonBinary(new byte[] {120, 121, 122}));
  BsonArray ba2 = new BsonArray();
  ba2.addAll(asList(new BsonInt32(9), new BsonInt32(8), new BsonInt32(7)));
  flatStructValue.put("myArray", ba2);

  nestedStructKey = new BsonDocument();
  nestedStructKey.put("_id", new BsonDocument("myString", new BsonString("doc")));
  nestedStructKey.put(
      "mySubDoc", new BsonDocument("mySubSubDoc", new BsonDocument("myInt", new BsonInt32(23))));

  nestedStructValue = new BsonDocument();
  nestedStructValue.put("mySubDocA", new BsonDocument("myBoolean", new BsonBoolean(false)));
  nestedStructValue.put(
      "mySubDocB",
      new BsonDocument(
          "mySubSubDocC", new BsonDocument("myString", new BsonString("some text..."))));
}
 
Example #14
Source File: UuidProvidedStrategy.java    From mongo-kafka with Apache License 2.0 5 votes vote down vote up
@Override
public BsonValue generateId(final SinkDocument doc, final SinkRecord orig) {
  BsonValue id = super.generateId(doc, orig);

  if (id.isBinary() && BsonBinarySubType.isUuid(id.asBinary().getType())) {
    return id;
  } else if (id.isString()) {
    return new BsonBinary(
        constructUuidObjectFromString(id.asString().getValue()), UuidRepresentation.STANDARD);
  }

  throw new DataException(format("UUID cannot be constructed from provided value: `%s`", id));
}
 
Example #15
Source File: UuidStrategy.java    From mongo-kafka with Apache License 2.0 5 votes vote down vote up
@Override
public BsonValue generateId(final SinkDocument doc, final SinkRecord orig) {
  UUID uuid = UUID.randomUUID();
  if (outputFormat.equals(UuidBsonFormat.STRING)) {
    return new BsonString(uuid.toString());
  }

  return new BsonBinary(uuid, UuidRepresentation.STANDARD);
}
 
Example #16
Source File: DocumentWriter.java    From morphia with Apache License 2.0 4 votes vote down vote up
@Override
public void writeBinaryData(final BsonBinary binary) {
    state.value(binary);
}
 
Example #17
Source File: DocumentWriter.java    From morphia with Apache License 2.0 4 votes vote down vote up
@Override
public void writeBinaryData(final String name, final BsonBinary binary) {
    state.name(name).value(binary);
}
 
Example #18
Source File: DocumentReader.java    From morphia with Apache License 2.0 4 votes vote down vote up
@Override
public BsonBinary readBinaryData(final String name) {
    verifyName(name);
    return readBinaryData();
}
 
Example #19
Source File: DocumentReader.java    From morphia with Apache License 2.0 4 votes vote down vote up
@Override
public int peekBinarySize() {
    return stage().<BsonBinary>value().getData().length;
}
 
Example #20
Source File: DocumentReader.java    From morphia with Apache License 2.0 4 votes vote down vote up
@Override
public byte peekBinarySubType() {
    return stage().<BsonBinary>value().getType();
}
 
Example #21
Source File: DocumentReader.java    From morphia with Apache License 2.0 4 votes vote down vote up
@Override
public BsonBinary readBinaryData() {
    return (BsonBinary) stage().value();
}
 
Example #22
Source File: BsonRecordReader.java    From Bats with Apache License 2.0 4 votes vote down vote up
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 #23
Source File: BsonGenerator.java    From immutables with Apache License 2.0 4 votes vote down vote up
@Override
public void writeBinary(Base64Variant bv, byte[] data, int offset, int len) throws IOException {
  BsonBinary binary = new BsonBinary(Arrays.copyOfRange(data, offset, offset + len));
  writer.writeBinaryData(binary);
}
 
Example #24
Source File: TypeAdapters.java    From immutables with Apache License 2.0 4 votes vote down vote up
@Override
public void write(JsonWriter out, byte[] value) throws IOException {
  checkArgument(out instanceof BsonWriter, "Should be BsonWriter, not some other JsonWriter");
  checkNotNull(value, "Value could not be null, delegate to #nullSafe() adapter if needed");
  ((BsonWriter) out).unwrap().writeBinaryData(new BsonBinary(value));
}
 
Example #25
Source File: WritecontextTest.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
@Test
    public void parseTestAbbreviation_BsonValueArray() throws IOException {

        BsonInt32[] bsonInt32s = new BsonInt32[40];
        for (int i = 0; i < 40; i++) {
            bsonInt32s[i] = new BsonInt32(i + 1);
        }

        BsonDocument document = new BsonDocument()
                .append("double", new BsonDouble(12.3))
                .append("arrayInt", new BsonArray(Arrays.asList(bsonInt32s)))
//                .append("arrayInt", new BsonArray({1,1,1,1,1,1,1,1,1,1,1})
                .append("binary1", new BsonBinary(new byte[]{(byte) 0xe0, 0x4f, (byte) 0xd0,
                        0x20, (byte) 0xea, 0x3a, 0x69, 0x10, (byte) 0xa2, (byte) 0xd8, 0x08, 0x00, 0x2b,
                        0x30, 0x30, (byte) 0x9d, (byte) 0xe0, 0x4f, (byte) 0xd0,
                        0x20, (byte) 0xea, 0x3a, 0x69, 0x10, (byte) 0xa2, (byte) 0xd8, 0x08, 0x00, 0x2b,
                        0x30, 0x30, (byte) 0x9d, (byte) 0xe0, 0x4f, (byte) 0xd0,
                        0x20, (byte) 0xea, 0x3a, 0x69, 0x10, (byte) 0xa2, (byte) 0xd8, 0x08, 0x00, 0x2b,
                        0x30, 0x30, (byte) 0x9d, (byte) 0xe0, 0x4f, (byte) 0xd0,
                        0x20, (byte) 0xea, 0x3a, 0x69, 0x10, (byte) 0xa2, (byte) 0xd8, 0x08, 0x00, 0x2b,
                        0x30, 0x30, (byte) 0x9d, (byte) 0xe0, 0x4f, (byte) 0xd0,
                        0x20, (byte) 0xea, 0x3a, 0x69, 0x10, (byte) 0xa2, (byte) 0xd8, 0x08, 0x00, 0x2b,
                        0x30, 0x30, (byte) 0x9d, (byte) 0xe0, 0x4f, (byte) 0xd0,
                        0x20, (byte) 0xea, 0x3a, 0x69, 0x10, (byte) 0xa2, (byte) 0xd8, 0x08, 0x00, 0x2b,
                        0x30, 0x30, (byte) 0x9d, (byte) 0xe0, 0x4f, (byte) 0xd0,
                        0x20, (byte) 0xea, 0x3a, 0x69, 0x10, (byte) 0xa2, (byte) 0xd8, 0x08, 0x00, 0x2b,
                        0x30, 0x30, (byte) 0x9d}))
                .append("oldBinary", new BsonBinary(BsonBinarySubType.OLD_BINARY, new byte[]{1, 1, 1, 1, 1}));

        BasicDBObject query = new BasicDBObject();
        query.put("ComplexBson", document);

        logger.debug("document:{}", document);

        NormalizedBson stringStringValue = MongoUtil.parseBson(new Object[]{query}, true);
        logger.debug("val:{}", stringStringValue);

        List list = objectMapper.readValue("[" + stringStringValue.getNormalizedBson() + "]", List.class);
        Assert.assertEquals(list.size(), 1);

        Map<String, ?> query1Map = (Map<String, ?>) list.get(0);

        checkValue(query1Map);
    }
 
Example #26
Source File: WritecontextTest.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
@Test
public void parseBsonArrayWithValues() throws IOException {

    BsonValue a = new BsonString("stest");
    BsonValue b = new BsonDouble(111);
    BsonValue c = new BsonBoolean(true);

    BsonDocument document = new BsonDocument()
            .append("int32", new BsonInt32(12))
            .append("int64", new BsonInt64(77L))
            .append("bo\"olean", new BsonBoolean(true))
            .append("date", new BsonDateTime(new Date().getTime()))
            .append("double", new BsonDouble(12.3))
            .append("string", new BsonString("pinpoint"))
            .append("objectId", new BsonObjectId(new ObjectId()))
            .append("code", new BsonJavaScript("int i = 10;"))
            .append("codeWithScope", new BsonJavaScriptWithScope("int x = y", new BsonDocument("y", new BsonInt32(1))))
            .append("regex", new BsonRegularExpression("^test.*regex.*xyz$", "big"))
            .append("symbol", new BsonSymbol("wow"))
            .append("timestamp", new BsonTimestamp(0x12345678, 5))
            .append("undefined", new BsonUndefined())
            .append("binary1", new BsonBinary(new byte[]{(byte) 0xe0, 0x4f, (byte) 0xd0, 0x20}))
            .append("oldBinary", new BsonBinary(BsonBinarySubType.OLD_BINARY, new byte[]{1, 1, 1, 1, 1}))
            .append("arrayInt", new BsonArray(Arrays.asList(a, b, c, new BsonInt32(7))))
            .append("document", new BsonDocument("a", new BsonInt32(77)))
            .append("dbPointer", new BsonDbPointer("db.coll", new ObjectId()))
            .append("null", new BsonNull())
            .append("decimal128", new BsonDecimal128(new Decimal128(55)));

    BasicDBObject query = new BasicDBObject();
    query.put("ComplexBson", document);

    logger.debug("document:{}", document);

    NormalizedBson stringStringValue = MongoUtil.parseBson(new Object[]{query}, true);
    logger.debug("val:{}", stringStringValue);

    List list = objectMapper.readValue("[" + stringStringValue.getNormalizedBson() + "]", List.class);
    Assert.assertEquals(list.size(), 1);
    Map<String, ?> query1Map = (Map<String, ?>) list.get(0);

    checkValue(query1Map);
}
 
Example #27
Source File: ClientEncryptionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 4 votes vote down vote up
@Override
public Publisher<BsonBinary> createDataKey(final String kmsProvider) {
    return createDataKey(kmsProvider, new DataKeyOptions());
}
 
Example #28
Source File: CoreDocumentSynchronizationConfig.java    From stitch-android-sdk with Apache License 2.0 4 votes vote down vote up
static CoreDocumentSynchronizationConfig fromBsonDocument(final BsonDocument document) {
  keyPresent(ConfigCodec.Fields.DOCUMENT_ID_FIELD, document);
  keyPresent(ConfigCodec.Fields.NAMESPACE_FIELD, document);
  keyPresent(ConfigCodec.Fields.SCHEMA_VERSION_FIELD, document);
  keyPresent(ConfigCodec.Fields.LAST_RESOLUTION_FIELD, document);
  keyPresent(ConfigCodec.Fields.IS_STALE, document);
  keyPresent(ConfigCodec.Fields.IS_PAUSED, document);

  final int schemaVersion =
      document.getNumber(ConfigCodec.Fields.SCHEMA_VERSION_FIELD).intValue();
  if (schemaVersion != 1) {
    throw new IllegalStateException(
        String.format(
            "unexpected schema version '%d' for %s",
            schemaVersion,
            CoreDocumentSynchronizationConfig.class.getSimpleName()));
  }

  final MongoNamespace namespace =
      new MongoNamespace(document.getString(ConfigCodec.Fields.NAMESPACE_FIELD).getValue());

  final BsonDocument lastVersion;
  if (document.containsKey(ConfigCodec.Fields.LAST_KNOWN_REMOTE_VERSION_FIELD)) {
    lastVersion = document.getDocument(ConfigCodec.Fields.LAST_KNOWN_REMOTE_VERSION_FIELD);
  } else {
    lastVersion = null;
  }

  final ChangeEvent<BsonDocument> lastUncommittedChangeEvent;
  if (document.containsKey(ConfigCodec.Fields.LAST_UNCOMMITTED_CHANGE_EVENT)) {
    final BsonBinary eventBin =
        document.getBinary(ConfigCodec.Fields.LAST_UNCOMMITTED_CHANGE_EVENT);
    final BsonReader innerReader = new BsonBinaryReader(ByteBuffer.wrap(eventBin.getData()));
    lastUncommittedChangeEvent = ResultDecoders.changeEventDecoder(BSON_DOCUMENT_CODEC)
        .decode(innerReader, DecoderContext.builder().build());
  } else {
    lastUncommittedChangeEvent = null;
  }

  return new CoreDocumentSynchronizationConfig(
      null,
      namespace,
      document.get(ConfigCodec.Fields.DOCUMENT_ID_FIELD),
      lastUncommittedChangeEvent,
      document.getNumber(ConfigCodec.Fields.LAST_RESOLUTION_FIELD).longValue(),
      lastVersion,
      new ReentrantReadWriteLock(),
      document.getBoolean(ConfigCodec.Fields.IS_STALE).getValue(),
      document.getBoolean(ConfigCodec.Fields.IS_PAUSED, new BsonBoolean(false)).getValue(),
      document.getInt64(ConfigCodec.Fields.LAST_KNOWN_HASH_FIELD, new BsonInt64(0))
        .getValue());
}
 
Example #29
Source File: SinkFieldConverterTest.java    From mongo-kafka with Apache License 2.0 4 votes vote down vote up
@TestFactory
@DisplayName("tests for bytes field conversions based on ByteBuffer")
List<DynamicTest> testBytesFieldConverterByteBuffer() {
  SinkFieldConverter converter = new BytesFieldConverter();
  List<DynamicTest> tests = new ArrayList<>();

  asList(
          ByteBuffer.wrap(new byte[] {-128, -127, 0}),
          ByteBuffer.wrap(new byte[] {}),
          ByteBuffer.wrap(new byte[] {0, 126, 127}))
      .forEach(
          el ->
              tests.add(
                  dynamicTest(
                      "conversion with "
                          + converter.getClass().getSimpleName()
                          + " for "
                          + el.toString()
                          + " -> "
                          + Arrays.toString(el.array()),
                      () ->
                          assertEquals(
                              el.array(), ((BsonBinary) converter.toBson(el)).getData()))));

  tests.add(
      dynamicTest(
          "optional type conversions",
          () -> {
            Schema valueOptionalDefault =
                SchemaBuilder.bytes().optional().defaultValue(ByteBuffer.wrap(new byte[] {}));
            assertAll(
                "checks",
                () ->
                    assertThrows(
                        DataException.class, () -> converter.toBson(null, Schema.BYTES_SCHEMA)),
                () ->
                    assertEquals(
                        new BsonNull(), converter.toBson(null, Schema.OPTIONAL_BYTES_SCHEMA)),
                () ->
                    assertEquals(
                        ((ByteBuffer) valueOptionalDefault.defaultValue()).array(),
                        ((BsonBinary) converter.toBson(null, valueOptionalDefault)).getData()));
          }));
  return tests;
}
 
Example #30
Source File: ClientEncryption.java    From mongo-java-driver-reactivestreams with Apache License 2.0 2 votes vote down vote up
/**
 * Decrypt the given value.
 *
 * @param value the value to decrypt, which must be of subtype 6
 * @return a Publisher containing the decrypted value
 */
Publisher<BsonValue> decrypt(BsonBinary value);