2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 using ArrayUtil = Mono.Lucene.Net.Util.ArrayUtil;
21 using Similarity = Mono.Lucene.Net.Search.Similarity;
23 namespace Mono.Lucene.Net.Index
26 /// <summary>Taps into DocInverter, as an InvertedDocEndConsumer,
27 /// which is called at the end of inverting each field. We
28 /// just look at the length for the field (docState.length)
29 /// and record the norm.
32 sealed class NormsWriterPerField:InvertedDocEndConsumerPerField, System.IComparable
35 internal NormsWriterPerThread perThread;
36 internal FieldInfo fieldInfo;
37 internal DocumentsWriter.DocState docState;
39 // Holds all docID/norm pairs we've seen
40 internal int[] docIDs = new int[1];
41 internal byte[] norms = new byte[1];
44 internal FieldInvertState fieldState;
48 // Shrink back if we are overallocated now:
49 docIDs = ArrayUtil.Shrink(docIDs, upto);
50 norms = ArrayUtil.Shrink(norms, upto);
54 public NormsWriterPerField(DocInverterPerField docInverterPerField, NormsWriterPerThread perThread, FieldInfo fieldInfo)
56 this.perThread = perThread;
57 this.fieldInfo = fieldInfo;
58 docState = perThread.docState;
59 fieldState = docInverterPerField.fieldState;
62 internal override void Abort()
67 public int CompareTo(System.Object other)
69 return String.CompareOrdinal(fieldInfo.name, ((NormsWriterPerField) other).fieldInfo.name);
72 internal override void Finish()
74 System.Diagnostics.Debug.Assert(docIDs.Length == norms.Length);
75 if (fieldInfo.isIndexed && !fieldInfo.omitNorms)
77 if (docIDs.Length <= upto)
79 System.Diagnostics.Debug.Assert(docIDs.Length == upto);
80 docIDs = ArrayUtil.Grow(docIDs, 1 + upto);
81 norms = ArrayUtil.Grow(norms, 1 + upto);
83 float norm = docState.similarity.ComputeNorm(fieldInfo.name, fieldState);
84 norms[upto] = Similarity.EncodeNorm(norm);
85 docIDs[upto] = docState.docID;