View Javadoc
1   /**
2    * This file Copyright (c) 2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.admincentral.findbar.search;
35  
36  import info.magnolia.periscope.Periscope;
37  import info.magnolia.periscope.SupplierAwareSearchResult;
38  import info.magnolia.periscope.search.SearchQuery;
39  import info.magnolia.periscope.search.SearchResultSupplier;
40  
41  import java.util.ArrayList;
42  import java.util.Collections;
43  import java.util.HashMap;
44  import java.util.LinkedList;
45  import java.util.List;
46  import java.util.Map;
47  import java.util.Queue;
48  import java.util.function.BiConsumer;
49  import java.util.function.BiFunction;
50  
51  import lombok.extern.slf4j.Slf4j;
52  
53  /**
54   * Accumulator of (sync and async) search results.
55   */
56  @Slf4j
57  public class ResultCollector {
58  
59      private static final int MAX_TOTAL_RESULTS = 100;
60  
61      private final BiFunction<SearchQuery, List<SearchResultSupplier>,
62              List<Periscope.ResultsPromise>> searchEngine;
63      private final BiConsumer<SearchQuery, List<SupplierAwareSearchResult>> onChange;
64      private SearchQuery currentQuery;
65  
66      public ResultCollector(BiFunction<SearchQuery, List<SearchResultSupplier>, List<Periscope.ResultsPromise>> searchEngine,
67                             BiConsumer<SearchQuery, List<SupplierAwareSearchResult>> onChange) {
68          this.searchEngine = searchEngine;
69          this.onChange = onChange;
70      }
71  
72      public void search(SearchQuery searchQuery, List<SearchResultSupplier> suppliers, Runnable onComplete) {
73          this.currentQuery = searchQuery;
74  
75          List<SupplierAwareSearchResult> allResults = Collections.synchronizedList(new ArrayList<>());
76  
77          /*
78           * Helpers to make sure we list results in the correct order w.r.t. their suppliers.
79           * E.g. if order is ["apps", "pages", "tours"], and we receive "pages" results first, we'll hold those back
80           * (in retainedResults) until after we have "apps" results in place.
81           */
82          Queue<SearchResultSupplier> missingSuppliers = new LinkedList<>(suppliers);
83          Map<SearchResultSupplier, List<SupplierAwareSearchResult>> retainedResults = new HashMap<>();
84  
85          List<Periscope.ResultsPromise> completableFutures = searchEngine.apply(searchQuery, suppliers);
86  
87          final Runnable triggerCallbacks = () -> {
88              // don't trigger callback with an empty interim list
89              if (allResults.isEmpty() && !missingSuppliers.isEmpty()) {
90                  return;
91              }
92  
93              try {
94                  List<SupplierAwareSearchResult> subList = allResults.subList(0, Math.min(MAX_TOTAL_RESULTS, allResults.size()));
95                  onChange.accept(searchQuery, new ArrayList<>(subList));
96              } catch (Exception e) {
97                  log.error("Updating result failed", e);
98              }
99  
100             if (missingSuppliers.isEmpty()) {
101                 onComplete.run();
102             }
103         };
104 
105         for (Periscope.ResultsPromise promise : completableFutures) {
106             promise.getResultsFuture().whenCompleteAsync((results, exception) -> {
107                 synchronized (this) {
108                     // if another search was already triggered in the meantime, ignore old results still dribbling in,
109                     // same if max number of results is already reached
110                     if (searchQuery != this.currentQuery || allResults.size() >= MAX_TOTAL_RESULTS) {
111                         return;
112                     }
113 
114                     if (exception != null) {
115                         log.error("An error occurred during the search process, therefore an empty collection will be returned.", exception);
116                         results = Collections.emptyList();
117                     }
118 
119                     if (promise.getSupplier() == null) {
120                         log.error("Results promise has no supplier - result listing might break because of this");
121                         throw new IllegalStateException("Results promise has no supplier");
122                     }
123 
124                     if (promise.getSupplier().equals(missingSuppliers.peek())) {
125                         allResults.addAll(results);
126                         missingSuppliers.poll();
127                     } else {
128                         retainedResults.put(promise.getSupplier(), results);
129                     }
130 
131                     // flush retained results
132                     while (retainedResults.containsKey(missingSuppliers.peek())) {
133                         allResults.addAll(retainedResults.remove(missingSuppliers.poll()));
134                     }
135 
136                     if (allResults.size() >= MAX_TOTAL_RESULTS) {
137                         missingSuppliers.clear();
138                     }
139 
140                     triggerCallbacks.run();
141                 }
142             });
143         }
144     }
145 }