MCPcopy Create free account
hub / github.com/antlr/codebuff / lockedGetOrLoad

Method lockedGetOrLoad

output/java_guava/1.4.13/LocalCache.java:2289–2362  ·  view source on GitHub ↗
(K key, int hash, CacheLoader<? super K, V> loader)

Source from the content-addressed store, hash-verified

2287 }
2288
2289 V lockedGetOrLoad(K key, int hash, CacheLoader<? super K, V> loader) throws ExecutionException {
2290 ReferenceEntry<K, V> e;
2291 ValueReference<K, V> valueReference = null;
2292 LoadingValueReference<K, V> loadingValueReference = null;
2293 boolean createNewEntry = true;
2294 lock();
2295 try {
2296 // re-read ticker once inside the lock
2297 long now = map.ticker.read();
2298 preWriteCleanup(now);
2299 int newCount = this.count - 1;
2300 AtomicReferenceArray<ReferenceEntry<K, V>> table = this.table;
2301 int index = hash & (table.length() - 1);
2302 ReferenceEntry<K, V> first = table.get(index);
2303 for (e = first; e != null; e = e.getNext()) {
2304 K entryKey = e.getKey();
2305 if (e.getHash() == hash && entryKey != null
2306 && map.keyEquivalence.equivalent(key, entryKey)) {
2307 valueReference = e.getValueReference();
2308 if (valueReference.isLoading()) {
2309 createNewEntry = false;
2310 } else {
2311 V value = valueReference.get();
2312 if (value == null) {
2313 enqueueNotification(entryKey, hash, value, valueReference.getWeight(), RemovalCause.COLLECTED);
2314 } else if (map.isExpired(e, now)) {
2315 // This is a duplicate check, as preWriteCleanup already purged expired
2316 // entries, but let's accomodate an incorrect expiration queue.
2317 enqueueNotification(entryKey, hash, value, valueReference.getWeight(), RemovalCause.EXPIRED);
2318 } else {
2319 recordLockedRead(e, now);
2320 statsCounter.recordHits(1);
2321 // we were concurrent with loading; don't consider refresh
2322 return value;
2323 }
2324
2325 // immediately reuse invalid entries
2326 writeQueue.remove(e);
2327 accessQueue.remove(e);
2328 this.count = newCount; // write-volatile
2329 }
2330 break;
2331 }
2332 }
2333 if (createNewEntry) {
2334 loadingValueReference = new LoadingValueReference<K, V>();
2335 if (e == null) {
2336 e = newEntry(key, hash, first);
2337 e.setValueReference(loadingValueReference);
2338 table.set(index, e);
2339 } else {
2340 e.setValueReference(loadingValueReference);
2341 }
2342 }
2343 } finally {
2344 unlock();
2345 postWriteCleanup();
2346 }

Callers 1

getMethod · 0.95

Calls 15

preWriteCleanupMethod · 0.95
enqueueNotificationMethod · 0.95
recordLockedReadMethod · 0.95
newEntryMethod · 0.95
postWriteCleanupMethod · 0.95
loadSyncMethod · 0.95
waitForLoadingValueMethod · 0.95
getMethod · 0.65
getNextMethod · 0.65
getKeyMethod · 0.65
getHashMethod · 0.65
getValueReferenceMethod · 0.65

Tested by

no test coverage detected