aboutsummaryrefslogtreecommitdiff
path: root/handlers.go
diff options
context:
space:
mode:
Diffstat (limited to 'handlers.go')
-rw-r--r--handlers.go92
1 files changed, 47 insertions, 45 deletions
diff --git a/handlers.go b/handlers.go
index 2f07500..23efa9a 100644
--- a/handlers.go
+++ b/handlers.go
@@ -3,6 +3,7 @@ package main
import (
"errors"
"net"
+ "strconv"
"time"
)
@@ -98,10 +99,8 @@ func handleDel(conn net.Conn, args []string) {
conn.Write(serializeRESP(deletedKeys))
}
-/*
+
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'")
@@ -110,35 +109,35 @@ func handleIncr(conn net.Conn, args []string) {
}
key := args[1]
+ result := db.Execute(key, func(s *Shard)interface{}{
+ item, exists := s.data[key]
+ if exists {
+ strValue, ok := item.Value.(string)
+ if !ok {
+ err := errors.New("value is not an integer or out of range")
+ return err
+ }
- item, exists := db.data[key]
- if exists {
- strValue, ok := item.Value.(string)
- if !ok {
- 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 {
+ err := errors.New("value is not an integer or out of range")
+ return err
+ }
- currentInt, err := strconv.Atoi(strValue)
- if err != nil {
- err := errors.New("value is not an integer or out of range")
- conn.Write(serializeRESP(err))
- return
+ newValueStr := strconv.Itoa(currentInt + 1)
+ s.data[key] = Item{Value: newValueStr}
+ return currentInt + 1
+ } else {
+ s.data[key] = Item{Value: "1"}
+ return 1
}
- newValueStr := strconv.Itoa(currentInt + 1)
- db.data[key] = Item{Value: newValueStr}
- conn.Write(serializeRESP(currentInt + 1))
- } else {
- db.data[key] = Item{Value: "1"}
- conn.Write(serializeRESP(1))
- }
+ })
+
+ conn.Write(serializeRESP(result))
}
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'")
@@ -147,30 +146,32 @@ func handleDecr(conn net.Conn, args []string) {
}
key := args[1]
+ result := db.Execute(key, func(s *Shard)interface{}{
+ item, exists := s.data[key]
+ if exists {
+ strValue, ok := item.Value.(string)
+ if !ok {
+ err := errors.New("value is not an integer or out of range")
+ return err
+ }
- item, exists := db.data[key]
- if exists {
- strValue, ok := item.Value.(string)
- if !ok {
- 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 {
+ err := errors.New("value is not an integer or out of range")
+ return err
+ }
- currentInt, err := strconv.Atoi(strValue)
- if err != nil {
- err := errors.New("value is not an integer or out of range")
- conn.Write(serializeRESP(err))
- return
+ newValueStr := strconv.Itoa(currentInt + 1)
+ s.data[key] = Item{Value: newValueStr}
+ return currentInt - 1
+ } else {
+ s.data[key] = Item{Value: "1"}
+ return -1
}
- newValueStr := strconv.Itoa(currentInt - 1)
- db.data[key] = Item{Value:newValueStr}
- conn.Write(serializeRESP(currentInt - 1))
- } else {
- db.data[key] = Item{Value: "-1"}
- conn.Write(serializeRESP(-1))
- }
+ })
+
+ conn.Write(serializeRESP(result))
}
func handlePing(conn net.Conn, args []string) {
@@ -182,6 +183,7 @@ func handlePing(conn net.Conn, args []string) {
conn.Write(serializeRESP(SimpleString("PONG")))
}
+/*
func handleFlushall(conn net.Conn, args []string) {
db.mu.Lock()
defer db.mu.Unlock()