Search in sources :

Example 11 with NamingScheme

use of org.redisson.liveobject.resolver.NamingScheme in project redisson by redisson.

the class RedissonObjectBuilder method toReference.

public RedissonReference toReference(Object object) {
    if (object != null && ClassUtils.isAnnotationPresent(object.getClass(), REntity.class)) {
        throw new IllegalArgumentException("REntity should be attached to Redisson before save");
    }
    if (object instanceof RObject && !(object instanceof RLiveObject)) {
        Class<?> clazz = object.getClass().getInterfaces()[0];
        RObject rObject = (RObject) object;
        if (rObject.getCodec() != null) {
            codecProvider.registerCodec((Class) rObject.getCodec().getClass(), rObject.getCodec());
        }
        return new RedissonReference(clazz, rObject.getName(), rObject.getCodec());
    }
    if (object instanceof RObjectReactive && !(object instanceof RLiveObject)) {
        Class<?> clazz = object.getClass().getInterfaces()[0];
        RObjectReactive rObject = (RObjectReactive) object;
        if (rObject.getCodec() != null) {
            codecProvider.registerCodec((Class) rObject.getCodec().getClass(), rObject.getCodec());
        }
        return new RedissonReference(clazz, rObject.getName(), rObject.getCodec());
    }
    if (object instanceof RObjectRx && !(object instanceof RLiveObject)) {
        Class<?> clazz = object.getClass().getInterfaces()[0];
        RObjectRx rObject = (RObjectRx) object;
        if (rObject.getCodec() != null) {
            codecProvider.registerCodec((Class) rObject.getCodec().getClass(), rObject.getCodec());
        }
        return new RedissonReference(clazz, rObject.getName(), rObject.getCodec());
    }
    try {
        if (object instanceof RLiveObject) {
            Class<? extends Object> rEntity = object.getClass().getSuperclass();
            NamingScheme ns = getNamingScheme(rEntity);
            return new RedissonReference(rEntity, ns.getName(rEntity, ((RLiveObject) object).getLiveObjectId()));
        }
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
    return null;
}
Also used : REntity(org.redisson.api.annotation.REntity) NamingScheme(org.redisson.liveobject.resolver.NamingScheme)

Example 12 with NamingScheme

use of org.redisson.liveobject.resolver.NamingScheme in project redisson by redisson.

the class LiveObjectSearch method traverseAnd.

private Set<Object> traverseAnd(ANDCondition condition, NamingScheme namingScheme, Class<?> entityClass) {
    Set<Object> allIds = new HashSet<Object>();
    List<String> eqNames = new ArrayList<String>();
    Map<RScoredSortedSet<Object>, Number> gtNumericNames = new HashMap<>();
    Map<RScoredSortedSet<Object>, Number> geNumericNames = new HashMap<>();
    Map<RScoredSortedSet<Object>, Number> ltNumericNames = new HashMap<>();
    Map<RScoredSortedSet<Object>, Number> leNumericNames = new HashMap<>();
    Map<RScoredSortedSet<Object>, Number> eqNumericNames = new HashMap<>();
    for (Condition cond : condition.getConditions()) {
        if (cond instanceof EQCondition) {
            EQCondition eqc = (EQCondition) cond;
            String indexName = namingScheme.getIndexName(entityClass, eqc.getName());
            if (eqc.getValue() instanceof Number) {
                RScoredSortedSet<Object> values = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
                eqNumericNames.put(values, (Number) eqc.getValue());
            } else {
                RSetMultimap<Object, Object> map = new RedissonSetMultimap<>(namingScheme.getCodec(), commandExecutor, indexName);
                RSet<Object> values = map.get(eqc.getValue());
                eqNames.add(((RedissonObject) values).getRawName());
            }
        }
        if (cond instanceof LTCondition) {
            LTCondition ltc = (LTCondition) cond;
            String indexName = namingScheme.getIndexName(entityClass, ltc.getName());
            RScoredSortedSet<Object> values = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
            ltNumericNames.put(values, ltc.getValue());
        }
        if (cond instanceof LECondition) {
            LECondition lec = (LECondition) cond;
            String indexName = namingScheme.getIndexName(entityClass, lec.getName());
            RScoredSortedSet<Object> values = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
            leNumericNames.put(values, lec.getValue());
        }
        if (cond instanceof GECondition) {
            GECondition gec = (GECondition) cond;
            String indexName = namingScheme.getIndexName(entityClass, gec.getName());
            RScoredSortedSet<Object> values = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
            geNumericNames.put(values, gec.getValue());
        }
        if (cond instanceof GTCondition) {
            GTCondition gtc = (GTCondition) cond;
            String indexName = namingScheme.getIndexName(entityClass, gtc.getName());
            RScoredSortedSet<Object> values = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
            gtNumericNames.put(values, gtc.getValue());
        }
        if (cond instanceof ORCondition) {
            Collection<Object> ids = traverseOr((ORCondition) cond, namingScheme, entityClass);
            if (ids.isEmpty()) {
                return Collections.emptySet();
            }
            if (!allIds.isEmpty()) {
                allIds.retainAll(ids);
            } else {
                allIds.addAll(ids);
            }
            if (allIds.isEmpty()) {
                return Collections.emptySet();
            }
        }
    }
    if (!eqNames.isEmpty()) {
        RSet<Object> set = new RedissonSet<>(commandExecutor, eqNames.get(0), null);
        Set<Object> intersect = set.readIntersection(eqNames.toArray(new String[eqNames.size()]));
        if (!allIds.isEmpty()) {
            allIds.retainAll(intersect);
            if (allIds.isEmpty()) {
                return Collections.emptySet();
            }
        } else {
            allIds.addAll(intersect);
        }
        if (allIds.isEmpty()) {
            return allIds;
        }
    }
    if (!checkValueRange(allIds, eqNumericNames, (r, v) -> {
        return r.valueRange(v.doubleValue(), true, v.doubleValue(), true);
    })) {
        return Collections.emptySet();
    }
    if (!checkValueRange(allIds, gtNumericNames, (r, v) -> {
        return r.valueRange(v.doubleValue(), false, Double.POSITIVE_INFINITY, false);
    })) {
        return Collections.emptySet();
    }
    if (!checkValueRange(allIds, geNumericNames, (r, v) -> {
        return r.valueRange(v.doubleValue(), true, Double.POSITIVE_INFINITY, false);
    })) {
        return Collections.emptySet();
    }
    if (!checkValueRange(allIds, ltNumericNames, (r, v) -> {
        return r.valueRange(Double.NEGATIVE_INFINITY, false, v.doubleValue(), false);
    })) {
        return Collections.emptySet();
    }
    if (!checkValueRange(allIds, leNumericNames, (r, v) -> {
        return r.valueRange(Double.NEGATIVE_INFINITY, false, v.doubleValue(), true);
    })) {
        return Collections.emptySet();
    }
    return allIds;
}
Also used : RedissonSet(org.redisson.RedissonSet) RedissonScoredSortedSet(org.redisson.RedissonScoredSortedSet) RScoredSortedSet(org.redisson.api.RScoredSortedSet) CommandAsyncExecutor(org.redisson.command.CommandAsyncExecutor) java.util(java.util) BiFunction(java.util.function.BiFunction) RSet(org.redisson.api.RSet) RSetMultimap(org.redisson.api.RSetMultimap) org.redisson.liveobject.condition(org.redisson.liveobject.condition) Condition(org.redisson.api.condition.Condition) RedissonSetMultimap(org.redisson.RedissonSetMultimap) RedissonObject(org.redisson.RedissonObject) Entry(java.util.Map.Entry) NamingScheme(org.redisson.liveobject.resolver.NamingScheme) RedissonSet(org.redisson.RedissonSet) RScoredSortedSet(org.redisson.api.RScoredSortedSet) Condition(org.redisson.api.condition.Condition) RedissonScoredSortedSet(org.redisson.RedissonScoredSortedSet) RedissonSetMultimap(org.redisson.RedissonSetMultimap) RedissonObject(org.redisson.RedissonObject)

Example 13 with NamingScheme

use of org.redisson.liveobject.resolver.NamingScheme in project redisson by redisson.

the class AccessorInterceptor method intercept.

@RuntimeType
@SuppressWarnings("NestedIfDepth")
public Object intercept(@Origin Method method, @SuperCall Callable<?> superMethod, @AllArguments Object[] args, @This Object me, @FieldValue("liveObjectLiveMap") RMap<String, Object> liveMap) throws Exception {
    if (isGetter(method, getREntityIdFieldName(me))) {
        return ((RLiveObject) me).getLiveObjectId();
    }
    if (isSetter(method, getREntityIdFieldName(me))) {
        ((RLiveObject) me).setLiveObjectId(args[0]);
        return null;
    }
    String fieldName = getFieldName(me.getClass().getSuperclass(), method);
    Field field = ClassUtils.getDeclaredField(me.getClass().getSuperclass(), fieldName);
    Class<?> fieldType = field.getType();
    if (isGetter(method, fieldName)) {
        Object result = liveMap.get(fieldName);
        if (result == null) {
            RObject ar = commandExecutor.getObjectBuilder().createObject(((RLiveObject) me).getLiveObjectId(), me.getClass().getSuperclass(), fieldType, fieldName);
            if (ar != null) {
                commandExecutor.getObjectBuilder().store(ar, fieldName, liveMap);
                return ar;
            }
        }
        if (result != null && fieldType.isEnum()) {
            if (result instanceof String) {
                return Enum.valueOf((Class) fieldType, (String) result);
            }
            return result;
        }
        if (result instanceof RedissonReference) {
            return commandExecutor.getObjectBuilder().fromReference((RedissonReference) result, RedissonObjectBuilder.ReferenceType.DEFAULT);
        }
        return result;
    }
    if (isSetter(method, fieldName)) {
        Object arg = args[0];
        if (arg != null && ClassUtils.isAnnotationPresent(arg.getClass(), REntity.class)) {
            throw new IllegalStateException("REntity object should be attached to Redisson first");
        }
        if (arg instanceof RLiveObject) {
            RLiveObject liveObject = (RLiveObject) arg;
            removeIndex(liveMap, me, field);
            storeIndex(field, me, liveObject.getLiveObjectId());
            Class<? extends Object> rEntity = liveObject.getClass().getSuperclass();
            NamingScheme ns = commandExecutor.getObjectBuilder().getNamingScheme(rEntity);
            if (commandExecutor instanceof CommandBatchService) {
                liveMap.fastPutAsync(fieldName, new RedissonReference(rEntity, ns.getName(rEntity, liveObject.getLiveObjectId())));
            } else {
                liveMap.fastPut(fieldName, new RedissonReference(rEntity, ns.getName(rEntity, liveObject.getLiveObjectId())));
            }
            return me;
        }
        if (!(arg instanceof RObject) && (arg instanceof Collection || arg instanceof Map) && TransformationMode.ANNOTATION_BASED.equals(ClassUtils.getAnnotation(me.getClass().getSuperclass(), REntity.class).fieldTransformation())) {
            RObject rObject = commandExecutor.getObjectBuilder().createObject(((RLiveObject) me).getLiveObjectId(), me.getClass().getSuperclass(), arg.getClass(), fieldName);
            if (arg != null) {
                if (rObject instanceof Collection) {
                    Collection<?> c = (Collection<?>) rObject;
                    c.clear();
                    c.addAll((Collection) arg);
                } else {
                    Map<?, ?> m = (Map<?, ?>) rObject;
                    m.clear();
                    m.putAll((Map) arg);
                }
            }
            if (rObject != null) {
                arg = rObject;
            }
        }
        if (arg instanceof RObject) {
            if (commandExecutor instanceof CommandBatchService) {
                commandExecutor.getObjectBuilder().storeAsync((RObject) arg, fieldName, liveMap);
            } else {
                commandExecutor.getObjectBuilder().store((RObject) arg, fieldName, liveMap);
            }
            return me;
        }
        removeIndex(liveMap, me, field);
        if (arg != null) {
            storeIndex(field, me, arg);
            if (commandExecutor instanceof CommandBatchService) {
                liveMap.fastPutAsync(fieldName, arg);
            } else {
                liveMap.fastPut(fieldName, arg);
            }
        }
        return me;
    }
    return superMethod.call();
}
Also used : RedissonReference(org.redisson.RedissonReference) NamingScheme(org.redisson.liveobject.resolver.NamingScheme) CommandBatchService(org.redisson.command.CommandBatchService) Field(java.lang.reflect.Field) REntity(org.redisson.api.annotation.REntity) RedissonObject(org.redisson.RedissonObject)

Aggregations

NamingScheme (org.redisson.liveobject.resolver.NamingScheme)13 REntity (org.redisson.api.annotation.REntity)6 RedissonObject (org.redisson.RedissonObject)5 RedissonScoredSortedSet (org.redisson.RedissonScoredSortedSet)4 RedissonSetMultimap (org.redisson.RedissonSetMultimap)4 Codec (org.redisson.client.codec.Codec)4 CommandBatchService (org.redisson.command.CommandBatchService)4 RedissonReference (org.redisson.RedissonReference)3 Field (java.lang.reflect.Field)2 java.util (java.util)2 Entry (java.util.Map.Entry)2 RLiveObject (org.redisson.api.RLiveObject)2 RScoredSortedSet (org.redisson.api.RScoredSortedSet)2 RIndex (org.redisson.api.annotation.RIndex)2 Condition (org.redisson.api.condition.Condition)2 RedisCommand (org.redisson.client.protocol.RedisCommand)2 ListMultiDecoder2 (org.redisson.client.protocol.decoder.ListMultiDecoder2)2 ListScanResult (org.redisson.client.protocol.decoder.ListScanResult)2 ListScanResultReplayDecoder (org.redisson.client.protocol.decoder.ListScanResultReplayDecoder)2 ObjectListReplayDecoder (org.redisson.client.protocol.decoder.ObjectListReplayDecoder)2