Search in sources :

Example 6 with NamingScheme

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

the class LiveObjectSearch method find.

public Set<Object> find(Class<?> entityClass, Condition condition) {
    NamingScheme namingScheme = commandExecutor.getObjectBuilder().getNamingScheme(entityClass);
    if (condition instanceof EQCondition) {
        EQCondition c = (EQCondition) condition;
        String indexName = namingScheme.getIndexName(entityClass, c.getName());
        if (c.getValue() instanceof Number) {
            RScoredSortedSet<Object> set = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
            double v = ((Number) c.getValue()).doubleValue();
            Collection<Object> gtIds = set.valueRange(v, true, v, true);
            return new HashSet<>(gtIds);
        } else {
            RSetMultimap<Object, Object> map = new RedissonSetMultimap<>(namingScheme.getCodec(), commandExecutor, indexName);
            return map.getAll(c.getValue());
        }
    } else if (condition instanceof GTCondition) {
        GTCondition c = (GTCondition) condition;
        String indexName = namingScheme.getIndexName(entityClass, c.getName());
        RScoredSortedSet<Object> set = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
        Collection<Object> gtIds = set.valueRange(c.getValue().doubleValue(), false, Double.POSITIVE_INFINITY, false);
        return new HashSet<>(gtIds);
    } else if (condition instanceof GECondition) {
        GECondition c = (GECondition) condition;
        String indexName = namingScheme.getIndexName(entityClass, c.getName());
        RScoredSortedSet<Object> set = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
        Collection<Object> gtIds = set.valueRange(c.getValue().doubleValue(), true, Double.POSITIVE_INFINITY, false);
        return new HashSet<>(gtIds);
    } else if (condition instanceof LTCondition) {
        LTCondition c = (LTCondition) condition;
        String indexName = namingScheme.getIndexName(entityClass, c.getName());
        RScoredSortedSet<Object> set = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
        Collection<Object> gtIds = set.valueRange(Double.NEGATIVE_INFINITY, false, c.getValue().doubleValue(), false);
        return new HashSet<>(gtIds);
    } else if (condition instanceof LECondition) {
        LECondition c = (LECondition) condition;
        String indexName = namingScheme.getIndexName(entityClass, c.getName());
        RScoredSortedSet<Object> set = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
        Collection<Object> gtIds = set.valueRange(Double.NEGATIVE_INFINITY, false, c.getValue().doubleValue(), true);
        return new HashSet<>(gtIds);
    } else if (condition instanceof ORCondition) {
        return traverseOr((ORCondition) condition, namingScheme, entityClass);
    } else if (condition instanceof ANDCondition) {
        return traverseAnd((ANDCondition) condition, namingScheme, entityClass);
    }
    throw new IllegalArgumentException();
}
Also used : RScoredSortedSet(org.redisson.api.RScoredSortedSet) NamingScheme(org.redisson.liveobject.resolver.NamingScheme) RedissonScoredSortedSet(org.redisson.RedissonScoredSortedSet) RedissonSetMultimap(org.redisson.RedissonSetMultimap) RedissonObject(org.redisson.RedissonObject)

Example 7 with NamingScheme

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

the class AccessorInterceptor method removeIndex.

