Skip to content

Commit

Permalink
fixup notifications records data structure
Browse files Browse the repository at this point in the history
Changed it from a list, which would be sequentially searched by id
and owner, to a multi_index container using hashed indices for both
of these fields.
  • Loading branch information
cbeck88 committed Jul 14, 2014
1 parent 4a20dd4 commit 29d5273
Showing 1 changed file with 44 additions and 21 deletions.
65 changes: 44 additions & 21 deletions src/desktop/dbus_notification.cpp
Expand Up @@ -25,8 +25,11 @@
#include <dbus/dbus.h>

#include <boost/cstdint.hpp>
#include <list>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/member.hpp>
#include <stdlib.h>
#include <string>

using boost::uint32_t;

Expand All @@ -41,19 +44,40 @@ bool kde_style = false;

struct wnotify
{
wnotify()
: id()
, owner()
, message()
wnotify(uint32_t id_arg, std::string owner_arg, std::string message_arg)
: id(id_arg)
, owner(owner_arg)
, message(message_arg)
{
}

uint32_t id;
std::string owner;
std::string message;
mutable std::string message;
};

std::list<wnotify> notifications;
struct by_id {};
struct by_owner {};

using boost::multi_index::hashed_unique;
using boost::multi_index::indexed_by;
using boost::multi_index::tag;
using boost::multi_index::member;

typedef boost::multi_index_container<
wnotify,
indexed_by<
//hashed by ids
hashed_unique<tag<by_id>, member<wnotify,const uint32_t,&wnotify::id> >,
//hashed by owners
hashed_unique<tag<by_owner>, member<wnotify,const std::string,&wnotify::owner> >
>
> wnotify_set;

typedef wnotify_set::index<by_owner>::type wnotify_by_owner;
typedef wnotify_by_owner::iterator wnotify_owner_it;

wnotify_set notifications; //!< Holds all the notifications transaction records

DBusHandlerResult filter_dbus_signal(DBusConnection *, DBusMessage *buf, void *)
{
Expand All @@ -66,11 +90,8 @@ DBusHandlerResult filter_dbus_signal(DBusConnection *, DBusMessage *buf, void *)
DBUS_TYPE_UINT32, &id,
DBUS_TYPE_INVALID);

std::list<wnotify>::iterator i = notifications.begin(),
i_end = notifications.end();
while (i != i_end && i->id != id) ++i;
if (i != i_end)
notifications.erase(i);
size_t num_erased = notifications.get<by_id>().erase(id);
LOG_DU << "Erased " << num_erased << " notifications records matching id=" << id;

return DBUS_HANDLER_RESULT_HANDLED;
}
Expand Down Expand Up @@ -177,11 +198,11 @@ void send_notification(const std::string & owner, const std::string & message)
DBusConnection *connection = get_dbus_connection();
if (!connection) return;

std::list<wnotify>::iterator i = notifications.begin(),
i_end = notifications.end();
while (i != i_end && i->owner != owner) ++i;
wnotify_by_owner & noticias = notifications.get<by_owner>();

wnotify_owner_it i = noticias.find(owner);

if (i != i_end) {
if (i != noticias.end()) {
i->message = message + "\n" + i->message;

size_t endl_pos = i->message.find('\n');
Expand All @@ -199,11 +220,13 @@ void send_notification(const std::string & owner, const std::string & message)
} else {
uint32_t id = send_dbus_notification(connection, 0, owner, message);
if (!id) return;
wnotify visual;
visual.id = id;
visual.owner = owner;
visual.message = message;
notifications.push_back(visual);
wnotify visual(id,owner,message);
std::pair<wnotify_owner_it, bool> result = noticias.insert(visual);
if (!result.second) {
ERR_DU << "Failed to insert a dbus notification message:\n"
<< "New Item:\n" << "\tid=" << id << "\n\towner=" << owner << "\n\tmessage=" << message << "\n"
<< "Old Item:\n" << "\tid=" << result.first->id << "\n\towner=" << result.first->owner << "\n\tmessage=" << result.first->message << "\n";
}
}
}

Expand Down

0 comments on commit 29d5273

Please sign in to comment.