Exemple #1
0
 /**
  * Replaces the site name(s) of {@link FederatedSource}s in the {@link QueryResponse} with the
  * fanout's site name to keep info about the {@link FederatedSource}s hidden from the external
  * client.
  *
  * @param queryResponse the original {@link QueryResponse} from the query request
  * @return the updated {@link QueryResponse} with all site names replaced with fanout's site name
  */
 public QueryResponse replaceSourceId(QueryResponse queryResponse) {
   LOGGER.trace("ENTERING: replaceSourceId()");
   List<Result> results = queryResponse.getResults();
   QueryResponseImpl newResponse =
       new QueryResponseImpl(queryResponse.getRequest(), queryResponse.getProperties());
   for (Result result : results) {
     MetacardImpl newMetacard = new MetacardImpl(result.getMetacard());
     newMetacard.setSourceId(this.getId());
     ResultImpl newResult = new ResultImpl(newMetacard);
     // Copy over scores
     newResult.setDistanceInMeters(result.getDistanceInMeters());
     newResult.setRelevanceScore(result.getRelevanceScore());
     newResponse.addResult(newResult, false);
   }
   newResponse.setHits(queryResponse.getHits());
   newResponse.closeResultQueue();
   LOGGER.trace("EXITING: replaceSourceId()");
   return newResponse;
 }
    @SuppressWarnings({"rawtypes", "unchecked"})
    @Override
    public void run() {
      String methodName = "run";
      logger.entry(methodName);

      SortBy sortBy = query.getSortBy();
      // Prepare the Comparators that we will use
      Comparator<Result> coreComparator = DEFAULT_COMPARATOR;

      if (sortBy != null && sortBy.getPropertyName() != null) {
        PropertyName sortingProp = sortBy.getPropertyName();
        String sortType = sortingProp.getPropertyName();
        SortOrder sortOrder =
            (sortBy.getSortOrder() == null) ? SortOrder.DESCENDING : sortBy.getSortOrder();
        logger.debug("Sorting by type: " + sortType);
        logger.debug("Sorting by Order: " + sortBy.getSortOrder());

        // Temporal searches are currently sorted by the effective time
        if (Metacard.EFFECTIVE.equals(sortType) || Result.TEMPORAL.equals(sortType)) {
          coreComparator = new TemporalResultComparator(sortOrder);
        } else if (Result.DISTANCE.equals(sortType)) {
          coreComparator = new DistanceResultComparator(sortOrder);
        } else if (Result.RELEVANCE.equals(sortType)) {
          coreComparator = new RelevanceResultComparator(sortOrder);
        }
      }

      List<Result> resultList = new ArrayList<Result>();
      long totalHits = 0;
      Set<ProcessingDetails> processingDetails = returnResults.getProcessingDetails();

      Map<String, Serializable> returnProperties = returnResults.getProperties();
      for (final Entry<Source, Future<SourceResponse>> entry : futures.entrySet()) {
        Source site = entry.getKey();
        SourceResponse sourceResponse = null;
        try {
          sourceResponse =
              query.getTimeoutMillis() < 1
                  ? entry.getValue().get()
                  : entry.getValue().get(getTimeRemaining(deadline), TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
          logger.warn(
              "Couldn't get results from completed federated query on site with ShortName "
                  + site.getId(),
              e);
          processingDetails.add(new ProcessingDetailsImpl(site.getId(), e));
        } catch (ExecutionException e) {
          logger.warn(
              "Couldn't get results from completed federated query on site " + site.getId(), e);
          if (logger.isDebugEnabled()) {
            logger.debug("Adding exception to response.");
          }
          processingDetails.add(new ProcessingDetailsImpl(site.getId(), e));
        } catch (TimeoutException e) {
          logger.warn("search timed out: " + new Date() + " on site " + site.getId());
          processingDetails.add(new ProcessingDetailsImpl(site.getId(), e));
        }
        if (sourceResponse != null) {
          List<Result> sourceResults = sourceResponse.getResults();
          resultList.addAll(sourceResults);
          long sourceHits = sourceResponse.getHits();

          totalHits += sourceHits;
          Map<String, Serializable> newSourceProperties = new HashMap<String, Serializable>();
          newSourceProperties.put(QueryResponse.TOTAL_HITS, sourceHits);
          newSourceProperties.put(QueryResponse.TOTAL_RESULTS_RETURNED, sourceResults.size());

          Map<String, Serializable> originalSourceProperties = sourceResponse.getProperties();
          if (originalSourceProperties != null) {
            Serializable object = originalSourceProperties.get(QueryResponse.ELAPSED_TIME);
            if (object != null && object instanceof Long) {
              newSourceProperties.put(QueryResponse.ELAPSED_TIME, (Long) object);
              originalSourceProperties.remove(QueryResponse.ELAPSED_TIME);
              logger.debug(
                  "Setting the ellapsedTime responseProperty to {} for source {}",
                  object,
                  site.getId());
            }

            // TODO: for now add all properties into outgoing response's properties.
            // this is not the best idea because we could get properties from records
            // that get eliminated by the max results enforcement done below.
            // See DDF-1183 for a possible solution.
            returnProperties.putAll(originalSourceProperties);
          }
          returnProperties.put(site.getId(), (Serializable) newSourceProperties);
          logger.debug("Setting the query responseProperties for site {}", site.getId());

          // Add a List of siteIds so endpoints know what sites got queried
          Serializable siteListObject = returnProperties.get(QueryResponse.SITE_LIST);
          if (siteListObject != null && siteListObject instanceof List<?>) {
            ((List) siteListObject).add(site.getId());
          } else {
            siteListObject = new ArrayList<String>();
            ((List) siteListObject).add(site.getId());
            returnProperties.put(QueryResponse.SITE_LIST, (Serializable) siteListObject);
          }
        }
      }
      logger.debug("all sites finished returning results: " + resultList.size());

      Collections.sort(resultList, coreComparator);

      returnResults.setHits(totalHits);
      int maxResults = query.getPageSize() > 0 ? query.getPageSize() : Integer.MAX_VALUE;

      returnResults.addResults(
          resultList.size() > maxResults ? resultList.subList(0, maxResults) : resultList, true);
    }