Katana Plug-in APIs 0.1
Public Member Functions | Protected Member Functions

Foundry::Katana::Util::AttributeKeyedCache< T, PointerT, Base > Class Template Reference

The AttributeKeyedCache is a templated class which takes an Attribute as a key and maps it to an instance of the templated type. More...

#include <AttributeKeyedCache.h>

List of all members.

Public Member Functions

 AttributeKeyedCache (const std::size_t maxNumEntries=0xffffffff, const std::size_t maxNumInvalidKeys=0xffffffff)
IMPLPtr getValue (const FnAttribute::Attribute &iAttr)
void clear ()

Protected Member Functions

virtual IMPLPtr createValue (const FnAttribute::Attribute &iAttr)=0

Detailed Description

template<class T, class PointerT = typename std::shared_ptr<T>, class Base = AttributeKeyedCacheLockFree<T, PointerT>>
class Foundry::Katana::Util::AttributeKeyedCache< T, PointerT, Base >

The AttributeKeyedCache is a templated class which takes an Attribute as a key and maps it to an instance of the templated type.

The cache accepts any Attribute to be used as the key, internally the cache relies upon the attribute's hash. The use of the hash as a key allows for efficient storage even with very large keys such as large CEL statements.

In order to make use of the cache the pure virtual function createValue() must be implemented. This function will be called by the cache in response to client requests for a particular value via getValue(attr). createValue() must return a valid object instance or a null pointer.

The cache is actually composed of two internal caches. The main cache contains mappings for all valid key-value mappings. The 'invalid' cache contains mappings of all invalid hash values. That is, all attribute hash values that return NULL when passed to your createValue() implementation. In the following example, all Attributes whose hash is even will be placed in the main cache, all odd hash values will be stored in the invalid cache.

  class EvenIntegerCache: public
 FnGeolibUtil::AttributeKeyedCache<int64_t>
  {
    public:
      EvenIntegerCache(std::size_t maxNumEntries,
                       std::size_t maxNumInvalidKeys)
        : FnGeolibUtil::AttributeKeyedCache<int64_t>(maxNumEntries,
                                                     maxNumInvalidKeys)
      {}

    private:
      EvenIntegerCache::IMPLPtr
        createValue(const FnAttribute::Attribute &attr)
      {
        // Only return valid value if the attribute's hash is even.
        int64_t hash = attr.getHash().uint64();
        if( hash & 0x1 )
          return EvenIntegerCache::IMPLPtr();
        else
          return EvenIntegerCache::IMPLPtr(new int64_t(hash));
      }
  };

Constructor & Destructor Documentation

template<class T, class PointerT = typename std::shared_ptr<T>, class Base = AttributeKeyedCacheLockFree<T, PointerT>>
Foundry::Katana::Util::AttributeKeyedCache< T, PointerT, Base >::AttributeKeyedCache ( const std::size_t  maxNumEntries = 0xffffffff,
const std::size_t  maxNumInvalidKeys = 0xffffffff 
) [inline]

Construct new instance of the AttributeKeyedCache with specified cache size and invalid cache item size.

Parameters:
maxNumEntriesmaximum size of the main cache, once the number of entries in the cache grows above this value existing entries will be removed.
maxNumInvalidKeysmaximum size of the invalid key set, once the number of entries in the cache grows above this value existing entries will be removed.

Member Function Documentation

template<class T, class PointerT = typename std::shared_ptr<T>, class Base = AttributeKeyedCacheLockFree<T, PointerT>>
void Foundry::Katana::Util::AttributeKeyedCache< T, PointerT, Base >::clear ( ) [inline]

Clears all entries from the cache.

template<class T, class PointerT = typename std::shared_ptr<T>, class Base = AttributeKeyedCacheLockFree<T, PointerT>>
virtual IMPLPtr Foundry::Katana::Util::AttributeKeyedCache< T, PointerT, Base >::createValue ( const FnAttribute::Attribute iAttr) [protected, pure virtual]

Called by getValue() to obtain an instance of the templated type which corresponds to the specified attribute.

Subclasses of AttributeKeyedCache should implement whatever logic necessary to determine the appropriate value to be returned given the supplied attribute. If no value is appropriate NULL should be returned.

Note: The implementation of createValue() WILL be called from multiple threads and therefore, must be thread safe.

Parameters:
iAttrthe attribute which should be used
Returns:
pointer to value corresponding to supplied parameter or NULL.
template<class T, class PointerT = typename std::shared_ptr<T>, class Base = AttributeKeyedCacheLockFree<T, PointerT>>
IMPLPtr Foundry::Katana::Util::AttributeKeyedCache< T, PointerT, Base >::getValue ( const FnAttribute::Attribute iAttr) [inline]

Returns a pointer to the value that corresponds to the specified attribute. Returns NULL if the there is no corresponding value for the specified attribute.

Parameters:
iAttrthe attribute key
Returns:
a pointer to the value associated with the specified attribute key or NULL if no such value exists.

The documentation for this class was generated from the following file:
 All Classes Functions Variables Typedefs Enumerations Enumerator