fixed feed bug by moving away from std::string as a return type to uniqueIdentifier(). Now uint64_ hash is used and cached

This commit is contained in:
csoler 2019-12-21 14:33:53 +01:00
parent ff86d3cd20
commit 137cb5271d
No known key found for this signature in database
GPG Key ID: 7BCA522266C0804C
22 changed files with 51 additions and 31 deletions

View File

@ -49,7 +49,7 @@ public:
const RsPostedPost &getPost() const; const RsPostedPost &getPost() const;
RsPostedPost &post(); RsPostedPost &post();
std::string uniqueIdentifier() const override { return "PostedItem " + mMessageId.toStdString(); } uint64_t uniqueIdentifier() const override { return hash_64bits("PostedItem " + mMessageId.toStdString()); }
protected: protected:
/* FeedItem */ /* FeedItem */
virtual void doExpand(bool open); virtual void doExpand(bool open);

View File

@ -179,7 +179,7 @@ void RSFeedWidget::addFeedItem(FeedItem *feedItem, Qt::ItemDataRole sortRole, co
QTreeWidgetItem *treeItem = new RSTreeWidgetItem(mFeedCompareRole); QTreeWidgetItem *treeItem = new RSTreeWidgetItem(mFeedCompareRole);
treeItem->setData(COLUMN_FEED, sortRole, value); treeItem->setData(COLUMN_FEED, sortRole, value);
treeItem->setData(COLUMN_IDENTIFIER, Qt::DisplayRole, QString::fromStdString(feedItem->uniqueIdentifier())); treeItem->setData(COLUMN_IDENTIFIER, Qt::DisplayRole, QString("%1").arg(feedItem->uniqueIdentifier(),8,16,QChar('0')));
ui->treeWidget->addTopLevelItem(treeItem); ui->treeWidget->addTopLevelItem(treeItem);
ui->treeWidget->setItemWidget(treeItem, 0, feedItem); ui->treeWidget->setItemWidget(treeItem, 0, feedItem);
@ -207,9 +207,9 @@ void RSFeedWidget::addFeedItem(FeedItem *feedItem, const QMap<Qt::ItemDataRole,
for (it = sort.begin(); it != sort.end(); ++it) { for (it = sort.begin(); it != sort.end(); ++it) {
treeItem->setData(COLUMN_FEED, it.key(), it.value()); treeItem->setData(COLUMN_FEED, it.key(), it.value());
} }
treeItem->setData(COLUMN_IDENTIFIER, Qt::DisplayRole, QString("%1").arg(feedItem->uniqueIdentifier(),8,16,QChar('0')));
ui->treeWidget->addTopLevelItem(treeItem); ui->treeWidget->addTopLevelItem(treeItem);
treeItem->setData(COLUMN_FEED, Qt::DisplayRole, QString::fromStdString(feedItem->uniqueIdentifier()));
ui->treeWidget->setItemWidget(treeItem, 0, feedItem); ui->treeWidget->setItemWidget(treeItem, 0, feedItem);
feedAdded(feedItem, treeItem); feedAdded(feedItem, treeItem);
@ -438,10 +438,10 @@ void RSFeedWidget::feedItemDestroyed(FeedItem *feedItem)
emit feedCountChanged(); emit feedCountChanged();
} }
QTreeWidgetItem *RSFeedWidget::findTreeWidgetItem(const std::string& identifier) QTreeWidgetItem *RSFeedWidget::findTreeWidgetItem(uint64_t identifier)
{ {
std::cerr << "FindTreeWidgetItem: looking for \"" << identifier << "\"" << std::endl; std::cerr << "FindTreeWidgetItem: looking for \"" << identifier << "\"" << std::endl;
QList<QTreeWidgetItem*> list = ui->treeWidget->findItems(QString::fromStdString(identifier),Qt::MatchExactly,COLUMN_IDENTIFIER); QList<QTreeWidgetItem*> list = ui->treeWidget->findItems(QString("%1").arg(identifier,8,16,QChar('0')),Qt::MatchExactly,COLUMN_IDENTIFIER);
if(list.empty()) if(list.empty())
{ {
@ -495,7 +495,7 @@ void RSFeedWidget::withAll(RSFeedWidgetCallbackFunction callback, void *data)
} }
} }
FeedItem *RSFeedWidget::findFeedItem(const std::string& identifier) FeedItem *RSFeedWidget::findFeedItem(uint64_t identifier)
{ {
QTreeWidgetItemIterator it(ui->treeWidget); QTreeWidgetItemIterator it(ui->treeWidget);
QTreeWidgetItem *treeItem=NULL; QTreeWidgetItem *treeItem=NULL;
@ -515,7 +515,7 @@ FeedItem *RSFeedWidget::findFeedItem(const std::string& identifier)
// if(feedItem->uniqueIdentifier() == identifier) // if(feedItem->uniqueIdentifier() == identifier)
// causes a crash. I dont know why! If someone ever finds why, please tell me. // causes a crash. I dont know why! If someone ever finds why, please tell me.
std::string id = feedItem->uniqueIdentifier(); uint64_t id = feedItem->uniqueIdentifier();
std::cerr << "Comparing \"" << id << "\""; std::cerr << "Comparing \"" << id << "\"";
std::cerr << " to " << identifier << "\"" << " pthread_t = " << pthread_self() << std::endl; std::cerr << " to " << identifier << "\"" << " pthread_t = " << pthread_self() << std::endl;

View File

@ -71,7 +71,7 @@ public:
bool scrollTo(FeedItem *feedItem, bool focus); bool scrollTo(FeedItem *feedItem, bool focus);
void withAll(RSFeedWidgetCallbackFunction callback, void *data); void withAll(RSFeedWidgetCallbackFunction callback, void *data);
FeedItem *findFeedItem(const std::string &identifier); FeedItem *findFeedItem(uint64_t identifier);
void selectedFeedItems(QList<FeedItem*> &feedItems); void selectedFeedItems(QList<FeedItem*> &feedItems);
@ -98,7 +98,7 @@ private:
void connectSignals(FeedItem *feedItem); void connectSignals(FeedItem *feedItem);
void disconnectSignals(FeedItem *feedItem); void disconnectSignals(FeedItem *feedItem);
FeedItem *feedItemFromTreeItem(QTreeWidgetItem *treeItem); FeedItem *feedItemFromTreeItem(QTreeWidgetItem *treeItem);
QTreeWidgetItem *findTreeWidgetItem(const std::string &identifier); QTreeWidgetItem *findTreeWidgetItem(uint64_t identifier);
void filterItems(); void filterItems();
void filterItem(QTreeWidgetItem *treeItem, FeedItem *feedItem); void filterItem(QTreeWidgetItem *treeItem, FeedItem *feedItem);

View File

@ -37,7 +37,7 @@ public:
void updateItemStatic(); void updateItemStatic();
virtual std::string uniqueIdentifier() const override { return "ChatMsgItem " + mPeerId.toStdString(); } virtual uint64_t uniqueIdentifier() const override { return hash_64bits("ChatMsgItem " + mPeerId.toStdString()); }
protected: protected:
/* FeedItem */ /* FeedItem */
virtual void doExpand(bool /*open*/) {} virtual void doExpand(bool /*open*/) {}

View File

@ -18,10 +18,11 @@
* * * *
*******************************************************************************/ *******************************************************************************/
#include <iostream>
#include "FeedItem.h" #include "FeedItem.h"
/** Constructor */ /** Constructor */
FeedItem::FeedItem(QWidget *parent) : QWidget(parent) FeedItem::FeedItem(QWidget *parent) : QWidget(parent), mHash(0)
{ {
mWasExpanded = false; mWasExpanded = false;
} }
@ -43,3 +44,18 @@ void FeedItem::expand(bool open)
mWasExpanded = true; mWasExpanded = true;
} }
} }
uint64_t FeedItem::hash_64bits(const std::string& s) const
{
if(mHash == 0)
{
mHash = 0x01110bbfa09;
for(uint32_t i=0;i<s.size();++i)
mHash = ~(((mHash << 31) ^ (mHash >> 3)) + s[i]*0x217898fbba7 + 0x0294379);
std::cerr << "Producing hash " << std::hex << mHash << std::dec << " from string \"" << s << "\"" << std::endl;
}
return mHash;
}

