mirror of
https://github.com/EchoVault/SugarDB.git
synced 2025-10-16 13:00:40 +08:00
Implemented LSET command
This commit is contained in:
@@ -53,6 +53,9 @@ func (p *plugin) HandleCommand(cmd []string, server interface{}, conn *bufio.Wri
|
|||||||
case c == "lrange":
|
case c == "lrange":
|
||||||
handleLRange(cmd, server.(Server), conn)
|
handleLRange(cmd, server.(Server), conn)
|
||||||
|
|
||||||
|
case c == "lset":
|
||||||
|
handleLSet(cmd, server.(Server), conn)
|
||||||
|
|
||||||
case utils.Contains[string]([]string{"lpush", "lpushx"}, c):
|
case utils.Contains[string]([]string{"lpush", "lpushx"}, c):
|
||||||
handleLPush(cmd, server.(Server), conn)
|
handleLPush(cmd, server.(Server), conn)
|
||||||
|
|
||||||
@@ -211,6 +214,48 @@ func handleLRange(cmd []string, server Server, conn *bufio.Writer) {
|
|||||||
conn.Flush()
|
conn.Flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleLSet(cmd []string, server Server, conn *bufio.Writer) {
|
||||||
|
if len(cmd) != 4 {
|
||||||
|
conn.Write([]byte("-Error wrong number of arguments for LSET command\r\n\n"))
|
||||||
|
conn.Flush()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
server.Lock()
|
||||||
|
|
||||||
|
list, ok := server.GetData(cmd[1]).([]interface{})
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
server.Unlock()
|
||||||
|
conn.Write([]byte("-Error LSET command on non-list item\r\n\n"))
|
||||||
|
conn.Flush()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
index, ok := utils.AdaptType(cmd[2]).(int)
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
server.Unlock()
|
||||||
|
conn.Write([]byte("-Error index must be an integer\r\n\n"))
|
||||||
|
conn.Flush()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !(index >= 0 && index < len(list)) {
|
||||||
|
server.Unlock()
|
||||||
|
conn.Write([]byte("-Error index must be within range\r\n\n"))
|
||||||
|
conn.Flush()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
list[index] = utils.AdaptType(cmd[3])
|
||||||
|
server.SetData(cmd[1], list)
|
||||||
|
server.Unlock()
|
||||||
|
|
||||||
|
conn.Write([]byte(OK))
|
||||||
|
conn.Flush()
|
||||||
|
}
|
||||||
|
|
||||||
func handleLPush(cmd []string, server Server, conn *bufio.Writer) {
|
func handleLPush(cmd []string, server Server, conn *bufio.Writer) {
|
||||||
if len(cmd) < 3 {
|
if len(cmd) < 3 {
|
||||||
conn.Write([]byte(fmt.Sprintf("-Error wrong number of arguments for %s command\r\n\n", strings.ToUpper(cmd[0]))))
|
conn.Write([]byte(fmt.Sprintf("-Error wrong number of arguments for %s command\r\n\n", strings.ToUpper(cmd[0]))))
|
||||||
@@ -345,17 +390,16 @@ func init() {
|
|||||||
"lpop", // (LPOP key) Removes and returns the first element of a list.
|
"lpop", // (LPOP key) Removes and returns the first element of a list.
|
||||||
"llen", // (LLEN key) Return the length of a list.
|
"llen", // (LLEN key) Return the length of a list.
|
||||||
"lrange", // (LRANGE key start end) Return a range of elements between the given indices.
|
"lrange", // (LRANGE key start end) Return a range of elements between the given indices.
|
||||||
"lmove", // (LMOVE key1 key2 LEFT/RIGHT LEFT/RIGHT) Move element from one list to the other specifying left/right for both lists.
|
"lindex", // (LINDEX key index) Gets list element by index.
|
||||||
"lrem", // (LREM key count value) Remove elements from list.
|
|
||||||
"lset", // (LSET key index value) Sets the value of an element in a list by its index.
|
"lset", // (LSET key index value) Sets the value of an element in a list by its index.
|
||||||
"ltrim", // (LTRIM key start end) Trims a list to the specified range.
|
"ltrim", // (LTRIM key start end) Trims a list to the specified range.
|
||||||
"lincr", // (LINCR key index) Increment the list element at the given index by 1.
|
"lrem", // (LREM key count value) Remove elements from list.
|
||||||
"lincrby", // (LINCRBY key index value) Increment the list element at the given index by the given value.
|
"lmove", // (LMOVE key1 key2 LEFT/RIGHT LEFT/RIGHT) Move element from one list to the other specifying left/right for both lists.
|
||||||
"lindex", // (LINDEX key index) Gets list element by index.
|
|
||||||
"rpop", // (RPOP key) Removes and gets the last element in a list.
|
"rpop", // (RPOP key) Removes and gets the last element in a list.
|
||||||
"rpoplpush", // (RPOPLPUSH key1 key2) Removes last element of one list, prepends it to another list and returns it.
|
|
||||||
"rpush", // (RPUSH key value [value2]) Appends one or multiple elements to the end of a list.
|
"rpush", // (RPUSH key value [value2]) Appends one or multiple elements to the end of a list.
|
||||||
"rpushx", // (RPUSHX key value) Appends an element to the end of a list, only if the list exists.
|
"rpushx", // (RPUSHX key value) Appends an element to the end of a list, only if the list exists.
|
||||||
|
"rpoplpush", // (RPOPLPUSH key1 key2) Removes last element of one list, prepends it to another list and returns it.
|
||||||
}
|
}
|
||||||
Plugin.description = "Handle List commands"
|
Plugin.description = "Handle List commands"
|
||||||
}
|
}
|
||||||
|
@@ -4,11 +4,13 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
@@ -111,6 +113,25 @@ func AdaptType(s string) interface{} {
|
|||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IncrBy(num interface{}, by interface{}) (interface{}, error) {
|
||||||
|
if !Contains[string]([]string{"int", "float64"}, reflect.TypeOf(num).String()) {
|
||||||
|
return nil, errors.New("can only increment number")
|
||||||
|
}
|
||||||
|
if !Contains[string]([]string{"int", "float64"}, reflect.TypeOf(by).String()) {
|
||||||
|
return nil, errors.New("can only increment by number")
|
||||||
|
}
|
||||||
|
|
||||||
|
n, _ := num.(float64)
|
||||||
|
b, _ := by.(float64)
|
||||||
|
res := n + b
|
||||||
|
|
||||||
|
if IsInteger(res) {
|
||||||
|
return int(res), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
|
||||||
func ReadMessage(r *bufio.ReadWriter) (message string, err error) {
|
func ReadMessage(r *bufio.ReadWriter) (message string, err error) {
|
||||||
var line [][]byte
|
var line [][]byte
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user