From e41af85715ead0d4a314062eec001607efadbb83 Mon Sep 17 00:00:00 2001 From: redkale Date: Fri, 22 Sep 2023 23:16:51 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4continuousid=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/redkale/persistence/Cacheable.java | 13 +-- .../org/redkale/source/CacheMemorySource.java | 18 ----- .../java/org/redkale/source/EntityCache.java | 80 ++----------------- .../java/org/redkale/source/EntityInfo.java | 11 +-- 4 files changed, 12 insertions(+), 110 deletions(-) diff --git a/src/main/java/org/redkale/persistence/Cacheable.java b/src/main/java/org/redkale/persistence/Cacheable.java index cd8fc4315..d0f2e00d8 100644 --- a/src/main/java/org/redkale/persistence/Cacheable.java +++ b/src/main/java/org/redkale/persistence/Cacheable.java @@ -51,22 +51,11 @@ public @interface Cacheable { */ int interval() default 0; - /** - * (Optional) DataSource是否直接返回对象的真实引用, 而不是copy一份 - * - * @return boolean - */ @Deprecated boolean direct() default false; - /** - * (Optional) 主键字段是否同时满足: 1、类型为int;2、主键值可为数组下标;3、记录总数有限;
- * 用于EntityCache的全量数据是否用Array存储,主键值作为数组下标 - * - * @return boolean - */ @Deprecated - boolean sequent() default true; + boolean sequent() default false; @Deprecated boolean continuousid() default false; diff --git a/src/main/java/org/redkale/source/CacheMemorySource.java b/src/main/java/org/redkale/source/CacheMemorySource.java index 1907b19da..6b45b47e7 100644 --- a/src/main/java/org/redkale/source/CacheMemorySource.java +++ b/src/main/java/org/redkale/source/CacheMemorySource.java @@ -1906,10 +1906,6 @@ public final class CacheMemorySource extends AbstractCacheSource { return entry; } - protected CacheEntryType findEntryType(Type type) { - return CacheEntryType.OBJECT; - } - public static enum CacheEntryType { OBJECT, ATOMIC, DOUBLE, SSET, ZSET, LIST, MAP; } @@ -1958,20 +1954,6 @@ public final class CacheMemorySource extends AbstractCacheSource { return JsonFactory.root().getConvert().convertTo(this); } -// @ConvertColumn(ignore = true) -// public boolean isListCacheType() { -// return cacheType == CacheEntryType.LIST; -// } -// -// @ConvertColumn(ignore = true) -// public boolean isSetCacheType() { -// return cacheType == CacheEntryType.SSET || cacheType == CacheEntryType.ZSET; -// } -// -// @ConvertColumn(ignore = true) -// public boolean isMapCacheType() { -// return cacheType == CacheEntryType.MAP; -// } @ConvertColumn(ignore = true) public boolean isExpired() { return expireMills > 0 && (lastAccessed + expireMills) < System.currentTimeMillis(); diff --git a/src/main/java/org/redkale/source/EntityCache.java b/src/main/java/org/redkale/source/EntityCache.java index 5e640213d..858324f1e 100644 --- a/src/main/java/org/redkale/source/EntityCache.java +++ b/src/main/java/org/redkale/source/EntityCache.java @@ -38,9 +38,6 @@ public final class EntityCache { // CopyOnWriteArrayList 插入慢、查询快; 10w数据插入需要3.2秒; ConcurrentLinkedQueue 插入快、查询慢;10w数据查询需要 0.062秒, 查询慢40%; private Collection list = new ConcurrentLinkedQueue(); - //sequent=true此字段值才有效 - private T[] array; - //Flipper.sort转换成Comparator的缓存 private final Map> sortComparators = new ConcurrentHashMap<>(); @@ -78,38 +75,23 @@ public final class EntityCache { //@Cacheable的定时更新秒数,为0表示不定时更新 final int interval; - //@Cacheable的主键字段是否同时满足: 1、类型为int;2、主键值可为数组下标;3、记录总数有限; - final boolean sequent; - //@Cacheable的定时器 private ScheduledThreadPoolExecutor scheduler; private CompletableFuture> loadFuture; public EntityCache(final EntityInfo info, final Cacheable c) { - this(info, c != null ? c.interval() : 0, c != null && c.direct(), c != null && c.sequent()); + this(info, c != null ? c.interval() : 0); } - EntityCache(final EntityInfo info, final int cacheInterval, final boolean cacheDirect, final boolean cacheContinuousid) { + EntityCache(final EntityInfo info, final int cacheInterval) { this.info = info; this.interval = cacheInterval < 0 ? 0 : cacheInterval; - this.sequent = cacheContinuousid && info.getPrimary().type() == int.class; this.type = info.getType(); this.arrayer = info.getArrayer(); this.creator = info.getCreator(); this.primary = info.primary; - org.redkale.persistence.VirtualEntity ve = info.getType().getAnnotation(org.redkale.persistence.VirtualEntity.class); - boolean direct = cacheDirect; - if (!direct) { - direct = ve != null && ve.direct(); - } - { //兼容废弃类 - org.redkale.source.VirtualEntity ve2 = info.getType().getAnnotation(org.redkale.source.VirtualEntity.class); - if (!direct && ve2 != null) { - direct = ve2.direct(); - } - } - this.needCopy = !direct; + this.needCopy = true; this.newCopier = Copier.create(type, type, (e, c) -> { try { return e.getAnnotation(Transient.class) == null && e.getAnnotation(javax.persistence.Transient.class) == null; @@ -147,26 +129,24 @@ public final class EntityCache { if (loading.getAndSet(true)) { return this.loadFuture; } - if (info.fullloader == null) { + if (info.fullLoader == null) { this.list = new ConcurrentLinkedQueue(); - this.array = null; this.map = new ConcurrentHashMap(); this.fullloaded = true; loading.set(false); return this.loadFuture; } this.fullloaded = false; - CompletableFuture allFuture = info.fullloader.apply(info.source, info); + CompletableFuture allFuture = info.fullLoader.apply(info.source, info); this.loadFuture = (CompletableFuture) allFuture; if (allFuture == null) { this.list = new ConcurrentLinkedQueue(); - this.array = null; this.map = new ConcurrentHashMap(); this.fullloaded = true; loading.set(false); return this.loadFuture; } - if (this.interval > 0 && this.scheduler == null && info.fullloader != null) { + if (this.interval > 0 && this.scheduler == null && info.fullLoader != null) { this.scheduler = new ScheduledThreadPoolExecutor(1, (Runnable r) -> { final Thread t = new Thread(r, "Redkale-EntityCache-" + type.getSimpleName() + "-Thread"); t.setDaemon(true); @@ -175,14 +155,13 @@ public final class EntityCache { this.scheduler.scheduleAtFixedRate(() -> { try { ConcurrentHashMap newmap2 = new ConcurrentHashMap(); - List all2 = info.fullloader.apply(info.source, info).join(); + List all2 = info.fullLoader.apply(info.source, info).join(); if (all2 != null) { all2.stream().filter(x -> x != null).forEach(x -> { newmap2.put(this.primary.get(x), x); }); } this.list = all2 == null ? new ConcurrentLinkedQueue() : new ConcurrentLinkedQueue(all2); - this.array = transferArray(all2); this.map = newmap2; } catch (Throwable t) { logger.log(Level.SEVERE, type + " schedule(interval=" + interval + "s) Cacheable error", t); @@ -202,7 +181,6 @@ public final class EntityCache { }); } this.list = new ConcurrentLinkedQueue(all); - this.array = transferArray(all); this.map = newmap; this.fullloaded = true; loading.set(false); @@ -210,24 +188,6 @@ public final class EntityCache { return this.loadFuture; } - private T[] transferArray(List all) { - if (sequent && all != null && !all.isEmpty()) { - try { - int maxid = all.stream().mapToInt(v -> v == null ? 0 : (Integer) primary.get(v)).max().orElse(0); - T[] result = arrayer.apply(maxid + 1); - for (T v : all) { - int index = v == null ? 0 : (Integer) primary.get(v); - result[index] = v; - } - return result; - } catch (Exception e) { //主键值可能是负数,导致数组下标异常 - return null; - } - } else { - return null; - } - } - public Class getType() { return type; } @@ -235,7 +195,6 @@ public final class EntityCache { public int clear() { this.fullloaded = false; this.list = new ConcurrentLinkedQueue(); - this.array = null; this.map = new ConcurrentHashMap(); if (this.scheduler != null) { this.scheduler.shutdownNow(); @@ -284,22 +243,6 @@ public final class EntityCache { return result; } } - if (sequent && array != null) { - T[] array0 = array; - T[] result = arrayer.apply(pks.length); - if (needCopy) { - for (int i = 0; i < result.length; i++) { - T rs = array0[(Integer) pks[i]]; - result[i] = rs == null ? null : newCopier.apply(this.creator.create(), rs); - } - } else { - for (int i = 0; i < result.length; i++) { - T rs = array0[(Integer) pks[i]]; - result[i] = rs == null ? null : rs; - } - } - return result; - } T[] result = arrayer.apply(pks.length); for (int i = 0; i < result.length; i++) { T rs = map.get(pks[i]); @@ -803,9 +746,6 @@ public final class EntityCache { T old = this.map.putIfAbsent(this.primary.get(rs), rs); if (old == null) { this.list.add(rs); - if (sequent) { - this.array = transferArray(new ArrayList<>(this.list)); - } return 1; } else { logger.log(Level.WARNING, this.type + " cache repeat insert data: " + entity); @@ -822,9 +762,6 @@ public final class EntityCache { return 0; } this.list.remove(rs); - if (sequent) { - this.array[(Integer) primary.get(rs)] = null; - } return 1; } @@ -851,9 +788,6 @@ public final class EntityCache { ids[++i] = this.primary.get(t); this.map.remove(ids[i]); this.list.remove(t); - if (sequent) { - this.array[(Integer) primary.get(t)] = null; - } } return ids; } diff --git a/src/main/java/org/redkale/source/EntityInfo.java b/src/main/java/org/redkale/source/EntityInfo.java index eb2ab79ea..e444630bf 100644 --- a/src/main/java/org/redkale/source/EntityInfo.java +++ b/src/main/java/org/redkale/source/EntityInfo.java @@ -203,7 +203,7 @@ public final class EntityInfo { final DataSource source; //全量数据的加载器 - final BiFunction> fullloader; + final BiFunction> fullLoader; //------------------------------------------------------------ /** @@ -344,9 +344,9 @@ public final class EntityInfo { } catch (Exception e) { logger.log(Level.SEVERE, type + " init @VirtualEntity.loader error", e); } - this.fullloader = loader; + this.fullLoader = loader; } else { - this.fullloader = fullloader; + this.fullLoader = fullloader; if (tableName0 != null && !tableName0.isEmpty() && tableName0.indexOf('.') >= 0) { throw new SourceException(type + " have illegal table.name on @Table"); } @@ -687,10 +687,7 @@ public final class EntityInfo { Cacheable c1 = type.getAnnotation(Cacheable.class); javax.persistence.Cacheable c2 = type.getAnnotation(javax.persistence.Cacheable.class); if (this.table == null || (!cacheForbidden && c1 != null && c1.value()) || (!cacheForbidden && c2 != null && c2.value())) { - this.cache = new EntityCache<>(this, - c1 == null ? (c2 == null ? 0 : c2.interval()) : c1.interval(), - c1 == null ? (c2 == null ? false : c2.direct()) : c1.direct(), - c1 == null ? false : c1.sequent()); + this.cache = new EntityCache<>(this, c1 == null ? (c2 == null ? 0 : c2.interval()) : c1.interval()); } else { this.cache = null; }