Merge branch 'master' into wip/EntityBeanIntercept-interface

This commit is contained in:
Rob Bygrave authored and GitHub committed 2022-04-12 21:09:58 +12:00
commit 45003b3d5f
140 files changed
+1072 -1581

No files matched your search

+7 -7
View File
@@ -3,7 +3,7 @@
<parent>
<artifactId>ebean-parent</artifactId>
<groupId>io.ebean</groupId>
<version>13.0.1-SNAPSHOT</version>
<version>13.3.0-SNAPSHOT</version>
</parent>
<artifactId>ebean-core</artifactId>
@@ -41,19 +41,19 @@
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-api</artifactId>
<version>13.0.1-SNAPSHOT</version>
<version>13.3.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-core-type</artifactId>
<version>13.0.1-SNAPSHOT</version>
<version>13.3.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-externalmapping-api</artifactId>
<version>13.0.1-SNAPSHOT</version>
<version>13.3.0-SNAPSHOT</version>
</dependency>
<dependency>
@@ -144,21 +144,21 @@
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-platform-h2</artifactId>
<version>13.0.1-SNAPSHOT</version>
<version>13.3.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-platform-postgres</artifactId>
<version>13.0.1-SNAPSHOT</version>
<version>13.3.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-platform-sqlserver</artifactId>
<version>13.0.1-SNAPSHOT</version>
<version>13.3.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
@@ -2,14 +2,7 @@ package io.ebeaninternal.server.cache;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
/**
* List of changes to be applied to L2 cache.
@@ -3,12 +3,7 @@ package io.ebeaninternal.server.cache;
import io.ebean.annotation.Cache;
import io.ebean.annotation.CacheBeanTuning;
import io.ebean.annotation.CacheQueryTuning;
import io.ebean.cache.QueryCacheEntryValidate;
import io.ebean.cache.ServerCache;
import io.ebean.cache.ServerCacheConfig;
import io.ebean.cache.ServerCacheFactory;
import io.ebean.cache.ServerCacheOptions;
import io.ebean.cache.ServerCacheType;
import io.ebean.cache.*;
import io.ebean.config.CurrentTenantProvider;
import io.ebean.meta.MetricVisitor;
import io.ebean.util.AnnotationUtil;
@@ -3,7 +3,6 @@ package io.ebeaninternal.server.cache;
import io.ebean.BackgroundExecutor;
import io.ebean.cache.ServerCache;
import io.ebean.cache.ServerCacheStatistics;
import io.ebean.cache.TenantAwareKey;
import io.ebean.meta.MetricVisitor;
import io.ebean.metric.CountMetric;
import io.ebean.metric.MetricFactory;
@@ -12,11 +11,7 @@ import org.slf4j.LoggerFactory;
import java.io.Serializable;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
@@ -52,14 +47,12 @@ public class DefaultServerCache implements ServerCache {
private final int trimFrequency;
private final int maxIdleSecs;
private final int maxSecsToLive;
private final TenantAwareKey tenantAwareKey;
public DefaultServerCache(DefaultServerCacheConfig config) {
this.name = config.getName();
this.shortName = config.getShortName();
this.map = config.getMap();
this.maxSize = config.getMaxSize();
this.tenantAwareKey = new TenantAwareKey(config.getTenantProvider());
this.maxIdleSecs = config.getMaxIdleSecs();
this.maxSecsToLive = config.getMaxSecsToLive();
this.trimFrequency = config.determineTrimFrequency();
@@ -92,7 +85,7 @@ public class DefaultServerCache implements ServerCache {
}
@Override
public ServerCacheStatistics getStatistics(boolean reset) {
public ServerCacheStatistics statistics(boolean reset) {
ServerCacheStatistics cacheStats = new ServerCacheStatistics();
cacheStats.setCacheName(name);
cacheStats.setMaxSize(maxSize);
@@ -121,7 +114,7 @@ public class DefaultServerCache implements ServerCache {
}
@Override
public int getHitRatio() {
public int hitRatio() {
long mc = missCount.get(false);
long hc = hitCount.get(false);
long totalCount = hc + mc;
@@ -152,19 +145,12 @@ public class DefaultServerCache implements ServerCache {
map.clear();
}
/**
* Return the tenant aware key.
*/
protected Object key(Object id) {
return tenantAwareKey.key(id);
}
/**
* Return a value from the cache.
*/
@Override
public Object get(Object id) {
CacheEntry entry = getCacheEntry(id);
public Object get(Object key) {
CacheEntry entry = getCacheEntry(key);
if (entry == null) {
missCount.increment();
return null;
@@ -184,8 +170,8 @@ public class DefaultServerCache implements ServerCache {
/**
* Get the cache entry - override for query cache to validate dependent tables.
*/
protected CacheEntry getCacheEntry(Object id) {
final SoftReference<CacheEntry> ref = map.get(key(id));
protected CacheEntry getCacheEntry(Object key) {
final SoftReference<CacheEntry> ref = map.get(key);
return ref != null ? ref.get() : null;
}
@@ -198,8 +184,7 @@ public class DefaultServerCache implements ServerCache {
* Put a value into the cache.
*/
@Override
public void put(Object id, Object value) {
Object key = key(id);
public void put(Object key, Object value) {
map.put(key, new SoftReference<>(new CacheEntry(key, value)));
putCount.increment();
}
@@ -208,8 +193,8 @@ public class DefaultServerCache implements ServerCache {
* Remove an entry from the cache.
*/
@Override
public void remove(Object id) {
SoftReference<CacheEntry> entry = map.remove(key(id));
public void remove(Object key) {
SoftReference<CacheEntry> entry = map.remove(key);
if (entry != null && entry.get() != null) {
removeCount.increment();
}
@@ -3,7 +3,6 @@ package io.ebeaninternal.server.cache;
import io.ebean.cache.QueryCacheEntryValidate;
import io.ebean.cache.ServerCacheConfig;
import io.ebean.cache.ServerCacheOptions;
import io.ebean.config.CurrentTenantProvider;
import io.ebeaninternal.server.cache.DefaultServerCache.CacheEntry;
import java.lang.ref.SoftReference;
@@ -34,10 +33,6 @@ public final class DefaultServerCacheConfig {
this.maxSize = options.getMaxSize();
}
public CurrentTenantProvider getTenantProvider() {
return config.getTenantProvider();
}
public QueryCacheEntryValidate getQueryCacheEntryValidate() {
return config.getQueryCacheEntryValidate();
}
@@ -1,11 +1,7 @@
package io.ebeaninternal.server.cache;
import io.ebean.BackgroundExecutor;
import io.ebean.cache.ServerCache;
import io.ebean.cache.ServerCacheConfig;
import io.ebean.cache.ServerCacheFactory;
import io.ebean.cache.ServerCacheNotification;
import io.ebean.cache.ServerCacheNotify;
import io.ebean.cache.*;
/**
@@ -41,7 +37,7 @@ final class DefaultServerCacheFactory implements ServerCacheFactory {
if (executor != null) {
cache.periodicTrim(executor);
}
return cache;
return config.tenantAware(cache);
}
@Override
@@ -10,11 +10,7 @@ import io.ebeaninternal.server.deploy.DCacheRegion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
* Manages the bean and query caches.
@@ -27,8 +27,7 @@ public class DefaultServerQueryCache extends DefaultServerCache {
}
@Override
protected CacheEntry getCacheEntry(Object id) {
Object key = key(id);
protected CacheEntry getCacheEntry(Object key) {
final SoftReference<CacheEntry> ref = map.get(key);
CacheEntry entry = ref != null ? ref.get() : null;
if (entry == null) {
@@ -978,21 +978,11 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
return new DefaultRelationalQuery(this, sql.trim());
}
@Override
public SqlQuery createSqlQuery(String sql) {
return sqlQuery(sql);
}
@Override
public SqlUpdate sqlUpdate(String sql) {
return new DefaultSqlUpdate(this, sql.trim());
}
@Override
public SqlUpdate createSqlUpdate(String sql) {
return sqlUpdate(sql);
}
@Override
public CallableSql createCallableSql(String sql) {
return new DefaultCallableSql(this, sql.trim());
@@ -1376,11 +1366,6 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
}
}
@Override
public <T> Stream<T> findLargeStream(Query<T> query, Transaction transaction) {
return findStream(query, transaction);
}
@Override
public <T> Stream<T> findStream(Query<T> query, Transaction transaction) {
return toStream(findIterate(query, transaction));
@@ -261,16 +261,6 @@ public final class DefaultSqlUpdate implements Serializable, SpiSqlUpdate {
return this;
}
@Override
public SqlUpdate setParams(Object... values) {
return setParameters(values);
}
@Override
public SqlUpdate setNextParameter(Object value) {
return setParameter(value);
}
@Override
public SqlUpdate setParameters(Object... values) {
for (Object value : values) {
@@ -2114,10 +2114,10 @@ public class BeanDescriptor<T> implements BeanType<T>, STreeType, SpiBeanType {
return getId((EntityBean) bean);
}
@Override
public Object getBeanId(T bean) {
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.
@@ -219,9 +219,7 @@ public class DeployBeanProperty {
return (AnnotationUtil.has(field, WhenCreated.class)
|| AnnotationUtil.has(field, WhenModified.class)
|| AnnotationUtil.has(field, WhoModified.class)
|| AnnotationUtil.has(field, WhoCreated.class)
|| AnnotationUtil.has(field, UpdatedTimestamp.class)
|| AnnotationUtil.has(field, CreatedTimestamp.class));
|| AnnotationUtil.has(field, WhoCreated.class));
}
public String getFullBeanName() {
@@ -1,38 +1,7 @@
package io.ebeaninternal.server.deploy.parse;
import io.ebean.annotation.Aggregation;
import io.ebean.annotation.CreatedTimestamp;
import io.ebean.annotation.DbArray;
import io.ebean.annotation.DbComment;
import io.ebean.annotation.DbDefault;
import io.ebean.annotation.DbJson;
import io.ebean.annotation.DbJsonB;
import io.ebean.annotation.DbMap;
import io.ebean.annotation.DbMigration;
import io.ebean.annotation.DocCode;
import io.ebean.annotation.DocEmbedded;
import io.ebean.annotation.DocProperty;
import io.ebean.annotation.DocSortable;
import io.ebean.annotation.Draft;
import io.ebean.annotation.DraftDirty;
import io.ebean.annotation.DraftOnly;
import io.ebean.annotation.DraftReset;
import io.ebean.annotation.Encrypted;
import io.ebean.annotation.Expose;
import io.ebean.annotation.Formula;
import io.ebean.annotation.HistoryExclude;
import io.ebean.annotation.Identity;
import io.ebean.annotation.Index;
import io.ebean.annotation.JsonIgnore;
import io.ebean.annotation.Length;
import io.ebean.annotation.SoftDelete;
import io.ebean.annotation.TenantId;
import io.ebean.annotation.UnmappedJson;
import io.ebean.annotation.UpdatedTimestamp;
import io.ebean.annotation.WhenCreated;
import io.ebean.annotation.WhenModified;
import io.ebean.annotation.WhoCreated;
import io.ebean.annotation.WhoModified;
import io.ebean.annotation.*;
import io.ebean.config.EncryptDeploy;
import io.ebean.config.EncryptDeploy.Mode;
import io.ebean.config.dbplatform.DbEncrypt;
@@ -51,24 +20,7 @@ import io.ebeaninternal.server.type.ScalarTypeBytesBase;
import io.ebeaninternal.server.type.ScalarTypeBytesEncrypted;
import io.ebeaninternal.server.type.ScalarTypeEncryptedWrapper;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PersistenceException;
import javax.persistence.SequenceGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.Version;
import javax.persistence.*;
import java.sql.Types;
import java.util.Set;
import java.util.UUID;
@@ -339,10 +291,10 @@ final class AnnotationFields extends AnnotationParser {
}
private void initWhen(DeployBeanProperty prop) {
if (has(prop, WhenCreated.class) || has(prop, CreatedTimestamp.class)) {
if (has(prop, WhenCreated.class)) {
generatedPropFactory.setInsertTimestamp(prop);
}
if (has(prop, WhenModified.class) || has(prop, UpdatedTimestamp.class)) {
if (has(prop, WhenModified.class)) {
generatedPropFactory.setUpdateTimestamp(prop);
}
}
@@ -48,7 +48,6 @@ final class DCountMetric implements CountMetric {
@Override
public void visit(MetricVisitor visitor) {
long val = visitor.reset() ? count.sumThenReset() : count.sum();
if (val > 0) {
visitor.visitCount(new DCountMetricStats(name, val));
@@ -251,11 +251,6 @@ final class DefaultFetchGroupQuery<T> implements SpiFetchGroupQuery<T>, SpiQuery
throw new RuntimeException("EB102: Only select() and fetch() clause is allowed on FetchGroup");
}
@Override
public Stream<T> findLargeStream() {
throw new RuntimeException("EB102: Only select() and fetch() clause is allowed on FetchGroup");
}
@Override
public void findEach(Consumer<T> consumer) {
throw new RuntimeException("EB102: Only select() and fetch() clause is allowed on FetchGroup");
@@ -1456,11 +1456,6 @@ public final class DefaultOrmQuery<T> extends AbstractQuery implements SpiQuery<
return server.findStream(this, transaction);
}
@Override
public Stream<T> findLargeStream() {
return server.findLargeStream(this, transaction);
}
@Override
public List<Version<T>> findVersions() {
this.temporalMode = TemporalMode.VERSIONS;
@@ -10,7 +10,6 @@ import io.ebeaninternal.api.BindParams;
import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.api.SpiSqlQuery;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
@@ -56,36 +55,6 @@ public final class DefaultRelationalQuery extends AbstractQuery implements SpiSq
return server.findList(this, null);
}
@Override
public BigDecimal findSingleDecimal() {
return server.findSingleAttribute(this, BigDecimal.class);
}
@Override
public Long findSingleLong() {
return server.findSingleAttribute(this, Long.class);
}
@Override
public <T> T findSingleAttribute(Class<T> cls) {
return server.findSingleAttribute(this, cls);
}
@Override
public <T> List<T> findSingleAttributeList(Class<T> cls) {
return server.findSingleAttributeList(this, cls);
}
@Override
public <T> T findOne(RowMapper<T> mapper) {
return server.findOneMapper(this, mapper);
}
@Override
public <T> List<T> findList(RowMapper<T> mapper) {
return server.findListMapper(this, mapper);
}
@Override
public void findEachRow(RowConsumer consumer) {
server.findEachRow(this, consumer);
@@ -101,12 +70,6 @@ public final class DefaultRelationalQuery extends AbstractQuery implements SpiSq
return Optional.ofNullable(findOne());
}
@Override
@Deprecated
public DefaultRelationalQuery setParams(Object... values) {
return setParameters(values);
}
@Override
public DefaultRelationalQuery setParameters(Object... values) {
bindParams.setNextParameters(values);
@@ -237,6 +200,14 @@ public final class DefaultRelationalQuery extends AbstractQuery implements SpiSq
return new Mapper(mapper);
}
private <T> T findSingleAttribute(Class<T> cls) {
return server.findSingleAttribute(this, cls);
}
private <T> List<T> findSingleAttributeList(Class<T> cls) {
return server.findSingleAttributeList(this, cls);
}
private class Scalar<T> implements SqlQuery.TypeQuery<T> {
private final Class<T> type;
@@ -54,14 +54,13 @@ public interface DocStoreBeanAdapter<T> extends BeanDocType<T> {
/**
* Return the index type for this bean type.
*/
@Override
String getIndexType();
String indexType();
/**
* Return the index name for this bean type.
*/
@Override
String getIndexName();
String indexName();
/**
* Process a delete by id of a given document.
@@ -102,7 +102,6 @@ public abstract class DocStoreBeanBaseAdapter<T> implements DocStoreBeanAdapter<
private boolean registerPaths;
public DocStoreBeanBaseAdapter(BeanDescriptor<T> desc, DeployBeanDescriptor<T> deploy) {
this.desc = desc;
this.server = desc.ebeanServer();
this.mapped = deploy.isDocStoreMapped();
@@ -123,7 +122,6 @@ public abstract class DocStoreBeanBaseAdapter<T> implements DocStoreBeanAdapter<
@Override
public DocumentMapping createDocMapping() {
if (documentMapping != null) {
return documentMapping;
}
@@ -143,12 +141,12 @@ public abstract class DocStoreBeanBaseAdapter<T> implements DocStoreBeanAdapter<
}
@Override
public String getIndexType() {
public String indexType() {
return indexType;
}
@Override
public String getIndexName() {
public String indexName() {
return indexName;
}
@@ -159,7 +157,6 @@ public abstract class DocStoreBeanBaseAdapter<T> implements DocStoreBeanAdapter<
@Override
public String rawProperty(String property) {
String rawProperty = sortableMap.get(property);
return rawProperty == null ? property : rawProperty;
}
@@ -192,7 +189,6 @@ public abstract class DocStoreBeanBaseAdapter<T> implements DocStoreBeanAdapter<
*/
@Override
public void registerInvalidationPath(String queueId, String path, Set<String> properties) {
if (!mapped) {
if (update == DocStoreMode.IGNORE) {
// bean type not mapped but is included as nested document
@@ -207,7 +203,6 @@ public abstract class DocStoreBeanBaseAdapter<T> implements DocStoreBeanAdapter<
* Return the DsInvalidationListener based on the properties, path.
*/
protected DocStoreEmbeddedInvalidation getEmbeddedInvalidation(String queueId, String path, Set<String> properties) {
if (properties.contains("*")) {
return new DocStoreEmbeddedInvalidation(queueId, path);
} else {
@@ -245,19 +240,15 @@ public abstract class DocStoreBeanBaseAdapter<T> implements DocStoreBeanAdapter<
* This can add derived/embedded/nested parts to the document.
*/
protected DocStructure derivePathProperties(PathProperties pathProps) {
boolean includeByDefault = (pathProps == null);
if (pathProps == null) {
pathProps = new PathProperties();
}
return getDocStructure(pathProps, includeByDefault);
}
protected DocStructure getDocStructure(PathProperties pathProps, final boolean includeByDefault) {
final DocStructure docStructure = new DocStructure(pathProps);
BeanProperty[] properties = desc.propertiesNonTransient();
for (BeanProperty property : properties) {
property.docStoreInclude(includeByDefault, docStructure);
@@ -271,17 +262,16 @@ public abstract class DocStoreBeanBaseAdapter<T> implements DocStoreBeanAdapter<
}
});
}
return docStructure;
}
@Override
public FetchPath getEmbedded(String path) {
public FetchPath embedded(String path) {
return docStructure.getEmbedded(path);
}
@Override
public FetchPath getEmbeddedManyRoot(String path) {
public FetchPath embeddedManyRoot(String path) {
return docStructure.getEmbeddedManyRoot(path);
}
@@ -297,7 +287,6 @@ public abstract class DocStoreBeanBaseAdapter<T> implements DocStoreBeanAdapter<
@Override
public DocStoreMode getMode(PersistRequest.Type persistType, DocStoreMode txnMode) {
if (txnMode == null) {
return getMode(persistType);
} else if (txnMode == DocStoreMode.IGNORE) {
@@ -1,5 +1,6 @@
package io.ebeaninternal.server.cache;
import io.ebean.cache.ServerCache;
import io.ebean.cache.ServerCacheFactory;
import io.ebean.cache.ServerCacheOptions;
import io.ebean.cache.ServerCacheType;
@@ -12,7 +13,7 @@ import org.tests.model.basic.Customer;
import static org.assertj.core.api.Assertions.assertThat;
public class DefaultCacheHolderTest {
class DefaultCacheHolderTest {
private final ThreadLocal<String> tenantId = new ThreadLocal<>();
@@ -25,42 +26,30 @@ public class DefaultCacheHolderTest {
.with(cacheFactory, new TableModState());
}
@Test
public void getCache_normal() {
void getCache_normal() {
DefaultCacheHolder holder = new DefaultCacheHolder(options());
DefaultServerCache cache = cache(holder, Customer.class);
assertThat(cache.getName()).isEqualTo("org.tests.model.basic.Customer_B");
assertThat(cache.getShortName()).isEqualTo("Customer_B");
DefaultServerCache cache1 = cache(holder, Customer.class);
ServerCache cache = cache(holder, Customer.class);
ServerCache cache1 = cache(holder, Customer.class);
assertThat(cache1).isSameAs(cache);
DefaultServerCache cache2 = cache(holder, Contact.class);
ServerCache cache2 = cache(holder, Contact.class);
assertThat(cache1).isNotSameAs(cache2);
assertThat(cache2.getName()).isEqualTo("org.tests.model.basic.Contact_B");
assertThat(cache2.getShortName()).isEqualTo("Contact_B");
}
private DefaultServerCache cache(DefaultCacheHolder holder, Class<?> type) {
return (DefaultServerCache) holder.getCache(type, ServerCacheType.BEAN);
private ServerCache cache(DefaultCacheHolder holder, Class<?> type) {
return holder.getCache(type, ServerCacheType.BEAN);
}
@Test
public void getCache_multiTenant() throws Exception {
void getCache_multiTenant() throws Exception {
CacheManagerOptions builder = options().with(tenantId::get);
DefaultCacheHolder holder = new DefaultCacheHolder(builder);
tenantId.set("ten_1");
DefaultServerCache cache = cache(holder, Customer.class);
assertThat(cache.getName()).isEqualTo("org.tests.model.basic.Customer_B");
assertThat(cache.getShortName()).isEqualTo("Customer_B");
ServerCache cache = cache(holder, Customer.class);
cache.put("1", "value-for-tenant1");
cache.put("2", "an other value-for-tenant1");
@@ -109,10 +98,9 @@ public class DefaultCacheHolderTest {
}
@Test
public void clearAll() {
void clearAll() {
DefaultCacheHolder holder = new DefaultCacheHolder(options());
DefaultServerCache cache = cache(holder, Customer.class);
ServerCache cache = cache(holder, Customer.class);
cache.put("foo", "foo");
assertThat(cache.size()).isEqualTo(1);
holder.clearAll();
@@ -121,12 +109,11 @@ public class DefaultCacheHolderTest {
}
@Test
public void clearAll_multiTenant() {
void clearAll_multiTenant() {
CacheManagerOptions options = options().with(tenantId::get);
DefaultCacheHolder holder = new DefaultCacheHolder(options);
DefaultServerCache cache = cache(holder, Customer.class);
ServerCache cache = cache(holder, Customer.class);
cache.put("foo", "foo");
assertThat(cache.size()).isEqualTo(1);
@@ -1,5 +1,6 @@
package io.ebeaninternal.server.cache;
import io.ebean.cache.ServerCache;
import io.ebean.config.ContainerConfig;
import io.ebean.config.CurrentTenantProvider;
import io.ebean.config.DatabaseConfig;
@@ -11,7 +12,7 @@ import org.tests.model.basic.Customer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class DefaultServerCacheManagerTest {
class DefaultServerCacheManagerTest {
private final ThreadLocal<String> tenantId = new ThreadLocal<>();
@@ -29,7 +30,7 @@ public class DefaultServerCacheManagerTest {
private final DefaultServerCacheManager multiTenantManager;
public DefaultServerCacheManagerTest(){
DefaultServerCacheManagerTest(){
CacheManagerOptions builder = new CacheManagerOptions(clusterManager, new DatabaseConfig(), true);
builder.with(new TdTenPro());
this.multiTenantManager = new DefaultServerCacheManager(builder);
@@ -37,86 +38,79 @@ public class DefaultServerCacheManagerTest {
@Test
public void getCache_normal() {
void getCache_normal() {
ServerCache cache = cache(manager, Customer.class);
DefaultServerCache dsc = cache.unwrap(DefaultServerCache.class);
assertThat(dsc.getName()).isEqualTo("org.tests.model.basic.Customer_B");
assertThat(dsc.getShortName()).isEqualTo("Customer_B");
DefaultServerCache cache = cache(manager, Customer.class);
assertThat(cache.getName()).isEqualTo("org.tests.model.basic.Customer_B");
assertThat(cache.getShortName()).isEqualTo("Customer_B");
DefaultServerCache cache1 = cache(manager, Customer.class);
ServerCache cache1 = cache(manager, Customer.class);
assertThat(cache1).isSameAs(cache);
DefaultServerCache cache2 = cache(manager, Contact.class);
ServerCache cache2 = cache(manager, Contact.class);
assertThat(cache1).isNotSameAs(cache2);
assertThat(cache2.getName()).isEqualTo("org.tests.model.basic.Contact_B");
assertThat(cache2.getShortName()).isEqualTo("Contact_B");
DefaultServerCache dsc2 = cache2.unwrap(DefaultServerCache.class);
assertThat(dsc2.getName()).isEqualTo("org.tests.model.basic.Contact_B");
assertThat(dsc2.getShortName()).isEqualTo("Contact_B");
DefaultServerCache natKeyCache = (DefaultServerCache) manager.getNaturalKeyCache(Customer.class);
assertThat(natKeyCache.getName()).isEqualTo("org.tests.model.basic.Customer_N");
assertThat(natKeyCache.getShortName()).isEqualTo("Customer_N");
ServerCache natKeyCache = manager.getNaturalKeyCache(Customer.class);
DefaultServerCache dscNatKey = natKeyCache.unwrap(DefaultServerCache.class);
assertThat(dscNatKey.getName()).isEqualTo("org.tests.model.basic.Customer_N");
assertThat(dscNatKey.getShortName()).isEqualTo("Customer_N");
DefaultServerCache queryCache = (DefaultServerCache) manager.getQueryCache(Customer.class);
assertThat(queryCache.getName()).isEqualTo("org.tests.model.basic.Customer_Q");
assertThat(queryCache.getShortName()).isEqualTo("Customer_Q");
ServerCache queryCache = manager.getQueryCache(Customer.class);
DefaultServerCache dscQueryCache = queryCache.unwrap(DefaultServerCache.class);
assertThat(dscQueryCache.getName()).isEqualTo("org.tests.model.basic.Customer_Q");
assertThat(dscQueryCache.getShortName()).isEqualTo("Customer_Q");
DefaultServerCache collCache = (DefaultServerCache) manager.getCollectionIdsCache(Customer.class, "contacts");
assertThat(collCache.getName()).isEqualTo("org.tests.model.basic.Customer.contacts_C");
assertThat(collCache.getShortName()).isEqualTo("Customer.contacts_C");
ServerCache collCache = manager.getCollectionIdsCache(Customer.class, "contacts");
DefaultServerCache dscCollCache = collCache.unwrap(DefaultServerCache.class);
assertThat(dscCollCache.getName()).isEqualTo("org.tests.model.basic.Customer.contacts_C");
assertThat(dscCollCache.getShortName()).isEqualTo("Customer.contacts_C");
cache.clearCount.reset();
collCache.clearCount.reset();
queryCache.clearCount.reset();
natKeyCache.clearCount.reset();
cache.statistics(true);
collCache.statistics(true);
queryCache.statistics(true);
natKeyCache.statistics(true);
manager.clear(Customer.class);
assertThat(cache.clearCount.get(true)).isEqualTo(1);
assertThat(natKeyCache.clearCount.get(true)).isEqualTo(1);
assertThat(queryCache.clearCount.get(true)).isEqualTo(1);
assertThat(collCache.clearCount.get(true)).isEqualTo(1);
assertThat(cache.statistics(true).getClearCount()).isEqualTo(1);
assertThat(natKeyCache.statistics(true).getClearCount()).isEqualTo(1);
assertThat(queryCache.statistics(true).getClearCount()).isEqualTo(1);
assertThat(collCache.statistics(true).getClearCount()).isEqualTo(1);
}
private DefaultServerCache cache(DefaultServerCacheManager manager, Class<?> beanType) {
return (DefaultServerCache) manager.getBeanCache(beanType);
private ServerCache cache(DefaultServerCacheManager manager, Class<?> beanType) {
return manager.getBeanCache(beanType);
}
@Test
public void getCache_multiTenant() {
void getCache_multiTenant() {
tenantId.set("ten1");
DefaultServerCache cache = cache(multiTenantManager, Customer.class);
assertThat(cache.getName()).isEqualTo("org.tests.model.basic.Customer_B");
ServerCache cache = cache(multiTenantManager, Customer.class);
cache.put("1", "tenant1");
tenantId.set("ten2");
assertThat(cache.get("1")).isNull();
tenantId.set("ten1");
assertThat(cache.get("1")).isNotNull();
assertThat(cache.get("1")).isEqualTo("tenant1");
}
@Test
public void getCache_singleTenant() {
void getCache_singleTenant() {
ServerCache cache = cache(manager, Customer.class);
tenantId.set("ten1");
DefaultServerCache cache = cache(manager, Customer.class);
assertThat(cache.getName()).isEqualTo("org.tests.model.basic.Customer_B");
cache.put("1", "tenant1");
tenantId.set("ten2");
assertThat(cache.get("1")).isEqualTo("tenant1");
}
@Test
public void isLocalL2Caching() {
void isLocalL2Caching() {
assertTrue(manager.isLocalL2Caching());
assertTrue(multiTenantManager.isLocalL2Caching());
}
@@ -40,7 +40,7 @@ public class Contact {
@OneToMany(cascade = CascadeType.ALL)
List<ContactNote> notes;
@CreatedTimestamp
@WhenCreated
Timestamp cretime;
@Version
@@ -33,7 +33,7 @@ public class Product implements Serializable {
String name;
@CreatedTimestamp
@WhenCreated
Timestamp cretime;
@Version