Skip to content

Commit

Permalink
Merge pull request steemit#1166 from steemit/1161-optimize-update-tags
Browse files Browse the repository at this point in the history
`update_tags` inefficient
  • Loading branch information
Michael Vandeberg authored Jun 7, 2017
2 parents 4f748ba + 86a4cd5 commit d16a211
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 26 deletions.
8 changes: 7 additions & 1 deletion libraries/plugins/tags/include/steemit/tags/tags_plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,13 @@ typedef multi_index_container<
tag_object,
indexed_by<
ordered_unique< tag< by_id >, member< tag_object, tag_id_type, &tag_object::id > >,
ordered_non_unique< tag< by_comment >, member< tag_object, comment_id_type, &tag_object::comment > >,
ordered_unique< tag< by_comment >,
composite_key< tag_object,
member< tag_object, comment_id_type, &tag_object::comment >,
member< tag_object, tag_id_type, &tag_object::id >
>,
composite_key_compare< std::less< comment_id_type >, std::less< tag_id_type > >
>,
ordered_unique< tag< by_author_comment >,
composite_key< tag_object,
member< tag_object, account_id_type, &tag_object::author >,
Expand Down
77 changes: 52 additions & 25 deletions libraries/plugins/tags/tags_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,45 +234,72 @@ struct operation_visitor {
}

/** finds tags that have been added or removed or updated */
void update_tags( const comment_object& c )const {
void update_tags( const comment_object& c, bool parse_tags = false )const
{
try {

auto hot = calculate_hot( c.net_rshares, c.created );
auto trending = calculate_trending( c.net_rshares, c.created );
auto meta = filter_tags( c );

const auto& comment_idx = _db.get_index< tag_index >().indices().get< by_comment >();
auto citr = comment_idx.lower_bound( c.id );

map< string, const tag_object* > existing_tags;
vector< const tag_object* > remove_queue;
while( citr != comment_idx.end() && citr->comment == c.id ) {
const tag_object* tag = &*citr;
++citr;
if( meta.tags.find( tag->tag ) == meta.tags.end() ) {
remove_queue.push_back(tag);
} else {
existing_tags[tag->tag] = tag;

if( parse_tags )
{
auto meta = filter_tags( c );
auto citr = comment_idx.lower_bound( c.id );

map< string, const tag_object* > existing_tags;
vector< const tag_object* > remove_queue;

while( citr != comment_idx.end() && citr->comment == c.id )
{
const tag_object* tag = &*citr;
++citr;

if( meta.tags.find( tag->tag ) == meta.tags.end() )
{
remove_queue.push_back(tag);
}
else
{
existing_tags[tag->tag] = tag;
}
}
}

for( const auto& tag : meta.tags ) {
auto existing = existing_tags.find(tag);
if( existing == existing_tags.end() ) {
create_tag( tag, c, hot, trending );
} else {
update_tag( *existing->second, c, hot, trending );
for( const auto& tag : meta.tags )
{
auto existing = existing_tags.find(tag);

if( existing == existing_tags.end() )
{
create_tag( tag, c, hot, trending );
}
else
{
update_tag( *existing->second, c, hot, trending );
}
}

for( const auto& item : remove_queue )
remove_tag(*item);
}
else
{
auto citr = comment_idx.lower_bound( c.id );

for( const auto& item : remove_queue )
remove_tag(*item);
while( citr != comment_idx.end() && citr->comment == c.id )
{
update_tag( *citr, c, hot, trending );
++citr;
}
}

if( c.parent_author.size() )
{
update_tags( _db.get_comment( c.parent_author, c.parent_permlink ) );
}
} FC_CAPTURE_LOG_AND_RETHROW( (c) )
}
} FC_CAPTURE_LOG_AND_RETHROW( (c) )
}

const peer_stats_object& get_or_create_peer_stats( account_id_type voter, account_id_type peer )const {
const auto& peeridx = _db.get_index<peer_stats_index>().indices().get<by_voter_peer>();
Expand Down Expand Up @@ -321,7 +348,7 @@ struct operation_visitor {
}

void operator()( const comment_operation& op )const {
update_tags( _db.get_comment( op.author, op.permlink ) );
update_tags( _db.get_comment( op.author, op.permlink ), true );
}

void operator()( const transfer_operation& op )const {
Expand Down

0 comments on commit d16a211

Please sign in to comment.