7 #include <Poco/JSON/Object.h>
8 #include <Poco/JSON/Array.h>
9 #include <Poco/JSON/JSON.h>
10 #include <Poco/JSON/Stringifier.h>
11 #include <Poco/JSON/Parser.h>
12 #include <Poco/JSON/JSONException.h>
13 #include <Poco/Dynamic/Var.h>
15 #include <Poco/String.h>
36 :
inherited(), fObj(fObj_), resultData(
""), message(fObj.getCustomMessage())
43 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
46 if (stat(path.c_str(), &st) != 0)
48 if (mkdir(path.c_str(), mode) != 0)
53 else if (!S_ISDIR(st.st_mode))
69 struct dirent* ent =
nullptr;
70 DIR* dir = opendir (path.c_str());
75 while ((ent = readdir (dir)) !=
nullptr)
77 fileName = ent->d_name;
78 if (fileName !=
"." && fileName !=
"..")
80 fileName = path+
"/"+ent->d_name;
82 if (
remove(fileName.c_str())==-1)
86 if (closedir(dir)==-1)
104 dir = opendir (path.c_str());
107 if (closedir(dir)==-1)
123 return (access(file.c_str(), F_OK)==0);
139 std::string cmd(
"cp");
140 std::vector<std::string> args = {srcFile, dstFile};
142 std::stringstream outMsg;
144 if (!procExec.
exec(outMsg, outMsg))
145 throw Poco::Exception(
"Copy '"+srcFile+
"' to '"+dstFile+
"' fail",
ERROR_COPY_FILE);
152 catch(Poco::Exception& e)
168 std::string cmd(
"mv");
169 std::vector<std::string> args = {srcFile, dstFile};
171 std::stringstream outMsg;
173 if (!procExec.
exec(outMsg, outMsg))
174 throw Poco::Exception(
"Move '"+srcFile+
"' to '"+dstFile+
"' fail",
ERROR_MOVE_FILE);
181 catch(Poco::Exception& e)
192 if (
remove(file.c_str())==0)
208 DIR* dir = opendir(srcPath.c_str());
212 std::string oldName, newName;
213 while((d = readdir(dir)))
216 if (oldName.find(indexFileName) != std::string::npos)
218 oldName = srcPath +
"/" + d->d_name;
219 newName = dstPath +
"/" + d->d_name;
227 if (closedir(dir)==-1)
229 errorMsg.append(
" ").append(strerror(errno));
242 DIR* dir = opendir(srcPath.c_str());
246 std::string oldName, newName;
247 while((d = readdir(dir)))
250 if (oldName.find(indexFileName) != std::string::npos)
252 oldName = srcPath +
"/" + d->d_name;
253 newName = dstPath +
"/" + d->d_name;
261 if (closedir(dir)==-1)
263 errorMsg.append(
" ").append(strerror(errno));
276 DIR* dir = opendir(path.c_str());
281 std::vector<std::string> files;
282 while((d = readdir(dir)))
284 fileName = d->d_name;
285 if (fileName.compare(0, indexFileName.length(), indexFileName)==0)
287 if (!(d->d_name == std::string(
".") || d->d_name == std::string(
"..")))
289 fileName = path +
"/" + d->d_name;
290 files.push_back(fileName);
295 if (closedir(dir)==-1)
297 errorMsg.append(
" ").append(strerror(errno));
301 for (
size_t i=0;i<files.size();++i)
304 if (
remove(files[i].c_str())!=0)
325 std::string findExtension =
"."+ext;
326 dir = opendir (path.c_str());
329 struct dirent* ent =
nullptr;
330 while ((ent = readdir (dir)) !=
nullptr)
332 std::string sourceFileName(ent->d_name);
333 size_t found = sourceFileName.find(findExtension);
334 if (found !=std::string::npos)
336 sourceFileName = sourceFileName.substr(0, found);
337 vFiles.push_back(sourceFileName);
340 if (closedir(dir)==-1)
362 struct dirent* ent =
nullptr;
364 dir = opendir (path.c_str());
367 std::set<std::string> sIndexes;
369 while ((ent = readdir (dir)) !=
nullptr)
371 if (!(ent->d_name == std::string(
".") || ent->d_name == std::string(
"..")))
373 fileName = ent->d_name;
374 size_t found = fileName.find_last_of(
'.');
375 if (found!=std::string::npos)
377 sIndexes.insert(fileName.substr(0, found));
381 if (closedir(dir)==-1)
385 vIndexes.assign(sIndexes.begin(), sIndexes.end());
410 std::set<unsigned int> ports;
412 struct dirent* ent =
nullptr;
418 Poco::AutoPtr<SphinxConfigCreator> pConf =
nullptr;
419 while ((ent = readdir (dir)) !=
nullptr)
421 std::string name = ent->d_name;
422 if (!(name==
"."||name==
".."))
437 catch(std::exception& exc)
447 if (closedir(dir)==-1)
452 std::set<unsigned int>::iterator iter = ports.find(i);
453 if (iter==ports.end())
462 catch(Poco::Exception& e)
486 std::vector<std::string> keys;
489 for (
size_t i=0;i<keys.size();++i)
495 pConf->setString(name, value);
499 std::string value = path+
"/"+trunkName;
500 pConf->setString(name, value);
513 pConf->save(propFile);
514 pConf->setPropertyFileConfiguration(propFile);
516 std::ofstream ofs(sphinxConfigFile.c_str(), std::fstream::trunc);
522 pConf->save(propFile);
526 catch(Poco::Exception& e)
561 Poco::SharedPtr<SphinxAdminCommand> pCmd;
563 if (commandString ==
"INDEX_CREATE")
565 else if (commandString ==
"INDEX_CHECK")
567 else if (commandString ==
"INDEX_STORE_DATA_FILE")
569 else if (commandString ==
"INDEX_STORE_SCHEMA_FILE")
571 else if (commandString ==
"INDEX_REBUILD")
575 else if (commandString ==
"INDEX_START")
577 else if (commandString ==
"INDEX_STOP")
579 else if (commandString ==
"INDEX_MERGE")
581 else if (commandString ==
"INDEX_MERGE_TRUNK")
583 else if (commandString ==
"INDEX_DELETE_DATA_FILE")
585 else if (commandString ==
"INDEX_DELETE_SCHEMA_FILE")
587 else if (commandString ==
"INDEX_APPEND_DATA_FILE")
589 else if (commandString ==
"INDEX_DELETE_DOC")
591 else if (commandString ==
"INDEX_DELETE_DOC_NUMBER")
593 else if (commandString ==
"INDEX_PACK_DOC_DATA")
595 else if (commandString ==
"INDEX_REMOVE")
597 else if (commandString ==
"INDEX_COPY")
599 else if (commandString ==
"INDEX_RENAME")
601 else if (commandString ==
"INDEX_SET_CONFIG_VAR")
603 else if (commandString ==
"INDEX_GET_CONFIG_VAR")
605 else if (commandString ==
"INDEX_CHECK_SCHEMA")
607 else if (commandString ==
"INDEX_STATUS_SEARCHD")
609 else if (commandString ==
"INDEX_STATUS")
611 else if (commandString ==
"INDEX_MAX_DOC_ID")
613 else if (commandString ==
"INDEX_DATA_LIST")
615 else if (commandString ==
"INDEX_BRANCHES_LIST")
617 else if (commandString ==
"INDEX_BRANCHES_INFO")
619 else if (commandString ==
"INDEX_BRANCHES_STATUS")
621 else if (commandString ==
"INDEX_CONNECT")
623 else if (commandString ==
"INDEX_DISCONNECT")
641 Poco::JSON::Object::Ptr obj =
new Poco::JSON::Object();
644 std::stringstream ostr;
645 Poco::JSON::Stringifier::stringify(obj, ostr);
650 catch(std::exception& e)
665 Poco::JSON::Parser parser;
666 Poco::Dynamic::Var res = parser.parse(json);
667 Poco::JSON::Object::Ptr obj = res.extract<Poco::JSON::Object::Ptr>();
675 catch(Poco::JSON::JSONException& e)
728 std::vector<std::string> keys;
731 for (
size_t i=0;i<keys.size();++i)
736 std::string value = pConf->getString(name);
737 size_t pos = value.find_last_of(
' ');
738 if (pos!=std::string::npos)
739 value = value.substr(pos+1);
741 std::stringstream str;
742 str <<
"cat " << dataDir <<
'/' << value;
743 pConf->setString(name, str.str());
749 std::string value = pConf->getString(name);
751 std::stringstream str;
752 str << indexDir <<
'/' << value;
753 pConf->setString(name, str.str());
786 pConf->save(dstFile);
787 pConf->setPropertyFileConfiguration(dstFile);
790 std::ofstream ofs(sphinxConfig.c_str(), std::fstream::trunc);
796 pConf->save(dstFile);
797 pConf->setPropertyFileConfiguration(dstFile);
802 catch(Poco::Exception& e)
851 catch(Poco::Exception& e)
874 Poco::JSON::Object::Ptr obj =
new Poco::JSON::Object();
879 std::stringstream ostr;
880 Poco::JSON::Stringifier::stringify(obj, ostr);
885 catch(std::exception& e)
902 Poco::JSON::Parser parser;
903 Poco::Dynamic::Var res = parser.parse(json);
904 Poco::JSON::Object::Ptr obj = res.extract<Poco::JSON::Object::Ptr>();
920 catch(Poco::JSON::JSONException& e)
944 if (str.str().empty())
961 std::set<std::string> delList;
962 std::string outString = xmlCleaner.
process(str.str(), delList);
966 path.append(
"/").append(
branchName).append(
".dat");
968 std::ofstream ofs(path.c_str(), std::fstream::trunc);
976 catch(Poco::Exception& e)
981 catch(std::exception& e)
1002 Poco::JSON::Object::Ptr obj =
new Poco::JSON::Object();
1006 std::stringstream ostr;
1007 Poco::JSON::Stringifier::stringify(obj, ostr);
1012 catch(std::exception& e)
1028 Poco::JSON::Parser parser;
1029 Poco::Dynamic::Var res = parser.parse(json);
1030 Poco::JSON::Object::Ptr obj = res.extract<Poco::JSON::Object::Ptr>();
1042 catch(Poco::JSON::JSONException& e)
1062 if (str.str().empty())
1068 std::vector<std::pair<std::string, int> > schemaVec;
1074 std::ofstream ofs(path.c_str(), std::fstream::trunc);
1082 catch(Poco::Exception& e)
1102 Poco::JSON::Object::Ptr obj =
new Poco::JSON::Object();
1105 Poco::JSON::Array::Ptr vecBranches =
new Poco::JSON::Array();
1108 for (
size_t i=0;i<
branches.size();++i)
1111 std::stringstream ostr;
1112 Poco::JSON::Stringifier::stringify(obj, ostr);
1117 catch(std::exception& e)
1133 Poco::JSON::Parser parser;
1134 Poco::Dynamic::Var res = parser.parse(json);
1135 Poco::JSON::Object::Ptr obj = res.extract<Poco::JSON::Object::Ptr>();
1142 if (!vecBranches.isNull())
1144 for (
size_t k = 0;k<vecBranches->size();++k)
1146 Poco::Dynamic::Var tmp = vecBranches->get(k);
1149 std::string
branchName = tmp.convert<std::string>();
1166 catch(Poco::JSON::JSONException& e)
1176 bool result =
false;
1179 std::ifstream ifsSchema(schemaFile.c_str());
1180 if (!ifsSchema.is_open())
1184 std::ifstream ifsDoc(dataFileName.c_str());
1185 if (!ifsDoc.is_open())
1194 std::fstream outputSource(outputFileName.c_str(), std::fstream::out | std::fstream::trunc);
1197 outputSource << dataSource <<
std::endl;
1200 catch(Poco::Exception& e)
1205 outputSource.close();
1209 catch(Poco::Exception& e)
1223 bool result =
false;
1235 std::vector<std::string> keys;
1239 for (
size_t i=0;i<keys.size();++i)
1244 std::stringstream str;
1245 str <<
"cat " << sourceFile;
1246 pConf->setString(name, str.str());
1251 std::stringstream str;
1253 pConf->setString(name, str.str());
1254 sectionName = keys[i];
1258 pConf->save(propertyFile);
1259 pConf->setPropertyFileConfiguration(propertyFile);
1260 pConf->renameSphinxConfigSection(sectionName,
"index "+branchName);
1262 std::ofstream ofs(configFile.c_str(), std::fstream::trunc);
1267 pConf->setPropertyFileConfiguration(propertyFile);
1268 pConf->cloneSphinxConfigSection(sectionName,
"index "+branchName);
1269 pConf->remove(sectionName);
1272 pConf->save(propertyFile);
1274 std::stringstream outMsg, errMsg;
1275 const std::string cmd(
"indexer --config " +configFile+
" --all");
1279 if (!procExec.
exec(outMsg, errMsg))
1283 throw Poco::Exception(outMsg.str());
1288 catch(Poco::Exception& e)
1315 if (!
isExistDir(path+
"/"+sphinx_admin_command_const::indexDir))
1316 throw Poco::Exception(
"Forder: '"+path+
"/"+sphinx_admin_command_const::indexDir+
"' not exists",
ERROR_INDEX_REBUILD);
1318 if (!
isExistDir(path+
"/"+sphinx_admin_command_const::dataDir))
1319 throw Poco::Exception(
"Forder: '"+path+
"/"+sphinx_admin_command_const::dataDir+
"' not exists",
ERROR_INDEX_REBUILD);
1322 std::vector<std::string> vFiles;
1325 unsigned int madeFailCount = 0;
1326 if (vFiles.size()==0)
1335 for (
size_t i=0;i<vFiles.size();++i)
1337 if (!
makeSource(schemaFileName, vFiles[i], dataDir, path))
1348 for (
size_t k=0;k<
branches.size();++k)
1349 for (
size_t i=0;i<vFiles.size();++i)
1352 if (!
makeSource(schemaFileName, vFiles[i], dataDir, path))
1362 if (madeFailCount==0)
1368 catch(Poco::Exception& e)
1420 throw Poco::Exception(
"Don't created run dir: '"+runDir+
"'",
ERROR_INDEX_START);
1426 throw Poco::Exception(
"Don't created log dir: '"+logDir+
"'",
ERROR_INDEX_START);
1432 throw Poco::Exception(
"Don't created default index log dir: '"+logDir+
"'",
ERROR_INDEX_START);
1447 fObj.
log() <<
"isRunSearchd = " << std::boolalpha << isRunSearchd <<
std::endl;
1460 catch(Poco::Exception& e)
1491 throw Poco::Exception(
"Config file '"+path+
"' not exist",
ERROR_INDEX_STOP);
1498 catch(Poco::Exception& e)
1514 bool result =
false;
1526 std::vector<std::string> keys;
1531 std::string trunkSectionName, branchSource;
1532 for (
size_t i=0;i<keys.size();++i)
1535 if (found!=std::string::npos)
1537 branchSource = keys[i];
1540 std::stringstream str;
1541 str <<
"cat " << sourceFile;
1542 pConf->setString(name, str.str());
1546 trunkSectionName = keys[i];
1549 if (trunkSectionName.empty())
1550 throw Poco::Exception(
"Section name: '"+trunkSectionName+
"' not valid");
1553 pConf->cloneSphinxConfigSection(trunkSectionName, branchSectionName);
1555 std::string branchSourceName = branchSource;
1557 pConf->cloneSphinxConfigSection(branchSourceName, trunkSourceName);
1560 std::string value = trunkSourceName;
1561 pConf->setString(name, value);
1564 value =
"echo \"\"";
1565 pConf->setString(name, value);
1569 pConf->setString(name, value);
1573 pConf->setString(name, value);
1586 pConf->save(propertyFile);
1587 pConf->setPropertyFileConfiguration(propertyFile);
1589 std::ofstream ofs(configFile.c_str(), std::fstream::trunc);
1593 std::stringstream outMsg;
1594 const std::string cmd(
"indexer");
1595 std::vector<std::string> args = {
"--merge", trunkName,
branchName,
"--config", configFile};
1597 bool ret = procExec.
exec(outMsg, outMsg);
1603 throw Poco::Exception(
"Merge '"+branchName+
"' to '"+trunkName+
"' fail");
1606 throw Poco::Exception(outMsg.str());
1610 catch(Poco::Exception& e)
1616 catch(std::exception& e)
1627 std::vector<std::string>& vIndexes,
unsigned long long& maxDocId)
throw (Poco::Exception)
1629 if (vIndexes.empty())
1633 for (
size_t i=0;i<vIndexes.size();++i)
1635 if (vIndexes[i]==trunkName)
1650 Poco::AutoPtr<SphinxConfigCreator> pConfBranch =
new SphinxConfigCreator(getPropertyFileName(configPath, vIndexes.front()));
1656 std::vector<std::string> keys;
1659 for (
size_t i=0;i<keys.size();++i)
1664 std::stringstream str;
1667 pConf->setString(name, str.str());
1672 std::stringstream str;
1674 pConf->setString(name, str.str());
1680 value = fObj.getRunDir()+
"/"+
1688 pConf->save(propertyFile);
1689 pConf->setPropertyFileConfiguration(propertyFile);
1691 std::ofstream ofs(configFile.c_str(), std::fstream::trunc);
1695 std::stringstream outMsg;
1696 const std::string cmd(
"indexer");
1697 std::vector<std::string> args = {
"--quiet", trunkName,
"--config", configFile};
1699 bool ret = procExec.
exec(outMsg, outMsg);
1701 if (!(removeFile(configFile) && removeFile(propertyFile)))
1705 throw Poco::Exception(
"Indexation '"+trunkName+
"' fail");
1707 if (isExistError(outMsg.str()))
1708 throw Poco::Exception(outMsg.str());
1710 vIndexes.erase(vIndexes.begin());
1712 catch(Poco::Exception& e)
1714 fObj.log() <<
"ERROR_MAKE_TRUNK_INDEX " << e.message() <<
std::endl;
1744 if (!fObject.isLoadedConfig())
1748 fObj.
log() <<
"removeIndexFile fObject.getIndexFileName() = " << fObject.getIndexFileName() <<
std::endl;
1753 std::vector<std::string> vIndexes;
1758 if (vIndexes.empty())
1762 unsigned long long maxDocId = 0;
1763 unsigned long long branchMaxDocId = 0;
1765 const std::string trunkFileName = *(vIndexes.begin());
1766 bool isMadeTrunk =
makeTrunk(fObject.getIndexFileName(), path, vIndexes, maxDocId);
1772 unsigned int madeFailCount = 0;
1775 for (
size_t i=0;i<vIndexes.size();++i)
1777 if (!
makeMerge(vIndexes[i], path, fObject.getIndexFileName(), branchMaxDocId))
1787 if (maxDocId < branchMaxDocId)
1788 maxDocId = branchMaxDocId;
1794 for (
size_t k=0;k<
branches.size();++k)
1795 for (
size_t i=0;i<vIndexes.size();++i)
1798 if (!
makeMerge(vIndexes[i], path, fObject.getIndexFileName(), branchMaxDocId))
1808 if (maxDocId < branchMaxDocId)
1809 maxDocId = branchMaxDocId;
1815 bool isActive = fObject.isActiveSearchd();
1816 fObject.stopSearchd();
1819 fObj.
log() <<
"IS ACTIVE SEARCHD => " << std::boolalpha << isActive <<
std::endl;
1834 if (!fObject.startSearchd())
1837 if (madeFailCount==0)
1843 catch(Poco::Exception& e)
1849 catch(std::exception& e)
1869 fObj.
log() <<
"\n***********\nIndexMergeTrunk::execute enter...." <<
std::endl;
1887 if (!fObject.isLoadedConfig())
1891 fObj.
log() <<
"fObject.getIndexFileName() = " << fObject.getIndexFileName() <<
std::endl;
1893 std::vector<std::string> vIndexes;
1897 if (vIndexes.empty())
1900 const std::string trunkFileName = *(vIndexes.begin());
1906 unsigned long long maxDocId = 0;
1907 unsigned long long branchMaxDocId = 0;
1909 unsigned int madeFailCount = 0;
1910 for (
size_t k=0;k<
branches.size();++k)
1911 for (
size_t i=0;i<vIndexes.size();++i)
1914 if (!
makeMerge(vIndexes[i], path, fObject.getIndexFileName(), branchMaxDocId))
1924 if (maxDocId < branchMaxDocId)
1925 maxDocId = branchMaxDocId;
1930 bool isActive = fObject.isActiveSearchd();
1931 fObject.stopSearchd();
1934 fObj.
log() <<
"IS ACTIVE SEARCHD => " << std::boolalpha << isActive <<
std::endl;
1947 if (!fObject.startSearchd())
1950 if (madeFailCount==0)
1953 catch(Poco::Exception& e)
1959 catch(std::exception& e)
1974 bool IndexDeleteDataFile::removeFilesList(
const std::string& path, std::vector<std::string>&
branches, std::vector<std::string>& files,
const std::string& ext)
throw (Poco::Exception)
1976 bool result =
false;
1977 unsigned int madeFailCount = 0;
1980 for (
size_t i=0;i<files.size();++i)
1982 std::string
fileName = path+
"/"+files[i]+
"."+ext;
1983 if (!removeFile(fileName))
1985 if (!resultData.empty())
1986 resultData.append(
",");
1987 resultData.append(fileName);
1994 for (
size_t k=0;k<
branches.size();++k)
1995 for (
size_t i=0;i<files.size();++i)
1998 std::string fileName = path+
"/"+files[i]+
"."+ext;
1999 if (!removeFile(fileName))
2001 if (!resultData.empty())
2002 resultData.append(
",");
2003 resultData.append(fileName);
2008 if (madeFailCount==0)
2014 bool IndexDeleteDataFile::removeIndexFiles(
const std::string& path, std::vector<std::string>&
branches, std::vector<std::string>& files)
throw (Poco::Exception)
2016 bool result =
false;
2017 unsigned int madeFailCount = 0;
2021 for (
size_t i=0;i<files.size();++i)
2025 if (!resultData.empty())
2026 resultData.append(
",");
2027 resultData.append(files[i]);
2034 for (
size_t k=0;k<
branches.size();++k)
2035 for (
size_t i=0;i<files.size();++i)
2040 if (!resultData.empty())
2041 resultData.append(
",");
2042 resultData.append(files[i]);
2047 if (madeFailCount==0)
2075 std::vector<std::string> dataFiles, sourceFiles, configFiles, propertyFiles, indexes;
2082 if (dataFiles.size()==0 && sourceFiles.size()==0 && indexes.size()==0 && configFiles.size()==0 && propertyFiles.size()==0)
2092 removeIndexFiles(indexDir,
branches, indexes));
2098 catch(Poco::Exception& e)
2129 catch(Poco::Exception& e)
2159 std::stringstream oldContent;
2161 std::ifstream ifs(dataFileName.c_str(), std::fstream::binary);
2164 oldContent << ifs.rdbuf();
2172 if (!dataFile.
append(strAppendContent))
2175 std::fstream fst(dataFileName.c_str(), std::fstream::out|std::fstream::trunc|std::fstream::binary);
2182 catch(Poco::Exception& e)
2206 Poco::JSON::Object::Ptr obj =
new Poco::JSON::Object();
2209 Poco::JSON::Array::Ptr array =
new Poco::JSON::Array();
2210 for (
size_t i=0;i<ids.size();++i)
2211 array->add(std::to_string(ids[i]));
2214 std::stringstream ostr;
2215 Poco::JSON::Stringifier::stringify(obj, ostr);
2220 catch(std::exception& e)
2236 Poco::JSON::Parser parser;
2237 Poco::Dynamic::Var res = parser.parse(json);
2238 Poco::JSON::Object::Ptr obj = res.extract<Poco::JSON::Object::Ptr>();
2245 if (!vecDocs.isNull())
2247 for (
size_t k = 0;k<vecDocs->size();++k)
2249 Poco::Dynamic::Var tmp = vecDocs->get(k);
2254 unsigned long long id = std::stoull(tmp.convert<std::string>());
2257 catch(std::exception& e)
2262 if (tmp.isNumeric())
2264 unsigned long long id = tmp.convert<Poco::Int64>();
2271 catch(Poco::JSON::JSONException& e)
2296 std::fstream fst(fileName.c_str(), std::fstream::out|std::fstream::app);
2300 for (
size_t i=0;i<ids.size();++i)
2301 fst << (
unsigned long long)(ids[i]) <<
std::endl;
2306 std::ofstream ofs(fileName.c_str(), std::fstream::trunc);
2307 for (
size_t i=0;i<ids.size();++i)
2313 catch(Poco::Exception& e)
2342 std::ifstream ifs(fileName.c_str());
2347 while(getline(ifs, line))
2356 catch(Poco::Exception& e)
2361 catch(std::exception& e)
2375 bool IndexPackDocData::packData(
const std::string& file, std::set<std::string>& delList)
2377 bool result =
false;
2381 std::string xmlString;
2382 std::string outString;
2385 std::ifstream in(file.c_str());
2387 throw Poco::Exception(
"File "+file+
" not opened");
2389 in.seekg(0, std::ios::end);
2390 xmlString.resize(in.tellg());
2391 in.seekg(0, std::ios::beg);
2392 in.read(&xmlString[0], xmlString.size());
2395 outString = xmlCleaner.process(xmlString, delList);
2397 std::ofstream ofs(file.c_str(), std::fstream::trunc);
2403 catch(Poco::Exception& e)
2405 errorMsg =
"packData: "+e.displayText();
2410 bool IndexPackDocData::getDocumentCount(
const std::string& file,
size_t& documentCount)
2412 bool result =
false;
2415 std::ifstream ifs(file.c_str());
2417 throw Poco::Exception(
"File '"+file+
"' not opened");
2419 SphinxDataFile dataFile;
2420 size_t count = dataFile.documentCount(ifs);
2423 if (dataFile.isError())
2424 throw Poco::Exception(dataFile.getErrorMsg());
2427 documentCount += count;
2429 catch(Poco::Exception& e)
2431 errorMsg =
"getDocumentCount: "+e.message();
2459 std::set<std::string> delList;
2463 std::ifstream ifs(fileName.c_str());
2468 while(getline(ifs, line))
2473 delList.insert(line);
2475 catch(std::exception& e)
2482 if (!
moveFile(fileName, backupFile))
2486 std::vector<std::string> vFiles;
2490 size_t documentCount = 0;
2492 if (vFiles.size()==0)
2498 for (
size_t i=0;i<vFiles.size();++i)
2509 for (
size_t k=0;k<
branches.size();++k)
2510 for (
size_t i=0;i<vFiles.size();++i)
2527 catch(Poco::Exception& e)
2532 catch(std::exception& e)
2561 if (!indexStop.execute())
2564 std::string cmd(
"rm");
2565 std::vector<std::string> args = {
"-R", path};
2567 std::stringstream outMsg;
2569 if (!procExec.
exec(outMsg, outMsg))
2579 args.push_back(
"-R");
2580 args.push_back(path);
2586 catch(Poco::Exception& e)
2606 Poco::JSON::Object::Ptr obj =
new Poco::JSON::Object();
2610 std::stringstream ostr;
2611 Poco::JSON::Stringifier::stringify(obj, ostr);
2616 catch(std::exception& e)
2632 Poco::JSON::Parser parser;
2633 Poco::Dynamic::Var res = parser.parse(json);
2634 Poco::JSON::Object::Ptr obj = res.extract<Poco::JSON::Object::Ptr>();
2646 catch(Poco::JSON::JSONException& e)
2670 throw Poco::Exception(
"Don't created log dir: '"+logDir+
"'",
ERROR_INDEX_COPY);
2672 const std::string cmd(
"cp");
2673 std::vector<std::string> args = {
"-R", srcIndex, dstIndex};
2675 if (!procExec.
exec())
2676 throw Poco::Exception(
"Copy '"+srcIndex+
"' to '"+dstIndex+
"' fail",
ERROR_INDEX_COPY);
2682 std::vector<std::string> keys;
2685 for (
size_t i=0;i<keys.size();++i)
2690 std::string value = pConf->getString(name);
2693 if (pos!=std::string::npos)
2696 pConf->setString(name, value);
2702 std::string value = pConf->getString(name);
2705 if (pos!=std::string::npos)
2708 pConf->setString(name, value);
2722 unsigned int port = 0;
2729 pConf->save(propFile);
2730 pConf->setPropertyFileConfiguration(propFile);
2733 std::ofstream ofs(sphinxConfigFile.c_str(), std::fstream::trunc);
2739 catch(Poco::Exception& e)
2744 catch(std::exception& e)
2780 catch(Poco::Exception& e)
2800 Poco::JSON::Object::Ptr obj =
new Poco::JSON::Object();
2807 std::stringstream ostr;
2808 Poco::JSON::Stringifier::stringify(obj, ostr);
2813 catch(std::exception& e)
2831 Poco::JSON::Parser parser;
2832 Poco::Dynamic::Var res = parser.parse(json);
2833 Poco::JSON::Object::Ptr obj = res.extract<Poco::JSON::Object::Ptr>();
2857 catch(Poco::JSON::JSONException& e)
2874 throw Poco::Exception(
"Forder: '"+path+
"' not exists");
2883 pConf->save(propFile);
2884 pConf->setPropertyFileConfiguration(propFile);
2887 std::ofstream ofs(configFile.c_str(), std::fstream::trunc);
2893 catch(Poco::Exception& e)
2913 Poco::JSON::Object::Ptr obj =
new Poco::JSON::Object();
2918 std::stringstream ostr;
2919 Poco::JSON::Stringifier::stringify(obj, ostr);
2924 catch(std::exception& e)
2941 Poco::JSON::Parser parser;
2942 Poco::Dynamic::Var res = parser.parse(json);
2943 Poco::JSON::Object::Ptr obj = res.extract<Poco::JSON::Object::Ptr>();
2959 catch(Poco::JSON::JSONException& e)
2976 throw Poco::Exception(
"Forder: '"+path+
"' not exists");
2987 catch(Poco::Exception& e)
3013 std::ifstream ifs(path.c_str(), std::fstream::binary);
3018 std::vector<std::pair<std::string, int> > oldSchemaVec;
3019 bool ret = oldSchemaFile.
getFields(oldSchemaVec);
3024 std::stringstream str;
3027 std::vector<std::pair<std::string, int> > checkSchemaVec;
3029 if (!checkSchemaFile.
getFields(checkSchemaVec))
3032 for (
size_t i=0;i<checkSchemaVec.size();++i)
3034 bool isFound =
false;
3035 for (
size_t k=0;k<oldSchemaVec.size();++k)
3037 if (checkSchemaVec[i].first==oldSchemaVec[k].first)
3049 catch(Poco::Exception& e)
3079 std::stringstream outMsg, errMsg;
3080 const std::string cmd(
"searchd");
3081 std::vector<std::string> args = {
"--status",
"--config", configFile};
3083 if (!procExec.
exec(outMsg, errMsg))
3093 catch(Poco::Exception& e)
3103 bool result =
false;
3112 catch(Poco::Exception& e)
3142 std::stringstream outMsg;
3143 const std::string cmd(
"indextool");
3144 std::vector<std::string> args = {
"--dumpheader",
fObj.
getIndexFileName(),
"--config", configFile};
3147 if (procExec.isError())
3157 catch(Poco::Exception& e)
3179 unsigned long long maxDocId = 0;
3191 std::vector<std::string> vFiles;
3197 for (
size_t i=0;i<vFiles.size();++i)
3200 ifs.open(fileName.c_str());
3203 if (dataSourceParameters.
load(ifs))
3205 if (maxDocId < dataSourceParameters.
getMaxDocId())
3219 catch(Poco::Exception& e)
3224 catch(std::exception& e)
3255 if (!
isExistDir(path+
"/"+sphinx_admin_command_const::dataDir))
3256 throw Poco::Exception(
"Forder: '"+path+
"/"+sphinx_admin_command_const::dataDir+
"' not exists",
ERROR_INDEX_DATA_LIST);
3258 std::vector<std::string> vFiles;
3261 for (
size_t i=0;i<vFiles.size();++i)
3270 catch(Poco::Exception& e)
3301 if (!
isExistDir(path+
"/"+sphinx_admin_command_const::indexDir))
3302 throw Poco::Exception(
"Forder: '"+path+
"/"+sphinx_admin_command_const::indexDir+
"' not exists",
ERROR_INDEX_BRANCHES_LIST);
3304 std::vector<std::string> vIndexes;
3307 for (
size_t i=0;i<vIndexes.size();++i)
3316 catch(Poco::Exception& e)
3332 bool result =
false;
3333 branchFieldsList.clear();
3340 ifs.open(branchFileName.c_str());
3342 throw Poco::Exception(
"Open "+branchFileName+
" fail");
3344 bool loaded = dataSourceParameters.
load(ifs);
3347 throw Poco::Exception(
"Load "+branchFileName+
" fail. "+dataSourceParameters.
getErrorMsg());
3349 std::string fieldsList;
3351 for (SphinxDataSourceParameters::PARAM_ATTR__MAP::iterator iter=paramAttr.begin();iter!=paramAttr.end();++iter)
3353 if (!fieldsList.empty())
3354 fieldsList.append(
",");
3355 fieldsList.append((*iter).first).append(
"=").append(std::to_string((*iter).second));
3357 Poco::URI::encode(fieldsList,
"", branchFieldsList);
3360 catch(Poco::Exception& e)
3391 std::vector<std::string> vFiles;
3394 unsigned int madeFailCount = 0;
3397 for (
size_t i=0;i<vFiles.size();++i)
3399 std::string branchFieldsList;
3404 resultData.append(vFiles[i]).append(
"=").append(branchFieldsList);
3412 for (
size_t k=0;k<
branches.size();++k)
3413 for (
size_t i=0;i<vFiles.size();++i)
3416 std::string branchFieldsList;
3421 resultData.append(vFiles[i]).append(
"=").append(branchFieldsList);
3428 if (madeFailCount==0)
3435 catch(Poco::Exception& e)
3451 bool result =
false;
3458 std::vector<std::string> keys;
3461 std::string trunkSectionName;
3462 for (
size_t i=0;i<keys.size();++i)
3468 pConf->setString(name, value);
3471 trunkSectionName = keys[i];
3475 pConf->setString(name, value);
3478 pConf->cloneSphinxConfigSection(trunkSectionName, branchSectionName);
3482 pConf->setString(name, value);
3495 pConf->save(propFile);
3496 pConf->setPropertyFileConfiguration(propFile);
3499 std::ofstream ofs(configFile.c_str(), std::fstream::trunc);
3504 catch(Poco::Exception& e)
3516 bool result =
false;
3517 information.clear();
3521 std::string branchConfigFileName;
3526 throw Poco::Exception(
"Config file '"+branchConfigFileName+
"' not exist",
ERROR_INDEX_STATUS);
3528 std::stringstream outMsg, errMsg;
3529 const std::string cmd(
"indextool");
3530 std::vector<std::string> args = {
"--dumpheader", indexFileName,
"--config", branchConfigFileName};
3532 if (!procExec.
exec(outMsg, errMsg))
3533 throw Poco::Exception(
"Get status '"+indexFileName+
"' fail\n"+errMsg.str(),
ERROR_INDEX_STATUS);
3538 Poco::URI::encode(outMsg.str(),
"", information);
3542 catch(Poco::Exception& e)
3574 std::vector<std::string> vIndexes;
3577 unsigned int madeFailCount = 0;
3580 for (
size_t i=0;i<vIndexes.size();++i)
3582 std::string information;
3587 resultData.append(vIndexes[i]).append(
"=").append(information);
3595 for (
size_t k=0;k<
branches.size();++k)
3596 for (
size_t i=0;i<vIndexes.size();++i)
3599 std::string information;
3604 resultData.append(vIndexes[i]).append(
"=").append(information);
3611 if (madeFailCount==0)
3618 catch(Poco::Exception& e)
3666 catch(Poco::Exception& e)
3686 Poco::JSON::Object::Ptr obj =
new Poco::JSON::Object();
3688 std::stringstream ostr;
3689 Poco::JSON::Stringifier::stringify(obj, ostr);
3694 catch(std::exception& e)
3708 Poco::JSON::Parser parser;
3709 Poco::Dynamic::Var res = parser.parse(json);
3710 Poco::JSON::Object::Ptr obj = res.extract<Poco::JSON::Object::Ptr>();
3714 catch(Poco::JSON::JSONException& e)
3736 catch(Poco::Exception& e)