Search in sources :

Example 1 with RList

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

use of org.redisson.api.RList in project gora by apache.

the class RedisStore method deleteByQuery.

@Override
public long deleteByQuery(Query<K, T> query) throws GoraException {
    Collection<K> range = runQuery(query);
    RBatch batchInstance = redisInstance.createBatch();
    RLexSortedSetAsync secundaryIndex = batchInstance.getLexSortedSet(generateIndexKey());
    if (query.getFields() != null && query.getFields().length < mapping.getFields().size()) {
        List<String> dbFields = new ArrayList<>();
        List<RedisType> dbTypes = new ArrayList<>();
        for (String af : query.getFields()) {
            dbFields.add(mapping.getFields().get(af));
            dbTypes.add(mapping.getTypes().get(af));
        }
        for (K key : range) {
            if (mode == StorageMode.SINGLEKEY) {
                RMapAsync<Object, Object> map = batchInstance.getMap(generateKeyHash(key));
                dbFields.forEach((field) -> {
                    map.removeAsync(field);
                });
            } else {
                for (int indexField = 0; indexField < dbFields.size(); indexField++) {
                    String field = dbFields.get(indexField);
                    RedisType type = dbTypes.get(indexField);
                    switch(type) {
                        case STRING:
                            RBucketAsync<Object> bucket = batchInstance.getBucket(generateKeyString(field, key));
                            bucket.deleteAsync();
                            break;
                        case LIST:
                            RListAsync<Object> rlist = batchInstance.getList(generateKeyString(field, key));
                            rlist.deleteAsync();
                            break;
                        case HASH:
                            RMapAsync<Object, Object> map = batchInstance.getMap(generateKeyString(field, key));
                            map.deleteAsync();
                            break;
                        default:
                            throw new AssertionError(type.name());
                    }
                }
            }
        }
    } else {
        range.stream().map((key) -> {
            secundaryIndex.removeAsync(key);
            return key;
        }).forEachOrdered((key) -> {
            if (mode == StorageMode.SINGLEKEY) {
                RMapAsync<Object, Object> map = batchInstance.getMap(generateKeyHash(key));
                map.deleteAsync();
            } else {
                redisInstance.getKeys().deleteByPattern(generateKeyStringBase(key) + WILDCARD);
            }
        });
    }
    batchInstance.execute();
    return range.size();
}
Also used : RLexSortedSetAsync(org.redisson.api.RLexSortedSetAsync) Config(org.redisson.config.Config) StorageMode(org.apache.gora.redis.util.StorageMode) LoggerFactory(org.slf4j.LoggerFactory) ReadMode(org.redisson.config.ReadMode) RedisResult(org.apache.gora.redis.query.RedisResult) Map(java.util.Map) RBatch(org.redisson.api.RBatch) GoraException(org.apache.gora.util.GoraException) Schema(org.apache.avro.Schema) RScoredSortedSetAsync(org.redisson.api.RScoredSortedSetAsync) PersistentBase(org.apache.gora.persistency.impl.PersistentBase) MethodHandles(java.lang.invoke.MethodHandles) Collection(java.util.Collection) Result(org.apache.gora.query.Result) Collectors(java.util.stream.Collectors) IOUtils(org.apache.commons.io.IOUtils) GORA_REDIS_READMODE(org.apache.gora.redis.util.RedisStoreConstants.GORA_REDIS_READMODE) List(java.util.List) RedisQuery(org.apache.gora.redis.query.RedisQuery) RListAsync(org.redisson.api.RListAsync) PartitionQueryImpl(org.apache.gora.query.impl.PartitionQueryImpl) RBucketAsync(org.redisson.api.RBucketAsync) RScoredSortedSet(org.redisson.api.RScoredSortedSet) RLexSortedSet(org.redisson.api.RLexSortedSet) PREFIX(org.apache.gora.redis.util.RedisStoreConstants.PREFIX) GORA_REDIS_STORAGE(org.apache.gora.redis.util.RedisStoreConstants.GORA_REDIS_STORAGE) RList(org.redisson.api.RList) DataStoreBase(org.apache.gora.store.impl.DataStoreBase) ArrayList(java.util.ArrayList) RFuture(org.redisson.api.RFuture) RMap(org.redisson.api.RMap) RBucket(org.redisson.api.RBucket) Charset(java.nio.charset.Charset) WILDCARD(org.apache.gora.redis.util.RedisStoreConstants.WILDCARD) PartitionQuery(org.apache.gora.query.PartitionQuery) START_TAG(org.apache.gora.redis.util.RedisStoreConstants.START_TAG) RMapAsync(org.redisson.api.RMapAsync) RedissonClient(org.redisson.api.RedissonClient) GORA_REDIS_MODE(org.apache.gora.redis.util.RedisStoreConstants.GORA_REDIS_MODE) Properties(java.util.Properties) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) END_TAG(org.apache.gora.redis.util.RedisStoreConstants.END_TAG) GORA_REDIS_MASTERNAME(org.apache.gora.redis.util.RedisStoreConstants.GORA_REDIS_MASTERNAME) IOException(java.io.IOException) INDEX(org.apache.gora.redis.util.RedisStoreConstants.INDEX) ServerMode(org.apache.gora.redis.util.ServerMode) ScoredEntry(org.redisson.client.protocol.ScoredEntry) Redisson(org.redisson.Redisson) FIELD_SEPARATOR(org.apache.gora.redis.util.RedisStoreConstants.FIELD_SEPARATOR) DatumHandler(org.apache.gora.redis.util.DatumHandler) GORA_REDIS_ADDRESS(org.apache.gora.redis.util.RedisStoreConstants.GORA_REDIS_ADDRESS) Query(org.apache.gora.query.Query) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) RLexSortedSetAsync(org.redisson.api.RLexSortedSetAsync) RBatch(org.redisson.api.RBatch)

Aggregations

ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Map (java.util.Map)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 MethodHandles (java.lang.invoke.MethodHandles)1 Charset (java.nio.charset.Charset)1 Deque (java.util.Deque)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Properties (java.util.Properties)1 Set (java.util.Set)1 SortedSet (java.util.SortedSet)1 TreeSet (java.util.TreeSet)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 Collectors (java.util.stream.Collectors)1