@@ -9,52 +9,49 @@ import (
9
9
10
10
const initFileListLength = 5000
11
11
12
+ type filesBucket struct {
13
+ lock * sync.RWMutex
14
+ files map [string ]bool
15
+ }
16
+
12
17
type InMemoryFileHandler struct {
13
- mutex sync.RWMutex
14
- m map [string ]* sync.RWMutex
15
- files map [string ]map [string ]bool
18
+ mutex sync.RWMutex
19
+ buckets map [string ]* filesBucket
16
20
}
17
21
18
22
var _ filehandler.FileHandler = (* InMemoryFileHandler )(nil )
19
23
20
24
func CreateInMemoryFileHandler () (* InMemoryFileHandler , error ) {
21
25
return & InMemoryFileHandler {
22
- m : make (map [string ]* sync.RWMutex ),
23
- files : make (map [string ]map [string ]bool , 20 ),
26
+ buckets : make (map [string ]* filesBucket , 20 ),
24
27
}, nil
25
28
}
26
29
27
30
func (s * InMemoryFileHandler ) AddFile (ctx context.Context , bucket , file string ) error {
28
-
29
31
// Acquire a read lock first
30
32
s .mutex .RLock ()
31
- bucketLock , ok := s .m [bucket ]
32
- bucketFiles , okF := s .files [bucket ]
33
+ bucketFiles , ok := s .buckets [bucket ]
33
34
s .mutex .RUnlock ()
34
35
35
36
// If the bucket doesn't exist, acquire a write lock to create the new bucket
36
- if ! ok || ! okF {
37
+ if ! ok {
37
38
s .mutex .Lock ()
38
39
// Double-check the bucket's existence to ensure another goroutine didn't already create it
39
- bucketLock , ok = s .m [bucket ]
40
+ bucketFiles , ok = s .buckets [bucket ]
40
41
if ! ok {
41
- bucketLock = & sync.RWMutex {}
42
- s .m [bucket ] = bucketLock
43
- }
44
-
45
- bucketFiles , okF = s .files [bucket ]
46
- if ! okF {
47
- bucketFiles = make (map [string ]bool , initFileListLength )
48
- s .files [bucket ] = bucketFiles
42
+ bucketFiles = & filesBucket {
43
+ lock : & sync.RWMutex {},
44
+ files : make (map [string ]bool , initFileListLength ),
45
+ }
46
+ s .buckets [bucket ] = bucketFiles
49
47
}
50
48
s .mutex .Unlock ()
51
49
}
52
50
53
51
// Acquire a write lock if the bucket already exists
54
- bucketLock .Lock ()
55
- defer bucketLock .Unlock ()
56
-
57
- bucketFiles [file ] = true
52
+ bucketFiles .lock .Lock ()
53
+ bucketFiles .files [file ] = true
54
+ bucketFiles .lock .Unlock ()
58
55
59
56
return nil
60
57
}
@@ -76,30 +73,22 @@ func shallowCopyMapStringBool(m map[string]bool) map[string]bool {
76
73
77
74
func (s * InMemoryFileHandler ) GetFiles (ctx context.Context , bucket string ) (map [string ]bool , error ) {
78
75
s .mutex .RLock ()
79
- bucketLock , ok := s .m [bucket ]
80
- bucketFiles , okFiles := s .files [bucket ]
76
+ bucketFiles , ok := s .buckets [bucket ]
81
77
s .mutex .RUnlock ()
82
78
83
- if ! ok || ! okFiles {
79
+ if ! ok {
84
80
return map [string ]bool {}, fmt .Errorf ("bucket does not exist for container %s" , bucket )
85
81
}
86
82
87
- bucketLock .RLock ()
88
- defer bucketLock .RUnlock ()
83
+ bucketFiles .lock .RLock ()
84
+ copy := shallowCopyMapStringBool (bucketFiles .files )
85
+ bucketFiles .lock .RUnlock ()
89
86
90
- return shallowCopyMapStringBool ( bucketFiles ) , nil
87
+ return copy , nil
91
88
}
92
89
func (s * InMemoryFileHandler ) RemoveBucket (ctx context.Context , bucket string ) error {
93
-
94
90
s .mutex .Lock ()
95
- bucketLock , ok := s .m [bucket ]
96
- if ok {
97
- bucketLock .Lock ()
98
- defer bucketLock .Unlock ()
99
- }
100
-
101
- delete (s .m , bucket )
102
- delete (s .files , bucket )
91
+ delete (s .buckets , bucket )
103
92
s .mutex .Unlock ()
104
93
105
94
return nil
0 commit comments