This repository was archived by the owner on Feb 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdbreader.go
311 lines (247 loc) · 6.92 KB
/
dbreader.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// dbreader.go -- Constant DB built on top of the BBHash MPH
//
// Author: Sudhi Herle <[email protected]>
//
// This software does not come with any express or implied
// warranty; it is provided "as is". No claim is made to its
// suitability for any purpose.
package bbhash
import (
"encoding/binary"
"errors"
"fmt"
"io"
"os"
"syscall"
"crypto/sha512"
"crypto/subtle"
"github.com/opencoff/golang-lru"
"github.com/opencoff/go-fasthash"
)
// DBReader represents the query interface for a previously constructed
// constant database (built using NewDBWriter()). The only meaningful
// operation on such a database is Lookup().
type DBReader struct {
bb *BBHash
salt uint64
saltkey []byte
cache *lru.ARCCache
// memory mapped offset table
offsets []uint64
nkeys uint64
fd *os.File
fn string
}
// NewDBReader reads a previously construct database in file 'fn' and prepares
// it for querying. Records are opportunistically cached after reading from disk.
// We retain upto 'cache' number of records in memory (default 128).
func NewDBReader(fn string, cache int) (rd *DBReader, err error) {
fd, err := os.Open(fn)
if err != nil {
return nil, err
}
defer func() {
if err != nil {
fd.Close()
}
}()
// Number of records to cache
if cache <= 0 {
cache = 128
}
rd = &DBReader{
saltkey: make([]byte, 16),
fd: fd,
fn: fn,
}
var st os.FileInfo
var hdr *header
st, err = fd.Stat()
if err != nil {
return nil, fmt.Errorf("%s: can't stat: %s", fn, err)
}
if st.Size() < (64 + 32) {
return nil, fmt.Errorf("%s: file too small or corrupted", fn)
}
var hdrb [64]byte
_, err = io.ReadFull(fd, hdrb[:])
if err != nil {
return nil, fmt.Errorf("%s: can't read header: %s", fn, err)
}
hdr, err = rd.decodeHeader(hdrb[:], st.Size())
if err != nil {
return nil, err
}
err = rd.verifyChecksum(hdrb[:], hdr.offtbl, st.Size())
if err != nil {
return nil, err
}
// sanity check - even though we have verified the strong checksum
tblsz := hdr.nkeys * 8
if uint64(st.Size()) < (64 + 32 + tblsz) {
return nil, fmt.Errorf("%s: corrupt header", fn)
}
rd.cache, err = lru.NewARC(cache)
if err != nil {
return nil, err
}
// Now, we are certain that the header, the offset-table and bbhash bits are
// all valid and uncorrupted.
// mmap the offset table and return.
rd.offsets, err = mmapUint64(int(fd.Fd()), hdr.offtbl, int(hdr.nkeys), syscall.PROT_READ, syscall.MAP_PRIVATE)
if err != nil {
return nil, fmt.Errorf("%s: can't mmap offset table (off %d, sz %d): %s",
fn, hdr.offtbl, hdr.nkeys*8, err)
}
// The hash table starts after the offset table.
fd.Seek(int64(hdr.offtbl)+int64(hdr.nkeys*8), 0)
rd.bb, err = UnmarshalBBHash(fd)
if err != nil {
return nil, fmt.Errorf("%s: can't unmarshal hash table: %s", fn, err)
}
rd.salt = hdr.salt
rd.nkeys = hdr.nkeys
binary.BigEndian.PutUint64(rd.saltkey[:8], rd.salt)
binary.BigEndian.PutUint64(rd.saltkey[8:], ^rd.salt)
return rd, nil
}
// TotalKeys returns the total number of distinct keys in the DB
func (rd *DBReader) TotalKeys() int {
return len(rd.offsets)
}
// Close closes the db
func (rd *DBReader) Close() {
munmapUint64(int(rd.fd.Fd()), rd.offsets)
rd.fd.Close()
rd.cache.Purge()
rd.bb = nil
rd.fd = nil
rd.salt = 0
rd.saltkey = nil
rd.fn = ""
}
// Lookup looks up 'key' in the table and returns the corresponding value.
// If the key is not found, value is nil and returns false.
func (rd *DBReader) Lookup(key []byte) ([]byte, bool) {
v, err := rd.Find(key)
if err != nil {
return nil, false
}
return v, true
}
// Find looks up 'key' in the table and returns the corresponding value.
// It returns an error if the key is not found or the disk i/o failed or
// the record checksum failed.
func (rd *DBReader) Find(key []byte) ([]byte, error) {
h := fasthash.Hash64(rd.salt, key)
if v, ok := rd.cache.Get(h); ok {
r := v.(*record)
return r.val, nil
}
// Not in cache. So, go to disk and find it.
i := rd.bb.Find(h)
if i == 0 {
return nil, ErrNoKey
}
//fmt.Printf("key %s => %#x => %d\n", string(key), h, i)
off := toLittleEndianUint64(rd.offsets[i-1])
r, err := rd.decodeRecord(off)
if err != nil {
return nil, err
}
if r.hash != h {
return nil, ErrNoKey
}
/*
// XXX Do we need this?
if subtle.ConstantTimeCompare(key, r.key) != 1 {
return nil, ErrNoKey
}
*/
rd.cache.Add(h, r)
return r.val, nil
}
// Verify checksum of all metadata: offset table, bbhash bits and the file header.
func (rd *DBReader) verifyChecksum(hdrb []byte, offtbl uint64, sz int64) error {
h := sha512.New512_256()
h.Write(hdrb[:])
// we now verify the offset table before decoding anything else or allocating
// any memory.
expsz := sz - int64(offtbl) - int64(32)
rd.fd.Seek(int64(offtbl), 0)
nw, err := io.CopyN(h, rd.fd, expsz)
if err != nil {
return fmt.Errorf("%s: i/o error: %s", rd.fn, err)
}
if nw != expsz {
return fmt.Errorf("%s: partial read while verifying checksum, exp %d, saw %d", rd.fn, expsz, nw)
}
var expsum [32]byte
// Read the trailer -- which is the expected checksum
rd.fd.Seek(sz-32, 0)
_, err = io.ReadFull(rd.fd, expsum[:])
if err != nil {
return fmt.Errorf("%s: i/o error: %s", rd.fn, err)
}
csum := h.Sum(nil)
if subtle.ConstantTimeCompare(csum[:], expsum[:]) != 1 {
return fmt.Errorf("%s: checksum failure; exp %#x, saw %#x", rd.fn, expsum[:], csum[:])
}
rd.fd.Seek(int64(offtbl), 0)
return nil
}
// entry condition: b is 64 bytes long.
func (rd *DBReader) decodeHeader(b []byte, sz int64) (*header, error) {
if string(b[:4]) != "BBHH" {
return nil, fmt.Errorf("%s: bad header", rd.fn)
}
be := binary.BigEndian
h := &header{}
i := 8
h.salt = be.Uint64(b[i : i+8])
i += 8
h.nkeys = be.Uint64(b[i : i+8])
i += 8
h.offtbl = be.Uint64(b[i : i+8])
if h.offtbl < 64 || h.offtbl >= uint64(sz-32) {
return nil, fmt.Errorf("%s: corrupt header", rd.fn)
}
return h, nil
}
// read the next full record at offset 'off' - by seeking to that offset.
// calculate the record checksum, validate it and so on.
func (rd *DBReader) decodeRecord(off uint64) (*record, error) {
_, err := rd.fd.Seek(int64(off), 0)
if err != nil {
return nil, err
}
var hdr [2 + 4 + 8]byte
_, err = io.ReadFull(rd.fd, hdr[:])
if err != nil {
return nil, err
}
be := binary.BigEndian
klen := int(be.Uint16(hdr[:2]))
vlen := int(be.Uint32(hdr[2:6]))
if klen <= 0 || vlen <= 0 || klen > 65535 {
return nil, fmt.Errorf("%s: key-len %d or value-len %d out of bounds", rd.fn, klen, vlen)
}
buf := make([]byte, klen+vlen)
_, err = io.ReadFull(rd.fd, buf)
if err != nil {
return nil, err
}
x := &record{
key: buf[:klen],
val: buf[klen:],
csum: be.Uint64(hdr[6:]),
}
csum := x.checksum(rd.saltkey, off)
if csum != x.csum {
return nil, fmt.Errorf("%s: corrupted record at off %d (exp %#x, saw %#x)", rd.fn, off, x.csum, csum)
}
x.hash = fasthash.Hash64(rd.salt, x.key)
return x, nil
}
// ErrNoKey is returned when a key cannot be found in the DB
var ErrNoKey = errors.New("No such key")