dbmanager

package
v0.0.0-...-0012533 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 11, 2023 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPlayer     = fmt.Errorf(" PlayerID == 0")
	ErrID         = fmt.Errorf(" ID == 0")
	ErrPartial    = fmt.Errorf(" SetUpdate fail GetIsPartial() fail")
	ErrAllMD      = fmt.Errorf(" SetUpdate fail is All data")          //这个数据做了全量更新了
	ErrNoData     = fmt.Errorf(" Load redis no data")                  //获取redis数据,没有对应的数据
	ErrNoLoadData = common.NewError(" Load data not exist.keyname:%s") //不存在缓存数据
)
View Source
var ErrTokenVerify = errors.New("token error")

Functions

func CheckModel

func CheckModel(md IDataBaseModel) error

模块检查

func DBResolveModel

func DBResolveModel(md IDataBaseModel) (result map[string]reflect.Value)

把数据分解成字段数据

func GetKeyNameResolve

func GetKeyNameResolve(keyname string) (table string, pid, uid uint64)

分解KeyName

func GetRowKeyName

func GetRowKeyName(data IDataBaseModel) string

获取KeyName

func GetRowKeyNameByPID

func GetRowKeyNameByPID(tname string, pid uint64) string

一个用户一行数据的KEY

func GetRowKeyNameByUID

func GetRowKeyNameByUID(tname string, pid, uid uint64) string

一个用户多行的KEY

func MsgDBUpUserDataReq

func MsgDBUpUserDataReq(pid, space, sid uint64, mds ...IDataBaseModel)

向DB服发送表数据更新

func MsgDBUpUserDatasReq

func MsgDBUpUserDatasReq(pid, space, sid uint64, args *DataArgs) msgdef.IMsg

向DB发送请求user数据请求

func NewEntityUtilDBData

func NewEntityUtilDBData(pid uint64) (result *dbsessionUtil)

SessionUtil 获得Session表的工具类

func PackMsgData

func PackMsgData(v reflect.Value) (result *protomsg.FieldData)

打包字段

func PostDBPlayer

func PostDBPlayer(pid uint64, reqMsg msgdef.IMsg)

收发消息

func PostDBServer

func PostDBServer(space, srvID uint64, reqMsg msgdef.IMsg)

向DB服发送消息

func PostOther

func PostOther(pid, space, srvid uint64, reqMsg msgdef.IMsg)

向其他服务器投递消息

func SetDataModel

func SetDataModel(md IDataBaseModel, msgdata *protomsg.TableData)

设置数据到差量中去

func SetDataModel2

func SetDataModel2(md IDataBaseModel, msgdata *protomsg.TableData, args *DataArgs)

设置数据到差量中去 包含标脏

func SetDataModel2ByMsg

func SetDataModel2ByMsg(md IDataBaseModel, msg *protomsg.DBDataModel, args *DataArgs)

从消息中设置数据差量 包含标脏

func SetDataModelByMsg

func SetDataModelByMsg(md IDataBaseModel, msg *protomsg.DBDataModel)

从消息中设置数据差量

func UnPackMsgDataToField

func UnPackMsgDataToField(f reflect.Value, msgdata *protomsg.FieldData)

Types

type DBPlayer

type DBPlayer struct {

	//是否加载同步模块完成
	IsLoadAllSync bool
	//是否加载所有模块
	IsLoadAll bool
	//模块列表
	ModelList map[string]IServiceModel
	//模块的加载顺序列表
	LoadLi []IServiceModel
	//延时加载的模块
	DelayLoadLi []IServiceModel
	//与他服系统通信的 模块列表
	OtherLoadLi []IServiceModel
	// contains filtered or unexported fields
}

这个对象是放在每个玩家身上的,可以用来做为管理系统使用 按照设计所有对象都应该放在这里管理 他们都实现了IServiceModel接口

func NewDBPlayer

func NewDBPlayer(player IServicePlayer, sid uint64, f func()) *DBPlayer

func (*DBPlayer) AddOtherModel

func (this *DBPlayer) AddOtherModel(v IServiceModel)

添加模块进 他服通信 模块列表

func (*DBPlayer) AddServiceModel

func (this *DBPlayer) AddServiceModel(v IServiceModel)

添加的时候,按加载的顺序进行添加

func (*DBPlayer) AllDataSync

func (this *DBPlayer) AllDataSync()

全数据同步

func (*DBPlayer) AllLoaded

func (this *DBPlayer) AllLoaded()

所有系统都加载完成后的调用,如果需要在用户级可以重载

func (*DBPlayer) DelayLoads

func (this *DBPlayer) DelayLoads()

