command.put("ordered", new BsonBoolean(ordered)); if (!writeConcern.isServerDefault() && !sessionContext.hasActiveTransaction()) { command.put("writeConcern", writeConcern.asDocument()); command.put("bypassDocumentValidation", new BsonBoolean(bypassDocumentValidation));
private BsonDocument getCommand() { BsonDocument command = new BsonDocument("listDatabases", new BsonInt32(1)); if (maxTimeMS > 0) { command.put("maxTimeMS", new BsonInt64(maxTimeMS)); } if (filter != null) { command.put("filter", filter); } if (nameOnly != null) { command.put("nameOnly", new BsonBoolean(nameOnly)); } return command; } }
collation.put("caseLevel", new BsonBoolean(caseLevel)); collation.put("numericOrdering", new BsonBoolean(numericOrdering)); collation.put("normalization", new BsonBoolean(normalization)); collation.put("backwards", new BsonBoolean(backwards));
/** * 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()); }
valueDocument.append(DELETED_FIELD, new BsonBoolean(isDeletion));
@Override public BsonDocument create(final ServerDescription serverDescription, final ConnectionDescription connectionDescription) { validateCollation(connectionDescription, collation); BsonDocument commandDocument = new BsonDocument("findAndModify", new BsonString(getNamespace().getCollectionName())); putIfNotNull(commandDocument, "query", getFilter()); putIfNotNull(commandDocument, "fields", getProjection()); putIfNotNull(commandDocument, "sort", getSort()); commandDocument.put("new", new BsonBoolean(!isReturnOriginal())); putIfTrue(commandDocument, "upsert", isUpsert()); putIfNotZero(commandDocument, "maxTimeMS", getMaxTime(MILLISECONDS)); commandDocument.put("update", getReplacement()); if (bypassDocumentValidation != null && serverIsAtLeastVersionThreeDotTwo(connectionDescription)) { commandDocument.put("bypassDocumentValidation", BsonBoolean.valueOf(bypassDocumentValidation)); } addWriteConcernToCommand(connectionDescription, commandDocument, sessionContext); if (collation != null) { commandDocument.put("collation", collation.asDocument()); } addTxnNumberToCommand(serverDescription, connectionDescription, commandDocument, sessionContext); return commandDocument; } };
@Override public BsonDocument create(final ServerDescription serverDescription, final ConnectionDescription connectionDescription) { validateCollation(connectionDescription, collation); BsonDocument commandDocument = new BsonDocument("findAndModify", new BsonString(getNamespace().getCollectionName())); putIfNotNull(commandDocument, "query", getFilter()); putIfNotNull(commandDocument, "fields", getProjection()); putIfNotNull(commandDocument, "sort", getSort()); commandDocument.put("new", new BsonBoolean(!isReturnOriginal())); putIfTrue(commandDocument, "upsert", isUpsert()); putIfNotZero(commandDocument, "maxTimeMS", getMaxTime(MILLISECONDS)); commandDocument.put("update", getUpdate()); if (bypassDocumentValidation != null && serverIsAtLeastVersionThreeDotTwo(connectionDescription)) { commandDocument.put("bypassDocumentValidation", BsonBoolean.valueOf(bypassDocumentValidation)); } addWriteConcernToCommand(connectionDescription, commandDocument, sessionContext); if (collation != null) { commandDocument.put("collation", collation.asDocument()); } if (arrayFilters != null) { commandDocument.put("arrayFilters", new BsonArray(arrayFilters)); } addTxnNumberToCommand(serverDescription, connectionDescription, commandDocument, sessionContext); return commandDocument; } };
public BsonValue toBson(Object data) { return new BsonBoolean((Boolean) data); }
public BsonValue toBson(Object data) { return new BsonBoolean((Boolean) data); }
private static BsonBoolean getIdAsBoolean(String id) throws IllegalArgumentException { if (id.equals(RequestContext.TRUE_KEY_ID)) { return new BsonBoolean(true); } if (id.equals(RequestContext.FALSE_KEY_ID)) { return new BsonBoolean(false); } return null; }
/** * * @param ref * @param href * @param templated */ public Link(String ref, String href, boolean templated) { this(ref, href); if (templated) { doc.getDocument(ref).put("templated", new BsonBoolean(true)); } }
String mapFunction = ... String reduceFunction = ... BsonDocument command = new BsonDocument(); BsonJavaScript map = new BsonJavaScript(mapFunction); BsonJavaScript red = new BsonJavaScript(reduceFunction); BsonDocument query = new BsonDocument("someidentifier", new BsonString("somevalue")); command.append("mapreduce", new BsonString("mySourceCollection")); command.append("query", query); command.append("map", map); command.append("reduce", red); command.append("out", new BsonDocument("inline", new BsonBoolean(true))); Document result = mongoClient.getDatabase(database).runCommand(command);
private BsonDocument getCommand() { BsonDocument command = new BsonDocument("listDatabases", new BsonInt32(1)); if (maxTimeMS > 0) { command.put("maxTimeMS", new BsonInt64(maxTimeMS)); } if (filter != null) { command.put("filter", filter); } if (nameOnly != null) { command.put("nameOnly", new BsonBoolean(nameOnly)); } return command; } }
@Test @DisplayName("test PartialKeyStrategy with whitelisting") public void testPartialKeyStrategyWhitelist() { BsonDocument keyDoc = new BsonDocument(); keyDoc.put("keyPart1", new BsonInt32(123)); keyDoc.put("keyPart2", new BsonString("ABC")); keyDoc.put("keyPart3", new BsonBoolean(true)); BsonDocument partialWhitelisted = new BsonDocument(); partialWhitelisted.put("keyPart1", new BsonInt32(123)); MongoDbSinkConnectorConfig cfg = mock(MongoDbSinkConnectorConfig.class); when(cfg.getKeyProjectionList("")).thenReturn(new HashSet<>(Arrays.asList("keyPart1"))); when(cfg.isUsingWhitelistKeyProjection("")).thenReturn(true); IdStrategy idS = new PartialKeyStrategy(new WhitelistKeyProjector(cfg,"")); SinkDocument sd = new SinkDocument(keyDoc,null); BsonValue id = idS.generateId(sd, null); assertAll("id checks PartialKeyStrategy with whitelisting", () -> assertTrue(id instanceof BsonDocument), () -> assertEquals(partialWhitelisted,id.asDocument()) ); assertEquals(new BsonDocument(),idS.generateId(new SinkDocument(null,null), null)); }
@BeforeEach public void setupDocumentsToRename() { keyDoc = new BsonDocument("fieldA",new BsonString("my field value")); keyDoc.put("f2",new BsonBoolean(true)); keyDoc.put("subDoc",new BsonDocument("fieldX",new BsonInt32(42))); keyDoc.put("my_field1",new BsonDocument("my_field2",new BsonString("testing rocks!"))); valueDoc = new BsonDocument("abc",new BsonString("my field value")); valueDoc.put("f2",new BsonBoolean(false)); valueDoc.put("subDoc",new BsonDocument("123",new BsonDouble(0.0))); valueDoc.put("foo.foo.foo",new BsonDocument(".blah..blah.",new BsonInt32(23))); }
@Test @DisplayName("when valid cdc event without PK then correct ReplaceOneModel") public void testValidSinkDocumentNoPK() { BsonDocument valueDocCreate = new BsonDocument("op",new BsonString("c")) .append("after",new BsonDocument("text", new BsonString("lalala")) .append("number", new BsonInt32(1234)) .append("active", new BsonBoolean(false))); verifyResultsNoPK(valueDocCreate); BsonDocument valueDocRead = new BsonDocument("op",new BsonString("r")) .append("after",new BsonDocument("text", new BsonString("lalala")) .append("number", new BsonInt32(1234)) .append("active", new BsonBoolean(false))); verifyResultsNoPK(valueDocRead); }
@BeforeAll public static void setupDocumentsToCompare() { expectedKeyDocFieldnameMapping = new BsonDocument("f1",new BsonString("my field value")); expectedKeyDocFieldnameMapping.put("fieldB",new BsonBoolean(true)); expectedKeyDocFieldnameMapping.put("subDoc",new BsonDocument("name_x",new BsonInt32(42))); expectedKeyDocFieldnameMapping.put("my_field1",new BsonDocument("my_field2",new BsonString("testing rocks!"))); expectedValueDocFieldnameMapping = new BsonDocument("xyz",new BsonString("my field value")); expectedValueDocFieldnameMapping.put("f_two",new BsonBoolean(false)); expectedValueDocFieldnameMapping.put("subDoc",new BsonDocument("789",new BsonDouble(0.0))); expectedValueDocFieldnameMapping.put("foo.foo.foo",new BsonDocument(".blah..blah.",new BsonInt32(23))); expectedKeyDocRegExpSettings = new BsonDocument("FA",new BsonString("my field value")); expectedKeyDocRegExpSettings.put("f2",new BsonBoolean(true)); expectedKeyDocRegExpSettings.put("subDoc",new BsonDocument("FX",new BsonInt32(42))); expectedKeyDocRegExpSettings.put("_F1",new BsonDocument("_F2",new BsonString("testing rocks!"))); expectedValueDocRegExpSettings = new BsonDocument("abc",new BsonString("my field value")); expectedValueDocRegExpSettings.put("f2",new BsonBoolean(false)); expectedValueDocRegExpSettings.put("subDoc",new BsonDocument("123",new BsonDouble(0.0))); expectedValueDocRegExpSettings.put("foo_foo_foo",new BsonDocument("_blah__blah_",new BsonInt32(23))); }
@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()); }
@Override public BsonDocument create(final ServerDescription serverDescription, final ConnectionDescription connectionDescription) { validateCollation(connectionDescription, collation); BsonDocument commandDocument = new BsonDocument("findAndModify", new BsonString(getNamespace().getCollectionName())); putIfNotNull(commandDocument, "query", getFilter()); putIfNotNull(commandDocument, "fields", getProjection()); putIfNotNull(commandDocument, "sort", getSort()); commandDocument.put("new", new BsonBoolean(!isReturnOriginal())); putIfTrue(commandDocument, "upsert", isUpsert()); putIfNotZero(commandDocument, "maxTimeMS", getMaxTime(MILLISECONDS)); commandDocument.put("update", getReplacement()); if (bypassDocumentValidation != null && serverIsAtLeastVersionThreeDotTwo(connectionDescription)) { commandDocument.put("bypassDocumentValidation", BsonBoolean.valueOf(bypassDocumentValidation)); } addWriteConcernToCommand(connectionDescription, commandDocument, sessionContext); if (collation != null) { commandDocument.put("collation", collation.asDocument()); } addTxnNumberToCommand(serverDescription, connectionDescription, commandDocument, sessionContext); return commandDocument; } };
private static SinkDocument buildSinkDocumentFlatStruct() { BsonDocument flatKey = new BsonDocument(); flatKey.put("_id", new BsonString("ABC-123")); flatKey.put("myBoolean",new BsonBoolean(true)); flatKey.put("myInt",new BsonInt32(42)); flatKey.put("myBytes",new BsonBinary(new byte[] {65,66,67})); flatKey.put("myArray", new BsonArray()); BsonDocument flatValue = new BsonDocument(); flatValue.put("_id", new BsonString("XYZ-789")); flatValue.put("myLong",new BsonInt64(42L)); flatValue.put("myDouble",new BsonDouble(23.23d)); flatValue.put("myString",new BsonString("BSON")); flatValue.put("myBytes",new BsonBinary(new byte[] {120,121,122})); flatValue.put("myArray", new BsonArray()); return new SinkDocument(flatKey,flatValue); }