Skip to content

Commit a2d1701

Browse files
authored
Get rid of AdditionalValidators and code duplication. Contains breaking changes! (#404)
* Fix for large database * Update sql queries * Remove AdditionalValidators * Up version because of breaking changes
1 parent f60e9b0 commit a2d1701

28 files changed

+112
-149
lines changed

build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ description = "pg-index-health build"
1111

1212
allprojects {
1313
group = "io.github.mfvanek"
14-
version = "0.11.2"
14+
version = "0.12.0"
1515

1616
repositories {
1717
mavenLocal()

docker/docker-compose.yml

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ version: "3.9"
33
services:
44
postgres:
55
image: postgres:16.2
6+
shm_size: "2gb"
67
environment:
78
POSTGRES_DB: "pgih-db"
89
POSTGRES_USER: "pgih-db-user"

pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/BloatAware.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ public interface BloatAware {
2929
*
3030
* @return bloat percentage
3131
*/
32-
int getBloatPercentage();
32+
double getBloatPercentage();
3333
}

pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/PgContext.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class PgContext {
2525
/**
2626
* Default bloat percentage threshold.
2727
*/
28-
public static final int DEFAULT_BLOAT_PERCENTAGE_THRESHOLD = 10;
28+
public static final double DEFAULT_BLOAT_PERCENTAGE_THRESHOLD = 10.0;
2929
/**
3030
* Default schema name.
3131
*/
@@ -36,13 +36,12 @@ public class PgContext {
3636
public static final double DEFAULT_REMAINING_PERCENTAGE_THRESHOLD = 10.0;
3737

3838
private final String schemaName;
39-
private final int bloatPercentageThreshold;
39+
private final double bloatPercentageThreshold;
4040
private final double remainingPercentageThreshold;
4141

42-
private PgContext(@Nonnull final String schemaName, final int bloatPercentageThreshold, final double remainingPercentageThreshold) {
42+
private PgContext(@Nonnull final String schemaName, final double bloatPercentageThreshold, final double remainingPercentageThreshold) {
4343
this.schemaName = Validators.notBlank(schemaName, "schemaName").toLowerCase(Locale.ROOT);
44-
this.bloatPercentageThreshold = Validators.argumentNotNegative(
45-
bloatPercentageThreshold, "bloatPercentageThreshold");
44+
this.bloatPercentageThreshold = Validators.validPercent(bloatPercentageThreshold, "bloatPercentageThreshold");
4645
this.remainingPercentageThreshold = Validators.validPercent(remainingPercentageThreshold, "remainingPercentageThreshold");
4746
}
4847

@@ -70,7 +69,7 @@ public boolean isDefaultSchema() {
7069
*
7170
* @return bloat percentage threshold
7271
*/
73-
public int getBloatPercentageThreshold() {
72+
public double getBloatPercentageThreshold() {
7473
return bloatPercentageThreshold;
7574
}
7675

@@ -128,7 +127,9 @@ public String enrichWithSchema(@Nonnull final String objectName) {
128127
* @return {@code PgContext}
129128
*/
130129
@Nonnull
131-
public static PgContext of(@Nonnull final String schemaName, final int bloatPercentageThreshold, final double remainingPercentageThreshold) {
130+
public static PgContext of(@Nonnull final String schemaName,
131+
final double bloatPercentageThreshold,
132+
final double remainingPercentageThreshold) {
132133
return new PgContext(schemaName, bloatPercentageThreshold, remainingPercentageThreshold);
133134
}
134135

@@ -141,7 +142,8 @@ public static PgContext of(@Nonnull final String schemaName, final int bloatPerc
141142
* @see PgContext#DEFAULT_REMAINING_PERCENTAGE_THRESHOLD
142143
*/
143144
@Nonnull
144-
public static PgContext of(@Nonnull final String schemaName, final int bloatPercentageThreshold) {
145+
public static PgContext of(@Nonnull final String schemaName,
146+
final double bloatPercentageThreshold) {
145147
return new PgContext(schemaName, bloatPercentageThreshold, DEFAULT_REMAINING_PERCENTAGE_THRESHOLD);
146148
}
147149

pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/index/IndexWithBloat.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@
2424
public class IndexWithBloat extends IndexWithSize implements IndexBloatAware {
2525

2626
private final long bloatSizeInBytes;
27-
private final int bloatPercentage;
27+
private final double bloatPercentage;
2828

2929
private IndexWithBloat(@Nonnull final String tableName,
3030
@Nonnull final String indexName,
3131
final long indexSizeInBytes,
3232
final long bloatSizeInBytes,
33-
final int bloatPercentage) {
33+
final double bloatPercentage) {
3434
super(tableName, indexName, indexSizeInBytes);
3535
this.bloatSizeInBytes = Validators.sizeNotNegative(bloatSizeInBytes, "bloatSizeInBytes");
36-
this.bloatPercentage = Validators.argumentNotNegative(bloatPercentage, "bloatPercentage");
36+
this.bloatPercentage = Validators.validPercent(bloatPercentage, "bloatPercentage");
3737
}
3838

3939
/**
@@ -48,7 +48,7 @@ public long getBloatSizeInBytes() {
4848
* {@inheritDoc}
4949
*/
5050
@Override
51-
public int getBloatPercentage() {
51+
public double getBloatPercentage() {
5252
return bloatPercentage;
5353
}
5454

@@ -86,7 +86,7 @@ public static IndexWithBloat of(@Nonnull final String tableName,
8686
@Nonnull final String indexName,
8787
final long indexSizeInBytes,
8888
final long bloatSizeInBytes,
89-
final int bloatPercentage) {
89+
final double bloatPercentage) {
9090
return new IndexWithBloat(tableName, indexName, indexSizeInBytes, bloatSizeInBytes, bloatPercentage);
9191
}
9292
}

pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/sequence/SequenceState.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* and the percentage of remaining values before it overflows.
2424
*
2525
* @author Blohny
26-
* @since 0.11.2
26+
* @since 0.12.0
2727
*/
2828
@Immutable
2929
public class SequenceState implements DbObject {

pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/table/TableWithBloat.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
public class TableWithBloat extends AbstractTableAware implements TableBloatAware, Comparable<TableWithBloat> {
2626

2727
private final long bloatSizeInBytes;
28-
private final int bloatPercentage;
28+
private final double bloatPercentage;
2929

3030
private TableWithBloat(@Nonnull final Table table,
3131
final long bloatSizeInBytes,
32-
final int bloatPercentage) {
32+
final double bloatPercentage) {
3333
super(table);
3434
this.bloatSizeInBytes = Validators.sizeNotNegative(bloatSizeInBytes, "bloatSizeInBytes");
35-
this.bloatPercentage = Validators.argumentNotNegative(bloatPercentage, "bloatPercentage");
35+
this.bloatPercentage = Validators.validPercent(bloatPercentage, "bloatPercentage");
3636
}
3737

3838
/**
@@ -47,7 +47,7 @@ public long getBloatSizeInBytes() {
4747
* {@inheritDoc}
4848
*/
4949
@Override
50-
public int getBloatPercentage() {
50+
public double getBloatPercentage() {
5151
return bloatPercentage;
5252
}
5353

@@ -109,7 +109,7 @@ public int compareTo(@Nonnull final TableWithBloat other) {
109109
public static TableWithBloat of(@Nonnull final String tableName,
110110
final long tableSizeInBytes,
111111
final long bloatSizeInBytes,
112-
final int bloatPercentage) {
112+
final double bloatPercentage) {
113113
final Table table = Table.of(tableName, tableSizeInBytes);
114114
return of(table, bloatSizeInBytes, bloatPercentage);
115115
}
@@ -125,7 +125,7 @@ public static TableWithBloat of(@Nonnull final String tableName,
125125
*/
126126
public static TableWithBloat of(@Nonnull final Table table,
127127
final long bloatSizeInBytes,
128-
final int bloatPercentage) {
128+
final double bloatPercentage) {
129129
return new TableWithBloat(table, bloatSizeInBytes, bloatPercentage);
130130
}
131131
}

pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/PgContextTest.java

+13-9
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,14 @@ void getSchemaNameForPublicSchemaWithUpperCase() {
4040

4141
@Test
4242
void getBloatPercentageThreshold() {
43-
assertThat(PgContext.of("s").getBloatPercentageThreshold()).isEqualTo(10);
44-
assertThat(PgContext.of("s", 22).getBloatPercentageThreshold()).isEqualTo(22);
45-
assertThat(PgContext.ofPublic().getBloatPercentageThreshold()).isEqualTo(10);
46-
assertThat(PgContext.of("s").getRemainingPercentageThreshold()).isEqualTo(10.0);
43+
assertThat(PgContext.of("s").getBloatPercentageThreshold())
44+
.isEqualTo(10.0);
45+
assertThat(PgContext.of("s", 22.0).getBloatPercentageThreshold())
46+
.isEqualTo(22.0);
47+
assertThat(PgContext.ofPublic().getBloatPercentageThreshold())
48+
.isEqualTo(10.0);
49+
assertThat(PgContext.of("s").getRemainingPercentageThreshold())
50+
.isEqualTo(10.0);
4751
}
4852

4953
@SuppressWarnings("ConstantConditions")
@@ -60,7 +64,7 @@ void withInvalidArguments() {
6064
.hasMessage("schemaName cannot be blank");
6165
assertThatThrownBy(() -> PgContext.of("s", -1))
6266
.isInstanceOf(IllegalArgumentException.class)
63-
.hasMessage("bloatPercentageThreshold cannot be less than zero");
67+
.hasMessage("bloatPercentageThreshold should be in the range from 0.0 to 100.0 inclusive");
6468
assertThatThrownBy(() -> PgContext.of("s", 1, -1))
6569
.isInstanceOf(IllegalArgumentException.class)
6670
.hasMessage("remainingPercentageThreshold should be in the range from 0.0 to 100.0 inclusive");
@@ -69,13 +73,13 @@ void withInvalidArguments() {
6973
@Test
7074
void testToString() {
7175
assertThat(PgContext.of("s"))
72-
.hasToString("PgContext{schemaName='s', bloatPercentageThreshold=10, remainingPercentageThreshold=10.0}");
76+
.hasToString("PgContext{schemaName='s', bloatPercentageThreshold=10.0, remainingPercentageThreshold=10.0}");
7377
assertThat(PgContext.of("s", 11))
74-
.hasToString("PgContext{schemaName='s', bloatPercentageThreshold=11, remainingPercentageThreshold=10.0}");
78+
.hasToString("PgContext{schemaName='s', bloatPercentageThreshold=11.0, remainingPercentageThreshold=10.0}");
7579
assertThat(PgContext.ofPublic())
76-
.hasToString("PgContext{schemaName='public', bloatPercentageThreshold=10, remainingPercentageThreshold=10.0}");
80+
.hasToString("PgContext{schemaName='public', bloatPercentageThreshold=10.0, remainingPercentageThreshold=10.0}");
7781
assertThat(PgContext.of("s", 11, 15.0))
78-
.hasToString("PgContext{schemaName='s', bloatPercentageThreshold=11, remainingPercentageThreshold=15.0}");
82+
.hasToString("PgContext{schemaName='s', bloatPercentageThreshold=11.0, remainingPercentageThreshold=15.0}");
7983
}
8084

8185
@Test

pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/index/IndexWithBloatTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void getBloatPercentage() {
3737
@Test
3838
void testToString() {
3939
assertThat(IndexWithBloat.of("t", "i", 2L, 1L, 50))
40-
.hasToString("IndexWithBloat{tableName='t', indexName='i', indexSizeInBytes=2, bloatSizeInBytes=1, bloatPercentage=50}");
40+
.hasToString("IndexWithBloat{tableName='t', indexName='i', indexSizeInBytes=2, bloatSizeInBytes=1, bloatPercentage=50.0}");
4141
}
4242

4343
@Test
@@ -47,7 +47,7 @@ void withInvalidArguments() {
4747
.hasMessage("bloatSizeInBytes cannot be less than zero");
4848
assertThatThrownBy(() -> IndexWithBloat.of("t", "i", 0L, 0L, -1))
4949
.isInstanceOf(IllegalArgumentException.class)
50-
.hasMessage("bloatPercentage cannot be less than zero");
50+
.hasMessage("bloatPercentage should be in the range from 0.0 to 100.0 inclusive");
5151
assertThatThrownBy(() -> IndexWithBloat.of("t", "i", -1L, 0L, 0))
5252
.isInstanceOf(IllegalArgumentException.class)
5353
.hasMessage("indexSizeInBytes cannot be less than zero");

pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/table/TableWithBloatTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void gettersShouldWork() {
3535
@Test
3636
void testToString() {
3737
assertThat(TableWithBloat.of("t", 2L, 1L, 50))
38-
.hasToString("TableWithBloat{tableName='t', tableSizeInBytes=2, bloatSizeInBytes=1, bloatPercentage=50}");
38+
.hasToString("TableWithBloat{tableName='t', tableSizeInBytes=2, bloatSizeInBytes=1, bloatPercentage=50.0}");
3939
}
4040

4141
@SuppressWarnings("ConstantConditions")
@@ -52,7 +52,7 @@ void withInvalidArguments() {
5252
.hasMessage("bloatSizeInBytes cannot be less than zero");
5353
assertThatThrownBy(() -> TableWithBloat.of("t", 0L, 0L, -1))
5454
.isInstanceOf(IllegalArgumentException.class)
55-
.hasMessage("bloatPercentage cannot be less than zero");
55+
.hasMessage("bloatPercentage should be in the range from 0.0 to 100.0 inclusive");
5656
assertThatThrownBy(() -> TableWithBloat.of("t", -1L, 0L, 0))
5757
.isInstanceOf(IllegalArgumentException.class)
5858
.hasMessage("tableSizeInBytes cannot be less than zero");

pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/validation/ValidatorsTest.java

+11
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,15 @@ void validPercentTest() {
6565
.isInstanceOf(IllegalArgumentException.class)
6666
.hasMessage("remainingPercentageThreshold should be in the range from 0.0 to 100.0 inclusive");
6767
}
68+
69+
@Test
70+
void argumentNotNegative() {
71+
assertThatThrownBy(() -> Validators.argumentNotNegative(-1, "arg"))
72+
.isInstanceOf(IllegalArgumentException.class)
73+
.hasMessage("arg cannot be less than zero");
74+
assertThat(Validators.argumentNotNegative(0, "arg"))
75+
.isEqualTo(0);
76+
assertThat(Validators.argumentNotNegative(11, "arg"))
77+
.isEqualTo(11);
78+
}
6879
}

pg-index-health/src/main/java/io/github/mfvanek/pg/checks/cluster/SequenceOverflowCheckOnCluster.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* Check for sequence overflow on all hosts in the cluster.
2121
*
2222
* @author Blohny
23-
* @since 0.11.2
23+
* @since 0.12.0
2424
*/
2525
public class SequenceOverflowCheckOnCluster extends AbstractCheckOnCluster<SequenceState> {
2626

pg-index-health/src/main/java/io/github/mfvanek/pg/checks/host/IndexesWithBloatCheckOnHost.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public List<IndexWithBloat> check(@Nonnull final PgContext pgContext) {
4848
final String indexName = rs.getString(INDEX_NAME);
4949
final long indexSize = rs.getLong(INDEX_SIZE);
5050
final long bloatSize = rs.getLong(BLOAT_SIZE);
51-
final int bloatPercentage = rs.getInt(BLOAT_PERCENTAGE);
51+
final double bloatPercentage = rs.getDouble(BLOAT_PERCENTAGE);
5252
return IndexWithBloat.of(tableName, indexName, indexSize, bloatSize, bloatPercentage);
5353
});
5454
}

pg-index-health/src/main/java/io/github/mfvanek/pg/checks/host/SequenceOverflowCheckOnHost.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* Check for sequence overflow on a specific host.
2323
*
2424
* @author Blohny
25-
* @since 0.11.2
25+
* @since 0.12.0
2626
*/
2727
public class SequenceOverflowCheckOnHost extends AbstractCheckOnHost<SequenceState> {
2828

pg-index-health/src/main/java/io/github/mfvanek/pg/checks/host/TablesWithBloatCheckOnHost.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public List<TableWithBloat> check(@Nonnull final PgContext pgContext) {
4747
final String tableName = rs.getString(TABLE_NAME);
4848
final long tableSize = rs.getLong(TABLE_SIZE);
4949
final long bloatSize = rs.getLong(BLOAT_SIZE);
50-
final int bloatPercentage = rs.getInt(BLOAT_PERCENTAGE);
50+
final double bloatPercentage = rs.getDouble(BLOAT_PERCENTAGE);
5151
return TableWithBloat.of(tableName, tableSize, bloatSize, bloatPercentage);
5252
});
5353
}

pg-index-health/src/main/java/io/github/mfvanek/pg/checks/predicates/AbstractFilterByBloat.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
import io.github.mfvanek.pg.model.BloatAware;
1414
import io.github.mfvanek.pg.model.validation.Validators;
15-
import io.github.mfvanek.pg.validation.AdditionalValidators;
1615

1716
import javax.annotation.Nonnull;
1817

@@ -25,11 +24,11 @@
2524
abstract class AbstractFilterByBloat {
2625

2726
private final long sizeThresholdInBytes;
28-
private final int percentageThreshold;
27+
private final double percentageThreshold;
2928

30-
protected AbstractFilterByBloat(final long sizeThresholdInBytes, final int percentageThreshold) {
29+
protected AbstractFilterByBloat(final long sizeThresholdInBytes, final double percentageThreshold) {
3130
this.sizeThresholdInBytes = Validators.sizeNotNegative(sizeThresholdInBytes, "sizeThresholdInBytes");
32-
this.percentageThreshold = AdditionalValidators.validPercent(percentageThreshold, "percentageThreshold");
31+
this.percentageThreshold = Validators.validPercent(percentageThreshold, "percentageThreshold");
3332
}
3433

3534
protected boolean isOk(@Nonnull final BloatAware bloatAware) {

pg-index-health/src/main/java/io/github/mfvanek/pg/checks/predicates/FilterIndexesByBloatPredicate.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424
public class FilterIndexesByBloatPredicate extends AbstractFilterByBloat implements Predicate<IndexBloatAware> {
2525

26-
private FilterIndexesByBloatPredicate(final long sizeThresholdInBytes, final int percentageThreshold) {
26+
private FilterIndexesByBloatPredicate(final long sizeThresholdInBytes, final double percentageThreshold) {
2727
super(sizeThresholdInBytes, percentageThreshold);
2828
}
2929

@@ -33,7 +33,7 @@ public boolean test(@Nonnull final IndexBloatAware indexBloatAware) {
3333
}
3434

3535
@Nonnull
36-
public static Predicate<IndexBloatAware> of(final long sizeThresholdInBytes, final int percentageThreshold) {
36+
public static Predicate<IndexBloatAware> of(final long sizeThresholdInBytes, final double percentageThreshold) {
3737
return new FilterIndexesByBloatPredicate(sizeThresholdInBytes, percentageThreshold);
3838
}
3939
}

pg-index-health/src/main/java/io/github/mfvanek/pg/checks/predicates/FilterTablesByBloatPredicate.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424
public class FilterTablesByBloatPredicate extends AbstractFilterByBloat implements Predicate<TableBloatAware> {
2525

26-
private FilterTablesByBloatPredicate(final long sizeThresholdInBytes, final int percentageThreshold) {
26+
private FilterTablesByBloatPredicate(final long sizeThresholdInBytes, final double percentageThreshold) {
2727
super(sizeThresholdInBytes, percentageThreshold);
2828
}
2929

@@ -33,7 +33,7 @@ public boolean test(@Nonnull final TableBloatAware tableBloatAware) {
3333
}
3434

3535
@Nonnull
36-
public static Predicate<TableBloatAware> of(final long sizeThresholdInBytes, final int percentageThreshold) {
36+
public static Predicate<TableBloatAware> of(final long sizeThresholdInBytes, final double percentageThreshold) {
3737
return new FilterTablesByBloatPredicate(sizeThresholdInBytes, percentageThreshold);
3838
}
3939
}

0 commit comments

Comments
 (0)