Search in sources :

Example 1 with RLexSortedSetAsync

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

the class RedisStore method put.

@Override
public void put(K key, T obj) throws GoraException {
    try {
        if (obj.isDirty()) {
            Schema objectSchema = obj.getSchema();
            List<Schema.Field> fields = objectSchema.getFields();
            RBatch batchInstance = redisInstance.createBatch();
            // update secundary index
            if (isNumericKey()) {
                RScoredSortedSetAsync<Object> secundaryIndex = batchInstance.getScoredSortedSet(generateIndexKey());
                secundaryIndex.addAsync(obtainDoubleValue(key), key);
            } else {
                RLexSortedSetAsync secundaryIndex = batchInstance.getLexSortedSet(generateIndexKey());
                secundaryIndex.addAsync(key.toString());
            }
            if (mode == StorageMode.SINGLEKEY) {
                RMapAsync<Object, Object> map = batchInstance.getMap(generateKeyHash(key));
                fields.forEach((field) -> {
                    Object fieldValue = handler.serializeFieldValue(field.schema(), obj.get(field.pos()));
                    if (fieldValue != null) {
                        map.fastPutAsync(mapping.getFields().get(field.name()), fieldValue);
                    } else {
                        map.fastRemoveAsync(mapping.getFields().get(field.name()));
                    }
                });
            } else {
                for (Schema.Field field : fields) {
                    Object fieldValue = obj.get(field.pos());
                    String redisField = mapping.getFields().get(field.name());
                    RedisType redisType = mapping.getTypes().get(field.name());
                    switch(redisType) {
                        case STRING:
                            RBucketAsync<Object> bucket = batchInstance.getBucket(generateKeyString(redisField, key));
                            bucket.deleteAsync();
                            if (fieldValue != null) {
                                fieldValue = handler.serializeFieldValue(field.schema(), fieldValue);
                                bucket.setAsync(fieldValue);
                            }
                            break;
                        case LIST:
                            RListAsync<Object> rlist = batchInstance.getList(generateKeyString(redisField, key));
                            rlist.deleteAsync();
                            if (fieldValue != null) {
                                List<Object> list = handler.serializeFieldList(field.schema(), fieldValue);
                                rlist.addAllAsync(list);
                            }
                            break;
                        case HASH:
                            RMapAsync<Object, Object> map = batchInstance.getMap(generateKeyString(redisField, key));
                            map.deleteAsync();
                            if (fieldValue != null) {
                                Map<Object, Object> mp = handler.serializeFieldMap(field.schema(), fieldValue);
                                map.putAllAsync(mp);
                            }
                            break;
                        default:
                            throw new AssertionError(redisType.name());
                    }
                }
            }
            batchInstance.execute();
        } else {
            LOG.info("Ignored putting object {} in the store as it is neither " + "new, neither dirty.", new Object[] { obj });
        }
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : Schema(org.apache.avro.Schema) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException) RLexSortedSetAsync(org.redisson.api.RLexSortedSetAsync) GoraException(org.apache.gora.util.GoraException) RBatch(org.redisson.api.RBatch)

Example 2 with RLexSortedSetAsync

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

the class RedisStore method delete.

@Override
public boolean delete(K key) throws GoraException {
    try {
        RBatch batchInstance = redisInstance.createBatch();
        // update secundary index
        if (isNumericKey()) {
            RScoredSortedSetAsync<Object> secundaryIndex = batchInstance.getScoredSortedSet(generateIndexKey());
            secundaryIndex.removeAsync(key);
        } else {
            RLexSortedSetAsync secundaryIndex = batchInstance.getLexSortedSet(generateIndexKey());
            secundaryIndex.removeAsync(key.toString());
        }
        if (mode == StorageMode.SINGLEKEY) {
            RMapAsync<Object, Object> map = batchInstance.getMap(generateKeyHash(key));
            RFuture<Boolean> deleteAsync = map.deleteAsync();
            batchInstance.execute();
            return deleteAsync.get();
        } else {
            batchInstance.execute();
            return redisInstance.getKeys().deleteByPattern(generateKeyStringBase(key) + WILDCARD) > 0;
        }
    } catch (Exception ex) {
        throw new GoraException(ex);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) RBatch(org.redisson.api.RBatch) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException) RLexSortedSetAsync(org.redisson.api.RLexSortedSetAsync)

Example 3 with RLexSortedSetAsync

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

IOException (java.io.IOException)3 GoraException (org.apache.gora.util.GoraException)3 RBatch (org.redisson.api.RBatch)3 Schema (org.apache.avro.Schema)2 RLexSortedSetAsync (org.redisson.api.RLexSortedSetAsync)2 InputStream (java.io.InputStream)1 MethodHandles (java.lang.invoke.MethodHandles)1 Charset (java.nio.charset.Charset)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Properties (java.util.Properties)1 Collectors (java.util.stream.Collectors)1 IOUtils (org.apache.commons.io.IOUtils)1 PersistentBase (org.apache.gora.persistency.impl.PersistentBase)1 PartitionQuery (org.apache.gora.query.PartitionQuery)1 Query (org.apache.gora.query.Query)1 Result (org.apache.gora.query.Result)1