001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * // Copyright (c) 1998, 2007, Oracle. All rights reserved.
005: *
006: *
007: * The contents of this file are subject to the terms of either the GNU
008: * General Public License Version 2 only ("GPL") or the Common Development
009: * and Distribution License("CDDL") (collectively, the "License"). You
010: * may not use this file except in compliance with the License. You can obtain
011: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
012: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
013: * language governing permissions and limitations under the License.
014: *
015: * When distributing the software, include this License Header Notice in each
016: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
017: * Sun designates this particular file as subject to the "Classpath" exception
018: * as provided by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the License
020: * Header, with the fields enclosed by brackets [] replaced by your own
021: * identifying information: "Portions Copyrighted [year]
022: * [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * If you wish your version of this file to be governed by only the CDDL or
027: * only the GPL Version 2, indicate your decision by adding "[Contributor]
028: * elects to include this software in this distribution under the [CDDL or GPL
029: * Version 2] license." If you don't indicate a single choice of license, a
030: * recipient has the option to distribute your version of this file under
031: * either the CDDL, the GPL Version 2 or to extend the choice of license to
032: * its licensees as provided above. However, if you add GPL Version 2 code
033: * and therefore, elected the GPL Version 2 license, then the option applies
034: * only if the new code is made subject to such option by the copyright
035: * holder.
036: */
037: package oracle.toplink.essentials.internal.identitymaps;
038:
039: import java.util.*;
040: import oracle.toplink.essentials.internal.helper.linkedlist.*;
041:
042: /**
043: * <p><b>Purpose</b>: A weak cache is identical to the weak identity map, however the weak
044: * can be a performance problem for some types of apps because it can cause too much garbage collection
045: * of objects read causing them to be re-read and re-built (this defeats the purpose of the cache).
046: * The weak cache solves this through also holding a fixed number of objects is memory to improve caching.
047: * This makes used of an exposed node linked list to maintain the objects by storing the link nodes in the cache key.
048: * <p><b>Responsibilities</b>:<ul>
049: * <li> Guarantees identity
050: * <li> Allows garbage collection
051: * <li> Increases performance through maintaining a fixed size cache of MRU objects when memory is available
052: * <li> The default size of the reference cache is half the max size
053: * </ul>
054: * @since TOPLink/Java 1.2
055: */
056: public class HardCacheWeakIdentityMap extends WeakIdentityMap {
057: protected ExposedNodeLinkedList referenceCache;
058:
059: public HardCacheWeakIdentityMap(int size) {
060: super (size);
061: this .referenceCache = new ExposedNodeLinkedList();
062: }
063:
064: /**
065: * Use a ReferenceCacheKey that also stores the linked list node to manage
066: * the LRU sub-cache of references.
067: */
068: public CacheKey createCacheKey(Vector primaryKey, Object object,
069: Object writeLockValue, long readTime) {
070: return new ReferenceCacheKey(primaryKey, object,
071: writeLockValue, readTime);
072: }
073:
074: /**
075: * Return the linked reference cache.
076: */
077: public ExposedNodeLinkedList getReferenceCache() {
078: return referenceCache;
079: }
080:
081: /**
082: * Creates a Soft reference if Required
083: * @param object is the domain object to cache.
084: */
085: public Object buildReference(Object object) {
086: return object;
087: }
088:
089: /**
090: * Checks if the object is null, or reference's object is null.
091: * @param the object for hard or the reference for soft.
092: */
093: public boolean hasReference(Object reference) {
094: return reference != null;
095: }
096:
097: /**
098: * Store the object in the cache with the cache key.
099: * Also store the linked list node in the cache key.
100: */
101: protected void put(CacheKey cacheKey) {
102: ReferenceCacheKey referenceCacheKey = (ReferenceCacheKey) cacheKey;
103: LinkedNode node = getReferenceCache().addFirst(
104: buildReference(referenceCacheKey.getObject()));
105: referenceCacheKey.setReferenceCacheNode(node);
106: super .put(cacheKey);
107: }
108:
109: /**
110: * Remove the cache key from the map and the sub-cache list.
111: */
112: public Object remove(CacheKey cacheKey) {
113: if (cacheKey == null) {
114: return null;
115: }
116: ReferenceCacheKey referenceCacheKey = (ReferenceCacheKey) cacheKey;
117: synchronized (this ) {
118: getReferenceCache().remove(
119: referenceCacheKey.getReferenceCacheNode());
120: }
121: return super .remove(cacheKey);
122: }
123:
124: /**
125: * This method will be used to update the max cache size.
126: */
127: public synchronized void updateMaxSize(int maxSize) {
128: setMaxSize(maxSize);
129: // Remove the LRU items if max size exceeded.
130: while (getReferenceCache().size() > getMaxSize()) {
131: getReferenceCache().removeLast();
132: }
133: }
134:
135: /**
136: * Inner class to define the specialized weak cache key.
137: * Keeps track of the linked list node to allow quick repositioning.
138: */
139: public class ReferenceCacheKey extends WeakCacheKey {
140: protected LinkedNode referenceNode;
141:
142: public ReferenceCacheKey(Vector primaryKey, Object object,
143: Object writeLockValue, long readTime) {
144: super (primaryKey, object, writeLockValue, readTime);
145: }
146:
147: public LinkedNode getReferenceCacheNode() {
148: return referenceNode;
149: }
150:
151: public void setReferenceCacheNode(LinkedNode referenceNode) {
152: this .referenceNode = referenceNode;
153: }
154:
155: public ExposedNodeLinkedList getReferenceCache() {
156: return referenceCache;
157: }
158:
159: /**
160: * Notifies that cache key that it has been accessed.
161: * Allows the LRU sub-cache to be maintained,
162: * the cache node must be moved to the front of the list.
163: */
164: public void updateAccess() {
165: // CR#3573797 must be synchronized on the map, not the cache key.
166: synchronized (HardCacheWeakIdentityMap.this ) {
167: // Check if the node's contents is null (was removed),
168: // also the object is null on initial put of acquired cache key,
169: // or ref value may have garbage collected so reset it.
170: if (!hasReference(getReferenceCacheNode().getContents())) {
171: getReferenceCacheNode().setContents(
172: buildReference(getObject()));
173: }
174:
175: // This is a fast constant time operations because of the linked list usage.
176: getReferenceCache().moveFirst(getReferenceCacheNode());
177: // Remove the old LRU items if max size exceeded (if was removed).
178: while (getReferenceCache().size() > getMaxSize()) {
179: getReferenceCache().removeLast();
180: }
181: }
182: }
183: }
184: }
|