/
wrapper.go
71 lines (61 loc) · 1.53 KB
/
wrapper.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
package store
// store/TrackingDirectoryWrapper.java
import (
"fmt"
"sync"
)
/*
A delegating Directory that records which files were written to and deleted.
*/
type TrackingDirectoryWrapper struct {
Directory
sync.Locker
createdFilenames map[string]bool // synchronized
}
func NewTrackingDirectoryWrapper(other Directory) *TrackingDirectoryWrapper {
return &TrackingDirectoryWrapper{
Directory: other,
Locker: &sync.Mutex{},
createdFilenames: make(map[string]bool),
}
}
func (w *TrackingDirectoryWrapper) DeleteFile(name string) error {
func() {
w.Lock()
defer w.Unlock()
delete(w.createdFilenames, name)
}()
return w.Directory.DeleteFile(name)
}
func (w *TrackingDirectoryWrapper) CreateOutput(name string, ctx IOContext) (IndexOutput, error) {
func() {
w.Lock()
defer w.Unlock()
w.createdFilenames[name] = true
}()
return w.Directory.CreateOutput(name, ctx)
}
func (w *TrackingDirectoryWrapper) String() string {
return fmt.Sprintf("TrackingDirectoryWrapper(%v)", w.Directory)
}
func (w *TrackingDirectoryWrapper) Copy(to Directory, src, dest string, ctx IOContext) error {
func() {
w.Lock()
defer w.Unlock()
w.createdFilenames[dest] = true
}()
return w.Directory.Copy(to, src, dest, ctx)
}
func (w *TrackingDirectoryWrapper) EachCreatedFiles(f func(name string)) {
w.Lock()
defer w.Unlock()
for name, _ := range w.createdFilenames {
f(name)
}
}
func (w *TrackingDirectoryWrapper) ContainsFile(name string) bool {
w.Lock()
defer w.Unlock()
_, ok := w.createdFilenames[name]
return ok
}