提交 d5e4fc32 编写于 作者: H hzcheng

TD-34

上级 e865255b
...@@ -19,10 +19,7 @@ ...@@ -19,10 +19,7 @@
extern "C" { extern "C" {
#endif #endif
typedef enum { typedef enum { TD_LIST_FORWARD, TD_LIST_BACKWARD } TD_LIST_DIRECTION_T;
TD_LIST_FORWARD,
TD_LIST_BACKWARD
} TD_LIST_DIRECTION_T;
typedef struct _list_node { typedef struct _list_node {
struct _list_node *next; struct _list_node *next;
...@@ -52,6 +49,8 @@ typedef struct { ...@@ -52,6 +49,8 @@ typedef struct {
SList * tdListNew(int eleSize); SList * tdListNew(int eleSize);
void tdListFree(SList *list); void tdListFree(SList *list);
void tdListEmpty(SList *list); void tdListEmpty(SList *list);
void tdListPrependNode(SList *list, SListNode *node);
void tdListAppendNode(SList *list, SListNode *node);
int tdListPrepend(SList *list, void *data); int tdListPrepend(SList *list, void *data);
int tdListAppend(SList *list, void *data); int tdListAppend(SList *list, void *data);
SListNode *tdListPopHead(SList *list); SListNode *tdListPopHead(SList *list);
......
...@@ -43,10 +43,7 @@ void tdListFree(SList *list) { ...@@ -43,10 +43,7 @@ void tdListFree(SList *list) {
free(list); free(list);
} }
int tdListPrepend(SList *list, void *data) { void tdListPrependNode(SList *list, SListNode *node) {
SListNode *node = (SListNode *)malloc(sizeof(SListNode) + list->eleSize);
if (node == NULL) return -1;
if (list->head == NULL) { if (list->head == NULL) {
list->head = node; list->head = node;
list->tail = node; list->tail = node;
...@@ -57,12 +54,9 @@ int tdListPrepend(SList *list, void *data) { ...@@ -57,12 +54,9 @@ int tdListPrepend(SList *list, void *data) {
list->head = node; list->head = node;
} }
list->numOfEles++; list->numOfEles++;
return 0;
} }
int tdListAppend(SList *list, void *data) { void tdListAppendNode(SList *list, SListNode *node) {
SListNode *node = (SListNode *)malloc(sizeof(SListNode) + list->eleSize);
if (node == NULL) return -1;
if (list->head == NULL) { if (list->head == NULL) {
list->head = node; list->head = node;
list->tail = node; list->tail = node;
...@@ -74,6 +68,25 @@ int tdListAppend(SList *list, void *data) { ...@@ -74,6 +68,25 @@ int tdListAppend(SList *list, void *data) {
} }
list->numOfEles++; list->numOfEles++;
}
int tdListPrepend(SList *list, void *data) {
SListNode *node = (SListNode *)malloc(sizeof(SListNode) + list->eleSize);
if (node == NULL) return -1;
memcpy((void *)(node->data), data, list->eleSize);
tdListPrependNode(list, node);
return 0;
}
int tdListAppend(SList *list, void *data) {
SListNode *node = (SListNode *)malloc(sizeof(SListNode) + list->eleSize);
if (node == NULL) return -1;
memcpy((void *)(node->data), data, list->eleSize);
tdListAppendNode(list, node);
return 0; return 0;
} }
...@@ -104,22 +117,22 @@ SListNode *tdListPopTail(SList *list) { ...@@ -104,22 +117,22 @@ SListNode *tdListPopTail(SList *list) {
} }
SListNode *tdListPopNode(SList *list, SListNode *node) { SListNode *tdListPopNode(SList *list, SListNode *node) {
if (list->head == node) { if (list->head == node) {
list->head = node->next; list->head = node->next;
} }
if (list->tail == node) { if (list->tail == node) {
list->tail = node->prev; list->tail = node->prev;
} }
if (node->prev != NULL) { if (node->prev != NULL) {
node->prev->next = node->next; node->prev->next = node->next;
} }
if (node->next != NULL) { if (node->next != NULL) {
node->next->prev = node->prev; node->next->prev = node->prev;
} }
list->numOfEles--; list->numOfEles--;
return node; return node;
} }
void tdListNodeGetData(SList *list, SListNode *node, void *target) { memcpy(node->data, target, list->eleSize); } void tdListNodeGetData(SList *list, SListNode *node, void *target) { memcpy(node->data, target, list->eleSize); }
......
...@@ -17,45 +17,39 @@ ...@@ -17,45 +17,39 @@
#include <stdint.h> #include <stdint.h>
// #include "cache.h" #include "tlist.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#define TSDB_DEFAULT_CACHE_BLOCK_SIZE 16*1024*1024 /* 16M */ #define TSDB_DEFAULT_CACHE_BLOCK_SIZE 16 * 1024 * 1024 /* 16M */
typedef struct { typedef struct {
int64_t skey; // start key int blockId;
int64_t ekey; // end key int offset;
int32_t numOfRows; // numOfRows int remain;
} STableCacheInfo; int padding;
char data[];
} STsdbCacheBlock;
typedef struct _tsdb_cache_block { typedef struct {
char * pData; int64_t index;
STableCacheInfo * pTableInfo; SList * memPool;
struct _tsdb_cache_block *prev; } STsdbCachePool;
struct _tsdb_cache_block *next;
} STSDBCacheBlock;
// Use a doublely linked list to implement this typedef struct {
typedef struct STSDBCache { int maxBytes;
// Number of blocks the cache is allocated int cacheBlockSize;
int32_t numOfBlocks; STsdbCachePool pool;
STSDBCacheBlock *cacheList; STsdbCacheBlock *curBlock;
void * current; SList * mem;
SList * imem;
} STsdbCache; } STsdbCache;
// ---- Operation on STSDBCacheBlock STsdbCache *tsdbInitCache(int maxBytes, int cacheBlockSize);
#define TSDB_CACHE_BLOCK_DATA(pBlock) ((pBlock)->pData) void tsdbFreeCache(STsdbCache *pCache);
#define TSDB_CACHE_AVAIL_SPACE(pBlock) ((char *)((pBlock)->pTableInfo) - ((pBlock)->pData)) void * tsdbAllocFromCache(STsdbCache *pCache, int bytes);
#define TSDB_TABLE_INFO_OF_CACHE(pBlock, tableId) ((pBlock)->pTableInfo)[tableId]
#define TSDB_NEXT_CACHE_BLOCK(pBlock) ((pBlock)->next)
#define TSDB_PREV_CACHE_BLOCK(pBlock) ((pBlock)->prev)
STsdbCache *tsdbInitCache(int64_t maxSize);
int32_t tsdbFreeCache(STsdbCache *pCache);
void * tsdbAllocFromCache(STsdbCache *pCache, int64_t bytes);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
...@@ -16,22 +16,103 @@ ...@@ -16,22 +16,103 @@
#include "tsdbCache.h" #include "tsdbCache.h"
STsdbCache *tsdbInitCache(int64_t maxSize) { static int tsdbAllocBlockFromPool(STsdbCache *pCache);
STsdbCache *pCacheHandle = (STsdbCache *)malloc(sizeof(STsdbCache)); static void tsdbFreeBlockList(SList *list);
if (pCacheHandle == NULL) {
// TODO : deal with the error STsdbCache *tsdbInitCache(int maxBytes, int cacheBlockSize) {
return NULL; STsdbCache *pCache = (STsdbCache *)calloc(1, sizeof(STsdbCache));
if (pCache == NULL) return NULL;
pCache->maxBytes = maxBytes;
pCache->cacheBlockSize = cacheBlockSize;
int nBlocks = maxBytes / cacheBlockSize + 1;
if (nBlocks <= 1) nBlocks = 2;
STsdbCachePool *pPool = &(pCache->pool);
pPool->index = 0;
pPool->memPool = tdListNew(sizeof(STsdbCacheBlock *));
if (pPool->memPool == NULL) goto _err;
for (int i = 0; i < nBlocks; i++) {
STsdbCacheBlock *pBlock = (STsdbCacheBlock *)malloc(sizeof(STsdbCacheBlock) + cacheBlockSize);
if (pBlock == NULL) {
goto _err;
}
pBlock->offset = 0;
pBlock->remain = cacheBlockSize;
tdListAppend(pPool->memPool, (void *)(&pBlock));
} }
return pCacheHandle; pCache->mem = tdListNew(sizeof(STsdbCacheBlock *));
if (pCache->mem == NULL) goto _err;
pCache->imem = tdListNew(sizeof(STsdbCacheBlock *));
if (pCache->imem == NULL) goto _err;
return pCache;
_err:
tsdbFreeCache(pCache);
return NULL;
} }
int32_t tsdbFreeCache(STsdbCache *pHandle) { return 0; } void tsdbFreeCache(STsdbCache *pCache) {
tsdbFreeBlockList(pCache->imem);
tsdbFreeBlockList(pCache->mem);
tsdbFreeBlockList(pCache->pool.memPool);
free(pCache);
}
void *tsdbAllocFromCache(STsdbCache *pCache, int64_t bytes) { void *tsdbAllocFromCache(STsdbCache *pCache, int bytes) {
// TODO: implement here if (pCache == NULL) return NULL;
void *ptr = malloc(bytes); if (bytes > pCache->cacheBlockSize) return NULL;
if (ptr == NULL) return NULL;
if (isListEmpty(pCache->imem)) {
if (tsdbAllocBlockFromPool(pCache) < 0) {
// TODO: deal with the error
}
}
if (pCache->curBlock->remain < bytes) {
if (tsdbAllocBlockFromPool(pCache) < 0) {
// TODO: deal with the error
}
}
void *ptr = (void *)(pCache->curBlock->data + pCache->curBlock->offset);
pCache->curBlock->offset += bytes;
pCache->curBlock->remain -= bytes;
return ptr; return ptr;
}
static void tsdbFreeBlockList(SList *list) {
if (list == NULL) return;
SListNode * node = NULL;
STsdbCacheBlock *pBlock = NULL;
while ((node = tdListPopHead(list)) != NULL) {
tdListNodeGetData(list, node, (void *)(&pBlock));
free(pBlock);
listNodeFree(node);
}
tdListFree(list);
}
static int tsdbAllocBlockFromPool(STsdbCache *pCache) {
STsdbCachePool *pPool = &(pCache->pool);
if (listNEles(pPool->memPool) == 0) return -1;
SListNode *node = tdListPopHead(pPool->memPool);
STsdbCacheBlock *pBlock = NULL;
tdListNodeGetData(pPool->memPool, node, (void *)(&pBlock));
pBlock->blockId = pPool->index++;
pBlock->offset = 0;
pBlock->remain = pCache->cacheBlockSize;
tdListAppendNode(pPool->memPool, node);
pCache->curBlock = pBlock;
return 0;
} }
\ No newline at end of file
...@@ -163,7 +163,7 @@ tsdb_repo_t *tsdbCreateRepo(char *rootDir, STsdbCfg *pCfg, void *limiter /* TODO ...@@ -163,7 +163,7 @@ tsdb_repo_t *tsdbCreateRepo(char *rootDir, STsdbCfg *pCfg, void *limiter /* TODO
pRepo->tsdbMeta = pMeta; pRepo->tsdbMeta = pMeta;
// Initialize cache // Initialize cache
STsdbCache *pCache = tsdbInitCache(pCfg->maxCacheSize); STsdbCache *pCache = tsdbInitCache(pCfg->maxCacheSize, -1);
if (pCache == NULL) { if (pCache == NULL) {
free(pRepo->rootDir); free(pRepo->rootDir);
tsdbFreeMeta(pRepo->tsdbMeta); tsdbFreeMeta(pRepo->tsdbMeta);
...@@ -244,7 +244,7 @@ tsdb_repo_t *tsdbOpenRepo(char *tsdbDir) { ...@@ -244,7 +244,7 @@ tsdb_repo_t *tsdbOpenRepo(char *tsdbDir) {
return NULL; return NULL;
} }
pRepo->tsdbCache = tsdbInitCache(pRepo->config.maxCacheSize); pRepo->tsdbCache = tsdbInitCache(pRepo->config.maxCacheSize, -1);
if (pRepo->tsdbCache == NULL) { if (pRepo->tsdbCache == NULL) {
tsdbFreeMeta(pRepo->tsdbMeta); tsdbFreeMeta(pRepo->tsdbMeta);
free(pRepo->rootDir); free(pRepo->rootDir);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册