This repository has been archived by the owner on Jun 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
keydir.go
85 lines (69 loc) · 1.48 KB
/
keydir.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
/*
Impletation of keydir
| --- | | --------------------------------------------------------------------------|
| key | --> | file id (int32) | value size (int32) | value pos (int32) | Tstamp (int64) |
| --- | | --------------------------------------------------------------------------|
*/
package bitcask_go
import (
"sync"
)
type Item struct {
Fid int32
Vsz int32
Vpos int32
Tstamp int64
}
// Keydir is a index data structure for bitcask
// It wrap for hashmap(builtin go)
// It is safe to call add, remove, get concurrently.
type Keydir struct {
sync.RWMutex
kv map[string]Item
}
func NewKeydir() *Keydir {
return &Keydir{
kv: make(map[string]Item),
}
}
func (k *Keydir) Add(key string, Fid, Vsz, Vpos int32, Tstamp int64) error {
k.Lock()
defer k.Unlock()
k.kv[key] = Item{Fid, Vsz, Vpos, Tstamp}
return nil
}
func (k *Keydir) Get(key string) (*Item, bool) {
k.RLock()
defer k.RUnlock()
v, b := k.kv[key]
return &v, b
}
func (k *Keydir) Remove(key string) {
k.Lock()
defer k.Unlock()
delete(k.kv, key)
}
func (k *Keydir) Keys() chan string {
ch := make(chan string)
go func() {
for k, _ := range k.kv {
ch <- k
}
close(ch)
}()
return ch
}
func (k *Keydir) Destroy() {
}
//const N int = 10000000
//
//func main() {
// kv := NewKeydir()
// t0 := time.Now()
// for i := 0; i < N; i++ {
// kv.add(string(i), 1, 1, 1, 1)
// }
// t1 := time.Now()
// fmt.Printf("%f ops/sec\n", float64(N)/t1.Sub(t0).Seconds())
//}
//950575.158332 ops/sec