1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.cache.infinispan.internal

File InfinispanCache.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

14
40
13
1
232
129
21
0.52
3.08
13
1.62

Classes

Class Line # Actions
InfinispanCache 51 40 0% 21 1
0.9850746498.5%
 

Contributing tests

This file is covered by 131 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.cache.infinispan.internal;
21   
22    import java.util.Map;
23    import java.util.concurrent.ConcurrentHashMap;
24    import java.util.concurrent.ConcurrentMap;
25   
26    import org.infinispan.Cache;
27    import org.infinispan.manager.EmbeddedCacheManager;
28    import org.infinispan.notifications.Listener;
29    import org.infinispan.notifications.cachelistener.annotation.CacheEntriesEvicted;
30    import org.infinispan.notifications.cachelistener.annotation.CacheEntryCreated;
31    import org.infinispan.notifications.cachelistener.annotation.CacheEntryExpired;
32    import org.infinispan.notifications.cachelistener.annotation.CacheEntryModified;
33    import org.infinispan.notifications.cachelistener.annotation.CacheEntryRemoved;
34    import org.infinispan.notifications.cachelistener.event.CacheEntriesEvictedEvent;
35    import org.infinispan.notifications.cachelistener.event.CacheEntryCreatedEvent;
36    import org.infinispan.notifications.cachelistener.event.CacheEntryExpiredEvent;
37    import org.infinispan.notifications.cachelistener.event.CacheEntryModifiedEvent;
38    import org.infinispan.notifications.cachelistener.event.CacheEntryRemovedEvent;
39    import org.xwiki.cache.config.CacheConfiguration;
40    import org.xwiki.cache.infinispan.internal.event.InfinispanCacheEntryEvent;
41    import org.xwiki.cache.util.AbstractCache;
42   
43    /**
44    * Implements {@link org.xwiki.cache.Cache} based on Infinispan.
45    *
46    * @param <T> the class of the data stored in the cache.
47    * @version $Id: d8af5bb0e9e6d1f59d0b0332d4202d658b66b1a2 $
48    * @since 3.3M1
49    */
50    @Listener
 
51    public class InfinispanCache<T> extends AbstractCache<T>
52    {
53    /**
54    * The Infinispan cache.
55    */
56    private Cache<String, T> cache;
57   
58    /**
59    * The state of the node before modification.
60    */
61    private ConcurrentMap<String, T> preEventData = new ConcurrentHashMap<String, T>();
62   
63    /**
64    * The Infinispan cache manager.
65    */
66    private EmbeddedCacheManager cacheManager;
67   
68    /**
69    * @param cacheManager the Infinispan cache manager
70    * @param configuration the XWiki Cache configuration
71    */
 
72  1873 toggle InfinispanCache(EmbeddedCacheManager cacheManager, CacheConfiguration configuration)
73    {
74  1873 super(configuration);
75   
76  1873 this.cacheManager = cacheManager;
77  1873 this.cache = cacheManager.<String, T>getCache(configuration.getConfigurationId());
78   
79  1873 this.cache.addListener(this);
80    }
81   
 
82  68691 toggle @Override
83    public void remove(String key)
84    {
85  68693 this.cache.remove(key);
86    }
87   
 
88  253616 toggle @Override
89    public void set(String key, T obj)
90    {
91  253631 if (obj != null) {
92  234800 this.cache.put(key, obj);
93    } else {
94  18827 this.cache.remove(key);
95    }
96    }
97   
 
98  58400158 toggle @Override
99    public T get(String key)
100    {
101  58406372 return this.cache.get(key);
102    }
103   
 
104  334 toggle @Override
105    public void removeAll()
106    {
107  334 this.cache.clear();
108    }
109   
 
110  12 toggle @Override
111    public void dispose()
112    {
113  12 super.dispose();
114   
115  12 this.cacheManager.removeCache(this.cache.getName());
116    }
117   
118    // ////////////////////////////////////////////////////////////////
119    // Events
120    // ////////////////////////////////////////////////////////////////
121   
122    /**
123    * @param event the eviction event.
124    */
 
125  45263 toggle @CacheEntriesEvicted
126    public void nodeEvicted(CacheEntriesEvictedEvent<String, T> event)
127    {
128  45263 for (Map.Entry<String, T> entry : event.getEntries().entrySet()) {
129  45264 String key = entry.getKey();
130  45264 T value = entry.getValue();
131   
132    // Looks like eviction does not produce any pre event
133  45264 cacheEntryRemoved(key, value);
134    }
135    }
136   
137    /**
138    * @param event the expiration event.
139    */
 
140  6278 toggle @CacheEntryExpired
141    public void nodeExpired(CacheEntryExpiredEvent<String, T> event)
142    {
143  6278 String key = event.getKey();
144  6278 T value = event.getValue();
145   
146    // Looks like eviction does not produce any pre event
147  6278 cacheEntryRemoved(key, value);
148    }
149   
150    /**
151    * @param event the eviction event.
152    */
 
153  297807 toggle @CacheEntryRemoved
154    public void nodeRemoved(CacheEntryRemovedEvent<String, T> event)
155    {
156    // TODO: remove != null when https://issues.jboss.org/browse/ISPN-9491 is fixed
157  297811 if (!event.isPre() && event.getOldValue() != null) {
158  135470 cacheEntryRemoved(event.getKey(), event.getOldValue());
159    }
160    }
161   
162    /**
163    * @param event the modification event.
164    */
 
165  547362 toggle @CacheEntryCreated
166    public void nodeCreated(CacheEntryCreatedEvent<String, T> event)
167    {
168  547364 String key = event.getKey();
169   
170  547375 if (!event.isPre()) {
171  273687 cacheEntryInserted(key, event.getValue());
172    }
173    }
174   
175    /**
176    * @param event the modification event.
177    */
 
178  25262 toggle @CacheEntryModified
179    public void nodeModified(CacheEntryModifiedEvent<String, T> event)
180    {
181  25265 String key = event.getKey();
182  25264 T value = event.getValue();
183   
184  25264 if (event.isPre()) {
185  12632 if (value != null) {
186  12633 this.preEventData.put(key, value);
187    }
188    } else {
189  12630 cacheEntryInserted(key, value);
190   
191  12633 this.preEventData.remove(key);
192    }
193    }
194   
195    /**
196    * Dispatch data insertion event.
197    *
198    * @param key the entry key.
199    * @param value the entry value.
200    */
 
201  286303 toggle private void cacheEntryInserted(String key, T value)
202    {
203  286317 InfinispanCacheEntryEvent<T> event =
204    new InfinispanCacheEntryEvent<T>(new InfinispanCacheEntry<T>(this, key, value));
205   
206  286293 T previousValue = this.preEventData.get(key);
207   
208  286312 if (previousValue != null) {
209  12633 if (previousValue != value) {
210  513 disposeCacheValue(previousValue);
211    }
212   
213  12632 sendEntryModifiedEvent(event);
214    } else {
215  273674 sendEntryAddedEvent(event);
216    }
217    }
218   
219    /**
220    * Dispatch data remove event.
221    *
222    * @param key the entry key.
223    * @param value the entry value.
224    */
 
225  187012 toggle private void cacheEntryRemoved(String key, T value)
226    {
227  187014 InfinispanCacheEntryEvent<T> event =
228    new InfinispanCacheEntryEvent<T>(new InfinispanCacheEntry<T>(this, key, value));
229   
230  187014 sendEntryRemovedEvent(event);
231    }
232    }