启动延时加载

func (*DBPlayer) GetArgs

func (this *DBPlayer) GetArgs() *DataArgs

func (*DBPlayer) Init

func (this *DBPlayer) Init(tmpli map[string]IDataBaseModel) (ok bool)

先加载试试,如果加载失败就走回调,如果加载完成就返回true

func (*DBPlayer) Loop

func (this *DBPlayer) Loop()

func (*DBPlayer) PopBackFunc

func (this *DBPlayer) PopBackFunc(key uint64) GetData

获取回调方法,获取的时候会从集合里移除

func (*DBPlayer) Save

func (this *DBPlayer) Save()

保存脏数据给dbserver

func (*DBPlayer) SetBackFunc

func (this *DBPlayer) SetBackFunc(key uint64, f GetData)

func (*DBPlayer) SetOnLoaded

func (this *DBPlayer) SetOnLoaded(f func())

type DataArgs

type DataArgs struct {
	// contains filtered or unexported fields
}

func NewDataArgs

func NewDataArgs(pid uint64) *DataArgs

func (*DataArgs) Clear

func (args *DataArgs) Clear()

清空数据

func (*DataArgs) Delete

func (args *DataArgs) Delete(md IDataBaseModel)

func (*DataArgs) GetDirtyData

func (args *DataArgs) GetDirtyData() map[string]IDataBaseModel

获取脏数据

func (*DataArgs) GetModelByKeyName

func (args *DataArgs) GetModelByKeyName(keyname string) IDataBaseModel

使用Keyname获取数据对象

func (*DataArgs) GetMsgByAllData

func (args *DataArgs) GetMsgByAllData(f func(v IDataBaseModel, db *protomsg.DBDataModel) (issync bool)) (result *protomsg.DBUpUserDatasReq)

获取全量更新数据包

func (*DataArgs) GetMsgByForFunc

func (args *DataArgs) GetMsgByForFunc(f func(v IDataBaseModel, db *protomsg.DBDataModel) (issave bool)) (result *protomsg.DBUpUserDatasReq)

返回发给DB服的消息,可以注入遍历时的函数

func (*DataArgs) GetPID

func (args *DataArgs) GetPID() uint64

func (*DataArgs) LoadData

func (args *DataArgs) LoadData(md IDataBaseModel)

加载需要支持差量更新的数据放在这里

func (*DataArgs) RevokeDelete

func (args *DataArgs) RevokeDelete(md IDataBaseModel)

撤销删除

func (*DataArgs) SetUpdate

func (args *DataArgs) SetUpdate(md IDataBaseModel, k string, v interface{}) error

设置脏字段,部分Field更新

func (*DataArgs) Update

func (args *DataArgs) Update(md IDataBaseModel) error

全量更新

func (*DataArgs) UpdateField

func (args *DataArgs) UpdateField(keyname, field string) error

新的使用字段名就可以更新数据的逻辑

type DataUtil

type DataUtil struct {
	// contains filtered or unexported fields
}

func NewRedisUtilDBData

func NewRedisUtilDBData(pid uint64) (result *DataUtil)

对象的redis操作集合

func (*DataUtil) DelDataArray

func (utilmd *DataUtil) DelDataArray(mli []*protomsg.DBDataModel)

删除标记为需要删的数据行

func (*DataUtil) DelDatas

func (utilmd *DataUtil) DelDatas(tname string, keynames []string)

删除指定表的一些行

func (*DataUtil) GetFieldKey

func (utilmd *DataUtil) GetFieldKey(id uint64, modname string) (key string)

获取字段Key

func (*DataUtil) GetKeySplitToFieldName

func (utilmd *DataUtil) GetKeySplitToFieldName(key string) (id uint64, field string)

获取字段名字和主键ID

func (*DataUtil) Key

func (utilmd *DataUtil) Key(tname string) string

func (*DataUtil) RowsSingleDels

func (utilmd *DataUtil) RowsSingleDels(table string, uid uint64, li map[string]reflect.Value)

func (*DataUtil) RowsSingleGet

func (utilmd *DataUtil) RowsSingleGet(table string) (result map[string][]byte, err error)

多行数据:一个用户有多行,同时支持标脏字段的操作方法 以下的方法是针对一个用户多行数据的,每个字段都需要单独更新的时候,才使用的的逻辑

[多行数据]获取

func (*DataUtil) RowsSingleSet

func (utilmd *DataUtil) RowsSingleSet(table string, pid, uid uint64, li map[string]reflect.Value)

[多行数据]写入

func (*DataUtil) SetDatas

func (utilmd *DataUtil) SetDatas(pmsgs []*protomsg.DBDataModel) error

