Class MongoConnection
java.lang.Object
org.apache.jackrabbit.oak.plugins.document.util.MongoConnection
The
MongoConnection
abstracts connection to the MongoDB
.-
Constructor Summary
ConstructorsConstructorDescriptionMongoConnection
(String uri) Constructs a new connection using the specified MongoDB connection string.MongoConnection
(String host, int port, String database) Constructs a newMongoConnection
.MongoConnection
(String uri, com.mongodb.MongoClient client) Constructs a newMongoConnection
.MongoConnection
(String uri, com.mongodb.MongoClientOptions.Builder builder) Constructs a new connection using the specified MongoDB connection String. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the underlying Mongo instancecom.mongodb.client.MongoDatabase
Returns theMongoDatabase
as passed in the URI of the constructor.com.mongodb.client.MongoDatabase
getDatabase
(@NotNull String name) Returns theMongoDatabase
with the given name.static com.mongodb.MongoClientOptions.Builder
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
com.mongodb.MongoClientURI
static boolean
hasReadConcern
(@NotNull String uri) Returnstrue
if the givenuri
has a read concern set.static boolean
hasWriteConcern
(@NotNull String uri) Returnstrue
if the givenuri
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) Returnstrue
if the given read concern is sufficient for Oak.static boolean
isSufficientWriteConcern
(@NotNull com.mongodb.MongoClient client, @NotNull com.mongodb.WriteConcern wc) Returnstrue
if the given write concern is sufficient for Oak.static com.mongodb.ReadConcernLevel
readConcernLevel
(com.mongodb.ReadConcern readConcern) static String
toString
(com.mongodb.MongoClientOptions opts)
-
Constructor Details
-
MongoConnection
Constructs a new connection using the specified MongoDB connection string. See also http://docs.mongodb.org/manual/reference/connection-string/- Parameters:
uri
- the MongoDB URI- Throws:
com.mongodb.MongoException
- if there are failures
-
MongoConnection
public MongoConnection(String uri, com.mongodb.MongoClientOptions.Builder builder) throws com.mongodb.MongoException Constructs a new connection using the specified MongoDB connection String. The default client options are taken from the provided builder.- Parameters:
uri
- the connection URI.builder
- the client option defaults.- Throws:
com.mongodb.MongoException
- if there are failures
-
MongoConnection
Constructs a newMongoConnection
.- Parameters:
host
- The host address.port
- The port.database
- The database name.- Throws:
com.mongodb.MongoException
- if there are failures
-
MongoConnection
Constructs a newMongoConnection
.- Parameters:
uri
- the connection URI.client
- the already connected client.
-
-
Method Details
-
getMongoURI
public com.mongodb.MongoClientURI getMongoURI()- Returns:
- the
MongoClientURI
for this connection
-
getMongoClient
public com.mongodb.MongoClient getMongoClient()- Returns:
- the
MongoClient
for this connection.
-
getDatabase
public com.mongodb.client.MongoDatabase getDatabase()Returns theMongoDatabase
as passed in the URI of the constructor.- Returns:
- the
MongoDatabase
.
-
getDatabase
Returns theMongoDatabase
with the given name.- Returns:
- The
MongoDatabase
.
-
getDBName
- Returns:
- the database name specified in the URI.
-
close
public void close()Closes the underlying Mongo instance -
getDefaultBuilder
public static com.mongodb.MongoClientOptions.Builder getDefaultBuilder()Constructs a builder with default options set. These can be overridden later- Returns:
- builder with default options set
-
toString
-
hasWriteConcern
Returnstrue
if the givenuri
has a write concern set.- Parameters:
uri
- the URI to check.- Returns:
true
if the URI has a write concern set,false
otherwise.
-
hasReadConcern
Returnstrue
if the givenuri
has a read concern set.- Parameters:
uri
- the URI to check.- Returns:
true
if the URI has a read concern set,false
otherwise.
-
getDefaultWriteConcern
public static com.mongodb.WriteConcern getDefaultWriteConcern(@NotNull @NotNull com.mongodb.MongoClient client) Returns the default write concern depending on MongoDB deployment.WriteConcern.MAJORITY
: for a MongoDB replica setWriteConcern.ACKNOWLEDGED
: for single MongoDB instance
- Parameters:
client
- the connection to MongoDB.- Returns:
- the default write concern to use for Oak.
-
getDefaultReadConcern
public static com.mongodb.ReadConcern getDefaultReadConcern(@NotNull @NotNull com.mongodb.MongoClient client, @NotNull @NotNull com.mongodb.client.MongoDatabase db) Returns the default read concern depending on MongoDB deployment.ReadConcern.MAJORITY
: for a MongoDB replica set with w=majorityReadConcern.LOCAL
: for other cases
- Parameters:
db
- the connection to MongoDB.- Returns:
- the default write concern to use for Oak.
-
isMajorityWriteConcern
public static boolean isMajorityWriteConcern(@NotNull @NotNull com.mongodb.client.MongoDatabase db) Returns true if the majority write concern is used for the given DB.- Parameters:
db
- the connection to MongoDB.- Returns:
- true if the majority write concern has been configured; false otherwise
-
isSufficientWriteConcern
public static boolean isSufficientWriteConcern(@NotNull @NotNull com.mongodb.MongoClient client, @NotNull @NotNull com.mongodb.WriteConcern wc) Returnstrue
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.- Parameters:
client
- the client.wc
- the write concern.- Returns:
- whether the write concern is sufficient.
-
isSufficientReadConcern
public static boolean isSufficientReadConcern(@NotNull @NotNull com.mongodb.MongoClient client, @NotNull @NotNull com.mongodb.ReadConcern rc) Returnstrue
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.- Parameters:
client
- the client.rc
- the read concern.- Returns:
- whether the read concern is sufficient.
-
readConcernLevel
public static com.mongodb.ReadConcernLevel readConcernLevel(com.mongodb.ReadConcern readConcern)
-