| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- // Author: simon (ynwdlxm@163.com)
- // Date: 2025/9/15 18:04
- // Desc:
- package database
- import (
- "strings"
- "github.com/runningwater/go-redis/datastruct/dict"
- "github.com/runningwater/go-redis/interface/database"
- "github.com/runningwater/go-redis/interface/resp"
- "github.com/runningwater/go-redis/resp/reply"
- )
- // DB 数据库
- type DB struct {
- index int
- data dict.Dict
- }
- type ExecFunc func(db *DB, args [][]byte) resp.Reply
- type CmdLine = [][]byte
- func NewDB() *DB {
- return &DB{
- index: 0,
- data: dict.NewSyncDict(),
- }
- }
- // Exec 执行一个redis命令
- // 参数:
- // - c: 客户端连接对象,表示发送命令的客户端连接
- // - cmdLine: 命令行参数,包含命令名称和所有参数的字节切片
- //
- // 返回值:
- // - resp.Reply: 命令执行结果的回复
- func (d *DB) Exec(c resp.Connection, cmdLine CmdLine) resp.Reply {
- // 检查命令行参数是否为空
- if len(cmdLine) == 0 {
- return reply.NewErrReply("empty command")
- }
- // ping set setnx get
- cmdName := strings.ToLower(string(cmdLine[0]))
- // 查找命令
- cmd, ok := cmdTable[cmdName]
- if !ok {
- return reply.NewUnknownErrReply(cmdName)
- }
- // 参数校验
- if !validateArity(cmd.arity, cmdLine[1:]) {
- return reply.NewArgNumErrReply(cmdName)
- }
- if cmd.executor == nil {
- return reply.NewErrReply("command not implement")
- }
- return cmd.executor(d, cmdLine[1:])
- }
- func validateArity(arity int, args [][]byte) bool {
- if arity >= 0 {
- return arity == len(args)
- }
- // 变长的 arity 设置为 负的最小个数
- return len(args) >= -arity
- }
- func (d *DB) GetEntity(key string) (*database.DataEntity, bool) {
- raw, ok := d.data.Get(key)
- if !ok {
- return nil, false
- }
- entity, _ := raw.(*database.DataEntity)
- return entity, true
- }
- func (d *DB) PutEntity(key string, entity *database.DataEntity) int {
- return d.data.Put(key, entity)
- }
- func (d *DB) Remove(key string) {
- d.data.Remove(key)
- }
- func (d *DB) Removes(keys ...string) (deleted int) {
- deleted = 0
- for _, key := range keys {
- if _, exists := d.data.Get(key); exists {
- d.Remove(key)
- deleted++
- }
- }
- return
- }
- func (d *DB) Flush() {
- d.data.Clear()
- }
|