private void removeIndex(RMap<String, Object> liveMap, Object me, Field field) {
    if (field.getAnnotation(RIndex.class) == null) {
        return;
    }
    NamingScheme namingScheme = commandExecutor.getObjectBuilder().getNamingScheme(me.getClass().getSuperclass());
    String indexName = namingScheme.getIndexName(me.getClass().getSuperclass(), field.getName());
    CommandBatchService ce;
    if (commandExecutor instanceof CommandBatchService) {
        ce = (CommandBatchService) commandExecutor;
    } else {
        ce = new CommandBatchService(commandExecutor);
    }
    if (Number.class.isAssignableFrom(field.getType()) || PRIMITIVE_CLASSES.contains(field.getType())) {
        RScoredSortedSetAsync<Object> set = new RedissonScoredSortedSet<>(namingScheme.getCodec(), ce, indexName, null);
        set.removeAsync(((RLiveObject) me).getLiveObjectId());
    } else {
        if (ClassUtils.isAnnotationPresent(field.getType(), REntity.class) || commandExecutor.getConnectionManager().isClusterMode()) {
            Object value = liveMap.remove(field.getName());
            if (value != null) {
                RMultimapAsync<Object, Object> map = new RedissonSetMultimap<>(namingScheme.getCodec(), ce, indexName);
                Object k = value;
                if (ClassUtils.isAnnotationPresent(field.getType(), REntity.class)) {
                    k = ((RLiveObject) value).getLiveObjectId();
                }
                map.removeAsync(k, ((RLiveObject) me).getLiveObjectId());
            }
        } else {
            removeAsync(ce, indexName, ((RedissonObject) liveMap).getRawName(), namingScheme.getCodec(), ((RLiveObject) me).getLiveObjectId(), field.getName());
        }
    }
    if (ce != commandExecutor) {
        ce.execute();
    }
}
Also used : RedissonSetMultimap(org.redisson.RedissonSetMultimap) REntity(org.redisson.api.annotation.REntity) RIndex(org.redisson.api.annotation.RIndex) NamingScheme(org.redisson.liveobject.resolver.NamingScheme) CommandBatchService(org.redisson.command.CommandBatchService) RedissonScoredSortedSet(org.redisson.RedissonScoredSortedSet) RedissonObject(org.redisson.RedissonObject)

Example 8 with NamingScheme

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

the class AccessorInterceptor method storeIndex.

private void storeIndex(Field field, Object me, Object arg) {
    if (field.getAnnotation(RIndex.class) == null) {
        return;
    }
    NamingScheme namingScheme = commandExecutor.getObjectBuilder().getNamingScheme(me.getClass().getSuperclass());
    String indexName = namingScheme.getIndexName(me.getClass().getSuperclass(), field.getName());
    boolean skipExecution = false;
    CommandBatchService ce;
    if (commandExecutor instanceof CommandBatchService) {
        ce = (CommandBatchService) commandExecutor;
        skipExecution = true;
    } else {
        ce = new CommandBatchService(commandExecutor);
    }
    if (arg instanceof Number) {
        RScoredSortedSetAsync<Object> set = new RedissonScoredSortedSet<>(namingScheme.getCodec(), ce, indexName, null);
        set.addAsync(((Number) arg).doubleValue(), ((RLiveObject) me).getLiveObjectId());
    } else {
        RMultimapAsync<Object, Object> map = new RedissonSetMultimap<>(namingScheme.getCodec(), ce, indexName);
        map.putAsync(arg, ((RLiveObject) me).getLiveObjectId());
    }
    if (!skipExecution) {
        ce.execute();
    }
}
Also used : RedissonSetMultimap(org.redisson.RedissonSetMultimap) RIndex(org.redisson.api.annotation.RIndex) NamingScheme(org.redisson.liveobject.resolver.NamingScheme) CommandBatchService(org.redisson.command.CommandBatchService) RedissonScoredSortedSet(org.redisson.RedissonScoredSortedSet) RedissonObject(org.redisson.RedissonObject)

Example 9 with NamingScheme

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

the class RedissonLiveObjectService method delete.

