Skip to content

Commit

Permalink
76 lindex to lists (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
robinalfengard authored Feb 9, 2024
2 parents 28f4337 + b3d4f9b commit 8a8f3eb
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/sonar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
distribution: 'temurin'
cache: maven
- name: Cache SonarCloud packages
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/fungover/haze/Command.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.fungover.haze;

public enum Command {
SET, GET, DEL, PING, SETNX, EXISTS, SAVE, RPUSH, LPUSH, LPOP, RPOP, LLEN, LMOVE, LTRIM, AUTH, INCR, DECR
SET, GET, DEL, PING, SETNX, EXISTS, SAVE, RPUSH, LPUSH, LPOP, RPOP, LLEN, LMOVE, LTRIM, AUTH, LINDEX, INCR, DECR

}
1 change: 0 additions & 1 deletion src/main/java/org/fungover/haze/HazeDatabase.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package org.fungover.haze;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/org/fungover/haze/HazeList.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,33 @@ public static String listValueAsString(List<String> list) {
return String.join("\r\n", list);
}

public String lIndex(List<String> inputList){
String key = getKey(inputList);
int index;
if (!hazeDatabase.containsKey(key))
return "Could not find list";
String indexStr = inputList.get(2);

try {
index = Integer.parseInt(indexStr);
} catch (NumberFormatException e) {
return "-ERR invalid index\r\n";
}

List<String> data = getValueAsList(hazeDatabase.getValue(key));
if(data.size()-1<index)
return NIL_RESPONSE;
String result;
if(index<0){
int newIndex = (data.size()) + index;
result = data.get(newIndex);
return "$" + result.length() + "\r\n" + result + "\r\n";
}

result = data.get(index);
return "$" + result.length() + "\r\n" + result + "\r\n";
}

private static String getKey(List<String> inputList) {
String key = null;
if (inputList.size() > 1)
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/fungover/haze/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public static String executeCommand(HazeDatabase hazeDatabase, List<String> inpu
case LLEN -> hazeList.lLen(inputList);
case LMOVE -> hazeList.lMove(inputList);
case LTRIM -> hazeList.callLtrim(inputList);
case LINDEX -> hazeList.lIndex(inputList);
case AUTH -> "+OK\r\n";
case INCR -> hazeDatabase.increaseValue(inputList);
case DECR -> hazeDatabase.decreaseValue(inputList);
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/org/fungover/haze/HazeListTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.fungover.haze;
import org.junit.jupiter.api.Test;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

class HazeListTest {
Expand Down Expand Up @@ -287,4 +289,29 @@ void CallLReturnCorrectErrorMessageWhenNotGivenNumbersAsArguments () {
void parserWithBadInputShouldReturnZero(){
assertEquals(0, HazeList.parser("This is not a number"));
}


@Test
void callingLindexWithValidPositiveIndexReturnValue(){
hazeList.rPush(List.of("", "key2", "val1", "val2", "val3"));
assertThat(hazeList.lIndex(List.of("", "key2", "2"))).isEqualTo("$4\r\nval3\r\n");
}

@Test
void callingLindexWithIndexOutOfBoundsReturnNil(){
hazeList.rPush(List.of("", "key2", "val1", "val2", "val3"));
assertThat(hazeList.lIndex(List.of("", "key2", "3"))).isEqualTo("$5\r\n(nil)\r\n");
}

@Test
void callingLindexWithValidNegativeIndexReturnValue(){
hazeList.rPush(List.of("", "key2", "val1", "val2", "val3"));
assertThat(hazeList.lIndex(List.of("", "key2", "-1"))).isEqualTo("$4\r\nval3\r\n");
}

@Test
void callingLindexWithValidIndexZeroReturnFirstValue(){
hazeList.rPush(List.of("", "key2", "val1", "val2", "val3"));
assertThat(hazeList.lIndex(List.of("", "key2", "0"))).isEqualTo("$4\r\nval1\r\n");
}
}
16 changes: 16 additions & 0 deletions src/test/java/org/fungover/haze/integration/HazeIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import redis.clients.jedis.Protocol;
import redis.clients.jedis.util.SafeEncoder;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(HazeExtension.class)
Expand Down Expand Up @@ -84,6 +86,20 @@ void listKeyWithMultipleValues() {
assertThat(pool.rpush("test", "fifth", "sixth")).isEqualTo(6);
assertThat(pool.llen("test")).isEqualTo(6);

pool.del("test");
assertThat(pool.exists("right")).isFalse();
}

@Test
void lindexReturnCorrectIndex() {
assertThat(pool.rpush("test", "hello")).isEqualTo(1);
assertThat(pool.rpush("test", "hey")).isEqualTo(2);
assertThat(pool.rpush("test", "bonjour")).isEqualTo(3);
assertThat(pool.rpush("test", "hej")).isEqualTo(4);
assertThat(pool.lindex("test", 0)).isEqualTo("hello");
assertThat(pool.lindex("test", -1)).isEqualTo("hej");


pool.del("test");
assertThat(pool.exists("right")).isFalse();
}
Expand Down

0 comments on commit 8a8f3eb

Please sign in to comment.