Make Cache/Storage const correct

- From the outside, all actions getting data from the cache/storage
  are const.
- Update documentation.
This commit is contained in:
Johan Wikman
2016-12-13 14:04:19 +02:00
parent e5cd9ba47e
commit 9f08e1300f
15 changed files with 200 additions and 99 deletions

View File

@ -83,12 +83,28 @@ public:
*/
virtual void refreshed(const CACHE_KEY& key, const CacheFilterSession* pSession) = 0;
virtual cache_result_t get_key(const char* zDefaultDb, const GWBUF* pQuery, CACHE_KEY* pKey) = 0;
/**
* See @Storage::get_key
*/
virtual cache_result_t get_key(const char* zDefaultDb,
const GWBUF* pQuery,
CACHE_KEY* pKey) const = 0;
virtual cache_result_t get_value(const CACHE_KEY& key, uint32_t flags, GWBUF** ppValue) = 0;
/**
* See @Storage::get_value
*/
virtual cache_result_t get_value(const CACHE_KEY& key,
uint32_t flags,
GWBUF** ppValue) const = 0;
/**
* See @Storage::put_value
*/
virtual cache_result_t put_value(const CACHE_KEY& key, const GWBUF* pValue) = 0;
/**
* See @Storage::del_value
*/
virtual cache_result_t del_value(const CACHE_KEY& key) = 0;
protected:

View File

@ -102,6 +102,7 @@ typedef struct cache_storage_api
*
* @param capabilities On successful return, contains a bitmask of
* cache_storage_capabilities_t values.
*
* @return True if the initialization succeeded, false otherwise.
*/
bool (*initialize)(uint32_t* capabilities);
@ -185,6 +186,7 @@ typedef struct cache_storage_api
* @param flags Mask of cache_flags_t values.
* @param result Pointer to variable that after a successful return will
* point to a GWBUF.
*
* @return CACHE_RESULT_OK if item was found,
* CACHE_RESULT_STALE if CACHE_FLAGS_INCLUDE_STALE was specified in
* flags and the item was found but stale,
@ -203,6 +205,7 @@ typedef struct cache_storage_api
* @param key A key generated with get_key.
* @param value Pointer to GWBUF containing the value to be stored.
* Must be one contiguous buffer.
*
* @return CACHE_RESULT_OK if item was successfully put,
* CACHE_RESULT_OUT_OF_RESOURCES if item could not be put, due to
* some resource having become exhausted, or some other error code.
@ -216,6 +219,7 @@ typedef struct cache_storage_api
*
* @param storage Pointer to a CACHE_STORAGE.
* @param key A key generated with get_key.
*
* @return CACHE_RESULT_OK if item was successfully deleted. Note that
* CACHE_RESULT_OK may be returned also if the entry was not present.
*/

View File

@ -122,12 +122,12 @@ json_t* CachePT::get_info(uint32_t what) const
return pInfo;
}
cache_result_t CachePT::get_key(const char* zDefaultDb, const GWBUF* pQuery, CACHE_KEY* pKey)
cache_result_t CachePT::get_key(const char* zDefaultDb, const GWBUF* pQuery, CACHE_KEY* pKey) const
{
return thread_cache().get_key(zDefaultDb, pQuery, pKey);
}
cache_result_t CachePT::get_value(const CACHE_KEY& key, uint32_t flags, GWBUF** ppValue)
cache_result_t CachePT::get_value(const CACHE_KEY& key, uint32_t flags, GWBUF** ppValue) const
{
return thread_cache().get_value(key, flags, ppValue);
}

View File

