Cache: Change key from char ptr to struct

This commit is contained in:
Johan Wikman
2016-11-25 15:44:49 +02:00
parent c1c447985d
commit 279bf7e2fa
12 changed files with 80 additions and 75 deletions

View File

@ -145,33 +145,33 @@ bool Cache::shouldUse(const SESSION* pSession)
cache_result_t Cache::getKey(const char* zDefaultDb, cache_result_t Cache::getKey(const char* zDefaultDb,
const GWBUF* pQuery, const GWBUF* pQuery,
char* pKey) CACHE_KEY* pKey)
{ {
return m_pStorage->getKey(zDefaultDb, pQuery, pKey); return m_pStorage->getKey(zDefaultDb, pQuery, pKey);
} }
cache_result_t Cache::getValue(const char* pKey, cache_result_t Cache::getValue(const CACHE_KEY& key,
uint32_t flags, uint32_t flags,
GWBUF** ppValue) GWBUF** ppValue)
{ {
return m_pStorage->getValue(pKey, flags, ppValue); return m_pStorage->getValue(key, flags, ppValue);
} }
cache_result_t Cache::putValue(const char* pKey, cache_result_t Cache::putValue(const CACHE_KEY& key,
const GWBUF* pValue) const GWBUF* pValue)
{ {
return m_pStorage->putValue(pKey, pValue); return m_pStorage->putValue(key, pValue);
} }
cache_result_t Cache::delValue(const char* pKey) cache_result_t Cache::delValue(const CACHE_KEY& key)
{ {
return m_pStorage->delValue(pKey); return m_pStorage->delValue(key);
} }
// protected // protected
long Cache::hashOfKey(const char* pKey) long Cache::hashOfKey(const CACHE_KEY& key)
{ {
return hash_of_key(pKey); return hash_of_key(key.data);
} }
// protected // protected

View File

@ -48,30 +48,30 @@ public:
/** /**
* Specifies whether a particular SessioCache should refresh the data. * Specifies whether a particular SessioCache should refresh the data.
* *
* @param pKey The hashed key for a query. * @param key The hashed key for a query.
* @param pSessionCache The session cache asking. * @param pSessionCache The session cache asking.
* *
* @return True, if the session cache should refresh the data. * @return True, if the session cache should refresh the data.
*/ */
virtual bool mustRefresh(const char* pKey, const SessionCache* pSessionCache) = 0; virtual bool mustRefresh(const CACHE_KEY& key, const SessionCache* pSessionCache) = 0;
/** /**
* To inform the cache that a particular item has been updated upon request. * To inform the cache that a particular item has been updated upon request.
* *
* @param pKey The hashed key for a query. * @param key The hashed key for a query.
* @param pSessionCache The session cache informing. * @param pSessionCache The session cache informing.
*/ */
virtual void refreshed(const char* pKey, const SessionCache* pSessionCache) = 0; virtual void refreshed(const CACHE_KEY& key, const SessionCache* pSessionCache) = 0;
const CACHE_CONFIG& config() const { return m_config; } const CACHE_CONFIG& config() const { return m_config; }
cache_result_t getKey(const char* zDefaultDb, const GWBUF* pQuery, char* pKey); cache_result_t getKey(const char* zDefaultDb, const GWBUF* pQuery, CACHE_KEY* pKey);
cache_result_t getValue(const char* pKey, uint32_t flags, GWBUF** ppValue); cache_result_t getValue(const CACHE_KEY& key, uint32_t flags, GWBUF** ppValue);
cache_result_t putValue(const char* pKey, const GWBUF* pValue); cache_result_t putValue(const CACHE_KEY& key, const GWBUF* pValue);
cache_result_t delValue(const char* pKey); cache_result_t delValue(const CACHE_KEY& key);
protected: protected:
Cache(const char* zName, Cache(const char* zName,
@ -86,7 +86,7 @@ protected:
StorageFactory** ppFactory, StorageFactory** ppFactory,
HASHTABLE** ppPending); HASHTABLE** ppPending);
long hashOfKey(const char* pKey); long hashOfKey(const CACHE_KEY& key);
bool mustRefresh(long key, const SessionCache* pSessionCache); bool mustRefresh(long key, const SessionCache* pSessionCache);

View File

