/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.lucene.sandbox.search;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.lucene.search.CollectorManager;
import org.apache.lucene.search.TopDocs;

/**
 * CollectorManager for {@link LargeNumHitsTopDocsCollector} that enables concurrent collection of
 * top docs across multiple segments.
 *
 * <p>Example usage:
 *
 * <pre class="prettyprint">
 * IndexSearcher searcher = ...; // your IndexSearcher
 * TopDocs topDocs = searcher.search(new MatchAllDocsQuery(),
 *     new LargeNumHitsTopDocsCollectorManager(100_000));
 * System.out.println("Total hits: " + topDocs.totalHits.value());
 * </pre>
 */
public class LargeNumHitsTopDocsCollectorManager
    implements CollectorManager<LargeNumHitsTopDocsCollector, TopDocs> {
  private final int numHits;
  private final List<LargeNumHitsTopDocsCollector> collectors;

  /**
   * Creates a new {@link LargeNumHitsTopDocsCollectorManager} given the number of hits to collect.
   *
   * @param numHits the number of results to collect.
   */
  public LargeNumHitsTopDocsCollectorManager(int numHits) {
    if (numHits <= 0) {
      throw new IllegalArgumentException("numHits must be > 0, got " + numHits);
    }
    this.numHits = numHits;
    this.collectors = new ArrayList<>();
  }

  @Override
  public LargeNumHitsTopDocsCollector newCollector() {
    LargeNumHitsTopDocsCollector collector = new LargeNumHitsTopDocsCollector(numHits);
    this.collectors.add(collector);
    return collector;
  }

  @Override
  public TopDocs reduce(Collection<LargeNumHitsTopDocsCollector> collectors) throws IOException {
    final TopDocs[] topDocs =
        collectors.stream().map(LargeNumHitsTopDocsCollector::topDocs).toArray(TopDocs[]::new);
    return TopDocs.merge(0, numHits, topDocs);
  }

  List<LargeNumHitsTopDocsCollector> getCollectors() {
    return this.collectors;
  }
}
