Ejemplo n.º 1
0
void MySQLConnection::connect() 
{
	bool reconnecting = false;
	if (_myConn != nullptr) //reconnection attempt
	{
		if (!mysql_ping(_myConn)) //ping ok
			return;
		else
			reconnecting = true;
	}

	//remove any state from previous session
	this->clear();

	Poco::Logger& logger = _dbEngine->getLogger();
	for(;;)
	{
		const char* unix_socket = nullptr;
		if (_unix_socket.length() > 0)
			unix_socket = _unix_socket.c_str();

		_myConn = mysql_real_connect(_myHandle, _host.c_str(), _user.c_str(), _password.c_str(), _database.c_str(), _port, unix_socket, CLIENT_REMEMBER_OPTIONS);
		if (!_myConn)
		{
			const char* actionToDo = "connect";
			if (reconnecting)
				actionToDo = "reconnect";

			unsigned int errNo = mysql_errno(_myHandle);
			if (IsConnectionErrFatal(errNo))
				throw SqlException(errNo,mysql_error(_myHandle),actionToDo);

			static const long sleepTime = 1000;
			logger.warning(Poco::format("Could not %s to MySQL database at %s: %s, retrying in %d seconds",
				string(actionToDo),_host,string(mysql_error(_myHandle)),static_cast<int>(sleepTime/1000)));
			Poco::Thread::sleep(sleepTime);

			continue;
		}
		break;
	}

	string actionDone = (reconnecting)?string("Reconnected"):string("Connected");

	poco_information(logger,Poco::format( actionDone + " to MySQL database %s:%d/%s client ver: %s server ver: %s",
		_host, _port,_database,string(mysql_get_client_info()),string(mysql_get_server_info(_myConn)) ));

	//Autocommit should be ON because without it, MySQL would require everything to be wrapped into a transaction
	if (!mysql_autocommit(_myConn, 1))
		poco_trace(logger,"Set autocommit to true");
	else
		poco_error(logger,"Failed to set autocommit to true");

	//set connection properties to UTF8 to properly handle locales for different server configs
	//core sends data in UTF8, so MySQL must expect UTF8 too
	if (!mysql_set_character_set(_myConn,"utf8"))
		poco_trace(logger,Poco::format("Character set changed to %s",string(mysql_character_set_name(_myConn))));
	else
		poco_error(logger,Poco::format("Failed to change charset, remains at %s",string(mysql_character_set_name(_myConn))));
}
Ejemplo n.º 2
0
vector<Item> ItemDB::getItemsPageKeyword(string keyWord, int64_t pageNumber, int64_t itemNumber) {
    vector<Item> lItem;

    if (pageNumber < 1) {
        poco_error(*logger, "getItemsPage: pageNumber < 0");
        return lItem;
    }
    if (itemNumber < 1) {
        poco_error(*logger, "getItemsPage: numberItems < 0");
        return lItem;
    }
    int64_t size = grassDB.count();

    int totalPageNumber;
    if (size % itemNumber == 0)
        totalPageNumber = size / itemNumber;
    else
        totalPageNumber = size / itemNumber + 1;

    if (pageNumber > totalPageNumber) {
        poco_warning_f2(*logger, "getItemsPage: pageNumber = %d > totalPageNumber = %d",
                pageNumber, totalPageNumber);
        pageNumber = totalPageNumber;
    }

    int64_t first = (pageNumber - 1) * itemNumber;
    int64_t last = pageNumber * itemNumber;
    int count = 0;
    string key, value;
    DB::Cursor* cur = grassDB.cursor();
    cur->jump();

    Item item;
    while (cur->get(&key, &value, true)) {
        if (Utils::findStringInString(value, keyWord)) {
            count++;
            if (count < first)
                continue;
            item = convertJsonToItem(value);
            item.itemID = key;
            lItem.push_back(item);
            if (count >= last)
                break;
        }
    }
    delete cur;
    return lItem;
}
Ejemplo n.º 3
0
void EDGAR_FilingFile::FindEDGAR_Tables(RunMode run_mode)
{
    // always start fresh

    EDGAR_filing_tables_.clear();

    CDocument the_filing;
    the_filing.parse(EDGAR_filing_content_.c_str());
    CSelection c = the_filing.find("table");

    // ConvertMarkupToText is a time consuming function -- it actually calls out to
    // an external process right now -- so let's do some async'ing !!

    // keep track of our async processes here.

    std::vector<std::future<std::string>> tasks;

    for (int indx = 0 ; indx < c.nodeNum(); ++indx)
    {
        CNode pNode = c.nodeAt(indx);

        // use the 'Outer' functions to include the table tags in the extracted content.

        std::string content = EDGAR_filing_content_.substr(pNode.startPosOuter(), pNode.endPosOuter() - pNode.startPosOuter());
		if (TableHasMarkup(content))
		{
            if (run_mode == RunMode::do_sync)
            {
    			std::string plain_table = ConvertMarkupToText(content);
                EDGAR_filing_tables_.push_back(plain_table);
            }
            else
                tasks.push_back(std::async(&EDGAR_FilingFile::ConvertMarkupToText, this, content));
		}
        else
            EDGAR_filing_tables_.push_back(content);
    }

    // now, let's go look for our output...

    for (int count = tasks.size(); count; --count)
    {
        int i = wait_for_any(tasks, std::chrono::microseconds{100});
        std::string converted_text;
        try
        {
            converted_text = tasks[i].get();
        }
        catch (...)
        {
            // any problems, let's just ignore them.
            poco_error(the_logger_, "Some problem with an async process");
            continue;
        }
        if (! converted_text.empty())
            EDGAR_filing_tables_.push_back(converted_text);
    }
}        // -----  end of method EDGAR_FilingFile::FindEDGAR_Tables  -----
Ejemplo n.º 4
0
LoggerChannel::LoggerChannel(QObject *parent):
    QObject(parent),
    _logger(Poco::Logger::get("")),
    _oldLevel(_logger.getLevel()),
    _splitter(dynamic_cast<Poco::SplitterChannel *>(_logger.getChannel()))
{
    _logger.setLevel(Poco::Message::PRIO_TRACE); //lowest level -> shows everything
    if (_splitter) _splitter->addChannel(this);
    else poco_error(Poco::Logger::get("PothosGui.LoggerChannel"), "expected SplitterChannel");
}
Ejemplo n.º 5
0
/**
 * 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";
    }
}
Ejemplo n.º 6
0
static void loadModuleAtPath(const std::string &path)
{
    try
    {
        Pothos::PluginModule::safeLoad(path);
    }
    catch(const Pothos::PluginModuleError &ex)
    {
        poco_error(Poco::Logger::get("Pothos.PluginLoader.load"), ex.displayText());
    }
}
Ejemplo n.º 7
0
bool TagDB::deleteTag(string tagID, ItemTagDB& itemTagDB) {
    if (grassDB.check(tagID) != -1) {
        grassDB.remove(tagID);
        itemTagDB.deleteItemTag(tagID);
        addQueue(DELETE, tagID, "");
        return true;
    }
    //cout << "Data doesn't exit!" << endl;
    poco_error(*logger, "deleteTag: Error deleteTag data doesn't existed!");
    return false;
}
Ejemplo n.º 8
0
/**
 * Lấy ListItem có itemID thuộc list itemIDs.
 * @param itemIDs
 * @return vector<Item>
 */
