forked from torbit/cdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdb.go
320 lines (302 loc) · 8.38 KB
/
cdb.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
// Package cdb reads and writes cdb ("constant database") files.
//
// See the original cdb specification and C implementation by D. J. Bernstein
// at http://cr.yp.to/cdb.html.
package cdb
import (
"bytes"
"encoding/binary"
"io"
"os"
"runtime"
)
const (
headerSize = uint32(256 * 8)
)
type Cdb struct {
r io.ReaderAt
closer io.Closer
}
type CdbIterator struct {
db *Cdb
// initErr is non-nil if an error happened when the iterator was created.
initErr error
// If it is modified the iterator will stop working properly.
key []byte
// loop is the index of the next value for this iterator.
loop uint32
// khash is the hash of the key.
khash uint32
// kpos is the next file position in the hash to check for the key.
kpos uint32
// hpos is the file position of the hash table that this key is in.
hpos uint32
// hslots is the number of slots in the hash table.
hslots uint32
// dpos is the file position of the data. Only valid if the last call to next
// returned nil.
dpos uint32
// dlen is the length of the data. Only valid if the last call to next
// returned nil.
dlen uint32
// buf is used as scratch space for io.
buf [64]byte
}
// Open opens the named file read-only and returns a new Cdb object. The file
// should exist and be a cdb-format database file.
func Open(name string) (*Cdb, error) {
f, err := os.Open(name)
if err != nil {
return nil, err
}
c := New(f)
c.closer = f
runtime.SetFinalizer(c, (*Cdb).Close)
return c, nil
}
// Close closes the cdb for any further reads.
func (c *Cdb) Close() (err error) {
if c.closer != nil {
err = c.closer.Close()
c.closer = nil
runtime.SetFinalizer(c, nil)
}
return err
}
// New creates a new Cdb from the given ReaderAt, which should be a cdb format database.
func New(r io.ReaderAt) *Cdb {
return &Cdb{r: r}
}
// Exists returns true if there are any values for this key.
//
// Threadsafe.
func (c *Cdb) Exists(key []byte) (bool, error) {
err := c.Iterate(key).next()
if err == io.EOF {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
// Bytes returns the first value for this key as a []byte. Returns EOF when
// there is no value.
//
// Threadsafe.
func (c *Cdb) Bytes(key []byte) ([]byte, error) {
return c.Iterate(key).NextBytes()
}
// Reader returns the first value for this key as an io.SectionReader. Returns
// EOF when there is no value.
//
// Threadsafe.
func (c *Cdb) Reader(key []byte) (*io.SectionReader, error) {
return c.Iterate(key).NextReader()
}
// Iterate returns an iterator that can be used to access all of the values for
// a key. Always returns a non-nil value, even if the key has no values.
//
// Because the iterator keeps a reference to the byte slice, it shouldn't be
// modified until the iterator is no longer in use.
//
// Threadsafe.
func (c *Cdb) Iterate(key []byte) *CdbIterator {
iter := new(CdbIterator)
iter.db = c
iter.key = key
// Calculate the hash of the key.
iter.khash = checksum(key)
// Read in the position and size of the hash table for this key.
iter.hpos, iter.hslots, iter.initErr = readNums(iter.db.r, iter.buf[:], iter.khash%256*8)
if iter.initErr != nil {
return iter
}
// If the hash table has no slots, there are no values.
if iter.hslots == 0 {
iter.initErr = io.EOF
return iter
}
// Calculate first possible file position of key.
hashslot := iter.khash / 256 % iter.hslots
iter.kpos = iter.hpos + hashslot*8
return iter
}
// NextBytes returns the next value for this iterator as a []byte. Returns EOF
// when there are no values left.
//
// Not threadsafe.
func (iter *CdbIterator) NextBytes() ([]byte, error) {
if err := iter.next(); err != nil {
return nil, err
}
data := make([]byte, iter.dlen)
if _, err := iter.db.r.ReadAt(data, int64(iter.dpos)); err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return nil, err
}
return data, nil
}
// NextReader returns the next value for this iterator as an io.SectionReader.
// Returns EOF when there are no values left.
//
// Not threadsafe.
func (iter *CdbIterator) NextReader() (*io.SectionReader, error) {
if err := iter.next(); err != nil {
return nil, err
}
return io.NewSectionReader(iter.db.r, int64(iter.dpos), int64(iter.dlen)), nil
}
// next iterates through the hash table until it finds the next match. If no
// matches are found, returns EOF.
//
// When a match is found dpos and dlen can be used to retreive the data.
func (iter *CdbIterator) next() error {
if iter.initErr != nil {
return iter.initErr
}
var err error
var khash, recPos uint32
// Iterate through all of the hash slots until we find our key.
for {
// If we have seen every hash slot, we are done.
if iter.loop >= iter.hslots {
return io.EOF
}
khash, recPos, err = readNums(iter.db.r, iter.buf[:], iter.kpos)
if err != nil {
return err
}
if recPos == 0 {
return io.EOF
}
// Move the iterator to the next position.
iter.loop++
iter.kpos += 8
// If the kpos goes past the end of the hash table, wrap around to the start.
if iter.kpos == iter.hpos+(iter.hslots*8) {
iter.kpos = iter.hpos
}
// If the key hash doesn't match, this hash slot isn't for our key. Keep iterating.
if khash != iter.khash {
continue
}
keyLen, dataLen, err := readNums(iter.db.r, iter.buf[:], recPos)
if err != nil {
return err
}
// Check that the keys actually match in case of a hash collision.
if keyLen != uint32(len(iter.key)) {
continue
}
if isMatch, err := match(iter.db.r, iter.buf[:], iter.key, recPos+8); err != nil {
return err
} else if isMatch == false {
continue
}
iter.dpos = recPos + 8 + keyLen
iter.dlen = dataLen
return nil
}
panic("unreached")
}
// ForEachReader calls onRecordFn for every key-val pair in the database.
//
// If onRecordFn returns an error, iteration will stop and the error will be
// returned.
//
// Threadsafe.
func (c *Cdb) ForEachReader(onRecordFn func(keyReader, valReader *io.SectionReader) error) error {
buf := make([]byte, 8)
// The start is the first record after the header.
pos := headerSize
// The end is the start of the first hash table.
end, _, err := readNums(c.r, buf, 0)
if err != nil {
return err
}
for pos < end {
klen, dlen, err := readNums(c.r, buf, pos)
if err != nil {
return err
}
// Create readers that point directly to sections of the underlying reader.
keyReader := io.NewSectionReader(c.r, int64(pos+8), int64(klen))
dataReader := io.NewSectionReader(c.r, int64(pos+8+klen), int64(dlen))
// Send them to the callback.
if err := onRecordFn(keyReader, dataReader); err != nil {
return err
}
// Move to the next record.
pos += 8 + klen + dlen
}
return nil
}
// ForEachBytes calls onRecordFn for every key-val pair in the database.
//
// The byte slices are only valid for the length of a call to onRecordFn.
//
// If onRecordFn returns an error, iteration will stop and the error will be
// returned.
//
// Threadsafe.
func (c *Cdb) ForEachBytes(onRecordFn func(key, val []byte) error) error {
var kbuf, dbuf []byte
return c.ForEachReader(func(keyReader, valReader *io.SectionReader) error {
// Correctly size the buffers.
klen, dlen := keyReader.Size(), valReader.Size()
if int64(cap(kbuf)) < klen {
kbuf = make([]byte, klen)
}
if int64(cap(dbuf)) < dlen {
dbuf = make([]byte, dlen)
}
kbuf, dbuf = kbuf[:klen], dbuf[:dlen]
// Read in the bytes.
if _, err := io.ReadFull(keyReader, kbuf); err != nil {
return err
}
if _, err := io.ReadFull(valReader, dbuf); err != nil {
return err
}
// Send them to the callback.
if err := onRecordFn(kbuf, dbuf); err != nil {
return err
}
return nil
})
}
// match returns true if the data at file position pos matches key.
func match(r io.ReaderAt, buf []byte, key []byte, pos uint32) (bool, error) {
klen := len(key)
for n := 0; n < klen; n += len(buf) {
nleft := klen - n
if len(buf) > nleft {
buf = buf[:nleft]
}
if _, err := r.ReadAt(buf, int64(pos)); err != nil {
return false, err
}
if !bytes.Equal(buf, key[n:n+len(buf)]) {
return false, nil
}
pos += uint32(len(buf))
}
return true, nil
}
func readNums(r io.ReaderAt, buf []byte, pos uint32) (uint32, uint32, error) {
n, err := r.ReadAt(buf[:8], int64(pos))
// Ignore EOFs when we have read the full 8 bytes.
if err == io.EOF && n == 8 {
err = nil
}
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
if err != nil {
return 0, 0, err
}
return binary.LittleEndian.Uint32(buf[:4]), binary.LittleEndian.Uint32(buf[4:8]), nil
}