diff options
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 60 |
1 files changed, 34 insertions, 26 deletions
@@ -9,6 +9,7 @@ import ( "strconv" "strings" "sync" + "errors" ) type RedisDB struct { @@ -47,17 +48,18 @@ func handleGet(conn net.Conn, key string) { value, exists := db.data[key] if !exists { - conn.Write([]byte("$-1\r\n")) - return + conn.Write(serializeRESP(nil)) + return } strValue, ok := value.(string) if !ok { - conn.Write([]byte("-WRONGTYPE Operation against a key holding the wrong kind of value\r\n")) - return + err := errors.New("WRONGTYPE Operation against a key holding the wrong kind of value") + conn.Write(serializeRESP(err)) + return } - conn.Write([]byte("+" + strValue + "\r\n")) + conn.Write(serializeRESP(strValue)) } func handleExists(conn net.Conn, key string) { @@ -66,9 +68,9 @@ func handleExists(conn net.Conn, key string) { _, exists := db.data[key] if exists { - conn.Write([]byte(":1\r\n")) + conn.Write(serializeRESP(1)) } else { - conn.Write([]byte(":0\r\n")) + conn.Write(serializeRESP(0)) } } @@ -77,7 +79,7 @@ func handleSet(conn net.Conn, key, value string) { defer db.mu.Unlock() db.data[key] = value - conn.Write([]byte("+OK\r\n")) + conn.Write(serializeRESP(SimpleString("OK"))) } func handleDel(conn net.Conn, key string) { @@ -87,9 +89,9 @@ func handleDel(conn net.Conn, key string) { _, exists := db.data[key] if exists { delete(db.data, key) - conn.Write([]byte(":1\r\n")) + conn.Write(serializeRESP(1)) } else { - conn.Write([]byte(":0\r\n")) + conn.Write(serializeRESP(0)) } } @@ -101,22 +103,24 @@ func handleIncr(conn net.Conn, key string) { if exists { strValue, ok := value.(string) if !ok { - conn.Write([]byte("-ERR value is not an integer or out of range\r\n")) + err := errors.New("value is not an integer or out of range") + conn.Write(serializeRESP(err)) return } currentInt, err := strconv.Atoi(strValue) if err != nil { - conn.Write([]byte("-ERR value is not an integer or out of range\r\n")) + err := errors.New("value is not an integer or out of range") + conn.Write(serializeRESP(err)) return } newValueStr := strconv.Itoa(currentInt + 1) db.data[key] = newValueStr - conn.Write([]byte(":" + newValueStr + "\r\n")) + conn.Write(serializeRESP(currentInt + 1)) } else { db.data[key] = "1" - conn.Write([]byte(":1\r\n")) + conn.Write(serializeRESP(1)) } } @@ -128,27 +132,29 @@ func handleDecr(conn net.Conn, key string) { if exists { strValue, ok := value.(string) if !ok { - conn.Write([]byte("-ERR value is not an integer or out of range\r\n")) + err := errors.New("value is not an integer or out of range") + conn.Write(serializeRESP(err)) return } currentInt, err := strconv.Atoi(strValue) if err != nil { - conn.Write([]byte("-ERR value is not an integer or out of range\r\n")) + err := errors.New("value is not an integer or out of range") + conn.Write(serializeRESP(err)) return } newValueStr := strconv.Itoa(currentInt - 1) db.data[key] = newValueStr - conn.Write([]byte(":" + newValueStr + "\r\n")) + conn.Write(serializeRESP(currentInt - 1)) } else { db.data[key] = "-1" - conn.Write([]byte(":-1\r\n")) + conn.Write(serializeRESP(-1)) } } func handlePing(conn net.Conn) { - conn.Write([]byte("+PONG\r\n")) + conn.Write(serializeRESP(SimpleString("PONG"))) } func handleFlushall(conn net.Conn) { @@ -156,7 +162,7 @@ func handleFlushall(conn net.Conn) { defer db.mu.Unlock() db.data = make(map[string]any) - conn.Write([]byte("+OK\r\n")) + conn.Write(serializeRESP(SimpleString("OK"))) } func handleRpush(conn net.Conn, args []string) { @@ -173,7 +179,8 @@ func handleRpush(conn net.Conn, args []string) { var ok bool list, ok = existingValue.([]string) if !ok { - conn.Write([]byte("-WRONGTYPE Operation against a key holding the wrong kind of value\r\n")) + err := errors.New("WRONGTYPE Operation against a key holding the wrong kind of value") + conn.Write(serializeRESP(err)) return } } else { @@ -181,8 +188,7 @@ func handleRpush(conn net.Conn, args []string) { } list = append(list, newItems...) db.data[key] = list - - conn.Write([]byte(":" + strconv.Itoa(len(list)) + "\r\n")) + conn.Write(serializeRESP(len(list))) } func handleLrange(conn net.Conn, key string, start, stop int) { @@ -191,13 +197,15 @@ func handleLrange(conn net.Conn, key string, start, stop int) { value, exists := db.data[key] if !exists { - conn.Write([]byte("*0\r\n")) + conn.Write(serializeRESP([]string{})) + return } typedList, ok := value.([]string) if !ok { - conn.Write([]byte("-WRONGTYPE Operation against a key holding the wrong kind of value\r\n")) + err := errors.New("WRONGTYPE Operation against a key holding the wrong kind of value") + conn.Write(serializeRESP(err)) return } @@ -222,7 +230,7 @@ func handleLrange(conn net.Conn, key string, start, stop int) { } if start > stop || start >= size { - conn.Write([]byte("*0\r\n")) + conn.Write(serializeRESP([]string{})) return } |
