Skip to content

Commit

Permalink
COH-23095 - Implement getOrDefault for NearCache
Browse files Browse the repository at this point in the history
[git-p4: depot-paths = "//dev/coherence-ce/main/": change = 85391]
  • Loading branch information
mgamanho committed Mar 18, 2021
1 parent 670262c commit 09f1827
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates.
* Copyright (c) 2000, 2021, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/

package com.tangosol.net.cache;

import com.tangosol.net.management.MBeanHelper;

import com.tangosol.io.ClassLoaderAware;

import com.tangosol.util.Base;
import com.tangosol.util.Filter;
import com.tangosol.util.MapListener;
import com.tangosol.util.ValueExtractor;

import com.tangosol.net.CacheService;
import com.tangosol.net.MemberEvent;
import com.tangosol.net.MemberListener;
import com.tangosol.net.NamedCache;
import com.tangosol.net.PartitionedService;
import com.tangosol.net.Service;

import com.tangosol.net.management.MBeanHelper;

import com.tangosol.util.Base;
import com.tangosol.util.Filter;
import com.tangosol.util.MapListener;
import com.tangosol.util.ValueExtractor;

import com.tangosol.util.function.Remote;

import java.util.Collection;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;

import java.util.function.Function;

/**
* A "near cache" is a CachingMap whose front map is a size-limited and/or
* auto-expiring local cache, and whose back map is a distributed cache.
Expand Down Expand Up @@ -462,6 +466,29 @@ public <T, E> void removeIndex(ValueExtractor<? super T, ? extends E> extractor)

// ----- InvocableMap interface -----------------------------------------

/**
* {@inheritDoc}
* <p>
* The operation executes against the back cache if the value is not in the front.
*/
public V computeIfAbsent(K key, Remote.Function<? super K, ? extends V> mappingFunction)
{
V value = getFrontMap().get(key);
return value == null ? getBackMap().computeIfAbsent(key, mappingFunction) : value;
}

/**
* {@inheritDoc}
* <p>
* The operation executes against the back cache if the value is not in the front.
*/
@Override
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)
{
V value = getFrontMap().get(key);
return value == null ? getBackMap().computeIfAbsent(key, mappingFunction) : value;
}

/**
* {@inheritDoc}
* <p>
Expand Down Expand Up @@ -512,6 +539,17 @@ public <R> R aggregate(Filter filter, EntryAggregator<? super K, ? super V, R> a
return getBackCache().aggregate(filter, aggregator);
}

/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("unchecked")
public V getOrDefault(Object oKey, V defaultValue)
{
V value = (V) get(oKey);

return value == null ? defaultValue : value;
}

// ----- internal helpers -----------------------------------------------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates.
* Copyright (c) 2000, 2021, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/

package extend;


import com.oracle.bedrock.testsupport.deferred.Eventually;
import common.TestMapListener;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.tangosol.net.NamedCache;

import com.tangosol.net.cache.CacheStatistics;
import com.tangosol.net.cache.LocalCache;
import com.tangosol.net.cache.NearCache;

import com.tangosol.util.MapEvent;

import common.TestMapListener;

import data.Person;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import static com.oracle.bedrock.deferred.DeferredHelper.invoking;

import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
* A collection of functional tests for Coherence*Extend that use the
Expand Down Expand Up @@ -100,4 +109,25 @@ public void testTruncate()

assertTrue(cache.isActive());
}

/**
* Test for COH-23095
*/
@Test
public void testCoh23095()
{
NearCache<Integer, Person> cache = (NearCache) getNamedCache();

CacheStatistics cacheStats = ((LocalCache) cache.getFrontMap()).getCacheStatistics();
long cInitialMisses = cacheStats.getCacheMisses();

cache.computeIfAbsent(2,
person -> new Person("4321", "frist", "lats", 2000, null, new String[0]));
assertEquals(cacheStats.getCacheMisses(), cInitialMisses + 1);

cache.getOrDefault(3,
new Person("5678", "a", "b", 1980, null, new String[0]));
// 2 misses in getOrDefault(), frontMap is checked twice
assertEquals(cacheStats.getCacheMisses(), cInitialMisses + 3);
}
}
51 changes: 41 additions & 10 deletions prj/test/functional/near/src/test/java/near/NearCacheTests.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates.
* Copyright (c) 2000, 2021, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/

package near;

import com.tangosol.net.events.EventInterceptor;
import com.tangosol.net.events.annotation.EntryEvents;
import com.tangosol.net.events.annotation.Interceptor;
import com.tangosol.net.events.partition.cache.EntryEvent;

import com.oracle.bedrock.options.Timeout;

import com.oracle.bedrock.testsupport.deferred.Eventually;

import com.tangosol.net.AbstractInvocable;
import com.tangosol.net.CacheFactory;
import com.tangosol.net.InvocationService;
import com.tangosol.net.NamedCache;

import com.tangosol.net.cache.CacheStatistics;
import com.tangosol.net.cache.LocalCache;
import com.tangosol.net.cache.NearCache;

import com.tangosol.net.events.EventInterceptor;
import com.tangosol.net.events.annotation.EntryEvents;
import com.tangosol.net.events.annotation.Interceptor;
import com.tangosol.net.events.partition.cache.EntryEvent;

import com.tangosol.net.management.MBeanHelper;

import com.tangosol.util.BinaryEntry;

import com.tangosol.util.processor.NumberIncrementor;
import com.tangosol.util.processor.PropertyManipulator;

import common.AbstractFunctionalTest;
import common.TestCoh15021;

import data.Person;

import java.io.Serializable;

import java.util.HashMap;
Expand All @@ -39,9 +46,6 @@
import java.util.Random;
import java.util.Set;

import common.AbstractFunctionalTest;
import common.TestCoh15021;

import javax.management.MBeanServer;
import javax.management.ObjectName;

Expand All @@ -52,9 +56,15 @@
import org.junit.Test;

import static com.oracle.bedrock.deferred.DeferredHelper.invoking;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

/**
* NearCache tests
Expand Down Expand Up @@ -476,6 +486,27 @@ public void testSyntheticUpdate()
}
}

/**
* Test for COH-23095
*/
@Test
public void testCoh23095()
{
NearCache<Integer, Person> cache = (NearCache) getNamedCache("near-coh-23095");

CacheStatistics cacheStats = ((LocalCache) cache.getFrontMap()).getCacheStatistics();
long cInitialMisses = cacheStats.getCacheMisses();

cache.computeIfAbsent(2,
person -> new Person("4321", "frist", "lats", 2000, null, new String[0]));
assertEquals(cacheStats.getCacheMisses(), cInitialMisses + 1);

cache.getOrDefault(3,
new Person("5678", "a", "b", 1980, null, new String[0]));
// 2 misses in getOrDefault(), frontMap is checked twice
assertEquals(cacheStats.getCacheMisses(), cInitialMisses + 3);
}

/**
* Test runner
*/
Expand Down

0 comments on commit 09f1827

Please sign in to comment.