1 package org.apache.lucene.facet.enhancements;
3 import java.io.IOException;
6 import org.apache.lucene.analysis.TokenStream;
8 import org.apache.lucene.facet.enhancements.params.EnhancementsIndexingParams;
9 import org.apache.lucene.facet.index.streaming.CategoryTokenizer;
10 import org.apache.lucene.util.Vint8;
13 * Licensed to the Apache Software Foundation (ASF) under one or more
14 * contributor license agreements. See the NOTICE file distributed with
15 * this work for additional information regarding copyright ownership.
16 * The ASF licenses this file to You under the Apache License, Version 2.0
17 * (the "License"); you may not use this file except in compliance with
18 * the License. You may obtain a copy of the License at
20 * http://www.apache.org/licenses/LICENSE-2.0
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS,
24 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
30 * A tokenizer which adds to each category token payload according to the
31 * {@link CategoryEnhancement}s defined in the given
32 * {@link EnhancementsIndexingParams}.
34 * @lucene.experimental
36 public class EnhancementsCategoryTokenizer extends CategoryTokenizer {
39 * The data buffer used for payload instance.
41 protected byte[] payloadBytes;
44 * The category enhancements to handle
46 protected List<CategoryEnhancement> enhancements;
49 * Buffers for enhancement payload bytes
51 protected byte[][] enhancementBytes;
59 * The stream of category tokens.
60 * @param indexingParams
61 * The indexing params to use.
64 public EnhancementsCategoryTokenizer(TokenStream input,
65 EnhancementsIndexingParams indexingParams) throws IOException {
66 super(input, indexingParams);
67 payloadBytes = new byte[Vint8.MAXIMUM_BYTES_NEEDED
68 * (indexingParams.getCategoryEnhancements().size() + 1)];
69 enhancements = indexingParams.getCategoryEnhancements();
70 if (enhancements != null) {
71 // create array of bytes per enhancement
72 enhancementBytes = new byte[enhancements.size()][];
73 // write once the number of enhancements in the payload bytes
74 nStart = Vint8.encode(enhancements.size(), payloadBytes, 0);
79 protected void setPayload() {
80 this.payloadAttribute.setPayload(null);
81 if (enhancements == null) {
84 // clear previous payload content
87 int nEnhancementBytes = 0;
88 for (CategoryEnhancement enhancement : enhancements) {
89 // get payload bytes from each enhancement
90 enhancementBytes[i] = enhancement
91 .getCategoryTokenBytes(categoryAttribute);
92 // write the number of bytes in the payload
93 if (enhancementBytes[i] == null) {
94 nBytes += Vint8.encode(0, payloadBytes, nBytes);
96 nBytes += Vint8.encode(enhancementBytes[i].length,
97 payloadBytes, nBytes);
98 nEnhancementBytes += enhancementBytes[i].length;
102 if (nEnhancementBytes > 0) {
103 // make sure we have space for all bytes
104 if (payloadBytes.length < nBytes + nEnhancementBytes) {
105 byte[] temp = new byte[(nBytes + nEnhancementBytes) * 2];
106 System.arraycopy(payloadBytes, 0, temp, 0, nBytes);
109 for (i = 0; i < enhancementBytes.length; i++) {
110 // add the enhancement payload bytes after the existing bytes
111 if (enhancementBytes[i] != null) {
112 System.arraycopy(enhancementBytes[i], 0, payloadBytes,
113 nBytes, enhancementBytes[i].length);
114 nBytes += enhancementBytes[i].length;
117 payload.setData(payloadBytes, 0, nBytes);
118 payloadAttribute.setPayload(payload);