Skip to content

Commit

Permalink
feat: edge case, query with no conditions or sort
Browse files Browse the repository at this point in the history
Signed-off-by: sepgh <13250403+sepgh@users.noreply.github.com>
  • Loading branch information
sepgh committed Nov 13, 2024
1 parent 1d6ce0a commit e75ea0a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/main/java/com/github/sepgh/testudo/operation/query/Query.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.sepgh.testudo.operation.query;

import com.github.sepgh.testudo.operation.CollectionIndexProvider;
import com.github.sepgh.testudo.utils.IteratorUtils;
import lombok.SneakyThrows;

import java.util.Collections;
Expand Down Expand Up @@ -55,19 +56,30 @@ public Query limit(int limit) {
}

@SneakyThrows
@SuppressWarnings("unchecked")
public <V extends Number & Comparable<V>> Iterator<V> execute(CollectionIndexProvider collectionIndexProvider) {
// Initialize iterator based on conditions and sorting
Iterator<V> iterator = rootCondition.evaluate(
collectionIndexProvider,
Order.DEFAULT
);

if (sortField != null) {
@SuppressWarnings("unchecked")
Iterator<V> sortedValueIterator = (Iterator<V>) collectionIndexProvider.getQueryableIndex(sortField.field()).getSortedValueIterator(sortField.order());
iterator = new SortedIterator<>(
iterator,
sortedValueIterator
Iterator<V> iterator;

if (rootCondition != null) {
// Initialize iterator based on conditions and sorting
iterator = rootCondition.evaluate(
collectionIndexProvider,
Order.DEFAULT
);

if (sortField != null) {
Iterator<V> sortedValueIterator = (Iterator<V>) collectionIndexProvider.getQueryableIndex(sortField.field()).getSortedValueIterator(sortField.order());
iterator = new SortedIterator<>(
iterator,
sortedValueIterator
);
}
} else if (sortField != null) {
iterator = (Iterator<V>) collectionIndexProvider.getQueryableIndex(sortField.field()).getSortedValueIterator(sortField.order());
} else {
iterator = IteratorUtils.modifyNext(
collectionIndexProvider.getClusterIndexManager().getSortedIterator(Order.DEFAULT),
pointerKeyValue -> (V) pointerKeyValue.key()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public interface Queryable<K extends Comparable<K>, V> {
default Iterator<V> getSortedValueIterator(Order order) throws InternalOperationException {
return IteratorUtils.modifyNext(getSortedKeyValueIterator(order), KeyValue::value);
}
default Iterator<K> getSortedKeyIterator(Order order) throws InternalOperationException {
return IteratorUtils.modifyNext(getSortedKeyValueIterator(order), KeyValue::key);
}
Iterator<V> getGreaterThan(K k, Order order) throws InternalOperationException;
Iterator<V> getGreaterThanEqual(K k, Order order) throws InternalOperationException;
Iterator<V> getLessThan(K k, Order order) throws InternalOperationException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,23 @@ public void simpleCondition() throws IOException, ExecutionException, Interrupte
Assertions.assertEquals(UnsignedInteger.valueOf(1), executedResults.getFirst());
Assertions.assertEquals(UnsignedInteger.valueOf(2), executedResults.get(1));

// Sort only query
query = new Query().sort(new SortField("age", Order.ASC));
executedResults = Lists.newArrayList(query.execute(collectionIndexProvider));;
Assertions.assertEquals(3, executedResults.size());
Assertions.assertEquals(UnsignedInteger.valueOf(1), executedResults.getFirst());
Assertions.assertEquals(UnsignedInteger.valueOf(2), executedResults.get(1));
Assertions.assertEquals(UnsignedInteger.valueOf(3), executedResults.get(2));


// No condition no sort
query = new Query();
executedResults = Lists.newArrayList(query.execute(collectionIndexProvider));;
Assertions.assertEquals(3, executedResults.size());
Assertions.assertEquals(UnsignedInteger.valueOf(1), executedResults.getFirst());
Assertions.assertEquals(UnsignedInteger.valueOf(2), executedResults.get(1));
Assertions.assertEquals(UnsignedInteger.valueOf(3), executedResults.get(2));

}

@Test
Expand Down

0 comments on commit e75ea0a

Please sign in to comment.