-
-
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.
Add SkipSmallIndexesPredicate (#493)
* Add SkipSmallIndexesPredicate * Fix checkstyle * Fix test
- Loading branch information
Showing
9 changed files
with
145 additions
and
10 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
70 changes: 70 additions & 0 deletions
70
...-model/src/main/java/io/github/mfvanek/pg/model/predicates/SkipSmallIndexesPredicate.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,70 @@ | ||
/* | ||
* 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.index.IndexSizeAware; | ||
|
||
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 indexes based on a specified size threshold. | ||
* This class extends {@link AbstractFilterBySize} and evaluates {@link IndexSizeAware} | ||
* instances to determine if they meet or exceed the specified minimum index size. | ||
* | ||
* @author Ivan Vakhrushev | ||
* @see IndexSizeAware | ||
* @see DbObject | ||
* @see AbstractFilterBySize | ||
* @since 0.13.3 | ||
*/ | ||
@Immutable | ||
@ThreadSafe | ||
public final class SkipSmallIndexesPredicate extends AbstractFilterBySize { | ||
|
||
private SkipSmallIndexesPredicate(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 IndexSizeAware}, 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 IndexSizeAware}; {@code false} otherwise. | ||
*/ | ||
@Override | ||
public boolean test(@Nonnull final DbObject dbObject) { | ||
if (thresholdInBytes == 0L) { | ||
return true; | ||
} | ||
if (!(dbObject instanceof IndexSizeAware)) { | ||
return true; | ||
} | ||
final IndexSizeAware indexSizeAware = (IndexSizeAware) dbObject; | ||
return indexSizeAware.getIndexSizeInBytes() >= thresholdInBytes; | ||
} | ||
|
||
/** | ||
* Creates a {@code SkipSmallIndexesPredicate} with the specified size threshold. | ||
* | ||
* @param thresholdInBytes the minimum index size in bytes required for an index to pass the filter; must be non-negative. | ||
* @return a predicate that filters out indexes smaller than the specified size threshold. | ||
*/ | ||
@Nonnull | ||
public static Predicate<DbObject> of(final long thresholdInBytes) { | ||
return new SkipSmallIndexesPredicate(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
48 changes: 48 additions & 0 deletions
48
...el/src/test/java/io/github/mfvanek/pg/model/predicates/SkipSmallIndexesPredicateTest.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.index.IndexWithSize; | ||
import io.github.mfvanek.pg.model.sequence.SequenceState; | ||
import io.github.mfvanek.pg.model.table.Table; | ||
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 SkipSmallIndexesPredicateTest { | ||
|
||
@Test | ||
void shouldThrowExceptionWhenInvalidDataPassed() { | ||
assertThatThrownBy(() -> SkipSmallIndexesPredicate.of(-1L)) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessage("thresholdInBytes cannot be less than zero"); | ||
} | ||
|
||
@Test | ||
void shouldNotCastObjectsWhenThresholdIsZero() { | ||
final IndexWithSize mockIndex = Mockito.mock(IndexWithSize.class); | ||
assertThat(SkipSmallIndexesPredicate.of(0L)) | ||
.accepts(mockIndex); | ||
Mockito.verify(mockIndex, Mockito.never()).getIndexSizeInBytes(); | ||
} | ||
|
||
@Test | ||
void shouldWork() { | ||
assertThat(SkipSmallIndexesPredicate.of(10L)) | ||
.accepts(Table.of("t", 0L)) | ||
.accepts(IndexWithSize.of("t", "i", 10L)) | ||
.accepts(IndexWithSize.of("t", "i", 11L)) | ||
.accepts(SequenceState.of("s1", "int", 80.0)) | ||
.rejects(IndexWithSize.of("t2", "i2", 9L)); | ||
} | ||
} |
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