From 300b1a1b849aa20f905c3ce1e362bd115df892d4 Mon Sep 17 00:00:00 2001 From: Viktor Lofgren Date: Thu, 15 Feb 2024 12:03:30 +0100 Subject: [PATCH] (index-query) Add some tests for the QueryFilter code --- .../query/filter/QueryFilterLetThrough.java | 17 +++-- .../index/query/filter/QueryFilterStepIf.java | 8 --- .../query/filter/QueryFilterStepIfTest.java | 67 +++++++++++++++++++ .../array/buffer/LongQueryBuffer.java | 10 +++ 4 files changed, 85 insertions(+), 17 deletions(-) create mode 100644 code/features-index/index-query/src/test/java/nu/marginalia/index/query/filter/QueryFilterStepIfTest.java diff --git a/code/features-index/index-query/src/main/java/nu/marginalia/index/query/filter/QueryFilterLetThrough.java b/code/features-index/index-query/src/main/java/nu/marginalia/index/query/filter/QueryFilterLetThrough.java index 5e849d79..ed02dd6d 100644 --- a/code/features-index/index-query/src/main/java/nu/marginalia/index/query/filter/QueryFilterLetThrough.java +++ b/code/features-index/index-query/src/main/java/nu/marginalia/index/query/filter/QueryFilterLetThrough.java @@ -1,25 +1,24 @@ package nu.marginalia.index.query.filter; +import nu.marginalia.array.buffer.LongQueryBuffer; + public class QueryFilterLetThrough implements QueryFilterStepIf { - static final QueryFilterStepIf instance = new QueryFilterLetThrough(); @Override public boolean test(long value) { return true; } + @Override + public void apply(LongQueryBuffer buffer) { + buffer.retainAll(); + buffer.finalizeFiltering(); + } + public double cost() { return 0.; } - public int retainDestructive(long[] items, int max) { - return 0; - } - - public int retainReorder(long[] items, int start, int max) { - return 0; - } - public String describe() { return "[PassThrough]"; } diff --git a/code/features-index/index-query/src/main/java/nu/marginalia/index/query/filter/QueryFilterStepIf.java b/code/features-index/index-query/src/main/java/nu/marginalia/index/query/filter/QueryFilterStepIf.java index e3692538..a13615ce 100644 --- a/code/features-index/index-query/src/main/java/nu/marginalia/index/query/filter/QueryFilterStepIf.java +++ b/code/features-index/index-query/src/main/java/nu/marginalia/index/query/filter/QueryFilterStepIf.java @@ -36,13 +36,5 @@ public interface QueryFilterStepIf extends Comparable { buffer.finalizeFiltering(); } - static QueryFilterStepIf noPass() { - return QueryFilterNoPass.instance; - } - static QueryFilterStepIf anyOf(List steps) { - return new QueryFilterAnyOf(steps); - } - - } diff --git a/code/features-index/index-query/src/test/java/nu/marginalia/index/query/filter/QueryFilterStepIfTest.java b/code/features-index/index-query/src/test/java/nu/marginalia/index/query/filter/QueryFilterStepIfTest.java new file mode 100644 index 00000000..a7450b11 --- /dev/null +++ b/code/features-index/index-query/src/test/java/nu/marginalia/index/query/filter/QueryFilterStepIfTest.java @@ -0,0 +1,67 @@ +package nu.marginalia.index.query.filter; + +import nu.marginalia.array.buffer.LongQueryBuffer; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class QueryFilterStepIfTest { + + private LongQueryBuffer createBuffer(long... data) { + return new LongQueryBuffer(data, data.length); + } + + @Test + public void testPassThrough() { + var buffer = createBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + var filter = new QueryFilterLetThrough(); + filter.apply(buffer); + assertArrayEquals(new long[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, buffer.copyData()); + } + + @Test + public void testNoPass() { + var buffer = createBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + var filter = new QueryFilterNoPass(); + filter.apply(buffer); + assertArrayEquals(new long[]{}, buffer.copyData()); + } + + @Test + public void testIncludePredicate() { + var buffer = createBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + var filter = new QueryFilterStepFromPredicate(value -> value % 2 == 0); + filter.apply(buffer); + assertArrayEquals(new long[]{2, 4, 6, 8, 10}, buffer.copyData()); + } + + @Test + public void testExcludePredicate() { + var buffer = createBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + var filter = new QueryFilterStepExcludeFromPredicate(value -> value % 2 == 1); + filter.apply(buffer); + assertArrayEquals(new long[]{2, 4, 6, 8, 10}, buffer.copyData()); + } + + @Test + public void testSuccessiveApplication() { + var buffer = createBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + var filter1 = new QueryFilterStepFromPredicate(value -> value % 2 == 0); + var filter2 = new QueryFilterStepExcludeFromPredicate(value -> value <= 6); + filter1.apply(buffer); + filter2.apply(buffer); + assertArrayEquals(new long[]{8, 10}, buffer.copyData()); + } + + @Test + public void testCombinedApplication() { + var buffer = createBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + var filter1 = new QueryFilterStepFromPredicate(value -> value % 3 == 0); + var filter2 = new QueryFilterStepFromPredicate(value -> value % 5 == 0); + var filter = new QueryFilterAnyOf(List.of(filter1, filter2)); + filter.apply(buffer); + assertArrayEquals(new long[]{3, 5, 6, 9, 10}, buffer.copyData()); + } +} \ No newline at end of file diff --git a/code/libraries/array/src/main/java/nu/marginalia/array/buffer/LongQueryBuffer.java b/code/libraries/array/src/main/java/nu/marginalia/array/buffer/LongQueryBuffer.java index 68de658f..390325ee 100644 --- a/code/libraries/array/src/main/java/nu/marginalia/array/buffer/LongQueryBuffer.java +++ b/code/libraries/array/src/main/java/nu/marginalia/array/buffer/LongQueryBuffer.java @@ -89,6 +89,15 @@ public class LongQueryBuffer { return ++read < end; } + /** Retains all values in the buffer, and updates + * the read and write pointer to the end pointer, + * as though all values were retained. + */ + public void retainAll() { + write = end; + read = end; + } + public boolean hasMore() { return read < end; } @@ -147,4 +156,5 @@ public class LongQueryBuffer { ",data = [" + Arrays.toString(Arrays.copyOf(data, end)) + "]]"; } + }