@ -30,9 +30,9 @@ public:
json_t* get_info(uint32_t what) const;
cache_result_t get_key(const char* zDefaultDb, const GWBUF* pQuery, CACHE_KEY* pKey);
cache_result_t get_key(const char* zDefaultDb, const GWBUF* pQuery, CACHE_KEY* pKey) const;
cache_result_t get_value(const CACHE_KEY& key, uint32_t flags, GWBUF** ppValue);
cache_result_t get_value(const CACHE_KEY& key, uint32_t flags, GWBUF** ppValue) const;
cache_result_t put_value(const CACHE_KEY& key, const GWBUF* pValue);
@ -55,6 +55,11 @@ private:
Cache& thread_cache();
const Cache& thread_cache() const
{
return const_cast<CachePT*>(this)->thread_cache();
}
private:
CachePT(const Cache&);
CachePT& operator = (const CachePT&);

View File

@ -52,14 +52,14 @@ bool CacheSimple::Create(const CACHE_CONFIG& config,
cache_result_t CacheSimple::get_key(const char* zDefaultDb,
const GWBUF* pQuery,
CACHE_KEY* pKey)
CACHE_KEY* pKey) const
{
return m_pStorage->get_key(zDefaultDb, pQuery, pKey);
}
cache_result_t CacheSimple::get_value(const CACHE_KEY& key,
uint32_t flags,
GWBUF** ppValue)
GWBUF** ppValue) const
{
return m_pStorage->get_value(key, flags, ppValue);
}

View File

@ -25,9 +25,9 @@ class CacheSimple : public Cache
public:
~CacheSimple();
cache_result_t get_key(const char* zDefaultDb, const GWBUF* pQuery, CACHE_KEY* pKey);
cache_result_t get_key(const char* zDefaultDb, const GWBUF* pQuery, CACHE_KEY* pKey) const;
cache_result_t get_value(const CACHE_KEY& key, uint32_t flags, GWBUF** ppValue);
cache_result_t get_value(const CACHE_KEY& key, uint32_t flags, GWBUF** ppValue) const;
cache_result_t put_value(const CACHE_KEY& key, const GWBUF* pValue);

View File

