88import java .util .List ;
99import java .util .Map ;
1010import java .util .Set ;
11- import java .util .concurrent .locks .ReentrantLock ;
1211import java .util .function .Function ;
1312import org .apache .commons .collections4 .CollectionUtils ;
1413import org .apache .commons .collections4 .MapUtils ;
@@ -32,10 +31,6 @@ public class Cache<ApiType extends KubernetesObject> implements Indexer<ApiType>
3231 /** indices stores objects' keys by their indices */
3332 private Map <String , Map <String , Set <String >>> indices = new HashMap <>();
3433
35- /** lock provides thread-safety */
36- // TODO: might remove the lock here and make the methods synchronized
37- private ReentrantLock lock = new ReentrantLock ();
38-
3934 public Cache () {
4035 this (
4136 Caches .NAMESPACE_INDEX ,
@@ -67,13 +62,10 @@ public Cache(
6762 @ Override
6863 public void add (ApiType obj ) {
6964 String key = keyFunc .apply (obj );
70- lock .lock ();
71- try {
65+ synchronized (this ) {
7266 ApiType oldObj = this .items .get (key );
7367 this .items .put (key , obj );
7468 this .updateIndices (oldObj , obj , key );
75- } finally {
76- lock .unlock ();
7769 }
7870 }
7971
@@ -85,13 +77,10 @@ public void add(ApiType obj) {
8577 @ Override
8678 public void update (ApiType obj ) {
8779 String key = keyFunc .apply (obj );
88- lock .lock ();
89- try {
80+ synchronized (this ) {
9081 ApiType oldObj = this .items .get (key );
9182 this .items .put (key , obj );
9283 updateIndices (oldObj , obj , key );
93- } finally {
94- lock .unlock ();
9584 }
9685 }
9786
@@ -103,15 +92,12 @@ public void update(ApiType obj) {
10392 @ Override
10493 public void delete (ApiType obj ) {
10594 String key = keyFunc .apply (obj );
106- lock .lock ();
107- try {
95+ synchronized (this ) {
10896 boolean exists = this .items .containsKey (key );
10997 if (exists ) {
11098 this .deleteFromIndices (this .items .get (key ), key );
11199 this .items .remove (key );
112100 }
113- } finally {
114- lock .unlock ();
115101 }
116102 }
117103
@@ -122,23 +108,18 @@ public void delete(ApiType obj) {
122108 * @param resourceVersion the resource version
123109 */
124110 @ 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 ;
134118
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 ());
142123 }
143124 }
144125
@@ -154,17 +135,12 @@ public void resync() {
154135 * @return the list
155136 */
156137 @ 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 ());
167142 }
143+ return keys ;
168144 }
169145
170146 /**
@@ -176,11 +152,8 @@ public List<String> listKeys() {
176152 @ Override
177153 public ApiType get (ApiType obj ) {
178154 String key = this .keyFunc .apply (obj );
179- lock .lock ();
180- try {
155+ synchronized (this ) {
181156 return this .getByKey (key );
182- } finally {
183- lock .unlock ();
184157 }
185158 }
186159
@@ -190,17 +163,12 @@ public ApiType get(ApiType obj) {
190163 * @return the list
191164 */
192165 @ 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 ());
203170 }
171+ return itemList ;
204172 }
205173
206174 /**
@@ -210,13 +178,8 @@ public List<ApiType> list() {
210178 * @return the get by key
211179 */
212180 @ 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 );
220183 }
221184
222185 /**
@@ -227,35 +190,30 @@ public ApiType getByKey(String key) {
227190 * @return the list
228191 */
229192 @ 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 ;
249208 }
209+ returnKeySet .addAll (set );
210+ }
250211
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 ));
258215 }
216+ return items ;
259217 }
260218
261219 /**
@@ -266,22 +224,17 @@ public List<ApiType> index(String indexName, ApiType obj) {
266224 * @return the list
267225 */
268226 @ 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 );
284236 }
237+ return keys ;
285238 }
286239
287240 /**
@@ -292,25 +245,20 @@ public List<String> indexKeys(String indexName, String indexKey) {
292245 * @return the list
293246 */
294247 @ 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 ));
313260 }
261+ return items ;
314262 }
315263
316264 /**
0 commit comments