tcache.h 4.9 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

S
cache  
Shengliang Guan 已提交
16 17 18 19
#ifndef _TD_UTIL_CACHE_H_
#define _TD_UTIL_CACHE_H_

#include "thash.h"
H
hzcheng 已提交
20 21 22 23 24

#ifdef __cplusplus
extern "C" {
#endif

S
cache  
Shengliang Guan 已提交
25 26 27
#if defined(_TD_ARM_32)
#define TSDB_CACHE_PTR_KEY  TSDB_DATA_TYPE_INT
#define TSDB_CACHE_PTR_TYPE int32_t
S
TD-1530  
Shengliang Guan 已提交
28
#else
S
cache  
Shengliang Guan 已提交
29 30
#define TSDB_CACHE_PTR_KEY  TSDB_DATA_TYPE_BIGINT
#define TSDB_CACHE_PTR_TYPE int64_t
S
TD-1530  
Shengliang Guan 已提交
31 32
#endif

S
cache  
Shengliang Guan 已提交
33 34
typedef void (*__cache_free_fn_t)(void *);
typedef void (*__cache_trav_fn_t)(void *, void *);
H
Haojun Liao 已提交
35

36 37 38 39 40 41 42
typedef struct SCacheStatis {
  int64_t missCount;
  int64_t hitCount;
  int64_t totalAccess;
  int64_t refreshCount;
} SCacheStatis;

H
Haojun Liao 已提交
43 44 45
typedef struct SCacheObj  SCacheObj;
typedef struct SCacheIter SCacheIter;
typedef struct STrashElem STrashElem;
H
hzcheng 已提交
46 47

/**
48
 * initialize the cache object
H
Haojun Liao 已提交
49 50 51 52 53
 * @param keyType              key type
 * @param refreshTimeInSeconds refresh operation interval time, the maximum survival time when one element is expired
 *                             and not referenced by other objects
 * @param extendLifespan       auto extend lifespan, if accessed
 * @param fn                   free resource callback function
H
hzcheng 已提交
54 55
 * @return
 */
S
cache  
Shengliang Guan 已提交
56 57
SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInSeconds, bool extendLifespan, __cache_free_fn_t fn,
                         const char *cacheName);
H
hzcheng 已提交
58 59 60 61 62 63 64 65 66 67 68

/**
 * add data into cache
 *
 * @param handle        cache object
 * @param key           key
 * @param pData         cached data
 * @param dataSize      data size
 * @param keepTime      survival time in second
 * @return              cached element
 */
S
cache  
Shengliang Guan 已提交
69 70
void *taosCachePut(SCacheObj *pCacheObj, const void *key, size_t keyLen, const void *pData, size_t dataSize,
                   int32_t durationMS);
H
hzcheng 已提交
71 72 73

/**
 * get data from cache
74
 * @param pCacheObj     cache object
H
hzcheng 已提交
75 76 77
 * @param key           key
 * @return              cached data or NULL
 */
H
Haojun Liao 已提交
78
void *taosCacheAcquireByKey(SCacheObj *pCacheObj, const void *key, size_t keyLen);
H
hzcheng 已提交
79 80

/**
81 82
 * Add one reference count for the exist data, and assign this data for a new owner.
 * The new owner needs to invoke the taosCacheRelease when it does not need this data anymore.
H
Haojun Liao 已提交
83 84
 * This procedure is a faster version of taosCacheAcquireByKey function, which avoids the sideeffect of the problem of
 * the data is moved to trash, and taosCacheAcquireByKey will fail to retrieve it again.
H
hzcheng 已提交
85 86
 *
 * @param handle
87 88
 * @param data
 * @return
H
hzcheng 已提交
89
 */
90
void *taosCacheAcquireByData(SCacheObj *pCacheObj, void *data);
H
hzcheng 已提交
91 92

/**
93
 * transfer the ownership of data in cache to another object without increasing reference count.
H
hzcheng 已提交
94
 * @param handle
95 96
 * @param data
 * @return
H
hzcheng 已提交
97
 */
98
void *taosCacheTransferData(SCacheObj *pCacheObj, void **data);
H
hzcheng 已提交
99

100
/**
101 102 103 104
 * remove data in cache, the data will not be removed immediately.
 * if it is referenced by other object, it will be remain in cache
 * @param handle    cache object
 * @param data      not the key, actually referenced data
H
Haojun Liao 已提交
105
 * @param _remove   force model, reduce the ref count and move the data into pTrash
106
 */
107
void taosCacheRelease(SCacheObj *pCacheObj, void **data, bool _remove);
108

H
Haojun Liao 已提交
109 110 111 112 113 114 115
/**
 *
 * @param pCacheObj
 * @return
 */
size_t taosCacheGetNumOfObj(const SCacheObj* pCacheObj);

116 117
/**
 *  move all data node into trash, clear node in trash can if it is not referenced by any clients
118 119
 * @param handle
 */
120
void taosCacheEmpty(SCacheObj *pCacheObj);
121 122

/**
123 124 125 126 127 128 129 130
 * release all allocated memory and destroy the cache object.
 *
 * This function only set the deleting flag, and the specific work of clean up cache is delegated to
 * taosCacheRefresh function, which will executed every SCacheObj->refreshTime sec.
 *
 * If the value of SCacheObj->refreshTime is too large, the taosCacheRefresh function may not be invoked
 * before the main thread terminated, in which case all allocated resources are simply recycled by OS.
 *
131 132
 * @param handle
 */
133
void taosCacheCleanup(SCacheObj *pCacheObj);
134

135 136 137 138 139 140
/**
 *
 * @param pCacheObj
 * @param fp
 * @return
 */
S
cache  
Shengliang Guan 已提交
141
void taosCacheRefresh(SCacheObj *pCacheObj, __cache_trav_fn_t fp, void *param1);
142

143 144 145 146 147
/**
 * stop background refresh worker thread
 */
void taosStopCacheRefreshWorker();

H
Haojun Liao 已提交
148 149 150 151 152 153
SCacheIter* taosCacheCreateIter(const SCacheObj* pCacheObj);
bool taosCacheIterNext(SCacheIter* pIter);
void* taosCacheIterGetData(const SCacheIter* pIter, size_t* dataLen);
void* taosCacheIterGetKey(const SCacheIter* pIter, size_t* keyLen);
void taosCacheDestroyIter(SCacheIter* pIter);

H
hzcheng 已提交
154 155 156 157
#ifdef __cplusplus
}
#endif

S
cache  
Shengliang Guan 已提交
158
#endif /*_TD_UTIL_CACHE_H_*/