Class MongoUtil

java.lang.Object
io.debezium.connector.mongodb.MongoUtil

public class MongoUtil extends Object
Utilities for working with MongoDB.
Author:
Randall Hauch
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final String
    The delimiter used between addresses.
    static final Pattern
     
    private static final Pattern
    Regular expression that gets the host and (optional) port.
    private static final Pattern
    Regular expression that gets the IPv6 host and (optional) port, where the IPv6 address must be surrounded by square brackets.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static com.mongodb.connection.ClusterDescription
    clusterDescription(com.mongodb.client.MongoClient client)
    Retrieves cluster description, forcing a connection if not yet available
    static <T> boolean
    contains(com.mongodb.client.MongoIterable<String> iterable, String match)
    Determine if the supplied MongoIterable contains an element that is equal to the supplied value.
    static <T> boolean
    contains(com.mongodb.client.MongoIterable<T> iterable, Predicate<T> matcher)
    Determine if the supplied MongoIterable contains at least one element that satisfies the given predicate.
    static <T> void
    forEach(com.mongodb.client.MongoIterable<T> iterable, Consumer<T> operation)
    Perform the given operation on each of the values in the iterable container.
    static void
    forEachCollectionNameInDatabase(com.mongodb.client.MongoClient client, String databaseName, Consumer<String> operation)
    Perform the given operation on each of the collection names in the named database.
    static void
    forEachDatabaseName(com.mongodb.client.MongoClient client, Consumer<String> operation)
    Perform the given operation on each of the database names.
    getChangeStreamSessionTransactionId(com.mongodb.client.model.changestream.ChangeStreamDocument<org.bson.BsonDocument> event)
    Helper function to extract the session transaction-id from an Change Stream event.
    static org.bson.BsonDocument
    getOplogEntry(com.mongodb.client.MongoClient client, int sortOrder, org.slf4j.Logger logger)
     
    static String
    getOplogSessionTransactionId(org.bson.BsonDocument oplogEvent)
    Helper function to extract the session transaction-id from an oplog event.
    protected static com.mongodb.ServerAddress
    getPreferredAddress(com.mongodb.client.MongoClient client, com.mongodb.ReadPreference preference)
     
    static void
    onCollection(com.mongodb.client.MongoClient client, String dbName, String collectionName, Consumer<com.mongodb.client.MongoCollection<org.bson.Document>> collectionOperation)
    Perform the given operation on the named collection in the named database, if the database and collection both exist.
    static void
    onCollectionDocuments(com.mongodb.client.MongoClient client, String dbName, String collectionName, BlockingConsumer<org.bson.Document> documentOperation)
    Perform the given operation on all of the documents inside the named collection in the named database, if the database and collection both exist.
    static void
    onDatabase(com.mongodb.client.MongoClient client, String dbName, Consumer<com.mongodb.client.MongoDatabase> dbOperation)
    Perform the given operation on the database with the given name, only if that database exists.
    static com.mongodb.ServerAddress
    parseAddress(String addressStr)
    Parse the server address string, of the form host:port or host.
    protected static List<com.mongodb.ServerAddress>
    parseAddresses(String addressStr)
    Parse the comma-separated list of server addresses.
    static String
    Find the name of the replica set precedes the host addresses.
    protected static String
    toString(com.mongodb.ServerAddress address)
     
    protected static String
    toString(List<com.mongodb.ServerAddress> addresses)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • ADDRESS_DELIMITER

      private static final String ADDRESS_DELIMITER
      The delimiter used between addresses.
      See Also:
    • ADDRESS_DELIMITER_PATTERN

      public static final Pattern ADDRESS_DELIMITER_PATTERN
    • ADDRESS_PATTERN

      private static final Pattern ADDRESS_PATTERN
      Regular expression that gets the host and (optional) port. The raw expression is ([^:]+)(:(\d+))?.
    • IPV6_ADDRESS_PATTERN

      private static final Pattern IPV6_ADDRESS_PATTERN
      Regular expression that gets the IPv6 host and (optional) port, where the IPv6 address must be surrounded by square brackets. The raw expression is (\[[^]]+\])(:(\d+))?.
  • Constructor Details

    • MongoUtil

      private MongoUtil()
  • Method Details

    • replicaSetUsedIn

      public static String replicaSetUsedIn(String addresses)
      Find the name of the replica set precedes the host addresses.
      Parameters:
      addresses - the string containing the host addresses, of the form replicaSetName/...; may not be null
      Returns:
      the replica set name, or null if no replica set name is in the string
    • forEachDatabaseName

      public static void forEachDatabaseName(com.mongodb.client.MongoClient client, Consumer<String> operation)
      Perform the given operation on each of the database names.
      Parameters:
      client - the MongoDB client; may not be null
      operation - the operation to perform; may not be null
    • forEachCollectionNameInDatabase

      public static void forEachCollectionNameInDatabase(com.mongodb.client.MongoClient client, String databaseName, Consumer<String> operation)
      Perform the given operation on each of the collection names in the named database.
      Parameters:
      client - the MongoDB client; may not be null
      databaseName - the name of the database; may not be null
      operation - the operation to perform; may not be null
    • forEach

      public static <T> void forEach(com.mongodb.client.MongoIterable<T> iterable, Consumer<T> operation)
      Perform the given operation on each of the values in the iterable container.
      Parameters:
      iterable - the iterable collection obtained from a MongoDB client; may not be null
      operation - the operation to perform; may not be null
    • onDatabase

      public static void onDatabase(com.mongodb.client.MongoClient client, String dbName, Consumer<com.mongodb.client.MongoDatabase> dbOperation)
      Perform the given operation on the database with the given name, only if that database exists.
      Parameters:
      client - the MongoDB client; may not be null
      dbName - the name of the database; may not be null
      dbOperation - the operation to perform; may not be null
    • onCollection

      public static void onCollection(com.mongodb.client.MongoClient client, String dbName, String collectionName, Consumer<com.mongodb.client.MongoCollection<org.bson.Document>> collectionOperation)
      Perform the given operation on the named collection in the named database, if the database and collection both exist.
      Parameters:
      client - the MongoDB client; may not be null
      dbName - the name of the database; may not be null
      collectionName - the name of the collection; may not be null
      collectionOperation - the operation to perform; may not be null
    • onCollectionDocuments

      public static void onCollectionDocuments(com.mongodb.client.MongoClient client, String dbName, String collectionName, BlockingConsumer<org.bson.Document> documentOperation)
      Perform the given operation on all of the documents inside the named collection in the named database, if the database and collection both exist. The operation is called once for each document, so if the collection exists but is empty then the function will not be called.
      Parameters:
      client - the MongoDB client; may not be null
      dbName - the name of the database; may not be null
      collectionName - the name of the collection; may not be null
      documentOperation - the operation to perform; may not be null
    • contains

      public static <T> boolean contains(com.mongodb.client.MongoIterable<String> iterable, String match)
      Determine if the supplied MongoIterable contains an element that is equal to the supplied value.
      Parameters:
      iterable - the iterable; may not be null
      match - the value to find in the iterable; may be null
      Returns:
      true if a matching value was found, or false otherwise
    • contains

      public static <T> boolean contains(com.mongodb.client.MongoIterable<T> iterable, Predicate<T> matcher)
      Determine if the supplied MongoIterable contains at least one element that satisfies the given predicate.
      Parameters:
      iterable - the iterable; may not be null
      matcher - the predicate function called on each value in the iterable until a match is found; may not be null
      Returns:
      true if a matching value was found, or false otherwise
    • parseAddress

      public static com.mongodb.ServerAddress parseAddress(String addressStr)
      Parse the server address string, of the form host:port or host.

      The IP address can be either an IPv4 address, or an IPv6 address surrounded by square brackets.

      Parameters:
      addressStr - the string containing the host and port; may be null
      Returns:
      the server address, or null if the string did not contain a host or host:port pair
    • getOplogEntry

      public static org.bson.BsonDocument getOplogEntry(com.mongodb.client.MongoClient client, int sortOrder, org.slf4j.Logger logger) throws com.mongodb.MongoQueryException
      Throws:
      com.mongodb.MongoQueryException
    • getOplogSessionTransactionId

      public static String getOplogSessionTransactionId(org.bson.BsonDocument oplogEvent)
      Helper function to extract the session transaction-id from an oplog event.
      Parameters:
      oplogEvent - the oplog event
      Returns:
      the session transaction id from the oplog event
    • getChangeStreamSessionTransactionId

      public static SourceInfo.SessionTransactionId getChangeStreamSessionTransactionId(com.mongodb.client.model.changestream.ChangeStreamDocument<org.bson.BsonDocument> event)
      Helper function to extract the session transaction-id from an Change Stream event.
      Parameters:
      event - the Change Stream event
      Returns:
      the session transaction id from the event
    • parseAddresses

      protected static List<com.mongodb.ServerAddress> parseAddresses(String addressStr)
      Parse the comma-separated list of server addresses. The format of the supplied string is one of the following:
       replicaSetName/host:port
       replicaSetName/host:port,host2:port2
       replicaSetName/host:port,host2:port2,host3:port3
       host:port
       host:port,host2:port2
       host:port,host2:port2,host3:port3
       
      where replicaSetName is the name of the replica set, host contains the resolvable hostname or IP address of the server, and port is the integral port number. If the port is not provided, the default port is used. If neither the host or port are provided (or addressString is null), then an address will use the default host and default port.

      The IP address can be either an IPv4 address, or an IPv6 address surrounded by square brackets.

      This method does not use the replica set name.

      Parameters:
      addressStr - the string containing a comma-separated list of host and port pairs, optionally preceded by a replica set name
      Returns:
      the list of server addresses; never null, but possibly empty
    • toString

      protected static String toString(com.mongodb.ServerAddress address)
    • toString

      protected static String toString(List<com.mongodb.ServerAddress> addresses)
    • getPreferredAddress

      protected static com.mongodb.ServerAddress getPreferredAddress(com.mongodb.client.MongoClient client, com.mongodb.ReadPreference preference)
    • clusterDescription

      public static com.mongodb.connection.ClusterDescription clusterDescription(com.mongodb.client.MongoClient client)
      Retrieves cluster description, forcing a connection if not yet available
      Parameters:
      client - cluster connection client
      Returns:
      cluster description