tcache.h 6.3 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * 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/>.
 */

#ifndef TDENGINE_TCACHE_H
#define TDENGINE_TCACHE_H

#ifdef __cplusplus
extern "C" {
#endif

23 24 25 26
#include "os.h"
#include "tref.h"
#include "hash.h"

H
Haojun Liao 已提交
27 28
typedef void (*__cache_freeres_fn_t)(void*);

29 30 31 32 33 34 35 36 37 38
typedef struct SCacheStatis {
  int64_t missCount;
  int64_t hitCount;
  int64_t totalAccess;
  int64_t refreshCount;
  int32_t numOfCollision;
} SCacheStatis;

typedef struct SCacheDataNode {
  uint64_t addedTime;    // the added time when this element is added or updated into cache
H
Haojun Liao 已提交
39
  uint64_t lifespan;     // expiredTime expiredTime when this element should be remove from cache
40 41 42
  uint64_t signature;
  uint32_t size;         // allocated size for current SCacheDataNode
  T_REF_DECLARE()
H
Haojun Liao 已提交
43 44 45 46 47
  uint16_t keySize: 15;  // max key size: 32kb
  bool     inTrashCan: 1;// denote if it is in trash or not
  int32_t  extendFactor; // number of life span extend
  char    *key;
  char     data[];
48 49 50 51 52
} SCacheDataNode;

typedef struct STrashElem {
  struct STrashElem *prev;
  struct STrashElem *next;
53
  SCacheDataNode    *pData;
54 55
} STrashElem;

56 57 58 59 60 61 62 63
/*
 * to accommodate the old data which has the same key value of new one in hashList
 * when an new node is put into cache, if an existed one with the same key:
 * 1. if the old one does not be referenced, update it.
 * 2. otherwise, move the old one to pTrash, addedTime the new one.
 *
 * when the node in pTrash does not be referenced, it will be release at the expired expiredTime
 */
64
typedef struct {
65 66 67
  int64_t         totalSize;          // total allocated buffer in this hash table, SCacheObj is not included.
  int64_t         refreshTime;
  STrashElem *    pTrash;
68
  const char *    cacheName;
H
Haojun Liao 已提交
69 70
//  void *          tmrCtrl;
//  void *          pTimer;
71 72
  SCacheStatis    statistics;
  SHashObj *      pHashTable;
H
Haojun Liao 已提交
73
  __cache_freeres_fn_t freeFp;
74 75 76
  uint32_t        numOfElemsInTrash;  // number of element in trash
  uint8_t         deleting;           // set the deleting flag to stop refreshing ASAP.
  pthread_t       refreshWorker;
H
Haojun Liao 已提交
77
  bool            extendLifespan;     // auto extend life span when one item is accessed.
78 79 80
#if defined(LINUX)
  pthread_rwlock_t lock;
#else
H
Haojun Liao 已提交
81
  pthread_mutex_t  lock;
82 83
#endif
} SCacheObj;
H
hzcheng 已提交
84 85

/**
86
 * initialize the cache object
H
Haojun Liao 已提交
87 88 89 90 91
 * @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 已提交
92 93
 * @return
 */
94
SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInSeconds, bool extendLifespan, __cache_freeres_fn_t fn, const char *cacheName);
95 96 97 98 99 100 101

/**
 * initialize the cache object and set the free object callback function
 * @param refreshTimeInSeconds
 * @param freeCb
 * @return
 */
102
SCacheObj *taosCacheInitWithCb(int32_t keyType, int64_t refreshTimeInSeconds, bool extendLifespan, __cache_freeres_fn_t fn, const char *cacheName);
H
hzcheng 已提交
103 104 105 106 107 108 109 110 111 112 113

/**
 * 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
 */
H
Haojun Liao 已提交
114
void *taosCachePut(SCacheObj *pCacheObj, const void *key, size_t keyLen, const void *pData, size_t dataSize, int keepTimeInSeconds);
H
hzcheng 已提交
115 116 117

/**
 * get data from cache
118
 * @param pCacheObj     cache object
H
hzcheng 已提交
119 120 121
 * @param key           key
 * @return              cached data or NULL
 */
H
Haojun Liao 已提交
122
void *taosCacheAcquireByKey(SCacheObj *pCacheObj, const void *key, size_t keyLen);
H
hzcheng 已提交
123

S
Shengliang Guan 已提交
124 125 126 127
/**
 * update the expire time of data in cache 
 * @param pCacheObj     cache object
 * @param key           key
H
Haojun Liao 已提交
128
 * @param keyLen        keyLen
S
Shengliang Guan 已提交
129 130 131
 * @param expireTime    new expire time of data
 * @return
 */ 
H
Haojun Liao 已提交
132
void* taosCacheUpdateExpireTimeByName(SCacheObj *pCacheObj, const char *key, size_t keyLen, uint64_t expireTime);
S
Shengliang Guan 已提交
133

H
hzcheng 已提交
134
/**
135 136
 * 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 已提交
137 138
 * 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 已提交
139 140
 *
 * @param handle
141 142
 * @param data
 * @return
H
hzcheng 已提交
143
 */
144
void *taosCacheAcquireByData(SCacheObj *pCacheObj, void *data);
H
hzcheng 已提交
145 146

/**
147
 * transfer the ownership of data in cache to another object without increasing reference count.
H
hzcheng 已提交
148
 * @param handle
149 150
 * @param data
 * @return
H
hzcheng 已提交
151
 */
152
void *taosCacheTransfer(SCacheObj *pCacheObj, void **data);
H
hzcheng 已提交
153

154
/**
155 156 157 158
 * 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 已提交
159
 * @param _remove   force model, reduce the ref count and move the data into pTrash
160
 */
161
void taosCacheRelease(SCacheObj *pCacheObj, void **data, bool _remove);
162 163 164

/**
 *  move all data node into trash, clear node in trash can if it is not referenced by any clients
165 166
 * @param handle
 */
167
void taosCacheEmpty(SCacheObj *pCacheObj);
168 169

/**
170 171 172 173 174 175 176 177
 * 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.
 *
178 179
 * @param handle
 */
180
void taosCacheCleanup(SCacheObj *pCacheObj);
181

H
hzcheng 已提交
182 183 184 185 186
#ifdef __cplusplus
}
#endif

#endif  // TDENGINE_TCACHE_H