Skip to content

Commit 3dc12d6

Browse files
committed
fix: handle different Redis return types for num_docs field (#639)
Fix ClassCastException when retrieving document count from Redis index info. Redis can return num_docs as either String or Long depending on version/config. Applied robust type checking to all occurrences: - RedisJSONKeyValueAdapter.count() - RedisEnhancedKeyValueAdapter.count() - RedisFluentQueryByExample (already fixed in SearchStreamImpl) The fix handles: - String values: parse to Long - Number values: convert to long - Null/unexpected: return 0L safely This is more robust than PR #640's approach as it: - Handles multiple Redis return types - Avoids NullPointerException - Prevents unnecessary String conversions - Provides safe fallback for unexpected types
1 parent 2d734bf commit 3dc12d6

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

redis-om-spring/src/main/java/com/redis/om/spring/RedisEnhancedKeyValueAdapter.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,21 @@ public long count(String keyspace) {
392392
String indexName = indexer.getIndexName(keyspace);
393393
SearchOperations<String> search = modulesOperations.opsForSearch(indexName);
394394
var info = search.getInfo();
395-
return (long) info.get("num_docs");
395+
return extractNumDocs(info);
396+
}
397+
398+
private long extractNumDocs(Map<String, Object> info) {
399+
Object numDocsValue = info.get("num_docs");
400+
401+
// Handle different return types from Redis
402+
if (numDocsValue instanceof String) {
403+
return Long.parseLong((String) numDocsValue);
404+
} else if (numDocsValue instanceof Number) {
405+
return ((Number) numDocsValue).longValue();
406+
} else {
407+
// Fallback to 0 if the value is null or unexpected type
408+
return 0L;
409+
}
396410
}
397411

398412
/*

redis-om-spring/src/main/java/com/redis/om/spring/RedisJSONKeyValueAdapter.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.ArrayList;
88
import java.util.Collection;
99
import java.util.List;
10+
import java.util.Map;
1011
import java.util.Optional;
1112
import java.util.concurrent.TimeUnit;
1213

@@ -346,7 +347,21 @@ public long count(String keyspace) {
346347
String indexName = indexer.getIndexName(keyspace);
347348
SearchOperations<String> search = modulesOperations.opsForSearch(indexName);
348349
var info = search.getInfo();
349-
return (long) info.get("num_docs");
350+
return extractNumDocs(info);
351+
}
352+
353+
private long extractNumDocs(Map<String, Object> info) {
354+
Object numDocsValue = info.get("num_docs");
355+
356+
// Handle different return types from Redis
357+
if (numDocsValue instanceof String) {
358+
return Long.parseLong((String) numDocsValue);
359+
} else if (numDocsValue instanceof Number) {
360+
return ((Number) numDocsValue).longValue();
361+
} else {
362+
// Fallback to 0 if the value is null or unexpected type
363+
return 0L;
364+
}
350365
}
351366

352367
/*

redis-om-spring/src/main/java/com/redis/om/spring/search/stream/RedisFluentQueryByExample.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,17 @@ public Page<R> page(Pageable pageable) {
271271
count = searchResult.getTotalResults();
272272
} else {
273273
var info = searchOps.getInfo();
274-
count = (long) info.get("num_docs");
274+
Object numDocsValue = info.get("num_docs");
275+
276+
// Handle different return types from Redis
277+
if (numDocsValue instanceof String) {
278+
count = Long.parseLong((String) numDocsValue);
279+
} else if (numDocsValue instanceof Number) {
280+
count = ((Number) numDocsValue).longValue();
281+
} else {
282+
// Fallback to 0 if the value is null or unexpected type
283+
count = 0L;
284+
}
275285
}
276286

277287
var pageContents = searchStream.limit(pageable.getPageSize()).skip(pageable.getOffset()).collect(Collectors

0 commit comments

Comments
 (0)