Cache: Provide Storage boiler-plate using template

All storage modules must implement the same interface and use the
same exception guards. Consequently that is conveniently provided
using a template.
This commit is contained in:
Johan Wikman
2016-12-20 13:10:06 +02:00
parent ee422fd3ec
commit 3967a0b8c2
7 changed files with 316 additions and 464 deletions

View File

@ -15,11 +15,15 @@
#include "inmemorystorage.hh" #include "inmemorystorage.hh"
#include <openssl/sha.h> #include <openssl/sha.h>
#include <algorithm> #include <algorithm>
#include <memory>
#include <set> #include <set>
#include <maxscale/alloc.h> #include <maxscale/alloc.h>
#include <maxscale/modutil.h> #include <maxscale/modutil.h>
#include <maxscale/query_classifier.h> #include <maxscale/query_classifier.h>
#include "inmemorystoragest.hh"
#include "inmemorystoragemt.hh"
using std::auto_ptr;
using std::set; using std::set;
using std::string; using std::string;
@ -45,8 +49,55 @@ InMemoryStorage::~InMemoryStorage()
{ {
} }
// static bool InMemoryStorage::Initialize(uint32_t* pcapabilities)
cache_result_t InMemoryStorage::get_key(const char* zdefault_db, const GWBUF* pquery, CACHE_KEY* pkey) {
*pcapabilities = CACHE_STORAGE_CAP_ST;
*pcapabilities = CACHE_STORAGE_CAP_MT;
return true;
}
InMemoryStorage* InMemoryStorage::Create_instance(const char* zname,
const CACHE_STORAGE_CONFIG& config,
int argc, char* argv[])
{
ss_dassert(zname);
if (config.max_count != 0)
{
MXS_WARNING("A maximum item count of %u specified, although 'storage_inMemory' "
"does not enforce such a limit.", (unsigned int)config.max_count);
}
if (config.max_size != 0)
{
MXS_WARNING("A maximum size of %lu specified, although 'storage_inMemory' "
"does not enforce such a limit.", (unsigned long)config.max_size);
}
auto_ptr<InMemoryStorage> sstorage;
switch (config.thread_model)
{
case CACHE_THREAD_MODEL_ST:
sstorage = InMemoryStorageST::create(zname, config, argc, argv);
break;
default:
ss_dassert(!true);
MXS_ERROR("Unknown thread model %d, creating multi-thread aware storage.",
(int)config.thread_model);
case CACHE_THREAD_MODEL_MT:
sstorage = InMemoryStorageMT::create(zname, config, argc, argv);
break;
}
MXS_NOTICE("Storage module created.");
return sstorage.release();
}
cache_result_t InMemoryStorage::Get_key(const char* zdefault_db, const GWBUF* pquery, CACHE_KEY* pkey)
{ {
ss_dassert(GWBUF_IS_CONTIGUOUS(pquery)); ss_dassert(GWBUF_IS_CONTIGUOUS(pquery));

View File

@ -24,7 +24,13 @@ class InMemoryStorage
public: public:
virtual ~InMemoryStorage(); virtual ~InMemoryStorage();
static cache_result_t get_key(const char* zdefault_db, const GWBUF* pquery, CACHE_KEY* pkey); static bool Initialize(uint32_t* pcapabilities);
static InMemoryStorage* Create_instance(const char* zname,
const CACHE_STORAGE_CONFIG& config,
int argc, char* argv[]);
static cache_result_t Get_key(const char* zdefault_db, const GWBUF* pquery, CACHE_KEY* pkey);
void get_config(CACHE_STORAGE_CONFIG* pConfig); void get_config(CACHE_STORAGE_CONFIG* pConfig);
virtual cache_result_t get_info(uint32_t what, json_t** ppInfo) const = 0; virtual cache_result_t get_info(uint32_t what, json_t** ppInfo) const = 0;

View File

@ -13,231 +13,16 @@
#define MXS_MODULE_NAME "storage_inmemory" #define MXS_MODULE_NAME "storage_inmemory"
#include <maxscale/cppdefs.hh> #include <maxscale/cppdefs.hh>
#include <inttypes.h>
#include "../../cache_storage_api.h" #include "../../cache_storage_api.h"
#include "../storagemodule.hh"
#include "inmemorystoragest.hh" #include "inmemorystoragest.hh"
#include "inmemorystoragemt.hh"
using std::auto_ptr;
namespace
{
bool initialize(uint32_t* pcapabilities)
{
*pcapabilities = CACHE_STORAGE_CAP_ST;
*pcapabilities = CACHE_STORAGE_CAP_MT;
return true;
}
CACHE_STORAGE* createInstance(const char* zname,
const CACHE_STORAGE_CONFIG* pConfig,
int argc, char* argv[])
{
ss_dassert(zname);
if (pConfig->max_count != 0)
{
MXS_WARNING("A maximum item count of %u specified, although 'storage_inMemory' "
"does not enforce such a limit.", (unsigned int)pConfig->max_count);
}
if (pConfig->max_size != 0)
{
MXS_WARNING("A maximum size of %lu specified, although 'storage_inMemory' "
"does not enforce such a limit.", (unsigned long)pConfig->max_size);
}
auto_ptr<InMemoryStorage> sStorage;
switch (pConfig->thread_model)
{
case CACHE_THREAD_MODEL_ST:
MXS_EXCEPTION_GUARD(sStorage = InMemoryStorageST::create(zname, *pConfig, argc, argv));
break;
default:
ss_dassert(!true);
MXS_ERROR("Unknown thread model %d, creating multi-thread aware storage.",
(int)pConfig->thread_model);
case CACHE_THREAD_MODEL_MT:
MXS_EXCEPTION_GUARD(sStorage = InMemoryStorageMT::create(zname, *pConfig, argc, argv));
break;
}
if (sStorage.get())
{
MXS_NOTICE("Storage module created.");
}
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));
}
void getConfig(CACHE_STORAGE* pStorage,
CACHE_STORAGE_CONFIG* pConfig)
{
ss_dassert(pStorage);
ss_dassert(pConfig);
MXS_EXCEPTION_GUARD(reinterpret_cast<InMemoryStorage*>(pStorage)->get_config(pConfig));
}
cache_result_t getInfo(CACHE_STORAGE* pStorage,
uint32_t what,
json_t** ppInfo)
{
ss_dassert(pStorage);
cache_result_t result = CACHE_RESULT_ERROR;
MXS_EXCEPTION_GUARD(result = reinterpret_cast<InMemoryStorage*>(pStorage)->get_info(what, ppInfo));
return result;
}
cache_result_t getValue(CACHE_STORAGE* pstorage,
const CACHE_KEY* pkey,
uint32_t flags,
GWBUF** ppresult)
{
ss_dassert(pstorage);
ss_dassert(pkey);
ss_dassert(ppresult);
cache_result_t result = CACHE_RESULT_ERROR;
MXS_EXCEPTION_GUARD(result = reinterpret_cast<InMemoryStorage*>(pstorage)->get_value(*pkey,
flags,
ppresult));
return result;
}
cache_result_t putValue(CACHE_STORAGE* pstorage,
const CACHE_KEY* pkey,
const GWBUF* pvalue)
{
ss_dassert(pstorage);
ss_dassert(pkey);
ss_dassert(pvalue);
cache_result_t result = CACHE_RESULT_ERROR;
MXS_EXCEPTION_GUARD(result = reinterpret_cast<InMemoryStorage*>(pstorage)->put_value(*pkey, pvalue));
return result;
}
cache_result_t delValue(CACHE_STORAGE* pstorage,
const CACHE_KEY* pkey)
{
ss_dassert(pstorage);
ss_dassert(pkey);
cache_result_t result = CACHE_RESULT_ERROR;
MXS_EXCEPTION_GUARD(result = reinterpret_cast<InMemoryStorage*>(pstorage)->del_value(*pkey));
return result;
}
cache_result_t getHead(CACHE_STORAGE* pstorage,
CACHE_KEY* pkey,
GWBUF** pphead)
{
ss_dassert(pstorage);
cache_result_t result = CACHE_RESULT_ERROR;
MXS_EXCEPTION_GUARD(result = reinterpret_cast<InMemoryStorage*>(pstorage)->get_head(pkey, pphead));
return result;
}
cache_result_t getTail(CACHE_STORAGE* pstorage,
CACHE_KEY* pkey,
GWBUF** pptail)
{
ss_dassert(pstorage);
cache_result_t result = CACHE_RESULT_ERROR;
MXS_EXCEPTION_GUARD(result = reinterpret_cast<InMemoryStorage*>(pstorage)->get_tail(pkey, pptail));
return result;
}
cache_result_t getSize(CACHE_STORAGE* pstorage,
uint64_t* psize)
{
ss_dassert(pstorage);
cache_result_t result = CACHE_RESULT_ERROR;
MXS_EXCEPTION_GUARD(result = reinterpret_cast<InMemoryStorage*>(pstorage)->get_size(psize));
return result;
}
cache_result_t getItems(CACHE_STORAGE* pstorage,
uint64_t* pitems)
{
ss_dassert(pstorage);
cache_result_t result = CACHE_RESULT_ERROR;
MXS_EXCEPTION_GUARD(result = reinterpret_cast<InMemoryStorage*>(pstorage)->get_items(pitems));
return result;
}
}
extern "C" extern "C"
{ {
CACHE_STORAGE_API* CacheGetStorageAPI() CACHE_STORAGE_API* CacheGetStorageAPI()
{ {
static CACHE_STORAGE_API api = return &StorageModule<InMemoryStorage>::s_api;
{
initialize,
createInstance,
getKey,
freeInstance,
getConfig,
getInfo,
getValue,
putValue,
delValue,
getHead,
getTail,
getSize,
getItems
};
return &api;
} }
} }

