欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Golang實(shí)現(xiàn)內(nèi)存數(shù)據(jù)庫的示例詳解

 更新時(shí)間:2023年03月27日 09:05:52   作者:CSGOPHER  
這篇文章主要為大家詳細(xì)介紹了如何基于Golang實(shí)現(xiàn)內(nèi)存數(shù)據(jù)庫,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的小伙伴可以參考一下

GO實(shí)現(xiàn)內(nèi)存數(shù)據(jù)庫

實(shí)現(xiàn)Redis的database層(核心層:處理命令并返回)

https://github.com/csgopher/go-redis

本文涉及以下文件:dict:定義字典的一些方法

  • sync_dict:實(shí)現(xiàn)dict
  • db:分?jǐn)?shù)據(jù)庫
  • command:定義指令
  • ping,keys,string:指令的具體處理邏輯
  • database:?jiǎn)螜C(jī)版數(shù)據(jù)庫

datastruct/dict/dict.go

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()
}

Dict接口:Redis數(shù)據(jù)結(jié)構(gòu)的接口。這里我們使用sync.Map作為字典的實(shí)現(xiàn),如果想用別的數(shù)據(jù)結(jié)構(gòu),換一個(gè)實(shí)現(xiàn)即可

Consumer:遍歷字典所有的鍵值對(duì),返回值是布爾,true繼續(xù)遍歷,false停止遍歷

datastruct/dict/sync_dict.go

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實(shí)現(xiàn)Dict接口

database/db.go

type DB struct {
	index int
	data  dict.Dict
}

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.MakeArgNumErrReply(cmdName)
	}
	fun := cmd.executor
	return fun(db, cmdLine[1:]) // 把 set k v 中的set切掉
}

func validateArity(arity int, cmdArgs [][]byte) bool {
	argNum := len(cmdArgs)
	if arity >= 0 {
		return argNum == arity
	}
	return argNum >= -arity
}

func (db *DB) GetEntity(key string) (*database.DataEntity, bool) {
	raw, ok := db.data.Get(key)
	if !ok {
		return nil, false
	}
	entity, _ := raw.(*database.DataEntity)
	return entity, true
}

func (db *DB) PutEntity(key string, entity *database.DataEntity) int {
	return db.data.Put(key, entity)
}

func (db *DB) PutIfExists(key string, entity *database.DataEntity) int {
	return db.data.PutIfExists(key, entity)
}

func (db *DB) PutIfAbsent(key string, entity *database.DataEntity) int {
	return db.data.PutIfAbsent(key, entity)
}

func (db *DB) Remove(key string) {
	db.data.Remove(key)
}

func (db *DB) Removes(keys ...string) (deleted int) {
	deleted = 0
	for _, key := range keys {
		_, exists := db.data.Get(key)
		if exists {
			db.Remove(key)
			deleted++
		}
	}
	return deleted
}

func (db *DB) Flush() {
	db.data.Clear()
}

實(shí)現(xiàn)Redis中的分?jǐn)?shù)據(jù)庫

ExecFunc:所有Redis的指令都寫成這樣的類型

validateArity方法:

  • 定長(zhǎng):set k v => arity=3;
  • 變長(zhǎng):exists k1 k2 k3 ... => arity=-2,表示參數(shù)>=2個(gè)

database/command.go

var cmdTable = make(map[string]*command)

type command struct {
   executor ExecFunc
   arity    int 
}

func RegisterCommand(name string, executor ExecFunc, arity int) {
   name = strings.ToLower(name)
   cmdTable[name] = &command{
      executor: executor,
      arity:    arity,
   }
}

command:每一個(gè)command結(jié)構(gòu)體都是一個(gè)指令,例如ping,keys等等

  • arity:參數(shù)數(shù)量
  • cmdTable:記錄所有指令和command結(jié)構(gòu)體的關(guān)系
  • RegisterCommand:注冊(cè)指令的實(shí)現(xiàn),在程序

database/ping.go

