Equalizer 1.0

instanceCache.h

00001 
00002 /* Copyright (c) 2009-2011, Stefan Eilemann <eile@equalizergraphics.com> 
00003  *
00004  * This library is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU Lesser General Public License version 2.1 as published
00006  * by the Free Software Foundation.
00007  *  
00008  * This library is distributed in the hope that it will be useful, but WITHOUT
00009  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00010  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00011  * details.
00012  * 
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this library; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016  */
00017 
00018 #ifndef CO_INSTANCECACHE_H
00019 #define CO_INSTANCECACHE_H
00020 
00021 #include <co/api.h>
00022 #include <co/types.h>
00023 
00024 #include <co/base/clock.h>     // member
00025 #include <co/base/lockable.h>  // member
00026 #include <co/base/stdExt.h>    // member
00027 #include <co/base/thread.h>    // member
00028 #include <co/base/uuid.h>      // member
00029 
00030 #include <iostream>
00031 
00032 namespace co
00033 {
00035     class InstanceCache
00036     {
00037     public:
00039         CO_API InstanceCache( const uint64_t maxSize = EQ_100MB );
00040 
00042         CO_API ~InstanceCache();
00043 
00053         CO_API bool add( const ObjectVersion& rev, const uint32_t instanceID, 
00054                   Command& command, const uint32_t usage = 0 );
00055 
00057         void remove( const NodeID& node );
00058 
00060         struct Data
00061         {
00062             Data();
00063             CO_API bool operator != ( const Data& rhs ) const;
00064             CO_API bool operator == ( const Data& rhs ) const;
00065 
00066             uint32_t masterInstanceID; 
00067             ObjectDataIStreamDeque versions; 
00068             CO_API static const Data NONE; 
00069         };
00070 
00083         CO_API const Data& operator[]( const base::UUID& id );
00084 
00093         CO_API bool release( const base::UUID& id, const uint32_t count = 1 );
00094 
00103         CO_API bool erase( const base::UUID& id );
00104 
00106         uint64_t getSize() const { return _size; }
00107 
00109         uint64_t getMaxSize() const { return _maxSize; }
00110 
00112         void expire( const int64_t age );
00113 
00114         bool empty( ){ return _items->empty(); }
00115 
00116     private:
00117         struct Item
00118         {
00119             Item();
00120             Data data;
00121             unsigned used;
00122             unsigned access;
00123             NodeID from;
00124 
00125             typedef std::deque< int64_t > TimeDeque;
00126             TimeDeque times;
00127         };
00128 
00129         typedef stde::hash_map< base::uint128_t, Item > ItemHash;
00130         base::Lockable< ItemHash > _items;
00131 
00132         const uint64_t _maxSize; 
00133         uint64_t _size;          
00134 
00135         const base::Clock _clock;  
00136 
00137         void _releaseItems( const uint32_t minUsage );
00138         void _releaseStreams( InstanceCache::Item& item );
00139         void _releaseStreams( InstanceCache::Item& item, 
00140                               const int64_t minTime );
00141         void _releaseFirstStream( InstanceCache::Item& item );
00142         void _deleteStream( ObjectDataIStream* iStream );
00143 
00144         EQ_TS_VAR( _thread );
00145     };
00146 
00147     CO_API std::ostream& operator << ( std::ostream&, const InstanceCache& );
00148 }
00149 #endif //CO_INSTANCECACHE_H
00150 
Generated on Sun May 8 2011 19:11:07 for Equalizer 1.0 by  doxygen 1.7.3