Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
NormsWriterPerField.cs
Go to the documentation of this file.
1 /*
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
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17 
18 using System;
19 
20 using ArrayUtil = Lucene.Net.Util.ArrayUtil;
21 using Similarity = Lucene.Net.Search.Similarity;
22 
23 namespace Lucene.Net.Index
24 {
25 
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.
30  /// </summary>
31 
32  sealed class NormsWriterPerField:InvertedDocEndConsumerPerField, System.IComparable<NormsWriterPerField>
33  {
34 
35  internal NormsWriterPerThread perThread;
36  internal FieldInfo fieldInfo;
37  internal DocumentsWriter.DocState docState;
38 
39  // Holds all docID/norm pairs we've seen
40  internal int[] docIDs = new int[1];
41  internal byte[] norms = new byte[1];
42  internal int upto;
43 
44  internal FieldInvertState fieldState;
45 
46  public void Reset()
47  {
48  // Shrink back if we are overallocated now:
49  docIDs = ArrayUtil.Shrink(docIDs, upto);
50  norms = ArrayUtil.Shrink(norms, upto);
51  upto = 0;
52  }
53 
54  public NormsWriterPerField(DocInverterPerField docInverterPerField, NormsWriterPerThread perThread, FieldInfo fieldInfo)
55  {
56  this.perThread = perThread;
57  this.fieldInfo = fieldInfo;
58  docState = perThread.docState;
59  fieldState = docInverterPerField.fieldState;
60  }
61 
62  internal override void Abort()
63  {
64  upto = 0;
65  }
66 
67  public int CompareTo(NormsWriterPerField other)
68  {
69  return String.CompareOrdinal(fieldInfo.name, other.fieldInfo.name);
70  }
71 
72  internal override void Finish()
73  {
74  System.Diagnostics.Debug.Assert(docIDs.Length == norms.Length);
75  if (fieldInfo.isIndexed && !fieldInfo.omitNorms)
76  {
77  if (docIDs.Length <= upto)
78  {
79  System.Diagnostics.Debug.Assert(docIDs.Length == upto);
80  docIDs = ArrayUtil.Grow(docIDs, 1 + upto);
81  norms = ArrayUtil.Grow(norms, 1 + upto);
82  }
83  float norm = docState.similarity.ComputeNorm(fieldInfo.name, fieldState);
84  norms[upto] = Similarity.EncodeNorm(norm);
85  docIDs[upto] = docState.docID;
86  upto++;
87  }
88  }
89  }
90 }