func Ping(db *DB, args [][]byte) resp.Reply {
    if len(args) == 0 {
        return &reply.PongReply{}
    } else if len(args) == 1 {
        return reply.MakeStatusReply(string(args[0]))
    } else {
        return reply.MakeErrReply("ERR wrong number of arguments for 'ping' command")
    }
}

func init() {
    RegisterCommand("ping", Ping, 1)
}

init方法:在啟動(dòng)程序時(shí)就會(huì)調(diào)用這個(gè)方法,用于初始化

database/keys.go

func execDel(db *DB, args [][]byte) resp.Reply {
   keys := make([]string, len(args))
   for i, v := range args {
      keys[i] = string(v)
   }

   deleted := db.Removes(keys...)
   return reply.MakeIntReply(int64(deleted))
}

func execExists(db *DB, args [][]byte) resp.Reply {
   result := int64(0)
   for _, arg := range args {
      key := string(arg)
      _, exists := db.GetEntity(key)
      if exists {
         result++
      }
   }
   return reply.MakeIntReply(result)
}

func execFlushDB(db *DB, args [][]byte) resp.Reply {
   db.Flush()
   return &reply.OkReply{}
}

func execType(db *DB, args [][]byte) resp.Reply {
   key := string(args[0])
   entity, exists := db.GetEntity(key)
   if !exists {
      return reply.MakeStatusReply("none")
   }
   switch entity.Data.(type) {
   case []byte:
      return reply.MakeStatusReply("string")
   }
   return &reply.UnknownErrReply{}
}

func execRename(db *DB, args [][]byte) resp.Reply {
   if len(args) != 2 {
      return reply.MakeErrReply("ERR wrong number of arguments for 'rename' command")
   }
   src := string(args[0])
   dest := string(args[1])
   
   entity, ok := db.GetEntity(src)
   if !ok {
      return reply.MakeErrReply("no such key")
   }
   db.PutEntity(dest, entity)
   db.Remove(src)
   return &reply.OkReply{}
}

func execRenameNx(db *DB, args [][]byte) resp.Reply {
   src := string(args[0])
   dest := string(args[1])

   _, exist := db.GetEntity(dest)
   if exist {
      return reply.MakeIntReply(0)
   }

   entity, ok := db.GetEntity(src)
   if !ok {
      return reply.MakeErrReply("no such key")
   }
   db.Removes(src, dest)
   db.PutEntity(dest, entity)
   return reply.MakeIntReply(1)
}

func execKeys(db *DB, args [][]byte) resp.Reply {
   pattern := wildcard.CompilePattern(string(args[0]))
   result := make([][]byte, 0)
   db.data.ForEach(func(key string, val interface{}) bool {
      if pattern.IsMatch(key) {
         result = append(result, []byte(key))
      }
      return true
   })
   return reply.MakeMultiBulkReply(result)
}

func init() {
   RegisterCommand("Del", execDel, -2)
   RegisterCommand("Exists", execExists, -2)
   RegisterCommand("Keys", execKeys, 2)
   RegisterCommand("FlushDB", execFlushDB, -1)
   RegisterCommand("Type", execType, 2)
   RegisterCommand("Rename", execRename, 3)
   RegisterCommand("RenameNx", execRenameNx, 3)
}

keys.go實(shí)現(xiàn)以下指令:

  • execDel:del k1 k2 k3 ...
  • execExists:exist k1 k2 k3 ...
  • execFlushDB:flushdb
  • execType:type k1
  • execRename:rename k1 k2
  • execRenameNx:renamenx k1 k2
  • execKeys:keys(依賴lib包的工具類wildcard.go)

database/string.go

func execGet(db *DB, args [][]byte) resp.Reply {
   key := string(args[0])
   bytes, err := db.getAsString(key)
   if err != nil {
      return err
   }
   if bytes == nil {
      return &reply.NullBulkReply{}
   }
   return reply.MakeBulkReply(bytes)
}

func (db *DB) getAsString(key string) ([]byte, reply.ErrorReply) {
   entity, ok := db.GetEntity(key)
   if !ok {
      return nil, nil
   }
   bytes, ok := entity.Data.([]byte)
   if !ok {
      return nil, &reply.WrongTypeErrReply{}
   }
   return bytes, nil
}

