23 namespace Lucene.Net.Index
37 private readonly
int maxNumberOfSkipLevels;
40 private int numberOfSkipLevels;
49 private const int numberOfLevelsToBuffer = 1;
52 private bool haveSkipped;
54 private bool isDisposed;
57 private readonly
long[] skipPointer;
58 private readonly
int[] skipInterval;
59 private readonly
int[] numSkipped;
61 private readonly
int[] skipDoc;
63 private readonly
long[] childPointer;
64 private long lastChildPointer;
66 private readonly
bool inputIsBuffered;
70 this.skipStream =
new IndexInput[maxSkipLevels];
71 this.skipPointer =
new long[maxSkipLevels];
72 this.childPointer =
new long[maxSkipLevels];
73 this.numSkipped =
new int[maxSkipLevels];
74 this.maxNumberOfSkipLevels = maxSkipLevels;
75 this.skipInterval =
new int[maxSkipLevels];
76 this.skipStream[0] = skipStream;
78 this.skipInterval[0] = skipInterval;
79 for (
int i = 1; i < maxSkipLevels; i++)
82 this.skipInterval[i] = this.skipInterval[i - 1] * skipInterval;
84 skipDoc =
new int[maxSkipLevels];
91 internal virtual int GetDoc()
100 internal virtual int SkipTo(
int target)
112 while (level < numberOfSkipLevels - 1 && target > skipDoc[level + 1])
119 if (target > skipDoc[level])
121 if (!LoadNextSkip(level))
129 if (level > 0 && lastChildPointer > skipStream[level - 1].FilePointer)
131 SeekChild(level - 1);
137 return numSkipped[0] - skipInterval[0] - 1;
140 private bool LoadNextSkip(
int level)
144 SetLastSkipData(level);
146 numSkipped[level] += skipInterval[level];
148 if (numSkipped[level] > docCount)
151 skipDoc[level] = System.Int32.MaxValue;
152 if (numberOfSkipLevels > level)
153 numberOfSkipLevels = level;
158 skipDoc[level] += ReadSkipData(level, skipStream[level]);
163 childPointer[level] = skipStream[level].ReadVLong() + skipPointer[level - 1];
170 protected internal virtual void SeekChild(
int level)
172 skipStream[level].Seek(lastChildPointer);
173 numSkipped[level] = numSkipped[level + 1] - skipInterval[level + 1];
174 skipDoc[level] = lastDoc;
177 childPointer[level] = skipStream[level].ReadVLong() + skipPointer[level - 1];
181 public void Dispose()
186 protected virtual void Dispose(
bool disposing)
188 if (isDisposed)
return;
192 for (
int i = 1; i < skipStream.Length; i++)
194 if (skipStream[i] != null)
196 skipStream[i].Close();
205 internal virtual void Init(
long skipPointer,
int df)
207 this.skipPointer[0] = skipPointer;
209 System.Array.Clear(skipDoc, 0, skipDoc.Length);
210 System.Array.Clear(numSkipped, 0, numSkipped.Length);
211 System.Array.Clear(childPointer, 0, childPointer.Length);
214 for (
int i = 1; i < numberOfSkipLevels; i++)
216 skipStream[i] = null;
221 private void LoadSkipLevels()
223 numberOfSkipLevels = docCount == 0?0:(int) System.Math.Floor(System.Math.Log(docCount) / System.Math.Log(skipInterval[0]));
224 if (numberOfSkipLevels > maxNumberOfSkipLevels)
226 numberOfSkipLevels = maxNumberOfSkipLevels;
229 skipStream[0].Seek(skipPointer[0]);
231 int toBuffer = numberOfLevelsToBuffer;
233 for (
int i = numberOfSkipLevels - 1; i > 0; i--)
236 long length = skipStream[0].ReadVLong();
239 skipPointer[i] = skipStream[0].FilePointer;
243 skipStream[i] =
new SkipBuffer(skipStream[0], (
int) length);
249 skipStream[i] = (
IndexInput) skipStream[0].Clone();
256 skipStream[0].Seek(skipStream[0].FilePointer + length);
261 skipPointer[0] = skipStream[0].FilePointer;
271 protected internal abstract int ReadSkipData(
int level,
IndexInput skipStream);
274 protected internal virtual void SetLastSkipData(
int level)
276 lastDoc = skipDoc[level];
277 lastChildPointer = childPointer[level];
285 private readonly
long pointer;
288 private bool isDisposed;
290 internal SkipBuffer(
IndexInput input,
int length)
292 data =
new byte[length];
293 pointer = input.FilePointer;
294 input.ReadBytes(data, 0, length);
297 protected override void Dispose(
bool disposing)
299 if (isDisposed)
return;
308 public override long FilePointer
310 get {
return pointer + pos; }
313 public override long Length()
318 public override byte ReadByte()
323 public override void ReadBytes(byte[] b,
int offset,
int len)
325 Array.Copy(data, pos, b, offset, len);
329 public override void Seek(
long pos)
331 this.pos = (int) (pos - pointer);
334 override public System.Object Clone()
336 System.Diagnostics.Debug.Fail(
"Port issue:",
"Lets see if we need this FilterIndexReader.Clone()");