Skip to content

Commit

Permalink
Refactor packages for pg-index-health-jdbc-connection (#502)
Browse files Browse the repository at this point in the history
* Refactor packages for pg-index-health-jdbc-connection

* Refactor validators
  • Loading branch information
mfvanek authored Nov 19, 2024
1 parent c0f6f7e commit 8d74e95
Show file tree
Hide file tree
Showing 63 changed files with 458 additions and 210 deletions.
2 changes: 1 addition & 1 deletion config/spotbugs/exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
</Match>
<Match>
<Bug pattern="IMC_IMMATURE_CLASS_BAD_SERIALVERSIONUID"/>
<Class name="io.github.mfvanek.pg.connection.PgSqlException"/>
<Class name="io.github.mfvanek.pg.exception.PgSqlException"/>
</Match>
<Match>
<Bug pattern="STT_STRING_PARSING_A_FIELD"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import io.github.mfvanek.pg.common.maintenance.Diagnostic;
import io.github.mfvanek.pg.common.maintenance.ResultSetExtractor;
import io.github.mfvanek.pg.connection.PgConnection;
import io.github.mfvanek.pg.connection.PgHost;
import io.github.mfvanek.pg.host.PgHost;
import io.github.mfvanek.pg.model.context.PgContext;
import io.github.mfvanek.pg.model.dbobject.DbObject;
import io.github.mfvanek.pg.utils.SqlQueryReader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

package io.github.mfvanek.pg.common.maintenance;

import io.github.mfvanek.pg.connection.HostAware;
import io.github.mfvanek.pg.host.HostAware;
import io.github.mfvanek.pg.model.context.PgContext;
import io.github.mfvanek.pg.model.dbobject.DbObject;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

package io.github.mfvanek.pg.settings.maintenance;

import io.github.mfvanek.pg.connection.HostAware;
import io.github.mfvanek.pg.host.HostAware;
import io.github.mfvanek.pg.settings.ConfigurationAware;
import io.github.mfvanek.pg.settings.ParamNameAware;
import io.github.mfvanek.pg.settings.PgParam;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
package io.github.mfvanek.pg.settings.maintenance;

import io.github.mfvanek.pg.connection.PgConnection;
import io.github.mfvanek.pg.connection.PgHost;
import io.github.mfvanek.pg.host.PgHost;
import io.github.mfvanek.pg.settings.ImportantParam;
import io.github.mfvanek.pg.settings.ParamNameAware;
import io.github.mfvanek.pg.settings.PgParam;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

package io.github.mfvanek.pg.statistics.maintenance;

import io.github.mfvanek.pg.connection.HostAware;
import io.github.mfvanek.pg.host.HostAware;
import io.github.mfvanek.pg.statistics.StatisticsAware;

import java.time.OffsetDateTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
package io.github.mfvanek.pg.statistics.maintenance;

import io.github.mfvanek.pg.connection.PgConnection;
import io.github.mfvanek.pg.connection.PgHost;
import io.github.mfvanek.pg.host.PgHost;
import io.github.mfvanek.pg.utils.QueryExecutors;

import java.time.OffsetDateTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import io.github.mfvanek.pg.common.maintenance.ResultSetExtractor;
import io.github.mfvanek.pg.connection.PgConnection;
import io.github.mfvanek.pg.connection.PgSqlException;
import io.github.mfvanek.pg.exception.PgSqlException;
import io.github.mfvanek.pg.model.context.PgContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

package io.github.mfvanek.pg.checks.host;

import io.github.mfvanek.pg.connection.PgSqlException;
import io.github.mfvanek.pg.exception.PgSqlException;
import io.github.mfvanek.pg.model.context.PgContext;
import io.github.mfvanek.pg.model.index.IndexWithNulls;
import io.github.mfvanek.pg.support.DatabaseAwareTestBase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

package io.github.mfvanek.pg.settings.maintenance;

import io.github.mfvanek.pg.connection.PgSqlException;
import io.github.mfvanek.pg.exception.PgSqlException;
import io.github.mfvanek.pg.settings.ImportantParam;
import io.github.mfvanek.pg.settings.PgParam;
import io.github.mfvanek.pg.settings.PgParamImpl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import io.github.mfvanek.pg.common.maintenance.DatabaseCheckOnHost;
import io.github.mfvanek.pg.common.maintenance.Diagnostic;
import io.github.mfvanek.pg.connection.PgHost;
import io.github.mfvanek.pg.host.PgHost;
import io.github.mfvanek.pg.model.context.PgContext;
import io.github.mfvanek.pg.model.dbobject.DbObject;
import org.assertj.core.api.AbstractAssert;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

import io.github.mfvanek.pg.connection.PgConnection;
import io.github.mfvanek.pg.connection.PgConnectionImpl;
import io.github.mfvanek.pg.connection.PgHostImpl;
import io.github.mfvanek.pg.connection.PgSqlException;
import io.github.mfvanek.pg.exception.PgSqlException;
import io.github.mfvanek.pg.host.PgHostImpl;
import io.github.mfvanek.pg.model.context.PgContext;
import io.github.mfvanek.pg.support.DatabaseAwareTestBase;
import io.github.mfvanek.pg.support.TestUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

package io.github.mfvanek.pg.support;

import io.github.mfvanek.pg.connection.ConnectionCredentials;
import io.github.mfvanek.pg.connection.HighAvailabilityPgConnection;
import io.github.mfvanek.pg.connection.HighAvailabilityPgConnectionImpl;
import io.github.mfvanek.pg.connection.PgConnection;
import io.github.mfvanek.pg.connection.PgConnectionImpl;
import io.github.mfvanek.pg.connection.PgHost;
import io.github.mfvanek.pg.connection.PgHostImpl;
import io.github.mfvanek.pg.connection.factory.ConnectionCredentials;
import io.github.mfvanek.pg.host.PgHost;
import io.github.mfvanek.pg.host.PgHostImpl;
import io.github.mfvanek.pg.model.context.PgContext;
import io.github.mfvanek.pg.testing.PostgreSqlContainerWrapper;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

package io.github.mfvanek.pg.support;

import io.github.mfvanek.pg.connection.PgSqlException;
import io.github.mfvanek.pg.exception.PgSqlException;
import io.github.mfvanek.pg.support.statements.DbStatement;

import java.sql.Connection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

package io.github.mfvanek.pg.support;

import io.github.mfvanek.pg.connection.PgSqlException;
import io.github.mfvanek.pg.exception.PgSqlException;
import io.github.mfvanek.pg.model.context.PgContext;

import java.sql.Connection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

package io.github.mfvanek.pg.support.statements;

import io.github.mfvanek.pg.connection.PgSqlException;
import io.github.mfvanek.pg.exception.PgSqlException;
import io.github.mfvanek.pg.support.SchemaNameHolder;

import java.sql.ResultSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

package io.github.mfvanek.pg.support.statements;

import io.github.mfvanek.pg.connection.PgSqlException;
import io.github.mfvanek.pg.exception.PgSqlException;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.postgresql.util.PGobject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private HighAvailabilityPgConnectionImpl(@Nonnull final PgConnection connectionT
this.primaryHostDeterminer = Objects.requireNonNull(primaryHostDeterminer);
Objects.requireNonNull(connectionToPrimary, "connectionToPrimary");
final Set<PgConnection> defensiveCopy = Set.copyOf(Objects.requireNonNull(connectionsToAllHostsInCluster, "connectionsToAllHostsInCluster"));
PgConnectionValidators.shouldContainsConnectionToPrimary(connectionToPrimary, defensiveCopy);
shouldContainsConnectionToPrimary(connectionToPrimary, defensiveCopy);
this.cachedConnectionToPrimary.set(connectionToPrimary);
this.connectionsToAllHostsInCluster = defensiveCopy;
}
Expand Down Expand Up @@ -131,4 +131,11 @@ private void updateConnectionToPrimary() {
}
});
}

