Author: genman
Date: 2008-12-07 13:53:38 -0500 (Sun, 07 Dec 2008)
New Revision: 7261
Added:
core/trunk/src/test/java/org/jboss/cache/loader/UnnecessaryLoadingSetDataTest.java
Modified:
core/trunk/src/main/java/org/jboss/cache/interceptors/CacheLoaderInterceptor.java
core/trunk/src/main/java/org/jboss/cache/loader/jdbm/JdbmCacheLoader.java
core/trunk/src/test/java/org/jboss/cache/loader/CacheLoaderTestsBase.java
core/trunk/src/test/java/org/jboss/cache/loader/JdbmCacheLoader2Test.java
core/trunk/src/test/java/org/jboss/cache/loader/JdbmCacheLoaderTest.java
Log:
JBCACHE-1442
Test for unncessary loading of setData for MVCC (Pessimistic is a TODO)
Fix issue with JDBM not clearing existing keys with put(Map) call
Modified:
core/trunk/src/main/java/org/jboss/cache/interceptors/CacheLoaderInterceptor.java
===================================================================
---
core/trunk/src/main/java/org/jboss/cache/interceptors/CacheLoaderInterceptor.java 2008-12-06
00:47:32 UTC (rev 7260)
+++
core/trunk/src/main/java/org/jboss/cache/interceptors/CacheLoaderInterceptor.java 2008-12-07
18:53:38 UTC (rev 7261)
@@ -113,9 +113,16 @@
@Override
public Object visitPutDataMapCommand(InvocationContext ctx, PutDataMapCommand command)
throws Throwable
{
- if (command.getFqn() != null && !command.isErase())
+ if (command.getFqn() != null)
{
- loadIfNeeded(ctx, command.getFqn(), null, true, true, false, false, false,
false, true);
+ if (command.isErase())
+ {
+ replace(ctx, command.getFqn());
+ }
+ else
+ {
+ loadIfNeeded(ctx, command.getFqn(), null, true, true, false, false, false,
false, true);
+ }
}
return invokeNextInterceptor(ctx, command);
}
@@ -261,6 +268,17 @@
}
return invokeNextInterceptor(ctx, command);
}
+
+ private void replace(InvocationContext ctx, Fqn fqn) throws InterruptedException
+ {
+ NodeSPI n = helper.wrapNodeForReading(ctx, fqn, true);
+ if (n instanceof NullMarkerNode)
+ {
+ ctx.getLookedUpNodes().remove(fqn);
+ }
+ n = helper.wrapNodeForWriting(ctx, fqn, true, true, true, false, false);
+ n.setDataLoaded(true);
+ }
private void loadIfNeeded(InvocationContext ctx, Fqn fqn, Object key, boolean allKeys,
boolean initNode, boolean acquireWriteLock, boolean recursive, boolean isMove, boolean
bypassLoadingData, boolean shouldLoadIfNodeIsNull) throws Throwable
{
Modified: core/trunk/src/main/java/org/jboss/cache/loader/jdbm/JdbmCacheLoader.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/loader/jdbm/JdbmCacheLoader.java 2008-12-06
00:47:32 UTC (rev 7260)
+++ core/trunk/src/main/java/org/jboss/cache/loader/jdbm/JdbmCacheLoader.java 2008-12-07
18:53:38 UTC (rev 7261)
@@ -462,12 +462,35 @@
{
return;
}
+
+ Fqn keys = keys(name);
+ Tuple t = new Tuple();
+ List<Object> removeList = new ArrayList<Object>();
+ synchronized (tree)
+ {
+ TupleBrowser browser = tree.browse(keys);
+ while (browser.getNext(t))
+ {
+ Fqn fqn = (Fqn) t.getKey();
+ if (!fqn.isChildOf(keys))
+ {
+ break;
+ }
+ Object k = fqn.getLastElement();
+ if (!values.containsKey(nullUnmask(k)))
+ removeList.add(fqn);
+ }
+ }
for (Map.Entry me : values.entrySet())
{
Fqn rec = key(name, me.getKey());
insert(rec, nullMask(me.getValue()));
}
+ for (Object o : removeList)
+ {
+ tree.remove(o);
+ }
}
/**
Modified: core/trunk/src/test/java/org/jboss/cache/loader/CacheLoaderTestsBase.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/loader/CacheLoaderTestsBase.java 2008-12-06
00:47:32 UTC (rev 7260)
+++ core/trunk/src/test/java/org/jboss/cache/loader/CacheLoaderTestsBase.java 2008-12-07
18:53:38 UTC (rev 7261)
@@ -2467,19 +2467,26 @@
public void testSetData() throws Exception
{
+ log.info("testSetData");
CacheSPI<Object, Object> cache = cacheTL.get();
Fqn key = Fqn.fromElements("key");
Map<Object, Object> map = new HashMap<Object, Object>();
+ Map<Object, Object> loaderMap;
+ CacheLoader loader = loaderTL.get();
map.put("a", "a");
map.put("c", "c");
+ log.info("PUT");
cache.put(key, "x", "x");
cache.setData(key, map);
+ assertEquals(map, cache.getData(key));
+ log.info("GET");
+ loaderMap = loader.get(key);
+ assertEquals(map, loaderMap);
assertNull(cache.get(key, "x"));
assertEquals("c", cache.get(key, "c"));
assertEquals("a", cache.get(key, "a"));
- CacheLoader loader = loaderTL.get();
- Map<Object, Object> loaderMap = loader.get(key);
+ loaderMap = loader.get(key);
assertEquals(map, loaderMap);
}
Modified: core/trunk/src/test/java/org/jboss/cache/loader/JdbmCacheLoader2Test.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/loader/JdbmCacheLoader2Test.java 2008-12-06
00:47:32 UTC (rev 7260)
+++ core/trunk/src/test/java/org/jboss/cache/loader/JdbmCacheLoader2Test.java 2008-12-07
18:53:38 UTC (rev 7261)
@@ -20,7 +20,7 @@
String tmpDir = System.getProperty("java.io.tmpdir", "/tmp");
String threadId = Thread.currentThread().getName();
String tmpCLLoc = tmpDir + "/JBossCache-JdbmCacheLoader2Test-" +
threadId;
-
cache.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("",
"org.jboss.cache.loader.jdbm.JdbmCacheLoader",
+
cache.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("",
"org.jboss.cache.loader.jdbm.JdbmCacheLoader2",
"location=" + tmpCLLoc, false, true, false));
TestingUtil.recursiveFileRemove(tmpCLLoc);
}
Modified: core/trunk/src/test/java/org/jboss/cache/loader/JdbmCacheLoaderTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/loader/JdbmCacheLoaderTest.java 2008-12-06
00:47:32 UTC (rev 7260)
+++ core/trunk/src/test/java/org/jboss/cache/loader/JdbmCacheLoaderTest.java 2008-12-07
18:53:38 UTC (rev 7261)
@@ -22,4 +22,12 @@
cache.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("",
"org.jboss.cache.loader.jdbm.JdbmCacheLoader", "location=" + tmpCLLoc,
false, true, false));
TestingUtil.recursiveFileRemove(tmpCLLoc);
}
+
+ public void testCacheLoaderThreadSafety() throws Exception
+ {
+ }
+
+ public void testCacheLoaderThreadSafetyMultipleFqns() throws Exception
+ {
+ }
}
Added: core/trunk/src/test/java/org/jboss/cache/loader/UnnecessaryLoadingSetDataTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/loader/UnnecessaryLoadingSetDataTest.java
(rev 0)
+++
core/trunk/src/test/java/org/jboss/cache/loader/UnnecessaryLoadingSetDataTest.java 2008-12-07
18:53:38 UTC (rev 7261)
@@ -0,0 +1,153 @@
+package org.jboss.cache.loader;
+
+import static org.easymock.EasyMock.*;
+import static org.testng.AssertJUnit.assertFalse;
+import static org.testng.AssertJUnit.assertTrue;
+import static org.testng.AssertJUnit.assertEquals;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.jboss.cache.CacheSPI;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.NodeSPI;
+import org.jboss.cache.UnitTestCacheFactory;
+import org.jboss.cache.config.CacheLoaderConfig;
+import org.jboss.cache.config.Configuration.NodeLockingScheme;
+import org.jboss.cache.util.CachePrinter;
+import org.jboss.cache.util.TestingUtil;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+/**
+ * TODO merge with {@link UnnecessaryLoadingTest}.
+ * @author Elias Ross
+ * @since 3.0.0
+ */
+@Test(groups = {"functional", "mvcc"})
+public class UnnecessaryLoadingSetDataTest
+{
+ private CacheSPI<Object, Object> cache;
+ private CacheLoader mockCacheLoader;
+ private Fqn parent = Fqn.fromString("/parent");
+
+ @DataProvider(name = "locking")
+ public Object[][] createData1() {
+ return new Object[][] {
+ // TODO
+ // { NodeLockingScheme.PESSIMISTIC },
+ { NodeLockingScheme.MVCC },
+ };
+ }
+
+ private void setUp(NodeLockingScheme locking) throws Exception
+ {
+ cache = (CacheSPI<Object, Object>) new UnitTestCacheFactory<Object,
Object>().createCache(false);
+ CacheLoaderConfig clc = new CacheLoaderConfig();
+ CacheLoaderConfig.IndividualCacheLoaderConfig iclc = new
CacheLoaderConfig.IndividualCacheLoaderConfig();
+ clc.addIndividualCacheLoaderConfig(iclc);
+ cache.getConfiguration().setCacheLoaderConfig(clc);
+ cache.getConfiguration().setNodeLockingScheme(locking);
+ mockCacheLoader = createMockCacheLoader();
+
+ iclc.setCacheLoader(mockCacheLoader);
+ cache.start();
+
+ reset(mockCacheLoader);
+ }
+
+ public static CacheLoader createMockCacheLoader() throws Exception {
+ CacheLoader mockCacheLoader = createMock(CacheLoader.class);
+
+ expect(mockCacheLoader.getConfig()).andReturn(null).anyTimes();
+ mockCacheLoader.setCache((CacheSPI) anyObject());
+ expectLastCall().anyTimes();
+ mockCacheLoader.setConfig((CacheLoaderConfig.IndividualCacheLoaderConfig)
anyObject());
+ expectLastCall().anyTimes();
+ mockCacheLoader.create();
+ expectLastCall().anyTimes();
+ mockCacheLoader.start();
+ expectLastCall().anyTimes();
+ mockCacheLoader.stop();
+ expectLastCall().anyTimes();
+ mockCacheLoader.destroy();
+ expectLastCall().anyTimes();
+ replay(mockCacheLoader);
+ return mockCacheLoader;
+ }
+
+ @AfterMethod(alwaysRun = true)
+ public void tearDown() throws Exception
+ {
+ reset(mockCacheLoader);
+ expect(mockCacheLoader.getConfig()).andReturn(null).anyTimes();
+ mockCacheLoader.setCache((CacheSPI) anyObject());
+ expectLastCall().anyTimes();
+ mockCacheLoader.setConfig((CacheLoaderConfig.IndividualCacheLoaderConfig)
anyObject());
+ expectLastCall().anyTimes();
+ mockCacheLoader.create();
+ expectLastCall().anyTimes();
+ mockCacheLoader.start();
+ expectLastCall().anyTimes();
+ mockCacheLoader.stop();
+ expectLastCall().anyTimes();
+ mockCacheLoader.destroy();
+ expectLastCall().anyTimes();
+ replay(mockCacheLoader);
+
+ TestingUtil.killCaches(cache);
+ cache = null;
+ }
+
+ protected void assertDataLoaded(Fqn f)
+ {
+ assertTrue("Data should be loaded for node " + f, cache.peek(f,
false).isDataLoaded());
+ }
+
+ protected void assertDataNotLoaded(Fqn f)
+ {
+ NodeSPI n = cache.peek(f, true);
+ assertFalse("Data should not be loaded for node " + f, n != null
&& n.isDataLoaded());
+ }
+
+ @Test(dataProvider="locking")
+ public void testDontLoadWithSetData(NodeLockingScheme locking) throws Exception
+ {
+ System.err.println(locking);
+ System.err.println(locking);
+ setUp(locking);
+
+ Map<Object, Object> m0 = new HashMap<Object, Object>();
+ m0.put("replace", "replace");
+ Map<Object, Object> m1 = new HashMap<Object, Object>();
+ m1.put("new", "new");
+
+ mockCacheLoader.put(eq(parent), eq(m0));
+ mockCacheLoader.put(eq(parent), eq(m1));
+ mockCacheLoader.put(eq(parent), eq(m0));
+ mockCacheLoader.get(parent);
+ expectLastCall().andStubThrow(new IllegalStateException("no need to call
get()"));
+ mockCacheLoader.exists(parent);
+ expectLastCall().andStubThrow(new IllegalStateException("no need to call
exists()"));
+ replay(mockCacheLoader);
+
+ assertDataNotLoaded(parent);
+ cache.setData(parent, m0);
+ assertDataLoaded(parent);
+ cache.setData(parent, m1);
+ assertEquals(m1, cache.peek(parent, false).getData());
+
+ // force removal, see if load happens
+ cache.evict(parent);
+ cache.setData(parent, m0);
+ // assertDataLoaded(parent);
+ assertEquals(m0, cache.peek(parent, false).getData());
+
+ verify(mockCacheLoader);
+ CachePrinter.printCacheDetails(cache);
+ cache.toString();
+ }
+
+}
\ No newline at end of file
Property changes on:
core/trunk/src/test/java/org/jboss/cache/loader/UnnecessaryLoadingSetDataTest.java
___________________________________________________________________
Name: svn:mergeinfo
+