From 72deb0e7c7d3ee5f8bdcef6de6a2fbaaaf189be8 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 10 May 2020 12:57:04 +0800 Subject: [PATCH] Implement LSET command for ledis server (#384) --- Makefile | 12 ++++++------ cmd/cli.go | 1 + server/cmd_list.go | 19 +++++++++++++++++++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 1c03c7f..1a4eda8 100644 --- a/Makefile +++ b/Makefile @@ -18,12 +18,12 @@ PACKAGES ?= $(shell GO111MODULE=on go list -mod=vendor ./... | grep -v /vendor/) all: build build: - go build -mod=vendor -o bin/ledis-server -tags '$(GO_BUILD_TAGS)' cmd/ledis-server/* - go build -mod=vendor -o bin/ledis-cli -tags '$(GO_BUILD_TAGS)' cmd/ledis-cli/* - go build -mod=vendor -o bin/ledis-benchmark -tags '$(GO_BUILD_TAGS)' cmd/ledis-benchmark/* - go build -mod=vendor -o bin/ledis-dump -tags '$(GO_BUILD_TAGS)' cmd/ledis-dump/* - go build -mod=vendor -o bin/ledis-load -tags '$(GO_BUILD_TAGS)' cmd/ledis-load/* - go build -mod=vendor -o bin/ledis-repair -tags '$(GO_BUILD_TAGS)' cmd/ledis-repair/* + go build -mod=vendor -o bin/ledis-server -tags '$(GO_BUILD_TAGS)' cmd/ledis-server/*.go + go build -mod=vendor -o bin/ledis-cli -tags '$(GO_BUILD_TAGS)' cmd/ledis-cli/*.go + go build -mod=vendor -o bin/ledis-benchmark -tags '$(GO_BUILD_TAGS)' cmd/ledis-benchmark/*.go + go build -mod=vendor -o bin/ledis-dump -tags '$(GO_BUILD_TAGS)' cmd/ledis-dump/*.go + go build -mod=vendor -o bin/ledis-load -tags '$(GO_BUILD_TAGS)' cmd/ledis-load/*.go + go build -mod=vendor -o bin/ledis-repair -tags '$(GO_BUILD_TAGS)' cmd/ledis-repair/*.go vet: go vet -mod=vendor -tags '$(GO_BUILD_TAGS)' ./... diff --git a/cmd/cli.go b/cmd/cli.go index ae28c6b..c907ce2 100644 --- a/cmd/cli.go +++ b/cmd/cli.go @@ -74,6 +74,7 @@ var helpCommands = [][]string{ {"LPOP", "key", "List"}, {"LPUSH", "key value [value ...]", "List"}, {"LRANGE", "key start stop", "List"}, + {"LSET", "key index value", "List"}, {"LTTL", "key", "List"}, {"MGET", "key [key ...]", "KV"}, {"MSET", "key value [key value ...]", "KV"}, diff --git a/server/cmd_list.go b/server/cmd_list.go index 61b4957..78caa26 100644 --- a/server/cmd_list.go +++ b/server/cmd_list.go @@ -134,6 +134,24 @@ func lrangeCommand(c *client) error { return nil } +func lsetCommand(c *client) error { + args := c.args + if len(args) != 3 { + return ErrCmdParams + } + + index, err := ledis.StrInt64(args[1], nil) + if err != nil { + return ErrValue + } + + if err := c.db.LSet(args[0], int32(index), args[2]); err != nil { + return err + } + c.resp.writeStatus(OK) + return nil +} + func lclearCommand(c *client) error { args := c.args if len(args) != 1 { @@ -479,6 +497,7 @@ func init() { register("llen", llenCommand) register("lpop", lpopCommand) register("lrange", lrangeCommand) + register("lset", lsetCommand) register("lpush", lpushCommand) register("rpop", rpopCommand) register("rpush", rpushCommand)