NF_SHARE_PTR<NFIRecordManager> NFCCommonRedisModule::NewRecordManager(const std::string& strClassName) { NF_SHARE_PTR<NFIRecordManager> pStaticClassRecordManager = m_pLogicClassModule->GetClassRecordManager(strClassName); if (pStaticClassRecordManager) { NFGUID ident; NF_SHARE_PTR<NFIRecordManager> pRecordManager(NF_NEW NFCRecordManager(ident)); NF_SHARE_PTR<NFIRecord> pConfigRecordInfo = pStaticClassRecordManager->First(); while (pConfigRecordInfo) { if (pConfigRecordInfo->GetSave() || pConfigRecordInfo->GetCache()) { NF_SHARE_PTR<NFIRecord> xRecord = pRecordManager->AddRecord(ident, pConfigRecordInfo->GetName(), pConfigRecordInfo->GetInitData(), pConfigRecordInfo->GetTag(), pConfigRecordInfo->GetRows()); xRecord->SetPublic(pConfigRecordInfo->GetPublic()); xRecord->SetPrivate(pConfigRecordInfo->GetPrivate()); xRecord->SetSave(pConfigRecordInfo->GetSave()); xRecord->SetCache(pConfigRecordInfo->GetCache()); } pConfigRecordInfo = pStaticClassRecordManager->Next(); } return pRecordManager; } return NF_SHARE_PTR<NFIRecordManager>(NULL); }
NF_SHARE_PTR<NFIPropertyManager> NFCCommonRedisModule::NewPropertyManager(const std::string& strClassName) { NF_SHARE_PTR<NFIPropertyManager> pStaticClassPropertyManager = m_pLogicClassModule->GetClassPropertyManager(strClassName); if (pStaticClassPropertyManager) { NFGUID ident; NF_SHARE_PTR<NFIPropertyManager> pPropertyManager(NF_NEW NFCPropertyManager(ident)); NF_SHARE_PTR<NFIProperty> pStaticConfigPropertyInfo = pStaticClassPropertyManager->First(); while (pStaticConfigPropertyInfo) { if (pStaticConfigPropertyInfo->GetSave() || pStaticConfigPropertyInfo->GetCache()) { NF_SHARE_PTR<NFIProperty> xProperty = pPropertyManager->AddProperty(ident, pStaticConfigPropertyInfo->GetKey(), pStaticConfigPropertyInfo->GetType()); xProperty->SetPublic(pStaticConfigPropertyInfo->GetPublic()); xProperty->SetPrivate(pStaticConfigPropertyInfo->GetPrivate()); xProperty->SetSave(pStaticConfigPropertyInfo->GetSave()); xProperty->SetCache(pStaticConfigPropertyInfo->GetCache()); xProperty->SetRef(pStaticConfigPropertyInfo->GetRef()); } pStaticConfigPropertyInfo = pStaticClassPropertyManager->Next(); } return pPropertyManager; } return NF_SHARE_PTR<NFIPropertyManager>(NULL); }
bool NFCCreateRoleModule::ConvertRecordManagerToPB(const NF_SHARE_PTR<NFIRecordManager>& pRecordManager, NFMsg::ObjectRecordList * pRecordDataList) { if (pRecordDataList == nullptr) { return false; } for (NF_SHARE_PTR<NFIRecord> pRecord = pRecordManager->First(); pRecord != NULL; pRecord = pRecordManager->Next()) { if (!pRecord->GetCache() && !pRecord->GetSave()) { continue; } NFMsg::ObjectRecordBase* pRecordData = pRecordDataList->add_record_list(); if (!pRecordData) { continue; } ConvertRecordToPB(pRecord, pRecordData); } return true; }
bool NFCCommonRedisModule::ConvertRecordManagerToVector(NF_SHARE_PTR<NFIRecordManager> pRecordManager, std::vector<std::string>& vKeyList, std::vector<std::string>& vValueList) { for (NF_SHARE_PTR<NFIRecord> pRecord = pRecordManager->First(); pRecord != NULL; pRecord = pRecordManager->Next()) { if (!pRecord->GetCache() && !pRecord->GetSave()) { continue; } NFMsg::ObjectRecordBase xRecordData; ConvertRecordToPB(pRecord, &xRecordData); //// std::string strValue; if (!xRecordData.SerializeToString(&strValue)) { continue; } vKeyList.push_back(xRecordData.record_name()); vValueList.push_back(strValue); } return true; }
bool NFCCommonRedisModule::ConvertVectorToRecordManager(std::vector<std::string>& vKeyList, std::vector<std::string>& vValueList, NF_SHARE_PTR<NFIRecordManager> pRecordManager) { if (vKeyList.size() == vValueList.size()) { for (int i = 0; i < vKeyList.size(); ++i) { const std::string& strKey = vKeyList[i]; const std::string& strValue = vValueList[i]; if (strValue.empty()) { continue; } NF_SHARE_PTR<NFIRecord> pRecord = pRecordManager->GetElement(strKey); if (!pRecord->GetCache() && !pRecord->GetSave()) { continue; } NFMsg::ObjectRecordBase xRecordData; if (xRecordData.ParseFromString(strValue)) { ConvertPBToRecord(xRecordData, pRecord); } else { //TODO //error } } } return true; }
bool NFCCommonRedisModule::ConvertVectorToPropertyManager(std::vector<std::string>& vKeyList, std::vector<std::string>& vValueList, NF_SHARE_PTR<NFIPropertyManager> pPropertyManager) { if (vKeyList.size() == vValueList.size()) { for (int i = 0; i < vKeyList.size(); ++i) { const std::string& strKey = vKeyList[i]; const std::string& strValue = vValueList[i]; if (strValue.empty()) { continue; } NF_SHARE_PTR<NFIProperty> pProperty = pPropertyManager->GetElement(strKey); if (!pProperty->GetCache() && !pProperty->GetSave()) { continue; } if(!pProperty->FromString(strValue)) { //TODO //error } } } return true; }
NF_SHARE_PTR<NFIPropertyManager> NFCCommonRedisModule::GetPropertyInfo(const NFGUID& self, const std::string& strClassName, std::vector<std::string>& vKeyCacheList, std::vector<std::string>& vValueCacheList) { //TODO optimize NF_SHARE_PTR<NFIPropertyManager> pPropertyManager = NewPropertyManager(strClassName); if (!pPropertyManager) { return nullptr; } NF_SHARE_PTR<NFIRedisClient> pDriver = m_pNoSqlModule->GetDriverBySuit(self.ToString()); if (!pDriver) { return nullptr; } //TODO //just run this function one time NF_SHARE_PTR<NFIProperty> xProperty = pPropertyManager->First(); while (xProperty) { if (xProperty->GetCache() || xProperty->GetSave()) { vKeyCacheList.push_back(xProperty->GetKey()); } xProperty = pPropertyManager->Next(); } //cache std::string strCacheKey = GetPropertyCacheKey(self); if (!pDriver->HMGET(strCacheKey, vKeyCacheList, vValueCacheList)) { return nullptr; } if (vKeyCacheList.size() == vValueCacheList.size()) { ConvertVectorToPropertyManager(vKeyCacheList, vValueCacheList, pPropertyManager); return pPropertyManager; } return nullptr; }
NF_SHARE_PTR<NFIProperty> NFCPropertyManager::AddProperty(const NFGUID& self, NF_SHARE_PTR<NFIProperty> pProperty) { const std::string& strProperty = pProperty->GetKey(); NF_SHARE_PTR<NFIProperty> pOldProperty = this->GetElement(strProperty); if (!pOldProperty.get()) { NF_SHARE_PTR<NFIProperty> pNewProperty(NF_NEW NFCProperty(self, strProperty, pProperty->GetType())); pNewProperty->SetPublic(pProperty->GetPublic()); pNewProperty->SetPrivate(pProperty->GetPrivate()); pNewProperty->SetSave(pProperty->GetSave()); pNewProperty->SetCache(pProperty->GetCache()); pNewProperty->SetRelationValue(pProperty->GetRelationValue()); this->AddElement(strProperty, pNewProperty); } return pOldProperty; }
bool NFCCommonRedisModule::ConvertPropertyManagerToVector(NF_SHARE_PTR<NFIPropertyManager> pPropertyManager, std::vector<std::string>& vKeyList, std::vector<std::string>& vValueList) { for (NF_SHARE_PTR<NFIProperty> pProperty = pPropertyManager->First(); pProperty != NULL; pProperty = pPropertyManager->Next()) { const int nType = pProperty->GetType(); if (!pProperty->GetCache() && !pProperty->GetSave()) { continue; } const std::string& strPropertyName = pProperty->GetKey(); const std::string& strPropertyValue = pProperty->ToString(); vKeyList.push_back(strPropertyName); vValueList.push_back(strPropertyValue); } return true; }
NF_SHARE_PTR<NFIRecordManager> NFCCommonRedisModule::GetRecordInfo(const NFGUID& self, const std::string& strClassName, std::vector<std::string>& vKeyCacheList, std::vector<std::string>& vValueCacheList) { NF_SHARE_PTR<NFIRecordManager> pRecordManager = NewRecordManager(strClassName); if (!pRecordManager.get()) { return nullptr; } NF_SHARE_PTR<NFIRedisClient> pDriver = m_pNoSqlModule->GetDriverBySuit(self.ToString()); if (!pDriver) { return nullptr; } //TODO //just run this function one time NF_SHARE_PTR<NFIRecord> xRecord = pRecordManager->First(); while (xRecord) { if (xRecord->GetCache() || xRecord->GetSave()) { vKeyCacheList.push_back(xRecord->GetName()); } xRecord = pRecordManager->Next(); } //cache std::string strCacheKey = GetRecordCacheKey(self); if (!pDriver->HMGET(strCacheKey, vKeyCacheList, vValueCacheList)) { return nullptr; } if (vKeyCacheList.size() == vValueCacheList.size()) { ConvertVectorToRecordManager(vKeyCacheList, vValueCacheList, pRecordManager); return pRecordManager; } return nullptr; }
bool NFCElementInfoModule::Load(rapidxml::xml_node<>* attrNode, NF_SHARE_PTR<NFILogicClass> pLogicClass) { //attrNode is the node of a object std::string strConfigID = attrNode->first_attribute("ID")->value(); if (strConfigID.empty()) { NFASSERT(0, strConfigID, __FILE__, __FUNCTION__); return false; } if (ExistElement(strConfigID)) { NFASSERT(0, strConfigID, __FILE__, __FUNCTION__); return false; } NF_SHARE_PTR<ElementConfigInfo> pElementInfo(NF_NEW ElementConfigInfo()); AddElement(strConfigID, pElementInfo); //can find all configid by class name pLogicClass->AddConfigName(strConfigID); //ElementConfigInfo* pElementInfo = CreateElement( strConfigID, pElementInfo ); NF_SHARE_PTR<NFIPropertyManager> pElementPropertyManager = pElementInfo->GetPropertyManager(); NF_SHARE_PTR<NFIRecordManager> pElementRecordManager = pElementInfo->GetRecordManager(); //1.add property //2.set the default value of them NF_SHARE_PTR<NFIPropertyManager> pClassPropertyManager = pLogicClass->GetPropertyManager(); NF_SHARE_PTR<NFIRecordManager> pClassRecordManager = pLogicClass->GetRecordManager(); if (pClassPropertyManager.get() && pClassRecordManager.get()) { NF_SHARE_PTR<NFIProperty> pProperty = pClassPropertyManager->First(); while (pProperty.get()) { pElementPropertyManager->AddProperty(NFGUID(), pProperty); pProperty = pClassPropertyManager->Next(); } NF_SHARE_PTR<NFIRecord> pRecord = pClassRecordManager->First(); while (pRecord.get()) { pElementRecordManager->AddRecord(NFGUID(), pRecord->GetName(), pRecord->GetInitData(), pRecord->GetKeyState(), pRecord->GetInitDesc(), pRecord->GetTag(), pRecord->GetRelatedRecord(), pRecord->GetRows(), pRecord->GetPublic(), pRecord->GetPrivate(), pRecord->GetSave(), pRecord->GetView(), pRecord->GetIndex()); pRecord = pClassRecordManager->Next(); } } //3.set the config value to them //const char* pstrConfigID = attrNode->first_attribute( "ID" ); for (rapidxml::xml_attribute<>* pAttribute = attrNode->first_attribute(); pAttribute; pAttribute = pAttribute->next_attribute()) { const char* pstrConfigName = pAttribute->name(); const char* pstrConfigValue = pAttribute->value(); //printf( "%s : %s\n", pstrConfigName, pstrConfigValue ); NF_SHARE_PTR<NFIProperty> temProperty = pElementPropertyManager->GetElement(pstrConfigName); if (!temProperty) { continue; } NFIDataList::TData var; TDATA_TYPE eType = temProperty->GetType(); switch (eType) { case TDATA_INT: { if (!LegalNumber(pstrConfigValue)) { NFASSERT(0, temProperty->GetKey(), __FILE__, __FUNCTION__); } var.SetInt(lexical_cast<NFINT64>(pstrConfigValue)); } break; case TDATA_FLOAT: { if (strlen(pstrConfigValue) <= 0) { NFASSERT(0, temProperty->GetKey(), __FILE__, __FUNCTION__); } var.SetFloat((double)atof(pstrConfigValue)); } break; case TDATA_STRING: var.SetString(pstrConfigValue); break; case TDATA_OBJECT: { if (strlen(pstrConfigValue) <= 0) { NFASSERT(0, temProperty->GetKey(), __FILE__, __FUNCTION__); } var.SetObject(NFGUID()); } break; default: NFASSERT(0, temProperty->GetKey(), __FILE__, __FUNCTION__); break; } pElementPropertyManager->SetProperty(pstrConfigName, var); } NFIDataList::TData xData; xData.SetString(pLogicClass->GetClassName()); pElementPropertyManager->SetProperty("ClassName", xData); return true; }
NF_SHARE_PTR<NFIProperty> NFCPropertyManager::AddProperty(const NFIDENTID& self, NF_SHARE_PTR<NFIProperty> pProperty) { const std::string& strProperty = pProperty->GetKey(); NF_SHARE_PTR<NFIProperty> pOldProperty = this->GetElement(strProperty); if (!pOldProperty.get()) { NF_SHARE_PTR<NFIProperty> pNewProperty(NF_NEW NFCProperty(self, strProperty, pProperty->GetType(), pProperty->GetPublic(), pProperty->GetPrivate(), pProperty->GetSave(), pProperty->GetView(), pProperty->GetIndex(), pProperty->GetRelationValue())); this->AddElement(strProperty, pNewProperty); if (pProperty->GetIndex() > 0) { mxPropertyIndexMap.insert(std::map<std::string, int>::value_type(strProperty, pProperty->GetIndex())); } } return pOldProperty; }
bool NFCCreateRoleModule::ConvertPropertyManagerToPB(const NF_SHARE_PTR<NFIPropertyManager>& pProps, NFMsg::ObjectPropertyList * pPropertyData) { if (pProps) { NF_SHARE_PTR<NFIProperty> xPropert = pProps->First(); while (xPropert) { if (xPropert->GetCache() || xPropert->GetSave()) { switch (xPropert->GetType()) { case NFDATA_TYPE::TDATA_INT: { NFMsg::PropertyInt* pData = pPropertyData->add_property_int_list(); pData->set_property_name(xPropert->GetKey()); pData->set_data(xPropert->GetInt()); } break; case NFDATA_TYPE::TDATA_FLOAT: { NFMsg::PropertyFloat* pData = pPropertyData->add_property_float_list(); pData->set_property_name(xPropert->GetKey()); pData->set_data(xPropert->GetFloat()); } break; case NFDATA_TYPE::TDATA_OBJECT: { NFMsg::PropertyObject* pData = pPropertyData->add_property_object_list(); pData->set_property_name(xPropert->GetKey()); *(pData->mutable_data()) = NFINetModule::NFToPB(xPropert->GetObject()); } break; case NFDATA_TYPE::TDATA_STRING: { NFMsg::PropertyString* pData = pPropertyData->add_property_string_list(); pData->set_property_name(xPropert->GetKey()); pData->set_data(xPropert->GetString()); std::cout << xPropert->GetKey() << " " << xPropert->GetString() << std::endl; } break; case NFDATA_TYPE::TDATA_VECTOR2: { NFMsg::PropertyVector2* pData = pPropertyData->add_property_vector2_list(); pData->set_property_name(xPropert->GetKey()); *(pData->mutable_data()) = NFINetModule::NFToPB(xPropert->GetVector2()); } break; case NFDATA_TYPE::TDATA_VECTOR3: { NFMsg::PropertyVector3* pData = pPropertyData->add_property_vector3_list(); pData->set_property_name(xPropert->GetKey()); *(pData->mutable_data()) = NFINetModule::NFToPB(xPropert->GetVector3()); } break; default: break; } } xPropert = pProps->Next(); } } return false; }
const bool NFCObjectSaveModule::LoadDataFormNoSql( const NFIDENTID& self , const std::string& strClassName) { NF_SHARE_PTR<NFIPropertyManager> pProManager = m_pLogicClassModule->GetClassPropertyManager(strClassName); NF_SHARE_PTR<NFIRecordManager> pRecordManager = m_pLogicClassModule->GetClassRecordManager(strClassName); if (!pProManager || !pRecordManager) { return false; } std::vector<std::string> vFieldVec; std::vector<std::string> vValueVec; int nIndex = 0; std::string strName; std::map<std::string, int> xDataIndex; //witch Property Need Load NF_SHARE_PTR<NFIProperty> xProperty = pProManager->First(strName); while (xProperty) { if (xProperty->GetSave()) { vFieldVec.push_back(strName); xDataIndex.insert(std::make_pair(strName, nIndex)); nIndex ++; } strName.clear(); xProperty = pProManager->Next(strName); } //witch Record Need Load NF_SHARE_PTR<NFIRecord> xRecord = pRecordManager->First(strName); while (xRecord) { if (xRecord->GetSave()) { vFieldVec.push_back(strName); xDataIndex.insert(std::make_pair(strName, nIndex)); nIndex ++; } strName.clear(); xRecord = pRecordManager->Next(strName); } if(!m_pClusterSQLModule->Query(strClassName, self.ToString(), vFieldVec, vValueVec)) { return false; } if (vFieldVec.size() != vValueVec.size()) { return false; } NF_SHARE_PTR<NFMapEx<std::string, std::string> > pSelf = mtObjectCache.GetElement(self); if (!pSelf) { pSelf = NF_SHARE_PTR< NFMapEx<std::string, std::string> > (NF_NEW NFMapEx<std::string, std::string>()); if (!mtObjectCache.AddElement(self, pSelf)) { return false; } } for (int i = 0; i< vValueVec.size(); i++) { const std::string& strName = vFieldVec[i]; const std::string& strValue = vValueVec[i]; pSelf->AddElement(strName, NF_SHARE_PTR<std::string>(NF_NEW std::string(strValue))); } return true; }
const bool NFCObjectSaveModule::AttachData( const NFIDENTID& self ) { NF_SHARE_PTR<NFIObject> pObject = m_pKernelModule->GetObject(self); if (!pObject) { return false; } NF_SHARE_PTR<NFMapEx<std::string, std::string> > pSelfData = mtObjectCache.GetElement(self); if (!pSelfData) { return false; } NF_SHARE_PTR<NFIPropertyManager> pProManager = pObject->GetPropertyManager(); NF_SHARE_PTR<NFIRecordManager> pRecordManager = pObject->GetRecordManager(); //assign property value NF_SHARE_PTR<NFIProperty> xProperty = pProManager->First(strName); while (xProperty) { if (xProperty->GetSave()) { NF_SHARE_PTR<std::string> pValue = pSelfData->GetElement(strName); if (pValue) { const std::string& strData = *pValue; if (!xProperty->FromString(strData)) { m_pLogModule->LogNormal(NFILogModule::NLL_ERROR_NORMAL, self, "Load Property fail " + strName , ": " + strData, __FUNCTION__, __LINE__); } else { m_pLogModule->LogNormal(NFILogModule::NLL_INFO_NORMAL, self, "Load Property success " + strName, ": " + strData, __FUNCTION__, __LINE__); } } } strName.clear(); xProperty = pProManager->Next(strName); } //assign record value NF_SHARE_PTR<NFIRecord> xRecord = pRecordManager->First(strName); while (xRecord) { if (xRecord->GetSave()) { NF_SHARE_PTR<std::string> pValue = pSelfData->GetElement(strName); if (pValue) { const std::string& strData = *pValue; NFMsg::PlayerRecordBase xRecordData; if (xRecordData.ParseFromString(strData) && xRecordData.record_name() == strName) { ConvertPBToRecord(xRecordData, xRecord); } } } strName.clear(); xRecord = pRecordManager->Next(strName); } mtObjectCache.RemoveElement(self); return true; }
const bool NFCObjectSaveModule::SaveDataToNoSql( const NFIDENTID& self ) { NF_SHARE_PTR<NFIObject> pObject = m_pKernelModule->GetObject( self ); if ( pObject.get() ) { NF_SHARE_PTR<NFIPropertyManager> pProManager = pObject->GetPropertyManager(); NF_SHARE_PTR<NFIRecordManager> pRecordManager = pObject->GetRecordManager(); std::vector<std::string> vFieldVec; std::vector<std::string> vValueVec; //witch property to save std::string strName; NF_SHARE_PTR<NFIProperty> xProperty = pProManager->First(strName); while (xProperty) { if (xProperty->GetSave()) { vFieldVec.push_back(strName); vValueVec.push_back(xProperty->ToString()); } strName.clear(); xProperty = pProManager->Next(strName); } //witch Record to save NF_SHARE_PTR<NFIRecord> xRecord = pRecordManager->First(strName); while (xRecord) { if (xRecord->GetSave()) { NFMsg::PlayerRecordBase xRecordData; xRecordData.set_record_name(strName); for (int i = 0; i < xRecord->GetRows(); ++i) { if(xRecord->IsUsed(i)) { for (int j = 0; j < xRecord->GetCols(); ++j) { switch (xRecord->GetColType(j)) { case TDATA_INT: { NFMsg::RecordInt* pRecordInt = xRecordData.add_record_int_list(); pRecordInt->set_row(i); pRecordInt->set_col(j); pRecordInt->set_data(xRecord->GetInt(i, j)); } break; case TDATA_FLOAT: { NFMsg::RecordFloat* xRecordFloat = xRecordData.add_record_float_list(); xRecordFloat->set_row(i); xRecordFloat->set_col(j); xRecordFloat->set_data(xRecord->GetFloat(i, j)); } break; case TDATA_STRING: { NFMsg::RecordString* xRecordString = xRecordData.add_record_string_list(); xRecordString->set_row(i); xRecordString->set_col(j); xRecordString->set_data(xRecord->GetString(i, j)); } break; case TDATA_OBJECT: { NFMsg::RecordObject* xRecordObejct = xRecordData.add_record_object_list(); xRecordObejct->set_row(i); xRecordObejct->set_col(j); *xRecordObejct->mutable_data() = NFINetModule::NFToPB(xRecord->GetObject(i, j)); } break; default: break; } } } } std::string strRecordValue; if(xRecordData.SerializeToString(&strRecordValue)) { vFieldVec.push_back(strName); vValueVec.push_back(strRecordValue); } } strName.clear(); xRecord = pRecordManager->Next(strName); } const std::string& strClass = m_pKernelModule->GetPropertyString(self, "ClassName"); if(!m_pClusterSQLModule->Updata(strClass, self.ToString(), vFieldVec, vValueVec)) { return false; } return true; } return false; }