refact tsdb
This commit is contained in:
parent
04e690b50b
commit
9a035fdb1d
|
@ -1,70 +0,0 @@
|
|||
/*
|
||||
* 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 _TD_UTIL_SKIPLIST2_H_
|
||||
#define _TD_UTIL_SKIPLIST2_H_
|
||||
|
||||
#include "os.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define SL_MAX_LEVEL 15
|
||||
|
||||
typedef struct SSkipList2 SSkipList2;
|
||||
typedef struct SSLCursor SSLCursor;
|
||||
typedef struct SSLCfg SSLCfg;
|
||||
typedef struct SSLNode SSLNode;
|
||||
|
||||
typedef int32_t (*tslCmprFn)(const void *pKey1, int32_t nKey1, const void *pKey2, int32_t nKey2);
|
||||
|
||||
// SSkipList2
|
||||
int32_t slOpen(const SSLCfg *pCfg, SSkipList2 **ppSl);
|
||||
int32_t slClose(SSkipList2 *pSl);
|
||||
int32_t slClear(SSkipList2 *pSl);
|
||||
|
||||
// SSLCursor
|
||||
int32_t slcOpen(SSkipList2 *pSl, SSLCursor *pSlc);
|
||||
int32_t slcClose(SSLCursor *pSlc);
|
||||
int32_t slcMoveTo(SSLCursor *pSlc, const void *pKey, int32_t nKey);
|
||||
int32_t slcMoveToNext(SSLCursor *pSlc);
|
||||
int32_t slcMoveToPrev(SSLCursor *pSlc);
|
||||
int32_t slcMoveToFirst(SSLCursor *pSlc);
|
||||
int32_t slcMoveToLast(SSLCursor *pSlc);
|
||||
int32_t slcPut(SSLCursor *pSlc, const void *pKey, int32_t nKey, const void *pData, int32_t nData);
|
||||
int32_t slcGet(SSLCursor *pSlc, const void **ppKey, int32_t *nKey, const void **ppData, int32_t *nData);
|
||||
int32_t slcDrop(SSLCursor *pSlc);
|
||||
|
||||
// struct
|
||||
struct SSLCfg {
|
||||
int8_t maxLevel;
|
||||
int32_t nKey;
|
||||
int32_t nData;
|
||||
tslCmprFn cmprFn;
|
||||
void *pPool;
|
||||
void *(*xMalloc)(void *, int32_t size);
|
||||
void (*xFree)(void *, void *);
|
||||
};
|
||||
|
||||
struct SSLCursor {
|
||||
SSkipList2 *pSl;
|
||||
SSLNode **forwards[SL_MAX_LEVEL];
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /*_TD_UTIL_SKIPLIST2_H_*/
|
|
@ -61,7 +61,7 @@ struct SMemData {
|
|||
|
||||
struct SMemSkipListCurosr {
|
||||
SMemSkipList *pSl;
|
||||
SMemSkipListNode *forwards[];
|
||||
SMemSkipListNode *pNodeC;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
|
@ -70,6 +70,8 @@ typedef struct {
|
|||
const STSRow *pRow;
|
||||
} STsdbRow;
|
||||
|
||||
#define SL_MAX_LEVEL 15
|
||||
|
||||
#define HASH_BUCKET(SUID, UID, NBUCKET) (TABS((SUID) + (UID)) % (NBUCKET))
|
||||
|
||||
#define SL_NODE_SIZE(l) (sizeof(SMemSkipListNode) + sizeof(SMemSkipListNode *) * (l)*2)
|
||||
|
@ -90,6 +92,9 @@ static int32_t tsdbMemSkipListCursorCreate(int8_t maxLevel, SMemSkipListCurosr *
|
|||
static void tsdbMemSkipListCursorDestroy(SMemSkipListCurosr *pSlc);
|
||||
static void tsdbMemSkipListCursorInit(SMemSkipListCurosr *pSlc, SMemSkipList *pSl);
|
||||
static void tsdbMemSkipListCursorPut(SMemSkipListCurosr *pSlc, SMemSkipListNode *pNode);
|
||||
static int32_t tsdbMemSkipListCursorMoveTo(SMemSkipListCurosr *pSlc, int64_t version, TSKEY ts, int32_t flags);
|
||||
static int32_t tsdbMemSkipListCursorMoveToNext(SMemSkipListCurosr *pSlc);
|
||||
static int32_t tsdbMemSkipListCursorMoveToPrev(SMemSkipListCurosr *pSlc);
|
||||
|
||||
// SMemTable
|
||||
int32_t tsdbMemTableCreate2(STsdb *pTsdb, SMemTable **ppMemTb) {
|
||||
|
@ -211,6 +216,7 @@ int32_t tsdbInsertData2(SMemTable *pMemTb, int64_t version, const SVSubmitBlk *p
|
|||
}
|
||||
|
||||
// move cursor
|
||||
tsdbMemSkipListCursorMoveTo(pMemTb->pSlc, version, tRow.pRow->ts, 0);
|
||||
|
||||
// encode row
|
||||
int8_t level = tsdbMemSkipListRandLevel(&pMemData->sl);
|
||||
|
@ -290,9 +296,9 @@ static void tsdbMemSkipListCursorDestroy(SMemSkipListCurosr *pSlc) { taosMemoryF
|
|||
static void tsdbMemSkipListCursorInit(SMemSkipListCurosr *pSlc, SMemSkipList *pSl) {
|
||||
SMemSkipListNode *pHead = SL_HEAD_NODE(pSl);
|
||||
pSlc->pSl = pSl;
|
||||
for (int8_t iLevel = 0; iLevel < pSl->maxLevel; iLevel++) {
|
||||
pSlc->forwards[iLevel] = pHead;
|
||||
}
|
||||
// for (int8_t iLevel = 0; iLevel < pSl->maxLevel; iLevel++) {
|
||||
// pSlc->forwards[iLevel] = pHead;
|
||||
// }
|
||||
}
|
||||
|
||||
static void tsdbMemSkipListCursorPut(SMemSkipListCurosr *pSlc, SMemSkipListNode *pNode) {
|
||||
|
@ -310,4 +316,32 @@ static void tsdbMemSkipListCursorPut(SMemSkipListCurosr *pSlc, SMemSkipListNode
|
|||
}
|
||||
|
||||
pSl->size += 1;
|
||||
}
|
||||
|
||||
static int32_t tsdbMemSkipListCursorMoveTo(SMemSkipListCurosr *pSlc, int64_t version, TSKEY ts, int32_t flags) {
|
||||
SMemSkipListNode **pForwards = NULL;
|
||||
SMemSkipList *pSl = pSlc->pSl;
|
||||
int8_t maxLevel = pSl->maxLevel;
|
||||
SMemSkipListNode *pHead = SL_HEAD_NODE(pSl);
|
||||
SMemSkipListNode *pTail = SL_TAIL_NODE(pSl);
|
||||
|
||||
for (int8_t iLevel = 0; iLevel < maxLevel; iLevel++) {
|
||||
pForwards[iLevel] = pHead;
|
||||
}
|
||||
|
||||
for (int8_t iLevel = maxLevel - 1; iLevel >= 0; iLevel--) {
|
||||
if (iLevel < pSl->level) {
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int32_t tsdbMemSkipListCursorMoveToNext(SMemSkipListCurosr *pSlc) {
|
||||
// TODO
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int32_t tsdbMemSkipListCursorMoveToPrev(SMemSkipListCurosr *pSlc) {
|
||||
// TODO
|
||||
return 0;
|
||||
}
|
|
@ -1,175 +0,0 @@
|
|||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include "tskiplist2.h"
|
||||
|
||||
struct SSLNode {
|
||||
int8_t level;
|
||||
SSLNode *forwards[];
|
||||
};
|
||||
|
||||
struct SSkipList2 {
|
||||
int8_t level;
|
||||
uint32_t seed;
|
||||
int32_t size;
|
||||
const SSLCfg *pCfg;
|
||||
SSLNode *pHead[];
|
||||
};
|
||||
|
||||
static void *slMalloc(void *pPool, int32_t size);
|
||||
static void slFree(void *pPool, void *p);
|
||||
static int32_t slCmprFn(const void *pKey, int32_t nKey, const void *pData, int32_t nData);
|
||||
|
||||
const SSLCfg slDefaultCfg = {.maxLevel = SL_MAX_LEVEL,
|
||||
.nKey = -1,
|
||||
.nData = -1,
|
||||
.cmprFn = slCmprFn,
|
||||
.pPool = NULL,
|
||||
.xMalloc = slMalloc,
|
||||
.xFree = slFree};
|
||||
|
||||
int32_t slOpen(const SSLCfg *pCfg, SSkipList2 **ppSl) {
|
||||
SSkipList2 *pSl = NULL;
|
||||
int32_t size;
|
||||
|
||||
*ppSl = NULL;
|
||||
if (pCfg == NULL) pCfg = &slDefaultCfg;
|
||||
|
||||
// check config (TODO)
|
||||
|
||||
// malloc handle
|
||||
size = sizeof(*pSl) + sizeof(SSLNode *) * pCfg->maxLevel * 2;
|
||||
pSl = pCfg->xMalloc(pCfg->pPool, size);
|
||||
if (pSl == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
pSl->level = 0;
|
||||
pSl->seed = taosRand();
|
||||
pSl->size = 0;
|
||||
pSl->pCfg = pCfg;
|
||||
|
||||
// init an empty skiplist
|
||||
for (int32_t i = 0; i < pCfg->maxLevel * 2; i++) {
|
||||
pSl->pHead[i] = NULL;
|
||||
}
|
||||
|
||||
*ppSl = pSl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t slClose(SSkipList2 *pSl) {
|
||||
if (pSl) {
|
||||
slClear(pSl);
|
||||
if (pSl->pCfg->xFree) {
|
||||
pSl->pCfg->xFree(pSl->pCfg->pPool, pSl);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t slClear(SSkipList2 *pSl) {
|
||||
// loop to clear sl
|
||||
for (;;) {
|
||||
// (TODO)
|
||||
}
|
||||
|
||||
// init sl (TODO)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t slcOpen(SSkipList2 *pSl, SSLCursor *pSlc) {
|
||||
pSlc->pSl = pSl;
|
||||
|
||||
for (int i = 0; i < SL_MAX_LEVEL; i++) {
|
||||
if (i < pSl->pCfg->maxLevel) {
|
||||
} else {
|
||||
pSlc->forwards[i] = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t slcClose(SSLCursor *pSlc) {
|
||||
// TODO
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t slcMoveTo(SSLCursor *pSlc, const void *pKey, int32_t nKey) {
|
||||
// TODO
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t slcMoveToNext(SSLCursor *pSlc) {
|
||||
// TODO
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t slcMoveToPrev(SSLCursor *pSlc) {
|
||||
// TODO
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t slcMoveToFirst(SSLCursor *pSlc) {
|
||||
// TODO
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t slcMoveToLast(SSLCursor *pSlc) {
|
||||
// TODO
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t slcPut(SSLCursor *pSlc, const void *pKey, int32_t nKey, const void *pData, int32_t nData) {
|
||||
// TODO
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t slcGet(SSLCursor *pSlc, const void **ppKey, int32_t *nKey, const void **ppData, int32_t *nData) {
|
||||
// TODO
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t slcDrop(SSLCursor *pSlc) {
|
||||
// TODO
|
||||
return 0;
|
||||
}
|
||||
|
||||
static FORCE_INLINE void *slMalloc(void *pPool, int32_t size) { return taosMemoryMalloc(size); }
|
||||
|
||||
static FORCE_INLINE void slFree(void *pPool, void *p) { taosMemoryFree(p); }
|
||||
|
||||
static int32_t slCmprFn(const void *pKey1, int32_t nKey1, const void *pKey2, int32_t nKey2) {
|
||||
ASSERT(nKey1 >= 0 && nKey2 >= 0);
|
||||
|
||||
int32_t nKey = nKey1 > nKey2 ? nKey2 : nKey1;
|
||||
int32_t c;
|
||||
|
||||
c = memcmp(pKey1, pKey2, nKey);
|
||||
if (c == 0) {
|
||||
if (nKey1 > nKey2) {
|
||||
c = 1;
|
||||
} else if (nKey1 < nKey2) {
|
||||
c = -1;
|
||||
}
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
Loading…
Reference in New Issue