Class MongoConnection


  • public class MongoConnection
    extends java.lang.Object
    The MongoConnection abstracts connection to the MongoDB.
    • Constructor Summary

      Constructors 
      Constructor 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.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method 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)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • MongoConnection

        public MongoConnection​(java.lang.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​(java.lang.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

        public MongoConnection​(java.lang.String host,
                               int port,
                               java.lang.String database)
                        throws com.mongodb.MongoException
        Constructs a new MongoConnection.
        Parameters:
        host - The host address.
        port - The port.
        database - The database name.
        Throws:
        com.mongodb.MongoException - if there are failures
      • MongoConnection

        public MongoConnection​(java.lang.String uri,
                               com.mongodb.MongoClient client)
        Constructs a new MongoConnection.
        Parameters:
        uri - the connection URI.
        client - the already connected client.
    • Method Detail

      • getMongoClient

        public com.mongodb.MongoClient getMongoClient()
        Returns:
        the MongoClient for this connection.
      • getDatabase

        public com.mongodb.client.MongoDatabase getDatabase()
        Returns the MongoDatabase as passed in the URI of the constructor.
        Returns:
        the MongoDatabase.
      • getDatabase

        public com.mongodb.client.MongoDatabase getDatabase​(@NotNull
                                                            @NotNull java.lang.String name)
        Returns the MongoDatabase with the given name.
        Returns:
        The MongoDatabase.
      • getDBName

        public java.lang.String 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

        public static java.lang.String toString​(com.mongodb.MongoClientOptions opts)
      • hasWriteConcern

        public static boolean hasWriteConcern​(@NotNull
                                              @NotNull java.lang.String uri)
        Returns true if the given uri has a write concern set.
        Parameters:
        uri - the URI to check.
        Returns:
        true if the URI has a write concern set, false otherwise.
      • hasReadConcern

        public static boolean hasReadConcern​(@NotNull
                                             @NotNull java.lang.String uri)
        Returns true if the given uri 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 set
        • WriteConcern.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=majority
        • ReadConcern.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)
        Returns 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.
        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)
        Returns 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.
        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)