aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.go66
1 files changed, 66 insertions, 0 deletions
diff --git a/main.go b/main.go
index 510a0f0..2fd1bd3 100644
--- a/main.go
+++ b/main.go
@@ -238,6 +238,72 @@ func handleLrange(conn net.Conn, key string, start, stop int) {
conn.Write(serializeRESP(resultList))
}
+func handleLpop(conn net.Conn, key string){
+ db.mu.Lock()
+ defer db.mu.Unlock()
+
+ value, exists := db.data[key]
+ if !exists {
+ conn.Write(serializeRESP(nil))
+ return
+ }
+
+ typedList, ok := value.([]string)
+ if !ok {
+ err := errors.New("WRONGTYPE Operation against a key holding the wrong kind of value")
+ conn.Write(serializeRESP(err))
+ return
+ }
+
+ if len(typedList) == 0 {
+ conn.Write(serializeRESP(nil))
+ return
+ }
+
+ item := typedList[0]
+ newList := typedList[1:]
+ if len(newList) <= 0 {
+ delete(db.data, key)
+ }else{
+ db.data[key] = newList
+ }
+ conn.Write(serializeRESP(item))
+
+}
+
+func handleRpop(conn net.Conn, key string){
+ db.mu.Lock()
+ defer db.mu.Unlock()
+
+ value, exists := db.data[key]
+ if !exists {
+ conn.Write(serializeRESP(nil))
+ return
+ }
+
+ typedList, ok := value.([]string)
+ if !ok {
+ err := errors.New("WRONGTYPE Operation against a key holding the wrong kind of value")
+ conn.Write(serializeRESP(err))
+ return
+ }
+
+ if len(typedList) == 0 {
+ conn.Write(serializeRESP(nil))
+ return
+ }
+
+ item := typedList[len(typedList)-1]
+ newList := typedList[:len(typedList)-1]
+ if len(newList) <= 0 {
+ delete(db.data, key)
+ }else{
+ db.data[key] = newList
+ }
+ conn.Write(serializeRESP(item))
+
+}
+
func serializeRESP(v any) []byte {
switch val := v.(type) {
case string: