github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/core/store/input.go (about) 1 package store 2 3 import ( 4 "errors" 5 "github.com/balzaczyy/golucene/core/util" 6 "io" 7 ) 8 9 type IndexInputService interface { 10 io.Closer 11 // Returns the current position in this file, where the next read will occur. 12 FilePointer() int64 13 // Sets current position in this file, where the next read will occur. 14 Seek(pos int64) error 15 Length() int64 16 } 17 18 type IndexInputSub interface { 19 io.Closer 20 util.DataReader 21 FilePointer() int64 22 Seek(pos int64) error 23 Length() int64 24 } 25 26 type IndexInput interface { 27 util.DataInput 28 IndexInputService 29 ReadBytesBuffered(buf []byte, useBuffer bool) error 30 // Clone 31 Clone() IndexInput 32 // Creates a slice of this index input, with the given description, 33 // offset, and length. The slice is seeked to the beginning. 34 Slice(desc string, offset, length int64) (IndexInput, error) 35 } 36 37 type IndexInputImpl struct { 38 *util.DataInputImpl 39 desc string 40 } 41 42 func NewIndexInputImpl(desc string, r util.DataReader) *IndexInputImpl { 43 assert2(desc != "", "resourceDescription must not be null") 44 return &IndexInputImpl{ 45 DataInputImpl: &util.DataInputImpl{Reader: r}, 46 desc: desc, 47 } 48 } 49 50 func (in *IndexInputImpl) String() string { 51 return in.desc 52 } 53 54 const ( 55 BUFFER_SIZE = 1024 56 MERGE_BUFFER_SIZE = 4096 57 ) 58 59 func bufferSize(context IOContext) int { 60 switch context.context { 61 case IO_CONTEXT_TYPE_MERGE: 62 // The normal read buffer size defaults to 1024, but 63 // increasing this during merging seems to yield 64 // performance gains. However we don't want to increase 65 // it too much because there are quite a few 66 // BufferedIndexInputs created during merging. See 67 // LUCENE-888 for details. 68 return MERGE_BUFFER_SIZE 69 default: 70 return BUFFER_SIZE 71 } 72 } 73 74 // store/ByteArrayDataInput.java 75 76 // DataInput backed by a byte array. 77 // Warning: this class omits all low-level checks. 78 type ByteArrayDataInput struct { 79 *util.DataInputImpl 80 bytes []byte 81 Pos int 82 limit int 83 } 84 85 func NewByteArrayDataInput(bytes []byte) *ByteArrayDataInput { 86 ans := &ByteArrayDataInput{} 87 ans.DataInputImpl = util.NewDataInput(ans) 88 ans.Reset(bytes) 89 return ans 90 } 91 92 func NewEmptyByteArrayDataInput() *ByteArrayDataInput { 93 ans := &ByteArrayDataInput{} 94 ans.DataInputImpl = util.NewDataInput(ans) 95 ans.Reset(make([]byte, 0)) 96 return ans 97 } 98 99 func (in *ByteArrayDataInput) Reset(bytes []byte) { 100 in.bytes = bytes 101 in.Pos = 0 102 in.limit = len(bytes) 103 } 104 105 func (in *ByteArrayDataInput) Position() int { 106 return in.Pos 107 } 108 109 // NOTE: sets pos to 0, which is not right if you had 110 // called reset w/ non-zero offset!! 111 func (in *ByteArrayDataInput) Rewind() { 112 in.Pos = 0 113 } 114 115 func (in *ByteArrayDataInput) Length() int { 116 return in.limit 117 } 118 119 func (in *ByteArrayDataInput) SkipBytes(count int64) { 120 in.Pos += int(count) 121 } 122 123 func (in *ByteArrayDataInput) ReadShort() (n int16, err error) { 124 in.Pos += 2 125 return (int16(in.bytes[in.Pos-2]) << 8) | int16(in.bytes[in.Pos-1]), nil 126 } 127 128 func (in *ByteArrayDataInput) ReadInt() (n int32, err error) { 129 in.Pos += 4 130 return (int32(in.bytes[in.Pos-4]) << 24) | (int32(in.bytes[in.Pos-3]) << 16) | 131 (int32(in.bytes[in.Pos-2]) << 8) | int32(in.bytes[in.Pos-1]), nil 132 } 133 134 func (in *ByteArrayDataInput) ReadLong() (n int64, err error) { 135 i1, _ := in.ReadInt() 136 i2, _ := in.ReadInt() 137 return (int64(i1) << 32) | int64(i2), nil 138 } 139 140 func (in *ByteArrayDataInput) ReadVInt() (n int32, err error) { 141 b := in.bytes[in.Pos] 142 in.Pos++ 143 if b < 128 { 144 return int32(b), nil 145 } 146 n = int32(b) & 0x7F 147 b = in.bytes[in.Pos] 148 in.Pos++ 149 n |= (int32(b) & 0x7F) << 7 150 if b < 128 { 151 return n, nil 152 } 153 b = in.bytes[in.Pos] 154 in.Pos++ 155 n |= (int32(b) & 0x7F) << 14 156 if b < 128 { 157 return n, nil 158 } 159 b = in.bytes[in.Pos] 160 in.Pos++ 161 n |= (int32(b) & 0x7F) << 21 162 if b < 128 { 163 return n, nil 164 } 165 b = in.bytes[in.Pos] 166 in.Pos++ 167 // Warning: the next ands use 0x0F / 0xF0 - beware copy/paste errors: 168 n |= (int32(b) & 0x0F) << 28 169 if (b & 0xF0) == 0 { 170 return n, nil 171 } 172 return 0, errors.New("Invalid vInt detected (too many bits)") 173 } 174 175 func (in *ByteArrayDataInput) ReadVLong() (n int64, err error) { 176 b := in.bytes[in.Pos] 177 in.Pos++ 178 if b < 128 { 179 return int64(b), nil 180 } 181 n = int64(b & 0x7F) 182 b = in.bytes[in.Pos] 183 in.Pos++ 184 n |= (int64(b&0x7F) << 7) 185 if b < 128 { 186 return n, nil 187 } 188 b = in.bytes[in.Pos] 189 in.Pos++ 190 n |= (int64(b&0x7F) << 14) 191 if b < 128 { 192 return n, nil 193 } 194 b = in.bytes[in.Pos] 195 in.Pos++ 196 n |= (int64(b&0x7F) << 21) 197 if b < 128 { 198 return n, nil 199 } 200 b = in.bytes[in.Pos] 201 in.Pos++ 202 n |= (int64(b&0x7F) << 28) 203 if b < 128 { 204 return n, nil 205 } 206 b = in.bytes[in.Pos] 207 in.Pos++ 208 n |= (int64(b&0x7F) << 35) 209 if b < 128 { 210 return n, nil 211 } 212 b = in.bytes[in.Pos] 213 in.Pos++ 214 n |= (int64(b&0x7F) << 42) 215 if b < 128 { 216 return n, nil 217 } 218 b = in.bytes[in.Pos] 219 in.Pos++ 220 n |= (int64(b&0x7F) << 49) 221 if b < 128 { 222 return n, nil 223 } 224 b = in.bytes[in.Pos] 225 in.Pos++ 226 n |= (int64(b&0x7F) << 56) 227 if b < 128 { 228 return n, nil 229 } 230 return 0, errors.New("Invalid vLong detected (negative values disallowed)") 231 } 232 233 func (in *ByteArrayDataInput) ReadByte() (b byte, err error) { 234 in.Pos++ 235 return in.bytes[in.Pos-1], nil 236 } 237 238 func (in *ByteArrayDataInput) ReadBytes(buf []byte) error { 239 copy(buf, in.bytes[in.Pos:in.Pos+len(buf)]) 240 in.Pos += len(buf) 241 return nil 242 }