Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,10 @@ public void testIssue178() {
assertEquals(cursor.size(), 1);

cursor = collection.find(where("field1").eq(5));
assertEquals(cursor.size(), 1);
assertEquals(cursor.size(), 2);

cursor = collection.find(where("field1").eq(5.0));
assertEquals(cursor.size(), 1);
assertEquals(cursor.size(), 2);

collection.createIndex("field1", "field2");
cursor = collection.find(and(where("field1").eq(0.03),
Expand All @@ -257,10 +257,10 @@ public void testIssue178() {
assertEquals(cursor.size(), 1);

cursor = collection.find(where("field1").eq(5));
assertEquals(cursor.size(), 1);
assertEquals(cursor.size(), 2);

cursor = collection.find(where("field1").eq(5.0));
assertEquals(cursor.size(), 1);
assertEquals(cursor.size(), 2);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,12 @@ public void testIssue178() {
collection.insert(doc1, doc2, doc3, doc4, doc5);

DocumentCursor cursor = collection.find(where("field").eq(5));
assertEquals(cursor.size(), 1);
assertEquals(cursor.size(), 2);

collection.createIndex(indexOptions(IndexType.NON_UNIQUE), "field");

cursor = collection.find(where("field").eq(5));
assertEquals(cursor.size(), 1);
assertEquals(cursor.size(), 2);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,12 @@ public void testIssue178() {
collection.insert(doc1, doc2, doc3, doc4, doc5);

DocumentCursor cursor = collection.find(where("field").eq(5));
assertEquals(cursor.size(), 1);
assertEquals(cursor.size(), 2);

collection.createIndex(indexOptions(IndexType.NON_UNIQUE), "field");

cursor = collection.find(where("field").eq(5));
assertEquals(cursor.size(), 1);
assertEquals(cursor.size(), 2);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,10 @@ public void testIssue178() {
assertEquals(cursor.size(), 1);

cursor = collection.find(where("field1").eq(5));
assertEquals(cursor.size(), 1);
assertEquals(cursor.size(), 2);

cursor = collection.find(where("field1").eq(5.0));
assertEquals(cursor.size(), 1);
assertEquals(cursor.size(), 2);

collection.createIndex("field1", "field2");
cursor = collection.find(and(where("field1").eq(0.03),
Expand All @@ -257,10 +257,10 @@ public void testIssue178() {
assertEquals(cursor.size(), 1);

cursor = collection.find(where("field1").eq(5));
assertEquals(cursor.size(), 1);
assertEquals(cursor.size(), 2);

cursor = collection.find(where("field1").eq(5.0));
assertEquals(cursor.size(), 1);
assertEquals(cursor.size(), 2);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,12 @@ public void testIssue178() {
collection.insert(doc1, doc2, doc3, doc4, doc5);

DocumentCursor cursor = collection.find(where("field").eq(5));
assertEquals(cursor.size(), 1);
assertEquals(cursor.size(), 2);

collection.createIndex(indexOptions(IndexType.NON_UNIQUE), "field");

cursor = collection.find(where("field").eq(5));
assertEquals(cursor.size(), 1);
assertEquals(cursor.size(), 2);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,12 @@ public void testIssue178() {
collection.insert(doc1, doc2, doc3, doc4, doc5);

DocumentCursor cursor = collection.find(where("field").eq(5));
assertEquals(cursor.size(), 1);
assertEquals(cursor.size(), 2);

collection.createIndex(indexOptions(IndexType.NON_UNIQUE), "field");

cursor = collection.find(where("field").eq(5));
assertEquals(cursor.size(), 1);
assertEquals(cursor.size(), 2);
}

@Test
Expand Down
14 changes: 13 additions & 1 deletion nitrite/src/main/java/org/dizitart/no2/common/DBValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ public class DBValue implements Comparable<DBValue>, Serializable {
@Setter(AccessLevel.PRIVATE)
private Comparable<?> value;

private DBValue() {
}

public DBValue(Comparable<?> value) {
this.value = value;
this.value = normalizeNumber(value);
}

@Override
Expand All @@ -55,6 +58,15 @@ public int compareTo(DBValue o) {
return Comparables.compare(value, o.value);
}

private static Comparable<?> normalizeNumber(Comparable<?> value) {
// Normalize all numeric types to Double for consistent serialization
// This ensures Integer(5) and Double(5.0) are treated the same in indexes
if (value instanceof Number && !(value instanceof Double)) {
return ((Number) value).doubleValue();
}
return value;
}

private void writeObject(ObjectOutputStream stream) throws IOException {
stream.writeObject(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ public static int compare(Comparable first, Comparable second) {
if (first instanceof Number && second instanceof Number) {
Number number1 = (Number) first;
Number number2 = (Number) second;
int result = Numbers.compare(number1, number2);
if (!first.getClass().equals(second.getClass())) {
if (result == 0) {
return first.toString().compareTo(second.toString()) < 0 ? -1 : 1;
}
}
return result;
return Numbers.compare(number1, number2);
}

return first.compareTo(second);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,6 @@ public static boolean deepEquals(Object o1, Object o2) {
}

if (o1 instanceof Number && o2 instanceof Number) {
if (o1.getClass() != o2.getClass()) {
return false;
}
// cast to Number and take care of boxing and compare
return Numbers.compare((Number) o1, (Number) o2) == 0;
} else if (o1 instanceof Iterable && o2 instanceof Iterable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import org.dizitart.no2.collection.Document;
import org.dizitart.no2.collection.NitriteId;
import org.dizitart.no2.common.DBNull;
import org.dizitart.no2.common.DBValue;
import org.dizitart.no2.common.tuples.Pair;
import org.dizitart.no2.index.IndexMap;

Expand All @@ -43,7 +45,9 @@ public boolean apply(Pair<NitriteId, Document> element) {

@Override
public List<?> applyOnIndex(IndexMap indexMap) {
Object value = indexMap.get((Comparable<?>) getValue());
Object fieldValue = getValue();
DBValue dbValue = fieldValue == null ? DBNull.getInstance() : new DBValue((Comparable<?>) fieldValue);
Object value = indexMap.get(dbValue);
if (value == null) {
return new ArrayList<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.dizitart.no2.collection.NitriteId;
import org.dizitart.no2.common.DBValue;
import org.dizitart.no2.common.mapper.NitriteMapper;
import org.dizitart.no2.exceptions.ValidationException;

Expand Down Expand Up @@ -102,7 +103,7 @@ protected void validateSearchTerm(NitriteMapper nitriteMapper, String field, Obj
*/
@SuppressWarnings("unchecked")
protected void processIndexValue(Object value,
List<NavigableMap<Comparable<?>, Object>> subMap,
List<NavigableMap<DBValue, Object>> subMap,
List<NitriteId> nitriteIds) {
if (value instanceof List) {
// if it is list then add it directly to nitrite ids
Expand All @@ -111,7 +112,7 @@ protected void processIndexValue(Object value,
}

if (value instanceof NavigableMap) {
subMap.add((NavigableMap<Comparable<?>, Object>) value);
subMap.add((NavigableMap<DBValue, Object>) value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import org.dizitart.no2.collection.Document;
import org.dizitart.no2.collection.NitriteId;
import org.dizitart.no2.common.DBNull;
import org.dizitart.no2.common.DBValue;
import org.dizitart.no2.common.tuples.Pair;
import org.dizitart.no2.common.util.Comparables;
import org.dizitart.no2.exceptions.FilterException;
Expand Down Expand Up @@ -58,27 +60,28 @@ public boolean apply(Pair<NitriteId, Document> element) {
}

@Override
@SuppressWarnings({"unchecked", "rawtypes"})
@SuppressWarnings({"rawtypes"})
public List<?> applyOnIndex(IndexMap indexMap) {
Comparable comparable = getComparable();
List<NavigableMap<Comparable<?>, Object>> subMaps = new ArrayList<>();
DBValue dbValue = comparable == null ? DBNull.getInstance() : new DBValue(comparable);
List<NavigableMap<DBValue, Object>> subMaps = new ArrayList<>();

// maintain the find sorting order
List<NitriteId> nitriteIds = new ArrayList<>();

if (isReverseScan()) {
// if reverse scan is required, then start from the last key
Comparable lastKey = indexMap.lastKey();
while(lastKey != null && Comparables.compare(lastKey, comparable) >= 0) {
DBValue lastKey = indexMap.lastKey();
while(lastKey != DBNull.getInstance() && Comparables.compare(lastKey, dbValue) >= 0) {
// get the starting value, it can be a navigable-map (compound index)
// or list (single field index)
Object value = indexMap.get(lastKey);
processIndexValue(value, subMaps, nitriteIds);
lastKey = indexMap.lowerKey(lastKey);
}
} else {
Comparable ceilingKey = indexMap.ceilingKey(comparable);
while (ceilingKey != null) {
DBValue ceilingKey = indexMap.ceilingKey(dbValue);
while (ceilingKey != DBNull.getInstance()) {
// get the starting value, it can be a navigable-map (compound index)
// or list (single field index)
Object value = indexMap.get(ceilingKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import org.dizitart.no2.collection.Document;
import org.dizitart.no2.collection.NitriteId;
import org.dizitart.no2.common.DBNull;
import org.dizitart.no2.common.DBValue;
import org.dizitart.no2.common.tuples.Pair;
import org.dizitart.no2.common.util.Comparables;
import org.dizitart.no2.exceptions.FilterException;
Expand Down Expand Up @@ -59,24 +61,25 @@ public boolean apply(Pair<NitriteId, Document> element) {
}

@Override
@SuppressWarnings({"unchecked", "rawtypes"})
@SuppressWarnings({"rawtypes"})
public List<?> applyOnIndex(IndexMap indexMap) {
Comparable comparable = getComparable();
List<NavigableMap<Comparable<?>, Object>> subMaps = new ArrayList<>();
DBValue dbValue = comparable == null ? DBNull.getInstance() : new DBValue(comparable);
List<NavigableMap<DBValue, Object>> subMaps = new ArrayList<>();
List<NitriteId> nitriteIds = new ArrayList<>();

if (isReverseScan()) {
Comparable lastKey = indexMap.lastKey();
while (lastKey != null && Comparables.compare(lastKey, comparable) > 0) {
DBValue lastKey = indexMap.lastKey();
while (lastKey != DBNull.getInstance() && Comparables.compare(lastKey, dbValue) > 0) {
// get the starting value, it can be a navigable-map (compound index)
// or list (single field index)
Object value = indexMap.get(lastKey);
processIndexValue(value, subMaps, nitriteIds);
lastKey = indexMap.lowerKey(lastKey);
}
} else {
Comparable higherKey = indexMap.higherKey(comparable);
while (higherKey != null) {
DBValue higherKey = indexMap.higherKey(dbValue);
while (higherKey != DBNull.getInstance()) {
// get the starting value, it can be a navigable-map (compound index)
// or list (single field index)
Object value = indexMap.get(higherKey);
Expand Down
13 changes: 10 additions & 3 deletions nitrite/src/main/java/org/dizitart/no2/filters/InFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
import lombok.Getter;
import org.dizitart.no2.collection.Document;
import org.dizitart.no2.collection.NitriteId;
import org.dizitart.no2.common.DBNull;
import org.dizitart.no2.common.DBValue;
import org.dizitart.no2.common.tuples.Pair;
import org.dizitart.no2.index.IndexMap;

import java.util.*;
import java.util.stream.Collectors;

/**
* @author Anindya Chatterjee
Expand Down Expand Up @@ -50,11 +53,15 @@ public boolean apply(Pair<NitriteId, Document> element) {
}

public List<?> applyOnIndex(IndexMap indexMap) {
List<NavigableMap<Comparable<?>, Object>> subMap = new ArrayList<>();
// convert comparable set to DBValue set
Set<DBValue> dbValueSet = comparableSet.stream().map(value -> value == null ? DBNull.getInstance()
: new DBValue(value)).collect(Collectors.toSet());

List<NavigableMap<DBValue, Object>> subMap = new ArrayList<>();
List<NitriteId> nitriteIds = new ArrayList<>();

for (Pair<Comparable<?>, ?> entry : indexMap.entries()) {
if (comparableSet.contains(entry.getFirst())) {
for (Pair<DBValue, ?> entry : indexMap.entries()) {
if (dbValueSet.contains(entry.getFirst())) {
processIndexValue(entry.getSecond(), subMap, nitriteIds);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import org.dizitart.no2.collection.Document;
import org.dizitart.no2.collection.NitriteId;
import org.dizitart.no2.common.DBNull;
import org.dizitart.no2.common.DBValue;
import org.dizitart.no2.common.tuples.Pair;
import org.dizitart.no2.common.util.Comparables;
import org.dizitart.no2.exceptions.FilterException;
Expand Down Expand Up @@ -58,24 +60,25 @@ public boolean apply(Pair<NitriteId, Document> element) {
}

@Override
@SuppressWarnings({"unchecked", "rawtypes"})
@SuppressWarnings({"rawtypes"})
public List<?> applyOnIndex(IndexMap indexMap) {
Comparable comparable = getComparable();
List<NavigableMap<Comparable<?>, Object>> subMap = new ArrayList<>();
DBValue dbValue = comparable == null ? DBNull.getInstance() : new DBValue(comparable);
List<NavigableMap<DBValue, Object>> subMap = new ArrayList<>();
List<NitriteId> nitriteIds = new ArrayList<>();

if (isReverseScan()) {
Comparable floorKey = indexMap.floorKey(comparable);
while (floorKey != null) {
DBValue floorKey = indexMap.floorKey(dbValue);
while (floorKey != DBNull.getInstance()) {
// get the starting value, it can be a navigable-map (compound index)
// or list (single field index)
Object value = indexMap.get(floorKey);
processIndexValue(value, subMap, nitriteIds);
floorKey = indexMap.lowerKey(floorKey);
}
} else {
Comparable firstKey = indexMap.firstKey();
while (firstKey != null && Comparables.compare(firstKey, comparable) <= 0) {
DBValue firstKey = indexMap.firstKey();
while (firstKey != DBNull.getInstance() && Comparables.compare(firstKey, dbValue) <= 0) {
// get the starting value, it can be a navigable-map (compound index)
// or list (single field index)
Object value = indexMap.get(firstKey);
Expand Down
Loading