diff options
| author | alex <[email protected]> | 2026-07-08 19:25:30 +0200 |
|---|---|---|
| committer | alex <[email protected]> | 2026-07-08 19:25:30 +0200 |
| commit | b523e77159b89cdb22a8e6e7f9d0046cd4269443 (patch) | |
| tree | 11c12d8d9d4e1409840a8a8e9d71a1b147000623 /handlers.go | |
| parent | 23eea730585ece03deadd28f46f3c5e46f155048 (diff) | |
| download | redis-clone-b523e77159b89cdb22a8e6e7f9d0046cd4269443.tar.xz redis-clone-b523e77159b89cdb22a8e6e7f9d0046cd4269443.zip | |
added LREM command
Diffstat (limited to 'handlers.go')
| -rw-r--r-- | handlers.go | 73 |
1 files changed, 72 insertions, 1 deletions
diff --git a/handlers.go b/handlers.go index 13b2d11..305d526 100644 --- a/handlers.go +++ b/handlers.go @@ -633,7 +633,7 @@ func handleKeys(conn net.Conn, args []string) { if len(args) == 3 { val, err := strconv.Atoi(args[2]) if err != nil { - conn.Write(serializeRESP(errors.New("ERR value is not an integer"))) + conn.Write(serializeRESP(errors.New("value is not an integer"))) return } limit = val @@ -659,4 +659,75 @@ func handleKeys(conn net.Conn, args []string) { db.mu.Unlock() conn.Write(serializeRESP(matches)) +} +func handleLrem(conn net.Conn, args []string){ + + if len(args) != 4 { + conn.Write(serializeRESP(errors.New("wrong number of arguments for 'LREM'"))) + return + } + db.mu.Lock() + defer db.mu.Unlock() + + key := args[1] + count,err := strconv.Atoi(args[2]) + toBeRemoved := args[3] + + if err != nil { + conn.Write(serializeRESP(errors.New("value is not an integer"))) + return + } + item, exists:= db.data[key] + if !exists { + + } + list, ok := item.Value.([]string) + if !ok { + conn.Write(serializeRESP(errors.New("WRONGTYPE Operation against a key holding the wrong kind of value"))) + return + } + counter := 0 + absCount := count + if count < 0 { + absCount = -count + } + switch{ + case count == 0: + newList := []string{} + for i, value := range list { + if value == toBeRemoved{ + newList = append(list[:i], list[i+1:]...) + counter += 1 + } + } + list = newList + case count > 0: + newList := []string{} + for i, value := range list { + if counter == absCount{ + break + } + if value == toBeRemoved{ + newList = append(list[:i], list[i+1:]...) + counter += 1 + } + } + list = newList + case count < 0: + newList := []string{} + for i := len(list) - 1; i >= 0; i-- { + value := list[i] + if counter == absCount{ + break + } + if value == toBeRemoved{ + newList = append(list[:i], list[i+1:]...) + counter += 1 + } + } + list = newList + } + item.Value = list + db.data[key] = item + conn.Write(serializeRESP(counter)) }
\ No newline at end of file |
