pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / index / DocFieldProcessor.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 import java.util.Collection;
22 import java.util.Map;
23 import java.util.HashMap;
24
25
26 /**
27  * This is a DocConsumer that gathers all fields under the
28  * same name, and calls per-field consumers to process field
29  * by field.  This class doesn't doesn't do any "real" work
30  * of its own: it just forwards the fields to a
31  * DocFieldConsumer.
32  */
33
34 final class DocFieldProcessor extends DocConsumer {
35
36   final DocumentsWriter docWriter;
37   final FieldInfos fieldInfos;
38   final DocFieldConsumer consumer;
39   final StoredFieldsWriter fieldsWriter;
40
41   public DocFieldProcessor(DocumentsWriter docWriter, DocFieldConsumer consumer) {
42     this.docWriter = docWriter;
43     this.consumer = consumer;
44     fieldInfos = docWriter.getFieldInfos();
45     consumer.setFieldInfos(fieldInfos);
46     fieldsWriter = new StoredFieldsWriter(docWriter, fieldInfos);
47   }
48
49   @Override
50   public void flush(Collection<DocConsumerPerThread> threads, SegmentWriteState state) throws IOException {
51
52     Map<DocFieldConsumerPerThread, Collection<DocFieldConsumerPerField>> childThreadsAndFields = new HashMap<DocFieldConsumerPerThread, Collection<DocFieldConsumerPerField>>();
53     for ( DocConsumerPerThread thread : threads) {
54       DocFieldProcessorPerThread perThread = (DocFieldProcessorPerThread) thread;
55       childThreadsAndFields.put(perThread.consumer, perThread.fields());
56       perThread.trimFields(state);
57     }
58
59     fieldsWriter.flush(state);
60     consumer.flush(childThreadsAndFields, state);
61
62     // Important to save after asking consumer to flush so
63     // consumer can alter the FieldInfo* if necessary.  EG,
64     // FreqProxTermsWriter does this with
65     // FieldInfo.storePayload.
66     final String fileName = IndexFileNames.segmentFileName(state.segmentName, IndexFileNames.FIELD_INFOS_EXTENSION);
67     fieldInfos.write(state.directory, fileName);
68   }
69
70   @Override
71   public void abort() {
72     try {
73       fieldsWriter.abort();
74     } finally {
75       consumer.abort();
76     }
77   }
78
79   @Override
80   public boolean freeRAM() {
81     return consumer.freeRAM();
82   }
83
84   @Override
85   public DocConsumerPerThread addThread(DocumentsWriterThreadState threadState) throws IOException {
86     return new DocFieldProcessorPerThread(threadState, this);
87   }
88 }