pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / index / TermVectorsTermsWriterPerThread.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 org.apache.lucene.util.UnicodeUtil;
21
22 final class TermVectorsTermsWriterPerThread extends TermsHashConsumerPerThread {
23
24   final TermVectorsTermsWriter termsWriter;
25   final TermsHashPerThread termsHashPerThread;
26   final DocumentsWriter.DocState docState;
27
28   TermVectorsTermsWriter.PerDoc doc;
29
30   public TermVectorsTermsWriterPerThread(TermsHashPerThread termsHashPerThread, TermVectorsTermsWriter termsWriter) {
31     this.termsWriter = termsWriter;
32     this.termsHashPerThread = termsHashPerThread;
33     docState = termsHashPerThread.docState;
34   }
35   
36   // Used by perField when serializing the term vectors
37   final ByteSliceReader vectorSliceReader = new ByteSliceReader();
38
39   final UnicodeUtil.UTF8Result utf8Results[] = {new UnicodeUtil.UTF8Result(),
40                                                 new UnicodeUtil.UTF8Result()};
41
42   @Override
43   public void startDocument() {
44     assert clearLastVectorFieldName();
45     if (doc != null) {
46       doc.reset();
47       doc.docID = docState.docID;
48     }
49   }
50
51   @Override
52   public DocumentsWriter.DocWriter finishDocument() {
53     try {
54       return doc;
55     } finally {
56       doc = null;
57     }
58   }
59
60   @Override
61   public TermsHashConsumerPerField addField(TermsHashPerField termsHashPerField, FieldInfo fieldInfo) {
62     return new TermVectorsTermsWriterPerField(termsHashPerField, this, fieldInfo);
63   }
64
65   @Override
66   public void abort() {
67     if (doc != null) {
68       doc.abort();
69       doc = null;
70     }
71   }
72
73   // Called only by assert
74   final boolean clearLastVectorFieldName() {
75     lastVectorFieldName = null;
76     return true;
77   }
78
79   // Called only by assert
80   String lastVectorFieldName;
81   final boolean vectorFieldsInOrder(FieldInfo fi) {
82     try {
83       if (lastVectorFieldName != null)
84         return lastVectorFieldName.compareTo(fi.name) < 0;
85       else
86         return true;
87     } finally {
88       lastVectorFieldName = fi.name;
89     }
90   }
91 }