MCPcopy
hub / github.com/google/guava / put

Method put

guava/src/com/google/common/cache/LocalCache.java:2793–2866  ·  view source on GitHub ↗
(K key, int hash, V value, boolean onlyIfAbsent)

Source from the content-addressed store, hash-verified

2791 }
2792
2793 @CanIgnoreReturnValue
2794 @Nullable V put(K key, int hash, V value, boolean onlyIfAbsent) {
2795 lock();
2796 try {
2797 long now = map.ticker.read();
2798 preWriteCleanup(now);
2799
2800 int newCount = this.count + 1;
2801 if (newCount > this.threshold) { // ensure capacity
2802 expand();
2803 newCount = this.count + 1;
2804 }
2805
2806 AtomicReferenceArray<ReferenceEntry<K, V>> table = this.table;
2807 int index = hash & (table.length() - 1);
2808 ReferenceEntry<K, V> first = table.get(index);
2809
2810 // Look for an existing entry.
2811 for (ReferenceEntry<K, V> e = first; e != null; e = e.getNext()) {
2812 K entryKey = e.getKey();
2813 if (e.getHash() == hash
2814 && entryKey != null
2815 && map.keyEquivalence.equivalent(key, entryKey)) {
2816 // We found an existing entry.
2817
2818 ValueReference<K, V> valueReference = e.getValueReference();
2819 V entryValue = valueReference.get();
2820
2821 if (entryValue == null) {
2822 ++modCount;
2823 if (valueReference.isActive()) {
2824 enqueueNotification(
2825 key, hash, entryValue, valueReference.getWeight(), RemovalCause.COLLECTED);
2826 setValue(e, key, value, now);
2827 newCount = this.count; // count remains unchanged
2828 } else {
2829 setValue(e, key, value, now);
2830 newCount = this.count + 1;
2831 }
2832 this.count = newCount; // write-volatile
2833 evictEntries(e);
2834 return null;
2835 } else if (onlyIfAbsent) {
2836 // Mimic
2837 // "if (!map.containsKey(key)) ...
2838 // else return map.get(key);
2839 recordLockedRead(e, now);
2840 return entryValue;
2841 } else {
2842 // clobber existing entry, count remains unchanged
2843 ++modCount;
2844 enqueueNotification(
2845 key, hash, entryValue, valueReference.getWeight(), RemovalCause.REPLACED);
2846 setValue(e, key, value, now);
2847 evictEntries(e);
2848 return entryValue;
2849 }
2850 }

Callers

nothing calls this directly

Calls 15

preWriteCleanupMethod · 0.95
expandMethod · 0.95
enqueueNotificationMethod · 0.95
setValueMethod · 0.95
evictEntriesMethod · 0.95
recordLockedReadMethod · 0.95
newEntryMethod · 0.95
postWriteCleanupMethod · 0.95
getMethod · 0.65
getNextMethod · 0.65
getKeyMethod · 0.65
getHashMethod · 0.65

Tested by

no test coverage detected