Class MongoConnection
- java.lang.Object
-
- org.apache.jackrabbit.oak.plugins.document.util.MongoConnection
-
public class MongoConnection extends Object
TheMongoConnectionabstracts connection to theMongoDB.
-
-
Field Summary
Fields Modifier and Type Field Description static StringMONGODB_PREFIX
-
Constructor Summary
Constructors Constructor Description MongoConnection(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.client.MongoClient client)Constructs a newMongoConnection.MongoConnection(String uri, com.mongodb.MongoClientSettings.Builder builder)Constructs a new connection using the specified MongoDB connection String.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the underlying Mongo instancecom.mongodb.client.MongoDatabasegetDatabase()Returns theMongoDatabaseas passed in the URI of the constructor.com.mongodb.client.MongoDatabasegetDatabase(@NotNull String name)Returns theMongoDatabasewith the given name.StringgetDBName()static com.mongodb.MongoClientSettings.BuildergetDefaultBuilder()Constructs a builder with default options set.static com.mongodb.ReadConcerngetDefaultReadConcern(@NotNull com.mongodb.client.MongoClient client, @NotNull com.mongodb.client.MongoDatabase db)Returns the default read concern depending on MongoDB deployment.static com.mongodb.WriteConcerngetDefaultWriteConcern(@NotNull com.mongodb.client.MongoClient client)Returns the default write concern depending on MongoDB deployment.com.mongodb.client.MongoClientgetMongoClient()com.mongodb.ConnectionStringgetMongoURI()static booleanhasReadConcern(@NotNull String uri)Returnstrueif the givenurihas a read concern set.static booleanhasWriteConcern(@NotNull String uri)Returnstrueif the givenurihas a write concern set.static booleanisMajorityWriteConcern(@NotNull com.mongodb.client.MongoDatabase db)Returns true if the majority write concern is used for the given DB.static booleanisSufficientReadConcern(@NotNull com.mongodb.client.MongoClient client, @NotNull com.mongodb.ReadConcern rc)Returnstrueif the given read concern is sufficient for Oak.static booleanisSufficientWriteConcern(@NotNull com.mongodb.client.MongoClient client, @NotNull com.mongodb.WriteConcern wc)Returnstrueif the given write concern is sufficient for Oak.static com.mongodb.ReadConcernLevelreadConcernLevel(com.mongodb.ReadConcern readConcern)static StringtoString(com.mongodb.MongoClientSettings settings)
-
-
-
Field Detail
-
MONGODB_PREFIX
public static final String MONGODB_PREFIX
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
MongoConnection
public MongoConnection(String uri) throws com.mongodb.MongoException
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.MongoClientSettings.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
public MongoConnection(String host, int port, String database) throws com.mongodb.MongoException
Constructs a newMongoConnection.- Parameters:
host- The host address.port- The port.database- The database name.- Throws:
com.mongodb.MongoException- if there are failures
-
MongoConnection
public MongoConnection(String uri, com.mongodb.client.MongoClient client)
Constructs a newMongoConnection.- Parameters:
uri- the connection URI.client- the already connected client.
-
-
Method Detail
-
getMongoURI
public com.mongodb.ConnectionString getMongoURI()
- Returns:
- the
ConnectionStringfor this connection
-
getMongoClient
public com.mongodb.client.MongoClient getMongoClient()
- Returns:
- the
MongoClientfor this connection.
-
getDatabase
public com.mongodb.client.MongoDatabase getDatabase()
Returns theMongoDatabaseas passed in the URI of the constructor.- Returns:
- the
MongoDatabase.
-
getDatabase
public com.mongodb.client.MongoDatabase getDatabase(@NotNull @NotNull String name)Returns theMongoDatabasewith the given name.- Returns:
- The
MongoDatabase.
-
getDBName
public String getDBName()
- Returns:
- the database name specified in the URI.
-
close
public void close()
Closes the underlying Mongo instance
-
getDefaultBuilder
public static com.mongodb.MongoClientSettings.Builder getDefaultBuilder()
Constructs a builder with default options set. These can be overridden later- Returns:
- builder with default options set
-
toString
public static String toString(com.mongodb.MongoClientSettings settings)
-
hasWriteConcern
public static boolean hasWriteConcern(@NotNull @NotNull String uri)Returnstrueif the givenurihas a write concern set.- Parameters:
uri- the URI to check.- Returns:
trueif the URI has a write concern set,falseotherwise.
-
hasReadConcern
public static boolean hasReadConcern(@NotNull @NotNull String uri)Returnstrueif the givenurihas a read concern set.- Parameters:
uri- the URI to check.- Returns:
trueif the URI has a read concern set,falseotherwise.
-
getDefaultWriteConcern
public static com.mongodb.WriteConcern getDefaultWriteConcern(@NotNull @NotNull com.mongodb.client.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.client.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.client.MongoClient client, @NotNull @NotNull com.mongodb.WriteConcern wc)Returnstrueif 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.client.MongoClient client, @NotNull @NotNull com.mongodb.ReadConcern rc)Returnstrueif 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)
-
-