Skip to content

Commit

Permalink
test: desc and asc LT, LTE, GT, GTE operations on Indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
sepgh committed Oct 31, 2024
1 parent 1c2b8ae commit 49c6797
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 32 deletions.
14 changes: 0 additions & 14 deletions src/main/java/com/github/sepgh/testudo/index/QueryableIndex.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ public LockableIterator<KeyValue<K, V>> getSortedIterator(Order order) throws In
IndexIOSession<K> indexIOSession = this.indexIOSessionFactory.create(indexStorageManager, indexId, nodeFactory, kvSize);

Iterator<KeyValue<K,V>> iterator = switch (order) {
case DESC -> BPlusTreeUtils.getDescendingIterator(indexIOSession, this, degree);
case ASC -> BPlusTreeUtils.getAscendingIterator(indexIOSession, this, degree);
case DESC -> BPlusTreeUtils.getDescendingIterator(indexIOSession, getRoot(indexIOSession), degree);
case ASC -> BPlusTreeUtils.getAscendingIterator(indexIOSession, getRoot(indexIOSession), degree);
};

return new LockableIterator<>() {
Expand Down Expand Up @@ -322,14 +322,15 @@ private QueryIterator(Order order, Operation operation, K identifier) throws Int
}

private void init() throws InternalOperationException {
leafTreeNode = BPlusTreeUtils.getResponsibleNode(indexStorageManager, getRoot(indexIOSession), identifier, indexId, degree, nodeFactory);
keyValueList = leafTreeNode.getKeyValueList(degree);


if (order == Order.DESC) {

// DESC - LT || LTE
if (operation == Operation.LT || operation == Operation.LTE) {

leafTreeNode = BPlusTreeUtils.getResponsibleNode(indexStorageManager, getRoot(indexIOSession), identifier, indexId, degree, nodeFactory);
keyValueList = leafTreeNode.getKeyValueList(degree);

if (operation == Operation.LT && keyValueList.getFirst().key().compareTo(identifier) >= 0 && leafTreeNode.getPreviousSiblingPointer(degree).isPresent()){
leafTreeNode = (AbstractLeafTreeNode<K, V>) indexIOSession.read(leafTreeNode.getPreviousSiblingPointer(degree).get());
keyValueList = leafTreeNode.getKeyValueList(degree);
Expand All @@ -346,6 +347,9 @@ private void init() throws InternalOperationException {
}
} else {

leafTreeNode = BPlusTreeUtils.getFarRightLeaf(indexIOSession, getRoot(indexIOSession));
keyValueList = leafTreeNode.getKeyValueList(degree);

// DESC - GT || GTE
// Todo: binary search?
for (int i = keyValueList.size() - 1; i >= 0; i--) {
Expand All @@ -361,6 +365,10 @@ private void init() throws InternalOperationException {

// ASC - GT || GTE
if (operation == Operation.GTE || operation == Operation.GT) {

leafTreeNode = BPlusTreeUtils.getResponsibleNode(indexStorageManager, getRoot(indexIOSession), identifier, indexId, degree, nodeFactory);
keyValueList = leafTreeNode.getKeyValueList(degree);

if (operation == Operation.GT && keyValueList.getLast().key().compareTo(identifier) <= 0 && leafTreeNode.getNextSiblingPointer(degree).isPresent()){
leafTreeNode = (AbstractLeafTreeNode<K, V>) indexIOSession.read(leafTreeNode.getNextSiblingPointer(degree).get());
keyValueList = leafTreeNode.getKeyValueList(degree);
Expand All @@ -376,6 +384,10 @@ private void init() throws InternalOperationException {
}
}
} else {

leafTreeNode = BPlusTreeUtils.getFarLeftLeaf(indexIOSession, getRoot(indexIOSession));
keyValueList = leafTreeNode.getKeyValueList(degree);

for (int i = 0; i < keyValueList.size(); i++) {
if (
operationFunctionMap.get(operation).apply(keyValueList.get(i).key())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ public static <K extends Comparable<K>, V> AbstractLeafTreeNode<K, V> getRespons

}

public static <K extends Comparable<K>, V> AbstractLeafTreeNode<K, V> getFarLeftLeaf(IndexIOSession<K> indexIOSession, BPlusTreeUniqueTreeIndexManager<K, V> node) throws InternalOperationException {
AbstractTreeNode<K> root = node.getRoot(indexIOSession);
public static <K extends Comparable<K>, V> AbstractLeafTreeNode<K, V> getFarLeftLeaf(IndexIOSession<K> indexIOSession, AbstractTreeNode<K> root) throws InternalOperationException {

if (root.isLeaf())
return (AbstractLeafTreeNode<K, V>) root;
Expand All @@ -124,9 +123,7 @@ public static <K extends Comparable<K>, V> AbstractLeafTreeNode<K, V> getFarLeft
return (AbstractLeafTreeNode<K, V>) farLeftChild;
}

public static <K extends Comparable<K>, V> AbstractLeafTreeNode<K, V> getFarRightLeaf(IndexIOSession<K> indexIOSession, BPlusTreeUniqueTreeIndexManager<K, V> node) throws InternalOperationException {
AbstractTreeNode<K> root = node.getRoot(indexIOSession);

public static <K extends Comparable<K>, V> AbstractLeafTreeNode<K, V> getFarRightLeaf(IndexIOSession<K> indexIOSession, AbstractTreeNode<K> root) throws InternalOperationException {
if (root.isLeaf())
return (AbstractLeafTreeNode<K, V>) root;

Expand All @@ -139,11 +136,11 @@ public static <K extends Comparable<K>, V> AbstractLeafTreeNode<K, V> getFarRigh
return (AbstractLeafTreeNode<K, V>) farRightChild;
}

public static <K extends Comparable<K>, V> Iterator<KeyValue<K, V>> getAscendingIterator(IndexIOSession<K> indexIOSession, BPlusTreeUniqueTreeIndexManager<K, V> node, int degree) throws InternalOperationException {
public static <K extends Comparable<K>, V> Iterator<KeyValue<K, V>> getAscendingIterator(IndexIOSession<K> indexIOSession, AbstractTreeNode<K> root, int degree) throws InternalOperationException {
return new Iterator<KeyValue<K, V>>() {

private int keyIndex = 0;
AbstractLeafTreeNode<K, V> currentLeaf = getFarLeftLeaf(indexIOSession, node);
AbstractLeafTreeNode<K, V> currentLeaf = getFarLeftLeaf(indexIOSession, root);

@Override
public boolean hasNext() {
Expand Down Expand Up @@ -171,10 +168,10 @@ public KeyValue<K, V> next() {
};
}

public static <K extends Comparable<K>, V> Iterator<KeyValue<K, V>> getDescendingIterator(IndexIOSession<K> indexIOSession, BPlusTreeUniqueTreeIndexManager<K, V> node, int degree) throws InternalOperationException {
public static <K extends Comparable<K>, V> Iterator<KeyValue<K, V>> getDescendingIterator(IndexIOSession<K> indexIOSession, AbstractTreeNode<K> root, int degree) throws InternalOperationException {
return new Iterator<KeyValue<K, V>>() {

private AbstractLeafTreeNode<K, V> currentLeaf = getFarRightLeaf(indexIOSession, node);
private AbstractLeafTreeNode<K, V> currentLeaf = getFarRightLeaf(indexIOSession, root);
private int keyIndex = currentLeaf.getKeyList(degree).size() - 1;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.github.sepgh.test.TestParams;
import com.github.sepgh.test.utils.FileUtils;
import com.github.sepgh.testudo.context.EngineConfig;
import com.github.sepgh.testudo.index.AscendingBinaryListIterator;
import com.github.sepgh.testudo.index.BinaryList;
import com.github.sepgh.testudo.index.data.IndexBinaryObjectFactory;
import com.github.sepgh.testudo.operation.query.Order;
Expand All @@ -19,7 +18,7 @@
import java.nio.file.Path;
import java.util.ListIterator;

public class AscendingBinaryListIteratorTestCase {
public class BinaryListIteratorTestCase {
private Path dbPath;
private EngineConfig engineConfig;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,29 @@ private void runLargerThanTest(DuplicateQueryableIndex<Integer, Integer> duplica
}
}
Assertions.assertFalse(greaterThan.hasNext());


greaterThan = duplicateQueryableIndex.getGreaterThan(1, Order.DESC);
for (int i = 3; i > 1; i--) {
for (int j = 2; j >= 0; j--) {
int expectedNext = (int) (i * Math.pow(10, j));
Assertions.assertTrue(greaterThan.hasNext(), "Expected to have next for " + expectedNext);
Assertions.assertEquals(expectedNext, greaterThan.next());
}
}
Assertions.assertFalse(greaterThan.hasNext());


greaterThanEQ = duplicateQueryableIndex.getGreaterThanEqual(1, Order.DESC);
for (int i = 3; i >= 1; i--) {
for (int j = 2; j >= 0; j--) {
int expectedNext = (int) (i * Math.pow(10, j));
Assertions.assertTrue(greaterThanEQ.hasNext(), "Expected to have next for " + expectedNext);
Assertions.assertEquals(expectedNext, greaterThanEQ.next());
}
}
Assertions.assertFalse(greaterThan.hasNext());

}

private void runLessThanTest(DuplicateQueryableIndex<Integer, Integer> duplicateQueryableIndex) throws InternalOperationException, IOException, ExecutionException, InterruptedException {
Expand Down Expand Up @@ -190,6 +213,29 @@ private void runLessThanTest(DuplicateQueryableIndex<Integer, Integer> duplicate
}
}
Assertions.assertFalse(lessThanEQ.hasNext());



lessThan = duplicateQueryableIndex.getLessThan(3, Order.ASC);
for (int i = 1; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int expectedNext = (int) (i * Math.pow(10, j));
Assertions.assertTrue(lessThan.hasNext(), "Expected to have next for " + expectedNext);
Assertions.assertEquals(expectedNext, lessThan.next());
}
}
Assertions.assertFalse(lessThan.hasNext());


lessThanEQ = duplicateQueryableIndex.getLessThanEqual(3, Order.ASC);
for (int i = 1; i <= 3; i++) {
for (int j = 0; j < 3; j++) {
int expectedNext = (int) (i * Math.pow(10, j));
Assertions.assertTrue(lessThanEQ.hasNext(), "Expected to have next for " + expectedNext);
Assertions.assertEquals(expectedNext, lessThanEQ.next());
}
}
Assertions.assertFalse(lessThanEQ.hasNext());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private OrganizedFileIndexStorageManager getCompactFileIndexStorageManager() thr

@Test
@Timeout(value = 2)
public void testGreaterThan() throws IOException, ExecutionException, InterruptedException, IndexExistsException, InternalOperationException {
public void testGreaterThan_ASC() throws IOException, ExecutionException, InterruptedException, IndexExistsException, InternalOperationException {
OrganizedFileIndexStorageManager organizedFileIndexStorageManager = getCompactFileIndexStorageManager();

UniqueQueryableIndex<Long, Long> uniqueQueryableIndex = new BPlusTreeUniqueTreeIndexManager<>(1, degree, organizedFileIndexStorageManager, LONG_INDEX_BINARY_OBJECT_FACTORY.get(), LONG_INDEX_BINARY_OBJECT_FACTORY.get());
Expand Down Expand Up @@ -127,7 +127,67 @@ public void testGreaterThan() throws IOException, ExecutionException, Interrupte

@Test
@Timeout(value = 2)
public void testLessThan() throws IOException, ExecutionException, InterruptedException, IndexExistsException, InternalOperationException {
public void testGreaterThan_DESC() throws IOException, ExecutionException, InterruptedException, IndexExistsException, InternalOperationException {
OrganizedFileIndexStorageManager organizedFileIndexStorageManager = getCompactFileIndexStorageManager();

UniqueQueryableIndex<Long, Long> uniqueQueryableIndex = new BPlusTreeUniqueTreeIndexManager<>(1, degree, organizedFileIndexStorageManager, LONG_INDEX_BINARY_OBJECT_FACTORY.get(), LONG_INDEX_BINARY_OBJECT_FACTORY.get());
uniqueQueryableIndex.addIndex(1L, 10L);
uniqueQueryableIndex.addIndex(2L, 20L);
uniqueQueryableIndex.addIndex(3L, 30L);
uniqueQueryableIndex.addIndex(4L, 40L);

// Test Larger than 1
Iterator<Long> largerThanIterator = uniqueQueryableIndex.getGreaterThan(1L, Order.DESC);
Assertions.assertTrue(largerThanIterator.hasNext());
Assertions.assertEquals(40L, largerThanIterator.next());
Assertions.assertTrue(largerThanIterator.hasNext());
Assertions.assertEquals(30L, largerThanIterator.next());
Assertions.assertTrue(largerThanIterator.hasNext());
Assertions.assertEquals(20L, largerThanIterator.next());
Assertions.assertFalse(largerThanIterator.hasNext());

// Test Larger than 2
largerThanIterator = uniqueQueryableIndex.getGreaterThan(2L, Order.DESC);
Assertions.assertTrue(largerThanIterator.hasNext());
Assertions.assertEquals(40L, largerThanIterator.next());
Assertions.assertTrue(largerThanIterator.hasNext());
Assertions.assertEquals(30L, largerThanIterator.next());
Assertions.assertFalse(largerThanIterator.hasNext());


// Test Larger than 10
largerThanIterator = uniqueQueryableIndex.getGreaterThan(10L, Order.DESC);
Assertions.assertFalse(largerThanIterator.hasNext());


// Test larger than equal 1
Iterator<Long> largerThanEQIterator = uniqueQueryableIndex.getGreaterThanEqual(1L, Order.DESC);
Assertions.assertTrue(largerThanEQIterator.hasNext());
Assertions.assertEquals(40L, largerThanEQIterator.next());
Assertions.assertTrue(largerThanEQIterator.hasNext());
Assertions.assertEquals(30L, largerThanEQIterator.next());
Assertions.assertTrue(largerThanEQIterator.hasNext());
Assertions.assertEquals(20L, largerThanEQIterator.next());
Assertions.assertTrue(largerThanEQIterator.hasNext());
Assertions.assertEquals(10L, largerThanEQIterator.next());
Assertions.assertFalse(largerThanEQIterator.hasNext());

// Test lte 3
largerThanEQIterator = uniqueQueryableIndex.getGreaterThanEqual(3L, Order.DESC);
Assertions.assertTrue(largerThanEQIterator.hasNext());
Assertions.assertEquals(40L, largerThanEQIterator.next());
Assertions.assertTrue(largerThanEQIterator.hasNext());
Assertions.assertEquals(30L, largerThanEQIterator.next());
Assertions.assertFalse(largerThanEQIterator.hasNext());

// Test lte 10
largerThanEQIterator = uniqueQueryableIndex.getGreaterThanEqual(10L, Order.DESC);
Assertions.assertFalse(largerThanEQIterator.hasNext());
}

@Test
@Timeout(value = 2)
public void testLessThan_DESC() throws IOException, ExecutionException, InterruptedException, IndexExistsException, InternalOperationException {
OrganizedFileIndexStorageManager organizedFileIndexStorageManager = getCompactFileIndexStorageManager();

UniqueQueryableIndex<Long, Long> uniqueQueryableIndex = new BPlusTreeUniqueTreeIndexManager<>(1, degree, organizedFileIndexStorageManager, LONG_INDEX_BINARY_OBJECT_FACTORY.get(), LONG_INDEX_BINARY_OBJECT_FACTORY.get());
Expand Down Expand Up @@ -185,4 +245,64 @@ public void testLessThan() throws IOException, ExecutionException, InterruptedEx
Assertions.assertFalse(largerThanEQIterator.hasNext());
}

@Test
@Timeout(value = 2)
public void testLessThan_ASC() throws IOException, ExecutionException, InterruptedException, IndexExistsException, InternalOperationException {
OrganizedFileIndexStorageManager organizedFileIndexStorageManager = getCompactFileIndexStorageManager();

UniqueQueryableIndex<Long, Long> uniqueQueryableIndex = new BPlusTreeUniqueTreeIndexManager<>(1, degree, organizedFileIndexStorageManager, LONG_INDEX_BINARY_OBJECT_FACTORY.get(), LONG_INDEX_BINARY_OBJECT_FACTORY.get());
uniqueQueryableIndex.addIndex(1L, 10L);
uniqueQueryableIndex.addIndex(2L, 20L);
uniqueQueryableIndex.addIndex(3L, 30L);
uniqueQueryableIndex.addIndex(4L, 40L);

// Test Larger than 1
Iterator<Long> largerThanIterator = uniqueQueryableIndex.getLessThan(4L, Order.ASC);
Assertions.assertTrue(largerThanIterator.hasNext());
Assertions.assertEquals(10L, largerThanIterator.next());
Assertions.assertTrue(largerThanIterator.hasNext());
Assertions.assertEquals(20L, largerThanIterator.next());
Assertions.assertTrue(largerThanIterator.hasNext());
Assertions.assertEquals(30L, largerThanIterator.next());
Assertions.assertFalse(largerThanIterator.hasNext());

// Test Larger than 2
largerThanIterator = uniqueQueryableIndex.getLessThan(3L, Order.ASC);
Assertions.assertTrue(largerThanIterator.hasNext());
Assertions.assertEquals(10L, largerThanIterator.next());
Assertions.assertTrue(largerThanIterator.hasNext());
Assertions.assertEquals(20L, largerThanIterator.next());
Assertions.assertFalse(largerThanIterator.hasNext());


// Test Larger than 10
largerThanIterator = uniqueQueryableIndex.getLessThan(1L, Order.ASC);
Assertions.assertFalse(largerThanIterator.hasNext());


// Test larger than equal 1
Iterator<Long> largerThanEQIterator = uniqueQueryableIndex.getLessThanEqual(4L, Order.ASC);
Assertions.assertTrue(largerThanEQIterator.hasNext());
Assertions.assertEquals(10L, largerThanEQIterator.next());
Assertions.assertTrue(largerThanEQIterator.hasNext());
Assertions.assertEquals(20L, largerThanEQIterator.next());
Assertions.assertTrue(largerThanEQIterator.hasNext());
Assertions.assertEquals(30L, largerThanEQIterator.next());
Assertions.assertTrue(largerThanEQIterator.hasNext());
Assertions.assertEquals(40L, largerThanEQIterator.next());
Assertions.assertFalse(largerThanEQIterator.hasNext());

// Test lte 3
largerThanEQIterator = uniqueQueryableIndex.getLessThanEqual(2L, Order.ASC);
Assertions.assertTrue(largerThanEQIterator.hasNext());
Assertions.assertEquals(10L, largerThanEQIterator.next());
Assertions.assertTrue(largerThanEQIterator.hasNext());
Assertions.assertEquals(20L, largerThanEQIterator.next());
Assertions.assertFalse(largerThanEQIterator.hasNext());

// Test lte 0
largerThanEQIterator = uniqueQueryableIndex.getLessThanEqual(0L, Order.DESC);
Assertions.assertFalse(largerThanEQIterator.hasNext());
}

}

0 comments on commit 49c6797

Please sign in to comment.