@ -29,7 +29,7 @@ LRUStorage::~LRUStorage()
cache_result_t LRUStorage::get_key(const char* zdefault_db,
const GWBUF* pquery,
CACHE_KEY* pkey)
CACHE_KEY* pkey) const
{
return pstorage_->get_key(zdefault_db, pquery, pkey);
}
@ -65,7 +65,7 @@ cache_result_t LRUStorage::do_get_info(uint32_t what,
cache_result_t LRUStorage::do_get_value(const CACHE_KEY& key,
uint32_t flags,
GWBUF** ppvalue)
GWBUF** ppvalue) const
{
NodesPerKey::iterator i = nodes_per_key_.find(key);
bool existed = (i != nodes_per_key_.end());
@ -121,8 +121,7 @@ cache_result_t LRUStorage::do_get_value(const CACHE_KEY& key,
return result;
}
cache_result_t LRUStorage::do_put_value(const CACHE_KEY& key,
const GWBUF* pvalue)
cache_result_t LRUStorage::do_put_value(const CACHE_KEY& key, const GWBUF* pvalue)
{
cache_result_t result = CACHE_RESULT_ERROR;
@ -264,8 +263,7 @@ cache_result_t LRUStorage::do_del_value(const CACHE_KEY& key)
return result;
}
cache_result_t LRUStorage::do_get_head(CACHE_KEY* pKey,
GWBUF** ppValue)
cache_result_t LRUStorage::do_get_head(CACHE_KEY* pKey, GWBUF** ppValue) const
{
cache_result_t result = CACHE_RESULT_NOT_FOUND;
@ -285,8 +283,7 @@ cache_result_t LRUStorage::do_get_head(CACHE_KEY* pKey,
return result;
}
cache_result_t LRUStorage::do_get_tail(CACHE_KEY* pKey,
GWBUF** ppValue)
cache_result_t LRUStorage::do_get_tail(CACHE_KEY* pKey, GWBUF** ppValue) const
{
cache_result_t result = CACHE_RESULT_NOT_FOUND;

View File

@ -28,73 +28,52 @@ public:
*/
cache_result_t get_key(const char* zDefaultDb,
const GWBUF* pQuery,
CACHE_KEY* pKey);
CACHE_KEY* pKey) const;
protected:
LRUStorage(Storage* pstorage, uint64_t max_count, uint64_t max_size);
/**
* Returns information about the LRU storage and the underlying real
* storage.
*
* @see Storage::get_info
*/
cache_result_t do_get_info(uint32_t what, json_t** ppInfo) const;
/**
* Fetches the value from the underlying storage and, if found, moves the
* entry to the top of the LRU list.
*
* @see Storage::get_value
*/
cache_result_t do_get_value(const CACHE_KEY& key,
uint32_t flags,
GWBUF** ppValue);
GWBUF** ppValue) const;
/**
* Stores the value to the underlying storage and, if successful, either
* places the entry at or moves the existing entry to the top of the LRU
* list.
*
* @see Storage::put_value
*/
cache_result_t do_put_value(const CACHE_KEY& key,
const GWBUF* pValue);
/**
* Deletes the value from the underlying storage and, if successful, removes
* the entry from the LRU list.
*
* @see Storage::del_value
*/
cache_result_t do_del_value(const CACHE_KEY& key);
/**
* Returns the head item.
*
* @see Storage::get_head
*/
cache_result_t do_get_head(CACHE_KEY* pKey,
GWBUF** ppValue);
GWBUF** ppValue) const;
/**
* Returns the tail item.
*
* @see Storage::get_tail
*/
cache_result_t do_get_tail(CACHE_KEY* pKey,
GWBUF** ppValue);
GWBUF** ppValue) const;
/**
* Returns the size of the storage.
*
* @see Storage::getSize
*/
cache_result_t do_get_size(uint64_t* pSize) const;
/**
* Returns the number of items in the storage.
*
* @see Storage::getItems
*/
cache_result_t do_get_items(uint64_t* pItems) const;
@ -222,11 +201,11 @@ private:
uint64_t evictions; /*< How many times an item has been evicted from the cache. */
};
Storage* pstorage_; /*< The actual storage. */
uint64_t max_count_; /*< The maximum number of items in the LRU list, */
uint64_t max_size_; /*< The maximum size of all cached items. */
Stats stats_; /*< Cache statistics. */
NodesPerKey nodes_per_key_; /*< Mapping from cache keys to corresponding Node. */
Node* phead_; /*< The node at the LRU list. */
Node* ptail_; /*< The node at bottom of the LRU list.*/
Storage* pstorage_; /*< The actual storage. */
uint64_t max_count_; /*< The maximum number of items in the LRU list, */
uint64_t max_size_; /*< The maximum size of all cached items. */
mutable Stats stats_; /*< Cache statistics. */
mutable NodesPerKey nodes_per_key_; /*< Mapping from cache keys to corresponding Node. */
mutable Node* phead_; /*< The node at the LRU list. */
mutable Node* ptail_; /*< The node at bottom of the LRU list.*/
};

View File

@ -47,15 +47,14 @@ cache_result_t LRUStorageMT::get_info(uint32_t what,
cache_result_t LRUStorageMT::get_value(const CACHE_KEY& key,
uint32_t flags,
GWBUF** ppvalue)
GWBUF** ppvalue) const
{
SpinLockGuard guard(lock_);
return do_get_value(key, flags, ppvalue);
}
cache_result_t LRUStorageMT::put_value(const CACHE_KEY& key,
const GWBUF* pvalue)
cache_result_t LRUStorageMT::put_value(const CACHE_KEY& key, const GWBUF* pvalue)
{
SpinLockGuard guard(lock_);
@ -69,20 +68,18 @@ cache_result_t LRUStorageMT::del_value(const CACHE_KEY& key)
return do_del_value(key);
}
cache_result_t LRUStorageMT::get_head(CACHE_KEY* pKey,
GWBUF** ppValue)
cache_result_t LRUStorageMT::get_head(CACHE_KEY* pKey, GWBUF** ppHead) const
{
SpinLockGuard guard(lock_);
return LRUStorage::do_get_head(pKey, ppValue);
return LRUStorage::do_get_head(pKey, ppHead);
}
cache_result_t LRUStorageMT::get_tail(CACHE_KEY* pKey,
GWBUF** ppValue)
cache_result_t LRUStorageMT::get_tail(CACHE_KEY* pKey, GWBUF** ppTail) const
{
SpinLockGuard guard(lock_);
return LRUStorage::do_get_tail(pKey, ppValue);
return LRUStorage::do_get_tail(pKey, ppTail);
}
cache_result_t LRUStorageMT::get_size(uint64_t* pSize) const

