org.bson.BsonString Java Examples

The following examples show how to use org.bson.BsonString. 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: MongoDbInsertTest.java    From mongo-kafka with Apache License 2.0 6 votes vote down vote up
@Test
@DisplayName("when valid cdc event then correct ReplaceOneModel")
void testValidSinkDocument() {
  BsonDocument keyDoc = new BsonDocument("id", new BsonString("1234"));
  BsonDocument valueDoc =
      new BsonDocument("op", new BsonString("c"))
          .append("after", new BsonString(REPLACEMENT_DOC.toJson()));

  WriteModel<BsonDocument> result = INSERT.perform(new SinkDocument(keyDoc, valueDoc));

  assertTrue(result instanceof ReplaceOneModel, "result expected to be of type ReplaceOneModel");

  ReplaceOneModel<BsonDocument> writeModel = (ReplaceOneModel<BsonDocument>) result;

  assertEquals(
      REPLACEMENT_DOC,
      writeModel.getReplacement(),
      "replacement doc not matching what is expected");
  assertTrue(
      writeModel.getFilter() instanceof BsonDocument,
      "filter expected to be of type BsonDocument");
  assertEquals(FILTER_DOC, writeModel.getFilter());
  assertTrue(
      writeModel.getReplaceOptions().isUpsert(),
      "replacement expected to be done in upsert mode");
}
 
Example #2
Source File: NamespaceSynchronizationConfig.java    From stitch-android-sdk with Apache License 2.0 6 votes vote down vote up
NamespaceSynchronizationConfig(
    final MongoCollection<NamespaceSynchronizationConfig> namespacesColl,
    final MongoCollection<CoreDocumentSynchronizationConfig> docsColl,
    final MongoNamespace namespace
) {
  this.namespacesColl = namespacesColl;
  this.docsColl = docsColl;
  this.namespace = namespace;
  this.syncedDocuments = new ConcurrentHashMap<>();
  this.nsLock = new ReentrantReadWriteLock();

  // Fill from db
  final BsonDocument docsFilter = new BsonDocument();
  docsFilter.put(
      CoreDocumentSynchronizationConfig.ConfigCodec.Fields.NAMESPACE_FIELD,
      new BsonString(namespace.toString()));
  docsColl.find(docsFilter, CoreDocumentSynchronizationConfig.class)
      .forEach(new Block<CoreDocumentSynchronizationConfig>() {
        @Override
        public void apply(@Nonnull final CoreDocumentSynchronizationConfig docConfig) {
          syncedDocuments.put(docConfig.getDocumentId(), new CoreDocumentSynchronizationConfig(
              docsColl,
              docConfig));
        }
      });
}
 
Example #3
Source File: SinkConverterTest.java    From mongo-kafka with Apache License 2.0 6 votes vote down vote up
@BeforeAll
static void initializeTestData() {
  String jsonString1 = "{\"myField\":\"some text\"}";
  Schema objSchema1 = SchemaBuilder.struct().field("myField", Schema.STRING_SCHEMA);
  Struct objStruct1 = new Struct(objSchema1).put("myField", "some text");

  Map<String, Object> objMap1 = new LinkedHashMap<>();
  objMap1.put("myField", "some text");

  expectedBsonDoc = new BsonDocument("myField", new BsonString("some text"));

  combinations = new HashMap<>();
  combinations.put(jsonString1, null);
  combinations.put(objStruct1, objSchema1);
  combinations.put(objMap1, null);
}
 
Example #4
Source File: RdbmsDeleteTest.java    From kafka-connect-mongodb with Apache License 2.0 6 votes vote down vote up
@Test
@DisplayName("when valid cdc event with single field PK then correct DeleteOneModel")
public void testValidSinkDocumentSingleFieldPK() {

    BsonDocument filterDoc =
            new BsonDocument(DBCollection.ID_FIELD_NAME,
                    new BsonDocument("id",new BsonInt32(1004)));

    BsonDocument keyDoc = new BsonDocument("id",new BsonInt32(1004));
    BsonDocument valueDoc = new BsonDocument("op",new BsonString("d"));

    WriteModel<BsonDocument> result =
            RDBMS_DELETE.perform(new SinkDocument(keyDoc,valueDoc));

    assertTrue(result instanceof DeleteOneModel,
            () -> "result expected to be of type DeleteOneModel");

    DeleteOneModel<BsonDocument> writeModel =
            (DeleteOneModel<BsonDocument>) result;

    assertTrue(writeModel.getFilter() instanceof BsonDocument,
            () -> "filter expected to be of type BsonDocument");

    assertEquals(filterDoc,writeModel.getFilter());

}
 
