Skip to content

Commit 2f75d98

Browse files
committed
https://github.com/kbengine/kbengine/issues/143
def持久化属性支持index标签, 在数据库中标明是索引. 值可以是NONE, UNIQUE,INDEX,分别对应无设置、唯一索引、普通索引,要这样就方便了
1 parent fd20de0 commit 2f75d98

File tree

6 files changed

+166
-33
lines changed

6 files changed

+166
-33
lines changed

kbe/src/lib/db_mysql/entity_table_mysql.cpp

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ bool EntityTableMysql::initialize(ScriptDefModule* sm, std::string name)
114114
// 找到所有存储属性并且创建出所有的字段
115115
ScriptDefModule::PROPERTYDESCRIPTION_MAP& pdescrsMap = sm->getPersistentPropertyDescriptions();
116116
ScriptDefModule::PROPERTYDESCRIPTION_MAP::const_iterator iter = pdescrsMap.begin();
117+
std::string hasUnique = "";
117118

118119
for(; iter != pdescrsMap.end(); iter++)
119120
{
@@ -199,6 +200,132 @@ void EntityTableMysql::init_db_item_name()
199200
}
200201
}
201202

203+
//-------------------------------------------------------------------------------------
204+
bool EntityTableMysql::syncIndexToDB(DBInterface* dbi)
205+
{
206+
std::vector<EntityTableItem*> indexs;
207+
208+
EntityTable::TABLEITEM_MAP::iterator iter = tableItems_.begin();
209+
for(; iter != tableItems_.end(); iter++)
210+
{
211+
if(strlen(iter->second->indexType()) == 0)
212+
continue;
213+
214+
indexs.push_back(iter->second.get());
215+
}
216+
217+
// 没有索引需要创建
218+
if(indexs.size() == 0)
219+
return true;
220+
221+
char sql_str[MAX_BUF];
222+
223+
kbe_snprintf(sql_str, MAX_BUF, "show index from "ENTITY_TABLE_PERFIX"_%s",
224+
tableName());
225+
226+
try
227+
{
228+
bool ret = dbi->query(sql_str, strlen(sql_str), false);
229+
if(!ret)
230+
{
231+
return false;
232+
}
233+
}
234+
catch(...)
235+
{
236+
return false;
237+
}
238+
239+
KBEUnordered_map<std::string, std::string> getkeys;
240+
241+
MYSQL_RES * pResult = mysql_store_result(static_cast<DBInterfaceMysql*>(dbi)->mysql());
242+
if(pResult)
243+
{
244+
MYSQL_ROW arow;
245+
while((arow = mysql_fetch_row(pResult)) != NULL)
246+
{
247+
std::string keytype = "UNIQUE";
248+
249+
if(std::string("1") == arow[1])
250+
keytype = "INDEX";
251+
252+
std::string keyname = arow[2];
253+
std::string colname = arow[4];
254+
255+
if(keyname == "PRIMARY" || colname != keyname)
256+
continue;
257+
258+
getkeys[colname] = keytype;
259+
}
260+
261+
mysql_free_result(pResult);
262+
}
263+
264+
bool done = false;
265+
std::string sql = fmt::format("ALTER TABLE "ENTITY_TABLE_PERFIX"_{} ", tableName());
266+
std::vector<EntityTableItem*>::iterator iiter = indexs.begin();
267+
for(; iiter != indexs.end(); )
268+
{
269+
std::string itemname = fmt::format(TABLE_ITEM_PERFIX"_{}", (*iiter)->itemName());
270+
KBEUnordered_map<std::string, std::string>::iterator fiter = getkeys.find(itemname);
271+
if(fiter != getkeys.end())
272+
{
273+
if(fiter->second != (*iiter)->indexType())
274+
{
275+
sql += fmt::format("DROP INDEX `{}`,", itemname);
276+
done = true;
277+
}
278+
else
279+
{
280+
iiter = indexs.erase(iiter);
281+
continue;
282+
}
283+
}
284+
285+
std::string lengthinfos = "";
286+
if((*iiter)->type() == TABLE_ITEM_TYPE_BLOB ||
287+
(*iiter)->type() == TABLE_ITEM_TYPE_STRING ||
288+
(*iiter)->type() == TABLE_ITEM_TYPE_UNICODE ||
289+
(*iiter)->type() == TABLE_ITEM_TYPE_PYTHON)
290+
{
291+
if((*iiter)->pPropertyDescription()->getDatabaseLength() == 0)
292+
{
293+
ERROR_MSG(fmt::format("EntityTableMysql::syncIndexToDB(): INDEX({}) without a key length, *.def-><{}>-><DatabaseLength> ? </DatabaseLength>",
294+
(*iiter)->itemName(), (*iiter)->itemName()));
295+
}
296+
else
297+
{
298+
lengthinfos = fmt::format("({})", (*iiter)->pPropertyDescription()->getDatabaseLength());
299+
}
300+
}
301+
302+
sql += fmt::format("ADD {} {}({}{}),", (*iiter)->indexType(), itemname, itemname, lengthinfos);
303+
iiter++;
304+
done = true;
305+
}
306+
307+
sql.erase(sql.end() - 1);
308+
309+
// 没有需要修改或者添加的
310+
if(!done)
311+
return true;
312+
313+
try
314+
{
315+
bool ret = dbi->query(sql.c_str(), sql.size(), true);
316+
if(!ret)
317+
{
318+
return false;
319+
}
320+
}
321+
catch(...)
322+
{
323+
return false;
324+
}
325+
326+
return true;
327+
}
328+
202329
//-------------------------------------------------------------------------------------
203330
bool EntityTableMysql::syncToDB(DBInterface* dbi)
204331
{
@@ -278,6 +405,10 @@ bool EntityTableMysql::syncToDB(DBInterface* dbi)
278405
}
279406
}
280407

