All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.data.redis.connection.ReactiveNumberCommands Maven / Gradle / Ivy

There is a newer version: 3.2.5
Show newest version
/*
 * 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 org.reactivestreams.Publisher;
import org.springframework.data.redis.connection.ReactiveRedisConnection.KeyCommand;
import org.springframework.data.redis.connection.ReactiveRedisConnection.NumericResponse;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

/**
 * Redis numeric commands executed using reactive infrastructure.
 *
 * @author Christoph Strobl
 * @author Mark Paluch
 * @since 2.0
 */
public interface ReactiveNumberCommands {

	/**
	 * Increment value of {@literal key} by 1.
	 *
	 * @param key must not be {@literal null}.
	 * @return
	 * @see Redis Documentation: INCR
	 */
	default Mono incr(ByteBuffer key) {

		Assert.notNull(key, "Key must not be null!");

		return incr(Mono.just(new KeyCommand(key))).next().map(NumericResponse::getOutput);
	}

	/**
	 * Increment value of {@literal key} by 1.
	 *
	 * @param keys must not be {@literal null}.
	 * @return
	 * @see Redis Documentation: INCR
	 */
	Flux> incr(Publisher keys);

	/**
	 * {@code INCRBY} command parameters.
	 *
	 * @author Christoph Strobl
	 * @see Redis Documentation: INCRBY
	 */
	class IncrByCommand extends KeyCommand {

		private @Nullable T value;

		private IncrByCommand(ByteBuffer key, @Nullable T value) {

			super(key);
			this.value = value;
		}

		/**
		 * Creates a new {@link IncrByCommand} given a {@link ByteBuffer key}.
		 *
		 * @param key must not be {@literal null}.
		 * @return a new {@link IncrByCommand} for {@link ByteBuffer key}.
		 */
		public static  IncrByCommand incr(ByteBuffer key) {

			Assert.notNull(key, "Key must not be null!");

			return new IncrByCommand<>(key, null);
		}

		/**
		 * Applies the numeric {@literal value}. Constructs a new command instance with all previously configured
		 * properties.
		 *
		 * @param value must not be {@literal null}.
		 * @return a new {@link IncrByCommand} with {@literal value} applied.
		 */
		public IncrByCommand by(T value) {

			Assert.notNull(value, "Value must not be null!");

			return new IncrByCommand<>(getKey(), value);
		}

		/**
		 * @return can be {@literal null}.
		 */
		@Nullable
		public T getValue() {
			return value;
		}
	}

	/**
	 * Increment value of {@literal key} by {@literal value}.
	 *
	 * @param key must not be {@literal null}.
	 * @param value must not be {@literal null}.
	 * @return
	 * @see Redis Documentation: INCRBY
	 * @see Redis Documentation: INCRBYFLOAT
	 */
	default  Mono incrBy(ByteBuffer key, T value) {

		Assert.notNull(key, "Key must not be null!");
		Assert.notNull(value, "Value must not be null!");

		return incrBy(Mono.just(IncrByCommand. incr(key).by(value))).next().map(NumericResponse::getOutput);
	}

	/**
	 * Increment value of {@literal key} by {@literal value}.
	 *
	 * @param commands must not be {@literal null}.
	 * @return
	 * @see Redis Documentation: INCRBY
	 * @see Redis Documentation: INCRBYFLOAT
	 */
	 Flux, T>> incrBy(
			Publisher> commands);

	/**
	 * {@code DECRBY} command parameters.
	 *
	 * @author Christoph Strobl
	 * @see Redis Documentation: DECRBY
	 */
	class DecrByCommand extends KeyCommand {

		private @Nullable T value;

		private DecrByCommand(ByteBuffer key, @Nullable T value) {
			super(key);
			this.value = value;
		}

		/**
		 * Creates a new {@link DecrByCommand} given a {@link ByteBuffer key}.
		 *
		 * @param key must not be {@literal null}.
		 * @return a new {@link DecrByCommand} for {@link ByteBuffer key}.
		 */
		public static  DecrByCommand decr(ByteBuffer key) {

			Assert.notNull(key, "Key must not be null!");

			return new DecrByCommand<>(key, null);
		}