Example #5
Source File: MongoDbUpdateTest.java    From mongo-kafka with Apache License 2.0 6 votes vote down vote up
@Test
@DisplayName(
    "when valid doc change cdc event containing internal oplog fields then correct UpdateOneModel")
public void testValidSinkDocumentWithInternalOploagFieldForUpdate() {
  BsonDocument keyDoc = BsonDocument.parse("{id: '1234'}");
  BsonDocument valueDoc =
      new BsonDocument("op", new BsonString("u"))
          .append("patch", new BsonString(UPDATE_DOC_WITH_OPLOG_INTERNALS.toJson()));

  WriteModel<BsonDocument> result = UPDATE.perform(new SinkDocument(keyDoc, valueDoc));
  assertTrue(
      result instanceof UpdateOneModel, () -> "result expected to be of type UpdateOneModel");

  UpdateOneModel<BsonDocument> writeModel = (UpdateOneModel<BsonDocument>) result;
  assertEquals(
      UPDATE_DOC, writeModel.getUpdate(), () -> "update doc not matching what is expected");
  assertTrue(
      writeModel.getFilter() instanceof BsonDocument,
      () -> "filter expected to be of type BsonDocument");
  assertEquals(FILTER_DOC, writeModel.getFilter());
}
 
Example #6
Source File: MongoDbUpdateTest.java    From mongo-kafka with Apache License 2.0 6 votes vote down vote up
@Test
@DisplayName("when valid doc change cdc event then correct UpdateOneModel")
void testValidSinkDocumentForUpdate() {
  BsonDocument keyDoc = BsonDocument.parse("{id: '1234'}");
  BsonDocument valueDoc =
      new BsonDocument("op", new BsonString("u"))
          .append("patch", new BsonString(UPDATE_DOC.toJson()));

  WriteModel<BsonDocument> result = UPDATE.perform(new SinkDocument(keyDoc, valueDoc));
  assertTrue(result instanceof UpdateOneModel, "result expected to be of type UpdateOneModel");

  UpdateOneModel<BsonDocument> writeModel = (UpdateOneModel<BsonDocument>) result;
  assertEquals(UPDATE_DOC, writeModel.getUpdate(), "update doc not matching what is expected");
  assertTrue(
      writeModel.getFilter() instanceof BsonDocument,
      "filter expected to be of type BsonDocument");
  assertEquals(FILTER_DOC, writeModel.getFilter());
}
 
Example #7
Source File: UpdateDescription.java    From stitch-android-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Converts this update description to its document representation as it would appear in a
 * MongoDB Change Event.
 *
 * @return the update description document as it would appear in a change event
 */
public BsonDocument toBsonDocument() {
  final BsonDocument updateDescDoc = new BsonDocument();
  updateDescDoc.put(
      Fields.UPDATED_FIELDS_FIELD,
      this.getUpdatedFields());

  final BsonArray removedFields = new BsonArray();
  for (final String field : this.getRemovedFields()) {
    removedFields.add(new BsonString(field));
  }
  updateDescDoc.put(
      Fields.REMOVED_FIELDS_FIELD,
      removedFields);

  return updateDescDoc;
}
 
Example #8
Source File: MongoPersistenceOperationsSelectionProviderTest.java    From ditto with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void selectEntityWhenNamespacesDisabled() {
    final MongoEventSourceSettings settings = MongoEventSourceSettings.of(PERSISTENCE_ID_PREFIX,
            false, METADATA_COLLECTION_NAME, JOURNAL_COLLECTION_NAME, SNAPSHOT_COLLECTION_NAME);
    final MongoPersistenceOperationsSelectionProvider underTest =
            MongoPersistenceOperationsSelectionProvider.of(settings);

    final Collection<MongoPersistenceOperationsSelection> selections =
            underTest.selectEntity(DefaultEntityId.of(ENTITY_NAME));

    final String pid = PERSISTENCE_ID_PREFIX + ENTITY_NAME;
    final Document pidFilter = new Document().append(KEY_PID, new BsonString(pid));
    final MongoPersistenceOperationsSelection expectedMetadataSelection =
            MongoPersistenceOperationsSelection.of(METADATA_COLLECTION_NAME, pidFilter);
    final MongoPersistenceOperationsSelection expectedJournalSelection =
            MongoPersistenceOperationsSelection.of(JOURNAL_COLLECTION_NAME, pidFilter);
    final MongoPersistenceOperationsSelection expectedSnapshotSelection =
            MongoPersistenceOperationsSelection.of(SNAPSHOT_COLLECTION_NAME, pidFilter);
    assertThat(selections)
            .containsExactlyInAnyOrder(expectedMetadataSelection, expectedJournalSelection,
                    expectedSnapshotSelection);
}
 
