Search in sources :

Example 1 with RSet

use of org.redisson.api.RSet in project redisson by redisson.

the class RedissonLiveObjectService method detach.

@SuppressWarnings("unchecked")
private <T> T detach(T attachedObject, Map<String, Object> alreadyDetached) {
    validateAttached(attachedObject);
    try {
        T detached = instantiateDetachedObject((Class<T>) attachedObject.getClass().getSuperclass(), asLiveObject(attachedObject).getLiveObjectId());
        BeanCopy.beans(attachedObject, detached).declared(true, true).copy();
        alreadyDetached.put(getMap(attachedObject).getName(), detached);
        for (Entry<String, Object> obj : getMap(attachedObject).entrySet()) {
            if (!checkCascade(attachedObject, RCascadeType.DETACH, obj.getKey())) {
                continue;
            }
            if (obj.getValue() instanceof RSortedSet) {
                SortedSet<Object> redissonSet = (SortedSet<Object>) obj.getValue();
                Set<Object> set = new TreeSet<Object>(redissonSet.comparator());
                for (Object object : redissonSet) {
                    if (isLiveObject(object)) {
                        Object detachedObject = alreadyDetached.get(getMap(object).getName());
                        if (detachedObject == null) {
                            detachedObject = detach(object, alreadyDetached);
                        }
                        object = detachedObject;
                    }
                    set.add(object);
                }
                ClassUtils.setField(detached, obj.getKey(), set);
            } else if (obj.getValue() instanceof RDeque) {
                Collection<Object> redissonDeque = (Collection<Object>) obj.getValue();
                Deque<Object> deque = new LinkedList<Object>();
                for (Object object : redissonDeque) {
                    if (isLiveObject(object)) {
                        Object detachedObject = alreadyDetached.get(getMap(object).getName());
                        if (detachedObject == null) {
                            detachedObject = detach(object, alreadyDetached);
                        }
                        object = detachedObject;
                    }
                    deque.add(object);
                }
                ClassUtils.setField(detached, obj.getKey(), deque);
            } else if (obj.getValue() instanceof RQueue) {
                Collection<Object> redissonQueue = (Collection<Object>) obj.getValue();
                Queue<Object> queue = new LinkedList<Object>();
                for (Object object : redissonQueue) {
                    if (isLiveObject(object)) {
                        Object detachedObject = alreadyDetached.get(getMap(object).getName());
                        if (detachedObject == null) {
                            detachedObject = detach(object, alreadyDetached);
                        }
                        object = detachedObject;
                    }
                    queue.add(object);
                }
                ClassUtils.setField(detached, obj.getKey(), queue);
            } else if (obj.getValue() instanceof RSet) {
                Set<Object> set = new HashSet<Object>();
                Collection<Object> redissonSet = (Collection<Object>) obj.getValue();
                for (Object object : redissonSet) {
                    if (isLiveObject(object)) {
                        Object detachedObject = alreadyDetached.get(getMap(object).getName());
                        if (detachedObject == null) {
                            detachedObject = detach(object, alreadyDetached);
                        }
                        object = detachedObject;
                    }
                    set.add(object);
                }
                ClassUtils.setField(detached, obj.getKey(), set);
            } else if (obj.getValue() instanceof RList) {
                List<Object> list = new ArrayList<Object>();
                Collection<Object> redissonList = (Collection<Object>) obj.getValue();
                for (Object object : redissonList) {
                    if (isLiveObject(object)) {
                        Object detachedObject = alreadyDetached.get(getMap(object).getName());
                        if (detachedObject == null) {
                            detachedObject = detach(object, alreadyDetached);
                        }
                        object = detachedObject;
                    }
                    list.add(object);
                }
                ClassUtils.setField(detached, obj.getKey(), list);
            } else if (isLiveObject(obj.getValue())) {
                Object detachedObject = alreadyDetached.get(getMap(obj.getValue()).getName());
                if (detachedObject == null) {
                    detachedObject = detach(obj.getValue(), alreadyDetached);
                }
                ClassUtils.setField(detached, obj.getKey(), detachedObject);
            } else if (obj.getValue() instanceof RMap) {
                Map<Object, Object> map = new LinkedHashMap<Object, Object>();
                Map<Object, Object> redissonMap = (Map<Object, Object>) obj.getValue();
                for (Entry<Object, Object> entry : redissonMap.entrySet()) {
                    Object key = entry.getKey();
                    Object value = entry.getValue();
                    if (isLiveObject(key)) {
                        Object detachedObject = alreadyDetached.get(getMap(key).getName());
                        if (detachedObject == null) {
                            detachedObject = detach(key, alreadyDetached);
                        }
                        key = detachedObject;
                    }
                    if (isLiveObject(value)) {
                        Object detachedObject = alreadyDetached.get(getMap(value).getName());
                        if (detachedObject == null) {
                            detachedObject = detach(value, alreadyDetached);
                        }
                        value = detachedObject;
                    }
                    map.put(key, value);
                }
                ClassUtils.setField(detached, obj.getKey(), map);
            } else {
                validateAnnotation(detached, obj.getKey());
            }
        }
        return detached;
    } catch (Exception ex) {
        throw ex instanceof RuntimeException ? (RuntimeException) ex : new RuntimeException(ex);
    }
}
Also used : SortedSet(java.util.SortedSet) Set(java.util.Set) RSortedSet(org.redisson.api.RSortedSet) RSet(org.redisson.api.RSet) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) RQueue(org.redisson.api.RQueue) ArrayList(java.util.ArrayList) RMap(org.redisson.api.RMap) SortedSet(java.util.SortedSet) RSortedSet(org.redisson.api.RSortedSet) LinkedHashMap(java.util.LinkedHashMap) TreeSet(java.util.TreeSet) RSet(org.redisson.api.RSet) Deque(java.util.Deque) RDeque(org.redisson.api.RDeque) LinkedList(java.util.LinkedList) RDeque(org.redisson.api.RDeque) RList(org.redisson.api.RList) Collection(java.util.Collection) RObject(org.redisson.api.RObject) RLiveObject(org.redisson.api.RLiveObject) RSortedSet(org.redisson.api.RSortedSet) Map(java.util.Map) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) RMap(org.redisson.api.RMap)

