101d1cc1
zhangqijia
feat: 一个基于redis的自...
|
1
2
3
|
package models
import (
|
a24dea4c
zhangqijia
fix: id自增做了写更新。阵容...
|
4
|
"fmt"
|
101d1cc1
zhangqijia
feat: 一个基于redis的自...
|
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
"github.com/garyburd/redigo/redis"
"pro2d/common"
"pro2d/common/db/mongoproxy"
"pro2d/common/db/redisproxy"
"pro2d/common/logger"
"pro2d/pb"
"reflect"
)
type STOIncrement map[interface{}]int64
var dbSeedSingleton *DBSeed
type DBSeed struct {
serverID int64
}
func NewDBSeed(serverID int64) *DBSeed {
if dbSeedSingleton == nil {
dbSeedSingleton = &DBSeed{
serverID: serverID,
}
}
return dbSeedSingleton
}
func DBSeedS() *DBSeed {
return dbSeedSingleton
}
func AccountModels() STOIncrement {
return STOIncrement{
&pb.Account{}: 0,
"uid": common.MaxUidNum,
}
}
func GameModels() STOIncrement {
return STOIncrement{
&pb.Increment{}: 0,
&pb.Role{}: common.MaxCommNum,
&pb.Equipment{}: 0,
&pb.Hero{}: 0,
&pb.Prop{}: 0,
&pb.Team{}: 0,
}
}
//初始化表自增id
func (d *DBSeed) InitAutoIncreUidTable(schema STOIncrement) {
for s, b := range schema {
if b <= 0 {
continue
}
var name string
if reflect.TypeOf(s).Kind() == reflect.String {
name = s.(string)
} else {
name = mongoproxy.GetCollName(s)
}
autoIncrement := NewIncrement(name)
var increId int64 = 0
if err := autoIncrement.Load(); err != nil {
//字段不存在 初始化 id
increId = d.serverID * b
autoIncrement.Incre.Key = name
autoIncrement.Incre.Val = increId
autoIncrement.Create()
} else {
increId = autoIncrement.Incre.Val
}
//设置到redis中,提供初始自增id
|
a24dea4c
zhangqijia
fix: id自增做了写更新。阵容...
|
80
|
relay, err := redisproxy.HGET(fmt.Sprintf(common.AutoIncrement, d.serverID), name)
|
101d1cc1
zhangqijia
feat: 一个基于redis的自...
|
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
if err != nil {
logger.Error(err.Error())
continue
}
var relayID int64 = 0
if relay != nil {
relayID, err = redis.Int64(relay, err)
if err != nil {
logger.Error(err.Error())
continue
}
}
|
a24dea4c
zhangqijia
fix: id自增做了写更新。阵容...
|
95
|
logger.Debug(relayID, fmt.Sprintf(common.AutoIncrement, d.serverID), name)
|
101d1cc1
zhangqijia
feat: 一个基于redis的自...
|
96
|
if relayID == 0 || increId > relayID {
|
a24dea4c
zhangqijia
fix: id自增做了写更新。阵容...
|
97
|
redisproxy.HSET(fmt.Sprintf(common.AutoIncrement, d.serverID), name, increId)
|
101d1cc1
zhangqijia
feat: 一个基于redis的自...
|
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
}
}
}
func (d *DBSeed) SaveAutoincrementTimer(schema STOIncrement) {
for s, b := range schema {
if b <= 0 {
continue
}
var name string
if reflect.TypeOf(s).Kind() == reflect.String {
name = s.(string)
} else {
name = mongoproxy.GetCollName(s)
}
//获取数据库中的id 持久化数据
autoIncrement := NewIncrement(name)
var dbID int64 = 0
if err := autoIncrement.Load(); err != nil {
continue
} else {
dbID = autoIncrement.Incre.Val
}
//获取redis中的id 内存中的数据。获取自增id
|
a24dea4c
zhangqijia
fix: id自增做了写更新。阵容...
|
124
|
relayID, err := redis.Int64(redisproxy.HGET(fmt.Sprintf(common.AutoIncrement, d.serverID), name))
|
101d1cc1
zhangqijia
feat: 一个基于redis的自...
|
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
if err != nil {
logger.Error(err.Error())
continue
}
if relayID > dbID {
//持久化数据
autoIncrement.SetProperty("val", relayID)
autoIncrement.Update()
}
}
}
//初始化服务器数据库以及服务器信息表
func (d *DBSeed) InitServerDatabase(schema STOIncrement) {
for s, _ := range schema {
if reflect.TypeOf(s).Kind() == reflect.String {
continue
}
coll, keys := mongoproxy.FindIndex(s)
mongoproxy.CreateTable(coll)
for _, index := range keys {
res, err := mongoproxy.SetUnique(coll, index)
if err != nil {
logger.Error("InitDoc unique: %s, err: %v", res, err)
continue
}
}
}
}
|