8
8
import java .util .List ;
9
9
import java .util .Map ;
10
10
import java .util .Set ;
11
- import java .util .concurrent .locks .ReentrantLock ;
12
11
import java .util .function .Function ;
13
12
import org .apache .commons .collections4 .CollectionUtils ;
14
13
import org .apache .commons .collections4 .MapUtils ;
@@ -32,10 +31,6 @@ public class Cache<ApiType extends KubernetesObject> implements Indexer<ApiType>
32
31
/** indices stores objects' keys by their indices */
33
32
private Map <String , Map <String , Set <String >>> indices = new HashMap <>();
34
33
35
- /** lock provides thread-safety */
36
- // TODO: might remove the lock here and make the methods synchronized
37
- private ReentrantLock lock = new ReentrantLock ();
38
-
39
34
public Cache () {
40
35
this (
41
36
Caches .NAMESPACE_INDEX ,
@@ -67,13 +62,10 @@ public Cache(
67
62
@ Override
68
63
public void add (ApiType obj ) {
69
64
String key = keyFunc .apply (obj );
70
- lock .lock ();
71
- try {
65
+ synchronized (this ) {
72
66
ApiType oldObj = this .items .get (key );
73
67
this .items .put (key , obj );
74
68
this .updateIndices (oldObj , obj , key );
75
- } finally {
76
- lock .unlock ();
77
69
}
78
70
}
79
71
@@ -85,13 +77,10 @@ public void add(ApiType obj) {
85
77
@ Override
86
78
public void update (ApiType obj ) {
87
79
String key = keyFunc .apply (obj );
88
- lock .lock ();
89
- try {
80
+ synchronized (this ) {
90
81
ApiType oldObj = this .items .get (key );
91
82
this .items .put (key , obj );
92
83
updateIndices (oldObj , obj , key );
93
- } finally {
94
- lock .unlock ();
95
84
}
96
85
}
97
86
@@ -103,15 +92,12 @@ public void update(ApiType obj) {
103
92
@ Override
104
93
public void delete (ApiType obj ) {
105
94
String key = keyFunc .apply (obj );
106
- lock .lock ();
107
- try {
95
+ synchronized (this ) {
108
96
boolean exists = this .items .containsKey (key );
109
97
if (exists ) {
110
98
this .deleteFromIndices (this .items .get (key ), key );
111
99
this .items .remove (key );
112
100
}
113
- } finally {
114
- lock .unlock ();
115
101
}
116
102
}
117
103
@@ -122,23 +108,18 @@ public void delete(ApiType obj) {
122
108
* @param resourceVersion the resource version
123
109
*/
124
110
@ Override
125
- public void replace (List <ApiType > list , String resourceVersion ) {
126
- lock .lock ();
127
- try {
128
- Map <String , ApiType > newItems = new HashMap <>();
129
- for (ApiType item : list ) {
130
- String key = keyFunc .apply (item );
131
- newItems .put (key , item );
132
- }
133
- this .items = newItems ;
111
+ public synchronized void replace (List <ApiType > list , String resourceVersion ) {
112
+ Map <String , ApiType > newItems = new HashMap <>();
113
+ for (ApiType item : list ) {
114
+ String key = keyFunc .apply (item );
115
+ newItems .put (key , item );
116
+ }
117
+ this .items = newItems ;
134
118
135
- // rebuild any index
136
- this .indices = new HashMap <>();
137
- for (Map .Entry <String , ApiType > itemEntry : items .entrySet ()) {
138
- this .updateIndices (null , itemEntry .getValue (), itemEntry .getKey ());
139
- }
140
- } finally {
141
- lock .unlock ();
119
+ // rebuild any index
120
+ this .indices = new HashMap <>();
121
+ for (Map .Entry <String , ApiType > itemEntry : items .entrySet ()) {
122
+ this .updateIndices (null , itemEntry .getValue (), itemEntry .getKey ());
142
123
}
143
124
}
144
125
@@ -154,17 +135,12 @@ public void resync() {
154
135
* @return the list
155
136
*/
156
137
@ Override
157
- public List <String > listKeys () {
158
- lock .lock ();
159
- try {
160
- List <String > keys = new ArrayList <>(this .items .size ());
161
- for (Map .Entry <String , ApiType > entry : this .items .entrySet ()) {
162
- keys .add (entry .getKey ());
163
- }
164
- return keys ;
165
- } finally {
166
- lock .unlock ();
138
+ public synchronized List <String > listKeys () {
139
+ List <String > keys = new ArrayList <>(this .items .size ());
140
+ for (Map .Entry <String , ApiType > entry : this .items .entrySet ()) {
141
+ keys .add (entry .getKey ());
167
142
}
143
+ return keys ;
168
144
}
169
145
170
146
/**
@@ -176,11 +152,8 @@ public List<String> listKeys() {
176
152
@ Override
177
153
public ApiType get (ApiType obj ) {
178
154
String key = this .keyFunc .apply (obj );
179
- lock .lock ();
180
- try {
155
+ synchronized (this ) {
181
156
return this .getByKey (key );
182
- } finally {
183
- lock .unlock ();
184
157
}
185
158
}
186
159
@@ -190,17 +163,12 @@ public ApiType get(ApiType obj) {
190
163
* @return the list
191
164
*/
192
165
@ Override
193
- public List <ApiType > list () {
194
- lock .lock ();
195
- try {
196
- List <ApiType > itemList = new ArrayList <>(this .items .size ());
197
- for (Map .Entry <String , ApiType > entry : this .items .entrySet ()) {
198
- itemList .add (entry .getValue ());
199
- }
200
- return itemList ;
201
- } finally {
202
- lock .unlock ();
166
+ public synchronized List <ApiType > list () {
167
+ List <ApiType > itemList = new ArrayList <>(this .items .size ());
168
+ for (Map .Entry <String , ApiType > entry : this .items .entrySet ()) {
169
+ itemList .add (entry .getValue ());
203
170
}
171
+ return itemList ;
204
172
}
205
173
206
174
/**
@@ -210,13 +178,8 @@ public List<ApiType> list() {
210
178
* @return the get by key
211
179
*/
212
180
@ Override
213
- public ApiType getByKey (String key ) {
214
- lock .lock ();
215
- try {
216
- return this .items .get (key );
217
- } finally {
218
- lock .unlock ();
219
- }
181
+ public synchronized ApiType getByKey (String key ) {
182
+ return this .items .get (key );
220
183
}
221
184
222
185
/**
@@ -227,35 +190,30 @@ public ApiType getByKey(String key) {
227
190
* @return the list
228
191
*/
229
192
@ Override
230
- public List <ApiType > index (String indexName , ApiType obj ) {
231
- lock .lock ();
232
- try {
233
- if (!this .indexers .containsKey (indexName )) {
234
- throw new IllegalArgumentException (String .format ("index %s doesn't exist!" , indexName ));
235
- }
236
- Function <ApiType , List <String >> indexFunc = this .indexers .get (indexName );
237
- List <String > indexKeys = indexFunc .apply ((ApiType ) obj );
238
- Map <String , Set <String >> index = this .indices .get (indexName );
239
- if (MapUtils .isEmpty (index )) {
240
- return new ArrayList <>();
241
- }
242
- Set <String > returnKeySet = new HashSet <>();
243
- for (String indexKey : indexKeys ) {
244
- Set <String > set = index .get (indexKey );
245
- if (CollectionUtils .isEmpty (set )) {
246
- continue ;
247
- }
248
- returnKeySet .addAll (set );
193
+ public synchronized List <ApiType > index (String indexName , ApiType obj ) {
194
+ if (!this .indexers .containsKey (indexName )) {
195
+ throw new IllegalArgumentException (String .format ("index %s doesn't exist!" , indexName ));
196
+ }
197
+ Function <ApiType , List <String >> indexFunc = this .indexers .get (indexName );
198
+ List <String > indexKeys = indexFunc .apply ((ApiType ) obj );
199
+ Map <String , Set <String >> index = this .indices .get (indexName );
200
+ if (MapUtils .isEmpty (index )) {
201
+ return new ArrayList <>();
202
+ }
203
+ Set <String > returnKeySet = new HashSet <>();
204
+ for (String indexKey : indexKeys ) {
205
+ Set <String > set = index .get (indexKey );
206
+ if (CollectionUtils .isEmpty (set )) {
207
+ continue ;
249
208
}
209
+ returnKeySet .addAll (set );
210
+ }
250
211
251
- List <ApiType > items = new ArrayList <>(returnKeySet .size ());
252
- for (String absoluteKey : returnKeySet ) {
253
- items .add (this .items .get (absoluteKey ));
254
- }
255
- return items ;
256
- } finally {
257
- lock .unlock ();
212
+ List <ApiType > items = new ArrayList <>(returnKeySet .size ());
213
+ for (String absoluteKey : returnKeySet ) {
214
+ items .add (this .items .get (absoluteKey ));
258
215
}
216
+ return items ;
259
217
}
260
218
261
219
/**
@@ -266,22 +224,17 @@ public List<ApiType> index(String indexName, ApiType obj) {
266
224
* @return the list
267
225
*/
268
226
@ Override
269
- public List <String > indexKeys (String indexName , String indexKey ) {
270
- lock .lock ();
271
- try {
272
- if (!this .indexers .containsKey (indexName )) {
273
- throw new IllegalArgumentException (String .format ("index %s doesn't exist!" , indexName ));
274
- }
275
- Map <String , Set <String >> index = this .indices .get (indexName );
276
- Set <String > set = index .get (indexKey );
277
- List <String > keys = new ArrayList <>(set .size ());
278
- for (String key : set ) {
279
- keys .add (key );
280
- }
281
- return keys ;
282
- } finally {
283
- lock .unlock ();
227
+ public synchronized List <String > indexKeys (String indexName , String indexKey ) {
228
+ if (!this .indexers .containsKey (indexName )) {
229
+ throw new IllegalArgumentException (String .format ("index %s doesn't exist!" , indexName ));
230
+ }
231
+ Map <String , Set <String >> index = this .indices .get (indexName );
232
+ Set <String > set = index .get (indexKey );
233
+ List <String > keys = new ArrayList <>(set .size ());
234
+ for (String key : set ) {
235
+ keys .add (key );
284
236
}
237
+ return keys ;
285
238
}
286
239
287
240
/**
@@ -292,25 +245,20 @@ public List<String> indexKeys(String indexName, String indexKey) {
292
245
* @return the list
293
246
*/
294
247
@ Override
295
- public List <ApiType > byIndex (String indexName , String indexKey ) {
296
- lock .lock ();
297
- try {
298
- if (!this .indexers .containsKey (indexName )) {
299
- throw new IllegalArgumentException (String .format ("index %s doesn't exist!" , indexName ));
300
- }
301
- Map <String , Set <String >> index = this .indices .get (indexName );
302
- Set <String > set = index .get (indexKey );
303
- if (set == null ) {
304
- return Arrays .asList ();
305
- }
306
- List <ApiType > items = new ArrayList <>(set .size ());
307
- for (String key : set ) {
308
- items .add (this .items .get (key ));
309
- }
310
- return items ;
311
- } finally {
312
- lock .unlock ();
248
+ public synchronized List <ApiType > byIndex (String indexName , String indexKey ) {
249
+ if (!this .indexers .containsKey (indexName )) {
250
+ throw new IllegalArgumentException (String .format ("index %s doesn't exist!" , indexName ));
251
+ }
252
+ Map <String , Set <String >> index = this .indices .get (indexName );
253
+ Set <String > set = index .get (indexKey );
254
+ if (set == null ) {
255
+ return Arrays .asList ();
256
+ }
257
+ List <ApiType > items = new ArrayList <>(set .size ());
258
+ for (String key : set ) {
259
+ items .add (this .items .get (key ));
313
260
}
261
+ return items ;
314
262
}
315
263
316
264
/**
0 commit comments