summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSascha Volkenandt <sascha (at) akv-soft (dot) de>2007-05-31 16:23:54 +0000
committerSascha Volkenandt <sascha (at) akv-soft (dot) de>2007-05-31 16:23:54 +0000
commit9fa0576e38559ebe7ff63a8d1161123b049a933b (patch)
tree276c6b65a024f7bcb7bd72b8fb623ae135b0091a
parent98e68abaf03928f29dd3fa1252d20ceb3b08144a (diff)
downloadvdr-plugin-live-9fa0576e38559ebe7ff63a8d1161123b049a933b.tar.gz
vdr-plugin-live-9fa0576e38559ebe7ff63a8d1161123b049a933b.tar.bz2
- some optimisations
- added dropping items out of cache
-rwxr-xr-xcache.h63
-rw-r--r--filecache.cpp14
-rw-r--r--filecache.h6
3 files changed, 57 insertions, 26 deletions
diff --git a/cache.h b/cache.h
index b34c1c2..0b81cf5 100755
--- a/cache.h
+++ b/cache.h
@@ -1,6 +1,7 @@
#ifndef VGSTOOLS_CACHE_H
#define VGSTOOLS_CACHE_H
+#include <cassert>
#include <algorithm>
#include <ctime>
#include <list>
@@ -22,22 +23,24 @@ class cache
{
public:
typedef TKey key_type;
- typedef TValue value_type;
- typedef std::tr1::shared_ptr< value_type > ptr_type;
+ typedef TValue mapped_type;
+ typedef std::tr1::shared_ptr< mapped_type > ptr_type;
private:
- struct Value
+ /*struct Value
{
- ptr_type value;
- std::time_t creation;
+ key_type key_;
+ ptr_type value_;
- Value( key_type const& key )
- : value( new value_type( key ) )
- , creation( 0 ) {}
- };
+ Value( key_type const& key, ptr_type const& value )
+ : key_( key )
+ , value_( value ) {}
+ };*/
- typedef std::list< Value > ValueList;
- typedef std::map< TKey, typename ValueList::iterator > KeyMap;
+ typedef std::pair< key_type, ptr_type > value_type;
+
+ typedef std::list< value_type > ValueList;
+ typedef std::map< key_type, typename ValueList::iterator > KeyMap;
public:
cache( size_t maxWeight )
@@ -49,6 +52,43 @@ public:
ptr_type get( key_type const& key )
{
+ assert( m_lookup.size() == m_values.size() );
+
+ typename KeyMap::iterator it = m_lookup.find( key );
+ ptr_type result = it != m_lookup.end() ? it->second->second : ptr_type( new mapped_type( key ) );
+
+ if ( it != m_lookup.end() ) {
+ if ( result->is_current() ) {
+ if ( it->second != m_values.begin() ) {
+ m_values.erase( it->second );
+ it->second = m_values.insert( m_values.begin(), std::make_pair( key, result ) );
+ }
+ return result;
+ }
+
+ m_currentWeight -= result->weight();
+ m_values.erase( it->second );
+ m_lookup.erase( it );
+ }
+
+ if ( !result->load() )
+ return ptr_type();
+
+ // put new object into cache
+ if ( result->weight() < m_maxWeight ) {
+ m_currentWeight += result->weight();
+ m_lookup.insert( std::make_pair( key, m_values.insert( m_values.begin(), std::make_pair( key, result ) ) ) );
+ while ( m_currentWeight > m_maxWeight ) {
+ value_type& value = m_values.back();
+ m_currentWeight -= value.second->weight();
+ m_lookup.erase( m_lookup.find( value.first ) );
+ m_values.pop_back();
+ }
+ }
+
+ return result;
+#if 0
+
typename KeyMap::iterator it = m_lookup.find( key );
if ( it == m_lookup.end() ) {
typename ValueList::iterator element = m_values.insert( m_values.begin(), Value( key ) );
@@ -75,6 +115,7 @@ public:
value = &*element;
}
return value->value;
+#endif
}
private:
diff --git a/filecache.cpp b/filecache.cpp
index 9a21647..37a1e85 100644
--- a/filecache.cpp
+++ b/filecache.cpp
@@ -10,22 +10,10 @@ std::time_t FileObject::get_filetime( std::string const& path )
{
struct stat sbuf;
if ( stat( path.c_str(), &sbuf ) < 0 )
- return 0; // XXX
+ return 0;
return sbuf.st_ctime;
}
-bool FileObject::is_current() const
-{
- return m_ctime < get_filetime( m_path );
- /*struct stat statBuf = { 0 };
- * if ( ::stat( m_path.c_str(), &statBuf ) < 0 ) {
- * }
- if ( m_ctime == 0 )
- return true;
- m_ctime = get_filetime( m_path );
- return timestamp < m_ctime;*/
-}
-
bool FileObject::load()
{
std::ifstream ifs( m_path.c_str(), std::ios::in | std::ios::binary | std::ios::ate );
diff --git a/filecache.h b/filecache.h
index 0556616..89d884f 100644
--- a/filecache.h
+++ b/filecache.h
@@ -1,6 +1,7 @@
#ifndef VDR_LIVE_FILECACHE_H
#define VDR_LIVE_FILECACHE_H
+#include <numeric>
#include <string>
#include <vector>
#include <vdr/tools.h>
@@ -12,12 +13,12 @@ class FileObject
{
public:
FileObject( std::string const& path )
- : m_ctime( 0 )
+ : m_ctime( std::numeric_limits< std::time_t >::max() )
, m_path( path ) {}
std::size_t size() const { return m_data.size(); }
std::size_t weight() const { return size(); }
- bool is_current() const;
+ bool is_current() const { return m_ctime == get_filetime( m_path ); }
bool load();
char const* data() const { return &m_data[0]; }
std::time_t ctime() const { return m_ctime; }
@@ -33,6 +34,7 @@ private:
class FileCache: public vgstools::cache< std::string, FileObject >
{
typedef vgstools::cache< std::string, FileObject > base_type;
+
public:
FileCache( size_t maxWeight ): base_type( maxWeight ) {}