Skip to content

DATAREDIS-469 - Throw DataRetrievalFailureException for atomic numbers when key is removed. #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.0.BUILD-SNAPSHOT</version>
<version>1.7.0.DATAREDIS-469-SNAPSHOT</version>

<name>Spring Data Redis</name>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2014 the original author or authors.
* Copyright 2013-2016 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.
Expand All @@ -20,6 +20,7 @@
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.BoundKeyOperations;
Expand All @@ -35,9 +36,11 @@
/**
* Atomic double backed by Redis. Uses Redis atomic increment/decrement and watch/multi/exec operations for CAS
* operations.
*
*
* @author Jennifer Hickey
* @author Thomas Darimont
* @author Christoph Strobl
* @author Mark Paluch
*/
public class RedisAtomicDouble extends Number implements Serializable, BoundKeyOperations<String> {

Expand All @@ -49,7 +52,7 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO

/**
* Constructs a new <code>RedisAtomicDouble</code> instance. Uses the value existing in Redis or 0 if none is found.
*
*
* @param redisCounter redis counter
* @param factory connection factory
*/
Expand All @@ -59,7 +62,7 @@ public RedisAtomicDouble(String redisCounter, RedisConnectionFactory factory) {

/**
* Constructs a new <code>RedisAtomicDouble</code> instance.
*
*
* @param redisCounter
* @param factory
* @param initialValue
Expand Down Expand Up @@ -94,7 +97,7 @@ private RedisAtomicDouble(String redisCounter, RedisConnectionFactory factory, D

/**
* Constructs a new <code>RedisAtomicDouble</code> instance. Uses the value existing in Redis or 0 if none is found.
*
*
* @param redisCounter the redis counter
* @param template the template
* @see #RedisAtomicDouble(String, RedisConnectionFactory, double)
Expand All @@ -108,7 +111,7 @@ public RedisAtomicDouble(String redisCounter, RedisOperations<String, Double> te
* with appropriate {@link RedisSerializer} for the key and value. As an alternative one could use the
* {@link #RedisAtomicDouble(String, RedisConnectionFactory, Double)} constructor which uses appropriate default
* serializers.
*
*
* @param redisCounter the redis counter
* @param template the template
* @param initialValue the initial value
Expand Down Expand Up @@ -139,16 +142,22 @@ private RedisAtomicDouble(String redisCounter, RedisOperations<String, Double> t

/**
* Gets the current value.
*
*
* @return the current value
*/
public double get() {
return operations.get(key);

Double value = operations.get(key);
if (value != null) {
return value.doubleValue();
}

throw new DataRetrievalFailureException(String.format("The key '%s' seems to no longer exist.", key));
}

/**
* Sets to the given value.
*
*
* @param newValue the new value
*/
public void set(double newValue) {
Expand All @@ -157,17 +166,23 @@ public void set(double newValue) {

/**
* Atomically sets to the given value and returns the old value.
*
*
* @param newValue the new value
* @return the previous value
*/
public double getAndSet(double newValue) {
return operations.getAndSet(key, newValue);

Double value = operations.getAndSet(key, newValue);
if (value != null) {
return value.doubleValue();
}

return 0;
}

/**
* Atomically sets the value to the given updated value if the current value {@code ==} the expected value.
*
*
* @param expect the expected value
* @param update the new value
* @return true if successful. False return indicates that the actual value was not equal to the expected value.
Expand Down Expand Up @@ -196,7 +211,7 @@ public Boolean execute(RedisOperations operations) {

/**
* Atomically increments by one the current value.
*
*
* @return the previous value
*/
public double getAndIncrement() {
Expand All @@ -205,7 +220,7 @@ public double getAndIncrement() {

/**
* Atomically decrements by one the current value.
*
*
* @return the previous value
*/
public double getAndDecrement() {
Expand All @@ -214,7 +229,7 @@ public double getAndDecrement() {

/**
* Atomically adds the given value to the current value.
*
*
* @param delta the value to add
* @return the previous value
*/
Expand All @@ -224,7 +239,7 @@ public double getAndAdd(final double delta) {

/**
* Atomically increments by one the current value.
*
*
* @return the updated value
*/
public double incrementAndGet() {
Expand All @@ -233,7 +248,7 @@ public double incrementAndGet() {

/**
* Atomically decrements by one the current value.
*
*
* @return the updated value
*/
public double decrementAndGet() {
Expand All @@ -242,7 +257,7 @@ public double decrementAndGet() {

/**
* Atomically adds the given value to the current value.
*
*
* @param delta the value to add
* @return the updated value
*/
Expand All @@ -252,7 +267,7 @@ public double addAndGet(double delta) {

/**
* Returns the String representation of the current value.
*
*
* @return the String representation of the current value.
*/
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright 2011-2014 the original author or authors.
*
* Copyright 2011-2016 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.
Expand All @@ -20,6 +20,7 @@
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.BoundKeyOperations;
Expand All @@ -35,10 +36,12 @@
/**
* Atomic integer backed by Redis. Uses Redis atomic increment/decrement and watch/multi/exec operations for CAS
* operations.
*
*
* @see java.util.concurrent.atomic.AtomicInteger
* @author Costin Leau
* @author Thomas Darimont
* @author Christoph Strobl
* @author Mark Paluch
*/
public class RedisAtomicInteger extends Number implements Serializable, BoundKeyOperations<String> {

Expand All @@ -50,7 +53,7 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey

/**
* Constructs a new <code>RedisAtomicInteger</code> instance. Uses the value existing in Redis or 0 if none is found.
*
*
* @param redisCounter redis counter
* @param factory connection factory
*/
Expand All @@ -60,7 +63,7 @@ public RedisAtomicInteger(String redisCounter, RedisConnectionFactory factory) {

/**
* Constructs a new <code>RedisAtomicInteger</code> instance.
*
*
* @param redisCounter the redis counter
* @param factory the factory
* @param initialValue the initial value
Expand All @@ -71,7 +74,7 @@ public RedisAtomicInteger(String redisCounter, RedisConnectionFactory factory, i

/**
* Constructs a new <code>RedisAtomicInteger</code> instance. Uses the value existing in Redis or 0 if none is found.
*
*
* @param redisCounter the redis counter
* @param template the template
* @see #RedisAtomicInteger(String, RedisConnectionFactory, int)
Expand All @@ -85,7 +88,7 @@ public RedisAtomicInteger(String redisCounter, RedisOperations<String, Integer>
* with appropriate {@link RedisSerializer} for the key and value. As an alternative one could use the
* {@link #RedisAtomicInteger(String, RedisConnectionFactory, Integer)} constructor which uses appropriate default
* serializers.
*
*
* @param redisCounter the redis counter
* @param template the template
* @param initialValue the initial value
Expand Down Expand Up @@ -137,16 +140,22 @@ private RedisAtomicInteger(String redisCounter, RedisOperations<String, Integer>

/**
* Get the current value.
*
*
* @return the current value
*/
public int get() {
return Integer.valueOf(operations.get(key));

Integer value = operations.get(key);
if (value != null) {
return value.intValue();
}

throw new DataRetrievalFailureException(String.format("The key '%s' seems to no longer exist.", key));
}

/**
* Set to the given value.
*
*
* @param newValue the new value
*/
public void set(int newValue) {
Expand All @@ -155,17 +164,23 @@ public void set(int newValue) {

/**
* Set to the give value and return the old value.
*
*
* @param newValue the new value
* @return the previous value
*/
public int getAndSet(int newValue) {
return operations.getAndSet(key, newValue);

Integer value = operations.getAndSet(key, newValue);
if (value != null) {
return value.intValue();
}

return 0;
}

/**
* Atomically set the value to the given updated value if the current value <tt>==</tt> the expected value.
*
*
* @param expect the expected value
* @param update the new value
* @return true if successful. False return indicates that the actual value was not equal to the expected value.
Expand Down Expand Up @@ -194,7 +209,7 @@ public Boolean execute(RedisOperations operations) {

/**
* Atomically increment by one the current value.
*
*
* @return the previous value
*/
public int getAndIncrement() {
Expand All @@ -203,7 +218,7 @@ public int getAndIncrement() {

/**
* Atomically decrement by one the current value.
*
*
* @return the previous value
*/
public int getAndDecrement() {
Expand All @@ -212,7 +227,7 @@ public int getAndDecrement() {

/**
* Atomically add the given value to current value.
*
*
* @param delta the value to add
* @return the previous value
*/
Expand All @@ -222,7 +237,7 @@ public int getAndAdd(final int delta) {

/**
* Atomically increment by one the current value.
*
*
* @return the updated value
*/
public int incrementAndGet() {
Expand All @@ -231,7 +246,7 @@ public int incrementAndGet() {

/**
* Atomically decrement by one the current value.
*
*
* @return the updated value
*/
public int decrementAndGet() {
Expand All @@ -240,7 +255,7 @@ public int decrementAndGet() {

/**
* Atomically add the given value to current value.
*
*
* @param delta the value to add
* @return the updated value
*/
Expand All @@ -250,7 +265,7 @@ public int addAndGet(int delta) {

/**
* Returns the String representation of the current value.
*
*
* @return the String representation of the current value.
*/
public String toString() {
Expand Down
Loading