add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / java / org / apache / lucene / facet / search / cache / CategoryListData.java
1 package org.apache.lucene.facet.search.cache;
2
3 import java.io.IOException;
4
5 import org.apache.lucene.index.IndexReader;
6
7 import org.apache.lucene.facet.index.params.CategoryListParams;
8 import org.apache.lucene.facet.index.params.FacetIndexingParams;
9 import org.apache.lucene.facet.search.CategoryListIterator;
10 import org.apache.lucene.facet.taxonomy.TaxonomyReader;
11 import org.apache.lucene.util.collections.IntArray;
12
13 /**
14  * Licensed to the Apache Software Foundation (ASF) under one or more
15  * contributor license agreements.  See the NOTICE file distributed with
16  * this work for additional information regarding copyright ownership.
17  * The ASF licenses this file to You under the Apache License, Version 2.0
18  * (the "License"); you may not use this file except in compliance with
19  * the License.  You may obtain a copy of the License at
20  *
21  *     http://www.apache.org/licenses/LICENSE-2.0
22  *
23  * Unless required by applicable law or agreed to in writing, software
24  * distributed under the License is distributed on an "AS IS" BASIS,
25  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26  * See the License for the specific language governing permissions and
27  * limitations under the License.
28  */
29
30 /**
31  * Category list data maintained in RAM.
32  * <p>
33  * Speeds up facets accumulation when more RAM is available.
34  * <p>
35  * Note that this will consume more memory: one int (4 bytes) for each category
36  * of each document.
37  * <p>
38  * Note: at the moment this class is insensitive to updates of the index, and,
39  * in particular, does not make use of Lucene's ability to refresh a single
40  * segment.
41  * <p>
42  * See {@link CategoryListCache#register(CategoryListParams, CategoryListData)}
43  * and
44  * {@link CategoryListCache#loadAndRegister(CategoryListParams, IndexReader, TaxonomyReader, FacetIndexingParams)}.
45  * 
46  * @lucene.experimental
47  */
48 public class CategoryListData {
49   
50   // TODO (Facet): experiment with different orders - p-d-c vs. current d-p-c.
51   private transient volatile int[][][] docPartitionCategories;  
52   
53   /**
54    * Empty constructor for extensions with modified computation of the data.
55    */
56   protected CategoryListData() {
57   }
58   
59   /**
60    * Compute category list data for caching for faster iteration.
61    */
62   CategoryListData(IndexReader reader, TaxonomyReader taxo, 
63       FacetIndexingParams iparams, CategoryListParams clp) throws IOException {
64   
65     final int maxDoc = reader.maxDoc();
66     int[][][]dpf  = new int[maxDoc][][];
67     int numPartitions = (int)Math.ceil(taxo.getSize()/(double)iparams.getPartitionSize());
68     IntArray docCategories = new IntArray(); 
69     for (int part=0; part<numPartitions; part++) {
70       CategoryListIterator cli = clp.createCategoryListIterator(reader, part);
71       if (cli.init()) {
72         for (int doc=0; doc<maxDoc; doc++) {
73           if (cli.skipTo(doc)) {
74             docCategories.clear(false);
75             if (dpf[doc]==null) {
76               dpf[doc] = new int[numPartitions][];
77             }
78             long category;
79             while ((category = cli.nextCategory()) <= Integer.MAX_VALUE) {
80               docCategories.addToArray((int)category);
81             }
82             final int size = docCategories.size();
83             dpf[doc][part] = new int[size];
84             for (int i=0; i<size; i++) {
85               dpf[doc][part][i] = docCategories.get(i);
86             }
87           }
88         }
89       }
90     }
91     docPartitionCategories = dpf;
92   }
93   
94   /**
95    * Iterate on the category list data for the specified partition.
96    */
97   public CategoryListIterator iterator(int partition) throws IOException {
98     return new RAMCategoryListIterator(partition, docPartitionCategories);
99   }
100
101   /**
102    * Internal: category list iterator over uncompressed category info in RAM
103    */
104   private static class RAMCategoryListIterator implements CategoryListIterator {
105     private final int part;
106     private final int[][][] dpc;
107     private int currDoc = -1;
108     private int nextCategoryIndex = -1;  
109     
110     RAMCategoryListIterator(int part, int[][][] docPartitionCategories) {
111       this.part = part;
112       dpc = docPartitionCategories;
113     }
114
115     public boolean init() throws IOException {
116       return dpc!=null && dpc.length>part;
117     }
118
119     public long nextCategory() throws IOException {
120       if (nextCategoryIndex >= dpc[currDoc][part].length) {
121         return 1L+Integer.MAX_VALUE;
122       }
123       return dpc[currDoc][part][nextCategoryIndex++]; 
124     }
125
126     public boolean skipTo(int docId) throws IOException {
127       final boolean res = dpc.length>docId && dpc[docId]!=null && dpc[docId][part]!=null;
128       if (res) {
129         currDoc = docId;
130         nextCategoryIndex = 0;
131       }
132       return res;
133     }
134   }
135 }