1 package org.apache.lucene.index;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import org.apache.lucene.store.IndexInput;
21 import org.apache.lucene.store.IndexOutput;
22 import java.io.IOException;
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 {
36 public int bufferOffset;
40 public void init(ByteBlockPool pool, int startIndex, int endIndex) {
42 assert endIndex-startIndex >= 0;
43 assert startIndex >= 0;
47 this.endIndex = endIndex;
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;
55 final int firstSize = ByteBlockPool.levelSizeArray[0];
57 if (startIndex+firstSize >= endIndex) {
58 // There is only this one slice to read
59 limit = endIndex & DocumentsWriter.BYTE_BLOCK_MASK;
61 limit = upto+firstSize-4;
64 public boolean eof() {
65 assert upto + bufferOffset <= endIndex;
66 return upto + bufferOffset == endIndex;
70 public byte readByte() {
75 return buffer[upto++];
78 public long writeTo(IndexOutput out) throws IOException {
81 if (limit + bufferOffset == endIndex) {
82 assert endIndex - bufferOffset >= upto;
83 out.writeBytes(buffer, upto, limit-upto);
87 out.writeBytes(buffer, upto, limit-upto);
96 public void nextSlice() {
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);
101 level = ByteBlockPool.nextLevelArray[level];
102 final int newSize = ByteBlockPool.levelSizeArray[level];
104 bufferUpto = nextIndex / DocumentsWriter.BYTE_BLOCK_SIZE;
105 bufferOffset = bufferUpto * DocumentsWriter.BYTE_BLOCK_SIZE;
107 buffer = pool.buffers[bufferUpto];
108 upto = nextIndex & DocumentsWriter.BYTE_BLOCK_MASK;
110 if (nextIndex + newSize >= endIndex) {
111 // We are advancing to the final slice
112 assert endIndex - nextIndex > 0;
113 limit = endIndex - bufferOffset;
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;
122 public void readBytes(byte[] b, int offset, int len) {
124 final int numLeft = limit-upto;
127 System.arraycopy(buffer, upto, b, offset, numLeft);
132 // This slice is the last one
133 System.arraycopy(buffer, upto, b, offset, len);
141 public long getFilePointer() {throw new RuntimeException("not implemented");}
143 public long length() {throw new RuntimeException("not implemented");}
145 public void seek(long pos) {throw new RuntimeException("not implemented");}
147 public void close() {throw new RuntimeException("not implemented");}