forked from xiaonanln/goworld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kvdb.go
204 lines (173 loc) · 4.82 KB
/
kvdb.go
1
2
3
4
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
124
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package kvdb
import (
"time"
"io"
"strconv"
"github.com/xiaonanln/goworld/engine/async"
"github.com/xiaonanln/goworld/engine/config"
"github.com/xiaonanln/goworld/engine/gwlog"
"github.com/xiaonanln/goworld/engine/kvdb/backend/kvdb_mongodb"
"github.com/xiaonanln/goworld/engine/kvdb/backend/kvdbmysql"
"github.com/xiaonanln/goworld/engine/kvdb/backend/kvdbredis"
"github.com/xiaonanln/goworld/engine/kvdb/types"
)
const (
_KVDB_ASYNC_JOB_GROUP = "_kvdb"
)
var (
kvdbEngine kvdbtypes.KVDBEngine
)
// KVDBGetCallback is type of KVDB Get callback
type KVDBGetCallback func(val string, err error)
// KVDBPutCallback is type of KVDB Get callback
type KVDBPutCallback func(err error)
// KVDBGetRangeCallback is type of KVDB GetRange callback
type KVDBGetRangeCallback func(items []kvdbtypes.KVItem, err error)
// KVDBGetOrPutCallback is type of KVDB GetOrPut callback
type KVDBGetOrPutCallback func(oldVal string, err error)
// Initialize the KVDB
//
// Called by game server engine
func Initialize() {
kvdbCfg := config.GetKVDB()
if kvdbCfg.Type == "" {
return
}
gwlog.Infof("KVDB initializing, config:\n%s", config.DumpPretty(kvdbCfg))
assureKVDBEngineReady()
}
func assureKVDBEngineReady() (err error) {
if kvdbEngine != nil { // connection is valid
return
}
kvdbCfg := config.GetKVDB()
if kvdbCfg.Type == "mongodb" {
kvdbEngine, err = kvdbmongo.OpenMongoKVDB(kvdbCfg.Url, kvdbCfg.DB, kvdbCfg.Collection)
} else if kvdbCfg.Type == "redis" {
var dbindex int = -1
if kvdbCfg.DB != "" {
dbindex, err = strconv.Atoi(kvdbCfg.DB)
if err != nil {
return err
}
}
kvdbEngine, err = kvdbredis.OpenRedisKVDB(kvdbCfg.Url, dbindex)
} else if kvdbCfg.Type == "sql" {
if kvdbCfg.Driver == "mysql" {
kvdbEngine, err = kvdbmysql.OpenMySQLKVDB(kvdbCfg.Url)
} else {
gwlog.Fatalf("KVDB mysql driver %s is unknown", kvdbCfg.Driver)
}
} else {
gwlog.Fatalf("KVDB type %s is not implemented", kvdbCfg.Type)
}
return
}
// Get gets value of key from KVDB, returns in callback
func Get(key string, callback KVDBGetCallback) {
var ac async.AsyncCallback
if callback != nil {
ac = func(res interface{}, err error) {
if err != nil {
callback("", err)
} else {
callback(res.(string), nil)
}
}
}
async.AppendAsyncJob(_KVDB_ASYNC_JOB_GROUP, kvdbRoutine(func() (res interface{}, err error) {
res, err = kvdbEngine.Get(key)
return
}), ac)
}
func kvdbRoutine(r func() (res interface{}, err error)) func() (res interface{}, err error) {
kvdbroutine := func() (res interface{}, err error) {
for {
err := assureKVDBEngineReady()
if err == nil {
break
} else {
gwlog.Errorf("KVDB engine is not ready: %s", err)
time.Sleep(time.Second)
}
}
res, err = r()
if err != nil && kvdbEngine.IsConnectionError(err) {
kvdbEngine.Close()
kvdbEngine = nil
}
return
}
return kvdbroutine
}
// Put puts key-value item to KVDB, returns in callback
func Put(key string, val string, callback KVDBPutCallback) {
var ac async.AsyncCallback
if callback != nil {
ac = func(res interface{}, err error) {
callback(err)
}
}
async.AppendAsyncJob(_KVDB_ASYNC_JOB_GROUP, kvdbRoutine(func() (res interface{}, err error) {
err = kvdbEngine.Put(key, val)
return
}), ac)
}
// GetOrPut gets value of key from KVDB, if val not exists or is "", put key-value to KVDB.
func GetOrPut(key string, val string, callback KVDBGetOrPutCallback) {
var ac async.AsyncCallback
if callback != nil {
ac = func(res interface{}, err error) {
if err == nil {
callback(res.(string), err)
} else {
callback("", err)
}
}
}
async.AppendAsyncJob(_KVDB_ASYNC_JOB_GROUP, kvdbRoutine(func() (res interface{}, err error) {
oldVal, err := kvdbEngine.Get(key)
if err == nil {
if oldVal == "" {
err = kvdbEngine.Put(key, val)
}
}
return oldVal, err
}), ac)
}
// GetRange retrives key-value items of specified key range, returns in callback
func GetRange(beginKey string, endKey string, callback KVDBGetRangeCallback) {
var ac async.AsyncCallback
if callback != nil {
ac = func(res interface{}, err error) {
if err == nil {
callback(res.([]kvdbtypes.KVItem), nil)
} else {
callback(nil, err)
}
}
}
async.AppendAsyncJob(_KVDB_ASYNC_JOB_GROUP, kvdbRoutine(func() (res interface{}, err error) {
it, err := kvdbEngine.Find(beginKey, endKey)
if err != nil {
return nil, err
}
var items []kvdbtypes.KVItem
for {
item, err := it.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
items = append(items, item)
}
return items, nil
}), ac)
}
// NextLargerKey finds the next key that is larger than the specified key,
// but smaller than any other keys that is larger than the specified key
func NextLargerKey(key string) string {
return key + "\x00" // the next string that is larger than key, but smaller than any other keys > key
}