org.redisson.RedissonMultimap Maven / Gradle / Ivy
/**
* Copyright 2018 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 java.util.AbstractCollection;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.redisson.api.RFuture;
import org.redisson.api.RLock;
import org.redisson.api.RMultimap;
import org.redisson.api.RReadWriteLock;
import org.redisson.client.RedisClient;
import org.redisson.client.codec.Codec;
import org.redisson.client.codec.LongCodec;
import org.redisson.client.codec.StringCodec;
import org.redisson.client.protocol.RedisCommand;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.client.protocol.decoder.MapScanResult;
import org.redisson.codec.CompositeCodec;
import org.redisson.command.CommandAsyncExecutor;
import org.redisson.command.CommandExecutor;
import org.redisson.misc.Hash;
import org.redisson.misc.RedissonPromise;
import io.netty.buffer.ByteBuf;
/**
* @author Nikita Koksharov
*
* @param key
* @param value
*/
public abstract class RedissonMultimap extends RedissonExpirable implements RMultimap {
final String prefix;
RedissonMultimap(CommandAsyncExecutor commandAsyncExecutor, String name) {
super(commandAsyncExecutor, name);
prefix = suffixName(getName(), "");
}
RedissonMultimap(Codec codec, CommandAsyncExecutor commandAsyncExecutor, String name) {
super(codec, commandAsyncExecutor, name);
prefix = suffixName(getName(), "");
}
@Override
public RLock getLock(K key) {
String lockName = getLockName(key);
return new RedissonLock((CommandExecutor)commandExecutor, lockName);
}
@Override
public RReadWriteLock getReadWriteLock(K key) {
String lockName = getLockName(key);
return new RedissonReadWriteLock((CommandExecutor)commandExecutor, lockName);
}
private String getLockName(Object key) {
ByteBuf keyState = encodeMapKey(key);
try {
return suffixName(getName(), Hash.hash128toBase64(keyState) + ":key");
} finally {
keyState.release();
}
}
protected String hash(ByteBuf objectState) {
return Hash.hash128toBase64(objectState);
}
protected String hashAndRelease(ByteBuf objectState) {
try {
return Hash.hash128toBase64(objectState);
} finally {
objectState.release();
}
}
@Override
public int size() {
return get(sizeAsync());
}
@Override
public int keySize() {
return get(keySizeAsync());
}
@Override
public boolean isEmpty() {
return size() == 0;
}
@Override
public boolean containsKey(Object key) {
return get(containsKeyAsync(key));
}
@Override
public boolean containsValue(Object value) {
return get(containsValueAsync(value));
}
@Override
public boolean containsEntry(Object key, Object value) {
return get(containsEntryAsync(key, value));
}
@Override
public boolean put(K key, V value) {
return get(putAsync(key, value));
}
String getValuesName(String hash) {
return suffixName(getName(), hash);
}
@Override
public boolean remove(Object key, Object value) {
return get(removeAsync(key, value));
}
@Override
public boolean putAll(K key, Iterable extends V> values) {
return get(putAllAsync(key, values));
}
@Override
public void clear() {
delete();
}
@Override
public Set keySet() {
return new KeySet();
}
@Override
public Collection values() {
return new Values();
}
@Override
public Collection getAll(K key) {
return get(getAllAsync(key));
}
@Override
public Collection removeAll(Object key) {
return get(removeAllAsync(key));
}
@Override
public Collection replaceValues(K key, Iterable extends V> values) {
return get(replaceValuesAsync(key, values));
}
@Override
public Collection> entries() {
return new EntrySet();
}
@Override
public Set readAllKeySet() {
return get(readAllKeySetAsync());
}
@Override
public RFuture> readAllKeySetAsync() {
return commandExecutor.readAsync(getName(), codec, RedisCommands.HKEYS, getName());
}
@Override
public long fastRemove(K ... keys) {
return get(fastRemoveAsync(keys));
}
@Override
public RFuture fastRemoveAsync(K ... keys) {
if (keys == null || keys.length == 0) {
return RedissonPromise.newSucceededFuture(0L);
}
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy