Skip to content

Commit

Permalink
Issue #1947 ( Follow/Tags Iterator Optimization )
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariusz-Trela committed Feb 16, 2018
1 parent b767197 commit 8147c68
Show file tree
Hide file tree
Showing 6 changed files with 414 additions and 129 deletions.
1 change: 1 addition & 0 deletions libraries/plugins/follow/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ add_library( follow_plugin
follow_plugin.cpp
follow_operations.cpp
follow_evaluators.cpp
inc_performance.cpp
)

target_link_libraries( follow_plugin chain_plugin )
Expand Down
67 changes: 31 additions & 36 deletions libraries/plugins/follow/follow_evaluators.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <steem/plugins/follow/follow_plugin.hpp>
#include <steem/plugins/follow/follow_operations.hpp>
#include <steem/plugins/follow/follow_objects.hpp>
#include <steem/plugins/follow/inc_performance.hpp>

#include <steem/chain/account_object.hpp>
#include <steem/chain/comment_object.hpp>
Expand Down Expand Up @@ -121,6 +122,8 @@ void reblog_evaluator::do_apply( const reblog_operation& o )
{
try
{
performance perf( _db );

const auto& c = _db.get_comment( o.author, o.permlink );
FC_ASSERT( c.parent_author.size() == 0, "Only top level posts can be reblogged" );

Expand Down Expand Up @@ -160,60 +163,52 @@ void reblog_evaluator::do_apply( const reblog_operation& o )
});
}

const auto& feed_idx = _db.get_index< feed_index >().indices().get< by_feed >();
const auto& comment_idx = _db.get_index< feed_index >().indices().get< by_comment >();
const auto& idx = _db.get_index< follow_index >().indices().get< by_following_follower >();
const auto& old_feed_idx = _db.get_index< feed_index >().indices().get< by_feed >();
auto itr = idx.find( o.account );

performance_data pd;

if( _db.head_block_time() >= _plugin->start_feeds )
{
while( itr != idx.end() && itr->following == o.account )
{

if( itr->what & ( 1 << blog ) )
{
uint32_t next_id = 0;
auto last_feed = feed_idx.lower_bound( itr->follower );

if( last_feed != feed_idx.end() && last_feed->account == itr->follower )
{
next_id = last_feed->account_feed_id + 1;
}

auto feed_itr = comment_idx.find( boost::make_tuple( c.id, itr->follower ) );
bool is_empty = feed_itr == comment_idx.end();

if( feed_itr == comment_idx.end() )
pd.init( o.account, _db.head_block_time(), c.id, is_empty, is_empty ? 0 : feed_itr->account_feed_id );
uint32_t next_id = perf.delete_old_objects< performance_data::t_creation_type::full_feed >( old_feed_idx, itr->follower, _plugin->max_feed_size, pd );

if( pd.s.creation )
{
_db.create< feed_object >( [&]( feed_object& f )
if( is_empty )
{
f.account = itr->follower;
f.reblogged_by.push_back( o.account );
f.first_reblogged_by = o.account;
f.first_reblogged_on = _db.head_block_time();
f.comment = c.id;
f.reblogs = 1;
f.account_feed_id = next_id;
});
}
else
{
_db.modify( *feed_itr, [&]( feed_object& f )
_db.create< feed_object >( [&]( feed_object& f )
{
f.account = itr->follower;
f.reblogged_by.push_back( o.account );
f.first_reblogged_by = o.account;
f.first_reblogged_on = _db.head_block_time();
f.comment = c.id;
f.account_feed_id = next_id;
});
}
else
{
f.reblogged_by.push_back( o.account );
f.reblogs++;
});
if( pd.s.allow_modify )
{
_db.modify( *feed_itr, [&]( feed_object& f )
{
f.reblogged_by.push_back( o.account );
});
}
}
}

const auto& old_feed_idx = _db.get_index< feed_index >().indices().get< by_old_feed >();
auto old_feed = old_feed_idx.lower_bound( itr->follower );

while( old_feed->account == itr->follower && next_id - old_feed->account_feed_id > _plugin->max_feed_size )
{
_db.remove( *old_feed );
old_feed = old_feed_idx.lower_bound( itr->follower );
};
}