批量设置行数据

type DelayLoadMgr

type DelayLoadMgr struct {
	IServiceModel
	// contains filtered or unexported fields
}

延时加载的处理器

func (*DelayLoadMgr) DelayLoad

func (this *DelayLoadMgr) DelayLoad(tmpli map[string]IDataBaseModel) bool

收到消息回调

func (*DelayLoadMgr) SendDBLoadMsg

func (this *DelayLoadMgr) SendDBLoadMsg(user *DBPlayer)

单独发本系统的数据请求

type GetData

type GetData func(tmpli map[string]IDataBaseModel) bool

拿到数数的回调

type GetMsgForFunc

type GetMsgForFunc func(v IDataBaseModel, db *protomsg.DBDataModel) (issave bool)

用于ModuleBase在SyncData里对单个数据进行生成数据的回调函数 issave 为true表示需要保存在DB;

type IDataBaseModel

type IDataBaseModel interface {
	IDeleted
	// KeyName 数据的唯一名字
	KeyName() string
	// GetPlayerID 用户主键
	GetPlayerID() uint64
	// TableName 拿到表名
	TableName() string
	// Marshal 序列化
	Marshal() ([]byte, error)
	// Unmarshal 反序列化
	Unmarshal(data []byte) error
	//是否支持部分更新
	GetIsPartial() bool
}

IDataBaseModel 可以被DB管理器管理的类需要实现的接口

type IDeleted

type IDeleted interface {
	IsDeleted() bool
	SetDelete()
	//撤销删除
	RevokeDelete()
}

type IPlayer

type IPlayer interface {
	GetDBID() uint64
	SetBackFunc(key uint64, f GetData)
	PopBackFunc(key uint64) GetData //获取回调方法,获取的时候会从集合里移除
}

Player接口

type IPlayerSubBase

type IPlayerSubBase interface {
	IDataBaseModel
	//获取主键ID
	GetID() uint64
	//设置主键ID
	SetID(id uint64)
}

支持一表多行的数据,每一行都会有一个自己的ID

type IReal

type IReal interface {
	IDataBaseModel
	GetMDReal() IDataBaseModel
}

放在dataargs中的对象可能是最外面的对象 如果是最多面的对象,需要可以获取DB使用的对象 用与DBResolveModel分解字段的时候,使用

type IRedisValuer

type IRedisValuer interface {
	RedisValue() (driver.Value, error)
}

用与redis保存数据的接口

type IServiceModel

type IServiceModel interface {
	//系统名字
	GetServiceName() string
	//初始化
	LoadDB(lis map[string][]IDataBaseModel)
	// //redis加载方式,如果加载成功就返回true
	// LoadRedis() bool
	//从redis获取数据
	GetRedisTableDataAllDoMD(tnames []string) (result map[string][]IDataBaseModel, err error)
	//redis加载方式,如果加载成功就返回true
	LoadData(lis map[string][]IDataBaseModel)
	//需要的表名
	GetTableNames() []string
	//是否加载过
	GetIsLoaded() bool
	//他服逻辑-同步数据
	OtherData(msg *protomsg.DBUpUserDatasReq)
	//同步数据到客户端;到redis;到dbserver
	SyncData()
	//全数据同步到客户端
	AllDataSyncClient()
	//他服数据请求
	OtherDataReqSrv() []*protomsg.DBDataModel
	//设置脏数据收集器
	SetDataArgs(args *DataArgs)
	//获取脏数据收集器
	GetDataArgs() *DataArgs
	//是否延时加载
	IsDelayLoad() bool
	//是否为单一表加载,并有Limit,如果没有限制,Limit为-1
	IsSingleTable() (result bool, limit int32)
}

系统模块接口,

type IServicePlayer

type IServicePlayer interface {
	IPlayer
	entity.IEntity
	IServiceModel
	//支持回调
	CallDelay(cb func()) error
	//所有系统都加载完成后的调用
	AllLoaded()
	//消息注册
	RegMsgProc(interface{})
}

服务器上的PlayerNtt对象

type ISetArgs

type ISetArgs interface {
	//设置标脏
	SetArgs(args msgdef.IArgs)
}

type Iprotomsg

type Iprotomsg interface {
	msgdef.IMsg
	Marshal() ([]byte, error)
}

type MapModel

type MapModel struct {
	Data map[string]reflect.Value

	Table     string
	DeletedAt gorm.DeletedAt `gorm:"index"`
	// contains filtered or unexported fields
}

map的model,用来做部分字段更新

func NewMapModel

func NewMapModel(keyname, table string) (result *MapModel)