		/**
		 * Applies the numeric {@literal value}. Constructs a new command instance with all previously configured
		 * properties.
		 *
		 * @param value must not be {@literal null}.
		 * @return a new {@link DecrByCommand} with {@literal value} applied.
		 */
		public DecrByCommand by(T value) {

			Assert.notNull(value, "Value must not be null!");

			return new DecrByCommand<>(getKey(), value);
		}

		/**
		 * @return can be {@literal null}.
		 */
		@Nullable
		public T getValue() {
			return value;
		}
	}

	/**
	 * Decrement value of {@literal key} by 1.
	 *
	 * @param key must not be {@literal null}.
	 * @return
	 * @see Redis Documentation: DECR
	 */
	default Mono decr(ByteBuffer key) {

		Assert.notNull(key, "Key must not be null!");

		return decr(Mono.just(new KeyCommand(key))).next().map(NumericResponse::getOutput);
	}

	/**
	 * Decrement value of {@literal key} by 1.
	 *
	 * @param keys must not be {@literal null}.
	 * @return
	 * @see Redis Documentation: DECR
	 */
	Flux> decr(Publisher keys);

	/**
	 * Decrement value of {@literal key} by {@literal value}.
	 *
	 * @param key must not be {@literal null}.
	 * @param value must not be {@literal null}.
	 * @return
	 * @see Redis Documentation: DECRBY
	 */
	default  Mono decrBy(ByteBuffer key, T value) {

		Assert.notNull(key, "Key must not be null!");
		Assert.notNull(value, "Value must not be null!");

		return decrBy(Mono.just(DecrByCommand. decr(key).by(value))).next().map(NumericResponse::getOutput);
	}

	/**
	 * Decrement value of {@literal key} by {@literal value}.
	 *
	 * @param commands must not be {@literal null}.
	 * @return
	 */
	 Flux, T>> decrBy(Publisher> commands);

	/**
	 * {@code HINCRBY} command parameters.
	 *
	 * @author Christoph Strobl
	 * @see Redis Documentation: HINCRBY
	 */
	class HIncrByCommand extends KeyCommand {

		private final ByteBuffer field;
		private final @Nullable T value;

		private HIncrByCommand(@Nullable ByteBuffer key, ByteBuffer field, @Nullable T value) {

			super(key);

			this.field = field;
			this.value = value;
		}

		/**
		 * Creates a new {@link HIncrByCommand} given a {@link ByteBuffer key}.
		 *
		 * @param field must not be {@literal null}.
		 * @return a new {@link HIncrByCommand} for {@link ByteBuffer key}.
		 */
		public static  HIncrByCommand incr(ByteBuffer field) {

			Assert.notNull(field, "Field must not be null!");

			return new HIncrByCommand<>(null, field, null);
		}

		/**
		 * Applies the numeric {@literal value}. Constructs a new command instance with all previously configured
		 * properties.
		 *
		 * @param value must not be {@literal null}.
		 * @return a new {@link HIncrByCommand} with {@literal value} applied.
		 */
		public HIncrByCommand by(T value) {

			Assert.notNull(value, "Value must not be null!");

			return new HIncrByCommand<>(getKey(), field, value);
		}

		/**
		 * 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 HIncrByCommand} with {@literal key} applied.
		 */
		public HIncrByCommand forKey(ByteBuffer key) {

			Assert.notNull(key, "Key must not be null!");

			return new HIncrByCommand<>(key, field, value);
		}

		/**
		 * @return can be {@literal null}.
		 */
		@Nullable
		public T getValue() {
			return value;
		}

		/**
		 * @return never {@literal null}.
		 */
		public ByteBuffer getField() {
			return field;
		}
	}

	/**
	 * Increment {@literal value} of a hash {@literal field} by the given {@literal value}.
	 *
	 * @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: HINCRBY
	 */
	default  Mono hIncrBy(ByteBuffer key, ByteBuffer field, T 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 hIncrBy(Mono.just(HIncrByCommand. incr(field).by(value).forKey(key))).next()
				.map(NumericResponse::getOutput);
	}

	/**
	 * Increment {@literal value} of a hash {@literal field} by the given {@literal value}.
	 *
	 * @return
	 * @see Redis Documentation: HINCRBY
	 */
	 Flux, T>> hIncrBy(Publisher> commands);

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy