public class MongoConnection
extends java.lang.Object
MongoConnection
abstracts connection to the MongoDB
.Constructor and Description |
---|
MongoConnection(java.lang.String uri)
Constructs a new connection using the specified MongoDB connection string.
|
MongoConnection(java.lang.String host,
int port,
java.lang.String database)
Constructs a new
MongoConnection . |
MongoConnection(java.lang.String uri,
com.mongodb.MongoClient client)
Constructs a new
MongoConnection . |
MongoConnection(java.lang.String uri,
com.mongodb.MongoClientOptions.Builder builder)
Constructs a new connection using the specified MongoDB connection
String.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the underlying Mongo instance
|
com.mongodb.client.MongoDatabase |
getDatabase()
Returns the
MongoDatabase as passed in the URI of the
constructor. |
com.mongodb.client.MongoDatabase |
getDatabase(@NotNull java.lang.String name)
Returns the
MongoDatabase with the given name. |
java.lang.String |
getDBName() |
static com.mongodb.MongoClientOptions.Builder |
getDefaultBuilder()
Constructs a builder with default options set.
|
static com.mongodb.ReadConcern |
getDefaultReadConcern(@NotNull com.mongodb.MongoClient client,
@NotNull com.mongodb.client.MongoDatabase db)
Returns the default read concern depending on MongoDB deployment.
|
static com.mongodb.WriteConcern |
getDefaultWriteConcern(@NotNull com.mongodb.MongoClient client)
Returns the default write concern depending on MongoDB deployment.
|
com.mongodb.MongoClient |
getMongoClient() |
static boolean |
hasReadConcern(@NotNull java.lang.String uri)
Returns
true if the given uri has a read concern set. |
static boolean |
hasWriteConcern(@NotNull java.lang.String uri)
Returns
true if the given uri has a write concern set. |
static boolean |
isMajorityWriteConcern(@NotNull com.mongodb.client.MongoDatabase db)
Returns true if the majority write concern is used for the given DB.
|
static boolean |
isSufficientReadConcern(@NotNull com.mongodb.MongoClient client,
@NotNull com.mongodb.ReadConcern rc)
Returns
true if the given read concern is sufficient for Oak. |
static boolean |
isSufficientWriteConcern(@NotNull com.mongodb.MongoClient client,
@NotNull com.mongodb.WriteConcern wc)
Returns
true if the given write concern is sufficient for Oak. |
static com.mongodb.ReadConcernLevel |
readConcernLevel(com.mongodb.ReadConcern readConcern) |
static java.lang.String |
toString(com.mongodb.MongoClientOptions opts) |
public MongoConnection(java.lang.String uri) throws com.mongodb.MongoException
uri
- the MongoDB URIcom.mongodb.MongoException
- if there are failurespublic MongoConnection(java.lang.String uri, com.mongodb.MongoClientOptions.Builder builder) throws com.mongodb.MongoException
uri
- the connection URI.builder
- the client option defaults.com.mongodb.MongoException
- if there are failurespublic MongoConnection(java.lang.String host, int port, java.lang.String database) throws com.mongodb.MongoException
MongoConnection
.host
- The host address.port
- The port.database
- The database name.com.mongodb.MongoException
- if there are failurespublic MongoConnection(java.lang.String uri, com.mongodb.MongoClient client)
MongoConnection
.uri
- the connection URI.client
- the already connected client.public com.mongodb.MongoClient getMongoClient()
MongoClient
for this connection.public com.mongodb.client.MongoDatabase getDatabase()
MongoDatabase
as passed in the URI of the
constructor.MongoDatabase
.public com.mongodb.client.MongoDatabase getDatabase(@NotNull @NotNull java.lang.String name)
MongoDatabase
with the given name.MongoDatabase
.public java.lang.String getDBName()
public void close()
public static com.mongodb.MongoClientOptions.Builder getDefaultBuilder()
public static java.lang.String toString(com.mongodb.MongoClientOptions opts)
public static boolean hasWriteConcern(@NotNull @NotNull java.lang.String uri)
true
if the given uri
has a write concern set.uri
- the URI to check.true
if the URI has a write concern set, false
otherwise.public static boolean hasReadConcern(@NotNull @NotNull java.lang.String uri)
true
if the given uri
has a read concern set.uri
- the URI to check.true
if the URI has a read concern set, false
otherwise.public static com.mongodb.WriteConcern getDefaultWriteConcern(@NotNull @NotNull com.mongodb.MongoClient client)
WriteConcern.MAJORITY
: for a MongoDB replica setWriteConcern.ACKNOWLEDGED
: for single MongoDB instanceclient
- the connection to MongoDB.public static com.mongodb.ReadConcern getDefaultReadConcern(@NotNull @NotNull com.mongodb.MongoClient client, @NotNull @NotNull com.mongodb.client.MongoDatabase db)
ReadConcern.MAJORITY
: for a MongoDB replica set with w=majorityReadConcern.LOCAL
: for other casesdb
- the connection to MongoDB.public static boolean isMajorityWriteConcern(@NotNull @NotNull com.mongodb.client.MongoDatabase db)
db
- the connection to MongoDB.public static boolean isSufficientWriteConcern(@NotNull @NotNull com.mongodb.MongoClient client, @NotNull @NotNull com.mongodb.WriteConcern wc)
true
if the given write concern is sufficient for Oak. On
a replica set Oak expects at least w=2. For a single MongoDB node
deployment w=1 is sufficient.client
- the client.wc
- the write concern.public static boolean isSufficientReadConcern(@NotNull @NotNull com.mongodb.MongoClient client, @NotNull @NotNull com.mongodb.ReadConcern rc)
true
if the given read concern is sufficient for Oak. On
a replica set Oak expects majority or linear. For a single MongoDB node
deployment local is sufficient.client
- the client.rc
- the read concern.public static com.mongodb.ReadConcernLevel readConcernLevel(com.mongodb.ReadConcern readConcern)
Copyright © 2012–2022 The Apache Software Foundation. All rights reserved.