View File

@ -41,17 +41,19 @@ public:
* \return returns a string that is unique to this specific item. The string will be used to search for an existing item that * \return returns a string that is unique to this specific item. The string will be used to search for an existing item that
* would contain the same information. It should therefore sumarise the data represented by the item. * would contain the same information. It should therefore sumarise the data represented by the item.
*/ */
virtual std::string uniqueIdentifier() const =0; virtual uint64_t uniqueIdentifier() const =0;
protected: protected:
virtual void doExpand(bool open) = 0; virtual void doExpand(bool open) = 0;
virtual void expandFill(bool /*first*/) {} virtual void expandFill(bool /*first*/) {}
uint64_t hash_64bits(const std::string& s) const;
signals: signals:
void sizeChanged(FeedItem *feedItem); void sizeChanged(FeedItem *feedItem);
void feedItemDestroyed(FeedItem *feedItem); void feedItemDestroyed(FeedItem *feedItem);
private: private:
bool mWasExpanded; bool mWasExpanded;
mutable uint64_t mHash;
}; };
#endif #endif

View File

@ -41,8 +41,7 @@ public:
~GxsChannelGroupItem(); ~GxsChannelGroupItem();
bool setGroup(const RsGxsChannelGroup &group); bool setGroup(const RsGxsChannelGroup &group);
uint64_t uniqueIdentifier() const override { return hash_64bits("GxsChannelGroupItem " + groupId().toStdString()) ; }
std::string uniqueIdentifier() const override { return "GxsChannelGroupItem " + mGroup.mMeta.mGroupId.toStdString() ; }
protected: protected:
/* FeedItem */ /* FeedItem */
virtual void doExpand(bool open); virtual void doExpand(bool open);

