Cache: Make storage_rocksdb exception safe

Now RocksDBStorage is exception safe in its own right, so exception
guards are only needed at the C/C++ boundary.
This commit is contained in:
Johan Wikman
2016-12-09 10:12:56 +02:00
parent bf35577941
commit 16fad9c2cd
3 changed files with 21 additions and 15 deletions

View File

@ -223,7 +223,9 @@ bool RocksDBStorage::Initialize()
} }
//static //static
RocksDBStorage* RocksDBStorage::Create(const char* zName, uint32_t ttl, int argc, char* argv[]) unique_ptr<RocksDBStorage> RocksDBStorage::Create(const char* zName,
uint32_t ttl,
int argc, char* argv[])
{ {
ss_dassert(zName); ss_dassert(zName);
@ -278,12 +280,12 @@ RocksDBStorage* RocksDBStorage::Create(const char* zName, uint32_t ttl, int argc
} }
// static // static
RocksDBStorage* RocksDBStorage::Create(const string& storageDirectory, unique_ptr<RocksDBStorage> RocksDBStorage::Create(const string& storageDirectory,
const char* zName, const char* zName,
uint32_t ttl, uint32_t ttl,
bool collectStatistics) bool collectStatistics)
{ {
RocksDBStorage* pStorage = nullptr; unique_ptr<RocksDBStorage> sStorage;
if (mkdir(storageDirectory.c_str(), S_IRWXU) == 0) if (mkdir(storageDirectory.c_str(), S_IRWXU) == 0)
{ {
@ -341,7 +343,7 @@ RocksDBStorage* RocksDBStorage::Create(const string& storageDirectory,
unique_ptr<rocksdb::DBWithTTL> sDb(pDb); unique_ptr<rocksdb::DBWithTTL> sDb(pDb);
pStorage = new RocksDBStorage(sDb, zName, path, ttl); sStorage = unique_ptr<RocksDBStorage>(new RocksDBStorage(sDb, zName, path, ttl));
} }
else else
{ {
@ -356,7 +358,7 @@ RocksDBStorage* RocksDBStorage::Create(const string& storageDirectory,
} }
} }
return pStorage; return sStorage;
} }
cache_result_t RocksDBStorage::getInfo(uint32_t what, json_t** ppInfo) const cache_result_t RocksDBStorage::getInfo(uint32_t what, json_t** ppInfo) const

View File

@ -21,9 +21,11 @@
class RocksDBStorage class RocksDBStorage
{ {
public: public:
typedef std::unique_ptr<RocksDBStorage> SRocksDBStorage;
static bool Initialize(); static bool Initialize();
static RocksDBStorage* Create(const char* zName, uint32_t ttl, int argc, char* argv[]); static SRocksDBStorage Create(const char* zName, uint32_t ttl, int argc, char* argv[]);
~RocksDBStorage(); ~RocksDBStorage();
cache_result_t getInfo(uint32_t flags, json_t** ppInfo) const; cache_result_t getInfo(uint32_t flags, json_t** ppInfo) const;
@ -41,7 +43,7 @@ private:
RocksDBStorage(const RocksDBStorage&) = delete; RocksDBStorage(const RocksDBStorage&) = delete;
RocksDBStorage& operator = (const RocksDBStorage&) = delete; RocksDBStorage& operator = (const RocksDBStorage&) = delete;
static RocksDBStorage* Create(const std::string& storageDirectory, static SRocksDBStorage Create(const std::string& storageDirectory,
const char* zName, const char* zName,
uint32_t ttl, uint32_t ttl,
bool collectStatistics); bool collectStatistics);

View File

@ -17,6 +17,8 @@
#include "../../cache_storage_api.h" #include "../../cache_storage_api.h"
#include "rocksdbstorage.hh" #include "rocksdbstorage.hh"
using std::unique_ptr;
namespace namespace
{ {
@ -48,21 +50,21 @@ CACHE_STORAGE* createInstance(cache_thread_model_t, // Ignored, RocksDB always M
"does not enforce such a limit.", (unsigned long)maxSize); "does not enforce such a limit.", (unsigned long)maxSize);
} }
RocksDBStorage* pStorage = NULL; unique_ptr<RocksDBStorage> sStorage;
MXS_EXCEPTION_GUARD(pStorage = RocksDBStorage::Create(zName, ttl, argc, argv)); MXS_EXCEPTION_GUARD(sStorage = RocksDBStorage::Create(zName, ttl, argc, argv));
if (pStorage) if (sStorage)
{ {
MXS_NOTICE("Storage module created."); MXS_NOTICE("Storage module created.");
} }
return reinterpret_cast<CACHE_STORAGE*>(pStorage); return reinterpret_cast<CACHE_STORAGE*>(sStorage.release());
} }
void freeInstance(CACHE_STORAGE* pInstance) void freeInstance(CACHE_STORAGE* pInstance)
{ {
delete reinterpret_cast<RocksDBStorage*>(pInstance); MXS_EXCEPTION_GUARD(delete reinterpret_cast<RocksDBStorage*>(pInstance));
} }
cache_result_t getInfo(CACHE_STORAGE* pStorage, cache_result_t getInfo(CACHE_STORAGE* pStorage,