vector<Item> ItemDB::getItemsFromListItemID(vector<string> itemIDs) {
    vector<Item> lItem;
    int64_t size = itemIDs.size();
    if (size == 0) {
        poco_error(*logger, "getItemsFromListItemID: List ItemIDs is empty.");
        return lItem;
    }
    for (int i = 0; i < size; i++) {
        lItem.push_back(getItemFromItemID(itemIDs[i]));
    }
    return lItem;
}
Ejemplo n.º 9
0
/**
 * 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;
}
Ejemplo n.º 10
0
/**
 * get item co tagID va keyword
 * @param keyword
 * @param tagID
 * @param itemTagDB
 * @return vector<Item>
 */
vector<Item> ItemDB::getItemKeyword(string keyword, string tagID, ItemTagDB& itemTagDB) {
    vector<Item> result;
    if (keyword.empty()) {
        poco_error(*logger, "getItemKeyword: keyword is empty.");
        return result;
    }
    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);
    }
    return result;
}
Ejemplo n.º 11
0
/**
 * Random 1 Item.
 * @return Item
 */
Item ItemDB::getRandomItem() {
    Item item;
    int64_t lastID = Utils::convertStringToInt(LASTID);
    string itemID;
    int i = 0;
    do {
        itemID = Utils::convertIntToString(Utils::getRandomNumber(lastID));
        item = getItemFromItemID(itemID);
        i++;
        if (i > 10) {
            poco_error(*logger, "getRandomItem: Random failed because more than 10 times.");
            break;
        }
    } while (item.itemID == "-1");
    return item;
}
Ejemplo n.º 12
0
/***********************************************************************
 * Instance of BlockRegistry peforms check and plugin registration
 **********************************************************************/
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", path);
    }
    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(std::shared_ptr<Block>) or
        factory.type(-1) == typeid(Topology*) or
        factory.type(-1) == typeid(std::shared_ptr<Topology>) or
        isOpaqueFactory(factory))
    {
        //register
        try
        {
            PluginRegistry::add(fullPath, factory);
        }
        catch (const PluginRegistryError &ex)
        {
            poco_error(Poco::Logger::get("Pothos.BlockRegistry"), ex.displayText());
            return;
        }
    }

    //otherwise report the error
    else
    {
        poco_error_f1(Poco::Logger::get("Pothos.BlockRegistry"), "Bad Factory, must return Block* or Topology*: %s", factory.toString());
    }
}
Ejemplo n.º 13
0
RemoteProxyHandle::~RemoteProxyHandle(void)
{
    //create request
    Pothos::ObjectKwargs req;
    req["action"] = Pothos::Object("~RemoteProxyHandle");
    req["handleID"] = Pothos::Object(this->remoteID);

    try
    {
        env->transact(req);
    }
    catch(const Pothos::Exception &ex)
    {
        if (not env->connectionActive) return;
        poco_error(Poco::Logger::get("Pothos.RemoteProxyHandle"), "destructor threw: "+ex.displayText());
    }
}
Ejemplo n.º 14
0
/**
 * Lấy listItem mà content có chứa từ khóa keyword.
 * @param keyword
 * @return vector<Item>
 */
