@@ -574,8 +574,10 @@ _UnsafeRestQuery.prototype.checkSubqueryDepth = function () {
574574// $inQuery clause.
575575// The $inQuery clause turns into an $in with values that are just
576576// pointers to the objects returned in the subquery.
577- _UnsafeRestQuery . prototype . replaceInQuery = async function ( ) {
578- var inQueryObject = findObjectWithKey ( this . restWhere , '$inQuery' ) ;
577+ _UnsafeRestQuery . prototype . replaceInQuery = async function (
578+ search = findObjectsWithKey ( this . restWhere , '$inQuery' )
579+ ) {
580+ var inQueryObject = search . next ( ) . value ;
579581 if ( ! inQueryObject ) {
580582 return ;
581583 }
@@ -616,8 +618,8 @@ _UnsafeRestQuery.prototype.replaceInQuery = async function () {
616618 } ) ;
617619 return subquery . execute ( ) . then ( response => {
618620 transformInQuery ( inQueryObject , subquery . className , response . results ) ;
619- // Recurse to repeat
620- return this . replaceInQuery ( ) ;
621+ // Continue the search to repeat
622+ return this . replaceInQuery ( search ) ;
621623 } ) ;
622624} ;
623625
@@ -642,8 +644,10 @@ function transformNotInQuery(notInQueryObject, className, results) {
642644// $notInQuery clause.
643645// The $notInQuery clause turns into a $nin with values that are just
644646// pointers to the objects returned in the subquery.
645- _UnsafeRestQuery . prototype . replaceNotInQuery = async function ( ) {
646- var notInQueryObject = findObjectWithKey ( this . restWhere , '$notInQuery' ) ;
647+ _UnsafeRestQuery . prototype . replaceNotInQuery = async function (
648+ search = findObjectsWithKey ( this . restWhere , '$notInQuery' )
649+ ) {
650+ var notInQueryObject = search . next ( ) . value ;
647651 if ( ! notInQueryObject ) {
648652 return ;
649653 }
@@ -685,8 +689,8 @@ _UnsafeRestQuery.prototype.replaceNotInQuery = async function () {
685689
686690 return subquery . execute ( ) . then ( response => {
687691 transformNotInQuery ( notInQueryObject , subquery . className , response . results ) ;
688- // Recurse to repeat
689- return this . replaceNotInQuery ( ) ;
692+ // Continue the search to repeat
693+ return this . replaceNotInQuery ( search ) ;
690694 } ) ;
691695} ;
692696
@@ -716,8 +720,10 @@ const transformSelect = (selectObject, key, objects) => {
716720// The $select clause turns into an $in with values selected out of
717721// the subquery.
718722// Returns a possible-promise.
719- _UnsafeRestQuery . prototype . replaceSelect = async function ( ) {
720- var selectObject = findObjectWithKey ( this . restWhere , '$select' ) ;
723+ _UnsafeRestQuery . prototype . replaceSelect = async function (
724+ search = findObjectsWithKey ( this . restWhere , '$select' )
725+ ) {
726+ var selectObject = search . next ( ) . value ;
721727 if ( ! selectObject ) {
722728 return ;
723729 }
@@ -767,7 +773,7 @@ _UnsafeRestQuery.prototype.replaceSelect = async function () {
767773 return subquery . execute ( ) . then ( response => {
768774 transformSelect ( selectObject , selectValue . key , response . results ) ;
769775 // Keep replacing $select clauses
770- return this . replaceSelect ( ) ;
776+ return this . replaceSelect ( search ) ;
771777 } ) ;
772778} ;
773779
@@ -789,8 +795,10 @@ const transformDontSelect = (dontSelectObject, key, objects) => {
789795// The $dontSelect clause turns into an $nin with values selected out of
790796// the subquery.
791797// Returns a possible-promise.
792- _UnsafeRestQuery . prototype . replaceDontSelect = async function ( ) {
793- var dontSelectObject = findObjectWithKey ( this . restWhere , '$dontSelect' ) ;
798+ _UnsafeRestQuery . prototype . replaceDontSelect = async function (
799+ search = findObjectsWithKey ( this . restWhere , '$dontSelect' )
800+ ) {
801+ var dontSelectObject = search . next ( ) . value ;
794802 if ( ! dontSelectObject ) {
795803 return ;
796804 }
@@ -838,7 +846,7 @@ _UnsafeRestQuery.prototype.replaceDontSelect = async function () {
838846 return subquery . execute ( ) . then ( response => {
839847 transformDontSelect ( dontSelectObject , dontSelectValue . key , response . results ) ;
840848 // Keep replacing $dontSelect clauses
841- return this . replaceDontSelect ( ) ;
849+ return this . replaceDontSelect ( search ) ;
842850 } ) ;
843851} ;
844852
@@ -1387,31 +1395,47 @@ function replacePointers(object, path, replace) {
13871395 return answer ;
13881396}
13891397
1390- // Finds a subobject that has the given key, if there is one.
1391- // Returns undefined otherwise.
1392- function findObjectWithKey ( root , key ) {
1393- if ( typeof root !== 'object' ) {
1394- return ;
1395- }
1396- if ( Array . isArray ( root ) ) {
1397- for ( var item of root ) {
1398- const answer = findObjectWithKey ( item , key ) ;
1399- if ( answer ) {
1400- return answer ;
1398+ // Iterates over the subobjects that have the given key, depth first.
1399+ // A yielded object is searched after the caller has replaced the key.
1400+ function * findObjectsWithKey ( root , key ) {
1401+ const isObject = value => value !== null && typeof value === 'object' ;
1402+ const stack = [ root ] . filter ( isObject ) ;
1403+ // Ends the search of circular references
1404+ const searched = new WeakSet ( ) ;
1405+ while ( stack . length > 0 ) {
1406+ const node = stack . pop ( ) ;
1407+ const start = stack . length ;
1408+ if ( Array . isArray ( node ) ) {
1409+ for ( let i = 0 ; i < node . length ; i ++ ) {
1410+ const item = node [ i ] ;
1411+ if ( isObject ( item ) ) {
1412+ stack . push ( item ) ;
1413+ }
1414+ }
1415+ } else {
1416+ if ( node [ key ] ) {
1417+ yield node ;
1418+ }
1419+ for ( const subkey in node ) {
1420+ const value = node [ subkey ] ;
1421+ if ( isObject ( value ) ) {
1422+ stack . push ( value ) ;
1423+ }
14011424 }
14021425 }
1403- // Arrays are fully traversed above; returning here avoids re-walking the same
1404- // elements through the `for (subkey in root)` loop below, which would make this
1405- // function O(2^n) for nested arrays (e.g. deeply nested $or/$and/$nor).
1406- return ;
1407- }
1408- if ( root && root [ key ] ) {
1409- return root ;
1410- }
1411- for ( var subkey in root ) {
1412- const answer = findObjectWithKey ( root [ subkey ] , key ) ;
1413- if ( answer ) {
1414- return answer ;
1426+ if ( stack . length === start ) {
1427+ continue ;
1428+ }
1429+ if ( searched . has ( node ) ) {
1430+ stack . length = start ;
1431+ continue ;
1432+ }
1433+ searched . add ( node ) ;
1434+ // The stack returns the last subobject first
1435+ for ( let i = start , j = stack . length - 1 ; i < j ; i ++ , j -- ) {
1436+ const last = stack [ j ] ;
1437+ stack [ j ] = stack [ i ] ;
1438+ stack [ i ] = last ;
14151439 }
14161440 }
14171441}
0 commit comments