KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdo > PMCacheEntry


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdo;
13
14 import java.lang.ref.Reference JavaDoc;
15 import java.lang.ref.ReferenceQueue JavaDoc;
16
17 import com.versant.core.common.*;
18
19 /**
20  * This is a wrapper instance used to represent an entry in the pm ManagedCache.
21  * It can either represent a OID-State pair or an OID-PCStateMan pair.
22  * It both cases the reference type can be weak or soft or hard.
23  */

24 public class PMCacheEntry {
25
26     /**
27      * The soft, weak or hard ref.
28      */

29     private Object JavaDoc ref;
30     /**
31      * This is a linked list of items to process at the end of the tx.
32      */

33     public PMCacheEntry processListNext;
34     public PMCacheEntry processListPrev;
35
36     public PMCacheEntry next;
37     public PMCacheEntry prev;
38
39     public int hash;
40     /**
41      * The type of ref(soft, weak or hard)
42      * @see com.versant.core.jdo.VersantPersistenceManager.PM_CACHE_REF_TYPE_WEAK
43      * @see com.versant.core.jdo.VersantPersistenceManager.PM_CACHE_REF_TYPE_SOFT
44      * @see com.versant.core.jdo.VersantPersistenceManager.PM_CACHE_REF_TYPE_STRONG
45      */

46     private int type;
47     /**
48      * The oid this mapping is done on.
49      */

50     public OID mappedOID;
51
52     public PMCacheEntry() {
53     }
54
55     public PMCacheEntry(int type, PCStateMan sm, ReferenceQueue JavaDoc queue) {
56         LocalPMCache.checkRefType(type);
57         this.type = type;
58         if (sm.oid.getRealOID() == null) {
59             this.hash = sm.oid.hashCode();
60             this.mappedOID = sm.oid;
61         } else {
62             this.mappedOID = sm.oid.getRealOID();
63             this.hash = sm.oid.getRealOID().hashCode();
64         }
65
66         switch (type) {
67             case VersantPersistenceManager.PM_CACHE_REF_TYPE_STRONG:
68                 ref = sm;
69                 break;
70             case VersantPersistenceManager.PM_CACHE_REF_TYPE_SOFT:
71                 ref = new SoftCacheEntryRef(sm, queue, this);
72                 break;
73             case VersantPersistenceManager.PM_CACHE_REF_TYPE_WEAK:
74                 ref = new WeakCacheEntryRef(sm, queue, this);
75                 break;
76             default:
77                 throw BindingSupportImpl.getInstance().internal("Unknown option: " + type);
78         }
79     }
80
81     public PMCacheEntry(int type, OID oid, State state, ReferenceQueue JavaDoc queue) {
82         LocalPMCache.checkRefType(type);
83         this.type = type;
84         this.hash = oid.hashCode();
85         this.mappedOID = oid;
86         if (mappedOID == null) {
87             throw BindingSupportImpl.getInstance().internal("");
88         }
89         switch (type) {
90             case VersantPersistenceManager.PM_CACHE_REF_TYPE_STRONG:
91                 ref = state;
92                 break;
93             case VersantPersistenceManager.PM_CACHE_REF_TYPE_SOFT:
94                 ref = new SoftCacheEntryRef(state, queue, this);
95                 break;
96             case VersantPersistenceManager.PM_CACHE_REF_TYPE_WEAK:
97                 ref = new WeakCacheEntryRef(state, queue, this);
98                 break;
99             default:
100                 throw BindingSupportImpl.getInstance().internal("Unknown option: " + type);
101         }
102     }
103
104     public void unlinkProcessList() {
105         if (processListNext != null) {
106             processListNext.processListPrev = null;
107         }
108         if (processListPrev != null) {
109             processListPrev.processListNext = null;
110         }
111
112         processListNext = null;
113         processListPrev = null;
114     }
115
116     public void unlinkNextList() {
117         if (prev != null) {
118             prev.next = next;
119         }
120         if (next != null) {
121             next.prev = prev;
122         }
123         next = null;
124         prev = null;
125     }
126
127     /**
128      * Upgrade the ref from a oid-state pair to a oid-pcstateman pair.
129      */

130     public PCStateMan upgradeToSm(PCStateMan sm, ReferenceQueue JavaDoc queue) {
131         sm.cacheEntry = this;
132         switch (type) {
133             case VersantPersistenceManager.PM_CACHE_REF_TYPE_STRONG:
134                 ref = sm;
135                 break;
136             case VersantPersistenceManager.PM_CACHE_REF_TYPE_SOFT:
137                 ((Reference JavaDoc)ref).clear();
138                 ref = new SoftCacheEntryRef(sm, queue, this);
139                 break;
140             case VersantPersistenceManager.PM_CACHE_REF_TYPE_WEAK:
141                 ((Reference JavaDoc)ref).clear();
142                 ref = new WeakCacheEntryRef(sm, queue, this);
143                 break;
144             default:
145                 throw BindingSupportImpl.getInstance().internal("Unknown option: " + type);
146         }
147         return sm;
148     }
149
150     public void setNext(PMCacheEntry nextEntry) {
151         if (Debug.DEBUG) {
152             if (nextEntry == this) {
153                 throw BindingSupportImpl.getInstance().internal("");
154             }
155         }
156         next = nextEntry;
157         if (next != null) {
158             next.prev = this;
159         }
160     }
161
162     public int hashCode() {
163         return hash;
164     }
165
166     /**
167      * Called when this is being remapped with a real oid.
168      */

169     public void reHash(OID newOID) {
170         mappedOID = newOID;
171         hash = newOID.hashCode();
172     }
173
174     /**
175      * Return the referenced instance.
176      * Either state of sm.
177      */

178     public Object JavaDoc get() {
179         if (ref instanceof Reference JavaDoc) {
180             return ((Reference JavaDoc)ref).get();
181         }
182         return ref;
183     }
184
185     public boolean hasReference(Object JavaDoc obj) {
186         return (obj == ref);
187     }
188
189     public void clear() {
190         if (ref instanceof Reference JavaDoc) {
191             ((Reference JavaDoc)ref).clear();
192         }
193         ref = null;
194     }
195
196     public void reset() {
197         clear();
198         next = null;
199         prev = null;
200         processListNext = null;
201         processListPrev = null;
202     }
203
204     public String JavaDoc toString() {
205         return "CacheEntryBase@" + System.identityHashCode(this) + " oid " + (mappedOID == null ? "null" : mappedOID.toStringImp());
206     }
207
208     public void changeToRefType(ReferenceQueue JavaDoc queue, int newType) {
209         LocalPMCache.checkRefType(newType);
210
211         if (this.type == newType) return;
212         Object JavaDoc oldRef = get();
213         clear();
214
215         this.type = newType;
216         switch (newType) {
217             case VersantPersistenceManager.PM_CACHE_REF_TYPE_STRONG:
218                 ref = oldRef;
219                 break;
220             case VersantPersistenceManager.PM_CACHE_REF_TYPE_SOFT:
221                 ref = new SoftCacheEntryRef(oldRef, queue, this);
222                 break;
223             case VersantPersistenceManager.PM_CACHE_REF_TYPE_WEAK:
224                 ref = new WeakCacheEntryRef(oldRef, queue, this);
225                 break;
226             default:
227                 throw BindingSupportImpl.getInstance().internal("Unknown option: " + type);
228         }
229     }
230 }
231
Popular Tags