-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.go
62 lines (51 loc) · 1.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
package bitcask
import (
"errors"
"io"
"os"
"sync"
)
type index struct {
entrys map[string]*entry
mu *sync.RWMutex
}
var (
ErrKeyNotFound = errors.New("key not found")
)
func newIndex() *index {
return &index{
entrys: make(map[string]*entry),
mu: &sync.RWMutex{},
}
}
func (i *index) put(key string, entry *entry) {
i.mu.Lock()
i.entrys[key] = entry
i.mu.Unlock()
}
func (i *index) get(key []byte) (*entry, error) {
i.mu.RLock()
defer i.mu.RUnlock()
if entry, ok := i.entrys[string(key)]; ok {
return entry, nil
}
return nil, ErrKeyNotFound
}
func (i *index) del(key string) {
i.mu.Lock()
delete(i.entrys, key)
i.mu.Unlock()
}
func (i *index) buildFromHint(fid uint32, hintFp *os.File) {
var offset int64 = 0
for {
header, err := decodeHintData(hintFp, offset)
if err != nil && err == io.EOF {
//TODO
break
}
entry := newEntry(fid, header.ksize, header.vsize, uint64(header.valueOffset), header.timestamp)
i.put(string(header.key), entry)
offset += int64(header.ksize) + int64(HintHeaderSize)
}
}