func (*MapModel) GetID

func (this *MapModel) GetID() uint64

获取主键ID

func (*MapModel) GetIsPartial

func (this *MapModel) GetIsPartial() bool

是否支持部分更新

func (*MapModel) GetPlayerID

func (this *MapModel) GetPlayerID() uint64

GetPlayerID 用户主键

func (*MapModel) IsDeleted

func (this *MapModel) IsDeleted() bool

func (*MapModel) KeyName

func (this *MapModel) KeyName() string

KeyName 数据的唯一名字

func (*MapModel) Marshal

func (this *MapModel) Marshal() ([]byte, error)

Marshal 序列化

func (*MapModel) RevokeDelete

func (this *MapModel) RevokeDelete()

撤销删除

func (*MapModel) SetData

func (this *MapModel) SetData(k string, v reflect.Value)

设置字段数据

func (*MapModel) SetDelete

func (this *MapModel) SetDelete()

func (*MapModel) SetID

func (this *MapModel) SetID(id uint64)

设置主键ID

func (*MapModel) SetPlayerID

func (this *MapModel) SetPlayerID(v uint64)

func (*MapModel) TableName

func (this *MapModel) TableName() string

TableName 拿到表名

func (*MapModel) Unmarshal

func (this *MapModel) Unmarshal(data []byte) error

Unmarshal 反序列化

type ModuleBase

type ModuleBase struct {
	Redisutil     *DataUtil     //redis操作
	IsLoad        bool          //是否加载过
	Args          *DataArgs     //标脏收集器
	CallBreakFunc GetMsgForFunc //用于ModuleBase在SyncData里对单个数据进行生成数据的回调函数
	// contains filtered or unexported fields
}

实现IServiceModel接口的基类

func NewModuleBase

func NewModuleBase(pid uint64) *ModuleBase

func (*ModuleBase) AllDataSyncClient

func (this *ModuleBase) AllDataSyncClient()

全数据同步到客户端

func (*ModuleBase) DelRedisDatas

func (this *ModuleBase) DelRedisDatas(li map[string]IDataBaseModel, dbli []*protomsg.DBDataModel)

删redis里的数据

func (*ModuleBase) GetDataArgs

func (this *ModuleBase) GetDataArgs() *DataArgs

获取脏数据收集器

func (*ModuleBase) GetIsLoaded

func (this *ModuleBase) GetIsLoaded() bool

是否加载过

func (*ModuleBase) GetRedisTableDataAllDoMD

func (this *ModuleBase) GetRedisTableDataAllDoMD(tnames []string) (result map[string][]IDataBaseModel, err error)

从redis里获取有关表的所有数据

func (*ModuleBase) GetRedisTableDataDoMD

func (this *ModuleBase) GetRedisTableDataDoMD(tname string) (result []IDataBaseModel, err error)

从redis中读数据,支持分字段的结构

func (*ModuleBase) GetServiceName

func (this *ModuleBase) GetServiceName() string

系统名字

func (*ModuleBase) GetTableNames

func (this *ModuleBase) GetTableNames() []string

需要的表名

func (*ModuleBase) IsDelayLoad

func (this *ModuleBase) IsDelayLoad() bool

是否延时加载

func (*ModuleBase) IsSingleTable

func (this *ModuleBase) IsSingleTable() (result bool, limit int32)

是否为单一表加载,并有Limit,如果没有限制,Limit为-1

func (*ModuleBase) LoadDB

func (this *ModuleBase) LoadDB(lis map[string][]IDataBaseModel)

初始化

func (*ModuleBase) LoadData

func (this *ModuleBase) LoadData(lis map[string][]IDataBaseModel)

同步加载的过程,一般是从redis里读出来后,再进行这个过程,不会失败

func (*ModuleBase) OtherData

func (this *ModuleBase) OtherData(*protomsg.DBUpUserDatasReq)

他服逻辑-同步数据

func (*ModuleBase) OtherDataReqSrv

func (this *ModuleBase) OtherDataReqSrv() []*protomsg.DBDataModel

请求他服数据

func (*ModuleBase) SaveRedis

func (this *ModuleBase) SaveRedis(li map[string]IDataBaseModel, msg *protomsg.DBUpUserDatasReq)

保存数据到redis

func (*ModuleBase) SaveRedisDatas

func (this *ModuleBase) SaveRedisDatas(li map[string]IDataBaseModel, dbli []*protomsg.DBDataModel)

func (*ModuleBase) SetDataArgs

func (this *ModuleBase) SetDataArgs(args *DataArgs)

设置脏数据收集器

func (*ModuleBase) SetRedisTableDataDoByte

