Skip to content

Commit 29dbc68

Browse files
authored
Merge branch 'main' into 78-lset-to-list
2 parents 4c48afc + e84e332 commit 29dbc68

File tree

5 files changed

+100
-3
lines changed

5 files changed

+100
-3
lines changed
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.fungover.haze;
22

33
public enum Command {
4-
SET, GET, DEL, PING, SETNX, EXISTS, SAVE, RPUSH, LPUSH, LPOP, RPOP, LLEN, LMOVE, LTRIM, AUTH,LSET,
5-
}
4+
5+
6+
7+
SET, GET, DEL, PING, SETNX, EXISTS, SAVE, RPUSH, LPUSH, LPOP, RPOP, LLEN, LMOVE, LTRIM, AUTH, INCR, DECR,LSET
8+

src/main/java/org/fungover/haze/HazeDatabase.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public String get(List<String> inputList) {
4646
}
4747

4848
public String delete(List<String> keys) {
49+
if (keys.isEmpty())
50+
throw new IllegalArgumentException("No keys provided");
51+
4952
var counter = new AtomicInteger(0);
5053
lock.lock();
5154
try {
@@ -62,6 +65,9 @@ public String delete(List<String> keys) {
6265
}
6366

6467
public String exists(List<String> keys) {
68+
if (keys.isEmpty())
69+
return ":0\r\n";
70+
6571
lock.lock();
6672
int numberOfKeys = 0;
6773
try {
@@ -109,6 +115,12 @@ public Map<String, String> copy() {
109115
}
110116

111117
public String ping(List<String> messageList) {
118+
if (messageList == null || messageList.isEmpty()) {
119+
throw new IllegalArgumentException("No message provided");
120+
} else if (messageList.size() > 2) {
121+
throw new IllegalArgumentException("Too many arguments for PING command");
122+
}
123+
112124
if (messageList.size() == 1)
113125
return "+PONG\r\n";
114126
else return "$" + (messageList.get(1)).length() + "\r\n" + messageList.get(1) + "\r\n";
@@ -143,4 +155,47 @@ public void addValue(String key, String value) {
143155
lock.unlock();
144156
}
145157
}
158+
159+
public String increaseValue(List<String> inputList) {
160+
lock.lock();
161+
String key = inputList.get(1);
162+
try {
163+
if (!database.containsKey(key)) {
164+
return "-ERR no such key\r\n";
165+
}
166+
String value = database.get(key);
167+
try {
168+
long longValue = Long.parseLong(value);
169+
longValue++;
170+
database.put(key, String.valueOf(longValue));
171+
return ":" + longValue + "\r\n";
172+
} catch (NumberFormatException e) {
173+
return "-WRONGTYPE value is not an integer or out of range\r\n";
174+
}
175+
} finally {
176+
lock.unlock();
177+
}
178+
}
179+
180+
public String decreaseValue(List<String> inputList) {
181+
lock.lock();
182+
String key = inputList.get(1);
183+
try {
184+
if (!database.containsKey(key)) {
185+
return "-ERR no such key\r\n";
186+
}
187+
String value = database.get(key);
188+
try {
189+
long longValue = Long.parseLong(value);
190+
longValue--;
191+
database.put(key, String.valueOf(longValue));
192+
return ":" + longValue + "\r\n";
193+
} catch (NumberFormatException e) {
194+
return "-WRONGTYPE value is not an integer or out of range\r\n";
195+
}
196+
} finally {
197+
lock.unlock();
198+
}
199+
}
200+
146201
}

src/main/java/org/fungover/haze/Main.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ public static String executeCommand(HazeDatabase hazeDatabase, List<String> inpu
122122
case LTRIM -> hazeList.callLtrim(inputList);
123123
case LSET -> hazeList.lSet(inputList);
124124
case AUTH -> "+OK\r\n";
125-
125+
case INCR -> hazeDatabase.increaseValue(inputList);
126+
case DECR -> hazeDatabase.decreaseValue(inputList);
126127
};
127128
}
128129

src/test/java/org/fungover/haze/HazeDatabaseTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,5 +159,31 @@ void shouldShouldReturnTrue(){
159159
assertThat(testDatabase.containsKey("key1")).isTrue();
160160
}
161161

162+
@Test
163+
void callingIncreaseWithKeyWithIntegerShouldIncreaseValueBy1(){
164+
testDatabase.addValue("key1", "1");
165+
166+
String increaseResult = testDatabase.increaseValue(List.of("INCR","key1"));
167+
assertThat(increaseResult).isEqualTo(":2\r\n");
168+
assertThat(testDatabase.getValue("key1")).isEqualTo("2");
169+
170+
}
171+
172+
@Test
173+
void callingIncreaseWithKeyThatDoesNotContainIntegerShouldReturnErrorMessage() {
174+
testDatabase.addValue("key1", "Gunnar");
175+
String increaseResult = testDatabase.increaseValue(List.of("INCR","key1"));
176+
assertThat(increaseResult).isEqualTo("-WRONGTYPE value is not an integer or out of range\r\n");
177+
}
178+
@Test
179+
void callingDecreaseWithKeyWithIntegerShouldDecreaseValueBy1(){
180+
testDatabase.addValue("key1", "1");
181+
182+
String increaseResult = testDatabase.decreaseValue(List.of("DECR","key1"));
183+
assertThat(increaseResult).isEqualTo(":0\r\n");
184+
assertThat(testDatabase.getValue("key1")).isEqualTo("0");
185+
186+
}
187+
162188

163189
}

src/test/java/org/fungover/haze/MainTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@ void callExecuteCommandWithLTRIMShouldReturnErrorMessageWhenKeyDoesNotExist() {
108108
assertThat(Main.executeCommand(database, List.of("LTRIM", "key", "2", "3"), hazeList)).isEqualTo("-The key is not present in the database.\r\n");
109109
}
110110

111+
@Test
112+
void callExecuteCommandWithIncrShouldIncreaseTheValueOfTheKeyBy1() {
113+
Main.executeCommand(database, List.of("SET", "key1", "1"), hazeList);
114+
assertThat(Main.executeCommand(database, List.of("INCR", "key1"), hazeList)).isEqualTo(":2\r\n");
115+
}
116+
117+
@Test
118+
void callExecuteCommandWithDecrShouldDecreaseTheValueOfTheKeyBy1(){
119+
Main.executeCommand(database, List.of("SET", "key1", "1"), hazeList);
120+
assertThat(Main.executeCommand(database, List.of("DECR", "key1"), hazeList)).isEqualTo(":0\r\n");
121+
}
122+
111123
@Test
112124
void testPrintThreadDebug() {
113125
ByteArrayOutputStream outContent = new ByteArrayOutputStream();

0 commit comments

Comments
 (0)