升级db系统 维护多数据节点连接实例

This commit is contained in:
liwei1dao 2022-08-09 13:35:23 +08:00
parent de510f42ac
commit 5e2a9fdf45
27 changed files with 140 additions and 1319 deletions

View File

@ -13,7 +13,6 @@ import (
"go_dreamfactory/modules/items"
"go_dreamfactory/modules/user"
"go_dreamfactory/services"
"go_dreamfactory/sys/cache"
"go_dreamfactory/sys/configure"
"go_dreamfactory/sys/db"
"os"
@ -35,11 +34,6 @@ type TestService struct {
//初始化相关系统
func (this *TestService) InitSys() {
this.RPCXService.InitSys()
if err := cache.OnInit(this.GetSettings().Sys["cache"]); err != nil {
panic(fmt.Sprintf("init sys.cache err: %s", err.Error()))
} else {
log.Infof("init sys.cache success!")
}
if err := db.OnInit(this.GetSettings().Sys["db"]); err != nil {
panic(fmt.Sprintf("init sys.db err: %s", err.Error()))
} else {

View File

@ -12,7 +12,6 @@ import (
"go_dreamfactory/lego/utils/codec"
"go_dreamfactory/lego/utils/codec/codecore"
"go_dreamfactory/lego/utils/codec/json"
"go_dreamfactory/sys/cache"
"go_dreamfactory/sys/db"
"reflect"
"unsafe"
@ -148,8 +147,9 @@ DB组件也封装进来
*/
type MCompModel struct {
cbase.ModuleCompBase
Redis redis.ISys
DB mgo.ISys
Redis redis.ISys
DB mgo.ISys
TableName string //redis key前缀
getListSha1 string //getList LusScript 的shal值
setListSha1 string //getList LusScript 的shal值
@ -165,8 +165,8 @@ const (
//组件初始化接口
func (this *MCompModel) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.ModuleCompBase.Init(service, module, comp, options)
this.Redis = cache.Redis()
this.DB = db.Mgo()
this.Redis = db.Local().Redis
this.DB = db.Local().Mgo
return
}
func (this *MCompModel) Start() (err error) {
@ -657,28 +657,6 @@ func (this *MCompModel) BatchDelLists(uid string) (err error) {
return
}
//日志操作可选项
func (this *MCompModel) logOpt(uid string, data interface{}, attrs ...*cache.OperationAttr) error {
ret := cache.OperationAttrs(attrs).Find(cache.ATTR_MGOLOG).Unwrap_Or(nil)
if ret == nil { //启用mgolog
ir := cache.OperationAttrs(attrs).Find(cache.ATTR_INSERT).Unwrap_Or(nil)
if ir == nil { //updte opt
where := bson.M{"uid": uid}
if reflect.ValueOf(data).Kind() == reflect.Map {
if m, ok := data.(map[string]interface{}); ok {
where["_id"] = m["_id"]
} else {
return fmt.Errorf("have %v,but want map[string]interface{}", data)
}
}
return this.UpdateModelLogs(this.TableName, uid, where, data)
} else { //insert opt
return this.InsertModelLogs(this.TableName, uid, data)
}
}
return nil
}
// 删除玩家缓存信息
func (this *MCompModel) CleanUserRecord(uid string) (err error) {
err = this.Redis.Delete(this.ukey(uid))

View File

@ -14,7 +14,6 @@ import (
"go_dreamfactory/modules/user"
"go_dreamfactory/pb"
"go_dreamfactory/services"
"go_dreamfactory/sys/cache"
"go_dreamfactory/sys/configure"
"go_dreamfactory/sys/db"
"os"
@ -38,11 +37,6 @@ type TestService struct {
//初始化相关系统
func (this *TestService) InitSys() {
this.RPCXService.InitSys()
if err := cache.OnInit(this.GetSettings().Sys["cache"]); err != nil {
panic(fmt.Sprintf("init sys.cache err: %s", err.Error()))
} else {
log.Infof("init sys.cache success!")
}
if err := db.OnInit(this.GetSettings().Sys["db"]); err != nil {
panic(fmt.Sprintf("init sys.db err: %s", err.Error()))
} else {

View File

@ -9,7 +9,6 @@ import (
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/modules/forum"
"go_dreamfactory/services"
"go_dreamfactory/sys/cache"
"go_dreamfactory/sys/configure"
"go_dreamfactory/sys/db"
"os"
@ -31,11 +30,6 @@ type TestService struct {
//初始化相关系统
func (this *TestService) InitSys() {
this.RPCXService.InitSys()
if err := cache.OnInit(this.GetSettings().Sys["cache"]); err != nil {
panic(fmt.Sprintf("init sys.cache err: %s", err.Error()))
} else {
log.Infof("init sys.cache success!")
}
if err := db.OnInit(this.GetSettings().Sys["db"]); err != nil {
panic(fmt.Sprintf("init sys.db err: %s", err.Error()))
} else {

View File

@ -8,7 +8,6 @@ import (
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/services"
"go_dreamfactory/sys/cache"
"go_dreamfactory/sys/configure"
"go_dreamfactory/sys/db"
"os"
@ -30,11 +29,6 @@ type TestService struct {
//初始化相关系统
func (this *TestService) InitSys() {
this.RPCXService.InitSys()
if err := cache.OnInit(this.GetSettings().Sys["cache"]); err != nil {
panic(fmt.Sprintf("init sys.cache err: %s", err.Error()))
} else {
log.Infof("init sys.cache success!")
}
if err := db.OnInit(this.GetSettings().Sys["db"]); err != nil {
panic(fmt.Sprintf("init sys.db err: %s", err.Error()))
} else {

View File

@ -13,7 +13,6 @@ import (
"go_dreamfactory/modules/items"
"go_dreamfactory/modules/user"
"go_dreamfactory/services"
"go_dreamfactory/sys/cache"
"go_dreamfactory/sys/configure"
"go_dreamfactory/sys/db"
"os"
@ -35,11 +34,6 @@ type TestService struct {
//初始化相关系统
func (this *TestService) InitSys() {
this.RPCXService.InitSys()
if err := cache.OnInit(this.GetSettings().Sys["cache"]); err != nil {
panic(fmt.Sprintf("init sys.cache err: %s", err.Error()))
} else {
log.Infof("init sys.cache success!")
}
if err := db.OnInit(this.GetSettings().Sys["db"]); err != nil {
panic(fmt.Sprintf("init sys.db err: %s", err.Error()))
} else {

View File

@ -15,7 +15,6 @@ import (
"go_dreamfactory/modules/user"
"go_dreamfactory/pb"
"go_dreamfactory/services"
"go_dreamfactory/sys/cache"
"go_dreamfactory/sys/configure"
"go_dreamfactory/sys/db"
"os"
@ -43,11 +42,6 @@ func newService(ops ...rpcx.Option) core.IService {
//初始化相关系统
func (this *TestService) InitSys() {
this.RPCXService.InitSys()
if err := cache.OnInit(this.GetSettings().Sys["cache"], cache.Set_Redis_Addr([]string{"10.0.0.9:10011"}), cache.Set_Redis_Password("li13451234"), cache.Set_Redis_DB(1)); err != nil {
panic(fmt.Sprintf("init sys.cache err: %s", err.Error()))
} else {
log.Infof("init sys.cache success!")
}
if err := db.OnInit(this.GetSettings().Sys["db"]); err != nil {
panic(fmt.Sprintf("init sys.db err: %s", err.Error()))
} else {

View File

@ -15,7 +15,6 @@ import (
"go_dreamfactory/modules/user"
"go_dreamfactory/pb"
"go_dreamfactory/services"
"go_dreamfactory/sys/cache"
"go_dreamfactory/sys/configure"
"go_dreamfactory/sys/db"
"os"
@ -39,11 +38,6 @@ type TestService struct {
//初始化相关系统
func (this *TestService) InitSys() {
this.RPCXService.InitSys()
if err := cache.OnInit(this.GetSettings().Sys["cache"]); err != nil {
panic(fmt.Sprintf("init sys.cache err: %s", err.Error()))
} else {
log.Infof("init sys.cache success!")
}
if err := db.OnInit(this.GetSettings().Sys["db"]); err != nil {
panic(fmt.Sprintf("init sys.db err: %s", err.Error()))
} else {

View File

@ -9,7 +9,6 @@ import (
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/modules/notify"
"go_dreamfactory/services"
"go_dreamfactory/sys/cache"
"go_dreamfactory/sys/configure"
"go_dreamfactory/sys/db"
"os"
@ -31,11 +30,6 @@ type TestService struct {
//初始化相关系统
func (this *TestService) InitSys() {
this.RPCXService.InitSys()
if err := cache.OnInit(this.GetSettings().Sys["cache"]); err != nil {
panic(fmt.Sprintf("init sys.cache err: %s", err.Error()))
} else {
log.Infof("init sys.cache success!")
}
if err := db.OnInit(this.GetSettings().Sys["db"]); err != nil {
panic(fmt.Sprintf("init sys.db err: %s", err.Error()))
} else {

View File

@ -5,7 +5,6 @@ import (
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/core/cbase"
"go_dreamfactory/lego/sys/redis"
"go_dreamfactory/sys/cache"
"sync"
"time"
)
@ -21,7 +20,6 @@ type ExpiredComp struct {
//组件初始化接口
func (this *ExpiredComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) {
this.ModuleCompBase.Init(service, module, comp, opt)
this.redis = cache.Redis()
this.data = make(map[string]*ModelDataExpired, 1024)
return
}

View File

@ -15,7 +15,6 @@ import (
"go_dreamfactory/modules/user"
"go_dreamfactory/pb"
"go_dreamfactory/services"
"go_dreamfactory/sys/cache"
"go_dreamfactory/sys/configure"
"go_dreamfactory/sys/db"
"os"
@ -39,11 +38,6 @@ type TestService struct {
//初始化相关系统
func (this *TestService) InitSys() {
this.RPCXService.InitSys()
if err := cache.OnInit(this.GetSettings().Sys["cache"]); err != nil {
panic(fmt.Sprintf("init sys.cache err: %s", err.Error()))
} else {
log.Infof("init sys.cache success!")
}
if err := db.OnInit(this.GetSettings().Sys["db"]); err != nil {
panic(fmt.Sprintf("init sys.db err: %s", err.Error()))
} else {

View File

@ -8,7 +8,6 @@ import (
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/services"
"go_dreamfactory/sys/cache"
"go_dreamfactory/sys/configure"
"go_dreamfactory/sys/db"
"os"
@ -34,11 +33,6 @@ func newService(ops ...rpcx.Option) core.IService {
//初始化相关系统
func (this *TestService) InitSys() {
this.RPCXService.InitSys()
if err := cache.OnInit(this.GetSettings().Sys["cache"]); err != nil {
panic(fmt.Sprintf("init sys.cache err: %s", err.Error()))
} else {
log.Infof("init sys.cache success!")
}
if err := db.OnInit(this.GetSettings().Sys["db"]); err != nil {
panic(fmt.Sprintf("init sys.db err: %s", err.Error()))
} else {

View File

@ -6,7 +6,6 @@ import (
"go_dreamfactory/modules/mgolog"
"go_dreamfactory/modules/web"
"go_dreamfactory/services"
"go_dreamfactory/sys/cache"
"go_dreamfactory/sys/db"
"go_dreamfactory/lego"
@ -53,12 +52,6 @@ type Service struct {
//初始化worker需要的一些系统工具
func (this *Service) InitSys() {
this.ServiceBase.InitSys()
//缓存系统
if err := cache.OnInit(this.GetSettings().Sys["cache"]); err != nil {
panic(fmt.Sprintf("init sys.cache err: %s", err.Error()))
} else {
log.Infof("init sys.cache success!")
}
//存储系统
if err := db.OnInit(this.GetSettings().Sys["db"]); err != nil {
panic(fmt.Sprintf("init sys.db err: %s", err.Error()))

View File

@ -16,7 +16,6 @@ import (
"go_dreamfactory/modules/task"
"go_dreamfactory/modules/user"
"go_dreamfactory/services"
"go_dreamfactory/sys/cache"
"go_dreamfactory/sys/db"
"go_dreamfactory/lego"
@ -73,12 +72,6 @@ type Service struct {
//初始化worker需要的一些系统工具
func (this *Service) InitSys() {
this.ServiceBase.InitSys()
//缓存系统
if err := cache.OnInit(this.GetSettings().Sys["cache"]); err != nil {
panic(fmt.Sprintf("init sys.cache err: %s", err.Error()))
} else {
log.Infof("init sys.cache success!")
}
//存储系统
if err := db.OnInit(this.GetSettings().Sys["db"]); err != nil {
panic(fmt.Sprintf("init sys.db err: %s", err.Error()))

41
sys/cache/cache.go vendored
View File

@ -1,41 +0,0 @@
package cache
import "go_dreamfactory/lego/sys/redis"
/*
缓存系统的基础结构 包含系统的配置阐述以及 底层操作redis的 redis.ISys 对象
*/
func newSys(options Options) (sys *Cache, err error) {
sys = &Cache{options: options}
err = sys.init()
return
}
type Cache struct {
options Options
redis redis.ISys
}
//初始化 redis 对象
func (this *Cache) init() (err error) {
if this.options.Redis_IsCluster {
this.redis, err = redis.NewSys(
redis.SetRedisType(redis.Redis_Cluster),
redis.SetRedis_Cluster_Addr(this.options.Redis_Addr),
redis.SetRedis_Cluster_Password(this.options.Redis_Password))
} else {
this.redis, err = redis.NewSys(
redis.SetRedisType(redis.Redis_Single),
redis.SetRedis_Single_Addr(this.options.Redis_Addr[0]),
redis.SetRedis_Single_Password(this.options.Redis_Password),
redis.SetRedis_Single_DB(this.options.Redis_DB),
)
}
return
}
//初始化 redis 对象
func (this *Cache) Redis() redis.ISys {
return this.redis
}

43
sys/cache/core.go vendored
View File

@ -1,43 +0,0 @@
package cache
import "go_dreamfactory/lego/sys/redis"
/*
redis 缓存数据管理系统
*/
type (
ISys interface {
Redis() redis.ISys
}
)
const ()
//全局使用的系统对象
var Defsys ISys
//初始化缓存系统
func OnInit(config map[string]interface{}, option ...Option) (err error) {
var options Options
if options, err = newOptions(config, option...); err != nil {
return
}
Defsys, err = newSys(options)
return
}
//系统实例化接口 每个系统默认都会提供全局以及实例化的接口
func NewSys(option ...Option) (sys ISys, err error) {
var options Options
if options, err = newOptionsByOption(option...); err != nil {
return
}
Defsys, err = newSys(options)
return
}
//系统实例化接口 每个系统默认都会提供全局以及实例化的接口
func Redis() redis.ISys {
return Defsys.Redis()
}

View File

@ -1,97 +0,0 @@
package cache_test
import (
"fmt"
"go_dreamfactory/sys/cache"
"go_dreamfactory/sys/db"
"os"
"testing"
"time"
)
//测试环境下初始化db和cache 系统
func TestMain(m *testing.M) {
if err := db.OnInit(nil, db.Set_MongodbUrl("mongodb://admin:123456@10.0.0.9:27018"), db.Set_MongodbDatabase("dreamfactory")); err != nil {
fmt.Printf("err:%v\n", err)
return
}
if err := cache.OnInit(nil, cache.Set_Redis_Addr([]string{"10.0.0.9:9001", "10.0.0.9:9002", "10.0.0.9:9003", "10.0.1.45:9004", "10.0.1.45:9005", "10.0.1.45:9006"}), cache.Set_Redis_Password("")); err != nil {
fmt.Printf("err:%v\n", err)
return
}
for i := 0; i < 50000; i++ {
//go func() {
// _mail := &pb.DB_MailData{
// ObjId: primitive.NewObjectID().Hex(),
// Uid: "uid123",
// Title: "系统邮件",
// Contex: "恭喜获得专属礼包一份",
// CreateTime: uint64(time.Now().Unix()),
// DueTime: uint64(time.Now().Unix()) + 30*24*3600,
// Check: false,
// Reward: false,
// }
// //db.InsertModelLogs("mail", "uid123", _mail)
// db.Defsys.Mgo().InsertOne("mail", _mail)
// data := &comm.Autogenerated{
// ID: primitive.NewObjectID().Hex(),
// UID: "uid123",
// Act: string(comm.LogHandleType_Insert),
// }
// data.D = append(data.D, "mail") // D[0]
// data.D = append(data.D, _mail) // D[1]
// _, err1 := db.Defsys.Mgo().InsertOne("model_log", data)
// if err1 != nil {
// log.Errorf("insert model db err %v", err1)
// }
// //}()
// ///////////////////////////////////////
// filter := bson.M{
// "userid": "uid123",
// "title": "系统邮件",
// }
// var nd *pb.DB_MailData
// err := db.Defsys.Mgo().FindOne("mail", filter).Decode(&nd)
// if err == nil {
// nd.Check = true
// nd.Reward = true
// data1 := &comm.Autogenerated{
// ID: primitive.NewObjectID().Hex(),
// UID: "uid123",
// Act: string(comm.LogHandleType_Update),
// }
// filter1 := bson.M{
// "userid": "uid123",
// "title": "系统邮件",
// }
// var ndmodify = &bson.M{
// "title": "xxxxx",
// "check": true,
// }
// data1.D = make([]interface{}, 0)
// data1.D = append(data1.D, "mail") // D[0]
// data1.D = append(data1.D, &filter1) // D[1]
// data1.D = append(data1.D, ndmodify) // D[2]
// nd.Title = "read"
// //db.Defsys.Mgo().UpdateMany("mail", filter1, bson.M{"$set": nd})
// _, err = db.Defsys.Mgo().InsertOne("model_log", data1)
// if err != nil {
// log.Errorf("insert model db err %v", err)
// }
// }
}
time.Sleep(time.Second * 10)
defer os.Exit(m.Run())
}
func TestSet(t *testing.T) {
}

View File

@ -1,17 +0,0 @@
package cache
type InterfaceResult struct {
Result interface{}
Err error
}
func NewInterfaceResult(result interface{}, err error) *InterfaceResult {
return &InterfaceResult{Result: result, Err: err}
}
func (ths *InterfaceResult) Unwrap_Or(v interface{}) interface{} {
if ths.Err != nil {
return v
}
return ths.Result
}

View File

@ -1,61 +0,0 @@
package cache
import (
"fmt"
"time"
)
const (
ATTR_EXPIRE = "expire"
ATTR_MGOLOG = "mgolog"
ATTR_INSERT = "insert"
OpenMgoLog = false //true表示不禁用Mgolog即不传入WithDisabledMgoLog()时也表示禁用
)
type empty struct {
}
type OperationAttr struct {
Name string
Value interface{}
}
type OperationAttrs []*OperationAttr
func (this OperationAttrs) Find(name string) *InterfaceResult {
for _, v := range this {
if v.Name == name {
return NewInterfaceResult(v.Value, nil)
}
}
if OpenMgoLog {
return NewInterfaceResult(ATTR_MGOLOG, nil)
}
return NewInterfaceResult(nil, fmt.Errorf("Operationattrs not found err: %v", name))
}
//缓存过期时间设置
func WithExpire(t time.Duration) *OperationAttr {
return &OperationAttr{
Name: ATTR_EXPIRE,
Value: t,
}
}
//禁用Mgolog操作
func WithDisabledMgoLog() *OperationAttr {
return &OperationAttr{
Name: ATTR_MGOLOG,
Value: empty{},
}
}
//新记录插入操作
func WithND() *OperationAttr {
return &OperationAttr{
Name: ATTR_INSERT,
Value: empty{},
}
}

77
sys/cache/options.go vendored
View File

@ -1,77 +0,0 @@
package cache
import (
"errors"
"go_dreamfactory/lego/utils/mapstructure"
)
/*
系统启动相关的配置参数定义
*/
type Option func(*Options)
type Options struct {
Redis_IsCluster bool //是否是集群
Redis_Addr []string //redis 的集群地址
Redis_Password string //redis的密码
Redis_DB int //数据库位置
}
//设置系统的集群地址
func Set_Redis_IsCluster(v bool) Option {
return func(o *Options) {
o.Redis_IsCluster = v
}
}
//设置系统的集群地址
func Set_Redis_Addr(v []string) Option {
return func(o *Options) {
o.Redis_Addr = v
}
}
//设置系统的密码配置
func Set_Redis_Password(v string) Option {
return func(o *Options) {
o.Redis_Password = v
}
}
//设置redis的数据存储卷
func Set_Redis_DB(v int) Option {
return func(o *Options) {
o.Redis_DB = v
}
}
//更具 map对象或者Option 序列化 系统参数对象
func newOptions(config map[string]interface{}, opts ...Option) (Options, error) {
options := Options{
Redis_DB: 1,
}
if config != nil {
mapstructure.Decode(config, &options)
}
for _, o := range opts {
o(&options)
}
if options.Redis_Addr == nil || len(options.Redis_Addr) == 0 {
return options, errors.New("Redis_Addr is null")
}
return options, nil
}
//更具 Option 序列化 系统参数对象
func newOptionsByOption(opts ...Option) (Options, error) {
options := Options{
Redis_DB: 1,
}
for _, o := range opts {
o(&options)
}
if options.Redis_Addr == nil || len(options.Redis_Addr) == 0 {
return options, errors.New("Redis_Addr is null")
}
return options, nil
}

View File

@ -1,460 +0,0 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.28.0
// protoc v3.20.0
// source: hero/hero_db.proto
package bench
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
type SkillData struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
SkillID int32 `protobuf:"varint,1,opt,name=skillID,proto3" json:"skillID"`
SkillLv int32 `protobuf:"varint,2,opt,name=skillLv,proto3" json:"skillLv"`
}
func (x *SkillData) Reset() {
*x = SkillData{}
if protoimpl.UnsafeEnabled {
mi := &file_hero_hero_db_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *SkillData) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*SkillData) ProtoMessage() {}
func (x *SkillData) ProtoReflect() protoreflect.Message {
mi := &file_hero_hero_db_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use SkillData.ProtoReflect.Descriptor instead.
func (*SkillData) Descriptor() ([]byte, []int) {
return file_hero_hero_db_proto_rawDescGZIP(), []int{0}
}
func (x *SkillData) GetSkillID() int32 {
if x != nil {
return x.SkillID
}
return 0
}
func (x *SkillData) GetSkillLv() int32 {
if x != nil {
return x.SkillLv
}
return 0
}
type DBHero struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id" bson:"_id"` //ID
Uid string `protobuf:"bytes,2,opt,name=uid,proto3" json:"uid"`
HeroID int32 `protobuf:"varint,3,opt,name=heroID,proto3" json:"heroID" bson:"heroID"` // 英雄的配置表ID
Star int32 `protobuf:"varint,4,opt,name=star,proto3" json:"star"` // 英雄星级
Lv int32 `protobuf:"varint,5,opt,name=lv,proto3" json:"lv"` // 英雄等级
Exp int32 `protobuf:"varint,6,opt,name=exp,proto3" json:"exp"` // 英雄经验
JuexingLv int32 `protobuf:"varint,7,opt,name=juexingLv,proto3" json:"juexingLv" bson:"juexingLv"` //觉醒等级
CaptainSkill int32 `protobuf:"varint,8,opt,name=captainSkill,proto3" json:"captainSkill" bson:"captainSkill"` //队长技能
NormalSkill []*SkillData `protobuf:"bytes,9,rep,name=normalSkill,proto3" json:"normalSkill" bson:"normalSkill"` //普通技能
Property map[string]int32 `protobuf:"bytes,10,rep,name=property,proto3" json:"property" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"varint,2,opt,name=value,proto3"` // 属性相关
AddProperty map[string]int32 `protobuf:"bytes,11,rep,name=addProperty,proto3" json:"addProperty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"varint,2,opt,name=value,proto3" bson:"addProperty"` //附加属性相关
Formation int32 `protobuf:"varint,12,opt,name=formation,proto3" json:"formation"` // 阵型类型
CardType int32 `protobuf:"varint,13,opt,name=cardType,proto3" json:"cardType" bson:"cardType"` //卡片类型(升星卡、经验卡、技能升级卡)
CurSkin int32 `protobuf:"varint,14,opt,name=curSkin,proto3" json:"curSkin" bson:"curSkin"` //当前装备的皮肤ID
Skins []int32 `protobuf:"varint,15,rep,packed,name=skins,proto3" json:"skins"` // 所有皮肤ID
Block bool `protobuf:"varint,16,opt,name=block,proto3" json:"block"` // 锁定
EquipID []string `protobuf:"bytes,17,rep,name=equipID,proto3" json:"equipID" bson:"equipID"` //装备 objID
ResonateNum int32 `protobuf:"varint,18,opt,name=resonateNum,proto3" json:"resonateNum" bson:"resonateNum"` //共鸣次数
DistributionResonate int32 `protobuf:"varint,19,opt,name=distributionResonate,proto3" json:"distributionResonate" bson:"distributionResonate"` //分配的共鸣能量
Energy map[int32]int32 `protobuf:"bytes,20,rep,name=energy,proto3" json:"energy" protobuf_key:"varint,1,opt,name=key,proto3" protobuf_val:"varint,2,opt,name=value,proto3"` // @go_tags(`bson:"energy"`)能量分配到哪里[1,0]
SameCount int32 `protobuf:"varint,21,opt,name=sameCount,proto3" json:"sameCount" bson:"sameCount"` // 卡片叠加数量
SuiteId int32 `protobuf:"varint,22,opt,name=suiteId,proto3" json:"suiteId" bson:"suiteId"` // 套装Id
SuiteExtId int32 `protobuf:"varint,23,opt,name=suiteExtId,proto3" json:"suiteExtId"` // go_tags(`bson:"suiteExtId"`) 扩展套装Id
IsOverlying bool `protobuf:"varint,24,opt,name=isOverlying,proto3" json:"isOverlying"` // go_tags(`bson:"isOverlying"`) 是否允许叠加 默认true
}
func (x *DBHero) Reset() {
*x = DBHero{}
if protoimpl.UnsafeEnabled {
mi := &file_hero_hero_db_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *DBHero) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*DBHero) ProtoMessage() {}
func (x *DBHero) ProtoReflect() protoreflect.Message {
mi := &file_hero_hero_db_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use DBHero.ProtoReflect.Descriptor instead.
func (*DBHero) Descriptor() ([]byte, []int) {
return file_hero_hero_db_proto_rawDescGZIP(), []int{1}
}
func (x *DBHero) GetId() string {
if x != nil {
return x.Id
}
return ""
}
func (x *DBHero) GetUid() string {
if x != nil {
return x.Uid
}
return ""
}
func (x *DBHero) GetHeroID() int32 {
if x != nil {
return x.HeroID
}
return 0
}
func (x *DBHero) GetStar() int32 {
if x != nil {
return x.Star
}
return 0
}
func (x *DBHero) GetLv() int32 {
if x != nil {
return x.Lv
}
return 0
}
func (x *DBHero) GetExp() int32 {
if x != nil {
return x.Exp
}
return 0
}
func (x *DBHero) GetJuexingLv() int32 {
if x != nil {
return x.JuexingLv
}
return 0
}
func (x *DBHero) GetCaptainSkill() int32 {
if x != nil {
return x.CaptainSkill
}
return 0
}
func (x *DBHero) GetNormalSkill() []*SkillData {
if x != nil {
return x.NormalSkill
}
return nil
}
func (x *DBHero) GetProperty() map[string]int32 {
if x != nil {
return x.Property
}
return nil
}
func (x *DBHero) GetAddProperty() map[string]int32 {
if x != nil {
return x.AddProperty
}
return nil
}
func (x *DBHero) GetFormation() int32 {
if x != nil {
return x.Formation
}
return 0
}
func (x *DBHero) GetCardType() int32 {
if x != nil {
return x.CardType
}
return 0
}
func (x *DBHero) GetCurSkin() int32 {
if x != nil {
return x.CurSkin
}
return 0
}
func (x *DBHero) GetSkins() []int32 {
if x != nil {
return x.Skins
}
return nil
}
func (x *DBHero) GetBlock() bool {
if x != nil {
return x.Block
}
return false
}
func (x *DBHero) GetEquipID() []string {
if x != nil {
return x.EquipID
}
return nil
}
func (x *DBHero) GetResonateNum() int32 {
if x != nil {
return x.ResonateNum
}
return 0
}
func (x *DBHero) GetDistributionResonate() int32 {
if x != nil {
return x.DistributionResonate
}
return 0
}
func (x *DBHero) GetEnergy() map[int32]int32 {
if x != nil {
return x.Energy
}
return nil
}
func (x *DBHero) GetSameCount() int32 {
if x != nil {
return x.SameCount
}
return 0
}
func (x *DBHero) GetSuiteId() int32 {
if x != nil {
return x.SuiteId
}
return 0
}
func (x *DBHero) GetSuiteExtId() int32 {
if x != nil {
return x.SuiteExtId
}
return 0
}
func (x *DBHero) GetIsOverlying() bool {
if x != nil {
return x.IsOverlying
}
return false
}
var File_hero_hero_db_proto protoreflect.FileDescriptor
var file_hero_hero_db_proto_rawDesc = []byte{
0x0a, 0x12, 0x68, 0x65, 0x72, 0x6f, 0x2f, 0x68, 0x65, 0x72, 0x6f, 0x5f, 0x64, 0x62, 0x2e, 0x70,
0x72, 0x6f, 0x74, 0x6f, 0x22, 0x3f, 0x0a, 0x09, 0x53, 0x6b, 0x69, 0x6c, 0x6c, 0x44, 0x61, 0x74,
0x61, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x6b, 0x69, 0x6c, 0x6c, 0x49, 0x44, 0x18, 0x01, 0x20, 0x01,
0x28, 0x05, 0x52, 0x07, 0x73, 0x6b, 0x69, 0x6c, 0x6c, 0x49, 0x44, 0x12, 0x18, 0x0a, 0x07, 0x73,
0x6b, 0x69, 0x6c, 0x6c, 0x4c, 0x76, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x07, 0x73, 0x6b,
0x69, 0x6c, 0x6c, 0x4c, 0x76, 0x22, 0xa6, 0x07, 0x0a, 0x06, 0x44, 0x42, 0x48, 0x65, 0x72, 0x6f,
0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64,
0x12, 0x10, 0x0a, 0x03, 0x75, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75,
0x69, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x68, 0x65, 0x72, 0x6f, 0x49, 0x44, 0x18, 0x03, 0x20, 0x01,
0x28, 0x05, 0x52, 0x06, 0x68, 0x65, 0x72, 0x6f, 0x49, 0x44, 0x12, 0x12, 0x0a, 0x04, 0x73, 0x74,
0x61, 0x72, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x73, 0x74, 0x61, 0x72, 0x12, 0x0e,
0x0a, 0x02, 0x6c, 0x76, 0x18, 0x05, 0x20, 0x01, 0x28, 0x05, 0x52, 0x02, 0x6c, 0x76, 0x12, 0x10,
0x0a, 0x03, 0x65, 0x78, 0x70, 0x18, 0x06, 0x20, 0x01, 0x28, 0x05, 0x52, 0x03, 0x65, 0x78, 0x70,
0x12, 0x1c, 0x0a, 0x09, 0x6a, 0x75, 0x65, 0x78, 0x69, 0x6e, 0x67, 0x4c, 0x76, 0x18, 0x07, 0x20,
0x01, 0x28, 0x05, 0x52, 0x09, 0x6a, 0x75, 0x65, 0x78, 0x69, 0x6e, 0x67, 0x4c, 0x76, 0x12, 0x22,
0x0a, 0x0c, 0x63, 0x61, 0x70, 0x74, 0x61, 0x69, 0x6e, 0x53, 0x6b, 0x69, 0x6c, 0x6c, 0x18, 0x08,
0x20, 0x01, 0x28, 0x05, 0x52, 0x0c, 0x63, 0x61, 0x70, 0x74, 0x61, 0x69, 0x6e, 0x53, 0x6b, 0x69,
0x6c, 0x6c, 0x12, 0x2c, 0x0a, 0x0b, 0x6e, 0x6f, 0x72, 0x6d, 0x61, 0x6c, 0x53, 0x6b, 0x69, 0x6c,
0x6c, 0x18, 0x09, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0a, 0x2e, 0x53, 0x6b, 0x69, 0x6c, 0x6c, 0x44,
0x61, 0x74, 0x61, 0x52, 0x0b, 0x6e, 0x6f, 0x72, 0x6d, 0x61, 0x6c, 0x53, 0x6b, 0x69, 0x6c, 0x6c,
0x12, 0x31, 0x0a, 0x08, 0x70, 0x72, 0x6f, 0x70, 0x65, 0x72, 0x74, 0x79, 0x18, 0x0a, 0x20, 0x03,
0x28, 0x0b, 0x32, 0x15, 0x2e, 0x44, 0x42, 0x48, 0x65, 0x72, 0x6f, 0x2e, 0x50, 0x72, 0x6f, 0x70,
0x65, 0x72, 0x74, 0x79, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x08, 0x70, 0x72, 0x6f, 0x70, 0x65,
0x72, 0x74, 0x79, 0x12, 0x3a, 0x0a, 0x0b, 0x61, 0x64, 0x64, 0x50, 0x72, 0x6f, 0x70, 0x65, 0x72,
0x74, 0x79, 0x18, 0x0b, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x18, 0x2e, 0x44, 0x42, 0x48, 0x65, 0x72,
0x6f, 0x2e, 0x41, 0x64, 0x64, 0x50, 0x72, 0x6f, 0x70, 0x65, 0x72, 0x74, 0x79, 0x45, 0x6e, 0x74,
0x72, 0x79, 0x52, 0x0b, 0x61, 0x64, 0x64, 0x50, 0x72, 0x6f, 0x70, 0x65, 0x72, 0x74, 0x79, 0x12,
0x1c, 0x0a, 0x09, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x0c, 0x20, 0x01,
0x28, 0x05, 0x52, 0x09, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x1a, 0x0a,
0x08, 0x63, 0x61, 0x72, 0x64, 0x54, 0x79, 0x70, 0x65, 0x18, 0x0d, 0x20, 0x01, 0x28, 0x05, 0x52,
0x08, 0x63, 0x61, 0x72, 0x64, 0x54, 0x79, 0x70, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x75, 0x72,
0x53, 0x6b, 0x69, 0x6e, 0x18, 0x0e, 0x20, 0x01, 0x28, 0x05, 0x52, 0x07, 0x63, 0x75, 0x72, 0x53,
0x6b, 0x69, 0x6e, 0x12, 0x14, 0x0a, 0x05, 0x73, 0x6b, 0x69, 0x6e, 0x73, 0x18, 0x0f, 0x20, 0x03,
0x28, 0x05, 0x52, 0x05, 0x73, 0x6b, 0x69, 0x6e, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x62, 0x6c, 0x6f,
0x63, 0x6b, 0x18, 0x10, 0x20, 0x01, 0x28, 0x08, 0x52, 0x05, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x12,
0x18, 0x0a, 0x07, 0x65, 0x71, 0x75, 0x69, 0x70, 0x49, 0x44, 0x18, 0x11, 0x20, 0x03, 0x28, 0x09,
0x52, 0x07, 0x65, 0x71, 0x75, 0x69, 0x70, 0x49, 0x44, 0x12, 0x20, 0x0a, 0x0b, 0x72, 0x65, 0x73,
0x6f, 0x6e, 0x61, 0x74, 0x65, 0x4e, 0x75, 0x6d, 0x18, 0x12, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0b,
0x72, 0x65, 0x73, 0x6f, 0x6e, 0x61, 0x74, 0x65, 0x4e, 0x75, 0x6d, 0x12, 0x32, 0x0a, 0x14, 0x64,
0x69, 0x73, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x6f, 0x6e,
0x61, 0x74, 0x65, 0x18, 0x13, 0x20, 0x01, 0x28, 0x05, 0x52, 0x14, 0x64, 0x69, 0x73, 0x74, 0x72,
0x69, 0x62, 0x75, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x6f, 0x6e, 0x61, 0x74, 0x65, 0x12,
0x2b, 0x0a, 0x06, 0x65, 0x6e, 0x65, 0x72, 0x67, 0x79, 0x18, 0x14, 0x20, 0x03, 0x28, 0x0b, 0x32,
0x13, 0x2e, 0x44, 0x42, 0x48, 0x65, 0x72, 0x6f, 0x2e, 0x45, 0x6e, 0x65, 0x72, 0x67, 0x79, 0x45,
0x6e, 0x74, 0x72, 0x79, 0x52, 0x06, 0x65, 0x6e, 0x65, 0x72, 0x67, 0x79, 0x12, 0x1c, 0x0a, 0x09,
0x73, 0x61, 0x6d, 0x65, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x15, 0x20, 0x01, 0x28, 0x05, 0x52,
0x09, 0x73, 0x61, 0x6d, 0x65, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x75,
0x69, 0x74, 0x65, 0x49, 0x64, 0x18, 0x16, 0x20, 0x01, 0x28, 0x05, 0x52, 0x07, 0x73, 0x75, 0x69,
0x74, 0x65, 0x49, 0x64, 0x12, 0x1e, 0x0a, 0x0a, 0x73, 0x75, 0x69, 0x74, 0x65, 0x45, 0x78, 0x74,
0x49, 0x64, 0x18, 0x17, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0a, 0x73, 0x75, 0x69, 0x74, 0x65, 0x45,
0x78, 0x74, 0x49, 0x64, 0x12, 0x20, 0x0a, 0x0b, 0x69, 0x73, 0x4f, 0x76, 0x65, 0x72, 0x6c, 0x79,
0x69, 0x6e, 0x67, 0x18, 0x18, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0b, 0x69, 0x73, 0x4f, 0x76, 0x65,
0x72, 0x6c, 0x79, 0x69, 0x6e, 0x67, 0x1a, 0x3b, 0x0a, 0x0d, 0x50, 0x72, 0x6f, 0x70, 0x65, 0x72,
0x74, 0x79, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01,
0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c,
0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a,
0x02, 0x38, 0x01, 0x1a, 0x3e, 0x0a, 0x10, 0x41, 0x64, 0x64, 0x50, 0x72, 0x6f, 0x70, 0x65, 0x72,
0x74, 0x79, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01,
0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c,
0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a,
0x02, 0x38, 0x01, 0x1a, 0x39, 0x0a, 0x0b, 0x45, 0x6e, 0x65, 0x72, 0x67, 0x79, 0x45, 0x6e, 0x74,
0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52,
0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20,
0x01, 0x28, 0x05, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x42, 0x06,
0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
file_hero_hero_db_proto_rawDescOnce sync.Once
file_hero_hero_db_proto_rawDescData = file_hero_hero_db_proto_rawDesc
)
func file_hero_hero_db_proto_rawDescGZIP() []byte {
file_hero_hero_db_proto_rawDescOnce.Do(func() {
file_hero_hero_db_proto_rawDescData = protoimpl.X.CompressGZIP(file_hero_hero_db_proto_rawDescData)
})
return file_hero_hero_db_proto_rawDescData
}
var file_hero_hero_db_proto_msgTypes = make([]protoimpl.MessageInfo, 5)
var file_hero_hero_db_proto_goTypes = []interface{}{
(*SkillData)(nil), // 0: SkillData
(*DBHero)(nil), // 1: DBHero
nil, // 2: DBHero.PropertyEntry
nil, // 3: DBHero.AddPropertyEntry
nil, // 4: DBHero.EnergyEntry
}
var file_hero_hero_db_proto_depIdxs = []int32{
0, // 0: DBHero.normalSkill:type_name -> SkillData
2, // 1: DBHero.property:type_name -> DBHero.PropertyEntry
3, // 2: DBHero.addProperty:type_name -> DBHero.AddPropertyEntry
4, // 3: DBHero.energy:type_name -> DBHero.EnergyEntry
4, // [4:4] is the sub-list for method output_type
4, // [4:4] is the sub-list for method input_type
4, // [4:4] is the sub-list for extension type_name
4, // [4:4] is the sub-list for extension extendee
0, // [0:4] is the sub-list for field type_name
}
func init() { file_hero_hero_db_proto_init() }
func file_hero_hero_db_proto_init() {
if File_hero_hero_db_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_hero_hero_db_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*SkillData); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_hero_hero_db_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*DBHero); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_hero_hero_db_proto_rawDesc,
NumEnums: 0,
NumMessages: 5,
NumExtensions: 0,
NumServices: 0,
},
GoTypes: file_hero_hero_db_proto_goTypes,
DependencyIndexes: file_hero_hero_db_proto_depIdxs,
MessageInfos: file_hero_hero_db_proto_msgTypes,
}.Build()
File_hero_hero_db_proto = out.File
file_hero_hero_db_proto_rawDesc = nil
file_hero_hero_db_proto_goTypes = nil
file_hero_hero_db_proto_depIdxs = nil
}

View File

@ -1,83 +0,0 @@
package bench
import (
"fmt"
"testing"
)
type Benchmark struct {
Parallelism int
Data interface{}
TargetBuilder TargetBuilder
}
type Target struct {
GetHeroList func() (interface{}, error)
}
type TargetBuilder struct {
Name string
Make func(bench Benchmark) (Target, error)
}
// func genData(n int) []*DBHero {
// heroes := []*DBHero{}
// GetList(&heroes)
// return heroes
// }
func compose(parallelisms, dataSize []int, builders []TargetBuilder) []Benchmark {
benchmarks := make([]Benchmark, 0, len(parallelisms)*len(dataSize)*len(builders))
for _, p := range parallelisms {
for _, _ = range dataSize {
// d := genData(k)
for _, builder := range builders {
benchmarks = append(benchmarks, Benchmark{
Parallelism: p,
// Data: d,
TargetBuilder: builder,
})
}
}
}
return benchmarks
}
func RunBenchmark(b *testing.B, benchmarks []Benchmark) {
// f, _ := os.Create("c://pprof")
// pprof.WriteHeapProfile(f)
// defer f.Close()
for _, bench := range benchmarks {
b.Run(fmt.Sprintf("%s-parallelism(%d)-", bench.TargetBuilder.Name, bench.Parallelism), func(b *testing.B) {
target, err := bench.TargetBuilder.Make(bench)
if err != nil {
b.Fatalf("%s setup fail: %v", bench.TargetBuilder.Name, err)
}
if bench.Parallelism == 0 {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := target.GetHeroList()
if err != nil {
b.Errorf("%s error during benchmark: %v", bench.TargetBuilder.Name, err)
}
}
} else {
b.SetParallelism(bench.Parallelism) //指定并行数目
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) { //并行执行
for pb.Next() {
_, err := target.GetHeroList()
if err != nil {
b.Errorf("%s error during benchmark: %v", bench.TargetBuilder.Name, err)
}
}
})
}
b.StopTimer()
})
}
}

View File

@ -1,250 +0,0 @@
package bench
import (
"context"
"fmt"
"go_dreamfactory/lego/sys/mgo"
"go_dreamfactory/sys/db"
"log"
"os"
reflect "reflect"
"testing"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var mdb *db.DB
// func TestMain(m *testing.M) {
// imgo, err := mgo.NewSys(mgo.SetMongodbUrl("mongodb://admin:123456@10.0.0.9:27018"), mgo.SetMongodbDatabase("dreamfactory"))
// if err != nil {
// log.Fatal(err)
// }
// mdb = &db.DB{}
// mdb.SetMgo(imgo)
// defer os.Exit(m.Run())
// }
var mgoDb *mongo.Database
func TestMain(m *testing.M) {
option := options.Client().ApplyURI("mongodb://admin:123456@10.0.0.9:27018")
client, err := mongo.Connect(context.TODO(), option)
if err != nil {
log.Fatal(err)
}
log.Print("connect mongo success")
mgoDb = client.Database("dreamfactory")
//
imgo, err := mgo.NewSys(mgo.SetMongodbUrl("mongodb://admin:123456@10.0.0.9:27018"), mgo.SetMongodbDatabase("dreamfactory"))
if err != nil {
log.Fatal(err)
}
mdb = &db.DB{}
mdb.SetMgo(imgo)
defer os.Exit(m.Run())
}
func Test_GetList(t *testing.T) {
heroes := []*DBHero{}
GetList(&heroes)
}
func Test_GetList0(t *testing.T) {
// heroes := []*DBHero{}
// GetListO(&heroes)
}
func BenchmarkMarsh(b *testing.B) {
var (
// ncpu = runtime.NumCPU()
parallelisms = []int{2} //平行数
dataSizes = []int{10, 100, 1000} //测试次数
builders = []TargetBuilder{
// {
// Name: "gen",
// Make: func(bench Benchmark) (Target, error) {
// return Target{
// GetHeroList: func() (any, error) {
// // heroes := GetList3(new(DBHero))
// heroes := []*DBHero{}
// GetList(&heroes)
// return heroes, nil
// },
// }, nil
// },
// },
{
Name: "Decode",
Make: func(bench Benchmark) (Target, error) {
return Target{
GetHeroList: func() (interface{}, error) {
GetListByDecode()
return nil, nil
},
}, nil
},
},
}
)
RunBenchmark(b, compose(parallelisms, dataSizes, builders))
}
func GetList(data interface{}) (err error) {
var c *mongo.Cursor
t := reflect.TypeOf(data)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t.Kind() == reflect.Slice {
t = t.Elem()
} else {
err = fmt.Errorf("Input param is not a slice")
}
sl := reflect.ValueOf(data)
if t.Kind() == reflect.Ptr {
sl = sl.Elem()
}
st := sl.Type()
sliceType := st.Elem()
if sliceType.Kind() == reflect.Ptr {
sliceType = sliceType.Elem()
}
//query from mgo
// if c, err = mdb.Mgo().Find(core.SqlTable("hero"), bson.M{}); err != nil {
if c, err = mgoDb.Collection("hero").Find(context.Background(), bson.M{}); err != nil {
return err
} else {
var temp map[string]interface{} = make(map[string]interface{})
for c.Next(context.Background()) {
_id := c.Current.Lookup("_id").StringValue()
if sl.Len() < sl.Cap() {
sl.Set(sl.Slice(0, sl.Len()+1))
elem := sl.Index(sl.Len() - 1)
if elem.IsNil() {
elem.Set(reflect.New(sliceType))
}
if err = c.Decode(elem.Elem().Addr().Interface()); err != nil {
return
}
temp[_id] = elem.Elem().Addr().Interface()
continue
}
elem := reflect.New(sliceType)
sl.Set(reflect.Append(sl, elem))
if err = c.Decode(elem.Elem().Addr().Interface()); err != nil {
return
}
temp[_id] = elem.Elem().Addr().Interface()
}
if len(temp) == 0 { //没有数据自己返回
return
}
}
return err
}
func GetListByDecode() (err error) {
// t2 := time.Now()
c, err := mgoDb.Collection("hero").Find(context.Background(), bson.M{"_id": "62ccd2670af3660e58e1730e"})
if err != nil {
fmt.Println(err)
}
defer c.Close(context.Background())
// fmt.Println(time.Since(t2))
// t := time.Now()
for c.Next(context.Background()) {
// go func(c1 *mongo.Cursor) {
var hero *DBHero
err2 := c.Decode(&hero)
if err2 != nil {
fmt.Println(err2)
}
// }(c)
}
// fmt.Printf("%v ", time.Since(t))
return err
}
func GetList2(data interface{}) (err error) {
// c, err := mdb.Mgo().Find(core.SqlTable("hero"), bson.M{})
c, err := mgoDb.Collection("hero").Find(context.Background(), bson.M{})
if err != nil {
fmt.Println(err)
return
}
// defer c.Close(context.Background())
list := make([]interface{}, 0)
for c.Next(context.Background()) {
val := reflect.ValueOf(data).Interface()
if err = c.Decode(val); err != nil {
continue
}
list = append(list, val)
}
return
}
func GetList3(data any) []any {
// c, err := mdb.Mgo().Collection("hero").Find(context.Background(), bson.M{})
c, err := mgoDb.Collection("hero").Find(context.Background(), bson.M{})
if err != nil {
fmt.Println(err)
}
defer c.Close(context.Background())
list := make([]any, 0)
for c.Next(context.Background()) {
_val := reflect.ValueOf(data).Interface()
err2 := c.Decode(_val)
if err2 != nil {
continue
}
list = append(list, _val)
}
return list
}
func GetList4(data *DBHero) []any {
// c, err := mdb.Mgo().Find(core.SqlTable("hero"), bson.M{})
c, err := mgoDb.Collection("hero").Find(context.Background(), bson.M{})
if err != nil {
fmt.Println(err)
}
defer c.Close(context.Background())
list := make([]any, 0)
for c.Next(context.Background()) {
err2 := c.Decode(data)
if err2 != nil {
continue
}
list = append(list, data)
}
return list
}
func BenchmarkSimple(b *testing.B) {
// f, _ := os.Create("c://pprof2")
// pprof.WriteHeapProfile(f)
// defer f.Close()
for i := 0; i < b.N; i++ {
GetListByDecode()
}
}

View File

@ -1,33 +1,44 @@
package db
import "go_dreamfactory/lego/sys/mgo"
type (
ISys interface {
Mgo() mgo.ISys
//本服数据连接
Local() *DBConn
//本服数据连接
Cross() *DBConn
//跨服列表数据层连接
ServerDBConn(stage string) (conn *DBConn)
}
)
var Defsys ISys
var defsys ISys
func OnInit(config map[string]interface{}, option ...Option) (err error) {
var options Options
var options *Options
if options, err = newOptions(config, option...); err != nil {
return
}
Defsys, err = newSys(options)
defsys, err = newSys(options)
return
}
func NewSys(option ...Option) (sys ISys, err error) {
var options Options
var options *Options
if options, err = newOptionsByOption(option...); err != nil {
return
}
Defsys, err = newSys(options)
defsys, err = newSys(options)
return
}
func Mgo() mgo.ISys {
return Defsys.Mgo()
func Local() *DBConn {
return defsys.Local()
}
func Cross() *DBConn {
return defsys.Cross()
}
func ServerDBConn(stage string) (conn *DBConn) {
return defsys.ServerDBConn(stage)
}

View File

@ -1,40 +1,97 @@
package db
import (
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/lego/sys/mgo"
"go_dreamfactory/lego/sys/redis"
)
func newSys(options Options) (sys *DB, err error) {
type DBConn struct {
Redis redis.ISys
Mgo mgo.ISys
}
func newDBConn(conf DBConfig) (conn *DBConn, err error) {
conn = &DBConn{}
if conf.RedisIsCluster {
conn.Redis, err = redis.NewSys(
redis.SetRedisType(redis.Redis_Cluster),
redis.SetRedis_Cluster_Addr(conf.RedisAddr),
redis.SetRedis_Cluster_Password(conf.RedisPassword))
} else {
conn.Redis, err = redis.NewSys(
redis.SetRedisType(redis.Redis_Single),
redis.SetRedis_Single_Addr(conf.RedisAddr[0]),
redis.SetRedis_Single_Password(conf.RedisPassword),
redis.SetRedis_Single_DB(conf.RedisDB),
)
}
if err != nil {
log.Error(err.Error(), log.Field{"config", conf})
return
}
if conn.Mgo, err = mgo.NewSys(
mgo.SetMongodbUrl(conf.MongodbUrl),
mgo.SetMongodbDatabase(conf.MongodbDatabase),
); err != nil {
log.Error(err.Error(), log.Field{"config", conf})
return
}
return
}
func newSys(options *Options) (sys *DB, err error) {
sys = &DB{options: options}
err = sys.init()
return
}
type DB struct {
options Options
mgo mgo.ISys
options *Options
local *DBConn
cross *DBConn
servers map[string]*DBConn
}
func (this *DB) init() (err error) {
if this.mgo, err = mgo.NewSys(
mgo.SetMongodbUrl(this.options.MongodbUrl),
mgo.SetMongodbDatabase(this.options.MongodbDatabase),
); err != nil {
if this.local, err = newDBConn(this.options.Loacl); err != nil {
return
}
if this.options.Cross.Enabled {
if this.cross, err = newDBConn(this.options.Cross); err != nil {
return
}
}
if this.options.ServerList != nil && len(this.options.ServerList) > 0 {
this.servers = make(map[string]*DBConn, len(this.options.ServerList))
for k, v := range this.options.ServerList {
if this.servers[k], err = newDBConn(v); err != nil {
return
}
}
}
return
}
func (this *DB) Mgo() mgo.ISys {
return this.mgo
func (this *DB) Local() *DBConn {
if this.cross == nil {
log.Panic("CrossDBConn on init")
}
return this.local
}
func (this *DB) SetMgo(mgo mgo.ISys) {
this.mgo = mgo
func (this *DB) Cross() *DBConn {
if this.cross == nil {
log.Panic("CrossDBConn on init")
}
return this.cross
}
func (this *DB) Table(tableName string) *DB {
this.mgo.Collection(core.SqlTable(tableName)).Database()
return this
func (this *DB) ServerDBConn(stage string) (conn *DBConn) {
ok := false
conn, ok = this.servers[stage]
if !ok {
log.Panicf("DBConn:%s on init", stage)
}
return
}

View File

@ -1,40 +1 @@
package db
import (
"fmt"
"go_dreamfactory/lego/sys/mgo"
"go_dreamfactory/pb"
"log"
"os"
"testing"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)
var db *DB
func TestMain(m *testing.M) {
imgo, err := mgo.NewSys(mgo.SetMongodbUrl("mongodb://10.0.0.9:10013"), mgo.SetMongodbDatabase("dreamfactory"))
if err != nil {
log.Fatal(err)
}
db = &DB{
mgo: imgo,
}
defer os.Exit(m.Run())
}
func TestMail(t *testing.T) {
mail := &pb.DBMailData{}
err := db.mgo.FindOneAndUpdate(
"mail",
bson.M{"_id": "62e784aa78d6d7bf12fb1f0e"},
bson.M{"$set": bson.M{
"check": true,
}},
options.FindOneAndUpdate().SetUpsert(true).SetReturnDocument(options.After),
).Decode(&mail)
fmt.Printf("%v", err)
}

View File

@ -1,50 +1,69 @@
package db
import (
"errors"
"go_dreamfactory/lego/utils/mapstructure"
)
type Option func(*Options)
type Options struct {
//DB层配置
type DBConfig struct {
Enabled bool //是否生效
RedisIsCluster bool //是否是集群
RedisAddr []string //redis 的集群地址
RedisPassword string //redis的密码
RedisDB int //数据库位置
MongodbUrl string
MongodbDatabase string
}
func Set_MongodbUrl(v string) Option {
type Option func(*Options)
type Options struct {
Loacl DBConfig //本服配置
Cross DBConfig //跨服配置
ServerList map[string]DBConfig //服务列表配置
}
//设置本服配置
func SetLoacl(v DBConfig) Option {
return func(o *Options) {
o.MongodbUrl = v
o.Loacl = v
}
}
func Set_MongodbDatabase(v string) Option {
//设置本服配置
func SetCross(v DBConfig) Option {
return func(o *Options) {
o.MongodbDatabase = v
o.Cross = v
}
}
func newOptions(config map[string]interface{}, opts ...Option) (Options, error) {
options := Options{}
//设置跨服区服列表
func SetServerList(v map[string]DBConfig) Option {
return func(o *Options) {
o.ServerList = v
}
}
func newOptions(config map[string]interface{}, opts ...Option) (options *Options, err error) {
options = &Options{
Loacl: DBConfig{},
Cross: DBConfig{},
ServerList: make(map[string]DBConfig),
}
if config != nil {
mapstructure.Decode(config, &options)
if err = mapstructure.Decode(config, options); err != nil {
return
}
}
for _, o := range opts {
o(&options)
o(options)
}
if len(options.MongodbUrl) == 0 || len(options.MongodbDatabase) == 0 {
return options, errors.New("MongodbUrl or MongodbDatabase is null")
}
return options, nil
return
}
func newOptionsByOption(opts ...Option) (Options, error) {
options := Options{}
func newOptionsByOption(opts ...Option) (options *Options, err error) {
options = &Options{}
for _, o := range opts {
o(&options)
o(options)
}
if len(options.MongodbUrl) == 0 || len(options.MongodbDatabase) == 0 {
return options, errors.New("MongodbUrl or MongodbDatabase is null")
}
return options, nil
return
}