func (this *ModuleBase) SetRedisTableDataDoByte(data *MapModel) (err error)

写入redis,支持分字段的结构

func (*ModuleBase) SyncData

func (this *ModuleBase) SyncData()

同步数据到客户端;到redis;到dbserver

type NewTableFunc

type NewTableFunc func() IDataBaseModel

新建表

type OtherSrvMsgProc

type OtherSrvMsgProc struct {
	// contains filtered or unexported fields
}

用来收DB来的数据

func NewOtherSrvMsgProc

func NewOtherSrvMsgProc(pmd IPlayer) *OtherSrvMsgProc

func (*OtherSrvMsgProc) MsgProc_DBGetPlayerInfoAck

func (this *OtherSrvMsgProc) MsgProc_DBGetPlayerInfoAck(imsg msgdef.IMsg)

func (*OtherSrvMsgProc) MsgProc_DBGetPlayerInfoEndAck

func (this *OtherSrvMsgProc) MsgProc_DBGetPlayerInfoEndAck(imsg msgdef.IMsg)

func (*OtherSrvMsgProc) MsgProc_DBUpUserDatasReq

func (this *OtherSrvMsgProc) MsgProc_DBUpUserDatasReq(imsg msgdef.IMsg)

收到他服的update数据

type PlayerBase

type PlayerBase struct {
	PID       uint64 `gorm:"primarykey;column:pid"`
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt gorm.DeletedAt `gorm:"index"`
}

PlayerBase 一个角色一行数据的表基类

func (*PlayerBase) GetIsPartial

func (md *PlayerBase) GetIsPartial() bool

是否支持部分更新

func (*PlayerBase) GetPlayerID

func (md *PlayerBase) GetPlayerID() uint64

func (*PlayerBase) IsDeleted

func (md *PlayerBase) IsDeleted() bool

func (*PlayerBase) RevokeDelete

func (md *PlayerBase) RevokeDelete()

撤销删除

func (*PlayerBase) SetDelete

func (md *PlayerBase) SetDelete()

type PlayerSubBase

type PlayerSubBase struct {
	ID        uint64 `gorm:"primarykey"`
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt gorm.DeletedAt `gorm:"index"`
	PID       uint64         `gorm:"index;column:pid"`
}

PlayerSubBase 角色下的表基类

func (*PlayerSubBase) GetID

func (md *PlayerSubBase) GetID() uint64

func (*PlayerSubBase) GetIsPartial

func (md *PlayerSubBase) GetIsPartial() bool

(每个子系统都需要检查这个值返回的)是否支持单个字段更新

func (*PlayerSubBase) GetPlayerID

func (md *PlayerSubBase) GetPlayerID() uint64

func (*PlayerSubBase) IsDeleted

func (md *PlayerSubBase) IsDeleted() bool

func (*PlayerSubBase) RevokeDelete

func (md *PlayerSubBase) RevokeDelete()

撤销删除

func (*PlayerSubBase) SetDelete

func (md *PlayerSubBase) SetDelete()

func (*PlayerSubBase) SetID

func (md *PlayerSubBase) SetID(id uint64)

type TypeMgr

type TypeMgr struct {
	// contains filtered or unexported fields
}

TypeMgr 管理获取和保存需要落盘数据的管理器

func GetDBSingleton

func GetDBSingleton() *TypeMgr

GetDBSingleton 拿到获取数据的管理器

func (*TypeMgr) GetModelByKeyName

func (mgr *TypeMgr) GetModelByKeyName(keyname string) IDataBaseModel

GetModelByKeyName 按KeyName拿新实例

func (*TypeMgr) GetPlayerMainTableName

func (mgr *TypeMgr) GetPlayerMainTableName() string

GetPlayerMainTableName 拿到主表的名字

func (*TypeMgr) GetTableList

func (mgr *TypeMgr) GetTableList() map[string]NewTableFunc

GetTableList 拿到所有表

func (*TypeMgr) GetTableNames

func (mgr *TypeMgr) GetTableNames() []string

GetTableNames 拿到全部表名

func (*TypeMgr) GetTypeByTname

func (mgr *TypeMgr) GetTypeByTname(tname string) IDataBaseModel

GetTypeByTname 按表拿到新实例

func (TypeMgr) GetValueByTname

func (mgr TypeMgr) GetValueByTname(tname string) reflect.Value

func (*TypeMgr) RegisterType

func (mgr *TypeMgr) RegisterType(f NewTableFunc)

RegisterType 注册表类型

type ValueMD

type ValueMD struct {
	reflect.Value
	Model IDataBaseModel
}

缓存的可以标脏的数据

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL