@@ -300,7 +300,7 @@ class _Bound<T> {
300300 {this .upperInclusive = true , this .lowerInclusive = true });
301301}
302302
303- class _GreaterEqualFilter extends ComparableFilter {
303+ class _GreaterEqualFilter extends SortingAwareFilter {
304304 _GreaterEqualFilter (super .field, super .value);
305305
306306 @override
@@ -320,21 +320,33 @@ class _GreaterEqualFilter extends ComparableFilter {
320320
321321 @override
322322 Stream <dynamic > applyOnIndex (IndexMap indexMap) async * {
323- var ceilingKey = await indexMap.ceilingKey (comparable);
324- while (ceilingKey != null ) {
325- // get the starting value, it can be a navigable-map (compound index)
326- // or list (single field index)
327- var val = await indexMap.get (ceilingKey);
328- yield * yieldValues (val);
329- ceilingKey = await indexMap.higherKey (ceilingKey);
323+ if (isReverseScan) {
324+ // if reverse scan is required, then start from the last key
325+ var lastKey = await indexMap.lastKey ();
326+ while (lastKey != null && compare (lastKey, comparable) >= 0 ) {
327+ // get the starting value, it can be a navigable-map (compound index)
328+ // or list (single field index)
329+ var val = await indexMap.get (lastKey);
330+ yield * yieldValues (val);
331+ lastKey = await indexMap.lowerKey (lastKey);
332+ }
333+ } else {
334+ var ceilingKey = await indexMap.ceilingKey (comparable);
335+ while (ceilingKey != null ) {
336+ // get the starting value, it can be a navigable-map (compound index)
337+ // or list (single field index)
338+ var val = await indexMap.get (ceilingKey);
339+ yield * yieldValues (val);
340+ ceilingKey = await indexMap.higherKey (ceilingKey);
341+ }
330342 }
331343 }
332344
333345 @override
334346 toString () => "($field >= $value )" ;
335347}
336348
337- class _GreaterThanFilter extends ComparableFilter {
349+ class _GreaterThanFilter extends SortingAwareFilter {
338350 _GreaterThanFilter (super .field, super .value);
339351
340352 @override
@@ -354,21 +366,32 @@ class _GreaterThanFilter extends ComparableFilter {
354366
355367 @override
356368 Stream <dynamic > applyOnIndex (IndexMap indexMap) async * {
357- var higherKey = await indexMap.higherKey (comparable);
358- while (higherKey != null ) {
359- // get the starting value, it can be a navigable-map (compound index)
360- // or list (single field index)
361- var val = await indexMap.get (higherKey);
362- yield * yieldValues (val);
363- higherKey = await indexMap.higherKey (higherKey);
369+ if (isReverseScan) {
370+ var lastKey = await indexMap.lastKey ();
371+ while (lastKey != null && compare (lastKey, comparable) > 0 ) {
372+ // get the starting value, it can be a navigable-map (compound index)
373+ // or list (single field index)
374+ var val = await indexMap.get (lastKey);
375+ yield * yieldValues (val);
376+ lastKey = await indexMap.lowerKey (lastKey);
377+ }
378+ } else {
379+ var higherKey = await indexMap.higherKey (comparable);
380+ while (higherKey != null ) {
381+ // get the starting value, it can be a navigable-map (compound index)
382+ // or list (single field index)
383+ var val = await indexMap.get (higherKey);
384+ yield * yieldValues (val);
385+ higherKey = await indexMap.higherKey (higherKey);
386+ }
364387 }
365388 }
366389
367390 @override
368391 toString () => "($field > $value )" ;
369392}
370393
371- class _LesserEqualFilter extends ComparableFilter {
394+ class _LesserEqualFilter extends SortingAwareFilter {
372395 _LesserEqualFilter (super .field, super .value);
373396
374397 @override
@@ -388,21 +411,32 @@ class _LesserEqualFilter extends ComparableFilter {
388411
389412 @override
390413 Stream <dynamic > applyOnIndex (IndexMap indexMap) async * {
391- var floorKey = await indexMap.floorKey (comparable);
392- while (floorKey != null ) {
393- // get the starting value, it can be a navigable-map (compound index)
394- // or list (single field index)
395- var val = await indexMap.get (floorKey);
396- yield * yieldValues (val);
397- floorKey = await indexMap.lowerKey (floorKey);
414+ if (isReverseScan) {
415+ var floorKey = await indexMap.floorKey (comparable);
416+ while (floorKey != null ) {
417+ // get the starting value, it can be a navigable-map (compound index)
418+ // or list (single field index)
419+ var val = await indexMap.get (floorKey);
420+ yield * yieldValues (val);
421+ floorKey = await indexMap.lowerKey (floorKey);
422+ }
423+ } else {
424+ var firstKey = await indexMap.firstKey ();
425+ while (firstKey != null && compare (firstKey, comparable) <= 0 ) {
426+ // get the starting value, it can be a navigable-map (compound index)
427+ // or list (single field index)
428+ var val = await indexMap.get (firstKey);
429+ yield * yieldValues (val);
430+ firstKey = await indexMap.higherKey (firstKey);
431+ }
398432 }
399433 }
400434
401435 @override
402436 toString () => "($field <= $value )" ;
403437}
404438
405- class _LesserThanFilter extends ComparableFilter {
439+ class _LesserThanFilter extends SortingAwareFilter {
406440 _LesserThanFilter (super .field, super .value);
407441
408442 @override
@@ -422,13 +456,24 @@ class _LesserThanFilter extends ComparableFilter {
422456
423457 @override
424458 Stream <dynamic > applyOnIndex (IndexMap indexMap) async * {
425- var lowerKey = await indexMap.lowerKey (comparable);
426- while (lowerKey != null ) {
427- // get the starting value, it can be a navigable-map (compound index)
428- // or list (single field index)
429- var val = await indexMap.get (lowerKey);
430- yield * yieldValues (val);
431- lowerKey = await indexMap.lowerKey (lowerKey);
459+ if (isReverseScan) {
460+ var lowerKey = await indexMap.lowerKey (comparable);
461+ while (lowerKey != null ) {
462+ // get the starting value, it can be a navigable-map (compound index)
463+ // or list (single field index)
464+ var val = await indexMap.get (lowerKey);
465+ yield * yieldValues (val);
466+ lowerKey = await indexMap.lowerKey (lowerKey);
467+ }
468+ } else {
469+ var firstKey = await indexMap.firstKey ();
470+ while (firstKey != null && compare (firstKey, comparable) < 0 ) {
471+ // get the starting value, it can be a navigable-map (compound index)
472+ // or list (single field index)
473+ var val = await indexMap.get (firstKey);
474+ yield * yieldValues (val);
475+ firstKey = await indexMap.higherKey (firstKey);
476+ }
432477 }
433478 }
434479
0 commit comments