-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature/postgresql-17-support
- Loading branch information
Showing
16 changed files
with
218 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...ealth-model/src/main/java/io/github/mfvanek/pg/model/predicates/AbstractFilterBySize.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright (c) 2019-2024. Ivan Vakhrushev and others. | ||
* https://github.com/mfvanek/pg-index-health | ||
* | ||
* This file is a part of "pg-index-health" - a Java library for | ||
* analyzing and maintaining indexes health in PostgreSQL databases. | ||
* | ||
* Licensed under the Apache License 2.0 | ||
*/ | ||
|
||
package io.github.mfvanek.pg.model.predicates; | ||
|
||
import io.github.mfvanek.pg.model.DbObject; | ||
import io.github.mfvanek.pg.model.validation.Validators; | ||
|
||
import java.util.function.Predicate; | ||
|
||
/** | ||
* An abstract base class for filtering {@link DbObject} instances based on a size threshold. | ||
* This class implements {@link Predicate} and serves as a foundation for classes that | ||
* filter database objects according to their size in bytes. | ||
* | ||
* <p>Subclasses must implement the {@code test} method to specify the filtering logic.</p> | ||
* | ||
* @author Ivan Vakhrushev | ||
* @see DbObject | ||
* @see Predicate | ||
* @since 0.13.3 | ||
*/ | ||
abstract class AbstractFilterBySize implements Predicate<DbObject> { | ||
|
||
/** | ||
* The size threshold in bytes for filtering {@link DbObject} instances. | ||
* Subclasses may use this threshold to include or exclude objects based on their size. | ||
*/ | ||
protected final long thresholdInBytes; | ||
|
||
/** | ||
* Constructs an {@code AbstractFilterBySize} with the specified size threshold. | ||
* | ||
* @param thresholdInBytes the minimum size in bytes for an object to pass the filter; | ||
* must be non-negative. | ||
* @throws IllegalArgumentException if {@code thresholdInBytes} is negative. | ||
*/ | ||
protected AbstractFilterBySize(final long thresholdInBytes) { | ||
this.thresholdInBytes = Validators.sizeNotNegative(thresholdInBytes, "thresholdInBytes"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...h-model/src/main/java/io/github/mfvanek/pg/model/predicates/SkipSmallTablesPredicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright (c) 2019-2024. Ivan Vakhrushev and others. | ||
* https://github.com/mfvanek/pg-index-health | ||
* | ||
* This file is a part of "pg-index-health" - a Java library for | ||
* analyzing and maintaining indexes health in PostgreSQL databases. | ||
* | ||
* Licensed under the Apache License 2.0 | ||
*/ | ||
|
||
package io.github.mfvanek.pg.model.predicates; | ||
|
||
import io.github.mfvanek.pg.model.DbObject; | ||
import io.github.mfvanek.pg.model.table.TableSizeAware; | ||
|
||
import java.util.function.Predicate; | ||
import javax.annotation.Nonnull; | ||
import javax.annotation.concurrent.Immutable; | ||
import javax.annotation.concurrent.ThreadSafe; | ||
|
||
/** | ||
* A predicate that filters out small tables based on a specified size threshold. | ||
* This class extends {@link AbstractFilterBySize} and evaluates {@link TableSizeAware} | ||
* instances to determine if they meet or exceed the specified minimum table size. | ||
* | ||
* @author Ivan Vakhrushev | ||
* @see TableSizeAware | ||
* @see DbObject | ||
* @see AbstractFilterBySize | ||
* @since 0.13.3 | ||
*/ | ||
@Immutable | ||
@ThreadSafe | ||
public final class SkipSmallTablesPredicate extends AbstractFilterBySize { | ||
|
||
private SkipSmallTablesPredicate(final long thresholdInBytes) { | ||
super(thresholdInBytes); | ||
} | ||
|
||
/** | ||
* Evaluates whether the given {@link DbObject} should pass the filter based on its size. | ||
* If the object is not an instance of {@link TableSizeAware}, or if the threshold is zero, | ||
* it automatically passes the filter. Otherwise, the object's size is compared to the threshold. | ||
* | ||
* @param dbObject the {@code DbObject} to be evaluated; must not be null. | ||
* @return {@code true} if the table size is greater than or equal to the threshold, or if the object is not {@code TableSizeAware}; {@code false} otherwise. | ||
*/ | ||
@Override | ||
public boolean test(@Nonnull final DbObject dbObject) { | ||
if (thresholdInBytes == 0L) { | ||
return true; | ||
} | ||
if (!(dbObject instanceof TableSizeAware)) { | ||
return true; | ||
} | ||
final TableSizeAware tableSizeAware = (TableSizeAware) dbObject; | ||
return tableSizeAware.getTableSizeInBytes() >= thresholdInBytes; | ||
} | ||
|
||
/** | ||
* Creates a {@code SkipSmallTablesPredicate} with the specified size threshold. | ||
* | ||
* @param thresholdInBytes the minimum table size in bytes required for a table to pass the filter; | ||
* must be non-negative. | ||
* @return a predicate that filters out tables smaller than the specified size threshold. | ||
*/ | ||
@Nonnull | ||
public static Predicate<DbObject> of(final long thresholdInBytes) { | ||
return new SkipSmallTablesPredicate(thresholdInBytes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...del/src/test/java/io/github/mfvanek/pg/model/predicates/SkipSmallTablesPredicateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright (c) 2019-2024. Ivan Vakhrushev and others. | ||
* https://github.com/mfvanek/pg-index-health | ||
* | ||
* This file is a part of "pg-index-health" - a Java library for | ||
* analyzing and maintaining indexes health in PostgreSQL databases. | ||
* | ||
* Licensed under the Apache License 2.0 | ||
*/ | ||
|
||
package io.github.mfvanek.pg.model.predicates; | ||
|
||
import io.github.mfvanek.pg.model.sequence.SequenceState; | ||
import io.github.mfvanek.pg.model.table.Table; | ||
import io.github.mfvanek.pg.model.table.TableWithBloat; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
class SkipSmallTablesPredicateTest { | ||
|
||
@Test | ||
void shouldThrowExceptionWhenInvalidDataPassed() { | ||
assertThatThrownBy(() -> SkipSmallTablesPredicate.of(-1L)) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessage("thresholdInBytes cannot be less than zero"); | ||
} | ||
|
||
@Test | ||
void shouldNotCastObjectsWhenThresholdIsZero() { | ||
final Table mockTable = Mockito.mock(Table.class); | ||
assertThat(SkipSmallTablesPredicate.of(0L)) | ||
.accepts(mockTable); | ||
Mockito.verify(mockTable, Mockito.never()).getTableSizeInBytes(); | ||
} | ||
|
||
@Test | ||
void shouldWork() { | ||
assertThat(SkipSmallTablesPredicate.of(10L)) | ||
.accepts(Table.of("t", 10L)) | ||
.accepts(Table.of("t", 11L)) | ||
.accepts(SequenceState.of("s1", "int", 80.0)) | ||
.rejects(TableWithBloat.of(Table.of("t2", 1L), 1L, 10.0)) | ||
.rejects(Table.of("t2", 1L)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.