func execSet(db *DB, args [][]byte) resp.Reply {
   key := string(args[0])
   value := args[1]
   entity := &database.DataEntity{
      Data: value,
   }
   db.PutEntity(key, entity)
   return &reply.OkReply{}
}

func execSetNX(db *DB, args [][]byte) resp.Reply {
   key := string(args[0])
   value := args[1]
   entity := &database.DataEntity{
      Data: value,
   }
   result := db.PutIfAbsent(key, entity)
   return reply.MakeIntReply(int64(result))
}

func execGetSet(db *DB, args [][]byte) resp.Reply {
   key := string(args[0])
   value := args[1]

   entity, exists := db.GetEntity(key)
   db.PutEntity(key, &database.DataEntity{Data: value})
   if !exists {
      return reply.MakeNullBulkReply()
   }
   old := entity.Data.([]byte)
   return reply.MakeBulkReply(old)
}

func execStrLen(db *DB, args [][]byte) resp.Reply {
   key := string(args[0])
   entity, exists := db.GetEntity(key)
   if !exists {
      return reply.MakeNullBulkReply()
   }
   old := entity.Data.([]byte)
   return reply.MakeIntReply(int64(len(old)))
}

func init() {
   RegisterCommand("Get", execGet, 2)
   RegisterCommand("Set", execSet, -3)
   RegisterCommand("SetNx", execSetNX, 3)
   RegisterCommand("GetSet", execGetSet, 3)
   RegisterCommand("StrLen", execStrLen, 2)
}

string.go實(shí)現(xiàn)以下指令:

  • execGet:get k1
  • execSet:set k v
  • execSetNX:setnex k v
  • execGetSet:getset k v 返回舊值
  • execStrLen:strlen k

database/database.go

type Database struct {
   dbSet []*DB
}

func NewDatabase() *Database {
   mdb := &Database{}
   if config.Properties.Databases == 0 {
      config.Properties.Databases = 16
   }
   mdb.dbSet = make([]*DB, config.Properties.Databases)
   for i := range mdb.dbSet {
      singleDB := makeDB()
      singleDB.index = i
      mdb.dbSet[i] = singleDB
   }
   return mdb
}

func (mdb *Database) Exec(c resp.Connection, cmdLine [][]byte) (result resp.Reply) {
   defer func() {
      if err := recover(); err != nil {
         logger.Warn(fmt.Sprintf("error occurs: %v\n%s", err, string(debug.Stack())))
      }
   }()

   cmdName := strings.ToLower(string(cmdLine[0]))
   if cmdName == "select" {
      if len(cmdLine) != 2 {
         return reply.MakeArgNumErrReply("select")
      }
      return execSelect(c, mdb, cmdLine[1:])
   }
   dbIndex := c.GetDBIndex()
   selectedDB := mdb.dbSet[dbIndex]
   return selectedDB.Exec(c, cmdLine)
}

func execSelect(c resp.Connection, mdb *Database, args [][]byte) resp.Reply {
   dbIndex, err := strconv.Atoi(string(args[0]))
   if err != nil {
      return reply.MakeErrReply("ERR invalid DB index")
   }
   if dbIndex >= len(mdb.dbSet) {
      return reply.MakeErrReply("ERR DB index is out of range")
   }
   c.SelectDB(dbIndex)
   return reply.MakeOkReply()
}

func (mdb *Database) Close() {
}

func (mdb *Database) AfterClientClose(c resp.Connection) {
}
  • Database:一組db的集合
  • Exec:執(zhí)行切換db指令或者其他指令
  • execSelect方法:選擇db(指令:select 2)

resp/handler/handler.go

import (
	database2 "go-redis/database"
)

func MakeHandler() *RespHandler {
   var db database.Database
   db = database2.NewDatabase()
   return &RespHandler{
      db: db,
   }
}

修改實(shí)現(xiàn)協(xié)議層handler的database實(shí)現(xiàn)

架構(gòu)小結(jié)

