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
FormatPostingsPositionsWriter.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 IndexInput = Lucene.Net.Store.IndexInput;
21 using IndexOutput = Lucene.Net.Store.IndexOutput;
22 
23 namespace Lucene.Net.Index
24 {
25 
27  {
28  internal FormatPostingsDocsWriter parent;
29  internal IndexOutput out_Renamed;
30 
31  internal bool omitTermFreqAndPositions;
32  internal bool storePayloads;
33  internal int lastPayloadLength = - 1;
34 
36  {
37  this.parent = parent;
38  omitTermFreqAndPositions = parent.omitTermFreqAndPositions;
39  if (parent.parent.parent.fieldInfos.HasProx())
40  {
41  // At least one field does not omit TF, so create the
42  // prox file
43  System.String fileName = IndexFileNames.SegmentFileName(parent.parent.parent.segment, IndexFileNames.PROX_EXTENSION);
44  state.flushedFiles.Add(fileName);
45  out_Renamed = parent.parent.parent.dir.CreateOutput(fileName);
46  parent.skipListWriter.SetProxOutput(out_Renamed);
47  }
48  // Every field omits TF so we will write no prox file
49  else
50  out_Renamed = null;
51  }
52 
53  internal int lastPosition;
54 
55  /// <summary>Add a new position &amp; payload </summary>
56  internal override void AddPosition(int position, byte[] payload, int payloadOffset, int payloadLength)
57  {
58  System.Diagnostics.Debug.Assert(!omitTermFreqAndPositions, "omitTermFreqAndPositions is true");
59  System.Diagnostics.Debug.Assert(out_Renamed != null);
60 
61  int delta = position - lastPosition;
62  lastPosition = position;
63 
64  if (storePayloads)
65  {
66  if (payloadLength != lastPayloadLength)
67  {
68  lastPayloadLength = payloadLength;
69  out_Renamed.WriteVInt((delta << 1) | 1);
70  out_Renamed.WriteVInt(payloadLength);
71  }
72  else
73  out_Renamed.WriteVInt(delta << 1);
74  if (payloadLength > 0)
75  out_Renamed.WriteBytes(payload, payloadLength);
76  }
77  else
78  out_Renamed.WriteVInt(delta);
79  }
80 
81  internal void SetField(FieldInfo fieldInfo)
82  {
83  omitTermFreqAndPositions = fieldInfo.omitTermFreqAndPositions;
84  storePayloads = omitTermFreqAndPositions?false:fieldInfo.storePayloads;
85  }
86 
87  /// <summary>Called when we are done adding positions &amp; payloads </summary>
88  internal override void Finish()
89  {
90  lastPosition = 0;
91  lastPayloadLength = - 1;
92  }
93 
94  public void Dispose()
95  {
96  // Move to protected method if class becomes unsealed
97  if (out_Renamed != null)
98  out_Renamed.Close();
99  }
100  }
101 }