From ef95359964368e0b382889c29c1ce78ade712f9a Mon Sep 17 00:00:00 2001 From: xgzlucario <912156837@qq.com> Date: Sun, 21 Jul 2024 19:36:05 +0800 Subject: [PATCH] feat: add flushdb command --- command.go | 7 +++++++ command_test.go | 27 ++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/command.go b/command.go index d8e4927..3b0bfe5 100644 --- a/command.go +++ b/command.go @@ -3,6 +3,7 @@ package main import ( "strconv" + "github.com/xgzlucario/rotom/internal/dict" "github.com/xgzlucario/rotom/internal/hash" "github.com/xgzlucario/rotom/internal/list" "github.com/xgzlucario/rotom/internal/zset" @@ -42,6 +43,7 @@ var cmdTable []*Command = []*Command{ {"ping", pingCommand, 0, false}, {"hgetall", hgetallCommand, 1, false}, {"lrange", lrangeCommand, 3, false}, + {"flushdb", flushdbCommand, 0, true}, // TODO {"mset", todoCommand, 0, false}, @@ -391,6 +393,11 @@ func zaddCommand(writer *RESPWriter, args []RESP) { writer.WriteInteger(newFields) } +func flushdbCommand(writer *RESPWriter, _ []RESP) { + db.dict = dict.New() + writer.WriteString("OK") +} + // TODO func todoCommand(writer *RESPWriter, _ []RESP) { writer.WriteString("OK") diff --git a/command_test.go b/command_test.go index 0c8f177..5b16497 100644 --- a/command_test.go +++ b/command_test.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "math/rand/v2" + "os" "sync" "testing" "time" @@ -14,8 +15,11 @@ import ( func startup() { config := &Config{ - Port: 20082, + Port: 20082, + AppendOnly: true, + AppendFileName: "appendonly-test.aof", } + os.Remove(config.AppendFileName) if err := InitDB(config); err != nil { log.Panic().Msgf("init db error: %v", err) } @@ -23,6 +27,7 @@ func startup() { log.Panic().Msgf("init server error: %v", err) } server.aeLoop.AddRead(server.fd, AcceptHandler, nil) + server.aeLoop.AddTimeEvent(AE_NORMAL, 500, CheckOutOfMemory, nil) server.aeLoop.AeMain() } @@ -57,6 +62,18 @@ func TestCommand(t *testing.T) { assert.Equal(res, "") }) + t.Run("pipline", func(t *testing.T) { + pip := rdb.Pipeline() + pip.RPush(ctx, "ls-pip", "A", "B", "C") + pip.LPop(ctx, "ls-pip") + + _, err := pip.Exec(ctx) + assert.Nil(err) + + res, _ := rdb.LRange(ctx, "ls-pip", 0, -1).Result() + assert.Equal(res, []string{"B", "C"}) + }) + t.Run("incr", func(t *testing.T) { res, _ := rdb.Incr(ctx, "testIncr").Result() assert.Equal(res, int64(1)) @@ -163,9 +180,13 @@ func TestCommand(t *testing.T) { assert.Equal(n, int64(2)) }) - t.Run("mset", func(t *testing.T) { - res, _ := rdb.MSet(ctx, "mk1", "mv1", "mk2", "mv2").Result() + t.Run("flushdb", func(t *testing.T) { + rdb.Set(ctx, "test-flush", "1", 0) + res, _ := rdb.FlushDB(ctx).Result() assert.Equal(res, "OK") + + _, err := rdb.Get(ctx, "test-flush").Result() + assert.Equal(err, redis.Nil) }) t.Run("concurrency", func(t *testing.T) {