View File

@ -208,9 +208,10 @@ RocksDBStorage::~RocksDBStorage()
{ {
} }
//static bool RocksDBStorage::Initialize(uint32_t* pCapabilities)
bool RocksDBStorage::Initialize()
{ {
*pCapabilities = CACHE_STORAGE_CAP_MT;
auto pEnv = rocksdb::Env::Default(); auto pEnv = rocksdb::Env::Default();
pEnv->SetBackgroundThreads(ROCKSDB_N_LOW_THREADS, rocksdb::Env::LOW); pEnv->SetBackgroundThreads(ROCKSDB_N_LOW_THREADS, rocksdb::Env::LOW);
pEnv->SetBackgroundThreads(ROCKSDB_N_HIGH_THREADS, rocksdb::Env::HIGH); pEnv->SetBackgroundThreads(ROCKSDB_N_HIGH_THREADS, rocksdb::Env::HIGH);
@ -222,8 +223,7 @@ bool RocksDBStorage::Initialize()
return true; return true;
} }
//static RocksDBStorage* RocksDBStorage::Create_instance(const char* zName,
unique_ptr<RocksDBStorage> RocksDBStorage::Create(const char* zName,
const CACHE_STORAGE_CONFIG& config, const CACHE_STORAGE_CONFIG& config,
int argc, char* argv[]) int argc, char* argv[])
{ {
@ -279,8 +279,7 @@ unique_ptr<RocksDBStorage> RocksDBStorage::Create(const char* zName,
return Create(zName, config, storageDirectory, collectStatistics); return Create(zName, config, storageDirectory, collectStatistics);
} }
// static RocksDBStorage* RocksDBStorage::Create(const char* zName,
unique_ptr<RocksDBStorage> RocksDBStorage::Create(const char* zName,
const CACHE_STORAGE_CONFIG& config, const CACHE_STORAGE_CONFIG& config,
const string& storageDirectory, const string& storageDirectory,
bool collectStatistics) bool collectStatistics)
@ -358,11 +357,10 @@ unique_ptr<RocksDBStorage> RocksDBStorage::Create(const char* zName,
} }
} }
return sStorage; return sStorage.release();
} }
// static cache_result_t RocksDBStorage::Get_key(const char* zDefaultDB, const GWBUF* pQuery, CACHE_KEY* 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));
@ -420,12 +418,12 @@ cache_result_t RocksDBStorage::GetKey(const char* zDefaultDB, const GWBUF* pQuer
return CACHE_RESULT_OK; return CACHE_RESULT_OK;
} }
void RocksDBStorage::getConfig(CACHE_STORAGE_CONFIG* pConfig) void RocksDBStorage::get_config(CACHE_STORAGE_CONFIG* pConfig)
{ {
*pConfig = m_config; *pConfig = m_config;
} }
cache_result_t RocksDBStorage::getInfo(uint32_t what, json_t** ppInfo) const cache_result_t RocksDBStorage::get_info(uint32_t what, json_t** ppInfo) const
{ {
json_t* pInfo = json_object(); json_t* pInfo = json_object();
@ -450,14 +448,14 @@ cache_result_t RocksDBStorage::getInfo(uint32_t what, json_t** ppInfo) const
return pInfo ? CACHE_RESULT_OK : CACHE_RESULT_OUT_OF_RESOURCES; return pInfo ? CACHE_RESULT_OK : CACHE_RESULT_OUT_OF_RESOURCES;
} }
cache_result_t RocksDBStorage::getValue(const CACHE_KEY* pKey, uint32_t flags, GWBUF** ppResult) cache_result_t RocksDBStorage::get_value(const CACHE_KEY& key, 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->data, ROCKSDB_KEY_LENGTH); rocksdb::Slice rocksdb_key(key.data, ROCKSDB_KEY_LENGTH);
string value; string value;
rocksdb::Status status = pDb->Get(rocksdb::ReadOptions(), key, &value); rocksdb::Status status = pDb->Get(rocksdb::ReadOptions(), rocksdb_key, &value);
cache_result_t result = CACHE_RESULT_ERROR; cache_result_t result = CACHE_RESULT_ERROR;
@ -512,45 +510,43 @@ cache_result_t RocksDBStorage::getValue(const CACHE_KEY* pKey, uint32_t flags, G
return result; return result;
} }
cache_result_t RocksDBStorage::putValue(const CACHE_KEY* pKey, const GWBUF* pValue) cache_result_t RocksDBStorage::put_value(const CACHE_KEY& key, const GWBUF* pValue)
{ {
ss_dassert(GWBUF_IS_CONTIGUOUS(pValue)); ss_dassert(GWBUF_IS_CONTIGUOUS(pValue));
rocksdb::Slice key(pKey->data, ROCKSDB_KEY_LENGTH); rocksdb::Slice rocksdb_key(key.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(), rocksdb_key, value);
return status.ok() ? CACHE_RESULT_OK : CACHE_RESULT_ERROR; return status.ok() ? CACHE_RESULT_OK : CACHE_RESULT_ERROR;
} }
cache_result_t RocksDBStorage::delValue(const CACHE_KEY* pKey) cache_result_t RocksDBStorage::del_value(const CACHE_KEY& key)
{ {
ss_dassert(pKey); rocksdb::Slice rocksdb_key(key.data, ROCKSDB_KEY_LENGTH);
rocksdb::Slice key(pKey->data, ROCKSDB_KEY_LENGTH); rocksdb::Status status = m_sDb->Delete(writeOptions(), rocksdb_key);
rocksdb::Status status = m_sDb->Delete(writeOptions(), key);
return status.ok() ? CACHE_RESULT_OK : CACHE_RESULT_ERROR; return status.ok() ? CACHE_RESULT_OK : CACHE_RESULT_ERROR;
} }
cache_result_t RocksDBStorage::getHead(CACHE_KEY* pKey, GWBUF** ppHead) const cache_result_t RocksDBStorage::get_head(CACHE_KEY* pKey, GWBUF** ppHead) const
{ {
return CACHE_RESULT_OUT_OF_RESOURCES; return CACHE_RESULT_OUT_OF_RESOURCES;
} }
cache_result_t RocksDBStorage::getTail(CACHE_KEY* pKey, GWBUF** ppHead) const cache_result_t RocksDBStorage::get_tail(CACHE_KEY* pKey, GWBUF** ppHead) const
{ {
return CACHE_RESULT_OUT_OF_RESOURCES; return CACHE_RESULT_OUT_OF_RESOURCES;
} }
cache_result_t RocksDBStorage::getSize(uint64_t* pSize) const cache_result_t RocksDBStorage::get_size(uint64_t* pSize) const
{ {
return CACHE_RESULT_OUT_OF_RESOURCES; return CACHE_RESULT_OUT_OF_RESOURCES;
} }
cache_result_t RocksDBStorage::getItems(uint64_t* pItems) const cache_result_t RocksDBStorage::get_items(uint64_t* pItems) const
{ {
return CACHE_RESULT_OUT_OF_RESOURCES; return CACHE_RESULT_OUT_OF_RESOURCES;
} }

