Skip to content

Commit

Permalink
Implement all string commands and unit test them.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Wilke committed Mar 20, 2015
1 parent b64c45b commit e405bd8
Show file tree
Hide file tree
Showing 203 changed files with 42,692 additions and 36 deletions.
12 changes: 6 additions & 6 deletions src/main/java/org/rarefiedredis/redis/AbstractRedisMock.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,31 +147,31 @@ public AbstractRedisMock() {
throw new NotImplementedException();
}

@Override public String incrbyfloat(String key, float increment) throws WrongTypeException, NotFloatException, NotImplementedException {
@Override public String incrbyfloat(String key, double increment) throws WrongTypeException, NotFloatException, NotImplementedException {
throw new NotImplementedException();
}

@Override public String mget(String ... keys) throws NotImplementedException {
@Override public String[] mget(String ... keys) throws NotImplementedException {
throw new NotImplementedException();
}

@Override public String mset(String ... keyvalues) throws NotImplementedException {
@Override public String mset(String ... keyvalues) throws ArgException, NotImplementedException {
throw new NotImplementedException();
}

@Override public String msetnx(String ... keyvalues) throws NotImplementedException {
@Override public Boolean msetnx(String ... keyvalues) throws ArgException, NotImplementedException {
throw new NotImplementedException();
}

@Override public String psetex(String key, int milliseconds, String value) throws NotImplementedException {
@Override public String psetex(String key, long milliseconds, String value) throws NotImplementedException {
throw new NotImplementedException();
}

@Override public String set(String key, String value, String ... options) throws NotImplementedException, SyntaxErrorException {
throw new NotImplementedException();
}

@Override public Long setbit(String key, long offset, boolean value) throws NotImplementedException {
@Override public Long setbit(String key, long offset, boolean value) throws WrongTypeException, NotImplementedException {
throw new NotImplementedException();
}

Expand Down
46 changes: 23 additions & 23 deletions src/main/java/org/rarefiedredis/redis/IRedisString.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,49 @@ public interface IRedisString {
/**
* Append value onto key.
*/
public Long append(String key, String value) throws WrongTypeException, NotImplementedException;
Long append(String key, String value) throws WrongTypeException, NotImplementedException;

public Long bitcount(String key, long ... options) throws WrongTypeException, NotImplementedException;
Long bitcount(String key, long ... options) throws WrongTypeException, NotImplementedException;

public Long bitop(String operation, String destkey, String ... keys) throws WrongTypeException, SyntaxErrorException, NotImplementedException;
Long bitop(String operation, String destkey, String ... keys) throws WrongTypeException, SyntaxErrorException, NotImplementedException;

public Long bitpos(String key, long bit, long ... options) throws WrongTypeException, BitArgException, NotImplementedException;
Long bitpos(String key, long bit, long ... options) throws WrongTypeException, BitArgException, NotImplementedException;

public Long decr(String key) throws WrongTypeException, NotIntegerException, NotImplementedException;
Long decr(String key) throws WrongTypeException, NotIntegerException, NotImplementedException;

public Long decrby(String key, long decrement) throws WrongTypeException, NotIntegerException, NotImplementedException;
Long decrby(String key, long decrement) throws WrongTypeException, NotIntegerException, NotImplementedException;

public String get(String key) throws WrongTypeException, NotImplementedException;
String get(String key) throws WrongTypeException, NotImplementedException;

public Boolean getbit(String key, long offset) throws WrongTypeException, NotImplementedException;
Boolean getbit(String key, long offset) throws WrongTypeException, NotImplementedException;

public String getrange(String key, long start, long end) throws WrongTypeException, NotImplementedException;
String getrange(String key, long start, long end) throws WrongTypeException, NotImplementedException;

public String getset(String key, String value) throws WrongTypeException, NotImplementedException;
String getset(String key, String value) throws WrongTypeException, NotImplementedException;

public Long incr(String key) throws WrongTypeException, NotIntegerException, NotImplementedException;
Long incr(String key) throws WrongTypeException, NotIntegerException, NotImplementedException;

public Long incrby(String key, long increment) throws WrongTypeException, NotIntegerException, NotImplementedException;
Long incrby(String key, long increment) throws WrongTypeException, NotIntegerException, NotImplementedException;

public String incrbyfloat(String key, float increment) throws WrongTypeException, NotFloatException, NotImplementedException;
String incrbyfloat(String key, double increment) throws WrongTypeException, NotFloatException, NotImplementedException;

public String mget(String ... keys) throws NotImplementedException;
String[] mget(String ... keys) throws NotImplementedException;

public String mset(String ... keyvalues) throws NotImplementedException;
String mset(String ... keyvalues) throws ArgException, NotImplementedException;

public String msetnx(String ... keyvalues) throws NotImplementedException;
Boolean msetnx(String ... keyvalues) throws ArgException, NotImplementedException;

public String psetex(String key, int milliseconds, String value) throws NotImplementedException;
String psetex(String key, long milliseconds, String value) throws NotImplementedException;

public String set(String key, String value, String ... options) throws NotImplementedException, SyntaxErrorException;
String set(String key, String value, String ... options) throws NotImplementedException, SyntaxErrorException;

public Long setbit(String key, long offset, boolean value) throws NotImplementedException;
Long setbit(String key, long offset, boolean value) throws WrongTypeException, NotImplementedException;

public String setex(String key, int seconds, String value) throws NotImplementedException;
String setex(String key, int seconds, String value) throws NotImplementedException;

public Long setnx(String key, String value) throws NotImplementedException;
Long setnx(String key, String value) throws NotImplementedException;

public Long setrange(String key, long offset, String value) throws WrongTypeException, NotImplementedException;
Long setrange(String key, long offset, String value) throws WrongTypeException, NotImplementedException;

public Long strlen(String key) throws WrongTypeException, NotImplementedException;
Long strlen(String key) throws WrongTypeException, NotImplementedException;
}
192 changes: 185 additions & 7 deletions src/main/java/org/rarefiedredis/redis/RedisMock.java
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ else if (operation.equals("not")) {
return stringCache.get(key);
}

@Override public synchronized Boolean getbit(String key, long offset) throws WrongTypeException {
@Override public synchronized Boolean getbit(final String key, final long offset) throws WrongTypeException {
if (!exists(key)) {
return false;
}
Expand All @@ -318,7 +318,7 @@ else if (operation.equals("not")) {
return ((n >> pos) & 0x01) == 1;
}

@Override public synchronized String getrange(String key, long start, long end) throws WrongTypeException {
@Override public synchronized String getrange(final String key, long start, long end) throws WrongTypeException {
if (!exists(key)) {
return "";
}
Expand All @@ -339,8 +339,8 @@ else if (operation.equals("not")) {
}
}

@Override public synchronized String getset(String key, String value) throws WrongTypeException {
String prev = "";
@Override public synchronized String getset(final String key, final String value) throws WrongTypeException {
String prev = null;
try {
prev = get(key);
set(key, value);
Expand All @@ -353,6 +353,97 @@ else if (operation.equals("not")) {
return prev;
}

@Override public synchronized Long incr(final String key) throws WrongTypeException, NotIntegerException {
return decrby(key, -1);
}

@Override public synchronized Long incrby(final String key, final long increment) throws WrongTypeException, NotIntegerException {
return decrby(key, -increment);
}

@Override public synchronized String incrbyfloat(final String key, final double increment) throws WrongTypeException, NotFloatException {
Double newValue = 0.0d;
try {
if (!exists(key)) {
set(key, "0.0");
}
checkType(key, "string");
double asDouble = Double.parseDouble(get(key));
newValue = asDouble + increment;
set(key, String.valueOf(newValue));
}
catch (NumberFormatException nfe) {
throw new NotFloatException();
}
catch (SyntaxErrorException see) {
}
return String.valueOf(newValue);
}

@Override public synchronized String[] mget(final String ... keys) {
String[] gets = new String[keys.length];
for (int idx = 0; idx < keys.length; ++idx) {
try {
gets[idx] = get(keys[idx]);
}
catch (WrongTypeException e) {
gets[idx] = null;
}
}
return gets;
}

@Override public synchronized String mset(final String ... keyvalues) throws ArgException {
if (keyvalues.length == 0 || keyvalues.length % 2 != 0) {
throw new ArgException("mset");
}
for (int idx = 0; idx < keyvalues.length; ++idx) {
if (idx % 2 != 0) {
continue;
}
try {
set(keyvalues[idx], keyvalues[idx + 1]);
}
catch (SyntaxErrorException e) {
}
}
return "OK";
}

@Override public synchronized Boolean msetnx(final String ... keyvalues) throws ArgException {
if (keyvalues.length == 0 || keyvalues.length % 2 != 0) {
throw new ArgException("msetnx");
}
for (int idx = 0; idx < keyvalues.length; ++idx) {
if (idx % 2 != 0) {
continue;
}
if (exists(keyvalues[idx])) {
return false;
}
}
for (int idx = 0; idx < keyvalues.length; ++idx) {
if (idx % 2 != 0) {
continue;
}
try {
set(keyvalues[idx], keyvalues[idx + 1]);
}
catch (SyntaxErrorException e) {
}
}
return true;
}

@Override public synchronized String psetex(String key, long milliseconds, String value) {
try {
set(key, value, "px", String.valueOf(milliseconds));
}
catch (SyntaxErrorException e) {
}
return "OK";
}

@Override public synchronized String set(final String key, final String value, String ... options) throws SyntaxErrorException {
boolean nx = false, xx = false;
int ex = -1;
Expand Down Expand Up @@ -407,13 +498,100 @@ else if (option == "px") {
return "OK";
}

@Override public synchronized Long setbit(final String key, final long offset, final boolean value) throws WrongTypeException {
checkType(key, "string");
if (!exists(key)) {
try {
set(key, "");
}
catch (SyntaxErrorException e) {
}
}
int byteIdx = (int)Math.floor(offset/8L);
int bitIdx = (int)(offset % 8L);
String val = get(key);
while (val.length() < byteIdx + 1) {
val += "\0";
}
int code = val.codePointAt(byteIdx);
int idx = 0;
int mask = 0x80;
while (idx < bitIdx) {
mask >>= 1;
idx += 1;
}
int bit = (code & mask) == 0 ? 0 : 1;
if (!value) {
code = code & (~mask);
}
else {
code = code | mask;
}
String newVal = "";
newVal += val.substring(0, byteIdx);
newVal += (char)(code);
newVal += val.substring(byteIdx + 1);
try {
set(key, newVal);
}
catch (SyntaxErrorException e) {
}
return (long)bit;
}

@Override public synchronized String setex(final String key, final int seconds, final String value) {
try {
set(key, value, "ex", String.valueOf(seconds));
}
catch (SyntaxErrorException e) {
}
return "OK";
}

@Override public synchronized Long setnx(final String key, final String value) {
if (!exists(key)) {
try {
set(key, value);
return 1L;
}
catch (SyntaxErrorException e) {
}
}
return 0L;
}

@Override public synchronized Long setrange(final String key, final long offset, final String value) throws WrongTypeException {
checkType(key, "string");
if (!exists(key)) {
try {
set(key, "");
}
catch (SyntaxErrorException e) {
}
}
String val = get(key);
int idx;
for (idx = val.length(); idx < (int)(offset + value.length()); ++idx) {
val += "\0";
}
String newValue = val.substring(0, (int)offset);
for (idx = (int)offset; idx < (int)(offset + value.length()); ++idx) {
newValue += value.charAt(idx - (int)offset);
}
newValue += val.substring((int)(offset + value.length()));
try {
set(key, newValue);
}
catch (SyntaxErrorException e) {
}
return (long)newValue.length();
}

@Override public synchronized Long strlen(final String key) throws WrongTypeException {
if (!exists(key)) {
return 0L;
}
if (type(key) != "string") {
throw new WrongTypeException();
}
checkType(key, "string");
return (long)stringCache.get(key).length();
}

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/rarefiedredis/redis/SetbitException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public final class SetbitException extends Exception {

public SetbitException() {
super("ERR bit is not an integer or out of range");
}
}
Loading

0 comments on commit e405bd8

Please sign in to comment.