提交 9df1fed6 编写于 作者: S Shengliang Guan

cache

上级 1360f6b2
...@@ -13,27 +13,26 @@ ...@@ -13,27 +13,26 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef _TD_UTIL_CACHE_H #ifndef _TD_UTIL_CACHE_H_
#define _TD_UTIL_CACHE_H #define _TD_UTIL_CACHE_H_
#include "thash.h"
#include "tlockfree.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#include "os.h" #if defined(_TD_ARM_32)
#include "tlockfree.h" #define TSDB_CACHE_PTR_KEY TSDB_DATA_TYPE_INT
#include "thash.h" #define TSDB_CACHE_PTR_TYPE int32_t
#if defined(_TD_ARM_32)
#define TSDB_CACHE_PTR_KEY TSDB_DATA_TYPE_INT
#define TSDB_CACHE_PTR_TYPE int32_t
#else #else
#define TSDB_CACHE_PTR_KEY TSDB_DATA_TYPE_BIGINT #define TSDB_CACHE_PTR_KEY TSDB_DATA_TYPE_BIGINT
#define TSDB_CACHE_PTR_TYPE int64_t #define TSDB_CACHE_PTR_TYPE int64_t
#endif #endif
typedef void (*__cache_free_fn_t)(void*); typedef void (*__cache_free_fn_t)(void *);
typedef void (*__cache_trav_fn_t)(void*, void*); typedef void (*__cache_trav_fn_t)(void *, void *);
typedef struct SCacheStatis { typedef struct SCacheStatis {
int64_t missCount; int64_t missCount;
...@@ -45,17 +44,17 @@ typedef struct SCacheStatis { ...@@ -45,17 +44,17 @@ typedef struct SCacheStatis {
struct STrashElem; struct STrashElem;
typedef struct SCacheDataNode { typedef struct SCacheDataNode {
uint64_t addedTime; // the added time when this element is added or updated into cache uint64_t addedTime; // the added time when this element is added or updated into cache
uint64_t lifespan; // life duration when this element should be remove from cache uint64_t lifespan; // life duration when this element should be remove from cache
uint64_t expireTime; // expire time uint64_t expireTime; // expire time
uint64_t signature; uint64_t signature;
struct STrashElem *pTNodeHeader; // point to trash node head struct STrashElem *pTNodeHeader; // point to trash node head
uint16_t keySize: 15; // max key size: 32kb uint16_t keySize : 15; // max key size: 32kb
bool inTrashcan: 1;// denote if it is in trash or not bool inTrashcan : 1; // denote if it is in trash or not
uint32_t size; // allocated size for current SCacheDataNode uint32_t size; // allocated size for current SCacheDataNode
T_REF_DECLARE() T_REF_DECLARE()
char *key; char *key;
char data[]; char data[];
} SCacheDataNode; } SCacheDataNode;
typedef struct STrashElem { typedef struct STrashElem {
...@@ -73,22 +72,22 @@ typedef struct STrashElem { ...@@ -73,22 +72,22 @@ typedef struct STrashElem {
* when the node in pTrash does not be referenced, it will be release at the expired expiredTime * when the node in pTrash does not be referenced, it will be release at the expired expiredTime
*/ */
typedef struct { typedef struct {
int64_t totalSize; // total allocated buffer in this hash table, SCacheObj is not included. int64_t totalSize; // total allocated buffer in this hash table, SCacheObj is not included.
int64_t refreshTime; int64_t refreshTime;
STrashElem * pTrash; STrashElem *pTrash;
char* name; char *name;
SCacheStatis statistics; SCacheStatis statistics;
SHashObj * pHashTable; SHashObj *pHashTable;
__cache_free_fn_t freeFp; __cache_free_fn_t freeFp;
uint32_t numOfElemsInTrash; // number of element in trash uint32_t numOfElemsInTrash; // number of element in trash
uint8_t deleting; // set the deleting flag to stop refreshing ASAP. uint8_t deleting; // set the deleting flag to stop refreshing ASAP.
pthread_t refreshWorker; pthread_t refreshWorker;
bool extendLifespan; // auto extend life span when one item is accessed. bool extendLifespan; // auto extend life span when one item is accessed.
int64_t checkTick; // tick used to record the check times of the refresh threads int64_t checkTick; // tick used to record the check times of the refresh threads
#if defined(LINUX) #if defined(LINUX)
pthread_rwlock_t lock; pthread_rwlock_t lock;
#else #else
pthread_mutex_t lock; pthread_mutex_t lock;
#endif #endif
} SCacheObj; } SCacheObj;
...@@ -101,7 +100,8 @@ typedef struct { ...@@ -101,7 +100,8 @@ typedef struct {
* @param fn free resource callback function * @param fn free resource callback function
* @return * @return
*/ */
SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInSeconds, bool extendLifespan, __cache_free_fn_t fn, const char *cacheName); SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInSeconds, bool extendLifespan, __cache_free_fn_t fn,
const char *cacheName);
/** /**
* add data into cache * add data into cache
...@@ -113,7 +113,8 @@ SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInSeconds, bool ext ...@@ -113,7 +113,8 @@ SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInSeconds, bool ext
* @param keepTime survival time in second * @param keepTime survival time in second
* @return cached element * @return cached element
*/ */
void *taosCachePut(SCacheObj *pCacheObj, const void *key, size_t keyLen, const void *pData, size_t dataSize, int durationMS); void *taosCachePut(SCacheObj *pCacheObj, const void *key, size_t keyLen, const void *pData, size_t dataSize,
int32_t durationMS);
/** /**
* get data from cache * get data from cache
...@@ -177,7 +178,7 @@ void taosCacheCleanup(SCacheObj *pCacheObj); ...@@ -177,7 +178,7 @@ void taosCacheCleanup(SCacheObj *pCacheObj);
* @param fp * @param fp
* @return * @return
*/ */
void taosCacheRefresh(SCacheObj *pCacheObj, __cache_trav_fn_t fp, void* param1); void taosCacheRefresh(SCacheObj *pCacheObj, __cache_trav_fn_t fp, void *param1);
/** /**
* stop background refresh worker thread * stop background refresh worker thread
...@@ -188,4 +189,4 @@ void taosStopCacheRefreshWorker(); ...@@ -188,4 +189,4 @@ void taosStopCacheRefreshWorker();
} }
#endif #endif
#endif /*_TD_UTIL_CACHE_H*/ #endif /*_TD_UTIL_CACHE_H_*/
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册