aboutsummaryrefslogtreecommitdiff
path: root/handlers.go
diff options
context:
space:
mode:
authoralex <[email protected]>2026-07-08 20:30:16 +0200
committeralex <[email protected]>2026-07-08 20:30:16 +0200
commit513e5e7961ecc9fe8155811d6cbb6a6c77e0638d (patch)
tree49a3aea7e17040b95fd54d322db8a5284933ca1a /handlers.go
parentb523e77159b89cdb22a8e6e7f9d0046cd4269443 (diff)
downloadredis-clone-513e5e7961ecc9fe8155811d6cbb6a6c77e0638d.tar.xz
redis-clone-513e5e7961ecc9fe8155811d6cbb6a6c77e0638d.zip
refactored EXISTS,SET,GET to support multiple shards
Diffstat (limited to 'handlers.go')
-rw-r--r--handlers.go75
1 files changed, 37 insertions, 38 deletions
diff --git a/handlers.go b/handlers.go
index 305d526..aefea85 100644
--- a/handlers.go
+++ b/handlers.go
@@ -3,50 +3,42 @@ package main
import (
"errors"
"net"
- "path/filepath"
- "sort"
- "strconv"
"time"
)
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]
- key := args[1]
+ result := db.Execute(key, func(s *Shard) interface{}{
+ item, exists := s.data[key]
+ if !exists {
+ return nil
+ }
- item, exists := db.data[key]
- if !exists {
- conn.Write(serializeRESP(nil))
- return
- }
+ if item.ExpiresAt != nil && time.Now().After(*item.ExpiresAt) {
+ delete(s.data, key)
+ return nil
+ }
- if item.ExpiresAt != nil && time.Now().After(*item.ExpiresAt) {
- delete(db.data, key)
- conn.Write(serializeRESP(nil))
- return
- }
+ strValue, ok := item.Value.(string)
+ if !ok {
+ err := errors.New("WRONGTYPE Operation against a key holding the wrong kind of value")
+ return err
+ }
+ return strValue
+ })
- strValue, ok := item.Value.(string)
- if !ok {
- err := errors.New("WRONGTYPE Operation against a key holding the wrong kind of value")
- conn.Write(serializeRESP(err))
- return
- }
- conn.Write(serializeRESP(strValue))
+ conn.Write(serializeRESP(result))
}
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))
@@ -54,13 +46,16 @@ func handleExists(conn net.Conn, args []string) {
}
key := args[1]
+ result := db.Execute(key, func(s *Shard) interface{}{
+ _, exists := s.data[key]
+ if exists {
+ return 1
+ } else {
+ return 0
+ }
+ })
- _, exists := db.data[key]
- if exists {
- conn.Write(serializeRESP(1))
- } else {
- conn.Write(serializeRESP(0))
- }
+ conn.Write(serializeRESP(result))
}
func handleSet(conn net.Conn, args []string) {
@@ -74,12 +69,15 @@ func handleSet(conn net.Conn, args []string) {
}
key := args[1]
- item := Item{Value: args[2]}
+ result := db.Execute(key, func(s *Shard) interface{}{
+ item := Item{Value: args[2]}
+ s.data[key] = item
+ return SimpleString("OK")
+ })
- db.data[key] = item
- conn.Write(serializeRESP(SimpleString("OK")))
+ conn.Write(serializeRESP(result))
}
-
+/*
func handleDel(conn net.Conn, args []string) {
db.mu.Lock()
defer db.mu.Unlock()
@@ -730,4 +728,5 @@ func handleLrem(conn net.Conn, args []string){
item.Value = list
db.data[key] = item
conn.Write(serializeRESP(counter))
-} \ No newline at end of file
+}
+*/ \ No newline at end of file