forked from miku/microblob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend_posix.go
47 lines (37 loc) · 970 Bytes
/
backend_posix.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
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
package microblob
import (
"bytes"
"encoding/binary"
"fmt"
"syscall"
)
// Get retrieves the data for a given key, using pread(2).
func (b *LevelDBBackend) Get(key string) (data []byte, err error) {
if err = b.openDatabase(); err != nil {
return nil, err
}
var value []byte
var offset, length int64
if value, err = b.db.Get([]byte(key), nil); err != nil {
return nil, err
}
if len(value) < 16 {
return nil, ErrInvalidValue
}
if offset, err = binary.ReadVarint(bytes.NewBuffer(value[:8])); err != nil {
return nil, err
}
if length, err = binary.ReadVarint(bytes.NewBuffer(value[8:])); err != nil {
return nil, err
}
if err = b.openBlob(); err != nil {
return nil, err
}
data = make([]byte, length)
_, err = syscall.Pread(int(b.blob.Fd()), data, offset)
if !b.AllowEmptyValues && IsAllZero(data) {
return nil, fmt.Errorf("empty value")
}
return data, err
}