Skip to content

Commit 32d7e02

Browse files
authored
Fixes #917 (#921)
1 parent 440f830 commit 32d7e02

File tree

18 files changed

+890
-149
lines changed

18 files changed

+890
-149
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### Issue Fixes
44

5+
- Fix for #917
56
- Fix for #916
67
- Fix for #911
78
- Version upgrade for several dependencies

nitrite-mvstore-adapter/src/main/java/org/dizitart/no2/mvstore/NitriteMVMap.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public Value putIfAbsent(Key key, Value value) {
133133

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

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

157+
@Override
158+
public Key firstKey() {
159+
return mvMap.firstKey();
160+
}
161+
162+
@Override
163+
public Key lastKey() {
164+
return mvMap.lastKey();
165+
}
166+
157167
@Override
158168
public Key higherKey(Key key) {
159169
return mvMap.higherKey(key);

nitrite-mvstore-adapter/src/test/java/org/dizitart/no2/integration/collection/CollectionFindBySingleFieldIndexTest.java

Lines changed: 209 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@
3838
import static org.dizitart.no2.filters.Filter.and;
3939
import static org.dizitart.no2.filters.Filter.or;
4040
import static org.dizitart.no2.filters.FluentFilter.where;
41-
import static org.junit.Assert.assertEquals;
42-
import static org.junit.Assert.assertTrue;
41+
import static org.junit.Assert.*;
4342

4443
/**
4544
* @author Anindya Chatterjee.
@@ -420,4 +419,212 @@ public void testIssue45() {
420419
cursor = collection.find(where("notes").text("lazy"));
421420
assertEquals(cursor.size(), 3);
422421
}
422+
423+
@Test
424+
public void testSortByIndexDescendingLessThenEqual() {
425+
NitriteCollection nitriteCollection = db.getCollection("testSortByIndexDescendingLessThenEqual");
426+
List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);
427+
integerList.forEach(i -> {
428+
Document doc = Document.createDocument();
429+
doc.put("name", i);
430+
nitriteCollection.insert(doc);
431+
});
432+
433+
DocumentCursor cursor = nitriteCollection.find(where("name").lte(3),
434+
orderBy("name", SortOrder.Descending));
435+
436+
List<Document> docIter = cursor.toList();
437+
Integer[] nonIndexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);
438+
439+
nitriteCollection.createIndex(IndexOptions.indexOptions(IndexType.UNIQUE), "name");
440+
441+
cursor = nitriteCollection.find(where("name").lte(3),
442+
orderBy("name", SortOrder.Descending));
443+
docIter = cursor.toList();
444+
Integer[] indexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);
445+
446+
assertArrayEquals(nonIndexedResult, indexedResult);
447+
}
448+
449+
@Test
450+
public void testSortByIndexAscendingLessThenEqual() {
451+
NitriteCollection nitriteCollection = db.getCollection("testSortByIndexAscendingLessThenEqual");
452+
List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);
453+
integerList.forEach(i -> {
454+
Document doc = Document.createDocument();
455+
doc.put("name", i);
456+
nitriteCollection.insert(doc);
457+
});
458+
459+
DocumentCursor cursor = nitriteCollection.find(where("name").lte(3),
460+
orderBy("name", SortOrder.Ascending));
461+
462+
List<Document> docIter = cursor.toList();
463+
Integer[] nonIndexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);
464+
465+
nitriteCollection.createIndex(IndexOptions.indexOptions(IndexType.UNIQUE), "name");
466+
467+
cursor = nitriteCollection.find(where("name").lte(3),
468+
orderBy("name", SortOrder.Ascending));
469+
docIter = cursor.toList();
470+
Integer[] indexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);
471+
472+
assertArrayEquals(nonIndexedResult, indexedResult);
473+
}
474+
475+
@Test
476+
public void testSortByIndexDescendingGreaterThanEqual() {
477+
NitriteCollection nitriteCollection = db.getCollection("testSortByIndexDescendingGreaterThanEqual");
478+
List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);
479+
integerList.forEach(i -> {
480+
Document doc = Document.createDocument();
481+
doc.put("name", i);
482+
nitriteCollection.insert(doc);
483+
});
484+
485+
DocumentCursor cursor = nitriteCollection.find(where("name").gte(3),
486+
orderBy("name", SortOrder.Descending));
487+
488+
List<Document> docIter = cursor.toList();
489+
Integer[] nonIndexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);
490+
491+
nitriteCollection.createIndex(IndexOptions.indexOptions(IndexType.UNIQUE), "name");
492+
493+
cursor = nitriteCollection.find(where("name").gte(3),
494+
orderBy("name", SortOrder.Descending));
495+
docIter = cursor.toList();
496+
Integer[] indexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);
497+
498+
assertArrayEquals(nonIndexedResult, indexedResult);
499+
}
500+
501+
@Test
502+
public void testSortByIndexAscendingGreaterThanEqual() {
503+
NitriteCollection nitriteCollection = db.getCollection("testSortByIndexAscendingGreaterThanEqual");
504+
List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);
505+
integerList.forEach(i -> {
506+
Document doc = Document.createDocument();
507+
doc.put("name", i);
508+
nitriteCollection.insert(doc);
509+
});
510+
511+
DocumentCursor cursor = nitriteCollection.find(where("name").gte(3),
512+
orderBy("name", SortOrder.Ascending));
513+
514+
List<Document> docIter = cursor.toList();
515+
Integer[] nonIndexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);
516+
517+
nitriteCollection.createIndex(IndexOptions.indexOptions(IndexType.UNIQUE), "name");
518+
519+
cursor = nitriteCollection.find(where("name").gte(3),
520+
orderBy("name", SortOrder.Ascending));
521+
docIter = cursor.toList();
522+
Integer[] indexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);
523+
524+
assertArrayEquals(nonIndexedResult, indexedResult);
525+
}
526+
527+
@Test
528+
public void testSortByIndexDescendingGreaterThan() {
529+
NitriteCollection nitriteCollection = db.getCollection("testSortByIndexDescendingGreaterThan");
530+
List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);
531+
integerList.forEach(i -> {
532+
Document doc = Document.createDocument();
533+
doc.put("name", i);
534+
nitriteCollection.insert(doc);
535+
});
536+
537+
DocumentCursor cursor = nitriteCollection.find(where("name").gt(3),
538+
orderBy("name", SortOrder.Descending));
539+
540+
List<Document> docIter = cursor.toList();
541+
Integer[] nonIndexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);
542+
543+
nitriteCollection.createIndex(IndexOptions.indexOptions(IndexType.UNIQUE), "name");
544+
545+
cursor = nitriteCollection.find(where("name").gt(3),
546+
orderBy("name", SortOrder.Descending));
547+
docIter = cursor.toList();
548+
Integer[] indexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);
549+
550+
assertArrayEquals(nonIndexedResult, indexedResult);
551+
}
552+
553+
@Test
554+
public void testSortByIndexAscendingGreaterThan() {
555+
NitriteCollection nitriteCollection = db.getCollection("testSortByIndexAscendingGreaterThan");
556+
List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);
557+
integerList.forEach(i -> {
558+
Document doc = Document.createDocument();
559+
doc.put("name", i);
560+
nitriteCollection.insert(doc);
561+
});
562+
563+
DocumentCursor cursor = nitriteCollection.find(where("name").gt(3),
564+
orderBy("name", SortOrder.Ascending));
565+
566+
List<Document> docIter = cursor.toList();
567+
Integer[] nonIndexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);
568+
569+
nitriteCollection.createIndex(IndexOptions.indexOptions(IndexType.UNIQUE), "name");
570+
571+
cursor = nitriteCollection.find(where("name").gt(3),
572+
orderBy("name", SortOrder.Ascending));
573+
docIter = cursor.toList();
574+
Integer[] indexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);
575+
576+
assertArrayEquals(nonIndexedResult, indexedResult);
577+
}
578+
579+
@Test
580+
public void testSortByIndexDescendingLessThan() {
581+
NitriteCollection nitriteCollection = db.getCollection("testSortByIndexDescendingLessThan");
582+
List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);
583+
integerList.forEach(i -> {
584+
Document doc = Document.createDocument();
585+
doc.put("name", i);
586+
nitriteCollection.insert(doc);
587+
});
588+
589+
DocumentCursor cursor = nitriteCollection.find(where("name").lt(3),
590+
orderBy("name", SortOrder.Descending));
591+
592+
List<Document> docIter = cursor.toList();
593+
Integer[] nonIndexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);
594+
595+
nitriteCollection.createIndex(IndexOptions.indexOptions(IndexType.UNIQUE), "name");
596+
597+
cursor = nitriteCollection.find(where("name").lt(3),
598+
orderBy("name", SortOrder.Descending));
599+
docIter = cursor.toList();
600+
Integer[] indexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);
601+
602+
assertArrayEquals(nonIndexedResult, indexedResult);
603+
}
604+
605+
@Test
606+
public void testSortByIndexAscendingLessThan() {
607+
NitriteCollection nitriteCollection = db.getCollection("testSortByIndexAscendingLessThan");
608+
List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);
609+
integerList.forEach(i -> {
610+
Document doc = Document.createDocument();
611+
doc.put("name", i);
612+
nitriteCollection.insert(doc);
613+
});
614+
615+
DocumentCursor cursor = nitriteCollection.find(where("name").lt(3),
616+
orderBy("name", SortOrder.Ascending));
617+
618+
List<Document> docIter = cursor.toList();
619+
Integer[] nonIndexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);
620+
621+
nitriteCollection.createIndex(IndexOptions.indexOptions(IndexType.UNIQUE), "name");
622+
623+
cursor = nitriteCollection.find(where("name").lt(3),
624+
orderBy("name", SortOrder.Ascending));
625+
docIter = cursor.toList();
626+
Integer[] indexedResult = docIter.stream().map(d -> d.get("name", Integer.class)).toArray(Integer[]::new);
627+
628+
assertArrayEquals(nonIndexedResult, indexedResult);
629+
}
423630
}

nitrite-rocksdb-adapter/src/main/java/org/dizitart/no2/rocksdb/RocksDBMap.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,32 @@ public RecordStream<Pair<K, V>> reversedEntries() {
224224
objectFormatter, getKeyType(), getValueType(), true));
225225
}
226226

227+
@Override
228+
@SuppressWarnings({"unchecked"})
229+
public K firstKey() {
230+
try (RocksIterator iterator = rocksDB.newIterator(columnFamilyHandle)) {
231+
iterator.seekToFirst();
232+
if (iterator.isValid()) {
233+
byte[] key = iterator.key();
234+
return (K) objectFormatter.decodeKey(key, getKeyType());
235+
}
236+
}
237+
return null;
238+
}
239+
240+
@Override
241+
@SuppressWarnings({"unchecked"})
242+
public K lastKey() {
243+
try (RocksIterator iterator = rocksDB.newIterator(columnFamilyHandle)) {
244+
iterator.seekToLast();
245+
if (iterator.isValid()) {
246+
byte[] key = iterator.key();
247+
return (K) objectFormatter.decodeKey(key, getKeyType());
248+
}
249+
}
250+
return null;
251+
}
252+
227253
@Override
228254
@SuppressWarnings({"unchecked", "rawtypes"})
229255
public K higherKey(K k) {

0 commit comments

Comments
 (0)