-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchunk_db.go
256 lines (183 loc) · 4.75 KB
/
chunk_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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package tsuki
import (
"bytes"
"fmt"
"io"
"os"
"path"
"strings"
"sync"
"syscall"
)
const (
ErrChunkExists = ChunkError("chunk already exists")
ErrChunkNotFound = ChunkError("chunk does not exists")
)
type ChunkError string
func (c ChunkError) Error() string { return string(c) }
type ChunkDB interface {
Get(id string) (io.Reader, func(), error)
Create(id string) (io.Writer, func(), error)
Exists(id string) bool
// Should be concurrency safe
Remove(id string) error
BytesAvailable() int
}
type InMemoryChunkStorage struct {
Index map[string]string
Mu sync.RWMutex
accessCount sync.WaitGroup
callsPerformed int
}
func NewInMemoryChunkStorage(index map[string]string) *InMemoryChunkStorage {
return &InMemoryChunkStorage {
Index: index,
}
}
func (s *InMemoryChunkStorage) Get(id string) (io.Reader, func(), error) {
if !s.Exists(id) {
return nil, func(){}, ErrChunkNotFound
}
s.accessCount.Add(1)
s.Mu.RLock()
defer s.Mu.RUnlock()
chunk := s.Index[id]
buf := bytes.NewBufferString(chunk)
closeFunc := func() {
s.accessCount.Done()
}
return buf, closeFunc, nil
}
func (s *InMemoryChunkStorage) Create(id string) (io.Writer, func(), error) {
if s.Exists(id) {
return nil, func(){}, ErrChunkExists
}
s.accessCount.Add(1)
s.Mu.Lock()
defer s.Mu.Unlock()
s.Index[id] = ""
buf := &bytes.Buffer{}
writeChunk := func() {
str := &strings.Builder{}
io.Copy(str, buf)
s.Index[id] = str.String()
s.accessCount.Done()
}
return buf, writeChunk, nil
}
func (s *InMemoryChunkStorage) Exists(id string) (exists bool) {
s.accessCount.Add(1)
defer s.accessCount.Done()
s.Mu.RLock()
defer s.Mu.RUnlock()
_, exists = s.Index[id]
return
}
func (s *InMemoryChunkStorage) Remove(id string) error {
s.accessCount.Add(1)
defer s.accessCount.Done()
s.Mu.Lock()
defer s.Mu.Unlock()
delete(s.Index, id)
return nil
}
func (s *InMemoryChunkStorage) BytesAvailable() int {
return 1024 * 1024 * 10
}
/*
Get(id string) (io.Reader, func(), error)
Create(id string) (io.Writer, func(), error)
Exists(id string) bool
// Should be concurrency safe
Remove(id string) error
BytesAvailable() int
------
Index map[string]string
Mu sync.RWMutex
accessCount sync.WaitGroup
callsPerformed int
*/
type FileSystemChunkStorage struct {
Dir string
index map[string]*sync.RWMutex
mu sync.RWMutex
}
func NewFileSystemChunkStorage(dir string) (*FileSystemChunkStorage, error) {
err := os.RemoveAll(dir)
if err != nil {
return nil, fmt.Errorf("clear storage: %v", err)
}
err = os.Mkdir(dir, 0666)
if err != nil {
return nil, fmt.Errorf("clear storage: %v", err)
}
store := &FileSystemChunkStorage{
Dir: dir,
index: make(map[string]*sync.RWMutex),
}
return store, nil
}
func (s *FileSystemChunkStorage) Create(id string) (io.Writer, func(), error) {
if s.Exists(id) {
return nil, func(){}, fmt.Errorf("create chunk: %s already exists", id)
}
file, err := os.Create(path.Join(s.Dir, id))
if err != nil {
return nil, func(){}, fmt.Errorf("create chunk: %v", err)
}
mu := &sync.RWMutex{}
mu.Lock() // begin
closeChunk := func() {
file.Close()
mu.Unlock() // end
}
s.mu.Lock()
defer s.mu.Unlock()
s.index[id] = mu
return file, closeChunk, nil
}
func (s *FileSystemChunkStorage) Get(id string) (io.Reader, func(), error) {
file, err := os.Open(id)
if err != nil {
return nil, func(){}, fmt.Errorf("get chunk: %v", err)
}
s.mu.RLock()
mu, exists := s.index[id]
s.mu.RUnlock()
if !exists {
file.Close()
return nil, func(){}, fmt.Errorf("get chunk: %s not found", id)
}
mu.RLock() // begin
closeChunk := func() {
file.Close()
mu.RUnlock() // end
}
return file, closeChunk, nil
}
func (s *FileSystemChunkStorage) Exists(id string) bool {
s.mu.RLock()
defer s.mu.RUnlock()
_, exists := s.index[id]
return exists
}
func (s *FileSystemChunkStorage) Remove(id string) error {
s.mu.RLock()
mu, exists := s.index[id]
s.mu.RUnlock()
if !exists {
return fmt.Errorf("remove chunk: %s does not exist", id)
}
mu.Lock()
defer mu.Unlock()
err := os.Remove(path.Join(s.Dir, id))
if err != nil {
return fmt.Errorf("remove chunk: %v", err)
}
return nil
}
func (s *FileSystemChunkStorage) BytesAvailable() int {
var stat syscall.Statfs_t
syscall.Statfs(s.Dir, &stat)
return int(stat.Bavail * uint64(stat.Bsize))
}