1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.cache.infinispan.internal

File InfinispanCache.java

 

Coverage histogram

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

Code metrics

16
45
13
1
240
136
21
0.47
3.46
13
1.62

Classes

Class Line # Actions
InfinispanCache 51 45 0% 21 2
0.97297397.3%
 

Contributing tests

This file is covered by 117 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: e9f1efdfa81ad8703a5c4040aefed8b7d826a06d $
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  1339 toggle InfinispanCache(EmbeddedCacheManager cacheManager, CacheConfiguration configuration)
73    {
74  1339 super(configuration);
75   
76  1339 this.cacheManager = cacheManager;
77  1339 this.cache = cacheManager.<String, T>getCache(configuration.getConfigurationId());
78   
79  1339 this.cache.addListener(this);
80    }
81   
 
82  11266 toggle @Override
83    public void remove(String key)
84    {
85  11266 this.cache.remove(key);
86    }
87   
 
88  38826 toggle @Override
89    public void set(String key, T obj)
90    {
91  38815 if (obj != null) {
92  35055 this.cache.put(key, obj);
93    } else {
94  3763 this.cache.remove(key);
95    }
96    }
97   
 
98  2387574 toggle @Override
99    public T get(String key)
100    {
101  2387566 return this.cache.get(key);
102    }
103   
 
104  214 toggle @Override
105    public void removeAll()
106    {
107  214 this.cache.clear();
108    }
109   
 
110  10 toggle @Override
111    public void dispose()
112    {
113  10 super.dispose();
114   
115  10 this.cacheManager.removeCache(this.cache.getName());
116    }
117   
118    // ////////////////////////////////////////////////////////////////
119    // Events
120    // ////////////////////////////////////////////////////////////////
121   
122    /**
123    * @param event the eviction event.
124    */
 
125  2 toggle @CacheEntriesEvicted
126    public void nodeEvicted(CacheEntriesEvictedEvent<String, T> event)
127    {
128  2 for (Map.Entry<String, T> entry : event.getEntries().entrySet()) {
129  2 String key = entry.getKey();
130  2 T value = entry.getValue();
131   
132    // Looks like eviction does not produce any pre event
133  2 cacheEntryRemoved(key, value);
134    }
135    }
136   
137    /**
138    * @param event the expiration event.
139    */
 
140  3 toggle @CacheEntryExpired
141    public void nodeExpired(CacheEntryExpiredEvent<String, T> event)
142    {
143  3 String key = event.getKey();
144  3 T value = event.getValue();
145   
146    // Looks like eviction does not produce any pre event
147  3 cacheEntryRemoved(key, value);
148    }
149   
150    /**
151    * @param event the eviction event.
152    */
 
153  44079 toggle @CacheEntryRemoved
154    public void nodeRemoved(CacheEntryRemovedEvent<String, T> event)
155    {
156  44079 String key = event.getKey();
157  44079 T value = event.getValue();
158   
159  44079 if (event.isPre()) {
160  16252 if (value != null) {
161  16252 this.preEventData.put(key, value);
162    }
163    } else {
164  27827 cacheEntryRemoved(event.getKey(), this.preEventData.get(key));
165   
166  27827 this.preEventData.remove(key);
167    }
168    }
169   
170    /**
171    * @param event the modification event.
172    */
 
173  77331 toggle @CacheEntryCreated
174    public void nodeCreated(CacheEntryCreatedEvent<String, T> event)
175    {
176  77330 String key = event.getKey();
177   
178  77326 if (!event.isPre()) {
179  38666 cacheEntryInserted(key, event.getValue());
180    }
181    }
182   
183    /**
184    * @param event the modification event.
185    */
 
186  1676 toggle @CacheEntryModified
187    public void nodeModified(CacheEntryModifiedEvent<String, T> event)
188    {
189  1676 String key = event.getKey();
190  1676 T value = event.getValue();
191   
192  1676 if (event.isPre()) {
193  838 if (value != null) {
194  838 this.preEventData.put(key, value);
195    }
196    } else {
197  838 cacheEntryInserted(key, value);
198   
199  838 this.preEventData.remove(key);
200    }
201    }
202   
203    /**
204    * Dispatch data insertion event.
205    *
206    * @param key the entry key.
207    * @param value the entry value.
208    */
 
209  39504 toggle private void cacheEntryInserted(String key, T value)
210    {
211  39502 InfinispanCacheEntryEvent<T> event =
212    new InfinispanCacheEntryEvent<T>(new InfinispanCacheEntry<T>(this, key, value));
213   
214  39501 T previousValue = this.preEventData.get(key);
215   
216  39502 if (previousValue != null) {
217  838 if (previousValue != value) {
218  81 disposeCacheValue(previousValue);
219    }
220   
221  837 sendEntryModifiedEvent(event);
222    } else {
223  38666 sendEntryAddedEvent(event);
224    }
225    }
226   
227    /**
228    * Dispatch data remove event.
229    *
230    * @param key the entry key.
231    * @param value the entry value.
232    */
 
233  27832 toggle private void cacheEntryRemoved(String key, T value)
234    {
235  27832 InfinispanCacheEntryEvent<T> event =
236    new InfinispanCacheEntryEvent<T>(new InfinispanCacheEntry<T>(this, key, value));
237   
238  27832 sendEntryRemovedEvent(event);
239    }
240    }