View File

@ -71,6 +71,7 @@ void GxsChannelPostItem::init(const RsGxsMessageId& messageId,const std::set<RsG
setup(); setup();
mPost.mMeta.mMsgId = messageId; // useful for uniqueIdentifer() before the post is loaded
mLoaded = false ; mLoaded = false ;
} }

View File

@ -53,7 +53,7 @@ public:
//GxsChannelPostItem(FeedHolder *feedHolder, uint32_t feedId, const RsGxsChannelPost &post, bool isHome, bool autoUpdate); //GxsChannelPostItem(FeedHolder *feedHolder, uint32_t feedId, const RsGxsChannelPost &post, bool isHome, bool autoUpdate);
virtual ~GxsChannelPostItem(); virtual ~GxsChannelPostItem();
virtual std::string uniqueIdentifier() const override { "GxsChannelPostItem " + mPost.mMeta.mMsgId.toStdString() ; } uint64_t uniqueIdentifier() const override { hash_64bits("GxsChannelPostItem " + mPost.mMeta.mMsgId.toStdString()) ; }
bool setGroup(const RsGxsChannelGroup &group, bool doFill = true); bool setGroup(const RsGxsChannelGroup &group, bool doFill = true);
bool setPost(const RsGxsChannelPost &post, bool doFill = true); bool setPost(const RsGxsChannelPost &post, bool doFill = true);

View File

@ -167,9 +167,9 @@ void GxsCircleItem::setup()
} }
std::string GxsCircleItem::uniqueIdentifier() const uint64_t GxsCircleItem::uniqueIdentifier() const
{ {
return "GxsCircle " + mCircleId.toStdString() + " " + mGxsId.toStdString() + " " + QString::number(mType).toStdString(); return hash_64bits("GxsCircle " + mCircleId.toStdString() + " " + mGxsId.toStdString() + " " + QString::number(mType).toStdString());
} }
void GxsCircleItem::removeItem() void GxsCircleItem::removeItem()

View File

@ -52,7 +52,7 @@ public:
GxsCircleItem(FeedHolder *feedHolder, uint32_t feedId, const RsGxsCircleId &circleId, const RsGxsId &gxsId, const uint32_t type); GxsCircleItem(FeedHolder *feedHolder, uint32_t feedId, const RsGxsCircleId &circleId, const RsGxsId &gxsId, const uint32_t type);
virtual ~GxsCircleItem(); virtual ~GxsCircleItem();
std::string uniqueIdentifier() const override; uint64_t uniqueIdentifier() const override;
void loadRequest(const TokenQueue *queue, const TokenRequest &req); void loadRequest(const TokenQueue *queue, const TokenRequest &req);

View File

@ -42,7 +42,7 @@ public:
bool setGroup(const RsGxsForumGroup &group); bool setGroup(const RsGxsForumGroup &group);
virtual std::string uniqueIdentifier() const override { return "GxsForumGroupItem " + mGroup.mMeta.mGroupId.toStdString() ; } uint64_t uniqueIdentifier() const override { return hash_64bits("GxsForumGroupItem " + groupId().toStdString()) ; }
protected: protected:
/* FeedItem */ /* FeedItem */
virtual void doExpand(bool open); virtual void doExpand(bool open);

View File

