use of org.apache.gora.redis.util.RedisStoreConstants.WILDCARD 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();
}
Aggregations