++itr;
}
}
Expand Down
54 changes: 18 additions & 36 deletions libraries/plugins/follow/follow_plugin.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <steem/plugins/follow/follow_plugin.hpp>
#include <steem/plugins/follow/follow_objects.hpp>
#include <steem/plugins/follow/follow_operations.hpp>
#include <steem/plugins/follow/inc_performance.hpp>

#include <steem/chain/util/impacted.hpp>

Expand Down Expand Up @@ -135,8 +136,10 @@ struct post_operation_visitor
{
follow_plugin_impl& _plugin;

performance perf;

post_operation_visitor( follow_plugin_impl& plugin )
: _plugin( plugin ) {}
: _plugin( plugin ), perf( plugin._db ) {}

typedef void result_type;

Expand Down Expand Up @@ -186,75 +189,54 @@ struct post_operation_visitor

const auto& idx = db.get_index< follow_index >().indices().get< by_following_follower >();
const auto& comment_idx = db.get_index< feed_index >().indices().get< by_comment >();
const auto& old_feed_idx = db.get_index< feed_index >().indices().get< by_feed >();
auto itr = idx.find( op.author );

const auto& feed_idx = db.get_index< feed_index >().indices().get< by_feed >();
performance_data pd;

if( db.head_block_time() >= _plugin._self.start_feeds )
{
while( itr != idx.end() && itr->following == op.author )
{
if( itr->what & ( 1 << blog ) )
{
uint32_t next_id = 0;
auto last_feed = feed_idx.lower_bound( itr->follower );
auto feed_itr = comment_idx.find( boost::make_tuple( c.id, itr->follower ) );
bool is_empty = feed_itr == comment_idx.end();

if( last_feed != feed_idx.end() && last_feed->account == itr->follower )
{
next_id = last_feed->account_feed_id + 1;
}
pd.init( c.id, is_empty );
uint32_t next_id = perf.delete_old_objects< performance_data::t_creation_type::part_feed >( old_feed_idx, itr->follower, _plugin._self.max_feed_size, pd );

if( comment_idx.find( boost::make_tuple( c.id, itr->follower ) ) == comment_idx.end() )
if( pd.s.creation && is_empty )
{
db.create< feed_object >( [&]( feed_object& f )
{
f.account = itr->follower;
f.comment = c.id;
f.account_feed_id = next_id;
});

const auto& old_feed_idx = db.get_index< feed_index >().indices().get< by_old_feed >();
auto old_feed = old_feed_idx.lower_bound( itr->follower );

while( old_feed->account == itr->follower && next_id - old_feed->account_feed_id > _plugin._self.max_feed_size )
{
db.remove( *old_feed );
old_feed = old_feed_idx.lower_bound( itr->follower );
}
}
}

}
++itr;
}
}

const auto& blog_idx = db.get_index< blog_index >().indices().get< by_blog >();
const auto& comment_blog_idx = db.get_index< blog_index >().indices().get< by_comment >();
auto last_blog = blog_idx.lower_bound( op.author );
uint32_t next_id = 0;
auto blog_itr = comment_blog_idx.find( boost::make_tuple( c.id, op.author ) );
const auto& old_blog_idx = db.get_index< blog_index >().indices().get< by_blog >();
bool is_empty = blog_itr == comment_blog_idx.end();

if( last_blog != blog_idx.end() && last_blog->account == op.author )
{
next_id = last_blog->blog_feed_id + 1;
}
pd.init( c.id, is_empty );
uint32_t next_id = perf.delete_old_objects< performance_data::t_creation_type::full_blog >( old_blog_idx, op.author, _plugin._self.max_feed_size, pd );

if( comment_blog_idx.find( boost::make_tuple( c.id, op.author ) ) == comment_blog_idx.end() )
if( pd.s.creation && is_empty )
{
db.create< blog_object >( [&]( blog_object& b)
{
b.account = op.author;
b.comment = c.id;
b.blog_feed_id = next_id;
});

const auto& old_blog_idx = db.get_index< blog_index >().indices().get< by_old_blog >();
auto old_blog = old_blog_idx.lower_bound( op.author );

while( old_blog->account == op.author && next_id - old_blog->blog_feed_id > _plugin._self.max_feed_size )
{
db.remove( *old_blog );
old_blog = old_blog_idx.lower_bound( op.author );
}
}
}
FC_LOG_AND_RETHROW()
Expand Down
Loading

0 comments on commit 8147c68

Please sign in to comment.