-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcdb.go
212 lines (177 loc) · 4.5 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
// Copyright 2013 Patrick Higgins. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cdb
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"log"
"os"
"os/exec"
"runtime"
"strings"
"syscall"
)
const slotWidth = 8
const headerWidth = 8
const nHeaders = 256
const headerSize = nHeaders * headerWidth
// CDB is an open constant database file
type CDB struct {
header [nHeaders]tablePointer
data []byte
}
type tablePointer struct {
pos uint32
nslots uint32
}
var errShortFile = errors.New("file is too short for a CDB")
// Create writes a new CDB file to outfile. The file is first written to a
// temp file and atomically renamed. The external program "cdb" must be
// in your PATH.
//
// The creator callback should write records created with AppendRecord to
// the provided io.Writer.
func Create(outfile string, creator func(io.Writer) error) error {
cdbCmd := exec.Command("cdb", "-c", outfile)
cdbPipe, err := cdbCmd.StdinPipe()
if err != nil {
return nil
}
err = cdbCmd.Start()
if err != nil {
return err
}
var errs []string
err = creator(cdbPipe)
if err != nil {
errs = append(errs, err.Error())
}
// complete the CDB records
_, err = cdbPipe.Write([]byte("\n"))
if err != nil {
return err
}
err = cdbPipe.Close()
if err != nil {
errs = append(errs, "cdb close: "+err.Error())
}
err = cdbCmd.Wait()
if err != nil {
errs = append(errs, "cdb wait: "+err.Error())
}
if len(errs) > 0 {
return errors.New(strings.Join(errs, "\n"))
}
return nil
}
// Open creates and returns a CDB from file.
func Open(file string) (*CDB, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
// it is OK to close a file after mapping it
defer f.Close()
fi, err := f.Stat()
if err != nil {
return nil, err
}
if fi.Size() == 0 {
// an empty CDB is valid, but never finds any data
// allowing this means /dev/null can be specified for data files that
// we want to be empty.
return &CDB{}, nil
}
if fi.Size() < headerSize {
return nil, errShortFile
}
data, err := syscall.Mmap(int(f.Fd()), 0, int(fi.Size()),
syscall.PROT_READ, syscall.MAP_PRIVATE)
if err != nil {
return nil, err
}
db := &CDB{
data: data,
}
// munmap when the reference is released
runtime.SetFinalizer(db, (*CDB).Close)
db.readHeader(data)
return db, nil
}
// Close releases resources associated with this CDB.
func (db *CDB) Close() error {
var err error
if db.data != nil {
err = syscall.Munmap(db.data)
}
db.data = nil
return err
}
var ErrNotFound = errors.New("key not found")
// Data returns the data associated with key. A shared copy of the data is
// returned and must not be modified. A non-recoverable panic will result
// if the data is written to.
// If not found, returns ErrNotFound.
func (db *CDB) Data(key []byte) (val []byte, err error) {
// catch array range checks, etc.
defer func() {
if val := recover(); val != nil {
const size = 4096
buf := make([]byte, size)
buf = buf[:runtime.Stack(buf, false)]
log.Printf("cdb: panic finding %v: %v\n%s", key, val, buf)
// set named return value
if perr, ok := val.(error); ok {
err = perr
} else {
err = fmt.Errorf("cdb: panic in find: %v", val)
}
}
}()
hashcode := uint32(5381)
for _, c := range key {
hashcode = ((hashcode << 5) + hashcode) ^ uint32(c)
}
header := db.header[hashcode&0xff]
if header.nslots == 0 {
return nil, ErrNotFound
}
table := db.data[header.pos : header.pos+header.nslots*slotWidth]
slot := ((hashcode >> 8) % header.nslots) * slotWidth
// prevent endless loops if no slots are empty
for i := uint32(0); i < header.nslots; i++ {
hash := binary.LittleEndian.Uint32(table[slot:])
pos := binary.LittleEndian.Uint32(table[slot+4:])
if pos == 0 {
return nil, ErrNotFound
}
if hash == hashcode {
record := db.data[pos:]
keyLen := binary.LittleEndian.Uint32(record)
dataLen := binary.LittleEndian.Uint32(record[4:])
recKey := record[8 : 8+keyLen]
if bytes.Equal(key, recKey) {
data := record[8+keyLen : 8+keyLen+dataLen]
return data, nil
}
}
// search next slot, wrapping around
slot += slotWidth
if slot >= uint32(len(table)) {
slot = 0
}
}
return nil, ErrNotFound
}
// readHeader populates db.header from data
func (db *CDB) readHeader(data []byte) {
for i := range db.header {
db.header[i].pos = binary.LittleEndian.Uint32(data)
db.header[i].nslots = binary.LittleEndian.Uint32(data[4:])
data = data[headerWidth:]
}
}