github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/core/index/byteSliceReader.go (about) 1 package index 2 3 import ( 4 "github.com/balzaczyy/golucene/core/util" 5 ) 6 7 /* 8 IndexInput that knows how to read the byte slices written by Posting 9 and PostingVector. We read the bytes in each slice until we hit the 10 end of that slice at which point we read the forwarding address of 11 the next slice and then jump to it. 12 */ 13 type ByteSliceReader struct { 14 *util.DataInputImpl 15 pool *util.ByteBlockPool 16 bufferUpto int 17 buffer []byte 18 upto int 19 limit int 20 level int 21 bufferOffset int 22 23 endIndex int 24 } 25 26 func newByteSliceReader() *ByteSliceReader { 27 ans := new(ByteSliceReader) 28 ans.DataInputImpl = util.NewDataInput(ans) 29 return ans 30 } 31 32 func (r *ByteSliceReader) init(pool *util.ByteBlockPool, startIndex, endIndex int) { 33 assert(endIndex-startIndex >= 0) 34 assert(startIndex >= 0) 35 assert(endIndex >= 0) 36 37 r.pool = pool 38 r.endIndex = endIndex 39 40 r.level = 0 41 r.bufferUpto = startIndex / util.BYTE_BLOCK_SIZE 42 r.bufferOffset = r.bufferUpto * util.BYTE_BLOCK_SIZE 43 r.buffer = pool.Buffers[r.bufferUpto] 44 r.upto = startIndex & util.BYTE_BLOCK_MASK 45 46 firstSize := util.LEVEL_SIZE_ARRAY[0] 47 48 if startIndex+firstSize >= endIndex { 49 // there is only this one slice to read 50 r.limit = endIndex & util.BYTE_BLOCK_MASK 51 } else { 52 r.limit = r.upto + firstSize - 4 53 } 54 } 55 56 func (r *ByteSliceReader) eof() bool { 57 assert(r.upto+r.bufferOffset <= r.endIndex) 58 return r.upto+r.bufferOffset == r.endIndex 59 } 60 61 func (r *ByteSliceReader) ReadByte() (byte, error) { 62 assert(!r.eof()) 63 assert(r.upto <= r.limit) 64 if r.upto == r.limit { 65 r.nextSlice() 66 } 67 b := r.buffer[r.upto] 68 r.upto++ 69 return b, nil 70 } 71 72 func (r *ByteSliceReader) nextSlice() { 73 // skip to our next slice 74 nextIndex := (int(r.buffer[r.limit]) << 24) + 75 (int(r.buffer[r.limit+1]) << 16) + 76 (int(r.buffer[r.limit+2]) << 8) + 77 int(r.buffer[r.limit+3]) 78 79 r.level = util.NEXT_LEVEL_ARRAY[r.level] 80 newSize := util.LEVEL_SIZE_ARRAY[r.level] 81 82 r.bufferUpto = nextIndex / util.BYTE_BLOCK_SIZE 83 r.bufferOffset = r.bufferUpto * util.BYTE_BLOCK_SIZE 84 85 r.buffer = r.pool.Buffers[r.bufferUpto] 86 r.upto = nextIndex & util.BYTE_BLOCK_MASK 87 88 if nextIndex+newSize >= r.endIndex { 89 // we are advancing to the final slice 90 assert(r.endIndex-nextIndex > 0) 91 r.limit = r.endIndex - r.bufferOffset 92 } else { 93 // this is not the final slice (subtract 4 for the forwarding 94 // address at the end of this new slice) 95 r.limit = r.upto + newSize - 4 96 } 97 } 98 99 func (r *ByteSliceReader) ReadBytes(buf []byte) error { 100 panic("not implemented yet") 101 }