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.Iterator;
23 import java.util.HashMap;
25 import java.util.List;
26 import java.util.ArrayList;
28 import org.apache.lucene.store.IndexOutput;
29 import org.apache.lucene.search.Similarity;
30 import org.apache.lucene.util.IOUtils;
32 // TODO FI: norms could actually be stored as doc store
34 /** Writes norms. Each thread X field accumulates the norms
35 * for the doc/fields it saw, then the flush method below
36 * merges all of these together into a single _X.nrm file.
39 final class NormsWriter extends InvertedDocEndConsumer {
41 private final byte defaultNorm = Similarity.getDefault().encodeNormValue(1.0f);
42 private FieldInfos fieldInfos;
44 public InvertedDocEndConsumerPerThread addThread(DocInverterPerThread docInverterPerThread) {
45 return new NormsWriterPerThread(docInverterPerThread, this);
49 public void abort() {}
51 // We only write the _X.nrm file at flush
52 void files(Collection<String> files) {}
55 void setFieldInfos(FieldInfos fieldInfos) {
56 this.fieldInfos = fieldInfos;
59 /** Produce _X.nrm if any document had a field with norms
62 public void flush(Map<InvertedDocEndConsumerPerThread,Collection<InvertedDocEndConsumerPerField>> threadsAndFields, SegmentWriteState state) throws IOException {
64 final Map<FieldInfo,List<NormsWriterPerField>> byField = new HashMap<FieldInfo,List<NormsWriterPerField>>();
66 // Typically, each thread will have encountered the same
67 // field. So first we collate by field, ie, all
68 // per-thread field instances that correspond to the
70 for (final Map.Entry<InvertedDocEndConsumerPerThread,Collection<InvertedDocEndConsumerPerField>> entry : threadsAndFields.entrySet()) {
71 final Collection<InvertedDocEndConsumerPerField> fields = entry.getValue();
72 final Iterator<InvertedDocEndConsumerPerField> fieldsIt = fields.iterator();
74 while (fieldsIt.hasNext()) {
75 final NormsWriterPerField perField = (NormsWriterPerField) fieldsIt.next();
77 if (perField.upto > 0) {
79 List<NormsWriterPerField> l = byField.get(perField.fieldInfo);
81 l = new ArrayList<NormsWriterPerField>();
82 byField.put(perField.fieldInfo, l);
86 // Remove this field since we haven't seen it
87 // since the previous flush
92 final String normsFileName = IndexFileNames.segmentFileName(state.segmentName, IndexFileNames.NORMS_EXTENSION);
93 IndexOutput normsOut = state.directory.createOutput(normsFileName);
94 boolean success = false;
96 normsOut.writeBytes(SegmentNorms.NORMS_HEADER, 0, SegmentNorms.NORMS_HEADER.length);
98 final int numField = fieldInfos.size();
102 for(int fieldNumber=0;fieldNumber<numField;fieldNumber++) {
104 final FieldInfo fieldInfo = fieldInfos.fieldInfo(fieldNumber);
106 List<NormsWriterPerField> toMerge = byField.get(fieldInfo);
108 if (toMerge != null) {
110 final int numFields = toMerge.size();
114 final NormsWriterPerField[] fields = new NormsWriterPerField[numFields];
115 int[] uptos = new int[numFields];
117 for(int j=0;j<numFields;j++)
118 fields[j] = toMerge.get(j);
120 int numLeft = numFields;
124 assert uptos[0] < fields[0].docIDs.length : " uptos[0]=" + uptos[0] + " len=" + (fields[0].docIDs.length);
127 int minDocID = fields[0].docIDs[uptos[0]];
129 for(int j=1;j<numLeft;j++) {
130 final int docID = fields[j].docIDs[uptos[j]];
131 if (docID < minDocID) {
137 assert minDocID < state.numDocs;
140 for(;upto<minDocID;upto++)
141 normsOut.writeByte(defaultNorm);
143 normsOut.writeByte(fields[minLoc].norms[uptos[minLoc]]);
147 if (uptos[minLoc] == fields[minLoc].upto) {
148 fields[minLoc].reset();
149 if (minLoc != numLeft-1) {
150 fields[minLoc] = fields[numLeft-1];
151 uptos[minLoc] = uptos[numLeft-1];
157 // Fill final hole with defaultNorm
158 for(;upto<state.numDocs;upto++)
159 normsOut.writeByte(defaultNorm);
160 } else if (fieldInfo.isIndexed && !fieldInfo.omitNorms) {
162 // Fill entire field with default norm:
163 for(;upto<state.numDocs;upto++)
164 normsOut.writeByte(defaultNorm);
167 assert 4+normCount*state.numDocs == normsOut.getFilePointer() : ".nrm file size mismatch: expected=" + (4+normCount*state.numDocs) + " actual=" + normsOut.getFilePointer();
172 IOUtils.close(normsOut);
174 IOUtils.closeWhileHandlingException(normsOut);