@@ -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
0 commit comments