github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/core/store/wrapper.go (about) 1 package store 2 3 // store/TrackingDirectoryWrapper.java 4 5 import ( 6 "fmt" 7 "sync" 8 ) 9 10 /* 11 A delegating Directory that records which files were written to and deleted. 12 */ 13 type TrackingDirectoryWrapper struct { 14 Directory 15 sync.Locker 16 createdFilenames map[string]bool // synchronized 17 } 18 19 func NewTrackingDirectoryWrapper(other Directory) *TrackingDirectoryWrapper { 20 return &TrackingDirectoryWrapper{ 21 Directory: other, 22 Locker: &sync.Mutex{}, 23 createdFilenames: make(map[string]bool), 24 } 25 } 26 27 func (w *TrackingDirectoryWrapper) DeleteFile(name string) error { 28 func() { 29 w.Lock() 30 defer w.Unlock() 31 delete(w.createdFilenames, name) 32 }() 33 return w.Directory.DeleteFile(name) 34 } 35 36 func (w *TrackingDirectoryWrapper) CreateOutput(name string, ctx IOContext) (IndexOutput, error) { 37 func() { 38 w.Lock() 39 defer w.Unlock() 40 w.createdFilenames[name] = true 41 }() 42 return w.Directory.CreateOutput(name, ctx) 43 } 44 45 func (w *TrackingDirectoryWrapper) String() string { 46 return fmt.Sprintf("TrackingDirectoryWrapper(%v)", w.Directory) 47 } 48 49 func (w *TrackingDirectoryWrapper) Copy(to Directory, src, dest string, ctx IOContext) error { 50 func() { 51 w.Lock() 52 defer w.Unlock() 53 w.createdFilenames[dest] = true 54 }() 55 return w.Directory.Copy(to, src, dest, ctx) 56 } 57 58 func (w *TrackingDirectoryWrapper) EachCreatedFiles(f func(name string)) { 59 w.Lock() 60 defer w.Unlock() 61 for name, _ := range w.createdFilenames { 62 f(name) 63 } 64 } 65 66 func (w *TrackingDirectoryWrapper) ContainsFile(name string) bool { 67 w.Lock() 68 defer w.Unlock() 69 _, ok := w.createdFilenames[name] 70 return ok 71 }