提交 a0621dab 编写于 作者: H hjxilinx

update the skiplist node level generate method, and signature of some functions.

上级 8bf1054d
...@@ -29,15 +29,6 @@ extern "C" { ...@@ -29,15 +29,6 @@ extern "C" {
#include "os.h" #include "os.h"
#include "ttypes.h" #include "ttypes.h"
/*
* generate random data with uniform&skewed distribution, extracted from levelDB
*/
typedef struct SRandom {
uint32_t s;
uint32_t (*rand)(struct SRandom *, int32_t n);
} SRandom;
/* /*
* key of each node * key of each node
* todo move to as the global structure in all search codes... * todo move to as the global structure in all search codes...
...@@ -54,10 +45,11 @@ typedef enum tSkipListPointQueryType { ...@@ -54,10 +45,11 @@ typedef enum tSkipListPointQueryType {
typedef struct tSkipListNode { typedef struct tSkipListNode {
uint16_t nLevel; uint16_t nLevel;
char * pData; char * pData;
tSkipListKey key;
struct tSkipListNode **pForward; struct tSkipListNode **pForward;
struct tSkipListNode **pBackward; struct tSkipListNode **pBackward;
tSkipListKey key;
} tSkipListNode; } tSkipListNode;
/* /*
...@@ -133,9 +125,6 @@ typedef struct tSkipList { ...@@ -133,9 +125,6 @@ typedef struct tSkipList {
__compar_fn_t comparator; __compar_fn_t comparator;
pthread_rwlock_t lock; // will be removed soon pthread_rwlock_t lock; // will be removed soon
// random generator
SRandom r;
// skiplist state // skiplist state
tSkipListState state; tSkipListState state;
} tSkipList; } tSkipList;
...@@ -150,14 +139,14 @@ typedef struct tSKipListQueryCond { ...@@ -150,14 +139,14 @@ typedef struct tSKipListQueryCond {
tSkipListKey upperBnd; tSkipListKey upperBnd;
int32_t lowerBndRelOptr; // relation operator to denote if lower bound is int32_t lowerBndRelOptr; // relation operator to denote if lower bound is
// included or not // included or not
int32_t upperBndRelOptr; int32_t upperBndRelOptr;
} tSKipListQueryCond; } tSKipListQueryCond;
int32_t tSkipListCreate(tSkipList **pSkipList, int16_t nMaxLevel, int16_t keyType, int16_t nMaxKeyLen, tSkipList* tSkipListCreate(int16_t nMaxLevel, int16_t keyType, int16_t nMaxKeyLen);
int32_t (*funcp)());
void tSkipListDestroy(tSkipList **pSkipList); void* tSkipListDestroy(tSkipList *pSkipList);
// create skip list key // create skip list key
tSkipListKey tSkipListCreateKey(int32_t type, char *val, size_t keyLength); tSkipListKey tSkipListCreateKey(int32_t type, char *val, size_t keyLength);
...@@ -170,8 +159,7 @@ tSkipListNode *tSkipListPut(tSkipList *pSkipList, void *pData, tSkipListKey *pKe ...@@ -170,8 +159,7 @@ tSkipListNode *tSkipListPut(tSkipList *pSkipList, void *pData, tSkipListKey *pKe
/* /*
* get only *one* node of which key is equalled to pKey, even there are more * get only *one* node of which key is equalled to pKey, even there are more
* than * than one nodes are of the same key
* one nodes are of the same key
*/ */
tSkipListNode *tSkipListGetOne(tSkipList *pSkipList, tSkipListKey *pKey); tSkipListNode *tSkipListGetOne(tSkipList *pSkipList, tSkipListKey *pKey);
...@@ -215,12 +203,6 @@ int32_t tSkipListPointQuery(tSkipList *pSkipList, tSkipListKey *pKey, int32_t nu ...@@ -215,12 +203,6 @@ int32_t tSkipListPointQuery(tSkipList *pSkipList, tSkipListKey *pKey, int32_t nu
tSkipListNode ***pResult); tSkipListNode ***pResult);
void removeNodeEachLevel(tSkipList *pSkipList, int32_t nLevel); void removeNodeEachLevel(tSkipList *pSkipList, int32_t nLevel);
// todo move to utility
void tInitMatrix(double *x, double *y, int32_t length, double p[2][3]);
int32_t tCompute(double p[2][3]);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
......
...@@ -48,7 +48,7 @@ enum { ...@@ -48,7 +48,7 @@ enum {
TSQL_NODE_TYPE_VALUE = 0x4, TSQL_NODE_TYPE_VALUE = 0x4,
}; };
extern char tTokenTypeSwitcher[12]; extern char tTokenTypeSwitcher[13];
#define toTSDBType(x) \ #define toTSDBType(x) \
do { \ do { \
......
...@@ -478,7 +478,7 @@ int mgmtRemoveMetricFromDb(SDbObj *pDb, STabObj *pMetric) { ...@@ -478,7 +478,7 @@ int mgmtRemoveMetricFromDb(SDbObj *pDb, STabObj *pMetric) {
pDb->numOfMetrics--; pDb->numOfMetrics--;
if (pMetric->pSkipList != NULL) { if (pMetric->pSkipList != NULL) {
tSkipListDestroy(&pMetric->pSkipList); pMetric->pSkipList = tSkipListDestroy(pMetric->pSkipList);
} }
return 0; return 0;
} }
......
...@@ -34,7 +34,7 @@ extern int64_t sdbVersion; ...@@ -34,7 +34,7 @@ extern int64_t sdbVersion;
#define mgmtDestroyMeter(pMeter) \ #define mgmtDestroyMeter(pMeter) \
do { \ do { \
tfree(pMeter->schema); \ tfree(pMeter->schema); \
tSkipListDestroy(&(pMeter->pSkipList));\ pMeter->pSkipList = tSkipListDestroy((pMeter)->pSkipList);\
tfree(pMeter); \ tfree(pMeter); \
} while (0) } while (0)
...@@ -781,8 +781,8 @@ static void addMeterIntoMetricIndex(STabObj *pMetric, STabObj *pMeter) { ...@@ -781,8 +781,8 @@ static void addMeterIntoMetricIndex(STabObj *pMetric, STabObj *pMeter) {
SSchema * pTagSchema = (SSchema *)(pMetric->schema + pMetric->numOfColumns * sizeof(SSchema)); SSchema * pTagSchema = (SSchema *)(pMetric->schema + pMetric->numOfColumns * sizeof(SSchema));
if (pMetric->pSkipList == NULL) { if (pMetric->pSkipList == NULL) {
tSkipListCreate(&pMetric->pSkipList, MAX_SKIP_LIST_LEVEL, pTagSchema[KEY_COLUMN_OF_TAGS].type, pMetric->pSkipList = tSkipListCreate(MAX_SKIP_LIST_LEVEL, pTagSchema[KEY_COLUMN_OF_TAGS].type,
pTagSchema[KEY_COLUMN_OF_TAGS].bytes, tSkipListDefaultCompare); pTagSchema[KEY_COLUMN_OF_TAGS].bytes);
} }
if (pMetric->pSkipList) { if (pMetric->pSkipList) {
......
...@@ -26,8 +26,6 @@ ...@@ -26,8 +26,6 @@
#include "vnodeStore.h" #include "vnodeStore.h"
#include "vnodeUtil.h" #include "vnodeUtil.h"
#pragma GCC diagnostic ignored "-Wformat-overflow="
int vnodeCreateMeterObjFile(int vnode); int vnodeCreateMeterObjFile(int vnode);
int tsMaxVnode = -1; int tsMaxVnode = -1;
......
...@@ -133,7 +133,7 @@ SHistogramInfo* tHistogramCreate(int32_t numOfEntries) { ...@@ -133,7 +133,7 @@ SHistogramInfo* tHistogramCreate(int32_t numOfEntries) {
SHistogramInfo* pHisto = malloc(sizeof(SHistogramInfo) + sizeof(SHistBin) * (numOfEntries + 1)); SHistogramInfo* pHisto = malloc(sizeof(SHistogramInfo) + sizeof(SHistBin) * (numOfEntries + 1));
#if !defined(USE_ARRAYLIST) #if !defined(USE_ARRAYLIST)
tSkipListCreate(&pHisto->pList, MAX_SKIP_LIST_LEVEL, TSDB_DATA_TYPE_DOUBLE, sizeof(double), NULL); pHisto->pList = tSkipListCreate(MAX_SKIP_LIST_LEVEL, TSDB_DATA_TYPE_DOUBLE, sizeof(double));
SInsertSupporter* pss = malloc(sizeof(SInsertSupporter)); SInsertSupporter* pss = malloc(sizeof(SInsertSupporter));
pss->numOfEntries = pHisto->maxEntries; pss->numOfEntries = pHisto->maxEntries;
pss->pSkipList = pHisto->pList; pss->pSkipList = pHisto->pList;
...@@ -623,11 +623,14 @@ double* tHistogramUniform(SHistogramInfo* pHisto, double* ratio, int32_t num) { ...@@ -623,11 +623,14 @@ double* tHistogramUniform(SHistogramInfo* pHisto, double* ratio, int32_t num) {
SHistogramInfo* tHistogramMerge(SHistogramInfo* pHisto1, SHistogramInfo* pHisto2, int32_t numOfEntries) { SHistogramInfo* tHistogramMerge(SHistogramInfo* pHisto1, SHistogramInfo* pHisto2, int32_t numOfEntries) {
SHistogramInfo* pResHistogram = tHistogramCreate(numOfEntries); SHistogramInfo* pResHistogram = tHistogramCreate(numOfEntries);
// error in histogram info
if (pHisto1->numOfEntries > MAX_HISTOGRAM_BIN || pHisto2->numOfEntries > MAX_HISTOGRAM_BIN) {
return pResHistogram;
}
SHistBin* pHistoBins = calloc(1, sizeof(SHistBin) * (pHisto1->numOfEntries + pHisto2->numOfEntries)); SHistBin* pHistoBins = calloc(1, sizeof(SHistBin) * (pHisto1->numOfEntries + pHisto2->numOfEntries));
int32_t i = 0, j = 0, k = 0;
int32_t i = 0;
int32_t j = 0;
int32_t k = 0;
while (i < pHisto1->numOfEntries && j < pHisto2->numOfEntries) { while (i < pHisto1->numOfEntries && j < pHisto2->numOfEntries) {
if (pHisto1->elems[i].val < pHisto2->elems[j].val) { if (pHisto1->elems[i].val < pHisto2->elems[j].val) {
pHistoBins[k++] = pHisto1->elems[i++]; pHistoBins[k++] = pHisto1->elems[i++];
...@@ -660,8 +663,8 @@ SHistogramInfo* tHistogramMerge(SHistogramInfo* pHisto1, SHistogramInfo* pHisto2 ...@@ -660,8 +663,8 @@ SHistogramInfo* tHistogramMerge(SHistogramInfo* pHisto1, SHistogramInfo* pHisto2
histogramMergeImpl(pHistoBins, &k); histogramMergeImpl(pHistoBins, &k);
} }
memcpy(pResHistogram->elems, pHistoBins, sizeof(SHistBin) * numOfEntries);
pResHistogram->numOfEntries = k; pResHistogram->numOfEntries = k;
memcpy(pResHistogram->elems, pHistoBins, sizeof(SHistBin) * k);
free(pHistoBins); free(pHistoBins);
return pResHistogram; return pResHistogram;
......
...@@ -23,28 +23,6 @@ ...@@ -23,28 +23,6 @@
#include "tskiplist.h" #include "tskiplist.h"
#include "tutil.h" #include "tutil.h"
static uint32_t doGetRand(SRandom *pRand, int32_t n) {
const uint32_t val = 2147483647L;
uint64_t p = pRand->s * 16807;
pRand->s = (uint32_t)((p >> 31) + (p & val));
if (pRand->s > val) {
pRand->s -= val;
}
return (pRand->s % n);
}
static SRandom getRand(uint32_t s) {
uint32_t seed = s & 0x7FFFFFFF;
if (seed == 0 || seed == INT32_MAX) {
seed = 1;
}
struct SRandom r = {seed, doGetRand};
return r;
}
void recordNodeEachLevel(tSkipList *pSkipList, int32_t nLevel); void recordNodeEachLevel(tSkipList *pSkipList, int32_t nLevel);
int32_t getSkipListNodeLevel(tSkipList *pSkipList); int32_t getSkipListNodeLevel(tSkipList *pSkipList);
...@@ -55,14 +33,14 @@ static int32_t getSkipListNodeRandomHeight(tSkipList *pSkipList) { ...@@ -55,14 +33,14 @@ static int32_t getSkipListNodeRandomHeight(tSkipList *pSkipList) {
const uint32_t factor = 4; const uint32_t factor = 4;
int32_t n = 1; int32_t n = 1;
while ((pSkipList->r.rand(&pSkipList->r, MAX_SKIP_LIST_LEVEL) % factor) == 0 && n <= MAX_SKIP_LIST_LEVEL) { while ((rand() % factor) == 0 && n <= pSkipList->nMaxLevel) {
n++; n++;
} }
return n; return n;
} }
void tSkipListDoRecordPutNode(tSkipList *pSkipList) { void tSkipListDoRecordPut(tSkipList *pSkipList) {
const int32_t MAX_RECORD_NUM = 1000; const int32_t MAX_RECORD_NUM = 1000;
if (pSkipList->state.nInsertObjs == MAX_RECORD_NUM) { if (pSkipList->state.nInsertObjs == MAX_RECORD_NUM) {
...@@ -239,33 +217,33 @@ static __compar_fn_t getKeyComparator(int32_t keyType) { ...@@ -239,33 +217,33 @@ static __compar_fn_t getKeyComparator(int32_t keyType) {
return comparator; return comparator;
} }
int32_t tSkipListCreate(tSkipList **pSkipList, int16_t nMaxLevel, int16_t keyType, int16_t nMaxKeyLen, tSkipList* tSkipListCreate(int16_t nMaxLevel, int16_t keyType, int16_t nMaxKeyLen) {
int32_t (*funcp)()) { tSkipList *pSkipList = (tSkipList *)calloc(1, sizeof(tSkipList));
(*pSkipList) = (tSkipList *)calloc(1, sizeof(tSkipList)); if (pSkipList == NULL) {
if ((*pSkipList) == NULL) { return NULL;
return -1;
} }
(*pSkipList)->keyType = keyType; pSkipList->keyType = keyType;
(*pSkipList)->comparator = getKeyComparator(keyType); pSkipList->comparator = getKeyComparator(keyType);
(*pSkipList)->pHead.pForward = (tSkipListNode **)calloc(1, POINTER_BYTES * MAX_SKIP_LIST_LEVEL); pSkipList->pHead.pForward = (tSkipListNode **)calloc(1, POINTER_BYTES * MAX_SKIP_LIST_LEVEL);
(*pSkipList)->nMaxLevel = MAX_SKIP_LIST_LEVEL; pSkipList->nMaxLevel = MAX_SKIP_LIST_LEVEL;
(*pSkipList)->nLevel = 1; pSkipList->nLevel = 1;
(*pSkipList)->nMaxKeyLen = nMaxKeyLen; pSkipList->nMaxKeyLen = nMaxKeyLen;
(*pSkipList)->nMaxLevel = nMaxLevel; pSkipList->nMaxLevel = nMaxLevel;
if (pthread_rwlock_init(&(*pSkipList)->lock, NULL) != 0) { if (pthread_rwlock_init(&pSkipList->lock, NULL) != 0) {
return -1; tfree(pSkipList->pHead.pForward);
tfree(pSkipList);
return NULL;
} }
srand(time(NULL)); srand(time(NULL));
(*pSkipList)->r = getRand(time(NULL)); pSkipList->state.nTotalMemSize += sizeof(tSkipList);
(*pSkipList)->state.nTotalMemSize += sizeof(tSkipList); return pSkipList;
return 0;
} }
static void doRemove(tSkipList *pSkipList, tSkipListNode *pNode, tSkipListNode *forward[]) { static void doRemove(tSkipList *pSkipList, tSkipListNode *pNode, tSkipListNode *forward[]) {
...@@ -324,77 +302,33 @@ static tSkipListNode *tSkipListCreateNode(void *pData, const tSkipListKey *pKey, ...@@ -324,77 +302,33 @@ static tSkipListNode *tSkipListCreateNode(void *pData, const tSkipListKey *pKey,
} }
tSkipListKey tSkipListCreateKey(int32_t type, char *val, size_t keyLength) { tSkipListKey tSkipListCreateKey(int32_t type, char *val, size_t keyLength) {
tSkipListKey k; tSkipListKey k = {0};
k.nType = (uint8_t)type; tVariantCreateB(&k, val, (uint32_t) keyLength, (uint32_t) type);
return k;
switch (type) {
case TSDB_DATA_TYPE_INT: {
k.i64Key = *(int32_t *)val;
return k;
}
case TSDB_DATA_TYPE_BIGINT: {
k.i64Key = *(int64_t *)val;
return k;
}
case TSDB_DATA_TYPE_DOUBLE: {
k.dKey = *(double *)val;
return k;
}
case TSDB_DATA_TYPE_FLOAT: {
k.dKey = *(float *)val;
return k;
}
case TSDB_DATA_TYPE_SMALLINT: {
k.i64Key = *(int16_t *)val;
return k;
}
case TSDB_DATA_TYPE_TINYINT: {
k.i64Key = *(int8_t *)val;
return k;
}
case TSDB_DATA_TYPE_BOOL: {
k.i64Key = *(int8_t *)val;
return k;
}
case TSDB_DATA_TYPE_BINARY: {
k.pz = malloc(keyLength + 1);
k.nLen = keyLength;
memcpy(k.pz, val, keyLength);
k.pz[keyLength] = 0;
return k;
}
case TSDB_DATA_TYPE_NCHAR: {
k.pz = malloc(keyLength + TSDB_NCHAR_SIZE);
k.nLen = keyLength / TSDB_NCHAR_SIZE;
wcsncpy(k.wpz, (wchar_t *)val, k.nLen);
k.wpz[k.nLen] = 0;
return k;
}
default:
return k;
}
} }
void tSkipListDestroyKey(tSkipListKey *pKey) { tVariantDestroy(pKey); } void tSkipListDestroyKey(tSkipListKey *pKey) { tVariantDestroy(pKey); }
void tSkipListDestroy(tSkipList **pSkipList) { void* tSkipListDestroy(tSkipList *pSkipList) {
if ((*pSkipList) == NULL) { if (pSkipList == NULL) {
return; return NULL;
} }
pthread_rwlock_wrlock(&(*pSkipList)->lock); pthread_rwlock_wrlock(&pSkipList->lock);
tSkipListNode *pNode = (*pSkipList)->pHead.pForward[0]; tSkipListNode *pNode = pSkipList->pHead.pForward[0];
while (pNode) { while (pNode) {
tSkipListNode *pTemp = pNode; tSkipListNode *pTemp = pNode;
pNode = pNode->pForward[0]; pNode = pNode->pForward[0];
tfree(pTemp); tfree(pTemp);
} }
tfree((*pSkipList)->pHead.pForward); tfree(pSkipList->pHead.pForward);
pthread_rwlock_unlock(&(*pSkipList)->lock); pthread_rwlock_unlock(&pSkipList->lock);
tfree(*pSkipList);
pthread_rwlock_destroy(&pSkipList->lock);
tfree(pSkipList);
return NULL;
} }
tSkipListNode *tSkipListPut(tSkipList *pSkipList, void *pData, tSkipListKey *pKey, int32_t insertIdenticalKey) { tSkipListNode *tSkipListPut(tSkipList *pSkipList, void *pData, tSkipListKey *pKey, int32_t insertIdenticalKey) {
...@@ -405,7 +339,7 @@ tSkipListNode *tSkipListPut(tSkipList *pSkipList, void *pData, tSkipListKey *pKe ...@@ -405,7 +339,7 @@ tSkipListNode *tSkipListPut(tSkipList *pSkipList, void *pData, tSkipListKey *pKe
pthread_rwlock_wrlock(&pSkipList->lock); pthread_rwlock_wrlock(&pSkipList->lock);
// record one node is put into skiplist // record one node is put into skiplist
tSkipListDoRecordPutNode(pSkipList); tSkipListDoRecordPut(pSkipList);
tSkipListNode *px = &pSkipList->pHead; tSkipListNode *px = &pSkipList->pHead;
...@@ -419,9 +353,9 @@ tSkipListNode *tSkipListPut(tSkipList *pSkipList, void *pData, tSkipListKey *pKe ...@@ -419,9 +353,9 @@ tSkipListNode *tSkipListPut(tSkipList *pSkipList, void *pData, tSkipListKey *pKe
forward[i] = px; forward[i] = px;
} }
// if the skiplist does not allowed identical key inserted, the new data will be discarded.
if ((insertIdenticalKey == 0) && forward[0] != &pSkipList->pHead && if ((insertIdenticalKey == 0) && forward[0] != &pSkipList->pHead &&
(pSkipList->comparator(&forward[0]->key, pKey) == 0)) { (pSkipList->comparator(&forward[0]->key, pKey) == 0)) {
/* ignore identical key*/
pthread_rwlock_unlock(&pSkipList->lock); pthread_rwlock_unlock(&pSkipList->lock);
return forward[0]; return forward[0];
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册