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;
21 import java.util.Collection;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.Iterator;
27 /** This class implements {@link InvertedDocConsumer}, which
28 * is passed each token produced by the analyzer on each
29 * field. It stores these tokens in a hash table, and
30 * allocates separate byte streams per token. Consumers of
31 * this class, eg {@link FreqProxTermsWriter} and {@link
32 * TermVectorsTermsWriter}, write their own byte streams
35 final class TermsHash extends InvertedDocConsumer {
37 final TermsHashConsumer consumer;
38 final TermsHash nextTermsHash;
39 final DocumentsWriter docWriter;
41 boolean trackAllocations;
43 public TermsHash(final DocumentsWriter docWriter, boolean trackAllocations, final TermsHashConsumer consumer, final TermsHash nextTermsHash) {
44 this.docWriter = docWriter;
45 this.consumer = consumer;
46 this.nextTermsHash = nextTermsHash;
47 this.trackAllocations = trackAllocations;
51 InvertedDocConsumerPerThread addThread(DocInverterPerThread docInverterPerThread) {
52 return new TermsHashPerThread(docInverterPerThread, this, nextTermsHash, null);
55 TermsHashPerThread addThread(DocInverterPerThread docInverterPerThread, TermsHashPerThread primaryPerThread) {
56 return new TermsHashPerThread(docInverterPerThread, this, nextTermsHash, primaryPerThread);
60 void setFieldInfos(FieldInfos fieldInfos) {
61 this.fieldInfos = fieldInfos;
62 consumer.setFieldInfos(fieldInfos);
70 if (nextTermsHash != null) {
71 nextTermsHash.abort();
77 synchronized void flush(Map<InvertedDocConsumerPerThread,Collection<InvertedDocConsumerPerField>> threadsAndFields, final SegmentWriteState state) throws IOException {
78 Map<TermsHashConsumerPerThread,Collection<TermsHashConsumerPerField>> childThreadsAndFields = new HashMap<TermsHashConsumerPerThread,Collection<TermsHashConsumerPerField>>();
79 Map<InvertedDocConsumerPerThread,Collection<InvertedDocConsumerPerField>> nextThreadsAndFields;
81 if (nextTermsHash != null)
82 nextThreadsAndFields = new HashMap<InvertedDocConsumerPerThread,Collection<InvertedDocConsumerPerField>>();
84 nextThreadsAndFields = null;
86 for (final Map.Entry<InvertedDocConsumerPerThread,Collection<InvertedDocConsumerPerField>> entry : threadsAndFields.entrySet()) {
88 TermsHashPerThread perThread = (TermsHashPerThread) entry.getKey();
90 Collection<InvertedDocConsumerPerField> fields = entry.getValue();
92 Iterator<InvertedDocConsumerPerField> fieldsIt = fields.iterator();
93 Collection<TermsHashConsumerPerField> childFields = new HashSet<TermsHashConsumerPerField>();
94 Collection<InvertedDocConsumerPerField> nextChildFields;
96 if (nextTermsHash != null)
97 nextChildFields = new HashSet<InvertedDocConsumerPerField>();
99 nextChildFields = null;
101 while(fieldsIt.hasNext()) {
102 TermsHashPerField perField = (TermsHashPerField) fieldsIt.next();
103 childFields.add(perField.consumer);
104 if (nextTermsHash != null)
105 nextChildFields.add(perField.nextPerField);
108 childThreadsAndFields.put(perThread.consumer, childFields);
109 if (nextTermsHash != null)
110 nextThreadsAndFields.put(perThread.nextPerThread, nextChildFields);
113 consumer.flush(childThreadsAndFields, state);
115 if (nextTermsHash != null)
116 nextTermsHash.flush(nextThreadsAndFields, state);
120 synchronized public boolean freeRAM() {