Skip to content

Commit

Permalink
Consolidate filteredEventKindsCache_ into filteredItemsCache_
Browse files Browse the repository at this point in the history
The filteredEventKindsCache_ held the kind-string, which is simply moved
to the FilteredItem struct.
  • Loading branch information
gin-ahirsch committed Mar 4, 2019
1 parent 70538f4 commit 9fabada
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
42 changes: 23 additions & 19 deletions src/data/logfiltereddata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <QString>
#include <cassert>
#include <limits>
#include <utility>

#include "utils.h"
#include "logdata.h"
Expand Down Expand Up @@ -180,7 +181,7 @@ void LogFilteredData::addMark( qint64 line, QChar mark )
int index = marks_.addMark( line, mark );
maxLengthMarks_ = qMax( maxLengthMarks_,
sourceLogData_->getLineLength( line ) );
insertIntoFilteredItemsCache( index, FilteredItem{ static_cast<LineNumber>( line ), Mark } );
insertIntoFilteredItemsCache( index, { static_cast<LineNumber>( line ), Mark } );
}
else
LOG(logERROR) << "LogFilteredData::addMark\
Expand Down Expand Up @@ -237,15 +238,15 @@ void LogFilteredData::deleteMark( QChar mark )
}

updateMaxLengthMarks( line );
removeFromFilteredItemsCache( index, FilteredItem{ static_cast<LineNumber>( line ), Mark } );
removeFromFilteredItemsCache( index, { static_cast<LineNumber>( line ), Mark } );
}

void LogFilteredData::deleteMark( qint64 line )
{
int index = marks_.deleteMark( line );

updateMaxLengthMarks( line );
removeFromFilteredItemsCache( index, FilteredItem{ static_cast<LineNumber>( line ), Mark } );
removeFromFilteredItemsCache( index, { static_cast<LineNumber>( line ), Mark } );
}

void LogFilteredData::updateMaxLengthMarks( qint64 removed_line )
Expand Down Expand Up @@ -479,13 +480,13 @@ void LogFilteredData::regenerateFilteredItemsCache() const
line = next_match;
++i;
}
filteredItemsCache_.push_back( FilteredItem( line, type ) );
filteredItemsCache_.emplace_back( line, type );
}

LOG(logDEBUG) << "finished regenerateFilteredItemsCache";
}

