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-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.connection.lettuce;
import io.lettuce.core.KeyValue;
import io.lettuce.core.ScanStream;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import org.reactivestreams.Publisher;
import org.springframework.data.redis.connection.ReactiveHashCommands;
import org.springframework.data.redis.connection.ReactiveRedisConnection.BooleanResponse;
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.connection.convert.Converters;
import org.springframework.util.Assert;
/**
* @author Christoph Strobl
* @author Mark Paluch
* @since 2.0
*/
class LettuceReactiveHashCommands implements ReactiveHashCommands {
private final LettuceReactiveRedisConnection connection;
/**
* Create new {@link LettuceReactiveHashCommands}.
*
* @param connection must not be {@literal null}.
*/
LettuceReactiveHashCommands(LettuceReactiveRedisConnection connection) {
Assert.notNull(connection, "Connection must not be null!");
this.connection = connection;
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveHashCommands#hSet(org.reactivestreams.Publisher)
*/
@Override
public Flux> hSet(Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
Assert.notNull(command.getFieldValueMap(), "FieldValueMap must not be null!");
Mono result;
if (command.getFieldValueMap().size() == 1) {
Entry entry = command.getFieldValueMap().entrySet().iterator().next();
result = command.isUpsert() ? cmd.hset(command.getKey(), entry.getKey(), entry.getValue())
: cmd.hsetnx(command.getKey(), entry.getKey(), entry.getValue());
} else {
Map entries = command.getFieldValueMap();
result = cmd.hmset(command.getKey(), entries).map(LettuceConverters::stringToBoolean);
}
return result.map(value -> new BooleanResponse<>(command, value));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveHashCommands#hMGet(org.reactivestreams.Publisher)
*/
@Override
public Flux> hMGet(Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
Assert.notNull(command.getFields(), "Fields must not be null!");
Mono>> result;
if (command.getFields().size() == 1) {
ByteBuffer key = command.getFields().iterator().next();
result = cmd.hget(command.getKey(), key.duplicate()).map(value -> KeyValue.fromNullable(key, value))
.defaultIfEmpty(KeyValue.empty(key)).map(Collections::singletonList);
} else {
result = cmd.hmget(command.getKey(), command.getFields().stream().toArray(ByteBuffer[]::new)).collectList();
}
return result.map(value -> new MultiValueResponse<>(command,
value.stream().map(keyValue -> keyValue.getValueOrElse(null)).collect(Collectors.toList())));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveHashCommands#hExists(org.reactivestreams.Publisher)
*/
@Override
public Flux> hExists(Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
Assert.notNull(command.getName(), "Name must not be null!");
return cmd.hexists(command.getKey(), command.getField()).map(value -> new BooleanResponse<>(command, value));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveHashCommands#hDel(org.reactivestreams.Publisher)
*/
@Override
public Flux> hDel(Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
Assert.notNull(command.getFields(), "Fields must not be null!");
return cmd.hdel(command.getKey(), command.getFields().stream().toArray(ByteBuffer[]::new))
.map(value -> new NumericResponse<>(command, value));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveHashCommands#hLen(org.reactivestreams.Publisher)
*/
@Override
public Flux> hLen(Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Command.getKey() must not be null!");
return cmd.hlen(command.getKey()).map(value -> new NumericResponse<>(command, value));
}));
}
@Override
public Flux>> hRandField(Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).map(command -> {
Assert.notNull(command.getKey(), "Command.getKey() must not be null!");
return new CommandResponse<>(command, cmd.hrandfield(command.getKey(), command.getCount()));
}));
}
@Override
public Flux>>> hRandFieldWithValues(
Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).map(command -> {
Assert.notNull(command.getKey(), "Command.getKey() must not be null!");
Flux> flux = cmd.hrandfieldWithvalues(command.getKey(), command.getCount())
.handle((it, sink) -> {
if (it.isEmpty()) {
return;
}
sink.next(Converters.entryOf(it.getKey(), it.getValue()));
});
return new CommandResponse<>(command, flux);
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveHashCommands#hKeys(org.reactivestreams.Publisher)
*/
@Override
public Flux>> hKeys(Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
Flux result = cmd.hkeys(command.getKey());
return Mono.just(new CommandResponse<>(command, result));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveHashCommands#hKeys(org.reactivestreams.Publisher)
*/
@Override
public Flux>> hVals(Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
Flux result = cmd.hvals(command.getKey());
return Mono.just(new CommandResponse<>(command, result));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveHashCommands#hGetAll(org.reactivestreams.Publisher)
*/
@Override
public Flux>>> hGetAll(
Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
Flux> result = cmd.hgetall(command.getKey());
return Mono.just(new CommandResponse<>(command, result.map(LettuceReactiveHashCommands::toEntry)));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveHashCommands#hScan(org.reactivestreams.Publisher)
*/
@Override
public Flux>>> hScan(
Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
Assert.notNull(command.getOptions(), "ScanOptions must not be null!");
Flux> result = ScanStream.hscan(cmd, command.getKey(),
LettuceConverters.toScanArgs(command.getOptions()));
Flux> entryFlux = result.map(it -> new Entry() {
@Override
public ByteBuffer getKey() {
return it.getKey();
}
@Override
public ByteBuffer getValue() {
return it.getValue();
}
@Override
public ByteBuffer setValue(ByteBuffer value) {
throw new UnsupportedOperationException("Cannot set value for entry in cursor.");
}
});
return Mono.just(new CommandResponse<>(command, entryFlux));
}));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.ReactiveHashCommands#hstrlen(org.reactivestreams.Publisher)
*/
@Override
public Flux> hStrLen(Publisher commands) {
return connection.execute(cmd -> Flux.from(commands).concatMap(command -> {
Assert.notNull(command.getKey(), "Key must not be null!");
Assert.notNull(command.getField(), "Field must not be null!");
return cmd.hstrlen(command.getKey(), command.getField()).map(value -> new NumericResponse<>(command, value));
}));
}
private static Map.Entry toEntry(KeyValue kv) {
return new Entry() {
@Override
public ByteBuffer getKey() {
return kv.getKey();
}
@Override
public ByteBuffer getValue() {
return kv.getValue();
}
@Override
public ByteBuffer setValue(ByteBuffer value) {
throw new UnsupportedOperationException("Cannot set value for entry");
}
};
}
}