Example #9
Source File: MongoUtil.java    From game-server with MIT License 6 votes vote down vote up
public static BsonValue getBsonValue(Object obj) {
    if (obj instanceof Integer) {
        return new BsonInt32((Integer) obj);
    }

    if (obj instanceof String) {
        return new BsonString((String) obj);
    }

    if (obj instanceof Long) {
        return new BsonInt64((Long) obj);
    }

    if (obj instanceof Date) {
        return new BsonDateTime(((Date) obj).getTime());
    }
    if (obj instanceof Double || obj instanceof Float) {
        return new BsonDouble((Double) obj);
    }
    return new BsonNull();

}
 
Example #10
Source File: MongoUtil.java    From game-server with MIT License 6 votes vote down vote up
public static BsonValue getBsonValue(Object obj) {
    if (obj instanceof Integer) {
        return new BsonInt32((Integer) obj);
    }

    if (obj instanceof String) {
        return new BsonString((String) obj);
    }

    if (obj instanceof Long) {
        return new BsonInt64((Long) obj);
    }

    if (obj instanceof Date) {
        return new BsonDateTime(((Date) obj).getTime());
    }
    if (obj instanceof Double || obj instanceof Float) {
        return new BsonDouble((Double) obj);
    }
    return new BsonNull();

}
 
Example #11
Source File: BulkWriteResultAckFlowTest.java    From ditto with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void allSuccess() {
    final List<AbstractWriteModel> writeModels = generate5WriteModels();
    final BulkWriteResult result = BulkWriteResult.acknowledged(0, 3, 1, 1,
            List.of(new BulkWriteUpsert(0, new BsonString("upsert 0")),
                    new BulkWriteUpsert(4, new BsonString("upsert 4")))
    );

    // WHEN
    final WriteResultAndErrors resultAndErrors = WriteResultAndErrors.success(writeModels, result);
    final String message = runBulkWriteResultAckFlowAndGetFirstLogEntry(resultAndErrors);

    // THEN
    actorSystem.log().info(message);
    assertThat(message).contains("Acknowledged: Success");
}
 
Example #12
Source File: MongoDbUpdateTest.java    From kafka-connect-mongodb with Apache License 2.0 6 votes vote down vote up
@Test
@DisplayName("when valid doc change cdc event then correct UpdateOneModel")
public void testValidSinkDocumentForUpdate() {
    BsonDocument keyDoc = new BsonDocument("id",new BsonString("1004"));

    BsonDocument valueDoc = new BsonDocument("op",new BsonString("u"))
            .append("patch",new BsonString(UPDATE_DOC.toJson()));

    WriteModel<BsonDocument> result =
            MONGODB_UPDATE.perform(new SinkDocument(keyDoc,valueDoc));

    assertTrue(result instanceof UpdateOneModel,
            () -> "result expected to be of type UpdateOneModel");

    UpdateOneModel<BsonDocument> writeModel =
            (UpdateOneModel<BsonDocument>) result;

    assertEquals(UPDATE_DOC,writeModel.getUpdate(),
            ()-> "update doc not matching what is expected");

    assertTrue(writeModel.getFilter() instanceof BsonDocument,
            () -> "filter expected to be of type BsonDocument");

    assertEquals(FILTER_DOC,writeModel.getFilter());

}
 
Example #13
Source File: MongoDbDeleteTest.java    From kafka-connect-mongodb with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("when key doc 'id' field contains invalid JSON then DataException")
public void testInvalidJsonIdFieldInKeyDocument() {
    BsonDocument keyDoc = new BsonDocument("id",new BsonString("{,NOT:JSON,}"));
    assertThrows(DataException.class,() ->
            MONGODB_DELETE.perform(new SinkDocument(keyDoc,new BsonDocument()))
    );
}
 
