Skip to content

Commit

Permalink
Add additional constructors to the Index and IndexWithBloat
Browse files Browse the repository at this point in the history
  • Loading branch information
mfvanek committed Dec 8, 2024
1 parent 8ebf69a commit e8620e3
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ void onDatabaseWithThem(final String schemaName) {
.executing(ctx)
.hasSize(4)
.containsExactlyInAnyOrder(
IndexWithBloat.of(accountsTableName, ctx.enrichWithSchema("accounts_account_number_key"), 0L, 0L, 0),
IndexWithBloat.of(accountsTableName, ctx.enrichWithSchema("accounts_pkey"), 0L, 0L, 0),
IndexWithBloat.of(clientsTableName, ctx.enrichWithSchema("clients_pkey"), 0L, 0L, 0),
IndexWithBloat.of(clientsTableName, ctx.enrichWithSchema("i_clients_email_phone"), 0L, 0L, 0))
IndexWithBloat.of(ctx, accountsTableName, "accounts_account_number_key", 0L, 0L, 0),
IndexWithBloat.of(ctx, accountsTableName, "accounts_pkey", 0L, 0L, 0),
IndexWithBloat.of(ctx, clientsTableName, "clients_pkey", 0L, 0L, 0),
IndexWithBloat.of(ctx, clientsTableName, "i_clients_email_phone", 0L, 0L, 0))
.allMatch(i -> i.getIndexSizeInBytes() > 1L)
.allMatch(i -> i.getBloatSizeInBytes() > 1L && i.getBloatPercentage() >= 14);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ void onDatabaseWithThem(final String schemaName) {
assertThat(check)
.executing(ctx)
.hasSize(1)
.containsExactly(
Index.of(ctx.enrichWithSchema("clients"), ctx.enrichWithSchema("i_clients_last_name_first_name")));
.containsExactly(Index.of(ctx, "clients", "i_clients_last_name_first_name"));

assertThat(check)
.executing(ctx, SkipTablesByNamePredicate.ofName(ctx, "clients"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

package io.github.mfvanek.pg.model.index;

import io.github.mfvanek.pg.model.context.PgContext;
import io.github.mfvanek.pg.model.dbobject.DbObject;
import io.github.mfvanek.pg.model.dbobject.PgObjectType;
import io.github.mfvanek.pg.model.table.TableNameAware;
Expand Down Expand Up @@ -146,7 +147,25 @@ public int compareTo(@Nonnull final Index other) {
* @param indexName index name; should be non-blank.
* @return {@code Index}
*/
public static Index of(@Nonnull final String tableName, @Nonnull final String indexName) {
@Nonnull
public static Index of(@Nonnull final String tableName,
@Nonnull final String indexName) {
return new Index(tableName, indexName);
}

/**
* Constructs an {@code Index} object with given context.
*
* @param pgContext the schema context to enrich index name; must be non-null.
* @param tableName table name; should be non-blank.
* @param indexName index name; should be non-blank.
* @return {@code Index}
* @since 0.14.3
*/
@Nonnull
public static Index of(@Nonnull final PgContext pgContext,
@Nonnull final String tableName,
@Nonnull final String indexName) {
return of(PgContext.enrichWith(tableName, pgContext), PgContext.enrichWith(indexName, pgContext));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package io.github.mfvanek.pg.model.index;

import io.github.mfvanek.pg.model.bloat.BloatAware;
import io.github.mfvanek.pg.model.context.PgContext;
import io.github.mfvanek.pg.model.validation.Validators;

import javax.annotation.Nonnull;
Expand Down Expand Up @@ -90,4 +91,27 @@ public static IndexWithBloat of(@Nonnull final String tableName,
final double bloatPercentage) {
return new IndexWithBloat(tableName, indexName, indexSizeInBytes, bloatSizeInBytes, bloatPercentage);
}

/**
* Constructs a {@code IndexWithBloat} object with given context.
*
* @param pgContext the schema context to enrich index name; must be non-null.
* @param tableName table name; should be non-blank.
* @param indexName index name; should be non-blank.
* @param indexSizeInBytes index size in bytes; should be positive or zero.
* @param bloatSizeInBytes bloat amount in bytes; should be positive or zero.
* @param bloatPercentage bloat percentage in the range from 0 to 100 inclusive.
* @return {@code IndexWithBloat}
* @since 0.14.3
*/
@Nonnull
public static IndexWithBloat of(@Nonnull final PgContext pgContext,
@Nonnull final String tableName,
@Nonnull final String indexName,
final long indexSizeInBytes,
final long bloatSizeInBytes,
final double bloatPercentage) {
return of(PgContext.enrichWith(tableName, pgContext), PgContext.enrichWith(indexName, pgContext),
indexSizeInBytes, bloatSizeInBytes, bloatPercentage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

package io.github.mfvanek.pg.model.index;

import io.github.mfvanek.pg.model.context.PgContext;
import io.github.mfvanek.pg.model.dbobject.PgObjectType;
import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -40,6 +41,9 @@ void validation() {
assertThatThrownBy(() -> Index.of("t", " "))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("indexName cannot be blank");
assertThatThrownBy(() -> Index.of(null, null, null))
.isInstanceOf(NullPointerException.class)
.hasMessage("pgContext cannot be null");
}

@Test
Expand All @@ -60,6 +64,8 @@ void getTableAndIndexName() {
void testToString() {
assertThat(Index.of("t", "i"))
.hasToString("Index{tableName='t', indexName='i'}");
assertThat(Index.of(PgContext.of("tst"), "t", "i"))
.hasToString("Index{tableName='tst.t', indexName='tst.i'}");
}

@SuppressWarnings("ConstantConditions")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

package io.github.mfvanek.pg.model.index;

import io.github.mfvanek.pg.model.context.PgContext;
import io.github.mfvanek.pg.model.dbobject.PgObjectType;
import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -41,6 +42,8 @@ void getBloatPercentage() {
void testToString() {
assertThat(IndexWithBloat.of("t", "i", 2L, 1L, 50))
.hasToString("IndexWithBloat{tableName='t', indexName='i', indexSizeInBytes=2, bloatSizeInBytes=1, bloatPercentage=50.0}");
assertThat(IndexWithBloat.of(PgContext.of("tst"), "t", "i", 2L, 1L, 50))
.hasToString("IndexWithBloat{tableName='tst.t', indexName='tst.i', indexSizeInBytes=2, bloatSizeInBytes=1, bloatPercentage=50.0}");
}

@Test
Expand All @@ -54,8 +57,8 @@ void withInvalidArguments() {
assertThatThrownBy(() -> IndexWithBloat.of("t", "i", -1L, 0L, 0))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("indexSizeInBytes cannot be less than zero");
final IndexWithBloat bloat = IndexWithBloat.of("t", "i", 0L, 0L, 0);
assertThat(bloat).isNotNull();
assertThat(IndexWithBloat.of("t", "i", 0L, 0L, 0))
.isNotNull();
}

@SuppressWarnings("ConstantConditions")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void shouldWorkWithCustomSchema(final String schemaName) {
final PgContext ctx = PgContext.of(schemaName);
assertThat(SkipFlywayTablesPredicate.of(ctx))
.accepts(Table.of(ctx, "t"))
.accepts(Index.of(ctx.enrichWithSchema("t"), ctx.enrichWithSchema("i")))
.accepts(Index.of(ctx, "t", "i"))
.accepts(SequenceState.of(ctx, "s", "int", 100.0))
.rejects(Table.of(ctx, "flyway_schema_history"))
.rejects(Table.of(ctx, "FLYWAY_SCHEMA_HISTORY"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void shouldWorkWithCustomSchema(final String schemaName) {
final PgContext ctx = PgContext.of(schemaName);
assertThat(SkipIndexesByNamePredicate.of(ctx, Set.of("i1", "i2")))
.accepts(Table.of(ctx, "t"))
.accepts(Index.of(ctx.enrichWithSchema("t1"), ctx.enrichWithSchema("i11")))
.rejects(Index.of(ctx.enrichWithSchema("t2"), ctx.enrichWithSchema("i2")));
.accepts(Index.of(ctx, "t1", "i11"))
.rejects(Index.of(ctx, "t2", "i2"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void shouldWorkWithCustomSchema(final String schemaName) {
final PgContext ctx = PgContext.of(schemaName);
assertThat(SkipLiquibaseTablesPredicate.of(ctx))
.accepts(Table.of(ctx, "t"))
.accepts(Index.of(ctx.enrichWithSchema("t"), ctx.enrichWithSchema("i")))
.accepts(Index.of(ctx, "t", "i"))
.accepts(SequenceState.of(ctx, "s", "int", 100.0))
.rejects(Table.of(ctx, "databasechangelog"))
.rejects(Table.of(ctx, "DATABASECHANGELOG"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ void shouldWorkWithCustomSchema(final String schemaName) {
final PgContext ctx = PgContext.of(schemaName);
assertThat(SkipTablesByNamePredicate.of(ctx, Set.of("t2", "T1")))
.accepts(Table.of(ctx, "t"))
.accepts(Index.of(ctx.enrichWithSchema("T"), ctx.enrichWithSchema("I")))
.accepts(Index.of(ctx, "T", "I"))
.accepts(SequenceState.of(ctx, "s", "int", 100.0))
.rejects(Index.of(ctx.enrichWithSchema("t1"), ctx.enrichWithSchema("i1")))
.rejects(Index.of(ctx.enrichWithSchema("T2"), ctx.enrichWithSchema("i2")))
.rejects(Index.of(ctx, "t1", "i1"))
.rejects(Index.of(ctx, "T2", "i2"))
.accepts(Table.of(ctx, "t11"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ void onDatabaseWithThem(final String schemaName) {
.executing(ctx)
.hasSize(4)
.containsExactlyInAnyOrder(
IndexWithBloat.of(accountsTableName, ctx.enrichWithSchema("accounts_account_number_key"), 0L, 0L, 0),
IndexWithBloat.of(accountsTableName, ctx.enrichWithSchema("accounts_pkey"), 0L, 0L, 0),
IndexWithBloat.of(clientsTableName, ctx.enrichWithSchema("clients_pkey"), 0L, 0L, 0),
IndexWithBloat.of(clientsTableName, ctx.enrichWithSchema("i_clients_email_phone"), 0L, 0L, 0))
IndexWithBloat.of(ctx, accountsTableName, "accounts_account_number_key", 0L, 0L, 0),
IndexWithBloat.of(ctx, accountsTableName, "accounts_pkey", 0L, 0L, 0),
IndexWithBloat.of(ctx, clientsTableName, "clients_pkey", 0L, 0L, 0),
IndexWithBloat.of(ctx, clientsTableName, "i_clients_email_phone", 0L, 0L, 0))
.allMatch(i -> i.getIndexSizeInBytes() > 1L)
.allMatch(i -> i.getBloatSizeInBytes() > 1L && i.getBloatPercentage() >= 14);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ void onDatabaseWithThem(final String schemaName) {
assertThat(check)
.executing(ctx)
.hasSize(1)
.containsExactly(
Index.of(ctx.enrichWithSchema("clients"), ctx.enrichWithSchema("i_clients_last_name_first_name")));
.containsExactly(Index.of(ctx, "clients", "i_clients_last_name_first_name"));

assertThat(check)
.executing(ctx, SkipTablesByNamePredicate.ofName(ctx, "clients"))
Expand Down

0 comments on commit e8620e3

Please sign in to comment.