@@ -170,7 +170,7 @@ public QueryContext(final Context context, final QueryContext parent, final Quer
170170 this .parent = parent ;
171171 this .info = info != null ? info : new QueryInfo (context );
172172 this .resources = parent != null ? parent .resources : new QueryResources (this );
173- this .ftPosData = parent != null ? parent .ftPosData : FTPosData . get ( context ) ;
173+ this .ftPosData = parent != null ? parent .ftPosData : null ;
174174 this .user = context .user ();
175175 }
176176
@@ -565,40 +565,38 @@ public String toString() {
565565 // CLASS METHODS ================================================================================
566566
567567 /**
568- * This function is called by the GUI.
569- * Caches the result of the specified query. If all nodes are of the same database
570- * instance, the returned value will be of type {@link DBNodes}.
568+ * Caches the result of the specified query.
569+ * This function is called by the GUI and must only be called after optimizing the query.
571570 * @param cmd query command
572- * @param max maximum number of items to cache (negative: return full result)
571+ * @param max maximum number of items to cache
573572 */
574573 void cache (final AQuery cmd , final int max ) {
575574 final ItemList items = new ItemList ();
576575 final IntList pres = new IntList ();
577576 final Data data = resources .globalData ();
577+ if (data != null ) ftPosData = new FTPosData (data );
578578 try {
579- optimize ();
580579 run (info .evaluating , () -> {
581580 // evaluates the query
582- final int mx = max >= 0 ? max : Integer .MAX_VALUE ;
583- final Iter iter = iter ();
581+ final Iter iter = updating ? update ().iter () : main .iter (this );
584582 Item item ;
585583
586584 do {
587585 // collect pre values if a database is opened globally
588586 if (defaultOutput && data != null ) {
589- while ((item = next (iter )) != null && item .data () == data && pres .size () < mx ) {
587+ while ((item = next (iter )) != null && item .data () == data && pres .size () < max ) {
590588 pres .add (((DBNode ) item ).pre ());
591589 }
592590 // skip if all results have been collected
593- if (item == null || pres .size () == mx ) break ;
591+ if (item == null || pres .size () >= max ) break ;
594592
595593 // otherwise, add nodes to standard iterator
596594 for (final int pre : pres .finish ()) items .add (new DBNode (data , pre ));
597595 items .add (item );
598596 }
599597
600598 // use standard iterator
601- while ((item = next (iter )) != null && items .size () < mx ) {
599+ while ((item = next (iter )) != null && items .size () < max ) {
602600 item .cache (false , null );
603601 items .add (item );
604602 }
0 commit comments