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.springframework.data.redis.core.DefaultHashOperations Maven / Gradle / Ivy
/*
* Copyright 2011-2022 the original author or authors.
*
* 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
*
* https://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.springframework.data.redis.core;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.redis.connection.convert.Converters;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* Default implementation of {@link HashOperations}.
*
* @author Costin Leau
* @author Christoph Strobl
* @author Ninad Divadkar
*/
class DefaultHashOperations extends AbstractOperations implements HashOperations {
@SuppressWarnings("unchecked")
DefaultHashOperations(RedisTemplate template) {
super((RedisTemplate) template);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.HashOperations#get(java.lang.Object, java.lang.Object)
*/
@Override
@SuppressWarnings("unchecked")
public HV get(K key, Object hashKey) {
byte[] rawKey = rawKey(key);
byte[] rawHashKey = rawHashKey(hashKey);
byte[] rawHashValue = execute(connection -> connection.hGet(rawKey, rawHashKey));
return (HV) rawHashValue != null ? deserializeHashValue(rawHashValue) : null;
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.HashOperations#hasKey(java.lang.Object, java.lang.Object)
*/
@Override
public Boolean hasKey(K key, Object hashKey) {
byte[] rawKey = rawKey(key);
byte[] rawHashKey = rawHashKey(hashKey);
return execute(connection -> connection.hExists(rawKey, rawHashKey));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.HashOperations#increment(java.lang.Object, java.lang.Object, long)
*/
@Override
public Long increment(K key, HK hashKey, long delta) {
byte[] rawKey = rawKey(key);
byte[] rawHashKey = rawHashKey(hashKey);
return execute(connection -> connection.hIncrBy(rawKey, rawHashKey, delta));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.HashOperations#increment(java.lang.Object, java.lang.Object, double)
*/
@Override
public Double increment(K key, HK hashKey, double delta) {
byte[] rawKey = rawKey(key);
byte[] rawHashKey = rawHashKey(hashKey);
return execute(connection -> connection.hIncrBy(rawKey, rawHashKey, delta));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.HashOperations#randomField(java.lang.Object)
*/
@Nullable
@Override
public HK randomKey(K key) {
byte[] rawKey = rawKey(key);
return deserializeHashKey(execute(connection -> connection.hRandField(rawKey)));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.HashOperations#randomValue(java.lang.Object)
*/
@Nullable
@Override
public Entry randomEntry(K key) {
byte[] rawKey = rawKey(key);
Entry rawEntry = execute(connection -> connection.hRandFieldWithValues(rawKey));
return rawEntry == null ? null
: Converters.entryOf(deserializeHashKey(rawEntry.getKey()), deserializeHashValue(rawEntry.getValue()));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.HashOperations#randomFields(java.lang.Object, long)
*/
@Nullable
@Override
public List randomKeys(K key, long count) {
byte[] rawKey = rawKey(key);
List rawValues = execute(connection -> connection.hRandField(rawKey, count));
return deserializeHashKeys(rawValues);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.HashOperations#randomValues(java.lang.Object, long)
*/
@Nullable
@Override
public Map randomEntries(K key, long count) {
Assert.isTrue(count > 0, "Count must not be negative");
byte[] rawKey = rawKey(key);
List> rawEntries = execute(connection -> connection.hRandFieldWithValues(rawKey, count));
if (rawEntries == null) {
return null;
}
Map rawMap = new LinkedHashMap<>(rawEntries.size());
rawEntries.forEach(entry -> rawMap.put(entry.getKey(), entry.getValue()));
return deserializeHashMap(rawMap);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.HashOperations#keys(java.lang.Object)
*/
@Override
public Set keys(K key) {
byte[] rawKey = rawKey(key);
Set rawValues = execute(connection -> connection.hKeys(rawKey));
return rawValues != null ? deserializeHashKeys(rawValues) : Collections.emptySet();
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.HashOperations#size(java.lang.Object)
*/
@Override
public Long size(K key) {
byte[] rawKey = rawKey(key);
return execute(connection -> connection.hLen(rawKey));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.HashOperations#lengthOfValue(java.lang.Object, java.lang.Object)
*/
@Nullable
@Override
public Long lengthOfValue(K key, HK hashKey) {
byte[] rawKey = rawKey(key);
byte[] rawHashKey = rawHashKey(hashKey);
return execute(connection -> connection.hStrLen(rawKey, rawHashKey));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.HashOperations#putAll(java.lang.Object, java.util.Map)
*/
@Override
public void putAll(K key, Map m) {
if (m.isEmpty()) {
return;
}
byte[] rawKey = rawKey(key);
Map hashes = new LinkedHashMap<>(m.size());
for (Map.Entry entry : m.entrySet()) {
hashes.put(rawHashKey(entry.getKey()), rawHashValue(entry.getValue()));
}
execute(connection -> {
connection.hMSet(rawKey, hashes);
return null;
});
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.HashOperations#multiGet(java.lang.Object, java.util.Collection)
*/
@Override
public List multiGet(K key, Collection fields) {
if (fields.isEmpty()) {
return Collections.emptyList();
}
byte[] rawKey = rawKey(key);
byte[][] rawHashKeys = new byte[fields.size()][];
int counter = 0;
for (HK hashKey : fields) {
rawHashKeys[counter++] = rawHashKey(hashKey);
}
List rawValues = execute(connection -> connection.hMGet(rawKey, rawHashKeys));
return deserializeHashValues(rawValues);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.HashOperations#put(java.lang.Object, java.lang.Object, java.lang.Object)
*/
@Override
public void put(K key, HK hashKey, HV value) {
byte[] rawKey = rawKey(key);
byte[] rawHashKey = rawHashKey(hashKey);
byte[] rawHashValue = rawHashValue(value);
execute(connection -> {
connection.hSet(rawKey, rawHashKey, rawHashValue);
return null;
});
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.HashOperations#putIfAbsent(java.lang.Object, java.lang.Object, java.lang.Object)
*/
@Override
public Boolean putIfAbsent(K key, HK hashKey, HV value) {
byte[] rawKey = rawKey(key);
byte[] rawHashKey = rawHashKey(hashKey);
byte[] rawHashValue = rawHashValue(value);
return execute(connection -> connection.hSetNX(rawKey, rawHashKey, rawHashValue));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.HashOperations#values(java.lang.Object)
*/
@Override
public List values(K key) {
byte[] rawKey = rawKey(key);
List rawValues = execute(connection -> connection.hVals(rawKey));
return rawValues != null ? deserializeHashValues(rawValues) : Collections.emptyList();
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.HashOperations#delete(java.lang.Object, java.lang.Object[])
*/
@Override
public Long delete(K key, Object... hashKeys) {
byte[] rawKey = rawKey(key);
byte[][] rawHashKeys = rawHashKeys(hashKeys);
return execute(connection -> connection.hDel(rawKey, rawHashKeys));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.HashOperations#entries(java.lang.Object)
*/
@Override
public Map entries(K key) {
byte[] rawKey = rawKey(key);
Map entries = execute(connection -> connection.hGetAll(rawKey));
return entries != null ? deserializeHashMap(entries) : Collections.emptyMap();
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.HashOperations#scan(java.lang.Object, org.springframework.data.redis.core.ScanOptions)
*/
@Override
public Cursor> scan(K key, ScanOptions options) {
byte[] rawKey = rawKey(key);
return template.executeWithStickyConnection(
(RedisCallback>>) connection -> new ConvertingCursor<>(connection.hScan(rawKey, options),
new Converter, Entry>() {
@Override
public Entry convert(final Entry source) {
return Converters.entryOf(deserializeHashKey(source.getKey()), deserializeHashValue(source.getValue()));
}
}));
}
}