-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
device.go
160 lines (138 loc) · 2.98 KB
/
device.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
package levelcache
import (
"encoding/gob"
"fmt"
"os"
"sync"
)
type segKey struct {
Key Hash
Index uint32
}
type segValue struct {
Block int64
Off int64
Size int64
}
type devBucket struct {
metaPath string
lock sync.RWMutex
segments map[Hash]map[uint32]*segValue
blockMap map[int64][]segKey
}
type device struct {
conf *DevConf
buckets [bucketLimit]*devBucket
store *store
}
func newDevice(level int, conf DevConf) *device {
d := &device{conf: &conf, store: newStore(conf.Dir, int64(conf.Capacity))}
for i := 0; i < bucketLimit; i++ {
d.buckets[i] = newDevBucket(conf.Dir, i)
}
return d
}
func newDevBucket(dir string, idx int) *devBucket {
metaPath := fmt.Sprintf("%s/%d-%02d.bkt", dir, version, idx)
bkt := &devBucket{
metaPath: metaPath,
segments: make(map[Hash]map[uint32]*segValue),
blockMap: make(map[int64][]segKey)}
if meta, err := os.Open(metaPath); err != nil {
gob.NewDecoder(meta).Decode(&bkt.segments)
meta.Close()
for k, v := range bkt.segments {
for i, s := range v {
if _, ok := bkt.blockMap[s.Block]; ok {
bkt.blockMap[s.Block] = make([]segKey, 0)
}
bkt.blockMap[s.Block] = append(bkt.blockMap[s.Block], segKey{Key: k, Index: i})
}
}
} else if !os.IsNotExist(err) {
panic(err)
}
return bkt
}
func (d *device) close() {
d.store.close()
d.dump(8)
}
func (d *device) dump(parallel int) {
d.foreachBucket(parallel, func(b *devBucket) {
b.lock.Lock()
defer b.lock.Unlock()
safeDump(b.metaPath, b.segments)
})
}
func (d *device) foreachBucket(parallel int, handler func(b *devBucket)) {
buckets := make(chan int, bucketLimit+parallel)
for i := 0; i < bucketLimit+parallel; i++ {
buckets <- i
}
var wg sync.WaitGroup
wg.Add(parallel)
for i := 0; i < parallel; i++ {
go func() {
defer wg.Done()
for idx := range buckets {
if idx >= bucketLimit {
return
}
b := d.buckets[idx]
handler(b)
}
}()
}
wg.Wait()
}
func (d *device) get(key Hash, seg uint32) []byte {
b := d.getBucket(key)
b.lock.RLock()
defer b.lock.RUnlock()
if s, ok := b.segments[key]; ok {
if v, ok := s[seg]; ok {
return d.store.get(v)
}
}
return nil
}
func (d *device) add(k Hash, seg uint32, data []byte) {
b := d.getBucket(k)
b.lock.Lock()
defer b.lock.Unlock()
block, off := d.store.add(data)
if _, ok := b.segments[k]; !ok {
b.segments[k] = make(map[uint32]*segValue)
}
b.segments[k][seg] = &segValue{
Block: block,
Off: off,
Size: int64(len(data))}
}
func (d *device) del(k Hash) {
b := d.getBucket(k)
b.lock.Lock()
defer b.lock.Unlock()
delete(b.segments, k)
}
func (d *device) delBlock(block int64) {
for i := 0; i < bucketLimit; i++ {
func() {
b := d.buckets[i]
b.lock.Lock()
defer b.lock.Unlock()
for _, k := range b.blockMap[block] {
if segs, ok := b.segments[k.Key]; ok {
delete(segs, k.Index)
if len(segs) == 0 {
delete(b.segments, k.Key)
}
}
}
}()
}
}
func (d *device) getBucket(k Hash) *devBucket {
return d.buckets[k[0]]
}