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