1 package org.apache.lucene.facet.enhancements;
3 import java.io.IOException;
6 import org.apache.lucene.index.IndexReader;
7 import org.apache.lucene.index.Term;
9 import org.apache.lucene.facet.search.PayloadIterator;
10 import org.apache.lucene.util.Vint8;
11 import org.apache.lucene.util.Vint8.Position;
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 * A {@link PayloadIterator} for iterating over category posting lists generated
32 * using {@link EnhancementsCategoryTokenizer}.
34 * @lucene.experimental
36 public class EnhancementsPayloadIterator extends PayloadIterator {
38 private CategoryEnhancement[] EnhancedCategories;
40 private int[] enhancementLength;
41 private int[] enhancementStart;
46 * @param enhancementsList
47 * A list of the {@link CategoryEnhancement}s from the indexing
50 * A reader of the index.
52 * The category term to iterate.
55 public EnhancementsPayloadIterator(
56 List<CategoryEnhancement> enhancementsList,
57 IndexReader indexReader, Term term) throws IOException {
58 super(indexReader, term);
59 EnhancedCategories = enhancementsList
60 .toArray(new CategoryEnhancement[enhancementsList.size()]);
61 enhancementLength = new int[EnhancedCategories.length];
62 enhancementStart = new int[EnhancedCategories.length];
66 public boolean setdoc(int docId) throws IOException {
67 if (!super.setdoc(docId)) {
71 // read header - number of enhancements and their lengths
72 Position position = new Position();
73 nEnhancements = Vint8.decode(buffer, position);
74 for (int i = 0; i < nEnhancements; i++) {
75 enhancementLength[i] = Vint8.decode(buffer, position);
78 // set enhancements start points
79 enhancementStart[0] = position.pos;
80 for (int i = 1; i < nEnhancements; i++) {
81 enhancementStart[i] = enhancementStart[i - 1] + enhancementLength[i - 1];
88 * Get the data of the current category and document for a certain
89 * enhancement, or {@code null} if no such enhancement exists.
91 * @param enhancedCategory
92 * The category enhancement to apply.
93 * @return the data of the current category and document for a certain
94 * enhancement, or {@code null} if no such enhancement exists.
96 public Object getCategoryData(CategoryEnhancement enhancedCategory) {
97 for (int i = 0; i < nEnhancements; i++) {
98 if (enhancedCategory.equals(EnhancedCategories[i])) {
99 return enhancedCategory.extractCategoryTokenData(buffer,
100 enhancementStart[i], enhancementLength[i]);