-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdbmgr.lua
More file actions
585 lines (474 loc) · 12.9 KB
/
dbmgr.lua
File metadata and controls
585 lines (474 loc) · 12.9 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
local skynet = require "skynet"
require "skynet.manager"
local redis = require "redis"
local helper = require "helper"
local dbname = skynet.getenv("mysql_db")
local config_table = {}
local user_table = {}
local common_table = {}
local schema = {}
local CMD = {}
local function do_redis( ... )
-- body
end
local function get_primary_key(tbname)
local sql = "select k.column_name " ..
"from information_schema.table_constraints t " ..
"join information_schema.key_column_usage k " ..
"using (constraint_name,table_schema,table_name) " ..
"where t.constraint_type = 'PRIMARY KEY' " ..
"and t.table_schema= '".. dbname .. "'" ..
"and t.table_name = '" .. tbname .. "'"
local t = skynet.call("mysqlpool", "lua", "execute",sql)
if not t then
print("nil")
else
print(type(t))
end
helper.dump(t)
return t[1]["column_name"]
end
local function get_fields(tbname)
local sql = string.format("select column_name from information_schema.columns where table_schema = '%s' and table_name = '%s'", dbname, tbname)
local rs = skynet.call("mysqlpool", "lua", "execute", sql)
local fields = {}
for _, row in pairs(rs) do
table.insert(fields, row["column_name"])
end
helper.dump(fields)
return fields
end
local function get_field_type(tbname, field)
local sql = string.format("select data_type from information_schema.columns where table_schema='%s' and table_name='%s' and column_name='%s'",
dbname, tbname, field)
local rs = skynet.call("mysqlpool", "lua", "execute", sql)
helper.dump(rs)
return rs[1]["data_type"]
end
local function load_schema_to_redis()
local sql = "select table_name from information_schema.tables where table_schema='" .. dbname .. "'"
local rs = skynet.call("mysqlpool", "lua", "execute", sql)
helper.dump(rs)
for _, row in pairs(rs) do
local tbname =row.table_name
schema[tbname] = {}
schema[tbname]["fields"] = {}
schema[tbname]["pk"] = get_primary_key(tbname)
local fields = get_fields(tbname)
for _, field in pairs(fields) do
local field_type = get_field_type(tbname, field)
if field_type == "char"
or field_type == "varchar"
or field_type == "tinytext"
or field_type == "text"
or field_type == "mediumtext"
or field_type == "longtext" then
schema[tbname]["fields"][field] = "string"
else
schema[tbname]["fields"][field] = "number"
end
end
end
end
local function convert_record(tbname, record)
for k, v in pairs(record) do
if schema[tbname]["fields"][k] == "number" then
record[k] = tonumber(v)
end
end
return record
end
local function make_rediskey(row, key)
local rediskey = ""
local fields = string.split(key, ",")
for i, field in pairs(fields) do
if i == 1 then
rediskey = row[field]
else
rediskey = rediskey .. ":" .. row[field]
end
end
return rediskey
end
local function load_data_impl(config, uid)
local tbname = config.name
local pk = schema[tbname]["pk"]
local offset = 0
local sql
local data = {}
while true do
if not uid then
if not config.columns then
sql = string.format("select * from %s order by %s asc limit %d, 1000", tbname, pk, offset)
else
sql = string.format("select %s from %s order by %s asc limit %d, 1000", config.columns, tbname, pk, offset)
end
else
if not config.columns then
sql = string.format("select * from %s where uid = %d order by %s asc limit %d, 1000", tbname, uid, pk, offset)
else
sql = string.format("select %s from %s where uid = %d order by %s asc limit %d, 1000", config.columns, tbname, uid, pk, offset)
end
end
local rs = skynet.call("mysqlpool", "lua", "execute", sql)
helper.dump(rs)
if #rs <= 0 then break end
for _, row in pairs(rs) do
local rediskey = make_rediskey(row, config.key)
do_redis({ "hmset", tbname .. ":" .. rediskey, row, true }, uid)
-- 建立索引
if config.indexkey then
local indexkey = make_rediskey(row, config.indexkey)
do_redis({ "zadd", tbname .. ":index:" .. indexkey, 0, rediskey }, uid)
end
table.insert(data, row)
end
if #rs < 1000 then break end
offset = offset + 1000
end
return data
end
local function load_config_data()
for _, v in pairs(config_table) do
load_data_impl(v)
end
end
local function load_common_data()
for _, v in pairs(common_table) do
load_data_impl(v)
end
end
local function load_maxkey_impl(tbname)
local pk = schema[tbname]["pk"]
local sql = string.format("select max(%s) as maxkey from %s", pk, tbname)
local result = skynet.call("mysqlpool", "lua", "execute", sql)
if #result > 0 and not table.empty(result[1]) then
do_redis({ "set", tbname .. ":" .. pk, result[1]["maxkey"] })
end
end
local function load_maxkey()
for k, v in pairs(user_table) do
load_maxkey_impl(k)
end
for k, v in pairs(common_table) do
load_maxkey_impl(k)
end
end
local function load_data_to_redis()
load_config_data()
load_common_data()
load_maxkey()
end
function make_pairs_table(t, fields)
assert(type(t) == "table", "make_pairs_table t is not table")
local data = {}
if not fields then
for i=1, #t, 2 do
data[t[i]] = t[i+1]
end
else
for i=1, #t do
data[fields[i]] = t[i]
end
end
return data
end
function CMD.start(config, user, common)
local mysqlpool = skynet.uniqueservice("mysqlpool")
skynet.call(mysqlpool, "lua", "start")
-- local redispool = skynet.uniqueservice("redispool")
-- skynet.call(redispool, "lua", "start")
local dbsync = skynet.uniqueservice("dbsync")
skynet.call(dbsync, "lua", "start")
-- for _, v in pairs(config) do
-- config_table[v.name] = v
-- end
-- for _, v in pairs(user) do
-- user_table[v.name] = v
-- end
-- for _, v in pairs(common) do
-- common_table[v.name] = v
-- end
load_schema_to_redis()
load_data_to_redis()
end
function CMD.stop()
end
-- 从redis获取config类型表数据
function CMD.get_config(tbname)
local data = {}
local config = config_table[tbname]
local keys = do_redis({ "keys", tbname .. ":*" })
for _, v in pairs(keys) do
local row = do_redis({ "hgetall", v })
row = make_pairs_table(row)
row = convert_record(tbname, row)
local key = make_rediskey(row, config.key)
data[key] = row
end
return data
end
-- 从redis获取common类型表数据
function CMD.get_common(tbname)
local data = {}
local config = common_table[tbname]
local indexkeys = do_redis({ "keys", tbname .. ":index:*" })
local keys = {}
for _, indexkey in pairs(indexkeys) do
local ids = do_redis({ "zrange", indexkey, 0, -1 })
for _, id in pairs(ids) do
table.insert(keys ,tbname .. ":" .. id)
end
end
if table.empty(keys) then
keys = do_redis({ "keys", tbname .. ":*" })
end
for _, v in pairs(keys) do
if v ~= tbname..":"..schema[tbname]["pk"] then
local row = do_redis({ "hgetall", v })
row = make_pairs_table(row)
row = convert_record(tbname, row)
local key = make_rediskey(row, config.key)
data[key] = row
end
end
return data
end
function CMD.get_schema(tbname)
return schema[tbname]
end
-- 加user类型表单行数据到redis
function CMD.load_user_single(tbname, uid)
local config = user_table[tbname]
local data = load_data_impl(config, uid)
assert(#data <= 1)
if #data == 1 then
return data[1]
end
return data -- 这里返回的一定是空表{}
end
-- 加user类型表多行数据到redis
function CMD.load_user_multi(tbname, uid)
local config = user_table[tbname]
local data = {}
local t = load_data_impl(config, uid)
local pk = schema[tbname]["pk"]
for k, v in pairs(t) do
data[v[pk]] = v
end
return data
end
function CMD.hmset(uid, key, t)
local data = {}
for k, v in pairs(t) do
table.insert(data, k)
table.insert(data, v)
end
local db = getconn(uid)
db:hmset(key, table.unpack(data))
return true
end
-- 从redis获取user类型表单行数据,如果不存在,则从mysql加载
-- fields为空,获取整行
function CMD.get_user_single(tbname, uid, fields)
local result
if fields then
result = do_redis({ "hmget", tbname .. ":" .. uid, table.unpack(fields) }, uid)
result = make_pairs_table(result, fields)
else
result = do_redis({ "hgetall", tbname .. ":" .. uid }, uid)
result = make_pairs_table(result)
end
-- redis没有数据返回,则从mysql加载
if table.empty(result) then
local t = CMD.load_user_single(tbname, uid)
if fields and not table.empty(t) then
result = {}
for k, v in pairs(fields) do
result[v] = t[v]
end
else
result = t
end
end
result = convert_record(tbname, result)
return result
end
-- 从redis获取user类型表多行数据,如果不存在,则从mysql加载
function CMD.get_user_multi(tbname, uid, id, fields)
local result
local ids = do_redis({ "zrange", tbname .. ":index:" .. uid, 0, -1 }, uid)
local pk = schema[tbname]["pk"]
if table.empty(ids) then
local t = CMD.load_user_multi(tbname, uid)
if id then
if fields then
result = {}
for k, v in pairs(fields) do
result[v] = t[id][v]
end
else
result = t[id]
end
else
if fields then
result = {}
for k, v in pairs(t) do
result[k] = {}
setmetatable(result, { __mode = "k" })
for i=1, #fields do
result[k][fields[i]] = t[k][fields[i]]
end
end
else
result = t
end
for _, id in pairs(ids) do
local t = do_redis({ "hgetall", tbname .. ":" .. id }, uid)
t = make_pairs_table(t)
t = convert_record(tbname, t)
result[tonumber(id)] = t
end
end
else
if id then
if fields then
result = do_redis({ "hmget", tbname .. ":" .. id, table.unpack(fields) }, uid)
result = make_pairs_table(result,fields)
else
result = do_redis({ "hgetall", tbname .. ":" .. id }, uid)
result = make_pairs_table(result)
end
else
result = {}
for _, id in pairs(ids) do
local t = do_redis({ "hgetall", tbname .. ":" .. id }, uid)
t = make_pairs_table(t)
t = convert_record(tbname, t)
result[tonumber(id)] = t
end
end
end
return result
end
-- redis中增加一行记录,并同步到mysql
function CMD.add(tbname, row, type, nosync)
local config
if type == 1 then
config = config_table[tbname]
elseif type == 2 then
config = user_table[tbname]
elseif type == 3 then
config = common_table[tbname]
end
local uid
if row.uid and type == 2 then
uid = row.uid
end
local key = config.key
local indexkey = config.indexkey
local rediskey = make_rediskey(row, key)
do_redis({ "hmset", tbname .. ":" .. rediskey, row }, uid)
if indexkey then
local linkey = make_rediskey(row,indexkey)
do_redis({ "zadd", tbname..":index:"..linkey, 0, rediskey }, uid)
end
if not nosync then
local columns
local values
for k, v in pairs(row) do
if not columns then
columns = k
else
columns = columns .. "," .. k
end
if not values then
values = "'" .. v .. "'"
else
values = values .. "," .. "'" .. v .. "'"
end
end
local sql = "insert into " .. tbname .. "(" .. columns .. ") values(" .. values .. ")"
skynet.call("dbsync", "lua", "sync", sql)
end
return true
end
-- redis中删除一行记录,并同步到mysql
function CMD.delete(tbname, row, type, nosync)
local config
if type == 1 then
config = config_table[tbname]
elseif type == 2 then
config = user_table[tbname]
elseif type == 3 then
config = common_table[tbname]
end
local uid
if row.uid and type == 2 then
uid = row.uid
end
local key = config.key
local indexkey = config.indexkey
local rediskey = make_rediskey(row, key)
local pk = schema[tbname]["pk"]
do_redis({ "del", tbname .. ":" .. rediskey }, uid)
if indexkey then
local linkey = make_rediskey(row,indexkey)
do_redis({ "zrem", tbname .. ":index:" .. linkey }, uid)
end
if not nosync then
local sql = "delete from " .. tbname.. " where " .. pk .. "=" .. "'" .. row[pk] .. "'"
skynet.call("dbsync", "lua", "sync", sql)
end
return true
end
-- redis中更新一行记录,并同步到mysql
function CMD.update(tbname, row, type, nosync)
local config
if type == 1 then
config = config_table[tbname]
elseif type == 2 then
config = user_table[tbname]
elseif type == 3 then
config = common_table[tbname]
end
local uid
if row.uid and type == 2 then
uid = row.uid
end
local key = config.key
local rediskey = make_rediskey(row, key)
do_redis({ "hmset", tbname .. ":" .. rediskey, row }, uid)
if not nosync then
local setvalues = ""
for k, v in pairs(row) do
setvalues = setvalues .. k .. "='" .. v .. "',"
end
setvalues = setvalues:trim(",")
local pk = schema[tbname]["pk"]
local sql = "update " .. tbname .. " set " .. setvalues .. " where " .. pk .. "='" .. row[pk] .. "'"
skynet.call("dbsync", "lua", "sync", sql)
end
return true
end
function CMD.get_table_key(tbname, type)
local t
if type == 1 then
t = config_table
elseif type == 2 then
t = user_table
elseif type == 3 then
t = common_table
end
return schema[tbname]["pk"], t[tbname].key, t[tbname].indexkey
end
function CMD.refresh_cache( )
load_config_data()
end
skynet.start(function()
skynet.dispatch("lua", function(session, source, cmd, ...)
local f = assert(CMD[cmd], cmd .. "not found")
skynet.retpack(f(...))
end)
skynet.register(SERVICE_NAME)
end)