KickJava   Java API By Example, From Geeks To Geeks.

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


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, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.identitymaps;
23
24 import java.util.*;
25
26 import oracle.toplink.essentials.exceptions.*;
27
28 /**
29  * <p><b>Purpose</b>: A fixed size LRU cache<p>
30  * Using a linked list as well as the hashtable from the superclass a LRU cache is maintained.
31  * When a get is executed the LRU list is updated and when a new object is inserted the object
32  * at the start of the list is deleted (provided the maxSize has been reached).
33  * <p><b>Responsibilities</b>:<ul>
34   * <li> Guarantees identity through primary key values
35  * <li> Keeps the LRU linked list updated.
36  * </ul>
37  * @since TOPLink/Java 1.0
38  */

39 public class CacheIdentityMap extends FullIdentityMap {
40
41     /** Provide handles on the linked list */
42     protected LinkedCacheKey first;
43
44     /** Provide handles on the linked list */
45     protected LinkedCacheKey last;
46
47     /**
48      * Initialize newly instantiated CacheIdentityMap.
49      * @param size int The size of the Cache
50      */

51     public CacheIdentityMap(int size) {
52         super(size);
53         this.first = new LinkedCacheKey(new Vector(2), null, null, 0);
54         this.last = new LinkedCacheKey(new Vector(2), null, null, 0);
55         this.first.setNext(this.last);
56         this.last.setPrevious(this.first);
57     }
58
59     public CacheKey createCacheKey(Vector primaryKey, Object JavaDoc object, Object JavaDoc writeLockValue, long readTime) {
60         return new LinkedCacheKey(primaryKey, object, writeLockValue, readTime);
61     }
62
63     /**
64      * Reduces the size of the receiver down to the maxSize removing objects from the
65      * start of the linked list.
66      */

67     protected void ensureFixedSize() {
68         // protect the case where someone attempts to break the cache by
69
// setting max size to 0.
70
synchronized(this.first) {
71         while (getMaxSize() > 0 && getSize() > getMaxSize()) {
72             remove(last.getPrevious());
73             }
74         }
75     }
76
77     /**
78      * Access the object within the table for the given primaryKey.
79      * Move the accessed key to the top of the order keys linked list to maintain LRU.
80      * @param aVector is the primary key for the object to search for.
81      * @return The LinkedCacheKey or null if none found for primaryKey
82      */

83     protected CacheKey getCacheKey(Vector primaryKeys) {
84         LinkedCacheKey cacheKey = (LinkedCacheKey)super.getCacheKey(primaryKeys);
85
86         if (cacheKey != null) {
87             synchronized (this.first) {
88                 removeLink(cacheKey);
89                 insertLink(cacheKey);
90             }
91         }
92
93         return cacheKey;
94     }
95
96     /**
97      * Insert a new element into the linked list of LinkedCacheKeys.
98      * New elements (Recently Used) are added at the end (last).
99      * @return The added LinkedCacheKey
100      */

101     protected LinkedCacheKey insertLink(LinkedCacheKey key) {
102         if (key == null){
103             return key;
104         }
105         // no sence on locking the entire cache, just lock on the list
106
synchronized (this.first){
107             this.first.getNext().setPrevious(key);
108             key.setNext(this.first.getNext());
109             key.setPrevious(this.first);
110             this.first.setNext(key);
111         }
112         return key;
113     }
114
115     /**
116      * Store the object in the identity map with the linked cache key
117      */

118     protected void put(CacheKey cacheKey) {
119         super.put(cacheKey);
120         insertLink((LinkedCacheKey)cacheKey);
121         ensureFixedSize();
122     }
123
124     /**
125      * Remove the LinkedCacheKey from the cache as well as from the linked list.
126      * @return The LinkedCacheKey to be removed
127      */

128     public Object JavaDoc remove(CacheKey key) {
129         super.remove(key);
130         // CR2408
131
if (key == null) {
132             Class JavaDoc cacheItemClass = null;
133
134             // Get the class of the CacheKey which we could not remove
135
// (if possible) for client debugging purposes.
136
// We can't get the descriptor, because we don't know the session.
137
if (!getCacheKeys().isEmpty()) {
138                 CacheKey aKey = (CacheKey)getCacheKeys().keys().nextElement();
139                 if ((aKey != null) && (aKey.getObject() != null)) {
140                     cacheItemClass = aKey.getObject().getClass();
141                 }
142             }
143             throw ValidationException.nullCacheKeyFoundOnRemoval(this, cacheItemClass);
144         }
145         return removeLink((LinkedCacheKey)key).getObject();
146     }
147
148     /**
149      * Remove the LinkedCacheKey from the linked list.
150      * @return The removed LinkedCacheKey
151      */

152     protected LinkedCacheKey removeLink(LinkedCacheKey key) {
153         if (key == null){
154             return key;
155         }
156         synchronized (this.first) {
157             if (key.getPrevious() == null || key.getNext() == null){
158                 //already removed by a competing thread, just return
159
return key;
160             }
161             key.getPrevious().setNext(key.getNext());
162             key.getNext().setPrevious(key.getPrevious());
163             key.setNext(null);
164             key.setPrevious(null);
165         }
166         return key;
167     }
168
169     /**
170      * INTERNAL:
171      * This method will be used to update the max cache size, any objects exceeding the max cache size will
172      * be remove from the cache. Please note that this does not remove the object from the identityMap, except in
173      * the case of the CacheIdentityMap.
174      */

175     public synchronized void updateMaxSize(int maxSize) {
176         setMaxSize(maxSize);
177         ensureFixedSize();
178     }
179 }
180
Popular Tags