aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--handlers.go111
-rw-r--r--main.go25
2 files changed, 109 insertions, 27 deletions
diff --git a/handlers.go b/handlers.go
index 4625f6a..885e348 100644
--- a/handlers.go
+++ b/handlers.go
@@ -6,10 +6,18 @@ import (
"strconv"
)
-func handleGet(conn net.Conn, key string) {
+func handleGet(conn net.Conn, args []string) {
db.mu.Lock()
defer db.mu.Unlock()
+ if len(args) != 2 {
+ err := errors.New("wrong number of arguments for 'GET'")
+ conn.Write(serializeRESP(err))
+ return
+ }
+
+ key := args[1]
+
value, exists := db.data[key]
if !exists {
conn.Write(serializeRESP(nil))
@@ -26,10 +34,18 @@ func handleGet(conn net.Conn, key string) {
conn.Write(serializeRESP(strValue))
}
-func handleExists(conn net.Conn, key string) {
+func handleExists(conn net.Conn, args []string) {
db.mu.Lock()
defer db.mu.Unlock()
+ if len(args) != 2 {
+ err := errors.New("wrong number of arguments for 'EXISTS'")
+ conn.Write(serializeRESP(err))
+ return
+ }
+
+ key := args[1]
+
_, exists := db.data[key]
if exists {
conn.Write(serializeRESP(1))
@@ -38,18 +54,35 @@ func handleExists(conn net.Conn, key string) {
}
}
-func handleSet(conn net.Conn, key, value string) {
+func handleSet(conn net.Conn, args []string) {
db.mu.Lock()
defer db.mu.Unlock()
+ if len(args) != 3 {
+ err := errors.New("wrong number of arguments for 'SET'")
+ conn.Write(serializeRESP(err))
+ return
+ }
+
+ key := args[1]
+ value := args[2]
+
db.data[key] = value
conn.Write(serializeRESP(SimpleString("OK")))
}
-func handleDel(conn net.Conn, key string) {
+func handleDel(conn net.Conn, args []string) {
db.mu.Lock()
defer db.mu.Unlock()
+ if len(args) != 2 {
+ err := errors.New("wrong number of arguments for 'DEL'")
+ conn.Write(serializeRESP(err))
+ return
+ }
+
+ key := args[1]
+
_, exists := db.data[key]
if exists {
delete(db.data, key)
@@ -59,10 +92,18 @@ func handleDel(conn net.Conn, key string) {
}
}
-func handleIncr(conn net.Conn, key string) {
+func handleIncr(conn net.Conn, args []string) {
db.mu.Lock()
defer db.mu.Unlock()
+ if len(args) != 2 {
+ err := errors.New("wrong number of arguments for 'INCR'")
+ conn.Write(serializeRESP(err))
+ return
+ }
+
+ key := args[1]
+
value, exists := db.data[key]
if exists {
strValue, ok := value.(string)
@@ -88,10 +129,18 @@ func handleIncr(conn net.Conn, key string) {
}
}
-func handleDecr(conn net.Conn, key string) {
+func handleDecr(conn net.Conn, args []string) {
db.mu.Lock()
defer db.mu.Unlock()
+ if len(args) != 2 {
+ err := errors.New("wrong number of arguments for 'DECR'")
+ conn.Write(serializeRESP(err))
+ return
+ }
+
+ key := args[1]
+
value, exists := db.data[key]
if exists {
strValue, ok := value.(string)
@@ -117,14 +166,25 @@ func handleDecr(conn net.Conn, key string) {
}
}
-func handlePing(conn net.Conn) {
+func handlePing(conn net.Conn, args []string) {
+ if len(args) != 1 {
+ err := errors.New("wrong number of arguments for 'PING'")
+ conn.Write(serializeRESP(err))
+ return
+ }
conn.Write(serializeRESP(SimpleString("PONG")))
}
-func handleFlushall(conn net.Conn) {
+func handleFlushall(conn net.Conn, args []string) {
db.mu.Lock()
defer db.mu.Unlock()
+ if len(args) != 1 {
+ err := errors.New("wrong number of arguments for 'FLUSHALL'")
+ conn.Write(serializeRESP(err))
+ return
+ }
+
db.data = make(map[string]any)
conn.Write(serializeRESP(SimpleString("OK")))
}
@@ -155,10 +215,25 @@ func handleRpush(conn net.Conn, args []string) {
conn.Write(serializeRESP(len(list)))
}
-func handleLrange(conn net.Conn, key string, start, stop int) {
+func handleLrange(conn net.Conn, args []string) {
db.mu.Lock()
defer db.mu.Unlock()
+ if len(args) != 4 {
+ err := errors.New("wrong number of arguments for 'LRANGE'")
+ conn.Write(serializeRESP(err))
+ return
+ }
+ start, err1 := strconv.Atoi(args[2])
+ stop, err2 := strconv.Atoi(args[3])
+ if err1 != nil || err2 != nil {
+ err := errors.New("value is not an integer or out of range")
+ conn.Write(serializeRESP(err))
+ return
+ }
+
+ key := args[1]
+
value, exists := db.data[key]
if !exists {
conn.Write(serializeRESP([]string{}))
@@ -202,10 +277,17 @@ func handleLrange(conn net.Conn, key string, start, stop int) {
conn.Write(serializeRESP(resultList))
}
-func handleLpop(conn net.Conn, key string){
+func handleLpop(conn net.Conn, args []string){
db.mu.Lock()
defer db.mu.Unlock()
+ if len(args) != 2 {
+ err := errors.New("wrong number of arguments for 'LPOP'")
+ conn.Write(serializeRESP(err))
+ return
+ }
+
+ key := args[1]
value, exists := db.data[key]
if !exists {
conn.Write(serializeRESP(nil))
@@ -235,10 +317,17 @@ func handleLpop(conn net.Conn, key string){
}
-func handleRpop(conn net.Conn, key string){
+func handleRpop(conn net.Conn, args []string){
db.mu.Lock()
defer db.mu.Unlock()
+ if len(args) != 2 {
+ err := errors.New("wrong number of arguments for 'RPOOP'")
+ conn.Write(serializeRESP(err))
+ return
+ }
+
+ key := args[1]
value, exists := db.data[key]
if !exists {
conn.Write(serializeRESP(nil))
diff --git a/main.go b/main.go
index 81ff1fc..38ccd34 100644
--- a/main.go
+++ b/main.go
@@ -5,7 +5,6 @@ import (
"fmt"
"net"
"os"
- "strconv"
"strings"
)
@@ -55,31 +54,25 @@ func handleConnection(conn net.Conn) {
switch {
case command == "GET" && len(args) == 2:
- handleGet(conn, args[1])
+ handleGet(conn, args)
case command == "SET" && len(args) == 3:
- handleSet(conn, args[1], args[2])
+ handleSet(conn, args)
case command == "EXISTS" && len(args) == 2:
- handleExists(conn, args[1])
+ handleExists(conn, args)
case command == "DEL" && len(args) == 2:
- handleDel(conn, args[1])
+ handleDel(conn, args)
case command == "INCR" && len(args) == 2:
- handleIncr(conn, args[1])
+ handleIncr(conn, args)
case command == "DECR" && len(args) == 2:
- handleDecr(conn, args[1])
+ handleDecr(conn, args)
case command == "PING" && len(args) == 1:
- handlePing(conn)
+ handlePing(conn, args)
case command == "FLUSHALL" && len(args) == 1:
- handleFlushall(conn)
+ handleFlushall(conn, args)
case command == "RPUSH" && len(args) >= 3:
handleRpush(conn, args)
case command == "LRANGE" && len(args) == 4:
- start, err1 := strconv.Atoi(args[2])
- stop, err2 := strconv.Atoi(args[3])
- if err1 != nil || err2 != nil {
- conn.Write([]byte("-ERR value is not an integer or out of range\r\n"))
- break
- }
- handleLrange(conn, args[1], start, stop)
+ handleLrange(conn, args)
default:
conn.Write([]byte("-ERR unknown command or wrong arguments\r\n"))
}