From fa58fdcde89655c425d248a6460aa8a209290e2e Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Mon, 28 Nov 2016 14:08:11 +0200 Subject: [PATCH] Cache: Make Storage abstract Storage is now abstract with StorageReal (that uses an actual storage module) derived from it. This to prepare for a stackable case where LRU behaviour is implemented in front of the actual physcal storage. --- server/modules/filter/cache/CMakeLists.txt | 2 +- server/modules/filter/cache/storage.cc | 31 +---------- server/modules/filter/cache/storage.h | 34 +++++------- server/modules/filter/cache/storagefactory.cc | 7 +-- server/modules/filter/cache/storagereal.cc | 53 +++++++++++++++++++ server/modules/filter/cache/storagereal.h | 47 ++++++++++++++++ 6 files changed, 118 insertions(+), 56 deletions(-) create mode 100644 server/modules/filter/cache/storagereal.cc create mode 100644 server/modules/filter/cache/storagereal.h diff --git a/server/modules/filter/cache/CMakeLists.txt b/server/modules/filter/cache/CMakeLists.txt index 12219a615..94ba9e012 100644 --- a/server/modules/filter/cache/CMakeLists.txt +++ b/server/modules/filter/cache/CMakeLists.txt @@ -1,5 +1,5 @@ if (JANSSON_FOUND) - add_library(cache SHARED cache.cc cachefilter.cc cachemt.cc cachept.cc cachesimple.cc cachest.cc rules.cc sessioncache.cc storage.cc storagefactory.cc) + add_library(cache SHARED cache.cc cachefilter.cc cachemt.cc cachept.cc cachesimple.cc cachest.cc rules.cc sessioncache.cc storage.cc storagefactory.cc storagereal.cc) target_link_libraries(cache maxscale-common jansson) set_target_properties(cache PROPERTIES VERSION "1.0.0") set_target_properties(cache PROPERTIES LINK_FLAGS -Wl,-z,defs) diff --git a/server/modules/filter/cache/storage.cc b/server/modules/filter/cache/storage.cc index 1453eff29..ff446c3d8 100644 --- a/server/modules/filter/cache/storage.cc +++ b/server/modules/filter/cache/storage.cc @@ -15,39 +15,10 @@ #include "storage.h" -Storage::Storage(CACHE_STORAGE_API* pApi, CACHE_STORAGE* pStorage) - : m_pApi(pApi) - , m_pStorage(pStorage) +Storage::Storage() { - ss_dassert(m_pApi); - ss_dassert(m_pStorage); } Storage::~Storage() { } - -cache_result_t Storage::get_key(const char* zDefaultDb, - const GWBUF* pQuery, - CACHE_KEY* pKey) -{ - return m_pApi->getKey(m_pStorage, zDefaultDb, pQuery, pKey); -} - -cache_result_t Storage::get_value(const CACHE_KEY& key, - uint32_t flags, - GWBUF** ppValue) -{ - return m_pApi->getValue(m_pStorage, &key, flags, ppValue); -} - -cache_result_t Storage::put_value(const CACHE_KEY& key, - const GWBUF* pValue) -{ - return m_pApi->putValue(m_pStorage, &key, pValue); -} - -cache_result_t Storage::del_value(const CACHE_KEY& key) -{ - return m_pApi->delValue(m_pStorage, &key); -} diff --git a/server/modules/filter/cache/storage.h b/server/modules/filter/cache/storage.h index 247b6db98..2f1a707b1 100644 --- a/server/modules/filter/cache/storage.h +++ b/server/modules/filter/cache/storage.h @@ -1,6 +1,4 @@ #pragma once -#ifndef _MAXSCALE_FILTER_CACHE_STORAGE_H -#define _MAXSCALE_FILTER_CACHE_STORAGE_H /* * Copyright (c) 2016 MariaDB Corporation Ab * @@ -20,32 +18,24 @@ class Storage { public: - ~Storage(); + virtual ~Storage(); - cache_result_t get_key(const char* zDefaultDb, - const GWBUF* pQuery, - CACHE_KEY* pKey); + virtual cache_result_t get_key(const char* zDefaultDb, + const GWBUF* pQuery, + CACHE_KEY* pKey) = 0; - cache_result_t get_value(const CACHE_KEY& key, - uint32_t flags, - GWBUF** ppValue); + virtual cache_result_t get_value(const CACHE_KEY& key, + uint32_t flags, + GWBUF** ppValue) = 0; - cache_result_t put_value(const CACHE_KEY& key, - const GWBUF* pValue); + virtual cache_result_t put_value(const CACHE_KEY& key, + const GWBUF* pValue) = 0; - cache_result_t del_value(const CACHE_KEY& key); + virtual cache_result_t del_value(const CACHE_KEY& key) = 0; -private: - friend class StorageFactory; - - Storage(CACHE_STORAGE_API* pApi, CACHE_STORAGE* pStorage); +protected: + Storage(); Storage(const Storage&); Storage& operator = (const Storage&); - -private: - CACHE_STORAGE_API* m_pApi; - CACHE_STORAGE* m_pStorage; }; - -#endif diff --git a/server/modules/filter/cache/storagefactory.cc b/server/modules/filter/cache/storagefactory.cc index 8dd49f0e4..a80861adb 100644 --- a/server/modules/filter/cache/storagefactory.cc +++ b/server/modules/filter/cache/storagefactory.cc @@ -19,7 +19,8 @@ #include #include #include -#include "storage.h" +#include "cachefilter.h" +#include "storagereal.h" namespace @@ -120,7 +121,7 @@ StorageFactory* StorageFactory::Open(const char* zName) if (open_cache_storage(zName, &handle, &pApi)) { - pFactory = new (std::nothrow) StorageFactory(handle, pApi); + CPP_GUARD(pFactory = new StorageFactory(handle, pApi)); if (!pFactory) { @@ -144,7 +145,7 @@ Storage* StorageFactory::createStorage(cache_thread_model_t model, if (pRawStorage) { - pStorage = new (std::nothrow) Storage(m_pApi, pRawStorage); + CPP_GUARD(pStorage = new StorageReal(m_pApi, pRawStorage)); if (!pStorage) { diff --git a/server/modules/filter/cache/storagereal.cc b/server/modules/filter/cache/storagereal.cc new file mode 100644 index 000000000..ee0e08b6e --- /dev/null +++ b/server/modules/filter/cache/storagereal.cc @@ -0,0 +1,53 @@ +/* + * 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. + */ + +#define MXS_MODULE_NAME "cache" +#include "storagereal.h" + + +StorageReal::StorageReal(CACHE_STORAGE_API* pApi, CACHE_STORAGE* pStorage) + : m_pApi(pApi) + , m_pStorage(pStorage) +{ + ss_dassert(m_pApi); + ss_dassert(m_pStorage); +} + +StorageReal::~StorageReal() +{ +} + +cache_result_t StorageReal::get_key(const char* zDefaultDb, + const GWBUF* pQuery, + CACHE_KEY* pKey) +{ + return m_pApi->getKey(m_pStorage, zDefaultDb, pQuery, pKey); +} + +cache_result_t StorageReal::get_value(const CACHE_KEY& key, + uint32_t flags, + GWBUF** ppValue) +{ + return m_pApi->getValue(m_pStorage, &key, flags, ppValue); +} + +cache_result_t StorageReal::put_value(const CACHE_KEY& key, + const GWBUF* pValue) +{ + return m_pApi->putValue(m_pStorage, &key, pValue); +} + +cache_result_t StorageReal::del_value(const CACHE_KEY& key) +{ + return m_pApi->delValue(m_pStorage, &key); +} diff --git a/server/modules/filter/cache/storagereal.h b/server/modules/filter/cache/storagereal.h new file mode 100644 index 000000000..165128ead --- /dev/null +++ b/server/modules/filter/cache/storagereal.h @@ -0,0 +1,47 @@ +#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 +#include "storage.h" + +class StorageReal : public Storage +{ +public: + ~StorageReal(); + + cache_result_t get_key(const char* zDefaultDb, + const GWBUF* pQuery, + CACHE_KEY* pKey); + + cache_result_t get_value(const CACHE_KEY& key, + uint32_t flags, + GWBUF** ppValue); + + cache_result_t put_value(const CACHE_KEY& key, + const GWBUF* pValue); + + cache_result_t del_value(const CACHE_KEY& key); + +private: + friend class StorageFactory; + + StorageReal(CACHE_STORAGE_API* pApi, CACHE_STORAGE* pStorage); + + StorageReal(const StorageReal&); + StorageReal& operator = (const StorageReal&); + +private: + CACHE_STORAGE_API* m_pApi; + CACHE_STORAGE* m_pStorage; +};