-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbreader.go
340 lines (273 loc) · 7.89 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// dbreader.go -- Constant DB built on top of the CHD MPH
//
// (c) Sudhi Herle 2018
//
// Author: Sudhi Herle <sudhi@herle.net>
//
// 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 chd
import (
"encoding/binary"
"fmt"
"io"
"os"
"syscall"
"crypto/sha512"
"crypto/subtle"
"github.com/dchest/siphash"
"github.com/opencoff/golang-lru"
)
// 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 {
chd *Chd
cache *lru.ARCCache
flags uint32
// memory mapped offset+hashkey table
offset []uint64
// memory mapped vlen table
vlen []uint32
nkeys uint64
salt []byte
offtbl uint64
// original mmap slice
mmap []byte
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
}
// Number of records to cache
if cache <= 0 {
cache = 128
}
rd = &DBReader{
chd: &Chd{},
salt: make([]byte, 16),
fd: fd,
fn: fn,
}
var st os.FileInfo
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)
}
offtbl, err := rd.decodeHeader(hdrb[:], st.Size())
if err != nil {
return nil, err
}
err = rd.verifyChecksum(hdrb[:], offtbl, st.Size())
if err != nil {
return nil, err
}
// All metadata is now verified.
// sanity check - even though we have verified the strong checksum
// 8 + 8 + 4: offset, hashkey, vlen
tblsz := rd.nkeys * (8 + 8 + 4)
if (rd.flags & _DB_KeysOnly) > 0 {
tblsz = rd.nkeys * 8
}
// 64 + 32: 64 bytes of header, 32 bytes of sha trailer
if uint64(st.Size()) < (64 + 32 + tblsz) {
return nil, fmt.Errorf("%s: corrupt header1", fn)
}
rd.cache, err = lru.NewARC(cache)
if err != nil {
return nil, err
}
// Now, we are certain that the header, the offset-table and chd bits are
// all valid and uncorrupted.
// mmap the offset table
mmapsz := st.Size() - int64(offtbl) - 32
bs, err := syscall.Mmap(int(fd.Fd()), int64(offtbl), int(mmapsz), syscall.PROT_READ, syscall.MAP_PRIVATE)
if err != nil {
return nil, fmt.Errorf("%s: can't mmap %d bytes at off %d: %s",
fn, mmapsz, offtbl, err)
}
// if this DB has only keys, then the offtbl is just u64 hash keys
offsz := rd.nkeys * (8 + 8)
vlensz := rd.nkeys * 4
if (rd.flags & _DB_KeysOnly) > 0 {
offsz = rd.nkeys * 8
vlensz = 0
}
rd.mmap = bs
rd.offset = bsToUint64Slice(bs[:offsz])
if vlensz > 0 {
rd.vlen = bsToUint32Slice(bs[offsz : offsz+vlensz])
}
// The CHD table starts here
if err := rd.chd.UnmarshalBinaryMmap(bs[offsz+vlensz:]); err != nil {
return nil, fmt.Errorf("%s: can't unmarshal hash table: %s", fn, err)
}
return rd, nil
}
// TotalKeys returns the total number of distinct keys in the DB
func (rd *DBReader) Len() int {
return int(rd.nkeys)
}
// Close closes the db
func (rd *DBReader) Close() {
syscall.Munmap(rd.mmap)
rd.fd.Close()
rd.cache.Purge()
rd.chd = nil
rd.fd = nil
rd.salt = 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 uint64) ([]byte, bool) {
v, err := rd.Find(key)
if err != nil {
return nil, false
}
return v, true
}
// Dump the metadata to io.Writer 'w'
func (rd *DBReader) DumpMeta(w io.Writer) {
if (rd.flags & _DB_KeysOnly) > 0 {
fmt.Fprintf(w, "CHDB: <KEYS> %d keys, hash-salt %#x, offtbl at %#x\n",
rd.nkeys, rd.salt, rd.offtbl)
rd.chd.DumpMeta(w)
for i := uint64(0); i < rd.nkeys; i++ {
fmt.Fprintf(w, " %3d: %x\n", i, rd.offset[i])
}
} else {
fmt.Fprintf(w, "CHDB: <KEYS+VALS> %d keys, hash-salt %#x, offtbl at %#x\n",
rd.nkeys, rd.salt, rd.offtbl)
rd.chd.DumpMeta(w)
for i := uint64(0); i < rd.nkeys; i++ {
j := i * 2
h := rd.offset[j]
o := rd.offset[j+1]
fmt.Fprintf(w, " %3d: %#x, %d bytes at %#x\n", i, h, rd.vlen[i], o)
}
}
}
// 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 uint64) ([]byte, error) {
if v, ok := rd.cache.Get(key); ok {
return v.([]byte), nil
}
// Not in cache. So, go to disk and find it.
// We are guaranteed that: 0 <= i < rd.nkeys
i := rd.chd.Find(key)
if (rd.flags & _DB_KeysOnly) > 0 {
// offtbl is just the keys; no values.
if hash := toLittleEndianUint64(rd.offset[i]); hash != key {
return nil, ErrNoKey
}
rd.cache.Add(key, nil)
return nil, nil
}
// we have keys _and_ values
j := i * 2
if hash := toLittleEndianUint64(rd.offset[j]); hash != key {
return nil, ErrNoKey
}
var val []byte
var err error
vlen := toLittleEndianUint32(rd.vlen[i])
off := toLittleEndianUint64(rd.offset[j+1])
if val, err = rd.decodeRecord(off, vlen); err != nil {
return nil, err
}
rd.cache.Add(key, val)
return val, 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, vlen uint32) ([]byte, error) {
_, err := rd.fd.Seek(int64(off), 0)
if err != nil {
return nil, err
}
data := make([]byte, vlen+8)
_, err = io.ReadFull(rd.fd, data)
if err != nil {
return nil, err
}
be := binary.BigEndian
csum := be.Uint64(data[:8])
var o [8]byte
be.PutUint64(o[:], off)
h := siphash.New(rd.salt)
h.Write(o[:])
h.Write(data[8:])
exp := h.Sum64()
if csum != exp {
return nil, fmt.Errorf("%s: corrupted record at off %d (exp %#x, saw %#x)", rd.fn, off, exp, csum)
}
return data[8:], nil
}
// Verify checksum of all metadata: offset table, chd bits and the file header.
// We know that offtbl is within the size bounds of the file - see decodeHeader() below.
// sz is the actual file size (includes the header we already read)
func (rd *DBReader) verifyChecksum(hdrb []byte, offtbl uint64, sz int64) error {
h := sha512.New512_256()
h.Write(hdrb[:])
// remsz is the size of the remaining metadata (which begins at offset 'offtbl')
// 32 bytes of SHA512_256 and the values already recorded.
remsz := sz - int64(offtbl) - 32
rd.fd.Seek(int64(offtbl), 0)
nw, err := io.CopyN(h, rd.fd, remsz)
if err != nil {
return fmt.Errorf("%s: metadata i/o error: %s", rd.fn, err)
}
if nw != remsz {
return fmt.Errorf("%s: partial read while verifying checksum, exp %d, saw %d", rd.fn, remsz, 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: checksum 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) (uint64, error) {
if string(b[:4]) != "CHDB" {
return 0, fmt.Errorf("%s: bad file magic", rd.fn)
}
be := binary.BigEndian
i := 4
rd.flags = be.Uint32(b[i : i+4])
i += 4
rd.salt = b[i : i+16]
i += 16
rd.nkeys = be.Uint64(b[i : i+8])
i += 8
rd.offtbl = be.Uint64(b[i : i+8])
if rd.offtbl < 64 || rd.offtbl >= uint64(sz-32) {
return 0, fmt.Errorf("%s: corrupt header0", rd.fn)
}
return rd.offtbl, nil
}