-
Notifications
You must be signed in to change notification settings - Fork 7
/
db.go
163 lines (154 loc) · 3.24 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
package tiny_bitcask
import (
"errors"
"io"
"sort"
"sync"
"tiny-bitcask/entity"
"tiny-bitcask/index"
"tiny-bitcask/storage"
)
var (
KeyNotFoundErr = errors.New("key not found")
NoNeedToMergeErr = errors.New("no need to merge")
)
type DB struct {
rw sync.RWMutex
kd *index.KeyDir
storage *storage.DataFiles
opt *Options
}
// NewDB create a new DB instance with Options
func NewDB(opt *Options) (db *DB, err error) {
db = &DB{}
db.kd = index.NewKD()
db.opt = opt
if isExist, _ := isDirExist(opt.Dir); isExist {
if err := db.recovery(opt); err != nil {
return nil, err
}
return db, nil
}
var fileSize = getSegmentSize(opt.SegmentSize)
db.storage, err = storage.NewDataFiles(opt.Dir, fileSize)
if err != nil {
return nil, err
}
return db, err
}
// Set sets a key-value pairs into DB
func (db *DB) Set(key []byte, value []byte) error {
db.rw.Lock()
defer db.rw.Unlock()
entry := entity.NewEntryWithData(key, value)
h, err := db.storage.WriterEntity(entry)
if err != nil {
return err
}
db.kd.AddIndexByData(h, entry)
return nil
}
// Get gets value by using key
func (db *DB) Get(key []byte) (value []byte, err error) {
db.rw.RLock()
defer db.rw.RUnlock()
i := db.kd.Find(string(key))
if i == nil {
return nil, KeyNotFoundErr
}
entry, err := db.storage.ReadEntry(i)
if err != nil {
return nil, err
}
return entry.Value, nil
}
// Delete delete a key
func (db *DB) Delete(key []byte) error {
db.rw.Lock()
defer db.rw.Unlock()
keyStr := string(key)
index := db.kd.Find(keyStr)
if index == nil {
return KeyNotFoundErr
}
e := entity.NewEntry()
e.Meta.Flag = entity.DeleteFlag
_, err := db.storage.WriterEntity(e)
if err != nil {
return err
}
db.kd.Delete(keyStr)
return nil
}
// Merge clean the useless data
func (db *DB) Merge() error {
db.rw.Lock()
defer db.rw.Unlock()
fids := db.storage.GetOldFiles()
if len(fids) < 2 {
return NoNeedToMergeErr
}
sort.Ints(fids)
for _, fid := range fids[:len(fids)-1] {
var off int64 = 0
reader := db.storage.GetOldFile(fid)
for {
entry, err := reader.ReadEntityWithOutLength(off)
if err == nil {
key := string(entry.Key)
off += entry.Size()
oldIndex := db.kd.Find(key)
if oldIndex == nil {
continue
}
if oldIndex.IsEqualPos(fid, off) {
h, err := db.storage.WriterEntity(entry)
if err != nil {
return err
}
db.kd.AddIndexByData(h, entry)
}
} else {
if err == io.EOF {
break
}
return err
}
}
err := db.storage.RemoveFile(fid)
if err != nil {
return err
}
}
return nil
}
// recovery will rebuild a db from existing dir
func (db *DB) recovery(opt *Options) (err error) {
var fileSize = getSegmentSize(opt.SegmentSize)
db.storage, err = storage.NewDataFileWithFiles(opt.Dir, fileSize)
if err != nil {
return err
}
fids := db.storage.GetOldFiles()
sort.Ints(fids)
for _, fid := range fids {
var off int64 = 0
reader := db.storage.GetOldFile(fid)
for {
entry, err := reader.ReadEntityWithOutLength(off)
if err == nil {
db.kd.AddIndexByRawInfo(fid, off, entry.Key, entry.Value)
off += entry.Size()
} else {
if err == storage.DeleteEntryErr {
continue
}
if err == io.EOF {
break
}
return err
}
}
}
return err
}