@Override
public <T> long delete(Class<T> entityClass, Object... ids) {
    CommandBatchService ce = new CommandBatchService(commandExecutor);
    FieldList<InDefinedShape> fields = Introspectior.getFieldsWithAnnotation(entityClass.getSuperclass(), RIndex.class);
    Set<String> fieldNames = fields.stream().map(f -> f.getName()).collect(Collectors.toSet());
    NamingScheme namingScheme = commandExecutor.getObjectBuilder().getNamingScheme(entityClass);
    for (Object id : ids) {
        delete(id, entityClass, namingScheme, ce, fieldNames);
    }
    BatchResult<Object> r = (BatchResult<Object>) ce.execute();
    return r.getResponses().stream().filter(s -> s instanceof Long).mapToLong(s -> (Long) s).sum();
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) org.redisson.api.annotation(org.redisson.api.annotation) CommandAsyncExecutor(org.redisson.command.CommandAsyncExecutor) java.util(java.util) ClassUtils(org.redisson.liveobject.misc.ClassUtils) CommandBatchService(org.redisson.command.CommandBatchService) RIdResolver(org.redisson.liveobject.resolver.RIdResolver) MethodDelegation(net.bytebuddy.implementation.MethodDelegation) ByteBuddy(net.bytebuddy.ByteBuddy) ListScanResult(org.redisson.client.protocol.decoder.ListScanResult) ObjectListReplayDecoder(org.redisson.client.protocol.decoder.ObjectListReplayDecoder) FieldDescription(net.bytebuddy.description.field.FieldDescription) BeanCopy(jodd.bean.BeanCopy) Condition(org.redisson.api.condition.Condition) Constructor(java.lang.reflect.Constructor) ConcurrentMap(java.util.concurrent.ConcurrentMap) ListScanResultReplayDecoder(org.redisson.client.protocol.decoder.ListScanResultReplayDecoder) Introspectior(org.redisson.liveobject.misc.Introspectior) org.redisson.api(org.redisson.api) AdvBeanCopy(org.redisson.liveobject.misc.AdvBeanCopy) org.redisson.liveobject.core(org.redisson.liveobject.core) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LiveObjectTemplate(org.redisson.liveobject.LiveObjectTemplate) Convertor(org.redisson.client.protocol.convertor.Convertor) LiveObjectSearch(org.redisson.liveobject.LiveObjectSearch) InDefinedShape(net.bytebuddy.description.field.FieldDescription.InDefinedShape) ClassLoadingStrategy(net.bytebuddy.dynamic.loading.ClassLoadingStrategy) Field(java.lang.reflect.Field) ClassIntrospector(jodd.introspector.ClassIntrospector) Collectors(java.util.stream.Collectors) ElementMatchers(net.bytebuddy.matcher.ElementMatchers) FieldList(net.bytebuddy.description.field.FieldList) Entry(java.util.Map.Entry) NamingScheme(org.redisson.liveobject.resolver.NamingScheme) FieldProxy(net.bytebuddy.implementation.bind.annotation.FieldProxy) RedisCommand(org.redisson.client.protocol.RedisCommand) ListMultiDecoder2(org.redisson.client.protocol.decoder.ListMultiDecoder2) BeanUtil(jodd.bean.BeanUtil) NamingScheme(org.redisson.liveobject.resolver.NamingScheme) CommandBatchService(org.redisson.command.CommandBatchService) InDefinedShape(net.bytebuddy.description.field.FieldDescription.InDefinedShape)

Example 10 with NamingScheme

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

the class RedissonLiveObjectService method findIds.

@Override
public <K> Iterable<K> findIds(Class<?> entityClass, int count) {
    NamingScheme namingScheme = commandExecutor.getObjectBuilder().getNamingScheme(entityClass);
    String pattern = namingScheme.getNamePattern(entityClass);
    RedissonKeys keys = new RedissonKeys(commandExecutor);
    RedisCommand<ListScanResult<String>> command = new RedisCommand<>("SCAN", new ListMultiDecoder2(new ListScanResultReplayDecoder(), new ObjectListReplayDecoder<Object>()), new Convertor<Object>() {

        @Override
        public Object convert(Object obj) {
            if (!(obj instanceof String)) {
                return obj;
            }
            return namingScheme.resolveId(obj.toString());
        }
    });
    return keys.getKeysByPattern(command, pattern, 0, count);
}
Also used : ListScanResult(org.redisson.client.protocol.decoder.ListScanResult) NamingScheme(org.redisson.liveobject.resolver.NamingScheme) ListMultiDecoder2(org.redisson.client.protocol.decoder.ListMultiDecoder2) RedisCommand(org.redisson.client.protocol.RedisCommand) ListScanResultReplayDecoder(org.redisson.client.protocol.decoder.ListScanResultReplayDecoder) ObjectListReplayDecoder(org.redisson.client.protocol.decoder.ObjectListReplayDecoder)

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