Cache: Separate C and C++ functionality

cache_storage_api.h contains pure C declarations, while
cache_storage_api.hh contains C++ declarations. Functions dealing
with CACHE_KEY moved to these headers.
This commit is contained in:
Johan Wikman
2016-12-08 22:39:46 +02:00
parent 855ed415f8
commit c9bc7a6e76
10 changed files with 157 additions and 94 deletions

View File

@ -3,6 +3,8 @@ if (JANSSON_FOUND)
cache.cc
cachefilter.cc
cachefiltersession.cc
cache_storage_api.c
cache_storage_api.cc
cachemt.cc
cachept.cc
cachesimple.cc

View File

@ -0,0 +1,44 @@
/*
* 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 "cache_storage_api.h"
size_t cache_key_hash(const CACHE_KEY* key)
{
ss_dassert(key);
size_t hash = 0;
const char* i = key->data;
const char* end = i + CACHE_KEY_MAXLEN;
while (i < end)
{
int c = *i;
hash = c + (hash << 6) + (hash << 16) - hash;
++i;
}
return hash;
}
bool cache_key_equal_to(const CACHE_KEY* lhs, const CACHE_KEY* rhs)
{
ss_dassert(lhs);
ss_dassert(rhs);
return memcmp(lhs->data, rhs->data, CACHE_KEY_MAXLEN) == 0;
}

View File

@ -0,0 +1,36 @@
/*
* 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 "cache_storage_api.hh"
#include <ctype.h>
using std::string;
std::string cache_key_to_string(const CACHE_KEY& key)
{
string s;
for (int i = 0; i < CACHE_KEY_MAXLEN; ++i)
{
char c = key.data[i];
if (!isprint(c))
{
c = '.';
}
s += c;
}
return s;
}

View File

@ -1,6 +1,4 @@
#pragma once
#ifndef _MAXSCALE_FILTER_CACHE_CACHE_STORAGE_API_H
#define _MAXSCALE_FILTER_CACHE_CACHE_STORAGE_API_H
/*
* Copyright (c) 2016 MariaDB Corporation Ab
*
@ -14,6 +12,7 @@
* Public License.
*/
#include <maxscale/cdefs.h>
#include <stdbool.h>
#include <stdint.h>
#include <jansson.h>
@ -62,6 +61,25 @@ typedef struct cache_key
char data[CACHE_KEY_MAXLEN];
} CACHE_KEY;
/**
* Hashes a CACHE_KEY to a size_t
*
* @param key The key to be hashed.
*
* @return The corresponding hash.
*/
size_t cache_key_hash(const CACHE_KEY* key);
/**
* Are two CACHE_KEYs equal.
*
* @param lhs One cache key.
* @param rhs Another cache key.
*
* @return True, if the keys are equal.
*/
bool cache_key_equal_to(const CACHE_KEY* lhs, const CACHE_KEY* rhs);
typedef enum cache_storage_capabilities
{
CACHE_STORAGE_CAP_NONE = 0x00,
@ -204,5 +222,3 @@ typedef struct cache_storage_api
typedef CACHE_STORAGE_API* (*CacheGetStorageAPIFN)();
MXS_END_DECLS
#endif

View File

@ -0,0 +1,51 @@
#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/cdefs.h>
#include <functional>
#include <string>
#include <tr1/functional>
#include "cache_storage_api.h"
namespace std
{
template<>
struct equal_to<CACHE_KEY>
{
bool operator()(const CACHE_KEY& lhs, const CACHE_KEY& rhs) const
{
return cache_key_equal_to(&lhs, &rhs);
}
};
namespace tr1
{
template<>
struct hash<CACHE_KEY>
{
size_t operator()(const CACHE_KEY& key) const
{
return cache_key_hash(&key);
}
};
}
}
std::string cache_key_to_string(const CACHE_KEY& key);

View File

@ -674,59 +674,3 @@ void cache_config_reset(CACHE_CONFIG& config)
{
memset(&config, 0, sizeof(config));
}
/**
* Hashes a CACHE_KEY to size_t.
*
* @param key The key to be hashed.
*
* @return The corresponding hash.
*/
size_t cache_key_hash(const CACHE_KEY& key)
{
size_t hash = 0;
const char* i = key.data;
const char* end = i + CACHE_KEY_MAXLEN;
while (i < end)
{
int c = *i;
hash = c + (hash << 6) + (hash << 16) - hash;
++i;
}
return hash;
}
/**
* Are two CACHE_KEYs equal.
*
* @param lhs One cache key.
* @param rhs Another cache key.
*
* @return True, if the keys are equal.
*/
bool cache_key_equal_to(const CACHE_KEY& lhs, const CACHE_KEY& rhs)
{
return memcmp(lhs.data, rhs.data, CACHE_KEY_MAXLEN) == 0;
}
std::string cache_key_to_string(const CACHE_KEY& key)
{
string s;
for (int i = 0; i < CACHE_KEY_MAXLEN; ++i)
{
char c = key.data[i];
if (!isprint(c))
{
c = '.';
}
s += c;
}
return s;
}

View File

@ -81,39 +81,6 @@ void cache_config_finish(CACHE_CONFIG& config);
void cache_config_free(CACHE_CONFIG* pConfig);
void cache_config_reset(CACHE_CONFIG& config);
size_t cache_key_hash(const CACHE_KEY& key);
bool cache_key_equal_to(const CACHE_KEY& lhs, const CACHE_KEY& rhs);
std::string cache_key_to_string(const CACHE_KEY& key);
namespace std
{
template<>
struct equal_to<CACHE_KEY>
{
bool operator()(const CACHE_KEY& lhs, const CACHE_KEY& rhs) const
{
return cache_key_equal_to(lhs, rhs);
}
};
namespace tr1
{
template<>
struct hash<CACHE_KEY>
{
size_t operator()(const CACHE_KEY& key) const
{
return cache_key_hash(key);
}
};
}
}
/**
* LockGuard is a RAII class whose constructor acquires a spinlock and
* destructor releases the same spinlock. To be used for locking a spinlock

View File

@ -16,6 +16,7 @@
#include <tr1/unordered_map>
#include <maxscale/hashtable.h>
#include "cache.h"
#include "cache_storage_api.hh"
class Storage;

View File

@ -14,8 +14,9 @@
#include <maxscale/cdefs.h>
#include <tr1/unordered_map>
#include "storage.h"
#include "cachefilter.h"
#include "cache_storage_api.hh"
#include "storage.h"
class LRUStorage : public Storage
{

View File

@ -18,6 +18,7 @@
#include <vector>
#include <tr1/unordered_map>
#include "../../cachefilter.h"
#include "../../cache_storage_api.hh"
class InMemoryStorage
{