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

15 setnx #67

Merged
merged 5 commits into from
Feb 10, 2023
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
13 changes: 9 additions & 4 deletions src/main/java/org/fungover/haze/HazeDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 "";
}
}
56 changes: 45 additions & 11 deletions src/main/java/org/fungover/haze/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<String> 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();

Expand All @@ -36,17 +41,46 @@ public static void main(String[] args) {
}
};
Thread.startVirtualThread(newThread);

}
} catch (IOException e) {
throw new RuntimeException(e);


}
}

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<String> 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<String> inputList) {
if (inputList.size() == 3)
return inputList.get(2);
return "";
}

private static void readInputStream(BufferedReader input, List<String> 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));
}
}
}
21 changes: 21 additions & 0 deletions src/test/java/org/fungover/haze/HazeDatabaseTest.java
Original file line number Diff line number Diff line change
@@ -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");
}
}