KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > identitymaps > WeakIdentityMap


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.identitymaps;
23
24 import java.util.*;
25
26 /**
27  * <p><b>Purpose</b>: A WeakIdentityMap holds all objects referenced by the application only.
28  * The weak identity map is similar to the full identity map except for the fact that it allows
29  * full garbage collection.
30  * <p><b>Responsibilities</b>:<ul>
31  * <li> Guarantees identity
32  * <li> Allows garbage collection
33  * </ul>
34  * @since TOPLink/Java 1.0
35  */

36 public class WeakIdentityMap extends FullIdentityMap {
37
38     /** Keep track of a counter to amortize cleanup of dead cache keys */
39     protected int cleanupCount;
40
41     /** PERF: Keep track of a cleanup size to avoid cleanup bottleneck for large caches. */
42     protected int cleanupSize;
43
44     public WeakIdentityMap(int size) {
45         super(size);
46         this.cleanupCount = 0;
47         this.cleanupSize = size;
48     }
49
50     /**
51      * Search for any cache keys that have been garbage collected and remove them.
52      * This must be done because allthough the objects held by the cache keys will garbage collect,
53      * the keys themselves will not and must be cleaned up. This is a linear opperation so
54      * is amortized through the cleanupCount to occur only once per cycle avergaing to make
55      * the total time still constant.
56      */

57     protected void cleanupDeadCacheKeys() {
58         for (Enumeration keysEnum = getCacheKeys().elements(); keysEnum.hasMoreElements();) {
59             CacheKey key = (CacheKey)keysEnum.nextElement();
60             if (key.getObject() == null) {
61                 // Check lock first.
62
//Change for CR 2317
63
if (key.acquireNoWait()) {
64                     try {
65                         if (key.getObject() == null) {
66                             getCacheKeys().remove(key);
67                         }
68                     } finally {
69                         key.release();
70                     }
71                 }
72
73                 //change complete CR 2317
74
}
75         }
76     }
77
78     public CacheKey createCacheKey(Vector primaryKey, Object JavaDoc object, Object JavaDoc writeLockValue, long readTime) {
79         return new WeakCacheKey(primaryKey, object, writeLockValue, readTime);
80     }
81
82     /**
83      * Used to amortized the cleanup of dead cache keys.
84      */

85     protected int getCleanupCount() {
86         return cleanupCount;
87     }
88
89     protected void setCleanupCount(int cleanupCount) {
90         this.cleanupCount = cleanupCount;
91     }
92
93     /**
94      * Used to amortized the cleanup of dead cache keys.
95      */

96     protected int getCleanupSize() {
97         return cleanupSize;
98     }
99
100     protected void setCleanupSize(int cleanupSize) {
101         this.cleanupSize = cleanupSize;
102     }
103
104     /**
105      * Store the object in the cache with the cache key.
106      */

107     protected void put(CacheKey cacheKey) {
108         //CR3712 Add the method back.
109
synchronized (this) {
110             if (getCleanupCount() > getCleanupSize()) {
111                 cleanupDeadCacheKeys();
112                 setCleanupCount(0);
113                 // PERF: Avoid cleanup bottleneck for large cache sizes, increase next cleanup.
114
if (getSize() > getCleanupSize()) {
115                     setCleanupSize(getSize());
116                 }
117             }
118             setCleanupCount(getCleanupCount() + 1);
119         }
120         super.put(cacheKey);
121     }
122 }
123
Popular Tags