-
Notifications
You must be signed in to change notification settings - Fork 624
/
MultiLevelSkipListWriter.cs
211 lines (188 loc) · 7.8 KB
/
MultiLevelSkipListWriter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using Lucene.Net.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
namespace Lucene.Net.Codecs
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using IndexOutput = Lucene.Net.Store.IndexOutput;
using MathUtil = Lucene.Net.Util.MathUtil;
using RAMOutputStream = Lucene.Net.Store.RAMOutputStream;
/// <summary>
/// This abstract class writes skip lists with multiple levels.
///
/// <code>
///
/// Example for skipInterval = 3:
/// c (skip level 2)
/// c c c (skip level 1)
/// x x x x x x x x x x (skip level 0)
/// d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d (posting list)
/// 3 6 9 12 15 18 21 24 27 30 (df)
///
/// d - document
/// x - skip data
/// c - skip data with child pointer
///
/// Skip level i contains every skipInterval-th entry from skip level i-1.
/// Therefore the number of entries on level i is: floor(df / ((skipInterval ^ (i + 1))).
///
/// Each skip entry on a level i>0 contains a pointer to the corresponding skip entry in list i-1.
/// this guarantees a logarithmic amount of skips to find the target document.
///
/// While this class takes care of writing the different skip levels,
/// subclasses must define the actual format of the skip data.
/// </code>
/// <para/>
/// @lucene.experimental
/// </summary>
public abstract class MultiLevelSkipListWriter
{
/// <summary>
/// Number of levels in this skip list. </summary>
protected internal int m_numberOfSkipLevels;
/// <summary>
/// The skip interval in the list with level = 0. </summary>
private readonly int skipInterval; // LUCENENET: marked readonly
/// <summary>
/// SkipInterval used for level > 0. </summary>
private readonly int skipMultiplier; // LUCENENET: marked readonly
/// <summary>
/// For every skip level a different buffer is used. </summary>
private RAMOutputStream[] skipBuffer;
/// <summary>
/// Creates a <see cref="MultiLevelSkipListWriter"/>. </summary>
protected MultiLevelSkipListWriter(int skipInterval, int skipMultiplier, int maxSkipLevels, int df)
{
this.skipInterval = skipInterval;
this.skipMultiplier = skipMultiplier;
// calculate the maximum number of skip levels for this document frequency
if (df <= skipInterval)
{
m_numberOfSkipLevels = 1;
}
else
{
m_numberOfSkipLevels = 1 + MathUtil.Log(df / skipInterval, skipMultiplier);
}
// make sure it does not exceed maxSkipLevels
if (m_numberOfSkipLevels > maxSkipLevels)
{
m_numberOfSkipLevels = maxSkipLevels;
}
}
/// <summary>
/// Creates a <see cref="MultiLevelSkipListWriter"/>, where
/// <see cref="skipInterval"/> and <see cref="skipMultiplier"/> are
/// the same.
/// </summary>
protected MultiLevelSkipListWriter(int skipInterval, int maxSkipLevels, int df)
: this(skipInterval, skipInterval, maxSkipLevels, df)
{
}
/// <summary>
/// Allocates internal skip buffers. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected virtual void Init()
{
skipBuffer = new RAMOutputStream[m_numberOfSkipLevels];
for (int i = 0; i < m_numberOfSkipLevels; i++)
{
skipBuffer[i] = new RAMOutputStream();
}
}
/// <summary>
/// Creates new buffers or empties the existing ones. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void ResetSkip()
{
if (skipBuffer == null)
{
Init();
}
else
{
for (int i = 0; i < skipBuffer.Length; i++)
{
skipBuffer[i].Reset();
}
}
}
/// <summary>
/// Subclasses must implement the actual skip data encoding in this method.
/// </summary>
/// <param name="level"> The level skip data shall be writing for. </param>
/// <param name="skipBuffer"> The skip buffer to write to. </param>
protected abstract void WriteSkipData(int level, IndexOutput skipBuffer);
/// <summary>
/// Writes the current skip data to the buffers. The current document frequency determines
/// the max level is skip data is to be written to.
/// </summary>
/// <param name="df"> The current document frequency. </param>
/// <exception cref="IOException"> If an I/O error occurs. </exception>
public virtual void BufferSkip(int df)
{
if (Debugging.AssertsEnabled) Debugging.Assert(df % skipInterval == 0);
int numLevels = 1;
df /= skipInterval;
// determine max level
while ((df % skipMultiplier) == 0 && numLevels < m_numberOfSkipLevels)
{
numLevels++;
df /= skipMultiplier;
}
long childPointer = 0;
for (int level = 0; level < numLevels; level++)
{
WriteSkipData(level, skipBuffer[level]);
long newChildPointer = skipBuffer[level].GetFilePointer();
if (level != 0)
{
// store child pointers for all levels except the lowest
skipBuffer[level].WriteVInt64(childPointer);
}
//remember the childPointer for the next level
childPointer = newChildPointer;
}
}
/// <summary>
/// Writes the buffered skip lists to the given output.
/// </summary>
/// <param name="output"> The <see cref="IndexOutput"/> the skip lists shall be written to. </param>
/// <returns> The pointer the skip list starts. </returns>
public virtual long WriteSkip(IndexOutput output)
{
long skipPointer = output.GetFilePointer();
//System.out.println("skipper.writeSkip fp=" + skipPointer);
if (skipBuffer == null || skipBuffer.Length == 0)
{
return skipPointer;
}
for (int level = m_numberOfSkipLevels - 1; level > 0; level--)
{
long length = skipBuffer[level].GetFilePointer();
if (length > 0)
{
output.WriteVInt64(length);
skipBuffer[level].WriteTo(output);
}
}
skipBuffer[0].WriteTo(output);
return skipPointer;
}
}
}