Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f5b10ab42c39a903d4e71d4805b375a2ba5c9fd7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*******************************************************************************
 * Copyright (c) 2000, 2020 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     George Suaridze <suag@1c.ru> (1C-Soft LLC) - Bug 560168
 *******************************************************************************/
package org.eclipse.help.internal.search;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.help.internal.base.remote.RemoteHelp;
import org.eclipse.help.internal.base.remote.RemoteSearchManager;
import org.eclipse.help.internal.search.federated.FederatedSearchEntry;
import org.eclipse.help.internal.search.federated.FederatedSearchJob;
import org.eclipse.help.search.AbstractSearchProcessor;
import org.eclipse.help.search.ISearchResult;

/*
 * Manages both local and remote searching, as well as merging of results.
 */
public class SearchManager {

	private LocalSearchManager localManager = new LocalSearchManager();
	private RemoteSearchManager remoteManager = new RemoteSearchManager();

	private class SearchState {

		public IProgressMonitor localMonitor;
		public IProgressMonitor remoteMonitor;

		public ISearchQuery searchQuery;
		public BufferedSearchHitCollector bufferedCollector = new BufferedSearchHitCollector();

		public Job localSearchJob;
		public Job remoteSearchJob;

		public SearchState() {
			/*
			 * We use these jobs to perform the local and remote searches in parallel in the
			 * background.
			 */
			localSearchJob = new Job("localSearchJob") { //$NON-NLS-1$

				@Override
				protected IStatus run(IProgressMonitor monitor) {
					localManager.search(searchQuery, bufferedCollector, localMonitor);
					return Status.OK_STATUS;
				}
			};
			remoteSearchJob = new Job("remoteSearchJob") { //$NON-NLS-1$

				@Override
				protected IStatus run(IProgressMonitor monitor) {
					remoteManager.search(searchQuery, bufferedCollector, remoteMonitor);
					return Status.OK_STATUS;
				}
			};
			localSearchJob.setSystem(true);
			remoteSearchJob.setSystem(true);
		}
	}

	/*
	 * Constructs a new SearchManager.
	 */
	public SearchManager() {

	}

	/*
	 * Perform the given search both locally and remotely if configured.
	 */
	public void search(ISearchQuery searchQuery, ISearchHitCollector collector, IProgressMonitor pm)
			throws QueryTooComplexException {
		if (RemoteHelp.isEnabled()) {
			searchLocalAndRemote(searchQuery, collector, pm);
		}
		else {
			searchLocal(searchQuery, collector, pm);
		}
	}

	/*
	 * Perform the given search locally only.
	 */
	public void searchLocal(ISearchQuery searchQuery, ISearchHitCollector collector, IProgressMonitor pm)
			throws QueryTooComplexException {
		localManager.search(searchQuery, collector, pm);
	}

	/*
	 * Perform the given search both locally and remotely.
	 */
	public void searchLocalAndRemote(ISearchQuery searchQuery, ISearchHitCollector collector, IProgressMonitor pm)
			throws QueryTooComplexException {
		SearchState state = new SearchState();
		state.searchQuery = searchQuery;

		SubMonitor subMonitor = SubMonitor.convert(pm, 100);

		// allocate half of the progress bar for each
		state.localMonitor = subMonitor.split(50, SubMonitor.SUPPRESS_SUBTASK);
		state.remoteMonitor = subMonitor.split(50, SubMonitor.SUPPRESS_SUBTASK);

		// start both searches in parallel
		state.localSearchJob.schedule();
		state.remoteSearchJob.schedule();

		// wait until finished
		try {
			state.localSearchJob.join();
			state.remoteSearchJob.join();
		}
		catch (InterruptedException e) {
			String msg = "Unexpected InterruptedException while waiting for help search jobs to finish"; //$NON-NLS-1$
			Platform.getLog(getClass()).error(msg, e);
		}

		// results are in; send them off to the collector
		state.bufferedCollector.flush(collector);
		pm.done();
	}

