Skip to content

Commit

Permalink
feat: add flushdb command
Browse files Browse the repository at this point in the history
  • Loading branch information
xgzlucario committed Jul 21, 2024
1 parent e2a5eab commit ef95359
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
7 changes: 7 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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")
Expand Down
27 changes: 24 additions & 3 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"math/rand/v2"
"os"
"sync"
"testing"
"time"
Expand All @@ -14,15 +15,19 @@ 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)
}
if err := initServer(config); err != nil {
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()
}

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit ef95359

Please sign in to comment.