use of org.bson.BsonInt32 in project mongo-java-driver by mongodb.
the class DBObjectCodecTest method shouldEncodedNestedMapsListsAndDocuments.
@Test
public void shouldEncodedNestedMapsListsAndDocuments() {
// {"0" : 0, "1", 1}
byte[] zeroOneDocumentBytes = new byte[] { 19, 0, 0, 0, 16, 48, 0, 0, 0, 0, 0, 16, 49, 0, 1, 0, 0, 0, 0 };
Map<String, Object> zeroOneMap = new HashMap<String, Object>();
zeroOneMap.put("0", 0);
zeroOneMap.put("1", 1);
DBObject zeroOneDBObject = new BasicDBObject();
zeroOneDBObject.putAll(zeroOneMap);
DBObject zeroOneDBList = new BasicDBList();
zeroOneDBList.putAll(zeroOneMap);
List<Integer> zeroOneList = asList(0, 1);
DBObjectCodec dbObjectCodec = new DBObjectCodec(fromProviders(asList(new ValueCodecProvider(), new DBObjectCodecProvider(), new BsonValueCodecProvider())));
DBObject doc = new BasicDBObject().append("map", zeroOneMap).append("dbDocument", zeroOneDBObject).append("dbList", zeroOneDBList).append("list", zeroOneList).append("array", new int[] { 0, 1 }).append("lazyDoc", new LazyDBObject(zeroOneDocumentBytes, new LazyBSONCallback())).append("lazyArray", new LazyDBList(zeroOneDocumentBytes, new LazyBSONCallback()));
BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
dbObjectCodec.encode(writer, doc, EncoderContext.builder().build());
BsonDocument zeroOneBsonDocument = new BsonDocument().append("0", new BsonInt32(0)).append("1", new BsonInt32(1));
BsonArray zeroOneBsonArray = new BsonArray(asList(new BsonInt32(0), new BsonInt32(1)));
assertEquals(new BsonDocument("map", zeroOneBsonDocument).append("dbDocument", zeroOneBsonDocument).append("dbList", zeroOneBsonArray).append("list", zeroOneBsonArray).append("array", zeroOneBsonArray).append("lazyDoc", zeroOneBsonDocument).append("lazyArray", zeroOneBsonArray), writer.getDocument());
}
use of org.bson.BsonInt32 in project mongo-java-driver by mongodb.
the class DBObjectCodecTest method shouldNotGenerateIdIfPresent.
@Test
public void shouldNotGenerateIdIfPresent() {
DBObjectCodec dbObjectCodec = new DBObjectCodec(fromProviders(asList(new ValueCodecProvider(), new DBObjectCodecProvider(), new BsonValueCodecProvider())));
DBObject document = new BasicDBObject("_id", 1);
assertTrue(dbObjectCodec.documentHasId(document));
document = dbObjectCodec.generateIdIfAbsentFromDocument(document);
assertTrue(dbObjectCodec.documentHasId(document));
assertEquals(new BsonInt32(1), dbObjectCodec.getDocumentId(document));
}
use of org.bson.BsonInt32 in project mongo-java-driver by mongodb.
the class AbstractExplainTest method testExplainOfFind.
@Test
public void testExplainOfFind() {
assumeTrue(serverVersionAtLeast(3, 0));
MongoCollection<BsonDocument> collection = client.getDatabase(getDefaultDatabaseName()).getCollection("explainTest", BsonDocument.class);
collection.drop();
collection.insertOne(new BsonDocument("_id", new BsonInt32(1)));
FindIterable<BsonDocument> iterable = collection.find().filter(Filters.eq("_id", 1));
Document explainDocument = iterable.explain();
assertNotNull(explainDocument);
assertTrue(explainDocument.containsKey("queryPlanner"));
assertTrue(explainDocument.containsKey("executionStats"));
explainDocument = iterable.explain(ExplainVerbosity.QUERY_PLANNER);
assertNotNull(explainDocument);
assertTrue(explainDocument.containsKey("queryPlanner"));
assertFalse(explainDocument.containsKey("executionStats"));
BsonDocument explainBsonDocument = iterable.explain(BsonDocument.class);
assertNotNull(explainBsonDocument);
assertTrue(explainBsonDocument.containsKey("queryPlanner"));
assertTrue(explainBsonDocument.containsKey("executionStats"));
explainBsonDocument = iterable.explain(BsonDocument.class, ExplainVerbosity.QUERY_PLANNER);
assertNotNull(explainBsonDocument);
assertTrue(explainBsonDocument.containsKey("queryPlanner"));
assertFalse(explainBsonDocument.containsKey("executionStats"));
}
use of org.bson.BsonInt32 in project mongo-java-driver by mongodb.
the class AbstractServerSelectionProseTest method operationCountBasedSelectionWithinLatencyWindow.
/**
* <a href="https://github.com/mongodb/specifications/blob/master/source/server-selection/server-selection-tests.rst#prose-test">
* {@code operationCount}-based Selection Within Latency Window</a>.
*/
@Test
@SuppressWarnings("try")
void operationCountBasedSelectionWithinLatencyWindow() throws InterruptedException, ExecutionException {
assumeTrue(isSharded());
ConnectionString multiMongosConnectionString = getMultiMongosConnectionString();
assumeTrue(multiMongosConnectionString != null);
assumeTrue(multiMongosConnectionString.getSslEnabled() == null || !multiMongosConnectionString.getSslEnabled());
assertEquals(2, multiMongosConnectionString.getHosts().size());
String appName = "loadBalancingTest";
int timeoutSeconds = 60;
int tasks = 10;
int opsPerTask = 100;
TestCommandListener commandListener = new TestCommandListener(singletonList("commandStartedEvent"), singletonList("drop"));
MongoClientSettings clientSettings = getMongoClientSettingsBuilder().applicationName(appName).applyConnectionString(multiMongosConnectionString).applyToConnectionPoolSettings(builder -> builder.minSize(tasks)).addCommandListener(commandListener).build();
BsonDocument configureFailPoint = new BsonDocument().append("configureFailPoint", new BsonString("failCommand")).append("mode", new BsonDocument().append("times", new BsonInt32(10_000))).append("data", new BsonDocument().append("failCommands", new BsonArray(singletonList(new BsonString("find")))).append("blockConnection", BsonBoolean.valueOf(true)).append("blockTimeMS", new BsonInt32(500)).append("appName", new BsonString(appName)));
ServerAddress serverWithFailPoint = clientSettings.getClusterSettings().getHosts().get(0);
ExecutorService executor = Executors.newFixedThreadPool(tasks);
try (MongoClient client = createClient(clientSettings)) {
MongoCollection<Document> collection = client.getDatabase(getDefaultDatabaseName()).getCollection("operationCountBasedSelectionWithinLatencyWindow");
collection.drop();
try (FailPoint ignored = FailPoint.enable(configureFailPoint, serverWithFailPoint)) {
Map<ServerAddress, Double> selectionRates = doSelections(collection, commandListener, executor, tasks, opsPerTask, timeoutSeconds);
double expectedServerWithFpSelectionRateUpperBound = 0.25;
assertTrue(selectionRates.containsKey(serverWithFailPoint));
assertTrue(selectionRates.get(serverWithFailPoint) < expectedServerWithFpSelectionRateUpperBound, selectionRates::toString);
assertEquals(1, selectionRates.values().stream().mapToDouble(Double::doubleValue).sum(), 0.01, selectionRates::toString);
}
commandListener.reset();
Map<ServerAddress, Double> selectionRates = doSelections(collection, commandListener, executor, tasks, opsPerTask, timeoutSeconds);
selectionRates.values().forEach(rate -> assertEquals(0.5, rate, 0.1, selectionRates::toString));
} finally {
executor.shutdownNow();
assertTrue(executor.awaitTermination(timeoutSeconds, SECONDS));
}
}
use of org.bson.BsonInt32 in project mongo-java-driver by mongodb.
the class NativeAuthenticationHelper method getAuthCommand.
public static BsonDocument getAuthCommand(final String userName, final String authHash, final String nonce) {
String key = nonce + userName + authHash;
BsonDocument cmd = new BsonDocument();
cmd.put("authenticate", new BsonInt32(1));
cmd.put("user", new BsonString(userName));
cmd.put("nonce", new BsonString(nonce));
cmd.put("key", new BsonString(hexMD5(key.getBytes(StandardCharsets.UTF_8))));
return cmd;
}
Aggregations