	/**
	 * Performs the federated search.
	 */
	public void search(String expression, FederatedSearchEntry[] entries) {
		for (int i = 0; i < entries.length; i++) {
			FederatedSearchJob job = new FederatedSearchJob(expression, entries[i]);
			job.schedule();
		}
	}

	/*
	 * Returns the manager responsible for handling local searching.
	 */
	public LocalSearchManager getLocalSearchManager() {
		return localManager;
	}

	/*
	 * Returns the manager responsible for handling remote searching.
	 */
	public RemoteSearchManager getRemoteSearchManager() {
		return remoteManager;
	}

	/*
	 * Performs any necessary cleanup (workbench is shutting down).
	 */
	public void close() {
		localManager.close();
	}

	/*
	 * Gets the list of registered search processors
	 */
	public static AbstractSearchProcessor[] getSearchProcessors()
	{
		IConfigurationElement[] configs =
			Platform.getExtensionRegistry().getConfigurationElementsFor("org.eclipse.help.base.searchProcessor"); //$NON-NLS-1$

		ArrayList<Object> processors = new ArrayList<>();

		for (int c=0;c<configs.length;c++)
		{
			try {
				processors.add(
						configs[c].createExecutableExtension("class"));//$NON-NLS-1$
			} catch (CoreException e) {}
		}

		return processors.toArray(new AbstractSearchProcessor[processors.size()]);
	}

	/*
	 * Convert Lucene SearchHits to ISearchResults
	 */
	public static ISearchResult[] convertHitsToResults(SearchHit hits[]) {

		ISearchResult results[] = new ISearchResult[hits.length];
		for (int r=0;r<results.length;r++)
		{
			SearchResult result = new SearchResult();
			if (hits[r].getHref()!=null)
				result.setHref(hits[r].getHref());
			if (hits[r].getId()!=null)
				result.setId(hits[r].getId());
			if (hits[r].getParticipantId()!=null)
				result.setParticipantId(hits[r].getParticipantId());
			if (hits[r].getDescription()!=null)
				result.setDescription(hits[r].getDescription());
			if (hits[r].getLabel()!=null)
				result.setLabel(hits[r].getLabel());
			if (hits[r].getSummary()!=null)
				result.setSummary(hits[r].getSummary());
			if (hits[r].getToc()!=null)
				result.setToc(hits[r].getToc());
			if (hits[r].getIconURL()!=null)
				result.setIcon(hits[r].getIconURL());
			result.setScore(hits[r].getScore());
			result.setPotentialHit(hits[r].isPotentialHit());
			results[r] = result;
		}
		return results;
	}

	/*
	 * Convert ISearchResults to SearchHits
	 */
	public static SearchHit[] convertResultsToHits(ISearchResult[] results) {

		SearchHit hits[] = new SearchHit[results.length];
		for (int r=0;r<results.length;r++)
		{
			hits[r] = new SearchHit(
					results[r].getHref(),
					results[r].getLabel(),
					results[r].getSummary(),
					results[r].getScore(),
					results[r].getToc(),
					results[r].getId(),
					results[r].getParticipantId(),
					results[r].isPotentialHit());
		}
		return hits;
	}


	/*
	 * Buffers hits, and only sends them off to the wrapped collector
	 * when flush() is called.
	 */
	private static class BufferedSearchHitCollector implements ISearchHitCollector {
		private Set<SearchHit> allHits = new HashSet<>();
		private String wordsSearched = null;

		@Override
		public void addHits(List<SearchHit> hits, String wordsSearched) {
			if (wordsSearched != null) {
				this.wordsSearched = wordsSearched;
			}
			allHits.addAll(hits);
		}

		/*
		 * Send all the buffered hits to the underlying collector,
		 * and reset the buffers.
		 */
		public void flush(ISearchHitCollector collector) {
			// sort by score
			List<SearchHit> hitsList = new ArrayList<>(allHits);
			hitsList.sort(null);
			collector.addHits(hitsList, wordsSearched);
			allHits.clear();
			wordsSearched = null;
		}

		@Override
		public void addQTCException(QueryTooComplexException exception) throws QueryTooComplexException {
			throw exception;
		}
	}
}

Back to the top