-
Notifications
You must be signed in to change notification settings - Fork 2
/
offline_pointreader.go
167 lines (146 loc) · 4.64 KB
/
offline_pointreader.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
package bkd
import (
"encoding/binary"
"github.com/geange/lucene-go/codecs/utils"
"github.com/geange/lucene-go/core/store"
"io"
)
var _ PointReader = &OfflinePointReader{}
type OfflinePointReader struct {
countLeft int
in store.IndexInput
onHeapBuffer []byte
offset int
checked bool
config *Config
pointsInBuffer int
maxPointOnHeap int
name string // File name we are reading
pointValue *OfflinePointValue
}
func NewOfflinePointReader(config *Config, tempDir store.Directory,
tempFileName string, start, length int, reusableBuffer []byte) (*OfflinePointReader, error) {
reader := &OfflinePointReader{
countLeft: 0,
in: nil,
onHeapBuffer: nil,
offset: 0,
checked: false,
config: config,
pointsInBuffer: 0,
maxPointOnHeap: len(reusableBuffer) / config.BytesPerDoc(),
name: tempFileName,
pointValue: nil,
}
// TODO: need impl it ?
// if ((start + length) * config.BytesPerDoc() + CodecUtil.footerLength() > tempDir.fileLength(tempFileName)) {
// throw new IllegalArgumentException("requested slice is beyond the length of this file: start=" + start + " length=" + length + " BytesPerDoc=" + config.BytesPerDoc() + " fileLength=" + tempDir.fileLength(tempFileName) + " tempFileName=" + tempFileName);
// }
// if (reusableBuffer == null) {
// throw new IllegalArgumentException("[reusableBuffer] cannot be null");
// }
// if (reusableBuffer.length < config.BytesPerDoc()) {
// throw new IllegalArgumentException("Len of [reusableBuffer] must be bigger than " + config.BytesPerDoc());
// }
// Best-effort checksumming:
fileLength, err := tempDir.FileLength(nil, tempFileName)
if err != nil {
return nil, err
}
if start == 0 && length*config.BytesPerDoc() == int(fileLength)-utils.FooterLength() {
// If we are going to read the entire file, e.g. because BKDWriter is now
// partitioning it, we open with checksums:
reader.in, err = store.OpenChecksumInput(tempDir, tempFileName)
if err != nil {
return nil, err
}
} else {
// Since we are going to seek somewhere in the middle of a possibly huge
// file, and not read all bytes from there, don't use ChecksumIndexInput here.
// This is typically fine, because this same file will later be read fully,
// at another level of the BKDWriter recursion
reader.in, err = tempDir.OpenInput(nil, tempFileName)
if err != nil {
return nil, err
}
}
seekFP := start * (config.BytesPerDoc())
if _, err = reader.in.Seek(int64(seekFP), io.SeekStart); err != nil {
return nil, err
}
reader.countLeft = length
reader.onHeapBuffer = reusableBuffer
reader.pointValue = NewOfflinePointValue(config, reader.onHeapBuffer)
return reader, nil
}
func (r *OfflinePointReader) Close() error {
if r.countLeft == 0 && r.checked == false {
if in, ok := r.in.(store.ChecksumIndexInput); ok {
r.checked = true
if err := utils.CheckFooter(in); err != nil {
return err
}
}
}
return r.in.Close()
}
func (r *OfflinePointReader) Next() (bool, error) {
if r.pointsInBuffer > 0 {
r.pointsInBuffer--
r.offset += r.config.BytesPerDoc()
return true, nil
}
if r.countLeft == 0 {
return false, nil
}
if r.countLeft > r.maxPointOnHeap {
size := r.maxPointOnHeap * r.config.BytesPerDoc()
_, err := r.in.Read(r.onHeapBuffer[0:size])
if err != nil {
return false, err
}
r.pointsInBuffer = r.maxPointOnHeap - 1
r.countLeft -= r.maxPointOnHeap
} else {
size := r.countLeft * r.config.BytesPerDoc()
_, err := r.in.Read(r.onHeapBuffer[0:size])
if err != nil {
return false, err
}
r.pointsInBuffer = r.countLeft - 1
r.countLeft = 0
}
r.offset = 0
return true, nil
}
func (r *OfflinePointReader) PointValue() PointValue {
r.pointValue.SetOffset(r.offset)
return r.pointValue
}
var _ PointValue = &OfflinePointValue{}
// OfflinePointValue Reusable implementation for a point value offline
type OfflinePointValue struct {
config *Config
bytes []byte
offset int
}
func NewOfflinePointValue(config *Config, value []byte) *OfflinePointValue {
return &OfflinePointValue{
config: config,
bytes: value,
}
}
func (v *OfflinePointValue) PackedValue() []byte {
return v.bytes[v.offset : v.offset+v.config.PackedBytesLength()]
}
func (v *OfflinePointValue) DocID() int {
position := v.offset + v.config.PackedBytesLength()
return int(binary.BigEndian.Uint32(v.bytes[position:]))
}
func (v *OfflinePointValue) PackedValueDocIDBytes() []byte {
return v.bytes[v.offset : v.offset+v.config.BytesPerDoc()]
}
// SetOffset Sets a new value by changing the offset.
func (v *OfflinePointValue) SetOffset(offset int) {
v.offset = offset
}