View File

@ -23,25 +23,25 @@ class RocksDBStorage
public: public:
typedef std::unique_ptr<RocksDBStorage> SRocksDBStorage; typedef std::unique_ptr<RocksDBStorage> SRocksDBStorage;
static bool Initialize(); static bool Initialize(uint32_t* pCapabilities);
static SRocksDBStorage Create(const char* zName, static RocksDBStorage* Create_instance(const char* zName,
const CACHE_STORAGE_CONFIG& config, const CACHE_STORAGE_CONFIG& config,
int argc, char* argv[]); int argc, char* argv[]);
~RocksDBStorage(); ~RocksDBStorage();
static cache_result_t GetKey(const char* zDefaultDB, const GWBUF* pQuery, CACHE_KEY* pKey); static cache_result_t Get_key(const char* zDefaultDB, const GWBUF* pQuery, CACHE_KEY* pKey);
void getConfig(CACHE_STORAGE_CONFIG* pConfig); void get_config(CACHE_STORAGE_CONFIG* pConfig);
cache_result_t getInfo(uint32_t flags, json_t** ppInfo) const; cache_result_t get_info(uint32_t flags, json_t** ppInfo) const;
cache_result_t getValue(const CACHE_KEY* pKey, uint32_t flags, GWBUF** ppResult); cache_result_t get_value(const CACHE_KEY& key, uint32_t flags, GWBUF** ppResult);
cache_result_t putValue(const CACHE_KEY* pKey, const GWBUF* pValue); cache_result_t put_value(const CACHE_KEY& key, const GWBUF* pValue);
cache_result_t delValue(const CACHE_KEY* pKey); cache_result_t del_value(const CACHE_KEY& key);
cache_result_t getHead(CACHE_KEY* pKey, GWBUF** ppHead) const; cache_result_t get_head(CACHE_KEY* pKey, GWBUF** ppHead) const;
cache_result_t getTail(CACHE_KEY* pKey, GWBUF** ppHead) const; cache_result_t get_tail(CACHE_KEY* pKey, GWBUF** ppHead) const;
cache_result_t getSize(uint64_t* pSize) const; cache_result_t get_size(uint64_t* pSize) const;
cache_result_t getItems(uint64_t* pItems) const; cache_result_t get_items(uint64_t* pItems) const;
private: private:
RocksDBStorage(const std::string& name, RocksDBStorage(const std::string& name,
@ -52,7 +52,7 @@ private:
RocksDBStorage(const RocksDBStorage&) = delete; RocksDBStorage(const RocksDBStorage&) = delete;
RocksDBStorage& operator = (const RocksDBStorage&) = delete; RocksDBStorage& operator = (const RocksDBStorage&) = delete;
static SRocksDBStorage Create(const char* zName, static RocksDBStorage* Create(const char* zName,
const CACHE_STORAGE_CONFIG& config, const CACHE_STORAGE_CONFIG& config,
const std::string& storageDirectory, const std::string& storageDirectory,
bool collectStatistics); bool collectStatistics);

View File

@ -13,213 +13,17 @@
#define MXS_MODULE_NAME "storage_rocksdb" #define MXS_MODULE_NAME "storage_rocksdb"
#include <maxscale/cppdefs.hh> #include <maxscale/cppdefs.hh>
#include <inttypes.h>
#include "../../cache_storage_api.h" #include "../../cache_storage_api.h"
#include "../storagemodule.hh"
#include "rocksdbstorage.hh" #include "rocksdbstorage.hh"
using std::unique_ptr;
namespace
{
bool initialize(uint32_t* pCapabilities)
{
*pCapabilities = CACHE_STORAGE_CAP_MT;
return RocksDBStorage::Initialize();
}
CACHE_STORAGE* createInstance(const char* zName,
const CACHE_STORAGE_CONFIG* pConfig,
int argc, char* argv[])
{
ss_dassert(zName);
if (pConfig->max_count != 0)
{
MXS_WARNING("A maximum item count of %u specifed, although 'storage_rocksdb' "
"does not enforce such a limit.", (unsigned int)pConfig->max_count);
}
if (pConfig->max_size != 0)
{
MXS_WARNING("A maximum size of %lu specified, although 'storage_rocksdb' "
"does not enforce such a limit.", (unsigned long)pConfig->max_size);
}
unique_ptr<RocksDBStorage> sStorage;
MXS_EXCEPTION_GUARD(sStorage = RocksDBStorage::Create(zName, *pConfig, argc, argv));
if (sStorage)
{
MXS_NOTICE("Storage module created.");
}
return reinterpret_cast<CACHE_STORAGE*>(sStorage.release());
}
void freeInstance(CACHE_STORAGE* pInstance)
{
MXS_EXCEPTION_GUARD(delete reinterpret_cast<RocksDBStorage*>(pInstance));
}
void getConfig(CACHE_STORAGE* pStorage, CACHE_STORAGE_CONFIG* pConfig)
{
ss_dassert(pStorage);
MXS_EXCEPTION_GUARD(reinterpret_cast<RocksDBStorage*>(pStorage)->getConfig(pConfig));
}
cache_result_t getInfo(CACHE_STORAGE* pStorage,
uint32_t what,
json_t** ppInfo)
{
ss_dassert(pStorage);
cache_result_t result = CACHE_RESULT_ERROR;
MXS_EXCEPTION_GUARD(result = reinterpret_cast<RocksDBStorage*>(pStorage)->getInfo(what, ppInfo));
return result;
}
cache_result_t getKey(const char* zDefaultDB,
const GWBUF* pQuery,
CACHE_KEY* pKey)
{
// zDefaultDB may be NULL.
ss_dassert(pQuery);
ss_dassert(pKey);
cache_result_t result = CACHE_RESULT_ERROR;
MXS_EXCEPTION_GUARD(result = RocksDBStorage::GetKey(zDefaultDB, pQuery, pKey));
return result;
}
cache_result_t getValue(CACHE_STORAGE* pStorage,
const CACHE_KEY* pKey,
uint32_t flags,
GWBUF** ppResult)
{
ss_dassert(pStorage);
ss_dassert(pKey);
ss_dassert(ppResult);
cache_result_t result = CACHE_RESULT_ERROR;
MXS_EXCEPTION_GUARD(result = reinterpret_cast<RocksDBStorage*>(pStorage)->getValue(pKey,
flags,
ppResult));
return result;
}
cache_result_t putValue(CACHE_STORAGE* pStorage,
const CACHE_KEY* pKey,
const GWBUF* pValue)
{
ss_dassert(pStorage);
ss_dassert(pKey);
ss_dassert(pValue);
cache_result_t result = CACHE_RESULT_ERROR;
MXS_EXCEPTION_GUARD(result = reinterpret_cast<RocksDBStorage*>(pStorage)->putValue(pKey, pValue));
return result;
}
cache_result_t delValue(CACHE_STORAGE* pStorage,
const CACHE_KEY* pKey)
{
ss_dassert(pStorage);
ss_dassert(pKey);
cache_result_t result = CACHE_RESULT_ERROR;
MXS_EXCEPTION_GUARD(result = reinterpret_cast<RocksDBStorage*>(pStorage)->delValue(pKey));
return result;
}
cache_result_t getHead(CACHE_STORAGE* pstorage,
CACHE_KEY* pkey,
GWBUF** pphead)
{
ss_dassert(pstorage);
cache_result_t result = CACHE_RESULT_ERROR;
MXS_EXCEPTION_GUARD(result = reinterpret_cast<RocksDBStorage*>(pstorage)->getHead(pkey, pphead));
return result;
}
cache_result_t getTail(CACHE_STORAGE* pstorage,
CACHE_KEY* pkey,
GWBUF** pptail)
{
ss_dassert(pstorage);
cache_result_t result = CACHE_RESULT_ERROR;
MXS_EXCEPTION_GUARD(result = reinterpret_cast<RocksDBStorage*>(pstorage)->getTail(pkey, pptail));
return result;
}
cache_result_t getSize(CACHE_STORAGE* pstorage,
uint64_t* psize)
{
ss_dassert(pstorage);
cache_result_t result = CACHE_RESULT_ERROR;
MXS_EXCEPTION_GUARD(result = reinterpret_cast<RocksDBStorage*>(pstorage)->getSize(psize));
return result;
}
cache_result_t getItems(CACHE_STORAGE* pstorage,
uint64_t* pitems)
{
ss_dassert(pstorage);
cache_result_t result = CACHE_RESULT_ERROR;
MXS_EXCEPTION_GUARD(result = reinterpret_cast<RocksDBStorage*>(pstorage)->getItems(pitems));
return result;
}
}
extern "C" extern "C"
{ {
CACHE_STORAGE_API* CacheGetStorageAPI() CACHE_STORAGE_API* CacheGetStorageAPI()
{ {
static CACHE_STORAGE_API api = return &StorageModule<RocksDBStorage>::s_api;
{
initialize,
createInstance,
getKey,
freeInstance,
getConfig,
getInfo,
getValue,
putValue,
delValue,
getHead,
getTail,
getSize,
getItems
};
return &api;
} }
} }

View File

@ -0,0 +1,210 @@
#pragma once
/*
* Copyright (c) 2016 MariaDB Corporation Ab
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file and at www.mariadb.com/bsl.
*
* Change Date: 2019-07-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2 or later of the General
* Public License.
*/
#include <maxscale/cppdefs.hh>
template<class StorageType>
class StorageModule
{
public:
static bool initialize(uint32_t* pCapabilities)
{
return StorageType::Initialize(pCapabilities);
}
static CACHE_STORAGE* createInstance(const char* zName,
const CACHE_STORAGE_CONFIG* pConfig,
int argc, char* argv[])
{
ss_dassert(zName);
ss_dassert(pConfig);
StorageType* pStorage = NULL;
MXS_EXCEPTION_GUARD(pStorage = StorageType::Create_instance(zName, *pConfig, argc, argv));
return reinterpret_cast<CACHE_STORAGE*>(pStorage);
}
static 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 = StorageType::Get_key(zDefault_db, pQuery, pKey));
return result;
}
static void freeInstance(CACHE_STORAGE* pInstance)
{
MXS_EXCEPTION_GUARD(delete reinterpret_cast<StorageType*>(pInstance));
}
static void getConfig(CACHE_STORAGE* pCache_storage,
CACHE_STORAGE_CONFIG* pConfig)
{
ss_dassert(pCache_storage);
ss_dassert(pConfig);
StorageType* pStorage = reinterpret_cast<StorageType*>(pCache_storage);
MXS_EXCEPTION_GUARD(pStorage->get_config(pConfig));
}
static cache_result_t getInfo(CACHE_STORAGE* pCache_storage,
uint32_t what,
json_t** ppInfo)
{
ss_dassert(pCache_storage);
cache_result_t result = CACHE_RESULT_ERROR;
StorageType* pStorage = reinterpret_cast<StorageType*>(pCache_storage);
MXS_EXCEPTION_GUARD(result = pStorage->get_info(what, ppInfo));
return result;
}
static cache_result_t getValue(CACHE_STORAGE* pCache_storage,
const CACHE_KEY* pKey,
uint32_t flags,
GWBUF** ppResult)
{
ss_dassert(pCache_storage);
ss_dassert(pKey);
ss_dassert(ppResult);
cache_result_t result = CACHE_RESULT_ERROR;
StorageType* pStorage = reinterpret_cast<StorageType*>(pCache_storage);
MXS_EXCEPTION_GUARD(result = pStorage->get_value(*pKey, flags, ppResult));
return result;
}
static cache_result_t putValue(CACHE_STORAGE* pCache_storage,
const CACHE_KEY* pKey,
const GWBUF* pValue)
{
ss_dassert(pCache_storage);
ss_dassert(pKey);
ss_dassert(pValue);
cache_result_t result = CACHE_RESULT_ERROR;
StorageType* pStorage = reinterpret_cast<StorageType*>(pCache_storage);
MXS_EXCEPTION_GUARD(result = pStorage->put_value(*pKey, pValue));
return result;
}
static cache_result_t delValue(CACHE_STORAGE* pCache_storage, const CACHE_KEY* pKey)
{
ss_dassert(pCache_storage);
ss_dassert(pKey);
cache_result_t result = CACHE_RESULT_ERROR;
StorageType* pStorage = reinterpret_cast<StorageType*>(pCache_storage);
MXS_EXCEPTION_GUARD(result = pStorage->del_value(*pKey));
return result;
}
static cache_result_t getHead(CACHE_STORAGE* pCache_storage,
CACHE_KEY* pKey,
GWBUF** ppHead)
{
ss_dassert(pCache_storage);
cache_result_t result = CACHE_RESULT_ERROR;
StorageType* pStorage = reinterpret_cast<StorageType*>(pCache_storage);
MXS_EXCEPTION_GUARD(result = pStorage->get_head(pKey, ppHead));
return result;
}
static cache_result_t getTail(CACHE_STORAGE* pCache_storage,
CACHE_KEY* pKey,
GWBUF** ppTail)
{
ss_dassert(pCache_storage);
cache_result_t result = CACHE_RESULT_ERROR;
StorageType* pStorage = reinterpret_cast<StorageType*>(pCache_storage);
MXS_EXCEPTION_GUARD(result = pStorage->get_tail(pKey, ppTail));
return result;
}
static cache_result_t getSize(CACHE_STORAGE* pCache_storage, uint64_t* pSize)
{
ss_dassert(pCache_storage);
cache_result_t result = CACHE_RESULT_ERROR;
StorageType* pStorage = reinterpret_cast<StorageType*>(pCache_storage);
MXS_EXCEPTION_GUARD(result = pStorage->get_size(pSize));
return result;
}
static cache_result_t getItems(CACHE_STORAGE* pCache_storage, uint64_t* pItems)
{
ss_dassert(pCache_storage);
cache_result_t result = CACHE_RESULT_ERROR;
StorageType* pStorage = reinterpret_cast<StorageType*>(pCache_storage);
MXS_EXCEPTION_GUARD(result = pStorage->get_items(pItems));
return result;
}
static CACHE_STORAGE_API s_api;
};
template<class StorageType>
CACHE_STORAGE_API StorageModule<StorageType>::s_api =
{
&StorageModule<StorageType>::initialize,
&StorageModule<StorageType>::createInstance,
&StorageModule<StorageType>::getKey,
&StorageModule<StorageType>::freeInstance,
&StorageModule<StorageType>::getConfig,
&StorageModule<StorageType>::getInfo,
&StorageModule<StorageType>::getValue,
&StorageModule<StorageType>::putValue,
&StorageModule<StorageType>::delValue,
&StorageModule<StorageType>::getHead,
&StorageModule<StorageType>::getTail,
&StorageModule<StorageType>::getSize,
&StorageModule<StorageType>::getItems
};