pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / index / ByteSliceReader.java
1 package org.apache.lucene.index;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import org.apache.lucene.store.IndexInput;
21 import org.apache.lucene.store.IndexOutput;
22 import java.io.IOException;
23
24 /* IndexInput that knows how to read the byte slices written
25  * by Posting and PostingVector.  We read the bytes in
26  * each slice until we hit the end of that slice at which
27  * point we read the forwarding address of the next slice
28  * and then jump to it.*/
29 final class ByteSliceReader extends IndexInput {
30   ByteBlockPool pool;
31   int bufferUpto;
32   byte[] buffer;
33   public int upto;
34   int limit;
35   int level;
36   public int bufferOffset;
37
38   public int endIndex;
39
40   public void init(ByteBlockPool pool, int startIndex, int endIndex) {
41
42     assert endIndex-startIndex >= 0;
43     assert startIndex >= 0;
44     assert endIndex >= 0;
45
46     this.pool = pool;
47     this.endIndex = endIndex;
48
49     level = 0;
50     bufferUpto = startIndex / DocumentsWriter.BYTE_BLOCK_SIZE;
51     bufferOffset = bufferUpto * DocumentsWriter.BYTE_BLOCK_SIZE;
52     buffer = pool.buffers[bufferUpto];
53     upto = startIndex & DocumentsWriter.BYTE_BLOCK_MASK;
54
55     final int firstSize = ByteBlockPool.levelSizeArray[0];
56
57     if (startIndex+firstSize >= endIndex) {
58       // There is only this one slice to read
59       limit = endIndex & DocumentsWriter.BYTE_BLOCK_MASK;
60     } else
61       limit = upto+firstSize-4;
62   }
63
64   public boolean eof() {
65     assert upto + bufferOffset <= endIndex;
66     return upto + bufferOffset == endIndex;
67   }
68
69   @Override
70   public byte readByte() {
71     assert !eof();
72     assert upto <= limit;
73     if (upto == limit)
74       nextSlice();
75     return buffer[upto++];
76   }
77
78   public long writeTo(IndexOutput out) throws IOException {
79     long size = 0;
80     while(true) {
81       if (limit + bufferOffset == endIndex) {
82         assert endIndex - bufferOffset >= upto;
83         out.writeBytes(buffer, upto, limit-upto);
84         size += limit-upto;
85         break;
86       } else {
87         out.writeBytes(buffer, upto, limit-upto);
88         size += limit-upto;
89         nextSlice();
90       }
91     }
92
93     return size;
94   }
95
96   public void nextSlice() {
97
98     // Skip to our next slice
99     final int nextIndex = ((buffer[limit]&0xff)<<24) + ((buffer[1+limit]&0xff)<<16) + ((buffer[2+limit]&0xff)<<8) + (buffer[3+limit]&0xff);
100
101     level = ByteBlockPool.nextLevelArray[level];
102     final int newSize = ByteBlockPool.levelSizeArray[level];
103
104     bufferUpto = nextIndex / DocumentsWriter.BYTE_BLOCK_SIZE;
105     bufferOffset = bufferUpto * DocumentsWriter.BYTE_BLOCK_SIZE;
106
107     buffer = pool.buffers[bufferUpto];
108     upto = nextIndex & DocumentsWriter.BYTE_BLOCK_MASK;
109
110     if (nextIndex + newSize >= endIndex) {
111       // We are advancing to the final slice
112       assert endIndex - nextIndex > 0;
113       limit = endIndex - bufferOffset;
114     } else {
115       // This is not the final slice (subtract 4 for the
116       // forwarding address at the end of this new slice)
117       limit = upto+newSize-4;
118     }
119   }
120
121   @Override
122   public void readBytes(byte[] b, int offset, int len) {
123     while(len > 0) {
124       final int numLeft = limit-upto;
125       if (numLeft < len) {
126         // Read entire slice
127         System.arraycopy(buffer, upto, b, offset, numLeft);
128         offset += numLeft;
129         len -= numLeft;
130         nextSlice();
131       } else {
132         // This slice is the last one
133         System.arraycopy(buffer, upto, b, offset, len);
134         upto += len;
135         break;
136       }
137     }
138   }
139
140   @Override
141   public long getFilePointer() {throw new RuntimeException("not implemented");}
142   @Override
143   public long length() {throw new RuntimeException("not implemented");}
144   @Override
145   public void seek(long pos) {throw new RuntimeException("not implemented");}
146   @Override
147   public void close() {throw new RuntimeException("not implemented");}
148 }
149