408+
// 同步表索引
409+
if(!syncIndexToDB(dbi))
410+
return false;
411+
281412
sync_ = true;
282413
return true;
283414
}
@@ -1123,6 +1254,7 @@ bool EntityTableItemMysqlBase::initialize(const PropertyDescription* pPropertyDe
11231254

11241255
pDataType_ = pDataType;
11251256
pPropertyDescription_ = pPropertyDescription;
1257+
indexType_ = pPropertyDescription->indexType();
11261258
return true;
11271259
}
11281260

kbe/src/lib/db_mysql/entity_table_mysql.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,11 @@ class EntityTableMysql : public EntityTable
508508
*/
509509
virtual bool syncToDB(DBInterface* dbi);
510510

511+
/**
512+
同步表索引
513+
*/
514+
virtual bool syncIndexToDB(DBInterface* dbi);
515+
511516
/**
512517
创建一个表item
513518
*/

kbe/src/lib/db_mysql/kbe_table_mysql.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class KBEEntityLogTableMysql : public KBEEntityLogTable
4343
同步entity表到数据库中
4444
*/
4545
virtual bool syncToDB(DBInterface* dbi);
46-
46+
virtual bool syncIndexToDB(DBInterface* dbi){ return true; }
47+
4748
virtual bool logEntity(DBInterface * dbi, const char* ip, uint32 port, DBID dbid,
4849
COMPONENT_ID componentID, ENTITY_ID entityID, ENTITY_SCRIPT_UID entityType);
4950

@@ -64,7 +65,8 @@ class KBEAccountTableMysql : public KBEAccountTable
6465
同步entity表到数据库中
6566
*/
6667
virtual bool syncToDB(DBInterface* dbi);
67-
68+
virtual bool syncIndexToDB(DBInterface* dbi){ return true; }
69+
6870
bool queryAccount(DBInterface * dbi, const std::string& name, ACCOUNT_INFOS& info);
6971
bool queryAccountAllInfos(DBInterface * dbi, const std::string& name, ACCOUNT_INFOS& info);
7072
bool logAccount(DBInterface * dbi, ACCOUNT_INFOS& info);
@@ -85,6 +87,7 @@ class KBEEmailVerificationTableMysql : public KBEEmailVerificationTable
8587
同步entity表到数据库中
8688
*/
8789
virtual bool syncToDB(DBInterface* dbi);
90+
virtual bool syncIndexToDB(DBInterface* dbi){ return true; }
8891

8992
virtual bool queryAccount(DBInterface * dbi, int8 type, const std::string& name, ACCOUNT_INFOS& info);
9093
virtual bool logAccount(DBInterface * dbi, int8 type, const std::string& name, const std::string& datas, const std::string& code);

kbe/src/lib/dbmgr_lib/entity_table.hpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ class EntityTableItem
102102
pPropertyDescription_(NULL),
103103
itemDBType_(itemDBType),
104104
datalength_(datalength),
105-
flags_(flags)
105+
flags_(flags),
106+
indexType_()
106107
{
107108
};
108109

@@ -115,6 +116,11 @@ class EntityTableItem
115116
void itemName(std::string name){ itemName_ = name; }
116117
const char* itemName(){ return itemName_.c_str(); }
117118

119+
void indexType(std::string index){ indexType_ = index; }
120+
const char* indexType(){ return indexType_.c_str(); }
121+
122+
const char* itemDBType(){ return itemDBType_.c_str(); }
123+
118124
void utype(int32/*ENTITY_PROPERTY_UID*/ utype){ utype_ = utype; }
119125
int32 utype(){ return utype_; }
120126