View File

@ -28,7 +28,7 @@ public:
cache_result_t get_value(const CACHE_KEY& key,
uint32_t flags,
GWBUF** ppvalue);
GWBUF** ppvalue) const;
cache_result_t put_value(const CACHE_KEY& key,
const GWBUF* pvalue);
@ -36,10 +36,10 @@ public:
cache_result_t del_value(const CACHE_KEY& key);
cache_result_t get_head(CACHE_KEY* pKey,
GWBUF** ppValue);
GWBUF** ppValue) const;
cache_result_t get_tail(CACHE_KEY* pKey,
GWBUF** ppValue);
GWBUF** ppValue) const;
cache_result_t get_size(uint64_t* pSize) const;

View File

@ -41,13 +41,12 @@ cache_result_t LRUStorageST::get_info(uint32_t what,
cache_result_t LRUStorageST::get_value(const CACHE_KEY& key,
uint32_t flags,
GWBUF** ppvalue)
GWBUF** ppvalue) const
{
return LRUStorage::do_get_value(key, flags, ppvalue);
}
cache_result_t LRUStorageST::put_value(const CACHE_KEY& key,
const GWBUF* pvalue)
cache_result_t LRUStorageST::put_value(const CACHE_KEY& key, const GWBUF* pvalue)
{
return LRUStorage::do_put_value(key, pvalue);
}
@ -57,14 +56,12 @@ cache_result_t LRUStorageST::del_value(const CACHE_KEY& key)
return LRUStorage::do_del_value(key);
}
cache_result_t LRUStorageST::get_head(CACHE_KEY* pKey,
GWBUF** ppValue)
cache_result_t LRUStorageST::get_head(CACHE_KEY* pKey, GWBUF** ppValue) const
{
return LRUStorage::do_get_head(pKey, ppValue);
}
cache_result_t LRUStorageST::get_tail(CACHE_KEY* pKey,
GWBUF** ppValue)
cache_result_t LRUStorageST::get_tail(CACHE_KEY* pKey, GWBUF** ppValue) const
{
return LRUStorage::do_get_tail(pKey, ppValue);
}

View File

@ -27,7 +27,7 @@ public:
cache_result_t get_value(const CACHE_KEY& key,
uint32_t flags,
GWBUF** ppValue);
GWBUF** ppValue) const;
cache_result_t put_value(const CACHE_KEY& key,
const GWBUF* pValue);
@ -35,10 +35,10 @@ public:
cache_result_t del_value(const CACHE_KEY& key);
cache_result_t get_head(CACHE_KEY* pKey,
GWBUF** ppValue);
GWBUF** ppValue) const;
cache_result_t get_tail(CACHE_KEY* pKey,
GWBUF** ppValue);
GWBUF** ppValue) const;
cache_result_t get_size(uint64_t* pSize) const;

View File

