diff --git a/src/main/java/org/fungover/haze/HazeDatabase.java b/src/main/java/org/fungover/haze/HazeDatabase.java index e21d6bc8..cf7b2aef 100644 --- a/src/main/java/org/fungover/haze/HazeDatabase.java +++ b/src/main/java/org/fungover/haze/HazeDatabase.java @@ -58,14 +58,19 @@ public String exists(String key) { return ""; } - public String setNX(String key) { + public String setNX(String key, String value) { + String replyWhenKeyNotSet = ":0\r\n"; + String replyWhenKeySet = ":1\r\n"; lock.lock(); try { - //sets value if key does not exists, if there is a key this operation is ignored. + if(database.containsKey(key)) + return replyWhenKeyNotSet; + else{ + database.put(key, value); + return replyWhenKeySet; + } } finally { lock.unlock(); - } - return ""; } } diff --git a/src/main/java/org/fungover/haze/Main.java b/src/main/java/org/fungover/haze/Main.java index 85c41a8f..370f4c87 100644 --- a/src/main/java/org/fungover/haze/Main.java +++ b/src/main/java/org/fungover/haze/Main.java @@ -4,10 +4,16 @@ import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; +import java.net.Socket; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; public class Main { public static void main(String[] args) { + HazeDatabase hazeDatabase = new HazeDatabase(); + try (ServerSocket serverSocket = new ServerSocket(6379)) { while (true) { var client = serverSocket.accept(); @@ -16,18 +22,17 @@ public static void main(String[] args) { try { - BufferedReader input = new BufferedReader(new InputStreamReader(client.getInputStream())); - System.out.println(input.readLine()); - System.out.println(input.readLine()); - System.out.println(input.readLine()); - System.out.println(input.readLine()); - System.out.println(input.readLine()); + BufferedReader input = new BufferedReader(new InputStreamReader(client.getInputStream())); + List inputList = new ArrayList<>(); + String firstReading = input.readLine(); + readInputStream(input, inputList, firstReading); - printThreadDebug(); + executeCommand(hazeDatabase, client, inputList); + inputList.forEach(System.out::println); // For checking incoming message - client.getOutputStream().write("+OK\r\n".getBytes()); + printThreadDebug(); client.close(); @@ -36,12 +41,9 @@ public static void main(String[] args) { } }; Thread.startVirtualThread(newThread); - } } catch (IOException e) { throw new RuntimeException(e); - - } } @@ -49,4 +51,36 @@ private static void printThreadDebug() { System.out.println("ThreadID " + Thread.currentThread().threadId()); // Only for Debug System.out.println("Is virtual Thread " + Thread.currentThread().isVirtual()); // Only for Debug } + + private static void executeCommand(HazeDatabase hazeDatabase, Socket client, List inputList) throws IOException { + String command = inputList.get(0); + String key = inputList.get(1); + String value = getValueIfExist(inputList); + + switch (command) { + case "SETNX" -> client.getOutputStream().write(hazeDatabase.setNX(key, value).getBytes()); + default -> client.getOutputStream().write("-ERR unknown command\r\n".getBytes()); + } + } + + private static String getValueIfExist(List inputList) { + if (inputList.size() == 3) + return inputList.get(2); + return ""; + } + + private static void readInputStream(BufferedReader input, List inputList, String firstReading) throws IOException { + int size; + if (firstReading.startsWith("*")) { + size = Integer.parseInt(firstReading.substring(1)) * 2; + for (int i = 0; i < size; i++) { + String temp = input.readLine(); + if (!temp.contains("$")) + inputList.add(temp); + } + } else { + String[] seperated = firstReading.split("\\s"); + inputList.addAll(Arrays.asList(seperated)); + } + } } diff --git a/src/test/java/org/fungover/haze/HazeDatabaseTest.java b/src/test/java/org/fungover/haze/HazeDatabaseTest.java new file mode 100644 index 00000000..98d4b7b5 --- /dev/null +++ b/src/test/java/org/fungover/haze/HazeDatabaseTest.java @@ -0,0 +1,21 @@ +package org.fungover.haze; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class HazeDatabaseTest { + + HazeDatabase testDatabase = new HazeDatabase(); + + @Test + void testSetNxReturnZeroWhenExistingKeyAreUsedWithDifferentValue() { + testDatabase.setNX("1", "Hej"); + assertThat(testDatabase.setNX("1", "Då")).isEqualTo(":0\r\n"); + } + + @Test + void testSetNxReturnOneWhenKeyDontExist() { + assertThat(testDatabase.setNX("2", "Då")).isEqualTo(":1\r\n"); + } +}