Cache: Move key generation to module level

The key generation is dependent upon the storage implemenation,
but not on a particular storage instance.
This commit is contained in:
Johan Wikman
2016-12-15 18:46:48 +02:00
parent 297b8e1a44
commit 9e89fe9246
10 changed files with 84 additions and 70 deletions

View File

@ -143,6 +143,18 @@ typedef struct cache_storage_api
uint64_t max_size,
int argc, char* argv[]);
/**
* Create a key for a GWBUF.
*
* @param query An SQL query. Must be one contiguous buffer.
* @param key Pointer to key.
*
* @return CACHE_RESULT_OK if a key was created, otherwise some error code.
*/
cache_result_t (*getKey)(const char* default_db,
const GWBUF* query,
CACHE_KEY* key);
/**
* Frees an CACHE_STORAGE instance earlier created with createInstance.
*
@ -164,20 +176,6 @@ typedef struct cache_storage_api
cache_result_t (*getInfo)(CACHE_STORAGE* storage,
uint32_t what,
json_t** info);
/**
* Create a key for a GWBUF.
*
* @param storage Pointer to a CACHE_STORAGE.
* @param query An SQL query. Must be one contiguous buffer.
* @param key Pointer to key.
*
* @return CACHE_RESULT_OK if a key was created, otherwise some error code.
*/
cache_result_t (*getKey)(CACHE_STORAGE* storage,
const char* default_db,
const GWBUF* query,
CACHE_KEY* key);
/**
* Get a value from the cache.
*

View File

@ -46,6 +46,7 @@ InMemoryStorage::~InMemoryStorage()
{
}
// static
cache_result_t InMemoryStorage::get_key(const char* zdefault_db, const GWBUF* pquery, CACHE_KEY* pkey)
{
ss_dassert(GWBUF_IS_CONTIGUOUS(pquery));

View File

@ -24,7 +24,7 @@ class InMemoryStorage
public:
virtual ~InMemoryStorage();
cache_result_t get_key(const char* zdefault_db, const GWBUF* pquery, CACHE_KEY* pkey);
static cache_result_t get_key(const char* zdefault_db, const GWBUF* pquery, CACHE_KEY* pkey);
virtual cache_result_t get_info(uint32_t what, json_t** ppInfo) const = 0;
virtual cache_result_t get_value(const CACHE_KEY& key, uint32_t flags, GWBUF** ppresult) = 0;

View File

@ -76,6 +76,21 @@ CACHE_STORAGE* createInstance(cache_thread_model_t model,
return reinterpret_cast<CACHE_STORAGE*>(sStorage.release());
}
cache_result_t getKey(const char* zdefault_db,
const GWBUF* pquery,
CACHE_KEY* pkey)
{
// zdefault_db may be NULL.
ss_dassert(pquery);
ss_dassert(pkey);
cache_result_t result = CACHE_RESULT_ERROR;
MXS_EXCEPTION_GUARD(result = InMemoryStorage::get_key(zdefault_db, pquery, pkey));
return result;
}
void freeInstance(CACHE_STORAGE* pinstance)
{
MXS_EXCEPTION_GUARD(delete reinterpret_cast<InMemoryStorage*>(pinstance));
@ -94,25 +109,6 @@ cache_result_t getInfo(CACHE_STORAGE* pStorage,
return result;
}
cache_result_t getKey(CACHE_STORAGE* pstorage,
const char* zdefault_db,
const GWBUF* pquery,
CACHE_KEY* pkey)
{
ss_dassert(pstorage);
// zdefault_db may be NULL.
ss_dassert(pquery);
ss_dassert(pkey);
cache_result_t result = CACHE_RESULT_ERROR;
MXS_EXCEPTION_GUARD(result = reinterpret_cast<InMemoryStorage*>(pstorage)->get_key(zdefault_db,
pquery,
pkey));
return result;
}
cache_result_t getValue(CACHE_STORAGE* pstorage,
const CACHE_KEY* pkey,
uint32_t flags,
@ -221,9 +217,9 @@ CACHE_STORAGE_API* CacheGetStorageAPI()
{
initialize,
createInstance,
getKey,
freeInstance,
getInfo,
getKey,
getValue,
putValue,
delValue,

View File

@ -361,32 +361,8 @@ unique_ptr<RocksDBStorage> RocksDBStorage::Create(const string& storageDirectory
return sStorage;
}
cache_result_t RocksDBStorage::getInfo(uint32_t what, json_t** ppInfo) const
{
json_t* pInfo = json_object();
if (pInfo)
{
auto sStatistics = m_sDb->GetOptions().statistics;
for_each(rocksdb::TickersNameMap.begin(), rocksdb::TickersNameMap.end(),
[pInfo, sStatistics](const std::pair<rocksdb::Tickers, string>& tickerName) {
json_t* pValue = json_integer(sStatistics->getTickerCount(tickerName.first));
if (pValue)
{
json_object_set(pInfo, tickerName.second.c_str(), pValue);
json_decref(pValue);
}
});
*ppInfo = pInfo;
}
return pInfo ? CACHE_RESULT_OK : CACHE_RESULT_OUT_OF_RESOURCES;
}
cache_result_t RocksDBStorage::getKey(const char* zDefaultDB, const GWBUF* pQuery, CACHE_KEY* pKey)
// static
cache_result_t RocksDBStorage::GetKey(const char* zDefaultDB, const GWBUF* pQuery, CACHE_KEY* pKey)
{
ss_dassert(GWBUF_IS_CONTIGUOUS(pQuery));
@ -444,6 +420,31 @@ cache_result_t RocksDBStorage::getKey(const char* zDefaultDB, const GWBUF* pQuer
return CACHE_RESULT_OK;
}
cache_result_t RocksDBStorage::getInfo(uint32_t what, json_t** ppInfo) const
{
json_t* pInfo = json_object();
if (pInfo)
{
auto sStatistics = m_sDb->GetOptions().statistics;
for_each(rocksdb::TickersNameMap.begin(), rocksdb::TickersNameMap.end(),
[pInfo, sStatistics](const std::pair<rocksdb::Tickers, string>& tickerName) {
json_t* pValue = json_integer(sStatistics->getTickerCount(tickerName.first));
if (pValue)
{
json_object_set(pInfo, tickerName.second.c_str(), pValue);
json_decref(pValue);
}
});
*ppInfo = pInfo;
}
return pInfo ? CACHE_RESULT_OK : CACHE_RESULT_OUT_OF_RESOURCES;
}
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.

View File

@ -28,8 +28,9 @@ public:
static SRocksDBStorage Create(const char* zName, uint32_t ttl, int argc, char* argv[]);
~RocksDBStorage();
static cache_result_t GetKey(const char* zDefaultDB, const GWBUF* pQuery, CACHE_KEY* pKey);
cache_result_t getInfo(uint32_t flags, json_t** ppInfo) const;
cache_result_t getKey(const char* zDefaultDB, const GWBUF* pQuery, CACHE_KEY* pKey);
cache_result_t getValue(const CACHE_KEY* pKey, uint32_t flags, GWBUF** ppResult);
cache_result_t putValue(const CACHE_KEY* pKey, const GWBUF* pValue);
cache_result_t delValue(const CACHE_KEY* pKey);

View File

@ -80,21 +80,17 @@ cache_result_t getInfo(CACHE_STORAGE* pStorage,
return result;
}
cache_result_t getKey(CACHE_STORAGE* pStorage,
const char* zDefaultDB,
cache_result_t getKey(const char* zDefaultDB,
const GWBUF* pQuery,
CACHE_KEY* pKey)
{
ss_dassert(pStorage);
// zDefaultDB may be NULL.
ss_dassert(pQuery);
ss_dassert(pKey);
cache_result_t result = CACHE_RESULT_ERROR;
MXS_EXCEPTION_GUARD(result = reinterpret_cast<RocksDBStorage*>(pStorage)->getKey(zDefaultDB,
pQuery,
pKey));
MXS_EXCEPTION_GUARD(result = RocksDBStorage::GetKey(zDefaultDB, pQuery, pKey));
return result;
}
@ -206,9 +202,9 @@ CACHE_STORAGE_API* CacheGetStorageAPI()
{
initialize,
createInstance,
getKey,
freeInstance,
getInfo,
getKey,
getValue,
putValue,
delValue,

View File

@ -238,3 +238,11 @@ Storage* StorageFactory::createRawStorage(cache_thread_model_t model,
return pStorage;
}
cache_result_t StorageFactory::get_key(const char* zDefaultDb,
const GWBUF* pQuery,
CACHE_KEY* pKey) const
{
return m_pApi->getKey(zDefaultDb, pQuery, pKey);
}

View File

@ -95,6 +95,19 @@ public:
uint64_t max_size,
int argc, char* argv[]);
/**
* 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.
*/
cache_result_t get_key(const char* zDefaultDb,
const GWBUF* pQuery,
CACHE_KEY* pKey) const;
private:
StorageFactory(void* handle, CACHE_STORAGE_API* pApi, uint32_t capabilities);

View File

@ -37,7 +37,7 @@ cache_result_t StorageReal::get_key(const char* zDefaultDb,
const GWBUF* pQuery,
CACHE_KEY* pKey) const
{
return m_pApi->getKey(m_pStorage, zDefaultDb, pQuery, pKey);
return m_pApi->getKey(zDefaultDb, pQuery, pKey);
}
cache_result_t StorageReal::get_value(const CACHE_KEY& key,