Skip to content

Commit

Permalink
GH-109 Add lower_bound(trx_id)
Browse files Browse the repository at this point in the history
  • Loading branch information
heifner committed Sep 16, 2022
1 parent dfbe904 commit b767da3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ class unapplied_transaction_queue {
iterator incoming_begin() { return queue.get<by_type>().lower_bound( trx_enum_type::incoming_persisted ); }
iterator incoming_end() { return queue.get<by_type>().end(); } // if changed to upper_bound, verify usage performance

iterator lower_bound( const transaction_id_type& id ) {
auto itr = queue.get<by_trx_id>().find( id );
if( itr == queue.get<by_trx_id>().end() ) return queue.get<by_type>().end();
return queue.project<by_type>(itr);
}

/// caller's responsibility to call next() if applicable
iterator erase( iterator itr ) {
removed( itr );
Expand Down
34 changes: 34 additions & 0 deletions unittests/unapplied_transaction_queue_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ auto create_test_block_state( deque<transaction_metadata_ptr> trx_metas ) {
return bsp;
}

// given a current itr make sure expected number of items are iterated over
void verify_order( unapplied_transaction_queue& q, unapplied_transaction_queue::iterator itr, size_t expected ) {
size_t size = 0;
std::set<transaction_id_type> ids;
for( ; itr != q.end(); ++itr, ++size ) {
ids.insert( itr->id() );
}
BOOST_TEST( size == expected );
BOOST_TEST( ids.size() == expected );
}

BOOST_AUTO_TEST_CASE( unapplied_transaction_queue_test ) try {

unapplied_transaction_queue q;
Expand Down Expand Up @@ -260,41 +271,64 @@ BOOST_AUTO_TEST_CASE( unapplied_transaction_queue_test ) try {
q.add_persisted( trx8 );
q.add_aborted( { trx18, trx19 } );
q.add_forked( { bs6, bs5, bs4 } );
// verify order
verify_order( q, q.begin(), 17 );
verify_order( q, q.lower_bound(trx16->id()), 17 );
// verify type order
BOOST_CHECK( q.size() == 17 );
BOOST_REQUIRE( next( q ) == trx16 );
BOOST_CHECK( q.size() == 16 );
verify_order( q, q.lower_bound(trx8->id()), 16 );
BOOST_REQUIRE( next( q ) == trx8 );
BOOST_CHECK( q.size() == 15 );
verify_order( q, q.lower_bound(trx1->id()), 15 );
BOOST_REQUIRE( next( q ) == trx1 );
BOOST_CHECK( q.size() == 14 );
verify_order( q, q.lower_bound(trx2->id()), 14 );
BOOST_REQUIRE( next( q ) == trx2 );
BOOST_CHECK( q.size() == 13 );
verify_order( q, q.lower_bound(trx3->id()), 13 );
verify_order( q, q.lower_bound(trx15->id()), 5 );
BOOST_REQUIRE_EQUAL( next( q ), trx3 );
BOOST_CHECK( q.size() == 12 );
verify_order( q, q.lower_bound(trx4->id()), 12 );
BOOST_REQUIRE( next( q ) == trx4 );
BOOST_CHECK( q.size() == 11 );
verify_order( q, q.lower_bound(trx5->id()), 11 );
BOOST_REQUIRE( next( q ) == trx5 );
BOOST_CHECK( q.size() == 10 );
verify_order( q, q.lower_bound(trx6->id()), 10 );
verify_order( q, q.lower_bound(trx15->id()), 5 );
BOOST_REQUIRE( next( q ) == trx6 );
BOOST_CHECK( q.size() == 9 );
verify_order( q, q.lower_bound(trx7->id()), 9 );
BOOST_REQUIRE( next( q ) == trx7 );
BOOST_CHECK( q.size() == 8 );
verify_order( q, q.lower_bound(trx11->id()), 8 );
BOOST_REQUIRE( next( q ) == trx11 );
BOOST_CHECK( q.size() == 7 );
verify_order( q, q.lower_bound(trx12->id()), 7 );
BOOST_REQUIRE_EQUAL( next( q ), trx12 );
BOOST_CHECK( q.size() == 6 );
verify_order( q, q.lower_bound(trx13->id()), 6 );
BOOST_REQUIRE( next( q ) == trx13 );
BOOST_CHECK( q.size() == 5 );
verify_order( q, q.lower_bound(trx15->id()), 5 );
BOOST_REQUIRE( next( q ) == trx15 );
BOOST_CHECK( q.size() == 4 );
verify_order( q, q.lower_bound(trx9->id()), 4 );
BOOST_REQUIRE( next( q ) == trx9 );
BOOST_CHECK( q.size() == 3 );
verify_order( q, q.lower_bound(trx14->id()), 3 );
BOOST_REQUIRE( next( q ) == trx14 );
BOOST_CHECK( q.size() == 2 );
verify_order( q, q.lower_bound(trx18->id()), 2 );
BOOST_REQUIRE( next( q ) == trx18 );
BOOST_CHECK( q.size() == 1 );
verify_order( q, q.lower_bound(trx19->id()), 1 );
BOOST_REQUIRE( next( q ) == trx19 );
BOOST_CHECK( q.size() == 0 );
verify_order( q, q.lower_bound(trx19->id()), 0 );
BOOST_REQUIRE( next( q ) == nullptr );
BOOST_CHECK( q.empty() );

Expand Down

0 comments on commit b767da3

Please sign in to comment.