1 21 22 package org.apache.derby.impl.services.cache; 23 24 import org.apache.derby.iapi.services.cache.Cacheable; 25 import org.apache.derby.iapi.services.cache.CacheableFactory; 26 import org.apache.derby.iapi.services.cache.CacheManager; 27 import org.apache.derby.iapi.error.StandardException; 28 29 import org.apache.derby.iapi.services.sanity.SanityManager; 30 import org.apache.derby.iapi.services.context.ContextService; 31 32 68 public final class CachedItem { 69 70 private static final int VALID = 0x00000001; 71 private static final int REMOVE_REQUESTED = 0x00000002; 72 private static final int SETTING_IDENTITY = 0x00000004; 73 private static final int REMOVE_OK = 0x00000008; 74 75 private static final int RECENTLY_USED = 0x00000010; 76 77 80 81 86 private int state; 87 88 94 private int keepCount; 95 96 101 private Cacheable entry; 102 103 106 public CachedItem() { 107 } 108 109 113 public void keepAfterSearch() { 114 keepCount++; 115 setUsed(true); 116 } 117 118 public void keepForCreate() { 119 if (SanityManager.DEBUG) { 120 SanityManager.ASSERT(!isKept()); 121 SanityManager.ASSERT(!isValid()); 122 } 123 keepCount = 1; 124 state |= SETTING_IDENTITY; 125 } 126 127 public void unkeepForCreate( ) 128 { 129 settingIdentityComplete(); 130 unkeep(); 131 } 132 133 public void keepForClean() { 134 if (SanityManager.DEBUG) { 135 SanityManager.ASSERT(isValid()); 136 } 137 keepCount++; 138 } 139 140 141 142 149 public synchronized boolean unkeep() { 150 boolean unkept = --keepCount == 0; 151 152 if (SanityManager.DEBUG) { 153 SanityManager.ASSERT(keepCount >= 0); 154 } 155 return unkept && ((state & REMOVE_REQUESTED) != 0); 156 } 157 158 163 public final boolean isKept() { 164 165 return keepCount != 0; 166 } 167 168 180 public void clean(boolean forRemove) throws StandardException 181 { 182 entry.clean(forRemove); 183 } 184 185 188 public synchronized void setRemoveState() { 189 state |= REMOVE_REQUESTED; 190 } 191 192 195 public final synchronized boolean isValid() { 196 return (state & VALID) != 0; 197 } 198 199 202 public synchronized void setValidState(boolean flag) { 203 204 if (flag) 205 state |= VALID; 206 else 207 state &= ~VALID; 208 209 state &= ~(REMOVE_REQUESTED | REMOVE_OK); 210 211 setUsed(flag); 212 } 213 214 217 public Cacheable getEntry() { 218 return entry; 219 } 220 221 224 public Cacheable takeOnIdentity(CacheManager cm, CacheableFactory holderFactory, 225 Object key, boolean forCreate, Object createParameter) 226 throws StandardException { 227 228 Cacheable oldEntry = entry; 230 if (oldEntry == null) 231 oldEntry = holderFactory.newCacheable(cm); 232 233 if (forCreate) { 234 entry = oldEntry.createIdentity(key, createParameter); 235 } else { 236 entry = oldEntry.setIdentity(key); 237 } 238 239 if (entry != null) { 240 if (SanityManager.DEBUG) { 242 SanityManager.ASSERT(entry.getIdentity().equals(key)); 243 } 244 245 return entry; 246 } 247 248 entry = oldEntry; 249 return null; 250 } 251 252 public synchronized void settingIdentityComplete() { 253 state &= ~SETTING_IDENTITY; 256 257 notifyAll(); 258 } 259 260 263 264 public synchronized Cacheable use() throws StandardException { 265 266 while ((state & SETTING_IDENTITY) != 0) { 267 try { 268 if (SanityManager.DEBUG) { 269 SanityManager.DEBUG("CacheTrace", 270 "trying to use a cached item that is taking on an identity"); 271 } 272 273 wait(); 274 275 } catch (InterruptedException ie) { 276 throw StandardException.interrupt(ie); 277 } 278 } 279 280 if (!isValid()) 282 return null; 283 284 if (SanityManager.DEBUG) 285 { 286 if (SanityManager.DEBUG_ON("CacheTrace")) 287 SanityManager.DEBUG( 288 "CacheTrace", "item keep count is " + keepCount); 289 } 290 291 292 return entry; 293 } 294 295 297 public void remove(boolean removeNow) throws StandardException { 298 299 if (!removeNow) { 300 301 synchronized (this) { 302 while ((state & REMOVE_OK) == 0) { 303 try { 304 wait(); 305 } catch (InterruptedException ie) { 306 throw StandardException.interrupt(ie); 307 } 308 } 309 } 310 } 311 312 clean(true); 313 } 314 315 public synchronized void notifyRemover() { 316 317 if (SanityManager.DEBUG) { 318 SanityManager.ASSERT((state & REMOVE_REQUESTED) != 0); 319 SanityManager.ASSERT(isKept()); 320 } 321 322 state |= REMOVE_OK; 323 notifyAll(); 324 } 325 326 329 public synchronized void setUsed(boolean flag) 330 { 331 if (flag) 332 state |= RECENTLY_USED; 333 else 334 state &= ~RECENTLY_USED; 335 } 336 337 341 public synchronized boolean recentlyUsed() { 342 return (state & RECENTLY_USED) != 0; 343 } 344 } 345 346 347 348 | Popular Tags |