-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdb.go
197 lines (151 loc) · 3.76 KB
/
db.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
package bitcask
import (
"errors"
"fmt"
"path/filepath"
"sort"
)
const (
WalFileSuffix = ".wal"
HintFileSuffix = ".hint"
MergeFileSuffix = ".merge"
TmpFileSuffix = ".tmp"
LockFile = "LOCK"
ManifestFilePrefix = "MANIFEST"
CurrentFile = "CURRENT"
)
const (
UnknownFileType = iota
WalFileType
HintFileType
MergeFileType
TmpFileType
LockFileType
ManifestFileType
CurrentFileType
)
var (
ErrKeyNotFound = errors.New("key not found")
ErrKeySoftDeleted = errors.New("key soft delete")
)
type WriteOptions struct {
Sync bool
}
type ReadOptions struct {
VerifyChecksum bool
}
type PickerWalInfo struct {
CreateTime uint64
FreeBytes uint64
WalSize uint64
Fid uint64
}
type (
CompactionPicker func([]PickerWalInfo) []uint64
CompactionFilter func(ns, key, val []byte, meta *Meta) bool
)
type Options struct {
Dir string
WalMaxSize uint64
ManifestMaxSize uint64
IndexCapacity uint64
IndexLimited uint64
IndexEvictionPoolCapacity uint64
IndexSampleKeys uint64
CompactionPicker CompactionPicker
CompactionFilter CompactionFilter
DiskUsageLimited uint64
NsSize uint64
EtagSize uint64
CompactionTriggerInterval uint64
CheckDiskUsageInterval uint64
CompactionPickerRatio float64
DisableCompaction bool
RecordBufferSize uint64
}
func (o *Options) Init() {
if o.CompactionPicker == nil {
o.CompactionPicker = DefaultCompactionPicker
}
if o.CompactionTriggerInterval <= 0 {
o.CompactionTriggerInterval = DefaultCompactionTriggerInterval
}
if o.CheckDiskUsageInterval <= 0 {
o.CheckDiskUsageInterval = DefaultCheckDiskUsageInterval
}
if o.CompactionPickerRatio <= 0 {
o.CompactionPickerRatio = DefaultCompactionPickerRatio
}
if o.RecordBufferSize <= 0 {
o.RecordBufferSize = DefaultRecordBufferSize
}
gOpts = o
}
var gOpts *Options
// read-only
func GetOptions() *Options {
return gOpts
}
type DB interface {
Get(ns, key []byte, opts *ReadOptions) (val []byte, meta *Meta, err error)
Put(ns, key, val []byte, meta *Meta, opts *WriteOptions) error
Write(batch *Batch, opts *WriteOptions) error
Delete(ns, key []byte, opts *WriteOptions) error
Close()
}
func TmpFilename(fid uint64) string {
return fmt.Sprintf("%06d%s", fid, TmpFileSuffix)
}
func WalFilename(fid uint64) string {
return fmt.Sprintf("%06d%s", fid, WalFileSuffix)
}
func HintFilename(fid uint64) string {
return fmt.Sprintf("%06d%s", fid, HintFileSuffix)
}
func MergeFilename(fid uint64) string {
return fmt.Sprintf("%06d%s", fid, MergeFileSuffix)
}
func ManifestFilename(fid uint64) string {
return fmt.Sprintf("%s-%06d", ManifestFilePrefix, fid)
}
func TmpPath(dir string, fid uint64) string {
return filepath.Join(dir, TmpFilename(fid))
}
func WalPath(dir string, fid uint64) string {
return filepath.Join(dir, WalFilename(fid))
}
func HintPath(dir string, fid uint64) string {
return filepath.Join(dir, HintFilename(fid))
}
func ManifestPath(dir string, fid uint64) string {
return filepath.Join(dir, ManifestFilename(fid))
}
func MergePath(dir string, fid uint64) string {
return filepath.Join(dir, MergeFilename(fid))
}
func LockPath(dir string) string {
return filepath.Join(dir, LockFile)
}
func CurrentPath(dir string) string {
return filepath.Join(dir, CurrentFile)
}
func DefaultCompactionPicker(wals []PickerWalInfo) []uint64 {
compactionPickerRatio := GetOptions().CompactionPickerRatio
// reverse order
sort.Slice(wals, func(i, j int) bool {
return wals[i].FreeBytes > wals[j].FreeBytes
})
var res []uint64
for idx := range wals {
size := float64(wals[idx].WalSize)
free := float64(wals[idx].FreeBytes)
if free/size < compactionPickerRatio {
break
}
res = append(res, wals[idx].Fid)
if len(res) >= 2 {
break
}
}
return res
}