Pothos::BlockRegistry::BlockRegistry(const std::string &path, const Callable &factory) { //check the path if (path.empty() or path.front() != '/') { poco_error_f1(Poco::Logger::get("Pothos.BlockRegistry"), "Invalid path: %s", path); return; } //parse the path PluginPath fullPath; try { fullPath = PluginPath("/blocks").join(path.substr(1)); } catch (const PluginPathError &) { poco_error_f1(Poco::Logger::get("Pothos.BlockRegistry"), "Invalid path: %s", path); return; } //check the factory if (factory.type(-1) == typeid(Block*) or factory.type(-1) == typeid(Topology*)) { //register PluginRegistry::add(fullPath, factory); } //otherwise report the error else { poco_error_f1(Poco::Logger::get("Pothos.BlockRegistry"), "Bad Factory, must return Block* or Topology*: %s", factory.toString()); } }
/** * Tang dislikeCounts * @param item * @return bool */ bool ItemDB::increaseDislikeCountItem(string itemID) { if (grassDB.check(itemID) == -1) { poco_error_f1(*logger, "increaseDislikeCountItem: Don't exits item %s in grassDB", itemID); return false; } Item item = getItemFromItemID(itemID); item.dislikeCounts++; string content = convertItemToJson(item); addQueue(UPDATE, itemID, content); if (grassDB.replace(itemID, content) == false) { poco_error_f1(*logger, "increaseViewCountItem: Replace error in GrassDB %s", grassDB.error().name()); cout << "error setting"; } return true; }
Tag TagDB::convertJsonToTag(string jsonString) { Tag itemReturn; Json::Value root; Json::Reader reader; bool parsedSuccess = reader.parse(jsonString, root, false); if (not parsedSuccess) { // Report failures and their locations in the document. cout << "Failed to parse JSON" << endl << reader.getFormatedErrorMessages() << endl; poco_error_f1(*logger, "convertJsonToTag: Failed to parse JSON %s", reader.getFormatedErrorMessages()); return itemReturn; } //Json::Value tagID = root["tagID"]; Json::Value tagName = root["tagName"]; Json::Value viewCounts = root["viewCounts"]; Json::Value dateAdd = root["dateAdd"]; Json::Value dateUpdate = root["dateUpdate"]; //itemReturn.tagID = tagID.asString(); itemReturn.tagName = tagName.asString(); itemReturn.viewCounts = viewCounts.asInt(); itemReturn.dateAdd = dateAdd.asString(); itemReturn.dateUpdate = dateUpdate.asString(); //poco_information(*logger, "convertJsonToTag: Convert from Json to Tag successfull"); return itemReturn; }
vector<Tag> TagDB::getTopTags(int number) { vector<Tag> topTag; if (number == 0 || number<-1) { poco_error_f1(*logger, "getTopTags: Number %d is available", number); return topTag; } vector<Tag> listTag = getAllTag(); int n = listTag.size(); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (listTag.at(i).viewCounts < listTag.at(j).viewCounts) { Tag temp = listTag.at(i); listTag.at(i) = listTag.at(j); listTag.at(j) = temp; } } } if (number > n) { poco_warning_f2(*logger, "getTopTags: number= %d > listTag.size()= %d", number, n); number = n; } for (int i = 0; i < number; i++) { topTag.push_back(listTag.at(i)); } return topTag; }
void GraphEditorTabs::loadState(void) { MainSplash::global()->postMessage(tr("Restoring graph editor...")); //load option topologies from file list auto settings = MainSettings::global(); auto files = settings->value("GraphEditorTabs/files").toStringList(); for (int i = 0; i < files.size(); i++) { if (files.at(i).isEmpty()) continue; //skip empty files if (not QFile::exists(files.at(i))) { poco_error_f1(Poco::Logger::get("PothosGui.GraphEditorTabs.loadState"), "File %s does not exist", files.at(i).toStdString()); continue; } auto editor = new GraphEditor(this); editor->setCurrentFilePath(files.at(i)); this->addTab(editor, ""); editor->load(); } //Nothing? make sure we have at least one editor this->ensureOneEditor(); //restore the active index setting this->setCurrentIndex(settings->value("GraphEditorTabs/activeIndex").toInt()); }
/** * Lấy Item có item.itemID=itemID. * @param itemID * @return "Item" tra ve Item co Item.itemID=-1 neu khong lay duoc. */ Item ItemDB::getItemFromItemID(string itemID) { // Không tăng viewcount, để cho front-end tăng viewcount. //increaseViewCountItem(itemID); Item item; item.itemID = "-1"; if (grassDB.check(itemID) == -1) { poco_error_f1(*logger, "getItemFromItemID: Don't exits itemID = %s in GrassDB", itemID); return item; } item = getItemInGrassDB(itemID); if (item.itemID == "-1") { poco_error_f1(*logger, "getItemFromItemID: Can't get itemID = %s in GrassDB", item.itemID); return item; } return item; }
void BlockTreeWidget::populate(void) { for (const auto &blockDescObj : *_blockDescs) { try { const auto blockDesc = blockDescObj.extract<Poco::JSON::Object::Ptr>(); if (not this->blockDescMatchesFilter(blockDesc)) continue; const auto path = blockDesc->get("path").extract<std::string>(); const auto name = blockDesc->get("name").extract<std::string>(); if (blockDesc->isArray("categories")) for (auto categoryObj : *blockDesc->getArray("categories")) { const auto category = categoryObj.extract<std::string>().substr(1); const auto key = category.substr(0, category.find("/")); if (_rootNodes.find(key) == _rootNodes.end()) _rootNodes[key] = new BlockTreeWidgetItem(this, key); _rootNodes[key]->load(blockDesc, category + "/" + name); } } catch (const Poco::Exception &ex) { poco_error_f1(Poco::Logger::get("PothosGui.BlockTree"), "Failed JSON Doc parse %s", ex.displayText()); } } //sort the columns alphabetically this->sortByColumn(0, Qt::AscendingOrder); emit this->blockDescEvent(Poco::JSON::Object::Ptr(), false); //unselect }
vector<Item> ItemDB::getListTopItem(int64_t number, string tagID, ItemTagDB& itemTagDB) { vector<Item> result; if (number < 1) { poco_error_f1(*logger, "getListTopItem: number = %d < 0.", number); return result; } return getItemsFromListItemID(getListTopItemID(number, tagID, itemTagDB)); }
/** * Insert Item to GrassDB. * @param item * @return "string" return itemID neu thanh cong, return -1 neu that bai. */ string ItemDB::insertItemToGrassDB(Item item) { string jsonItem = convertItemToJson(item); if (grassDB.set(item.itemID, jsonItem)) { return item.itemID; } else { poco_error_f1(*logger, "insertItemToGrassDB: Error %s.", grassDB.error().name()); return "-1"; } }
bool TagDB::deleteAllTag(ItemTagDB& itemTagDB) { try { grassDB.clear(); itemTagDB.deleteAllItemTag(); return true; } catch (char* str) { //cout << "error deleteAllTag" << endl; poco_error_f1(*logger, "deleteAllTag: Error deleteAllTag %s", str); return false; } }
/** * Xoa tat ca cac item co itemID thuoc list itemIDs. * @param itemIDs * @return bool */ bool ItemDB::deleteAllItem(vector<string> itemIDs, ItemTagDB& itemTagDB) { int size = itemIDs.size(); bool result = true; for (int i = 0; i < size; i++) { if (!deleteItem(itemIDs[i], itemTagDB)) { poco_error_f1(*logger, "deleteAllItem: Error to delete ItemID = %s", itemIDs[i]); result = false; } } return result; }
/** * Xoa 1 item. * @param itemID * @return bool */ bool ItemDB::deleteItem(string itemID, ItemTagDB& itemTagDB) { if (grassDB.check(itemID) == -1) { poco_error_f1(*logger, "deleteItem: Don't exits ItemID %s", itemID); return false; } Item item = getItemFromItemID(itemID); if (!grassDB.remove(itemID)) { poco_error_f1(*logger, "deleteItem: Error in GrassDB %s", grassDB.error().name()); return false; } vector<string>::iterator it; it = std::find(lTopItemID.begin(), lTopItemID.end(), itemID); if (it != lTopItemID.end()) { lTopItemID.erase(it); } int size = item.tagsID.size(); for (int i = 0; i < size; i++) itemTagDB.deleteItemIDinTag(item.tagsID[i], itemID); addQueue(DELETE, item.itemID, ""); return true; }
bool TagDB::insertTag(Tag& tag, ItemTagDB& itemTagDB) { try { string jsonString = convertTagToJson(tag); string ckey = tag.tagID; if (grassDB.check(ckey) != -1) { //cout << "Data existed!" << endl; poco_error_f1(*logger, "insertTag: Data existed key=%s", tag.tagID); return false; } grassDB.set(ckey, jsonString); addQueue(ADD, ckey, jsonString); //DBUtils::setLastID(grassDB, ckey); LASTID = ckey; vector<string> lItemID; itemTagDB.insertItemTag(tag.tagID, lItemID); return true; } catch (char *str) { //cout << str << endl; poco_error_f1(*logger, "insertTag: Error insertTag: %s", str); return false; } }
bool TagDB::deleteAllTag(vector<string> tagIDs, ItemTagDB& itemTagDB) { try { int n = tagIDs.size(); for (int i = 0; i < n; i++) { deleteTag(tagIDs[i], itemTagDB); //itemTagDB.deleteItemTag(tagIDs[i]); } return true; } catch (char* str) { //cout << "error deleteAllTag" << endl; poco_error_f1(*logger, "deleteAllTag: Error deleteAllTag %s", str); return false; } }
/** * Lay number Item thuoc tagID, neu numberItems = -1 thi lay tat ca * @param tagID * @return vector<Item> */ vector<Item> ItemDB::getAllItemshaveTag(string tagID, int64_t numberItems, ItemTagDB& itemTagDB) { vector<Item> listItem; if (numberItems == 0 || numberItems<-1) { poco_error_f1(*logger, "getAllItemshaveTag: number = %ld.", numberItems); return listItem; } if (!itemTagDB.checkTagExits(tagID)) { poco_error_f1(*logger, "getAllItemshaveTag: tagID = %s is not exits.", tagID); return listItem; } vector<string> liststring = itemTagDB.getAllItemsIdHaveTag(tagID); int64_t n = liststring.size(); if (numberItems == -1 || numberItems > n) numberItems = n; for (int i = 0; i < numberItems; i++) { int64_t index = Utils::getRandomNumber(numberItems); Item item = getItemInGrassDB(liststring[index]); listItem.push_back(item); } return listItem; }
bool TagDB::setViewCountTag(string tagID, int viewCounts) { if (grassDB.check(tagID) == -1) { return false; } Tag tag = getTag(tagID); tag.viewCounts = viewCounts; string jsonString = convertTagToJson(tag); try { grassDB.replace(tagID, jsonString); addQueue(UPDATE, tagID, jsonString); return true; } catch (char* str) { poco_error_f1(*logger, "setViewCountTag: Error editTag %s", str); return false; } }
Tag TagDB::getTag(string tagID) { Tag tag; string value; if (grassDB.get(tagID, &value)) { } else { cerr << "getTag: Get error: " << grassDB.error().name() << endl; poco_error_f1(*logger, "getTag: Can't open tagID %s in GrassDB", tagID); tag.tagID = "-1"; cerr << "getTag: Error on Item : " << tag.tagID << endl; return tag; } tag = convertJsonToTag(value); tag.tagID = tagID; //poco_information_f1(*logger, "getTag: getTag In GrassDB: Get TagID = %s successful", tag.tagID); return tag; }
/** * Ham get Item co key=itemID trong GrassDB. * @param grassDB * @param itemID * @return "Item" Neu khong get duoc thi tra ve item.itemID=-1. */ Item ItemDB::getItemInGrassDB(string itemID) { //int n_record = grassDB.count(); string value; Item itemReturn; try { grassDB.get(itemID, &value); itemReturn = convertJsonToItem(value); itemReturn.itemID = itemID; return itemReturn; } catch (...) { //cerr << "Get error: " << grassDB.error().name() << endl; poco_error_f1(*logger, "getItemInGrassDB: Can't open ItemID %s in GrassDB", itemID); itemReturn.itemID = "-1"; //cerr << "Error on Item : " << itemReturn.itemID << endl; return itemReturn; } }
vector<Item> ItemDB::getListTopItem(int64_t number) { vector<Item> result; if (number < 1) { poco_error_f1(*logger, "getListTopItem: number = %d < 0.", number); return result; } if (lTopItemID.empty()) { poco_error(*logger, "getListTopItem: lTopItemID is empty."); return result; } int64_t sizeLTop = lTopItemID.size(); if (number > sizeLTop) { poco_warning_f2(*logger, "getListTopItem: number = %d > lTopItemID.size() = %d", number, sizeLTop); number = sizeLTop; } for (int i = 0; i < number; i++) result.push_back(getItemFromItemID(lTopItemID[i])); return result; }
/** * Them Item co value=content va thuoc nhung tag co tagID nam trong tagIDs. * @param content * @param tagIDs * @return "string" Tra ve itemID neu thanh cong, tra ve -1 neu that bai, * tra ve -2 neu content rong, tra ve -3 tagsID rong, */ string ItemDB::insertItem(string content, vector<string> tagsID, ItemTagDB& itemTagDB) { if (content.empty()) { poco_error(*logger, "insertItem: Content is empty."); return "-2"; } if (tagsID.empty()) { poco_error(*logger, "insertItem: List TagsID is Empty."); return "-3"; } string temp; int lastID = Utils::convertStringToInt(LASTID); lastID++; Item item; item.itemID = Utils::convertIntToString(lastID); item.content = content; int sizeTagsID = tagsID.size(); for (int i = 0; i < sizeTagsID; i++) { temp = tagsID[i]; item.tagsID.push_back(temp); } item.viewCounts = 0; item.likeCounts = 0; item.dislikeCounts = 0; item.dateAdd = Utils::getTimeNow(); item.dateUpdate = "0"; //string result = insertItemToGrassDB(item); string jsonStr = convertItemToJson(item); try { grassDB.set(item.itemID, jsonStr); //DBUtils::setLastID(grassDB, item.itemID); sizeTagsID = item.tagsID.size(); for (int i = 0; i < sizeTagsID; i++) itemTagDB.insertItemIDToTag(item.tagsID[i], item.itemID); addQueue(ADD, item.itemID, jsonStr); LASTID = item.itemID; return item.itemID; } catch (...) { cout << "insertItem: Error to add itemID=" << item.itemID << endl; poco_error_f1(*logger, "insertItem: Error to add itemID=%s", item.itemID); return "-1"; } }
/** * Ham convert mot chuoi string duoc ma hoa bang Json sang Item. ( Khong tra ve itemID cho item). * @param jsonString * @return Item */ Item ItemDB::convertJsonToItem(string jsonString) { Item itemReturn; Value root; Reader reader; bool parsedSuccess = reader.parse(jsonString, root, false); if (not parsedSuccess) { // Report failures and their locations // in the document. cout << "Failed to parse JSON" << endl << reader.getFormatedErrorMessages() << endl; poco_error_f1(*logger, "convertJsonToItem: Failed to parse JSON %s", reader.getFormatedErrorMessages()); return itemReturn; } Value content = root["content"]; Value tagsID = root["tagsID"]; Value viewCounts = root["viewCounts"]; Value likeCounts = root["likeCounts"]; Value dislikeCounts = root["dislikeCounts"]; Value dateAdd = root["dateAdd"]; Value dateUpdate = root["dateUpdate"]; //cout << "Tags:" << tags << endl; //itemReturn.itemID = itemID.asString(); itemReturn.content = content.asString(); int n = tagsID.size(); for (int i = 0; i < n; i++) { string str = tagsID[i].asString(); itemReturn.tagsID.push_back(str); } itemReturn.viewCounts = viewCounts.asInt(); itemReturn.likeCounts = likeCounts.asInt(); itemReturn.dislikeCounts = dislikeCounts.asInt(); itemReturn.dateAdd = dateAdd.asString(); itemReturn.dateUpdate = dateUpdate.asString(); return itemReturn; }
/** * Get number item co tagID va keyword * @param keyword * @param tagID * @param itemTagDB * @param numberItems * @return vector<Item> */ vector<Item> ItemDB::getItemKeyword(string keyword, string tagID, ItemTagDB& itemTagDB, int32_t numberItems) { vector<Item> result; if (keyword.empty()) { poco_error(*logger, "getItemKeyword: keyword is empty."); return result; } if (numberItems < 1) { poco_error_f1(*logger, "getItemKeyword: numberItem = %d < 1", numberItems); return result; } int32_t index = 0; vector<string> lItemID = itemTagDB.getAllItemsIdHaveTag(tagID); int64_t n = lItemID.size(); for (int i = 0; i < n; i++) { Item item = getItemFromItemID(lItemID[i]); if (Utils::findStringInString(item.content, keyword)) result.push_back(item); index++; if (index == numberItems) return result; } return result; }
/** * Edit a Item. * @param itemID * @param newItemValue * @param newTagIDs * @return bool */ bool ItemDB::editItem(string itemID, string newItemValue, vector<string> newTagIDs, ItemTagDB& itemTagDB) { if (newItemValue.empty()) { poco_error(*logger, "editItem: newItemValue is empty"); return false; } if (newTagIDs.empty()) { poco_error(*logger, "editItem: listTag is empty"); return false; } if (grassDB.check(itemID) == -1) { poco_error_f1(*logger, "editItem: Don't exits Item has itemID= %s in DB", itemID); return false; } Item item = getItemInGrassDB(itemID); //FastMutex::ScopedLock lock(mutex); item.content = newItemValue; // Xóa trong ItemTagDB for (int i = 0; i < item.tagsID.size(); i++) itemTagDB.deleteItemIDinTag(item.tagsID[i], itemID); item.tagsID.clear(); string temp; int size = newTagIDs.size(); for (int i = 0; i < size; i++) { temp = newTagIDs[i]; item.tagsID.push_back(temp); //Edit trong ItemTagDB. itemTagDB.insertItemIDToTag(temp, item.itemID); } item.dateUpdate = Utils::getTimeNow(); string json = convertItemToJson(item); if (!grassDB.replace(item.itemID, json)) { poco_error(*logger, "editItem: Error to update in GrassDB"); return false; } addQueue(UPDATE, item.itemID, json); return true; }
Template::Ptr TemplateCache::getTemplate(const Path& path) { if ( _logger ) { poco_trace_f1(*_logger, "Trying to load %s", path.toString()); } Path templatePath = resolvePath(path); std::string templatePathname = templatePath.toString(); if ( _logger ) { poco_trace_f1(*_logger, "Path resolved to %s", templatePathname); } File templateFile(templatePathname); Template::Ptr tpl; std::map<std::string, Template::Ptr>::iterator it = _cache.find(templatePathname); if ( it == _cache.end() ) { if ( templateFile.exists() ) { if ( _logger ) { poco_information_f1(*_logger, "Loading template %s", templatePath.toString()); } tpl = new Template(templatePath); try { tpl->parse(); _cache[templatePathname] = tpl; } catch(JSONTemplateException& jte) { if ( _logger ) { poco_error_f2(*_logger, "Template %s contains an error: %s", templatePath.toString(), jte.message()); } } } else { if ( _logger ) { poco_error_f1(*_logger, "Template file %s doesn't exist", templatePath.toString()); } throw FileNotFoundException(templatePathname); } } else { tpl = it->second; if ( tpl->parseTime() < templateFile.getLastModified() ) { if ( _logger ) { poco_information_f1(*_logger, "Reloading template %s", templatePath.toString()); } tpl = new Template(templatePath); try { tpl->parse(); _cache[templatePathname] = tpl; } catch(JSONTemplateException& jte) { if ( _logger ) { poco_error_f2(*_logger, "Template %s contains an error: %s", templatePath.toString(), jte.message()); } } } } return tpl; }