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.
/*
* Copyright 2016-2018 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
*
* 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.springframework.data.redis.connection;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.reactivestreams.Publisher;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.redis.connection.ReactiveRedisConnection.BooleanResponse;
import org.springframework.data.redis.connection.ReactiveRedisConnection.Command;
import org.springframework.data.redis.connection.ReactiveRedisConnection.CommandResponse;
import org.springframework.data.redis.connection.ReactiveRedisConnection.KeyCommand;
import org.springframework.data.redis.connection.ReactiveRedisConnection.KeyScanCommand;
import org.springframework.data.redis.connection.ReactiveRedisConnection.MultiValueResponse;
import org.springframework.data.redis.connection.ReactiveRedisConnection.NumericResponse;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* Redis Hash commands executed using reactive infrastructure.
*
* @author Christoph Strobl
* @author Mark Paluch
* @since 2.0
*/
public interface ReactiveHashCommands {
/**
* {@literal HSET} {@link Command}.
*
* @author Christoph Strobl
* @see Redis Documentation: HSET
*/
class HSetCommand extends KeyCommand {
private static final ByteBuffer SINGLE_VALUE_KEY = ByteBuffer.allocate(0);
private final Map fieldValueMap;
private final boolean upsert;
private HSetCommand(@Nullable ByteBuffer key, Map keyValueMap, boolean upsert) {
super(key);
this.fieldValueMap = keyValueMap;
this.upsert = upsert;
}
/**
* Creates a new {@link HSetCommand} given a {@link ByteBuffer key}.
*
* @param value must not be {@literal null}.
* @return a new {@link HSetCommand} for {@link ByteBuffer key}.
*/
public static HSetCommand value(ByteBuffer value) {
Assert.notNull(value, "Value must not be null!");
return new HSetCommand(null, Collections.singletonMap(SINGLE_VALUE_KEY, value), Boolean.TRUE);
}
/**
* Creates a new {@link HSetCommand} given a {@link Map} of field values.
*
* @param fieldValueMap must not be {@literal null}.
* @return a new {@link HSetCommand} for a {@link Map} of field values.
*/
public static HSetCommand fieldValues(Map fieldValueMap) {
Assert.notNull(fieldValueMap, "Field values map must not be null!");
return new HSetCommand(null, fieldValueMap, Boolean.TRUE);
}
/**
* Applies a field. Constructs a new command instance with all previously configured properties.
*
* @param field must not be {@literal null}.
* @return a new {@link HSetCommand} with {@literal field} applied.
*/
public HSetCommand ofField(ByteBuffer field) {
if (!fieldValueMap.containsKey(SINGLE_VALUE_KEY)) {
throw new InvalidDataAccessApiUsageException("Value has not been set.");
}
Assert.notNull(field, "Field not be null!");
return new HSetCommand(getKey(), Collections.singletonMap(field, fieldValueMap.get(SINGLE_VALUE_KEY)), upsert);
}
/**
* Applies the {@literal key}. Constructs a new command instance with all previously configured properties.
*
* @param key must not be {@literal null}.
* @return a new {@link HSetCommand} with {@literal key} applied.
*/
public HSetCommand forKey(ByteBuffer key) {
Assert.notNull(key, "Key not be null!");
return new HSetCommand(key, fieldValueMap, upsert);
}
/**
* Disable upsert. Constructs a new command instance with all previously configured properties.
*
* @return a new {@link HSetCommand} with upsert disabled.
*/
public HSetCommand ifValueNotExists() {
return new HSetCommand(getKey(), fieldValueMap, Boolean.FALSE);
}
/**
* @return
*/
public boolean isUpsert() {
return upsert;
}
/**
* @return never {@literal null}.
*/
public Map getFieldValueMap() {
return fieldValueMap;
}
}
/**
* Set the {@literal value} of a hash {@literal field}.
*
* @param key must not be {@literal null}.
* @param field must not be {@literal null}.
* @param value must not be {@literal null}.
* @return
* @see Redis Documentation: HSET
*/
default Mono hSet(ByteBuffer key, ByteBuffer field, ByteBuffer value) {
Assert.notNull(key, "Key must not be null!");
Assert.notNull(field, "Field must not be null!");
Assert.notNull(value, "Value must not be null!");
return hSet(Mono.just(HSetCommand.value(value).ofField(field).forKey(key))).next().map(BooleanResponse::getOutput);
}
/**
* Set the {@literal value} of a hash {@literal field}.
*
* @param key must not be {@literal null}.
* @param field must not be {@literal null}.
* @param value must not be {@literal null}.
* @return
* @see Redis Documentation: HSETNX
*/
default Mono hSetNX(ByteBuffer key, ByteBuffer field, ByteBuffer value) {
Assert.notNull(key, "Key must not be null!");
Assert.notNull(field, "Field must not be null!");
Assert.notNull(value, "Value must not be null!");
return hSet(Mono.just(HSetCommand.value(value).ofField(field).forKey(key).ifValueNotExists())).next()
.map(BooleanResponse::getOutput);
}
/**
* Set multiple hash fields to multiple values using data provided in {@literal fieldValueMap}.
*
* @param key must not be {@literal null}.
* @param fieldValueMap must not be {@literal null}.
* @return
* @see Redis Documentation: HMSET
*/
default Mono hMSet(ByteBuffer key, Map fieldValueMap) {
Assert.notNull(key, "Key must not be null!");
Assert.notNull(fieldValueMap, "Field must not be null!");
return hSet(Mono.just(HSetCommand.fieldValues(fieldValueMap).forKey(key))).next().map(it -> true);
}
/**
* Set the {@literal value} of a hash {@literal field}.
*
* @param commands must not be {@literal null}.
* @return
* @see Redis Documentation: HSET
*/
Flux> hSet(Publisher commands);
/**
* {@literal HGET} {@link Command}.
*
* @author Christoph Strobl
* @see Redis Documentation: HGET
*/
class HGetCommand extends KeyCommand {
private List fields;
private HGetCommand(@Nullable ByteBuffer key, List fields) {
super(key);
this.fields = fields;
}
/**
* Creates a new {@link HGetCommand} given a {@link ByteBuffer field name}.
*
* @param field must not be {@literal null}.
* @return a new {@link HGetCommand} for a {@link ByteBuffer field name}.
*/
public static HGetCommand field(ByteBuffer field) {
Assert.notNull(field, "Field must not be null!");
return new HGetCommand(null, Collections.singletonList(field));
}
/**
* Creates a new {@link HGetCommand} given a {@link Collection} of field names.
*
* @param fields must not be {@literal null}.
* @return a new {@link HGetCommand} for a {@link Collection} of field names.
*/
public static HGetCommand fields(Collection fields) {
Assert.notNull(fields, "Fields must not be null!");
return new HGetCommand(null, new ArrayList<>(fields));
}
/**
* Applies the hash {@literal key}. Constructs a new command instance with all previously configured properties.
*
* @param key must not be {@literal null}.
* @return a new {@link HGetCommand} with {@literal key} applied.
*/
public HGetCommand from(ByteBuffer key) {
Assert.notNull(key, "Key must not be null!");
return new HGetCommand(key, fields);
}
/**
* @return never {@literal null}.
*/
public List getFields() {
return fields;
}
}
/**
* Get value for given {@literal field} from hash at {@literal key}.
*
* @param key must not be {@literal null}.
* @param field must not be {@literal null}.
* @return
* @see Redis Documentation: HGET
*/
default Mono hGet(ByteBuffer key, ByteBuffer field) {
return hMGet(key, Collections.singletonList(field)).map(val -> val.isEmpty() ? null : val.iterator().next());
}
/**
* Get values for given {@literal fields} from hash at {@literal key}.
*
* @param key must not be {@literal null}.
* @param fields must not be {@literal null}.
* @return
* @see Redis Documentation: HMGET
*/
default Mono> hMGet(ByteBuffer key, Collection fields) {
Assert.notNull(key, "Key must not be null!");
Assert.notNull(fields, "Fields must not be null!");
return hMGet(Mono.just(HGetCommand.fields(fields).from(key))).next().map(MultiValueResponse::getOutput);
}
/**
* Get values for given {@literal fields} from hash at {@literal key}.
*
* @param commands must not be {@literal null}.
* @return
* @see Redis Documentation: HMGET
*/
Flux> hMGet(Publisher commands);
/**
* {@literal HEXISTS} {@link Command}.
*
* @author Christoph Strobl
* @see Redis Documentation: HEXISTS
*/
class HExistsCommand extends KeyCommand {
private final ByteBuffer field;
private HExistsCommand(@Nullable ByteBuffer key, ByteBuffer field) {
super(key);
this.field = field;
}
/**
* Creates a new {@link HExistsCommand} given a {@link ByteBuffer field name}.
*
* @param field must not be {@literal null}.
* @return a new {@link HExistsCommand} for a {@link ByteBuffer field name}.
*/
public static HExistsCommand field(ByteBuffer field) {
Assert.notNull(field, "Field must not be null!");
return new HExistsCommand(null, field);
}
/**
* Applies the hash {@literal key}. Constructs a new command instance with all previously configured properties.
*
* @param key must not be {@literal null}.
* @return a new {@link HExistsCommand} with {@literal key} applied.
*/
public HExistsCommand in(ByteBuffer key) {
Assert.notNull(key, "Key must not be null!");
return new HExistsCommand(key, field);
}
/**
* @return never {@literal null}.
*/
public ByteBuffer getField() {
return field;
}
}
/**
* Determine if given hash {@literal field} exists.
*
* @param key must not be {@literal null}.
* @param field must not be {@literal null}.
* @return
* @see Redis Documentation: HEXISTS
*/
default Mono hExists(ByteBuffer key, ByteBuffer field) {
Assert.notNull(key, "Key must not be null!");
Assert.notNull(field, "Field must not be null!");
return hExists(Mono.just(HExistsCommand.field(field).in(key))).next().map(BooleanResponse::getOutput);
}
/**
* Determine if given hash {@literal field} exists.
*
* @param commands
* @return
* @see Redis Documentation: HEXISTS
*/
Flux> hExists(Publisher commands);
/**
* @author Christoph Strobl
* @see Redis Documentation: HDEL
*/
class HDelCommand extends KeyCommand {
private final List fields;
private HDelCommand(@Nullable ByteBuffer key, List fields) {
super(key);
this.fields = fields;
}
/**
* Creates a new {@link HDelCommand} given a {@link ByteBuffer field name}.
*
* @param field must not be {@literal null}.
* @return a new {@link HDelCommand} for a {@link ByteBuffer field name}.
*/
public static HDelCommand field(ByteBuffer field) {
Assert.notNull(field, "Field must not be null!");
return new HDelCommand(null, Collections.singletonList(field));
}
/**
* Creates a new {@link HDelCommand} given a {@link Collection} of field names.
*
* @param fields must not be {@literal null}.
* @return a new {@link HDelCommand} for a {@link Collection} of field names.
*/
public static HDelCommand fields(Collection fields) {
Assert.notNull(fields, "Fields must not be null!");
return new HDelCommand(null, new ArrayList<>(fields));
}
/**
* Applies the hash {@literal key}. Constructs a new command instance with all previously configured properties.
*
* @param key must not be {@literal null}.
* @return a new {@link HDelCommand} with {@literal key} applied.
*/
public HDelCommand from(ByteBuffer key) {
Assert.notNull(key, "Key must not be null!");
return new HDelCommand(key, fields);
}
/**
* @return never {@literal null}.
*/
public List getFields() {
return fields;
}
}
/**
* Delete given hash {@literal field}.
*
* @param key must not be {@literal null}.
* @param field must not be {@literal null}.
* @return
* @see Redis Documentation: HDEL
*/
default Mono hDel(ByteBuffer key, ByteBuffer field) {
Assert.notNull(field, "Field must not be null!");
return hDel(key, Collections.singletonList(field)).map(val -> val > 0 ? Boolean.TRUE : Boolean.FALSE);
}
/**
* Delete given hash {@literal fields}.
*
* @param key must not be {@literal null}.
* @param fields must not be {@literal null}.
* @return
* @see Redis Documentation: HDEL
*/
default Mono hDel(ByteBuffer key, Collection fields) {
Assert.notNull(key, "Key must not be null!");
Assert.notNull(fields, "Fields must not be null!");
return hDel(Mono.just(HDelCommand.fields(fields).from(key))).next().map(NumericResponse::getOutput);
}
/**
* Delete given hash {@literal fields}.
*
* @param commands must not be {@literal null}.
* @return
* @see Redis Documentation: HDEL
*/
Flux> hDel(Publisher commands);
/**
* Get size of hash at {@literal key}.
*
* @param key must not be {@literal null}.
* @return
* @see Redis Documentation: HLEN
*/
default Mono hLen(ByteBuffer key) {
Assert.notNull(key, "Key must not be null!");
return hLen(Mono.just(new KeyCommand(key))).next().map(NumericResponse::getOutput);
}
/**
* Get size of hash at {@literal key}.
*
* @param commands must not be {@literal null}.
* @return
* @see Redis Documentation: HLEN
*/
Flux> hLen(Publisher commands);
/**
* Get key set (fields) of hash at {@literal key}.
*
* @param key must not be {@literal null}.
* @return
* @see Redis Documentation: HKEYS
*/
default Flux hKeys(ByteBuffer key) {
Assert.notNull(key, "Key must not be null!");
return hKeys(Mono.just(new KeyCommand(key))).flatMap(CommandResponse::getOutput);
}
/**
* Get key set (fields) of hash at {@literal key}.
*
* @param commands must not be {@literal null}.
* @return
* @see Redis Documentation: HKEYS
*/
Flux>> hKeys(Publisher commands);
/**
* Get entry set (values) of hash at {@literal key}.
*
* @param key must not be {@literal null}.
* @return
* @see Redis Documentation: HVALS
*/
default Flux hVals(ByteBuffer key) {
Assert.notNull(key, "Key must not be null!");
return hVals(Mono.just(new KeyCommand(key))).flatMap(CommandResponse::getOutput);
}
/**
* Get entry set (values) of hash at {@literal key}.
*
* @param commands must not be {@literal null}.
* @return
* @see Redis Documentation: HVALS
*/
Flux>> hVals(Publisher commands);
/**
* Get entire hash stored at {@literal key}.
*
* @param key must not be {@literal null}.
* @return
* @see Redis Documentation: HGETALL
*/
default Flux> hGetAll(ByteBuffer key) {
Assert.notNull(key, "Key must not be null!");
return hGetAll(Mono.just(new KeyCommand(key))).flatMap(CommandResponse::getOutput);
}
/**
* Get entire hash stored at {@literal key}.
*
* @param commands must not be {@literal null}.
* @return
* @see Redis Documentation: HGETALL
*/
Flux>>> hGetAll(Publisher commands);
/**
* Use a {@link Flux} to iterate over entries in the hash at {@code key}. The resulting {@link Flux} acts as a cursor
* and issues {@code HSCAN} commands itself as long as the subscriber signals demand.
*
* @param key must not be {@literal null}.
* @return the {@link Flux} emitting {@link java.util.Map.Entry entries} one by one.
* @throws IllegalArgumentException in case the given key is {@literal null}.
* @see Redis Documentation: HSCAN
* @since 2.1
*/
default Flux> hScan(ByteBuffer key) {
return hScan(key, ScanOptions.NONE);
}
/**
* Use a {@link Flux} to iterate over entries in the hash at {@code key} given {@link ScanOptions}. The resulting
* {@link Flux} acts as a cursor and issues {@code HSCAN} commands itself as long as the subscriber signals demand.
*
* @param key must not be {@literal null}.
* @param options must not be {@literal null}. Use {@link ScanOptions#NONE} instead.
* @return the {@link Flux} emitting the raw {@link java.util.Map.Entry entries} one by one.
* @throws IllegalArgumentException in case one of the required arguments is {@literal null}.
* @see Redis Documentation: HSCAN
* @since 2.1
*/
default Flux> hScan(ByteBuffer key, ScanOptions options) {
return hScan(Mono.just(KeyScanCommand.key(key).withOptions(options))).map(CommandResponse::getOutput)
.flatMap(it -> it);
}
/**
* Use a {@link Flux} to iterate over entries in the hash at {@code key}. The resulting {@link Flux} acts as a cursor
* and issues {@code HSCAN} commands itself as long as the subscriber signals demand.
*
* @param commands must not be {@literal null}.
* @return the {@link Flux} emitting {@link CommandResponse} one by one.
* @see Redis Documentation: HSCAN
* @since 2.1
*/
Flux>>> hScan(Publisher commands);
/**
* @author Christoph Strobl
* @see Redis Documentation: HSTRLEN
* @since 2.1
*/
class HStrLenCommand extends KeyCommand {
private ByteBuffer field;
/**
* Creates a new {@link HStrLenCommand} given a {@code key}.
*
* @param key can be {@literal null}.
* @param field must not be {@literal null}.
*/
private HStrLenCommand(@Nullable ByteBuffer key, ByteBuffer field) {
super(key);
this.field = field;
}
/**
* Specify the {@code field} within the hash to get the length of the {@code value} of.ø
*
* @param field must not be {@literal null}.
* @return new instance of {@link HStrLenCommand}.
*/
public static HStrLenCommand lengthOf(ByteBuffer field) {
Assert.notNull(field, "Field must not be null!");
return new HStrLenCommand(null, field);
}
/**
* Define the {@code key} the hash is stored at.
*
* @param key must not be {@literal null}.
* @return new instance of {@link HStrLenCommand}.
*/
public HStrLenCommand from(ByteBuffer key) {
return new HStrLenCommand(key, field);
}
/**
* @return the field.
*/
public ByteBuffer getField() {
return field;
}
}
/**
* Get the length of the value associated with {@code field}. If either the {@code key} or the {@code field} do not
* exist, {@code 0} is emitted.
*
* @param key must not be {@literal null}.
* @param field must not be {@literal null}.
* @return never {@literal null}.
* @since 2.1
*/
default Mono hStrLen(ByteBuffer key, ByteBuffer field) {
Assert.notNull(key, "Key must not be null!");
Assert.notNull(field, "Field must not be null!");
return hStrLen(Mono.just(HStrLenCommand.lengthOf(field).from(key))).next().map(NumericResponse::getOutput);
}
/**
* Get the length of the value associated with {@code field}. If either the {@code key} or the {@code field} do not
* exist, {@code 0} is emitted.
*
* @param commands must not be {@literal null}.
* @return never {@literal null}.
* @since 2.1
*/
Flux> hStrLen(Publisher commands);
}