diff --git a/ebean-core/src/main/java/io/ebeaninternal/api/LoadManyRequest.java b/ebean-core/src/main/java/io/ebeaninternal/api/LoadManyRequest.java index 41cd37de9..392a16b6c 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/api/LoadManyRequest.java +++ b/ebean-core/src/main/java/io/ebeaninternal/api/LoadManyRequest.java @@ -120,7 +120,7 @@ public final class LoadManyRequest extends LoadRequest { log.log(DEBUG, "BeanCollection after lazy load was empty. type:{0} id:{1} owner:{2}", ownerBean.getClass().getName(), parentId, ownerBean); } } else if (loadCache && many.isUseCache()) { - desc.cacheManyPropPut(many, bc, desc.getId(bc.getOwnerBean())); + desc.cacheManyPropPut(many, bc, desc.cacheKeyForBean(bc.getOwnerBean())); } } } diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/cache/CacheChangeSet.java b/ebean-core/src/main/java/io/ebeaninternal/server/cache/CacheChangeSet.java index 31fa0e19b..38818bbfd 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/cache/CacheChangeSet.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/cache/CacheChangeSet.java @@ -91,15 +91,15 @@ public final class CacheChangeSet { /** * Add many property remove. */ - public void addManyRemove(BeanDescriptor desc, String manyProperty, Object parentId) { - many(desc, manyProperty).addRemove(parentId); + public void addManyRemove(BeanDescriptor desc, String manyProperty, String parentKey) { + many(desc, manyProperty).addRemove(parentKey); } /** * Add many property put. */ - public void addManyPut(BeanDescriptor desc, String manyProperty, Object parentId, CachedManyIds entry) { - many(desc, manyProperty).addPut(parentId, entry); + public void addManyPut(BeanDescriptor desc, String manyProperty, String parentKey, CachedManyIds entry) { + many(desc, manyProperty).addPut(parentKey, entry); } /** @@ -164,8 +164,8 @@ public final class CacheChangeSet { private static final class ManyChange implements CacheChange { final ManyKey key; - final Set removes = new HashSet<>(); - final Map puts = new LinkedHashMap<>(); + final Set removes = new HashSet<>(); + final Map puts = new LinkedHashMap<>(); boolean clear; ManyChange(ManyKey key) { @@ -183,17 +183,17 @@ public final class CacheChangeSet { /** * Remove entry for the given parentId. */ - void addRemove(Object parentId) { + void addRemove(String parentKey) { if (!clear) { - removes.add(parentId); + removes.add(parentKey); } } /** * Put entry for the given parentId. */ - void addPut(Object parentId, CachedManyIds entry) { - puts.put(parentId, entry); + void addPut(String parentKey, CachedManyIds entry) { + puts.put(parentKey, entry); } @Override @@ -201,11 +201,11 @@ public final class CacheChangeSet { if (clear) { key.cacheClear(); } else { - for (Map.Entry entry : puts.entrySet()) { + for (Map.Entry entry : puts.entrySet()) { key.cachePut(entry.getKey(), entry.getValue()); } - for (Object parentId : removes) { - key.cacheRemove(parentId); + for (String parentKey : removes) { + key.cacheRemove(parentKey); } } } @@ -241,12 +241,12 @@ public final class CacheChangeSet { desc.cacheManyPropClear(manyProperty); } - void cachePut(Object parentId, CachedManyIds entry) { - desc.cacheManyPropPut(manyProperty, parentId, entry); + void cachePut(String parentKey, CachedManyIds entry) { + desc.cacheManyPropPut(manyProperty, parentKey, entry); } - void cacheRemove(Object parentId) { - desc.cacheManyPropRemove(manyProperty, parentId); + void cacheRemove(String parentKey) { + desc.cacheManyPropRemove(manyProperty, parentKey); } } } diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/core/DefaultBeanLoader.java b/ebean-core/src/main/java/io/ebeaninternal/server/core/DefaultBeanLoader.java index 38a0ebe4d..5c71229b9 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/core/DefaultBeanLoader.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/core/DefaultBeanLoader.java @@ -72,7 +72,8 @@ final class DefaultBeanLoader { if (ebi.isReadOnly()) { readOnly = Boolean.TRUE; } - if (parentDesc.cacheManyPropLoad(many, beanCollection, parentId, readOnly)) { + final String parentKey = parentDesc.cacheKey(parentId); + if (parentDesc.cacheManyPropLoad(many, beanCollection, parentKey, readOnly)) { return; } } @@ -113,7 +114,8 @@ final class DefaultBeanLoader { log.log(DEBUG, "BeanCollection after load was empty. Owner:{0}", beanCollection.getOwnerBean()); } } else if (useManyIdCache) { - parentDesc.cacheManyPropPut(many, beanCollection, parentId); + final String parentKey = parentDesc.cacheKey(parentId); + parentDesc.cacheManyPropPut(many, beanCollection, parentKey); } } } diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptor.java b/ebean-core/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptor.java index eddf39e5d..8e4987dc4 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptor.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptor.java @@ -1202,26 +1202,26 @@ public class BeanDescriptor implements BeanType, STreeType, SpiBeanType { /** * Try to load the beanCollection from cache return true if successful. */ - public boolean cacheManyPropLoad(BeanPropertyAssocMany many, BeanCollection bc, Object parentId, Boolean readOnly) { - return cacheHelp.manyPropLoad(many, bc, parentId, readOnly); + public boolean cacheManyPropLoad(BeanPropertyAssocMany many, BeanCollection bc, String parentKey, Boolean readOnly) { + return cacheHelp.manyPropLoad(many, bc, parentKey, readOnly); } /** * Put the beanCollection into the cache. */ - public void cacheManyPropPut(BeanPropertyAssocMany many, BeanCollection bc, Object parentId) { - cacheHelp.manyPropPut(many, bc, parentId); + public void cacheManyPropPut(BeanPropertyAssocMany many, BeanCollection bc, String parentKey) { + cacheHelp.manyPropPut(many, bc, parentKey); } /** * Update the bean collection entry in the cache. */ - public void cacheManyPropPut(String name, Object parentId, CachedManyIds entry) { - cacheHelp.cachePutManyIds(parentId, name, entry); + public void cacheManyPropPut(String name, String parentKey, CachedManyIds entry) { + cacheHelp.cachePutManyIds(name, parentKey, entry); } - public void cacheManyPropRemove(String propertyName, Object parentId) { - cacheHelp.manyPropRemove(propertyName, parentId); + public void cacheManyPropRemove(String propertyName, String parentKey) { + cacheHelp.manyPropRemove(propertyName, parentKey); } public void cacheManyPropClear(String propertyName) { @@ -2096,7 +2096,7 @@ public class BeanDescriptor implements BeanType, STreeType, SpiBeanType { /** * Return the cache key for the given bean (based on id value). */ - String cacheKeyForBean(EntityBean bean) { + public String cacheKeyForBean(EntityBean bean) { return cacheKey(idProperty.getValue(bean)); } @@ -2112,11 +2112,6 @@ public class BeanDescriptor implements BeanType, STreeType, SpiBeanType { return getId((EntityBean) bean); } -// @Override -// public Object beanId(T bean) { -// return getId((EntityBean) bean); -// } - /** * Return the Id value for the bean with embeddedId beans converted into maps. *

diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptorCacheHelp.java b/ebean-core/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptorCacheHelp.java index eba2da8ee..9bf0ec753 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptorCacheHelp.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptorCacheHelp.java @@ -226,13 +226,12 @@ final class BeanDescriptorCacheHelp { queryCache.put(id, entry); } - - void manyPropRemove(String propertyName, Object parentId) { + void manyPropRemove(String propertyName, String parentKey) { ServerCache collectionIdsCache = cacheManager.getCollectionIdsCache(beanType, propertyName); if (manyLog.isLoggable(TRACE)) { - manyLog.log(TRACE, " REMOVE {0}({1}).{2}", cacheName, parentId, propertyName); + manyLog.log(TRACE, " REMOVE {0}({1}).{2}", cacheName, parentKey, propertyName); } - collectionIdsCache.remove(parentId); + collectionIdsCache.remove(parentKey); } void manyPropClear(String propertyName) { @@ -246,15 +245,15 @@ final class BeanDescriptorCacheHelp { /** * Return the CachedManyIds for a given bean many property. Returns null if not in the cache. */ - private CachedManyIds manyPropGet(Object parentId, String propertyName) { + private CachedManyIds manyPropGet(String propertyName, String parentKey) { ServerCache collectionIdsCache = cacheManager.getCollectionIdsCache(beanType, propertyName); - CachedManyIds entry = (CachedManyIds) collectionIdsCache.get(parentId); + CachedManyIds entry = (CachedManyIds) collectionIdsCache.get(parentKey); if (entry == null) { if (manyLog.isLoggable(TRACE)) { - manyLog.log(TRACE, " GET {0}({1}).{2} - cache miss", cacheName, parentId, propertyName); + manyLog.log(TRACE, " GET {0}({1}).{2} - cache miss", cacheName, parentKey, propertyName); } } else if (manyLog.isLoggable(DEBUG)) { - manyLog.log(DEBUG, " GET {0}({1}).{2} - hit", cacheName, parentId, propertyName); + manyLog.log(DEBUG, " GET {0}({1}).{2} - hit", cacheName, parentKey, propertyName); } return entry; } @@ -262,12 +261,12 @@ final class BeanDescriptorCacheHelp { /** * Try to load the bean collection from cache return true if successful. */ - boolean manyPropLoad(BeanPropertyAssocMany many, BeanCollection bc, Object parentId, Boolean readOnly) { + boolean manyPropLoad(BeanPropertyAssocMany many, BeanCollection bc, String parentKey, Boolean readOnly) { if (many.isElementCollection()) { // held as part of the bean cache so skip return false; } - CachedManyIds entry = manyPropGet(parentId, many.name()); + CachedManyIds entry = manyPropGet(many.name(), parentKey); if (entry == null) { // not in cache so return unsuccessful return false; @@ -293,9 +292,9 @@ final class BeanDescriptorCacheHelp { /** * Put the beanCollection into the cache. */ - void manyPropPut(BeanPropertyAssocMany many, Object details, Object parentId) { + void manyPropPut(BeanPropertyAssocMany many, Object details, String parentKey) { if (many.isElementCollection()) { - CachedBeanData data = (CachedBeanData) beanCache.get(parentId); + CachedBeanData data = (CachedBeanData) beanCache.get(parentKey); if (data != null) { try { // add as JSON to bean cache @@ -305,9 +304,9 @@ final class BeanDescriptorCacheHelp { CachedBeanData newData = data.update(changes, data.getVersion()); if (beanLog.isLoggable(DEBUG)) { - beanLog.log(DEBUG, " UPDATE {0}({1}) changes:{2}", cacheName, parentId, changes); + beanLog.log(DEBUG, " UPDATE {0}({1}) changes:{2}", cacheName, parentKey, changes); } - beanCache.put(parentId, newData); + beanCache.put(parentKey, newData); } catch (IOException e) { log.log(ERROR, "Error updating L2 cache", e); } @@ -315,17 +314,17 @@ final class BeanDescriptorCacheHelp { } else { CachedManyIds entry = createManyIds(many, details); if (entry != null) { - cachePutManyIds(parentId, many.name(), entry); + cachePutManyIds(many.name(), parentKey, entry); } } } - void cachePutManyIds(Object parentId, String manyName, CachedManyIds entry) { + void cachePutManyIds(String manyName, String parentKey, CachedManyIds entry) { ServerCache collectionIdsCache = cacheManager.getCollectionIdsCache(beanType, manyName); if (manyLog.isLoggable(DEBUG)) { - manyLog.log(DEBUG, " PUT {0}({1}).{2} - ids:{3}", cacheName, parentId, manyName, entry); + manyLog.log(DEBUG, " PUT {0}({1}).{2} - ids:{3}", cacheName, parentKey, manyName, entry); } - collectionIdsCache.put(parentId, entry); + collectionIdsCache.put(parentKey, entry); } private CachedManyIds createManyIds(BeanPropertyAssocMany many, Object details) { @@ -828,7 +827,8 @@ final class BeanDescriptorCacheHelp { Object details = many.getValue(updateRequest.entityBean()); CachedManyIds entry = createManyIds(many, details); if (entry != null) { - changeSet.addManyPut(desc, many.name(), id, entry); + final String parentKey = desc.cacheKey(id); + changeSet.addManyPut(desc, many.name(), parentKey, entry); } } } diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/deploy/BeanPropertyAssocOne.java b/ebean-core/src/main/java/io/ebeaninternal/server/deploy/BeanPropertyAssocOne.java index a6a706cc6..bcc75aa4c 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/deploy/BeanPropertyAssocOne.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/deploy/BeanPropertyAssocOne.java @@ -197,7 +197,8 @@ public class BeanPropertyAssocOne extends BeanPropertyAssoc implements STr if (assocBean != null) { Object parentId = targetDescriptor.id(assocBean); if (parentId != null) { - changeSet.addManyRemove(targetDescriptor, relationshipProperty.name(), parentId); + final String parentKey = targetDescriptor.cacheKey(parentId); + changeSet.addManyRemove(targetDescriptor, relationshipProperty.name(), parentKey); } } } diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/loadcontext/DLoadManyContext.java b/ebean-core/src/main/java/io/ebeaninternal/server/loadcontext/DLoadManyContext.java index 68bd61d81..15403f8b1 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/loadcontext/DLoadManyContext.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/loadcontext/DLoadManyContext.java @@ -205,7 +205,8 @@ final class DLoadManyContext extends DLoadBaseContext implements LoadManyContext EntityBean ownerBean = bc.getOwnerBean(); BeanDescriptor parentDesc = context.desc.descriptor(ownerBean.getClass()); Object parentId = parentDesc.getId(ownerBean); - if (parentDesc.cacheManyPropLoad(context.property, bc, parentId, context.parent.isReadOnly())) { + final String parentKey = parentDesc.cacheKey(parentId); + if (parentDesc.cacheManyPropLoad(context.property, bc, parentKey, context.parent.isReadOnly())) { // we loaded the bean collection from cache so remove it from the buffer for (int i = 0; i < list.size(); i++) { // find it using instance equality - avoiding equals() and potential deadlock issue diff --git a/ebean-test/src/test/java/org/tests/cache/TestCacheCollectionIds.java b/ebean-test/src/test/java/org/tests/cache/TestCacheCollectionIds.java index fd6696f4b..65c2efb13 100644 --- a/ebean-test/src/test/java/org/tests/cache/TestCacheCollectionIds.java +++ b/ebean-test/src/test/java/org/tests/cache/TestCacheCollectionIds.java @@ -14,13 +14,12 @@ import java.util.List; import static org.junit.jupiter.api.Assertions.*; -public class TestCacheCollectionIds extends BaseTestCase { +class TestCacheCollectionIds extends BaseTestCase { - private ServerCacheManager cacheManager = DB.cacheManager(); + private final ServerCacheManager cacheManager = DB.cacheManager(); @Test - public void test() { - + void test() { ResetBasicData.reset(); awaitL2Cache(); @@ -75,7 +74,6 @@ public class TestCacheCollectionIds extends BaseTestCase { } private int fetchCustomer(Integer id) { - Customer customer2 = DB.find(Customer.class, id); List contacts2 = customer2.getContacts(); @@ -91,7 +89,7 @@ public class TestCacheCollectionIds extends BaseTestCase { * When updating a ManyToMany relations also the collection cache must be updated. */ @Test - public void testUpdatingCollectionCacheForManyToManyRelations() { + void testUpdatingCollectionCacheForManyToManyRelations() { // arrange ResetBasicData.reset(); @@ -107,7 +105,7 @@ public class TestCacheCollectionIds extends BaseTestCase { dummyToLoad.getCountries().size(); ServerCache cachedBeanCountriesCache = cacheManager.collectionIdsCache(OCachedBean.class, "countries"); - CachedManyIds cachedManyIds = (CachedManyIds) cachedBeanCountriesCache.get(cachedBean.getId()); + CachedManyIds cachedManyIds = (CachedManyIds) cachedBeanCountriesCache.get(String.valueOf(cachedBean.getId())); // confirm the starting data and cache entry assertEquals(2, dummyToLoad.getCountries().size()); @@ -124,7 +122,7 @@ public class TestCacheCollectionIds extends BaseTestCase { // Get the data to assert/check against OCachedBean result = DB.find(OCachedBean.class, cachedBean.getId()); - cachedManyIds = (CachedManyIds) cachedBeanCountriesCache.get(result.getId()); + cachedManyIds = (CachedManyIds) cachedBeanCountriesCache.get(String.valueOf(result.getId())); // assert that data and cache both show correct data assertEquals(1, result.getCountries().size()); @@ -134,7 +132,7 @@ public class TestCacheCollectionIds extends BaseTestCase { } @Test - public void testChangingCollectionByEditingConnectedCachedBean() { + void testChangingCollectionByEditingConnectedCachedBean() { ResetBasicData.reset(); OCachedBean cachedBean = new OCachedBean(); @@ -174,7 +172,7 @@ public class TestCacheCollectionIds extends BaseTestCase { } @Test - public void testChangingCollectionByEditingConnectedNotCachedBean() { + void testChangingCollectionByEditingConnectedNotCachedBean() { ResetBasicData.reset(); OCachedBean cachedBean = new OCachedBean(); @@ -213,13 +211,12 @@ public class TestCacheCollectionIds extends BaseTestCase { DB.delete(cachedBean); } - /** * When updating a ManyToMany relations also the collection cache must be updated. * Alternate to above test where in this case the bean is dirty - loadedBean.setName("goodbye");. */ @Test - public void testUpdatingCollectionCacheForManyToManyRelationsWithUpdatedBean() { + void testUpdatingCollectionCacheForManyToManyRelationsWithUpdatedBean() { // arrange ResetBasicData.reset(); @@ -235,7 +232,7 @@ public class TestCacheCollectionIds extends BaseTestCase { dummyToLoad.getCountries().size(); ServerCache cachedBeanCountriesCache = cacheManager.collectionIdsCache(OCachedBean.class, "countries"); - CachedManyIds cachedManyIds = (CachedManyIds) cachedBeanCountriesCache.get(cachedBean.getId()); + CachedManyIds cachedManyIds = (CachedManyIds) cachedBeanCountriesCache.get(String.valueOf(cachedBean.getId())); // confirm the starting data and cache entry assertEquals(2, dummyToLoad.getCountries().size()); @@ -253,7 +250,7 @@ public class TestCacheCollectionIds extends BaseTestCase { // Get the data to assert/check against OCachedBean result = DB.find(OCachedBean.class, cachedBean.getId()); - cachedManyIds = (CachedManyIds) cachedBeanCountriesCache.get(result.getId()); + cachedManyIds = (CachedManyIds) cachedBeanCountriesCache.get(String.valueOf(result.getId())); // assert that data and cache both show correct data assertEquals(1, result.getCountries().size()); @@ -266,7 +263,7 @@ public class TestCacheCollectionIds extends BaseTestCase { * When updating a ManyToMany relations also the collection cache must be updated. */ @Test - public void testUpdatingCollectionCacheForManyToManyRelationsWithinStatelessUpdate() { + void testUpdatingCollectionCacheForManyToManyRelationsWithinStatelessUpdate() { // arrange ResetBasicData.reset(); @@ -289,7 +286,7 @@ public class TestCacheCollectionIds extends BaseTestCase { // assert that the cache contains the expected entry assertEquals(1, cachedBeanCountriesCache.size()); - CachedManyIds dummyEntry = (CachedManyIds) cachedBeanCountriesCache.get(dummyLoad.getId()); + CachedManyIds dummyEntry = (CachedManyIds) cachedBeanCountriesCache.get(String.valueOf(dummyLoad.getId())); assertNotNull(dummyEntry); assertEquals(2, dummyEntry.getIdList().size()); assertTrue(dummyEntry.getIdList().contains("NZ")); @@ -307,7 +304,7 @@ public class TestCacheCollectionIds extends BaseTestCase { assertEquals(1, cachedBeanCountriesCache.size()); - CachedManyIds cachedManyIds = (CachedManyIds) cachedBeanCountriesCache.get(update.getId()); + CachedManyIds cachedManyIds = (CachedManyIds) cachedBeanCountriesCache.get(String.valueOf(update.getId())); // assert cache updated assertEquals(1, cachedManyIds.getIdList().size()); @@ -321,8 +318,7 @@ public class TestCacheCollectionIds extends BaseTestCase { } @Test - public void testUpdating_noChange() throws InterruptedException { - + void testUpdating_noChange() throws InterruptedException { ResetBasicData.reset(); try (Transaction txn = DB.beginTransaction()) { @@ -355,7 +351,7 @@ public class TestCacheCollectionIds extends BaseTestCase { */ @Test @Disabled - public void testClearingCollectionCacheOnORMUpdate() { + void testClearingCollectionCacheOnORMUpdate() { // arrange ResetBasicData.reset(); @@ -390,7 +386,7 @@ public class TestCacheCollectionIds extends BaseTestCase { */ @Test @Disabled - public void testClearingCollectionCacheOnExternalModification() { + void testClearingCollectionCacheOnExternalModification() { // arrange ResetBasicData.reset();