pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / index / DocInverter.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.HashMap;
23 import java.util.HashSet;
24
25 import java.util.Map;
26
27
28 /** This is a DocFieldConsumer that inverts each field,
29  *  separately, from a Document, and accepts a
30  *  InvertedTermsConsumer to process those terms. */
31
32 final class DocInverter extends DocFieldConsumer {
33
34   final InvertedDocConsumer consumer;
35   final InvertedDocEndConsumer endConsumer;
36
37   public DocInverter(InvertedDocConsumer consumer, InvertedDocEndConsumer endConsumer) {
38     this.consumer = consumer;
39     this.endConsumer = endConsumer;
40   }
41
42   @Override
43   void setFieldInfos(FieldInfos fieldInfos) {
44     super.setFieldInfos(fieldInfos);
45     consumer.setFieldInfos(fieldInfos);
46     endConsumer.setFieldInfos(fieldInfos);
47   }
48
49   @Override
50   void flush(Map<DocFieldConsumerPerThread, Collection<DocFieldConsumerPerField>> threadsAndFields, SegmentWriteState state) throws IOException {
51
52     Map<InvertedDocConsumerPerThread,Collection<InvertedDocConsumerPerField>> childThreadsAndFields = new HashMap<InvertedDocConsumerPerThread,Collection<InvertedDocConsumerPerField>>();
53     Map<InvertedDocEndConsumerPerThread,Collection<InvertedDocEndConsumerPerField>> endChildThreadsAndFields = new HashMap<InvertedDocEndConsumerPerThread,Collection<InvertedDocEndConsumerPerField>>();
54
55     for (Map.Entry<DocFieldConsumerPerThread,Collection<DocFieldConsumerPerField>> entry : threadsAndFields.entrySet() ) {
56       DocInverterPerThread perThread = (DocInverterPerThread) entry.getKey();
57
58       Collection<InvertedDocConsumerPerField> childFields = new HashSet<InvertedDocConsumerPerField>();
59       Collection<InvertedDocEndConsumerPerField> endChildFields = new HashSet<InvertedDocEndConsumerPerField>();
60       for (final DocFieldConsumerPerField field: entry.getValue() ) {  
61         DocInverterPerField perField = (DocInverterPerField) field;
62         childFields.add(perField.consumer);
63         endChildFields.add(perField.endConsumer);
64       }
65
66       childThreadsAndFields.put(perThread.consumer, childFields);
67       endChildThreadsAndFields.put(perThread.endConsumer, endChildFields);
68     }
69     
70     consumer.flush(childThreadsAndFields, state);
71     endConsumer.flush(endChildThreadsAndFields, state);
72   }
73
74   @Override
75   void abort() {
76     try {
77       consumer.abort();
78     } finally {
79       endConsumer.abort();
80     }
81   }
82
83   @Override
84   public boolean freeRAM() {
85     return consumer.freeRAM();
86   }
87
88   @Override
89   public DocFieldConsumerPerThread addThread(DocFieldProcessorPerThread docFieldProcessorPerThread) {
90     return new DocInverterPerThread(docFieldProcessorPerThread, this);
91   }
92 }