Skip to content

Commit

Permalink
LRU for main table model (#575)
Browse files Browse the repository at this point in the history
* Add generic lrucache structure
* Use LRU cache
* Use LRU cache 2
* Handle color of corrupted messages

Signed-off-by: Viktor Kopp <[email protected]>
  • Loading branch information
vifactor authored Nov 15, 2024
1 parent f9f45ed commit 77c0efc
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 197 deletions.
3 changes: 2 additions & 1 deletion qdlt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ add_library(qdlt SHARED
qdltimporter.cpp
fieldnames.cpp
dltmessagematcher.cpp
dltmessagematcher.h)
dltmessagematcher.h
qdltlrucache.hpp)

target_compile_definitions(qdlt PRIVATE
BYTE_ORDER=LITTLE_ENDIAN
Expand Down
4 changes: 2 additions & 2 deletions qdlt/qdltfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ void QDltFile::addFilterIndex (int index)
}

#ifdef USECOLOR
QColor QDltFile::checkMarker(QDltMsg &msg)
QColor QDltFile::checkMarker(const QDltMsg &msg)
{
if(!filterFlag)
{
Expand All @@ -498,7 +498,7 @@ void QDltFile::addFilterIndex (int index)
}

#else
QString QDltFile::checkMarker(QDltMsg &msg)
QString QDltFile::checkMarker(const QDltMsg &msg)
{
if(!filterFlag)
{
Expand Down
4 changes: 2 additions & 2 deletions qdlt/qdltfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ class QDLT_EXPORT QDltFile : public QDlt
\return 0 if message will not be marked, colour if message will be marked
*/
#ifdef USECOLOR
QColor checkMarker(QDltMsg &msg);
QColor checkMarker(const QDltMsg &msg);
#else
QString checkMarker(QDltMsg &msg);
QString checkMarker(const QDltMsg &msg);
#endif

//! Get file name of the underlying file object
Expand Down
2 changes: 1 addition & 1 deletion qdlt/qdltfilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ bool QDltFilter::compileRegexps()
appidRegularExpression.isValid());
}

bool QDltFilter::match(QDltMsg &msg) const
bool QDltFilter::match(const QDltMsg &msg) const
{

if( (true == enableEcuid) && (msg.getEcuid() != ecuid))
Expand Down
2 changes: 1 addition & 1 deletion qdlt/qdltfilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class QDLT_EXPORT QDltFilter
/*!
\return true if filter matches the message, else false
*/
bool match(QDltMsg &msg) const;
bool match(const QDltMsg &msg) const;

//! Save filter parameters in XML file.
/*!
Expand Down
4 changes: 2 additions & 2 deletions qdlt/qdltfilterlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void QDltFilterList::addFilter(QDltFilter *_filter)


#ifdef USECOLOR
QColor QDltFilterList::checkMarker(QDltMsg &msg)
QColor QDltFilterList::checkMarker(const QDltMsg &msg)
{
QDltFilter *filter;
QColor color;
Expand All @@ -104,7 +104,7 @@ QColor QDltFilterList::checkMarker(QDltMsg &msg)
return color;
}
#else
QString QDltFilterList::checkMarker(QDltMsg &msg)
QString QDltFilterList::checkMarker(const QDltMsg &msg)
{
QDltFilter *filter;
QString color=""; // invalid colour
Expand Down
4 changes: 2 additions & 2 deletions qdlt/qdltfilterlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ class QDLT_EXPORT QDltFilterList
\return 0 if message will not be marked, colour if message will be marked
*/
#ifdef USECOLOR
QColor checkMarker(QDltMsg &msg);
QColor checkMarker(const QDltMsg &msg);
#else
QString checkMarker(QDltMsg &msg);
QString checkMarker(const QDltMsg &msg);
#endif


Expand Down
59 changes: 59 additions & 0 deletions qdlt/qdltlrucache.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#ifndef QDLTLRUCACHE_HPP
#define QDLTLRUCACHE_HPP

#include <unordered_map>
#include <list>
#include <stdexcept>

template<typename Key, typename Value>
class QDltLruCache {

struct CacheEntry {
Key key;
Value value;
};

using CacheListIterator = typename std::list<CacheEntry>::iterator;
public:

QDltLruCache(size_t capacity) :
m_capacity(capacity) {
}

void put(const Key& key, const Value& value) {
auto it = m_keyIteratorsMap.find(key);
m_cacheItems.push_front(CacheEntry{key, value});
if (it != m_keyIteratorsMap.end()) {
m_cacheItems.erase(it->second);
m_keyIteratorsMap.erase(it);
}
m_keyIteratorsMap[key] = m_cacheItems.begin();

if (m_keyIteratorsMap.size() > m_capacity) {
auto last = std::prev(m_cacheItems.end());
m_keyIteratorsMap.erase(last->key);
m_cacheItems.pop_back();
}
}

const Value& get(const Key& key) {
auto it = m_keyIteratorsMap.find(key);
if (it == m_keyIteratorsMap.end()) {
throw std::range_error("no such key in cache found");
}

m_cacheItems.splice(m_cacheItems.begin(), m_cacheItems, it->second);
return it->second->value;
}

bool exists(const Key& key) const {
return m_keyIteratorsMap.find(key) != m_keyIteratorsMap.end();
}

private:
std::list<CacheEntry> m_cacheItems;
std::unordered_map<Key, CacheListIterator> m_keyIteratorsMap;
const size_t m_capacity;
};

#endif // QDLTLRUCACHE_HPP
Loading

0 comments on commit 77c0efc

Please sign in to comment.