add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / java / org / apache / lucene / index / DocInverterPerThread.java
1 package org.apache.lucene.index;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import java.io.IOException;
21
22 import org.apache.lucene.util.AttributeSource;
23 import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
24 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
25
26 /** This is a DocFieldConsumer that inverts each field,
27  *  separately, from a Document, and accepts a
28  *  InvertedTermsConsumer to process those terms. */
29
30 final class DocInverterPerThread extends DocFieldConsumerPerThread {
31   final DocInverter docInverter;
32   final InvertedDocConsumerPerThread consumer;
33   final InvertedDocEndConsumerPerThread endConsumer;
34   final SingleTokenAttributeSource singleToken = new SingleTokenAttributeSource();
35   
36   static class SingleTokenAttributeSource extends AttributeSource {
37     final CharTermAttribute termAttribute;
38     final OffsetAttribute offsetAttribute;
39     
40     private SingleTokenAttributeSource() {
41       termAttribute = addAttribute(CharTermAttribute.class);
42       offsetAttribute = addAttribute(OffsetAttribute.class);
43     }
44     
45     public void reinit(String stringValue, int startOffset,  int endOffset) {
46       termAttribute.setEmpty().append(stringValue);
47       offsetAttribute.setOffset(startOffset, endOffset);
48     }
49   }
50   
51   final DocumentsWriter.DocState docState;
52
53   final FieldInvertState fieldState = new FieldInvertState();
54
55   // Used to read a string value for a field
56   final ReusableStringReader stringReader = new ReusableStringReader();
57
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);
63   }
64
65   @Override
66   public void startDocument() throws IOException {
67     consumer.startDocument();
68     endConsumer.startDocument();
69   }
70
71   @Override
72   public DocumentsWriter.DocWriter finishDocument() throws IOException {
73     // TODO: allow endConsumer.finishDocument to also return
74     // a DocWriter
75     endConsumer.finishDocument();
76     return consumer.finishDocument();
77   }
78
79   @Override
80   void abort() {
81     try {
82       consumer.abort();
83     } finally {
84       endConsumer.abort();
85     }
86   }
87
88   @Override
89   public DocFieldConsumerPerField addField(FieldInfo fi) {
90     return new DocInverterPerField(this, fi);
91   }
92 }