@ -43,6 +43,8 @@
GxsForumMsgItem::GxsForumMsgItem(FeedHolder *feedHolder, uint32_t feedId, const RsGxsGroupId &groupId, const RsGxsMessageId &messageId, bool isHome, bool autoUpdate) : GxsForumMsgItem::GxsForumMsgItem(FeedHolder *feedHolder, uint32_t feedId, const RsGxsGroupId &groupId, const RsGxsMessageId &messageId, bool isHome, bool autoUpdate) :
GxsFeedItem(feedHolder, feedId, groupId, messageId, isHome, rsGxsForums, autoUpdate) GxsFeedItem(feedHolder, feedId, groupId, messageId, isHome, rsGxsForums, autoUpdate)
{ {
mMessage.mMeta.mMsgId = messageId; // useful for uniqueIdentifier() before the post is actually loaded
mMessage.mMeta.mGroupId = groupId;
setup(); setup();
requestGroup(); requestGroup();

View File

@ -43,7 +43,7 @@ public:
bool setGroup(const RsGxsForumGroup &group, bool doFill = true); bool setGroup(const RsGxsForumGroup &group, bool doFill = true);
bool setMessage(const RsGxsForumMsg &msg, bool doFill = true); bool setMessage(const RsGxsForumMsg &msg, bool doFill = true);
std::string uniqueIdentifier() const override { return "GxsForumMsgItem " + mMessage.mMeta.mMsgId.toStdString() ; } uint64_t uniqueIdentifier() const override { return hash_64bits("GxsForumMsgItem " + mMessage.mMeta.mMsgId.toStdString()) ; }
protected: protected:
/* FeedItem */ /* FeedItem */
virtual void doExpand(bool open); virtual void doExpand(bool open);

View File

@ -38,7 +38,7 @@ public:
void updateItemStatic(); void updateItemStatic();
virtual std::string uniqueIdentifier() const override { return "MsgItem " + mMsgId ; } uint64_t uniqueIdentifier() const override { return hash_64bits("MsgItem " + mMsgId) ; }
protected: protected:
/* FeedItem */ /* FeedItem */
virtual void doExpand(bool open); virtual void doExpand(bool open);

View File

@ -67,9 +67,9 @@ PeerItem::PeerItem(FeedHolder *parent, uint32_t feedId, const RsPeerId &peerId,
updateItem(); updateItem();
} }
std::string PeerItem::uniqueIdentifier() const uint64_t PeerItem::uniqueIdentifier() const
{ {
return "PeerItem " + mPeerId.toStdString() + " " + QString::number(mType).toStdString() ; return hash_64bits("PeerItem " + mPeerId.toStdString() + " " + QString::number(mType).toStdString()) ;
} }
void PeerItem::updateItemStatic() void PeerItem::updateItemStatic()

View File

@ -43,7 +43,7 @@ public:
void updateItemStatic(); void updateItemStatic();
std::string uniqueIdentifier() const override; uint64_t uniqueIdentifier() const override;
protected: protected:
/* FeedItem */ /* FeedItem */

View File

@ -42,7 +42,7 @@ public:
bool setGroup(const RsPostedGroup &group); bool setGroup(const RsPostedGroup &group);
virtual std::string uniqueIdentifier() const override { return "PostedGroupItem " + mGroup.mMeta.mGroupId.toStdString() ; } uint64_t uniqueIdentifier() const override { return hash_64bits("PostedGroupItem " + groupId().toStdString()) ; }
protected: protected:
/* FeedItem */ /* FeedItem */

View File

@ -78,9 +78,9 @@ void SecurityIpItem::setup()
updateItem(); updateItem();
} }
std::string SecurityIpItem::uniqueIdentifier() const uint64_t SecurityIpItem::uniqueIdentifier() const
{ {
return "SecurityItem " + QString::number(mType).toStdString() + " " + mSslId.toStdString() + " " + mIpAddr + " " + mIpAddrReported ; return hash_64bits("SecurityItem " + QString::number(mType).toStdString() + " " + mSslId.toStdString() + " " + mIpAddr + " " + mIpAddrReported) ;
} }
void SecurityIpItem::updateItemStatic() void SecurityIpItem::updateItemStatic()

View File

@ -43,7 +43,7 @@ public:
void updateItemStatic(); void updateItemStatic();
std::string uniqueIdentifier() const override; uint64_t uniqueIdentifier() const override;
protected: protected:
/* FeedItem */ /* FeedItem */

View File

@ -81,9 +81,9 @@ SecurityItem::SecurityItem(FeedHolder *parent, uint32_t feedId, const RsPgpId &g
updateItem(); updateItem();
} }
std::string SecurityItem::uniqueIdentifier() const uint64_t SecurityItem::uniqueIdentifier() const
{ {
return "SecurityItem " + QString::number(mType).toStdString() + " " + mSslId.toStdString(); return hash_64bits("SecurityItem " + QString::number(mType).toStdString() + " " + mSslId.toStdString());
} }
void SecurityItem::updateItemStatic() void SecurityItem::updateItemStatic()

View File

@ -42,7 +42,7 @@ public:
void updateItemStatic(); void updateItemStatic();
std::string uniqueIdentifier() const override; uint64_t uniqueIdentifier() const override;
protected: protected:
/* FeedItem */ /* FeedItem */