void LogFilteredData::insertIntoFilteredItemsCache( size_t insert_index, FilteredItem item )
void LogFilteredData::insertIntoFilteredItemsCache( size_t insert_index, FilteredItem &&item )
{
using std::begin;
using std::end;
Expand All @@ -501,16 +502,16 @@ void LogFilteredData::insertIntoFilteredItemsCache( size_t insert_index, Filtere
// We can start the search from insert_index, since lineNumber >= index is always true.
auto found = std::lower_bound( next( begin( filteredItemsCache_ ), insert_index ), end( filteredItemsCache_ ), item );
if ( found == end( filteredItemsCache_ ) || found->lineNumber() > item.lineNumber() ) {
filteredItemsCache_.insert( found, item );
filteredItemsCache_.emplace( found, std::move( item ) );
} else {
assert( found->lineNumber() == item.lineNumber() );
Q_ASSERT( found->lineNumber() == item.lineNumber() );
found->add( item.type() );
}
}

void LogFilteredData::insertIntoFilteredItemsCache( FilteredItem item )
void LogFilteredData::insertIntoFilteredItemsCache( FilteredItem &&item )
{
insertIntoFilteredItemsCache( 0, item );
return insertIntoFilteredItemsCache( 0, std::move( item ) );
}

void LogFilteredData::insertMatchesIntoFilteredItemsCache( size_t start_index )
Expand All @@ -537,21 +538,19 @@ void LogFilteredData::insertMatchesIntoFilteredItemsCache( size_t start_index )
// We can start the search from insert_index, since lineNumber >= index is always true.
auto filteredIt = next( begin( filteredItemsCache_ ), start_index );
for ( auto matchesIt = next( begin( matching_lines_ ), start_index ); matchesIt != end( matching_lines_ ); ++matchesIt ) {
FilteredItem item{ matchesIt->lineNumber(), Match };
filteredIt = std::lower_bound( filteredIt, end( filteredItemsCache_ ), item );
if ( filteredIt == end( filteredItemsCache_ ) || filteredIt->lineNumber() > item.lineNumber() ) {
filteredIt = filteredItemsCache_.insert( filteredIt, item );
filteredIt = std::lower_bound( filteredIt, end( filteredItemsCache_ ), matchesIt->lineNumber() );
if ( filteredIt == end( filteredItemsCache_ ) || filteredIt->lineNumber() > matchesIt->lineNumber() ) {
filteredIt = filteredItemsCache_.emplace( filteredIt, matchesIt->lineNumber(), Match );
} else {
assert( filteredIt->lineNumber() == matchesIt->lineNumber() );
filteredIt->add( item.type() );
filteredIt->add( Match );
}
}
}

void LogFilteredData::removeFromFilteredItemsCache( size_t remove_index, FilteredItem item )
void LogFilteredData::removeFromFilteredItemsCache( size_t remove_index, const FilteredItem &item )
{
using std::begin;
using std::distance;
using std::end;
using std::next;

Expand All @@ -565,21 +564,26 @@ void LogFilteredData::removeFromFilteredItemsCache( size_t remove_index, Filtere
// Search for the corresponding index.
// We can start the search from remove_index, since lineNumber >= index is always true.
auto found = std::equal_range( next( begin( filteredItemsCache_ ), remove_index ), end( filteredItemsCache_ ), item );
if( found.first == end( filteredItemsCache_ ) ) {
if( found.first == found.second ) {
LOG(logERROR) << "Attempt to remove line " << item.lineNumber() << " from filteredItemsCache_ failed, since it was not found";
return;
}

if ( distance( found.first, found.second ) > 1 ) {
if ( next( found.first ) != found.second ) {
LOG(logERROR) << "Multiple matches found for line " << item.lineNumber() << " in filteredItemsCache_";
// FIXME: collapse them?
}

if ( !found.first->remove( item.type() ) ){
if ( !found.first->remove( item.type() ) ) {
filteredItemsCache_.erase( found.first );
}
}

void LogFilteredData::removeFromFilteredItemsCache( const FilteredItem &item )
{
removeFromFilteredItemsCache( 0, item );
}

void LogFilteredData::removeAllFromFilteredItemsCache( FilteredLineType type )
{
using std::begin;
Expand Down
15 changes: 6 additions & 9 deletions src/data/logfiltereddata.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,14 @@ class LogFilteredData : public AbstractLogData {
void regenerateFilteredItemsCache() const;
// start_index can be passed in as an optimization when finding the item.
// It refers to the index of the singular arrays (Marks or SearchResultArray) where the item was inserted.
void insertIntoFilteredItemsCache( size_t start_index, FilteredItem item );
void insertIntoFilteredItemsCache( FilteredItem item );
void insertIntoFilteredItemsCache( size_t start_index, FilteredItem &&item );
void insertIntoFilteredItemsCache( FilteredItem &&item );
// Insert entries from matching_lines_ into filteredItemsCache_ starting by start_index.
void insertMatchesIntoFilteredItemsCache( size_t start_index );
// remove_index can be passed in as an optimization when finding the item.
// It refers to the index of the singular arrays (Marks or SearchResultArray) where the item was removed.
void removeFromFilteredItemsCache( size_t remove_index, FilteredItem item );
void removeFromFilteredItemsCache( FilteredItem item );
void removeFromFilteredItemsCache( size_t remove_index, const FilteredItem &item );
void removeFromFilteredItemsCache( const FilteredItem &item );
void removeAllFromFilteredItemsCache( FilteredLineType type );

// update maxLengthMarks_ when a Mark was removed.
Expand Down Expand Up @@ -206,11 +206,8 @@ inline LogFilteredData::FilteredLineType operator~(LogFilteredData::FilteredLine
// of pointer (less small allocations and no RTTI).
class LogFilteredData::FilteredItem {
public:
// A default ctor seems to be necessary for QVector
FilteredItem()
{ lineNumber_ = 0; }
FilteredItem( LineNumber lineNumber, FilteredLineType type )
{ lineNumber_ = lineNumber; type_ = type; }
: lineNumber_{ lineNumber }, type_{ type } {}

LineNumber lineNumber() const
{ return lineNumber_; }
Expand All @@ -225,7 +222,7 @@ class LogFilteredData::FilteredItem {
{ return type_ &= ~type; }

bool operator <( const LogFilteredData::FilteredItem& other ) const
{ return lineNumber_ < other.lineNumber_; }
{ return *this < other.lineNumber_; }

bool operator <( const LineNumber& lineNumber ) const
{ return lineNumber_ < lineNumber; }
Expand Down

0 comments on commit 9fabada

Please sign in to comment.