1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| package com.opensymphony.oscache.base.algorithm; |
36 |
| |
37 |
| |
38 |
| |
39 |
| import com.opensymphony.oscache.base.CacheEntry; |
40 |
| import com.opensymphony.oscache.base.persistence.CachePersistenceException; |
41 |
| import com.opensymphony.oscache.base.persistence.PersistenceListener; |
42 |
| |
43 |
| import org.apache.commons.logging.Log; |
44 |
| import org.apache.commons.logging.LogFactory; |
45 |
| |
46 |
| import java.io.IOException; |
47 |
| import java.io.Serializable; |
48 |
| |
49 |
| import java.util.*; |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
| |
115 |
| |
116 |
| |
117 |
| |
118 |
| |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
| |
130 |
| |
131 |
| |
132 |
| |
133 |
| |
134 |
| |
135 |
| |
136 |
| |
137 |
| |
138 |
| |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
| |
149 |
| |
150 |
| |
151 |
| |
152 |
| |
153 |
| public abstract class AbstractConcurrentReadCache extends AbstractMap implements Map, Cloneable, Serializable { |
154 |
| |
155 |
| |
156 |
| |
157 |
| |
158 |
| public static final int DEFAULT_INITIAL_CAPACITY = 32; |
159 |
| |
160 |
| |
161 |
| |
162 |
| |
163 |
| |
164 |
| |
165 |
| |
166 |
| private static final int MINIMUM_CAPACITY = 4; |
167 |
| |
168 |
| |
169 |
| |
170 |
| |
171 |
| |
172 |
| |
173 |
| |
174 |
| private static final int MAXIMUM_CAPACITY = 1 << 30; |
175 |
| |
176 |
| |
177 |
| |
178 |
| |
179 |
| |
180 |
| public static final float DEFAULT_LOAD_FACTOR = 0.75f; |
181 |
| |
182 |
| |
183 |
| protected static final String NULL = "_nul!~"; |
184 |
| |
185 |
| private static final Log log = LogFactory.getLog(AbstractConcurrentReadCache.class); |
186 |
| |
187 |
| |
188 |
| |
189 |
| |
190 |
| |
191 |
| |
192 |
| |
193 |
| |
194 |
| |
195 |
| |
196 |
| |
197 |
| |
198 |
| |
199 |
| |
200 |
| |
201 |
| |
202 |
| |
203 |
| |
204 |
| |
205 |
| |
206 |
| |
207 |
| |
208 |
| |
209 |
| |
210 |
| |
211 |
| |
212 |
| |
213 |
| |
214 |
| |
215 |
| |
216 |
| protected final Boolean barrierLock = new Boolean(true); |
217 |
| |
218 |
| |
219 |
| |
220 |
| |
221 |
| protected transient Object lastWrite; |
222 |
| |
223 |
| |
224 |
| |
225 |
| |
226 |
| protected transient Entry[] table; |
227 |
| |
228 |
| |
229 |
| |
230 |
| |
231 |
| protected transient int count; |
232 |
| |
233 |
| |
234 |
| |
235 |
| |
236 |
| protected transient PersistenceListener persistenceListener = null; |
237 |
| |
238 |
| |
239 |
| |
240 |
| |
241 |
| protected boolean memoryCaching = true; |
242 |
| |
243 |
| |
244 |
| |
245 |
| |
246 |
| protected boolean unlimitedDiskCache = false; |
247 |
| |
248 |
| |
249 |
| |
250 |
| |
251 |
| |
252 |
| |
253 |
| protected float loadFactor; |
254 |
| |
255 |
| |
256 |
| |
257 |
| |
258 |
| protected final int DEFAULT_MAX_ENTRIES = 100; |
259 |
| |
260 |
| |
261 |
| |
262 |
| |
263 |
| protected final int UNLIMITED = 2147483646; |
264 |
| protected transient Collection values = null; |
265 |
| |
266 |
| |
267 |
| |
268 |
| |
269 |
| |
270 |
| |
271 |
| |
272 |
| protected HashMap groups = new HashMap(); |
273 |
| protected transient Set entrySet = null; |
274 |
| |
275 |
| |
276 |
| protected transient Set keySet = null; |
277 |
| |
278 |
| |
279 |
| |
280 |
| |
281 |
| protected int maxEntries = DEFAULT_MAX_ENTRIES; |
282 |
| |
283 |
| |
284 |
| |
285 |
| |
286 |
| |
287 |
| |
288 |
| |
289 |
| protected int threshold; |
290 |
| |
291 |
| |
292 |
| |
293 |
| |
294 |
| private boolean overflowPersistence = false; |
295 |
| |
296 |
| |
297 |
| |
298 |
| |
299 |
| |
300 |
| |
301 |
| |
302 |
| |
303 |
| |
304 |
| |
305 |
| |
306 |
265
| public AbstractConcurrentReadCache(int initialCapacity, float loadFactor) {
|
307 |
265
| if (loadFactor <= 0) {
|
308 |
0
| throw new IllegalArgumentException("Illegal Load factor: " + loadFactor);
|
309 |
| } |
310 |
| |
311 |
265
| this.loadFactor = loadFactor;
|
312 |
| |
313 |
265
| int cap = p2capacity(initialCapacity);
|
314 |
265
| table = new Entry[cap];
|
315 |
265
| threshold = (int) (cap * loadFactor);
|
316 |
| } |
317 |
| |
318 |
| |
319 |
| |
320 |
| |
321 |
| |
322 |
| |
323 |
| |
324 |
| |
325 |
| |
326 |
| |
327 |
0
| public AbstractConcurrentReadCache(int initialCapacity) {
|
328 |
0
| this(initialCapacity, DEFAULT_LOAD_FACTOR);
|
329 |
| } |
330 |
| |
331 |
| |
332 |
| |
333 |
| |
334 |
265
| public AbstractConcurrentReadCache() {
|
335 |
265
| this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
|
336 |
| } |
337 |
| |
338 |
| |
339 |
| |
340 |
| |
341 |
| |
342 |
| |
343 |
0
| public AbstractConcurrentReadCache(Map t) {
|
344 |
0
| this(Math.max(2 * t.size(), 11), DEFAULT_LOAD_FACTOR);
|
345 |
0
| putAll(t);
|
346 |
| } |
347 |
| |
348 |
| |
349 |
| |
350 |
| |
351 |
| |
352 |
| |
353 |
0
| public synchronized boolean isEmpty() {
|
354 |
0
| return count == 0;
|
355 |
| } |
356 |
| |
357 |
| |
358 |
| |
359 |
| |
360 |
| |
361 |
| |
362 |
| |
363 |
| |
364 |
| |
365 |
45
| public Set getGroup(String groupName) {
|
366 |
45
| if (log.isDebugEnabled()) {
|
367 |
0
| log.debug("getGroup called (group=" + groupName + ")");
|
368 |
| } |
369 |
| |
370 |
45
| Set groupEntries = null;
|
371 |
| |
372 |
45
| if (memoryCaching && (groups != null)) {
|
373 |
27
| groupEntries = (Set) getGroupForReading(groupName);
|
374 |
| } |
375 |
| |
376 |
45
| if (groupEntries == null) {
|
377 |
| |
378 |
21
| groupEntries = persistRetrieveGroup(groupName);
|
379 |
| } |
380 |
| |
381 |
45
| return groupEntries;
|
382 |
| } |
383 |
| |
384 |
| |
385 |
| |
386 |
| |
387 |
120
| public void setMaxEntries(int newLimit) {
|
388 |
120
| if (newLimit > 0) {
|
389 |
100
| maxEntries = newLimit;
|
390 |
| |
391 |
100
| synchronized (this) {
|
392 |
| |
393 |
100
| while (size() > maxEntries) {
|
394 |
20
| remove(removeItem(), false, false);
|
395 |
| } |
396 |
| } |
397 |
| } else { |
398 |
| |
399 |
20
| throw new IllegalArgumentException("Cache maximum number of entries must be at least 1");
|
400 |
| } |
401 |
| } |
402 |
| |
403 |
| |
404 |
| |
405 |
| |
406 |
40
| public int getMaxEntries() {
|
407 |
40
| return maxEntries;
|
408 |
| } |
409 |
| |
410 |
| |
411 |
| |
412 |
| |
413 |
265
| public void setMemoryCaching(boolean memoryCaching) {
|
414 |
265
| this.memoryCaching = memoryCaching;
|
415 |
| } |
416 |
| |
417 |
| |
418 |
| |
419 |
| |
420 |
60
| public boolean isMemoryCaching() {
|
421 |
60
| return memoryCaching;
|
422 |
| } |
423 |
| |
424 |
| |
425 |
| |
426 |
| |
427 |
196
| public void setPersistenceListener(PersistenceListener listener) {
|
428 |
196
| this.persistenceListener = listener;
|
429 |
| } |
430 |
| |
431 |
| |
432 |
| |
433 |
| |
434 |
80
| public PersistenceListener getPersistenceListener() {
|
435 |
80
| return persistenceListener;
|
436 |
| } |
437 |
| |
438 |
| |
439 |
| |
440 |
| |
441 |
235
| public void setUnlimitedDiskCache(boolean unlimitedDiskCache) {
|
442 |
235
| this.unlimitedDiskCache = unlimitedDiskCache;
|
443 |
| } |
444 |
| |
445 |
| |
446 |
| |
447 |
| |
448 |
0
| public boolean isUnlimitedDiskCache() {
|
449 |
0
| return unlimitedDiskCache;
|
450 |
| } |
451 |
| |
452 |
| |
453 |
| |
454 |
| |
455 |
| |
456 |
| |
457 |
20
| public boolean isOverflowPersistence() {
|
458 |
20
| return this.overflowPersistence;
|
459 |
| } |
460 |
| |
461 |
| |
462 |
| |
463 |
| |
464 |
| |
465 |
| |
466 |
305
| public void setOverflowPersistence(boolean overflowPersistence) {
|
467 |
305
| this.overflowPersistence = overflowPersistence;
|
468 |
| } |
469 |
| |
470 |
| |
471 |
| |
472 |
| |
473 |
30
| public synchronized int capacity() {
|
474 |
30
| return table.length;
|
475 |
| } |
476 |
| |
477 |
| |
478 |
| |
479 |
| |
480 |
345
| public synchronized void clear() {
|
481 |
345
| Entry[] tab = table;
|
482 |
| |
483 |
345
| for (int i = 0; i < tab.length; ++i) {
|
484 |
| |
485 |
11040
| for (Entry e = tab[i]; e != null; e = e.next) {
|
486 |
335
| e.value = null;
|
487 |
| |
488 |
| |
489 |
335
| itemRemoved(e.key);
|
490 |
| |
491 |
| |
492 |
| } |
493 |
| |
494 |
11040
| tab[i] = null;
|
495 |
| } |
496 |
| |
497 |
| |
498 |
345
| persistClear();
|
499 |
| |
500 |
345
| count = 0;
|
501 |
345
| recordModification(tab);
|
502 |
| } |
503 |
| |
504 |
| |
505 |
| |
506 |
| |
507 |
| |
508 |
| |
509 |
| |
510 |
| |
511 |
0
| public synchronized Object clone() {
|
512 |
0
| try {
|
513 |
0
| AbstractConcurrentReadCache t = (AbstractConcurrentReadCache) super.clone();
|
514 |
0
| t.keySet = null;
|
515 |
0
| t.entrySet = null;
|
516 |
0
| t.values = null;
|
517 |
| |
518 |
0
| Entry[] tab = table;
|
519 |
0
| t.table = new Entry[tab.length];
|
520 |
| |
521 |
0
| Entry[] ttab = t.table;
|
522 |
| |
523 |
0
| for (int i = 0; i < tab.length; ++i) {
|
524 |
0
| Entry first = tab[i];
|
525 |
| |
526 |
0
| if (first != null) {
|
527 |
0
| ttab[i] = (Entry) (first.clone());
|
528 |
| } |
529 |
| } |
530 |
| |
531 |
0
| return t;
|
532 |
| } catch (CloneNotSupportedException e) { |
533 |
| |
534 |
0
| throw new InternalError();
|
535 |
| } |
536 |
| } |
537 |
| |
538 |
| |
539 |
| |
540 |
| |
541 |
| |
542 |
| |
543 |
| |
544 |
| |
545 |
| |
546 |
| |
547 |
| |
548 |
| |
549 |
| |
550 |
| |
551 |
| |
552 |
| |
553 |
| |
554 |
| |
555 |
| |
556 |
60
| public boolean contains(Object value) {
|
557 |
60
| return containsValue(value);
|
558 |
| } |
559 |
| |
560 |
| |
561 |
| |
562 |
| |
563 |
| |
564 |
| |
565 |
| |
566 |
| |
567 |
| |
568 |
| |
569 |
| |
570 |
| |
571 |
30
| public boolean containsKey(Object key) {
|
572 |
30
| return get(key) != null;
|
573 |
| |
574 |
| |
575 |
| |
576 |
| |
577 |
| |
578 |
| |
579 |
| } |
580 |
| |
581 |
| |
582 |
| |
583 |
| |
584 |
| |
585 |
| |
586 |
| |
587 |
| |
588 |
| |
589 |
| |
590 |
| |
591 |
| |
592 |
60
| public boolean containsValue(Object value) {
|
593 |
60
| if (value == null) {
|
594 |
0
| throw new NullPointerException();
|
595 |
| } |
596 |
| |
597 |
60
| Entry[] tab = getTableForReading();
|
598 |
| |
599 |
60
| for (int i = 0; i < tab.length; ++i) {
|
600 |
1650
| for (Entry e = tab[i]; e != null; e = e.next) {
|
601 |
30
| Object v = e.value;
|
602 |
| |
603 |
30
| if ((v != null) && value.equals(v)) {
|
604 |
30
| return true;
|
605 |
| } |
606 |
| } |
607 |
| } |
608 |
| |
609 |
30
| return false;
|
610 |
| } |
611 |
| |
612 |
| |
613 |
| |
614 |
| |
615 |
| |
616 |
| |
617 |
| |
618 |
| |
619 |
| |
620 |
| |
621 |
| |
622 |
| |
623 |
30
| public Enumeration elements() {
|
624 |
30
| return new ValueIterator();
|
625 |
| } |
626 |
| |
627 |
| |
628 |
| |
629 |
| |
630 |
| |
631 |
| |
632 |
| |
633 |
| |
634 |
| |
635 |
| |
636 |
| |
637 |
| |
638 |
| |
639 |
30
| public Set entrySet() {
|
640 |
30
| Set es = entrySet;
|
641 |
| |
642 |
30
| if (es != null) {
|
643 |
0
| return es;
|
644 |
| } else { |
645 |
30
| return entrySet = new AbstractSet() {
|
646 |
30
| public Iterator iterator() {
|
647 |
30
| return new HashIterator();
|
648 |
| } |
649 |
| |
650 |
0
| public boolean contains(Object o) {
|
651 |
0
| if (!(o instanceof Map.Entry)) {
|
652 |
0
| return false;
|
653 |
| } |
654 |
| |
655 |
0
| Map.Entry entry = (Map.Entry) o;
|
656 |
0
| Object key = entry.getKey();
|
657 |
0
| Object v = AbstractConcurrentReadCache.this.get(key);
|
658 |
| |
659 |
0
| return (v != null) && v.equals(entry.getValue());
|
660 |
| } |
661 |
| |
662 |
0
| public boolean remove(Object o) {
|
663 |
0
| if (!(o instanceof Map.Entry)) {
|
664 |
0
| return false;
|
665 |
| } |
666 |
| |
667 |
0
| return AbstractConcurrentReadCache.this.findAndRemoveEntry((Map.Entry) o);
|
668 |
| } |
669 |
| |
670 |
0
| public int size() {
|
671 |
0
| return AbstractConcurrentReadCache.this.size();
|
672 |
| } |
673 |
| |
674 |
0
| public void clear() {
|
675 |
0
| AbstractConcurrentReadCache.this.clear();
|
676 |
| } |
677 |
| }; |
678 |
| } |
679 |
| } |
680 |
| |
681 |
| |
682 |
| |
683 |
| |
684 |
| |
685 |
| |
686 |
| |
687 |
| |
688 |
| |
689 |
| |
690 |
| |
691 |
| |
692 |
4628714
| public Object get(Object key) {
|
693 |
4631277
| if (log.isDebugEnabled()) {
|
694 |
0
| log.debug("get called (key=" + key + ")");
|
695 |
| } |
696 |
| |
697 |
| |
698 |
4631277
| int hash = hash(key);
|
699 |
| |
700 |
| |
701 |
| |
702 |
| |
703 |
| |
704 |
| |
705 |
| |
706 |
| |
707 |
| |
708 |
4631247
| Entry[] tab = table;
|
709 |
4631247
| int index = hash & (tab.length - 1);
|
710 |
4630432
| Entry first = tab[index];
|
711 |
4631247
| Entry e = first;
|
712 |
| |
713 |
4631247
| for (;;) {
|
714 |
4687757
| if (e == null) {
|
715 |
| |
716 |
| |
717 |
120461
| tab = getTableForReading();
|
718 |
| |
719 |
120461
| if (first == tab[index]) {
|
720 |
| |
721 |
| |
722 |
| |
723 |
| |
724 |
| |
725 |
| |
726 |
120461
| Object value = persistRetrieve(key);
|
727 |
| |
728 |
120461
| if (value != null) {
|
729 |
| |
730 |
43
| put(key, value, false);
|
731 |
| } |
732 |
| |
733 |
120461
| return value;
|
734 |
| |
735 |
| |
736 |
| } else { |
737 |
| |
738 |
0
| e = first = tab[index = hash & (tab.length - 1)];
|
739 |
| } |
740 |
| } |
741 |
| |
742 |
4567296
| else if ((key == e.key) || ((e.hash == hash) && key.equals(e.key))) {
|
743 |
4510786
| Object value = e.value;
|
744 |
| |
745 |
4510786
| if (value != null) {
|
746 |
| |
747 |
| |
748 |
| |
749 |
| |
750 |
4510786
| if (NULL.equals(value)) {
|
751 |
| |
752 |
256
| value = persistRetrieve(e.key);
|
753 |
| |
754 |
256
| if (value != null) {
|
755 |
254
| itemRetrieved(key);
|
756 |
| } |
757 |
| |
758 |
256
| return value;
|
759 |
| } else { |
760 |
4510530
| itemRetrieved(key);
|
761 |
| |
762 |
4510530
| return value;
|
763 |
| } |
764 |
| |
765 |
| |
766 |
| } |
767 |
| |
768 |
| |
769 |
| |
770 |
| |
771 |
| |
772 |
0
| synchronized (this) {
|
773 |
0
| tab = table;
|
774 |
| } |
775 |
| |
776 |
0
| e = first = tab[index = hash & (tab.length - 1)];
|
777 |
| } else { |
778 |
56510
| e = e.next;
|
779 |
| } |
780 |
| } |
781 |
| } |
782 |
| |
783 |
| |
784 |
| |
785 |
| |
786 |
| |
787 |
| |
788 |
| |
789 |
| |
790 |
| |
791 |
| |
792 |
| |
793 |
| |
794 |
30
| public Set keySet() {
|
795 |
30
| Set ks = keySet;
|
796 |
| |
797 |
30
| if (ks != null) {
|
798 |
10
| return ks;
|
799 |
| } else { |
800 |
20
| return keySet = new AbstractSet() {
|
801 |
30
| public Iterator iterator() {
|
802 |
30
| return new KeyIterator();
|
803 |
| } |
804 |
| |
805 |
0
| public int size() {
|
806 |
0
| return AbstractConcurrentReadCache.this.size();
|
807 |
| } |
808 |
| |
809 |
0
| public boolean contains(Object o) {
|
810 |
0
| return AbstractConcurrentReadCache.this.containsKey(o);
|
811 |
| } |
812 |
| |
813 |
0
| public boolean remove(Object o) {
|
814 |
0
| return AbstractConcurrentReadCache.this.remove(o) != null;
|
815 |
| } |
816 |
| |
817 |
0
| public void clear() {
|
818 |
0
| AbstractConcurrentReadCache.this.clear();
|
819 |
| } |
820 |
| }; |
821 |
| } |
822 |
| } |
823 |
| |
824 |
| |
825 |
| |
826 |
| |
827 |
| |
828 |
| |
829 |
| |
830 |
| |
831 |
| |
832 |
| |
833 |
0
| public Enumeration keys() {
|
834 |
0
| return new KeyIterator();
|
835 |
| } |
836 |
| |
837 |
| |
838 |
| |
839 |
| |
840 |
0
| public float loadFactor() {
|
841 |
0
| return loadFactor;
|
842 |
| } |
843 |
| |
844 |
| |
845 |
| |
846 |
| |
847 |
| |
848 |
| |
849 |
| |
850 |
| |
851 |
| |
852 |
| |
853 |
| |
854 |
| |
855 |
| |
856 |
| |
857 |
| |
858 |
| |
859 |
| |
860 |
| |
861 |
| |
862 |
66054
| public Object put(Object key, Object value) {
|
863 |
| |
864 |
66054
| return put(key, value, true);
|
865 |
| } |
866 |
| |
867 |
| |
868 |
| |
869 |
| |
870 |
| |
871 |
| |
872 |
| |
873 |
| |
874 |
| |
875 |
0
| public synchronized void putAll(Map t) {
|
876 |
0
| for (Iterator it = t.entrySet().iterator(); it.hasNext();) {
|
877 |
0
| Map.Entry entry = (Map.Entry) it.next();
|
878 |
0
| Object key = entry.getKey();
|
879 |
0
| Object value = entry.getValue();
|
880 |
0
| put(key, value);
|
881 |
| } |
882 |
| } |
883 |
| |
884 |
| |
885 |
| |
886 |
| |
887 |
| |
888 |
| |
889 |
| |
890 |
| |
891 |
| |
892 |
| |
893 |
70
| public Object remove(Object key) {
|
894 |
70
| return remove(key, true, false);
|
895 |
| } |
896 |
| |
897 |
| |
898 |
| |
899 |
| |
900 |
| |
901 |
| |
902 |
| |
903 |
| |
904 |
| |
905 |
0
| public Object removeForce(Object key) {
|
906 |
0
| return remove(key, true, true);
|
907 |
| } |
908 |
| |
909 |
| |
910 |
| |
911 |
| |
912 |
| |
913 |
| |
914 |
61288
| public synchronized int size() {
|
915 |
61288
| return count;
|
916 |
| } |
917 |
| |
918 |
| |
919 |
| |
920 |
| |
921 |
| |
922 |
| |
923 |
| |
924 |
| |
925 |
| |
926 |
| |
927 |
| |
928 |
| |
929 |
0
| public Collection values() {
|
930 |
0
| Collection vs = values;
|
931 |
| |
932 |
0
| if (vs != null) {
|
933 |
0
| return vs;
|
934 |
| } else { |
935 |
0
| return values = new AbstractCollection() {
|
936 |
0
| public Iterator iterator() {
|
937 |
0
| return new ValueIterator();
|
938 |
| } |
939 |
| |
940 |
0
| public int size() {
|
941 |
0
| return AbstractConcurrentReadCache.this.size();
|
942 |
| } |
943 |
| |
944 |
0
| public boolean contains(Object o) {
|
945 |
0
| return AbstractConcurrentReadCache.this.containsValue(o);
|
946 |
| } |
947 |
| |
948 |
0
| public void clear() {
|
949 |
0
| AbstractConcurrentReadCache.this.clear();
|
950 |
| } |
951 |
| }; |
952 |
| } |
953 |
| } |
954 |
| |
955 |
| |
956 |
| |
957 |
| |
958 |
| |
959 |
| |
960 |
| |
961 |
| |
962 |
| |
963 |
27
| protected synchronized final Set getGroupForReading(String groupName) {
|
964 |
27
| Set group = (Set) getGroupsForReading().get(groupName);
|
965 |
3
| if (group == null) return null;
|
966 |
24
| return new HashSet(group);
|
967 |
| } |
968 |
| |
969 |
| |
970 |
| |
971 |
| |
972 |
| |
973 |
| |
974 |
| |
975 |
67
| protected final Map getGroupsForReading() {
|
976 |
67
| synchronized (barrierLock) {
|
977 |
67
| return groups;
|
978 |
| } |
979 |
| } |
980 |
| |
981 |
| |
982 |
| |
983 |
| |
984 |
| |
985 |
| |
986 |
120611
| protected final Entry[] getTableForReading() {
|
987 |
120611
| synchronized (barrierLock) {
|
988 |
120611
| return table;
|
989 |
| } |
990 |
| } |
991 |
| |
992 |
| |
993 |
| |
994 |
| |
995 |
| |
996 |
| |
997 |
70433
| protected final void recordModification(Object x) {
|
998 |
70433
| synchronized (barrierLock) {
|
999 |
70433
| lastWrite = x;
|
1000 |
| } |
1001 |
| } |
1002 |
| |
1003 |
| |
1004 |
| |
1005 |
| |
1006 |
0
| protected synchronized boolean findAndRemoveEntry(Map.Entry entry) {
|
1007 |
0
| Object key = entry.getKey();
|
1008 |
0
| Object v = get(key);
|
1009 |
| |
1010 |
0
| if ((v != null) && v.equals(entry.getValue())) {
|
1011 |
0
| remove(key);
|
1012 |
| |
1013 |
0
| return true;
|
1014 |
| } else { |
1015 |
0
| return false;
|
1016 |
| } |
1017 |
| } |
1018 |
| |
1019 |
| |
1020 |
| |
1021 |
| |
1022 |
| |
1023 |
9072
| protected void persistRemove(Object key) {
|
1024 |
9072
| if (log.isDebugEnabled()) {
|
1025 |
0
| log.debug("PersistRemove called (key=" + key + ")");
|
1026 |
| } |
1027 |
| |
1028 |
9072
| if (persistenceListener != null) {
|
1029 |
42
| try {
|
1030 |
42
| persistenceListener.remove((String) key);
|
1031 |
| } catch (CachePersistenceException e) { |
1032 |
0
| log.error("[oscache] Exception removing cache entry with key '" + key + "' from persistence", e);
|
1033 |
| } |
1034 |
| } |
1035 |
| } |
1036 |
| |
1037 |
| |
1038 |
| |
1039 |
| |
1040 |
| |
1041 |
20
| protected void persistRemoveGroup(String groupName) {
|
1042 |
20
| if (log.isDebugEnabled()) {
|
1043 |
0
| log.debug("persistRemoveGroup called (groupName=" + groupName + ")");
|
1044 |
| } |
1045 |
| |
1046 |
20
| if (persistenceListener != null) {
|
1047 |
20
| try {
|
1048 |
20
| persistenceListener.removeGroup(groupName);
|
1049 |
| } catch (CachePersistenceException e) { |
1050 |
0
| log.error("[oscache] Exception removing group " + groupName, e);
|
1051 |
| } |
1052 |
| } |
1053 |
| } |
1054 |
| |
1055 |
| |
1056 |
| |
1057 |
| |
1058 |
| |
1059 |
120785
| protected Object persistRetrieve(Object key) {
|
1060 |
120785
| if (log.isDebugEnabled()) {
|
1061 |
0
| log.debug("persistRetrieve called (key=" + key + ")");
|
1062 |
| } |
1063 |
| |
1064 |
120785
| Object entry = null;
|
1065 |
| |
1066 |
120785
| if (persistenceListener != null) {
|
1067 |
647
| try {
|
1068 |
647
| entry = persistenceListener.retrieve((String) key);
|
1069 |
| } catch (CachePersistenceException e) { |
1070 |
| |
1071 |
| |
1072 |
| |
1073 |
| |
1074 |
| |
1075 |
| } |
1076 |
| } |
1077 |
| |
1078 |
120785
| return entry;
|
1079 |
| } |
1080 |
| |
1081 |
| |
1082 |
| |
1083 |
| |
1084 |
| |
1085 |
307
| protected Set persistRetrieveGroup(String groupName) {
|
1086 |
307
| if (log.isDebugEnabled()) {
|
1087 |
0
| log.debug("persistRetrieveGroup called (groupName=" + groupName + ")");
|
1088 |
| } |
1089 |
| |
1090 |
307
| if (persistenceListener != null) {
|
1091 |
248
| try {
|
1092 |
248
| return persistenceListener.retrieveGroup(groupName);
|
1093 |
| } catch (CachePersistenceException e) { |
1094 |
0
| log.error("[oscache] Exception retrieving group " + groupName, e);
|
1095 |
| } |
1096 |
| } |
1097 |
| |
1098 |
59
| return null;
|
1099 |
| } |
1100 |
| |
1101 |
| |
1102 |
| |
1103 |
| |
1104 |
| |
1105 |
| |
1106 |
65708
| protected void persistStore(Object key, Object obj) {
|
1107 |
65708
| if (log.isDebugEnabled()) {
|
1108 |
0
| log.debug("persistStore called (key=" + key + ")");
|
1109 |
| } |
1110 |
| |
1111 |
65708
| if (persistenceListener != null) {
|
1112 |
362
| try {
|
1113 |
362
| persistenceListener.store((String) key, obj);
|
1114 |
| } catch (CachePersistenceException e) { |
1115 |
0
| log.error("[oscache] Exception persisting " + key, e);
|
1116 |
| } |
1117 |
| } |
1118 |
| } |
1119 |
| |
1120 |
| |
1121 |
| |
1122 |
| |
1123 |
| |
1124 |
| |
1125 |
260
| protected void persistStoreGroup(String groupName, Set group) {
|
1126 |
260
| if (log.isDebugEnabled()) {
|
1127 |
0
| log.debug("persistStoreGroup called (groupName=" + groupName + ")");
|
1128 |
| } |
1129 |
| |
1130 |
260
| if (persistenceListener != null) {
|
1131 |
208
| try {
|
1132 |
208
| if ((group == null) || group.isEmpty()) {
|
1133 |
0
| persistenceListener.removeGroup(groupName);
|
1134 |
| } else { |
1135 |
208
| persistenceListener.storeGroup(groupName, group);
|
1136 |
| } |
1137 |
| } catch (CachePersistenceException e) { |
1138 |
0
| log.error("[oscache] Exception persisting group " + groupName, e);
|
1139 |
| } |
1140 |
| } |
1141 |
| } |
1142 |
| |
1143 |
| |
1144 |
| |
1145 |
| |
1146 |
345
| protected void persistClear() {
|
1147 |
345
| if (log.isDebugEnabled()) {
|
1148 |
0
| log.debug("persistClear called");
|
1149 |
| ; |
1150 |
| } |
1151 |
| |
1152 |
345
| if (persistenceListener != null) {
|
1153 |
168
| try {
|
1154 |
168
| persistenceListener.clear();
|
1155 |
| } catch (CachePersistenceException e) { |
1156 |
0
| log.error("[oscache] Exception clearing persistent cache", e);
|
1157 |
| } |
1158 |
| } |
1159 |
| } |
1160 |
| |
1161 |
| |
1162 |
| |
1163 |
| |
1164 |
| |
1165 |
| |
1166 |
| protected abstract void itemPut(Object key); |
1167 |
| |
1168 |
| |
1169 |
| |
1170 |
| |
1171 |
| |
1172 |
| |
1173 |
| protected abstract void itemRetrieved(Object key); |
1174 |
| |
1175 |
| |
1176 |
| |
1177 |
| |
1178 |
| |
1179 |
| |
1180 |
| protected abstract void itemRemoved(Object key); |
1181 |
| |
1182 |
| |
1183 |
| |
1184 |
| |
1185 |
| |
1186 |
| |
1187 |
| |
1188 |
| protected abstract Object removeItem(); |
1189 |
| |
1190 |
| |
1191 |
| |
1192 |
| |
1193 |
| |
1194 |
| |
1195 |
0
| private synchronized void readObject(java.io.ObjectInputStream s) throws IOException, ClassNotFoundException {
|
1196 |
| |
1197 |
0
| s.defaultReadObject();
|
1198 |
| |
1199 |
| |
1200 |
0
| int numBuckets = s.readInt();
|
1201 |
0
| table = new Entry[numBuckets];
|
1202 |
| |
1203 |
| |
1204 |
0
| int size = s.readInt();
|
1205 |
| |
1206 |
| |
1207 |
0
| for (int i = 0; i < size; i++) {
|
1208 |
0
| Object key = s.readObject();
|
1209 |
0
| Object value = s.readObject();
|
1210 |
0
| put(key, value);
|
1211 |
| } |
1212 |
| } |
1213 |
| |
1214 |
| |
1215 |
| |
1216 |
| |
1217 |
| |
1218 |
| |
1219 |
80
| protected void rehash() {
|
1220 |
80
| Entry[] oldMap = table;
|
1221 |
80
| int oldCapacity = oldMap.length;
|
1222 |
| |
1223 |
80
| if (oldCapacity >= MAXIMUM_CAPACITY) {
|
1224 |
0
| return;
|
1225 |
| } |
1226 |
| |
1227 |
80
| int newCapacity = oldCapacity << 1;
|
1228 |
80
| Entry[] newMap = new Entry[newCapacity];
|
1229 |
80
| threshold = (int) (newCapacity * loadFactor);
|
1230 |
| |
1231 |
| |
1232 |
| |
1233 |
| |
1234 |
| |
1235 |
| |
1236 |
| |
1237 |
| |
1238 |
| |
1239 |
| |
1240 |
| |
1241 |
| |
1242 |
80
| for (int i = 0; i < oldCapacity; ++i) {
|
1243 |
84160
| Entry l = null;
|
1244 |
84160
| Entry h = null;
|
1245 |
84160
| Entry e = oldMap[i];
|
1246 |
| |
1247 |
84160
| while (e != null) {
|
1248 |
55321
| int hash = e.hash;
|
1249 |
55321
| Entry next = e.next;
|
1250 |
| |
1251 |
55321
| if ((hash & oldCapacity) == 0) {
|
1252 |
| |
1253 |
26866
| if (l == null) {
|
1254 |
| |
1255 |
24239
| if ((next == null) || ((next.next == null) && ((next.hash & oldCapacity) == 0))) {
|
1256 |
19738
| l = e;
|
1257 |
| |
1258 |
19738
| break;
|
1259 |
| } |
1260 |
| } |
1261 |
| |
1262 |
7128
| l = new Entry(hash, e.key, e.value, l);
|
1263 |
| } else { |
1264 |
| |
1265 |
28455
| if (h == null) {
|
1266 |
25976
| if ((next == null) || ((next.next == null) && ((next.hash & oldCapacity) != 0))) {
|
1267 |
21513
| h = e;
|
1268 |
| |
1269 |
21513
| break;
|
1270 |
| } |
1271 |
| } |
1272 |
| |
1273 |
6942
| h = new Entry(hash, e.key, e.value, h);
|
1274 |
| } |
1275 |
| |
1276 |
14070
| e = next;
|
1277 |
| } |
1278 |
| |
1279 |
84160
| newMap[i] = l;
|
1280 |
84160
| newMap[oldCapacity + i] = h;
|
1281 |
| } |
1282 |
| |
1283 |
80
| table = newMap;
|
1284 |
80
| recordModification(newMap);
|
1285 |
| } |
1286 |
| |
1287 |
| |
1288 |
| |
1289 |
| |
1290 |
| |
1291 |
| |
1292 |
| |
1293 |
| |
1294 |
| |
1295 |
33
| protected Object sput(Object key, Object value, int hash, boolean persist) {
|
1296 |
| |
1297 |
33
| Entry[] tab = table;
|
1298 |
33
| int index = hash & (tab.length - 1);
|
1299 |
33
| Entry first = tab[index];
|
1300 |
33
| Entry e = first;
|
1301 |
| |
1302 |
33
| for (;;) {
|
1303 |
63
| if (e == null) {
|
1304 |
| |
1305 |
| |
1306 |
| |
1307 |
| |
1308 |
33
| Entry newEntry;
|
1309 |
| |
1310 |
33
| if (memoryCaching) {
|
1311 |
31
| newEntry = new Entry(hash, key, value, first);
|
1312 |
| } else { |
1313 |
2
| newEntry = new Entry(hash, key, NULL, first);
|
1314 |
| } |
1315 |
| |
1316 |
33
| itemPut(key);
|
1317 |
| |
1318 |
| |
1319 |
33
| if (persist && !overflowPersistence) {
|
1320 |
33
| persistStore(key, value);
|
1321 |
| } |
1322 |
| |
1323 |
| |
1324 |
33
| if (value instanceof CacheEntry) {
|
1325 |
33
| updateGroups(null, (CacheEntry) value, persist);
|
1326 |
| } |
1327 |
| |
1328 |
| |
1329 |
33
| tab[index] = newEntry;
|
1330 |
| |
1331 |
33
| if (++count >= threshold) {
|
1332 |
0
| rehash();
|
1333 |
| } else { |
1334 |
33
| recordModification(newEntry);
|
1335 |
| } |
1336 |
| |
1337 |
33
| return null;
|
1338 |
30
| } else if ((key == e.key) || ((e.hash == hash) && key.equals(e.key))) {
|
1339 |
0
| Object oldValue = e.value;
|
1340 |
| |
1341 |
| |
1342 |
| |
1343 |
| |
1344 |
| |
1345 |
0
| if (memoryCaching) {
|
1346 |
0
| e.value = value;
|
1347 |
| } |
1348 |
| |
1349 |
| |
1350 |
0
| if (persist && overflowPersistence) {
|
1351 |
0
| persistRemove(key);
|
1352 |
0
| } else if (persist) {
|
1353 |
0
| persistStore(key, value);
|
1354 |
| } |
1355 |
| |
1356 |
0
| updateGroups(oldValue, value, persist);
|
1357 |
| |
1358 |
0
| itemPut(key);
|
1359 |
| |
1360 |
| |
1361 |
0
| return oldValue;
|
1362 |
| } else { |
1363 |
30
| e = e.next;
|
1364 |
| } |
1365 |
| } |
1366 |
| } |
1367 |
| |
1368 |
| |
1369 |
| |
1370 |
| |
1371 |
| |
1372 |
| |
1373 |
| |
1374 |
| |
1375 |
| |
1376 |
0
| protected Object sremove(Object key, int hash, boolean invokeAlgorithm) {
|
1377 |
| |
1378 |
0
| Entry[] tab = table;
|
1379 |
0
| int index = hash & (tab.length - 1);
|
1380 |
0
| Entry first = tab[index];
|
1381 |
0
| Entry e = first;
|
1382 |
| |
1383 |
0
| for (;;) {
|
1384 |
0
| if (e == null) {
|
1385 |
0
| return null;
|
1386 |
0
| } else if ((key == e.key) || ((e.hash == hash) && key.equals(e.key))) {
|
1387 |
0
| Object oldValue = e.value;
|
1388 |
0
| if (persistenceListener != null && (oldValue == NULL)) {
|
1389 |
0
| oldValue = persistRetrieve(key);
|
1390 |
| } |
1391 |
| |
1392 |
0
| e.value = null;
|
1393 |
0
| count--;
|
1394 |
| |
1395 |
| |
1396 |
0
| if (!unlimitedDiskCache && !overflowPersistence) {
|
1397 |
0
| persistRemove(e.key);
|
1398 |
| |
1399 |
0
| if (oldValue instanceof CacheEntry) {
|
1400 |
0
| CacheEntry oldEntry = (CacheEntry)oldValue;
|
1401 |
0
| removeGroupMappings(oldEntry.getKey(),
|
1402 |
| oldEntry.getGroups(), true); |
1403 |
| } |
1404 |
| } else { |
1405 |
| |
1406 |
0
| if (oldValue instanceof CacheEntry) {
|
1407 |
0
| CacheEntry oldEntry = (CacheEntry)oldValue;
|
1408 |
0
| removeGroupMappings(oldEntry.getKey(),
|
1409 |
| oldEntry.getGroups(), false); |
1410 |
| } |
1411 |
| } |
1412 |
| |
1413 |
0
| if (overflowPersistence && ((size() + 1) >= maxEntries)) {
|
1414 |
0
| persistStore(key, oldValue);
|
1415 |
| |
1416 |
0
| if (oldValue instanceof CacheEntry) {
|
1417 |
0
| CacheEntry oldEntry = (CacheEntry)oldValue;
|
1418 |
0
| addGroupMappings(oldEntry.getKey(), oldEntry.getGroups(), true, false);
|
1419 |
| } |
1420 |
| } |
1421 |
| |
1422 |
0
| if (invokeAlgorithm) {
|
1423 |
0
| itemRemoved(key);
|
1424 |
| } |
1425 |
| |
1426 |
| |
1427 |
0
| Entry head = e.next;
|
1428 |
| |
1429 |
0
| for (Entry p = first; p != e; p = p.next) {
|
1430 |
0
| head = new Entry(p.hash, p.key, p.value, head);
|
1431 |
| } |
1432 |
| |
1433 |
0
| tab[index] = head;
|
1434 |
0
| recordModification(head);
|
1435 |
| |
1436 |
0
| return oldValue;
|
1437 |
| } else { |
1438 |
0
| e = e.next;
|
1439 |
| } |
1440 |
| } |
1441 |
| } |
1442 |
| |
1443 |
| |
1444 |
| |
1445 |
| |
1446 |
| |
1447 |
| |
1448 |
| |
1449 |
| |
1450 |
| |
1451 |
| |
1452 |
| |
1453 |
| |
1454 |
| |
1455 |
0
| private synchronized void writeObject(java.io.ObjectOutputStream s) throws IOException {
|
1456 |
| |
1457 |
0
| s.defaultWriteObject();
|
1458 |
| |
1459 |
| |
1460 |
0
| s.writeInt(table.length);
|
1461 |
| |
1462 |
| |
1463 |
0
| s.writeInt(count);
|
1464 |
| |
1465 |
| |
1466 |
0
| for (int index = table.length - 1; index >= 0; index--) {
|
1467 |
0
| Entry entry = table[index];
|
1468 |
| |
1469 |
0
| while (entry != null) {
|
1470 |
0
| s.writeObject(entry.key);
|
1471 |
0
| s.writeObject(entry.value);
|
1472 |
0
| entry = entry.next;
|
1473 |
| } |
1474 |
| } |
1475 |
| } |
1476 |
| |
1477 |
| |
1478 |
| |
1479 |
| |
1480 |
| |
1481 |
| |
1482 |
| |
1483 |
4704882
| private static int hash(Object x) {
|
1484 |
4706524
| int h = x.hashCode();
|
1485 |
| |
1486 |
| |
1487 |
| |
1488 |
| |
1489 |
4706494
| return ((h << 7) - h + (h >>> 9) + (h >>> 17));
|
1490 |
| } |
1491 |
| |
1492 |
| |
1493 |
| |
1494 |
| |
1495 |
| |
1496 |
| |
1497 |
| |
1498 |
| |
1499 |
| |
1500 |
| |
1501 |
| |
1502 |
| |
1503 |
| |
1504 |
| |
1505 |
| |
1506 |
317
| private void addGroupMappings(String key, Set newGroups, boolean persist, boolean memory) {
|
1507 |
317
| if (newGroups == null) {
|
1508 |
20
| return;
|
1509 |
| } |
1510 |
| |
1511 |
| |
1512 |
297
| for (Iterator it = newGroups.iterator(); it.hasNext();) {
|
1513 |
282
| String groupName = (String) it.next();
|
1514 |
| |
1515 |
| |
1516 |
282
| if (memoryCaching && memory) {
|
1517 |
182
| if (groups == null) {
|
1518 |
0
| groups = new HashMap();
|
1519 |
| } |
1520 |
| |
1521 |
182
| Set memoryGroup = (Set) groups.get(groupName);
|
1522 |
| |
1523 |
182
| if (memoryGroup == null) {
|
1524 |
113
| memoryGroup = new HashSet();
|
1525 |
113
| groups.put(groupName, memoryGroup);
|
1526 |
| } |
1527 |
| |
1528 |
182
| memoryGroup.add(key);
|
1529 |
| } |
1530 |
| |
1531 |
| |
1532 |
282
| if (persist) {
|
1533 |
236
| Set persistentGroup = persistRetrieveGroup(groupName);
|
1534 |
| |
1535 |
236
| if (persistentGroup == null) {
|
1536 |
112
| persistentGroup = new HashSet();
|
1537 |
| } |
1538 |
| |
1539 |
236
| persistentGroup.add(key);
|
1540 |
236
| persistStoreGroup(groupName, persistentGroup);
|
1541 |
| } |
1542 |
| } |
1543 |
| } |
1544 |
| |
1545 |
| |
1546 |
| |
1547 |
| |
1548 |
| |
1549 |
| |
1550 |
265
| private int p2capacity(int initialCapacity) {
|
1551 |
265
| int cap = initialCapacity;
|
1552 |
| |
1553 |
| |
1554 |
265
| int result;
|
1555 |
| |
1556 |
265
| if ((cap > MAXIMUM_CAPACITY) || (cap < 0)) {
|
1557 |
0
| result = MAXIMUM_CAPACITY;
|
1558 |
| } else { |
1559 |
265
| result = MINIMUM_CAPACITY;
|
1560 |
| |
1561 |
265
| while (result < cap) {
|
1562 |
795
| result <<= 1;
|
1563 |
| } |
1564 |
| } |
1565 |
| |
1566 |
265
| return result;
|
1567 |
| } |
1568 |
| |
1569 |
| |
1570 |
| |
1571 |
66097
| private Object put(Object key, Object value, boolean persist) {
|
1572 |
| |
1573 |
66097
| if (value == null) {
|
1574 |
30
| throw new NullPointerException();
|
1575 |
| } |
1576 |
| |
1577 |
66067
| int hash = hash(key);
|
1578 |
66067
| Entry[] tab = table;
|
1579 |
66067
| int index = hash & (tab.length - 1);
|
1580 |
66067
| Entry first = tab[index];
|
1581 |
66067
| Entry e = first;
|
1582 |
| |
1583 |
66067
| for (;;) {
|
1584 |
96367
| if (e == null) {
|
1585 |
60908
| synchronized (this) {
|
1586 |
60908
| tab = table;
|
1587 |
| |
1588 |
| |
1589 |
| |
1590 |
| |
1591 |
| |
1592 |
| |
1593 |
| |
1594 |
| |
1595 |
| |
1596 |
| |
1597 |
| |
1598 |
| |
1599 |
| |
1600 |
60908
| Object oldValue = null;
|
1601 |
| |
1602 |
| |
1603 |
60908
| if (size() >= maxEntries) {
|
1604 |
| |
1605 |
9090
| oldValue = remove(removeItem(), false, false);
|
1606 |
| } |
1607 |
| |
1608 |
60908
| if (first == tab[index]) {
|
1609 |
| |
1610 |
60875
| Entry newEntry = null;
|
1611 |
| |
1612 |
60875
| if (memoryCaching) {
|
1613 |
60739
| newEntry = new Entry(hash, key, value, first);
|
1614 |
| } else { |
1615 |
136
| newEntry = new Entry(hash, key, NULL, first);
|
1616 |
| } |
1617 |
| |
1618 |
60875
| tab[index] = newEntry;
|
1619 |
60875
| itemPut(key);
|
1620 |
| |
1621 |
| |
1622 |
60875
| if (persist && !overflowPersistence) {
|
1623 |
60458
| persistStore(key, value);
|
1624 |
| } |
1625 |
| |
1626 |
| |
1627 |
60875
| if (value instanceof CacheEntry) {
|
1628 |
60475
| updateGroups(null, (CacheEntry) value, persist);
|
1629 |
| } |
1630 |
| |
1631 |
60875
| if (++count >= threshold) {
|
1632 |
80
| rehash();
|
1633 |
| } else { |
1634 |
60795
| recordModification(newEntry);
|
1635 |
| } |
1636 |
| |
1637 |
60875
| return oldValue;
|
1638 |
| |
1639 |
| |
1640 |
| } else { |
1641 |
| |
1642 |
| |
1643 |
| |
1644 |
| |
1645 |
| |
1646 |
| |
1647 |
33
| return sput(key, value, hash, persist);
|
1648 |
| |
1649 |
| |
1650 |
| } |
1651 |
| } |
1652 |
35459
| } else if ((key == e.key) || ((e.hash == hash) && key.equals(e.key))) {
|
1653 |
| |
1654 |
| |
1655 |
5159
| synchronized (this) {
|
1656 |
5159
| tab = table;
|
1657 |
| |
1658 |
5159
| Object oldValue = e.value;
|
1659 |
| |
1660 |
| |
1661 |
5159
| if (persist && (oldValue == NULL)) {
|
1662 |
68
| oldValue = persistRetrieve(key);
|
1663 |
| } |
1664 |
| |
1665 |
5159
| if ((first == tab[index]) && (oldValue != null)) {
|
1666 |
| |
1667 |
| |
1668 |
| |
1669 |
| |
1670 |
| |
1671 |
5159
| if (memoryCaching) {
|
1672 |
5091
| e.value = value;
|
1673 |
| } |
1674 |
| |
1675 |
| |
1676 |
5159
| if (persist && overflowPersistence) {
|
1677 |
22
| persistRemove(key);
|
1678 |
5137
| } else if (persist) {
|
1679 |
5137
| persistStore(key, value);
|
1680 |
| } |
1681 |
| |
1682 |
5159
| updateGroups(oldValue, value, persist);
|
1683 |
5159
| itemPut(key);
|
1684 |
| |
1685 |
5159
| return oldValue;
|
1686 |
| |
1687 |
| |
1688 |
| } else { |
1689 |
| |
1690 |
| |
1691 |
| |
1692 |
| |
1693 |
| |
1694 |
| |
1695 |
0
| return sput(key, value, hash, persist);
|
1696 |
| |
1697 |
| |
1698 |
| } |
1699 |
| } |
1700 |
| } else { |
1701 |
30300
| e = e.next;
|
1702 |
| } |
1703 |
| } |
1704 |
| } |
1705 |
| |
1706 |
9180
| private synchronized Object remove(Object key, boolean invokeAlgorithm, boolean forcePersist)
|
1707 |
| |
1708 |
| |
1709 |
| |
1710 |
| { |
1711 |
| |
1712 |
| |
1713 |
| |
1714 |
| |
1715 |
| |
1716 |
| |
1717 |
| |
1718 |
| |
1719 |
| |
1720 |
| |
1721 |
| |
1722 |
| |
1723 |
| |
1724 |
9180
| if (key == null) {
|
1725 |
0
| return null;
|
1726 |
| } |
1727 |
| |
1728 |
| |
1729 |
9180
| int hash = hash(key);
|
1730 |
9180
| Entry[] tab = table;
|
1731 |
9180
| int index = hash & (tab.length - 1);
|
1732 |
9180
| Entry first = tab[index];
|
1733 |
9180
| Entry e = first;
|
1734 |
| |
1735 |
9180
| for (;;) {
|
1736 |
12370
| if (e == null) {
|
1737 |
0
| tab = getTableForReading();
|
1738 |
| |
1739 |
0
| if (first == tab[index]) {
|
1740 |
0
| return null;
|
1741 |
| } else { |
1742 |
| |
1743 |
| |
1744 |
| |
1745 |
| |
1746 |
| |
1747 |
| |
1748 |
0
| return sremove(key, hash, invokeAlgorithm);
|
1749 |
| |
1750 |
| |
1751 |
| } |
1752 |
12370
| } else if ((key == e.key) || ((e.hash == hash) && key.equals(e.key))) {
|
1753 |
9180
| synchronized (this) {
|
1754 |
9180
| tab = table;
|
1755 |
| |
1756 |
9180
| Object oldValue = e.value;
|
1757 |
9180
| if (persistenceListener != null && (oldValue == NULL)) {
|
1758 |
0
| oldValue = persistRetrieve(key);
|
1759 |
| } |
1760 |
| |
1761 |
| |
1762 |
9180
| if ((first != tab[index]) || (oldValue == null)) {
|
1763 |
| |
1764 |
| |
1765 |
| |
1766 |
| |
1767 |
0
| return sremove(key, hash, invokeAlgorithm);
|
1768 |
| } |
1769 |
| |
1770 |
| |
1771 |
9180
| e.value = null;
|
1772 |
9180
| count--;
|
1773 |
| |
1774 |
| |
1775 |
9180
| if (forcePersist || (!unlimitedDiskCache && !overflowPersistence)) {
|
1776 |
9050
| persistRemove(e.key);
|
1777 |
| |
1778 |
9050
| if (oldValue instanceof CacheEntry) {
|
1779 |
9020
| CacheEntry oldEntry = (CacheEntry) oldValue;
|
1780 |
9020
| removeGroupMappings(oldEntry.getKey(),
|
1781 |
| oldEntry.getGroups(), true); |
1782 |
| } |
1783 |
| } else { |
1784 |
| |
1785 |
130
| if (oldValue instanceof CacheEntry) {
|
1786 |
60
| CacheEntry oldEntry = (CacheEntry) oldValue;
|
1787 |
60
| removeGroupMappings(oldEntry.getKey(), oldEntry
|
1788 |
| .getGroups(), false); |
1789 |
| } |
1790 |
| } |
1791 |
| |
1792 |
9180
| if (!forcePersist && overflowPersistence && ((size() + 1) >= maxEntries)) {
|
1793 |
80
| persistStore(key, oldValue);
|
1794 |
| |
1795 |
80
| if (oldValue instanceof CacheEntry) {
|
1796 |
40
| CacheEntry oldEntry = (CacheEntry) oldValue;
|
1797 |
40
| addGroupMappings(oldEntry.getKey(), oldEntry.getGroups(), true, false);
|
1798 |
| } |
1799 |
| } |
1800 |
| |
1801 |
9180
| if (invokeAlgorithm) {
|
1802 |
70
| itemRemoved(key);
|
1803 |
| } |
1804 |
| |
1805 |
| |
1806 |
9180
| if (oldValue instanceof CacheEntry) {
|
1807 |
9080
| CacheEntry oldEntry = (CacheEntry) oldValue;
|
1808 |
9080
| oldValue = oldEntry.getContent();
|
1809 |
| } |
1810 |
| |
1811 |
| |
1812 |
9180
| Entry head = e.next;
|
1813 |
| |
1814 |
9180
| for (Entry p = first; p != e; p = p.next) {
|
1815 |
3190
| head = new Entry(p.hash, p.key, p.value, head);
|
1816 |
| } |
1817 |
| |
1818 |
9180
| tab[index] = head;
|
1819 |
9180
| recordModification(head);
|
1820 |
| |
1821 |
9180
| return oldValue;
|
1822 |
| } |
1823 |
| } else { |
1824 |
3190
| e = e.next;
|
1825 |
| } |
1826 |
| } |
1827 |
| } |
1828 |
| |
1829 |
| |
1830 |
| |
1831 |
| |
1832 |
| |
1833 |
| |
1834 |
| |
1835 |
| |
1836 |
| |
1837 |
| |
1838 |
| |
1839 |
| |
1840 |
| |
1841 |
| |
1842 |
9184
| private void removeGroupMappings(String key, Set oldGroups, boolean persist) {
|
1843 |
9184
| if (oldGroups == null) {
|
1844 |
9020
| return;
|
1845 |
| } |
1846 |
| |
1847 |
164
| for (Iterator it = oldGroups.iterator(); it.hasNext();) {
|
1848 |
90
| String groupName = (String) it.next();
|
1849 |
| |
1850 |
| |
1851 |
90
| if (memoryCaching && (this.groups != null)) {
|
1852 |
78
| Set memoryGroup = (Set) groups.get(groupName);
|
1853 |
| |
1854 |
78
| if (memoryGroup != null) {
|
1855 |
78
| memoryGroup.remove(key);
|
1856 |
| |
1857 |
78
| if (memoryGroup.isEmpty()) {
|
1858 |
60
| groups.remove(groupName);
|
1859 |
| } |
1860 |
| } |
1861 |
| } |
1862 |
| |
1863 |
| |
1864 |
90
| if (persist) {
|
1865 |
50
| Set persistentGroup = persistRetrieveGroup(groupName);
|
1866 |
| |
1867 |
50
| if (persistentGroup != null) {
|
1868 |
44
| persistentGroup.remove(key);
|
1869 |
| |
1870 |
44
| if (persistentGroup.isEmpty()) {
|
1871 |
20
| persistRemoveGroup(groupName);
|
1872 |
| } else { |
1873 |
24
| persistStoreGroup(groupName, persistentGroup);
|
1874 |
| } |
1875 |
| } |
1876 |
| } |
1877 |
| } |
1878 |
| } |
1879 |
| |
1880 |
| |
1881 |
| |
1882 |
| |
1883 |
| |
1884 |
| |
1885 |
| |
1886 |
| |
1887 |
| |
1888 |
| |
1889 |
5159
| private void updateGroups(Object oldValue, Object newValue, boolean persist) {
|
1890 |
| |
1891 |
5159
| boolean oldIsCE = oldValue instanceof CacheEntry;
|
1892 |
5159
| boolean newIsCE = newValue instanceof CacheEntry;
|
1893 |
| |
1894 |
5159
| if (newIsCE && oldIsCE) {
|
1895 |
5159
| updateGroups((CacheEntry) oldValue, (CacheEntry) newValue, persist);
|
1896 |
0
| } else if (newIsCE) {
|
1897 |
0
| updateGroups(null, (CacheEntry) newValue, persist);
|
1898 |
0
| } else if (oldIsCE) {
|
1899 |
0
| updateGroups((CacheEntry) oldValue, null, persist);
|
1900 |
| } |
1901 |
| } |
1902 |
| |
1903 |
| |
1904 |
| |
1905 |
| |
1906 |
| |
1907 |
| |
1908 |
| |
1909 |
| |
1910 |
| |
1911 |
| |
1912 |
65667
| private void updateGroups(CacheEntry oldValue, CacheEntry newValue, boolean persist) {
|
1913 |
65667
| Set oldGroups = null;
|
1914 |
65667
| Set newGroups = null;
|
1915 |
| |
1916 |
65667
| if (oldValue != null) {
|
1917 |
5159
| oldGroups = oldValue.getGroups();
|
1918 |
| } |
1919 |
| |
1920 |
65667
| if (newValue != null) {
|
1921 |
65667
| newGroups = newValue.getGroups();
|
1922 |
| } |
1923 |
| |
1924 |
| |
1925 |
65667
| if (oldGroups != null) {
|
1926 |
104
| Set removeFromGroups = new HashSet();
|
1927 |
| |
1928 |
104
| for (Iterator it = oldGroups.iterator(); it.hasNext();) {
|
1929 |
173
| String groupName = (String) it.next();
|
1930 |
| |
1931 |
173
| if ((newGroups == null) || !newGroups.contains(groupName)) {
|
1932 |
| |
1933 |
30
| removeFromGroups.add(groupName);
|
1934 |
| } |
1935 |
| } |
1936 |
| |
1937 |
104
| removeGroupMappings(oldValue.getKey(), removeFromGroups, persist);
|
1938 |
| } |
1939 |
| |
1940 |
| |
1941 |
65667
| if (newGroups != null) {
|
1942 |
277
| Set addToGroups = new HashSet();
|
1943 |
| |
1944 |
277
| for (Iterator it = newGroups.iterator(); it.hasNext();) {
|
1945 |
405
| String groupName = (String) it.next();
|
1946 |
| |
1947 |
405
| if ((oldGroups == null) || !oldGroups.contains(groupName)) {
|
1948 |
| |
1949 |
262
| addToGroups.add(groupName);
|
1950 |
| } |
1951 |
| } |
1952 |
| |
1953 |
277
| addGroupMappings(newValue.getKey(), addToGroups, persist, true);
|
1954 |
| } |
1955 |
| } |
1956 |
| |
1957 |
| |
1958 |
| |
1959 |
| |
1960 |
| protected static class Entry implements Map.Entry { |
1961 |
| protected final Entry next; |
1962 |
| protected final Object key; |
1963 |
| |
1964 |
| |
1965 |
| |
1966 |
| |
1967 |
| |
1968 |
| |
1969 |
| |
1970 |
| protected final int hash; |
1971 |
| protected volatile Object value; |
1972 |
| |
1973 |
78168
| Entry(int hash, Object key, Object value, Entry next) {
|
1974 |
78168
| this.hash = hash;
|
1975 |
78168
| this.key = key;
|
1976 |
78168
| this.next = next;
|
1977 |
78168
| this.value = value;
|
1978 |
| } |
1979 |
| |
1980 |
| |
1981 |
0
| public Object getKey() {
|
1982 |
0
| return key;
|
1983 |
| } |
1984 |
| |
1985 |
| |
1986 |
| |
1987 |
| |
1988 |
| |
1989 |
| |
1990 |
| |
1991 |
| |
1992 |
| |
1993 |
| |
1994 |
| |
1995 |
| |
1996 |
| |
1997 |
| |
1998 |
| |
1999 |
| |
2000 |
| |
2001 |
| |
2002 |
| |
2003 |
| |
2004 |
| |
2005 |
| |
2006 |
0
| public Object setValue(Object value) {
|
2007 |
0
| if (value == null) {
|
2008 |
0
| throw new NullPointerException();
|
2009 |
| } |
2010 |
| |
2011 |
0
| Object oldValue = this.value;
|
2012 |
0
| this.value = value;
|
2013 |
| |
2014 |
0
| return oldValue;
|
2015 |
| } |
2016 |
| |
2017 |
| |
2018 |
| |
2019 |
| |
2020 |
| |
2021 |
| |
2022 |
| |
2023 |
| |
2024 |
| |
2025 |
| |
2026 |
| |
2027 |
| |
2028 |
| |
2029 |
| |
2030 |
0
| public Object getValue() {
|
2031 |
0
| return value;
|
2032 |
| } |
2033 |
| |
2034 |
0
| public boolean equals(Object o) {
|
2035 |
0
| if (!(o instanceof Map.Entry)) {
|
2036 |
0
| return false;
|
2037 |
| } |
2038 |
| |
2039 |
0
| Map.Entry e = (Map.Entry) o;
|
2040 |
| |
2041 |
0
| if (!key.equals(e.getKey())) {
|
2042 |
0
| return false;
|
2043 |
| } |
2044 |
| |
2045 |
0
| Object v = value;
|
2046 |
| |
2047 |
0
| return (v == null) ? (e.getValue() == null) : v.equals(e.getValue());
|
2048 |
| } |
2049 |
| |
2050 |
0
| public int hashCode() {
|
2051 |
0
| Object v = value;
|
2052 |
| |
2053 |
0
| return hash ^ ((v == null) ? 0 : v.hashCode());
|
2054 |
| } |
2055 |
| |
2056 |
0
| public String toString() {
|
2057 |
0
| return key + "=" + value;
|
2058 |
| } |
2059 |
| |
2060 |
0
| protected Object clone() {
|
2061 |
0
| return new Entry(hash, key, value, ((next == null) ? null : (Entry) next.clone()));
|
2062 |
| } |
2063 |
| } |
2064 |
| |
2065 |
| protected class HashIterator implements Iterator, Enumeration { |
2066 |
| protected final Entry[] tab; |
2067 |
| protected Entry entry = null; |
2068 |
| protected Entry lastReturned = null; |
2069 |
| protected Object currentKey; |
2070 |
| protected Object currentValue; |
2071 |
| protected int index; |
2072 |
| |
2073 |
90
| protected HashIterator() {
|
2074 |
90
| tab = AbstractConcurrentReadCache.this.getTableForReading();
|
2075 |
90
| index = tab.length - 1;
|
2076 |
| } |
2077 |
| |
2078 |
100
| public boolean hasMoreElements() {
|
2079 |
100
| return hasNext();
|
2080 |
| } |
2081 |
| |
2082 |
250
| public boolean hasNext() {
|
2083 |
| |
2084 |
| |
2085 |
| |
2086 |
| |
2087 |
| |
2088 |
| |
2089 |
| |
2090 |
250
| for (;;) {
|
2091 |
380
| if (entry != null) {
|
2092 |
160
| Object v = entry.value;
|
2093 |
| |
2094 |
160
| if (v != null) {
|
2095 |
160
| currentKey = entry.key;
|
2096 |
160
| currentValue = v;
|
2097 |
| |
2098 |
160
| return true;
|
2099 |
| } else { |
2100 |
0
| entry = entry.next;
|
2101 |
| } |
2102 |
| } |
2103 |
| |
2104 |
220
| while ((entry == null) && (index >= 0)) {
|
2105 |
2880
| entry = tab[index--];
|
2106 |
| } |
2107 |
| |
2108 |
220
| if (entry == null) {
|
2109 |
90
| currentKey = currentValue = null;
|
2110 |
| |
2111 |
90
| return false;
|
2112 |
| } |
2113 |
| } |
2114 |
| } |
2115 |
| |
2116 |
130
| public Object next() {
|
2117 |
130
| if ((currentKey == null) && !hasNext()) {
|
2118 |
0
| throw new NoSuchElementException();
|
2119 |
| } |
2120 |
| |
2121 |
130
| Object result = returnValueOfNext();
|
2122 |
130
| lastReturned = entry;
|
2123 |
130
| currentKey = currentValue = null;
|
2124 |
130
| entry = entry.next;
|
2125 |
| |
2126 |
130
| return result;
|
2127 |
| } |
2128 |
| |
2129 |
40
| public Object nextElement() {
|
2130 |
40
| return next();
|
2131 |
| } |
2132 |
| |
2133 |
0
| public void remove() {
|
2134 |
0
| if (lastReturned == null) {
|
2135 |
0
| throw new IllegalStateException();
|
2136 |
| } |
2137 |
| |
2138 |
0
| AbstractConcurrentReadCache.this.remove(lastReturned.key);
|
2139 |
| } |
2140 |
| |
2141 |
0
| protected Object returnValueOfNext() {
|
2142 |
0
| return entry;
|
2143 |
| } |
2144 |
| } |
2145 |
| |
2146 |
| protected class KeyIterator extends HashIterator { |
2147 |
90
| protected Object returnValueOfNext() {
|
2148 |
90
| return currentKey;
|
2149 |
| } |
2150 |
| } |
2151 |
| |
2152 |
| protected class ValueIterator extends HashIterator { |
2153 |
40
| protected Object returnValueOfNext() {
|
2154 |
40
| return currentValue;
|
2155 |
| } |
2156 |
| } |
2157 |
| } |