TCP層服務(wù)TCP的連接,然后將連接交給RESP協(xié)議層的handler,handler監(jiān)聽客戶端的連接,將指令解析后發(fā)給管道,管道轉(zhuǎn)給database層(database/database.go),核心層根據(jù)命令類型執(zhí)行不同的方法,然后返回。

到此這篇關(guān)于基于Golang實(shí)現(xiàn)內(nèi)存數(shù)據(jù)庫的示例詳解的文章就介紹到這了,更多相關(guān)Golang內(nèi)存數(shù)據(jù)庫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • go代碼實(shí)現(xiàn)買房貸款月供計(jì)算的方法

    go代碼實(shí)現(xiàn)買房貸款月供計(jì)算的方法

    今天小編就為大家分享一篇關(guān)于go代碼實(shí)現(xiàn)買房貸款月供計(jì)算的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-04-04
  • Go結(jié)合Redis用最簡(jiǎn)單的方式實(shí)現(xiàn)分布式鎖

    Go結(jié)合Redis用最簡(jiǎn)單的方式實(shí)現(xiàn)分布式鎖

    本文主要介紹了Go結(jié)合Redis用最簡(jiǎn)單的方式實(shí)現(xiàn)分布式鎖示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 詳解Golang如何優(yōu)雅的終止一個(gè)服務(wù)

    詳解Golang如何優(yōu)雅的終止一個(gè)服務(wù)

    后端服務(wù)通常會(huì)需要?jiǎng)?chuàng)建子協(xié)程來進(jìn)行相應(yīng)的作業(yè),但進(jìn)程接受到終止信號(hào)或正常結(jié)束時(shí),并沒有判斷或等待子協(xié)程執(zhí)行結(jié)束,下面這篇文章主要給大家介紹了關(guān)于Golang如何優(yōu)雅的終止一個(gè)服務(wù)的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • Go語言通過http抓取網(wǎng)頁的方法

    Go語言通過http抓取網(wǎng)頁的方法

    這篇文章主要介紹了Go語言通過http抓取網(wǎng)頁的方法,實(shí)例分析了Go語言通過http操作頁面的技巧,需要的朋友可以參考下
    2015-03-03
  • go常用指令之go?mod詳解

    go常用指令之go?mod詳解

    當(dāng)go命令運(yùn)行時(shí),它查找當(dāng)前目錄然后查找相繼的父目錄來找出 go.mod,下面這篇文章主要給大家介紹了關(guān)于go常用指令之go?mod的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • Golang 定時(shí)器的終止與重置實(shí)現(xiàn)

    Golang 定時(shí)器的終止與重置實(shí)現(xiàn)

    在實(shí)際開發(fā)過程中,我們有時(shí)候需要編寫一些定時(shí)任務(wù)。很多人都熟悉定時(shí)器的使用,那么定時(shí)器應(yīng)該如何終止與重置,下面我們就一起來了解一下
    2021-08-08
  • go mod 安裝依賴 unkown revision問題的解決方案

    go mod 安裝依賴 unkown revision問題的解決方案

    這篇文章主要介紹了go mod 安裝依賴 unkown revision問題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-05-05
  • VSCode必裝Go語言以下插件的思路詳解

    VSCode必裝Go語言以下插件的思路詳解

    這篇文章主要介紹了VSCode必裝Go語言以下插件的思路詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-04-04
  • 為什么GO不支持循環(huán)引用

    為什么GO不支持循環(huán)引用

    這篇文章主要介紹的是為什么GO不支持循環(huán)引用,學(xué)習(xí) Go 語言的開發(fā)者越來越多了,很多小伙伴在使用時(shí),就會(huì)遇到種種不理解的問題,其中一點(diǎn)就是包的循環(huán)引用的報(bào)錯(cuò),下main文章我們一起來看看學(xué)習(xí)原因
    2021-10-10
  • Go語言sort包函數(shù)使用示例

    Go語言sort包函數(shù)使用示例

    這篇文章主要為大家介紹了Go語言sort包函數(shù)使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06

最新評(píng)論