package main
import (
"errors"
"net"
"strconv"
"time"
)
func handleGet(conn net.Conn, args []string) {
if len(args) != 2 {
err := errors.New("wrong number of arguments for 'GET'")
conn.Write(serializeRESP(err))
return
}
key := args[1]
result := db.ExecuteRead(key, func(s *Shard) interface{}{
item, exists := s.data[key]
if !exists {
return nil
}
if item.ExpiresAt != nil && time.Now().After(*item.ExpiresAt) {
delete(s.data, key)
return nil
}
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
})
conn.Write(serializeRESP(result))
}
func handleExists(conn net.Conn, args []string) {
if len(args) != 2 {
err := errors.New("wrong number of arguments for 'EXISTS'")
conn.Write(serializeRESP(err))
return
}
key := args[1]
result := db.ExecuteRead(key, func(s *Shard) interface{}{
_, exists := s.data[key]
if exists {
return 1
} else {
return 0
}
})
conn.Write(serializeRESP(result))
}
func handleSet(conn net.Conn, args []string) {
if len(args) != 3 {
err := errors.New("wrong number of arguments for 'SET'")
conn.Write(serializeRESP(err))
return
}
key := args[1]
result := db.Execute(key, func(s *Shard) interface{}{
item := Item{Value: args[2]}
s.data[key] = item
return SimpleString("OK")
})
conn.Write(serializeRESP(result))
}
func handleDel(conn net.Conn, args []string) {
if len(args) < 2 {
err := errors.New("wrong number of arguments for 'DEL'")
conn.Write(serializeRESP(err))
return
}
keys := args[1:]
deletedKeys := db.ExecuteMulti(keys, func(shards []*Shard) interface{}{
count := 0
for _, key := range keys{
shard := db.getShard(key)
_, exists := shard.data[key]
if exists {
delete(shard.data, key)
count += 1
}
}
return count
})
conn.Write(serializeRESP(deletedKeys))
}
func handleIncr(conn net.Conn, args []string) {
if len(args) != 2 {
err := errors.New("wrong number of arguments for 'INCR'")
conn.Write(serializeRESP(err))
return
}
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
}
currentInt, err := strconv.Atoi(strValue)
if err != nil {
err := errors.New("value is not an integer or out of range")
return err
}
newValueStr := strconv.Itoa(currentInt + 1)
s.data[key] = Item{Value: newValueStr}
return currentInt + 1
} else {
s.data[key] = Item{Value: "1"}
return 1
}
})
conn.Write(serializeRESP(result))
}
func handleDecr(conn net.Conn, args []string) {
if len(args) != 2 {
err := errors.New("wrong number of arguments for 'DECR'")
conn.Write(serializeRESP(err))
return
}
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
}
currentInt, err := strconv.Atoi(strValue)
if err != nil {
err := errors.New("value is not an integer or out of range")
return err
}
newValueStr := strconv.Itoa(currentInt + 1)
s.data[key] = Item{Value: newValueStr}
return currentInt - 1
} else {
s.data[key] = Item{Value: "1"}
return -1
}
})
conn.Write(serializeRESP(result))
}
func handlePing(conn net.Conn, args []string) {
if