From 3967a0b8c20e6d93e324014ffaaa6508b4c67af5 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Tue, 20 Dec 2016 13:10:06 +0200 Subject: [PATCH] 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. --- .../storage_inmemory/inmemorystorage.cc | 55 ++++- .../storage_inmemory/inmemorystorage.hh | 8 +- .../storage_inmemory/storage_inmemory.cc | 219 +----------------- .../storage/storage_rocksdb/rocksdbstorage.cc | 58 +++-- .../storage/storage_rocksdb/rocksdbstorage.hh | 30 +-- .../storage_rocksdb/storage_rocksdb.cc | 200 +--------------- .../filter/cache/storage/storagemodule.hh | 210 +++++++++++++++++ 7 files changed, 316 insertions(+), 464 deletions(-) create mode 100644 server/modules/filter/cache/storage/storagemodule.hh diff --git a/server/modules/filter/cache/storage/storage_inmemory/inmemorystorage.cc b/server/modules/filter/cache/storage/storage_inmemory/inmemorystorage.cc index 67f531a36..3ce9453cd 100644 --- a/server/modules/filter/cache/storage/storage_inmemory/inmemorystorage.cc +++ b/server/modules/filter/cache/storage/storage_inmemory/inmemorystorage.cc @@ -15,11 +15,15 @@ #include "inmemorystorage.hh" #include #include +#include #include #include #include #include +#include "inmemorystoragest.hh" +#include "inmemorystoragemt.hh" +using std::auto_ptr; using std::set; using std::string; @@ -45,8 +49,55 @@ InMemoryStorage::~InMemoryStorage() { } -// static -cache_result_t InMemoryStorage::get_key(const char* zdefault_db, const GWBUF* pquery, CACHE_KEY* pkey) +bool InMemoryStorage::Initialize(uint32_t* pcapabilities) +{ + *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 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)); diff --git a/server/modules/filter/cache/storage/storage_inmemory/inmemorystorage.hh b/server/modules/filter/cache/storage/storage_inmemory/inmemorystorage.hh index 456d46ffa..65c94ec47 100644 --- a/server/modules/filter/cache/storage/storage_inmemory/inmemorystorage.hh +++ b/server/modules/filter/cache/storage/storage_inmemory/inmemorystorage.hh @@ -24,7 +24,13 @@ class InMemoryStorage public: 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); virtual cache_result_t get_info(uint32_t what, json_t** ppInfo) const = 0; diff --git a/server/modules/filter/cache/storage/storage_inmemory/storage_inmemory.cc b/server/modules/filter/cache/storage/storage_inmemory/storage_inmemory.cc index 98e9eb931..6099e11c8 100644 --- a/server/modules/filter/cache/storage/storage_inmemory/storage_inmemory.cc +++ b/server/modules/filter/cache/storage/storage_inmemory/storage_inmemory.cc @@ -13,231 +13,16 @@ #define MXS_MODULE_NAME "storage_inmemory" #include -#include #include "../../cache_storage_api.h" +#include "../storagemodule.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 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(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(pinstance)); -} - -void getConfig(CACHE_STORAGE* pStorage, - CACHE_STORAGE_CONFIG* pConfig) -{ - ss_dassert(pStorage); - ss_dassert(pConfig); - - MXS_EXCEPTION_GUARD(reinterpret_cast(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(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(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(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(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(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(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(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(pstorage)->get_items(pitems)); - - return result; -} - -} extern "C" { CACHE_STORAGE_API* CacheGetStorageAPI() { - static CACHE_STORAGE_API api = - { - initialize, - createInstance, - getKey, - freeInstance, - getConfig, - getInfo, - getValue, - putValue, - delValue, - getHead, - getTail, - getSize, - getItems - }; - - return &api; + return &StorageModule::s_api; } } diff --git a/server/modules/filter/cache/storage/storage_rocksdb/rocksdbstorage.cc b/server/modules/filter/cache/storage/storage_rocksdb/rocksdbstorage.cc index 483d66adc..dbe467f1e 100644 --- a/server/modules/filter/cache/storage/storage_rocksdb/rocksdbstorage.cc +++ b/server/modules/filter/cache/storage/storage_rocksdb/rocksdbstorage.cc @@ -208,9 +208,10 @@ RocksDBStorage::~RocksDBStorage() { } -//static -bool RocksDBStorage::Initialize() +bool RocksDBStorage::Initialize(uint32_t* pCapabilities) { + *pCapabilities = CACHE_STORAGE_CAP_MT; + auto pEnv = rocksdb::Env::Default(); pEnv->SetBackgroundThreads(ROCKSDB_N_LOW_THREADS, rocksdb::Env::LOW); pEnv->SetBackgroundThreads(ROCKSDB_N_HIGH_THREADS, rocksdb::Env::HIGH); @@ -222,10 +223,9 @@ bool RocksDBStorage::Initialize() return true; } -//static -unique_ptr RocksDBStorage::Create(const char* zName, - const CACHE_STORAGE_CONFIG& config, - int argc, char* argv[]) +RocksDBStorage* RocksDBStorage::Create_instance(const char* zName, + const CACHE_STORAGE_CONFIG& config, + int argc, char* argv[]) { ss_dassert(zName); @@ -279,11 +279,10 @@ unique_ptr RocksDBStorage::Create(const char* zName, return Create(zName, config, storageDirectory, collectStatistics); } -// static -unique_ptr RocksDBStorage::Create(const char* zName, - const CACHE_STORAGE_CONFIG& config, - const string& storageDirectory, - bool collectStatistics) +RocksDBStorage* RocksDBStorage::Create(const char* zName, + const CACHE_STORAGE_CONFIG& config, + const string& storageDirectory, + bool collectStatistics) { unique_ptr sStorage; @@ -358,11 +357,10 @@ unique_ptr RocksDBStorage::Create(const char* zName, } } - return sStorage; + return sStorage.release(); } -// static -cache_result_t RocksDBStorage::GetKey(const char* zDefaultDB, const GWBUF* pQuery, CACHE_KEY* pKey) +cache_result_t RocksDBStorage::Get_key(const char* zDefaultDB, const GWBUF* pQuery, CACHE_KEY* pKey) { 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; } -void RocksDBStorage::getConfig(CACHE_STORAGE_CONFIG* pConfig) +void RocksDBStorage::get_config(CACHE_STORAGE_CONFIG* pConfig) { *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(); @@ -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; } -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. 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; - 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; @@ -512,45 +510,43 @@ cache_result_t RocksDBStorage::getValue(const CACHE_KEY* pKey, uint32_t flags, G 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)); - 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::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; } -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(), key); + rocksdb::Status status = m_sDb->Delete(writeOptions(), rocksdb_key); 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; } -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; } -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; } -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; } diff --git a/server/modules/filter/cache/storage/storage_rocksdb/rocksdbstorage.hh b/server/modules/filter/cache/storage/storage_rocksdb/rocksdbstorage.hh index 09296aec1..440108348 100644 --- a/server/modules/filter/cache/storage/storage_rocksdb/rocksdbstorage.hh +++ b/server/modules/filter/cache/storage/storage_rocksdb/rocksdbstorage.hh @@ -23,25 +23,25 @@ class RocksDBStorage public: typedef std::unique_ptr SRocksDBStorage; - static bool Initialize(); + static bool Initialize(uint32_t* pCapabilities); - static SRocksDBStorage Create(const char* zName, - const CACHE_STORAGE_CONFIG& config, - int argc, char* argv[]); + static RocksDBStorage* Create_instance(const char* zName, + const CACHE_STORAGE_CONFIG& config, + int argc, char* argv[]); ~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); - cache_result_t getInfo(uint32_t flags, json_t** ppInfo) const; - 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); + void get_config(CACHE_STORAGE_CONFIG* pConfig); + cache_result_t get_info(uint32_t flags, json_t** ppInfo) const; + cache_result_t get_value(const CACHE_KEY& key, uint32_t flags, GWBUF** ppResult); + cache_result_t put_value(const CACHE_KEY& key, const GWBUF* pValue); + cache_result_t del_value(const CACHE_KEY& key); - cache_result_t getHead(CACHE_KEY* pKey, GWBUF** ppHead) const; - cache_result_t getTail(CACHE_KEY* pKey, GWBUF** ppHead) const; - cache_result_t getSize(uint64_t* pSize) const; - cache_result_t getItems(uint64_t* pItems) const; + cache_result_t get_head(CACHE_KEY* pKey, GWBUF** ppHead) const; + cache_result_t get_tail(CACHE_KEY* pKey, GWBUF** ppHead) const; + cache_result_t get_size(uint64_t* pSize) const; + cache_result_t get_items(uint64_t* pItems) const; private: RocksDBStorage(const std::string& name, @@ -52,7 +52,7 @@ private: RocksDBStorage(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 std::string& storageDirectory, bool collectStatistics); diff --git a/server/modules/filter/cache/storage/storage_rocksdb/storage_rocksdb.cc b/server/modules/filter/cache/storage/storage_rocksdb/storage_rocksdb.cc index b9d44e90f..52f95589d 100644 --- a/server/modules/filter/cache/storage/storage_rocksdb/storage_rocksdb.cc +++ b/server/modules/filter/cache/storage/storage_rocksdb/storage_rocksdb.cc @@ -13,213 +13,17 @@ #define MXS_MODULE_NAME "storage_rocksdb" #include -#include #include "../../cache_storage_api.h" +#include "../storagemodule.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 sStorage; - - MXS_EXCEPTION_GUARD(sStorage = RocksDBStorage::Create(zName, *pConfig, argc, argv)); - - if (sStorage) - { - MXS_NOTICE("Storage module created."); - } - - return reinterpret_cast(sStorage.release()); -} - -void freeInstance(CACHE_STORAGE* pInstance) -{ - MXS_EXCEPTION_GUARD(delete reinterpret_cast(pInstance)); -} - -void getConfig(CACHE_STORAGE* pStorage, CACHE_STORAGE_CONFIG* pConfig) -{ - ss_dassert(pStorage); - - MXS_EXCEPTION_GUARD(reinterpret_cast(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(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(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(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(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(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(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(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(pstorage)->getItems(pitems)); - - return result; -} -} extern "C" { CACHE_STORAGE_API* CacheGetStorageAPI() { - static CACHE_STORAGE_API api = - { - initialize, - createInstance, - getKey, - freeInstance, - getConfig, - getInfo, - getValue, - putValue, - delValue, - getHead, - getTail, - getSize, - getItems - }; - - return &api; + return &StorageModule::s_api; } } diff --git a/server/modules/filter/cache/storage/storagemodule.hh b/server/modules/filter/cache/storage/storagemodule.hh new file mode 100644 index 000000000..e134e5b2a --- /dev/null +++ b/server/modules/filter/cache/storage/storagemodule.hh @@ -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 + +template +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(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(pInstance)); + } + + static void getConfig(CACHE_STORAGE* pCache_storage, + CACHE_STORAGE_CONFIG* pConfig) + { + ss_dassert(pCache_storage); + ss_dassert(pConfig); + + StorageType* pStorage = reinterpret_cast(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(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(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(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(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(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(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(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(pCache_storage); + + MXS_EXCEPTION_GUARD(result = pStorage->get_items(pItems)); + + return result; + } + + static CACHE_STORAGE_API s_api; +}; + +template +CACHE_STORAGE_API StorageModule::s_api = +{ + &StorageModule::initialize, + &StorageModule::createInstance, + &StorageModule::getKey, + &StorageModule::freeInstance, + &StorageModule::getConfig, + &StorageModule::getInfo, + &StorageModule::getValue, + &StorageModule::putValue, + &StorageModule::delValue, + &StorageModule::getHead, + &StorageModule::getTail, + &StorageModule::getSize, + &StorageModule::getItems +};