-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucket.go
105 lines (93 loc) · 2.6 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
package simpledb
import (
"encoding/binary"
)
const (
slotsCountPerBucket = 31
bucketSize = 512 // approximately equals 31*(4+2+2+4+4)+1+8=505
)
// slot corresponds to a single item in the hash table.
type slot struct {
hash uint32
segmentID uint16
keySize uint16
valueSize uint32
offset uint32 // Segment offset.
}
type bucket struct {
slots [slotsCountPerBucket]slot
next int64 // Offset of overflow bucket
}
// bucketHandle is used to link the bucket in disk with bucket in memory
type bucketHandle struct {
*bucket
*MmapFile
offset int64
}
func (b *bucket) MarshalBinary() ([]byte, error) {
buf := make([]byte, bucketSize)
data := buf
for i := 0; i < slotsCountPerBucket; i++ {
sl := b.slots[i]
binary.LittleEndian.PutUint32(buf[:4], sl.hash)
binary.LittleEndian.PutUint16(buf[4:6], sl.segmentID)
binary.LittleEndian.PutUint16(buf[6:8], sl.keySize)
binary.LittleEndian.PutUint32(buf[8:12], sl.valueSize)
binary.LittleEndian.PutUint32(buf[12:16], sl.offset)
buf = buf[16:]
}
binary.LittleEndian.PutUint64(buf[:8], uint64(b.next))
return data, nil
}
func (b *bucket) UnmarshalBinary(data []byte) error {
for i := 0; i < slotsCountPerBucket; i++ {
_ = data[16] // bounds check hint to compiler; see golang.org/issue/14808
b.slots[i].hash = binary.LittleEndian.Uint32(data[:4])
b.slots[i].segmentID = binary.LittleEndian.Uint16(data[4:6])
b.slots[i].keySize = binary.LittleEndian.Uint16(data[6:8])
b.slots[i].valueSize = binary.LittleEndian.Uint32(data[8:12])
b.slots[i].offset = binary.LittleEndian.Uint32(data[12:16])
data = data[16:]
}
b.next = int64(binary.LittleEndian.Uint64(data[:8]))
return nil
}
// read a bucket data from disk to memory
func (b *bucketHandle) read() error {
buf, err := b.MmapFile.ReadRandom(b.offset, bucketSize)
if err != nil {
return err
}
if buf == nil {
return nil
}
return b.UnmarshalBinary(buf)
}
// write a bucket data from memory to disk
func (b *bucketHandle) write() error {
buf, err := b.MarshalBinary()
if err != nil {
return err
}
b.MmapFile.WriteAt(b.offset, buf)
return nil
}
// Iterate the slots in a bucket for a matched hash. It will stop at
// 1) the position of a matched slot (found); 2) the next position of the last slot (not found)
func (b * bucket) iterateSlots(hash uint32) (*slot, int) {
i := 0
for ; i< slotsCountPerBucket; i++ {
if b.slots[i].hash == hash { // found
return &b.slots[i], i
} else if b.slots[i].offset == 0 {
break
}
}
return nil, i
}
func (sl *slot) kvSize() uint32 {
return uint32(sl.keySize) + sl.valueSize
}
func (b *bucket) insert(sl *slot, pos int) {
b.slots[pos] = *sl
}