forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 3
/
MultiLevelSkipListReader.cs
392 lines (341 loc) · 13.3 KB
/
MultiLevelSkipListReader.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
using System;
using System.Diagnostics;
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 BufferedIndexInput = Lucene.Net.Store.BufferedIndexInput;
using IndexInput = Lucene.Net.Store.IndexInput;
using MathUtil = Lucene.Net.Util.MathUtil;
/// <summary>
/// This abstract class reads skip lists with multiple levels.
/// <para/>
/// See <see cref="MultiLevelSkipListWriter"/> for the information about the encoding
/// of the multi level skip lists.
/// <para/>
/// Subclasses must implement the abstract method <see cref="ReadSkipData(int, IndexInput)"/>
/// which defines the actual format of the skip data.
/// <para/>
/// @lucene.experimental
/// </summary>
public abstract class MultiLevelSkipListReader : IDisposable
{
/// <summary>
/// The maximum number of skip levels possible for this index. </summary>
protected internal int m_maxNumberOfSkipLevels;
// number of levels in this skip list
private int numberOfSkipLevels;
// Expert: defines the number of top skip levels to buffer in memory.
// Reducing this number results in less memory usage, but possibly
// slower performance due to more random I/Os.
// Please notice that the space each level occupies is limited by
// the skipInterval. The top level can not contain more than
// skipLevel entries, the second top level can not contain more
// than skipLevel^2 entries and so forth.
private int numberOfLevelsToBuffer = 1;
private int docCount;
private bool haveSkipped;
/// <summary>
/// SkipStream for each level. </summary>
private IndexInput[] skipStream;
/// <summary>
/// The start pointer of each skip level. </summary>
private long[] skipPointer;
/// <summary>
/// SkipInterval of each level. </summary>
private int[] skipInterval;
/// <summary>
/// Number of docs skipped per level. </summary>
private int[] numSkipped;
/// <summary>
/// Doc id of current skip entry per level. </summary>
protected internal int[] m_skipDoc;
/// <summary>
/// Doc id of last read skip entry with docId <= target. </summary>
private int lastDoc;
/// <summary>
/// Child pointer of current skip entry per level. </summary>
private long[] childPointer;
/// <summary>
/// childPointer of last read skip entry with docId <=
/// target.
/// </summary>
private long lastChildPointer;
private bool inputIsBuffered;
private readonly int skipMultiplier;
/// <summary>
/// Creates a <see cref="MultiLevelSkipListReader"/>. </summary>
protected MultiLevelSkipListReader(IndexInput skipStream, int maxSkipLevels, int skipInterval, int skipMultiplier)
{
this.skipStream = new IndexInput[maxSkipLevels];
this.skipPointer = new long[maxSkipLevels];
this.childPointer = new long[maxSkipLevels];
this.numSkipped = new int[maxSkipLevels];
this.m_maxNumberOfSkipLevels = maxSkipLevels;
this.skipInterval = new int[maxSkipLevels];
this.skipMultiplier = skipMultiplier;
this.skipStream[0] = skipStream;
this.inputIsBuffered = (skipStream is BufferedIndexInput);
this.skipInterval[0] = skipInterval;
for (int i = 1; i < maxSkipLevels; i++)
{
// cache skip intervals
this.skipInterval[i] = this.skipInterval[i - 1] * skipMultiplier;
}
m_skipDoc = new int[maxSkipLevels];
}
/// <summary>
/// Creates a <see cref="MultiLevelSkipListReader"/>, where
/// <see cref="skipInterval"/> and <see cref="skipMultiplier"/> are
/// the same.
/// </summary>
protected internal MultiLevelSkipListReader(IndexInput skipStream, int maxSkipLevels, int skipInterval)
: this(skipStream, maxSkipLevels, skipInterval, skipInterval)
{
}
/// <summary>
/// Returns the id of the doc to which the last call of <see cref="SkipTo(int)"/>
/// has skipped.
/// </summary>
public virtual int Doc
{
get
{
return lastDoc;
}
}
/// <summary>
/// Skips entries to the first beyond the current whose document number is
/// greater than or equal to <paramref name="target"/>. Returns the current doc count.
/// </summary>
public virtual int SkipTo(int target)
{
if (!haveSkipped)
{
// first time, load skip levels
LoadSkipLevels();
haveSkipped = true;
}
// walk up the levels until highest level is found that has a skip
// for this target
int level = 0;
while (level < numberOfSkipLevels - 1 && target > m_skipDoc[level + 1])
{
level++;
}
while (level >= 0)
{
if (target > m_skipDoc[level])
{
if (!LoadNextSkip(level))
{
continue;
}
}
else
{
// no more skips on this level, go down one level
if (level > 0 && lastChildPointer > skipStream[level - 1].GetFilePointer())
{
SeekChild(level - 1);
}
level--;
}
}
return numSkipped[0] - skipInterval[0] - 1;
}
private bool LoadNextSkip(int level)
{
// we have to skip, the target document is greater than the current
// skip list entry
SetLastSkipData(level);
numSkipped[level] += skipInterval[level];
if (numSkipped[level] > docCount)
{
// this skip list is exhausted
m_skipDoc[level] = int.MaxValue;
if (numberOfSkipLevels > level)
{
numberOfSkipLevels = level;
}
return false;
}
// read next skip entry
m_skipDoc[level] += ReadSkipData(level, skipStream[level]);
if (level != 0)
{
// read the child pointer if we are not on the leaf level
childPointer[level] = skipStream[level].ReadVInt64() + skipPointer[level - 1];
}
return true;
}
/// <summary>
/// Seeks the skip entry on the given level. </summary>
protected virtual void SeekChild(int level)
{
skipStream[level].Seek(lastChildPointer);
numSkipped[level] = numSkipped[level + 1] - skipInterval[level + 1];
m_skipDoc[level] = lastDoc;
if (level > 0)
{
childPointer[level] = skipStream[level].ReadVInt64() + skipPointer[level - 1];
}
}
/// <summary>
/// Disposes all resources used by this object.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Disposes all resources used by this object. Subclasses may override
/// to dispose their own resources.
/// </summary>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
for (int i = 1; i < skipStream.Length; i++)
{
if (skipStream[i] != null)
{
skipStream[i].Dispose();
}
}
}
}
/// <summary>
/// Initializes the reader, for reuse on a new term. </summary>
public virtual void Init(long skipPointer, int df)
{
this.skipPointer[0] = skipPointer;
this.docCount = df;
Debug.Assert(skipPointer >= 0 && skipPointer <= skipStream[0].Length, "invalid skip pointer: " + skipPointer + ", length=" + skipStream[0].Length);
Array.Clear(m_skipDoc, 0, m_skipDoc.Length);
Array.Clear(numSkipped, 0, numSkipped.Length);
Array.Clear(childPointer, 0, childPointer.Length);
haveSkipped = false;
for (int i = 1; i < numberOfSkipLevels; i++)
{
skipStream[i] = null;
}
}
/// <summary>
/// Loads the skip levels. </summary>
private void LoadSkipLevels()
{
if (docCount <= skipInterval[0])
{
numberOfSkipLevels = 1;
}
else
{
numberOfSkipLevels = 1 + MathUtil.Log(docCount / skipInterval[0], skipMultiplier);
}
if (numberOfSkipLevels > m_maxNumberOfSkipLevels)
{
numberOfSkipLevels = m_maxNumberOfSkipLevels;
}
skipStream[0].Seek(skipPointer[0]);
int toBuffer = numberOfLevelsToBuffer;
for (int i = numberOfSkipLevels - 1; i > 0; i--)
{
// the length of the current level
long length = skipStream[0].ReadVInt64();
// the start pointer of the current level
skipPointer[i] = skipStream[0].GetFilePointer();
if (toBuffer > 0)
{
// buffer this level
skipStream[i] = new SkipBuffer(skipStream[0], (int)length);
toBuffer--;
}
else
{
// clone this stream, it is already at the start of the current level
skipStream[i] = (IndexInput)skipStream[0].Clone();
if (inputIsBuffered && length < BufferedIndexInput.BUFFER_SIZE)
{
((BufferedIndexInput)skipStream[i]).SetBufferSize((int)length);
}
// move base stream beyond the current level
skipStream[0].Seek(skipStream[0].GetFilePointer() + length);
}
}
// use base stream for the lowest level
skipPointer[0] = skipStream[0].GetFilePointer();
}
/// <summary>
/// Subclasses must implement the actual skip data encoding in this method.
/// </summary>
/// <param name="level"> The level skip data shall be read from. </param>
/// <param name="skipStream"> The skip stream to read from. </param>
protected abstract int ReadSkipData(int level, IndexInput skipStream);
/// <summary>
/// Copies the values of the last read skip entry on this <paramref name="level"/>. </summary>
protected virtual void SetLastSkipData(int level)
{
lastDoc = m_skipDoc[level];
lastChildPointer = childPointer[level];
}
/// <summary>
/// Used to buffer the top skip levels. </summary>
private sealed class SkipBuffer : IndexInput
{
private byte[] data;
private long pointer;
private int pos;
internal SkipBuffer(IndexInput input, int length)
: base("SkipBuffer on " + input)
{
data = new byte[length];
pointer = input.GetFilePointer();
input.ReadBytes(data, 0, length);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
data = null;
}
}
public override long GetFilePointer()
{
return pointer + pos;
}
public override long Length
{
get { return data.Length; }
}
public override byte ReadByte()
{
return data[pos++];
}
public override void ReadBytes(byte[] b, int offset, int len)
{
Array.Copy(data, pos, b, offset, len);
pos += len;
}
public override void Seek(long pos)
{
this.pos = (int)(pos - pointer);
}
}
}
}