Example #14
Source File: RdbmsDeleteTest.java    From kafka-connect-mongodb with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("when key doc and value 'before' field both empty then DataException")
public void testEmptyKeyDocAndEmptyValueBeforeField() {
    assertThrows(DataException.class,() ->
            RDBMS_DELETE.perform(new SinkDocument(new BsonDocument(),
                    new BsonDocument("op",new BsonString("d")).append("before",new BsonDocument())))
    );
}
 
Example #15
Source File: TodoItem.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
static BsonDocument toBsonDocument(final TodoItem item) {
  final BsonDocument asDoc = new BsonDocument();
  asDoc.put(Fields.ID, new BsonObjectId(item.getId()));
  asDoc.put(Fields.OWNER_ID, new BsonString(item.getOwnerId()));
  asDoc.put(Fields.TASK, new BsonString(item.getTask()));
  asDoc.put(Fields.CHECKED, new BsonBoolean(item.isChecked()));
  if (item.getDoneDate() != null) {
    asDoc.put(Fields.DONE_DATE, new BsonDateTime(item.getDoneDate().getTime()));
  }
  return asDoc;
}
 
Example #16
Source File: MongoDbHandlerTest.java    From kafka-connect-mongodb with Apache License 2.0 5 votes vote down vote up
@TestFactory
@DisplayName("when valid cdc operation type mapped to NO OP then CdcOperation of type MongoDbNoOp")
public Stream<DynamicTest> testValidCdcOpertionWithNoOpMappings() {

    return Stream.of(OperationType.values()).map(ot ->
            dynamicTest("test operation " + ot, () ->
                    assertTrue(MONGODB_HANDLER_NOOP_MAPPING.getCdcOperation(
                            new BsonDocument("op", new BsonString("c")))
                            instanceof MongoDbNoOp)
            )
    );

}
 
Example #17
Source File: MongoDbHandlerTest.java    From kafka-connect-mongodb with Apache License 2.0 5 votes vote down vote up
@TestFactory
    @DisplayName("when valid cdc operation type then correct MongoDB CdcOperation")
    public Stream<DynamicTest> testValidCdcOpertionTypes() {

    return Stream.of(
            dynamicTest("test operation "+OperationType.CREATE, () ->
                    assertTrue(MONGODB_HANDLER_DEFAULT_MAPPING.getCdcOperation(
                            new BsonDocument("op",new BsonString("c")))
                            instanceof MongoDbInsert)
            ),
            dynamicTest("test operation "+OperationType.READ, () ->
                    assertTrue(MONGODB_HANDLER_DEFAULT_MAPPING.getCdcOperation(
                            new BsonDocument("op",new BsonString("r")))
                            instanceof MongoDbInsert)
            ),
            dynamicTest("test operation "+OperationType.UPDATE, () ->
                    assertTrue(MONGODB_HANDLER_DEFAULT_MAPPING.getCdcOperation(
                            new BsonDocument("op",new BsonString("u")))
                            instanceof MongoDbUpdate)
            ),
            dynamicTest("test operation "+OperationType.DELETE, () ->
                    assertTrue(MONGODB_HANDLER_DEFAULT_MAPPING.getCdcOperation(
                            new BsonDocument("op",new BsonString("d")))
                            instanceof MongoDbDelete)
            )
    );

}
 
Example #18
Source File: MongoDbHandlerTest.java    From kafka-connect-mongodb with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("when value doc contains unmapped operation type then DataException")
public void testUnmappedCdcOperationType() {
    SinkDocument cdcEvent = new SinkDocument(
            new BsonDocument("_id",new BsonInt32(1234)),
            new BsonDocument("op",new BsonString("c"))
                    .append("after",new BsonString("{_id:1234,foo:\"blah\"}"))
    );
    assertThrows(DataException.class, () ->
            MONGODB_HANDLER_EMPTY_MAPPING.handle(cdcEvent)
    );
}
 
Example #19
Source File: MongoDbUpdateTest.java    From kafka-connect-mongodb with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("when missing key doc then DataException")
public void testMissingKeyDocument() {
    assertThrows(DataException.class,() ->
            MONGODB_UPDATE.perform(new SinkDocument(null,
                        new BsonDocument("patch",new BsonString("{}"))))
    );
}
 
