1 package org.apache.lucene.facet.search.cache;
3 import java.io.IOException;
5 import org.apache.lucene.index.IndexReader;
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;
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
21 * http://www.apache.org/licenses/LICENSE-2.0
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.
31 * Category list data maintained in RAM.
33 * Speeds up facets accumulation when more RAM is available.
35 * Note that this will consume more memory: one int (4 bytes) for each category
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
42 * See {@link CategoryListCache#register(CategoryListParams, CategoryListData)}
44 * {@link CategoryListCache#loadAndRegister(CategoryListParams, IndexReader, TaxonomyReader, FacetIndexingParams)}.
46 * @lucene.experimental
48 public class CategoryListData {
50 // TODO (Facet): experiment with different orders - p-d-c vs. current d-p-c.
51 private transient volatile int[][][] docPartitionCategories;
54 * Empty constructor for extensions with modified computation of the data.
56 protected CategoryListData() {
60 * Compute category list data for caching for faster iteration.
62 CategoryListData(IndexReader reader, TaxonomyReader taxo,
63 FacetIndexingParams iparams, CategoryListParams clp) throws IOException {
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);
72 for (int doc=0; doc<maxDoc; doc++) {
73 if (cli.skipTo(doc)) {
74 docCategories.clear(false);
76 dpf[doc] = new int[numPartitions][];
79 while ((category = cli.nextCategory()) <= Integer.MAX_VALUE) {
80 docCategories.addToArray((int)category);
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);
91 docPartitionCategories = dpf;
95 * Iterate on the category list data for the specified partition.
97 public CategoryListIterator iterator(int partition) throws IOException {
98 return new RAMCategoryListIterator(partition, docPartitionCategories);
102 * Internal: category list iterator over uncompressed category info in RAM
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;
110 RAMCategoryListIterator(int part, int[][][] docPartitionCategories) {
112 dpc = docPartitionCategories;
115 public boolean init() throws IOException {
116 return dpc!=null && dpc.length>part;
119 public long nextCategory() throws IOException {
120 if (nextCategoryIndex >= dpc[currDoc][part].length) {
121 return 1L+Integer.MAX_VALUE;
123 return dpc[currDoc][part][nextCategoryIndex++];
126 public boolean skipTo(int docId) throws IOException {
127 final boolean res = dpc.length>docId && dpc[docId]!=null && dpc[docId][part]!=null;
130 nextCategoryIndex = 0;