Пример #1
0
  /**
   * finds tasks that match the given pattern string
   *
   * @param patternString the pattern string, used to match tasks
   * @param collector the collector that receives tasks
   * @param resultsLimit the maximum number of tasks to find. Specifying a limit enables the index
   *     to be more efficient since it can skip over matching tasks that do not score highly enough.
   *     Specify {@link Integer#MAX_VALUE} if there should be no limit.
   */
  public void find(String patternString, TaskCollector collector, int resultsLimit) {
    Assert.isNotNull(patternString);
    Assert.isNotNull(collector);
    Assert.isTrue(resultsLimit > 0);

    Lock readLock = indexReaderLock.readLock();
    readLock.lock();
    try {
      IndexReader indexReader = getIndexReader();
      if (indexReader != null) {
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        try {
          Query query = computeQuery(patternString);
          TopDocs results = indexSearcher.search(query, resultsLimit);
          for (ScoreDoc scoreDoc : results.scoreDocs) {
            Document document = indexReader.document(scoreDoc.doc);
            String taskIdentifier = document.get(FIELD_IDENTIFIER.getIndexKey());
            AbstractTask task = taskList.getTask(taskIdentifier);
            if (task != null) {
              collector.collect(task);
            }
          }
        } catch (IOException e) {
          StatusHandler.log(
              new Status(
                  IStatus.ERROR,
                  TasksIndexCore.ID_PLUGIN,
                  "Unexpected failure within task list index",
                  e)); //$NON-NLS-1$
        } finally {
          try {
            indexSearcher.close();
          } catch (IOException e) {
            // ignore
          }
        }
      }
    } finally {
      readLock.unlock();
    }
  }