pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / java / org / apache / lucene / facet / util / PartitionsUtils.java
1 package org.apache.lucene.facet.util;
2
3 import org.apache.lucene.facet.index.params.CategoryListParams;
4 import org.apache.lucene.facet.index.params.FacetIndexingParams;
5 import org.apache.lucene.facet.search.params.FacetSearchParams;
6 import org.apache.lucene.facet.taxonomy.TaxonomyReader;
7
8 /**
9  * Licensed to the Apache Software Foundation (ASF) under one or more
10  * contributor license agreements.  See the NOTICE file distributed with
11  * this work for additional information regarding copyright ownership.
12  * The ASF licenses this file to You under the Apache License, Version 2.0
13  * (the "License"); you may not use this file except in compliance with
14  * the License.  You may obtain a copy of the License at
15  *
16  *     http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  */
24
25 /**
26  * Utilities for partitions - sizes and such
27  * 
28  * @lucene.experimental
29  */
30 public final class PartitionsUtils {
31
32   /**
33    * Get the offset for a given partition.  That is, what is the minimum number an
34    * ordinal could be for a particular partition. 
35    */
36   public final static int partitionOffset (  FacetIndexingParams iParams, 
37                         int partitionNumber, 
38                         final TaxonomyReader taxonomyReader) {
39     return partitionNumber * partitionSize(iParams, taxonomyReader);
40   }
41
42   /**
43    * @see #partitionOffset(FacetIndexingParams, int, TaxonomyReader)
44    */
45   public final static int partitionOffset (  FacetSearchParams sParams, 
46                         int partitionNumber, 
47                         final TaxonomyReader taxonomyReader) {
48     return partitionOffset(sParams.getFacetIndexingParams(), partitionNumber, taxonomyReader);
49   }
50
51   /**
52    * Get the partition size in this parameter, or return the size of the taxonomy, which
53    * is smaller.  (Guarantees usage of as little memory as possible at search time).
54    */
55   public final static int partitionSize(FacetIndexingParams indexingParams, final TaxonomyReader taxonomyReader) {
56     return Math.min(indexingParams.getPartitionSize(), taxonomyReader.getSize());
57   }
58
59   /**
60    * @see #partitionSize(FacetIndexingParams, TaxonomyReader)
61    */
62   public final static int partitionSize(FacetSearchParams sParams, final TaxonomyReader taxonomyReader) {
63     return partitionSize(sParams.getFacetIndexingParams(), taxonomyReader);
64   }
65
66   /**
67    * Partition number of an ordinal.
68    * <p>
69    * This allows to locate the partition containing a certain (facet) ordinal.
70    * @see FacetIndexingParams#getPartitionSize()      
71    */
72   public final static int partitionNumber(FacetIndexingParams iParams, int ordinal) {
73     return ordinal / iParams.getPartitionSize();
74   }
75
76   /**
77    * @see #partitionNumber(FacetIndexingParams, int)
78    */
79   public final static int partitionNumber(FacetSearchParams sParams, int ordinal) {
80     return partitionNumber(sParams.getFacetIndexingParams(), ordinal);
81   }
82
83   /**
84    * Partition name by category ordinal
85    */
86   public final static String partitionNameByOrdinal(  FacetIndexingParams iParams, 
87                             CategoryListParams clParams, 
88                             int ordinal) {
89     int partition = partitionNumber(iParams, ordinal); 
90     return partitionName(clParams, partition);
91   }
92
93   /** 
94    * Partition name by its number
95    */
96   public final static String partitionName(CategoryListParams clParams, int partition) {
97     String term = clParams.getTerm().text();
98     if (partition == 0) {
99       return term; // for backwards compatibility we do not add a partition number in this case
100     }
101     return term + partition;
102   }
103
104 }