Example #20
Source File: RdbmsDeleteTest.java    From kafka-connect-mongodb with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("when valid cdc event without PK then correct DeleteOneModel")
public void testValidSinkDocumentNoPK() {

    BsonDocument filterDoc = new BsonDocument("text", new BsonString("hohoho"))
            .append("number", new BsonInt32(9876))
            .append("active", new BsonBoolean(true));

    BsonDocument keyDoc = new BsonDocument();

    BsonDocument valueDoc = new BsonDocument("op",new BsonString("c"))
            .append("before",new BsonDocument("text", new BsonString("hohoho"))
                    .append("number", new BsonInt32(9876))
                    .append("active", new BsonBoolean(true)));

    WriteModel<BsonDocument> result =
            RDBMS_DELETE.perform(new SinkDocument(keyDoc,valueDoc));

    assertTrue(result instanceof DeleteOneModel,
            () -> "result expected to be of type DeleteOneModel");

    DeleteOneModel<BsonDocument> writeModel =
            (DeleteOneModel<BsonDocument>) result;

    assertTrue(writeModel.getFilter() instanceof BsonDocument,
            () -> "filter expected to be of type BsonDocument");

    assertEquals(filterDoc,writeModel.getFilter());

}
 
Example #21
Source File: NamespaceSynchronizationConfig.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
static BsonDocument getNsFilter(
    final MongoNamespace namespace
) {
  final BsonDocument filter = new BsonDocument();
  filter.put(ConfigCodec.Fields.NAMESPACE_FIELD, new BsonString(namespace.toString()));
  return filter;
}
 
Example #22
Source File: MongoDbInsertTest.java    From kafka-connect-mongodb with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("when invalid json in value doc 'after' field then DataException")
public void testInvalidAfterField() {
    assertThrows(DataException.class,() ->
            MONGODB_INSERT.perform(
                    new SinkDocument(new BsonDocument(),
                        new BsonDocument("op",new BsonString("c"))
                            .append("after",new BsonString("{NO : JSON [HERE] GO : AWAY}")))
            )
    );
}
 
Example #23
Source File: MongoDbHandlerTest.java    From kafka-connect-mongodb with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("when value doc contains unknown operation type then DataException")
public void testUnkownCdcOperationType() {
    SinkDocument cdcEvent = new SinkDocument(
            new BsonDocument("id",new BsonInt32(1234)),
            new BsonDocument("op",new BsonString("x"))
    );
    assertThrows(DataException.class, () ->
            MONGODB_HANDLER_DEFAULT_MAPPING.handle(cdcEvent)
    );
}
 
Example #24
Source File: ChangeEvent.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Serializes this change event into a {@link BsonDocument}.
 * @return the serialized document.
 */
@Override
public BsonDocument toBsonDocument() {
  final BsonDocument asDoc = new BsonDocument();
  asDoc.put(Fields.ID_FIELD, id);

  asDoc.put(Fields.OPERATION_TYPE_FIELD, new BsonString(getOperationType().toRemote()));

  final BsonDocument nsDoc = new BsonDocument();
  nsDoc.put(Fields.NS_DB_FIELD, new BsonString(ns.getDatabaseName()));
  nsDoc.put(Fields.NS_COLL_FIELD, new BsonString(getNamespace().getCollectionName()));
  asDoc.put(Fields.NS_FIELD, nsDoc);

  asDoc.put(Fields.DOCUMENT_KEY_FIELD, getDocumentKey());

  if (getFullDocument() != null && (getFullDocument() instanceof BsonValue)
      && ((BsonValue) getFullDocument()).isDocument()) {
    asDoc.put(Fields.FULL_DOCUMENT_FIELD, (BsonValue) getFullDocument());
  }

  if (getUpdateDescription() != null) {
    asDoc.put(Fields.UPDATE_DESCRIPTION_FIELD, getUpdateDescription().toBsonDocument());
  }

  asDoc.put(Fields.WRITE_PENDING_FIELD, new BsonBoolean(hasUncommittedWrites()));
  return asDoc;
}
 
