From 1ee5a02c6c392c94cbf9be22acd7ed2cda751394 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 10 May 2020 18:17:39 +0800 Subject: [PATCH] Fix some lint error (#385) * Fix some lint error * Fix misspell * Improve make lint --- Makefile | 9 +- README.md | 4 +- cmd/benchmark.go | 2 +- cmd/cli.go | 5 +- cmd/dump.go | 2 +- cmd/ledis-benchmark/main.go | 2 +- cmd/ledis-cli/main.go | 2 +- cmd/ledis-dump/main.go | 2 +- cmd/ledis-load/main.go | 2 +- cmd/ledis-repair/main.go | 2 +- cmd/ledis-server/main.go | 4 +- cmd/ledis/main.go | 12 ++- cmd/load.go | 2 +- cmd/repair.go | 2 +- cmd/server.go | 3 +- ledis/scan.go | 4 +- ledis/t_set.go | 2 +- rpl/file_io.go | 6 +- rpl/file_table.go | 4 +- rpl/rpl.go | 8 +- server/cmd_hash.go | 129 +++++++++++-------------- server/cmd_kv.go | 169 ++++++++++++++------------------- server/cmd_list.go | 127 +++++++++++-------------- server/cmd_migrate.go | 75 +++++++-------- server/cmd_migrate_test.go | 8 +- server/cmd_replication.go | 45 +++++---- server/cmd_replication_test.go | 8 +- server/cmd_server.go | 55 +++++------ server/cmd_set.go | 91 ++++++++---------- server/cmd_sort.go | 6 +- server/cmd_zset.go | 62 ++++++------ server/info.go | 6 +- server/replication.go | 21 ++-- store/db.go | 51 +++++----- store/iterator.go | 18 ++-- store/snapshot.go | 15 ++- store/store.go | 12 +-- store/writebatch.go | 2 +- 38 files changed, 440 insertions(+), 539 deletions(-) diff --git a/Makefile b/Makefile index 5c3746a..991ad1a 100644 --- a/Makefile +++ b/Makefile @@ -32,6 +32,13 @@ build-commands: go build -mod=vendor -o $(DIST)/ledis-load -tags '$(GO_BUILD_TAGS)' -ldflags '-s -w $(LDFLAGS)' cmd/ledis-load/*.go go build -mod=vendor -o $(DIST)/ledis-repair -tags '$(GO_BUILD_TAGS)' -ldflags '-s -w $(LDFLAGS)' cmd/ledis-repair/*.go +.PHONY: lint +lint: + @hash golint > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ + cd /tmp && GO111MODULE=on go get -u -v golang.org/x/lint/golint; \ + fi + golint $(PACKAGES) + vet: go vet -mod=vendor -tags '$(GO_BUILD_TAGS)' ./... @@ -39,7 +46,7 @@ test: go test -mod=vendor --race -tags '$(GO_BUILD_TAGS)' -cover -coverprofile coverage.out -timeout 10m $(PACKAGES) clean: - go clean -i ./... + go clean -i $(PACKAGES) fmt: gofmt -w -s . 2>&1 | grep -vE 'vendor' | awk '{print} END{if(NR>0) {exit 1}}' diff --git a/README.md b/README.md index e4741d1..9a396b9 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # LedisDB -[![Build Status](https://travis-ci.org/ledisdb/ledisdb.svg?branch=develop)](https://travis-ci.org/ledisdb/ledisdb) [![codecov](https://codecov.io/gh/ledisdb/ledisdb/branch/master/graph/badge.svg)](https://codecov.io/gh/ledisdb/ledisdb) +[![Build Status](https://travis-ci.org/ledisdb/ledisdb.svg?branch=develop)](https://travis-ci.org/ledisdb/ledisdb) [![codecov](https://codecov.io/gh/ledisdb/ledisdb/branch/master/graph/badge.svg)](https://codecov.io/gh/ledisdb/ledisdb) [![goreportcard](https://goreportcard.com/badge/github.com/ledisdb/ledisdb)](https://goreportcard.com/report/github.com/ledisdb/ledisdb) -Ledisdb is a high-performance NoSQL database, similar to Redis, written in [Go](http://golang.org/). It supports many data structures including kv, list, hash, zset, set. +Ledisdb is a high-performance NoSQL database library and server written in [Go](http://golang.org). It's similar to Redis but store data in disk. It supports many data structures including kv, list, hash, zset, set. LedisDB now supports multiple different databases as backends. diff --git a/cmd/benchmark.go b/cmd/benchmark.go index 088cb79..aeaca42 100644 --- a/cmd/benchmark.go +++ b/cmd/benchmark.go @@ -250,7 +250,7 @@ var round = flag.Int("r", 1, "benchmark round number") var valueSize = flag.Int("vsize", 100, "kv value size") var tests = flag.String("t", "set,get,randget,del,lpush,lrange,lpop,hset,hget,hdel,zadd,zincr,zrange,zrevrange,zdel", "only run the comma separated list of tests") -func CmdBenchmark() { +func Benchmark() { benchIP = flag.String("ip", "127.0.0.1", "redis/ledis/ssdb server ip") number = flag.Int("n", 1000, "request number") benchPort = flag.Int("port", 6380, "redis/ledis/ssdb server port") diff --git a/cmd/cli.go b/cmd/cli.go index c907ce2..1a310e0 100644 --- a/cmd/cli.go +++ b/cmd/cli.go @@ -1,4 +1,5 @@ -//This file was generated by .tools/generate_commands.py on Sat Oct 28 2017 18:15:49 -0500 +// Package cmd +// This file was generated by .tools/generate_commands.py on Sat Oct 28 2017 18:15:49 -0500 package cmd import ( @@ -164,7 +165,7 @@ var ( historyPath = "/tmp/ledis-cli" ) -func CmdCli() { +func Cli() { var dbn = flag.Int("n", 0, "ledisdb database number(default 0)") flag.Parse() diff --git a/cmd/dump.go b/cmd/dump.go index 97bccb0..b27b313 100644 --- a/cmd/dump.go +++ b/cmd/dump.go @@ -8,7 +8,7 @@ import ( "github.com/siddontang/goredis" ) -func CmdDump() { +func Dump() { var host = flag.String("host", "127.0.0.1", "ledis server host") var dumpPort = flag.Int("port", 6380, "ledis server port") var sock = flag.String("sock", "", "ledis unix socket domain") diff --git a/cmd/ledis-benchmark/main.go b/cmd/ledis-benchmark/main.go index c29a656..548f05c 100644 --- a/cmd/ledis-benchmark/main.go +++ b/cmd/ledis-benchmark/main.go @@ -18,5 +18,5 @@ func main() { } fmt.Println() - cmd.CmdBenchmark() + cmd.Benchmark() } diff --git a/cmd/ledis-cli/main.go b/cmd/ledis-cli/main.go index 6f5f903..cfbea9e 100644 --- a/cmd/ledis-cli/main.go +++ b/cmd/ledis-cli/main.go @@ -18,5 +18,5 @@ func main() { } fmt.Println() - cmd.CmdCli() + cmd.Cli() } diff --git a/cmd/ledis-dump/main.go b/cmd/ledis-dump/main.go index 111f089..5f608a5 100644 --- a/cmd/ledis-dump/main.go +++ b/cmd/ledis-dump/main.go @@ -18,5 +18,5 @@ func main() { } fmt.Println() - cmd.CmdDump() + cmd.Dump() } diff --git a/cmd/ledis-load/main.go b/cmd/ledis-load/main.go index fb92c79..28128ea 100644 --- a/cmd/ledis-load/main.go +++ b/cmd/ledis-load/main.go @@ -18,5 +18,5 @@ func main() { } fmt.Println() - cmd.CmdLoad() + cmd.Load() } diff --git a/cmd/ledis-repair/main.go b/cmd/ledis-repair/main.go index d28598c..6963d31 100644 --- a/cmd/ledis-repair/main.go +++ b/cmd/ledis-repair/main.go @@ -18,5 +18,5 @@ func main() { } fmt.Println() - cmd.CmdRepair() + cmd.Repair() } diff --git a/cmd/ledis-server/main.go b/cmd/ledis-server/main.go index cc4c5eb..389cfd1 100644 --- a/cmd/ledis-server/main.go +++ b/cmd/ledis-server/main.go @@ -3,6 +3,8 @@ package main import ( "fmt" + _ "net/http/pprof" + "github.com/ledisdb/ledisdb/cmd" ) @@ -18,5 +20,5 @@ func main() { } fmt.Println() - cmd.CmdServer() + cmd.Server() } diff --git a/cmd/ledis/main.go b/cmd/ledis/main.go index aade6d9..5ff764d 100644 --- a/cmd/ledis/main.go +++ b/cmd/ledis/main.go @@ -4,6 +4,8 @@ import ( "fmt" "os" + _ "net/http/pprof" + "github.com/ledisdb/ledisdb/cmd" ) @@ -46,18 +48,18 @@ func main() { switch subCmd { case "repair": - cmd.CmdRepair() + cmd.Repair() case "benchmark": - cmd.CmdBenchmark() + cmd.Benchmark() case "cli": - cmd.CmdCli() + cmd.Cli() case "dump": - cmd.CmdDump() + cmd.Dump() case "help": printSubCmds() case "server": fallthrough default: - cmd.CmdServer() + cmd.Server() } } diff --git a/cmd/load.go b/cmd/load.go index dc50925..ab17e8e 100644 --- a/cmd/load.go +++ b/cmd/load.go @@ -7,7 +7,7 @@ import ( "github.com/ledisdb/ledisdb/ledis" ) -func CmdLoad() { +func Load() { var configPath = flag.String("config", "", "ledisdb config file") var dumpPath = flag.String("dump_file", "", "ledisdb dump file") diff --git a/cmd/repair.go b/cmd/repair.go index b2cd760..83a7f5b 100644 --- a/cmd/repair.go +++ b/cmd/repair.go @@ -7,7 +7,7 @@ import ( "github.com/ledisdb/ledisdb/store" ) -func CmdRepair() { +func Repair() { var fileName = flag.String("config", "", "ledisdb config file") flag.Parse() diff --git a/cmd/server.go b/cmd/server.go index 0d1321f..081394b 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -5,7 +5,6 @@ import ( "fmt" "log" "net/http" - _ "net/http/pprof" "os" "os/signal" "runtime" @@ -28,7 +27,7 @@ var rplSync = flag.Bool("rpl_sync", false, "enable sync replication or not") var ttlCheck = flag.Int("ttl_check", 0, "TTL check interval") var databases = flag.Int("databases", 0, "ledisdb maximum database number") -func CmdServer() { +func Server() { var configFile = flag.String("config", "", "ledisdb config file") runtime.GOMAXPROCS(runtime.NumCPU()) diff --git a/ledis/scan.go b/ledis/scan.go index f4014e5..798ab80 100644 --- a/ledis/scan.go +++ b/ledis/scan.go @@ -197,7 +197,7 @@ func (db *DB) decodeScanKey(storeDataType byte, ek []byte) (key []byte, err erro return } -// for specail data scan +// for special data scan func (db *DB) buildDataScanKeyRange(storeDataType byte, key []byte, cursor []byte, reverse bool) (minKey []byte, maxKey []byte, err error) { if !reverse { @@ -232,7 +232,7 @@ func (db *DB) encodeDataScanMaxKey(storeDataType byte, key []byte, cursor []byte return nil, err } - // here, the last byte is the start seperator, set it to stop seperator + // here, the last byte is the start separator, set it to stop separator k[len(k)-1] = k[len(k)-1] + 1 return k, nil } diff --git a/ledis/t_set.go b/ledis/t_set.go index 4ea93a5..01c1dc1 100644 --- a/ledis/t_set.go +++ b/ledis/t_set.go @@ -596,7 +596,7 @@ func (db *DB) SMclear(keys ...[]byte) (int64, error) { return int64(len(keys)), err } -// SExpire expries the set. +// SExpire expires the set. func (db *DB) SExpire(key []byte, duration int64) (int64, error) { if duration <= 0 { return 0, errExpireValue diff --git a/rpl/file_io.go b/rpl/file_io.go index 6eac878..52218a1 100644 --- a/rpl/file_io.go +++ b/rpl/file_io.go @@ -349,15 +349,13 @@ func (m *mmapReadFile) Name() string { func newWriteFile(useMmap bool, name string, size int64) (writeFile, error) { if useMmap { return newMmapWriteFile(name, size) - } else { - return newRawWriteFile(name, size) } + return newRawWriteFile(name, size) } func newReadFile(useMmap bool, name string) (readFile, error) { if useMmap { return newMmapReadFile(name) - } else { - return newRawReadFile(name) } + return newRawReadFile(name) } diff --git a/rpl/file_table.go b/rpl/file_table.go index 9658102..3a9b53b 100644 --- a/rpl/file_table.go +++ b/rpl/file_table.go @@ -211,8 +211,8 @@ func (t *tableReader) repair() error { meta, err = newWriteFile(false, fmtTableMetaName(t.base, t.index), int64(defaultLogNumInFile*4)) var l Log - var pos int64 = 0 - var nextPos int64 = 0 + var pos int64 + var nextPos int64 b := make([]byte, 4) t.first = 0 diff --git a/rpl/rpl.go b/rpl/rpl.go index ce44711..a12e099 100644 --- a/rpl/rpl.go +++ b/rpl/rpl.go @@ -126,10 +126,10 @@ func (r *Replication) Log(data []byte) (*Log, error) { return nil, err } - commitId := r.commitID - if lastID < commitId { - lastID = commitId - } else if lastID > commitId { + commitID := r.commitID + if lastID < commitID { + lastID = commitID + } else if lastID > commitID { r.m.Unlock() return nil, ErrCommitIDBehind } diff --git a/server/cmd_hash.go b/server/cmd_hash.go index 45491e5..0c5dc31 100644 --- a/server/cmd_hash.go +++ b/server/cmd_hash.go @@ -10,12 +10,11 @@ func hsetCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.HSet(args[0], args[1], args[2]); err != nil { + n, err := c.db.HSet(args[0], args[1], args[2]) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -25,12 +24,11 @@ func hgetCommand(c *client) error { return ErrCmdParams } - if v, err := c.db.HGet(args[0], args[1]); err != nil { + v, err := c.db.HGet(args[0], args[1]) + if err != nil { return err - } else { - c.resp.writeBulk(v) } - + c.resp.writeBulk(v) return nil } @@ -41,15 +39,15 @@ func hexistsCommand(c *client) error { } var n int64 = 1 - if v, err := c.db.HGet(args[0], args[1]); err != nil { + v, err := c.db.HGet(args[0], args[1]) + if err != nil { return err - } else { - if v == nil { - n = 0 - } - - c.resp.writeInteger(n) } + if v == nil { + n = 0 + } + + c.resp.writeInteger(n) return nil } @@ -59,12 +57,11 @@ func hdelCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.HDel(args[0], args[1:]...); err != nil { + n, err := c.db.HDel(args[0], args[1:]...) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -74,12 +71,11 @@ func hlenCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.HLen(args[0]); err != nil { + n, err := c.db.HLen(args[0]) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -94,12 +90,11 @@ func hincrbyCommand(c *client) error { return ErrValue } - var n int64 - if n, err = c.db.HIncrBy(args[0], args[1], delta); err != nil { + n, err := c.db.HIncrBy(args[0], args[1], delta) + if err != nil { return err - } else { - c.resp.writeInteger(n) } + c.resp.writeInteger(n) return nil } @@ -125,10 +120,8 @@ func hmsetCommand(c *client) error { if err := c.db.HMset(key, kvs...); err != nil { return err - } else { - c.resp.writeStatus(OK) } - + c.resp.writeStatus(OK) return nil } @@ -138,12 +131,11 @@ func hmgetCommand(c *client) error { return ErrCmdParams } - if v, err := c.db.HMget(args[0], args[1:]...); err != nil { + v, err := c.db.HMget(args[0], args[1:]...) + if err != nil { return err - } else { - c.resp.writeSliceArray(v) } - + c.resp.writeSliceArray(v) return nil } @@ -153,12 +145,11 @@ func hgetallCommand(c *client) error { return ErrCmdParams } - if v, err := c.db.HGetAll(args[0]); err != nil { + v, err := c.db.HGetAll(args[0]) + if err != nil { return err - } else { - c.resp.writeFVPairArray(v) } - + c.resp.writeFVPairArray(v) return nil } @@ -168,12 +159,11 @@ func hkeysCommand(c *client) error { return ErrCmdParams } - if v, err := c.db.HKeys(args[0]); err != nil { + v, err := c.db.HKeys(args[0]) + if err != nil { return err - } else { - c.resp.writeSliceArray(v) } - + c.resp.writeSliceArray(v) return nil } @@ -183,12 +173,11 @@ func hvalsCommand(c *client) error { return ErrCmdParams } - if v, err := c.db.HValues(args[0]); err != nil { + v, err := c.db.HValues(args[0]) + if err != nil { return err - } else { - c.resp.writeSliceArray(v) } - + c.resp.writeSliceArray(v) return nil } @@ -198,12 +187,11 @@ func hclearCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.HClear(args[0]); err != nil { + n, err := c.db.HClear(args[0]) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -213,12 +201,11 @@ func hmclearCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.HMclear(args...); err != nil { + n, err := c.db.HMclear(args...) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -233,12 +220,11 @@ func hexpireCommand(c *client) error { return ErrValue } - if v, err := c.db.HExpire(args[0], duration); err != nil { + v, err := c.db.HExpire(args[0], duration) + if err != nil { return err - } else { - c.resp.writeInteger(v) } - + c.resp.writeInteger(v) return nil } @@ -253,12 +239,11 @@ func hexpireAtCommand(c *client) error { return ErrValue } - if v, err := c.db.HExpireAt(args[0], when); err != nil { + v, err := c.db.HExpireAt(args[0], when) + if err != nil { return err - } else { - c.resp.writeInteger(v) } - + c.resp.writeInteger(v) return nil } @@ -268,12 +253,11 @@ func httlCommand(c *client) error { return ErrCmdParams } - if v, err := c.db.HTTL(args[0]); err != nil { + v, err := c.db.HTTL(args[0]) + if err != nil { return err - } else { - c.resp.writeInteger(v) } - + c.resp.writeInteger(v) return nil } @@ -283,12 +267,11 @@ func hpersistCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.HPersist(args[0]); err != nil { + n, err := c.db.HPersist(args[0]) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -297,11 +280,11 @@ func hkeyexistsCommand(c *client) error { if len(args) != 1 { return ErrCmdParams } - if n, err := c.db.HKeyExists(args[0]); err != nil { + n, err := c.db.HKeyExists(args[0]) + if err != nil { return err - } else { - c.resp.writeInteger(n) } + c.resp.writeInteger(n) return nil } diff --git a/server/cmd_kv.go b/server/cmd_kv.go index 3b5dcb3..365d800 100644 --- a/server/cmd_kv.go +++ b/server/cmd_kv.go @@ -28,13 +28,11 @@ func getCommand(c *client) error { if v, err := c.db.GetSlice(args[0]); err != nil { return err + } else if v == nil { + c.resp.writeBulk(nil) } else { - if v == nil { - c.resp.writeBulk(nil) - } else { - c.resp.writeBulk(v.Data()) - v.Free() - } + c.resp.writeBulk(v.Data()) + v.Free() } return nil } @@ -47,10 +45,8 @@ func setCommand(c *client) error { if err := c.db.Set(args[0], args[1]); err != nil { return err - } else { - c.resp.writeStatus(OK) } - + c.resp.writeStatus(OK) return nil } @@ -60,12 +56,11 @@ func getsetCommand(c *client) error { return ErrCmdParams } - if v, err := c.db.GetSet(args[0], args[1]); err != nil { + v, err := c.db.GetSet(args[0], args[1]) + if err != nil { return err - } else { - c.resp.writeBulk(v) } - + c.resp.writeBulk(v) return nil } @@ -75,12 +70,11 @@ func setnxCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.SetNX(args[0], args[1]); err != nil { + n, err := c.db.SetNX(args[0], args[1]) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -97,10 +91,8 @@ func setexCommand(c *client) error { if err := c.db.SetEX(args[0], sec, args[2]); err != nil { return err - } else { - c.resp.writeStatus(OK) } - + c.resp.writeStatus(OK) return nil } @@ -110,12 +102,11 @@ func existsCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.Exists(args[0]); err != nil { + n, err := c.db.Exists(args[0]) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -125,12 +116,11 @@ func incrCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.Incr(c.args[0]); err != nil { + n, err := c.db.Incr(c.args[0]) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -140,12 +130,11 @@ func decrCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.Decr(c.args[0]); err != nil { + n, err := c.db.Decr(c.args[0]) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -160,12 +149,11 @@ func incrbyCommand(c *client) error { return ErrValue } - if n, err := c.db.IncrBy(c.args[0], delta); err != nil { + n, err := c.db.IncrBy(c.args[0], delta) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -180,12 +168,11 @@ func decrbyCommand(c *client) error { return ErrValue } - if n, err := c.db.DecrBy(c.args[0], delta); err != nil { + n, err := c.db.DecrBy(c.args[0], delta) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -195,12 +182,11 @@ func delCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.Del(args...); err != nil { + n, err := c.db.Del(args...) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -218,10 +204,8 @@ func msetCommand(c *client) error { if err := c.db.MSet(kvs...); err != nil { return err - } else { - c.resp.writeStatus(OK) } - + c.resp.writeStatus(OK) return nil } @@ -235,12 +219,11 @@ func mgetCommand(c *client) error { return ErrCmdParams } - if v, err := c.db.MGet(args...); err != nil { + v, err := c.db.MGet(args...) + if err != nil { return err - } else { - c.resp.writeSliceArray(v) } - + c.resp.writeSliceArray(v) return nil } @@ -255,12 +238,11 @@ func expireCommand(c *client) error { return ErrValue } - if v, err := c.db.Expire(args[0], duration); err != nil { + v, err := c.db.Expire(args[0], duration) + if err != nil { return err - } else { - c.resp.writeInteger(v) } - + c.resp.writeInteger(v) return nil } @@ -275,12 +257,11 @@ func expireAtCommand(c *client) error { return ErrValue } - if v, err := c.db.ExpireAt(args[0], when); err != nil { + v, err := c.db.ExpireAt(args[0], when) + if err != nil { return err - } else { - c.resp.writeInteger(v) } - + c.resp.writeInteger(v) return nil } @@ -290,12 +271,11 @@ func ttlCommand(c *client) error { return ErrCmdParams } - if v, err := c.db.TTL(args[0]); err != nil { + v, err := c.db.TTL(args[0]) + if err != nil { return err - } else { - c.resp.writeInteger(v) } - + c.resp.writeInteger(v) return nil } @@ -305,12 +285,11 @@ func persistCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.Persist(args[0]); err != nil { + n, err := c.db.Persist(args[0]) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -320,11 +299,11 @@ func appendCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.Append(args[0], args[1]); err != nil { + n, err := c.db.Append(args[0], args[1]) + if err != nil { return err - } else { - c.resp.writeInteger(n) } + c.resp.writeInteger(n) return nil } @@ -345,12 +324,11 @@ func getrangeCommand(c *client) error { return err } - if v, err := c.db.GetRange(key, start, end); err != nil { + v, err := c.db.GetRange(key, start, end) + if err != nil { return err - } else { - c.resp.writeBulk(v) } - + c.resp.writeBulk(v) return nil } @@ -369,11 +347,11 @@ func setrangeCommand(c *client) error { value := args[2] - if n, err := c.db.SetRange(key, offset, value); err != nil { + n, err := c.db.SetRange(key, offset, value) + if err != nil { return err - } else { - c.resp.writeInteger(n) } + c.resp.writeInteger(n) return nil } @@ -382,11 +360,11 @@ func strlenCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.StrLen(c.args[0]); err != nil { + n, err := c.db.StrLen(c.args[0]) + if err != nil { return err - } else { - c.resp.writeInteger(n) } + c.resp.writeInteger(n) return nil } @@ -419,11 +397,11 @@ func bitcountCommand(c *client) error { return err } - if n, err := c.db.BitCount(key, start, end); err != nil { + n, err := c.db.BitCount(key, start, end) + if err != nil { return err - } else { - c.resp.writeInteger(n) } + c.resp.writeInteger(n) return nil } @@ -437,12 +415,11 @@ func bitopCommand(c *client) error { destKey := args[1] srcKeys := args[2:] - if n, err := c.db.BitOP(op, destKey, srcKeys...); err != nil { + n, err := c.db.BitOP(op, destKey, srcKeys...) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -462,11 +439,11 @@ func bitposCommand(c *client) error { return err } - if n, err := c.db.BitPos(key, bit, start, end); err != nil { + n, err := c.db.BitPos(key, bit, start, end) + if err != nil { return err - } else { - c.resp.writeInteger(n) } + c.resp.writeInteger(n) return nil } @@ -482,11 +459,11 @@ func getbitCommand(c *client) error { return err } - if n, err := c.db.GetBit(key, offset); err != nil { + n, err := c.db.GetBit(key, offset) + if err != nil { return err - } else { - c.resp.writeInteger(n) } + c.resp.writeInteger(n) return nil } @@ -507,11 +484,11 @@ func setbitCommand(c *client) error { return err } - if n, err := c.db.SetBit(key, offset, value); err != nil { + n, err := c.db.SetBit(key, offset, value) + if err != nil { return err - } else { - c.resp.writeInteger(n) } + c.resp.writeInteger(n) return nil } diff --git a/server/cmd_list.go b/server/cmd_list.go index 78caa26..4553da7 100644 --- a/server/cmd_list.go +++ b/server/cmd_list.go @@ -16,12 +16,11 @@ func lpushCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.LPush(args[0], args[1:]...); err != nil { + n, err := c.db.LPush(args[0], args[1:]...) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -31,12 +30,11 @@ func rpushCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.RPush(args[0], args[1:]...); err != nil { + n, err := c.db.RPush(args[0], args[1:]...) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -46,12 +44,11 @@ func lpopCommand(c *client) error { return ErrCmdParams } - if v, err := c.db.LPop(args[0]); err != nil { + v, err := c.db.LPop(args[0]) + if err != nil { return err - } else { - c.resp.writeBulk(v) } - + c.resp.writeBulk(v) return nil } @@ -61,12 +58,11 @@ func rpopCommand(c *client) error { return ErrCmdParams } - if v, err := c.db.RPop(args[0]); err != nil { + v, err := c.db.RPop(args[0]) + if err != nil { return err - } else { - c.resp.writeBulk(v) } - + c.resp.writeBulk(v) return nil } @@ -76,12 +72,11 @@ func llenCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.LLen(args[0]); err != nil { + n, err := c.db.LLen(args[0]) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -96,12 +91,11 @@ func lindexCommand(c *client) error { return ErrValue } - if v, err := c.db.LIndex(args[0], int32(index)); err != nil { + v, err := c.db.LIndex(args[0], int32(index)) + if err != nil { return err - } else { - c.resp.writeBulk(v) } - + c.resp.writeBulk(v) return nil } @@ -125,12 +119,11 @@ func lrangeCommand(c *client) error { return ErrValue } - if v, err := c.db.LRange(args[0], int32(start), int32(stop)); err != nil { + v, err := c.db.LRange(args[0], int32(start), int32(stop)) + if err != nil { return err - } else { - c.resp.writeSliceArray(v) } - + c.resp.writeSliceArray(v) return nil } @@ -158,12 +151,11 @@ func lclearCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.LClear(args[0]); err != nil { + n, err := c.db.LClear(args[0]) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -173,12 +165,11 @@ func lmclearCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.LMclear(args...); err != nil { + n, err := c.db.LMclear(args...) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -193,12 +184,11 @@ func lexpireCommand(c *client) error { return ErrValue } - if v, err := c.db.LExpire(args[0], duration); err != nil { + v, err := c.db.LExpire(args[0], duration) + if err != nil { return err - } else { - c.resp.writeInteger(v) } - + c.resp.writeInteger(v) return nil } @@ -213,12 +203,11 @@ func lexpireAtCommand(c *client) error { return ErrValue } - if v, err := c.db.LExpireAt(args[0], when); err != nil { + v, err := c.db.LExpireAt(args[0], when) + if err != nil { return err - } else { - c.resp.writeInteger(v) } - + c.resp.writeInteger(v) return nil } @@ -228,12 +217,11 @@ func lttlCommand(c *client) error { return ErrCmdParams } - if v, err := c.db.LTTL(args[0]); err != nil { + v, err := c.db.LTTL(args[0]) + if err != nil { return err - } else { - c.resp.writeInteger(v) } - + c.resp.writeInteger(v) return nil } @@ -243,12 +231,11 @@ func lpersistCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.LPersist(args[0]); err != nil { + n, err := c.db.LPersist(args[0]) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -258,11 +245,11 @@ func blpopCommand(c *client) error { return err } - if ay, err := c.db.BLPop(keys, timeout); err != nil { + ay, err := c.db.BLPop(keys, timeout) + if err != nil { return err - } else { - c.resp.writeArray(ay) } + c.resp.writeArray(ay) return nil } @@ -272,11 +259,11 @@ func brpopCommand(c *client) error { return err } - if ay, err := c.db.BRPop(keys, timeout); err != nil { + ay, err := c.db.BRPop(keys, timeout) + if err != nil { return err - } else { - c.resp.writeArray(ay) } + c.resp.writeArray(ay) return nil } @@ -408,11 +395,11 @@ func lkeyexistsCommand(c *client) error { if len(args) != 1 { return ErrCmdParams } - if n, err := c.db.LKeyExists(args[0]); err != nil { + n, err := c.db.LKeyExists(args[0]) + if err != nil { return err - } else { - c.resp.writeInteger(n) } + c.resp.writeInteger(n) return nil } @@ -437,10 +424,8 @@ func lTrimCommand(c *client) error { if err := c.db.LTrim(args[0], start, stop); err != nil { return err - } else { - c.resp.writeStatus(OK) } - + c.resp.writeStatus(OK) return nil } @@ -458,12 +443,11 @@ func lTrimFrontCommand(c *client) error { return ErrValue } - if n, err := c.db.LTrimFront(args[0], trimSize); err != nil { + n, err := c.db.LTrimFront(args[0], trimSize) + if err != nil { return err - } else { - c.resp.writeInteger(int64(n)) } - + c.resp.writeInteger(int64(n)) return nil } @@ -481,12 +465,11 @@ func lTrimBackCommand(c *client) error { return ErrValue } - if n, err := c.db.LTrimBack(args[0], trimSize); err != nil { + n, err := c.db.LTrimBack(args[0], trimSize) + if err != nil { return err - } else { - c.resp.writeInteger(int64(n)) } - + c.resp.writeInteger(int64(n)) return nil } diff --git a/server/cmd_migrate.go b/server/cmd_migrate.go index 6f29459..ec40bf2 100644 --- a/server/cmd_migrate.go +++ b/server/cmd_migrate.go @@ -19,12 +19,11 @@ func dumpCommand(c *client) error { } key := c.args[0] - if data, err := c.db.Dump(key); err != nil { + data, err := c.db.Dump(key) + if err != nil { return err - } else { - c.resp.writeBulk(data) } - + c.resp.writeBulk(data) return nil } @@ -34,12 +33,11 @@ func ldumpCommand(c *client) error { } key := c.args[0] - if data, err := c.db.LDump(key); err != nil { + data, err := c.db.LDump(key) + if err != nil { return err - } else { - c.resp.writeBulk(data) } - + c.resp.writeBulk(data) return nil } @@ -49,12 +47,11 @@ func hdumpCommand(c *client) error { } key := c.args[0] - if data, err := c.db.HDump(key); err != nil { + data, err := c.db.HDump(key) + if err != nil { return err - } else { - c.resp.writeBulk(data) } - + c.resp.writeBulk(data) return nil } @@ -64,12 +61,11 @@ func sdumpCommand(c *client) error { } key := c.args[0] - if data, err := c.db.SDump(key); err != nil { + data, err := c.db.SDump(key) + if err != nil { return err - } else { - c.resp.writeBulk(data) } - + c.resp.writeBulk(data) return nil } @@ -79,12 +75,11 @@ func zdumpCommand(c *client) error { } key := c.args[0] - if data, err := c.db.ZDump(key); err != nil { + data, err := c.db.ZDump(key) + if err != nil { return err - } else { - c.resp.writeBulk(data) } - + c.resp.writeBulk(data) return nil } @@ -104,10 +99,8 @@ func restoreCommand(c *client) error { if err = c.db.Restore(key, ttl, data); err != nil { return err - } else { - c.resp.writeStatus(OK) } - + c.resp.writeStatus(OK) return nil } @@ -128,12 +121,11 @@ func xrestoreCommand(c *client) error { if err = c.db.Restore(key, ttl, data); err != nil { return err - } else { - c.resp.writeStatus(OK) } - + c.resp.writeStatus(OK) return nil } + func xdump(db *ledis.DB, tp string, key []byte) ([]byte, error) { var err error var data []byte @@ -216,11 +208,11 @@ func xdumpCommand(c *client) error { tp := strings.ToUpper(string(args[0])) key := args[1] - if data, err := xdump(c.db, tp, key); err != nil { + data, err := xdump(c.db, tp, key) + if err != nil { return err - } else { - c.resp.writeBulk(data) } + c.resp.writeBulk(data) return nil } @@ -272,18 +264,18 @@ func newMigrateKeyLocker() *migrateKeyLocker { return m } -func (a *App) newMigrateKeyLockers() { - a.migrateKeyLockers = make(map[string]*migrateKeyLocker) +func (app *App) newMigrateKeyLockers() { + app.migrateKeyLockers = make(map[string]*migrateKeyLocker) - a.migrateKeyLockers[KVName] = newMigrateKeyLocker() - a.migrateKeyLockers[HashName] = newMigrateKeyLocker() - a.migrateKeyLockers[ListName] = newMigrateKeyLocker() - a.migrateKeyLockers[SetName] = newMigrateKeyLocker() - a.migrateKeyLockers[ZSetName] = newMigrateKeyLocker() + app.migrateKeyLockers[KVName] = newMigrateKeyLocker() + app.migrateKeyLockers[HashName] = newMigrateKeyLocker() + app.migrateKeyLockers[ListName] = newMigrateKeyLocker() + app.migrateKeyLockers[SetName] = newMigrateKeyLocker() + app.migrateKeyLockers[ZSetName] = newMigrateKeyLocker() } -func (a *App) migrateKeyLock(tp string, key []byte) bool { - l, ok := a.migrateKeyLockers[strings.ToUpper(tp)] +func (app *App) migrateKeyLock(tp string, key []byte) bool { + l, ok := app.migrateKeyLockers[strings.ToUpper(tp)] if !ok { return false } @@ -291,8 +283,8 @@ func (a *App) migrateKeyLock(tp string, key []byte) bool { return l.Lock(key) } -func (a *App) migrateKeyUnlock(tp string, key []byte) { - l, ok := a.migrateKeyLockers[strings.ToUpper(tp)] +func (app *App) migrateKeyUnlock(tp string, key []byte) { + l, ok := app.migrateKeyLockers[strings.ToUpper(tp)] if !ok { return } @@ -438,9 +430,8 @@ func xmigrateCommand(c *client) error { if err == errNoKey { c.resp.writeStatus(NOKEY) return nil - } else { - return err } + return err } c.resp.writeStatus(OK) diff --git a/server/cmd_migrate_test.go b/server/cmd_migrate_test.go index 09ccf67..4b6448f 100644 --- a/server/cmd_migrate_test.go +++ b/server/cmd_migrate_test.go @@ -52,15 +52,15 @@ func testDumpRestore(c *goredis.PoolConn, dump string, key string, t *testing.T) } func TestMigrate(t *testing.T) { - data_dir := "/tmp/test_migrate" - os.RemoveAll(data_dir) + dataDir := "/tmp/test_migrate" + os.RemoveAll(dataDir) s1Cfg := config.NewConfigDefault() - s1Cfg.DataDir = fmt.Sprintf("%s/s1", data_dir) + s1Cfg.DataDir = fmt.Sprintf("%s/s1", dataDir) s1Cfg.Addr = "127.0.0.1:11185" s2Cfg := config.NewConfigDefault() - s2Cfg.DataDir = fmt.Sprintf("%s/s2", data_dir) + s2Cfg.DataDir = fmt.Sprintf("%s/s2", dataDir) s2Cfg.Addr = "127.0.0.1:11186" s1, err := NewApp(s1Cfg) diff --git a/server/cmd_replication.go b/server/cmd_replication.go index ed5ff50..da29191 100644 --- a/server/cmd_replication.go +++ b/server/cmd_replication.go @@ -104,14 +104,14 @@ func syncCommand(c *client) error { return ErrCmdParams } - var logId uint64 + var logID uint64 var err error - if logId, err = ledis.StrUint64(args[0], nil); err != nil { + if logID, err = ledis.StrUint64(args[0], nil); err != nil { return ErrCmdParams } - lastLogID := logId - 1 + lastLogID := logID - 1 stat, err := c.app.ldb.ReplicationStat() if err != nil { @@ -119,7 +119,7 @@ func syncCommand(c *client) error { } if lastLogID > stat.LastID { - return fmt.Errorf("invalid sync logid %d > %d + 1", logId, stat.LastID) + return fmt.Errorf("invalid sync logid %d > %d + 1", logID, stat.LastID) } c.lastLogID.Set(lastLogID) @@ -132,21 +132,20 @@ func syncCommand(c *client) error { c.syncBuf.Write(dummyBuf) - if _, _, err := c.app.ldb.ReadLogsToTimeout(logId, &c.syncBuf, 1, c.app.quit); err != nil { + if _, _, err := c.app.ldb.ReadLogsToTimeout(logID, &c.syncBuf, 1, c.app.quit); err != nil { return err - } else { - buf := c.syncBuf.Bytes() - - stat, err = c.app.ldb.ReplicationStat() - if err != nil { - return err - } - - binary.BigEndian.PutUint64(buf, stat.LastID) - - c.resp.writeBulk(buf) } + buf := c.syncBuf.Bytes() + + stat, err = c.app.ldb.ReplicationStat() + if err != nil { + return err + } + + binary.BigEndian.PutUint64(buf, stat.LastID) + + c.resp.writeBulk(buf) return nil } @@ -171,11 +170,11 @@ func replconfCommand(c *client) error { if _, err = num.ParseUint16(hack.String(args[i+1])); err != nil { return err } - if host, _, err = net.SplitHostPort(c.remoteAddr); err != nil { + host, _, err = net.SplitHostPort(c.remoteAddr) + if err != nil { return err - } else { - c.slaveListeningAddr = net.JoinHostPort(host, hack.String(args[i+1])) } + c.slaveListeningAddr = net.JoinHostPort(host, hack.String(args[i+1])) c.app.addSlave(c) default: @@ -200,16 +199,16 @@ func roleCommand(c *client) error { ay := make([]interface{}, 0, 5) - var lastId int64 = 0 + var lastID int64 = 0 stat, _ := c.app.ldb.ReplicationStat() if stat != nil { - lastId = int64(stat.LastID) + lastID = int64(stat.LastID) } if isMaster { ay = append(ay, []byte("master")) - ay = append(ay, lastId) + ay = append(ay, lastID) items := make([]interface{}, 0, 3) @@ -229,7 +228,7 @@ func roleCommand(c *client) error { ay = append(ay, []byte(host)) ay = append(ay, int64(port)) ay = append(ay, []byte(replStatetring(c.app.m.state.Get()))) - ay = append(ay, lastId) + ay = append(ay, lastID) } c.resp.writeArray(ay) diff --git a/server/cmd_replication_test.go b/server/cmd_replication_test.go index 0f74c0c..f56943c 100644 --- a/server/cmd_replication_test.go +++ b/server/cmd_replication_test.go @@ -36,11 +36,11 @@ func checkDataEqual(master *App, slave *App) error { } func TestReplication(t *testing.T) { - data_dir := "/tmp/test_replication" - os.RemoveAll(data_dir) + dataDir := "/tmp/test_replication" + os.RemoveAll(dataDir) masterCfg := config.NewConfigDefault() - masterCfg.DataDir = fmt.Sprintf("%s/master", data_dir) + masterCfg.DataDir = fmt.Sprintf("%s/master", dataDir) masterCfg.Addr = "127.0.0.1:11182" masterCfg.UseReplication = true masterCfg.Replication.Sync = true @@ -56,7 +56,7 @@ func TestReplication(t *testing.T) { defer master.Close() slaveCfg := config.NewConfigDefault() - slaveCfg.DataDir = fmt.Sprintf("%s/slave", data_dir) + slaveCfg.DataDir = fmt.Sprintf("%s/slave", dataDir) slaveCfg.Addr = "127.0.0.1:11183" slaveCfg.SlaveOf = masterCfg.Addr slaveCfg.UseReplication = true diff --git a/server/cmd_server.go b/server/cmd_server.go index 5282452..1dd4385 100644 --- a/server/cmd_server.go +++ b/server/cmd_server.go @@ -34,10 +34,9 @@ func authCommand(c *client) error { c.isAuthed = true c.resp.writeStatus(OK) return nil - } else { - c.isAuthed = false - return ErrAuthenticationFailure } + c.isAuthed = false + return ErrAuthenticationFailure } func echoCommand(c *client) error { @@ -54,31 +53,30 @@ func selectCommand(c *client) error { return ErrCmdParams } - if index, err := strconv.Atoi(hack.String(c.args[0])); err != nil { + index, err := strconv.Atoi(hack.String(c.args[0])) + if err != nil { return err - } else { - // if c.db.IsInMulti() { - // if err := c.script.Select(index); err != nil { - // return err - // } else { - // c.db = c.script.DB - // } - // } else { - // if db, err := c.ldb.Select(index); err != nil { - // return err - // } else { - // c.db = db - // } - // } - - if db, err := c.ldb.Select(index); err != nil { - return err - } else { - c.db = db - } - - c.resp.writeStatus(OK) } + // if c.db.IsInMulti() { + // if err := c.script.Select(index); err != nil { + // return err + // } else { + // c.db = c.script.DB + // } + // } else { + // if db, err := c.ldb.Select(index); err != nil { + // return err + // } else { + // c.db = db + // } + // } + + db, err := c.ldb.Select(index) + if err != nil { + return err + } + c.db = db + c.resp.writeStatus(OK) return nil } @@ -170,10 +168,9 @@ func configCommand(c *client) error { case "rewrite": if err := c.app.cfg.Rewrite(); err != nil { return err - } else { - c.resp.writeStatus(OK) - return nil } + c.resp.writeStatus(OK) + return nil case "get": return configGetCommand(c) default: diff --git a/server/cmd_set.go b/server/cmd_set.go index cbe8879..2ee3248 100644 --- a/server/cmd_set.go +++ b/server/cmd_set.go @@ -10,12 +10,11 @@ func saddCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.SAdd(args[0], args[1:]...); err != nil { + n, err := c.db.SAdd(args[0], args[1:]...) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -39,10 +38,8 @@ func soptGeneric(c *client, optType byte) error { if err != nil { return err - } else { - c.resp.writeSliceArray(v) } - + c.resp.writeSliceArray(v) return nil } @@ -67,10 +64,8 @@ func soptStoreGeneric(c *client, optType byte) error { if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -80,12 +75,11 @@ func scardCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.SCard(args[0]); err != nil { + n, err := c.db.SCard(args[0]) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -112,12 +106,11 @@ func sismemberCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.SIsMember(args[0], args[1]); err != nil { + n, err := c.db.SIsMember(args[0], args[1]) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -127,12 +120,11 @@ func smembersCommand(c *client) error { return ErrCmdParams } - if v, err := c.db.SMembers(args[0]); err != nil { + v, err := c.db.SMembers(args[0]) + if err != nil { return err - } else { - c.resp.writeSliceArray(v) } - + c.resp.writeSliceArray(v) return nil } @@ -143,12 +135,11 @@ func sremCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.SRem(args[0], args[1:]...); err != nil { + n, err := c.db.SRem(args[0], args[1:]...) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -167,12 +158,11 @@ func sclearCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.SClear(args[0]); err != nil { + n, err := c.db.SClear(args[0]) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -182,12 +172,11 @@ func smclearCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.SMclear(args...); err != nil { + n, err := c.db.SMclear(args...) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -202,12 +191,11 @@ func sexpireCommand(c *client) error { return ErrValue } - if v, err := c.db.SExpire(args[0], duration); err != nil { + v, err := c.db.SExpire(args[0], duration) + if err != nil { return err - } else { - c.resp.writeInteger(v) } - + c.resp.writeInteger(v) return nil } @@ -222,12 +210,11 @@ func sexpireAtCommand(c *client) error { return ErrValue } - if v, err := c.db.SExpireAt(args[0], when); err != nil { + v, err := c.db.SExpireAt(args[0], when) + if err != nil { return err - } else { - c.resp.writeInteger(v) } - + c.resp.writeInteger(v) return nil } @@ -237,12 +224,11 @@ func sttlCommand(c *client) error { return ErrCmdParams } - if v, err := c.db.STTL(args[0]); err != nil { + v, err := c.db.STTL(args[0]) + if err != nil { return err - } else { - c.resp.writeInteger(v) } - + c.resp.writeInteger(v) return nil } @@ -253,12 +239,11 @@ func spersistCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.SPersist(args[0]); err != nil { + n, err := c.db.SPersist(args[0]) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -267,11 +252,11 @@ func skeyexistsCommand(c *client) error { if len(args) != 1 { return ErrCmdParams } - if n, err := c.db.SKeyExists(args[0]); err != nil { + n, err := c.db.SKeyExists(args[0]) + if err != nil { return err - } else { - c.resp.writeInteger(n) } + c.resp.writeInteger(n) return nil } diff --git a/server/cmd_sort.go b/server/cmd_sort.go index 5a9cf19..d91b0e3 100644 --- a/server/cmd_sort.go +++ b/server/cmd_sort.go @@ -102,11 +102,11 @@ func handleXSort(c *client, tp string) error { return err } - if n, err := c.db.RPush(storeKey, ay...); err != nil { + n, err := c.db.RPush(storeKey, ay...) + if err != nil { return err - } else { - c.resp.writeInteger(n) } + c.resp.writeInteger(n) } return nil } diff --git a/server/cmd_zset.go b/server/cmd_zset.go index 6bffb1a..9f53b0a 100644 --- a/server/cmd_zset.go +++ b/server/cmd_zset.go @@ -55,12 +55,11 @@ func zcardCommand(c *client) error { return ErrCmdParams } - if n, err := c.db.ZCard(args[0]); err != nil { + n, err := c.db.ZCard(args[0]) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -206,12 +205,11 @@ func zcountCommand(c *client) error { return nil } - if n, err := c.db.ZCount(args[0], min, max); err != nil { + n, err := c.db.ZCount(args[0], min, max) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -263,7 +261,6 @@ func zremrangebyrankCommand(c *client) error { } n, err := c.db.ZRemRangeByRank(key, start, stop) - if err == nil { c.resp.writeInteger(n) } @@ -331,11 +328,11 @@ func zrangeGeneric(c *client, reverse bool) error { } } - if datas, err := c.db.ZRangeGeneric(key, start, stop, reverse); err != nil { + datas, err := c.db.ZRangeGeneric(key, start, stop, reverse) + if err != nil { return err - } else { - c.resp.writeScorePairArray(datas, withScores) } + c.resp.writeScorePairArray(datas, withScores) return nil } @@ -408,12 +405,11 @@ func zrangebyscoreGeneric(c *client, reverse bool) error { return nil } - if datas, err := c.db.ZRangeByScoreGeneric(key, min, max, offset, count, reverse); err != nil { + datas, err := c.db.ZRangeByScoreGeneric(key, min, max, offset, count, reverse) + if err != nil { return err - } else { - c.resp.writeScorePairArray(datas, withScores) } - + c.resp.writeScorePairArray(datas, withScores) return nil } @@ -501,12 +497,11 @@ func zttlCommand(c *client) error { return ErrCmdParams } - if v, err := c.db.ZTTL(args[0]); err != nil { + v, err := c.db.ZTTL(args[0]) + if err != nil { return err - } else { - c.resp.writeInteger(v) } - + c.resp.writeInteger(v) return nil } @@ -713,12 +708,11 @@ func zrangebylexCommand(c *client) error { } key := args[0] - if ay, err := c.db.ZRangeByLex(key, min, max, rangeType, offset, count); err != nil { + ay, err := c.db.ZRangeByLex(key, min, max, rangeType, offset, count) + if err != nil { return err - } else { - c.resp.writeSliceArray(ay) } - + c.resp.writeSliceArray(ay) return nil } @@ -734,12 +728,11 @@ func zremrangebylexCommand(c *client) error { } key := args[0] - if n, err := c.db.ZRemRangeByLex(key, min, max, rangeType); err != nil { + n, err := c.db.ZRemRangeByLex(key, min, max, rangeType) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -755,12 +748,11 @@ func zlexcountCommand(c *client) error { } key := args[0] - if n, err := c.db.ZLexCount(key, min, max, rangeType); err != nil { + n, err := c.db.ZLexCount(key, min, max, rangeType) + if err != nil { return err - } else { - c.resp.writeInteger(n) } - + c.resp.writeInteger(n) return nil } @@ -769,11 +761,11 @@ func zkeyexistsCommand(c *client) error { if len(args) != 1 { return ErrCmdParams } - if n, err := c.db.ZKeyExists(args[0]); err != nil { + n, err := c.db.ZKeyExists(args[0]) + if err != nil { return err - } else { - c.resp.writeInteger(n) } + c.resp.writeInteger(n) return nil } diff --git a/server/info.go b/server/info.go index 07cd552..66d1a5e 100644 --- a/server/info.go +++ b/server/info.go @@ -21,7 +21,7 @@ type info struct { Server struct { OS string - ProceessId int + ProceessID int } Replication struct { @@ -39,7 +39,7 @@ func newInfo(app *App) (i *info, err error) { i.app = app i.Server.OS = runtime.GOOS - i.Server.ProceessId = os.Getpid() + i.Server.ProceessID = os.Getpid() return i, nil } @@ -103,7 +103,7 @@ func (i *info) dumpServer(buf *bytes.Buffer) { buf.WriteString("# Server\r\n") i.dumpPairs(buf, infoPair{"os", i.Server.OS}, - infoPair{"process_id", i.Server.ProceessId}, + infoPair{"process_id", i.Server.ProceessID}, infoPair{"addr", i.app.cfg.Addr}, infoPair{"http_addr", i.app.cfg.HttpAddr}, infoPair{"readonly", i.app.cfg.Readonly}, diff --git a/server/replication.go b/server/replication.go index eca6c1c..2d42379 100644 --- a/server/replication.go +++ b/server/replication.go @@ -298,9 +298,8 @@ func (m *master) nextSyncLogID() (uint64, error) { if s.LastID > s.CommitID { return s.LastID + 1, nil - } else { - return s.CommitID + 1, nil } + return s.CommitID + 1, nil } func (m *master) sync() error { @@ -321,9 +320,8 @@ func (m *master) sync() error { if err = m.conn.ReceiveBulkTo(&m.syncBuf); err != nil { if strings.Contains(err.Error(), ledis.ErrLogMissed.Error()) { return m.fullSync() - } else { - return err } + return err } m.state.Set(replConnectedState) @@ -393,9 +391,8 @@ func (app *App) tryReSlaveof() error { if len(app.cfg.SlaveOf) == 0 { return nil - } else { - return app.m.startReplication(app.cfg.SlaveOf, true) } + return app.m.startReplication(app.cfg.SlaveOf, true) } func (app *App) addSlave(c *client) { @@ -459,14 +456,14 @@ func (app *App) publishNewLog(l *rpl.Log) { } n := 0 - logId := l.ID + logID := l.ID for _, s := range app.slaves { lastLogID := s.lastLogID.Get() - if lastLogID == logId { + if lastLogID == logID { //slave has already owned this log n++ - } else if lastLogID > logId { - log.Errorf("invalid slave %s, lastlogid %d > %d", s.slaveListeningAddr, lastLogID, logId) + } else if lastLogID > logID { + log.Errorf("invalid slave %s, lastlogid %d > %d", s.slaveListeningAddr, lastLogID, logID) } } @@ -483,8 +480,8 @@ func (app *App) publishNewLog(l *rpl.Log) { n := 0 for i := 0; i < slaveNum; i++ { id := <-app.slaveSyncAck - if id < logId { - log.Infof("some slave may close with last logid %d < %d", id, logId) + if id < logID { + log.Infof("some slave may close with last logid %d < %d", id, logID) } else { n++ if n >= total { diff --git a/store/db.go b/store/db.go index 41e4353..a4b488f 100644 --- a/store/db.go +++ b/store/db.go @@ -52,11 +52,8 @@ func (db *DB) Put(key []byte, value []byte) error { if db.needSyncCommit() { return db.db.SyncPut(key, value) - - } else { - return db.db.Put(key, value) - } + return db.db.Put(key, value) } func (db *DB) Delete(key []byte) error { @@ -64,9 +61,8 @@ func (db *DB) Delete(key []byte) error { if db.needSyncCommit() { return db.db.SyncDelete(key) - } else { - return db.db.Delete(key) } + return db.db.Delete(key) } func (db *DB) NewWriteBatch() *WriteBatch { @@ -110,14 +106,14 @@ func (db *DB) RevRangeIterator(min []byte, max []byte, rangeType uint8) *RangeLi return NewRevRangeLimitIterator(db.NewIterator(), &Range{min, max, rangeType}, &Limit{0, -1}) } -//count < 0, unlimit. +//RangeLimitIterator count < 0, unlimit. // //offset must >= 0, if < 0, will get nothing. func (db *DB) RangeLimitIterator(min []byte, max []byte, rangeType uint8, offset int, count int) *RangeLimitIterator { return NewRangeLimitIterator(db.NewIterator(), &Range{min, max, rangeType}, &Limit{offset, count}) } -//count < 0, unlimit. +//RevRangeLimitIterator count < 0, unlimit. // //offset must >= 0, if < 0, will get nothing. func (db *DB) RevRangeLimitIterator(min []byte, max []byte, rangeType uint8, offset int, count int) *RangeLimitIterator { @@ -133,20 +129,19 @@ func (db *DB) needSyncCommit() bool { return false } else if db.cfg.DBSyncCommit == 2 { return true - } else { - n := time.Now() - need := false - db.m.Lock() - - if n.Sub(db.lastCommit) > time.Second { - need = true - } - db.lastCommit = n - - db.m.Unlock() - return need } + n := time.Now() + need := false + db.m.Lock() + + if n.Sub(db.lastCommit) > time.Second { + need = true + } + db.lastCommit = n + + db.m.Unlock() + return need } func (db *DB) GetSlice(key []byte) (Slice, error) { @@ -156,14 +151,12 @@ func (db *DB) GetSlice(key []byte) (Slice, error) { db.st.statGet(v, err) db.st.GetTotalTime.Add(time.Now().Sub(t)) return v, err - } else { - v, err := db.Get(key) - if err != nil { - return nil, err - } else if v == nil { - return nil, nil - } else { - return driver.GoSlice(v), nil - } } + v, err := db.Get(key) + if err != nil { + return nil, err + } else if v == nil { + return nil, nil + } + return driver.GoSlice(v), nil } diff --git a/store/iterator.go b/store/iterator.go index 944b2b0..af3533d 100644 --- a/store/iterator.go +++ b/store/iterator.go @@ -18,7 +18,7 @@ const ( RangeOpen uint8 = 0x11 ) -// min must less or equal than max +// Range min must less or equal than max // // range type: // @@ -44,7 +44,7 @@ type Iterator struct { st *Stat } -// Returns a copy of key. +// Key returns a copy of key. func (it *Iterator) Key() []byte { k := it.it.Key() if k == nil { @@ -54,7 +54,7 @@ func (it *Iterator) Key() []byte { return append([]byte{}, k...) } -// Returns a copy of value. +// Value returns a copy of value. func (it *Iterator) Value() []byte { v := it.it.Value() if v == nil { @@ -64,19 +64,19 @@ func (it *Iterator) Value() []byte { return append([]byte{}, v...) } -// Returns a reference of key. +// RawKey returns a reference of key. // you must be careful that it will be changed after next iterate. func (it *Iterator) RawKey() []byte { return it.it.Key() } -// Returns a reference of value. +// RawValue returns a reference of value. // you must be careful that it will be changed after next iterate. func (it *Iterator) RawValue() []byte { return it.it.Value() } -// Copy key to b, if b len is small or nil, returns a new one. +// BufKey copy key to b, if b len is small or nil, returns a new one. func (it *Iterator) BufKey(b []byte) []byte { k := it.RawKey() if k == nil { @@ -90,7 +90,7 @@ func (it *Iterator) BufKey(b []byte) []byte { return append(b, k...) } -// Copy value to b, if b len is small or nil, returns a new one. +// BufValue copy value to b, if b len is small or nil, returns a new one. func (it *Iterator) BufValue(b []byte) []byte { v := it.RawValue() if v == nil { @@ -142,7 +142,7 @@ func (it *Iterator) Seek(key []byte) { it.it.Seek(key) } -// Finds by key, if not found, nil returns. +// Find finds by key, if not found, nil returns. func (it *Iterator) Find(key []byte) []byte { it.Seek(key) if it.Valid() { @@ -157,7 +157,7 @@ func (it *Iterator) Find(key []byte) []byte { return nil } -// Finds by key, if not found, nil returns, else a reference of value returns. +// RawFind finds by key, if not found, nil returns, else a reference of value returns. // you must be careful that it will be changed after next iterate. func (it *Iterator) RawFind(key []byte) []byte { it.Seek(key) diff --git a/store/snapshot.go b/store/snapshot.go index 6b75180..02606ef 100644 --- a/store/snapshot.go +++ b/store/snapshot.go @@ -30,15 +30,14 @@ func (s *Snapshot) GetSlice(key []byte) (Slice, error) { v, err := d.GetSlice(key) s.st.statGet(v, err) return v, err + } + v, err := s.Get(key) + if err != nil { + return nil, err + } else if v == nil { + return nil, nil } else { - v, err := s.Get(key) - if err != nil { - return nil, err - } else if v == nil { - return nil, nil - } else { - return driver.GoSlice(v), nil - } + return driver.GoSlice(v), nil } } diff --git a/store/store.go b/store/store.go index b767963..a40a40f 100644 --- a/store/store.go +++ b/store/store.go @@ -8,17 +8,16 @@ import ( "github.com/ledisdb/ledisdb/config" "github.com/ledisdb/ledisdb/store/driver" - _ "github.com/ledisdb/ledisdb/store/goleveldb" - _ "github.com/ledisdb/ledisdb/store/leveldb" - _ "github.com/ledisdb/ledisdb/store/rocksdb" + _ "github.com/ledisdb/ledisdb/store/goleveldb" // register goleveldb + _ "github.com/ledisdb/ledisdb/store/leveldb" // register leveldb + _ "github.com/ledisdb/ledisdb/store/rocksdb" // register rocksdb ) func getStorePath(cfg *config.Config) string { if len(cfg.DBPath) > 0 { return cfg.DBPath - } else { - return path.Join(cfg.DataDir, fmt.Sprintf("%s_data", cfg.DBName)) } + return path.Join(cfg.DataDir, fmt.Sprintf("%s_data", cfg.DBName)) } func Open(cfg *config.Config) (*DB, error) { @@ -57,6 +56,3 @@ func Repair(cfg *config.Config) error { return s.Repair(path, cfg) } - -func init() { -} diff --git a/store/writebatch.go b/store/writebatch.go index 446df3f..5d7654c 100644 --- a/store/writebatch.go +++ b/store/writebatch.go @@ -59,7 +59,7 @@ func (wb *WriteBatch) Rollback() error { return wb.wb.Rollback() } -// the data will be undefined after commit or rollback +// BatchData the data will be undefined after commit or rollback func (wb *WriteBatch) BatchData() *BatchData { data := wb.wb.Data() if wb.data == nil {