@ -25,30 +25,140 @@ public:
virtual ~Storage();
virtual cache_result_t get_info(uint32_t what,
json_t** ppInfo) const = 0;
/**
* Returns information about the storage.
*
* @param what Bitmask of cache_storage_info_t values.
* @param info Upon successful return points to json_t object containing
* information. The caller should call @c json_decref on the
* object when it is no longer needed.
*
* @return CACHE_RESULT_OK if a json object could be created.
*/
virtual cache_result_t get_info(uint32_t what, json_t** ppInfo) const = 0;
/**
* Create a key for a GWBUF.
*
* @param zDefaultDb The default DB or NULL.
* @param query An SQL query. Must be one contiguous buffer.
* @param pKey Pointer to object where key will be stored.
*
* @return CACHE_RESULT_OK if a key was created, otherwise some error code.
*/
virtual cache_result_t get_key(const char* zDefaultDb,
const GWBUF* pQuery,
CACHE_KEY* pKey) = 0;
CACHE_KEY* pKey) const = 0;
/**
* Get a value from the cache.
*
* @param key A key generated with get_key.
* @param flags Mask of cache_flags_t values.
* @param ppValue Pointer to variable that after a successful return will
* point to a GWBUF.
*
* @return CACHE_RESULT_OK if item was found,
* CACHE_RESULT_STALE if CACHE_FLAGS_INCLUDE_STALE was specified in
* flags and the item was found but stale,
* CACHE_RESULT_NOT_FOUND if item was not found (which may be because
* the ttl was reached), or some other error code.
*/
virtual cache_result_t get_value(const CACHE_KEY& key,
uint32_t flags,
GWBUF** ppValue) = 0;
GWBUF** ppValue) const = 0;
virtual cache_result_t put_value(const CACHE_KEY& key,
const GWBUF* pValue) = 0;
/**
* Put a value to the cache.
*
* @param key A key generated with get_key.
* @param pValue Pointer to GWBUF containing the value to be stored.
* Must be one contiguous buffer.
* @return CACHE_RESULT_OK if item was successfully put,
* CACHE_RESULT_OUT_OF_RESOURCES if item could not be put, due to
* some resource having become exhausted, or some other error code.
*/
virtual cache_result_t put_value(const CACHE_KEY& key, const GWBUF* pValue) = 0;
/**
* Delete a value from the cache.
*
* @param storage Pointer to a CACHE_STORAGE.
* @param key A key generated with get_key.
*
* @return CACHE_RESULT_OK if item was successfully deleted. Note that
* CACHE_RESULT_OK may be returned also if the entry was not present.
*/
virtual cache_result_t del_value(const CACHE_KEY& key) = 0;
virtual cache_result_t get_head(CACHE_KEY* pKey,
GWBUF** ppValue) = 0;
/**
* Get the head item from the storage. This is only intended for testing and
* debugging purposes and if the storage is being used by different threads
* at the same time, the returned result may be incorrect the moment it has
* been returned.
*
* @param key Pointer to variable that after a successful return will
* contain the key.
* @param ppHead Pointer to variable that after a successful return will
* point to a GWBUF.
*
* @return CACHE_RESULT_OK if the head item was returned,
* CACHE_RESULT_NOT_FOUND if the cache is empty,
* CACHE_RESULT_OUT_OF_RESOURCES if the storage is incapable of
* returning the head, and
* CACHE_RESULT_ERROR otherwise.
*/
virtual cache_result_t get_head(CACHE_KEY* pKey, GWBUF** ppHead) const = 0;
virtual cache_result_t get_tail(CACHE_KEY* pKey,
GWBUF** ppValue) = 0;
/**
* Get the tail item from the cache. This is only intended for testing and
* debugging purposes and if the storage is being used by different threads
* at the same time, the returned result may become incorrect the moment it
* has been returned.
*
* @param key Pointer to variable that after a successful return will
* contain the key.
* @param ppTail Pointer to variable that after a successful return will
* point to a GWBUF.
*
* @return CACHE_RESULT_OK if the head item was returned,
* CACHE_RESULT_NOT_FOUND if the cache is empty,
* CACHE_RESULT_OUT_OF_RESOURCES if the storage is incapable of
* returning the tail, and
* CACHE_RESULT_ERROR otherwise.
*/
virtual cache_result_t get_tail(CACHE_KEY* pKey, GWBUF** ppTail) const = 0;
/**
* Get the current size of the storage. This is only intended for testing and
* debugging purposes and if the storage is being used by different threads
* at the same time, the returned result may become incorrect the moment it
* has been returned.
*
* @param pSize Pointer to variable that after a successful return will
* contain the current size of the storage.
*
* @return CACHE_RESULT_OK if the size was returned,
* CACHE_RESULT_OUT_OF_RESOURCES if the storage
* is incapable of returning the size, and
* CACHE_RESULT_ERROR otherwise.
*/
virtual cache_result_t get_size(uint64_t* pSize) const = 0;
/**
* Get the current number of items in the storage. This is only intended for
* testing and debugging purposes and if the storage is being used by different
* threads at the same time, the returned result may become incorrect the moment
* it has been returned.
*
* @param pItems Pointer to variable that after a successful return will
* contain the current number of items in the storage.
*
* @return CACHE_RESULT_OK if the size was returned,
* CACHE_RESULT_OUT_OF_RESOURCES if the storage
* is incapable of returning the size, and
* CACHE_RESULT_ERROR otherwise.
*/
virtual cache_result_t get_items(uint64_t* pItems) const = 0;
protected:

