Пример #1
0
  /**
   * This method should not be called by clients. Use {@link IndexRegistry#getSearcher()} instead.
   *
   * @param corruptedIndexes A list that will be filled by this constructor with indexes that
   *     couldn't be loaded.
   */
  @VisibleForPackageGroup
  public Searcher(
      @NotNull IndexRegistry indexRegistry,
      @NotNull FileFactory fileFactory,
      @NotNull OutlookMailFactory outlookMailFactory,
      @NotNull final List<CorruptedIndex> corruptedIndexes)
      throws IOException {
    Util.checkNotNull(indexRegistry, fileFactory, outlookMailFactory);
    this.indexRegistry = indexRegistry;
    this.fileFactory = fileFactory;
    this.outlookMailFactory = outlookMailFactory;

    readLock = indexRegistry.getReadLock();
    writeLock = indexRegistry.getWriteLock();

    // Handler for index additions
    addedListener =
        new Event.Listener<LuceneIndex>() {
          public void update(LuceneIndex eventData) {
            replaceLuceneSearcher();
          }
        };

    /*
     * This lock could be moved into the indexes handler, but we'll put it
     * here to avoid releasing and reacquiring it.
     */
    writeLock.lock();
    try {
      indexRegistry.addListeners(
          new ExistingIndexesHandler() {
            // Handle existing indexes
            public void handleExistingIndexes(List<LuceneIndex> indexes) {
              try {
                corruptedIndexes.addAll(setLuceneSearcher(indexes));
              } catch (IOException e) {
                ioException = e;
              }
            }
          },
          addedListener,
          null); // removedListener is null, see deletion thread below
    } finally {
      writeLock.unlock();
    }

    if (ioException != null) throw ioException;

    // Handler for index removals
    deletionThread =
        new Thread(Searcher.class.getName() + " (Approve pending deletions)") {
          public void run() {
            while (true) {
              try {
                List<PendingDeletion> deletions = deletionQueue.take();
                replaceLuceneSearcher();
                for (PendingDeletion deletion : deletions) deletion.setApprovedBySearcher();
              } catch (InterruptedException e) {
                break;
              }
            }
          }
        };
    deletionThread.start();
  }