From 4ddc8ff6a3938fde3be4dda9ef6b8c49b00e4e2e Mon Sep 17 00:00:00 2001 From: Nikita Koksharov Date: Thu, 24 Sep 2020 12:12:15 +0300 Subject: [PATCH] refactoring --- .../redisson/RedissonLiveObjectService.java | 29 ++++++++++--------- .../core/LiveObjectInterceptor.java | 4 ++- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/redisson/src/main/java/org/redisson/RedissonLiveObjectService.java b/redisson/src/main/java/org/redisson/RedissonLiveObjectService.java index 69234425b..09c650089 100644 --- a/redisson/src/main/java/org/redisson/RedissonLiveObjectService.java +++ b/redisson/src/main/java/org/redisson/RedissonLiveObjectService.java @@ -566,9 +566,9 @@ public class RedissonLiveObjectService implements RLiveObjectService { FieldList fields = Introspectior.getFieldsWithAnnotation(entityClass.getSuperclass(), RIndex.class); Set fieldNames = fields.stream().map(f -> f.getName()).collect(Collectors.toSet()); + NamingScheme namingScheme = connectionManager.getCommandExecutor().getObjectBuilder().getNamingScheme(entityClass); for (Object id: ids) { - T entity = createLiveObject(entityClass, id); - delete(entity, asRMap(entity), ce, fieldNames); + delete(id, entityClass, namingScheme, ce, fieldNames); } BatchResult r = (BatchResult) ce.execute(); @@ -577,32 +577,35 @@ public class RedissonLiveObjectService implements RLiveObjectService { .mapToLong(s -> (Long) s).sum(); } - private RFuture delete(Object me, RMap map, CommandBatchService ce, Set fieldNames) { - Map values = map.getAll(fieldNames); + private RFuture delete(Object id, Class entityClass, NamingScheme namingScheme, CommandBatchService ce, Set fieldNames) { + String mapName = namingScheme.getName(entityClass, id); + Object liveObjectId = namingScheme.resolveId(mapName); + + RMap liveMap = new RedissonMap<>(namingScheme.getCodec(), connectionManager.getCommandExecutor(), + mapName, null, null, null); + Map values = liveMap.getAll(fieldNames); for (String fieldName : fieldNames) { Object value = values.get(fieldName); if (value == null) { continue; } - NamingScheme namingScheme = connectionManager.getCommandExecutor().getObjectBuilder().getNamingScheme(me.getClass().getSuperclass()); - String indexName = namingScheme.getIndexName(me.getClass().getSuperclass(), fieldName); - + String indexName = namingScheme.getIndexName(entityClass, fieldName); if (value instanceof Number) { RScoredSortedSetAsync set = new RedissonScoredSortedSet<>(namingScheme.getCodec(), ce, indexName, null); - set.removeAsync(((RLiveObject) me).getLiveObjectId()); + set.removeAsync(liveObjectId); } else { RMultimapAsync idsMultimap = new RedissonSetMultimap<>(namingScheme.getCodec(), ce, indexName); - idsMultimap.removeAsync(value, ((RLiveObject) me).getLiveObjectId()); + idsMultimap.removeAsync(value, liveObjectId); } } - return new RedissonKeys(ce).deleteAsync(map.getName()); + return new RedissonKeys(ce).deleteAsync(mapName); } - public RFuture delete(Object me, RMap map, CommandBatchService ce) { - FieldList fields = Introspectior.getFieldsWithAnnotation(me.getClass().getSuperclass(), RIndex.class); + public RFuture delete(Object id, Class entityClass, NamingScheme namingScheme, CommandBatchService ce) { + FieldList fields = Introspectior.getFieldsWithAnnotation(entityClass, RIndex.class); Set fieldNames = fields.stream().map(f -> f.getName()).collect(Collectors.toSet()); - return delete(me, map, ce, fieldNames); + return delete(id, entityClass, namingScheme, ce, fieldNames); } @Override diff --git a/redisson/src/main/java/org/redisson/liveobject/core/LiveObjectInterceptor.java b/redisson/src/main/java/org/redisson/liveobject/core/LiveObjectInterceptor.java index ac970915d..ef180b37f 100644 --- a/redisson/src/main/java/org/redisson/liveobject/core/LiveObjectInterceptor.java +++ b/redisson/src/main/java/org/redisson/liveobject/core/LiveObjectInterceptor.java @@ -19,6 +19,7 @@ import net.bytebuddy.implementation.bind.annotation.*; import org.redisson.RedissonLiveObjectService; import org.redisson.RedissonMap; import org.redisson.api.RFuture; +import org.redisson.api.RLiveObject; import org.redisson.api.RMap; import org.redisson.client.RedisException; import org.redisson.command.CommandAsyncExecutor; @@ -125,7 +126,8 @@ public class LiveObjectInterceptor { ce = new CommandBatchService(connectionManager); } - RFuture deleteFuture = service.delete(me, map, ce); + Object idd = ((RLiveObject)me).getLiveObjectId(); + RFuture deleteFuture = service.delete(idd, me.getClass().getSuperclass(), namingScheme, ce); ce.execute(); return deleteFuture.getNow() > 0;