Skip to content

Commit

Permalink
Add additional constructors to the TableWithBloat and TableWithMissin…
Browse files Browse the repository at this point in the history
…gIndex
  • Loading branch information
mfvanek committed Dec 7, 2024
1 parent 2d538e4 commit 0c7749f
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ void onDatabaseWithThem(final String schemaName) {
.executing(ctx)
.hasSize(2)
.containsExactlyInAnyOrder(
TableWithBloat.of(ctx.enrichWithSchema("accounts"), 0L, 0L, 0),
TableWithBloat.of(ctx.enrichWithSchema("clients"), 0L, 0L, 0))
TableWithBloat.of(ctx, "accounts", 0L, 0L, 0),
TableWithBloat.of(ctx, "clients", 0L, 0L, 0))
.allMatch(t -> t.getTableSizeInBytes() > 0L) // real size doesn't matter
.allMatch(t -> t.getBloatPercentage() == 0 && t.getBloatSizeInBytes() == 0L);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void onDatabaseWithThem(final String schemaName) {
.executing(ctx)
.hasSize(1)
.containsExactly(
TableWithMissingIndex.of(ctx.enrichWithSchema("accounts"), 0L, 0L, 0L))
TableWithMissingIndex.of(ctx, "accounts", 0L, 0L, 0L))
.allMatch(t -> t.getSeqScans() >= AMOUNT_OF_TRIES)
.allMatch(t -> t.getIndexScans() == 0)
.allMatch(t -> t.getTableSizeInBytes() > 1L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package io.github.mfvanek.pg.model.table;

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 java.util.Objects;
Expand Down Expand Up @@ -107,6 +108,7 @@ public int compareTo(@Nonnull final TableWithBloat other) {
* @param bloatPercentage bloat percentage in the range from 0 to 100 inclusive.
* @return {@code TableWithBloat}
*/
@Nonnull
public static TableWithBloat of(@Nonnull final String tableName,
final long tableSizeInBytes,
final long bloatSizeInBytes,
Expand All @@ -115,6 +117,27 @@ public static TableWithBloat of(@Nonnull final String tableName,
return of(table, bloatSizeInBytes, bloatPercentage);
}

/**
* Constructs a {@code TableWithBloat} object with given context.
*
* @param pgContext the schema context to enrich table name; must be non-null.
* @param tableName table name; should be non-blank.
* @param tableSizeInBytes table 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 TableWithBloat}
* @since 0.14.3
*/
@Nonnull
public static TableWithBloat of(@Nonnull final PgContext pgContext,
@Nonnull final String tableName,
final long tableSizeInBytes,
final long bloatSizeInBytes,
final double bloatPercentage) {
final Table table = Table.of(pgContext, tableName, tableSizeInBytes);
return of(table, bloatSizeInBytes, bloatPercentage);
}

/**
* Constructs a {@code TableWithBloat} object.
*
Expand All @@ -124,6 +147,7 @@ public static TableWithBloat of(@Nonnull final String tableName,
* @return {@code TableWithBloat}
* @since 0.7.0
*/
@Nonnull
public static TableWithBloat of(@Nonnull final Table table,
final long bloatSizeInBytes,
final double bloatPercentage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

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

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

import java.util.Objects;
Expand Down Expand Up @@ -118,6 +119,7 @@ public int compareTo(@Nonnull final TableWithMissingIndex other) {
* @param indexScans number of index scans initiated on this table; should be non-negative.
* @return {@code TableWithMissingIndex}
*/
@Nonnull
public static TableWithMissingIndex of(@Nonnull final String tableName,
final long tableSizeInBytes,
final long seqScans,
Expand All @@ -126,6 +128,27 @@ public static TableWithMissingIndex of(@Nonnull final String tableName,
return of(table, seqScans, indexScans);
}

/**
* Constructs a {@code TableWithMissingIndex} object with given context.
*
* @param pgContext the schema context to enrich table name; must be non-null.
* @param tableName table name; should be non-blank.
* @param tableSizeInBytes table size in bytes; should be positive or zero.
* @param seqScans number of sequential scans initiated on this table; should be non-negative.
* @param indexScans number of index scans initiated on this table; should be non-negative.
* @return {@code TableWithMissingIndex}
* @since 0.14.3
*/
@Nonnull
public static TableWithMissingIndex of(@Nonnull final PgContext pgContext,
@Nonnull final String tableName,
final long tableSizeInBytes,
final long seqScans,
final long indexScans) {
final Table table = Table.of(pgContext, tableName, tableSizeInBytes);
return of(table, seqScans, indexScans);
}

/**
* Constructs a {@code TableWithMissingIndex} object.
*
Expand All @@ -135,6 +158,7 @@ public static TableWithMissingIndex of(@Nonnull final String tableName,
* @return {@code TableWithMissingIndex}
* @since 0.7.0
*/
@Nonnull
public static TableWithMissingIndex of(@Nonnull final Table table,
final long seqScans,
final long indexScans) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ void testToString() {
.hasToString("Table{tableName='t', tableSizeInBytes=2}");
assertThat(Table.of(PgContext.ofPublic(), "t", 2L))
.hasToString("Table{tableName='t', tableSizeInBytes=2}");
assertThat(Table.of(PgContext.of("test_schema"), "t"))
.hasToString("Table{tableName='test_schema.t', tableSizeInBytes=0}");
assertThat(Table.of(PgContext.of("tst"), "t"))
.hasToString("Table{tableName='tst.t', tableSizeInBytes=0}");
}

@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.table;

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 @@ -39,6 +40,8 @@ void gettersShouldWork() {
void testToString() {
assertThat(TableWithBloat.of("t", 2L, 1L, 50))
.hasToString("TableWithBloat{tableName='t', tableSizeInBytes=2, bloatSizeInBytes=1, bloatPercentage=50.0}");
assertThat(TableWithBloat.of(PgContext.of("tst"), "t", 2L, 1L, 50))
.hasToString("TableWithBloat{tableName='tst.t', tableSizeInBytes=2, bloatSizeInBytes=1, bloatPercentage=50.0}");
}

@SuppressWarnings("ConstantConditions")
Expand All @@ -59,6 +62,9 @@ void withInvalidArguments() {
assertThatThrownBy(() -> TableWithBloat.of("t", -1L, 0L, 0))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("tableSizeInBytes cannot be less than zero");
assertThatThrownBy(() -> TableWithBloat.of(null, "t", 0L, 0L, 0))
.isInstanceOf(NullPointerException.class)
.hasMessage("pgContext cannot be null");

assertThat(TableWithBloat.of("t", 0L, 0L, 0))
.isNotNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

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

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 @@ -59,12 +60,17 @@ void invalidArguments() {
assertThatThrownBy(() -> TableWithMissingIndex.of(null, 0L, 0L))
.isInstanceOf(NullPointerException.class)
.hasMessage("table cannot be null");
assertThatThrownBy(() -> TableWithMissingIndex.of(null, "t", 0L, 0L, 0L))
.isInstanceOf(NullPointerException.class)
.hasMessage("pgContext cannot be null");
}

@Test
void testToString() {
assertThat(TableWithMissingIndex.of("t", 11L, 33L, 22L))
.hasToString("TableWithMissingIndex{tableName='t', tableSizeInBytes=11, seqScans=33, indexScans=22}");
assertThat(TableWithMissingIndex.of(PgContext.of("tst"), "t", 11L, 33L, 22L))
.hasToString("TableWithMissingIndex{tableName='tst.t', tableSizeInBytes=11, seqScans=33, indexScans=22}");
}

@SuppressWarnings("ConstantConditions")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ void onDatabaseWithThem(final String schemaName) {
.executing(ctx)
.hasSize(2)
.containsExactlyInAnyOrder(
TableWithBloat.of(ctx.enrichWithSchema("accounts"), 0L, 0L, 0),
TableWithBloat.of(ctx.enrichWithSchema("clients"), 0L, 0L, 0))
TableWithBloat.of(ctx, "accounts", 0L, 0L, 0),
TableWithBloat.of(ctx, "clients", 0L, 0L, 0))
.allMatch(t -> t.getTableSizeInBytes() > 0L) // real size doesn't matter
.allMatch(t -> t.getBloatPercentage() == 0 && t.getBloatSizeInBytes() == 0L);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void onDatabaseWithThem(final String schemaName) {
.executing(ctx)
.hasSize(1)
.containsExactly(
TableWithMissingIndex.of(ctx.enrichWithSchema("accounts"), 0L, 0L, 0L))
TableWithMissingIndex.of(ctx, "accounts", 0L, 0L, 0L))
.allMatch(t -> t.getSeqScans() >= AMOUNT_OF_TRIES)
.allMatch(t -> t.getIndexScans() == 0)
.allMatch(t -> t.getTableSizeInBytes() > 1L);
Expand Down

0 comments on commit 0c7749f

Please sign in to comment.