Skip to content

Commit 2418a26

Browse files
anidotnetclaude
andcommitted
Flatten findSuitableStream and ofBoundedRange
PMD's NPathComplexity, reported by Codacy: findSuitableStream at 450 against a threshold of 200, ofBoundedRange at 228. findSuitableStream was a four-deep if/else doing three unrelated jobs. The by-id and indexed sources are their own methods now, the branch is a flat else-if chain, and the residual filter's condition says what it means - subPlans.isEmpty() && collectionScanFilter != null - instead of leaving it to nesting depth. ofBoundedRange's switch became isLowerBound/isUpperBound. No behaviour change; nitrite 1783 tests pass. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 25f4626 commit 2418a26

2 files changed

Lines changed: 67 additions & 60 deletions

File tree

‎nitrite/src/main/java/org/dizitart/no2/collection/operation/ReadOperations.java‎

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,42 @@ private static Object indexedValue(DBValue dbValue) {
206206
return dbValue == null || dbValue instanceof DBNull ? null : dbValue.getValue();
207207
}
208208

209+
/** The single row a by-id plan can match, or nothing when that id is not there. */
210+
private RecordStream<Pair<NitriteId, Document>> byIdStream(FindPlan findPlan) {
211+
Object idValue = findPlan.getByIdFilter().getValue();
212+
// the search term may be any numeric or String representation of an id,
213+
// e.g. a String for databases written before 4.4 (gh-1263)
214+
NitriteId nitriteId = idValue instanceof Long
215+
? NitriteId.createId((long) idValue)
216+
: NitriteId.createId(String.valueOf(idValue));
217+
// one lookup: a document removed between containsKey and get would be a null row
218+
Document document = nitriteMap.get(nitriteId);
219+
return document == null
220+
? RecordStream.empty()
221+
: RecordStream.single(pair(nitriteId, document));
222+
}
223+
224+
/**
225+
* The rows an index plan matches, lazily where the indexer offers a stream for this plan
226+
* shape, otherwise from the materialized id set, whose size is recorded so a {@code size()}
227+
* with no row-dropping step downstream can answer without fetching a document.
228+
*/
229+
private RecordStream<Pair<NitriteId, Document>> indexedStream(FindPlan findPlan, IndexScan scan) {
230+
IndexDescriptor indexDescriptor = findPlan.getIndexDescriptor();
231+
NitriteIndexer indexer = nitriteConfig.findIndexer(indexDescriptor.getIndexType());
232+
233+
RecordStream<NitriteId> idStream = indexer.findByFilterStream(findPlan, nitriteConfig);
234+
if (idStream != null) {
235+
// the index walks its matches lazily; a size() is counted from it on demand
236+
scan.lazyStream = new IndexedStream(idStream, nitriteMap);
237+
return scan.lazyStream;
238+
}
239+
240+
LinkedHashSet<NitriteId> nitriteIds = indexer.findByFilter(findPlan, nitriteConfig);
241+
scan.idCount = nitriteIds.size();
242+
return new IndexedStream(nitriteIds, nitriteMap);
243+
}
244+
209245
private RecordStream<Pair<NitriteId, Document>> findSuitableStream(FindPlan findPlan, IndexScan scan) {
210246
RecordStream<Pair<NitriteId, Document>> rawStream;
211247
RecordStream<Pair<NitriteId, Document>> indexSortedStream = null;
@@ -225,50 +261,18 @@ private RecordStream<Pair<NitriteId, Document>> findSuitableStream(FindPlan find
225261
// Always apply distinct stream for OR filters to avoid duplicates
226262
// when the same document matches multiple sub-plans (different indexes)
227263
rawStream = new DistinctStream(rawStream);
264+
} else if (findPlan.getByIdFilter() != null) {
265+
rawStream = byIdStream(findPlan);
266+
} else if (findPlan.getIndexDescriptor() != null) {
267+
rawStream = indexedStream(findPlan, scan);
228268
} else {
229-
// and or single filter
230-
if (findPlan.getByIdFilter() != null) {
231-
FieldBasedFilter byIdFilter = findPlan.getByIdFilter();
232-
Object idValue = byIdFilter.getValue();
233-
// the search term may be any numeric or String representation of an id,
234-
// e.g. a String for databases written before 4.4 (gh-1263)
235-
NitriteId nitriteId = idValue instanceof Long
236-
? NitriteId.createId((long) idValue)
237-
: NitriteId.createId(String.valueOf(idValue));
238-
// one lookup: a document removed between containsKey and get would be a null row
239-
Document document = nitriteMap.get(nitriteId);
240-
rawStream = document == null
241-
? RecordStream.empty()
242-
: RecordStream.single(pair(nitriteId, document));
243-
} else {
244-
IndexDescriptor indexDescriptor = findPlan.getIndexDescriptor();
245-
if (indexDescriptor != null) {
246-
// get optimized filter
247-
NitriteIndexer indexer = nitriteConfig.findIndexer(indexDescriptor.getIndexType());
248-
RecordStream<NitriteId> idStream = indexer.findByFilterStream(findPlan, nitriteConfig);
249-
if (idStream != null) {
250-
// the index walks its matches lazily; a size() is counted from it on demand
251-
scan.lazyStream = new IndexedStream(idStream, nitriteMap);
252-
rawStream = scan.lazyStream;
253-
} else {
254-
LinkedHashSet<NitriteId> nitriteIds = indexer.findByFilter(findPlan, nitriteConfig);
255-
256-
// the index supplied the exact matching id set; record its size so a size()
257-
// with no row-dropping step downstream can answer from it without fetching
258-
scan.idCount = nitriteIds.size();
259-
260-
// create indexed stream from optimized filter
261-
rawStream = new IndexedStream(nitriteIds, nitriteMap);
262-
}
263-
} else {
264-
indexSortedStream = indexSortedStream(findPlan);
265-
rawStream = indexSortedStream != null ? indexSortedStream : nitriteMap.entries();
266-
}
267-
}
269+
indexSortedStream = indexSortedStream(findPlan);
270+
rawStream = indexSortedStream != null ? indexSortedStream : nitriteMap.entries();
271+
}
268272

269-
if (findPlan.getCollectionScanFilter() != null) {
270-
rawStream = new FilteredStream(rawStream, findPlan.getCollectionScanFilter());
271-
}
273+
// an or-plan's branches carry their own filters; only a single plan has a residual one
274+
if (findPlan.getSubPlans().isEmpty() && findPlan.getCollectionScanFilter() != null) {
275+
rawStream = new FilteredStream(rawStream, findPlan.getCollectionScanFilter());
272276
}
273277

274278
// sort and bound stage

‎nitrite/src/main/java/org/dizitart/no2/index/SingleFieldIndex.java‎

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,14 @@ private static Range ofEquality(ComparableFilter filter) {
215215
return new Range(key, true, key, true);
216216
}
217217

218+
private static boolean isLowerBound(ComparisonMode mode) {
219+
return mode == ComparisonMode.GreaterEqual || mode == ComparisonMode.Greater;
220+
}
221+
222+
private static boolean isUpperBound(ComparisonMode mode) {
223+
return mode == ComparisonMode.LesserEqual || mode == ComparisonMode.Lesser;
224+
}
225+
218226
/** A two-sided range on one field, the same shape {@code IndexScanner.scanBoundedRange} takes. */
219227
private static Range ofBoundedRange(List<ComparableFilter> filters) {
220228
String field = filters.get(0).getField();
@@ -232,25 +240,20 @@ private static Range ofBoundedRange(List<ComparableFilter> filters) {
232240
}
233241
DBValue key = new DBValue((Comparable<?>) value);
234242
ComparisonMode mode = ((SortingAwareFilter) filter).getComparisonMode();
235-
switch (mode) {
236-
case GreaterEqual:
237-
case Greater:
238-
if (lower != null) {
239-
return null;
240-
}
241-
lower = key;
242-
lowerInclusive = mode == ComparisonMode.GreaterEqual;
243-
break;
244-
case LesserEqual:
245-
case Lesser:
246-
if (upper != null) {
247-
return null;
248-
}
249-
upper = key;
250-
upperInclusive = mode == ComparisonMode.LesserEqual;
251-
break;
252-
default:
243+
if (isLowerBound(mode)) {
244+
if (lower != null) {
253245
return null;
246+
}
247+
lower = key;
248+
lowerInclusive = mode == ComparisonMode.GreaterEqual;
249+
} else if (isUpperBound(mode)) {
250+
if (upper != null) {
251+
return null;
252+
}
253+
upper = key;
254+
upperInclusive = mode == ComparisonMode.LesserEqual;
255+
} else {
256+
return null;
254257
}
255258
}
256259
return lower == null || upper == null ? null : new Range(lower, lowerInclusive, upper, upperInclusive);

0 commit comments

Comments
 (0)