@@ -129,6 +135,10 @@ class EntityTableItem
129135

130136
const DataType* pDataType(){ return pDataType_; }
131137

138+
uint32 datalength()const{ return datalength_; }
139+
140+
const PropertyDescription* pPropertyDescription()const{ return pPropertyDescription_; }
141+
132142
/**
133143
初始化
134144
*/
@@ -167,6 +177,8 @@ class EntityTableItem
167177
std::string itemDBType_;
168178
uint32 datalength_;
169179
uint32 flags_;
180+
181+
std::string indexType_;
170182
};
171183

172184
/*
@@ -201,6 +213,11 @@ class EntityTable
201213
*/
202214
virtual bool syncToDB(DBInterface* dbi) = 0;
203215

216+
/**
217+
同步entity表索引到数据库中
218+
*/
219+
virtual bool syncIndexToDB(DBInterface* dbi) = 0;
220+
204221
/**
205222
创建一个表item
206223
*/
@@ -243,7 +260,7 @@ class EntityTable
243260
// 所有的字段
244261
TABLEITEM_MAP tableItems_;
245262

246-
// 和ScriptDefModule中保持一致持续的item引用
263+
// 和ScriptDefModule中保持一致秩序的item引用
247264
std::vector<EntityTableItem*> tableFixedOrderItems_;
248265

249266
// 是否为子表

kbe/src/lib/entitydef/entitydef.cpp

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -208,30 +208,6 @@ bool EntityDef::initialize(std::vector<PyTypeObject*>& scriptBaseTypes,
208208

209209
return false;
210210
}
211-
212-
// 检查索引的合法性
213-
ScriptDefModule::PROPERTYDESCRIPTION_MAP tpmaps;
214-
tpmaps.insert(scriptModule->getBasePropertyDescriptions().begin(), scriptModule->getBasePropertyDescriptions().end());
215-
tpmaps.insert(scriptModule->getCellPropertyDescriptions().begin(), scriptModule->getCellPropertyDescriptions().end());
216-
tpmaps.insert(scriptModule->getClientPropertyDescriptions().begin(), scriptModule->getClientPropertyDescriptions().end());
217-
218-
ScriptDefModule::PROPERTYDESCRIPTION_MAP::iterator pmiter = tpmaps.begin();
219-
std::string hasUnique = "";
220-
for(; pmiter != tpmaps.end(); pmiter++)
221-
{
222-
if(strcmp(pmiter->second->indexType(), "unique") == 0)
223-
{
224-
if(hasUnique.size() != 0)
225-
{
226-
ERROR_MSG(fmt::format("EntityDef::initialize({}):can be only one \'unique index\'({}, {})!\n",
227-
moduleName.c_str(), pmiter->second->getName(), hasUnique));
228-
229-
return false;
230-
}
231-
232-
hasUnique = pmiter->second->getName();
233-
}
234-
}
235211

236212
scriptModule->onLoaded();
237213
}
@@ -661,7 +637,7 @@ bool EntityDef::loadDefPropertys(const std::string& moduleName,
661637
indexType = xml->getValStr(indexTypeNode);
662638

663639
std::transform(indexType.begin(), indexType.end(),
664-
indexType.begin(), tolower); // 转换为小写
640+
indexType.begin(), toupper); // 转换为大写
665641
}
666642

667643

@@ -676,10 +652,10 @@ bool EntityDef::loadDefPropertys(const std::string& moduleName,
676652
isIdentifier = true;
677653
}
678654

679-
//TiXmlNode* databaseLengthNode = xml->enterNode(defPropertyNode->FirstChild(), "Identifier");
680-
if(identifierNode)
655+
TiXmlNode* databaseLengthNode = xml->enterNode(defPropertyNode->FirstChild(), "DatabaseLength");
656+
if(databaseLengthNode)
681657
{
682-
databaseLength = xml->getValInt(identifierNode);
658+
databaseLength = xml->getValInt(databaseLengthNode);
683659
}
684660

685661
TiXmlNode* defaultValNode =

kbe/src/server/dbmgr/dbmgr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ bool Dbmgr::initializeEnd()
209209
loopCheckTimerHandle_ = this->mainDispatcher().addTimer(1000000, this,
210210
reinterpret_cast<void *>(TIMEOUT_CHECK_STATUS));
211211

212-
mainProcessTimer_ = this->mainDispatcher().addTimer(1000000 / g_kbeSrvConfig.gameUpdateHertz(), this,
212+
mainProcessTimer_ = this->mainDispatcher().addTimer(1000000 / 50, this,
213213
reinterpret_cast<void *>(TIMEOUT_TICK));
214214

215215
// 添加globalData, baseAppData, cellAppData支持

0 commit comments

Comments
 (0)