private static void shouldContainsConnectionToPrimary(@Nonnull final PgConnection connectionToPrimary,
@Nonnull final Set<PgConnection> connectionsToAllHostsInCluster) {
if (!connectionsToAllHostsInCluster.contains(connectionToPrimary)) {
throw new IllegalArgumentException("connectionsToAllHostsInCluster have to contain a connection to the primary");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

package io.github.mfvanek.pg.connection;

import io.github.mfvanek.pg.host.HostAware;
import io.github.mfvanek.pg.host.PgHost;

import javax.annotation.Nonnull;
import javax.sql.DataSource;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

package io.github.mfvanek.pg.connection;

import io.github.mfvanek.pg.host.PgHost;

import java.util.Objects;
import javax.annotation.Nonnull;
import javax.sql.DataSource;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

import javax.annotation.Nonnull;

/**
* Represents a service that determines if a given database connection is established with a primary host.
*/
public interface PrimaryHostDeterminer {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

package io.github.mfvanek.pg.connection;

import io.github.mfvanek.pg.exception.PgSqlException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
* Licensed under the Apache License 2.0
*/

package io.github.mfvanek.pg.connection;
package io.github.mfvanek.pg.connection.factory;

import io.github.mfvanek.pg.host.PgUrlValidators;

import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -27,7 +29,7 @@
* @author Ivan Vakhrushev
*/
@Immutable
public class ConnectionCredentials {
public final class ConnectionCredentials {

private final SortedSet<String> connectionUrls;
private final String userName;
Expand Down Expand Up @@ -77,7 +79,7 @@ public String getPassword() {
* {@inheritDoc}
*/
@Override
public final boolean equals(final Object other) {
public boolean equals(final Object other) {
if (this == other) {
return true;
}
Expand All @@ -96,7 +98,7 @@ public final boolean equals(final Object other) {
* {@inheritDoc}
*/
@Override
public final int hashCode() {
public int hashCode() {
return Objects.hash(connectionUrls, userName, password);
}

Expand All @@ -113,18 +115,34 @@ public String toString() {
'}';
}

/**
* Creates a {@link ConnectionCredentials} object with multiple connection URLs.
*
* @param connectionUrls the collection of connection URLs; must not be null or empty, and all URLs must be valid.
* @param userName the username for authentication; must not be null or blank.
* @param password the password for authentication; must not be null or blank.
* @return a new {@link ConnectionCredentials} instance.
*/
@Nonnull
public static ConnectionCredentials of(@Nonnull final Collection<String> connectionUrls,
@Nonnull final String userName,
@Nonnull final String password) {
return new ConnectionCredentials(connectionUrls, userName, password);
}

/**
* Creates a {@link ConnectionCredentials} object for a single connection URL.
*
* @param writeUrl the write connection URL; must not be null, blank, or have an invalid format.
* @param userName the username for authentication; must not be null or blank.
* @param password the password for authentication; must not be null or blank.
* @return a new {@link ConnectionCredentials} instance containing the validated URL.
*/
@Nonnull
public static ConnectionCredentials ofUrl(@Nonnull final String writeUrl,
@Nonnull final String userName,
@Nonnull final String password) {
final Set<String> connectionUrls = Set.of(PgConnectionValidators.pgUrlNotBlankAndValid(writeUrl, "writeUrl"));
final Set<String> connectionUrls = Set.of(PgUrlValidators.pgUrlNotBlankAndValid(writeUrl, "writeUrl"));
return new ConnectionCredentials(connectionUrls, userName, password);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
* Licensed under the Apache License 2.0
*/

package io.github.mfvanek.pg.connection;
package io.github.mfvanek.pg.connection.factory;

import io.github.mfvanek.pg.connection.HighAvailabilityPgConnection;

import java.util.Collection;
import javax.annotation.Nonnull;
Expand Down
Loading

0 comments on commit 8d74e95

Please sign in to comment.