Skip to content

Commit

Permalink
Merge branch 'main' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
anidotnet committed Mar 5, 2024
2 parents 55d9026 + 9584140 commit 8184449
Show file tree
Hide file tree
Showing 19 changed files with 891 additions and 150 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Issue Fixes

- Fix for #917
- Fix for #916
- Fix for #911
- Version upgrade for several dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public Value putIfAbsent(Key key, Value value) {

@Override
public RecordStream<Pair<Key, Value>> entries() {
return () -> new Iterator<Pair<Key, Value>>() {
return () -> new Iterator<>() {
final Iterator<Map.Entry<Key, Value>> entryIterator = mvMap.entrySet().iterator();

@Override
Expand All @@ -154,6 +154,16 @@ public RecordStream<Pair<Key, Value>> reversedEntries() {
return () -> new ReverseIterator<>(mvMap);
}

@Override
public Key firstKey() {
return mvMap.firstKey();
}

@Override
public Key lastKey() {
return mvMap.lastKey();
}

@Override
public Key higherKey(Key key) {
return mvMap.higherKey(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
import static org.dizitart.no2.filters.Filter.and;
import static org.dizitart.no2.filters.Filter.or;
import static org.dizitart.no2.filters.FluentFilter.where;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

/**
* @author Anindya Chatterjee.
Expand Down Expand Up @@ -420,4 +419,212 @@ public void testIssue45() {
cursor = collection.find(where("notes").text("lazy"));
assertEquals(cursor.size(), 3);
}

@Test
public void testSortByIndexDescendingLessThenEqual() {
NitriteCollection nitriteCollection = db.getCollection("testSortByIndexDescendingLessThenEqual");
List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);
integerList.forEach(i -> {
Document doc = Document.createDocument();
doc.put("name", i);
nitriteCollection.insert(doc);
});

DocumentCursor cursor = nitriteCollection.find(where("name").lte(3),
orderBy("name", SortOrder.Descending));

List<Document> docIter = cursor.toList();
Integer[] nonIndexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);

nitriteCollection.createIndex(IndexOptions.indexOptions(IndexType.UNIQUE), "name");

cursor = nitriteCollection.find(where("name").lte(3),
orderBy("name", SortOrder.Descending));
docIter = cursor.toList();
Integer[] indexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);

assertArrayEquals(nonIndexedResult, indexedResult);
}

@Test
public void testSortByIndexAscendingLessThenEqual() {
NitriteCollection nitriteCollection = db.getCollection("testSortByIndexAscendingLessThenEqual");
List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);
integerList.forEach(i -> {
Document doc = Document.createDocument();
doc.put("name", i);
nitriteCollection.insert(doc);
});

DocumentCursor cursor = nitriteCollection.find(where("name").lte(3),
orderBy("name", SortOrder.Ascending));

List<Document> docIter = cursor.toList();
Integer[] nonIndexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);

nitriteCollection.createIndex(IndexOptions.indexOptions(IndexType.UNIQUE), "name");

cursor = nitriteCollection.find(where("name").lte(3),
orderBy("name", SortOrder.Ascending));
docIter = cursor.toList();
Integer[] indexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);

assertArrayEquals(nonIndexedResult, indexedResult);
}

@Test
public void testSortByIndexDescendingGreaterThanEqual() {
NitriteCollection nitriteCollection = db.getCollection("testSortByIndexDescendingGreaterThanEqual");
List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);
integerList.forEach(i -> {
Document doc = Document.createDocument();
doc.put("name", i);
nitriteCollection.insert(doc);
});

DocumentCursor cursor = nitriteCollection.find(where("name").gte(3),
orderBy("name", SortOrder.Descending));

List<Document> docIter = cursor.toList();
Integer[] nonIndexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);

nitriteCollection.createIndex(IndexOptions.indexOptions(IndexType.UNIQUE), "name");

cursor = nitriteCollection.find(where("name").gte(3),
orderBy("name", SortOrder.Descending));
docIter = cursor.toList();
Integer[] indexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);

assertArrayEquals(nonIndexedResult, indexedResult);
}

@Test
public void testSortByIndexAscendingGreaterThanEqual() {
NitriteCollection nitriteCollection = db.getCollection("testSortByIndexAscendingGreaterThanEqual");
List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);
integerList.forEach(i -> {
Document doc = Document.createDocument();
doc.put("name", i);
nitriteCollection.insert(doc);
});

DocumentCursor cursor = nitriteCollection.find(where("name").gte(3),
orderBy("name", SortOrder.Ascending));

List<Document> docIter = cursor.toList();
Integer[] nonIndexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);

nitriteCollection.createIndex(IndexOptions.indexOptions(IndexType.UNIQUE), "name");

cursor = nitriteCollection.find(where("name").gte(3),
orderBy("name", SortOrder.Ascending));
docIter = cursor.toList();
Integer[] indexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);

assertArrayEquals(nonIndexedResult, indexedResult);
}

@Test
public void testSortByIndexDescendingGreaterThan() {
NitriteCollection nitriteCollection = db.getCollection("testSortByIndexDescendingGreaterThan");
List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);
integerList.forEach(i -> {
Document doc = Document.createDocument();
doc.put("name", i);
nitriteCollection.insert(doc);
});

DocumentCursor cursor = nitriteCollection.find(where("name").gt(3),
orderBy("name", SortOrder.Descending));

List<Document> docIter = cursor.toList();
Integer[] nonIndexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);

nitriteCollection.createIndex(IndexOptions.indexOptions(IndexType.UNIQUE), "name");

cursor = nitriteCollection.find(where("name").gt(3),
orderBy("name", SortOrder.Descending));
docIter = cursor.toList();
Integer[] indexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);

assertArrayEquals(nonIndexedResult, indexedResult);
}

@Test
public void testSortByIndexAscendingGreaterThan() {
NitriteCollection nitriteCollection = db.getCollection("testSortByIndexAscendingGreaterThan");
List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);
integerList.forEach(i -> {
Document doc = Document.createDocument();
doc.put("name", i);
nitriteCollection.insert(doc);
});

DocumentCursor cursor = nitriteCollection.find(where("name").gt(3),
orderBy("name", SortOrder.Ascending));

List<Document> docIter = cursor.toList();
Integer[] nonIndexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);

nitriteCollection.createIndex(IndexOptions.indexOptions(IndexType.UNIQUE), "name");

cursor = nitriteCollection.find(where("name").gt(3),
orderBy("name", SortOrder.Ascending));
docIter = cursor.toList();
Integer[] indexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);

assertArrayEquals(nonIndexedResult, indexedResult);
}

@Test
public void testSortByIndexDescendingLessThan() {
NitriteCollection nitriteCollection = db.getCollection("testSortByIndexDescendingLessThan");
List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);
integerList.forEach(i -> {
Document doc = Document.createDocument();
doc.put("name", i);
nitriteCollection.insert(doc);
});

DocumentCursor cursor = nitriteCollection.find(where("name").lt(3),
orderBy("name", SortOrder.Descending));

List<Document> docIter = cursor.toList();
Integer[] nonIndexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);

nitriteCollection.createIndex(IndexOptions.indexOptions(IndexType.UNIQUE), "name");

cursor = nitriteCollection.find(where("name").lt(3),
orderBy("name", SortOrder.Descending));
docIter = cursor.toList();
Integer[] indexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);

assertArrayEquals(nonIndexedResult, indexedResult);
}

@Test
public void testSortByIndexAscendingLessThan() {
NitriteCollection nitriteCollection = db.getCollection("testSortByIndexAscendingLessThan");
List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);
integerList.forEach(i -> {
Document doc = Document.createDocument();
doc.put("name", i);
nitriteCollection.insert(doc);
});

DocumentCursor cursor = nitriteCollection.find(where("name").lt(3),
orderBy("name", SortOrder.Ascending));

List<Document> docIter = cursor.toList();
Integer[] nonIndexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);

nitriteCollection.createIndex(IndexOptions.indexOptions(IndexType.UNIQUE), "name");

cursor = nitriteCollection.find(where("name").lt(3),
orderBy("name", SortOrder.Ascending));
docIter = cursor.toList();
Integer[] indexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);

assertArrayEquals(nonIndexedResult, indexedResult);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,32 @@ public RecordStream<Pair<K, V>> reversedEntries() {
objectFormatter, getKeyType(), getValueType(), true));
}

@Override
@SuppressWarnings({"unchecked"})
public K firstKey() {
try (RocksIterator iterator = rocksDB.newIterator(columnFamilyHandle)) {
iterator.seekToFirst();
if (iterator.isValid()) {
byte[] key = iterator.key();
return (K) objectFormatter.decodeKey(key, getKeyType());
}
}
return null;
}

@Override
@SuppressWarnings({"unchecked"})
public K lastKey() {
try (RocksIterator iterator = rocksDB.newIterator(columnFamilyHandle)) {
iterator.seekToLast();
if (iterator.isValid()) {
byte[] key = iterator.key();
return (K) objectFormatter.decodeKey(key, getKeyType());
}
}
return null;
}

@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public K higherKey(K k) {
Expand Down
Loading

0 comments on commit 8184449

Please sign in to comment.