-
Notifications
You must be signed in to change notification settings - Fork 5
/
reader.go
269 lines (212 loc) · 5.96 KB
/
reader.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package cdb
import (
"bytes"
"encoding/binary"
"errors"
"io"
)
// EntryDoesNotExists could be returned for Get method is cdb has no such key
var ErrEntryNotFound = errors.New("cdb entry not found")
// hashTableRef is a pointer that state a position and a length of the hash table
// position is the starting byte position of the hash table.
// The length is the number of slots in the hash table.
type hashTableRef struct {
position, length uint32
}
// readerImpl implements Reader interface
type readerImpl struct {
refs [tableNum]hashTableRef
reader io.ReaderAt
hasher Hasher
endPos uint32
size int
}
// newReader returns a new readerImpl object on success, otherwise returns nil and an error
func newReader(reader io.ReaderAt, hasher Hasher) (*readerImpl, error) {
r := &readerImpl{
reader: reader,
hasher: hasher,
}
if err := r.initialize(); err != nil {
return nil, err
}
return r, nil
}
// initialize reads hashTableRefs from r.reader
func (r *readerImpl) initialize() error {
buf := make([]byte, tablesRefsSize)
_, err := r.reader.ReadAt(buf, 0)
if err != nil {
return errors.New("Invalid db header, impossible to read hashTableRefs structures")
}
for i := range &r.refs {
j := i * 8
r.refs[i].position, r.refs[i].length = binary.LittleEndian.Uint32(buf[j:j+4]), binary.LittleEndian.Uint32(buf[j+4:j+8])
r.size += int(r.refs[i].length >> 1)
}
for _, ref := range &r.refs {
if ref.position != 0 {
r.endPos = ref.position
break
}
}
return nil
}
// IsEmpty returns true if cdb has no records
func (r *readerImpl) IsEmpty() bool {
return r.endPos == 0
}
// Get returns the first value associated with the given key
func (r *readerImpl) Get(key []byte) ([]byte, error) {
valueSection, err := r.findEntry(key)
if err != nil {
return nil, err
}
if valueSection == nil {
return nil, ErrEntryNotFound
}
value := make([]byte, valueSection.size)
if _, err = valueSection.reader.ReadAt(value, int64(valueSection.position)); err != nil {
return nil, err
}
return value, nil
}
// Has returns true if the given key exists, otherwise returns false.
func (r *readerImpl) Has(key []byte) (bool, error) {
valueSection, err := r.findEntry(key)
return valueSection != nil, err
}
// Iterator returns new Iterator object that points on first record
func (r *readerImpl) Iterator() (Iterator, error) {
iterator, err := r.newIterator(tablesRefsSize, nil, nil)
if err != nil {
return nil, err
}
if _, err := iterator.Next(); err != nil {
return nil, err
}
return iterator, nil
}
// IteratorAt returns a new Iterator object that points on the first record associated with the given key.
func (r *readerImpl) IteratorAt(key []byte) (Iterator, error) {
valueSection, err := r.findEntry(key)
if err != nil || valueSection == nil {
return nil, err
}
return r.newIterator(
valueSection.position+valueSection.size,
§ionReaderFactory{
reader: bytes.NewReader(key),
size: uint32(len(key)),
},
valueSection,
)
}
// Size returns the size of the dataset
func (r *readerImpl) Size() int {
return r.size
}
// findEntry finds an entry for the given key
//
// A record is located as follows:
// * Compute the hash value of the key in the record.
// * The hash value modulo 256 (tableNum) is the number of a hash table.
// * The hash value divided by 256, modulo the length of that table, is a slot number.
// * Probe that slot, the next higher slot, and so on, until you find the record or run into an empty slot.
func (r *readerImpl) findEntry(key []byte) (*sectionReaderFactory, error) {
h := r.calcHash(key)
ref := &r.refs[h%tableNum]
if ref.length == 0 {
return nil, nil
}
var (
entry slot
j uint32
valueSection *sectionReaderFactory
err error
)
k := (h >> 8) % ref.length
for j = 0; j < ref.length; j++ {
r.readPair(ref.position+k*slotSize, &entry.hash, &entry.position)
if entry.position == 0 {
return nil, nil
}
if entry.hash == h {
valueSection, err = r.checkEntry(entry, key)
if err != nil {
return nil, err
}
if valueSection != nil {
break
}
}
k = (k + 1) % ref.length
}
return valueSection, nil
}
// calcHash returns hash value of given key
func (r *readerImpl) calcHash(key []byte) uint32 {
hashFunc := r.hasher()
hashFunc.Write(key)
return hashFunc.Sum32()
}
// checkEntry returns io.SectionReader if given slot belongs to given key, otherwise nil
func (r *readerImpl) checkEntry(entry slot, key []byte) (*sectionReaderFactory, error) {
var (
keySize, valSize uint32
givenKeySize = uint32(len(key))
)
if err := r.readPair(entry.position, &keySize, &valSize); err != nil {
return nil, err
}
if keySize != givenKeySize {
return nil, nil
}
data := make([]byte, keySize)
if _, err := r.reader.ReadAt(data, int64(entry.position+8)); err != nil {
return nil, err
}
if bytes.Compare(data, key) != 0 {
return nil, nil
}
return §ionReaderFactory{
reader: r.reader,
position: entry.position + 8 + keySize,
size: valSize,
}, nil
}
// readPair reads from r.reader uint_32 pair if possible. Returns an error on failure
func (r *readerImpl) readPair(pos uint32, a, b *uint32) error {
pair := make([]byte, 8, 8)
_, err := r.reader.ReadAt(pair, int64(pos))
if err != nil {
return err
}
*a, *b = binary.LittleEndian.Uint32(pair), binary.LittleEndian.Uint32(pair[4:])
return nil
}
// newIterator returns new instance of Iterator object
func (r *readerImpl) newIterator(position uint32, keySectionFactory, valueSectionFactory *sectionReaderFactory) (Iterator, error) {
if r.IsEmpty() {
return nil, ErrEmptyCDB
}
if keySectionFactory == nil {
keySectionFactory = §ionReaderFactory{
reader: r.reader,
}
}
if valueSectionFactory == nil {
valueSectionFactory = §ionReaderFactory{
reader: r.reader,
}
}
resIterator := &iterator{
position: position,
cdbReader: r,
record: &record{
keySectionFactory: keySectionFactory,
valueSectionFactory: valueSectionFactory,
},
}
return resIterator, nil
}