Example 2 with RSet

use of org.redisson.api.RSet in project redisson by redisson.

the class RedissonSessionManager method startInternal.

@Override
protected void startInternal() throws LifecycleException {
    super.startInternal();
    redisson = buildClient();
    final ClassLoader applicationClassLoader;
    if (getContext().getLoader().getClassLoader() != null) {
        applicationClassLoader = getContext().getLoader().getClassLoader();
    } else if (Thread.currentThread().getContextClassLoader() != null) {
        applicationClassLoader = Thread.currentThread().getContextClassLoader();
    } else {
        applicationClassLoader = getClass().getClassLoader();
    }
    Codec codec = redisson.getConfig().getCodec();
    try {
        codecToUse = codec.getClass().getConstructor(ClassLoader.class, codec.getClass()).newInstance(applicationClassLoader, codec);
    } catch (Exception e) {
        throw new LifecycleException(e);
    }
    Pipeline pipeline = getContext().getPipeline();
    synchronized (pipeline) {
        if (readMode == ReadMode.REDIS) {
            Optional<Valve> res = Arrays.stream(pipeline.getValves()).filter(v -> v.getClass() == UsageValve.class).findAny();
            if (res.isPresent()) {
                ((UsageValve) res.get()).incUsage();
            } else {
                pipeline.addValve(new UsageValve());
            }
        }
        if (updateMode == UpdateMode.AFTER_REQUEST) {
            Optional<Valve> res = Arrays.stream(pipeline.getValves()).filter(v -> v.getClass() == UpdateValve.class).findAny();
            if (res.isPresent()) {
                ((UpdateValve) res.get()).incUsage();
            } else {
                pipeline.addValve(new UpdateValve());
            }
        }
    }
    if (readMode == ReadMode.MEMORY && this.broadcastSessionUpdates || broadcastSessionEvents) {
        RTopic updatesTopic = getTopic();
        messageListener = new MessageListener<AttributeMessage>() {

            @Override
            public void onMessage(CharSequence channel, AttributeMessage msg) {
                try {
                    if (msg.getNodeId().equals(nodeId)) {
                        return;
                    }
                    RedissonSession session = (RedissonSession) RedissonSessionManager.super.findSession(msg.getSessionId());
                    if (session != null) {
                        if (msg instanceof SessionDestroyedMessage) {
                            session.expire();
                        }
                        if (msg instanceof AttributeRemoveMessage) {
                            for (String name : ((AttributeRemoveMessage) msg).getNames()) {
                                session.superRemoveAttributeInternal(name, true);
                            }
                        }
                        if (msg instanceof AttributesClearMessage) {
                            RedissonSessionManager.super.remove(session, false);
                        }
                        if (msg instanceof AttributesPutAllMessage) {
                            AttributesPutAllMessage m = (AttributesPutAllMessage) msg;
                            Map<String, Object> attrs = m.getAttrs(codecToUse.getMapValueDecoder());
                            session.load(attrs);
                        }
                        if (msg instanceof AttributeUpdateMessage) {
                            AttributeUpdateMessage m = (AttributeUpdateMessage) msg;
                            session.superSetAttribute(m.getName(), m.getValue(codecToUse.getMapValueDecoder()), true);
                        }
                    } else {
                        if (msg instanceof SessionCreatedMessage) {
                            Session s = findSession(msg.getSessionId());
                            if (s == null) {
                                throw new IllegalStateException("Unable to find session: " + msg.getSessionId());
                            }
                        }
                        if (msg instanceof SessionDestroyedMessage) {
                            Session s = findSession(msg.getSessionId(), false);
                            if (s == null) {
                                throw new IllegalStateException("Unable to find session: " + msg.getSessionId());
                            }
                            s.expire();
                            RSet<String> set = getNotifiedNodes(msg.getSessionId());
                            set.add(nodeId);
                        }
                    }
                } catch (Exception e) {
                    log.error("Unable to handle topic message", e);
                }
            }
        };
        updatesTopic.addListener(AttributeMessage.class, messageListener);
    }
    setState(LifecycleState.STARTING);
}
Also used : HttpSession(javax.servlet.http.HttpSession) java.util(java.util) Codec(org.redisson.client.codec.Codec) StringCodec(org.redisson.client.codec.StringCodec) ManagerBase(org.apache.catalina.session.ManagerBase) Config(org.redisson.config.Config) RTopic(org.redisson.api.RTopic) RSet(org.redisson.api.RSet) IOException(java.io.IOException) Log(org.apache.juli.logging.Log) LogFactory(org.apache.juli.logging.LogFactory) File(java.io.File) Redisson(org.redisson.Redisson) RMap(org.redisson.api.RMap) CompositeCodec(org.redisson.codec.CompositeCodec) org.apache.catalina(org.apache.catalina) RedissonClient(org.redisson.api.RedissonClient) MessageListener(org.redisson.api.listener.MessageListener) Codec(org.redisson.client.codec.Codec) StringCodec(org.redisson.client.codec.StringCodec) CompositeCodec(org.redisson.codec.CompositeCodec) RSet(org.redisson.api.RSet) IOException(java.io.IOException) RTopic(org.redisson.api.RTopic) RMap(org.redisson.api.RMap) HttpSession(javax.servlet.http.HttpSession)

Example 3 with RSet

use of org.redisson.api.RSet 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)

Aggregations

RSet (org.redisson.api.RSet)3 java.util (java.util)2 RMap (org.redisson.api.RMap)2 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Deque (java.util.Deque)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 SortedSet (java.util.SortedSet)1 TreeSet (java.util.TreeSet)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 BiFunction (java.util.function.BiFunction)1 HttpSession (javax.servlet.http.HttpSession)1