View File

@ -27,28 +27,26 @@ StorageReal::~StorageReal()
{
}
cache_result_t StorageReal::get_info(uint32_t flags,
json_t** ppInfo) const
cache_result_t StorageReal::get_info(uint32_t flags, json_t** ppInfo) const
{
return m_pApi->getInfo(m_pStorage, flags, ppInfo);
}
cache_result_t StorageReal::get_key(const char* zDefaultDb,
const GWBUF* pQuery,
CACHE_KEY* pKey)
CACHE_KEY* pKey) const
{
return m_pApi->getKey(m_pStorage, zDefaultDb, pQuery, pKey);
}
cache_result_t StorageReal::get_value(const CACHE_KEY& key,
uint32_t flags,
GWBUF** ppValue)
GWBUF** ppValue) const
{
return m_pApi->getValue(m_pStorage, &key, flags, ppValue);
}
cache_result_t StorageReal::put_value(const CACHE_KEY& key,
const GWBUF* pValue)
cache_result_t StorageReal::put_value(const CACHE_KEY& key, const GWBUF* pValue)
{
return m_pApi->putValue(m_pStorage, &key, pValue);
}
@ -58,16 +56,14 @@ cache_result_t StorageReal::del_value(const CACHE_KEY& key)
return m_pApi->delValue(m_pStorage, &key);
}
cache_result_t StorageReal::get_head(CACHE_KEY* pKey,
GWBUF** ppValue)
cache_result_t StorageReal::get_head(CACHE_KEY* pKey, GWBUF** ppHead) const
{
return m_pApi->getHead(m_pStorage, pKey, ppValue);
return m_pApi->getHead(m_pStorage, pKey, ppHead);
}
cache_result_t StorageReal::get_tail(CACHE_KEY* pKey,
GWBUF** ppValue)
cache_result_t StorageReal::get_tail(CACHE_KEY* pKey, GWBUF** ppTail) const
{
return m_pApi->getTail(m_pStorage, pKey, ppValue);
return m_pApi->getTail(m_pStorage, pKey, ppTail);
}
cache_result_t StorageReal::get_size(uint64_t* pSize) const

View File

@ -21,15 +21,15 @@ public:
~StorageReal();
cache_result_t get_info(uint32_t flags,
json_t** ppInfo) const;
json_t** ppInfo) const;
cache_result_t get_key(const char* zDefaultDb,
const GWBUF* pQuery,
CACHE_KEY* pKey);
CACHE_KEY* pKey) const;
cache_result_t get_value(const CACHE_KEY& key,
uint32_t flags,
GWBUF** ppValue);
GWBUF** ppValue) const;
cache_result_t put_value(const CACHE_KEY& key,
const GWBUF* pValue);
@ -37,10 +37,10 @@ public:
cache_result_t del_value(const CACHE_KEY& key);
cache_result_t get_head(CACHE_KEY* pKey,
GWBUF** ppValue);
GWBUF** ppValue) const;
cache_result_t get_tail(CACHE_KEY* pKey,
GWBUF** ppValue);
GWBUF** ppValue) const;
cache_result_t get_size(uint64_t* pSize) const;