@ -39,8 +39,8 @@ typedef enum cache_flags
typedef enum cache_thread_model typedef enum cache_thread_model
{ {
CACHE_THREAD_MODEL_ST = 0x1, CACHE_THREAD_MODEL_ST,
CACHE_THREAD_MODEL_MT = 0x2, CACHE_THREAD_MODEL_MT
} cache_thread_model_t; } cache_thread_model_t;
typedef void* CACHE_STORAGE; typedef void* CACHE_STORAGE;
@ -50,6 +50,11 @@ enum
CACHE_KEY_MAXLEN = 128 CACHE_KEY_MAXLEN = 128
}; };
typedef struct cache_key
{
char data[CACHE_KEY_MAXLEN];
} CACHE_KEY;
typedef struct cache_storage_api typedef struct cache_storage_api
{ {
/** /**
@ -93,14 +98,14 @@ typedef struct cache_storage_api
* *
* @param storage Pointer to a CACHE_STORAGE. * @param storage Pointer to a CACHE_STORAGE.
* @param query An SQL query. Must be one contiguous buffer. * @param query An SQL query. Must be one contiguous buffer.
* @param key Pointer to array of CACHE_KEY_MAXLEN size where * @param key Pointer to key.
* the key will be written. *
* @return CACHE_RESULT_OK if a key was created, otherwise some error code. * @return CACHE_RESULT_OK if a key was created, otherwise some error code.
*/ */
cache_result_t (*getKey)(CACHE_STORAGE* storage, cache_result_t (*getKey)(CACHE_STORAGE* storage,
const char* default_db, const char* default_db,
const GWBUF* query, const GWBUF* query,
char* key); CACHE_KEY* key);
/** /**
* Get a value from the cache. * Get a value from the cache.
* *
@ -116,7 +121,7 @@ typedef struct cache_storage_api
* the ttl was reached), or some other error code. * the ttl was reached), or some other error code.
*/ */
cache_result_t (*getValue)(CACHE_STORAGE* storage, cache_result_t (*getValue)(CACHE_STORAGE* storage,
const char* key, const CACHE_KEY* key,
uint32_t flags, uint32_t flags,
GWBUF** result); GWBUF** result);
@ -132,7 +137,7 @@ typedef struct cache_storage_api
* some resource having become exhausted, or some other error code. * some resource having become exhausted, or some other error code.
*/ */
cache_result_t (*putValue)(CACHE_STORAGE* storage, cache_result_t (*putValue)(CACHE_STORAGE* storage,
const char* key, const CACHE_KEY* key,
const GWBUF* value); const GWBUF* value);
/** /**
@ -144,7 +149,7 @@ typedef struct cache_storage_api
* CACHE_RESULT_OK may be returned also if the entry was not present. * CACHE_RESULT_OK may be returned also if the entry was not present.
*/ */
cache_result_t (*delValue)(CACHE_STORAGE* storage, cache_result_t (*delValue)(CACHE_STORAGE* storage,
const char* key); const CACHE_KEY* key);
} CACHE_STORAGE_API; } CACHE_STORAGE_API;
#define CACHE_STORAGE_ENTRY_POINT "CacheGetStorageAPI" #define CACHE_STORAGE_ENTRY_POINT "CacheGetStorageAPI"

View File

@ -68,22 +68,22 @@ CacheMT* CacheMT::Create(const char* zName, CACHE_CONFIG& config)
return pCache; return pCache;
} }
bool CacheMT::mustRefresh(const char* pKey, const SessionCache* pSessionCache) bool CacheMT::mustRefresh(const CACHE_KEY& key, const SessionCache* pSessionCache)
{ {
long key = hashOfKey(pKey); long k = hashOfKey(key);
spinlock_acquire(&m_lockPending); spinlock_acquire(&m_lockPending);
bool rv = Cache::mustRefresh(key, pSessionCache); bool rv = Cache::mustRefresh(k, pSessionCache);
spinlock_release(&m_lockPending); spinlock_release(&m_lockPending);
return rv; return rv;
} }
void CacheMT::refreshed(const char* pKey, const SessionCache* pSessionCache) void CacheMT::refreshed(const CACHE_KEY& key, const SessionCache* pSessionCache)
{ {
long key = hashOfKey(pKey); long k = hashOfKey(key);
spinlock_acquire(&m_lockPending); spinlock_acquire(&m_lockPending);
Cache::refreshed(key, pSessionCache); Cache::refreshed(k, pSessionCache);
spinlock_release(&m_lockPending); spinlock_release(&m_lockPending);
} }

View File