vector<Item> ItemDB::getItemKeyword(string keyword) {
    vector<Item> result;
    if (keyword.empty()) {
        poco_error(*logger, "getItemKeyword: keyword is empty.");
        return result;
    }
    DB::Cursor* cur = grassDB.cursor();
    cur->jump();
    string ckey, cvalue;
    while (cur->get(&ckey, &cvalue, true)) {
        if (Utils::findStringInString(cvalue, keyword)) {
            Item item = getItemFromItemID(ckey);
            item.itemID = ckey;
            result.push_back(item);
        }
    }
    delete cur;
    return result;
}
Ejemplo n.º 15
0
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;
}
Ejemplo n.º 16
0
vector<Tag> TagDB::getTagKeyword(string keyword) {
    vector<Tag> result;
    if (keyword.empty()) {
        poco_error(*logger, "getTagKeyword: keyword is NULL.");
        return result;
    }
    DB::Cursor* cur = grassDB.cursor();
    cur->jump();
    string ckey, cvalue;
    while (cur->get(&ckey, &cvalue, true)) {

        if (Utils::findStringInStringForTag(cvalue, keyword)) {
            Tag tag = convertJsonToTag(cvalue);
            tag.tagID = ckey;
            result.push_back(tag);
        }
    }
    delete cur;
    return result;

}
Ejemplo n.º 17
0
/**
 * Lay list number Item theo keyword.
 * @param keyword
 * @param numberItems
 * @return vector<Item>
 */
