add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / grouping / src / java / org / apache / lucene / search / grouping / TermSecondPassGroupingCollector.java
1 package org.apache.lucene.search.grouping;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.IOException;
21 import java.util.Collection;
22
23 import org.apache.lucene.index.IndexReader;
24 import org.apache.lucene.search.FieldCache;
25 import org.apache.lucene.search.Sort;
26
27 /**
28  * Concrete implementation of {@link AbstractSecondPassGroupingCollector} that groups based on
29  * field values and more specifically uses {@link org.apache.lucene.search.FieldCache.StringIndex}
30  * to collect grouped docs.
31  *
32  * @lucene.experimental
33  */
34 public class TermSecondPassGroupingCollector extends AbstractSecondPassGroupingCollector<String> {
35
36   private final SentinelIntSet ordSet;
37   private FieldCache.StringIndex index;
38   private final String groupField;
39
40   @SuppressWarnings("unchecked")
41   public TermSecondPassGroupingCollector(String groupField, Collection<SearchGroup<String>> groups, Sort groupSort, Sort withinGroupSort,
42                                          int maxDocsPerGroup, boolean getScores, boolean getMaxScores, boolean fillSortFields)
43       throws IOException {
44     super(groups, groupSort, withinGroupSort, maxDocsPerGroup, getScores, getMaxScores, fillSortFields);
45     ordSet = new SentinelIntSet(groupMap.size(), -1);
46     this.groupField = groupField;
47     groupDocs = (SearchGroupDocs<String>[]) new SearchGroupDocs[ordSet.keys.length];
48   }
49
50   @Override
51   public void setNextReader(IndexReader reader, int docBase) throws IOException {
52     super.setNextReader(reader, docBase);
53     index = FieldCache.DEFAULT.getStringIndex(reader, groupField);
54
55     // Rebuild ordSet
56     ordSet.clear();
57     for (SearchGroupDocs<String> group : groupMap.values()) {
58 //      System.out.println("  group=" + (group.groupValue == null ? "null" : group.groupValue.utf8ToString()));
59       int ord = group.groupValue == null ? 0 : index.binarySearchLookup(group.groupValue);
60       if (ord >= 0) {
61         groupDocs[ordSet.put(ord)] = group;
62       }
63     }
64   }
65
66   @Override
67   protected SearchGroupDocs<String> retrieveGroup(int doc) throws IOException {
68     int slot = ordSet.find(index.order[doc]);
69     if (slot >= 0) {
70       return groupDocs[slot];
71     }
72     return null;
73   }
74 }