Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.redisson.RedissonSetMultimap Maven / Gradle / Ivy
Go to download
Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava3/Reactive API. Client side caching. Over 50 Redis based Java objects and services: JCache API, Apache Tomcat, Hibernate, Spring, Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Scheduler, RPC
/**
* Copyright (c) 2013-2024 Nikita Koksharov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.redisson;
import io.netty.buffer.ByteBuf;
import org.redisson.api.RFuture;
import org.redisson.api.RSet;
import org.redisson.api.RSetMultimap;
import org.redisson.client.codec.Codec;
import org.redisson.client.protocol.RedisCommand;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.client.protocol.RedisStrictCommand;
import org.redisson.client.protocol.convertor.BooleanAmountReplayConvertor;
import org.redisson.client.protocol.convertor.BooleanReplayConvertor;
import org.redisson.command.CommandAsyncExecutor;
import org.redisson.misc.CompletableFutureWrapper;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
/**
* @author Nikita Koksharov
*
* @param key
* @param value
*/
public class RedissonSetMultimap extends RedissonMultimap implements RSetMultimap {
private static final RedisStrictCommand SCARD_VALUE = new RedisStrictCommand("SCARD", new BooleanAmountReplayConvertor());
private static final RedisCommand SISMEMBER_VALUE = new RedisCommand("SISMEMBER", new BooleanReplayConvertor());
public RedissonSetMultimap(CommandAsyncExecutor connectionManager, String name) {
super(connectionManager, name);
}
public RedissonSetMultimap(Codec codec, CommandAsyncExecutor connectionManager, String name) {
super(codec, connectionManager, name);
}
@Override
public RFuture sizeAsync() {
return commandExecutor.evalReadAsync(getRawName(), codec, RedisCommands.EVAL_INTEGER,
"local keys = redis.call('hgetall', KEYS[1]); " +
"local size = 0; " +
"for i, v in ipairs(keys) do " +
"if i % 2 == 0 then " +
"local name = ARGV[1] .. v; " +
"size = size + redis.call('scard', name); " +
"end;" +
"end; " +
"return size; ",
Arrays.asList(getRawName()),
prefix);
}
@Override
public RFuture fastRemoveValueAsync(V... values) {
List args = new ArrayList<>(values.length + 1);
args.add(prefix);
encodeMapValues(args, Arrays.asList(values));
return commandExecutor.evalWriteAsync(getRawName(), codec, RedisCommands.EVAL_INTEGER,
"local keys = redis.call('hgetall', KEYS[1]); " +
"local size = 0; " +
"for i, v in ipairs(keys) do " +
"if i % 2 == 0 then " +
"local name = ARGV[1] .. v; " +
"for j = 2, #ARGV, 1 do " +
"size = size + redis.call('srem', name, ARGV[j]); " +
"end; " +
"if redis.call('exists', name) == 0 then " +
"redis.call('hdel', KEYS[1], keys[i-1]); " +
"end; " +
"end;" +
"end; " +
"return 0; ",
Arrays.asList(getRawName()),
args.toArray());
}
@Override
public RFuture containsKeyAsync(Object key) {
String keyHash = keyHash(key);
String setName = getValuesName(keyHash);
return commandExecutor.readAsync(getRawName(), codec, SCARD_VALUE, setName);
}
@Override
public RFuture containsValueAsync(Object value) {
ByteBuf valueState = encodeMapValue(value);
return commandExecutor.evalReadAsync(getRawName(), codec, RedisCommands.EVAL_BOOLEAN,
"local keys = redis.call('hgetall', KEYS[1]); " +
"for i, v in ipairs(keys) do " +
"if i % 2 == 0 then " +
"local name = ARGV[2] .. v; " +
"if redis.call('sismember', name, ARGV[1]) == 1 then "
+ "return 1; " +
"end;" +
"end;" +
"end; " +
"return 0; ",
Arrays.asList(getRawName()),
valueState, prefix);
}
@Override
public RFuture containsEntryAsync(Object key, Object value) {
String keyHash = keyHash(key);
ByteBuf valueState = encodeMapValue(value);
String setName = getValuesName(keyHash);
return commandExecutor.readAsync(getRawName(), codec, SISMEMBER_VALUE, setName, valueState);
}
@Override
public RFuture putAsync(K key, V value) {
ByteBuf keyState = encodeMapKey(key);
String keyHash = hash(keyState);
ByteBuf valueState = encodeMapValue(value);
String setName = getValuesName(keyHash);
return commandExecutor.evalWriteAsync(getRawName(), codec, RedisCommands.EVAL_BOOLEAN,
"redis.call('hset', KEYS[1], ARGV[1], ARGV[2]); " +
"return redis.call('sadd', KEYS[2], ARGV[3]); ",
Arrays.asList(getRawName(), setName), keyState, keyHash, valueState);
}
@Override
public RFuture removeAsync(Object key, Object value) {
ByteBuf keyState = encodeMapKey(key);
String keyHash = hash(keyState);
ByteBuf valueState = encodeMapValue(value);
String setName = getValuesName(keyHash);
return commandExecutor.evalWriteAsync(getRawName(), codec, RedisCommands.EVAL_BOOLEAN,
"local res = redis.call('srem', KEYS[2], ARGV[2]); "
+ "if res == 1 and redis.call('scard', KEYS[2]) == 0 then "
+ "redis.call('hdel', KEYS[1], ARGV[1]); "
+ "end; "
+ "return res; ",
Arrays.asList(getRawName(), setName), keyState, valueState);
}
@Override
public RFuture putAllAsync(K key, Iterable extends V> values) {
List params = new ArrayList();
ByteBuf keyState = encodeMapKey(key);
params.add(keyState);
String keyHash = hash(keyState);
params.add(keyHash);
for (Object value : values) {
ByteBuf valueState = encodeMapValue(value);
params.add(valueState);
}
String setName = getValuesName(keyHash);
return commandExecutor.evalWriteAsync(getRawName(), codec, RedisCommands.EVAL_BOOLEAN_AMOUNT,
"redis.call('hset', KEYS[1], ARGV[1], ARGV[2]); " +
"local n = 0; " +
"for i=3, #ARGV, 5000 do " +
"n = n + redis.call('sadd', KEYS[2], unpack(ARGV, i, math.min(i+4999, table.getn(ARGV)))) " +
"end; " +
"return n; ",
Arrays.asList(getRawName(), setName), params.toArray());
}
@Override
public RSet get(final K key) {
String keyHash = keyHash(key);
final String setName = getValuesName(keyHash);
return new RedissonSet(codec, commandExecutor, setName, null) {
@Override
public RFuture addAsync(V value) {
return RedissonSetMultimap.this.putAsync(key, value);
}
@Override
public RFuture addAllAsync(Collection extends V> c) {
return RedissonSetMultimap.this.putAllAsync(key, c);
}
@Override
public RFuture removeAsync(Object value) {
return RedissonSetMultimap.this.removeAsync(key, value);
}
@Override
public RFuture removeAllAsync(Collection> c) {
if (c.isEmpty()) {
return new CompletableFutureWrapper<>(false);
}
List args = new ArrayList(c.size() + 1);
args.add(encodeMapKey(key));
encode(args, c);
return commandExecutor.evalWriteAsync(RedissonSetMultimap.this.getRawName(), codec, RedisCommands.EVAL_BOOLEAN_AMOUNT,
"local count = 0;" +
"for i=2, #ARGV, 5000 do " +
"count = count + redis.call('srem', KEYS[2], unpack(ARGV, i, math.min(i+4999, table.getn(ARGV)))) " +
"end; " +
"if count > 0 then " +
"if redis.call('scard', KEYS[2]) == 0 then " +
"redis.call('hdel', KEYS[1], ARGV[1]); " +
"end; " +
"return 1;" +
"end;" +
"return 0; ",
Arrays.asList(RedissonSetMultimap.this.getRawName(), setName),
args.toArray());
}
@Override
public RFuture deleteAsync() {
ByteBuf keyState = encodeMapKey(key);
return RedissonSetMultimap.this.fastRemoveAsync(Arrays.asList(keyState),
Arrays.asList(RedissonSetMultimap.this.getRawName(), setName), RedisCommands.EVAL_BOOLEAN_AMOUNT);
}
@Override
public RFuture clearExpireAsync() {
throw new UnsupportedOperationException("This operation is not supported for SetMultimap values Set");
}
@Override
public RFuture expireAsync(long timeToLive, TimeUnit timeUnit, String param, String... keys) {
throw new UnsupportedOperationException("This operation is not supported for SetMultimap values Set");
}
@Override
protected RFuture expireAtAsync(long timestamp, String param, String... keys) {
throw new UnsupportedOperationException("This operation is not supported for SetMultimap values Set");
}
@Override
public RFuture remainTimeToLiveAsync() {
throw new UnsupportedOperationException("This operation is not supported for SetMultimap values Set");
}
@Override
public RFuture renameAsync(String newName) {
throw new UnsupportedOperationException("This operation is not supported for SetMultimap values Set");
}
@Override
public RFuture renamenxAsync(String newName) {
throw new UnsupportedOperationException("This operation is not supported for SetMultimap values Set");
}
};
}
@Override
public Set getAll(K key) {
return (Set) super.getAll(key);
}
@Override
public RFuture> getAllAsync(K key) {
String keyHash = keyHash(key);
String setName = getValuesName(keyHash);
return commandExecutor.readAsync(getRawName(), codec, RedisCommands.SMEMBERS, setName);
}
@Override
public Set removeAll(Object key) {
return (Set) get(removeAllAsync(key));
}
@Override
public RFuture> removeAllAsync(Object key) {
ByteBuf keyState = encodeMapKey(key);
String keyHash = hash(keyState);
String setName = getValuesName(keyHash);
return commandExecutor.evalWriteAsync(getRawName(), codec, RedisCommands.EVAL_SET,
"redis.call('hdel', KEYS[1], ARGV[1]); " +
"local members = redis.call('smembers', KEYS[2]); " +
"redis.call('del', KEYS[2]); " +
"return members; ",
Arrays.asList(getRawName(), setName), keyState);
}
@Override
public Set> entries() {
return (Set>) super.entries();
}
@Override
public Set replaceValues(K key, Iterable extends V> values) {
return (Set) get(replaceValuesAsync(key, values));
}
@Override
Iterator valuesIterator() {
return new RedissonSetMultimapIterator(RedissonSetMultimap.this, commandExecutor, codec) {
@Override
V getValue(V entry) {
return (V) entry;
}
};
}
@Override
RedissonSetMultimapIterator> entryIterator() {
return new RedissonSetMultimapIterator<>(RedissonSetMultimap.this, commandExecutor, codec);
}
@Override
public RFuture> replaceValuesAsync(K key, Iterable extends V> values) {
List params = new ArrayList();
ByteBuf keyState = encodeMapKey(key);
params.add(keyState);
String keyHash = hash(keyState);
params.add(keyHash);
for (Object value : values) {
ByteBuf valueState = encodeMapValue(value);
params.add(valueState);
}
String setName = getValuesName(keyHash);
return commandExecutor.evalWriteAsync(getRawName(), codec, RedisCommands.EVAL_SET,
"redis.call('hset', KEYS[1], ARGV[1], ARGV[2]); " +
"local members = redis.call('smembers', KEYS[2]); " +
"redis.call('del', KEYS[2]); " +
"if #ARGV > 2 then " +
"for i=3, #ARGV, 5000 do "
+ "redis.call('sadd', KEYS[2], unpack(ARGV, i, math.min(i+4999, table.getn(ARGV)))) "
+ "end; " +
"end; " +
"return members; ",
Arrays.asList(getRawName(), setName), params.toArray());
}
}