-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.go
174 lines (136 loc) · 3.04 KB
/
index.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
package bitcask
import (
"bytes"
"sync"
"time"
"github.com/spaolacci/murmur3"
)
type IndexHelper interface {
Rand(uint64) uint64
WallTime() time.Time
}
type IndexOperator struct {
helper IndexHelper
}
func (optr *IndexOperator) Hash(key *[]byte) uint64 {
hasher := murmur3.New64()
hasher.Write(*key)
return hasher.Sum64()
}
func (optr *IndexOperator) Equals(lhs, rhs *[]byte) bool {
return bytes.Equal(*lhs, *rhs)
}
func (optr *IndexOperator) Rand(upper uint64) uint64 {
return optr.helper.Rand(upper)
}
func (optr *IndexOperator) WallTime() time.Time {
return optr.helper.WallTime()
}
type IndexValue struct {
fid uint64
valueOff uint64
valueSize uint64
}
type Index struct {
ivPool sync.Pool
maps *ShardMap[[]byte, IndexValue]
}
type IndexOptions struct {
Capacity uint64
Limited uint64
EvictionPoolCapacity uint64
SampleKeys uint64
Helper IndexHelper
}
// TODO: batch optimization
func NewIndex(opts *IndexOptions) (*Index, error) {
mapOpts := &MapOptions{
Capacity: opts.Capacity,
Limited: opts.Limited,
EvictionPoolCapacity: opts.EvictionPoolCapacity,
SampleKeys: opts.SampleKeys,
}
optr := &IndexOperator{
helper: opts.Helper,
}
maps, err := NewShardMap[[]byte, IndexValue](optr, mapOpts)
if err != nil {
return nil, err
}
return &Index{
maps: maps,
ivPool: sync.Pool{
New: func() any {
return &IndexValue{}
},
},
}, nil
}
func (i *Index) Get(ns, key []byte) (fid uint64, off uint64, sz uint64, err error) {
var val *IndexValue
mergedKey := MergedKey(ns, key)
if val, err = i.maps.Get(&mergedKey); err != nil {
return
}
fid = val.fid
off = val.valueOff
sz = val.valueSize
if off == 0 { // invalid offset
err = ErrKeySoftDeleted
}
return
}
type WriteStat struct {
// how much disk space is freed by this write
FreeBytes uint64
// which wal is affected
FreeWalFid uint64
}
func (i *Index) Delete(ns, key []byte, stat *WriteStat) error {
mergedKey := MergedKey(ns, key)
old, err := i.maps.Delete(&mergedKey)
if err != nil {
return err
}
if stat != nil && old != nil {
stat.FreeBytes = old.valueSize
stat.FreeWalFid = old.fid
i.ivPool.Put(old)
}
return nil
}
func (i *Index) SoftDelete(ns, key []byte, stat *WriteStat) error {
mergedKey := MergedKey(ns, key)
old, err := i.maps.Set(&mergedKey, &IndexValue{
valueOff: 0, // invalid offset
})
if err != nil {
return err
}
if stat != nil && old != nil {
stat.FreeBytes = old.valueSize
stat.FreeWalFid = old.fid
i.ivPool.Put(old)
}
return nil
}
func (i *Index) Put(ns, key []byte, fid uint64, off uint64, sz uint64, stat *WriteStat) error {
mergedKey := MergedKey(ns, key)
iv, _ := i.ivPool.Get().(*IndexValue)
iv.fid = fid
iv.valueOff = off
iv.valueSize = sz
old, err := i.maps.Set(&mergedKey, iv)
if err != nil {
return err
}
if stat != nil && old != nil {
stat.FreeBytes = old.valueSize
stat.FreeWalFid = old.fid
i.ivPool.Put(old)
}
return nil
}
func (i *Index) Capacity() uint64 {
return i.maps.Capacity()
}