--合服逻辑 --[=[ 0. --创建一个mongo连接实例 ]=]-- local DB = require("common.DB") local LuaMongo = _G.lua_mongo local Config = require("Config") local Log = require("common.Log") local MergeServerDefine = require("merge.MergeServerDefine") local primaryKeyTable = {} local dbcache = {} --重复_id缓存表 local repeatIdTb = {} --创建一个mongo连接实例 local function clientMongoDb() LuaMongo.client(Config.DB_IP) end --切换数据库 local function chooseMongoDb(dbName) if not dbName or dbName == "" then print(string.format("数据库名错误, dbName = %s\n", dbName)) return end LuaMongo.auth(dbName, Config.DB_USER, Config.DB_PASS) end --生成新的 _id local function generateNewId() local newId = LuaMongo.id() return newId end ---------------------------------------优化----------------------------- local function processCollection(sourceDb, targetDb, collectionName) local finalName = sourceDb .. collectionName local targetCollection = targetDb .. collectionName LuaMongo.find(finalName, nil, {}) while true do local data = {} if not LuaMongo.next(data) then break end --直接插入目标库 local ok, err = pcall(LuaMongo.insert, targetCollection, data) if not ok then print(string.format("=================插入失败, sourceDb = %s, collectionName = %s, _id = %s, err = %s\n", sourceDb, collectionName, data._id, err)) --Log.write(Log.LOGID_OSS_MERGE, finalName, data._id, err) end end end local writeQueue = {} local function flushWriteQueue() for _, task in ipairs(writeQueue) do local ok, err = pcall(LuaMongo.insert, task.collection, task.data) if not ok then Log.error("插入失败", task) end end writeQueue = {} end -- 每积累100条数据批量写入 local function bufferedInsert(collection, data) table.insert(writeQueue, {collection=collection, data=data}) if #writeQueue >= 100 then flushWriteQueue() end end ---------------------------------------------------------------- -----------------------------------------------------char集合中_id的重复检测和处理------------------------------------------------- --用于检测不同数据库的char集合中的_id是否有重复, 一般来说通过mongo生成的_id基本不会重复,但是还是检测下 local function primaryKeyRepeatCheck(dbName) if not dbName or dbName == "" then print(string.format("数据库名错误, dbName = %s\n", dbName)) return end local collectionName = MergeServerDefine.COLLECTIONS[1] local finalName = dbName .. collectionName LuaMongo.find(finalName, nil, {newUniqueTag = 1, _id = 1}) while true do local data = {} if not LuaMongo.next(data) then break end if primaryKeyTable[data._id] then print(string.format("重复的_id, dbName = %s, _id = %s\n", dbName, data._id)) end primaryKeyTable[data._id] = '1' end end --好友集合 local function handleFriend(data) if repeatIdTb[data.uuid1] then data.uuid1 = repeatIdTb[data.uuid1] end if repeatIdTb[data.uuid2] then data.uuid2 = repeatIdTb[data.uuid2] end end --邮件集合 local function handleMail(data) if repeatIdTb[data.receiverUuid] then data.receiverUuid = repeatIdTb[data.receiverUuid] end end --公会集合 local function handleUnion(data) local tb = {} for id, v in pairs(data.member) do if repeatIdTb[id] then tb[id] = v end end for id, v in pairs(tb) do data.member[id] = nil data.member[repeatIdTb[id]] = v end tb = {} for id, v in pairs(data.apply) do if repeatIdTb[id] then tb[id] = v end end for id, v in pairs(tb) do data.apply[id] = nil data.apply[repeatIdTb[id]] = v end if data.presidentUuid and repeatIdTb[data.presidentUuid] then data.presidentUuid = repeatIdTb[data.presidentUuid] end end --星空争霸集合 local function handleStar(data) if repeatIdTb[data.uuid] then data.uuid = repeatIdTb[data.uuid] end end --单人竞技场集合 local function handleJJC(data) if not data.monsterOutID and repeatIdTb[data._id] then data._id = repeatIdTb[data._id] end end --战斗视频集合 local function handleVideo(data) local uuid = data.combatInfo.attacker.uuid if repeatIdTb[uuid] then data.combatInfo.attacker.uuid = repeatIdTb[uuid] end end --------------------------------------------------------------------------------------------------------------------------- local tblList = { "ckwy_fy_S350064", "ckwy_fy_S350004" } local collName = ".middle_act_group" --合服开始函数 function StartMergeServer() print("========================合服开始=====================\n") --连接数据库 clientMongoDb() local targetDb = MergeServerDefine.MERGEDBTB[1] for i = 2, #MergeServerDefine.MERGEDBTB do local sourceDb = MergeServerDefine.MERGEDBTB[i] LuaMongo.auth(sourceDb, Config.DB_USER, Config.DB_PASS) for _, coll in ipairs(MergeServerDefine.COLLECTIONS) do if not MergeServerDefine.NOINSERTCOLLECTIONS[coll] then print(string.format("=====================开始合并集合: %s\n", coll)) processCollection(sourceDb, targetDb, coll) end end end -- for _, db in ipairs(tblList) do -- LuaMongo.auth(db, Config.DB_USER, Config.DB_PASS) -- local coll = db .. collName -- LuaMongo.find(coll) -- while true do -- local data = {} -- if not LuaMongo.next(data) then -- break -- end -- print(string.format("================[StartMergeServer], _id : %s, startIdx :%s, endIdx :%s\n", data._id, data.startIdx, data.endIdx)) -- end -- end end