forked from akrylysov/pogreb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucket.go
118 lines (102 loc) · 2.5 KB
/
bucket.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
package pogreb
import (
"encoding/binary"
"github.com/akrylysov/pogreb/fs"
)
type slot struct {
hash uint32
keySize uint16
valueSize uint32
kvOffset int64
}
func (sl slot) kvSize() uint32 {
return uint32(sl.keySize) + sl.valueSize
}
type bucket struct {
slots [slotsPerBucket]slot
next int64
}
type bucketHandle struct {
bucket
file fs.MmapFile
offset int64
}
const (
bucketSize uint32 = 512
)
func align512(n uint32) uint32 {
return (n + 511) &^ 511
}
func (b bucket) MarshalBinary() ([]byte, error) {
buf := make([]byte, bucketSize)
data := buf
for i := 0; i < slotsPerBucket; i++ {
sl := b.slots[i]
binary.LittleEndian.PutUint32(buf[:4], sl.hash)
binary.LittleEndian.PutUint16(buf[4:6], sl.keySize)
binary.LittleEndian.PutUint32(buf[6:10], sl.valueSize)
binary.LittleEndian.PutUint64(buf[10:18], uint64(sl.kvOffset))
buf = buf[18:]
}
binary.LittleEndian.PutUint64(buf[:8], uint64(b.next))
return data, nil
}
func (b *bucket) UnmarshalBinary(data []byte) error {
for i := 0; i < slotsPerBucket; i++ {
_ = data[18] // bounds check hint to compiler; see golang.org/issue/14808
b.slots[i].hash = binary.LittleEndian.Uint32(data[:4])
b.slots[i].keySize = binary.LittleEndian.Uint16(data[4:6])
b.slots[i].valueSize = binary.LittleEndian.Uint32(data[6:10])
b.slots[i].kvOffset = int64(binary.LittleEndian.Uint64(data[10:18]))
data = data[18:]
}
b.next = int64(binary.LittleEndian.Uint64(data[:8]))
return nil
}
func (b *bucket) del(slotIdx int) {
i := slotIdx
for ; i < slotsPerBucket-1; i++ {
b.slots[i] = b.slots[i+1]
}
b.slots[i] = slot{}
}
func (b *bucketHandle) read() error {
buf := b.file.Slice(b.offset, b.offset+int64(bucketSize))
return b.UnmarshalBinary(buf)
}
func (b *bucketHandle) write() error {
buf, err := b.MarshalBinary()
if err != nil {
return err
}
_, err = b.file.WriteAt(buf, b.offset)
return err
}
type slotWriter struct {
bucket *bucketHandle
slotIdx int
prevBuckets []*bucketHandle
}
func (sw *slotWriter) insert(sl slot, db *DB) error {
if sw.slotIdx == slotsPerBucket {
nextBucket, err := db.createOverflowBucket()
if err != nil {
return err
}
sw.bucket.next = nextBucket.offset
sw.prevBuckets = append(sw.prevBuckets, sw.bucket)
sw.bucket = nextBucket
sw.slotIdx = 0
}
sw.bucket.slots[sw.slotIdx] = sl
sw.slotIdx++
return nil
}
func (sw *slotWriter) write() error {
for i := len(sw.prevBuckets) - 1; i >= 0; i-- {
if err := sw.prevBuckets[i].write(); err != nil {
return err
}
}
return sw.bucket.write()
}