Example #25
Source File: RdbmsHandlerTest.java    From kafka-connect-mongodb with Apache License 2.0 5 votes vote down vote up
@TestFactory
@DisplayName("when valid cdc operation type mapped to NO OP then CdcOperation of type RdbmsNoOp")
public Stream<DynamicTest> testValidCdcOpertionWithNoOpMappings() {

    return Stream.of(OperationType.values()).map(ot ->
            dynamicTest("test operation " + ot, () ->
                    assertTrue(RDBMS_HANDLER_NOOP_MAPPING.getCdcOperation(
                            new BsonDocument("op", new BsonString("c")))
                            instanceof RdbmsNoOp)
            )
    );

}
 
Example #26
Source File: RdbmsHandlerTest.java    From kafka-connect-mongodb with Apache License 2.0 5 votes vote down vote up
@TestFactory
@DisplayName("when valid cdc operation type then correct RDBMS CdcOperation")
public Stream<DynamicTest> testValidCdcOpertionTypes() {

    return Stream.of(
            dynamicTest("test operation " + OperationType.CREATE, () ->
                    assertTrue(RDBMS_HANDLER_DEFAULT_MAPPING.getCdcOperation(
                            new BsonDocument("op", new BsonString("c")))
                            instanceof RdbmsInsert)
            ),
            dynamicTest("test operation " + OperationType.READ, () ->
                    assertTrue(RDBMS_HANDLER_DEFAULT_MAPPING.getCdcOperation(
                            new BsonDocument("op", new BsonString("r")))
                            instanceof RdbmsInsert)
            ),
            dynamicTest("test operation " + OperationType.UPDATE, () ->
                    assertTrue(RDBMS_HANDLER_DEFAULT_MAPPING.getCdcOperation(
                            new BsonDocument("op", new BsonString("u")))
                            instanceof RdbmsUpdate)
            ),
            dynamicTest("test operation " + OperationType.DELETE, () ->
                    assertTrue(RDBMS_HANDLER_DEFAULT_MAPPING.getCdcOperation(
                            new BsonDocument("op", new BsonString("d")))
                            instanceof RdbmsDelete)
            )
    );

}
 
Example #27
Source File: RdbmsHandlerTest.java    From kafka-connect-mongodb with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("when value doc contains unmapped operation type then DataException")
public void testUnmappedCdcOperationType() {
    SinkDocument cdcEvent = new SinkDocument(
            new BsonDocument("id",new BsonInt32(1004)),
            new BsonDocument("op",new BsonString("c"))
                    .append("after",new BsonDocument("id",new BsonInt32(1004))
                            .append("foo",new BsonString("blah")))
    );
    assertThrows(DataException.class, () ->
            RDBMS_HANDLER_EMPTY_MAPPING.handle(cdcEvent)
    );
}
 
Example #28
Source File: RdbmsHandlerTest.java    From kafka-connect-mongodb with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("when value doc contains unknown operation type then DataException")
public void testUnkownCdcOperationType() {
    SinkDocument cdcEvent = new SinkDocument(
            new BsonDocument("id",new BsonInt32(1234)),
                    new BsonDocument("op",new BsonString("x"))
    );
    assertThrows(DataException.class, () ->
            RDBMS_HANDLER_DEFAULT_MAPPING.handle(cdcEvent)
    );
}
 
Example #29
Source File: NamespaceSynchronizationConfig.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
NamespaceSynchronizationConfig(
    final MongoCollection<NamespaceSynchronizationConfig> namespacesColl,
    final MongoCollection<CoreDocumentSynchronizationConfig> docsColl,
    final NamespaceSynchronizationConfig config
) {
  this.namespacesColl = namespacesColl;
  this.docsColl = docsColl;
  this.namespace = config.namespace;
  this.syncedDocuments = new ConcurrentHashMap<>();
  this.nsLock = config.nsLock;

  // Fill from db
  final BsonDocument docsFilter = new BsonDocument();
  docsFilter.put(
      CoreDocumentSynchronizationConfig.ConfigCodec.Fields.NAMESPACE_FIELD,
      new BsonString(namespace.toString()));
  docsColl.find(docsFilter, CoreDocumentSynchronizationConfig.class)
      .forEach(new Block<CoreDocumentSynchronizationConfig>() {
        @Override
        public void apply(
            @Nonnull final CoreDocumentSynchronizationConfig docConfig
        ) {
          syncedDocuments.put(docConfig.getDocumentId(), new CoreDocumentSynchronizationConfig(
              docsColl,
              docConfig));
        }
      });
}
 
Example #30
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();
  }
}