r229 - in trunk/wikitty-api/src: main/java/org/nuiton/wikitty test/java/org/nuiton/wikitty/cache
Author: bleny Date: 2010-08-04 18:16:03 +0200 (Wed, 04 Aug 2010) New Revision: 229 Url: http://nuiton.org/repositories/revision/wikitty/229 Log: #793 cache avec politique de copie syst?\195?\169matique en option + test Modified: trunk/wikitty-api/src/main/java/org/nuiton/wikitty/WikittyServiceCached.java trunk/wikitty-api/src/test/java/org/nuiton/wikitty/cache/WikittyServiceCachedTest.java Modified: trunk/wikitty-api/src/main/java/org/nuiton/wikitty/WikittyServiceCached.java =================================================================== --- trunk/wikitty-api/src/main/java/org/nuiton/wikitty/WikittyServiceCached.java 2010-08-04 15:32:36 UTC (rev 228) +++ trunk/wikitty-api/src/main/java/org/nuiton/wikitty/WikittyServiceCached.java 2010-08-04 16:16:03 UTC (rev 229) @@ -51,11 +51,26 @@ */ static public final String WIKITTY_CACHE_LISTENEVENTS_OPTION = "wikitty.service.cache.listenevents"; + /** used as property name in props given in the constructor */ + public static final String WIKITTY_CACHE_POLICY_OPTION = "wikitty.service.cache.allwaysRestoreCopies"; + /** Cache. */ protected WikittyCache cache = null; /** Delegated wikitty service implementation. */ protected WikittyService ws; + + /** cache policy (configuration) + * + * if true the cache will always restore copies of the wikitty + * if false the cache will restore wikitties that do a lazy copy when the + * wikitty is modified ({@link WikittyCopyOnWrite} instances) + * + * default set to false. To change this value, use + * {@link WikittyServiceCached#WikittyServiceCached(WikittyService, Properties)} + * and set the good property. + */ + protected boolean allwaysRestoreCopies = false; /** * Default constructor. @@ -76,8 +91,32 @@ this.ws = ws; cache = new WikittyCache(); registerWikittyServiceListener(props); + + // reading configuration and set allwaysRestoreCopies accordingly + if (props != null && props.containsKey(WIKITTY_CACHE_POLICY_OPTION)) { + allwaysRestoreCopies = Boolean.parseBoolean( + props.getProperty(WIKITTY_CACHE_POLICY_OPTION)); + } } + /** wrap the wikitty or copy it according to allwaysRestoreCopies value */ + protected Wikitty wrapWikitty(Wikitty wikitty) { + Wikitty result = null; + if (allwaysRestoreCopies) { + try { + result = wikitty.clone(); + } catch (CloneNotSupportedException e) { + log.error("unable to clone " + wikitty, e); + } + } + + // if allwaysRestoreCopies is false and above clone failed + if (result == null) { + result = new WikittyCopyOnWrite(wikitty); + } + return result; + } + /** * Add cache as service listener if configuration request it. * @@ -297,7 +336,7 @@ } // wrap wikitty to prevent conflict in cache modification - result = new WikittyCopyOnWrite(result); + result = wrapWikitty(result); return result; } @@ -336,7 +375,7 @@ // wrap the resulting wikitties to prevent cache conflicts ArrayList<Wikitty> result = new ArrayList<Wikitty>(); for (Wikitty w : tmp) { - result.add(new WikittyCopyOnWrite(w)); + result.add(wrapWikitty(w)); } return result; @@ -379,7 +418,7 @@ // wrap the resulting wikitties to prevent cache conflicts ArrayList<Wikitty> result = new ArrayList<Wikitty>(); for (Wikitty w : tmp) { - result.add(new WikittyCopyOnWrite(w)); + result.add(wrapWikitty(w)); } return result; Modified: trunk/wikitty-api/src/test/java/org/nuiton/wikitty/cache/WikittyServiceCachedTest.java =================================================================== --- trunk/wikitty-api/src/test/java/org/nuiton/wikitty/cache/WikittyServiceCachedTest.java 2010-08-04 15:32:36 UTC (rev 228) +++ trunk/wikitty-api/src/test/java/org/nuiton/wikitty/cache/WikittyServiceCachedTest.java 2010-08-04 16:16:03 UTC (rev 229) @@ -2,9 +2,12 @@ import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertTrue; import java.util.ArrayList; import java.util.List; +import java.util.Properties; import org.junit.Before; import org.junit.Test; @@ -37,17 +40,17 @@ /** create a service, an extension, a wikitty, login, and store wikitty */ @Before public void setUp() throws Exception { - service = new WikittyServiceCached(new WikittyServiceInMemory()); - token = service.login(null, null); - extension = ExtensionFactory.create(EXT_NAME, "1") .addField(FIELD_NAME, TYPE.STRING) .extension(); aWikitty = new WikittyImpl(); aWikitty.addExtension(extension); aWikitty.setField(EXT_NAME, FIELD_NAME, "myvalue"); - service.store(token, aWikitty); + + service = new WikittyServiceCached(new WikittyServiceInMemory()); + token = service.login(null, null); + service.store(token, aWikitty); } /** setting a field value doesn't corrupt cache */ @@ -88,4 +91,23 @@ assertEquals("myvalue", anotherWikitty.getFieldAsString(EXT_NAME, FIELD_NAME)); } + /** this test is the same but service cache policy is changed */ + @Test + public void testRestoreAllwaysCopyPolicy() throws Exception { + + Properties props = new Properties(); + props.setProperty(WikittyServiceCached.WIKITTY_CACHE_POLICY_OPTION, "true"); + + service = new WikittyServiceCached(new WikittyServiceInMemory(), props); + token = service.login(null, null); + service.store(token, aWikitty); + + // restoring two times the same wikitty should produces two different copies + Wikitty anotherWikitty = service.restore(token, aWikitty.getId()); + Wikitty yetAnotherWikitty = service.restore(token, anotherWikitty.getId()); + + assertEquals(anotherWikitty, yetAnotherWikitty); + assertNotSame(anotherWikitty, yetAnotherWikitty); // two different objects + } + }
participants (1)
-
bleny@users.nuiton.org