Skip to content
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

16 incr decr incrby decrby #190

Merged
merged 2 commits into from
Feb 8, 2024
Merged
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 src/main/java/org/fungover/haze/Command.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.fungover.haze;

public enum Command {
SET, GET, DEL, PING, SETNX, EXISTS, SAVE, RPUSH, LPUSH, LPOP, RPOP, LLEN, LMOVE, LTRIM, AUTH
SET, GET, DEL, PING, SETNX, EXISTS, SAVE, RPUSH, LPUSH, LPOP, RPOP, LLEN, LMOVE, LTRIM, AUTH, INCR, DECR
}
43 changes: 43 additions & 0 deletions src/main/java/org/fungover/haze/HazeDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,47 @@ public void addValue(String key, String value) {
lock.unlock();
}
}

public String increaseValue(List<String> inputList) {
lock.lock();
String key = inputList.get(1);
try {
if (!database.containsKey(key)) {
return "-ERR no such key\r\n";
}
String value = database.get(key);
try {
long longValue = Long.parseLong(value);
longValue++;
database.put(key, String.valueOf(longValue));
return ":" + longValue + "\r\n";
} catch (NumberFormatException e) {
return "-WRONGTYPE value is not an integer or out of range\r\n";
}
} finally {
lock.unlock();
}
}

public String decreaseValue(List<String> inputList) {
lock.lock();
String key = inputList.get(1);
try {
if (!database.containsKey(key)) {
return "-ERR no such key\r\n";
}
String value = database.get(key);
try {
long longValue = Long.parseLong(value);
longValue--;
database.put(key, String.valueOf(longValue));
return ":" + longValue + "\r\n";
} catch (NumberFormatException e) {
return "-WRONGTYPE value is not an integer or out of range\r\n";
}
} finally {
lock.unlock();
}
}

}
3 changes: 2 additions & 1 deletion src/main/java/org/fungover/haze/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ public static String executeCommand(HazeDatabase hazeDatabase, List<String> inpu
case LMOVE -> hazeList.lMove(inputList);
case LTRIM -> hazeList.callLtrim(inputList);
case AUTH -> "+OK\r\n";

case INCR -> hazeDatabase.increaseValue(inputList);
case DECR -> hazeDatabase.decreaseValue(inputList);
};
}

Expand Down
26 changes: 26 additions & 0 deletions src/test/java/org/fungover/haze/HazeDatabaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,31 @@ void shouldShouldReturnTrue(){
assertThat(testDatabase.containsKey("key1")).isTrue();
}

@Test
void callingIncreaseWithKeyWithIntegerShouldIncreaseValueBy1(){
testDatabase.addValue("key1", "1");

String increaseResult = testDatabase.increaseValue(List.of("INCR","key1"));
assertThat(increaseResult).isEqualTo(":2\r\n");
assertThat(testDatabase.getValue("key1")).isEqualTo("2");

}

@Test
void callingIncreaseWithKeyThatDoesNotContainIntegerShouldReturnErrorMessage() {
testDatabase.addValue("key1", "Gunnar");
String increaseResult = testDatabase.increaseValue(List.of("INCR","key1"));
assertThat(increaseResult).isEqualTo("-WRONGTYPE value is not an integer or out of range\r\n");
}
@Test
void callingDecreaseWithKeyWithIntegerShouldDecreaseValueBy1(){
testDatabase.addValue("key1", "1");

String increaseResult = testDatabase.decreaseValue(List.of("DECR","key1"));
assertThat(increaseResult).isEqualTo(":0\r\n");
assertThat(testDatabase.getValue("key1")).isEqualTo("0");

}


}
12 changes: 12 additions & 0 deletions src/test/java/org/fungover/haze/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ void callExecuteCommandWithLTRIMShouldReturnErrorMessageWhenKeyDoesNotExist() {
assertThat(Main.executeCommand(database, List.of("LTRIM", "key", "2", "3"), hazeList)).isEqualTo("-The key is not present in the database.\r\n");
}

@Test
void callExecuteCommandWithIncrShouldIncreaseTheValueOfTheKeyBy1() {
Main.executeCommand(database, List.of("SET", "key1", "1"), hazeList);
assertThat(Main.executeCommand(database, List.of("INCR", "key1"), hazeList)).isEqualTo(":2\r\n");
}

@Test
void callExecuteCommandWithDecrShouldDecreaseTheValueOfTheKeyBy1(){
Main.executeCommand(database, List.of("SET", "key1", "1"), hazeList);
assertThat(Main.executeCommand(database, List.of("DECR", "key1"), hazeList)).isEqualTo(":0\r\n");
}

@Test
void testPrintThreadDebug() {
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
Expand Down
Loading