设为首页 加入收藏

TOP

GO实现Redis:GO实现内存数据库(3)(一)
2023-07-23 13:31:29 】 浏览:71
Tags:实现 Redis

  • 实现Redis的database层(核心层:处理命令并返回)
  • https://github.com/csgopher/go-redis
  • 本文涉及以下文件:
    dict:定义字典的一些方法
    sync_dict:实现dict
    db:分数据库
    command:定义指令
    ping,keys,string:指令的具体处理逻辑
    database:单机版数据库

datastruct/dict/dict.go

// 遍历字典所有的键值对,返回值是布尔,true继续遍历,false停止遍历
type Consumer func(key string, val interface{}) bool

type Dict interface {
   Get(key string) (val interface{}, exists bool)
   Len() int
   Put(key string, val interface{}) (result int)
   PutIfAbsent(key string, val interface{}) (result int)
   PutIfExists(key string, val interface{}) (result int)
   Remove(key string) (result int)
   ForEach(consumer Consumer)
   Keys() []string
   RandomKeys(limit int) []string
   RandomDistinctKeys(limit int) []string
   Clear()
}

Consumer:遍历字典所有的键值对,返回值是布尔,true继续遍历,false停止遍历

Dict接口:Redis数据结构的接口。这里使用sync.Map作为字典的实现,如果想用别的数据结构,换一个实现即可


datastruct/dict/sync_dict.go

// 使用sync.Map实现Dict接口
type SyncDict struct {
   m sync.Map
}

func MakeSyncDict() *SyncDict {
   return &SyncDict{}
}

func (dict *SyncDict) Get(key string) (val interface{}, exists bool) {
   val, ok := dict.m.Load(key)
   return val, ok
}

func (dict *SyncDict) Len() int {
   length := 0
   dict.m.Range(func(k, v interface{}) bool {
      length++
      return true
   })
   return length
}

func (dict *SyncDict) Put(key string, val interface{}) (result int) {
   _, existed := dict.m.Load(key)
   dict.m.Store(key, val)
   if existed {
      return 0
   }
   return 1
}

func (dict *SyncDict) PutIfAbsent(key string, val interface{}) (result int) {
   _, existed := dict.m.Load(key)
   if existed {
      return 0
   }
   dict.m.Store(key, val)
   return 1
}

func (dict *SyncDict) PutIfExists(key string, val interface{}) (result int) {
   _, existed := dict.m.Load(key)
   if existed {
      dict.m.Store(key, val)
      return 1
   }
   return 0
}

func (dict *SyncDict) Remove(key string) (result int) {
   _, existed := dict.m.Load(key)
   dict.m.Delete(key)
   if existed {
      return 1
   }
   return 0
}

func (dict *SyncDict) ForEach(consumer Consumer) {
   dict.m.Range(func(key, value interface{}) bool {
      consumer(key.(string), value)
      return true
   })
}

func (dict *SyncDict) Keys() []string {
   result := make([]string, dict.Len())
   i := 0
   dict.m.Range(func(key, value interface{}) bool {
      result[i] = key.(string)
      i++
      return true
   })
   return result
}

func (dict *SyncDict) RandomKeys(limit int) []string {
   result := make([]string, limit)
   for i := 0; i < limit; i++ {
      dict.m.Range(func(key, value interface{}) bool {
         result[i] = key.(string)
         return false
      })
   }
   return result
}

func (dict *SyncDict) RandomDistinctKeys(limit int) []string {
   result := make([]string, limit)
   i := 0
   dict.m.Range(func(key, value interface{}) bool {
      result[i] = key.(string)
      i++
      if i == limit {
         return false
      }
      return true
   })
   return result
}

func (dict *SyncDict) Clear() {
   *dict = *MakeSyncDict()
}

使用sync.Map实现Dict接口

database/db.go

// Redis中的分数据库
type DB struct {
	index int
	data  dict.Dict
}

// 所有Redis的指令都写成这样的类型
type ExecFunc func(db *DB, args [][]byte) resp.Reply

type CmdLine = [][]byte

func makeDB() *DB {
	db := &DB{
		data: dict.MakeSyncDict(),
	}
	return db
}

func (db *DB) Exec(c resp.Connection, cmdLine [][]byte) resp.Reply {
	cmdName := strings.ToLower(string(cmdLine[0]))
	cmd, ok := cmdTable[cmdName]
	if !ok {
		return reply.MakeErrReply("ERR unknown command '" + cmdName + "'")
	}
	if !validateArity(cmd.arity, cmdLine) {
		return reply.Make
首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇「高频必考」Docker&K8S面试题和.. 下一篇Go语言入门2(流程控制,string)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目