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
ByteSliceReader.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 
26  /* IndexInput that knows how to read the byte slices written
27  * by Posting and PostingVector. We read the bytes in
28  * each slice until we hit the end of that slice at which
29  * point we read the forwarding address of the next slice
30  * and then jump to it.*/
31  public sealed class ByteSliceReader : IndexInput
32  {
33  internal ByteBlockPool pool;
34  internal int bufferUpto;
35  internal byte[] buffer;
36  public int upto;
37  internal int limit;
38  internal int level;
39  public int bufferOffset;
40 
41  public int endIndex;
42 
43  public void Init(ByteBlockPool pool, int startIndex, int endIndex)
44  {
45 
46  System.Diagnostics.Debug.Assert(endIndex - startIndex >= 0);
47  System.Diagnostics.Debug.Assert(startIndex >= 0);
48  System.Diagnostics.Debug.Assert(endIndex >= 0);
49 
50  this.pool = pool;
51  this.endIndex = endIndex;
52 
53  level = 0;
54  bufferUpto = startIndex / DocumentsWriter.BYTE_BLOCK_SIZE;
55  bufferOffset = bufferUpto * DocumentsWriter.BYTE_BLOCK_SIZE;
56  buffer = pool.buffers[bufferUpto];
57  upto = startIndex & DocumentsWriter.BYTE_BLOCK_MASK;
58 
59  int firstSize = ByteBlockPool.levelSizeArray[0];
60 
61  if (startIndex + firstSize >= endIndex)
62  {
63  // There is only this one slice to read
64  limit = endIndex & DocumentsWriter.BYTE_BLOCK_MASK;
65  }
66  else
67  limit = upto + firstSize - 4;
68  }
69 
70  public bool Eof()
71  {
72  System.Diagnostics.Debug.Assert(upto + bufferOffset <= endIndex);
73  return upto + bufferOffset == endIndex;
74  }
75 
76  public override byte ReadByte()
77  {
78  System.Diagnostics.Debug.Assert(!Eof());
79  System.Diagnostics.Debug.Assert(upto <= limit);
80  if (upto == limit)
81  NextSlice();
82  return buffer[upto++];
83  }
84 
85  public long WriteTo(IndexOutput @out)
86  {
87  long size = 0;
88  while (true)
89  {
90  if (limit + bufferOffset == endIndex)
91  {
92  System.Diagnostics.Debug.Assert(endIndex - bufferOffset >= upto);
93  @out.WriteBytes(buffer, upto, limit - upto);
94  size += limit - upto;
95  break;
96  }
97  else
98  {
99  @out.WriteBytes(buffer, upto, limit - upto);
100  size += limit - upto;
101  NextSlice();
102  }
103  }
104 
105  return size;
106  }
107 
108  public void NextSlice()
109  {
110 
111  // Skip to our next slice
112  int nextIndex = ((buffer[limit] & 0xff) << 24) + ((buffer[1 + limit] & 0xff) << 16) + ((buffer[2 + limit] & 0xff) << 8) + (buffer[3 + limit] & 0xff);
113 
114  level = ByteBlockPool.nextLevelArray[level];
115  int newSize = ByteBlockPool.levelSizeArray[level];
116 
117  bufferUpto = nextIndex / DocumentsWriter.BYTE_BLOCK_SIZE;
118  bufferOffset = bufferUpto * DocumentsWriter.BYTE_BLOCK_SIZE;
119 
120  buffer = pool.buffers[bufferUpto];
121  upto = nextIndex & DocumentsWriter.BYTE_BLOCK_MASK;
122 
123  if (nextIndex + newSize >= endIndex)
124  {
125  // We are advancing to the final slice
126  System.Diagnostics.Debug.Assert(endIndex - nextIndex > 0);
127  limit = endIndex - bufferOffset;
128  }
129  else
130  {
131  // This is not the final slice (subtract 4 for the
132  // forwarding address at the end of this new slice)
133  limit = upto + newSize - 4;
134  }
135  }
136 
137  public override void ReadBytes(byte[] b, int offset, int len)
138  {
139  while (len > 0)
140  {
141  int numLeft = limit - upto;
142  if (numLeft < len)
143  {
144  // Read entire slice
145  Array.Copy(buffer, upto, b, offset, numLeft);
146  offset += numLeft;
147  len -= numLeft;
148  NextSlice();
149  }
150  else
151  {
152  // This slice is the last one
153  Array.Copy(buffer, upto, b, offset, len);
154  upto += len;
155  break;
156  }
157  }
158  }
159 
160  public override long FilePointer
161  {
162  get { throw new NotImplementedException(); }
163  }
164 
165  public override long Length()
166  {
167  throw new NotImplementedException();
168  }
169  public override void Seek(long pos)
170  {
171  throw new NotImplementedException();
172  }
173 
174  protected override void Dispose(bool disposing)
175  {
176  // Do nothing...
177  }
178 
179  override public Object Clone()
180  {
181  System.Diagnostics.Debug.Fail("Port issue:", "Let see if we need this ByteSliceReader.Clone()"); // {{Aroush-2.9}}
182  return null;
183  }
184  }
185 }