Package io.debezium.connector.mongodb
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 StringADDRESS_DELIMITERThe delimiter used between addresses.static PatternADDRESS_DELIMITER_PATTERNprivate static PatternADDRESS_PATTERNRegular expression that gets the host and (optional) port.private static PatternIPV6_ADDRESS_PATTERNRegular 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 privateMongoUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> booleancontains(com.mongodb.client.MongoIterable<String> iterable, String match)Determine if the suppliedMongoIterablecontains an element that is equal to the supplied value.static <T> booleancontains(com.mongodb.client.MongoIterable<T> iterable, Predicate<T> matcher)Determine if the suppliedMongoIterablecontains at least one element that satisfies the given predicate.static <T> voidforEach(com.mongodb.client.MongoIterable<T> iterable, Consumer<T> operation)Perform the given operation on each of the values in the iterable container.static voidforEachCollectionNameInDatabase(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 voidforEachDatabaseName(com.mongodb.client.MongoClient client, Consumer<String> operation)Perform the given operation on each of the database names.static StringgetOplogSessionTransactionId(org.bson.Document oplogEvent)Helper function to extract the session transaction-id from an oplog event.protected static com.mongodb.ServerAddressgetPrimaryAddress(com.mongodb.client.MongoClient client)static voidonCollection(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 voidonCollectionDocuments(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 voidonDatabase(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.ServerAddressparseAddress(String addressStr)Parse the server address string, of the formhost:portorhost.protected static List<com.mongodb.ServerAddress>parseAddresses(String addressStr)Parse the comma-separated list of server addresses.static StringreplicaSetUsedIn(String addresses)Find the name of the replica set precedes the host addresses.protected static StringtoString(com.mongodb.ServerAddress address)protected static StringtoString(List<com.mongodb.ServerAddress> addresses)
-
-
-
Field Detail
-
ADDRESS_DELIMITER
private static final String ADDRESS_DELIMITER
The delimiter used between addresses.- See Also:
- Constant Field Values
-
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+))?.
-
-
Method Detail
-
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 formreplicaSetName/...; may not be null- Returns:
- the replica set name, or
nullif 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 nulloperation- 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 nulldatabaseName- the name of the database; may not be nulloperation- 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 nulloperation- 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 nulldbName- the name of the database; may not be nulldbOperation- 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 nulldbName- the name of the database; may not be nullcollectionName- the name of the collection; may not be nullcollectionOperation- 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 nulldbName- the name of the database; may not be nullcollectionName- the name of the collection; may not be nulldocumentOperation- 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 suppliedMongoIterablecontains an element that is equal to the supplied value.- Parameters:
iterable- the iterable; may not be nullmatch- the value to find in the iterable; may be null- Returns:
trueif a matching value was found, orfalseotherwise
-
contains
public static <T> boolean contains(com.mongodb.client.MongoIterable<T> iterable, Predicate<T> matcher)Determine if the suppliedMongoIterablecontains at least one element that satisfies the given predicate.- Parameters:
iterable- the iterable; may not be nullmatcher- the predicate function called on each value in the iterable until a match is found; may not be null- Returns:
trueif a matching value was found, orfalseotherwise
-
parseAddress
public static com.mongodb.ServerAddress parseAddress(String addressStr)
Parse the server address string, of the formhost:portorhost.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
nullif the string did not contain a host or host:port pair
-
getOplogSessionTransactionId
public static String getOplogSessionTransactionId(org.bson.Document 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
-
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
wherereplicaSetNameis the name of the replica set,hostcontains the resolvable hostname or IP address of the server, andportis the integral port number. If the port is not provided, thedefault portis used. If neither the host or port are provided (oraddressStringisnull), then an address will use thedefault hostanddefault 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)
-
getPrimaryAddress
protected static com.mongodb.ServerAddress getPrimaryAddress(com.mongodb.client.MongoClient client)
-
-