2020-07-11 21:23:48 +00:00
|
|
|
#pragma once
|
|
|
|
#include "sds.h"
|
|
|
|
|
|
|
|
class StorageCache
|
|
|
|
{
|
|
|
|
std::shared_ptr<IStorage> m_spstorage;
|
2021-03-04 07:41:06 +00:00
|
|
|
dict *m_pdict = nullptr;
|
|
|
|
int m_collisionCount = 0;
|
2020-08-09 23:36:20 +00:00
|
|
|
mutable fastlock m_lock {"StorageCache"};
|
2021-06-01 19:15:01 +00:00
|
|
|
std::atomic<int> bulkInsertsInProgress;
|
2020-07-11 21:23:48 +00:00
|
|
|
|
2021-03-05 00:54:11 +00:00
|
|
|
StorageCache(IStorage *storage, bool fNoCache);
|
2020-07-11 21:23:48 +00:00
|
|
|
|
|
|
|
void cacheKey(sds key);
|
|
|
|
void cacheKey(const char *rgchKey, size_t cchKey);
|
|
|
|
|
|
|
|
struct load_iter_data
|
|
|
|
{
|
|
|
|
StorageCache *cache;
|
|
|
|
IStorageFactory::key_load_iterator itrParent;
|
|
|
|
void *privdataParent;
|
|
|
|
};
|
|
|
|
static void key_load_itr(const char *rgchKey, size_t cchKey, void *privdata)
|
|
|
|
{
|
|
|
|
load_iter_data *data = (load_iter_data*)privdata;
|
|
|
|
data->cache->cacheKey(rgchKey, cchKey);
|
|
|
|
if (data->itrParent)
|
|
|
|
data->itrParent(rgchKey, cchKey, data->privdataParent);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2021-07-19 15:10:48 +00:00
|
|
|
~StorageCache();
|
|
|
|
|
2020-07-11 21:23:48 +00:00
|
|
|
static StorageCache *create(IStorageFactory *pfactory, int db, IStorageFactory::key_load_iterator fn, void *privdata) {
|
2021-03-05 00:54:11 +00:00
|
|
|
StorageCache *cache = new StorageCache(nullptr, pfactory->FSlow() /*fCache*/);
|
2020-07-11 21:23:48 +00:00
|
|
|
load_iter_data data = {cache, fn, privdata};
|
|
|
|
cache->m_spstorage = std::shared_ptr<IStorage>(pfactory->create(db, key_load_itr, (void*)&data));
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear();
|
|
|
|
void insert(sds key, const void *data, size_t cbdata, bool fOverwrite);
|
2021-06-01 19:15:01 +00:00
|
|
|
void bulkInsert(sds *rgkeys, sds *rgvals, size_t celem);
|
2021-03-04 07:41:06 +00:00
|
|
|
void retrieve(sds key, IStorage::callbackSingle fn) const;
|
2020-07-11 21:23:48 +00:00
|
|
|
bool erase(sds key);
|
|
|
|
|
|
|
|
bool enumerate(IStorage::callback fn) const { return m_spstorage->enumerate(fn); }
|
|
|
|
|
|
|
|
void beginWriteBatch();
|
2021-06-14 03:50:47 +00:00
|
|
|
void endWriteBatch() { m_spstorage->endWriteBatch(); }
|
2020-07-11 21:23:48 +00:00
|
|
|
void batch_lock() { return m_spstorage->batch_lock(); }
|
|
|
|
void batch_unlock() { return m_spstorage->batch_unlock(); }
|
|
|
|
|
|
|
|
size_t count() const;
|
|
|
|
|
|
|
|
const StorageCache *clone();
|
2021-06-14 03:50:47 +00:00
|
|
|
};
|