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

Method lockedGetOrLoad

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

Source from the content-addressed store, hash-verified

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

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