void CMessageModel::getUnReadCntAll(uint32_t nUserId, uint32_t &nTotalCnt) { CacheManager* pCacheManager = CacheManager::getInstance(); CacheConn* pCacheConn = pCacheManager->GetCacheConn("unread"); if (pCacheConn) { map<string, string> mapUnread; string strKey = "unread_" + int2string(nUserId); bool bRet = pCacheConn->hgetAll(strKey, mapUnread); pCacheManager->RelCacheConn(pCacheConn); if(bRet) { for (auto it = mapUnread.begin(); it != mapUnread.end(); it++) { nTotalCnt += atoi(it->second.c_str()); } } else { log("hgetall %s failed!", strKey.c_str()); } } else { log("no cache connection for unread"); } }
TEST_F(TestCacheManager, AddUncompressibleData) { string test_data = "let's hope this won't compress too terribly well."; string data_fn; ASSERT_TRUE(makeTempFile(test_data, &data_fn)); CacheManager cm; string name = "/test/name"; ASSERT_TRUE(cm.addFileContents(name, data_fn)); ASSERT_EQ(unlink(data_fn.c_str()), 0); ASSERT_TRUE(cm.dirExists("/test")); // Read it back from the cache. const char* data; uint64_t data_len; bool compressed; EXPECT_TRUE(cm.getFileContents(name, &data, &data_len, &compressed)); ASSERT_FALSE(compressed); EXPECT_EQ(data_len, test_data.length()); ASSERT_EQ(memcmp(test_data.c_str(), data, data_len), 0); uint64_t file_size; ASSERT_TRUE(cm.getUncompressedFileSize(name, &file_size)); EXPECT_EQ(file_size, test_data.length()); }
/* * 初始化函数,从cache里面加载上次同步的时间信息等 */ void CSyncCenter::init() { // Load total update time CacheManager* pCacheManager = CacheManager::getInstance(); // increase message count CacheConn* pCacheConn = pCacheManager->GetCacheConn("unread"); if (pCacheConn) { string strTotalUpdate = pCacheConn->get("total_user_updated"); string strLastUpdateGroup = pCacheConn->get("last_update_group"); pCacheManager->RelCacheConn(pCacheConn); if(strTotalUpdate != "") { m_nLastUpdate = string2int(strTotalUpdate); } else { updateTotalUpdate(time(NULL)); } if(strLastUpdateGroup.empty()) { m_nLastUpdateGroup = string2int(strLastUpdateGroup); } else { updateLastUpdateGroup(time(NULL)); } } else { log("no cache connection to get total_user_updated"); } }
TEST_F(TestCacheManager, NoLeadingSlash) { CacheManager cm; ASSERT_TRUE(cm.addEmptyEntry("foo/bar/test.php")); EXPECT_TRUE(cm.dirExists("foo")); EXPECT_TRUE(cm.dirExists("foo/bar")); EXPECT_TRUE(cm.entryExists("foo/bar/test.php")); }
TEST_F(TestCacheManager, Duplicates) { CacheManager cm; string name = "/test/name"; EXPECT_TRUE(cm.addEmptyEntry(name)); EXPECT_FALSE(cm.addEmptyEntry(name)); EXPECT_FALSE(cm.addFileContents(name, "/invalid/path")); }
// Long enough for the magic number read and check to happen (and fail). TEST_F(TestCacheManager, LoadNonCache) { string data = "_________________nope____________________"; string fn; ASSERT_TRUE(makeTempFile(data, &fn)); CacheManager cm; ASSERT_FALSE(cm.loadCache(fn)); ASSERT_EQ(unlink(fn.c_str()), 0); }
TEST_F(TestCacheManager, SaveNothing) { string temp_dir; ASSERT_TRUE(makeTempDir(&temp_dir)); string save_path(temp_dir); save_path.append("/cm.save"); CacheManager cm; ASSERT_TRUE(cm.saveCache(save_path)); ASSERT_EQ(unlink(save_path.c_str()), 0); }
void CMessageModel::incMsgCount(uint32_t nFromId, uint32_t nToId) { CacheManager* pCacheManager = CacheManager::getInstance(); // increase message count CacheConn* pCacheConn = pCacheManager->GetCacheConn("unread"); if (pCacheConn) { pCacheConn->hincrBy("unread_" + int2string(nToId), int2string(nFromId), 1); pCacheManager->RelCacheConn(pCacheConn); } else { log("no cache connection to increase unread count: %d->%d", nFromId, nToId); } }
uint32_t CMessageModel::getMsgId(uint32_t nRelateId) { uint32_t nMsgId = 0; CacheManager* pCacheManager = CacheManager::getInstance(); CacheConn* pCacheConn = pCacheManager->GetCacheConn("unread"); if(pCacheConn) { string strKey = "msg_id_" + int2string(nRelateId); nMsgId = pCacheConn->incrBy(strKey, 1); pCacheManager->RelCacheConn(pCacheConn); } return nMsgId; }
void CMessageModel::getUnreadMsgCount(uint32_t nUserId, uint32_t &nTotalCnt, list<IM::BaseDefine::UnreadInfo>& lsUnreadCount) { CacheManager* pCacheManager = CacheManager::getInstance(); CacheConn* pCacheConn = pCacheManager->GetCacheConn("unread"); if (pCacheConn) { map<string, string> mapUnread; string strKey = "unread_" + int2string(nUserId); bool bRet = pCacheConn->hgetAll(strKey, mapUnread); pCacheManager->RelCacheConn(pCacheConn); if(bRet) { IM::BaseDefine::UnreadInfo cUnreadInfo; for (auto it = mapUnread.begin(); it != mapUnread.end(); it++) { cUnreadInfo.set_session_id(atoi(it->first.c_str())); cUnreadInfo.set_unread_cnt(atoi(it->second.c_str())); cUnreadInfo.set_session_type(IM::BaseDefine::SESSION_TYPE_SINGLE); uint32_t nMsgId = 0; string strMsgData; IM::BaseDefine::MsgType nMsgType; getLastMsg(cUnreadInfo.session_id(), nUserId, nMsgId, strMsgData, nMsgType); if(IM::BaseDefine::MsgType_IsValid(nMsgType)) { cUnreadInfo.set_latest_msg_id(nMsgId); cUnreadInfo.set_latest_msg_data(strMsgData); cUnreadInfo.set_latest_msg_type(nMsgType); cUnreadInfo.set_latest_msg_from_user_id(cUnreadInfo.session_id()); lsUnreadCount.push_back(cUnreadInfo); nTotalCnt += cUnreadInfo.unread_cnt(); } else { log("invalid msgType. userId=%u, peerId=%u, msgType=%u", nUserId, cUnreadInfo.session_id(), nMsgType); } } } else { log("hgetall %s failed!", strKey.c_str()); } } else { log("no cache connection for unread"); } }
void CSyncCenter::updateTotalUpdate(uint32_t nUpdated) { CacheManager* pCacheManager = CacheManager::getInstance(); CacheConn* pCacheConn = pCacheManager->GetCacheConn("unread"); if (pCacheConn) { last_update_lock_.lock(); m_nLastUpdate = nUpdated; last_update_lock_.unlock(); string strUpdated = int2string(nUpdated); pCacheConn->set("total_user_update", strUpdated); pCacheManager->RelCacheConn(pCacheConn); } else { log("no cache connection to get total_user_updated"); } }
bool CMessageModel::resetMsgId(uint32_t nRelateId) { bool bRet = false; uint32_t nMsgId = 0; CacheManager* pCacheManager = CacheManager::getInstance(); CacheConn* pCacheConn = pCacheManager->GetCacheConn("unread"); if(pCacheConn) { string strKey = "msg_id_" + int2string(nRelateId); string strValue = "0"; string strReply = pCacheConn->set(strKey, strValue); if(strReply == strValue) { bRet = true; } pCacheManager->RelCacheConn(pCacheConn); } return bRet; }
TEST_F(TestFileCache, AutodetectNewCache) { // Make a quick new cache file on disk. CacheManager cm; ASSERT_TRUE(cm.addEmptyEntry("test_entry")); string temp_dir; ASSERT_TRUE(makeTempDir(&temp_dir)); string cache_fn(temp_dir); cache_fn.append("/cache.dump"); ASSERT_TRUE(cm.saveCache(cache_fn)); // Now make sure this file-cache is in default mode. FileCache fc; FileCache::UseNewCache = false; fc.loadMmap(cache_fn.c_str(), 1); ASSERT_TRUE(fc.exists("test_entry")); }
bool Inode::GetOnline(OnlineState & state) { unsigned long long number; if ( ! GetNumber(number) ) { state = OnlineStateOnline; return true; } // MetaManager * meta = Factory::GetMetaManager(); // if ( meta == NULL ) { // state = OnlineStateUnknown; // return true; // } // string fullname = Path().string(); // fs::path path = Factory::GetMetaFolder() / Factory::GetService(); // string parent = path.string(); // if ( fullname.find(parent) != 0 ) { // LogError(fullname << " is not a meta file pathname"); // state = OnlineStateUnknown; // return true; // } // string name = fullname.substr(parent.size()); // if ( meta->IsFileInUse(name) ) { // state = OnlineStateUnknown; // return true; // } CacheManager * cache = Factory::GetCacheManager(); if ( cache == NULL ) { state = OnlineStateOffline; return true; } if ( cache->IsFullFile(number) ) { state = OnlineStateOnline; } else { state = OnlineStateOffline; } return true; }
/// Initialize this package loader for loading from the specified cache files. /// /// @param[in] cacheName Name of the cache to use. /// /// @return True if initialization was successful, false if not. /// /// @see Shutdown() bool CachePackageLoader::Initialize( Name cacheName ) { HELIUM_ASSERT( !cacheName.IsEmpty() ); Shutdown(); // Acquire the cache. CacheManager* pCacheManager = CacheManager::GetInstance(); HELIUM_ASSERT( pCacheManager ); m_pCache = pCacheManager->GetCache( cacheName ); if( !m_pCache ) { HELIUM_TRACE( TraceLevels::Error, "CachePackageLoader::Initialize(): Failed to initialize cache \"%s\".\n", *cacheName ); return false; } return true; }
TEST_F(TestCacheManager, LocalStoreAndFetch) { CacheManager cm; string name = "/test/name"; EXPECT_FALSE(cm.entryExists(name)); EXPECT_FALSE(cm.fileExists(name)); EXPECT_FALSE(cm.dirExists(name)); const char* data; uint64_t data_len; bool compressed; EXPECT_FALSE(cm.getFileContents(name, &data, &data_len, &compressed)); string uncompressed; EXPECT_FALSE(cm.getDecompressed(name, &uncompressed)); uint64_t uncompressed_size; EXPECT_FALSE(cm.getUncompressedFileSize(name, &uncompressed_size)); // Add a real (highly compressible) file for it to read in. string test_data; for (int i = 0; i < 1024; ++i) { test_data.append("123456789012345678901234567890"); } string data_fn; ASSERT_TRUE(makeTempFile(test_data, &data_fn)); ASSERT_TRUE(cm.addFileContents(name, data_fn)); ASSERT_EQ(unlink(data_fn.c_str()), 0); EXPECT_TRUE(cm.entryExists(name)); EXPECT_TRUE(cm.fileExists(name)); EXPECT_FALSE(cm.dirExists(name)); // The "/test" of "/test/name" should have been added as a directory. EXPECT_TRUE(cm.dirExists("/test")); EXPECT_TRUE(cm.getFileContents(name, &data, &data_len, &compressed)); EXPECT_TRUE(compressed); // It's compressed, so it'll be different. ASSERT_NE(memcmp(test_data.c_str(), data, data_len), 0); ASSERT_TRUE(cm.getUncompressedFileSize(name, &uncompressed_size)); EXPECT_EQ(test_data.length(), uncompressed_size); // So let's decompress it. ASSERT_TRUE(cm.getDecompressed(name, &uncompressed)); ASSERT_EQ(test_data.length(), uncompressed.length()); ASSERT_EQ(test_data, uncompressed); }
TEST_F(TestCacheManager, BadLoadPath) { CacheManager cm; EXPECT_FALSE(cm.addFileContents("/test/name", "/invalid/path")); }
void CacheMonitorServer::ScanFolder(const fs::path & folder) { CacheManager * cache = Factory::GetCacheManager(); fs::path folderMeta = Factory::GetMetaFolder() / Factory::GetService(); try { fs::directory_iterator end; for ( fs::directory_iterator i(folder); i != end; ++ i ) { if ( fs::is_directory(i->status()) ) { ScanFolder(i->path()); } else { struct stat stat; if ( 0 != ::stat(i->path().string().c_str(),&stat) ) { continue; } ExtendedAttribute ea(i->path()); unsigned long long number; int valuesize; if ( ! ea.GetValue( Inode::ATTRIBUTE_NUMBER, &number, sizeof(number), valuesize ) ) { continue; } long state; if ( ! ea.GetValue( Inode::ATTRIBUTE_STATE, &state, sizeof(state), valuesize) ) { continue; } if ( state != Inode::StateBegin ) { continue; } long long size = 0; if ( ! ea.GetValue( Inode::ATTRIBUTE_SIZE, &size, sizeof(size), valuesize) ) { continue; } if ( size <= 0 ) { continue; } if ( ! cache->ExistFile(number) ) { continue; } vector<FileInfo> files; FileMap::iterator iter = files_.insert( FileMap::value_type(stat.st_atime,files) ).first; FileInfo file; file.service = Factory::GetService(); file.path = i->path().string().substr( folderMeta.string().size() ); iter->second.push_back(file); } } } catch ( const boost::filesystem::filesystem_error& e ) { LogError(e.what()); } catch ( const std::exception & e ) { LogError(e.what()); } }
TEST_F(TestCacheManager, SaveBadPath) { CacheManager cm; ASSERT_FALSE(cm.saveCache("/__invalid__/__path__/")); }
TEST_F(TestCacheManager, SaveAndLoad) { CacheManager cm; // Create an uncompressed file. //1234567890123456 string test_data_1 = "don't compress!!"; string data_fn_1; ASSERT_TRUE(makeTempFile(test_data_1, &data_fn_1)); string name_1 = "/test/name/not_compressible12345"; ASSERT_TRUE(cm.addFileContents(name_1, data_fn_1)); ASSERT_EQ(unlink(data_fn_1.c_str()), 0); ASSERT_TRUE(cm.dirExists("/test")); ASSERT_TRUE(cm.dirExists("/test/name")); // Create a compressed file. string test_data_2; test_data_2.assign(1048576, 0x55); string data_fn_2; ASSERT_TRUE(makeTempFile(test_data_2, &data_fn_2)); string name_2 = "/test/name/very/compressible1234"; ASSERT_TRUE(cm.addFileContents(name_2, data_fn_2)); ASSERT_EQ(unlink(data_fn_2.c_str()), 0); ASSERT_TRUE(cm.dirExists("/test")); ASSERT_TRUE(cm.dirExists("/test/name")); ASSERT_TRUE(cm.dirExists("/test/name/very")); // Create an empty entry. string name_3 = "/test/with/a/very/long/name/for/an/empty/entry"; ASSERT_TRUE(cm.addEmptyEntry(name_3)); // Write it out ... string temp_dir; ASSERT_TRUE(makeTempDir(&temp_dir)); string save_path(temp_dir); save_path.append("/cm.save"); ASSERT_TRUE(cm.saveCache(save_path)); // ... and load it back in. CacheManager cm2; ASSERT_TRUE(cm2.loadCache(save_path)); ASSERT_TRUE(cm2.fileExists(name_1)); ASSERT_TRUE(cm2.fileExists(name_2)); ASSERT_TRUE(cm2.entryExists(name_3)); EXPECT_TRUE(cm.dirExists("/test")); EXPECT_TRUE(cm.dirExists("/test/name")); // Empty entries also get their parent directories added. EXPECT_TRUE(cm.dirExists("/test/with")); const char* compare_1; uint64_t compare_len_1; bool compressed_1; ASSERT_TRUE(cm2.getFileContents(name_1, &compare_1, &compare_len_1, &compressed_1)); ASSERT_FALSE(compressed_1); ASSERT_EQ(test_data_1.length(), compare_len_1); ASSERT_EQ(memcmp(test_data_1.c_str(), compare_1, compare_len_1), 0); string compare_2; ASSERT_TRUE(cm2.getDecompressed(name_2, &compare_2)); EXPECT_EQ(compare_2, test_data_2); // Clean up the mess. ASSERT_EQ(unlink(save_path.c_str()), 0); ASSERT_EQ(rmdir(temp_dir.c_str()), 0); }
void CacheManagerTest::testFile() { CacheManager * cache = Factory::GetCacheManager(); const unsigned long long bigNumber = 1024LL * 1024 * 1024 * 1024; unsigned long long number = 0; CPPUNIT_ASSERT( false == cache->ExistFile(0) ); CPPUNIT_ASSERT( false == cache->ExistFile(1) ); CPPUNIT_ASSERT( true == cache->CreateNewFile(number) ); CPPUNIT_ASSERT( 1 == number ); CPPUNIT_ASSERT( true == cache->CreateNewFile(number) ); CPPUNIT_ASSERT( 2 == number ); Factory::ReleaseCacheManager(); Factory::CreateCacheManager(); cache = Factory::GetCacheManager(); CPPUNIT_ASSERT( false == cache->ExistFile(0) ); CPPUNIT_ASSERT( true == cache->ExistFile(1) ); CPPUNIT_ASSERT( true == cache->ExistFile(2) ); CPPUNIT_ASSERT( false == cache->ExistFile(3) ); CPPUNIT_ASSERT( true == cache->CreateFile(bigNumber) ); CPPUNIT_ASSERT( false == cache->ExistFile(bigNumber - 1) ); CPPUNIT_ASSERT( true == cache->ExistFile(bigNumber) ); CPPUNIT_ASSERT( true == cache->CreateNewFile(number) ); CPPUNIT_ASSERT( bigNumber + 1 == number ); CPPUNIT_ASSERT( true == cache->ExistFile(bigNumber + 1) ); Factory::ReleaseCacheManager(); Factory::CreateCacheManager(); cache = Factory::GetCacheManager(); CPPUNIT_ASSERT( false == cache->ExistFile(0) ); CPPUNIT_ASSERT( true == cache->ExistFile(1) ); CPPUNIT_ASSERT( true == cache->ExistFile(2) ); CPPUNIT_ASSERT( false == cache->ExistFile(3) ); CPPUNIT_ASSERT( false == cache->ExistFile(bigNumber - 1) ); CPPUNIT_ASSERT( true == cache->ExistFile(bigNumber) ); CPPUNIT_ASSERT( true == cache->ExistFile(bigNumber + 1) ); CPPUNIT_ASSERT( true == cache->CreateNewFile(number) ); CPPUNIT_ASSERT_MESSAGE( boost::lexical_cast<string>(number), bigNumber + 2 == number ); CPPUNIT_ASSERT( true == cache->ExistFile(bigNumber + 2) ); for ( unsigned long long i = 0; i < 4; ++ i ) { CPPUNIT_ASSERT( true == cache->DeleteFile(i) ); CPPUNIT_ASSERT( false == cache->ExistFile(i) ); } for ( unsigned long long i = bigNumber - 1; i < bigNumber + 3; ++ i ) { CPPUNIT_ASSERT( true == cache->DeleteFile(i) ); CPPUNIT_ASSERT( false == cache->ExistFile(i) ); } CPPUNIT_ASSERT( true == cache->CreateNewFile(number) ); CPPUNIT_ASSERT_MESSAGE( boost::lexical_cast<string>(number), bigNumber + 3 == number ); CPPUNIT_ASSERT( true == cache->ExistFile(bigNumber + 3) ); }
void getDevicesToken(CImPdu* pPdu, uint32_t conn_uuid) { IM::Server::IMGetDeviceTokenReq msg; IM::Server::IMGetDeviceTokenRsp msgResp; if(msg.ParseFromArray(pPdu->GetBodyData(), pPdu->GetBodyLength())) { CacheManager* pCacheManager = CacheManager::getInstance(); CacheConn* pCacheConn = pCacheManager->GetCacheConn("token"); CImPdu* pPduResp = new CImPdu; uint32_t nCnt = msg.user_id_size(); if (pCacheConn) { vector<string> vecTokens; for (uint32_t i=0; i<nCnt; ++i) { string strKey = "device_"+int2string(msg.user_id(i)); vecTokens.push_back(strKey); } map<string, string> mapTokens; bool bRet = pCacheConn->mget(vecTokens, mapTokens); pCacheManager->RelCacheConn(pCacheConn); if(bRet) { for (auto it=mapTokens.begin(); it!=mapTokens.end(); ++it) { string strKey = it->first; size_t nPos = strKey.find("device_"); if( nPos != string::npos) { string strUserId = strKey.substr(nPos + strlen("device_")); uint32_t nUserId = string2int(strUserId); string strValue = it->second; nPos = strValue.find(":"); if(nPos!=string::npos) { string strType = strValue.substr(0, nPos); string strToken = strValue.substr(nPos + 1); IM::BaseDefine::ClientType nClientType = IM::BaseDefine::ClientType(0); if(strType == "ios") { nClientType = IM::BaseDefine::CLIENT_TYPE_IOS; } else if(strType == "android") { nClientType = IM::BaseDefine::CLIENT_TYPE_ANDROID; } if(IM::BaseDefine::ClientType_IsValid(nClientType)) { IM::BaseDefine::UserTokenInfo* pToken = msgResp.add_user_token_info(); pToken->set_user_id(nUserId); pToken->set_token(strToken); pToken->set_user_type(nClientType); uint32_t nTotalCnt = 0; CMessageModel::getInstance()->getUnReadCntAll(nUserId, nTotalCnt); CGroupMessageModel::getInstance()->getUnReadCntAll(nUserId, nTotalCnt); pToken->set_push_count(nTotalCnt); pToken->set_push_type(1); } else { log("invalid clientType.clientType=%u", nClientType); } } else { log("invalid value. value=%s", strValue.c_str()); } } else { log("invalid key.key=%s", strKey.c_str()); } } } else { log("mget failed!"); } } else { log("no cache connection for token"); } log("req devices token.reqCnt=%u, resCnt=%u", nCnt, msgResp.user_token_info_size()); msgResp.set_attach_data(msg.attach_data()); pPduResp->SetPBMsg(&msgResp); pPduResp->SetSeqNum(pPdu->GetSeqNum()); pPduResp->SetServiceId(IM::BaseDefine::SID_OTHER); pPduResp->SetCommandId(IM::BaseDefine::CID_OTHER_GET_DEVICE_TOKEN_RSP); CProxyConn::AddResponsePdu(conn_uuid, pPduResp); } else { log("parse pb failed"); } }
void setDevicesToken(CImPdu* pPdu, uint32_t conn_uuid) { IM::Login::IMDeviceTokenReq msg; IM::Login::IMDeviceTokenRsp msgResp; if(msg.ParseFromArray(pPdu->GetBodyData(), pPdu->GetBodyLength())) { uint32_t nUserId = msg.user_id(); string strToken = msg.device_token(); CImPdu* pPduResp = new CImPdu; CacheManager* pCacheManager = CacheManager::getInstance(); CacheConn* pCacheConn = pCacheManager->GetCacheConn("token"); if (pCacheConn) { IM::BaseDefine::ClientType nClientType = msg.client_type(); string strValue; if(IM::BaseDefine::CLIENT_TYPE_IOS == nClientType) { strValue = "ios:"+strToken; } else if(IM::BaseDefine::CLIENT_TYPE_ANDROID == nClientType) { strValue = "android:"+strToken; } else { strValue = strToken; } string strOldValue = pCacheConn->get("device_"+int2string(nUserId)); if(!strOldValue.empty()) { size_t nPos = strOldValue.find(":"); if(nPos!=string::npos) { string strOldToken = strOldValue.substr(nPos + 1); string strReply = pCacheConn->get("device_"+strOldToken); if (!strReply.empty()) { string strNewValue(""); pCacheConn->set("device_"+strOldToken, strNewValue); } } } pCacheConn->set("device_"+int2string(nUserId), strValue); string strNewValue = int2string(nUserId); pCacheConn->set("device_"+strToken, strNewValue); log("setDeviceToken. userId=%u, deviceToken=%s", nUserId, strToken.c_str()); pCacheManager->RelCacheConn(pCacheConn); } else { log("no cache connection for token"); } log("setDeviceToken. userId=%u, deviceToken=%s", nUserId, strToken.c_str()); msgResp.set_attach_data(msg.attach_data()); msgResp.set_user_id(nUserId); pPduResp->SetPBMsg(&msgResp); pPduResp->SetSeqNum(pPdu->GetSeqNum()); pPduResp->SetServiceId(IM::BaseDefine::SID_LOGIN); pPduResp->SetCommandId(IM::BaseDefine::CID_LOGIN_RES_DEVICETOKEN); CProxyConn::AddResponsePdu(conn_uuid, pPduResp); } else { log("parse pb failed"); } }
bool CGroupModel::insertNewMember(uint32_t nGroupId, set<uint32_t>& setUsers) { bool bRet = false; uint32_t nUserCnt = (uint32_t)setUsers.size(); if(nGroupId != INVALID_VALUE && nUserCnt > 0) { CDBManager* pDBManager = CDBManager::getInstance(); CDBConn* pDBConn = pDBManager->GetDBConn("dffxIMDB_slave"); if (pDBConn) { uint32_t nCreated = (uint32_t)time(NULL); // 获取 已经存在群里的用户 string strClause; bool bFirst = true; for (auto it=setUsers.begin(); it!=setUsers.end(); ++it) { if(bFirst) { bFirst = false; strClause = int2string(*it); } else { strClause += ("," + int2string(*it)); } } string strSql = "select userId from IMGroupMember where groupId=" + int2string(nGroupId) + " and userId in (" + strClause + ")"; CResultSet* pResultSet = pDBConn->ExecuteQuery(strSql.c_str()); set<uint32_t> setHasUser; if(pResultSet) { while (pResultSet->Next()) { setHasUser.insert(pResultSet->GetInt("userId")); } pResultSet->Clear(); } else { log("no result for sql:%s", strSql.c_str()); } pDBManager->RelDBConn(pDBConn); pDBConn = pDBManager->GetDBConn("dffxIMDB_master"); if (pDBConn) { CacheManager* pCacheManager = CacheManager::getInstance(); CacheConn* pCacheConn = pCacheManager->GetCacheConn("group_member"); if (pCacheConn) { // 设置已经存在群中人的状态 if (!setHasUser.empty()) { strClause.clear(); bFirst = true; for (auto it=setHasUser.begin(); it!=setHasUser.end(); ++it) { if(bFirst) { bFirst = false; strClause = int2string(*it); } else { strClause += ("," + int2string(*it)); } } strSql = "update IMGroupMember set status=0, updated="+int2string(nCreated)+" where groupId=" + int2string(nGroupId) + " and userId in (" + strClause + ")"; pDBConn->ExecuteUpdate(strSql.c_str()); } strSql = "insert into IMGroupMember(`groupId`, `userId`, `status`, `created`, `updated`) values\ (?,?,?,?,?)"; //插入新成员 auto it = setUsers.begin(); uint32_t nStatus = 0; uint32_t nIncMemberCnt = 0; for (;it != setUsers.end();) { uint32_t nUserId = *it; if(setHasUser.find(nUserId) == setHasUser.end()) { CPrepareStatement* pStmt = new CPrepareStatement(); if (pStmt->Init(pDBConn->GetMysql(), strSql)) { uint32_t index = 0; pStmt->SetParam(index++, nGroupId); pStmt->SetParam(index++, nUserId); pStmt->SetParam(index++, nStatus); pStmt->SetParam(index++, nCreated); pStmt->SetParam(index++, nCreated); pStmt->ExecuteUpdate(); ++nIncMemberCnt; delete pStmt; } else { setUsers.erase(it++); delete pStmt; continue; } } ++it; } if(nIncMemberCnt != 0) { strSql = "update IMGroup set userCnt=userCnt+" + int2string(nIncMemberCnt) + " where id="+int2string(nGroupId); pDBConn->ExecuteUpdate(strSql.c_str()); } //更新一份到redis中 string strKey = "group_member_"+int2string(nGroupId); for(auto it = setUsers.begin(); it!=setUsers.end(); ++it) { pCacheConn->hset(strKey, int2string(*it), int2string(nCreated)); } pCacheManager->RelCacheConn(pCacheConn); bRet = true; } else { log("no cache connection"); } pDBManager->RelDBConn(pDBConn); } else {
TEST_F(TestCacheManager, Empty) { CacheManager cm; string name = "/test/name"; EXPECT_FALSE(cm.entryExists(name)); EXPECT_FALSE(cm.fileExists(name)); EXPECT_FALSE(cm.dirExists(name)); ASSERT_TRUE(cm.addEmptyEntry(name)); EXPECT_TRUE(cm.entryExists(name)); EXPECT_FALSE(cm.fileExists(name)); EXPECT_FALSE(cm.dirExists(name)); const char* data; uint64_t data_len; bool compressed; EXPECT_FALSE(cm.getFileContents(name, &data, &data_len, &compressed)); string uncompressed; EXPECT_FALSE(cm.getDecompressed(name, &uncompressed)); uint64_t file_size; EXPECT_FALSE(cm.getUncompressedFileSize(name, &file_size)); }