10
10
import java .util .Set ;
11
11
import java .util .TreeSet ;
12
12
import java .util .concurrent .Callable ;
13
+ import java .util .concurrent .ExecutionException ;
13
14
import java .util .concurrent .Future ;
14
15
import java .util .concurrent .TimeUnit ;
16
+ import java .util .concurrent .TimeoutException ;
17
+ import java .util .logging .Level ;
15
18
import java .util .logging .Logger ;
16
19
import org .eclipse .microprofile .openapi .annotations .media .Schema ;
17
20
import org .unicode .cldr .util .CLDRFile ;
@@ -117,6 +120,7 @@ synchronized void addResult(SearchResult r) {
117
120
118
121
/** Mark the search as complete. */
119
122
synchronized void complete () {
123
+ logger .finest (() -> "completing.." );
120
124
if (isComplete || !isOngoing ) return ;
121
125
isComplete = true ;
122
126
isOngoing = false ;
@@ -167,20 +171,22 @@ private class Search implements Callable<Search> {
167
171
public void begin () {
168
172
// TODO: could look up cached results here.
169
173
170
- this . future = SurveyThreadManager .getExecutorService ().submit (this );
174
+ future = SurveyThreadManager .getExecutorService ().submit (this );
171
175
}
172
176
173
177
final CLDRLocale EN = CLDRLocale .getInstance ("en" );
174
178
175
179
@ Override
176
180
public Search call () throws Exception {
181
+ logger .finest ("begin call()" );
177
182
// first, simple search of exact values
178
183
addExactMatches (locale );
179
-
180
184
CLDRLocale cLoc = CLDRLocale .getInstance (locale );
181
185
CLDRLocale parLoc = cLoc .getParent ();
182
186
while (response .isEmpty () && parLoc != null ) {
183
- addExactMatches (parLoc .getBaseName ());
187
+ final String parLocName = parLoc .getBaseName ();
188
+ logger .finest (() -> " trying " + parLocName );
189
+ addExactMatches (parLocName );
184
190
parLoc = parLoc .getParent ();
185
191
}
186
192
@@ -192,37 +198,40 @@ public Search call() throws Exception {
192
198
addExactMatches (EN .getBaseName ());
193
199
}
194
200
201
+ logger .finest (() -> "completing" );
195
202
// All done (for now!)
196
203
response .complete ();
197
204
198
205
return this ;
199
206
}
200
207
201
208
private void addCodes (final String locale ) {
209
+ logger .finest (() -> "AddCodes " + locale );
202
210
final CLDRFile resolvedFile = factory .make (locale , true );
211
+ logger .finest (() -> "AddCodes made resolved " + locale );
212
+
203
213
for (final String x : resolvedFile .fullIterable ()) {
214
+ final PathHeader ph = phf .fromPath (x );
215
+ if (!ph .getSurveyToolStatus ().visible ()) continue ; // skip invisible paths
216
+
204
217
// match exact xpath
205
218
if (x .equals (request .value )) {
206
219
response .addResult (new SearchResult (x , "Exact XPath" , locale ));
207
220
return ;
208
221
}
209
222
210
- final PathHeader ph = phf .fromPath (x );
211
- if (ph .getCode ().equalsIgnoreCase (request .value )) {
212
- response .addResult (new SearchResult (x , "code: " + ph .getCode (), locale ));
213
- return ;
214
- }
215
- }
216
- // now try partial
217
- for (final String x : resolvedFile .fullIterable ()) {
218
223
// match partial xpath
219
224
if (x .startsWith (request .value )) {
220
225
response .addResult (new SearchResult (x , "Partial XPath" , locale ));
221
226
return ;
222
227
}
223
228
229
+ if (ph .getCode ().equalsIgnoreCase (request .value )) {
230
+ response .addResult (new SearchResult (x , "code: " + ph .getCode (), locale ));
231
+ return ;
232
+ }
233
+
224
234
// partial code
225
- final PathHeader ph = phf .fromPath (x );
226
235
if (ph .getCode ().contains (request .value )) {
227
236
response .addResult (new SearchResult (x , "code: " + ph .getCode (), locale ));
228
237
return ;
@@ -231,14 +240,20 @@ private void addCodes(final String locale) {
231
240
}
232
241
233
242
private CLDRFile addExactMatches (final String locale ) {
243
+ logger .finest (() -> "AEM " + locale + " on " + factory );
234
244
CLDRFile file = factory .make (locale , false );
245
+ logger .finest (() -> "AEM called make on " + locale );
235
246
Set <String > xresult = new TreeSet <>();
236
247
file .getPathsWithValue (request .value , "" , null , xresult );
237
248
for (final String xpath : xresult ) {
238
249
// Skip if this isn’t found "here"
239
250
if (!file .isHere (xpath )) {
240
251
continue ;
241
252
}
253
+
254
+ final PathHeader ph = phf .fromPath (xpath );
255
+ if (!ph .getSurveyToolStatus ().visible ()) continue ; // skip invisible paths
256
+
242
257
// Add incrementally. A user may get a partial result if they request before we are
243
258
// done.
244
259
response .addResult (new SearchResult (xpath , request .value , locale ));
@@ -294,11 +309,35 @@ public SearchResponse newSearch(final SearchRequest request, final String locale
294
309
*
295
310
* @param token
296
311
* @return
312
+ * @throws TimeoutException
313
+ * @throws ExecutionException
314
+ * @throws InterruptedException
297
315
*/
298
- public SearchResponse getSearch (final String token ) {
299
- Search s = (Search ) searches .getIfPresent (token );
300
- if (s == null ) return null ;
301
- return s .response ;
316
+ public SearchResponse getSearch (final String token ) throws ExecutionException {
317
+ final Search s = (Search ) searches .getIfPresent (token );
318
+ if (s == null ) return null ; // not present
319
+ try {
320
+ return s .future .get (1 , TimeUnit .SECONDS ).response ;
321
+ } catch (InterruptedException | TimeoutException e ) {
322
+ // the exception here isn't interesting
323
+ logger .log (
324
+ Level .FINEST ,
325
+ () ->
326
+ String .format (
327
+ "Search %s was interrupted or timeout, but will return best available result: %s" ,
328
+ token , e .toString ()));
329
+ return s .response ;
330
+ } catch (ExecutionException e ) {
331
+ logger .log (
332
+ Level .WARNING ,
333
+ e ,
334
+ () ->
335
+ String .format (
336
+ "Search %s had exception %s" ,
337
+ token , e .getCause ().getMessage ()));
338
+ s .response .complete (); // mark as complete
339
+ throw e ;
340
+ }
302
341
}
303
342
304
343
/**
0 commit comments