1 package org.apache.lucene.index;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.IOException;
22 import org.apache.lucene.util.AttributeSource;
23 import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
24 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
26 /** This is a DocFieldConsumer that inverts each field,
27 * separately, from a Document, and accepts a
28 * InvertedTermsConsumer to process those terms. */
30 final class DocInverterPerThread extends DocFieldConsumerPerThread {
31 final DocInverter docInverter;
32 final InvertedDocConsumerPerThread consumer;
33 final InvertedDocEndConsumerPerThread endConsumer;
34 final SingleTokenAttributeSource singleToken = new SingleTokenAttributeSource();
36 static class SingleTokenAttributeSource extends AttributeSource {
37 final CharTermAttribute termAttribute;
38 final OffsetAttribute offsetAttribute;
40 private SingleTokenAttributeSource() {
41 termAttribute = addAttribute(CharTermAttribute.class);
42 offsetAttribute = addAttribute(OffsetAttribute.class);
45 public void reinit(String stringValue, int startOffset, int endOffset) {
46 termAttribute.setEmpty().append(stringValue);
47 offsetAttribute.setOffset(startOffset, endOffset);
51 final DocumentsWriter.DocState docState;
53 final FieldInvertState fieldState = new FieldInvertState();
55 // Used to read a string value for a field
56 final ReusableStringReader stringReader = new ReusableStringReader();
58 public DocInverterPerThread(DocFieldProcessorPerThread docFieldProcessorPerThread, DocInverter docInverter) {
59 this.docInverter = docInverter;
60 docState = docFieldProcessorPerThread.docState;
61 consumer = docInverter.consumer.addThread(this);
62 endConsumer = docInverter.endConsumer.addThread(this);
66 public void startDocument() throws IOException {
67 consumer.startDocument();
68 endConsumer.startDocument();
72 public DocumentsWriter.DocWriter finishDocument() throws IOException {
73 // TODO: allow endConsumer.finishDocument to also return
75 endConsumer.finishDocument();
76 return consumer.finishDocument();
89 public DocFieldConsumerPerField addField(FieldInfo fi) {
90 return new DocInverterPerField(this, fi);