vector<Item> ItemDB::getItemKeyword(string keyword, int32_t numberItems) {
    vector<Item> result;
    if (keyword.empty()) {
        poco_error(*logger, "getItemKeyword: keyword is empty.");
        return result;
    }
    int32_t i = 0;
    DB::Cursor* cur = grassDB.cursor();
    cur->jump();
    string ckey, cvalue;
    while (cur->get(&ckey, &cvalue, true)) {
        if (Utils::findStringInString(cvalue, keyword)) {
            i++;
            Item item = convertJsonToItem(cvalue);
            item.itemID = ckey;
            result.push_back(item);
        }
        if (i == numberItems)
            break;
    }
    delete cur;
    return result;
}
Ejemplo n.º 18
0
/**
 * Lấy List Item theo kiểu phân trang, nếu tagID==-1 thì sẽ lấy trong ItemDB,
 * tagID>1 thì sẽ lấy Item thuộc tagID đó.
 * @param pageNumber
 * @param itemNumber
 * @param tagID
 * @return vector<Item>
 */
vector<Item> ItemDB::getItemsPage(int64_t pageNumber, int32_t numberItems, string tagID,
        ItemTagDB& itemTagDB) {

    vector<Item> lItem;

    if (pageNumber < 1) {
        poco_error(*logger, "getItemsPage: pageNumber < 0");
        return lItem;
    }
    if (numberItems < 1) {
        poco_error(*logger, "getItemsPage: numberItems < 0");
        return lItem;
    }
    Item item;
    if (tagID == "-1") {
        int64_t size = grassDB.count();
        int totalPageNumber;
        if (size % numberItems == 0)
            totalPageNumber = size / numberItems;
        else
            totalPageNumber = size / numberItems + 1;
        if (pageNumber > totalPageNumber) {
            poco_warning_f2(*logger, "getItemsPage: pageNumber = %d > totalPageNumber = %d",
                    pageNumber, totalPageNumber);
            pageNumber = totalPageNumber;
        }
        int64_t last = pageNumber * numberItems;
        int64_t first = (pageNumber - 1) * numberItems - 1;
        int64_t i = 0;
        string key, value;
        DB::Cursor* cur = grassDB.cursor();
        cur->jump();
        while (i < last) {
            cur->get(&key, &value, true);
            if (i > first) {
                item = convertJsonToItem(value);
                item.itemID = key;
                lItem.push_back(item);
            }
            i++;
        }
        delete cur;
    } else {
        vector<string> lItemID = itemTagDB.getAllItemsIdHaveTag(tagID);
        int64_t sizeLItemID = lItemID.size();
        int totalPageNumber;
        if (sizeLItemID % numberItems == 0)
            totalPageNumber = sizeLItemID / numberItems;
        else
            totalPageNumber = sizeLItemID / numberItems + 1;
        if (pageNumber > totalPageNumber) {
            poco_warning_f2(*logger, "getItemsPage: pageNumber = %d > totalPageNumber = %d",
                    pageNumber, totalPageNumber);
            pageNumber = totalPageNumber;
        }
        int64_t last = pageNumber * numberItems;
        int64_t first = (pageNumber - 1) * numberItems - 1;
        int64_t i = 0;
        if (!lItemID.empty()) {
            for (i = first + 1; i < last; i++) {
                if (i == sizeLItemID)
                    return lItem;
                lItem.push_back(getItemFromItemID(lItemID[i]));
            }
        }
    }
    return lItem;
}