@ -23,9 +23,9 @@ public:
static CacheMT* Create(const char* zName, CACHE_CONFIG& config); static CacheMT* Create(const char* zName, CACHE_CONFIG& config);
bool mustRefresh(const char* pKey, const SessionCache* pSessionCache); bool mustRefresh(const CACHE_KEY& key, const SessionCache* pSessionCache);
void refreshed(const char* pKey, const SessionCache* pSessionCache); void refreshed(const CACHE_KEY& key, const SessionCache* pSessionCache);
private: private:
CacheMT(const char* zName, CacheMT(const char* zName,

View File

@ -29,7 +29,7 @@ SessionCache::SessionCache(Cache* pCache, SESSION* pSession, char* zDefaultDb)
{ {
memset(&m_down, 0, sizeof(m_down)); memset(&m_down, 0, sizeof(m_down));
memset(&m_up, 0, sizeof(m_up)); memset(&m_up, 0, sizeof(m_up));
memset(m_key, 0, CACHE_KEY_MAXLEN); memset(m_key.data, 0, CACHE_KEY_MAXLEN);
reset_response_state(); reset_response_state();
} }
@ -607,7 +607,7 @@ void SessionCache::reset_response_state()
*/ */
cache_result_t SessionCache::get_cached_response(const GWBUF *pQuery, GWBUF **ppResponse) cache_result_t SessionCache::get_cached_response(const GWBUF *pQuery, GWBUF **ppResponse)
{ {
cache_result_t result = m_pCache->getKey(m_zDefaultDb, pQuery, m_key); cache_result_t result = m_pCache->getKey(m_zDefaultDb, pQuery, &m_key);
if (result == CACHE_RESULT_OK) if (result == CACHE_RESULT_OK)
{ {

View File

@ -128,15 +128,15 @@ private:
SessionCache& operator = (const SessionCache&); SessionCache& operator = (const SessionCache&);
private: private:
cache_session_state_t m_state; /**< What state is the session in, what data is expected. */ cache_session_state_t m_state; /**< What state is the session in, what data is expected. */
Cache* m_pCache; /**< The cache instance the session is associated with. */ Cache* m_pCache; /**< The cache instance the session is associated with. */
SESSION* m_pSession; /**< The session this data is associated with. */ SESSION* m_pSession; /**< The session this data is associated with. */
DOWNSTREAM m_down; /**< The previous filter or equivalent. */ DOWNSTREAM m_down; /**< The previous filter or equivalent. */
UPSTREAM m_up; /**< The next filter or equivalent. */ UPSTREAM m_up; /**< The next filter or equivalent. */
CACHE_RESPONSE_STATE m_res; /**< The response state. */ CACHE_RESPONSE_STATE m_res; /**< The response state. */
char m_key[CACHE_KEY_MAXLEN]; /**< Key storage. */ CACHE_KEY m_key; /**< Key storage. */
char* m_zDefaultDb; /**< The default database. */ char* m_zDefaultDb; /**< The default database. */
char* m_zUseDb; /**< Pending default database. Needs server response. */ char* m_zUseDb; /**< Pending default database. Needs server response. */
bool m_refreshing; /**< Whether the session is updating a stale cache entry. */ bool m_refreshing; /**< Whether the session is updating a stale cache entry. */
}; };

View File

@ -29,25 +29,25 @@ Storage::~Storage()
cache_result_t Storage::getKey(const char* zDefaultDb, cache_result_t Storage::getKey(const char* zDefaultDb,
const GWBUF* pQuery, const GWBUF* pQuery,
char* pKey) CACHE_KEY* pKey)
{ {
return m_pApi->getKey(m_pStorage, zDefaultDb, pQuery, pKey); return m_pApi->getKey(m_pStorage, zDefaultDb, pQuery, pKey);
} }
cache_result_t Storage::getValue(const char* pKey, cache_result_t Storage::getValue(const CACHE_KEY& key,
uint32_t flags, uint32_t flags,
GWBUF** ppValue) GWBUF** ppValue)
{ {
return m_pApi->getValue(m_pStorage, pKey, flags, ppValue); return m_pApi->getValue(m_pStorage, &key, flags, ppValue);
} }
cache_result_t Storage::putValue(const char* pKey, cache_result_t Storage::putValue(const CACHE_KEY& key,
const GWBUF* pValue) const GWBUF* pValue)
{ {
return m_pApi->putValue(m_pStorage, pKey, pValue); return m_pApi->putValue(m_pStorage, &key, pValue);
} }
cache_result_t Storage::delValue(const char* pKey) cache_result_t Storage::delValue(const CACHE_KEY& key)
{ {
return m_pApi->delValue(m_pStorage, pKey); return m_pApi->delValue(m_pStorage, &key);
} }

View File

@ -24,16 +24,16 @@ public:
cache_result_t getKey(const char* zDefaultDb, cache_result_t getKey(const char* zDefaultDb,
const GWBUF* pQuery, const GWBUF* pQuery,
char* pKey); CACHE_KEY* pKey);
cache_result_t getValue(const char* pKey, cache_result_t getValue(const CACHE_KEY& key,
uint32_t flags, uint32_t flags,
GWBUF** ppValue); GWBUF** ppValue);
cache_result_t putValue(const char* pKey, cache_result_t putValue(const CACHE_KEY& key,
const GWBUF* pValue); const GWBUF* pValue);
cache_result_t delValue(const char* pKey); cache_result_t delValue(const CACHE_KEY& key);
private: private:
friend class StorageFactory; friend class StorageFactory;

View File

@ -345,7 +345,7 @@ RocksDBStorage* RocksDBStorage::Create(const string& storageDirectory, const cha
return pStorage; return pStorage;
} }
cache_result_t RocksDBStorage::getKey(const char* zDefaultDB, const GWBUF* pQuery, char* pKey) cache_result_t RocksDBStorage::getKey(const char* zDefaultDB, const GWBUF* pQuery, CACHE_KEY* pKey)
{ {
ss_dassert(GWBUF_IS_CONTIGUOUS(pQuery)); ss_dassert(GWBUF_IS_CONTIGUOUS(pQuery));
@ -380,7 +380,7 @@ cache_result_t RocksDBStorage::getKey(const char* zDefaultDB, const GWBUF* pQuer
string tag; string tag;
for_each(dbs.begin(), dbs.end(), [&tag](const string& db) { tag.append(db); }); for_each(dbs.begin(), dbs.end(), [&tag](const string& db) { tag.append(db); });
memset(pKey, 0, CACHE_KEY_MAXLEN); memset(pKey->data, 0, CACHE_KEY_MAXLEN);
const unsigned char* pData; const unsigned char* pData;
@ -389,7 +389,7 @@ cache_result_t RocksDBStorage::getKey(const char* zDefaultDB, const GWBUF* pQuer
// This will also mean that entries related to the same databases will // This will also mean that entries related to the same databases will
// be placed near each other. // be placed near each other.
pData = reinterpret_cast<const unsigned char*>(tag.data()); pData = reinterpret_cast<const unsigned char*>(tag.data());
SHA512(pData, tag.length(), reinterpret_cast<unsigned char*>(pKey)); SHA512(pData, tag.length(), reinterpret_cast<unsigned char*>(pKey->data));
char *pSql; char *pSql;
int length; int length;
@ -398,16 +398,16 @@ cache_result_t RocksDBStorage::getKey(const char* zDefaultDB, const GWBUF* pQuer
// Then we store the query itself in the second half of the key. // Then we store the query itself in the second half of the key.
pData = reinterpret_cast<const unsigned char*>(pSql); pData = reinterpret_cast<const unsigned char*>(pSql);
SHA512(pData, length, reinterpret_cast<unsigned char*>(pKey) + SHA512_DIGEST_LENGTH); SHA512(pData, length, reinterpret_cast<unsigned char*>(pKey->data) + SHA512_DIGEST_LENGTH);
return CACHE_RESULT_OK; return CACHE_RESULT_OK;
} }
cache_result_t RocksDBStorage::getValue(const char* pKey, uint32_t flags, GWBUF** ppResult) cache_result_t RocksDBStorage::getValue(const CACHE_KEY* pKey, uint32_t flags, GWBUF** ppResult)
{ {
// Use the root DB so that we get the value *with* the timestamp at the end. // Use the root DB so that we get the value *with* the timestamp at the end.
rocksdb::DB* pDb = m_sDb->GetRootDB(); rocksdb::DB* pDb = m_sDb->GetRootDB();
rocksdb::Slice key(pKey, ROCKSDB_KEY_LENGTH); rocksdb::Slice key(pKey->data, ROCKSDB_KEY_LENGTH);
string value; string value;
rocksdb::Status status = pDb->Get(rocksdb::ReadOptions(), key, &value); rocksdb::Status status = pDb->Get(rocksdb::ReadOptions(), key, &value);
@ -465,11 +465,11 @@ cache_result_t RocksDBStorage::getValue(const char* pKey, uint32_t flags, GWBUF*
return result; return result;
} }
cache_result_t RocksDBStorage::putValue(const char* pKey, const GWBUF* pValue) cache_result_t RocksDBStorage::putValue(const CACHE_KEY* pKey, const GWBUF* pValue)
{ {
ss_dassert(GWBUF_IS_CONTIGUOUS(pValue)); ss_dassert(GWBUF_IS_CONTIGUOUS(pValue));
rocksdb::Slice key(pKey, ROCKSDB_KEY_LENGTH); rocksdb::Slice key(pKey->data, ROCKSDB_KEY_LENGTH);
rocksdb::Slice value((char*)GWBUF_DATA(pValue), GWBUF_LENGTH(pValue)); rocksdb::Slice value((char*)GWBUF_DATA(pValue), GWBUF_LENGTH(pValue));
rocksdb::Status status = m_sDb->Put(writeOptions(), key, value); rocksdb::Status status = m_sDb->Put(writeOptions(), key, value);
@ -477,11 +477,11 @@ cache_result_t RocksDBStorage::putValue(const char* pKey, const GWBUF* pValue)
return status.ok() ? CACHE_RESULT_OK : CACHE_RESULT_ERROR; return status.ok() ? CACHE_RESULT_OK : CACHE_RESULT_ERROR;
} }
cache_result_t RocksDBStorage::delValue(const char* pKey) cache_result_t RocksDBStorage::delValue(const CACHE_KEY* pKey)
{ {
ss_dassert(pKey); ss_dassert(pKey);
rocksdb::Slice key(pKey, ROCKSDB_KEY_LENGTH); rocksdb::Slice key(pKey->data, ROCKSDB_KEY_LENGTH);
rocksdb::Status status = m_sDb->Delete(writeOptions(), key); rocksdb::Status status = m_sDb->Delete(writeOptions(), key);

View File

@ -29,10 +29,10 @@ public:
static RocksDBStorage* Create(const char* zName, uint32_t ttl, int argc, char* argv[]); static RocksDBStorage* Create(const char* zName, uint32_t ttl, int argc, char* argv[]);
~RocksDBStorage(); ~RocksDBStorage();
cache_result_t getKey(const char* zDefaultDB, const GWBUF* pQuery, char* pKey); cache_result_t getKey(const char* zDefaultDB, const GWBUF* pQuery, CACHE_KEY* pKey);
cache_result_t getValue(const char* pKey, uint32_t flags, GWBUF** ppResult); cache_result_t getValue(const CACHE_KEY* pKey, uint32_t flags, GWBUF** ppResult);
cache_result_t putValue(const char* pKey, const GWBUF* pValue); cache_result_t putValue(const CACHE_KEY* pKey, const GWBUF* pValue);
cache_result_t delValue(const char* pKey); cache_result_t delValue(const CACHE_KEY* pKey);
private: private:
RocksDBStorage(std::unique_ptr<rocksdb::DBWithTTL>& sDb, RocksDBStorage(std::unique_ptr<rocksdb::DBWithTTL>& sDb,

View File

@ -60,7 +60,7 @@ void freeInstance(CACHE_STORAGE* pInstance)
cache_result_t getKey(CACHE_STORAGE* pStorage, cache_result_t getKey(CACHE_STORAGE* pStorage,
const char* zDefaultDB, const char* zDefaultDB,
const GWBUF* pQuery, const GWBUF* pQuery,
char* pKey) CACHE_KEY* pKey)
{ {
ss_dassert(pStorage); ss_dassert(pStorage);
// zDefaultDB may be NULL. // zDefaultDB may be NULL.
@ -90,7 +90,7 @@ cache_result_t getKey(CACHE_STORAGE* pStorage,
} }
cache_result_t getValue(CACHE_STORAGE* pStorage, cache_result_t getValue(CACHE_STORAGE* pStorage,
const char* pKey, const CACHE_KEY* pKey,
uint32_t flags, uint32_t flags,
GWBUF** ppResult) GWBUF** ppResult)
{ {
@ -121,7 +121,7 @@ cache_result_t getValue(CACHE_STORAGE* pStorage,
} }
cache_result_t putValue(CACHE_STORAGE* pStorage, cache_result_t putValue(CACHE_STORAGE* pStorage,
const char* pKey, const CACHE_KEY* pKey,
const GWBUF* pValue) const GWBUF* pValue)
{ {
ss_dassert(pStorage); ss_dassert(pStorage);
@ -151,7 +151,7 @@ cache_result_t putValue(CACHE_STORAGE* pStorage,
} }
cache_result_t delValue(CACHE_STORAGE* pStorage, cache_result_t delValue(CACHE_STORAGE* pStorage,
const char* pKey) const CACHE_KEY* pKey)
{ {
ss_dassert(pStorage); ss_dassert(pStorage);
ss_dassert(pKey); ss_dassert(pKey);