diff --git a/include/common/tcompare.h b/include/common/tcompare.h deleted file mode 100644 index 5dcbf37b90..0000000000 --- a/include/common/tcompare.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef _TD_TCOMPARE_H_ -#define _TD_TCOMPARE_H_ - -#include "compare.h" -#include "ttypes.h" - -#ifdef __cplusplus -extern "C" { -#endif - -int32_t compareStrPatternMatch(const void* pLeft, const void* pRight); -int32_t compareStrPatternNotMatch(const void* pLeft, const void* pRight); - -int32_t compareWStrPatternMatch(const void* pLeft, const void* pRight); -int32_t compareWStrPatternNotMatch(const void* pLeft, const void* pRight); - -__compar_fn_t getComparFunc(int32_t type, int32_t optr); -__compar_fn_t getKeyComparFunc(int32_t keyType, int32_t order); -int32_t doCompare(const char* a, const char* b, int32_t type, size_t size); - -#ifdef __cplusplus -} -#endif - -#endif /*_TD_TCOMPARE_H_*/ diff --git a/include/common/tglobal.h b/include/common/tglobal.h index a6cd04e006..40999c453c 100644 --- a/include/common/tglobal.h +++ b/include/common/tglobal.h @@ -20,7 +20,6 @@ extern "C" { #endif -#include "tcfg.h" #include "tdef.h" #include "tarray.h" diff --git a/include/common/tmsg.h b/include/common/tmsg.h index 9ab78f3c96..fbeb212218 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -20,7 +20,7 @@ extern "C" { #endif -#include "encode.h" +#include "tencode.h" #include "taosdef.h" #include "taoserror.h" #include "tarray.h" diff --git a/include/common/ttypes.h b/include/common/ttypes.h index 39a5f0e2b8..00dc75e24e 100644 --- a/include/common/ttypes.h +++ b/include/common/ttypes.h @@ -15,11 +15,6 @@ typedef uint8_t TDRowValT; typedef uint16_t col_id_t; typedef int8_t col_type_t; -typedef struct tstr { - VarDataLenT len; - char data[]; -} tstr; - #pragma pack(push, 1) typedef struct { VarDataLenT len; diff --git a/include/dnode/mgmt/dnode.h b/include/dnode/mgmt/dnode.h index bb0f66ca09..068e9a6917 100644 --- a/include/dnode/mgmt/dnode.h +++ b/include/dnode/mgmt/dnode.h @@ -17,7 +17,6 @@ #define _TD_DNODE_H_ #include "tdef.h" -#include "tcfg.h" #ifdef __cplusplus extern "C" { diff --git a/include/libs/tfs/tfs.h b/include/libs/tfs/tfs.h index 289cfd9b04..b6d10c81bf 100644 --- a/include/libs/tfs/tfs.h +++ b/include/libs/tfs/tfs.h @@ -16,7 +16,7 @@ #ifndef _TD_TFS_H_ #define _TD_TFS_H_ -#include "tcfg.h" +#include "tdef.h" #ifdef __cplusplus extern "C" { diff --git a/include/util/exception.h b/include/util/exception.h deleted file mode 100644 index 8012e34315..0000000000 --- a/include/util/exception.h +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Copyright (c) 2020 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef _TD_UTIL_EXCEPTION_H -#define _TD_UTIL_EXCEPTION_H - -#include "os.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/* - * cleanup actions - */ -typedef struct SCleanupAction { - bool failOnly; - uint8_t wrapper; - uint16_t reserved; - void* func; - union { - void* Ptr; - bool Bool; - char Char; - int8_t Int8; - uint8_t Uint8; - int16_t Int16; - uint16_t Uint16; - int Int; - unsigned int Uint; - int32_t Int32; - uint32_t Uint32; - int64_t Int64; - uint64_t Uint64; - float Float; - double Double; - } arg1, arg2; -} SCleanupAction; - - -/* - * exception hander registration - */ -typedef struct SExceptionNode { - struct SExceptionNode* prev; - jmp_buf jb; - int32_t code; - int32_t maxCleanupAction; - int32_t numCleanupAction; - SCleanupAction* cleanupActions; -} SExceptionNode; - -//////////////////////////////////////////////////////////////////////////////// -// functions & macros for auto-cleanup - -void cleanupPush_void_ptr_ptr ( bool failOnly, void* func, void* arg1, void* arg2 ); -void cleanupPush_void_ptr_bool ( bool failOnly, void* func, void* arg1, bool arg2 ); -void cleanupPush_void_ptr ( bool failOnly, void* func, void* arg ); -void cleanupPush_int_int ( bool failOnly, void* func, int arg ); -void cleanupPush_void ( bool failOnly, void* func ); -void cleanupPush_int_ptr ( bool failOnly, void* func, void* arg ); - -int32_t cleanupGetActionCount(); -void cleanupExecuteTo( int32_t anchor, bool failed ); -void cleanupExecute( SExceptionNode* node, bool failed ); -bool cleanupExceedLimit(); - -#define CLEANUP_PUSH_VOID_PTR_PTR( failOnly, func, arg1, arg2 ) cleanupPush_void_ptr_ptr( (failOnly), (void*)(func), (void*)(arg1), (void*)(arg2) ) -#define CLEANUP_PUSH_VOID_PTR_BOOL( failOnly, func, arg1, arg2 ) cleanupPush_void_ptr_bool( (failOnly), (void*)(func), (void*)(arg1), (bool)(arg2) ) -#define CLEANUP_PUSH_VOID_PTR( failOnly, func, arg ) cleanupPush_void_ptr( (failOnly), (void*)(func), (void*)(arg) ) -#define CLEANUP_PUSH_INT_INT( failOnly, func, arg ) cleanupPush_void_ptr( (failOnly), (void*)(func), (int)(arg) ) -#define CLEANUP_PUSH_VOID( failOnly, func ) cleanupPush_void( (failOnly), (void*)(func) ) -#define CLEANUP_PUSH_INT_PTR( failOnly, func, arg ) cleanupPush_int_ptr( (failOnly), (void*)(func), (void*)(arg) ) -#define CLEANUP_PUSH_FREE( failOnly, arg ) cleanupPush_void_ptr( (failOnly), free, (void*)(arg) ) -#define CLEANUP_PUSH_CLOSE( failOnly, arg ) cleanupPush_int_int( (failOnly), close, (int)(arg) ) -#define CLEANUP_PUSH_FCLOSE( failOnly, arg ) cleanupPush_int_ptr( (failOnly), fclose, (void*)(arg) ) - -#define CLEANUP_GET_ANCHOR() cleanupGetActionCount() -#define CLEANUP_EXECUTE_TO( anchor, failed ) cleanupExecuteTo( (anchor), (failed) ) -#define CLEANUP_EXCEED_LIMIT() cleanupExceedLimit() - -//////////////////////////////////////////////////////////////////////////////// -// functions & macros for exception handling - -void exceptionPushNode( SExceptionNode* node ); -int32_t exceptionPopNode(); -void exceptionThrow( int32_t code ); - -#define TRY(maxCleanupActions) do { \ - SExceptionNode exceptionNode = { 0 }; \ - SCleanupAction cleanupActions[(maxCleanupActions) > 0 ? (maxCleanupActions) : 1]; \ - exceptionNode.maxCleanupAction = (maxCleanupActions) > 0 ? (maxCleanupActions) : 1; \ - exceptionNode.cleanupActions = cleanupActions; \ - exceptionPushNode( &exceptionNode ); \ - int caughtException = setjmp( exceptionNode.jb ); \ - if( caughtException == 0 ) - -#define CATCH( code ) int32_t code = exceptionPopNode(); \ - if( caughtException == 1 ) - -#define FINALLY( code ) int32_t code = exceptionPopNode(); - -#define END_TRY } while( 0 ); - -#define THROW( x ) exceptionThrow( (x) ) -#define CAUGHT_EXCEPTION() ((bool)(caughtException == 1)) -#define CLEANUP_EXECUTE() cleanupExecute( &exceptionNode, CAUGHT_EXCEPTION() ) - -#ifdef __cplusplus -} -#endif - -#endif /*_TD_UTIL_EXCEPTION_H*/ diff --git a/include/util/talgo.h b/include/util/talgo.h index 575f98b486..0fd44a6e91 100644 --- a/include/util/talgo.h +++ b/include/util/talgo.h @@ -13,16 +13,18 @@ * along with this program. If not, see . */ -#ifndef _TD_UTIL_TALGO_H -#define _TD_UTIL_TALGO_H +#ifndef _TD_UTIL_TALGO_H_ +#define _TD_UTIL_TALGO_H_ + +#include "os.h" #ifdef __cplusplus extern "C" { #endif #ifndef __COMPAR_FN_T -# define __COMPAR_FN_T -typedef int (*__compar_fn_t) (const void *, const void *); +#define __COMPAR_FN_T +typedef int32_t (*__compar_fn_t)(const void *, const void *); #endif #define TD_EQ 0x1 @@ -45,7 +47,7 @@ typedef void (*__ext_swap_fn_t)(void *p1, void *p2, const void *param); * @param param * @param comparFn */ -void taosqsort(void *src, size_t numOfElem, size_t size, const void* param, __ext_compar_fn_t comparFn); +void taosqsort(void *src, int64_t numOfElem, int64_t size, const void *param, __ext_compar_fn_t comparFn); /** * binary search, with range support @@ -58,7 +60,7 @@ void taosqsort(void *src, size_t numOfElem, size_t size, const void* param, __ex * @param flags * @return */ -void *taosbsearch(const void *key, const void *base, size_t nmemb, size_t size, __compar_fn_t fn, int flags); +void *taosbsearch(const void *key, const void *base, int64_t nmemb, int64_t size, __compar_fn_t fn, int32_t flags); /** * adjust heap @@ -74,7 +76,8 @@ void *taosbsearch(const void *key, const void *base, size_t nmemb, size_t size, * @param maxroot: if heap is max root heap * @return */ -void taosheapadjust(void *base, int32_t size, int32_t start, int32_t end, const void *parcompar, __ext_compar_fn_t compar, const void *parswap, __ext_swap_fn_t swap, bool maxroot); +void taosheapadjust(void *base, int32_t size, int32_t start, int32_t end, const void *parcompar, + __ext_compar_fn_t compar, const void *parswap, __ext_swap_fn_t swap, bool maxroot); /** * sort heap to make sure it is a max/min root heap @@ -89,10 +92,11 @@ void taosheapadjust(void *base, int32_t size, int32_t start, int32_t end, const * @param maxroot: if heap is max root heap * @return */ -void taosheapsort(void *base, int32_t size, int32_t len, const void *parcompar, __ext_compar_fn_t compar, const void *parswap, __ext_swap_fn_t swap, bool maxroot); - +void taosheapsort(void *base, int32_t size, int32_t len, const void *parcompar, __ext_compar_fn_t compar, + const void *parswap, __ext_swap_fn_t swap, bool maxroot); #ifdef __cplusplus } #endif -#endif /*_TD_UTIL_TALGO_H*/ + +#endif /*_TD_UTIL_TALGO_H_*/ diff --git a/include/util/taoserror.h b/include/util/taoserror.h index 7b42f3fea0..2b569a491a 100644 --- a/include/util/taoserror.h +++ b/include/util/taoserror.h @@ -13,15 +13,13 @@ * along with this program. If not, see . */ -#ifndef _TD_UTIL_TAOS_ERROR_H_ -#define _TD_UTIL_TAOS_ERROR_H_ +#ifndef _TD_UTIL_ERROR_H_ +#define _TD_UTIL_ERROR_H_ #ifdef __cplusplus extern "C" { #endif -// clang-format off - #define TAOS_DEF_ERROR_CODE(mod, code) ((int32_t)((0x80000000 | ((mod)<<16) | (code)))) #define TAOS_SYSTEM_ERROR(code) (0x80ff0000 | (code)) @@ -469,4 +467,4 @@ int32_t* taosGetErrno(); } #endif -#endif /*_TD_UTIL_TAOS_ERROR_H_*/ +#endif /*_TD_UTIL_ERROR_H_*/ diff --git a/include/util/tarray.h b/include/util/tarray.h index 117e60dede..521e54040d 100644 --- a/include/util/tarray.h +++ b/include/util/tarray.h @@ -13,16 +13,15 @@ * along with this program. If not, see . */ -#ifndef _TD_UTIL_ARRAY_H -#define _TD_UTIL_ARRAY_H +#ifndef _TD_UTIL_ARRAY_H_ +#define _TD_UTIL_ARRAY_H_ + +#include "talgo.h" #ifdef __cplusplus extern "C" { #endif -#include "os.h" -#include "talgo.h" - #if 0 #define TARRAY(TYPE) \ struct { \ @@ -31,15 +30,15 @@ extern "C" { struct TYPE* td_array_data_; \ } -#define TARRAY_SIZE(ARRAY) (ARRAY)->tarray_size_ -#define TARRAY_NELES(ARRAY) (ARRAY)->tarray_neles_ +#define TARRAY_SIZE(ARRAY) (ARRAY)->tarray_size_ +#define TARRAY_NELES(ARRAY) (ARRAY)->tarray_neles_ #define TARRAY_ELE_AT(ARRAY, IDX) ((ARRAY)->td_array_data_ + idx) #endif -#define TARRAY_MIN_SIZE 8 +#define TARRAY_MIN_SIZE 8 #define TARRAY_GET_ELEM(array, index) ((void*)((char*)((array)->pData) + (index) * (array)->elemSize)) -#define TARRAY_ELEM_IDX(array, ele) (POINTER_DISTANCE(ele, (array)->pData) / (array)->elemSize) -#define TARRAY_GET_START(array) ((array)->pData) +#define TARRAY_ELEM_IDX(array, ele) (POINTER_DISTANCE(ele, (array)->pData) / (array)->elemSize) +#define TARRAY_GET_START(array) ((array)->pData) typedef struct SArray { size_t size; @@ -70,7 +69,7 @@ int32_t taosArrayEnsureCap(SArray* pArray, size_t tsize); * @param nEles * @return */ -void* taosArrayAddBatch(SArray* pArray, const void* pData, int nEles); +void* taosArrayAddBatch(SArray* pArray, const void* pData, int32_t nEles); /** * @@ -238,7 +237,7 @@ void taosArraySortString(SArray* pArray, __compar_fn_t comparFn); * @param compar * @param key */ -void* taosArraySearch(const SArray* pArray, const void* key, __compar_fn_t comparFn, int flags); +void* taosArraySearch(const SArray* pArray, const void* key, __compar_fn_t comparFn, int32_t flags); /** * search the array, return index of the element @@ -246,14 +245,14 @@ void* taosArraySearch(const SArray* pArray, const void* key, __compar_fn_t compa * @param compar * @param key */ -int32_t taosArraySearchIdx(const SArray* pArray, const void* key, __compar_fn_t comparFn, int flags); +int32_t taosArraySearchIdx(const SArray* pArray, const void* key, __compar_fn_t comparFn, int32_t flags); /** * search the array * @param pArray * @param key */ -char* taosArraySearchString(const SArray* pArray, const char* key, __compar_fn_t comparFn, int flags); +char* taosArraySearchString(const SArray* pArray, const char* key, __compar_fn_t comparFn, int32_t flags); /** * sort the pointer data in the array @@ -269,4 +268,4 @@ void taosArraySortPWithExt(SArray* pArray, __ext_compar_fn_t fn, const void* par } #endif -#endif /*_TD_UTIL_ARRAY_H*/ +#endif /*_TD_UTIL_ARRAY_H_*/ diff --git a/source/util/inc/utilInt.h b/include/util/tbase64.h similarity index 74% rename from source/util/inc/utilInt.h rename to include/util/tbase64.h index ae2bad792d..22b8f95c5a 100644 --- a/source/util/inc/utilInt.h +++ b/include/util/tbase64.h @@ -13,15 +13,20 @@ * along with this program. If not, see . */ -#ifndef _TD_UTIL_INT_H_ -#define _TD_UTIL_INT_H_ +#ifndef _TD_UTIL_BASE64_H_ +#define _TD_UTIL_BASE64_H_ + +#include "os.h" #ifdef __cplusplus extern "C" { #endif +uint8_t *base64_decode(const char *value, int32_t inlen, int32_t *outlen); +char *base64_encode(const uint8_t *value, int32_t vlen); + #ifdef __cplusplus } #endif -#endif /*_TD_UTIL_INT_H_*/ \ No newline at end of file +#endif /*_TD_UTIL_BASE64_H_*/ \ No newline at end of file diff --git a/include/util/tbuffer.h b/include/util/tbuffer.h index 6bb7f67e3d..2ed1b7326b 100644 --- a/include/util/tbuffer.h +++ b/include/util/tbuffer.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef _TD_UTIL_BUFFER_H -#define _TD_UTIL_BUFFER_H +#ifndef _TD_UTIL_BUFFER_H_ +#define _TD_UTIL_BUFFER_H_ #include "os.h" @@ -26,9 +26,9 @@ extern "C" { // usage example /* #include -#include "exception.h" +#include "texception.h" -int main( int argc, char** argv ) { +int32_t main( int32_t argc, char** argv ) { SBufferWriter bw = tbufInitWriter( NULL, false ); TRY( 1 ) { @@ -39,7 +39,7 @@ int main( int argc, char** argv ) { // reserve space for the interger count size_t pos = tbufReserve( &bw, sizeof(int32_t) ); // write 5 integers to the buffer - for( int i = 0; i < 5; i++) { + for( int32_t i = 0; i < 5; i++) { tbufWriteInt32( &bw, i ); } // write the integer count to buffer at reserved position @@ -55,7 +55,7 @@ int main( int argc, char** argv ) { SBufferReader br = tbufInitReader( data, size, false ); // read & print out all integers int32_t count = tbufReadInt32( &br ); - for( int i = 0; i < count; i++ ) { + for( int32_t i = 0; i < count; i++ ) { printf( "%d\n", tbufReadInt32(&br) ); } // read & print out a string @@ -87,12 +87,10 @@ typedef struct SBufferWriter { void* (*allocator)(void*, size_t); } SBufferWriter; -//////////////////////////////////////////////////////////////////////////////// // common functions & macros for both reader & writer #define tbufTell(buf) ((buf)->pos) -//////////////////////////////////////////////////////////////////////////////// /* ------------------------ BUFFER WRITER FUNCTIONS AND MACROS ------------------------ */ // *Allocator*, function to allocate memory, will use 'realloc' if NULL // *Endian*, if true, writer functions of primitive types will do 'hton' automatically @@ -167,4 +165,4 @@ double tbufReadDouble(SBufferReader* buf); } #endif -#endif /*_TD_UTIL_BUFFER_H*/ +#endif /*_TD_UTIL_BUFFER_H_*/ diff --git a/include/util/tcache.h b/include/util/tcache.h index 11f9044d28..0de3ab3a28 100644 --- a/include/util/tcache.h +++ b/include/util/tcache.h @@ -13,27 +13,25 @@ * along with this program. If not, see . */ -#ifndef _TD_UTIL_CACHE_H -#define _TD_UTIL_CACHE_H +#ifndef _TD_UTIL_CACHE_H_ +#define _TD_UTIL_CACHE_H_ + +#include "thash.h" #ifdef __cplusplus extern "C" { #endif -#include "os.h" -#include "tlockfree.h" -#include "thash.h" - -#if defined(_TD_ARM_32) - #define TSDB_CACHE_PTR_KEY TSDB_DATA_TYPE_INT - #define TSDB_CACHE_PTR_TYPE int32_t +#if defined(_TD_ARM_32) +#define TSDB_CACHE_PTR_KEY TSDB_DATA_TYPE_INT +#define TSDB_CACHE_PTR_TYPE int32_t #else - #define TSDB_CACHE_PTR_KEY TSDB_DATA_TYPE_BIGINT - #define TSDB_CACHE_PTR_TYPE int64_t +#define TSDB_CACHE_PTR_KEY TSDB_DATA_TYPE_BIGINT +#define TSDB_CACHE_PTR_TYPE int64_t #endif -typedef void (*__cache_free_fn_t)(void*); -typedef void (*__cache_trav_fn_t)(void*, void*); +typedef void (*__cache_free_fn_t)(void *); +typedef void (*__cache_trav_fn_t)(void *, void *); typedef struct SCacheStatis { int64_t missCount; @@ -45,17 +43,17 @@ typedef struct SCacheStatis { struct STrashElem; typedef struct SCacheDataNode { - uint64_t addedTime; // the added time when this element is added or updated into cache - uint64_t lifespan; // life duration when this element should be remove from cache - uint64_t expireTime; // expire time + uint64_t addedTime; // the added time when this element is added or updated into cache + uint64_t lifespan; // life duration when this element should be remove from cache + uint64_t expireTime; // expire time uint64_t signature; - struct STrashElem *pTNodeHeader; // point to trash node head - uint16_t keySize: 15; // max key size: 32kb - bool inTrashcan: 1;// denote if it is in trash or not - uint32_t size; // allocated size for current SCacheDataNode + struct STrashElem *pTNodeHeader; // point to trash node head + uint16_t keySize : 15; // max key size: 32kb + bool inTrashcan : 1; // denote if it is in trash or not + uint32_t size; // allocated size for current SCacheDataNode T_REF_DECLARE() - char *key; - char data[]; + char *key; + char data[]; } SCacheDataNode; typedef struct STrashElem { @@ -73,22 +71,22 @@ typedef struct STrashElem { * when the node in pTrash does not be referenced, it will be release at the expired expiredTime */ typedef struct { - int64_t totalSize; // total allocated buffer in this hash table, SCacheObj is not included. - int64_t refreshTime; - STrashElem * pTrash; - char* name; - SCacheStatis statistics; - SHashObj * pHashTable; + int64_t totalSize; // total allocated buffer in this hash table, SCacheObj is not included. + int64_t refreshTime; + STrashElem *pTrash; + char *name; + SCacheStatis statistics; + SHashObj *pHashTable; __cache_free_fn_t freeFp; - uint32_t numOfElemsInTrash; // number of element in trash - uint8_t deleting; // set the deleting flag to stop refreshing ASAP. - pthread_t refreshWorker; - bool extendLifespan; // auto extend life span when one item is accessed. - int64_t checkTick; // tick used to record the check times of the refresh threads + uint32_t numOfElemsInTrash; // number of element in trash + uint8_t deleting; // set the deleting flag to stop refreshing ASAP. + pthread_t refreshWorker; + bool extendLifespan; // auto extend life span when one item is accessed. + int64_t checkTick; // tick used to record the check times of the refresh threads #if defined(LINUX) pthread_rwlock_t lock; #else - pthread_mutex_t lock; + pthread_mutex_t lock; #endif } SCacheObj; @@ -101,7 +99,8 @@ typedef struct { * @param fn free resource callback function * @return */ -SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInSeconds, bool extendLifespan, __cache_free_fn_t fn, const char *cacheName); +SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInSeconds, bool extendLifespan, __cache_free_fn_t fn, + const char *cacheName); /** * add data into cache @@ -113,7 +112,8 @@ SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInSeconds, bool ext * @param keepTime survival time in second * @return cached element */ -void *taosCachePut(SCacheObj *pCacheObj, const void *key, size_t keyLen, const void *pData, size_t dataSize, int durationMS); +void *taosCachePut(SCacheObj *pCacheObj, const void *key, size_t keyLen, const void *pData, size_t dataSize, + int32_t durationMS); /** * get data from cache @@ -177,7 +177,7 @@ void taosCacheCleanup(SCacheObj *pCacheObj); * @param fp * @return */ -void taosCacheRefresh(SCacheObj *pCacheObj, __cache_trav_fn_t fp, void* param1); +void taosCacheRefresh(SCacheObj *pCacheObj, __cache_trav_fn_t fp, void *param1); /** * stop background refresh worker thread @@ -188,4 +188,4 @@ void taosStopCacheRefreshWorker(); } #endif -#endif /*_TD_UTIL_CACHE_H*/ +#endif /*_TD_UTIL_CACHE_H_*/ diff --git a/include/util/tchecksum.h b/include/util/tchecksum.h index fb6c85edcd..28fb784c46 100644 --- a/include/util/tchecksum.h +++ b/include/util/tchecksum.h @@ -13,24 +13,22 @@ * along with this program. If not, see . */ -#ifndef _TD_UTIL_CHECKSUM_H -#define _TD_UTIL_CHECKSUM_H +#ifndef _TD_UTIL_CHECKSUM_H_ +#define _TD_UTIL_CHECKSUM_H_ + +#include "tcrc32c.h" #ifdef __cplusplus extern "C" { #endif -#include "os.h" -#include "tcrc32c.h" -#include "tutil.h" - typedef uint32_t TSCKSUM; static FORCE_INLINE TSCKSUM taosCalcChecksum(TSCKSUM csi, const uint8_t *stream, uint32_t ssize) { return (*crc32c)(csi, stream, (size_t)ssize); } -static FORCE_INLINE int taosCalcChecksumAppend(TSCKSUM csi, uint8_t *stream, uint32_t ssize) { +static FORCE_INLINE int32_t taosCalcChecksumAppend(TSCKSUM csi, uint8_t *stream, uint32_t ssize) { if (ssize < sizeof(TSCKSUM)) return -1; *((TSCKSUM *)(stream + ssize - sizeof(TSCKSUM))) = (*crc32c)(csi, stream, (size_t)(ssize - sizeof(TSCKSUM))); @@ -38,11 +36,11 @@ static FORCE_INLINE int taosCalcChecksumAppend(TSCKSUM csi, uint8_t *stream, uin return 0; } -static FORCE_INLINE int taosCheckChecksum(const uint8_t *stream, uint32_t ssize, TSCKSUM checksum) { +static FORCE_INLINE int32_t taosCheckChecksum(const uint8_t *stream, uint32_t ssize, TSCKSUM checksum) { return (checksum != (*crc32c)(0, stream, (size_t)ssize)); } -static FORCE_INLINE int taosCheckChecksumWhole(const uint8_t *stream, uint32_t ssize) { +static FORCE_INLINE int32_t taosCheckChecksumWhole(const uint8_t *stream, uint32_t ssize) { if (ssize < sizeof(TSCKSUM)) return 0; #if (_WIN64) @@ -56,4 +54,4 @@ static FORCE_INLINE int taosCheckChecksumWhole(const uint8_t *stream, uint32_t s } #endif -#endif /*_TD_UTIL_CHECKSUM_H*/ +#endif /*_TD_UTIL_CHECKSUM_H_*/ diff --git a/include/util/tcoding.h b/include/util/tcoding.h index fed9d12cee..943a1f9eca 100644 --- a/include/util/tcoding.h +++ b/include/util/tcoding.h @@ -12,29 +12,30 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#ifndef _TD_UTIL_CODING_H -#define _TD_UTIL_CODING_H + +#ifndef _TD_UTIL_CODING_H_ +#define _TD_UTIL_CODING_H_ + +#include "os.h" #ifdef __cplusplus extern "C" { #endif -#include "os.h" - -#define ENCODE_LIMIT (((uint8_t)1) << 7) +#define ENCODE_LIMIT (((uint8_t)1) << 7) #define ZIGZAGE(T, v) ((u##T)((v) >> (sizeof(T) * 8 - 1))) ^ (((u##T)(v)) << 1) // zigzag encode #define ZIGZAGD(T, v) ((v) >> 1) ^ -((T)((v)&1)) // zigzag decode /* ------------------------ LEGACY CODES ------------------------ */ #if 1 // ---- Fixed U8 -static FORCE_INLINE int taosEncodeFixedU8(void **buf, uint8_t value) { +static FORCE_INLINE int32_t taosEncodeFixedU8(void **buf, uint8_t value) { if (buf != NULL) { ((uint8_t *)(*buf))[0] = value; *buf = POINTER_SHIFT(*buf, sizeof(value)); } - return (int)sizeof(value); + return (int32_t)sizeof(value); } static FORCE_INLINE void *taosDecodeFixedU8(const void *buf, uint8_t *value) { @@ -43,12 +44,12 @@ static FORCE_INLINE void *taosDecodeFixedU8(const void *buf, uint8_t *value) { } // ---- Fixed I8 -static FORCE_INLINE int taosEncodeFixedI8(void **buf, int8_t value) { +static FORCE_INLINE int32_t taosEncodeFixedI8(void **buf, int8_t value) { if (buf != NULL) { ((int8_t *)(*buf))[0] = value; *buf = POINTER_SHIFT(*buf, sizeof(value)); } - return (int)sizeof(value); + return (int32_t)sizeof(value); } static FORCE_INLINE void *taosDecodeFixedI8(const void *buf, int8_t *value) { @@ -57,7 +58,7 @@ static FORCE_INLINE void *taosDecodeFixedI8(const void *buf, int8_t *value) { } // ---- Fixed U16 -static FORCE_INLINE int taosEncodeFixedU16(void **buf, uint16_t value) { +static FORCE_INLINE int32_t taosEncodeFixedU16(void **buf, uint16_t value) { if (buf != NULL) { if (IS_LITTLE_ENDIAN()) { memcpy(*buf, &value, sizeof(value)); @@ -68,7 +69,7 @@ static FORCE_INLINE int taosEncodeFixedU16(void **buf, uint16_t value) { *buf = POINTER_SHIFT(*buf, sizeof(value)); } - return (int)sizeof(value); + return (int32_t)sizeof(value); } static FORCE_INLINE void *taosDecodeFixedU16(const void *buf, uint16_t *value) { @@ -83,7 +84,7 @@ static FORCE_INLINE void *taosDecodeFixedU16(const void *buf, uint16_t *value) { } // ---- Fixed I16 -static FORCE_INLINE int taosEncodeFixedI16(void **buf, int16_t value) { +static FORCE_INLINE int32_t taosEncodeFixedI16(void **buf, int16_t value) { return taosEncodeFixedU16(buf, ZIGZAGE(int16_t, value)); } @@ -95,7 +96,7 @@ static FORCE_INLINE void *taosDecodeFixedI16(const void *buf, int16_t *value) { } // ---- Fixed U32 -static FORCE_INLINE int taosEncodeFixedU32(void **buf, uint32_t value) { +static FORCE_INLINE int32_t taosEncodeFixedU32(void **buf, uint32_t value) { if (buf != NULL) { if (IS_LITTLE_ENDIAN()) { memcpy(*buf, &value, sizeof(value)); @@ -108,7 +109,7 @@ static FORCE_INLINE int taosEncodeFixedU32(void **buf, uint32_t value) { *buf = POINTER_SHIFT(*buf, sizeof(value)); } - return (int)sizeof(value); + return (int32_t)sizeof(value); } static FORCE_INLINE void *taosDecodeFixedU32(const void *buf, uint32_t *value) { @@ -125,7 +126,7 @@ static FORCE_INLINE void *taosDecodeFixedU32(const void *buf, uint32_t *value) { } // ---- Fixed I32 -static FORCE_INLINE int taosEncodeFixedI32(void **buf, int32_t value) { +static FORCE_INLINE int32_t taosEncodeFixedI32(void **buf, int32_t value) { return taosEncodeFixedU32(buf, ZIGZAGE(int32_t, value)); } @@ -137,7 +138,7 @@ static FORCE_INLINE void *taosDecodeFixedI32(const void *buf, int32_t *value) { } // ---- Fixed U64 -static FORCE_INLINE int taosEncodeFixedU64(void **buf, uint64_t value) { +static FORCE_INLINE int32_t taosEncodeFixedU64(void **buf, uint64_t value) { if (buf != NULL) { if (IS_LITTLE_ENDIAN()) { memcpy(*buf, &value, sizeof(value)); @@ -155,7 +156,7 @@ static FORCE_INLINE int taosEncodeFixedU64(void **buf, uint64_t value) { *buf = POINTER_SHIFT(*buf, sizeof(value)); } - return (int)sizeof(value); + return (int32_t)sizeof(value); } static FORCE_INLINE void *taosDecodeFixedU64(const void *buf, uint64_t *value) { @@ -176,7 +177,7 @@ static FORCE_INLINE void *taosDecodeFixedU64(const void *buf, uint64_t *value) { } // ---- Fixed I64 -static FORCE_INLINE int taosEncodeFixedI64(void **buf, int64_t value) { +static FORCE_INLINE int32_t taosEncodeFixedI64(void **buf, int64_t value) { return taosEncodeFixedU64(buf, ZIGZAGE(int64_t, value)); } @@ -188,8 +189,8 @@ static FORCE_INLINE void *taosDecodeFixedI64(const void *buf, int64_t *value) { } // ---- Variant U16 -static FORCE_INLINE int taosEncodeVariantU16(void **buf, uint16_t value) { - int i = 0; +static FORCE_INLINE int32_t taosEncodeVariantU16(void **buf, uint16_t value) { + int32_t i = 0; while (value >= ENCODE_LIMIT) { if (buf != NULL) ((uint8_t *)(*buf))[i] = (uint8_t)(value | ENCODE_LIMIT); value >>= 7; @@ -206,7 +207,7 @@ static FORCE_INLINE int taosEncodeVariantU16(void **buf, uint16_t value) { } static FORCE_INLINE void *taosDecodeVariantU16(const void *buf, uint16_t *value) { - int i = 0; + int32_t i = 0; uint16_t tval = 0; *value = 0; while (i < 3) { @@ -224,7 +225,7 @@ static FORCE_INLINE void *taosDecodeVariantU16(const void *buf, uint16_t *value) } // ---- Variant I16 -static FORCE_INLINE int taosEncodeVariantI16(void **buf, int16_t value) { +static FORCE_INLINE int32_t taosEncodeVariantI16(void **buf, int16_t value) { return taosEncodeVariantU16(buf, ZIGZAGE(int16_t, value)); } @@ -236,8 +237,8 @@ static FORCE_INLINE void *taosDecodeVariantI16(const void *buf, int16_t *value) } // ---- Variant U32 -static FORCE_INLINE int taosEncodeVariantU32(void **buf, uint32_t value) { - int i = 0; +static FORCE_INLINE int32_t taosEncodeVariantU32(void **buf, uint32_t value) { + int32_t i = 0; while (value >= ENCODE_LIMIT) { if (buf != NULL) ((uint8_t *)(*buf))[i] = (value | ENCODE_LIMIT); value >>= 7; @@ -254,7 +255,7 @@ static FORCE_INLINE int taosEncodeVariantU32(void **buf, uint32_t value) { } static FORCE_INLINE void *taosDecodeVariantU32(const void *buf, uint32_t *value) { - int i = 0; + int32_t i = 0; uint32_t tval = 0; *value = 0; while (i < 5) { @@ -272,7 +273,7 @@ static FORCE_INLINE void *taosDecodeVariantU32(const void *buf, uint32_t *value) } // ---- Variant I32 -static FORCE_INLINE int taosEncodeVariantI32(void **buf, int32_t value) { +static FORCE_INLINE int32_t taosEncodeVariantI32(void **buf, int32_t value) { return taosEncodeVariantU32(buf, ZIGZAGE(int32_t, value)); } @@ -284,8 +285,8 @@ static FORCE_INLINE void *taosDecodeVariantI32(const void *buf, int32_t *value) } // ---- Variant U64 -static FORCE_INLINE int taosEncodeVariantU64(void **buf, uint64_t value) { - int i = 0; +static FORCE_INLINE int32_t taosEncodeVariantU64(void **buf, uint64_t value) { + int32_t i = 0; while (value >= ENCODE_LIMIT) { if (buf != NULL) ((uint8_t *)(*buf))[i] = (uint8_t)(value | ENCODE_LIMIT); value >>= 7; @@ -302,7 +303,7 @@ static FORCE_INLINE int taosEncodeVariantU64(void **buf, uint64_t value) { } static FORCE_INLINE void *taosDecodeVariantU64(const void *buf, uint64_t *value) { - int i = 0; + int32_t i = 0; uint64_t tval = 0; *value = 0; while (i < 10) { @@ -320,7 +321,7 @@ static FORCE_INLINE void *taosDecodeVariantU64(const void *buf, uint64_t *value) } // ---- Variant I64 -static FORCE_INLINE int taosEncodeVariantI64(void **buf, int64_t value) { +static FORCE_INLINE int32_t taosEncodeVariantI64(void **buf, int64_t value) { return taosEncodeVariantU64(buf, ZIGZAGE(int64_t, value)); } @@ -332,16 +333,16 @@ static FORCE_INLINE void *taosDecodeVariantI64(const void *buf, int64_t *value) } // ---- string -static FORCE_INLINE int taosEncodeString(void **buf, const char *value) { - int tlen = 0; - size_t size = strlen(value); +static FORCE_INLINE int32_t taosEncodeString(void **buf, const char *value) { + int32_t tlen = 0; + size_t size = strlen(value); tlen += taosEncodeVariantU64(buf, size); if (buf != NULL) { memcpy(*buf, value, size); *buf = POINTER_SHIFT(*buf, size); } - tlen += (int)size; + tlen += (int32_t)size; return tlen; } @@ -372,14 +373,14 @@ static FORCE_INLINE void *taosDecodeStringTo(const void *buf, char *value) { } // ---- binary -static FORCE_INLINE int taosEncodeBinary(void **buf, const void *value, int32_t valueLen) { - int tlen = 0; +static FORCE_INLINE int32_t taosEncodeBinary(void **buf, const void *value, int32_t valueLen) { + int32_t tlen = 0; if (buf != NULL) { memcpy(*buf, value, valueLen); *buf = POINTER_SHIFT(*buf, valueLen); } - tlen += (int)valueLen; + tlen += (int32_t)valueLen; return tlen; } @@ -403,4 +404,4 @@ static FORCE_INLINE void *taosDecodeBinaryTo(const void *buf, void *value, int32 } #endif -#endif /*_TD_UTIL_CODING_H*/ +#endif /*_TD_UTIL_CODING_H_*/ diff --git a/include/util/compare.h b/include/util/tcompare.h similarity index 90% rename from include/util/compare.h rename to include/util/tcompare.h index ece8ffaf42..4c80eeb4f6 100644 --- a/include/util/compare.h +++ b/include/util/tcompare.h @@ -17,12 +17,12 @@ #define _TD_UTIL_COMPARE_H_ #include "os.h" +#include "taos.h" #ifdef __cplusplus extern "C" { #endif - #define TSDB_PATTERN_MATCH 0 #define TSDB_PATTERN_NOMATCH 1 #define TSDB_PATTERN_NOWILDCARDMATCH 2 @@ -99,7 +99,15 @@ int32_t compareUint64ValDesc(const void *pLeft, const void *pRight); int32_t compareLenPrefixedStrDesc(const void *pLeft, const void *pRight); int32_t compareLenPrefixedWStrDesc(const void *pLeft, const void *pRight); +int32_t compareStrPatternMatch(const void *pLeft, const void *pRight); +int32_t compareStrPatternNotMatch(const void *pLeft, const void *pRight); + +int32_t compareWStrPatternMatch(const void *pLeft, const void *pRight); +int32_t compareWStrPatternNotMatch(const void *pLeft, const void *pRight); + __compar_fn_t getComparFunc(int32_t type, int32_t optr); +__compar_fn_t getKeyComparFunc(int32_t keyType, int32_t order); +int32_t doCompare(const char *a, const char *b, int32_t type, size_t size); #ifdef __cplusplus } diff --git a/include/util/tcompression.h b/include/util/tcompression.h index 4cb1193578..d7ba4dfa3f 100644 --- a/include/util/tcompression.h +++ b/include/util/tcompression.h @@ -13,22 +13,23 @@ * along with this program. If not, see . */ -#ifndef _TD_UTIL_COMPRESSION_H -#define _TD_UTIL_COMPRESSION_H +#ifndef _TD_UTIL_COMPRESSION_H_ +#define _TD_UTIL_COMPRESSION_H_ + +#include "os.h" +#include "taos.h" +#include "tutil.h" #ifdef __cplusplus extern "C" { #endif -#include "taos.h" -#include "tutil.h" - #define COMP_OVERFLOW_BYTES 2 -#define BITS_PER_BYTE 8 +#define BITS_PER_BYTE 8 // Masks #define INT64MASK(_x) ((((uint64_t)1) << _x) - 1) #define INT32MASK(_x) (((uint32_t)1 << _x) - 1) -#define INT8MASK(_x) (((uint8_t)1 << _x) - 1) +#define INT8MASK(_x) (((uint8_t)1 << _x) - 1) // Compression algorithm #define NO_COMPRESSION 0 #define ONE_STAGE_COMP 1 @@ -41,48 +42,51 @@ extern "C" { // // compression data mode save first byte lower 1 bit -#define MODE_NOCOMPRESS 0 // original data -#define MODE_COMPRESS 1 // compatible old compress +#define MODE_NOCOMPRESS 0 // original data +#define MODE_COMPRESS 1 // compatible old compress // compression algorithm save first byte higher 7 bit -#define ALGO_SZ_LOSSY 1 // SZ compress +#define ALGO_SZ_LOSSY 1 // SZ compress -#define HEAD_MODE(x) x%2 -#define HEAD_ALGO(x) x/2 +#define HEAD_MODE(x) x % 2 +#define HEAD_ALGO(x) x / 2 -extern int tsCompressINTImp(const char *const input, const int nelements, char *const output, const char type); -extern int tsDecompressINTImp(const char *const input, const int nelements, char *const output, const char type); -extern int tsCompressBoolImp(const char *const input, const int nelements, char *const output); -extern int tsDecompressBoolImp(const char *const input, const int nelements, char *const output); -extern int tsCompressStringImp(const char *const input, int inputSize, char *const output, int outputSize); -extern int tsDecompressStringImp(const char *const input, int compressedSize, char *const output, int outputSize); -extern int tsCompressTimestampImp(const char *const input, const int nelements, char *const output); -extern int tsDecompressTimestampImp(const char *const input, const int nelements, char *const output); -extern int tsCompressDoubleImp(const char *const input, const int nelements, char *const output); -extern int tsDecompressDoubleImp(const char *const input, const int nelements, char *const output); -extern int tsCompressFloatImp(const char *const input, const int nelements, char *const output); -extern int tsDecompressFloatImp(const char *const input, const int nelements, char *const output); +extern int32_t tsCompressINTImp(const char *const input, const int32_t nelements, char *const output, const char type); +extern int32_t tsDecompressINTImp(const char *const input, const int32_t nelements, char *const output, + const char type); +extern int32_t tsCompressBoolImp(const char *const input, const int32_t nelements, char *const output); +extern int32_t tsDecompressBoolImp(const char *const input, const int32_t nelements, char *const output); +extern int32_t tsCompressStringImp(const char *const input, int32_t inputSize, char *const output, int32_t outputSize); +extern int32_t tsDecompressStringImp(const char *const input, int32_t compressedSize, char *const output, + int32_t outputSize); +extern int32_t tsCompressTimestampImp(const char *const input, const int32_t nelements, char *const output); +extern int32_t tsDecompressTimestampImp(const char *const input, const int32_t nelements, char *const output); +extern int32_t tsCompressDoubleImp(const char *const input, const int32_t nelements, char *const output); +extern int32_t tsDecompressDoubleImp(const char *const input, const int32_t nelements, char *const output); +extern int32_t tsCompressFloatImp(const char *const input, const int32_t nelements, char *const output); +extern int32_t tsDecompressFloatImp(const char *const input, const int32_t nelements, char *const output); // lossy -extern int tsCompressFloatLossyImp(const char * input, const int nelements, char *const output); -extern int tsDecompressFloatLossyImp(const char * input, int compressedSize, const int nelements, char *const output); -extern int tsCompressDoubleLossyImp(const char * input, const int nelements, char *const output); -extern int tsDecompressDoubleLossyImp(const char * input, int compressedSize, const int nelements, char *const output); +extern int32_t tsCompressFloatLossyImp(const char *input, const int32_t nelements, char *const output); +extern int32_t tsDecompressFloatLossyImp(const char *input, int32_t compressedSize, const int32_t nelements, + char *const output); +extern int32_t tsCompressDoubleLossyImp(const char *input, const int32_t nelements, char *const output); +extern int32_t tsDecompressDoubleLossyImp(const char *input, int32_t compressedSize, const int32_t nelements, + char *const output); #ifdef TD_TSZ extern bool lossyFloat; extern bool lossyDouble; -// init call -int tsCompressInit(); -// exit call -void tsCompressExit(); +int32_t tsCompressInit(); +void tsCompressExit(); #endif -static FORCE_INLINE int tsCompressTinyint(const char *const input, int inputSize, const int nelements, char *const output, int outputSize, char algorithm, - char *const buffer, int bufferSize) { +static FORCE_INLINE int32_t tsCompressTinyint(const char *const input, int32_t inputSize, const int32_t nelements, + char *const output, int32_t outputSize, char algorithm, + char *const buffer, int32_t bufferSize) { if (algorithm == ONE_STAGE_COMP) { return tsCompressINTImp(input, nelements, output, TSDB_DATA_TYPE_TINYINT); } else if (algorithm == TWO_STAGE_COMP) { - int len = tsCompressINTImp(input, nelements, buffer, TSDB_DATA_TYPE_TINYINT); + int32_t len = tsCompressINTImp(input, nelements, buffer, TSDB_DATA_TYPE_TINYINT); return tsCompressStringImp(buffer, len, output, outputSize); } else { assert(0); @@ -90,8 +94,9 @@ static FORCE_INLINE int tsCompressTinyint(const char *const input, int inputSize } } -static FORCE_INLINE int tsDecompressTinyint(const char *const input, int compressedSize, const int nelements, char *const output, - int outputSize, char algorithm, char *const buffer, int bufferSize) { +static FORCE_INLINE int32_t tsDecompressTinyint(const char *const input, int32_t compressedSize, + const int32_t nelements, char *const output, int32_t outputSize, + char algorithm, char *const buffer, int32_t bufferSize) { if (algorithm == ONE_STAGE_COMP) { return tsDecompressINTImp(input, nelements, output, TSDB_DATA_TYPE_TINYINT); } else if (algorithm == TWO_STAGE_COMP) { @@ -103,12 +108,13 @@ static FORCE_INLINE int tsDecompressTinyint(const char *const input, int compres } } -static FORCE_INLINE int tsCompressSmallint(const char *const input, int inputSize, const int nelements, char *const output, int outputSize, char algorithm, - char *const buffer, int bufferSize) { +static FORCE_INLINE int32_t tsCompressSmallint(const char *const input, int32_t inputSize, const int32_t nelements, + char *const output, int32_t outputSize, char algorithm, + char *const buffer, int32_t bufferSize) { if (algorithm == ONE_STAGE_COMP) { return tsCompressINTImp(input, nelements, output, TSDB_DATA_TYPE_SMALLINT); } else if (algorithm == TWO_STAGE_COMP) { - int len = tsCompressINTImp(input, nelements, buffer, TSDB_DATA_TYPE_SMALLINT); + int32_t len = tsCompressINTImp(input, nelements, buffer, TSDB_DATA_TYPE_SMALLINT); return tsCompressStringImp(buffer, len, output, outputSize); } else { assert(0); @@ -116,8 +122,9 @@ static FORCE_INLINE int tsCompressSmallint(const char *const input, int inputSiz } } -static FORCE_INLINE int tsDecompressSmallint(const char *const input, int compressedSize, const int nelements, char *const output, - int outputSize, char algorithm, char *const buffer, int bufferSize) { +static FORCE_INLINE int32_t tsDecompressSmallint(const char *const input, int32_t compressedSize, + const int32_t nelements, char *const output, int32_t outputSize, + char algorithm, char *const buffer, int32_t bufferSize) { if (algorithm == ONE_STAGE_COMP) { return tsDecompressINTImp(input, nelements, output, TSDB_DATA_TYPE_SMALLINT); } else if (algorithm == TWO_STAGE_COMP) { @@ -129,12 +136,13 @@ static FORCE_INLINE int tsDecompressSmallint(const char *const input, int compre } } -static FORCE_INLINE int tsCompressInt(const char *const input, int inputSize, const int nelements, char *const output, int outputSize, char algorithm, - char *const buffer, int bufferSize) { +static FORCE_INLINE int32_t tsCompressInt(const char *const input, int32_t inputSize, const int32_t nelements, + char *const output, int32_t outputSize, char algorithm, char *const buffer, + int32_t bufferSize) { if (algorithm == ONE_STAGE_COMP) { return tsCompressINTImp(input, nelements, output, TSDB_DATA_TYPE_INT); } else if (algorithm == TWO_STAGE_COMP) { - int len = tsCompressINTImp(input, nelements, buffer, TSDB_DATA_TYPE_INT); + int32_t len = tsCompressINTImp(input, nelements, buffer, TSDB_DATA_TYPE_INT); return tsCompressStringImp(buffer, len, output, outputSize); } else { assert(0); @@ -142,8 +150,9 @@ static FORCE_INLINE int tsCompressInt(const char *const input, int inputSize, co } } -static FORCE_INLINE int tsDecompressInt(const char *const input, int compressedSize, const int nelements, char *const output, - int outputSize, char algorithm, char *const buffer, int bufferSize) { +static FORCE_INLINE int32_t tsDecompressInt(const char *const input, int32_t compressedSize, const int32_t nelements, + char *const output, int32_t outputSize, char algorithm, char *const buffer, + int32_t bufferSize) { if (algorithm == ONE_STAGE_COMP) { return tsDecompressINTImp(input, nelements, output, TSDB_DATA_TYPE_INT); } else if (algorithm == TWO_STAGE_COMP) { @@ -155,12 +164,13 @@ static FORCE_INLINE int tsDecompressInt(const char *const input, int compressedS } } -static FORCE_INLINE int tsCompressBigint(const char *const input, int inputSize, const int nelements, char *const output, int outputSize, - char algorithm, char *const buffer, int bufferSize) { +static FORCE_INLINE int32_t tsCompressBigint(const char *const input, int32_t inputSize, const int32_t nelements, + char *const output, int32_t outputSize, char algorithm, char *const buffer, + int32_t bufferSize) { if (algorithm == ONE_STAGE_COMP) { return tsCompressINTImp(input, nelements, output, TSDB_DATA_TYPE_BIGINT); } else if (algorithm == TWO_STAGE_COMP) { - int len = tsCompressINTImp(input, nelements, buffer, TSDB_DATA_TYPE_BIGINT); + int32_t len = tsCompressINTImp(input, nelements, buffer, TSDB_DATA_TYPE_BIGINT); return tsCompressStringImp(buffer, len, output, outputSize); } else { assert(0); @@ -168,8 +178,9 @@ static FORCE_INLINE int tsCompressBigint(const char *const input, int inputSize, } } -static FORCE_INLINE int tsDecompressBigint(const char *const input, int compressedSize, const int nelements, char *const output, - int outputSize, char algorithm, char *const buffer, int bufferSize) { +static FORCE_INLINE int32_t tsDecompressBigint(const char *const input, int32_t compressedSize, const int32_t nelements, + char *const output, int32_t outputSize, char algorithm, + char *const buffer, int32_t bufferSize) { if (algorithm == ONE_STAGE_COMP) { return tsDecompressINTImp(input, nelements, output, TSDB_DATA_TYPE_BIGINT); } else if (algorithm == TWO_STAGE_COMP) { @@ -181,12 +192,13 @@ static FORCE_INLINE int tsDecompressBigint(const char *const input, int compress } } -static FORCE_INLINE int tsCompressBool(const char *const input, int inputSize, const int nelements, char *const output, int outputSize, - char algorithm, char *const buffer, int bufferSize) { +static FORCE_INLINE int32_t tsCompressBool(const char *const input, int32_t inputSize, const int32_t nelements, + char *const output, int32_t outputSize, char algorithm, char *const buffer, + int32_t bufferSize) { if (algorithm == ONE_STAGE_COMP) { return tsCompressBoolImp(input, nelements, output); } else if (algorithm == TWO_STAGE_COMP) { - int len = tsCompressBoolImp(input, nelements, buffer); + int32_t len = tsCompressBoolImp(input, nelements, buffer); return tsCompressStringImp(buffer, len, output, outputSize); } else { assert(0); @@ -194,8 +206,9 @@ static FORCE_INLINE int tsCompressBool(const char *const input, int inputSize, c } } -static FORCE_INLINE int tsDecompressBool(const char *const input, int compressedSize, const int nelements, char *const output, - int outputSize, char algorithm, char *const buffer, int bufferSize) { +static FORCE_INLINE int32_t tsDecompressBool(const char *const input, int32_t compressedSize, const int32_t nelements, + char *const output, int32_t outputSize, char algorithm, char *const buffer, + int32_t bufferSize) { if (algorithm == ONE_STAGE_COMP) { return tsDecompressBoolImp(input, nelements, output); } else if (algorithm == TWO_STAGE_COMP) { @@ -207,47 +220,51 @@ static FORCE_INLINE int tsDecompressBool(const char *const input, int compressed } } -static FORCE_INLINE int tsCompressString(const char *const input, int inputSize, const int nelements, char *const output, int outputSize, - char algorithm, char *const buffer, int bufferSize) { +static FORCE_INLINE int32_t tsCompressString(const char *const input, int32_t inputSize, const int32_t nelements, + char *const output, int32_t outputSize, char algorithm, char *const buffer, + int32_t bufferSize) { return tsCompressStringImp(input, inputSize, output, outputSize); } -static FORCE_INLINE int tsDecompressString(const char *const input, int compressedSize, const int nelements, char *const output, - int outputSize, char algorithm, char *const buffer, int bufferSize) { +static FORCE_INLINE int32_t tsDecompressString(const char *const input, int32_t compressedSize, const int32_t nelements, + char *const output, int32_t outputSize, char algorithm, + char *const buffer, int32_t bufferSize) { return tsDecompressStringImp(input, compressedSize, output, outputSize); } -static FORCE_INLINE int tsCompressFloat(const char *const input, int inputSize, const int nelements, char *const output, int outputSize, - char algorithm, char *const buffer, int bufferSize) { +static FORCE_INLINE int32_t tsCompressFloat(const char *const input, int32_t inputSize, const int32_t nelements, + char *const output, int32_t outputSize, char algorithm, char *const buffer, + int32_t bufferSize) { #ifdef TD_TSZ // lossy mode - if(lossyFloat) { + if (lossyFloat) { return tsCompressFloatLossyImp(input, nelements, output); - // lossless mode + // lossless mode } else { -#endif +#endif if (algorithm == ONE_STAGE_COMP) { return tsCompressFloatImp(input, nelements, output); } else if (algorithm == TWO_STAGE_COMP) { - int len = tsCompressFloatImp(input, nelements, buffer); + int32_t len = tsCompressFloatImp(input, nelements, buffer); return tsCompressStringImp(buffer, len, output, outputSize); } else { assert(0); return -1; - } -#ifdef TD_TSZ + } +#ifdef TD_TSZ } #endif } -static FORCE_INLINE int tsDecompressFloat(const char *const input, int compressedSize, const int nelements, char *const output, - int outputSize, char algorithm, char *const buffer, int bufferSize) { +static FORCE_INLINE int32_t tsDecompressFloat(const char *const input, int32_t compressedSize, const int32_t nelements, + char *const output, int32_t outputSize, char algorithm, + char *const buffer, int32_t bufferSize) { #ifdef TD_TSZ - if(HEAD_ALGO(input[0]) == ALGO_SZ_LOSSY){ + if (HEAD_ALGO(input[0]) == ALGO_SZ_LOSSY) { // decompress lossy return tsDecompressFloatLossyImp(input, compressedSize, nelements, output); } else { -#endif +#endif // decompress lossless if (algorithm == ONE_STAGE_COMP) { return tsDecompressFloatImp(input, nelements, output); @@ -258,43 +275,44 @@ static FORCE_INLINE int tsDecompressFloat(const char *const input, int compresse assert(0); return -1; } -#ifdef TD_TSZ +#ifdef TD_TSZ } #endif } - -static FORCE_INLINE int tsCompressDouble(const char *const input, int inputSize, const int nelements, char *const output, int outputSize, - char algorithm, char *const buffer, int bufferSize) { -#ifdef TD_TSZ - if(lossyDouble){ +static FORCE_INLINE int32_t tsCompressDouble(const char *const input, int32_t inputSize, const int32_t nelements, + char *const output, int32_t outputSize, char algorithm, char *const buffer, + int32_t bufferSize) { +#ifdef TD_TSZ + if (lossyDouble) { // lossy mode return tsCompressDoubleLossyImp(input, nelements, output); } else { -#endif +#endif // lossless mode if (algorithm == ONE_STAGE_COMP) { return tsCompressDoubleImp(input, nelements, output); } else if (algorithm == TWO_STAGE_COMP) { - int len = tsCompressDoubleImp(input, nelements, buffer); + int32_t len = tsCompressDoubleImp(input, nelements, buffer); return tsCompressStringImp(buffer, len, output, outputSize); } else { assert(0); return -1; } -#ifdef TD_TSZ +#ifdef TD_TSZ } -#endif +#endif } -static FORCE_INLINE int tsDecompressDouble(const char *const input, int compressedSize, const int nelements, char *const output, - int outputSize, char algorithm, char *const buffer, int bufferSize) { - #ifdef TD_TSZ - if(HEAD_ALGO(input[0]) == ALGO_SZ_LOSSY){ +static FORCE_INLINE int32_t tsDecompressDouble(const char *const input, int32_t compressedSize, const int32_t nelements, + char *const output, int32_t outputSize, char algorithm, + char *const buffer, int32_t bufferSize) { +#ifdef TD_TSZ + if (HEAD_ALGO(input[0]) == ALGO_SZ_LOSSY) { // decompress lossy return tsDecompressDoubleLossyImp(input, compressedSize, nelements, output); } else { - #endif +#endif // decompress lossless if (algorithm == ONE_STAGE_COMP) { return tsDecompressDoubleImp(input, nelements, output); @@ -305,43 +323,48 @@ static FORCE_INLINE int tsDecompressDouble(const char *const input, int compress assert(0); return -1; } -#ifdef TD_TSZ +#ifdef TD_TSZ } -#endif +#endif } -#ifdef TD_TSZ +#ifdef TD_TSZ // // lossy float double // -static FORCE_INLINE int tsCompressFloatLossy(const char *const input, int inputSize, const int nelements, char *const output, int outputSize, - char algorithm, char *const buffer, int bufferSize) { +static FORCE_INLINE int32_t tsCompressFloatLossy(const char *const input, int32_t inputSize, const int32_t nelements, + char *const output, int32_t outputSize, char algorithm, + char *const buffer, int32_t bufferSize) { return tsCompressFloatLossyImp(input, nelements, output); } -static FORCE_INLINE int tsDecompressFloatLossy(const char *const input, int compressedSize, const int nelements, char *const output, - int outputSize, char algorithm, char *const buffer, int bufferSize){ +static FORCE_INLINE int32_t tsDecompressFloatLossy(const char *const input, int32_t compressedSize, + const int32_t nelements, char *const output, int32_t outputSize, + char algorithm, char *const buffer, int32_t bufferSize) { return tsDecompressFloatLossyImp(input, compressedSize, nelements, output); } -static FORCE_INLINE int tsCompressDoubleLossy(const char *const input, int inputSize, const int nelements, char *const output, int outputSize, - char algorithm, char *const buffer, int bufferSize){ +static FORCE_INLINE int32_t tsCompressDoubleLossy(const char *const input, int32_t inputSize, const int32_t nelements, + char *const output, int32_t outputSize, char algorithm, + char *const buffer, int32_t bufferSize) { return tsCompressDoubleLossyImp(input, nelements, output); } -static FORCE_INLINE int tsDecompressDoubleLossy(const char *const input, int compressedSize, const int nelements, char *const output, - int outputSize, char algorithm, char *const buffer, int bufferSize){ +static FORCE_INLINE int32_t tsDecompressDoubleLossy(const char *const input, int32_t compressedSize, + const int32_t nelements, char *const output, int32_t outputSize, + char algorithm, char *const buffer, int32_t bufferSize) { return tsDecompressDoubleLossyImp(input, compressedSize, nelements, output); } #endif -static FORCE_INLINE int tsCompressTimestamp(const char *const input, int inputSize, const int nelements, char *const output, int outputSize, - char algorithm, char *const buffer, int bufferSize) { +static FORCE_INLINE int32_t tsCompressTimestamp(const char *const input, int32_t inputSize, const int32_t nelements, + char *const output, int32_t outputSize, char algorithm, + char *const buffer, int32_t bufferSize) { if (algorithm == ONE_STAGE_COMP) { return tsCompressTimestampImp(input, nelements, output); } else if (algorithm == TWO_STAGE_COMP) { - int len = tsCompressTimestampImp(input, nelements, buffer); + int32_t len = tsCompressTimestampImp(input, nelements, buffer); return tsCompressStringImp(buffer, len, output, outputSize); } else { assert(0); @@ -349,8 +372,9 @@ static FORCE_INLINE int tsCompressTimestamp(const char *const input, int inputSi } } -static FORCE_INLINE int tsDecompressTimestamp(const char *const input, int compressedSize, const int nelements, char *const output, - int outputSize, char algorithm, char *const buffer, int bufferSize) { +static FORCE_INLINE int32_t tsDecompressTimestamp(const char *const input, int32_t compressedSize, + const int32_t nelements, char *const output, int32_t outputSize, + char algorithm, char *const buffer, int32_t bufferSize) { if (algorithm == ONE_STAGE_COMP) { return tsDecompressTimestampImp(input, nelements, output); } else if (algorithm == TWO_STAGE_COMP) { @@ -366,4 +390,4 @@ static FORCE_INLINE int tsDecompressTimestamp(const char *const input, int compr } #endif -#endif /*_TD_UTIL_COMPRESSION_H*/ \ No newline at end of file +#endif /*_TD_UTIL_COMPRESSION_H_*/ \ No newline at end of file diff --git a/include/util/tconfig.h b/include/util/tconfig.h index 11dd5414ec..8275054a64 100644 --- a/include/util/tconfig.h +++ b/include/util/tconfig.h @@ -17,7 +17,6 @@ #ifndef _TD_CONFIG_H_ #define _TD_CONFIG_H_ -#include "os.h" #include "tarray.h" #ifdef __cplusplus @@ -54,11 +53,11 @@ typedef struct SConfigItem { bool tsc; char *name; union { - bool bval; - float fval; - int32_t i32; - int64_t i64; - char *str; + bool bval; + float fval; + int32_t i32; + int64_t i64; + char *str; }; union { int64_t imin; @@ -76,16 +75,17 @@ typedef struct { const char *value; } SConfigPair; -typedef struct SConfig SConfig; +typedef struct SConfig { + ECfgSrcType stype; + SArray *array; +} SConfig; SConfig *cfgInit(); int32_t cfgLoad(SConfig *pCfg, ECfgSrcType cfgType, const char *sourceStr); -int32_t cfgLoadArray(SConfig *pCfg, SArray *pArgs); // SConfigPair +int32_t cfgLoadArray(SConfig *pCfg, SArray *pArgs); // SConfigPair void cfgCleanup(SConfig *pCfg); int32_t cfgGetSize(SConfig *pCfg); -SConfigItem *cfgIterate(SConfig *pCfg, SConfigItem *pIter); -void cfgCancelIterate(SConfig *pCfg, SConfigItem *pIter); SConfigItem *cfgGetItem(SConfig *pCfg, const char *name); int32_t cfgSetItem(SConfig *pCfg, const char *name, const char *value, ECfgSrcType stype); diff --git a/include/util/tcrc32c.h b/include/util/tcrc32c.h index 200103b173..0246e09c31 100644 --- a/include/util/tcrc32c.h +++ b/include/util/tcrc32c.h @@ -18,8 +18,10 @@ 3. This notice may not be removed or altered from any source distribution. */ -#ifndef _TD_UTIL_CRC32_H -#define _TD_UTIL_CRC32_H +#ifndef _TD_UTIL_CRC32_H_ +#define _TD_UTIL_CRC32_H_ + +#include "os.h" #ifdef __cplusplus extern "C" { @@ -39,4 +41,4 @@ void taosResolveCRC(); } #endif -#endif /*_TD_UTIL_CRC32_H*/ +#endif /*_TD_UTIL_CRC32_H_*/ diff --git a/include/util/tdef.h b/include/util/tdef.h index d0f2b77f1f..1a5c07d75a 100644 --- a/include/util/tdef.h +++ b/include/util/tdef.h @@ -13,10 +13,8 @@ * along with this program. If not, see . */ -// clang-format off - -#ifndef _TD_UTIL_DEF_H -#define _TD_UTIL_DEF_H +#ifndef _TD_UTIL_DEF_H_ +#define _TD_UTIL_DEF_H_ #include "os.h" @@ -26,89 +24,91 @@ extern "C" { #define TSDB__packed -#define TSKEY int64_t -#define TSKEY_MIN INT64_MIN -#define TSKEY_MAX (INT64_MAX - 1) +#define TSKEY int64_t +#define TSKEY_MIN INT64_MIN +#define TSKEY_MAX (INT64_MAX - 1) #define TSKEY_INITIAL_VAL TSKEY_MIN // Bytes for each type. extern const int32_t TYPE_BYTES[15]; // TODO: replace and remove code below -#define CHAR_BYTES sizeof(char) -#define SHORT_BYTES sizeof(int16_t) -#define INT_BYTES sizeof(int32_t) -#define LONG_BYTES sizeof(int64_t) -#define FLOAT_BYTES sizeof(float) -#define DOUBLE_BYTES sizeof(double) -#define POINTER_BYTES sizeof(void *) // 8 by default assert(sizeof(ptrdiff_t) == sizseof(void*) -#define TSDB_KEYSIZE sizeof(TSKEY) -#define TSDB_NCHAR_SIZE sizeof(int32_t) +#define CHAR_BYTES sizeof(char) +#define SHORT_BYTES sizeof(int16_t) +#define INT_BYTES sizeof(int32_t) +#define LONG_BYTES sizeof(int64_t) +#define FLOAT_BYTES sizeof(float) +#define DOUBLE_BYTES sizeof(double) +#define POINTER_BYTES sizeof(void *) // 8 by default assert(sizeof(ptrdiff_t) == sizseof(void*) +#define TSDB_KEYSIZE sizeof(TSKEY) +#define TSDB_NCHAR_SIZE sizeof(int32_t) // NULL definition -#define TSDB_DATA_BOOL_NULL 0x02 -#define TSDB_DATA_TINYINT_NULL 0x80 -#define TSDB_DATA_SMALLINT_NULL 0x8000 -#define TSDB_DATA_INT_NULL 0x80000000L -#define TSDB_DATA_BIGINT_NULL 0x8000000000000000L -#define TSDB_DATA_TIMESTAMP_NULL TSDB_DATA_BIGINT_NULL +#define TSDB_DATA_BOOL_NULL 0x02 +#define TSDB_DATA_TINYINT_NULL 0x80 +#define TSDB_DATA_SMALLINT_NULL 0x8000 +#define TSDB_DATA_INT_NULL 0x80000000L +#define TSDB_DATA_BIGINT_NULL 0x8000000000000000L +#define TSDB_DATA_TIMESTAMP_NULL TSDB_DATA_BIGINT_NULL -#define TSDB_DATA_FLOAT_NULL 0x7FF00000 // it is an NAN -#define TSDB_DATA_DOUBLE_NULL 0x7FFFFF0000000000L // an NAN -#define TSDB_DATA_NCHAR_NULL 0xFFFFFFFF -#define TSDB_DATA_BINARY_NULL 0xFF +#define TSDB_DATA_FLOAT_NULL 0x7FF00000 // it is an NAN +#define TSDB_DATA_DOUBLE_NULL 0x7FFFFF0000000000L // an NAN +#define TSDB_DATA_NCHAR_NULL 0xFFFFFFFF +#define TSDB_DATA_BINARY_NULL 0xFF -#define TSDB_DATA_UTINYINT_NULL 0xFF -#define TSDB_DATA_USMALLINT_NULL 0xFFFF -#define TSDB_DATA_UINT_NULL 0xFFFFFFFF -#define TSDB_DATA_UBIGINT_NULL 0xFFFFFFFFFFFFFFFFL +#define TSDB_DATA_UTINYINT_NULL 0xFF +#define TSDB_DATA_USMALLINT_NULL 0xFFFF +#define TSDB_DATA_UINT_NULL 0xFFFFFFFF +#define TSDB_DATA_UBIGINT_NULL 0xFFFFFFFFFFFFFFFFL -#define TSDB_DATA_NULL_STR "NULL" -#define TSDB_DATA_NULL_STR_L "null" +#define TSDB_DATA_NULL_STR "NULL" +#define TSDB_DATA_NULL_STR_L "null" -#define TSDB_NETTEST_USER "nettestinternal" -#define TSDB_DEFAULT_USER "root" +#define TSDB_NETTEST_USER "nettestinternal" +#define TSDB_DEFAULT_USER "root" #ifdef _TD_POWER_ -#define TSDB_DEFAULT_PASS "powerdb" +#define TSDB_DEFAULT_PASS "powerdb" #elif (_TD_TQ_ == true) -#define TSDB_DEFAULT_PASS "tqueue" +#define TSDB_DEFAULT_PASS "tqueue" #elif (_TD_PRO_ == true) -#define TSDB_DEFAULT_PASS "prodb" +#define TSDB_DEFAULT_PASS "prodb" #else -#define TSDB_DEFAULT_PASS "taosdata" +#define TSDB_DEFAULT_PASS "taosdata" #endif -#define SHELL_MAX_PASSWORD_LEN 20 +#define SHELL_MAX_PASSWORD_LEN 20 -#define TSDB_TRUE 1 -#define TSDB_FALSE 0 -#define TSDB_OK 0 +#define TSDB_TRUE 1 +#define TSDB_FALSE 0 +#define TSDB_OK 0 #define TSDB_ERR -1 #define TS_PATH_DELIMITER "." #define TS_ESCAPE_CHAR '`' -#define TSDB_TIME_PRECISION_MILLI 0 -#define TSDB_TIME_PRECISION_MICRO 1 -#define TSDB_TIME_PRECISION_NANO 2 +#define TSDB_TIME_PRECISION_MILLI 0 +#define TSDB_TIME_PRECISION_MICRO 1 +#define TSDB_TIME_PRECISION_NANO 2 #define TSDB_TIME_PRECISION_MILLI_STR "ms" #define TSDB_TIME_PRECISION_MICRO_STR "us" #define TSDB_TIME_PRECISION_NANO_STR "ns" -#define TSDB_TICK_PER_SECOND(precision) ((int64_t)((precision)==TSDB_TIME_PRECISION_MILLI ? 1e3L : ((precision)==TSDB_TIME_PRECISION_MICRO ? 1e6L : 1e9L))) +#define TSDB_TICK_PER_SECOND(precision) \ + ((int64_t)((precision) == TSDB_TIME_PRECISION_MILLI ? 1e3L \ + : ((precision) == TSDB_TIME_PRECISION_MICRO ? 1e6L : 1e9L))) #define T_MEMBER_SIZE(type, member) sizeof(((type *)0)->member) -#define T_APPEND_MEMBER(dst, ptr, type, member) \ -do {\ - memcpy((void *)(dst), (void *)(&((ptr)->member)), T_MEMBER_SIZE(type, member));\ - dst = (void *)((char *)(dst) + T_MEMBER_SIZE(type, member));\ -} while(0) -#define T_READ_MEMBER(src, type, target) \ -do { \ - (target) = *(type *)(src); \ - (src) = (void *)((char *)src + sizeof(type));\ -} while(0) +#define T_APPEND_MEMBER(dst, ptr, type, member) \ + do { \ + memcpy((void *)(dst), (void *)(&((ptr)->member)), T_MEMBER_SIZE(type, member)); \ + dst = (void *)((char *)(dst) + T_MEMBER_SIZE(type, member)); \ + } while (0) +#define T_READ_MEMBER(src, type, target) \ + do { \ + (target) = *(type *)(src); \ + (src) = (void *)((char *)src + sizeof(type)); \ + } while (0) typedef enum EOperatorType { // arithmetic operator @@ -149,267 +149,269 @@ typedef enum EOperatorType { OP_TYPE_JSON_CONTAINS } EOperatorType; - typedef enum ELogicConditionType { LOGIC_COND_TYPE_AND, LOGIC_COND_TYPE_OR, LOGIC_COND_TYPE_NOT, } ELogicConditionType; +#define FUNCTION_CEIL 4500 +#define FUNCTION_FLOOR 4501 +#define FUNCTION_ABS 4502 +#define FUNCTION_ROUND 4503 -#define FUNCTION_CEIL 4500 -#define FUNCTION_FLOOR 4501 -#define FUNCTION_ABS 4502 -#define FUNCTION_ROUND 4503 +#define FUNCTION_LENGTH 4800 +#define FUNCTION_CONCAT 4801 +#define FUNCTION_LTRIM 4802 +#define FUNCTION_RTRIM 4803 -#define FUNCTION_LENGTH 4800 -#define FUNCTION_CONCAT 4801 -#define FUNCTION_LTRIM 4802 -#define FUNCTION_RTRIM 4803 +#define TSDB_NAME_DELIMITER_LEN 1 -#define TSDB_NAME_DELIMITER_LEN 1 - -#define TSDB_UNI_LEN 24 -#define TSDB_USER_LEN TSDB_UNI_LEN +#define TSDB_UNI_LEN 24 +#define TSDB_USER_LEN TSDB_UNI_LEN // ACCOUNT is a 32 bit positive integer // this is the length of its string representation, including the terminator zero -#define TSDB_ACCT_ID_LEN 11 +#define TSDB_ACCT_ID_LEN 11 -#define TSDB_MAX_COLUMNS 4096 -#define TSDB_MIN_COLUMNS 2 //PRIMARY COLUMN(timestamp) + other columns +#define TSDB_MAX_COLUMNS 4096 +#define TSDB_MIN_COLUMNS 2 // PRIMARY COLUMN(timestamp) + other columns -#define TSDB_NODE_NAME_LEN 64 -#define TSDB_TABLE_NAME_LEN 193 // it is a null-terminated string -#define TSDB_TOPIC_NAME_LEN 193 // it is a null-terminated string -#define TSDB_DB_NAME_LEN 65 -#define TSDB_DB_FNAME_LEN (TSDB_ACCT_ID_LEN + TSDB_DB_NAME_LEN + TSDB_NAME_DELIMITER_LEN) +#define TSDB_NODE_NAME_LEN 64 +#define TSDB_TABLE_NAME_LEN 193 // it is a null-terminated string +#define TSDB_TOPIC_NAME_LEN 193 // it is a null-terminated string +#define TSDB_DB_NAME_LEN 65 +#define TSDB_DB_FNAME_LEN (TSDB_ACCT_ID_LEN + TSDB_DB_NAME_LEN + TSDB_NAME_DELIMITER_LEN) -#define TSDB_FUNC_NAME_LEN 65 -#define TSDB_FUNC_COMMENT_LEN 4096 -#define TSDB_FUNC_CODE_LEN (65535 - 512) -#define TSDB_FUNC_BUF_SIZE 512 -#define TSDB_FUNC_TYPE_SCALAR 1 -#define TSDB_FUNC_TYPE_AGGREGATE 2 -#define TSDB_FUNC_MAX_RETRIEVE 1024 +#define TSDB_FUNC_NAME_LEN 65 +#define TSDB_FUNC_COMMENT_LEN 4096 +#define TSDB_FUNC_CODE_LEN (65535 - 512) +#define TSDB_FUNC_BUF_SIZE 512 +#define TSDB_FUNC_TYPE_SCALAR 1 +#define TSDB_FUNC_TYPE_AGGREGATE 2 +#define TSDB_FUNC_MAX_RETRIEVE 1024 -#define TSDB_TYPE_STR_MAX_LEN 32 -#define TSDB_TABLE_FNAME_LEN (TSDB_DB_FNAME_LEN + TSDB_TABLE_NAME_LEN + TSDB_NAME_DELIMITER_LEN) -#define TSDB_TOPIC_FNAME_LEN TSDB_TABLE_FNAME_LEN -#define TSDB_CONSUMER_GROUP_LEN 192 -#define TSDB_SUBSCRIBE_KEY_LEN (TSDB_CONSUMER_GROUP_LEN + TSDB_TOPIC_FNAME_LEN + 2) -#define TSDB_PARTITION_KEY_LEN (TSDB_CONSUMER_GROUP_LEN + TSDB_TOPIC_FNAME_LEN + 2) -#define TSDB_COL_NAME_LEN 65 -#define TSDB_MAX_SAVED_SQL_LEN TSDB_MAX_COLUMNS * 64 -#define TSDB_MAX_SQL_LEN TSDB_PAYLOAD_SIZE -#define TSDB_MAX_SQL_SHOW_LEN 1024 -#define TSDB_MAX_ALLOWED_SQL_LEN (1*1024*1024u) // sql length should be less than 1mb +#define TSDB_TYPE_STR_MAX_LEN 32 +#define TSDB_TABLE_FNAME_LEN (TSDB_DB_FNAME_LEN + TSDB_TABLE_NAME_LEN + TSDB_NAME_DELIMITER_LEN) +#define TSDB_TOPIC_FNAME_LEN TSDB_TABLE_FNAME_LEN +#define TSDB_CONSUMER_GROUP_LEN 192 +#define TSDB_SUBSCRIBE_KEY_LEN (TSDB_CONSUMER_GROUP_LEN + TSDB_TOPIC_FNAME_LEN + 2) +#define TSDB_PARTITION_KEY_LEN (TSDB_CONSUMER_GROUP_LEN + TSDB_TOPIC_FNAME_LEN + 2) +#define TSDB_COL_NAME_LEN 65 +#define TSDB_MAX_SAVED_SQL_LEN TSDB_MAX_COLUMNS * 64 +#define TSDB_MAX_SQL_LEN TSDB_PAYLOAD_SIZE +#define TSDB_MAX_SQL_SHOW_LEN 1024 +#define TSDB_MAX_ALLOWED_SQL_LEN (1 * 1024 * 1024u) // sql length should be less than 1mb -#define TSDB_APP_NAME_LEN TSDB_UNI_LEN -#define TSDB_STB_COMMENT_LEN 1024 - /** - * In some scenarios uint16_t (0~65535) is used to store the row len. - * - Firstly, we use 65531(65535 - 4), as the SDataRow/SKVRow contains 4 bits header. - * - Secondly, if all cols are VarDataT type except primary key, we need 4 bits to store the offset, thus - * the final value is 65531-(4096-1)*4 = 49151. - */ -#define TSDB_MAX_BYTES_PER_ROW 49151 -#define TSDB_MAX_TAGS_LEN 16384 -#define TSDB_MAX_TAGS 128 -#define TSDB_MAX_TAG_CONDITIONS 1024 +#define TSDB_APP_NAME_LEN TSDB_UNI_LEN +#define TSDB_STB_COMMENT_LEN 1024 +/** + * In some scenarios uint16_t (0~65535) is used to store the row len. + * - Firstly, we use 65531(65535 - 4), as the SDataRow/SKVRow contains 4 bits header. + * - Secondly, if all cols are VarDataT type except primary key, we need 4 bits to store the offset, thus + * the final value is 65531-(4096-1)*4 = 49151. + */ +#define TSDB_MAX_BYTES_PER_ROW 49151 +#define TSDB_MAX_TAGS_LEN 16384 +#define TSDB_MAX_TAGS 128 +#define TSDB_MAX_TAG_CONDITIONS 1024 -#define TSDB_AUTH_LEN 16 -#define TSDB_PASSWORD_LEN 32 -#define TSDB_USET_PASSWORD_LEN 129 -#define TSDB_VERSION_LEN 12 -#define TSDB_LABEL_LEN 8 +#define TSDB_AUTH_LEN 16 +#define TSDB_PASSWORD_LEN 32 +#define TSDB_USET_PASSWORD_LEN 129 +#define TSDB_VERSION_LEN 12 +#define TSDB_LABEL_LEN 8 -#define TSDB_CLUSTER_ID_LEN 40 -#define TSDB_FQDN_LEN 128 -#define TSDB_EP_LEN (TSDB_FQDN_LEN + 6) -#define TSDB_IPv4ADDR_LEN 16 -#define TSDB_FILENAME_LEN 128 -#define TSDB_SHOW_SQL_LEN 512 -#define TSDB_SHOW_SUBQUERY_LEN 1000 -#define TSDB_SLOW_QUERY_SQL_LEN 512 +#define TSDB_CLUSTER_ID_LEN 40 +#define TSDB_FQDN_LEN 128 +#define TSDB_EP_LEN (TSDB_FQDN_LEN + 6) +#define TSDB_IPv4ADDR_LEN 16 +#define TSDB_FILENAME_LEN 128 +#define TSDB_SHOW_SQL_LEN 512 +#define TSDB_SHOW_SUBQUERY_LEN 1000 +#define TSDB_SLOW_QUERY_SQL_LEN 512 -#define TSDB_TRANS_STAGE_LEN 12 -#define TSDB_TRANS_TYPE_LEN 16 -#define TSDB_TRANS_ERROR_LEN 64 +#define TSDB_TRANS_STAGE_LEN 12 +#define TSDB_TRANS_TYPE_LEN 16 +#define TSDB_TRANS_ERROR_LEN 64 -#define TSDB_STEP_NAME_LEN 32 -#define TSDB_STEP_DESC_LEN 128 +#define TSDB_STEP_NAME_LEN 32 +#define TSDB_STEP_DESC_LEN 128 -#define TSDB_ERROR_MSG_LEN 1024 -#define TSDB_DNODE_CONFIG_LEN 128 -#define TSDB_DNODE_VALUE_LEN 256 +#define TSDB_ERROR_MSG_LEN 1024 +#define TSDB_DNODE_CONFIG_LEN 128 +#define TSDB_DNODE_VALUE_LEN 256 -#define TSDB_MQTT_HOSTNAME_LEN 64 -#define TSDB_MQTT_PORT_LEN 8 -#define TSDB_MQTT_USER_LEN 24 -#define TSDB_MQTT_PASS_LEN 24 -#define TSDB_MQTT_TOPIC_LEN 64 -#define TSDB_MQTT_CLIENT_ID_LEN 32 +#define TSDB_MQTT_HOSTNAME_LEN 64 +#define TSDB_MQTT_PORT_LEN 8 +#define TSDB_MQTT_USER_LEN 24 +#define TSDB_MQTT_PASS_LEN 24 +#define TSDB_MQTT_TOPIC_LEN 64 +#define TSDB_MQTT_CLIENT_ID_LEN 32 -#define TSDB_DB_TYPE_DEFAULT 0 -#define TSDB_DB_TYPE_TOPIC 1 +#define TSDB_DB_TYPE_DEFAULT 0 +#define TSDB_DB_TYPE_TOPIC 1 -#define TSDB_DEFAULT_PKT_SIZE 65480 //same as RPC_MAX_UDP_SIZE +#define TSDB_DEFAULT_PKT_SIZE 65480 // same as RPC_MAX_UDP_SIZE #define TSDB_PAYLOAD_SIZE TSDB_DEFAULT_PKT_SIZE -#define TSDB_DEFAULT_PAYLOAD_SIZE 5120 // default payload size, greater than PATH_MAX value -#define TSDB_EXTRA_PAYLOAD_SIZE 128 // extra bytes for auth +#define TSDB_DEFAULT_PAYLOAD_SIZE 5120 // default payload size, greater than PATH_MAX value +#define TSDB_EXTRA_PAYLOAD_SIZE 128 // extra bytes for auth #define TSDB_CQ_SQL_SIZE 1024 #define TSDB_MIN_VNODES 16 #define TSDB_MAX_VNODES 512 #define TSDB_MIN_VNODES_PER_DB 1 #define TSDB_MAX_VNODES_PER_DB 4096 -#define TSDB_DEFAULT_VN_PER_DB 2 +#define TSDB_DEFAULT_VN_PER_DB 2 -#define TSDB_DNODE_ROLE_ANY 0 -#define TSDB_DNODE_ROLE_MGMT 1 -#define TSDB_DNODE_ROLE_VNODE 2 +#define TSDB_DNODE_ROLE_ANY 0 +#define TSDB_DNODE_ROLE_MGMT 1 +#define TSDB_DNODE_ROLE_VNODE 2 -#define TSDB_MAX_REPLICA 5 +#define TSDB_MAX_REPLICA 5 -#define TSDB_TBNAME_COLUMN_INDEX (-1) -#define TSDB_UD_COLUMN_INDEX (-1000) -#define TSDB_RES_COL_ID (-5000) +#define TSDB_TBNAME_COLUMN_INDEX (-1) +#define TSDB_UD_COLUMN_INDEX (-1000) +#define TSDB_RES_COL_ID (-5000) -#define TSDB_MULTI_TABLEMETA_MAX_NUM 100000 // maximum batch size allowed to load table meta - -#define TSDB_MIN_CACHE_BLOCK_SIZE 1 -#define TSDB_MAX_CACHE_BLOCK_SIZE 128 // 128MB for each vnode -#define TSDB_DEFAULT_CACHE_BLOCK_SIZE 16 +#define TSDB_MULTI_TABLEMETA_MAX_NUM 100000 // maximum batch size allowed to load table meta -#define TSDB_MIN_TOTAL_BLOCKS 3 -#define TSDB_MAX_TOTAL_BLOCKS 10000 -#define TSDB_DEFAULT_TOTAL_BLOCKS 6 +#define TSDB_MIN_CACHE_BLOCK_SIZE 1 +#define TSDB_MAX_CACHE_BLOCK_SIZE 128 // 128MB for each vnode +#define TSDB_DEFAULT_CACHE_BLOCK_SIZE 16 -#define TSDB_MIN_DAYS_PER_FILE 1 -#define TSDB_MAX_DAYS_PER_FILE 3650 -#define TSDB_DEFAULT_DAYS_PER_FILE 10 +#define TSDB_MIN_TOTAL_BLOCKS 3 +#define TSDB_MAX_TOTAL_BLOCKS 10000 +#define TSDB_DEFAULT_TOTAL_BLOCKS 6 -#define TSDB_MIN_KEEP 1 // data in db to be reserved. -#define TSDB_MAX_KEEP 365000 // data in db to be reserved. -#define TSDB_DEFAULT_KEEP 3650 // ten years +#define TSDB_MIN_DAYS_PER_FILE 1 +#define TSDB_MAX_DAYS_PER_FILE 3650 +#define TSDB_DEFAULT_DAYS_PER_FILE 10 -#define TSDB_MIN_MIN_ROW_FBLOCK 10 -#define TSDB_MAX_MIN_ROW_FBLOCK 1000 -#define TSDB_DEFAULT_MIN_ROW_FBLOCK 100 +#define TSDB_MIN_KEEP 1 // data in db to be reserved. +#define TSDB_MAX_KEEP 365000 // data in db to be reserved. +#define TSDB_DEFAULT_KEEP 3650 // ten years -#define TSDB_MIN_MAX_ROW_FBLOCK 200 -#define TSDB_MAX_MAX_ROW_FBLOCK 10000 -#define TSDB_DEFAULT_MAX_ROW_FBLOCK 4096 +#define TSDB_MIN_MIN_ROW_FBLOCK 10 +#define TSDB_MAX_MIN_ROW_FBLOCK 1000 +#define TSDB_DEFAULT_MIN_ROW_FBLOCK 100 -#define TSDB_MIN_COMMIT_TIME 30 -#define TSDB_MAX_COMMIT_TIME 40960 -#define TSDB_DEFAULT_COMMIT_TIME 3600 +#define TSDB_MIN_MAX_ROW_FBLOCK 200 +#define TSDB_MAX_MAX_ROW_FBLOCK 10000 +#define TSDB_DEFAULT_MAX_ROW_FBLOCK 4096 -#define TSDB_MIN_FSYNC_PERIOD 0 -#define TSDB_MAX_FSYNC_PERIOD 180000 // millisecond -#define TSDB_DEFAULT_FSYNC_PERIOD 3000 // three second +#define TSDB_MIN_COMMIT_TIME 30 +#define TSDB_MAX_COMMIT_TIME 40960 +#define TSDB_DEFAULT_COMMIT_TIME 3600 -#define TSDB_MIN_WAL_LEVEL 0 -#define TSDB_MAX_WAL_LEVEL 2 -#define TSDB_DEFAULT_WAL_LEVEL 1 +#define TSDB_MIN_FSYNC_PERIOD 0 +#define TSDB_MAX_FSYNC_PERIOD 180000 // millisecond +#define TSDB_DEFAULT_FSYNC_PERIOD 3000 // three second -#define TSDB_MIN_PRECISION TSDB_TIME_PRECISION_MILLI -#define TSDB_MAX_PRECISION TSDB_TIME_PRECISION_NANO -#define TSDB_DEFAULT_PRECISION TSDB_TIME_PRECISION_MILLI +#define TSDB_MIN_WAL_LEVEL 0 +#define TSDB_MAX_WAL_LEVEL 2 +#define TSDB_DEFAULT_WAL_LEVEL 1 -#define TSDB_MIN_COMP_LEVEL 0 -#define TSDB_MAX_COMP_LEVEL 2 -#define TSDB_DEFAULT_COMP_LEVEL 2 +#define TSDB_MIN_PRECISION TSDB_TIME_PRECISION_MILLI +#define TSDB_MAX_PRECISION TSDB_TIME_PRECISION_NANO +#define TSDB_DEFAULT_PRECISION TSDB_TIME_PRECISION_MILLI -#define TSDB_MIN_DB_REPLICA_OPTION 1 -#define TSDB_MAX_DB_REPLICA_OPTION 3 -#define TSDB_DEFAULT_DB_REPLICA_OPTION 1 +#define TSDB_MIN_COMP_LEVEL 0 +#define TSDB_MAX_COMP_LEVEL 2 +#define TSDB_DEFAULT_COMP_LEVEL 2 -#define TSDB_MIN_DB_QUORUM_OPTION 1 -#define TSDB_MAX_DB_QUORUM_OPTION 2 -#define TSDB_DEFAULT_DB_QUORUM_OPTION 1 +#define TSDB_MIN_DB_REPLICA_OPTION 1 +#define TSDB_MAX_DB_REPLICA_OPTION 3 +#define TSDB_DEFAULT_DB_REPLICA_OPTION 1 -#define TSDB_MIN_DB_UPDATE 0 -#define TSDB_MAX_DB_UPDATE 2 -#define TSDB_DEFAULT_DB_UPDATE_OPTION 0 +#define TSDB_MIN_DB_QUORUM_OPTION 1 +#define TSDB_MAX_DB_QUORUM_OPTION 2 +#define TSDB_DEFAULT_DB_QUORUM_OPTION 1 -#define TSDB_MIN_DB_CACHE_LAST_ROW 0 -#define TSDB_MAX_DB_CACHE_LAST_ROW 3 -#define TSDB_DEFAULT_CACHE_LAST_ROW 0 +#define TSDB_MIN_DB_UPDATE 0 +#define TSDB_MAX_DB_UPDATE 2 +#define TSDB_DEFAULT_DB_UPDATE_OPTION 0 -#define TSDB_MAX_JOIN_TABLE_NUM 10 -#define TSDB_MAX_UNION_CLAUSE 5 +#define TSDB_MIN_DB_CACHE_LAST_ROW 0 +#define TSDB_MAX_DB_CACHE_LAST_ROW 3 +#define TSDB_DEFAULT_CACHE_LAST_ROW 0 -#define TSDB_MAX_FIELD_LEN 16384 -#define TSDB_MAX_BINARY_LEN (TSDB_MAX_FIELD_LEN-TSDB_KEYSIZE) // keep 16384 -#define TSDB_MAX_NCHAR_LEN (TSDB_MAX_FIELD_LEN-TSDB_KEYSIZE) // keep 16384 -#define PRIMARYKEY_TIMESTAMP_COL_ID 1 -#define COL_REACH_END(colId, maxColId) ((colId) > (maxColId)) +#define TSDB_MAX_JOIN_TABLE_NUM 10 +#define TSDB_MAX_UNION_CLAUSE 5 -#define TSDB_MAX_RPC_THREADS 5 +#define TSDB_MAX_FIELD_LEN 16384 +#define TSDB_MAX_BINARY_LEN (TSDB_MAX_FIELD_LEN - TSDB_KEYSIZE) // keep 16384 +#define TSDB_MAX_NCHAR_LEN (TSDB_MAX_FIELD_LEN - TSDB_KEYSIZE) // keep 16384 +#define PRIMARYKEY_TIMESTAMP_COL_ID 1 +#define COL_REACH_END(colId, maxColId) ((colId) > (maxColId)) -#define TSDB_QUERY_TYPE_NON_TYPE 0x00u // none type -#define TSDB_QUERY_TYPE_FREE_RESOURCE 0x01u // free qhandle at vnode - -#define TSDB_META_COMPACT_RATIO 0 // disable tsdb meta compact by default +#define TSDB_MAX_RPC_THREADS 5 +#define TSDB_QUERY_TYPE_NON_TYPE 0x00u // none type +#define TSDB_QUERY_TYPE_FREE_RESOURCE 0x01u // free qhandle at vnode +#define TSDB_META_COMPACT_RATIO 0 // disable tsdb meta compact by default /* * 1. ordinary sub query for select * from super_table * 2. all sqlobj generated by createSubqueryObj with this flag */ -#define TSDB_QUERY_TYPE_SUBQUERY 0x02u -#define TSDB_QUERY_TYPE_STABLE_SUBQUERY 0x04u // two-stage subquery for super table +#define TSDB_QUERY_TYPE_SUBQUERY 0x02u +#define TSDB_QUERY_TYPE_STABLE_SUBQUERY 0x04u // two-stage subquery for super table -#define TSDB_QUERY_TYPE_TABLE_QUERY 0x08u // query ordinary table; below only apply to client side -#define TSDB_QUERY_TYPE_STABLE_QUERY 0x10u // query on super table -#define TSDB_QUERY_TYPE_JOIN_QUERY 0x20u // join query -#define TSDB_QUERY_TYPE_PROJECTION_QUERY 0x40u // select *,columns... query -#define TSDB_QUERY_TYPE_JOIN_SEC_STAGE 0x80u // join sub query at the second stage +#define TSDB_QUERY_TYPE_TABLE_QUERY 0x08u // query ordinary table; below only apply to client side +#define TSDB_QUERY_TYPE_STABLE_QUERY 0x10u // query on super table +#define TSDB_QUERY_TYPE_JOIN_QUERY 0x20u // join query +#define TSDB_QUERY_TYPE_PROJECTION_QUERY 0x40u // select *,columns... query +#define TSDB_QUERY_TYPE_JOIN_SEC_STAGE 0x80u // join sub query at the second stage -#define TSDB_QUERY_TYPE_TAG_FILTER_QUERY 0x400u -#define TSDB_QUERY_TYPE_INSERT 0x100u // insert type -#define TSDB_QUERY_TYPE_MULTITABLE_QUERY 0x200u -#define TSDB_QUERY_TYPE_FILE_INSERT 0x400u // insert data from file -#define TSDB_QUERY_TYPE_STMT_INSERT 0x800u // stmt insert type -#define TSDB_QUERY_TYPE_NEST_SUBQUERY 0x1000u // nested sub query +#define TSDB_QUERY_TYPE_TAG_FILTER_QUERY 0x400u +#define TSDB_QUERY_TYPE_INSERT 0x100u // insert type +#define TSDB_QUERY_TYPE_MULTITABLE_QUERY 0x200u +#define TSDB_QUERY_TYPE_FILE_INSERT 0x400u // insert data from file +#define TSDB_QUERY_TYPE_STMT_INSERT 0x800u // stmt insert type +#define TSDB_QUERY_TYPE_NEST_SUBQUERY 0x1000u // nested sub query -#define TSDB_QUERY_HAS_TYPE(x, _type) (((x) & (_type)) != 0) -#define TSDB_QUERY_SET_TYPE(x, _type) ((x) |= (_type)) -#define TSDB_QUERY_CLEAR_TYPE(x, _type) ((x) &= (~_type)) -#define TSDB_QUERY_RESET_TYPE(x) ((x) = TSDB_QUERY_TYPE_NON_TYPE) +#define TSDB_QUERY_HAS_TYPE(x, _type) (((x) & (_type)) != 0) +#define TSDB_QUERY_SET_TYPE(x, _type) ((x) |= (_type)) +#define TSDB_QUERY_CLEAR_TYPE(x, _type) ((x) &= (~_type)) +#define TSDB_QUERY_RESET_TYPE(x) ((x) = TSDB_QUERY_TYPE_NON_TYPE) -#define TSDB_ORDER_ASC 1 -#define TSDB_ORDER_DESC 2 - -#define TSDB_DEFAULT_CLUSTER_HASH_SIZE 1 -#define TSDB_DEFAULT_MNODES_HASH_SIZE 5 -#define TSDB_DEFAULT_DNODES_HASH_SIZE 10 -#define TSDB_DEFAULT_ACCOUNTS_HASH_SIZE 10 -#define TSDB_DEFAULT_USERS_HASH_SIZE 20 -#define TSDB_DEFAULT_DBS_HASH_SIZE 100 -#define TSDB_DEFAULT_VGROUPS_HASH_SIZE 100 -#define TSDB_DEFAULT_STABLES_HASH_SIZE 100 -#define TSDB_DEFAULT_CTABLES_HASH_SIZE 20000 +#define TSDB_ORDER_ASC 1 +#define TSDB_ORDER_DESC 2 -#define TSDB_MAX_WAL_SIZE (1024*1024*3) +#define TSDB_DEFAULT_CLUSTER_HASH_SIZE 1 +#define TSDB_DEFAULT_MNODES_HASH_SIZE 5 +#define TSDB_DEFAULT_DNODES_HASH_SIZE 10 +#define TSDB_DEFAULT_ACCOUNTS_HASH_SIZE 10 +#define TSDB_DEFAULT_USERS_HASH_SIZE 20 +#define TSDB_DEFAULT_DBS_HASH_SIZE 100 +#define TSDB_DEFAULT_VGROUPS_HASH_SIZE 100 +#define TSDB_DEFAULT_STABLES_HASH_SIZE 100 +#define TSDB_DEFAULT_CTABLES_HASH_SIZE 20000 -#define TSDB_ARB_DUMMY_TIME 4765104000000 // 2121-01-01 00:00:00.000, :P +#define TSDB_MAX_WAL_SIZE (1024 * 1024 * 3) -#define TFS_MAX_TIERS 3 +#define TSDB_ARB_DUMMY_TIME 4765104000000 // 2121-01-01 00:00:00.000, :P + +#define TFS_MAX_TIERS 3 #define TFS_MAX_DISKS_PER_TIER 16 -#define TFS_MAX_DISKS (TFS_MAX_TIERS * TFS_MAX_DISKS_PER_TIER) -#define TFS_MIN_LEVEL 0 -#define TFS_MAX_LEVEL (TFS_MAX_TIERS - 1) -#define TFS_PRIMARY_LEVEL 0 -#define TFS_PRIMARY_ID 0 +#define TFS_MAX_DISKS (TFS_MAX_TIERS * TFS_MAX_DISKS_PER_TIER) +#define TFS_MIN_LEVEL 0 +#define TFS_MAX_LEVEL (TFS_MAX_TIERS - 1) +#define TFS_PRIMARY_LEVEL 0 +#define TFS_PRIMARY_ID 0 #define TFS_MIN_DISK_FREE_SIZE 50 * 1024 * 1024 enum { TRANS_STAT_INIT = 0, TRANS_STAT_EXECUTING, TRANS_STAT_EXECUTED, TRANS_STAT_ROLLBACKING, TRANS_STAT_ROLLBACKED }; enum { TRANS_OPER_INIT = 0, TRANS_OPER_EXECUTE, TRANS_OPER_ROLLBACK }; +typedef struct { + char dir[TSDB_FILENAME_LEN]; + int32_t level; + int32_t primary; +} SDiskCfg; + #ifdef __cplusplus } #endif diff --git a/include/util/tcfg.h b/include/util/tdes.h similarity index 77% rename from include/util/tcfg.h rename to include/util/tdes.h index 7ccfd4b0f5..edb325da28 100644 --- a/include/util/tcfg.h +++ b/include/util/tdes.h @@ -13,23 +13,20 @@ * along with this program. If not, see . */ -#ifndef _TD_COMMON_CFG_H_ -#define _TD_COMMON_CFG_H_ +#ifndef _TD_UTIL_DES_H +#define _TD_UTIL_DES_H + +#include "os.h" #ifdef __cplusplus extern "C" { #endif -#include "tdef.h" - -typedef struct { - char dir[TSDB_FILENAME_LEN]; - int level; - int primary; -} SDiskCfg; +char *taosDesEncode(int64_t key, char *src, int32_t len); +char *taosDesDecode(int64_t key, char *src, int32_t len); #ifdef __cplusplus } #endif -#endif /*_TD_COMMON_CFG_H_*/ +#endif /*_TD_UTIL_DES_H*/ \ No newline at end of file diff --git a/include/util/encode.h b/include/util/tencode.h similarity index 58% rename from include/util/encode.h rename to include/util/tencode.h index ba63759737..c058eebb50 100644 --- a/include/util/encode.h +++ b/include/util/tencode.h @@ -16,8 +16,8 @@ #ifndef _TD_UTIL_ENCODE_H_ #define _TD_UTIL_ENCODE_H_ -#include "freelist.h" #include "tcoding.h" +#include "tfreelist.h" #include "tmacro.h" #ifdef __cplusplus @@ -71,62 +71,62 @@ typedef struct { TD_SLIST(SCoderNode) stack; } SCoder; -#define TD_CODER_POS(CODER) ((CODER)->pos) -#define TD_CODER_CURRENT(CODER) ((CODER)->data + (CODER)->pos) -#define TD_CODER_MOVE_POS(CODER, MOVE) ((CODER)->pos += (MOVE)) +#define TD_CODER_POS(CODER) ((CODER)->pos) +#define TD_CODER_CURRENT(CODER) ((CODER)->data + (CODER)->pos) +#define TD_CODER_MOVE_POS(CODER, MOVE) ((CODER)->pos += (MOVE)) #define TD_CODER_CHECK_CAPACITY_FAILED(CODER, EXPSIZE) (((CODER)->size - (CODER)->pos) < (EXPSIZE)) -#define TCODER_MALLOC(SIZE, CODER) TFL_MALLOC(SIZE, &((CODER)->fl)) +#define TCODER_MALLOC(SIZE, CODER) TFL_MALLOC(SIZE, &((CODER)->fl)) void tCoderInit(SCoder* pCoder, td_endian_t endian, uint8_t* data, int32_t size, td_coder_t type); void tCoderClear(SCoder* pCoder); /* ------------------------ ENCODE ------------------------ */ -int tStartEncode(SCoder* pEncoder); -void tEndEncode(SCoder* pEncoder); -static int tEncodeU8(SCoder* pEncoder, uint8_t val); -static int tEncodeI8(SCoder* pEncoder, int8_t val); -static int tEncodeU16(SCoder* pEncoder, uint16_t val); -static int tEncodeI16(SCoder* pEncoder, int16_t val); -static int tEncodeU32(SCoder* pEncoder, uint32_t val); -static int tEncodeI32(SCoder* pEncoder, int32_t val); -static int tEncodeU64(SCoder* pEncoder, uint64_t val); -static int tEncodeI64(SCoder* pEncoder, int64_t val); -static int tEncodeU16v(SCoder* pEncoder, uint16_t val); -static int tEncodeI16v(SCoder* pEncoder, int16_t val); -static int tEncodeU32v(SCoder* pEncoder, uint32_t val); -static int tEncodeI32v(SCoder* pEncoder, int32_t val); -static int tEncodeU64v(SCoder* pEncoder, uint64_t val); -static int tEncodeI64v(SCoder* pEncoder, int64_t val); -static int tEncodeFloat(SCoder* pEncoder, float val); -static int tEncodeDouble(SCoder* pEncoder, double val); -static int tEncodeBinary(SCoder* pEncoder, const void* val, uint64_t len); -static int tEncodeCStrWithLen(SCoder* pEncoder, const char* val, uint64_t len); -static int tEncodeCStr(SCoder* pEncoder, const char* val); +int32_t tStartEncode(SCoder* pEncoder); +void tEndEncode(SCoder* pEncoder); +static int32_t tEncodeU8(SCoder* pEncoder, uint8_t val); +static int32_t tEncodeI8(SCoder* pEncoder, int8_t val); +static int32_t tEncodeU16(SCoder* pEncoder, uint16_t val); +static int32_t tEncodeI16(SCoder* pEncoder, int16_t val); +static int32_t tEncodeU32(SCoder* pEncoder, uint32_t val); +static int32_t tEncodeI32(SCoder* pEncoder, int32_t val); +static int32_t tEncodeU64(SCoder* pEncoder, uint64_t val); +static int32_t tEncodeI64(SCoder* pEncoder, int64_t val); +static int32_t tEncodeU16v(SCoder* pEncoder, uint16_t val); +static int32_t tEncodeI16v(SCoder* pEncoder, int16_t val); +static int32_t tEncodeU32v(SCoder* pEncoder, uint32_t val); +static int32_t tEncodeI32v(SCoder* pEncoder, int32_t val); +static int32_t tEncodeU64v(SCoder* pEncoder, uint64_t val); +static int32_t tEncodeI64v(SCoder* pEncoder, int64_t val); +static int32_t tEncodeFloat(SCoder* pEncoder, float val); +static int32_t tEncodeDouble(SCoder* pEncoder, double val); +static int32_t tEncodeBinary(SCoder* pEncoder, const void* val, uint64_t len); +static int32_t tEncodeCStrWithLen(SCoder* pEncoder, const char* val, uint64_t len); +static int32_t tEncodeCStr(SCoder* pEncoder, const char* val); /* ------------------------ DECODE ------------------------ */ -int tStartDecode(SCoder* pDecoder); -void tEndDecode(SCoder* pDecoder); -static bool tDecodeIsEnd(SCoder* pCoder); -static int tDecodeU8(SCoder* pDecoder, uint8_t* val); -static int tDecodeI8(SCoder* pDecoder, int8_t* val); -static int tDecodeU16(SCoder* pDecoder, uint16_t* val); -static int tDecodeI16(SCoder* pDecoder, int16_t* val); -static int tDecodeU32(SCoder* pDecoder, uint32_t* val); -static int tDecodeI32(SCoder* pDecoder, int32_t* val); -static int tDecodeU64(SCoder* pDecoder, uint64_t* val); -static int tDecodeI64(SCoder* pDecoder, int64_t* val); -static int tDecodeU16v(SCoder* pDecoder, uint16_t* val); -static int tDecodeI16v(SCoder* pDecoder, int16_t* val); -static int tDecodeU32v(SCoder* pDecoder, uint32_t* val); -static int tDecodeI32v(SCoder* pDecoder, int32_t* val); -static int tDecodeU64v(SCoder* pDecoder, uint64_t* val); -static int tDecodeI64v(SCoder* pDecoder, int64_t* val); -static int tDecodeFloat(SCoder* pDecoder, float* val); -static int tDecodeDouble(SCoder* pDecoder, double* val); -static int tDecodeBinary(SCoder* pDecoder, const void** val, uint64_t* len); -static int tDecodeCStrAndLen(SCoder* pDecoder, const char** val, uint64_t* len); -static int tDecodeCStr(SCoder* pDecoder, const char** val); -static int tDecodeCStrTo(SCoder* pDecoder, char* val); +int32_t tStartDecode(SCoder* pDecoder); +void tEndDecode(SCoder* pDecoder); +static bool tDecodeIsEnd(SCoder* pCoder); +static int32_t tDecodeU8(SCoder* pDecoder, uint8_t* val); +static int32_t tDecodeI8(SCoder* pDecoder, int8_t* val); +static int32_t tDecodeU16(SCoder* pDecoder, uint16_t* val); +static int32_t tDecodeI16(SCoder* pDecoder, int16_t* val); +static int32_t tDecodeU32(SCoder* pDecoder, uint32_t* val); +static int32_t tDecodeI32(SCoder* pDecoder, int32_t* val); +static int32_t tDecodeU64(SCoder* pDecoder, uint64_t* val); +static int32_t tDecodeI64(SCoder* pDecoder, int64_t* val); +static int32_t tDecodeU16v(SCoder* pDecoder, uint16_t* val); +static int32_t tDecodeI16v(SCoder* pDecoder, int16_t* val); +static int32_t tDecodeU32v(SCoder* pDecoder, uint32_t* val); +static int32_t tDecodeI32v(SCoder* pDecoder, int32_t* val); +static int32_t tDecodeU64v(SCoder* pDecoder, uint64_t* val); +static int32_t tDecodeI64v(SCoder* pDecoder, int64_t* val); +static int32_t tDecodeFloat(SCoder* pDecoder, float* val); +static int32_t tDecodeDouble(SCoder* pDecoder, double* val); +static int32_t tDecodeBinary(SCoder* pDecoder, const void** val, uint64_t* len); +static int32_t tDecodeCStrAndLen(SCoder* pDecoder, const char** val, uint64_t* len); +static int32_t tDecodeCStr(SCoder* pDecoder, const char** val); +static int32_t tDecodeCStrTo(SCoder* pDecoder, char* val); /* ------------------------ IMPL ------------------------ */ #define TD_ENCODE_MACRO(CODER, VAL, TYPE, BITS) \ @@ -190,7 +190,7 @@ static int tDecodeCStrTo(SCoder* pDecoder, char* val); return 0; // 8 -static FORCE_INLINE int tEncodeU8(SCoder* pEncoder, uint8_t val) { +static FORCE_INLINE int32_t tEncodeU8(SCoder* pEncoder, uint8_t val) { if (pEncoder->data) { if (TD_CODER_CHECK_CAPACITY_FAILED(pEncoder, sizeof(val))) return -1; tPut(uint8_t, TD_CODER_CURRENT(pEncoder), val); @@ -199,7 +199,7 @@ static FORCE_INLINE int tEncodeU8(SCoder* pEncoder, uint8_t val) { return 0; } -static FORCE_INLINE int tEncodeI8(SCoder* pEncoder, int8_t val) { +static FORCE_INLINE int32_t tEncodeI8(SCoder* pEncoder, int8_t val) { if (pEncoder->data) { if (TD_CODER_CHECK_CAPACITY_FAILED(pEncoder, sizeof(val))) return -1; tPut(int8_t, TD_CODER_CURRENT(pEncoder), val); @@ -209,31 +209,31 @@ static FORCE_INLINE int tEncodeI8(SCoder* pEncoder, int8_t val) { } // 16 -static FORCE_INLINE int tEncodeU16(SCoder* pEncoder, uint16_t val) { TD_ENCODE_MACRO(pEncoder, val, uint16_t, 16); } -static FORCE_INLINE int tEncodeI16(SCoder* pEncoder, int16_t val) { TD_ENCODE_MACRO(pEncoder, val, int16_t, 16); } +static FORCE_INLINE int32_t tEncodeU16(SCoder* pEncoder, uint16_t val) { TD_ENCODE_MACRO(pEncoder, val, uint16_t, 16); } +static FORCE_INLINE int32_t tEncodeI16(SCoder* pEncoder, int16_t val) { TD_ENCODE_MACRO(pEncoder, val, int16_t, 16); } // 32 -static FORCE_INLINE int tEncodeU32(SCoder* pEncoder, uint32_t val) { TD_ENCODE_MACRO(pEncoder, val, uint32_t, 32); } -static FORCE_INLINE int tEncodeI32(SCoder* pEncoder, int32_t val) { TD_ENCODE_MACRO(pEncoder, val, int32_t, 32); } +static FORCE_INLINE int32_t tEncodeU32(SCoder* pEncoder, uint32_t val) { TD_ENCODE_MACRO(pEncoder, val, uint32_t, 32); } +static FORCE_INLINE int32_t tEncodeI32(SCoder* pEncoder, int32_t val) { TD_ENCODE_MACRO(pEncoder, val, int32_t, 32); } // 64 -static FORCE_INLINE int tEncodeU64(SCoder* pEncoder, uint64_t val) { TD_ENCODE_MACRO(pEncoder, val, uint64_t, 64); } -static FORCE_INLINE int tEncodeI64(SCoder* pEncoder, int64_t val) { TD_ENCODE_MACRO(pEncoder, val, int64_t, 64); } +static FORCE_INLINE int32_t tEncodeU64(SCoder* pEncoder, uint64_t val) { TD_ENCODE_MACRO(pEncoder, val, uint64_t, 64); } +static FORCE_INLINE int32_t tEncodeI64(SCoder* pEncoder, int64_t val) { TD_ENCODE_MACRO(pEncoder, val, int64_t, 64); } // 16v -static FORCE_INLINE int tEncodeU16v(SCoder* pEncoder, uint16_t val) { TD_ENCODE_VARIANT_MACRO(pEncoder, val); } -static FORCE_INLINE int tEncodeI16v(SCoder* pEncoder, int16_t val) { +static FORCE_INLINE int32_t tEncodeU16v(SCoder* pEncoder, uint16_t val) { TD_ENCODE_VARIANT_MACRO(pEncoder, val); } +static FORCE_INLINE int32_t tEncodeI16v(SCoder* pEncoder, int16_t val) { return tEncodeU16v(pEncoder, ZIGZAGE(int16_t, val)); } // 32v -static FORCE_INLINE int tEncodeU32v(SCoder* pEncoder, uint32_t val) { TD_ENCODE_VARIANT_MACRO(pEncoder, val); } -static FORCE_INLINE int tEncodeI32v(SCoder* pEncoder, int32_t val) { +static FORCE_INLINE int32_t tEncodeU32v(SCoder* pEncoder, uint32_t val) { TD_ENCODE_VARIANT_MACRO(pEncoder, val); } +static FORCE_INLINE int32_t tEncodeI32v(SCoder* pEncoder, int32_t val) { return tEncodeU32v(pEncoder, ZIGZAGE(int32_t, val)); } // 64v -static FORCE_INLINE int tEncodeU64v(SCoder* pEncoder, uint64_t val) { TD_ENCODE_VARIANT_MACRO(pEncoder, val); } -static FORCE_INLINE int tEncodeI64v(SCoder* pEncoder, int64_t val) { +static FORCE_INLINE int32_t tEncodeU64v(SCoder* pEncoder, uint64_t val) { TD_ENCODE_VARIANT_MACRO(pEncoder, val); } +static FORCE_INLINE int32_t tEncodeI64v(SCoder* pEncoder, int64_t val) { return tEncodeU64v(pEncoder, ZIGZAGE(int64_t, val)); } -static FORCE_INLINE int tEncodeFloat(SCoder* pEncoder, float val) { +static FORCE_INLINE int32_t tEncodeFloat(SCoder* pEncoder, float val) { union { uint32_t ui; float f; @@ -242,7 +242,7 @@ static FORCE_INLINE int tEncodeFloat(SCoder* pEncoder, float val) { return tEncodeU32(pEncoder, v.ui); } -static FORCE_INLINE int tEncodeDouble(SCoder* pEncoder, double val) { +static FORCE_INLINE int32_t tEncodeDouble(SCoder* pEncoder, double val) { union { uint64_t ui; double d; @@ -251,7 +251,7 @@ static FORCE_INLINE int tEncodeDouble(SCoder* pEncoder, double val) { return tEncodeU64(pEncoder, v.ui); } -static FORCE_INLINE int tEncodeBinary(SCoder* pEncoder, const void* val, uint64_t len) { +static FORCE_INLINE int32_t tEncodeBinary(SCoder* pEncoder, const void* val, uint64_t len) { if (tEncodeU64v(pEncoder, len) < 0) return -1; if (pEncoder->data) { if (TD_CODER_CHECK_CAPACITY_FAILED(pEncoder, len)) return -1; @@ -262,24 +262,24 @@ static FORCE_INLINE int tEncodeBinary(SCoder* pEncoder, const void* val, uint64_ return 0; } -static FORCE_INLINE int tEncodeCStrWithLen(SCoder* pEncoder, const char* val, uint64_t len) { +static FORCE_INLINE int32_t tEncodeCStrWithLen(SCoder* pEncoder, const char* val, uint64_t len) { return tEncodeBinary(pEncoder, (void*)val, len + 1); } -static FORCE_INLINE int tEncodeCStr(SCoder* pEncoder, const char* val) { +static FORCE_INLINE int32_t tEncodeCStr(SCoder* pEncoder, const char* val) { return tEncodeCStrWithLen(pEncoder, val, (uint64_t)strlen(val)); } /* ------------------------ FOR DECODER ------------------------ */ // 8 -static FORCE_INLINE int tDecodeU8(SCoder* pDecoder, uint8_t* val) { +static FORCE_INLINE int32_t tDecodeU8(SCoder* pDecoder, uint8_t* val) { if (TD_CODER_CHECK_CAPACITY_FAILED(pDecoder, sizeof(*val))) return -1; tGet(uint8_t, TD_CODER_CURRENT(pDecoder), *val); TD_CODER_MOVE_POS(pDecoder, sizeof(*val)); return 0; } -static FORCE_INLINE int tDecodeI8(SCoder* pDecoder, int8_t* val) { +static FORCE_INLINE int32_t tDecodeI8(SCoder* pDecoder, int8_t* val) { if (TD_CODER_CHECK_CAPACITY_FAILED(pDecoder, sizeof(*val))) return -1; tGet(int8_t, TD_CODER_CURRENT(pDecoder), *val); TD_CODER_MOVE_POS(pDecoder, sizeof(*val)); @@ -287,21 +287,27 @@ static FORCE_INLINE int tDecodeI8(SCoder* pDecoder, int8_t* val) { } // 16 -static FORCE_INLINE int tDecodeU16(SCoder* pDecoder, uint16_t* val) { TD_DECODE_MACRO(pDecoder, val, uint16_t, 16); } -static FORCE_INLINE int tDecodeI16(SCoder* pDecoder, int16_t* val) { TD_DECODE_MACRO(pDecoder, val, int16_t, 16); } +static FORCE_INLINE int32_t tDecodeU16(SCoder* pDecoder, uint16_t* val) { + TD_DECODE_MACRO(pDecoder, val, uint16_t, 16); +} +static FORCE_INLINE int32_t tDecodeI16(SCoder* pDecoder, int16_t* val) { TD_DECODE_MACRO(pDecoder, val, int16_t, 16); } // 32 -static FORCE_INLINE int tDecodeU32(SCoder* pDecoder, uint32_t* val) { TD_DECODE_MACRO(pDecoder, val, uint32_t, 32); } -static FORCE_INLINE int tDecodeI32(SCoder* pDecoder, int32_t* val) { TD_DECODE_MACRO(pDecoder, val, int32_t, 32); } +static FORCE_INLINE int32_t tDecodeU32(SCoder* pDecoder, uint32_t* val) { + TD_DECODE_MACRO(pDecoder, val, uint32_t, 32); +} +static FORCE_INLINE int32_t tDecodeI32(SCoder* pDecoder, int32_t* val) { TD_DECODE_MACRO(pDecoder, val, int32_t, 32); } // 64 -static FORCE_INLINE int tDecodeU64(SCoder* pDecoder, uint64_t* val) { TD_DECODE_MACRO(pDecoder, val, uint64_t, 64); } -static FORCE_INLINE int tDecodeI64(SCoder* pDecoder, int64_t* val) { TD_DECODE_MACRO(pDecoder, val, int64_t, 64); } +static FORCE_INLINE int32_t tDecodeU64(SCoder* pDecoder, uint64_t* val) { + TD_DECODE_MACRO(pDecoder, val, uint64_t, 64); +} +static FORCE_INLINE int32_t tDecodeI64(SCoder* pDecoder, int64_t* val) { TD_DECODE_MACRO(pDecoder, val, int64_t, 64); } // 16v -static FORCE_INLINE int tDecodeU16v(SCoder* pDecoder, uint16_t* val) { +static FORCE_INLINE int32_t tDecodeU16v(SCoder* pDecoder, uint16_t* val) { TD_DECODE_VARIANT_MACRO(pDecoder, val, uint16_t); } -static FORCE_INLINE int tDecodeI16v(SCoder* pDecoder, int16_t* val) { +static FORCE_INLINE int32_t tDecodeI16v(SCoder* pDecoder, int16_t* val) { uint16_t tval; if (tDecodeU16v(pDecoder, &tval) < 0) { return -1; @@ -311,11 +317,11 @@ static FORCE_INLINE int tDecodeI16v(SCoder* pDecoder, int16_t* val) { } // 32v -static FORCE_INLINE int tDecodeU32v(SCoder* pDecoder, uint32_t* val) { +static FORCE_INLINE int32_t tDecodeU32v(SCoder* pDecoder, uint32_t* val) { TD_DECODE_VARIANT_MACRO(pDecoder, val, uint32_t); } -static FORCE_INLINE int tDecodeI32v(SCoder* pDecoder, int32_t* val) { +static FORCE_INLINE int32_t tDecodeI32v(SCoder* pDecoder, int32_t* val) { uint32_t tval; if (tDecodeU32v(pDecoder, &tval) < 0) { return -1; @@ -325,11 +331,11 @@ static FORCE_INLINE int tDecodeI32v(SCoder* pDecoder, int32_t* val) { } // 64v -static FORCE_INLINE int tDecodeU64v(SCoder* pDecoder, uint64_t* val) { +static FORCE_INLINE int32_t tDecodeU64v(SCoder* pDecoder, uint64_t* val) { TD_DECODE_VARIANT_MACRO(pDecoder, val, uint64_t); } -static FORCE_INLINE int tDecodeI64v(SCoder* pDecoder, int64_t* val) { +static FORCE_INLINE int32_t tDecodeI64v(SCoder* pDecoder, int64_t* val) { uint64_t tval; if (tDecodeU64v(pDecoder, &tval) < 0) { return -1; @@ -338,7 +344,7 @@ static FORCE_INLINE int tDecodeI64v(SCoder* pDecoder, int64_t* val) { return 0; } -static FORCE_INLINE int tDecodeFloat(SCoder* pDecoder, float* val) { +static FORCE_INLINE int32_t tDecodeFloat(SCoder* pDecoder, float* val) { union { uint32_t ui; float f; @@ -352,7 +358,7 @@ static FORCE_INLINE int tDecodeFloat(SCoder* pDecoder, float* val) { return 0; } -static FORCE_INLINE int tDecodeDouble(SCoder* pDecoder, double* val) { +static FORCE_INLINE int32_t tDecodeDouble(SCoder* pDecoder, double* val) { union { uint64_t ui; double d; @@ -366,7 +372,7 @@ static FORCE_INLINE int tDecodeDouble(SCoder* pDecoder, double* val) { return 0; } -static FORCE_INLINE int tDecodeBinary(SCoder* pDecoder, const void** val, uint64_t* len) { +static FORCE_INLINE int32_t tDecodeBinary(SCoder* pDecoder, const void** val, uint64_t* len) { if (tDecodeU64v(pDecoder, len) < 0) return -1; if (TD_CODER_CHECK_CAPACITY_FAILED(pDecoder, *len)) return -1; @@ -376,18 +382,18 @@ static FORCE_INLINE int tDecodeBinary(SCoder* pDecoder, const void** val, uint64 return 0; } -static FORCE_INLINE int tDecodeCStrAndLen(SCoder* pDecoder, const char** val, uint64_t* len) { +static FORCE_INLINE int32_t tDecodeCStrAndLen(SCoder* pDecoder, const char** val, uint64_t* len) { if (tDecodeBinary(pDecoder, (const void**)val, len) < 0) return -1; (*len) -= 1; return 0; } -static FORCE_INLINE int tDecodeCStr(SCoder* pDecoder, const char** val) { +static FORCE_INLINE int32_t tDecodeCStr(SCoder* pDecoder, const char** val) { uint64_t len; return tDecodeCStrAndLen(pDecoder, val, &len); } -static int tDecodeCStrTo(SCoder* pDecoder, char* val) { +static int32_t tDecodeCStrTo(SCoder* pDecoder, char* val) { const char* pStr; uint64_t len; if (tDecodeCStrAndLen(pDecoder, &pStr, &len) < 0) return -1; diff --git a/include/util/texception.h b/include/util/texception.h new file mode 100644 index 0000000000..576545d96c --- /dev/null +++ b/include/util/texception.h @@ -0,0 +1,129 @@ +/* + * Copyright (c) 2020 TAOS Data, Inc. + * + * 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 . + */ + +#ifndef _TD_UTIL_EXCEPTION_H_ +#define _TD_UTIL_EXCEPTION_H_ + +#include "os.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * cleanup actions + */ +typedef struct SCleanupAction { + bool failOnly; + uint8_t wrapper; + uint16_t reserved; + void* func; + union { + void* Ptr; + bool Bool; + char Char; + int8_t Int8; + uint8_t Uint8; + int16_t Int16; + uint16_t Uint16; + int32_t Int; + uint32_t Uint; + int32_t Int32; + uint32_t Uint32; + int64_t Int64; + uint64_t Uint64; + float Float; + double Double; + } arg1, arg2; +} SCleanupAction; + +/* + * exception hander registration + */ +typedef struct SExceptionNode { + struct SExceptionNode* prev; + jmp_buf jb; + int32_t code; + int32_t maxCleanupAction; + int32_t numCleanupAction; + SCleanupAction* cleanupActions; +} SExceptionNode; + +// functions & macros for auto-cleanup + +void cleanupPush_void_ptr_ptr(bool failOnly, void* func, void* arg1, void* arg2); +void cleanupPush_void_ptr_bool(bool failOnly, void* func, void* arg1, bool arg2); +void cleanupPush_void_ptr(bool failOnly, void* func, void* arg); +void cleanupPush_int_int(bool failOnly, void* func, int32_t arg); +void cleanupPush_void(bool failOnly, void* func); +void cleanupPush_int_ptr(bool failOnly, void* func, void* arg); + +int32_t cleanupGetActionCount(); +void cleanupExecuteTo(int32_t anchor, bool failed); +void cleanupExecute(SExceptionNode* node, bool failed); +bool cleanupExceedLimit(); + +#define CLEANUP_PUSH_VOID_PTR_PTR(failOnly, func, arg1, arg2) \ + cleanupPush_void_ptr_ptr((failOnly), (void*)(func), (void*)(arg1), (void*)(arg2)) +#define CLEANUP_PUSH_VOID_PTR_BOOL(failOnly, func, arg1, arg2) \ + cleanupPush_void_ptr_bool((failOnly), (void*)(func), (void*)(arg1), (bool)(arg2)) +#define CLEANUP_PUSH_VOID_PTR(failOnly, func, arg) cleanupPush_void_ptr((failOnly), (void*)(func), (void*)(arg)) +#define CLEANUP_PUSH_INT_INT(failOnly, func, arg) cleanupPush_void_ptr((failOnly), (void*)(func), (int32_t)(arg)) +#define CLEANUP_PUSH_VOID(failOnly, func) cleanupPush_void((failOnly), (void*)(func)) +#define CLEANUP_PUSH_INT_PTR(failOnly, func, arg) cleanupPush_int_ptr((failOnly), (void*)(func), (void*)(arg)) +#define CLEANUP_PUSH_FREE(failOnly, arg) cleanupPush_void_ptr((failOnly), free, (void*)(arg)) +#define CLEANUP_PUSH_CLOSE(failOnly, arg) cleanupPush_int_int((failOnly), close, (int32_t)(arg)) +#define CLEANUP_PUSH_FCLOSE(failOnly, arg) cleanupPush_int_ptr((failOnly), fclose, (void*)(arg)) + +#define CLEANUP_GET_ANCHOR() cleanupGetActionCount() +#define CLEANUP_EXECUTE_TO(anchor, failed) cleanupExecuteTo((anchor), (failed)) +#define CLEANUP_EXCEED_LIMIT() cleanupExceedLimit() + +// functions & macros for exception handling + +void exceptionPushNode(SExceptionNode* node); +int32_t exceptionPopNode(); +void exceptionThrow(int32_t code); + +#define TRY(maxCleanupActions) \ + do { \ + SExceptionNode exceptionNode = {0}; \ + SCleanupAction cleanupActions[(maxCleanupActions) > 0 ? (maxCleanupActions) : 1]; \ + exceptionNode.maxCleanupAction = (maxCleanupActions) > 0 ? (maxCleanupActions) : 1; \ + exceptionNode.cleanupActions = cleanupActions; \ + exceptionPushNode(&exceptionNode); \ + int32_t caughtException = setjmp(exceptionNode.jb); \ + if (caughtException == 0) + +#define CATCH(code) \ + int32_t code = exceptionPopNode(); \ + if (caughtException == 1) + +#define FINALLY(code) int32_t code = exceptionPopNode(); + +#define END_TRY \ + } \ + while (0) \ + ; + +#define THROW(x) exceptionThrow((x)) +#define CAUGHT_EXCEPTION() ((bool)(caughtException == 1)) +#define CLEANUP_EXECUTE() cleanupExecute(&exceptionNode, CAUGHT_EXCEPTION()) + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_UTIL_EXCEPTION_H_*/ diff --git a/include/util/tfile.h b/include/util/tfile.h deleted file mode 100644 index 59953de861..0000000000 --- a/include/util/tfile.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef _TD_UTIL_FILE_H -#define _TD_UTIL_FILE_H - -#include "os.h" - -#ifdef __cplusplus -extern "C" { -#endif - -// init taos file module -int32_t tfInit(); - -// clean up taos file module -void tfCleanup(); - -// the same syntax as UNIX standard open/close/read/write -// but FD is int64_t and will never be reused -// int64_t tfOpenRead(const char *pathname); -// int64_t tfOpenReadWrite(const char *pathname); -// int64_t tfOpenCreateWrite(const char *pathname); -// int64_t tfOpenCreateWriteAppend(const char *pathname); - -// int64_t tfClose(int64_t tfd); -// int64_t tfWrite(int64_t tfd, void *buf, int64_t count); -// int64_t tfRead(int64_t tfd, void *buf, int64_t count); -// int64_t tfPread(int64_t tfd, void *buf, int64_t count, int64_t offset); -// int32_t tfFsync(int64_t tfd); -// bool tfValid(int64_t tfd); -// int64_t tfLseek(int64_t tfd, int64_t offset, int32_t whence); -// int32_t tfFtruncate(int64_t tfd, int64_t length); -// void * tfMmapReadOnly(int64_t tfd, int64_t length); -#ifdef __cplusplus -} -#endif - -#endif /*_TD_UTIL_FILE_H*/ diff --git a/include/util/freelist.h b/include/util/tfreelist.h similarity index 99% rename from include/util/freelist.h rename to include/util/tfreelist.h index 497a6d58c3..c1913ebaf9 100644 --- a/include/util/freelist.h +++ b/include/util/tfreelist.h @@ -16,7 +16,6 @@ #ifndef _TD_UTIL_FREELIST_H_ #define _TD_UTIL_FREELIST_H_ -#include "os.h" #include "tlist.h" #ifdef __cplusplus diff --git a/include/util/tfunctional.h b/include/util/tfunctional.h index c96f997f06..43e3cd5e48 100644 --- a/include/util/tfunctional.h +++ b/include/util/tfunctional.h @@ -12,45 +12,45 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#ifndef _TD_UTIL_FUNCTIONAL_H -#define _TD_UTIL_FUNCTIONAL_H +#ifndef _TD_UTIL_FUNCTIONAL_H_ +#define _TD_UTIL_FUNCTIONAL_H_ + +#include "os.h" #ifdef __cplusplus extern "C" { #endif -#include "os.h" - -//TODO: hard to use, trying to rewrite it using va_list +// TODO: hard to use, trying to rewrite it using va_list typedef void* (*GenericVaFunc)(void* args[]); -typedef int32_t (*I32VaFunc) (void* args[]); -typedef void (*VoidVaFunc) (void* args[]); +typedef int32_t (*I32VaFunc)(void* args[]); +typedef void (*VoidVaFunc)(void* args[]); typedef struct GenericSavedFunc { GenericVaFunc func; - void * args[]; + void* args[]; } tGenericSavedFunc; typedef struct I32SavedFunc { I32VaFunc func; - void * args[]; + void* args[]; } tI32SavedFunc; typedef struct VoidSavedFunc { VoidVaFunc func; - void * args[]; + void* args[]; } tVoidSavedFunc; -tGenericSavedFunc* genericSavedFuncInit(GenericVaFunc func, int numOfArgs); -tI32SavedFunc* i32SavedFuncInit(I32VaFunc func, int numOfArgs); -tVoidSavedFunc* voidSavedFuncInit(VoidVaFunc func, int numOfArgs); -void* genericInvoke(tGenericSavedFunc* const pSavedFunc); -int32_t i32Invoke(tI32SavedFunc* const pSavedFunc); -void voidInvoke(tVoidSavedFunc* const pSavedFunc); +tGenericSavedFunc* genericSavedFuncInit(GenericVaFunc func, int32_t numOfArgs); +tI32SavedFunc* i32SavedFuncInit(I32VaFunc func, int32_t numOfArgs); +tVoidSavedFunc* voidSavedFuncInit(VoidVaFunc func, int32_t numOfArgs); +void* genericInvoke(tGenericSavedFunc* const pSavedFunc); +int32_t i32Invoke(tI32SavedFunc* const pSavedFunc); +void voidInvoke(tVoidSavedFunc* const pSavedFunc); #ifdef __cplusplus } #endif -#endif /*_TD_UTIL_FUNCTIONAL_H*/ +#endif /*_TD_UTIL_FUNCTIONAL_H_*/ diff --git a/include/util/thash.h b/include/util/thash.h index 1f72045a83..5c344f3f0f 100644 --- a/include/util/thash.h +++ b/include/util/thash.h @@ -13,22 +13,22 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_HASH_H -#define TDENGINE_HASH_H +#ifndef _TD_UTIL_HASH_H_ +#define _TD_UTIL_HASH_H_ + +#include "tarray.h" +#include "tlockfree.h" #ifdef __cplusplus extern "C" { #endif -#include "tarray.h" -#include "tlockfree.h" - typedef uint32_t (*_hash_fn_t)(const char *, uint32_t); -typedef int32_t (*_equal_fn_t)(const void*, const void*, size_t len); +typedef int32_t (*_equal_fn_t)(const void *, const void *, size_t len); typedef void (*_hash_before_fn_t)(void *); typedef void (*_hash_free_fn_t)(void *); -#define HASH_MAX_CAPACITY (1024 * 1024 * 16) +#define HASH_MAX_CAPACITY (1024 * 1024 * 16) #define HASH_DEFAULT_LOAD_FACTOR (0.75) #define HASH_INDEX(v, c) ((v) & ((c)-1)) @@ -59,43 +59,43 @@ _equal_fn_t taosGetDefaultEqualFunction(int32_t type); typedef struct SHashNode { struct SHashNode *next; - uint32_t hashVal; // the hash value of key - uint32_t dataLen; // length of data - uint32_t keyLen; // length of the key - uint16_t count; // reference count - int8_t removed; // flag to indicate removed + uint32_t hashVal; // the hash value of key + uint32_t dataLen; // length of data + uint32_t keyLen; // length of the key + uint16_t count; // reference count + int8_t removed; // flag to indicate removed char data[]; } SHashNode; -#define GET_HASH_NODE_KEY(_n) ((char*)(_n) + sizeof(SHashNode) + (_n)->dataLen) -#define GET_HASH_NODE_DATA(_n) ((char*)(_n) + sizeof(SHashNode)) -#define GET_HASH_PNODE(_n) ((SHashNode *)((char*)(_n) - sizeof(SHashNode))) +#define GET_HASH_NODE_KEY(_n) ((char *)(_n) + sizeof(SHashNode) + (_n)->dataLen) +#define GET_HASH_NODE_DATA(_n) ((char *)(_n) + sizeof(SHashNode)) +#define GET_HASH_PNODE(_n) ((SHashNode *)((char *)(_n) - sizeof(SHashNode))) typedef enum SHashLockTypeE { - HASH_NO_LOCK = 0, - HASH_ENTRY_LOCK = 1, + HASH_NO_LOCK = 0, + HASH_ENTRY_LOCK = 1, } SHashLockTypeE; typedef struct SHashEntry { - int32_t num; // number of elements in current entry - SRWLatch latch; // entry latch + int32_t num; // number of elements in current entry + SRWLatch latch; // entry latch SHashNode *next; } SHashEntry; typedef struct SHashObj { - SHashEntry **hashList; - uint32_t capacity; // number of slots - uint32_t size; // number of elements in hash table + SHashEntry **hashList; + uint32_t capacity; // number of slots + uint32_t size; // number of elements in hash table - _hash_fn_t hashFp; // hash function - _hash_free_fn_t freeFp; // hash node free callback function - _equal_fn_t equalFp; // equal function - _hash_before_fn_t callbackFp; // function invoked before return the value to caller + _hash_fn_t hashFp; // hash function + _hash_free_fn_t freeFp; // hash node free callback function + _equal_fn_t equalFp; // equal function + _hash_before_fn_t callbackFp; // function invoked before return the value to caller - SRWLatch lock; // read-write spin lock - SHashLockTypeE type; // lock type - bool enableUpdate; // enable update - SArray *pMemBlock; // memory block allocated for SHashEntry + SRWLatch lock; // read-write spin lock + SHashLockTypeE type; // lock type + bool enableUpdate; // enable update + SArray *pMemBlock; // memory block allocated for SHashEntry } SHashObj; /** @@ -128,7 +128,6 @@ int32_t taosHashPut(SHashObj *pHashObj, const void *key, size_t keyLen, void *da int32_t taosHashPutExt(SHashObj *pHashObj, const void *key, size_t keyLen, void *data, size_t size, bool *newAdded); - /** * return the payload data with the specified key * @@ -147,7 +146,7 @@ void *taosHashGet(SHashObj *pHashObj, const void *key, size_t keyLen); * @param destBuf * @return */ -void *taosHashGetClone(SHashObj *pHashObj, const void *key, size_t keyLen, void* destBuf); +void *taosHashGetClone(SHashObj *pHashObj, const void *key, size_t keyLen, void *destBuf); /** * Clone the result to interval allocated buffer @@ -157,7 +156,7 @@ void *taosHashGetClone(SHashObj *pHashObj, const void *key, size_t keyLen, void* * @param destBuf * @return */ -void* taosHashGetCloneExt(SHashObj *pHashObj, const void *key, size_t keyLen, void (*fp)(void *), void** d, size_t *sz); +void *taosHashGetCloneExt(SHashObj *pHashObj, const void *key, size_t keyLen, void (*fp)(void *), void **d, size_t *sz); /** * remove item with the specified key @@ -206,15 +205,14 @@ void *taosHashIterate(SHashObj *pHashObj, void *p); * @param pHashObj * @param p */ -void taosHashCancelIterate(SHashObj *pHashObj, void *p); +void taosHashCancelIterate(SHashObj *pHashObj, void *p); /** * Get the corresponding key information for a given data in hash table * @param data * @return */ -int32_t taosHashGetKey(void *data, void** key, size_t* keyLen); - +int32_t taosHashGetKey(void *data, void **key, size_t *keyLen); /** * Get the corresponding key information for a given data in hash table, using memcpy @@ -222,13 +220,13 @@ int32_t taosHashGetKey(void *data, void** key, size_t* keyLen); * @param dst * @return */ -static FORCE_INLINE int32_t taosHashCopyKey(void *data, void* dst) { +static FORCE_INLINE int32_t taosHashCopyKey(void *data, void *dst) { if (NULL == data || NULL == dst) { return -1; } - - SHashNode * node = GET_HASH_PNODE(data); - void* key = GET_HASH_NODE_KEY(node); + + SHashNode *node = GET_HASH_PNODE(data); + void *key = GET_HASH_NODE_KEY(node); memcpy(dst, key, node->keyLen); return 0; @@ -249,7 +247,7 @@ int32_t taosHashGetDataLen(void *data); * @param keyLen * @return */ -void* taosHashAcquire(SHashObj *pHashObj, const void *key, size_t keyLen); +void *taosHashAcquire(SHashObj *pHashObj, const void *key, size_t keyLen); /** * release the prevous acquired obj @@ -262,9 +260,8 @@ void taosHashRelease(SHashObj *pHashObj, void *p); void taosHashSetEqualFp(SHashObj *pHashObj, _equal_fn_t fp); - #ifdef __cplusplus } #endif -#endif // TDENGINE_HASH_H +#endif // _TD_UTIL_HASH_H_ diff --git a/include/util/theap.h b/include/util/theap.h index fd1a39f8dd..fb5ff8301a 100644 --- a/include/util/theap.h +++ b/include/util/theap.h @@ -12,19 +12,20 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#ifndef TDENGINE_HEAP_H -#define TDENGINE_HEAP_H + +#ifndef _TD_UTIL_HEAP_H_ +#define _TD_UTIL_HEAP_H_ + +#include "os.h" #ifdef __cplusplus extern "C" { #endif -#include "os.h" - struct HeapNode; /* Return non-zero if a < b. */ -typedef int (*HeapCompareFn)(const struct HeapNode* a, const struct HeapNode* b); +typedef int32_t (*HeapCompareFn)(const struct HeapNode* a, const struct HeapNode* b); typedef struct HeapNode { struct HeapNode* left; @@ -38,15 +39,14 @@ typedef struct HeapNode { * */ typedef struct { - HeapNode* min; - size_t nelts; - HeapCompareFn compFn; + HeapNode* min; + size_t nelts; + HeapCompareFn compFn; } Heap; - Heap* heapCreate(HeapCompareFn fn); -void heapDestroy(Heap *heap); +void heapDestroy(Heap* heap); HeapNode* heapMin(const Heap* heap); @@ -56,10 +56,10 @@ void heapRemove(Heap* heap, struct HeapNode* node); void heapDequeue(Heap* heap); -size_t heapSize(Heap *heap); +size_t heapSize(Heap* heap); #ifdef __cplusplus } #endif -#endif // TDENGINE_HASH_H +#endif /*_TD_UTIL_HEAP_H_*/ diff --git a/include/util/tidpool.h b/include/util/tidpool.h index 1a977fd04c..8596b439e3 100644 --- a/include/util/tidpool.h +++ b/include/util/tidpool.h @@ -13,31 +13,34 @@ * along with this program. If not, see . */ -#ifndef _TD_UTIL_IDPOOL_H -#define _TD_UTIL_IDPOOL_H +#ifndef _TD_UTIL_IDPOOL_H_ +#define _TD_UTIL_IDPOOL_H_ + +#include "os.h" #ifdef __cplusplus extern "C" { #endif -void *taosInitIdPool(int maxId); +typedef struct { + int32_t maxId; + int32_t numOfFree; + int32_t freeSlot; + bool *freeList; + pthread_mutex_t mutex; +} id_pool_t; -int taosUpdateIdPool(void *handle, int maxId); - -int taosIdPoolMaxSize(void *handle); - -int taosAllocateId(void *handle); - -void taosFreeId(void *handle, int id); - -void taosIdPoolCleanUp(void *handle); - -int taosIdPoolNumOfUsed(void *handle); - -bool taosIdPoolMarkStatus(void *handle, int id); +void *taosInitIdPool(int32_t maxId); +int32_t taosUpdateIdPool(id_pool_t *handle, int32_t maxId); +int32_t taosIdPoolMaxSize(id_pool_t *handle); +int32_t taosAllocateId(id_pool_t *handle); +void taosFreeId(id_pool_t *handle, int32_t id); +void taosIdPoolCleanUp(id_pool_t *handle); +int32_t taosIdPoolNumOfUsed(id_pool_t *handle); +bool taosIdPoolMarkStatus(id_pool_t *handle, int32_t id); #ifdef __cplusplus } #endif -#endif /*_TD_UTIL_IDPOOL_H*/ +#endif /*_TD_UTIL_IDPOOL_H_*/ diff --git a/include/util/tjson.h b/include/util/tjson.h index e42e40efa7..2d9f433ab2 100644 --- a/include/util/tjson.h +++ b/include/util/tjson.h @@ -16,18 +16,18 @@ #ifndef _TD_UTIL_JSON_H_ #define _TD_UTIL_JSON_H_ +#include "os.h" + #ifdef __cplusplus extern "C" { #endif -#include "os.h" - typedef void SJson; SJson* tjsonCreateObject(); -void tjsonDelete(SJson* pJson); +void tjsonDelete(SJson* pJson); -SJson* tjsonAddArrayToObject(SJson* pJson, const char* pName); +SJson* tjsonAddArrayToObject(SJson* pJson, const char* pName); int32_t tjsonAddIntegerToObject(SJson* pJson, const char* pName, const uint64_t number); int32_t tjsonAddDoubleToObject(SJson* pJson, const char* pName, const double number); int32_t tjsonAddBoolToObject(SJson* pJson, const char* pName, const bool boolean); @@ -35,7 +35,7 @@ int32_t tjsonAddStringToObject(SJson* pJson, const char* pName, const char* pVal int32_t tjsonAddItemToObject(SJson* pJson, const char* pName, SJson* pItem); int32_t tjsonAddItemToArray(SJson* pJson, SJson* pItem); -SJson* tjsonGetObjectItem(const SJson* pJson, const char* pName); +SJson* tjsonGetObjectItem(const SJson* pJson, const char* pName); int32_t tjsonGetStringValue(const SJson* pJson, const char* pName, char* pVal); int32_t tjsonDupStringValue(const SJson* pJson, const char* pName, char** pVal); int32_t tjsonGetBigIntValue(const SJson* pJson, const char* pName, int64_t* pVal); @@ -48,7 +48,7 @@ int32_t tjsonGetBoolValue(const SJson* pJson, const char* pName, bool* pVal); int32_t tjsonGetDoubleValue(const SJson* pJson, const char* pName, double* pVal); int32_t tjsonGetArraySize(const SJson* pJson); -SJson* tjsonGetArrayItem(const SJson* pJson, int32_t index); +SJson* tjsonGetArrayItem(const SJson* pJson, int32_t index); typedef int32_t (*FToJson)(const void* pObj, SJson* pJson); diff --git a/include/util/tkey.h b/include/util/tkey.h deleted file mode 100644 index 197cfff958..0000000000 --- a/include/util/tkey.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ -#ifndef _TD_UTIL_KEY_H -#define _TD_UTIL_KEY_H - -#ifdef __cplusplus -extern "C" { -#endif - -unsigned char *base64_decode(const char *value, int inlen, int *outlen); -char * base64_encode(const unsigned char *value, int vlen); -char * taosDesEncode(int64_t key, char *src, int len); -char * taosDesDecode(int64_t key, char *src, int len); - -#ifdef __cplusplus -} -#endif - -#endif /*_TD_UTIL_KEY_H*/ \ No newline at end of file diff --git a/include/util/tlist.h b/include/util/tlist.h index 134873a993..caa6424918 100644 --- a/include/util/tlist.h +++ b/include/util/tlist.h @@ -12,8 +12,11 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#ifndef _TD_UTIL_LIST_H -#define _TD_UTIL_LIST_H + +#ifndef _TD_UTIL_LIST_H_ +#define _TD_UTIL_LIST_H_ + +#include "os.h" #ifdef __cplusplus extern "C" { @@ -28,12 +31,12 @@ extern "C" { #define TD_SLIST(TYPE) \ struct { \ struct TYPE *sl_head_; \ - int sl_neles_; \ + int32_t sl_neles_; \ } -#define TD_SLIST_HEAD(sl) ((sl)->sl_head_) -#define TD_SLIST_NELES(sl) ((sl)->sl_neles_) -#define TD_SLIST_NODE_NEXT(sln) ((sln)->sl_next_) +#define TD_SLIST_HEAD(sl) ((sl)->sl_head_) +#define TD_SLIST_NELES(sl) ((sl)->sl_neles_) +#define TD_SLIST_NODE_NEXT(sln) ((sln)->sl_next_) #define TD_SLIST_NODE_NEXT_WITH_FIELD(sln, field) ((sln)->field.sl_next_) #define TD_SLIST_INIT(sl) \ @@ -79,16 +82,16 @@ extern "C" { struct { \ struct TYPE *dl_head_; \ struct TYPE *dl_tail_; \ - int dl_neles_; \ + int32_t dl_neles_; \ } -#define TD_DLIST_NODE_PREV(dln) ((dln)->dl_prev_) -#define TD_DLIST_NODE_NEXT(dln) ((dln)->dl_next_) +#define TD_DLIST_NODE_PREV(dln) ((dln)->dl_prev_) +#define TD_DLIST_NODE_NEXT(dln) ((dln)->dl_next_) #define TD_DLIST_NODE_PREV_WITH_FIELD(dln, field) ((dln)->field.dl_prev_) #define TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field) ((dln)->field.dl_next_) -#define TD_DLIST_HEAD(dl) ((dl)->dl_head_) -#define TD_DLIST_TAIL(dl) ((dl)->dl_tail_) -#define TD_DLIST_NELES(dl) ((dl)->dl_neles_) +#define TD_DLIST_HEAD(dl) ((dl)->dl_head_) +#define TD_DLIST_TAIL(dl) ((dl)->dl_tail_) +#define TD_DLIST_NELES(dl) ((dl)->dl_neles_) #define TD_DLIST_INIT(dl) \ do { \ @@ -200,29 +203,29 @@ typedef struct SListNode { typedef struct { TD_DLIST(SListNode); - int eleSize; + int32_t eleSize; } SList; typedef struct { - SListNode * next; + SListNode *next; TD_LIST_DIRECTION_T direction; } SListIter; -#define listHead(l) TD_DLIST_HEAD(l) -#define listTail(l) TD_DLIST_TAIL(l) -#define listNEles(l) TD_DLIST_NELES(l) -#define listEleSize(l) ((l)->eleSize) -#define isListEmpty(l) (TD_DLIST_NELES(l) == 0) +#define listHead(l) TD_DLIST_HEAD(l) +#define listTail(l) TD_DLIST_TAIL(l) +#define listNEles(l) TD_DLIST_NELES(l) +#define listEleSize(l) ((l)->eleSize) +#define isListEmpty(l) (TD_DLIST_NELES(l) == 0) #define listNodeFree(n) free(n) -void tdListInit(SList *list, int eleSize); +void tdListInit(SList *list, int32_t eleSize); void tdListEmpty(SList *list); -SList * tdListNew(int eleSize); -void * tdListFree(SList *list); +SList *tdListNew(int32_t eleSize); +void *tdListFree(SList *list); void tdListPrependNode(SList *list, SListNode *node); void tdListAppendNode(SList *list, SListNode *node); -int tdListPrepend(SList *list, void *data); -int tdListAppend(SList *list, void *data); +int32_t tdListPrepend(SList *list, void *data); +int32_t tdListAppend(SList *list, void *data); SListNode *tdListPopHead(SList *list); SListNode *tdListPopTail(SList *list); SListNode *tdListGetHead(SList *list); @@ -239,4 +242,4 @@ SListNode *tdListNext(SListIter *pIter); } #endif -#endif /*_TD_UTIL_LIST_H*/ \ No newline at end of file +#endif /*_TD_UTIL_LIST_H_*/ \ No newline at end of file diff --git a/include/util/tlockfree.h b/include/util/tlockfree.h index fec346cd02..e49e89c309 100644 --- a/include/util/tlockfree.h +++ b/include/util/tlockfree.h @@ -12,8 +12,9 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#ifndef _TD_UTIL_LOCK_FREE_H -#define _TD_UTIL_LOCK_FREE_H + +#ifndef _TD_UTIL_LOCK_FREE_H_ +#define _TD_UTIL_LOCK_FREE_H_ #include "os.h" @@ -22,7 +23,7 @@ extern "C" { #endif // reference counting -typedef void (*_ref_fn_t)(const void* pObj); +typedef void (*_ref_fn_t)(const void *pObj); #define T_REF_DECLARE() \ struct { \ @@ -67,8 +68,6 @@ typedef void (*_ref_fn_t)(const void* pObj); #define T_REF_VAL_GET(x) (x)->_ref.val - - // single writer multiple reader lock typedef volatile int32_t SRWLatch; @@ -78,35 +77,33 @@ void taosWUnLockLatch(SRWLatch *pLatch); void taosRLockLatch(SRWLatch *pLatch); void taosRUnLockLatch(SRWLatch *pLatch); - - // copy on read -#define taosCorBeginRead(x) for (uint32_t i_ = 1; 1; ++i_) { \ +#define taosCorBeginRead(x) \ + for (uint32_t i_ = 1; 1; ++i_) { \ int32_t old_ = atomic_add_fetch_32((x), 0); \ - if (old_ & 0x00000001) { \ - if (i_ % 1000 == 0) { \ - sched_yield(); \ - } \ - continue; \ + if (old_ & 0x00000001) { \ + if (i_ % 1000 == 0) { \ + sched_yield(); \ + } \ + continue; \ } -#define taosCorEndRead(x) \ - if (atomic_add_fetch_32((x), 0) == old_) { \ - break; \ - } \ +#define taosCorEndRead(x) \ + if (atomic_add_fetch_32((x), 0) == old_) { \ + break; \ + } \ } -#define taosCorBeginWrite(x) taosCorBeginRead(x) \ - if (atomic_val_compare_exchange_32((x), old_, old_ + 1) != old_) { \ - continue; \ - } +#define taosCorBeginWrite(x) \ + taosCorBeginRead(x) if (atomic_val_compare_exchange_32((x), old_, old_ + 1) != old_) { continue; } -#define taosCorEndWrite(x) atomic_add_fetch_32((x), 1); \ - break; \ +#define taosCorEndWrite(x) \ + atomic_add_fetch_32((x), 1); \ + break; \ } #ifdef __cplusplus } #endif -#endif /*_TD_UTIL_LOCK_FREE_H*/ +#endif /*_TD_UTIL_LOCK_FREE_H_*/ diff --git a/include/util/tlog.h b/include/util/tlog.h index 1c14cc445f..6e6795e9a2 100644 --- a/include/util/tlog.h +++ b/include/util/tlog.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef _TD_UTIL_LOG_H -#define _TD_UTIL_LOG_H +#ifndef _TD_UTIL_LOG_H_ +#define _TD_UTIL_LOG_H_ #include "os.h" @@ -84,4 +84,4 @@ extern int8_t tscEmbeddedInUtil; } #endif -#endif /*_TD_UTIL_LOG_H*/ +#endif /*_TD_UTIL_LOG_H_*/ diff --git a/include/util/tlosertree.h b/include/util/tlosertree.h index 241647ba1e..51906443f5 100644 --- a/include/util/tlosertree.h +++ b/include/util/tlosertree.h @@ -13,14 +13,16 @@ * along with this program. If not, see . */ -#ifndef _TD_UTIL_LOSERTREE_H -#define _TD_UTIL_LOSERTREE_H +#ifndef _TD_UTIL_LOSERTREE_H_ +#define _TD_UTIL_LOSERTREE_H_ + +#include "os.h" #ifdef __cplusplus extern "C" { #endif -typedef int (*__merge_compare_fn_t)(const void *, const void *, void *param); +typedef int32_t (*__merge_compare_fn_t)(const void *, const void *, void *param); typedef struct STreeNode { int32_t index; @@ -31,16 +33,17 @@ typedef struct SMultiwayMergeTreeInfo { int32_t numOfSources; int32_t totalSources; __merge_compare_fn_t comparFn; - void * param; + void *param; struct STreeNode *pNode; } SMultiwayMergeTreeInfo; #define tMergeTreeGetChosenIndex(t_) ((t_)->pNode[0].index) #define tMergeTreeGetAdjustIndex(t_) (tMergeTreeGetChosenIndex(t_) + (t_)->numOfSources) -int32_t tMergeTreeCreate(SMultiwayMergeTreeInfo **pTree, uint32_t numOfEntries, void *param, __merge_compare_fn_t compareFn); +int32_t tMergeTreeCreate(SMultiwayMergeTreeInfo **pTree, uint32_t numOfEntries, void *param, + __merge_compare_fn_t compareFn); -void tMergeTreeDestroy(SMultiwayMergeTreeInfo* pTree); +void tMergeTreeDestroy(SMultiwayMergeTreeInfo *pTree); void tMergeTreeAdjust(SMultiwayMergeTreeInfo *pTree, int32_t idx); @@ -52,4 +55,4 @@ void tMergeTreePrint(const SMultiwayMergeTreeInfo *pTree); } #endif -#endif /*_TD_UTIL_LOSERTREE_H*/ +#endif /*_TD_UTIL_LOSERTREE_H_*/ diff --git a/include/util/tmacro.h b/include/util/tmacro.h index 297c37d62a..07c6e6509e 100644 --- a/include/util/tmacro.h +++ b/include/util/tmacro.h @@ -24,7 +24,7 @@ extern "C" { // Module init/clear MACRO definitions #define TD_MOD_UNINITIALIZED 0 -#define TD_MOD_INITIALIZED 1 +#define TD_MOD_INITIALIZED 1 typedef int8_t td_mode_flag_t; diff --git a/include/util/mallocator.h b/include/util/tmallocator.h similarity index 85% rename from include/util/mallocator.h rename to include/util/tmallocator.h index 5ecdc316a4..e9eb3e1b72 100644 --- a/include/util/mallocator.h +++ b/include/util/tmallocator.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef _TD_MALLOCATOR_H_ -#define _TD_MALLOCATOR_H_ +#ifndef _TD_UTIL_MALLOCATOR_H_ +#define _TD_UTIL_MALLOCATOR_H_ #include "os.h" @@ -29,10 +29,10 @@ extern "C" { void (*free_)(struct TYPE *, void *ptr); \ } #define TD_MA_MALLOC_FUNC(TMA) (TMA)->malloc_ -#define TD_MA_FREE_FUNC(TMA) (TMA)->free_ +#define TD_MA_FREE_FUNC(TMA) (TMA)->free_ #define TD_MA_MALLOC(TMA, SIZE) (*((TMA)->malloc_))(TMA, (SIZE)) -#define TD_MA_FREE(TMA, PTR) (*((TMA)->free_))(TMA, (PTR)) +#define TD_MA_FREE(TMA, PTR) (*((TMA)->free_))(TMA, (PTR)) typedef struct SMemAllocator { void *impl; @@ -40,7 +40,7 @@ typedef struct SMemAllocator { } SMemAllocator; #define tMalloc(pMA, SIZE) TD_MA_MALLOC(PMA, SIZE) -#define tFree(pMA, PTR) TD_MA_FREE(PMA, PTR) +#define tFree(pMA, PTR) TD_MA_FREE(PMA, PTR) typedef struct SMemAllocatorFactory { void *impl; @@ -52,4 +52,4 @@ typedef struct SMemAllocatorFactory { } #endif -#endif /*_TD_MALLOCATOR_H_*/ \ No newline at end of file +#endif /*_TD_UTIL_MALLOCATOR_H_*/ \ No newline at end of file diff --git a/include/util/tmd5.h b/include/util/tmd5.h index f8114ad57b..6f88d95e24 100644 --- a/include/util/tmd5.h +++ b/include/util/tmd5.h @@ -25,6 +25,12 @@ #ifndef _TD_UTIL_MD5_H #define _TD_UTIL_MD5_H +#include "os.h" + +#ifdef __cplusplus +extern "C" { +#endif + typedef struct { uint32_t i[2]; /* number of _bits_ handled mod 2^64 */ uint32_t buf[4]; /* scratch buffer */ @@ -33,7 +39,11 @@ typedef struct { } T_MD5_CTX; void tMD5Init(T_MD5_CTX *mdContext); -void tMD5Update(T_MD5_CTX *mdContext, uint8_t *inBuf, unsigned int inLen); +void tMD5Update(T_MD5_CTX *mdContext, uint8_t *inBuf, uint32_t inLen); void tMD5Final(T_MD5_CTX *mdContext); +#ifdef __cplusplus +} +#endif + #endif /*_TD_UTIL_MD5_H*/ diff --git a/include/util/tmempool.h b/include/util/tmempool.h index 3e3db738a9..7a5aca7b34 100644 --- a/include/util/tmempool.h +++ b/include/util/tmempool.h @@ -12,25 +12,24 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#ifndef _TD_UTIL_MEMPOOL_H -#define _TD_UTIL_MEMPOOL_H +#ifndef _TD_UTIL_MEMPOOL_H_ +#define _TD_UTIL_MEMPOOL_H_ + +#include "os.h" #ifdef __cplusplus extern "C" { #endif -#define mpool_h void * +typedef void *mpool_h; -mpool_h taosMemPoolInit(int maxNum, int blockSize); - -char *taosMemPoolMalloc(mpool_h handle); - -void taosMemPoolFree(mpool_h handle, char *p); - -void taosMemPoolCleanUp(mpool_h handle); +mpool_h taosMemPoolInit(int32_t maxNum, int32_t blockSize); +char *taosMemPoolMalloc(mpool_h handle); +void taosMemPoolFree(mpool_h handle, char *p); +void taosMemPoolCleanUp(mpool_h handle); #ifdef __cplusplus } #endif -#endif /*_TD_UTIL_MEMPOOL_H*/ +#endif /*_TD_UTIL_MEMPOOL_H_*/ diff --git a/include/util/tpagedbuf.h b/include/util/tpagedbuf.h index 76501b51ad..ce9a57c2c3 100644 --- a/include/util/tpagedbuf.h +++ b/include/util/tpagedbuf.h @@ -13,23 +13,22 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_TPAGEDBUF_H -#define TDENGINE_TPAGEDBUF_H +#ifndef _TD_UTIL_PAGEDBUF_H_ +#define _TD_UTIL_PAGEDBUF_H_ + +#include "thash.h" +#include "tlist.h" +#include "tlockfree.h" #ifdef __cplusplus extern "C" { #endif -#include "tlist.h" -#include "thash.h" -#include "os.h" -#include "tlockfree.h" - -typedef struct SArray* SIDList; -typedef struct SPageInfo SPageInfo; +typedef struct SArray* SIDList; +typedef struct SPageInfo SPageInfo; typedef struct SDiskbasedBuf SDiskbasedBuf; -#define DEFAULT_INTERN_BUF_PAGE_SIZE (1024L) // in bytes +#define DEFAULT_INTERN_BUF_PAGE_SIZE (1024L) // in bytes typedef struct SFilePage { int32_t num; @@ -193,4 +192,4 @@ void dBufPrintStatis(const SDiskbasedBuf* pBuf); } #endif -#endif // TDENGINE_TPAGEDBUF_H +#endif // _TD_UTIL_PAGEDBUF_H_ diff --git a/include/util/tqueue.h b/include/util/tqueue.h index cfa5a65c2a..d51184edfc 100644 --- a/include/util/tqueue.h +++ b/include/util/tqueue.h @@ -13,8 +13,9 @@ * along with this program. If not, see . */ -#ifndef _TD_UTIL_QUEUE_H -#define _TD_UTIL_QUEUE_H +#ifndef _TD_UTIL_QUEUE_H_ +#define _TD_UTIL_QUEUE_H_ + #include "os.h" #ifdef __cplusplus @@ -47,7 +48,7 @@ typedef void (*FItems)(void *ahandle, STaosQall *qall, int32_t numOfItems); STaosQueue *taosOpenQueue(); void taosCloseQueue(STaosQueue *queue); void taosSetQueueFp(STaosQueue *queue, FItem itemFp, FItems itemsFp); -void * taosAllocateQitem(int32_t size); +void *taosAllocateQitem(int32_t size); void taosFreeQitem(void *pItem); int32_t taosWriteQitem(STaosQueue *queue, void *pItem); int32_t taosReadQitem(STaosQueue *queue, void **ppItem); @@ -80,4 +81,4 @@ int32_t taosGetQsetItemsNumber(STaosQset *qset); } #endif -#endif /*_TD_UTIL_QUEUE_H*/ +#endif /*_TD_UTIL_QUEUE_H_*/ diff --git a/include/util/tref.h b/include/util/tref.h index 6680204d63..7e08bb045b 100644 --- a/include/util/tref.h +++ b/include/util/tref.h @@ -14,8 +14,8 @@ * along with this program. If not, see . */ -#ifndef _TD_UTIL_REF_H -#define _TD_UTIL_REF_H +#ifndef _TD_UTIL_REF_H_ +#define _TD_UTIL_REF_H_ #include "os.h" @@ -25,45 +25,45 @@ extern "C" { // open a reference set, max is the mod used by hash, fp is the pointer to free resource function // return rsetId which will be used by other APIs. On error, -1 is returned, and terrno is set appropriately -int taosOpenRef(int max, void (*fp)(void *)); +int32_t taosOpenRef(int32_t max, void (*fp)(void *)); // close the reference set, refId is the return value by taosOpenRef // return 0 if success. On error, -1 is returned, and terrno is set appropriately -int taosCloseRef(int refId); +int32_t taosCloseRef(int32_t refId); // add ref, p is the pointer to resource or pointer ID // return Reference ID(rid) allocated. On error, -1 is returned, and terrno is set appropriately -int64_t taosAddRef(int refId, void *p); +int64_t taosAddRef(int32_t refId, void *p); // remove ref, rid is the reference ID returned by taosAddRef // return 0 if success. On error, -1 is returned, and terrno is set appropriately -int taosRemoveRef(int rsetId, int64_t rid); +int32_t taosRemoveRef(int32_t rsetId, int64_t rid); // acquire ref, rid is the reference ID returned by taosAddRef // return the resource p. On error, NULL is returned, and terrno is set appropriately -void *taosAcquireRef(int rsetId, int64_t rid); +void *taosAcquireRef(int32_t rsetId, int64_t rid); // release ref, rid is the reference ID returned by taosAddRef // return 0 if success. On error, -1 is returned, and terrno is set appropriately -int taosReleaseRef(int rsetId, int64_t rid); +int32_t taosReleaseRef(int32_t rsetId, int64_t rid); -// return the first reference if rid is 0, otherwise return the next after current reference. +// return the first reference if rid is 0, otherwise return the next after current reference. // if return value is NULL, it means list is over(if terrno is set, it means error happens) -void *taosIterateRef(int rsetId, int64_t rid); +void *taosIterateRef(int32_t rsetId, int64_t rid); // return the number of references in system -int taosListRef(); +int32_t taosListRef(); #define RID_VALID(x) ((x) > 0) -/* sample code to iterate the refs +/* sample code to iterate the refs -void demoIterateRefs(int rsetId) { +void demoIterateRefs(int32_t rsetId) { void *p = taosIterateRef(refId, 0); while (p) { // process P - + // get the rid from p p = taosIterateRef(rsetId, rid); @@ -76,4 +76,4 @@ void demoIterateRefs(int rsetId) { } #endif -#endif /*_TD_UTIL_REF_H*/ +#endif /*_TD_UTIL_REF_H_*/ diff --git a/include/util/tsched.h b/include/util/tsched.h index d60c0c5ad4..3bf740f528 100644 --- a/include/util/tsched.h +++ b/include/util/tsched.h @@ -13,8 +13,10 @@ * along with this program. If not, see . */ -#ifndef _TD_UTIL_SCHED_H -#define _TD_UTIL_SCHED_H +#ifndef _TD_UTIL_SCHED_H_ +#define _TD_UTIL_SCHED_H_ + +#include "os.h" #ifdef __cplusplus extern "C" { @@ -36,7 +38,7 @@ typedef struct SSchedMsg { * @param label the label of the queue * @return the created queue scheduler */ -void *taosInitScheduler(int capacity, int numOfThreads, const char *label); +void *taosInitScheduler(int32_t capacity, int32_t numOfThreads, const char *label); /** * Create a thread-safe ring-buffer based task queue and return the instance. @@ -47,7 +49,7 @@ void *taosInitScheduler(int capacity, int numOfThreads, const char *label); * @param tmrCtrl the timer controller, tmr_ctrl_t* * @return the created queue scheduler */ -void *taosInitSchedulerWithInfo(int capacity, int numOfThreads, const char *label, void *tmrCtrl); +void *taosInitSchedulerWithInfo(int32_t capacity, int32_t numOfThreads, const char *label, void *tmrCtrl); /** * Clean up the queue scheduler instance and free the memory. @@ -68,4 +70,4 @@ void taosScheduleTask(void *queueScheduler, SSchedMsg *pMsg); } #endif -#endif /*_TD_UTIL_SCHED_H*/ +#endif /*_TD_UTIL_SCHED_H_*/ diff --git a/include/util/tskiplist.h b/include/util/tskiplist.h index 823a6f247f..64cab08cfe 100644 --- a/include/util/tskiplist.h +++ b/include/util/tskiplist.h @@ -16,22 +16,22 @@ #ifndef _TD_UTIL_SKILIST_H #define _TD_UTIL_SKILIST_H -#ifdef __cplusplus -extern "C" { -#endif - #include "os.h" #include "taos.h" #include "tarray.h" #include "tfunctional.h" -#define MAX_SKIP_LIST_LEVEL 15 +#ifdef __cplusplus +extern "C" { +#endif + +#define MAX_SKIP_LIST_LEVEL 15 #define SKIP_LIST_RECORD_PERFORMANCE 0 // For key property setting -#define SL_ALLOW_DUP_KEY (uint8_t)0x0 // Allow duplicate key exists (for tag index usage) +#define SL_ALLOW_DUP_KEY (uint8_t)0x0 // Allow duplicate key exists (for tag index usage) #define SL_DISCARD_DUP_KEY (uint8_t)0x1 // Discard duplicate key (for data update=0 case) -#define SL_UPDATE_DUP_KEY (uint8_t)0x2 // Update duplicate key by remove/insert (for data update!=0 case) +#define SL_UPDATE_DUP_KEY (uint8_t)0x2 // Update duplicate key by remove/insert (for data update!=0 case) // For thread safety setting #define SL_THREAD_SAFE (uint8_t)0x4 @@ -39,17 +39,17 @@ extern "C" { typedef char *SSkipListKey; typedef char *(*__sl_key_fn_t)(const void *); -typedef void (*sl_patch_row_fn_t)(void * pDst, const void * pSrc); -typedef void* (*iter_next_fn_t)(void *iter); +typedef void (*sl_patch_row_fn_t)(void *pDst, const void *pSrc); +typedef void *(*iter_next_fn_t)(void *iter); typedef struct SSkipListNode { - uint8_t level; - void * pData; + uint8_t level; + void *pData; struct SSkipListNode *forwards[]; } SSkipListNode; -#define SL_GET_NODE_DATA(n) (n)->pData -#define SL_NODE_GET_FORWARD_POINTER(n, l) (n)->forwards[(l)] +#define SL_GET_NODE_DATA(n) (n)->pData +#define SL_NODE_GET_FORWARD_POINTER(n, l) (n)->forwards[(l)] #define SL_NODE_GET_BACKWARD_POINTER(n, l) (n)->forwards[(n)->level + (l)] /* @@ -100,14 +100,10 @@ typedef struct tSkipListState { uint64_t nTotalElapsedTimeForInsert; } tSkipListState; -typedef enum { - SSkipListPutSuccess = 0, - SSkipListPutEarlyStop = 1, - SSkipListPutSkipOne = 2 -} SSkipListPutStatus; +typedef enum { SSkipListPutSuccess = 0, SSkipListPutEarlyStop = 1, SSkipListPutSkipOne = 2 } SSkipListPutStatus; typedef struct SSkipList { - unsigned int seed; + uint32_t seed; __compar_fn_t comparFn; __sl_key_fn_t keyFn; pthread_rwlock_t *lock; @@ -117,41 +113,41 @@ typedef struct SSkipList { uint8_t type; // static info above uint8_t level; uint32_t size; - SSkipListNode * pHead; // point to the first element - SSkipListNode * pTail; // point to the last element + SSkipListNode *pHead; // point to the first element + SSkipListNode *pTail; // point to the last element #if SKIP_LIST_RECORD_PERFORMANCE tSkipListState state; // skiplist state #endif - tGenericSavedFunc* insertHandleFn; + tGenericSavedFunc *insertHandleFn; } SSkipList; typedef struct SSkipListIterator { - SSkipList * pSkipList; + SSkipList *pSkipList; SSkipListNode *cur; - int32_t step; // the number of nodes that have been checked already - int32_t order; // order of the iterator - SSkipListNode *next; // next points to the true qualified node in skiplist + int32_t step; // the number of nodes that have been checked already + int32_t order; // order of the iterator + SSkipListNode *next; // next points to the true qualified node in skiplist } SSkipListIterator; -#define SL_IS_THREAD_SAFE(s) (((s)->flags) & SL_THREAD_SAFE) -#define SL_DUP_MODE(s) (((s)->flags) & ((((uint8_t)1) << 2) - 1)) +#define SL_IS_THREAD_SAFE(s) (((s)->flags) & SL_THREAD_SAFE) +#define SL_DUP_MODE(s) (((s)->flags) & ((((uint8_t)1) << 2) - 1)) #define SL_GET_NODE_KEY(s, n) ((s)->keyFn((n)->pData)) -#define SL_GET_MIN_KEY(s) SL_GET_NODE_KEY(s, SL_NODE_GET_FORWARD_POINTER((s)->pHead, 0)) -#define SL_GET_MAX_KEY(s) SL_GET_NODE_KEY((s), SL_NODE_GET_BACKWARD_POINTER((s)->pTail, 0)) -#define SL_SIZE(s) (s)->size +#define SL_GET_MIN_KEY(s) SL_GET_NODE_KEY(s, SL_NODE_GET_FORWARD_POINTER((s)->pHead, 0)) +#define SL_GET_MAX_KEY(s) SL_GET_NODE_KEY((s), SL_NODE_GET_BACKWARD_POINTER((s)->pTail, 0)) +#define SL_SIZE(s) (s)->size SSkipList *tSkipListCreate(uint8_t maxLevel, uint8_t keyType, uint16_t keyLen, __compar_fn_t comparFn, uint8_t flags, __sl_key_fn_t fn); void tSkipListDestroy(SSkipList *pSkipList); -SSkipListNode * tSkipListPut(SSkipList *pSkipList, void *pData); +SSkipListNode *tSkipListPut(SSkipList *pSkipList, void *pData); void tSkipListPutBatchByIter(SSkipList *pSkipList, void *iter, iter_next_fn_t iterate); -SArray * tSkipListGet(SSkipList *pSkipList, SSkipListKey pKey); +SArray *tSkipListGet(SSkipList *pSkipList, SSkipListKey pKey); void tSkipListPrint(SSkipList *pSkipList, int16_t nlevel); SSkipListIterator *tSkipListCreateIter(SSkipList *pSkipList); SSkipListIterator *tSkipListCreateIterFromVal(SSkipList *pSkipList, const char *val, int32_t type, int32_t order); bool tSkipListIterNext(SSkipListIterator *iter); -SSkipListNode * tSkipListIterGet(SSkipListIterator *iter); -void * tSkipListDestroyIter(SSkipListIterator *iter); +SSkipListNode *tSkipListIterGet(SSkipListIterator *iter); +void *tSkipListDestroyIter(SSkipListIterator *iter); uint32_t tSkipListRemove(SSkipList *pSkipList, SSkipListKey key); void tSkipListRemoveNode(SSkipList *pSkipList, SSkipListNode *pNode); @@ -159,4 +155,4 @@ void tSkipListRemoveNode(SSkipList *pSkipList, SSkipListNode *pNod } #endif -#endif /*_TD_UTIL_SKILIST_H*/ +#endif /*_TD_UTIL_SKILIST_H*/ diff --git a/include/util/tstep.h b/include/util/tstep.h deleted file mode 100644 index e4ce7f584b..0000000000 --- a/include/util/tstep.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef _TD_UTIL_STEP_H_ -#define _TD_UTIL_STEP_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct SSteps SSteps; -typedef int32_t (*InitFp)(); -typedef void (*CleanupFp)(); -typedef void (*ReportFp)(char *name, char *desc); - -SSteps *taosStepInit(int32_t maxsize, ReportFp fp); -int32_t taosStepExec(SSteps *steps); -void taosStepCleanup(SSteps *steps); -int32_t taosStepAdd(SSteps *steps, char *name, InitFp initFp, CleanupFp cleanupFp); - -#ifdef __cplusplus -} -#endif - -#endif /*_TD_UTIL_STEP_H_*/ diff --git a/include/util/tstrbuild.h b/include/util/tstrbuild.h index 48a302531c..89f3472346 100644 --- a/include/util/tstrbuild.h +++ b/include/util/tstrbuild.h @@ -13,12 +13,10 @@ * along with this program. If not, see . */ -#ifndef _TD_UTIL_STRING_BUILDER_H -#define _TD_UTIL_STRING_BUILDER_H +#ifndef _TD_UTIL_STRING_BUILDER_H_ +#define _TD_UTIL_STRING_BUILDER_H_ -#include -#include -#include +#include "os.h" #ifdef __cplusplus extern "C" { @@ -26,16 +24,16 @@ extern "C" { typedef struct SStringBuilder { jmp_buf jb; - size_t size; - size_t pos; - char* buf; + size_t size; + size_t pos; + char* buf; } SStringBuilder; #define taosStringBuilderSetJmp(sb) setjmp((sb)->jb) -void taosStringBuilderEnsureCapacity(SStringBuilder* sb, size_t size); +void taosStringBuilderEnsureCapacity(SStringBuilder* sb, size_t size); char* taosStringBuilderGetResult(SStringBuilder* sb, size_t* len); -void taosStringBuilderDestroy(SStringBuilder* sb); +void taosStringBuilderDestroy(SStringBuilder* sb); void taosStringBuilderAppend(SStringBuilder* sb, const void* data, size_t len); void taosStringBuilderAppendChar(SStringBuilder* sb, char c); @@ -49,4 +47,4 @@ void taosStringBuilderAppendDouble(SStringBuilder* sb, double v); } #endif -#endif /*_TD_UTIL_STRING_BUILDER_H*/ \ No newline at end of file +#endif /*_TD_UTIL_STRING_BUILDER_H_*/ \ No newline at end of file diff --git a/include/util/tthread.h b/include/util/tthread.h index 7a5fd1f4c8..4941206944 100644 --- a/include/util/tthread.h +++ b/include/util/tthread.h @@ -13,25 +13,21 @@ * along with this program. If not, see . */ -#ifndef _TD_UTIL_THREAD_H -#define _TD_UTIL_THREAD_H +#ifndef _TD_UTIL_THREAD_H_ +#define _TD_UTIL_THREAD_H_ + +#include "os.h" #ifdef __cplusplus extern "C" { #endif -#include "os.h" -#include "tdef.h" - -// create new thread pthread_t* taosCreateThread(void* (*__start_routine)(void*), void* param); -// destory thread -bool taosDestoryThread(pthread_t* pthread); -// thread running return true -bool taosThreadRunning(pthread_t* pthread); +bool taosDestoryThread(pthread_t* pthread); +bool taosThreadRunning(pthread_t* pthread); #ifdef __cplusplus } #endif -#endif /*_TD_UTIL_THREAD_H*/ +#endif /*_TD_UTIL_THREAD_H_*/ diff --git a/include/util/ttimer.h b/include/util/ttimer.h index 01d70c7d02..f2ee825c4e 100644 --- a/include/util/ttimer.h +++ b/include/util/ttimer.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef _TD_UTIL_TIMER_H -#define _TD_UTIL_TIMER_H +#ifndef _TD_UTIL_TIMER_H_ +#define _TD_UTIL_TIMER_H_ #include "os.h" @@ -25,23 +25,23 @@ extern "C" { typedef void *tmr_h; typedef void (*TAOS_TMR_CALLBACK)(void *, void *); -extern int taosTmrThreads; +extern int32_t taosTmrThreads; #define MSECONDS_PER_TICK 5 -void *taosTmrInit(int maxTmr, int resoultion, int longest, const char *label); +void *taosTmrInit(int32_t maxTmr, int32_t resoultion, int32_t longest, const char *label); -tmr_h taosTmrStart(TAOS_TMR_CALLBACK fp, int mseconds, void *param, void *handle); +tmr_h taosTmrStart(TAOS_TMR_CALLBACK fp, int32_t mseconds, void *param, void *handle); bool taosTmrStop(tmr_h tmrId); bool taosTmrStopA(tmr_h *timerId); -bool taosTmrReset(TAOS_TMR_CALLBACK fp, int mseconds, void *param, void *handle, tmr_h *pTmrId); +bool taosTmrReset(TAOS_TMR_CALLBACK fp, int32_t mseconds, void *param, void *handle, tmr_h *pTmrId); void taosTmrCleanUp(void *handle); -int32_t taosInitTimer(void (*callback)(int), int32_t ms); +int32_t taosInitTimer(void (*callback)(int32_t), int32_t ms); void taosUninitTimer(); @@ -49,4 +49,4 @@ void taosUninitTimer(); } #endif -#endif /*_TD_UTIL_TIMER_H*/ +#endif /*_TD_UTIL_TIMER_H_*/ diff --git a/include/util/tutil.h b/include/util/tutil.h index c315948702..e0f92be76a 100644 --- a/include/util/tutil.h +++ b/include/util/tutil.h @@ -13,18 +13,18 @@ * along with this program. If not, see . */ -#ifndef _TD_UTIL_UTIL_H -#define _TD_UTIL_UTIL_H - -#ifdef __cplusplus -extern "C" { -#endif +#ifndef _TD_UTIL_UTIL_H_ +#define _TD_UTIL_UTIL_H_ #include "os.h" #include "tcrc32c.h" #include "tdef.h" #include "tmd5.h" +#ifdef __cplusplus +extern "C" { +#endif + int32_t strdequote(char *src); int32_t strndequote(char *dst, const char *z, int32_t len); int32_t strRmquote(char *z, int32_t len); @@ -49,7 +49,7 @@ void taosIpPort2String(uint32_t ip, uint16_t port, char *str); static FORCE_INLINE void taosEncryptPass(uint8_t *inBuf, size_t inLen, char *target) { T_MD5_CTX context; tMD5Init(&context); - tMD5Update(&context, inBuf, (unsigned int)inLen); + tMD5Update(&context, inBuf, (uint32_t)inLen); tMD5Final(&context); memcpy(target, context.digest, tListLen(context.digest)); } @@ -57,17 +57,17 @@ static FORCE_INLINE void taosEncryptPass(uint8_t *inBuf, size_t inLen, char *tar static FORCE_INLINE void taosEncryptPass_c(uint8_t *inBuf, size_t len, char *target) { T_MD5_CTX context; tMD5Init(&context); - tMD5Update(&context, inBuf, (unsigned int)len); + tMD5Update(&context, inBuf, (uint32_t)len); tMD5Final(&context); - sprintf(target, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", context.digest[0], context.digest[1], context.digest[2], - context.digest[3], context.digest[4], context.digest[5], context.digest[6], context.digest[7], - context.digest[8], context.digest[9], context.digest[10], context.digest[11], context.digest[12], - context.digest[13], context.digest[14], context.digest[15]); + sprintf(target, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", context.digest[0], + context.digest[1], context.digest[2], context.digest[3], context.digest[4], context.digest[5], + context.digest[6], context.digest[7], context.digest[8], context.digest[9], context.digest[10], + context.digest[11], context.digest[12], context.digest[13], context.digest[14], context.digest[15]); } #ifdef __cplusplus } #endif -#endif /*_TD_UTIL_UTIL_H*/ +#endif /*_TD_UTIL_UTIL_H_*/ diff --git a/include/util/tversion.h b/include/util/tversion.h index 3d7a7e1b66..c924752a01 100644 --- a/include/util/tversion.h +++ b/include/util/tversion.h @@ -13,8 +13,10 @@ * along with this program. If not, see . */ -#ifndef _TD_UTIL_VERSION_H -#define _TD_UTIL_VERSION_H +#ifndef _TD_UTIL_VERSION_H_ +#define _TD_UTIL_VERSION_H_ + +#include "os.h" #ifdef __cplusplus extern "C" { @@ -28,4 +30,4 @@ int32_t taosCheckVersionCompatible(int32_t clientVer, int32_t serverVer, int32_t } #endif -#endif /*_TD_UTIL_VERSION_H*/ +#endif /*_TD_UTIL_VERSION_H_*/ diff --git a/include/util/tworker.h b/include/util/tworker.h index 771c7c9433..e6f6bc077c 100644 --- a/include/util/tworker.h +++ b/include/util/tworker.h @@ -13,8 +13,9 @@ * along with this program. If not, see . */ -#ifndef _TD_UTIL_WORKER_H -#define _TD_UTIL_WORKER_H +#ifndef _TD_UTIL_WORKER_H_ +#define _TD_UTIL_WORKER_H_ + #include "tqueue.h" #ifdef __cplusplus @@ -34,25 +35,25 @@ typedef struct SQWorkerPool { int32_t max; // max number of workers int32_t min; // min number of workers int32_t num; // current number of workers - STaosQset * qset; - const char * name; - SQWorker * workers; + STaosQset *qset; + const char *name; + SQWorker *workers; pthread_mutex_t mutex; } SQWorkerPool, SFWorkerPool; typedef struct SWWorker { int32_t id; // worker id pthread_t thread; // thread - STaosQall * qall; - STaosQset * qset; // queue set + STaosQall *qall; + STaosQset *qset; // queue set SWWorkerPool *pool; } SWWorker; typedef struct SWWorkerPool { int32_t max; // max number of workers int32_t nextId; // from 0 to max-1, cyclic - const char * name; - SWWorker * workers; + const char *name; + SWWorker *workers; pthread_mutex_t mutex; } SWWorkerPool; @@ -75,4 +76,4 @@ void tWWorkerFreeQueue(SWWorkerPool *pool, STaosQueue *queue); } #endif -#endif /*_TD_UTIL_WORKER_H*/ +#endif /*_TD_UTIL_WORKER_H_*/ diff --git a/include/util/types.h b/include/util/types.h index 25688df56c..f7a535c965 100644 --- a/include/util/types.h +++ b/include/util/types.h @@ -22,14 +22,14 @@ extern "C" { #endif -#define GET_INT8_VAL(x) (*(int8_t *)(x)) -#define GET_INT16_VAL(x) (*(int16_t *)(x)) -#define GET_INT32_VAL(x) (*(int32_t *)(x)) -#define GET_INT64_VAL(x) (*(int64_t *)(x)) -#define GET_UINT8_VAL(x) (*(uint8_t*) (x)) -#define GET_UINT16_VAL(x) (*(uint16_t *)(x)) -#define GET_UINT32_VAL(x) (*(uint32_t *)(x)) -#define GET_UINT64_VAL(x) (*(uint64_t *)(x)) +#define GET_INT8_VAL(x) (*(int8_t *)(x)) +#define GET_INT16_VAL(x) (*(int16_t *)(x)) +#define GET_INT32_VAL(x) (*(int32_t *)(x)) +#define GET_INT64_VAL(x) (*(int64_t *)(x)) +#define GET_UINT8_VAL(x) (*(uint8_t *)(x)) +#define GET_UINT16_VAL(x) (*(uint16_t *)(x)) +#define GET_UINT32_VAL(x) (*(uint32_t *)(x)) +#define GET_UINT64_VAL(x) (*(uint64_t *)(x)) static FORCE_INLINE float taos_align_get_float(const char *pBuf) { #if __STDC_VERSION__ >= 201112L @@ -64,23 +64,33 @@ static FORCE_INLINE double taos_align_get_double(const char *pBuf) { // #define SET_FLOAT_PTR(x, y) { (*(int32_t*) x = *(int32_t*)y); } // #define SET_DOUBLE_PTR(x, y) { (*(int64_t*) x = *(int64_t*)y); } // #else - #define GET_FLOAT_VAL(x) (*(float *)(x)) - #define GET_DOUBLE_VAL(x) (*(double *)(x)) - #define SET_BIGINT_VAL(x, y) { (*(int64_t *)(x)) = (int64_t)(y); } - #define SET_FLOAT_VAL(x, y) { (*(float *)(x)) = (float)(y); } - #define SET_DOUBLE_VAL(x, y) { (*(double *)(x)) = (double)(y); } - #define SET_FLOAT_PTR(x, y) { (*(float *)(x)) = (*(float *)(y)); } - #define SET_DOUBLE_PTR(x, y) { (*(double *)(x)) = (*(double *)(y)); } +#define GET_FLOAT_VAL(x) (*(float *)(x)) +#define GET_DOUBLE_VAL(x) (*(double *)(x)) +#define SET_BIGINT_VAL(x, y) \ + { (*(int64_t *)(x)) = (int64_t)(y); } +#define SET_FLOAT_VAL(x, y) \ + { (*(float *)(x)) = (float)(y); } +#define SET_DOUBLE_VAL(x, y) \ + { (*(double *)(x)) = (double)(y); } +#define SET_FLOAT_PTR(x, y) \ + { (*(float *)(x)) = (*(float *)(y)); } +#define SET_DOUBLE_PTR(x, y) \ + { (*(double *)(x)) = (*(double *)(y)); } // #endif -typedef uint16_t VarDataLenT; // maxVarDataLen: 32767 -#define VARSTR_HEADER_SIZE sizeof(VarDataLenT) +typedef uint16_t VarDataLenT; // maxVarDataLen: 32767 +#define VARSTR_HEADER_SIZE sizeof(VarDataLenT) #define varDataLen(v) ((VarDataLenT *)(v))[0] #define varDataVal(v) ((void *)((char *)v + VARSTR_HEADER_SIZE)) typedef int32_t VarDataOffsetT; +typedef struct tstr { + VarDataLenT len; + char data[]; +} tstr; + #ifdef __cplusplus } #endif diff --git a/include/util/version.h b/include/util/version.h index 01efecc59d..b241dd248b 100644 --- a/include/util/version.h +++ b/include/util/version.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef _TD_UTIL_VERSION_H -#define _TD_UTIL_VERSION_H +#ifndef _TD_UTIL_VERSION_H_ +#define _TD_UTIL_VERSION_H_ #ifdef __cplusplus extern "C" { @@ -30,4 +30,4 @@ extern char buildinfo[]; } #endif -#endif /*_TD_UTIL_VERSION_H*/ +#endif /*_TD_UTIL_VERSION_H_*/ diff --git a/source/common/src/tcompare.c b/source/common/src/tcompare.c deleted file mode 100644 index ef441c97c7..0000000000 --- a/source/common/src/tcompare.c +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#include "tcompare.h" - - -__compar_fn_t getKeyComparFunc(int32_t keyType, int32_t order) { - __compar_fn_t comparFn = NULL; - - switch (keyType) { - case TSDB_DATA_TYPE_TINYINT: - case TSDB_DATA_TYPE_BOOL: - comparFn = (order == TSDB_ORDER_ASC)? compareInt8Val:compareInt8ValDesc; - break; - case TSDB_DATA_TYPE_SMALLINT: - comparFn = (order == TSDB_ORDER_ASC)? compareInt16Val:compareInt16ValDesc; - break; - case TSDB_DATA_TYPE_INT: - comparFn = (order == TSDB_ORDER_ASC)? compareInt32Val:compareInt32ValDesc; - break; - case TSDB_DATA_TYPE_BIGINT: - case TSDB_DATA_TYPE_TIMESTAMP: - comparFn = (order == TSDB_ORDER_ASC)? compareInt64Val:compareInt64ValDesc; - break; - case TSDB_DATA_TYPE_FLOAT: - comparFn = (order == TSDB_ORDER_ASC)? compareFloatVal:compareFloatValDesc; - break; - case TSDB_DATA_TYPE_DOUBLE: - comparFn = (order == TSDB_ORDER_ASC)? compareDoubleVal:compareDoubleValDesc; - break; - case TSDB_DATA_TYPE_UTINYINT: - comparFn = (order == TSDB_ORDER_ASC)? compareUint8Val:compareUint8ValDesc; - break; - case TSDB_DATA_TYPE_USMALLINT: - comparFn = (order == TSDB_ORDER_ASC)? compareUint16Val:compareUint16ValDesc; - break; - case TSDB_DATA_TYPE_UINT: - comparFn = (order == TSDB_ORDER_ASC)? compareUint32Val:compareUint32ValDesc; - break; - case TSDB_DATA_TYPE_UBIGINT: - comparFn = (order == TSDB_ORDER_ASC)? compareUint64Val:compareUint64ValDesc; - break; - case TSDB_DATA_TYPE_BINARY: - comparFn = (order == TSDB_ORDER_ASC)? compareLenPrefixedStr:compareLenPrefixedStrDesc; - break; - case TSDB_DATA_TYPE_NCHAR: - comparFn = (order == TSDB_ORDER_ASC)? compareLenPrefixedWStr:compareLenPrefixedWStrDesc; - break; - default: - comparFn = (order == TSDB_ORDER_ASC)? compareInt32Val:compareInt32ValDesc; - break; - } - - return comparFn; -} - -int32_t doCompare(const char* f1, const char* f2, int32_t type, size_t size) { - switch (type) { - case TSDB_DATA_TYPE_INT: DEFAULT_COMP(GET_INT32_VAL(f1), GET_INT32_VAL(f2)); - case TSDB_DATA_TYPE_DOUBLE: DEFAULT_DOUBLE_COMP(GET_DOUBLE_VAL(f1), GET_DOUBLE_VAL(f2)); - case TSDB_DATA_TYPE_FLOAT: DEFAULT_FLOAT_COMP(GET_FLOAT_VAL(f1), GET_FLOAT_VAL(f2)); - case TSDB_DATA_TYPE_BIGINT: DEFAULT_COMP(GET_INT64_VAL(f1), GET_INT64_VAL(f2)); - case TSDB_DATA_TYPE_SMALLINT: DEFAULT_COMP(GET_INT16_VAL(f1), GET_INT16_VAL(f2)); - case TSDB_DATA_TYPE_TINYINT: - case TSDB_DATA_TYPE_BOOL: DEFAULT_COMP(GET_INT8_VAL(f1), GET_INT8_VAL(f2)); - case TSDB_DATA_TYPE_UTINYINT: DEFAULT_COMP(GET_UINT8_VAL(f1), GET_UINT8_VAL(f2)); - case TSDB_DATA_TYPE_USMALLINT: DEFAULT_COMP(GET_UINT16_VAL(f1), GET_UINT16_VAL(f2)); - case TSDB_DATA_TYPE_UINT: DEFAULT_COMP(GET_UINT32_VAL(f1), GET_UINT32_VAL(f2)); - case TSDB_DATA_TYPE_UBIGINT: DEFAULT_COMP(GET_UINT64_VAL(f1), GET_UINT64_VAL(f2)); - case TSDB_DATA_TYPE_NCHAR: { - tstr* t1 = (tstr*) f1; - tstr* t2 = (tstr*) f2; - - if (t1->len != t2->len) { - return t1->len > t2->len? 1:-1; - } - int32_t ret = memcmp((wchar_t*) t1, (wchar_t*) t2, t2->len); - if (ret == 0) { - return ret; - } - return (ret < 0) ? -1 : 1; - } - default: { // todo refactor - tstr* t1 = (tstr*) f1; - tstr* t2 = (tstr*) f2; - - if (t1->len != t2->len) { - return t1->len > t2->len? 1:-1; - } else { - int32_t ret = strncmp(t1->data, t2->data, t1->len); - if (ret == 0) { - return 0; - } else { - return ret < 0? -1:1; - } - } - } - } -} diff --git a/source/common/src/tep.c b/source/common/src/tep.c index d325f57c91..573ff6d9b7 100644 --- a/source/common/src/tep.c +++ b/source/common/src/tep.c @@ -1,5 +1,5 @@ #include "tep.h" -#include +#include "tcompare.h" #include "common.h" #include "tglobal.h" #include "tlockfree.h" diff --git a/source/common/src/tglobal.c b/source/common/src/tglobal.c index 26d33199c1..38685855df 100644 --- a/source/common/src/tglobal.c +++ b/source/common/src/tglobal.c @@ -207,6 +207,8 @@ static int32_t taosLoadCfg(SConfig *pCfg, const char *inputCfgDir, const char *e } static int32_t taosAddClientLogCfg(SConfig *pCfg) { + if (cfgAddDir(pCfg, "configDir", configDir, 1) != 0) return -1; + if (cfgAddDir(pCfg, "scriptDir", configDir, 1) != 0) return -1; if (cfgAddDir(pCfg, "logDir", tsLogDir, 1) != 0) return -1; if (cfgAddFloat(pCfg, "minimalLogDirGB", 1.0f, 0.001f, 10000000, 1) != 0) return -1; if (cfgAddInt32(pCfg, "numOfLogLines", tsNumOfLogLines, 1000, 2000000000, 1) != 0) return -1; @@ -218,8 +220,6 @@ static int32_t taosAddClientLogCfg(SConfig *pCfg) { if (cfgAddInt32(pCfg, "tmrDebugFlag", tmrDebugFlag, 0, 255, 1) != 0) return -1; if (cfgAddInt32(pCfg, "jniDebugFlag", jniDebugFlag, 0, 255, 1) != 0) return -1; if (cfgAddInt32(pCfg, "simDebugFlag", 143, 0, 255, 1) != 0) return -1; - if (cfgAddDir(pCfg, "configDir", configDir, 1) != 0) return -1; - if (cfgAddDir(pCfg, "scriptDir", configDir, 1) != 0) return -1; if (cfgAddInt32(pCfg, "debugFlag", 0, 0, 255, 1) != 0) return -1; return 0; } @@ -325,6 +325,7 @@ static int32_t taosAddServerCfg(SConfig *pCfg) { static void taosSetClientLogCfg(SConfig *pCfg) { SConfigItem *pItem = cfgGetItem(pCfg, "logDir"); tstrncpy(tsLogDir, cfgGetItem(pCfg, "logDir")->str, PATH_MAX); + taosExpandDir(tsLogDir, tsLogDir, PATH_MAX); tsLogSpace.reserved = cfgGetItem(pCfg, "minimalLogDirGB")->fval; tsNumOfLogLines = cfgGetItem(pCfg, "numOfLogLines")->i32; tsAsyncLog = cfgGetItem(pCfg, "asyncLog")->bval; @@ -349,12 +350,24 @@ static void taosSetServerLogCfg(SConfig *pCfg) { } static void taosSetClientCfg(SConfig *pCfg) { - tstrncpy(tsFirst, cfgGetItem(pCfg, "firstEp")->str, TSDB_EP_LEN); - tstrncpy(tsSecond, cfgGetItem(pCfg, "secondEp")->str, TSDB_EP_LEN); tstrncpy(tsLocalFqdn, cfgGetItem(pCfg, "fqdn")->str, TSDB_EP_LEN); tsServerPort = (uint16_t)cfgGetItem(pCfg, "serverPort")->i32; snprintf(tsLocalEp, sizeof(tsLocalEp), "%s:%u", tsLocalFqdn, tsServerPort); + + SConfigItem *pFirstEpItem = cfgGetItem(pCfg, "firstEp"); + SEp firstEp = {0}; + taosGetFqdnPortFromEp(pFirstEpItem->str, &firstEp); + snprintf(tsFirst, sizeof(tsFirst), "%s:%u", firstEp.fqdn, firstEp.port); + cfgSetItem(pCfg, "firstEp", tsFirst, pFirstEpItem->stype); + + SConfigItem *pSecondpItem = cfgGetItem(pCfg, "secondEp"); + SEp secondEp = {0}; + taosGetFqdnPortFromEp(pSecondpItem->str, &secondEp); + snprintf(tsSecond, sizeof(tsSecond), "%s:%u", secondEp.fqdn, secondEp.port); + cfgSetItem(pCfg, "secondEp", tsSecond, pSecondpItem->stype); + tstrncpy(tsLogDir, cfgGetItem(pCfg, "tempDir")->str, PATH_MAX); + taosExpandDir(tsLogDir, tsLogDir, PATH_MAX); tsTempSpace.reserved = cfgGetItem(pCfg, "minimalTempDirGB")->fval; tsNumOfThreadsPerCore = cfgGetItem(pCfg, "maxTmrCtrl")->fval; @@ -395,6 +408,8 @@ static void taosSetSystemCfg(SConfig *pCfg) { static void taosSetServerCfg(SConfig *pCfg) { tstrncpy(tsDataDir, cfgGetItem(pCfg, "dataDir")->str, PATH_MAX); + taosExpandDir(tsDataDir, tsDataDir, PATH_MAX); + tsTempSpace.reserved = cfgGetItem(pCfg, "minimalDataDirGB")->fval; tsNumOfCommitThreads = cfgGetItem(pCfg, "numOfCommitThreads")->i32; tsRatioOfQueryCores = cfgGetItem(pCfg, "ratioOfQueryCores")->fval; @@ -473,10 +488,10 @@ int32_t taosInitCfg(const char *cfgDir, const char *envFile, const char *apolloU if (taosAddClientLogCfg(tsCfg) != 0) return -1; if (taosAddClientCfg(tsCfg) != 0) return -1; } else { - if (taosAddClientLogCfg(tsCfg) != 0) return -1; - if (taosAddServerLogCfg(tsCfg) != 0) return -1; if (taosAddClientCfg(tsCfg) != 0) return -1; if (taosAddServerCfg(tsCfg) != 0) return -1; + if (taosAddClientLogCfg(tsCfg) != 0) return -1; + if (taosAddServerLogCfg(tsCfg) != 0) return -1; } taosAddSystemCfg(tsCfg); diff --git a/source/dnode/mgmt/daemon/src/dmnCfg.c b/source/dnode/mgmt/daemon/src/dmnCfg.c index 57c788dee1..afb9ffbdd0 100644 --- a/source/dnode/mgmt/daemon/src/dmnCfg.c +++ b/source/dnode/mgmt/daemon/src/dmnCfg.c @@ -22,11 +22,11 @@ SDnodeObjCfg dmnGetObjCfg() { SDnodeObjCfg objCfg = {0}; objCfg.numOfSupportVnodes = cfgGetItem(pCfg, "supportVnodes")->i32; - tstrncpy(objCfg.dataDir, cfgGetItem(pCfg, "dataDir")->str, sizeof(objCfg.dataDir)); - tstrncpy(objCfg.firstEp, cfgGetItem(pCfg, "firstEp")->str, sizeof(objCfg.firstEp)); - tstrncpy(objCfg.secondEp, cfgGetItem(pCfg, "secondEp")->str, sizeof(objCfg.firstEp)); - objCfg.serverPort = (uint16_t)cfgGetItem(pCfg, "serverPort")->i32; - tstrncpy(objCfg.localFqdn, cfgGetItem(pCfg, "fqdn")->str, sizeof(objCfg.localFqdn)); + tstrncpy(objCfg.dataDir, tsDataDir, sizeof(objCfg.dataDir)); + tstrncpy(objCfg.firstEp, tsFirst, sizeof(objCfg.firstEp)); + tstrncpy(objCfg.secondEp, tsSecond, sizeof(objCfg.firstEp)); + objCfg.serverPort = tsServerPort; + tstrncpy(objCfg.localFqdn, tsLocalFqdn, sizeof(objCfg.localFqdn)); snprintf(objCfg.localEp, sizeof(objCfg.localEp), "%s:%u", objCfg.localFqdn, objCfg.serverPort); objCfg.pDisks = tsDiskCfg; objCfg.numOfDisks = tsDiskCfgNum; diff --git a/source/dnode/mnode/impl/src/mndUser.c b/source/dnode/mnode/impl/src/mndUser.c index 7cdae79c0d..b1cdf484b4 100644 --- a/source/dnode/mnode/impl/src/mndUser.c +++ b/source/dnode/mnode/impl/src/mndUser.c @@ -19,7 +19,7 @@ #include "mndDb.h" #include "mndShow.h" #include "mndTrans.h" -#include "tkey.h" +#include "tbase64.h" #define TSDB_USER_VER_NUMBER 1 #define TSDB_USER_RESERVE_SIZE 64 diff --git a/source/dnode/mnode/impl/test/db/db.cpp b/source/dnode/mnode/impl/test/db/db.cpp index a3efad8aa2..17fda48cd7 100644 --- a/source/dnode/mnode/impl/test/db/db.cpp +++ b/source/dnode/mnode/impl/test/db/db.cpp @@ -119,8 +119,8 @@ TEST_F(MndTestDb, 02_Create_Alter_Drop_Db) { EXPECT_EQ(test.GetShowRows(), 2); CheckInt32(2); CheckInt32(3); - CheckInt32(0); - CheckInt32(0); + IgnoreInt32(); + IgnoreInt32(); CheckInt16(1); CheckInt16(1); CheckBinary("master", 9); diff --git a/source/dnode/vnode/inc/meta.h b/source/dnode/vnode/inc/meta.h index 44a352ec54..c98a97d0dc 100644 --- a/source/dnode/vnode/inc/meta.h +++ b/source/dnode/vnode/inc/meta.h @@ -16,8 +16,7 @@ #ifndef _TD_META_H_ #define _TD_META_H_ -#include "mallocator.h" -#include "os.h" +#include "tmallocator.h" #include "tmsg.h" #include "trow.h" diff --git a/source/dnode/vnode/inc/tq.h b/source/dnode/vnode/inc/tq.h index a516f423bb..f4d3b405d9 100644 --- a/source/dnode/vnode/inc/tq.h +++ b/source/dnode/vnode/inc/tq.h @@ -18,9 +18,8 @@ #include "common.h" #include "executor.h" -#include "mallocator.h" +#include "tmallocator.h" #include "meta.h" -#include "os.h" #include "scheduler.h" #include "taoserror.h" #include "tlist.h" diff --git a/source/dnode/vnode/inc/tsdb.h b/source/dnode/vnode/inc/tsdb.h index 7bdd8fc266..d578c8b12a 100644 --- a/source/dnode/vnode/inc/tsdb.h +++ b/source/dnode/vnode/inc/tsdb.h @@ -16,7 +16,7 @@ #ifndef _TD_TSDB_H_ #define _TD_TSDB_H_ -#include "mallocator.h" +#include "tmallocator.h" #include "meta.h" #include "common.h" #include "tfs.h" diff --git a/source/dnode/vnode/src/inc/metaDef.h b/source/dnode/vnode/src/inc/metaDef.h index b76c08b7b4..f4128f7170 100644 --- a/source/dnode/vnode/src/inc/metaDef.h +++ b/source/dnode/vnode/src/inc/metaDef.h @@ -16,7 +16,7 @@ #ifndef _TD_META_DEF_H_ #define _TD_META_DEF_H_ -#include "mallocator.h" +#include "tmallocator.h" #include "meta.h" diff --git a/source/dnode/vnode/src/inc/tsdbDef.h b/source/dnode/vnode/src/inc/tsdbDef.h index 55e16218ad..98c0de32a8 100644 --- a/source/dnode/vnode/src/inc/tsdbDef.h +++ b/source/dnode/vnode/src/inc/tsdbDef.h @@ -16,7 +16,7 @@ #ifndef _TD_TSDB_DEF_H_ #define _TD_TSDB_DEF_H_ -#include "mallocator.h" +#include "tmallocator.h" #include "meta.h" #include "tcompression.h" #include "tglobal.h" diff --git a/source/dnode/vnode/src/inc/vnd.h b/source/dnode/vnode/src/inc/vnd.h index fb81ddbc5c..75088687a8 100644 --- a/source/dnode/vnode/src/inc/vnd.h +++ b/source/dnode/vnode/src/inc/vnd.h @@ -16,7 +16,7 @@ #ifndef _TD_VNODE_DEF_H_ #define _TD_VNODE_DEF_H_ -#include "mallocator.h" +#include "tmallocator.h" // #include "sync.h" #include "tcoding.h" #include "tfs.h" diff --git a/source/dnode/vnode/src/tsdb/tsdbRead.c b/source/dnode/vnode/src/tsdb/tsdbRead.c index 653e1b6380..c1cf776cb5 100644 --- a/source/dnode/vnode/src/tsdb/tsdbRead.c +++ b/source/dnode/vnode/src/tsdb/tsdbRead.c @@ -19,7 +19,7 @@ #include "tsdbLog.h" #include "tsdbReadImpl.h" #include "ttime.h" -#include "exception.h" +#include "texception.h" #include "os.h" #include "talgo.h" #include "tcompare.h" diff --git a/source/libs/executor/src/executorMain.c b/source/libs/executor/src/executorMain.c index f632a84c1f..1642120d15 100644 --- a/source/libs/executor/src/executorMain.c +++ b/source/libs/executor/src/executorMain.c @@ -15,7 +15,7 @@ #include #include "dataSinkMgt.h" -#include "exception.h" +#include "texception.h" #include "os.h" #include "tarray.h" #include "tcache.h" diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index a4a09c91fe..c400ded1c1 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -12,11 +12,12 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ + #include "os.h" #include "tep.h" #include "tsort.h" -#include "exception.h" +#include "texception.h" #include "parser.h" #include "tglobal.h" #include "tmsg.h" diff --git a/source/libs/executor/src/tlinearhash.c b/source/libs/executor/src/tlinearhash.c index 3a58253d81..7f3da9bcac 100644 --- a/source/libs/executor/src/tlinearhash.c +++ b/source/libs/executor/src/tlinearhash.c @@ -14,7 +14,7 @@ */ #include "tlinearhash.h" -#include "tcfg.h" +#include "tdef.h" #include "taoserror.h" #include "tpagedbuf.h" diff --git a/source/libs/executor/src/tsort.c b/source/libs/executor/src/tsort.c index 6e7b6b4659..e75ca069c5 100644 --- a/source/libs/executor/src/tsort.c +++ b/source/libs/executor/src/tsort.c @@ -18,7 +18,7 @@ #include "tsort.h" #include "tep.h" -#include "tcfg.h" +#include "tdef.h" #include "tlosertree.h" #include "tpagedbuf.h" #include "tutil.h" diff --git a/source/libs/function/src/texpr.c b/source/libs/function/src/texpr.c index 7d9cb97400..bede8b80fd 100644 --- a/source/libs/function/src/texpr.c +++ b/source/libs/function/src/texpr.c @@ -16,7 +16,7 @@ #include "function.h" #include "os.h" -#include "exception.h" +#include "texception.h" #include "taosdef.h" #include "tmsg.h" #include "tarray.h" diff --git a/source/libs/function/src/tpercentile.c b/source/libs/function/src/tpercentile.c index 27cdc15d94..1aa0c04ec4 100644 --- a/source/libs/function/src/tpercentile.c +++ b/source/libs/function/src/tpercentile.c @@ -12,8 +12,9 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#include -#include "os.h" + +#include "tglobal.h" +#include "tcompare.h" #include "taosdef.h" #include "tcompare.h" diff --git a/source/libs/index/inc/indexInt.h b/source/libs/index/inc/indexInt.h index 90ad1e15f4..59ed5b79f5 100644 --- a/source/libs/index/inc/indexInt.h +++ b/source/libs/index/inc/indexInt.h @@ -16,12 +16,15 @@ #ifndef _TD_INDEX_INT_H_ #define _TD_INDEX_INT_H_ +#include "os.h" + #include "index.h" -#include "index_fst.h" #include "taos.h" +#include "tarray.h" #include "tchecksum.h" #include "thash.h" #include "tlog.h" +#include "tutil.h" #ifdef USE_LUCENE #include diff --git a/source/libs/index/inc/index_cache.h b/source/libs/index/inc/index_cache.h index 9db913debf..1445a1bc56 100644 --- a/source/libs/index/inc/index_cache.h +++ b/source/libs/index/inc/index_cache.h @@ -15,10 +15,9 @@ #ifndef __INDEX_CACHE_H__ #define __INDEX_CACHE_H__ -#include "index.h" #include "indexInt.h" -#include "tlockfree.h" #include "tskiplist.h" + // ----------------- key structure in skiplist --------------------- /* A data row, the format is like below diff --git a/source/libs/index/inc/index_fst.h b/source/libs/index/inc/index_fst.h index 072ec93e4e..5da0dc537b 100644 --- a/source/libs/index/inc/index_fst.h +++ b/source/libs/index/inc/index_fst.h @@ -20,11 +20,12 @@ extern "C" { #endif +#include "indexInt.h" +#include "index_fst_node.h" #include "index_fst_automation.h" #include "index_fst_counting_writer.h" #include "index_fst_registry.h" #include "index_fst_util.h" -#include "tarray.h" #define OUTPUT_PREFIX(a, b) ((a) > (b) ? (b) : (a) diff --git a/source/libs/index/inc/index_fst_automation.h b/source/libs/index/inc/index_fst_automation.h index be056f38fa..786d3eb7d2 100644 --- a/source/libs/index/inc/index_fst_automation.h +++ b/source/libs/index/inc/index_fst_automation.h @@ -19,6 +19,7 @@ extern "C" { #endif +#include "indexInt.h" #include "index_fst_util.h" typedef struct AutomationCtx AutomationCtx; diff --git a/source/libs/index/inc/index_fst_counting_writer.h b/source/libs/index/inc/index_fst_counting_writer.h index 86e829aa95..f8a6246723 100644 --- a/source/libs/index/inc/index_fst_counting_writer.h +++ b/source/libs/index/inc/index_fst_counting_writer.h @@ -16,12 +16,12 @@ #ifndef __INDEX_FST_COUNTING_WRITER_H__ #define __INDEX_FST_COUNTING_WRITER_H__ +#include "indexInt.h" + #ifdef __cplusplus extern "C" { #endif -#include "tfile.h" - //#define USE_MMAP 1 #define DefaultMem 1024 * 1024 diff --git a/source/libs/index/inc/index_fst_node.h b/source/libs/index/inc/index_fst_node.h index 32de7ea621..4dbf4cea04 100644 --- a/source/libs/index/inc/index_fst_node.h +++ b/source/libs/index/inc/index_fst_node.h @@ -20,6 +20,7 @@ extern "C" { #endif +#include "indexInt.h" #include "index_fst_counting_writer.h" #include "index_fst_util.h" diff --git a/source/libs/index/inc/index_fst_registry.h b/source/libs/index/inc/index_fst_registry.h index f94a21d8dd..20b70e2866 100644 --- a/source/libs/index/inc/index_fst_registry.h +++ b/source/libs/index/inc/index_fst_registry.h @@ -19,9 +19,9 @@ extern "C" { #endif +#include "indexInt.h" #include "index_fst_node.h" #include "index_fst_util.h" -#include "tarray.h" typedef struct FstRegistryCell { CompiledAddr addr; diff --git a/source/libs/index/inc/index_fst_util.h b/source/libs/index/inc/index_fst_util.h index 71a378584d..f173dd9a15 100644 --- a/source/libs/index/inc/index_fst_util.h +++ b/source/libs/index/inc/index_fst_util.h @@ -20,8 +20,8 @@ extern "C" { #endif +#include "indexInt.h" #include "index_fst_common.h" -#include "tarray.h" typedef uint64_t FstType; typedef uint64_t CompiledAddr; diff --git a/source/libs/index/inc/index_tfile.h b/source/libs/index/inc/index_tfile.h index 4618a39197..f676651e52 100644 --- a/source/libs/index/inc/index_tfile.h +++ b/source/libs/index/inc/index_tfile.h @@ -15,7 +15,6 @@ #ifndef __INDEX_TFILE_H__ #define __INDEX_TFILE_H__ -#include "index.h" #include "indexInt.h" #include "index_fst.h" #include "index_fst_counting_writer.h" diff --git a/source/libs/index/inc/index_util.h b/source/libs/index/inc/index_util.h index 36830a68bc..985dd657d0 100644 --- a/source/libs/index/inc/index_util.h +++ b/source/libs/index/inc/index_util.h @@ -15,7 +15,7 @@ #ifndef __INDEX_UTIL_H__ #define __INDEX_UTIL_H__ -#include "tarray.h" +#include "indexInt.h" #ifdef __cplusplus extern "C" { diff --git a/source/libs/index/src/index_fst_counting_writer.c b/source/libs/index/src/index_fst_counting_writer.c index 78c7a8d193..16d9893778 100644 --- a/source/libs/index/src/index_fst_counting_writer.c +++ b/source/libs/index/src/index_fst_counting_writer.c @@ -12,6 +12,8 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ + +#include "os.h" #include "index_fst_counting_writer.h" #include "indexInt.h" #include "index_fst_util.h" diff --git a/source/libs/index/src/index_fst_registry.c b/source/libs/index/src/index_fst_registry.c index 07afc52ecf..b28b518fc1 100644 --- a/source/libs/index/src/index_fst_registry.c +++ b/source/libs/index/src/index_fst_registry.c @@ -13,6 +13,7 @@ * along with this program. If not, see . */ +#include "os.h" #include "index_fst_registry.h" uint64_t fstRegistryHash(FstRegistry* registry, FstBuilderNode* bNode) { diff --git a/source/libs/index/test/fstTest.cc b/source/libs/index/test/fstTest.cc index 65118a2bce..698945ae14 100644 --- a/source/libs/index/test/fstTest.cc +++ b/source/libs/index/test/fstTest.cc @@ -49,7 +49,6 @@ class FstWriter { class FstReadMemory { public: FstReadMemory(size_t size, const std::string& fileName = "/tmp/tindex.tindex") { - tfInit(); _wc = writerCtxCreate(TFile, fileName.c_str(), true, 64 * 1024); _w = fstCountingWriterCreate(_wc); _size = size; @@ -116,7 +115,6 @@ class FstReadMemory { fstDestroy(_fst); fstSliceDestroy(&_s); writerCtxDestroy(_wc, false); - tfCleanup(); } private: @@ -170,7 +168,6 @@ void Performance_fstReadRecords(FstReadMemory* m) { } void checkMillonWriteAndReadOfFst() { - tfInit(); FstWriter* fw = new FstWriter; Performance_fstWriteRecords(fw); delete fw; @@ -181,11 +178,9 @@ void checkMillonWriteAndReadOfFst() { } Performance_fstReadRecords(fr); - tfCleanup(); delete fr; } void checkFstLongTerm() { - tfInit(); FstWriter* fw = new FstWriter; // Performance_fstWriteRecords(fw); @@ -235,12 +230,10 @@ void checkFstLongTerm() { // for (int i = 0; i < result.size(); i++) { // assert(result[i] == i); // check result //} - tfCleanup(); // free(ctx); // delete m; } void checkFstCheckIterator() { - tfInit(); FstWriter* fw = new FstWriter; int64_t s = taosGetTimestampUs(); int count = 2; @@ -275,7 +268,6 @@ void checkFstCheckIterator() { free(ctx); delete m; - tfCleanup(); } void fst_get(Fst* fst) { @@ -294,8 +286,6 @@ void fst_get(Fst* fst) { #define NUM_OF_THREAD 10 void validateTFile(char* arg) { - tfInit(); - std::thread threads[NUM_OF_THREAD]; // std::vector threads; TFileReader* reader = tfileReaderOpen(arg, 0, 20000000, "tag1"); @@ -309,12 +299,9 @@ void validateTFile(char* arg) { // wait join threads[i].join(); } - tfCleanup(); } void iterTFileReader(char* path, char* ver) { - tfInit(); - int version = atoi(ver); TFileReader* reader = tfileReaderOpen(path, 0, version, "tag1"); Iterate* iter = tfileIteratorCreate(reader); @@ -331,7 +318,6 @@ void iterTFileReader(char* path, char* ver) { printf("total size: %d\n term count: %d\n", count, termCount); tfileIteratorDestroy(iter); - tfCleanup(); } int main(int argc, char* argv[]) { diff --git a/source/libs/index/test/fstUT.cc b/source/libs/index/test/fstUT.cc index 9665198b3b..2d3d2f8894 100644 --- a/source/libs/index/test/fstUT.cc +++ b/source/libs/index/test/fstUT.cc @@ -24,8 +24,6 @@ static char tindex[PATH_MAX] = {0}; static char tindexDir[PATH_MAX] = {0}; static void EnvInit() { - tfInit(); - std::string path = dir; taosRemoveDir(path.c_str()); taosMkDir(path.c_str()); @@ -136,7 +134,6 @@ class FstReadMemory { fstDestroy(_fst); fstSliceDestroy(&_s); writerCtxDestroy(_wc, false); - // tfCleanup(); } private: diff --git a/source/libs/index/test/indexTests.cc b/source/libs/index/test/indexTests.cc index bbcc654ae2..a50e91b094 100644 --- a/source/libs/index/test/indexTests.cc +++ b/source/libs/index/test/indexTests.cc @@ -420,7 +420,6 @@ class IndexTFileEnv : public ::testing::Test { virtual void SetUp() { taosRemoveDir(dir.c_str()); taosMkDir(dir.c_str()); - tfInit(); fObj = new TFileObj(dir, colName); } @@ -428,7 +427,6 @@ class IndexTFileEnv : public ::testing::Test { // indexClose(index); // indexeptsDestroy(opts); delete fObj; - tfCleanup(); // tfileWriterDestroy(twrite); } TFileObj* fObj; @@ -800,13 +798,10 @@ class IndexObj { class IndexEnv2 : public ::testing::Test { protected: virtual void SetUp() { - tfInit(); index = new IndexObj(); - // } virtual void TearDown() { delete index; - tfCleanup(); } IndexObj* index; }; diff --git a/source/libs/planner/src/physicalPlan.c b/source/libs/planner/src/physicalPlan.c index eadc95b98d..748be8fcd8 100644 --- a/source/libs/planner/src/physicalPlan.c +++ b/source/libs/planner/src/physicalPlan.c @@ -14,7 +14,7 @@ */ #include "plannerInt.h" -#include "exception.h" +#include "texception.h" #include "parser.h" #define STORE_CURRENT_SUBPLAN(cxt) SSubplan* _ = cxt->pCurrentSubplan diff --git a/source/libs/wal/inc/walInt.h b/source/libs/wal/inc/walInt.h index 4624e05f10..ca0c756141 100644 --- a/source/libs/wal/inc/walInt.h +++ b/source/libs/wal/inc/walInt.h @@ -16,7 +16,7 @@ #ifndef _TD_WAL_INT_H_ #define _TD_WAL_INT_H_ -#include "compare.h" +#include "tcompare.h" #include "taoserror.h" #include "tchecksum.h" #include "tcoding.h" diff --git a/source/libs/wal/src/walMgmt.c b/source/libs/wal/src/walMgmt.c index d3cd23f284..b1caeab3b1 100644 --- a/source/libs/wal/src/walMgmt.c +++ b/source/libs/wal/src/walMgmt.c @@ -14,10 +14,9 @@ */ #define _DEFAULT_SOURCE -#include "compare.h" +#include "tcompare.h" #include "os.h" #include "taoserror.h" -#include "tfile.h" #include "tref.h" #include "walInt.h" @@ -40,15 +39,9 @@ int32_t walInit() { int8_t old = atomic_val_compare_exchange_8(&tsWal.inited, 0, 1); if (old == 1) return 0; - int code = tfInit(); - if (code != 0) { - wError("failed to init tfile since %s", tstrerror(code)); - atomic_store_8(&tsWal.inited, 0); - return code; - } tsWal.refSetId = taosOpenRef(TSDB_MIN_VNODES, walFreeObj); - code = walCreateThread(); + int32_t code = walCreateThread(); if (code != 0) { wError("failed to init wal module since %s", tstrerror(code)); atomic_store_8(&tsWal.inited, 0); diff --git a/source/libs/wal/src/walRead.c b/source/libs/wal/src/walRead.c index 159d281759..9e1ffeae7f 100644 --- a/source/libs/wal/src/walRead.c +++ b/source/libs/wal/src/walRead.c @@ -13,7 +13,6 @@ * along with this program. If not, see . */ -#include "tfile.h" #include "walInt.h" #include "taoserror.h" diff --git a/source/libs/wal/src/walSeek.c b/source/libs/wal/src/walSeek.c index 6b3abcd0f9..140b7ddc32 100644 --- a/source/libs/wal/src/walSeek.c +++ b/source/libs/wal/src/walSeek.c @@ -16,7 +16,6 @@ #define _DEFAULT_SOURCE #include "os.h" #include "taoserror.h" -#include "tfile.h" #include "tref.h" #include "walInt.h" diff --git a/source/libs/wal/src/walWrite.c b/source/libs/wal/src/walWrite.c index ba07134003..4b1f0ba306 100644 --- a/source/libs/wal/src/walWrite.c +++ b/source/libs/wal/src/walWrite.c @@ -18,7 +18,6 @@ #include "os.h" #include "taoserror.h" #include "tchecksum.h" -#include "tfile.h" #include "walInt.h" int32_t walCommit(SWal *pWal, int64_t ver) { diff --git a/source/os/src/osTimezone.c b/source/os/src/osTimezone.c index 07846781ad..68da2ce25e 100644 --- a/source/os/src/osTimezone.c +++ b/source/os/src/osTimezone.c @@ -46,6 +46,8 @@ #endif void taosSetSystemTimezone(const char *inTimezone, char *outTimezone, int8_t *outDaylight) { + if (inTimezone == NULL || inTimezone[0] == 0) return; + #ifdef WINDOWS char winStr[TD_LOCALE_LEN * 2]; sprintf(winStr, "TZ=%s", inTimezone); diff --git a/source/util/src/exception.c b/source/util/src/exception.c deleted file mode 100644 index 9740b9031b..0000000000 --- a/source/util/src/exception.c +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#include "os.h" -#include "exception.h" - -static threadlocal SExceptionNode* expList; - -void exceptionPushNode( SExceptionNode* node ) { - node->prev = expList; - expList = node; -} - -int32_t exceptionPopNode() { - SExceptionNode* node = expList; - expList = node->prev; - return node->code; -} - -void exceptionThrow( int32_t code ) { - expList->code = code; - longjmp( expList->jb, 1 ); -} - - - -static void cleanupWrapper_void_ptr_ptr( SCleanupAction* ca ) { - void (*func)( void*, void* ) = ca->func; - func( ca->arg1.Ptr, ca->arg2.Ptr ); -} - -static void cleanupWrapper_void_ptr_bool( SCleanupAction* ca ) { - void (*func)( void*, bool ) = ca->func; - func( ca->arg1.Ptr, ca->arg2.Bool ); -} - -static void cleanupWrapper_void_ptr( SCleanupAction* ca ) { - void (*func)( void* ) = ca->func; - func( ca->arg1.Ptr ); -} - -static void cleanupWrapper_int_int( SCleanupAction* ca ) { - int (*func)( int ) = ca->func; - func( ca->arg1.Int ); -} - -static void cleanupWrapper_void( SCleanupAction* ca ) { - void (*func)() = ca->func; - func(); -} - -static void cleanupWrapper_int_ptr( SCleanupAction* ca ) { - int (*func)( void* ) = ca->func; - func( ca->arg1.Ptr ); -} - -typedef void (*wrapper)(SCleanupAction*); -static wrapper wrappers[] = { - cleanupWrapper_void_ptr_ptr, - cleanupWrapper_void_ptr_bool, - cleanupWrapper_void_ptr, - cleanupWrapper_int_int, - cleanupWrapper_void, - cleanupWrapper_int_ptr, -}; - - -void cleanupPush_void_ptr_ptr( bool failOnly, void* func, void* arg1, void* arg2 ) { - assert( expList->numCleanupAction < expList->maxCleanupAction ); - - SCleanupAction *ca = expList->cleanupActions + expList->numCleanupAction++; - ca->wrapper = 0; - ca->failOnly = failOnly; - ca->func = func; - ca->arg1.Ptr = arg1; - ca->arg2.Ptr = arg2; -} - -void cleanupPush_void_ptr_bool( bool failOnly, void* func, void* arg1, bool arg2 ) { - assert( expList->numCleanupAction < expList->maxCleanupAction ); - - SCleanupAction *ca = expList->cleanupActions + expList->numCleanupAction++; - ca->wrapper = 1; - ca->failOnly = failOnly; - ca->func = func; - ca->arg1.Ptr = arg1; - ca->arg2.Bool = arg2; -} - -void cleanupPush_void_ptr( bool failOnly, void* func, void* arg ) { - assert( expList->numCleanupAction < expList->maxCleanupAction ); - - SCleanupAction *ca = expList->cleanupActions + expList->numCleanupAction++; - ca->wrapper = 2; - ca->failOnly = failOnly; - ca->func = func; - ca->arg1.Ptr = arg; -} - -void cleanupPush_int_int( bool failOnly, void* func, int arg ) { - assert( expList->numCleanupAction < expList->maxCleanupAction ); - - SCleanupAction *ca = expList->cleanupActions + expList->numCleanupAction++; - ca->wrapper = 3; - ca->failOnly = failOnly; - ca->func = func; - ca->arg1.Int = arg; -} - -void cleanupPush_void( bool failOnly, void* func ) { - assert( expList->numCleanupAction < expList->maxCleanupAction ); - - SCleanupAction *ca = expList->cleanupActions + expList->numCleanupAction++; - ca->wrapper = 4; - ca->failOnly = failOnly; - ca->func = func; -} - -void cleanupPush_int_ptr( bool failOnly, void* func, void* arg ) { - assert( expList->numCleanupAction < expList->maxCleanupAction ); - - SCleanupAction *ca = expList->cleanupActions + expList->numCleanupAction++; - ca->wrapper = 5; - ca->failOnly = failOnly; - ca->func = func; - ca->arg1.Ptr = arg; -} - - -int32_t cleanupGetActionCount() { - return expList->numCleanupAction; -} - - -static void doExecuteCleanup( SExceptionNode* node, int32_t anchor, bool failed ) { - while( node->numCleanupAction > anchor ) { - --node->numCleanupAction; - SCleanupAction *ca = node->cleanupActions + node->numCleanupAction; - if( failed || !(ca->failOnly) ) { - wrappers[ca->wrapper]( ca ); - } - } -} - -void cleanupExecuteTo( int32_t anchor, bool failed ) { - doExecuteCleanup( expList, anchor, failed ); -} - -void cleanupExecute( SExceptionNode* node, bool failed ) { - doExecuteCleanup( node, 0, failed ); -} -bool cleanupExceedLimit() { - return expList->numCleanupAction >= expList->maxCleanupAction; -} diff --git a/source/util/src/talgo.c b/source/util/src/talgo.c index f8520b0742..cb0a2d25a9 100644 --- a/source/util/src/talgo.c +++ b/source/util/src/talgo.c @@ -13,30 +13,33 @@ * along with this program. If not, see . */ -#include "os.h" +#define _DEFAULT_SOURCE #include "talgo.h" -#define doswap(__left, __right, __size, __buf) do {\ - memcpy((__buf), (__left), (__size));\ - memcpy((__left), (__right),(__size));\ - memcpy((__right), (__buf), (__size));\ -} while (0); +#define doswap(__left, __right, __size, __buf) \ + do { \ + memcpy((__buf), (__left), (__size)); \ + memcpy((__left), (__right), (__size)); \ + memcpy((__right), (__buf), (__size)); \ + } while (0); -static void median(void *src, size_t size, size_t s, size_t e, const void *param, __ext_compar_fn_t comparFn, void* buf) { +static void median(void *src, int64_t size, int64_t s, int64_t e, const void *param, __ext_compar_fn_t comparFn, + void *buf) { int32_t mid = ((int32_t)(e - s) >> 1u) + (int32_t)s; - + if (comparFn(elePtrAt(src, size, mid), elePtrAt(src, size, s), param) == 1) { doswap(elePtrAt(src, size, mid), elePtrAt(src, size, s), size, buf); } - + if (comparFn(elePtrAt(src, size, mid), elePtrAt(src, size, e), param) == 1) { doswap(elePtrAt(src, size, mid), elePtrAt(src, size, s), size, buf); doswap(elePtrAt(src, size, mid), elePtrAt(src, size, e), size, buf); } else if (comparFn(elePtrAt(src, size, s), elePtrAt(src, size, e), param) == 1) { doswap(elePtrAt(src, size, s), elePtrAt(src, size, e), size, buf); } - - assert(comparFn(elePtrAt(src, size, mid), elePtrAt(src, size, s), param) <= 0 && comparFn(elePtrAt(src, size, s), elePtrAt(src, size, e), param) <= 0); + + assert(comparFn(elePtrAt(src, size, mid), elePtrAt(src, size, s), param) <= 0 && + comparFn(elePtrAt(src, size, s), elePtrAt(src, size, e), param) <= 0); #ifdef _DEBUG_VIEW // tTagsPrints(src[s], pOrderDesc->pColumnModel, &pOrderDesc->orderIdx); @@ -45,8 +48,8 @@ static void median(void *src, size_t size, size_t s, size_t e, const void *param #endif } -static void tInsertSort(void *src, size_t size, int32_t s, int32_t e, const void *param, __ext_compar_fn_t comparFn, - void* buf) { +static void tInsertSort(void *src, int64_t size, int32_t s, int32_t e, const void *param, __ext_compar_fn_t comparFn, + void *buf) { for (int32_t i = s + 1; i <= e; ++i) { for (int32_t j = i; j > s; --j) { if (comparFn(elePtrAt(src, size, j), elePtrAt(src, size, j - 1), param) == -1) { @@ -58,117 +61,117 @@ static void tInsertSort(void *src, size_t size, int32_t s, int32_t e, const void } } -static void tqsortImpl(void *src, int32_t start, int32_t end, size_t size, const void *param, __ext_compar_fn_t comparFn, - void* buf) { +static void tqsortImpl(void *src, int32_t start, int32_t end, int64_t size, const void *param, + __ext_compar_fn_t comparFn, void *buf) { // short array sort, incur another sort procedure instead of quick sort process const int32_t THRESHOLD_SIZE = 6; if (end - start + 1 <= THRESHOLD_SIZE) { tInsertSort(src, size, start, end, param, comparFn, buf); return; } - + median(src, size, start, end, param, comparFn, buf); - + int32_t s = start, e = end; int32_t endRightS = end, startLeftS = start; - + while (s < e) { while (e > s) { int32_t ret = comparFn(elePtrAt(src, size, e), elePtrAt(src, size, s), param); if (ret < 0) { break; } - - //move the data that equals to pivotal value to the right end of the list + + // move the data that equals to pivotal value to the right end of the list if (ret == 0 && e != endRightS) { doswap(elePtrAt(src, size, e), elePtrAt(src, size, endRightS), size, buf); endRightS--; } - + e--; } - + if (e != s) { doswap(elePtrAt(src, size, e), elePtrAt(src, size, s), size, buf); } - + while (s < e) { int32_t ret = comparFn(elePtrAt(src, size, s), elePtrAt(src, size, e), param); if (ret > 0) { break; } - + if (ret == 0 && s != startLeftS) { doswap(elePtrAt(src, size, s), elePtrAt(src, size, startLeftS), size, buf); startLeftS++; } s++; } - + if (e != s) { doswap(elePtrAt(src, size, s), elePtrAt(src, size, e), size, buf); } } - + int32_t rightPartStart = e + 1; if (endRightS != end && e < end) { int32_t left = rightPartStart; int32_t right = end; - + while (right > endRightS && left <= endRightS) { doswap(elePtrAt(src, size, left), elePtrAt(src, size, right), size, buf); - + left++; right--; } - + rightPartStart += (end - endRightS); } - + int32_t leftPartEnd = e - 1; if (startLeftS != end && s > start) { int32_t left = start; int32_t right = leftPartEnd; - + while (left < startLeftS && right >= startLeftS) { doswap(elePtrAt(src, size, left), elePtrAt(src, size, right), size, buf); - + left++; right--; } - + leftPartEnd -= (startLeftS - start); } - + if (leftPartEnd > start) { tqsortImpl(src, start, leftPartEnd, size, param, comparFn, buf); } - + if (rightPartStart < end) { tqsortImpl(src, rightPartStart, end, size, param, comparFn, buf); } } -void taosqsort(void *src, size_t numOfElem, size_t size, const void* param, __ext_compar_fn_t comparFn) { - char *buf = calloc(1, size); // prepare the swap buffer +void taosqsort(void *src, int64_t numOfElem, int64_t size, const void *param, __ext_compar_fn_t comparFn) { + char *buf = calloc(1, size); // prepare the swap buffer tqsortImpl(src, 0, (int32_t)numOfElem - 1, (int32_t)size, param, comparFn, buf); tfree(buf); } -void * taosbsearch(const void *key, const void *base, size_t nmemb, size_t size, __compar_fn_t compar, int flags) { +void *taosbsearch(const void *key, const void *base, int64_t nmemb, int64_t size, __compar_fn_t compar, int32_t flags) { // TODO: need to check the correctness of this function - int l = 0; - int r = (int)nmemb; - int idx = 0; - int comparison; - + int32_t l = 0; + int32_t r = (int32_t)nmemb; + int32_t idx = 0; + int32_t comparison; + if (flags == TD_EQ) { return bsearch(key, base, nmemb, size, compar); } else if (flags == TD_GE) { if (nmemb <= 0) return NULL; if ((*compar)(key, elePtrAt(base, size, 0)) <= 0) return elePtrAt(base, size, 0); if ((*compar)(key, elePtrAt(base, size, nmemb - 1)) > 0) return NULL; - + while (l < r) { idx = (l + r) / 2; comparison = (*compar)(key, elePtrAt(base, size, idx)); @@ -180,7 +183,7 @@ void * taosbsearch(const void *key, const void *base, size_t nmemb, size_t size, return elePtrAt(base, size, idx); } } - + if ((*compar)(key, elePtrAt(base, size, idx)) < 0) { return elePtrAt(base, size, idx); } else { @@ -194,7 +197,7 @@ void * taosbsearch(const void *key, const void *base, size_t nmemb, size_t size, if (nmemb <= 0) return NULL; if ((*compar)(key, elePtrAt(base, size, nmemb - 1)) >= 0) return elePtrAt(base, size, nmemb - 1); if ((*compar)(key, elePtrAt(base, size, 0)) < 0) return NULL; - + while (l < r) { idx = (l + r) / 2; comparison = (*compar)(key, elePtrAt(base, size, idx)); @@ -206,7 +209,7 @@ void * taosbsearch(const void *key, const void *base, size_t nmemb, size_t size, return elePtrAt(base, size, idx); } } - + if ((*compar)(key, elePtrAt(base, size, idx)) > 0) { return elePtrAt(base, size, idx); } else { @@ -216,20 +219,20 @@ void * taosbsearch(const void *key, const void *base, size_t nmemb, size_t size, return elePtrAt(base, size, idx - 1); } } - + } else { assert(0); return NULL; } - + return NULL; } -void taosheapadjust(void *base, int32_t size, int32_t start, int32_t end, const void *parcompar, __ext_compar_fn_t compar, const void *parswap, __ext_swap_fn_t swap, bool maxroot) -{ - int32_t parent; - int32_t child; - char *buf; +void taosheapadjust(void *base, int32_t size, int32_t start, int32_t end, const void *parcompar, + __ext_compar_fn_t compar, const void *parswap, __ext_swap_fn_t swap, bool maxroot) { + int32_t parent; + int32_t child; + char *buf; if (base && size > 0 && compar) { parent = start; @@ -244,7 +247,8 @@ void taosheapadjust(void *base, int32_t size, int32_t start, int32_t end, const if (maxroot) { while (child <= end) { - if (child + 1 <= end && (*compar)(elePtrAt(base, size, child), elePtrAt(base, size, child + 1), parcompar) < 0) { + if (child + 1 <= end && + (*compar)(elePtrAt(base, size, child), elePtrAt(base, size, child + 1), parcompar) < 0) { child++; } @@ -263,7 +267,8 @@ void taosheapadjust(void *base, int32_t size, int32_t start, int32_t end, const } } else { while (child <= end) { - if (child + 1 <= end && (*compar)(elePtrAt(base, size, child), elePtrAt(base, size, child + 1), parcompar) > 0) { + if (child + 1 <= end && + (*compar)(elePtrAt(base, size, child), elePtrAt(base, size, child + 1), parcompar) > 0) { child++; } @@ -288,9 +293,9 @@ void taosheapadjust(void *base, int32_t size, int32_t start, int32_t end, const } } -void taosheapsort(void *base, int32_t size, int32_t len, const void *parcompar, __ext_compar_fn_t compar, const void *parswap, __ext_swap_fn_t swap, bool maxroot) -{ - int32_t i; +void taosheapsort(void *base, int32_t size, int32_t len, const void *parcompar, __ext_compar_fn_t compar, + const void *parswap, __ext_swap_fn_t swap, bool maxroot) { + int32_t i; if (base && size > 0) { for (i = len / 2 - 1; i >= 0; i--) { @@ -298,15 +303,14 @@ void taosheapsort(void *base, int32_t size, int32_t len, const void *parcompar, } } -/* - char *buf = calloc(1, size); + /* + char *buf = calloc(1, size); - for (i = len - 1; i > 0; i--) { - doswap(elePtrAt(base, size, 0), elePtrAt(base, size, i)); - taosheapadjust(base, size, 0, i - 1, parcompar, compar, parswap, swap, maxroot); - } + for (i = len - 1; i > 0; i--) { + doswap(elePtrAt(base, size, 0), elePtrAt(base, size, i)); + taosheapadjust(base, size, 0, i - 1, parcompar, compar, parswap, swap, maxroot); + } - tfree(buf); -*/ + tfree(buf); + */ } - diff --git a/source/util/src/tarray.c b/source/util/src/tarray.c index 3e6f97c0c3..1127a21255 100644 --- a/source/util/src/tarray.c +++ b/source/util/src/tarray.c @@ -13,9 +13,8 @@ * along with this program. If not, see . */ -#include "os.h" +#define _DEFAULT_SOURCE #include "tarray.h" -#include "talgo.h" SArray* taosArrayInit(size_t size, size_t elemSize) { assert(elemSize > 0); @@ -54,7 +53,7 @@ static int32_t taosArrayResize(SArray* pArray) { pArray->pData = tmp; pArray->capacity = size; - + return 0; } @@ -75,12 +74,12 @@ int32_t taosArrayEnsureCap(SArray* pArray, size_t newCap) { return 0; } -void* taosArrayAddBatch(SArray* pArray, const void* pData, int nEles) { +void* taosArrayAddBatch(SArray* pArray, const void* pData, int32_t nEles) { if (pArray == NULL || pData == NULL) { return NULL; } - if(taosArrayEnsureCap(pArray, pArray->size + nEles) != 0){ + if (taosArrayEnsureCap(pArray, pArray->size + nEles) != 0) { return NULL; } @@ -91,7 +90,7 @@ void* taosArrayAddBatch(SArray* pArray, const void* pData, int nEles) { return dst; } -void taosArrayRemoveBatch(SArray *pArray, const int32_t* pData, int32_t numOfElems) { +void taosArrayRemoveBatch(SArray* pArray, const int32_t* pData, int32_t numOfElems) { assert(pArray != NULL && pData != NULL); if (numOfElems <= 0) { return; @@ -104,7 +103,7 @@ void taosArrayRemoveBatch(SArray *pArray, const int32_t* pData, int32_t numOfEle } int32_t i = pData[0] + 1, j = 0; - while(i < size) { + while (i < size) { if (j == numOfElems - 1) { break; } @@ -133,7 +132,7 @@ void taosArrayRemoveBatch(SArray *pArray, const int32_t* pData, int32_t numOfEle pArray->size -= numOfElems; } -void taosArrayRemoveDuplicate(SArray *pArray, __compar_fn_t comparFn, void (*fp)(void*)) { +void taosArrayRemoveDuplicate(SArray* pArray, __compar_fn_t comparFn, void (*fp)(void*)) { assert(pArray); size_t size = pArray->size; @@ -142,7 +141,7 @@ void taosArrayRemoveDuplicate(SArray *pArray, __compar_fn_t comparFn, void (*fp) } int32_t pos = 0; - for(int32_t i = 1; i < size; ++i) { + for (int32_t i = 1; i < size; ++i) { char* p1 = taosArrayGet(pArray, pos); char* p2 = taosArrayGet(pArray, i); @@ -164,7 +163,7 @@ void taosArrayRemoveDuplicate(SArray *pArray, __compar_fn_t comparFn, void (*fp) } if (fp != NULL) { - for(int32_t i = pos + 1; i < pArray->size; ++i) { + for (int32_t i = pos + 1; i < pArray->size; ++i) { void* p = taosArrayGet(pArray, i); fp(p); } @@ -174,11 +173,11 @@ void taosArrayRemoveDuplicate(SArray *pArray, __compar_fn_t comparFn, void (*fp) } void* taosArrayAddAll(SArray* pArray, const SArray* pInput) { - return taosArrayAddBatch(pArray, pInput->pData, (int32_t) taosArrayGetSize(pInput)); + return taosArrayAddBatch(pArray, pInput->pData, (int32_t)taosArrayGetSize(pInput)); } void* taosArrayPop(SArray* pArray) { - assert( pArray != NULL ); + assert(pArray != NULL); if (pArray->size == 0) { return NULL; @@ -194,15 +193,13 @@ void* taosArrayGet(const SArray* pArray, size_t index) { void* taosArrayGetP(const SArray* pArray, size_t index) { assert(index < pArray->size); - + void* d = TARRAY_GET_ELEM(pArray, index); - + return *(void**)d; } -void* taosArrayGetLast(const SArray* pArray) { - return TARRAY_GET_ELEM(pArray, pArray->size - 1); -} +void* taosArrayGetLast(const SArray* pArray) { return TARRAY_GET_ELEM(pArray, pArray->size - 1); } size_t taosArrayGetSize(const SArray* pArray) { if (pArray == NULL) { @@ -227,7 +224,7 @@ void* taosArrayInsert(SArray* pArray, size_t index, void* pData) { if (pArray->size >= pArray->capacity) { int32_t ret = taosArrayResize(pArray); - + if (ret < 0) { return NULL; } @@ -240,7 +237,7 @@ void* taosArrayInsert(SArray* pArray, size_t index, void* pData) { memcpy(dst, pData, pArray->elemSize); pArray->size += 1; - + return dst; } @@ -252,7 +249,7 @@ void taosArraySet(SArray* pArray, size_t index, void* pData) { void taosArrayPopFrontBatch(SArray* pArray, size_t cnt) { assert(cnt <= pArray->size); pArray->size = pArray->size - cnt; - if(pArray->size == 0) { + if (pArray->size == 0) { return; } memmove(pArray->pData, (char*)pArray->pData + cnt * pArray->elemSize, pArray->size * pArray->elemSize); @@ -265,14 +262,15 @@ void taosArrayPopTailBatch(SArray* pArray, size_t cnt) { void taosArrayRemove(SArray* pArray, size_t index) { assert(index < pArray->size); - + if (index == pArray->size - 1) { taosArrayPop(pArray); return; } - + size_t remain = pArray->size - index - 1; - memmove((char*)pArray->pData + index * pArray->elemSize, (char*)pArray->pData + (index + 1) * pArray->elemSize, remain * pArray->elemSize); + memmove((char*)pArray->pData + index * pArray->elemSize, (char*)pArray->pData + (index + 1) * pArray->elemSize, + remain * pArray->elemSize); pArray->size -= 1; } @@ -288,13 +286,13 @@ SArray* taosArrayFromList(const void* src, size_t size, size_t elemSize) { SArray* taosArrayDup(const SArray* pSrc) { assert(pSrc != NULL); - - if (pSrc->size == 0) { // empty array list + + if (pSrc->size == 0) { // empty array list return taosArrayInit(8, pSrc->elemSize); } - + SArray* dst = taosArrayInit(pSrc->size, pSrc->elemSize); - + memcpy(dst->pData, pSrc->pData, pSrc->elemSize * pSrc->size); dst->size = pSrc->size; return dst; @@ -324,7 +322,7 @@ void taosArrayDestroyEx(SArray* pArray, void (*fp)(void*)) { return; } - for(int32_t i = 0; i < pArray->size; ++i) { + for (int32_t i = 0; i < pArray->size; ++i) { fp(TARRAY_GET_ELEM(pArray, i)); } @@ -338,14 +336,14 @@ void taosArraySort(SArray* pArray, __compar_fn_t compar) { qsort(pArray->pData, pArray->size, pArray->elemSize, compar); } -void* taosArraySearch(const SArray* pArray, const void* key, __compar_fn_t comparFn, int flags) { +void* taosArraySearch(const SArray* pArray, const void* key, __compar_fn_t comparFn, int32_t flags) { assert(pArray != NULL && comparFn != NULL); assert(key != NULL); return taosbsearch(key, pArray->pData, pArray->size, pArray->elemSize, comparFn, flags); } -int32_t taosArraySearchIdx(const SArray* pArray, const void* key, __compar_fn_t comparFn, int flags) { +int32_t taosArraySearchIdx(const SArray* pArray, const void* key, __compar_fn_t comparFn, int32_t flags) { void* item = taosArraySearch(pArray, key, comparFn, flags); return item == NULL ? -1 : (int32_t)((char*)item - (char*)pArray->pData) / pArray->elemSize; } @@ -355,7 +353,7 @@ void taosArraySortString(SArray* pArray, __compar_fn_t comparFn) { qsort(pArray->pData, pArray->size, pArray->elemSize, comparFn); } -char* taosArraySearchString(const SArray* pArray, const char* key, __compar_fn_t comparFn, int flags) { +char* taosArraySearchString(const SArray* pArray, const char* key, __compar_fn_t comparFn, int32_t flags) { assert(pArray != NULL); assert(key != NULL); @@ -366,47 +364,53 @@ char* taosArraySearchString(const SArray* pArray, const char* key, __compar_fn_t return *(char**)p; } -static int taosArrayPartition(SArray *pArray, int i, int j, __ext_compar_fn_t fn, const void *userData) { - void* key = taosArrayGetP(pArray, i); +static int32_t taosArrayPartition(SArray* pArray, int32_t i, int32_t j, __ext_compar_fn_t fn, const void* userData) { + void* key = taosArrayGetP(pArray, i); while (i < j) { - while (i < j && fn(taosArrayGetP(pArray, j), key, userData) >= 0) { j--; } - if (i < j) { - void *a = taosArrayGetP(pArray, j); + while (i < j && fn(taosArrayGetP(pArray, j), key, userData) >= 0) { + j--; + } + if (i < j) { + void* a = taosArrayGetP(pArray, j); taosArraySet(pArray, i, &a); } - while (i < j && fn(taosArrayGetP(pArray, i), key, userData) <= 0) { i++;} + while (i < j && fn(taosArrayGetP(pArray, i), key, userData) <= 0) { + i++; + } if (i < j) { - void *a = taosArrayGetP(pArray, i); + void* a = taosArrayGetP(pArray, i); taosArraySet(pArray, j, &a); } } - taosArraySet(pArray, i, &key); + taosArraySet(pArray, i, &key); return i; } -static void taosArrayQuicksortHelper(SArray *pArray, int low, int high, __ext_compar_fn_t fn, const void *param) { +static void taosArrayQuicksortHelper(SArray* pArray, int32_t low, int32_t high, __ext_compar_fn_t fn, + const void* param) { if (low < high) { - int idx = taosArrayPartition(pArray, low, high, fn, param); + int32_t idx = taosArrayPartition(pArray, low, high, fn, param); taosArrayQuicksortHelper(pArray, low, idx - 1, fn, param); taosArrayQuicksortHelper(pArray, idx + 1, high, fn, param); - } + } } -static void taosArrayQuickSort(SArray* pArray, __ext_compar_fn_t fn, const void *param) { +static void taosArrayQuickSort(SArray* pArray, __ext_compar_fn_t fn, const void* param) { if (pArray->size <= 1) { return; - } - taosArrayQuicksortHelper(pArray, 0, (int)(taosArrayGetSize(pArray) - 1), fn, param); + } + taosArrayQuicksortHelper(pArray, 0, (int32_t)(taosArrayGetSize(pArray) - 1), fn, param); } -static void taosArrayInsertSort(SArray* pArray, __ext_compar_fn_t fn, const void *param) { + +static void taosArrayInsertSort(SArray* pArray, __ext_compar_fn_t fn, const void* param) { if (pArray->size <= 1) { return; - } - for (int i = 1; i <= pArray->size - 1; ++i) { - for (int j = i; j > 0; --j) { + } + for (int32_t i = 1; i <= pArray->size - 1; ++i) { + for (int32_t j = i; j > 0; --j) { if (fn(taosArrayGetP(pArray, j), taosArrayGetP(pArray, j - 1), param) == -1) { - void *a = taosArrayGetP(pArray, j); - void *b = taosArrayGetP(pArray, j - 1); + void* a = taosArrayGetP(pArray, j); + void* b = taosArrayGetP(pArray, j - 1); taosArraySet(pArray, j - 1, &a); taosArraySet(pArray, j, &b); } else { @@ -415,11 +419,10 @@ static void taosArrayInsertSort(SArray* pArray, __ext_compar_fn_t fn, const void } } return; - -} -// order array -void taosArraySortPWithExt(SArray* pArray, __ext_compar_fn_t fn, const void *param) { - taosArrayGetSize(pArray) > 8 ? - taosArrayQuickSort(pArray, fn, param) : taosArrayInsertSort(pArray, fn, param); } -//TODO(yihaoDeng) add order array + +// order array +void taosArraySortPWithExt(SArray* pArray, __ext_compar_fn_t fn, const void* param) { + taosArrayGetSize(pArray) > 8 ? taosArrayQuickSort(pArray, fn, param) : taosArrayInsertSort(pArray, fn, param); +} +// TODO(yihaoDeng) add order array diff --git a/source/util/src/tbase64.c b/source/util/src/tbase64.c index 1b1f53df17..c5119e8b2d 100644 --- a/source/util/src/tbase64.c +++ b/source/util/src/tbase64.c @@ -13,15 +13,15 @@ * along with this program. If not, see . */ -#include "os.h" +#define _DEFAULT_SOURCE +#include "tbase64.h" -// deprecated this file for bug prone -// base64 encode static char basis_64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; -char *base64_encode(const unsigned char *value, int vlen) { - unsigned char oval = 0; - char * result = (char *)malloc((size_t)(vlen * 4) / 3 + 10); - char * out = result; + +char *base64_encode(const uint8_t *value, int32_t vlen) { + uint8_t oval = 0; + char *result = (char *)malloc((size_t)(vlen * 4) / 3 + 10); + char *out = result; while (vlen >= 3) { *out++ = basis_64[value[0] >> 2]; *out++ = basis_64[((value[0] << 4) & 0x30) | (value[1] >> 4)]; @@ -42,8 +42,8 @@ char *base64_encode(const unsigned char *value, int vlen) { return result; } -// base64 decode #define CHAR64(c) (((c) < 0 || (c) > 127) ? -1 : index_64[(c)]) + static signed char index_64[128] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, @@ -51,10 +51,10 @@ static signed char index_64[128] = { 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1}; -unsigned char *base64_decode(const char *value, int inlen, int *outlen) { - int c1, c2, c3, c4; - unsigned char *result = (unsigned char *)malloc((size_t)(inlen * 3) / 4 + 1); - unsigned char *out = result; +uint8_t *base64_decode(const char *value, int32_t inlen, int32_t *outlen) { + int32_t c1, c2, c3, c4; + uint8_t *result = (uint8_t *)malloc((size_t)(inlen * 3) / 4 + 1); + uint8_t *out = result; *outlen = 0; @@ -80,13 +80,13 @@ unsigned char *base64_decode(const char *value, int inlen, int *outlen) { if ((c4 != '=') && (CHAR64(c4) == -1)) goto base64_decode_error; value += 4; - *out++ = (unsigned char)((CHAR64(c1) << 2) | (CHAR64(c2) >> 4)); + *out++ = (uint8_t)((CHAR64(c1) << 2) | (CHAR64(c2) >> 4)); *outlen += 1; if (c3 != '=') { - *out++ = (unsigned char)(((CHAR64(c2) << 4) & 0xf0) | (CHAR64(c3) >> 2)); + *out++ = (uint8_t)(((CHAR64(c2) << 4) & 0xf0) | (CHAR64(c3) >> 2)); *outlen += 1; if (c4 != '=') { - *out++ = (unsigned char)(((CHAR64(c3) << 6) & 0xc0) | CHAR64(c4)); + *out++ = (uint8_t)(((CHAR64(c3) << 6) & 0xc0) | CHAR64(c4)); *outlen += 1; } } diff --git a/source/util/src/tbuffer.c b/source/util/src/tbuffer.c index 0456d6a2ee..dc8c4b70c5 100644 --- a/source/util/src/tbuffer.c +++ b/source/util/src/tbuffer.c @@ -14,11 +14,8 @@ */ #define _DEFAULT_SOURCE - #include "tbuffer.h" -#include "exception.h" -#include "os.h" -//#include "taoserror.h" +#include "texception.h" typedef union Un4B { uint32_t ui; diff --git a/source/util/src/tcache.c b/source/util/src/tcache.c index 6914e7e2d0..64d822f750 100644 --- a/source/util/src/tcache.c +++ b/source/util/src/tcache.c @@ -14,11 +14,10 @@ */ #define _DEFAULT_SOURCE -#include "os.h" +#include "tcache.h" #include "tlog.h" #include "ttimer.h" #include "tutil.h" -#include "tcache.h" static FORCE_INLINE void __cache_wr_lock(SCacheObj *pCacheObj) { #if defined(LINUX) @@ -62,15 +61,15 @@ static void doCleanupDataCache(SCacheObj *pCacheObj); * refresh cache to remove data in both hash list and trash, if any nodes' refcount == 0, every pCacheObj->refreshTime * @param handle Cache object handle */ -static void* taosCacheTimedRefresh(void *handle); +static void *taosCacheTimedRefresh(void *handle); -static pthread_t cacheRefreshWorker = {0}; -static pthread_once_t cacheThreadInit = PTHREAD_ONCE_INIT; -static pthread_mutex_t guard = PTHREAD_MUTEX_INITIALIZER; -static SArray* pCacheArrayList = NULL; -static bool stopRefreshWorker = false; -static bool refreshWorkerNormalStopped = false; -static bool refreshWorkerUnexpectedStopped = false; +static pthread_t cacheRefreshWorker = {0}; +static pthread_once_t cacheThreadInit = PTHREAD_ONCE_INIT; +static pthread_mutex_t guard = PTHREAD_MUTEX_INITIALIZER; +static SArray *pCacheArrayList = NULL; +static bool stopRefreshWorker = false; +static bool refreshWorkerNormalStopped = false; +static bool refreshWorkerUnexpectedStopped = false; static void doInitRefreshThread(void) { pCacheArrayList = taosArrayInit(4, POINTER_BYTES); @@ -83,7 +82,7 @@ static void doInitRefreshThread(void) { pthread_attr_destroy(&thattr); } -pthread_t doRegisterCacheObj(SCacheObj* pCacheObj) { +pthread_t doRegisterCacheObj(SCacheObj *pCacheObj) { pthread_once(&cacheThreadInit, doInitRefreshThread); pthread_mutex_lock(&guard); @@ -102,7 +101,8 @@ pthread_t doRegisterCacheObj(SCacheObj* pCacheObj) { * @param lifespan total survial expiredTime from now * @return SCacheDataNode */ -static SCacheDataNode *taosCreateCacheNode(const char *key, size_t keyLen, const char *pData, size_t size, uint64_t duration); +static SCacheDataNode *taosCreateCacheNode(const char *key, size_t keyLen, const char *pData, size_t size, + uint64_t duration); /** * addedTime object node into trash, and this object is closed for referencing if it is addedTime to trash @@ -146,18 +146,18 @@ static FORCE_INLINE void taosCacheReleaseNode(SCacheObj *pCacheObj, SCacheDataNo free(pNode); } -static FORCE_INLINE STrashElem* doRemoveElemInTrashcan(SCacheObj* pCacheObj, STrashElem *pElem) { - if (pElem->pData->signature != (uint64_t) pElem->pData) { +static FORCE_INLINE STrashElem *doRemoveElemInTrashcan(SCacheObj *pCacheObj, STrashElem *pElem) { + if (pElem->pData->signature != (uint64_t)pElem->pData) { uWarn("key:sig:0x%" PRIx64 " %p data has been released, ignore", pElem->pData->signature, pElem->pData); return NULL; } - STrashElem* next = pElem->next; + STrashElem *next = pElem->next; pCacheObj->numOfElemsInTrash--; if (pElem->prev) { pElem->prev->next = pElem->next; - } else { // pnode is the header, update header + } else { // pnode is the header, update header pCacheObj->pTrash = pElem->next; } @@ -172,7 +172,7 @@ static FORCE_INLINE STrashElem* doRemoveElemInTrashcan(SCacheObj* pCacheObj, STr return next; } -static FORCE_INLINE void doDestroyTrashcanElem(SCacheObj* pCacheObj, STrashElem *pElem) { +static FORCE_INLINE void doDestroyTrashcanElem(SCacheObj *pCacheObj, STrashElem *pElem) { if (pCacheObj->freeFp) { pCacheObj->freeFp(pElem->pData->data); } @@ -181,19 +181,20 @@ static FORCE_INLINE void doDestroyTrashcanElem(SCacheObj* pCacheObj, STrashElem free(pElem); } -SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInSeconds, bool extendLifespan, __cache_free_fn_t fn, const char* cacheName) { - const int32_t SLEEP_DURATION = 500; //500 ms +SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInSeconds, bool extendLifespan, __cache_free_fn_t fn, + const char *cacheName) { + const int32_t SLEEP_DURATION = 500; // 500 ms if (refreshTimeInSeconds <= 0) { return NULL; } - + SCacheObj *pCacheObj = (SCacheObj *)calloc(1, sizeof(SCacheObj)); if (pCacheObj == NULL) { uError("failed to allocate memory, reason:%s", strerror(errno)); return NULL; } - + pCacheObj->pHashTable = taosHashInit(4096, taosGetDefaultHashFunction(keyType), false, HASH_ENTRY_LOCK); pCacheObj->name = strdup(cacheName); if (pCacheObj->pHashTable == NULL) { @@ -201,17 +202,17 @@ SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInSeconds, bool ext uError("failed to allocate memory, reason:%s", strerror(errno)); return NULL; } - + // set free cache node callback function - pCacheObj->freeFp = fn; + pCacheObj->freeFp = fn; pCacheObj->refreshTime = refreshTimeInSeconds * 1000; - pCacheObj->checkTick = pCacheObj->refreshTime / SLEEP_DURATION; + pCacheObj->checkTick = pCacheObj->refreshTime / SLEEP_DURATION; pCacheObj->extendLifespan = extendLifespan; // the TTL after the last access if (__cache_lock_init(pCacheObj) != 0) { taosHashCleanup(pCacheObj->pHashTable); free(pCacheObj); - + uError("failed to init lock, reason:%s", strerror(errno)); return NULL; } @@ -220,7 +221,8 @@ SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInSeconds, bool ext return pCacheObj; } -void *taosCachePut(SCacheObj *pCacheObj, const void *key, size_t keyLen, const void *pData, size_t dataSize, int durationMS) { +void *taosCachePut(SCacheObj *pCacheObj, const void *key, size_t keyLen, const void *pData, size_t dataSize, + int32_t durationMS) { if (pCacheObj == NULL || pCacheObj->pHashTable == NULL || pCacheObj->deleting == 1) { return NULL; } @@ -242,8 +244,8 @@ void *taosCachePut(SCacheObj *pCacheObj, const void *key, size_t keyLen, const v (int32_t)taosHashGetSize(pCacheObj->pHashTable), pCacheObj->totalSize, (int64_t)dataSize); } else { // duplicated key exists while (1) { - SCacheDataNode* p = NULL; -// int32_t ret = taosHashRemoveWithData(pCacheObj->pHashTable, key, keyLen, (void*) &p, sizeof(void*)); + SCacheDataNode *p = NULL; + // int32_t ret = taosHashRemoveWithData(pCacheObj->pHashTable, key, keyLen, (void*) &p, sizeof(void*)); int32_t ret = taosHashRemove(pCacheObj->pHashTable, key, keyLen); // add to trashcan @@ -283,10 +285,10 @@ void *taosCachePut(SCacheObj *pCacheObj, const void *key, size_t keyLen, const v return pNode1->data; } -static void incRefFn(void* ptNode) { +static void incRefFn(void *ptNode) { assert(ptNode != NULL); - SCacheDataNode** p = (SCacheDataNode**) ptNode; + SCacheDataNode **p = (SCacheDataNode **)ptNode; assert(T_REF_VAL_GET(*p) >= 0); int32_t ret = T_REF_INC(*p); @@ -303,15 +305,16 @@ void *taosCacheAcquireByKey(SCacheObj *pCacheObj, const void *key, size_t keyLen return NULL; } - SCacheDataNode* ptNode = NULL; + SCacheDataNode *ptNode = NULL; taosHashGetClone(pCacheObj->pHashTable, key, keyLen, &ptNode); -// taosHashGetClone(pCacheObj->pHashTable, key, keyLen, incRefFn, &ptNode); + // taosHashGetClone(pCacheObj->pHashTable, key, keyLen, incRefFn, &ptNode); - void* pData = (ptNode != NULL)? ptNode->data:NULL; + void *pData = (ptNode != NULL) ? ptNode->data : NULL; if (pData != NULL) { atomic_add_fetch_32(&pCacheObj->statistics.hitCount, 1); - uDebug("cache:%s, key:%p, %p is retrieved from cache, refcnt:%d", pCacheObj->name, key, pData, T_REF_VAL_GET(ptNode)); + uDebug("cache:%s, key:%p, %p is retrieved from cache, refcnt:%d", pCacheObj->name, key, pData, + T_REF_VAL_GET(ptNode)); } else { atomic_add_fetch_32(&pCacheObj->statistics.missCount, 1); uDebug("cache:%s, key:%p, not in cache, retrieved failed", pCacheObj->name, key); @@ -323,10 +326,10 @@ void *taosCacheAcquireByKey(SCacheObj *pCacheObj, const void *key, size_t keyLen void *taosCacheAcquireByData(SCacheObj *pCacheObj, void *data) { if (pCacheObj == NULL || data == NULL) return NULL; - + size_t offset = offsetof(SCacheDataNode, data); SCacheDataNode *ptNode = (SCacheDataNode *)((char *)data - offset); - + if (ptNode->signature != (uint64_t)ptNode) { uError("cache:%s, key: %p the data from cache is invalid", pCacheObj->name, ptNode); return NULL; @@ -342,22 +345,22 @@ void *taosCacheAcquireByData(SCacheObj *pCacheObj, void *data) { void *taosCacheTransfer(SCacheObj *pCacheObj, void **data) { if (pCacheObj == NULL || data == NULL || (*data) == NULL) return NULL; - + size_t offset = offsetof(SCacheDataNode, data); SCacheDataNode *ptNode = (SCacheDataNode *)((char *)(*data) - offset); - + if (ptNode->signature != (uint64_t)ptNode) { uError("cache:%s, key: %p the data from cache is invalid", pCacheObj->name, ptNode); return NULL; } - + assert(T_REF_VAL_GET(ptNode) >= 1); - + char *d = *data; - + // clear its reference to old area *data = NULL; - + return d; } @@ -371,13 +374,12 @@ void taosCacheRelease(SCacheObj *pCacheObj, void **data, bool _remove) { return; } - // The operation of removal from hash table and addition to trashcan is not an atomic operation, // therefore the check for the empty of both the hash table and the trashcan has a race condition. // It happens when there is only one object in the cache, and two threads which has referenced this object // start to free the it simultaneously [TD-1569]. size_t offset = offsetof(SCacheDataNode, data); - + SCacheDataNode *pNode = (SCacheDataNode *)((char *)(*data) - offset); if (pNode->signature != (uint64_t)pNode) { uError("cache:%s, %p, release invalid cache data", pCacheObj->name, pNode); @@ -391,20 +393,20 @@ void taosCacheRelease(SCacheObj *pCacheObj, void **data, bool _remove) { if (pCacheObj->extendLifespan && (!inTrashcan) && (!_remove)) { atomic_store_64(&pNode->expireTime, pNode->lifespan + taosGetTimestampMs()); - uDebug("cache:%s, data:%p extend expire time: %"PRId64, pCacheObj->name, pNode->data, pNode->expireTime); + uDebug("cache:%s, data:%p extend expire time: %" PRId64, pCacheObj->name, pNode->data, pNode->expireTime); } if (_remove) { // NOTE: once refcount is decrease, pNode may be freed by other thread immediately. - char* key = pNode->key; - char* d = pNode->data; + char *key = pNode->key; + char *d = pNode->data; int32_t ref = T_REF_VAL_GET(pNode); uDebug("cache:%s, key:%p, %p is released, refcnt:%d, in trashcan:%d", pCacheObj->name, key, d, ref - 1, inTrashcan); /* - * If it is not referenced by other users, remove it immediately. Otherwise move this node to trashcan wait for all users - * releasing this resources. + * If it is not referenced by other users, remove it immediately. Otherwise move this node to trashcan wait for all + * users releasing this resources. * * NOTE: previous ref is 0, and current ref is still 0, remove it. If previous is not 0, there is another thread * that tries to do the same thing. @@ -433,16 +435,19 @@ void taosCacheRelease(SCacheObj *pCacheObj, void **data, bool _remove) { // NOTE: remove it from hash in the first place, otherwise, the pNode may have been released by other thread // when reaches here. SCacheDataNode *p = NULL; - int32_t ret = taosHashRemove(pCacheObj->pHashTable, pNode->key, pNode->keySize); -// int32_t ret = taosHashRemoveWithData(pCacheObj->pHashTable, pNode->key, pNode->keySize, &p, sizeof(void *)); + int32_t ret = taosHashRemove(pCacheObj->pHashTable, pNode->key, pNode->keySize); + // int32_t ret = taosHashRemoveWithData(pCacheObj->pHashTable, pNode->key, pNode->keySize, &p, sizeof(void + // *)); ref = T_REF_DEC(pNode); // successfully remove from hash table, if failed, this node must have been move to trash already, do nothing. // note that the remove operation can be executed only once. if (ret == 0) { if (p != pNode) { - uDebug( "cache:%s, key:%p, successfully removed a new entry:%p, refcnt:%d, prev entry:%p has been removed by " - "others already", pCacheObj->name, pNode->key, p->data, T_REF_VAL_GET(p), pNode->data); + uDebug( + "cache:%s, key:%p, successfully removed a new entry:%p, refcnt:%d, prev entry:%p has been removed by " + "others already", + pCacheObj->name, pNode->key, p->data, T_REF_VAL_GET(p), pNode->data); assert(p->pTNodeHeader == NULL); taosAddToTrashcan(pCacheObj, p); @@ -468,35 +473,36 @@ void taosCacheRelease(SCacheObj *pCacheObj, void **data, bool _remove) { } } } else { - uDebug("cache:%s, key:%p, %p has been removed from hash table by others already, refcnt:%d", - pCacheObj->name, pNode->key, pNode->data, ref); + uDebug("cache:%s, key:%p, %p has been removed from hash table by others already, refcnt:%d", pCacheObj->name, + pNode->key, pNode->data, ref); } } } else { // NOTE: once refcount is decrease, pNode may be freed by other thread immediately. - char* key = pNode->key; - char* p = pNode->data; + char *key = pNode->key; + char *p = pNode->data; -// int32_t ref = T_REF_VAL_GET(pNode); -// -// if (ref == 1 && inTrashcan) { -// // If it is the last ref, remove it from trashcan linked-list first, and then destroy it.Otherwise, it may be -// // destroyed by refresh worker if decrease ref count before removing it from linked-list. -// assert(pNode->pTNodeHeader->pData == pNode); -// -// __cache_wr_lock(pCacheObj); -// doRemoveElemInTrashcan(pCacheObj, pNode->pTNodeHeader); -// __cache_unlock(pCacheObj); -// -// ref = T_REF_DEC(pNode); -// assert(ref == 0); -// -// doDestroyTrashcanElem(pCacheObj, pNode->pTNodeHeader); -// } else { -// ref = T_REF_DEC(pNode); -// assert(ref >= 0); -// } + // int32_t ref = T_REF_VAL_GET(pNode); + // + // if (ref == 1 && inTrashcan) { + // // If it is the last ref, remove it from trashcan linked-list first, and then destroy it.Otherwise, it may + // be + // // destroyed by refresh worker if decrease ref count before removing it from linked-list. + // assert(pNode->pTNodeHeader->pData == pNode); + // + // __cache_wr_lock(pCacheObj); + // doRemoveElemInTrashcan(pCacheObj, pNode->pTNodeHeader); + // __cache_unlock(pCacheObj); + // + // ref = T_REF_DEC(pNode); + // assert(ref == 0); + // + // doDestroyTrashcanElem(pCacheObj, pNode->pTNodeHeader); + // } else { + // ref = T_REF_DEC(pNode); + // assert(ref >= 0); + // } int32_t ref = T_REF_DEC(pNode); uDebug("cache:%s, key:%p, %p released, refcnt:%d, data in trashcan:%d", pCacheObj->name, key, p, ref, inTrashcan); @@ -504,21 +510,21 @@ void taosCacheRelease(SCacheObj *pCacheObj, void **data, bool _remove) { } typedef struct SHashTravSupp { - SCacheObj* pCacheObj; - int64_t time; + SCacheObj *pCacheObj; + int64_t time; __cache_trav_fn_t fp; - void* param1; + void *param1; } SHashTravSupp; -static bool travHashTableEmptyFn(void* param, void* data) { - SHashTravSupp* ps = (SHashTravSupp*) param; - SCacheObj* pCacheObj= ps->pCacheObj; +static bool travHashTableEmptyFn(void *param, void *data) { + SHashTravSupp *ps = (SHashTravSupp *)param; + SCacheObj *pCacheObj = ps->pCacheObj; - SCacheDataNode *pNode = *(SCacheDataNode **) data; + SCacheDataNode *pNode = *(SCacheDataNode **)data; if (T_REF_VAL_GET(pNode) == 0) { taosCacheReleaseNode(pCacheObj, pNode); - } else { // do add to trashcan + } else { // do add to trashcan taosAddToTrashcan(pCacheObj, pNode); } @@ -529,7 +535,7 @@ static bool travHashTableEmptyFn(void* param, void* data) { void taosCacheEmpty(SCacheObj *pCacheObj) { SHashTravSupp sup = {.pCacheObj = pCacheObj, .fp = NULL, .time = taosGetTimestampMs()}; -// taosHashCondTraverse(pCacheObj->pHashTable, travHashTableEmptyFn, &sup); + // taosHashCondTraverse(pCacheObj->pHashTable, travHashTableEmptyFn, &sup); taosTrashcanEmpty(pCacheObj, false); } @@ -542,9 +548,9 @@ void taosCacheCleanup(SCacheObj *pCacheObj) { // wait for the refresh thread quit before destroying the cache object. // But in the dll, the child thread will be killed before atexit takes effect. - while(atomic_load_8(&pCacheObj->deleting) != 0) { - if (refreshWorkerNormalStopped) break; - if (refreshWorkerUnexpectedStopped) return; + while (atomic_load_8(&pCacheObj->deleting) != 0) { + if (refreshWorkerNormalStopped) break; + if (refreshWorkerUnexpectedStopped) return; taosMsleep(50); } @@ -568,11 +574,11 @@ SCacheDataNode *taosCreateCacheNode(const char *key, size_t keyLen, const char * memcpy(pNewNode->key, key, keyLen); - pNewNode->addedTime = (uint64_t)taosGetTimestampMs(); - pNewNode->lifespan = duration; - pNewNode->expireTime = pNewNode->addedTime + pNewNode->lifespan; - pNewNode->signature = (uint64_t)pNewNode; - pNewNode->size = (uint32_t)totalSize; + pNewNode->addedTime = (uint64_t)taosGetTimestampMs(); + pNewNode->lifespan = duration; + pNewNode->expireTime = pNewNode->addedTime + pNewNode->lifespan; + pNewNode->signature = (uint64_t)pNewNode; + pNewNode->size = (uint32_t)totalSize; return pNewNode; } @@ -610,16 +616,17 @@ void taosTrashcanEmpty(SCacheObj *pCacheObj, bool force) { if (pCacheObj->numOfElemsInTrash == 0) { if (pCacheObj->pTrash != NULL) { pCacheObj->pTrash = NULL; - uError("cache:%s, key:inconsistency data in cache, numOfElem in trashcan:%d", pCacheObj->name, pCacheObj->numOfElemsInTrash); + uError("cache:%s, key:inconsistency data in cache, numOfElem in trashcan:%d", pCacheObj->name, + pCacheObj->numOfElemsInTrash); } __cache_unlock(pCacheObj); return; } - const char* stat[] = {"false", "true"}; + const char *stat[] = {"false", "true"}; uDebug("cache:%s start to cleanup trashcan, numOfElem in trashcan:%d, free:%s", pCacheObj->name, - pCacheObj->numOfElemsInTrash, (force? stat[1]:stat[0])); + pCacheObj->numOfElemsInTrash, (force ? stat[1] : stat[0])); STrashElem *pElem = pCacheObj->pTrash; while (pElem) { @@ -627,8 +634,8 @@ void taosTrashcanEmpty(SCacheObj *pCacheObj, bool force) { assert(pElem->next != pElem && pElem->prev != pElem); if (force || (T_REF_VAL_GET(pElem->pData) == 0)) { - uDebug("cache:%s, key:%p, %p removed from trashcan. numOfElem in trashcan:%d", pCacheObj->name, pElem->pData->key, pElem->pData->data, - pCacheObj->numOfElemsInTrash - 1); + uDebug("cache:%s, key:%p, %p removed from trashcan. numOfElem in trashcan:%d", pCacheObj->name, pElem->pData->key, + pElem->pData->data, pCacheObj->numOfElemsInTrash - 1); doRemoveElemInTrashcan(pCacheObj, pElem); doDestroyTrashcanElem(pCacheObj, pElem); @@ -642,25 +649,25 @@ void taosTrashcanEmpty(SCacheObj *pCacheObj, bool force) { } void doCleanupDataCache(SCacheObj *pCacheObj) { -// SHashTravSupp sup = {.pCacheObj = pCacheObj, .fp = NULL, .time = taosGetTimestampMs()}; -// taosHashCondTraverse(pCacheObj->pHashTable, travHashTableEmptyFn, &sup); + // SHashTravSupp sup = {.pCacheObj = pCacheObj, .fp = NULL, .time = taosGetTimestampMs()}; + // taosHashCondTraverse(pCacheObj->pHashTable, travHashTableEmptyFn, &sup); // todo memory leak if there are object with refcount greater than 0 in hash table? taosHashCleanup(pCacheObj->pHashTable); taosTrashcanEmpty(pCacheObj, true); __cache_lock_destroy(pCacheObj); - + tfree(pCacheObj->name); memset(pCacheObj, 0, sizeof(SCacheObj)); free(pCacheObj); } -bool travHashTableFn(void* param, void* data) { - SHashTravSupp* ps = (SHashTravSupp*) param; - SCacheObj* pCacheObj= ps->pCacheObj; +bool travHashTableFn(void *param, void *data) { + SHashTravSupp *ps = (SHashTravSupp *)param; + SCacheObj *pCacheObj = ps->pCacheObj; - SCacheDataNode* pNode = *(SCacheDataNode **) data; + SCacheDataNode *pNode = *(SCacheDataNode **)data; if ((int64_t)pNode->expireTime < ps->time && T_REF_VAL_GET(pNode) <= 0) { taosCacheReleaseNode(pCacheObj, pNode); @@ -676,30 +683,30 @@ bool travHashTableFn(void* param, void* data) { return true; } -static void doCacheRefresh(SCacheObj* pCacheObj, int64_t time, __cache_trav_fn_t fp, void* param1) { +static void doCacheRefresh(SCacheObj *pCacheObj, int64_t time, __cache_trav_fn_t fp, void *param1) { assert(pCacheObj != NULL); SHashTravSupp sup = {.pCacheObj = pCacheObj, .fp = fp, .time = time, .param1 = param1}; -// taosHashCondTraverse(pCacheObj->pHashTable, travHashTableFn, &sup); + // taosHashCondTraverse(pCacheObj->pHashTable, travHashTableFn, &sup); } void taosCacheRefreshWorkerUnexpectedStopped(void) { - if(!refreshWorkerNormalStopped) { - refreshWorkerUnexpectedStopped=true; + if (!refreshWorkerNormalStopped) { + refreshWorkerUnexpectedStopped = true; } } -void* taosCacheTimedRefresh(void *handle) { +void *taosCacheTimedRefresh(void *handle) { assert(pCacheArrayList != NULL); uDebug("cache refresh thread starts"); setThreadName("cacheRefresh"); - const int32_t SLEEP_DURATION = 500; //500 ms - int64_t count = 0; + const int32_t SLEEP_DURATION = 500; // 500 ms + int64_t count = 0; atexit(taosCacheRefreshWorkerUnexpectedStopped); - while(1) { + while (1) { taosMsleep(SLEEP_DURATION); if (stopRefreshWorker) { goto _end; @@ -711,9 +718,9 @@ void* taosCacheTimedRefresh(void *handle) { count += 1; - for(int32_t i = 0; i < size; ++i) { + for (int32_t i = 0; i < size; ++i) { pthread_mutex_lock(&guard); - SCacheObj* pCacheObj = taosArrayGetP(pCacheArrayList, i); + SCacheObj *pCacheObj = taosArrayGetP(pCacheArrayList, i); if (pCacheObj == NULL) { uError("object is destroyed. ignore and try next"); @@ -726,8 +733,8 @@ void* taosCacheTimedRefresh(void *handle) { taosArrayRemove(pCacheArrayList, i); size = taosArrayGetSize(pCacheArrayList); - uDebug("%s is destroying, remove it from refresh list, remain cache obj:%"PRIzu, pCacheObj->name, size); - pCacheObj->deleting = 0; //reset the deleting flag to enable pCacheObj to continue releasing resources. + uDebug("%s is destroying, remove it from refresh list, remain cache obj:%" PRIzu, pCacheObj->name, size); + pCacheObj->deleting = 0; // reset the deleting flag to enable pCacheObj to continue releasing resources. pthread_mutex_unlock(&guard); continue; @@ -757,18 +764,18 @@ void* taosCacheTimedRefresh(void *handle) { } } - _end: +_end: taosArrayDestroy(pCacheArrayList); pCacheArrayList = NULL; pthread_mutex_destroy(&guard); - refreshWorkerNormalStopped=true; + refreshWorkerNormalStopped = true; uDebug("cache refresh thread quits"); return NULL; } -void taosCacheRefresh(SCacheObj *pCacheObj, __cache_trav_fn_t fp, void* param1) { +void taosCacheRefresh(SCacheObj *pCacheObj, __cache_trav_fn_t fp, void *param1) { if (pCacheObj == NULL) { return; } @@ -777,6 +784,4 @@ void taosCacheRefresh(SCacheObj *pCacheObj, __cache_trav_fn_t fp, void* param1) doCacheRefresh(pCacheObj, now, fp, param1); } -void taosStopCacheRefreshWorker(void) { - stopRefreshWorker = true; -} \ No newline at end of file +void taosStopCacheRefreshWorker(void) { stopRefreshWorker = true; } \ No newline at end of file diff --git a/source/util/src/compare.c b/source/util/src/tcompare.c similarity index 65% rename from source/util/src/compare.c rename to source/util/src/tcompare.c index b597eb32bf..062d144933 100644 --- a/source/util/src/compare.c +++ b/source/util/src/tcompare.c @@ -16,15 +16,12 @@ #define _GNU_SOURCE #define _XOPEN_SOURCE #define _DEFAULT_SOURCE -#include "os.h" - -#include "compare.h" +#include "tcompare.h" #include "regex.h" -#include "thash.h" -#include "types.h" -#include "tlog.h" #include "tdef.h" -#include "taos.h" +#include "thash.h" +#include "tlog.h" +#include "types.h" int32_t setChkInBytes1(const void *pLeft, const void *pRight) { return NULL != taosHashGet((SHashObj *)pRight, pLeft, 1) ? 1 : 0; @@ -58,16 +55,14 @@ int32_t setChkNotInBytes8(const void *pLeft, const void *pRight) { return NULL == taosHashGet((SHashObj *)pRight, pLeft, 8) ? 1 : 0; } - -int32_t compareChkInString(const void *pLeft, const void* pRight) { +int32_t compareChkInString(const void *pLeft, const void *pRight) { return NULL != taosHashGet((SHashObj *)pRight, varDataVal(pLeft), varDataLen(pLeft)) ? 1 : 0; } -int32_t compareChkNotInString(const void *pLeft, const void* pRight) { +int32_t compareChkNotInString(const void *pLeft, const void *pRight) { return NULL == taosHashGet((SHashObj *)pRight, varDataVal(pLeft), varDataLen(pLeft)) ? 1 : 0; } - int32_t compareInt8Val(const void *pLeft, const void *pRight) { int8_t left = GET_INT8_VAL(pLeft), right = GET_INT8_VAL(pRight); if (left > right) return 1; @@ -75,9 +70,7 @@ int32_t compareInt8Val(const void *pLeft, const void *pRight) { return 0; } -int32_t compareInt8ValDesc(const void *pLeft, const void *pRight) { - return compareInt8Val(pRight, pLeft); -} +int32_t compareInt8ValDesc(const void *pLeft, const void *pRight) { return compareInt8Val(pRight, pLeft); } int32_t compareInt16Val(const void *pLeft, const void *pRight) { int16_t left = GET_INT16_VAL(pLeft), right = GET_INT16_VAL(pRight); @@ -86,9 +79,7 @@ int32_t compareInt16Val(const void *pLeft, const void *pRight) { return 0; } -int32_t compareInt16ValDesc(const void* pLeft, const void* pRight) { - return compareInt16Val(pRight, pLeft); -} +int32_t compareInt16ValDesc(const void *pLeft, const void *pRight) { return compareInt16Val(pRight, pLeft); } int32_t compareInt32Val(const void *pLeft, const void *pRight) { int32_t left = GET_INT32_VAL(pLeft), right = GET_INT32_VAL(pRight); @@ -97,9 +88,7 @@ int32_t compareInt32Val(const void *pLeft, const void *pRight) { return 0; } -int32_t compareInt32ValDesc(const void* pLeft, const void* pRight) { - return compareInt32Val(pRight, pLeft); -} +int32_t compareInt32ValDesc(const void *pLeft, const void *pRight) { return compareInt32Val(pRight, pLeft); } int32_t compareInt64Val(const void *pLeft, const void *pRight) { int64_t left = GET_INT64_VAL(pLeft), right = GET_INT64_VAL(pRight); @@ -108,9 +97,7 @@ int32_t compareInt64Val(const void *pLeft, const void *pRight) { return 0; } -int32_t compareInt64ValDesc(const void* pLeft, const void* pRight) { - return compareInt64Val(pRight, pLeft); -} +int32_t compareInt64ValDesc(const void *pLeft, const void *pRight) { return compareInt64Val(pRight, pLeft); } int32_t compareUint32Val(const void *pLeft, const void *pRight) { uint32_t left = GET_UINT32_VAL(pLeft), right = GET_UINT32_VAL(pRight); @@ -119,9 +106,7 @@ int32_t compareUint32Val(const void *pLeft, const void *pRight) { return 0; } -int32_t compareUint32ValDesc(const void* pLeft, const void* pRight) { - return compareUint32Val(pRight, pLeft); -} +int32_t compareUint32ValDesc(const void *pLeft, const void *pRight) { return compareUint32Val(pRight, pLeft); } int32_t compareUint64Val(const void *pLeft, const void *pRight) { uint64_t left = GET_UINT64_VAL(pLeft), right = GET_UINT64_VAL(pRight); @@ -130,9 +115,7 @@ int32_t compareUint64Val(const void *pLeft, const void *pRight) { return 0; } -int32_t compareUint64ValDesc(const void* pLeft, const void* pRight) { - return compareUint64Val(pRight, pLeft); -} +int32_t compareUint64ValDesc(const void *pLeft, const void *pRight) { return compareUint64Val(pRight, pLeft); } int32_t compareUint16Val(const void *pLeft, const void *pRight) { uint16_t left = GET_UINT16_VAL(pLeft), right = GET_UINT16_VAL(pRight); @@ -141,20 +124,16 @@ int32_t compareUint16Val(const void *pLeft, const void *pRight) { return 0; } -int32_t compareUint16ValDesc(const void* pLeft, const void* pRight) { - return compareUint16Val(pRight, pLeft); -} +int32_t compareUint16ValDesc(const void *pLeft, const void *pRight) { return compareUint16Val(pRight, pLeft); } -int32_t compareUint8Val(const void* pLeft, const void* pRight) { +int32_t compareUint8Val(const void *pLeft, const void *pRight) { uint8_t left = GET_UINT8_VAL(pLeft), right = GET_UINT8_VAL(pRight); if (left > right) return 1; if (left < right) return -1; return 0; } -int32_t compareUint8ValDesc(const void* pLeft, const void* pRight) { - return compareUint8Val(pRight, pLeft); -} +int32_t compareUint8ValDesc(const void *pLeft, const void *pRight) { return compareUint8Val(pRight, pLeft); } int32_t compareFloatVal(const void *pLeft, const void *pRight) { float p1 = GET_FLOAT_VAL(pLeft); @@ -174,12 +153,10 @@ int32_t compareFloatVal(const void *pLeft, const void *pRight) { if (FLT_EQUAL(p1, p2)) { return 0; } - return FLT_GREATER(p1, p2) ? 1: -1; + return FLT_GREATER(p1, p2) ? 1 : -1; } -int32_t compareFloatValDesc(const void* pLeft, const void* pRight) { - return compareFloatVal(pRight, pLeft); -} +int32_t compareFloatValDesc(const void *pLeft, const void *pRight) { return compareFloatVal(pRight, pLeft); } int32_t compareDoubleVal(const void *pLeft, const void *pRight) { double p1 = GET_DOUBLE_VAL(pLeft); @@ -199,41 +176,19 @@ int32_t compareDoubleVal(const void *pLeft, const void *pRight) { if (FLT_EQUAL(p1, p2)) { return 0; } - return FLT_GREATER(p1, p2) ? 1: -1; + return FLT_GREATER(p1, p2) ? 1 : -1; } -int32_t compareDoubleValDesc(const void* pLeft, const void* pRight) { - return compareDoubleVal(pRight, pLeft); -} +int32_t compareDoubleValDesc(const void *pLeft, const void *pRight) { return compareDoubleVal(pRight, pLeft); } int32_t compareLenPrefixedStr(const void *pLeft, const void *pRight) { int32_t len1 = varDataLen(pLeft); int32_t len2 = varDataLen(pRight); if (len1 != len2) { - return len1 > len2? 1:-1; + return len1 > len2 ? 1 : -1; } else { int32_t ret = strncmp(varDataVal(pLeft), varDataVal(pRight), len1); - if (ret == 0) { - return 0; - } else { - return ret > 0 ? 1:-1; - } - } -} - -int32_t compareLenPrefixedStrDesc(const void* pLeft, const void* pRight) { - return compareLenPrefixedStr(pRight, pLeft); -} - -int32_t compareLenPrefixedWStr(const void *pLeft, const void *pRight) { - int32_t len1 = varDataLen(pLeft); - int32_t len2 = varDataLen(pRight); - - if (len1 != len2) { - return len1 > len2? 1:-1; - } else { - int32_t ret = memcmp((wchar_t*) pLeft, (wchar_t*) pRight, len1); if (ret == 0) { return 0; } else { @@ -242,7 +197,27 @@ int32_t compareLenPrefixedWStr(const void *pLeft, const void *pRight) { } } -int32_t compareLenPrefixedWStrDesc(const void* pLeft, const void* pRight) { +int32_t compareLenPrefixedStrDesc(const void *pLeft, const void *pRight) { + return compareLenPrefixedStr(pRight, pLeft); +} + +int32_t compareLenPrefixedWStr(const void *pLeft, const void *pRight) { + int32_t len1 = varDataLen(pLeft); + int32_t len2 = varDataLen(pRight); + + if (len1 != len2) { + return len1 > len2 ? 1 : -1; + } else { + int32_t ret = memcmp((wchar_t *)pLeft, (wchar_t *)pRight, len1); + if (ret == 0) { + return 0; + } else { + return ret > 0 ? 1 : -1; + } + } +} + +int32_t compareLenPrefixedWStrDesc(const void *pLeft, const void *pRight) { return compareLenPrefixedWStr(pRight, pLeft); } @@ -302,10 +277,13 @@ int32_t patternMatch(const char *patterStr, const char *str, size_t size, const } c1 = str[j++]; - ++o; - + ++o; + if (j <= size) { - if (c == '\\' && patterStr[i] == '_' && c1 == '_') { i++; continue; } + if (c == '\\' && patterStr[i] == '_' && c1 == '_') { + i++; + continue; + } if (c == c1 || tolower(c) == tolower(c1) || (c == pInfo->matchOne && c1 != 0)) { continue; } @@ -365,21 +343,19 @@ int32_t WCSPatternMatch(const wchar_t *patterStr, const wchar_t *str, size_t siz return TSDB_PATTERN_NOMATCH; } - + return (str[j] == 0 || j >= size) ? TSDB_PATTERN_MATCH : TSDB_PATTERN_NOMATCH; } -int32_t compareStrRegexCompMatch(const void* pLeft, const void* pRight) { - return compareStrRegexComp(pLeft, pRight); -} +int32_t compareStrRegexCompMatch(const void *pLeft, const void *pRight) { return compareStrRegexComp(pLeft, pRight); } -int32_t compareStrRegexCompNMatch(const void* pLeft, const void* pRight) { +int32_t compareStrRegexCompNMatch(const void *pLeft, const void *pRight) { return compareStrRegexComp(pLeft, pRight) ? 0 : 1; } -int32_t compareStrRegexComp(const void* pLeft, const void* pRight) { +int32_t compareStrRegexComp(const void *pLeft, const void *pRight) { size_t sz = varDataLen(pRight); - char *pattern = malloc(sz + 1); + char *pattern = malloc(sz + 1); memcpy(pattern, varDataVal(pRight), varDataLen(pRight)); pattern[sz] = 0; @@ -414,15 +390,14 @@ int32_t compareStrRegexComp(const void* pLeft, const void* pRight) { return result; } -int32_t taosArrayCompareString(const void* a, const void* b) { - const char* x = *(const char**)a; - const char* y = *(const char**)b; +int32_t taosArrayCompareString(const void *a, const void *b) { + const char *x = *(const char **)a; + const char *y = *(const char **)b; return compareLenPrefixedStr(x, y); } - -int32_t compareStrPatternMatch(const void* pLeft, const void* pRight) { +int32_t compareStrPatternMatch(const void *pLeft, const void *pRight) { SPatternCompareInfo pInfo = {'%', '_'}; assert(varDataLen(pRight) <= TSDB_MAX_FIELD_LEN); @@ -430,7 +405,7 @@ int32_t compareStrPatternMatch(const void* pLeft, const void* pRight) { memcpy(pattern, varDataVal(pRight), varDataLen(pRight)); size_t sz = varDataLen(pLeft); - char *buf = malloc(sz + 1); + char *buf = malloc(sz + 1); memcpy(buf, varDataVal(pLeft), sz); buf[sz] = 0; @@ -440,11 +415,11 @@ int32_t compareStrPatternMatch(const void* pLeft, const void* pRight) { return (ret == TSDB_PATTERN_MATCH) ? 0 : 1; } -int32_t compareStrPatternNotMatch(const void* pLeft, const void* pRight) { +int32_t compareStrPatternNotMatch(const void *pLeft, const void *pRight) { return compareStrPatternMatch(pLeft, pRight) ? 0 : 1; } -int32_t compareWStrPatternMatch(const void* pLeft, const void* pRight) { +int32_t compareWStrPatternMatch(const void *pLeft, const void *pRight) { SPatternCompareInfo pInfo = {'%', '_'}; assert(varDataLen(pRight) <= TSDB_MAX_FIELD_LEN * TSDB_NCHAR_SIZE); @@ -452,17 +427,16 @@ int32_t compareWStrPatternMatch(const void* pLeft, const void* pRight) { wchar_t *pattern = calloc(varDataLen(pRight) + 1, sizeof(wchar_t)); memcpy(pattern, varDataVal(pRight), varDataLen(pRight)); - int32_t ret = WCSPatternMatch(pattern, varDataVal(pLeft), varDataLen(pLeft)/TSDB_NCHAR_SIZE, &pInfo); + int32_t ret = WCSPatternMatch(pattern, varDataVal(pLeft), varDataLen(pLeft) / TSDB_NCHAR_SIZE, &pInfo); free(pattern); return (ret == TSDB_PATTERN_MATCH) ? 0 : 1; } -int32_t compareWStrPatternNotMatch(const void* pLeft, const void* pRight) { +int32_t compareWStrPatternNotMatch(const void *pLeft, const void *pRight) { return compareWStrPatternMatch(pLeft, pRight) ? 0 : 1; } - __compar_fn_t getComparFunc(int32_t type, int32_t optr) { __compar_fn_t comparFn = NULL; @@ -514,13 +488,25 @@ __compar_fn_t getComparFunc(int32_t type, int32_t optr) { switch (type) { case TSDB_DATA_TYPE_BOOL: - case TSDB_DATA_TYPE_TINYINT: comparFn = compareInt8Val; break; - case TSDB_DATA_TYPE_SMALLINT: comparFn = compareInt16Val; break; - case TSDB_DATA_TYPE_INT: comparFn = compareInt32Val; break; + case TSDB_DATA_TYPE_TINYINT: + comparFn = compareInt8Val; + break; + case TSDB_DATA_TYPE_SMALLINT: + comparFn = compareInt16Val; + break; + case TSDB_DATA_TYPE_INT: + comparFn = compareInt32Val; + break; case TSDB_DATA_TYPE_BIGINT: - case TSDB_DATA_TYPE_TIMESTAMP: comparFn = compareInt64Val; break; - case TSDB_DATA_TYPE_FLOAT: comparFn = compareFloatVal; break; - case TSDB_DATA_TYPE_DOUBLE: comparFn = compareDoubleVal; break; + case TSDB_DATA_TYPE_TIMESTAMP: + comparFn = compareInt64Val; + break; + case TSDB_DATA_TYPE_FLOAT: + comparFn = compareFloatVal; + break; + case TSDB_DATA_TYPE_DOUBLE: + comparFn = compareDoubleVal; + break; case TSDB_DATA_TYPE_BINARY: { if (optr == OP_TYPE_MATCH) { comparFn = compareStrRegexCompMatch; @@ -560,10 +546,18 @@ __compar_fn_t getComparFunc(int32_t type, int32_t optr) { break; } - case TSDB_DATA_TYPE_UTINYINT: comparFn = compareUint8Val; break; - case TSDB_DATA_TYPE_USMALLINT: comparFn = compareUint16Val;break; - case TSDB_DATA_TYPE_UINT: comparFn = compareUint32Val;break; - case TSDB_DATA_TYPE_UBIGINT: comparFn = compareUint64Val;break; + case TSDB_DATA_TYPE_UTINYINT: + comparFn = compareUint8Val; + break; + case TSDB_DATA_TYPE_USMALLINT: + comparFn = compareUint16Val; + break; + case TSDB_DATA_TYPE_UINT: + comparFn = compareUint32Val; + break; + case TSDB_DATA_TYPE_UBIGINT: + comparFn = compareUint64Val; + break; default: comparFn = compareInt32Val; @@ -573,4 +567,106 @@ __compar_fn_t getComparFunc(int32_t type, int32_t optr) { return comparFn; } +__compar_fn_t getKeyComparFunc(int32_t keyType, int32_t order) { + __compar_fn_t comparFn = NULL; + switch (keyType) { + case TSDB_DATA_TYPE_TINYINT: + case TSDB_DATA_TYPE_BOOL: + comparFn = (order == TSDB_ORDER_ASC) ? compareInt8Val : compareInt8ValDesc; + break; + case TSDB_DATA_TYPE_SMALLINT: + comparFn = (order == TSDB_ORDER_ASC) ? compareInt16Val : compareInt16ValDesc; + break; + case TSDB_DATA_TYPE_INT: + comparFn = (order == TSDB_ORDER_ASC) ? compareInt32Val : compareInt32ValDesc; + break; + case TSDB_DATA_TYPE_BIGINT: + case TSDB_DATA_TYPE_TIMESTAMP: + comparFn = (order == TSDB_ORDER_ASC) ? compareInt64Val : compareInt64ValDesc; + break; + case TSDB_DATA_TYPE_FLOAT: + comparFn = (order == TSDB_ORDER_ASC) ? compareFloatVal : compareFloatValDesc; + break; + case TSDB_DATA_TYPE_DOUBLE: + comparFn = (order == TSDB_ORDER_ASC) ? compareDoubleVal : compareDoubleValDesc; + break; + case TSDB_DATA_TYPE_UTINYINT: + comparFn = (order == TSDB_ORDER_ASC) ? compareUint8Val : compareUint8ValDesc; + break; + case TSDB_DATA_TYPE_USMALLINT: + comparFn = (order == TSDB_ORDER_ASC) ? compareUint16Val : compareUint16ValDesc; + break; + case TSDB_DATA_TYPE_UINT: + comparFn = (order == TSDB_ORDER_ASC) ? compareUint32Val : compareUint32ValDesc; + break; + case TSDB_DATA_TYPE_UBIGINT: + comparFn = (order == TSDB_ORDER_ASC) ? compareUint64Val : compareUint64ValDesc; + break; + case TSDB_DATA_TYPE_BINARY: + comparFn = (order == TSDB_ORDER_ASC) ? compareLenPrefixedStr : compareLenPrefixedStrDesc; + break; + case TSDB_DATA_TYPE_NCHAR: + comparFn = (order == TSDB_ORDER_ASC) ? compareLenPrefixedWStr : compareLenPrefixedWStrDesc; + break; + default: + comparFn = (order == TSDB_ORDER_ASC) ? compareInt32Val : compareInt32ValDesc; + break; + } + + return comparFn; +} + +int32_t doCompare(const char *f1, const char *f2, int32_t type, size_t size) { + switch (type) { + case TSDB_DATA_TYPE_INT: + DEFAULT_COMP(GET_INT32_VAL(f1), GET_INT32_VAL(f2)); + case TSDB_DATA_TYPE_DOUBLE: + DEFAULT_DOUBLE_COMP(GET_DOUBLE_VAL(f1), GET_DOUBLE_VAL(f2)); + case TSDB_DATA_TYPE_FLOAT: + DEFAULT_FLOAT_COMP(GET_FLOAT_VAL(f1), GET_FLOAT_VAL(f2)); + case TSDB_DATA_TYPE_BIGINT: + DEFAULT_COMP(GET_INT64_VAL(f1), GET_INT64_VAL(f2)); + case TSDB_DATA_TYPE_SMALLINT: + DEFAULT_COMP(GET_INT16_VAL(f1), GET_INT16_VAL(f2)); + case TSDB_DATA_TYPE_TINYINT: + case TSDB_DATA_TYPE_BOOL: + DEFAULT_COMP(GET_INT8_VAL(f1), GET_INT8_VAL(f2)); + case TSDB_DATA_TYPE_UTINYINT: + DEFAULT_COMP(GET_UINT8_VAL(f1), GET_UINT8_VAL(f2)); + case TSDB_DATA_TYPE_USMALLINT: + DEFAULT_COMP(GET_UINT16_VAL(f1), GET_UINT16_VAL(f2)); + case TSDB_DATA_TYPE_UINT: + DEFAULT_COMP(GET_UINT32_VAL(f1), GET_UINT32_VAL(f2)); + case TSDB_DATA_TYPE_UBIGINT: + DEFAULT_COMP(GET_UINT64_VAL(f1), GET_UINT64_VAL(f2)); + case TSDB_DATA_TYPE_NCHAR: { + tstr *t1 = (tstr *)f1; + tstr *t2 = (tstr *)f2; + + if (t1->len != t2->len) { + return t1->len > t2->len ? 1 : -1; + } + int32_t ret = memcmp((wchar_t *)t1, (wchar_t *)t2, t2->len); + if (ret == 0) { + return ret; + } + return (ret < 0) ? -1 : 1; + } + default: { // todo refactor + tstr *t1 = (tstr *)f1; + tstr *t2 = (tstr *)f2; + + if (t1->len != t2->len) { + return t1->len > t2->len ? 1 : -1; + } else { + int32_t ret = strncmp(t1->data, t2->data, t1->len); + if (ret == 0) { + return 0; + } else { + return ret < 0 ? -1 : 1; + } + } + } + } +} diff --git a/source/util/src/tcompression.c b/source/util/src/tcompression.c index 03a4846c3d..b2332aded7 100644 --- a/source/util/src/tcompression.c +++ b/source/util/src/tcompression.c @@ -16,7 +16,7 @@ /* README.md TAOS compression * * INTEGER Compression Algorithm: - * To compress integers (including char, short, int, int64_t), the difference + * To compress integers (including char, short, int32_t, int64_t), the difference * between two integers is calculated at first. Then the difference is * transformed to positive by zig-zag encoding method * (https://gist.github.com/mfuerstenau/ba870a29e16536fdbaba). Then the value is @@ -47,69 +47,65 @@ * */ -#include "os.h" -#include "lz4.h" -#ifdef TD_TSZ - #include "td_sz.h" -#endif +#define _DEFAULT_SOURCE #include "tcompression.h" +#include "lz4.h" #include "tlog.h" -static const int TEST_NUMBER = 1; -#define is_bigendian() ((*(char *)&TEST_NUMBER) == 0) +#ifdef TD_TSZ +#include "td_sz.h" +#endif + +static const int32_t TEST_NUMBER = 1; +#define is_bigendian() ((*(char *)&TEST_NUMBER) == 0) #define SIMPLE8B_MAX_INT64 ((uint64_t)2305843009213693951L) -#define safeInt64Add(a, b) (((a >= 0) && (b <= INT64_MAX - a)) || ((a < 0) && (b >= INT64_MIN - a))) +#define safeInt64Add(a, b) (((a >= 0) && (b <= INT64_MAX - a)) || ((a < 0) && (b >= INT64_MIN - a))) #define ZIGZAG_ENCODE(T, v) ((u##T)((v) >> (sizeof(T) * 8 - 1))) ^ (((u##T)(v)) << 1) // zigzag encode #define ZIGZAG_DECODE(T, v) ((v) >> 1) ^ -((T)((v)&1)) // zigzag decode #ifdef TD_TSZ -bool lossyFloat = false; +bool lossyFloat = false; bool lossyDouble = false; // init call -int tsCompressInit(){ - // config - if(lossyColumns[0] == 0){ +int32_t tsCompressInit() { + // config + if (lossyColumns[0] == 0) { lossyFloat = false; lossyDouble = false; return 0; } - lossyFloat = strstr(lossyColumns, "float") != NULL; + lossyFloat = strstr(lossyColumns, "float") != NULL; lossyDouble = strstr(lossyColumns, "double") != NULL; - if(lossyFloat == false && lossyDouble == false) - return 0; - + if (lossyFloat == false && lossyDouble == false) return 0; + tdszInit(fPrecision, dPrecision, maxRange, curRange, Compressor); - if(lossyFloat) - uInfo("lossy compression float is opened. "); - if(lossyDouble) - uInfo("lossy compression double is opened. "); + if (lossyFloat) uInfo("lossy compression float is opened. "); + if (lossyDouble) uInfo("lossy compression double is opened. "); return 1; } // exit call -void tsCompressExit(){ - tdszExit(); -} +void tsCompressExit() { tdszExit(); } #endif /* * Compress Integer (Simple8B). */ -int tsCompressINTImp(const char *const input, const int nelements, char *const output, const char type) { +int32_t tsCompressINTImp(const char *const input, const int32_t nelements, char *const output, const char type) { // Selector value: 0 1 2 3 4 5 6 7 8 9 10 11 // 12 13 14 15 - char bit_per_integer[] = {0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 15, 20, 30, 60}; - int selector_to_elems[] = {240, 120, 60, 30, 20, 15, 12, 10, 8, 7, 6, 5, 4, 3, 2, 1}; - char bit_to_selector[] = {0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 11, 11, 12, 12, 12, 13, 13, 13, 13, 13, + char bit_per_integer[] = {0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 15, 20, 30, 60}; + int32_t selector_to_elems[] = {240, 120, 60, 30, 20, 15, 12, 10, 8, 7, 6, 5, 4, 3, 2, 1}; + char bit_to_selector[] = {0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 11, 11, 12, 12, 12, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15}; // get the byte limit. - int word_length = 0; + int32_t word_length = 0; switch (type) { case TSDB_DATA_TYPE_BIGINT: word_length = LONG_BYTES; @@ -128,17 +124,17 @@ int tsCompressINTImp(const char *const input, const int nelements, char *const o return -1; } - int byte_limit = nelements * word_length + 1; - int opos = 1; + int32_t byte_limit = nelements * word_length + 1; + int32_t opos = 1; int64_t prev_value = 0; - for (int i = 0; i < nelements;) { + for (int32_t i = 0; i < nelements;) { char selector = 0; char bit = 0; - int elems = 0; + int32_t elems = 0; int64_t prev_value_tmp = prev_value; - for (int j = i; j < nelements; j++) { + for (int32_t j = i; j < nelements; j++) { // Read data from the input stream and convert it to INT64 type. int64_t curr_value = 0; switch (type) { @@ -172,16 +168,17 @@ int tsCompressINTImp(const char *const input, const int nelements, char *const o tmp_bit = (LONG_BYTES * BITS_PER_BYTE) - BUILDIN_CLZL(zigzag_value); } - if (elems + 1 <= selector_to_elems[(int)selector] && elems + 1 <= selector_to_elems[(int)(bit_to_selector[(int)tmp_bit])]) { + if (elems + 1 <= selector_to_elems[(int32_t)selector] && + elems + 1 <= selector_to_elems[(int32_t)(bit_to_selector[(int32_t)tmp_bit])]) { // If can hold another one. - selector = selector > bit_to_selector[(int)tmp_bit] ? selector : bit_to_selector[(int)tmp_bit]; + selector = selector > bit_to_selector[(int32_t)tmp_bit] ? selector : bit_to_selector[(int32_t)tmp_bit]; elems++; - bit = bit_per_integer[(int)selector]; + bit = bit_per_integer[(int32_t)selector]; } else { // if cannot hold another one. - while (elems < selector_to_elems[(int)selector]) selector++; - elems = selector_to_elems[(int)selector]; - bit = bit_per_integer[(int)selector]; + while (elems < selector_to_elems[(int32_t)selector]) selector++; + elems = selector_to_elems[(int32_t)selector]; + bit = bit_per_integer[(int32_t)selector]; break; } prev_value_tmp = curr_value; @@ -189,7 +186,7 @@ int tsCompressINTImp(const char *const input, const int nelements, char *const o uint64_t buffer = 0; buffer |= (uint64_t)selector; - for (int k = 0; k < elems; k++) { + for (int32_t k = 0; k < elems; k++) { int64_t curr_value = 0; /* get current values */ switch (type) { case TSDB_DATA_TYPE_TINYINT: @@ -229,8 +226,8 @@ int tsCompressINTImp(const char *const input, const int nelements, char *const o return opos; } -int tsDecompressINTImp(const char *const input, const int nelements, char *const output, const char type) { - int word_length = 0; +int32_t tsDecompressINTImp(const char *const input, const int32_t nelements, char *const output, const char type) { + int32_t word_length = 0; switch (type) { case TSDB_DATA_TYPE_BIGINT: word_length = LONG_BYTES; @@ -257,12 +254,12 @@ int tsDecompressINTImp(const char *const input, const int nelements, char *const // Selector value: 0 1 2 3 4 5 6 7 8 9 10 11 // 12 13 14 15 - char bit_per_integer[] = {0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 15, 20, 30, 60}; - int selector_to_elems[] = {240, 120, 60, 30, 20, 15, 12, 10, 8, 7, 6, 5, 4, 3, 2, 1}; + char bit_per_integer[] = {0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 15, 20, 30, 60}; + int32_t selector_to_elems[] = {240, 120, 60, 30, 20, 15, 12, 10, 8, 7, 6, 5, 4, 3, 2, 1}; const char *ip = input + 1; - int count = 0; - int _pos = 0; + int32_t count = 0; + int32_t _pos = 0; int64_t prev_value = 0; while (1) { @@ -271,11 +268,11 @@ int tsDecompressINTImp(const char *const input, const int nelements, char *const uint64_t w = 0; memcpy(&w, ip, LONG_BYTES); - char selector = (char)(w & INT64MASK(4)); // selector = 4 - char bit = bit_per_integer[(int)selector]; // bit = 3 - int elems = selector_to_elems[(int)selector]; + char selector = (char)(w & INT64MASK(4)); // selector = 4 + char bit = bit_per_integer[(int32_t)selector]; // bit = 3 + int32_t elems = selector_to_elems[(int32_t)selector]; - for (int i = 0; i < elems; i++) { + for (int32_t i = 0; i < elems; i++) { uint64_t zigzag_value; if (selector == 0 || selector == 1) { @@ -320,11 +317,11 @@ int tsDecompressINTImp(const char *const input, const int nelements, char *const /* ----------------------------------------------Bool Compression * ---------------------------------------------- */ // TODO: You can also implement it using RLE method. -int tsCompressBoolImp(const char *const input, const int nelements, char *const output) { - int pos = -1; - int ele_per_byte = BITS_PER_BYTE / 2; +int32_t tsCompressBoolImp(const char *const input, const int32_t nelements, char *const output) { + int32_t pos = -1; + int32_t ele_per_byte = BITS_PER_BYTE / 2; - for (int i = 0; i < nelements; i++) { + for (int32_t i = 0; i < nelements; i++) { if (i % ele_per_byte == 0) { pos++; output[pos] = 0; @@ -351,11 +348,11 @@ int tsCompressBoolImp(const char *const input, const int nelements, char *const return pos + 1; } -int tsDecompressBoolImp(const char *const input, const int nelements, char *const output) { - int ipos = -1, opos = 0; - int ele_per_byte = BITS_PER_BYTE / 2; +int32_t tsDecompressBoolImp(const char *const input, const int32_t nelements, char *const output) { + int32_t ipos = -1, opos = 0; + int32_t ele_per_byte = BITS_PER_BYTE / 2; - for (int i = 0; i < nelements; i++) { + for (int32_t i = 0; i < nelements; i++) { if (i % ele_per_byte == 0) { ipos++; } @@ -374,10 +371,10 @@ int tsDecompressBoolImp(const char *const input, const int nelements, char *cons } /* Run Length Encoding(RLE) Method */ -int tsCompressBoolRLEImp(const char *const input, const int nelements, char *const output) { - int _pos = 0; +int32_t tsCompressBoolRLEImp(const char *const input, const int32_t nelements, char *const output) { + int32_t _pos = 0; - for (int i = 0; i < nelements;) { + for (int32_t i = 0; i < nelements;) { unsigned char counter = 1; char num = input[i]; @@ -407,8 +404,8 @@ int tsCompressBoolRLEImp(const char *const input, const int nelements, char *con return _pos; } -int tsDecompressBoolRLEImp(const char *const input, const int nelements, char *const output) { - int ipos = 0, opos = 0; +int32_t tsDecompressBoolRLEImp(const char *const input, const int32_t nelements, char *const output) { + int32_t ipos = 0, opos = 0; while (1) { char encode = input[ipos++]; unsigned counter = (encode >> 1) & INT8MASK(7); @@ -427,9 +424,9 @@ int tsDecompressBoolRLEImp(const char *const input, const int nelements, char *c // Note: the size of the output must be larger than input_size + 1 and // LZ4_compressBound(size) + 1; // >= max(input_size, LZ4_compressBound(input_size)) + 1; -int tsCompressStringImp(const char *const input, int inputSize, char *const output, int outputSize) { +int32_t tsCompressStringImp(const char *const input, int32_t inputSize, char *const output, int32_t outputSize) { // Try to compress using LZ4 algorithm. - const int compressed_data_size = LZ4_compress_default(input, output + 1, inputSize, outputSize-1); + const int32_t compressed_data_size = LZ4_compress_default(input, output + 1, inputSize, outputSize - 1); // If cannot compress or after compression, data becomes larger. if (compressed_data_size <= 0 || compressed_data_size > inputSize) { @@ -443,12 +440,12 @@ int tsCompressStringImp(const char *const input, int inputSize, char *const outp return compressed_data_size + 1; } -int tsDecompressStringImp(const char *const input, int compressedSize, char *const output, int outputSize) { +int32_t tsDecompressStringImp(const char *const input, int32_t compressedSize, char *const output, int32_t outputSize) { // compressedSize is the size of data after compression. - + if (input[0] == 1) { /* It is compressed by LZ4 algorithm */ - const int decompressed_size = LZ4_decompress_safe(input + 1, output, compressedSize - 1, outputSize); + const int32_t decompressed_size = LZ4_decompress_safe(input + 1, output, compressedSize - 1, outputSize); if (decompressed_size < 0) { uError("Failed to decompress string with LZ4 algorithm, decompressed size:%d", decompressed_size); return -1; @@ -468,24 +465,24 @@ int tsDecompressStringImp(const char *const input, int compressedSize, char *con /* --------------------------------------------Timestamp Compression * ---------------------------------------------- */ // TODO: Take care here, we assumes little endian encoding. -int tsCompressTimestampImp(const char *const input, const int nelements, char *const output) { - int _pos = 1; +int32_t tsCompressTimestampImp(const char *const input, const int32_t nelements, char *const output) { + int32_t _pos = 1; assert(nelements >= 0); if (nelements == 0) return 0; int64_t *istream = (int64_t *)input; - int64_t prev_value = istream[0]; - if(prev_value >= 0x8000000000000000) { - uWarn("compression timestamp is over signed long long range. ts = 0x%"PRIx64" \n", prev_value); - goto _exit_over; + int64_t prev_value = istream[0]; + if (prev_value >= 0x8000000000000000) { + uWarn("compression timestamp is over signed long long range. ts = 0x%" PRIx64 " \n", prev_value); + goto _exit_over; } int64_t prev_delta = -prev_value; uint8_t flags = 0, flag1 = 0, flag2 = 0; uint64_t dd1 = 0, dd2 = 0; - for (int i = 0; i < nelements; i++) { + for (int32_t i = 0; i < nelements; i++) { int64_t curr_value = istream[i]; if (!safeInt64Add(curr_value, -prev_value)) goto _exit_over; int64_t curr_delta = curr_value - prev_value; @@ -564,7 +561,7 @@ _exit_over: return nelements * LONG_BYTES + 1; } -int tsDecompressTimestampImp(const char *const input, const int nelements, char *const output) { +int32_t tsDecompressTimestampImp(const char *const input, const int32_t nelements, char *const output) { assert(nelements >= 0); if (nelements == 0) return 0; @@ -574,7 +571,7 @@ int tsDecompressTimestampImp(const char *const input, const int nelements, char } else if (input[0] == 1) { // Decompress int64_t *ostream = (int64_t *)output; - int ipos = 1, opos = 0; + int32_t ipos = 1, opos = 0; int8_t nbytes = 0; int64_t prev_value = 0; int64_t prev_delta = 0; @@ -635,9 +632,9 @@ int tsDecompressTimestampImp(const char *const input, const int nelements, char } /* --------------------------------------------Double Compression * ---------------------------------------------- */ -void encodeDoubleValue(uint64_t diff, uint8_t flag, char *const output, int *const pos) { +void encodeDoubleValue(uint64_t diff, uint8_t flag, char *const output, int32_t *const pos) { uint8_t nbytes = (flag & INT8MASK(3)) + 1; - int nshift = (LONG_BYTES * BITS_PER_BYTE - nbytes * BITS_PER_BYTE) * (flag >> 3); + int32_t nshift = (LONG_BYTES * BITS_PER_BYTE - nbytes * BITS_PER_BYTE) * (flag >> 3); diff >>= nshift; while (nbytes) { @@ -647,9 +644,9 @@ void encodeDoubleValue(uint64_t diff, uint8_t flag, char *const output, int *con } } -int tsCompressDoubleImp(const char *const input, const int nelements, char *const output) { - int byte_limit = nelements * DOUBLE_BYTES + 1; - int opos = 1; +int32_t tsCompressDoubleImp(const char *const input, const int32_t nelements, char *const output) { + int32_t byte_limit = nelements * DOUBLE_BYTES + 1; + int32_t opos = 1; uint64_t prev_value = 0; uint64_t prev_diff = 0; @@ -658,7 +655,7 @@ int tsCompressDoubleImp(const char *const input, const int nelements, char *cons double *istream = (double *)input; // Main loop - for (int i = 0; i < nelements; i++) { + for (int32_t i = 0; i < nelements; i++) { union { double real; uint64_t bits; @@ -670,8 +667,8 @@ int tsCompressDoubleImp(const char *const input, const int nelements, char *cons uint64_t predicted = prev_value; uint64_t diff = curr.bits ^ predicted; - int leading_zeros = LONG_BYTES * BITS_PER_BYTE; - int trailing_zeros = leading_zeros; + int32_t leading_zeros = LONG_BYTES * BITS_PER_BYTE; + int32_t trailing_zeros = leading_zeros; if (diff) { trailing_zeros = BUILDIN_CTZL(diff); @@ -696,8 +693,8 @@ int tsCompressDoubleImp(const char *const input, const int nelements, char *cons prev_diff = diff; prev_flag = flag; } else { - int nbyte1 = (prev_flag & INT8MASK(3)) + 1; - int nbyte2 = (flag & INT8MASK(3)) + 1; + int32_t nbyte1 = (prev_flag & INT8MASK(3)) + 1; + int32_t nbyte2 = (flag & INT8MASK(3)) + 1; if (opos + 1 + nbyte1 + nbyte2 <= byte_limit) { uint8_t flags = prev_flag | (flag << 4); output[opos++] = flags; @@ -713,8 +710,8 @@ int tsCompressDoubleImp(const char *const input, const int nelements, char *cons } if (nelements % 2) { - int nbyte1 = (prev_flag & INT8MASK(3)) + 1; - int nbyte2 = 1; + int32_t nbyte1 = (prev_flag & INT8MASK(3)) + 1; + int32_t nbyte2 = 1; if (opos + 1 + nbyte1 + nbyte2 <= byte_limit) { uint8_t flags = prev_flag; output[opos++] = flags; @@ -731,19 +728,19 @@ int tsCompressDoubleImp(const char *const input, const int nelements, char *cons return opos; } -uint64_t decodeDoubleValue(const char *const input, int *const ipos, uint8_t flag) { +uint64_t decodeDoubleValue(const char *const input, int32_t *const ipos, uint8_t flag) { uint64_t diff = 0ul; - int nbytes = (flag & INT8MASK(3)) + 1; - for (int i = 0; i < nbytes; i++) { + int32_t nbytes = (flag & INT8MASK(3)) + 1; + for (int32_t i = 0; i < nbytes; i++) { diff = diff | ((INT64MASK(8) & input[(*ipos)++]) << BITS_PER_BYTE * i); } - int shift_width = (LONG_BYTES * BITS_PER_BYTE - nbytes * BITS_PER_BYTE) * (flag >> 3); + int32_t shift_width = (LONG_BYTES * BITS_PER_BYTE - nbytes * BITS_PER_BYTE) * (flag >> 3); diff <<= shift_width; return diff; } -int tsDecompressDoubleImp(const char *const input, const int nelements, char *const output) { +int32_t tsDecompressDoubleImp(const char *const input, const int32_t nelements, char *const output) { // output stream double *ostream = (double *)output; @@ -753,11 +750,11 @@ int tsDecompressDoubleImp(const char *const input, const int nelements, char *co } uint8_t flags = 0; - int ipos = 1; - int opos = 0; + int32_t ipos = 1; + int32_t opos = 0; uint64_t prev_value = 0; - for (int i = 0; i < nelements; i++) { + for (int32_t i = 0; i < nelements; i++) { if (i % 2 == 0) { flags = input[ipos++]; } @@ -783,9 +780,9 @@ int tsDecompressDoubleImp(const char *const input, const int nelements, char *co /* --------------------------------------------Float Compression * ---------------------------------------------- */ -void encodeFloatValue(uint32_t diff, uint8_t flag, char *const output, int *const pos) { +void encodeFloatValue(uint32_t diff, uint8_t flag, char *const output, int32_t *const pos) { uint8_t nbytes = (flag & INT8MASK(3)) + 1; - int nshift = (FLOAT_BYTES * BITS_PER_BYTE - nbytes * BITS_PER_BYTE) * (flag >> 3); + int32_t nshift = (FLOAT_BYTES * BITS_PER_BYTE - nbytes * BITS_PER_BYTE) * (flag >> 3); diff >>= nshift; while (nbytes) { @@ -795,17 +792,17 @@ void encodeFloatValue(uint32_t diff, uint8_t flag, char *const output, int *cons } } -int tsCompressFloatImp(const char *const input, const int nelements, char *const output) { - float *istream = (float *)input; - int byte_limit = nelements * FLOAT_BYTES + 1; - int opos = 1; +int32_t tsCompressFloatImp(const char *const input, const int32_t nelements, char *const output) { + float *istream = (float *)input; + int32_t byte_limit = nelements * FLOAT_BYTES + 1; + int32_t opos = 1; uint32_t prev_value = 0; uint32_t prev_diff = 0; uint8_t prev_flag = 0; // Main loop - for (int i = 0; i < nelements; i++) { + for (int32_t i = 0; i < nelements; i++) { union { float real; uint32_t bits; @@ -817,8 +814,8 @@ int tsCompressFloatImp(const char *const input, const int nelements, char *const uint32_t predicted = prev_value; uint32_t diff = curr.bits ^ predicted; - int leading_zeros = FLOAT_BYTES * BITS_PER_BYTE; - int trailing_zeros = leading_zeros; + int32_t leading_zeros = FLOAT_BYTES * BITS_PER_BYTE; + int32_t trailing_zeros = leading_zeros; if (diff) { trailing_zeros = BUILDIN_CTZ(diff); @@ -843,8 +840,8 @@ int tsCompressFloatImp(const char *const input, const int nelements, char *const prev_diff = diff; prev_flag = flag; } else { - int nbyte1 = (prev_flag & INT8MASK(3)) + 1; - int nbyte2 = (flag & INT8MASK(3)) + 1; + int32_t nbyte1 = (prev_flag & INT8MASK(3)) + 1; + int32_t nbyte2 = (flag & INT8MASK(3)) + 1; if (opos + 1 + nbyte1 + nbyte2 <= byte_limit) { uint8_t flags = prev_flag | (flag << 4); output[opos++] = flags; @@ -860,8 +857,8 @@ int tsCompressFloatImp(const char *const input, const int nelements, char *const } if (nelements % 2) { - int nbyte1 = (prev_flag & INT8MASK(3)) + 1; - int nbyte2 = 1; + int32_t nbyte1 = (prev_flag & INT8MASK(3)) + 1; + int32_t nbyte2 = 1; if (opos + 1 + nbyte1 + nbyte2 <= byte_limit) { uint8_t flags = prev_flag; output[opos++] = flags; @@ -878,19 +875,19 @@ int tsCompressFloatImp(const char *const input, const int nelements, char *const return opos; } -uint32_t decodeFloatValue(const char *const input, int *const ipos, uint8_t flag) { +uint32_t decodeFloatValue(const char *const input, int32_t *const ipos, uint8_t flag) { uint32_t diff = 0ul; - int nbytes = (flag & INT8MASK(3)) + 1; - for (int i = 0; i < nbytes; i++) { + int32_t nbytes = (flag & INT8MASK(3)) + 1; + for (int32_t i = 0; i < nbytes; i++) { diff = diff | ((INT32MASK(8) & input[(*ipos)++]) << BITS_PER_BYTE * i); } - int shift_width = (FLOAT_BYTES * BITS_PER_BYTE - nbytes * BITS_PER_BYTE) * (flag >> 3); + int32_t shift_width = (FLOAT_BYTES * BITS_PER_BYTE - nbytes * BITS_PER_BYTE) * (flag >> 3); diff <<= shift_width; return diff; } -int tsDecompressFloatImp(const char *const input, const int nelements, char *const output) { +int32_t tsDecompressFloatImp(const char *const input, const int32_t nelements, char *const output) { float *ostream = (float *)output; if (input[0] == 1) { @@ -899,11 +896,11 @@ int tsDecompressFloatImp(const char *const input, const int nelements, char *con } uint8_t flags = 0; - int ipos = 1; - int opos = 0; + int32_t ipos = 1; + int32_t opos = 0; uint32_t prev_value = 0; - for (int i = 0; i < nelements; i++) { + for (int32_t i = 0; i < nelements; i++) { if (i % 2 == 0) { flags = input[ipos++]; } @@ -927,15 +924,15 @@ int tsDecompressFloatImp(const char *const input, const int nelements, char *con return nelements * FLOAT_BYTES; } -#ifdef TD_TSZ +#ifdef TD_TSZ // // ---------- float double lossy ----------- // -int tsCompressFloatLossyImp(const char * input, const int nelements, char *const output){ - // compress with sz - int compressedSize = tdszCompress(SZ_FLOAT, input, nelements, output + 1); +int32_t tsCompressFloatLossyImp(const char *input, const int32_t nelements, char *const output) { + // compress with sz + int32_t compressedSize = tdszCompress(SZ_FLOAT, input, nelements, output + 1); unsigned char algo = ALGO_SZ_LOSSY << 1; - if (compressedSize == 0 || compressedSize >= nelements*sizeof(float)){ + if (compressedSize == 0 || compressedSize >= nelements * sizeof(float)) { // compressed error or large than original output[0] = MODE_NOCOMPRESS | algo; memcpy(output + 1, input, nelements * sizeof(float)); @@ -949,25 +946,26 @@ int tsCompressFloatLossyImp(const char * input, const int nelements, char *const return compressedSize; } -int tsDecompressFloatLossyImp(const char * input, int compressedSize, const int nelements, char *const output){ - int decompressedSize = 0; - if( HEAD_MODE(input[0]) == MODE_NOCOMPRESS){ +int32_t tsDecompressFloatLossyImp(const char *input, int32_t compressedSize, const int32_t nelements, + char *const output) { + int32_t decompressedSize = 0; + if (HEAD_MODE(input[0]) == MODE_NOCOMPRESS) { // orginal so memcpy directly decompressedSize = nelements * sizeof(float); memcpy(output, input + 1, decompressedSize); return decompressedSize; - } + } // decompressed with sz return tdszDecompress(SZ_FLOAT, input + 1, compressedSize - 1, nelements, output); } -int tsCompressDoubleLossyImp(const char * input, const int nelements, char *const output){ - // compress with sz - int compressedSize = tdszCompress(SZ_DOUBLE, input, nelements, output + 1); +int32_t tsCompressDoubleLossyImp(const char *input, const int32_t nelements, char *const output) { + // compress with sz + int32_t compressedSize = tdszCompress(SZ_DOUBLE, input, nelements, output + 1); unsigned char algo = ALGO_SZ_LOSSY << 1; - if (compressedSize == 0 || compressedSize >= nelements*sizeof(double)) { + if (compressedSize == 0 || compressedSize >= nelements * sizeof(double)) { // compressed error or large than original output[0] = MODE_NOCOMPRESS | algo; memcpy(output + 1, input, nelements * sizeof(double)); @@ -978,18 +976,19 @@ int tsCompressDoubleLossyImp(const char * input, const int nelements, char *cons compressedSize += 1; } - return compressedSize; + return compressedSize; } -int tsDecompressDoubleLossyImp(const char * input, int compressedSize, const int nelements, char *const output){ - int decompressedSize = 0; - if( HEAD_MODE(input[0]) == MODE_NOCOMPRESS){ +int32_t tsDecompressDoubleLossyImp(const char *input, int32_t compressedSize, const int32_t nelements, + char *const output) { + int32_t decompressedSize = 0; + if (HEAD_MODE(input[0]) == MODE_NOCOMPRESS) { // orginal so memcpy directly decompressedSize = nelements * sizeof(double); memcpy(output, input + 1, decompressedSize); return decompressedSize; - } + } // decompressed with sz return tdszDecompress(SZ_DOUBLE, input + 1, compressedSize - 1, nelements, output); diff --git a/source/util/src/tconfig.c b/source/util/src/tconfig.c index dc9fa33595..b495e1ade1 100644 --- a/source/util/src/tconfig.c +++ b/source/util/src/tconfig.c @@ -16,19 +16,12 @@ #define _DEFAULT_SOURCE #include "tconfig.h" #include "taoserror.h" -#include "tcfg.h" -#include "thash.h" -#include "tutil.h" #include "tlog.h" +#include "tutil.h" #define CFG_NAME_PRINT_LEN 24 #define CFG_SRC_PRINT_LEN 12 -typedef struct SConfig { - ECfgSrcType stype; - SHashObj *hash; -} SConfig; - int32_t cfgLoadFromCfgFile(SConfig *pConfig, const char *filepath); int32_t cfgLoadFromEnvFile(SConfig *pConfig, const char *filepath); int32_t cfgLoadFromEnvVar(SConfig *pConfig); @@ -42,8 +35,8 @@ SConfig *cfgInit() { return NULL; } - pCfg->hash = taosHashInit(16, MurmurHash3_32, false, HASH_NO_LOCK); - if (pCfg->hash == NULL) { + pCfg->array = taosArrayInit(32, sizeof(SConfigItem)); + if (pCfg->array == NULL) { free(pCfg); terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; @@ -92,25 +85,18 @@ static void cfgFreeItem(SConfigItem *pItem) { void cfgCleanup(SConfig *pCfg) { if (pCfg != NULL) { - if (pCfg->hash != NULL) { - SConfigItem *pItem = taosHashIterate(pCfg->hash, NULL); - while (pItem != NULL) { - cfgFreeItem(pItem); - tfree(pItem->name); - pItem = taosHashIterate(pCfg->hash, pItem); - } - taosHashCleanup(pCfg->hash); - pCfg->hash == NULL; + int32_t size = taosArrayGetSize(pCfg->array); + for (int32_t i = 0; i < size; ++i) { + SConfigItem *pItem = taosArrayGet(pCfg->array, i); + cfgFreeItem(pItem); + tfree(pItem->name); } + taosArrayDestroy(pCfg->array); free(pCfg); } } -int32_t cfgGetSize(SConfig *pCfg) { return taosHashGetSize(pCfg->hash); } - -SConfigItem *cfgIterate(SConfig *pCfg, SConfigItem *pIter) { return taosHashIterate(pCfg->hash, pIter); } - -void cfgCancelIterate(SConfig *pCfg, SConfigItem *pIter) { return taosHashCancelIterate(pCfg->hash, pIter); } +int32_t cfgGetSize(SConfig *pCfg) { return taosArrayGetSize(pCfg->array); } static int32_t cfgCheckAndSetTimezone(SConfigItem *pItem, const char *timezone) { cfgFreeItem(pItem); @@ -359,16 +345,16 @@ int32_t cfgSetItem(SConfig *pCfg, const char *name, const char *value, ECfgSrcTy } SConfigItem *cfgGetItem(SConfig *pCfg, const char *name) { - int32_t len = strlen(name); - char lowcaseName[CFG_NAME_MAX_LEN + 1] = {0}; - strntolower(lowcaseName, name, TMIN(CFG_NAME_MAX_LEN, len)); - - SConfigItem *pItem = taosHashGet(pCfg->hash, lowcaseName, len + 1); - if (pItem == NULL) { - terrno = TSDB_CODE_CFG_NOT_FOUND; + int32_t size = taosArrayGetSize(pCfg->array); + for (int32_t i = 0; i < size; ++i) { + SConfigItem *pItem = taosArrayGet(pCfg->array, i); + if (strcasecmp(pItem->name, name) == 0) { + return pItem; + } } - return pItem; + terrno = TSDB_CODE_CFG_NOT_FOUND; + return NULL; } static int32_t cfgAddItem(SConfig *pCfg, SConfigItem *pItem, const char *name) { @@ -383,7 +369,7 @@ static int32_t cfgAddItem(SConfig *pCfg, SConfigItem *pItem, const char *name) { char lowcaseName[CFG_NAME_MAX_LEN + 1] = {0}; strntolower(lowcaseName, name, TMIN(CFG_NAME_MAX_LEN, len)); - if (taosHashPut(pCfg->hash, lowcaseName, len + 1, pItem, sizeof(SConfigItem)) != 0) { + if (taosArrayPush(pCfg->array, pItem) == NULL) { if (pItem->dtype == CFG_DTYPE_STRING) { free(pItem->str); } @@ -536,8 +522,9 @@ void cfgDumpCfg(SConfig *pCfg, bool tsc, bool dump) { char src[CFG_SRC_PRINT_LEN + 1] = {0}; char name[CFG_NAME_PRINT_LEN + 1] = {0}; - SConfigItem *pItem = cfgIterate(pCfg, NULL); - while (pItem != NULL) { + int32_t size = taosArrayGetSize(pCfg->array); + for (int32_t i = 0; i < size; ++i) { + SConfigItem *pItem = taosArrayGet(pCfg->array, i); if (tsc && !pItem->tsc) continue; tstrncpy(src, cfgStypeStr(pItem->stype), CFG_SRC_PRINT_LEN); for (int32_t i = 0; i < CFG_SRC_PRINT_LEN; ++i) { @@ -596,7 +583,6 @@ void cfgDumpCfg(SConfig *pCfg, bool tsc, bool dump) { } break; } - pItem = cfgIterate(pCfg, pItem); } if (dump) { @@ -619,7 +605,7 @@ int32_t cfgLoadFromEnvFile(SConfig *pConfig, const char *filepath) { int32_t cfgLoadFromCfgFile(SConfig *pConfig, const char *filepath) { char *line = NULL, *name, *value, *value2, *value3; - int olen, vlen, vlen2, vlen3; + int32_t olen, vlen, vlen2, vlen3; ssize_t _bytes = 0; // FILE *fp = fopen(filepath, "r"); diff --git a/source/util/src/tcrc32c.c b/source/util/src/tcrc32c.c index 73bba4480e..f7e1060be0 100644 --- a/source/util/src/tcrc32c.c +++ b/source/util/src/tcrc32c.c @@ -18,7 +18,7 @@ 3. This notice may not be removed or altered from any source distribution. */ -#include "os.h" +#define _DEFAULT_SOURCE #include "tcrc32c.h" #include "tdef.h" @@ -26,8 +26,6 @@ #include #endif - - #define POLY 0x82f63b78 #define LONG_SHIFT 8192 #define SHORT_SHIFT 256 @@ -1097,7 +1095,7 @@ static uint32_t short_shifts[4][256] = { static uint32_t append_trivial(uint32_t crc, crc_stream input, size_t length) { for (size_t i = 0; i < length; ++i) { crc = crc ^ input[i]; - for (int j = 0; j < 8; j++) + for (int32_t j = 0; j < 8; j++) crc = (crc >> 1) ^ 0x80000000 ^ ((~crc & 1) * POLY); } return crc; @@ -1358,7 +1356,7 @@ void taosResolveCRC() { #if defined _TD_ARM_ || defined _TD_MIPS_ || defined WINDOWS crc32c = crc32c_sf; #else - int sse42; + int32_t sse42; SSE42(sse42); crc32c = sse42 ? crc32c_hw : crc32c_sf; #endif @@ -1371,10 +1369,10 @@ void taosResolveCRC() { #include #include -int main(int argc, char *argv[]) { +int32_t main(int32_t argc, char *argv[]) { char str[1024] = "\0"; char *ptr = str; - int count = 0; + int32_t count = 0; while ((count = read(0, ptr, 10)) > 0) { ptr += count; } diff --git a/source/util/src/tdes.c b/source/util/src/tdes.c index f72ddcaa3b..105dd7f95f 100644 --- a/source/util/src/tdes.c +++ b/source/util/src/tdes.c @@ -13,43 +13,44 @@ * along with this program. If not, see . */ -#include "os.h" -#include "tkey.h" +#define _DEFAULT_SOURCE +#include "tdes.h" #define ENCRYPTION_MODE 1 #define DECRYPTION_MODE 0 typedef struct { - unsigned char k[8]; - unsigned char c[4]; - unsigned char d[4]; + uint8_t k[8]; + uint8_t c[4]; + uint8_t d[4]; } key_set; -void generate_key(unsigned char* key); -void generate_sub_keys(unsigned char* main_key, key_set* key_sets); -void process_message(unsigned char* message_piece, unsigned char* processed_piece, key_set* key_sets, int mode); +void generate_key(uint8_t* key); +void generate_sub_keys(uint8_t* main_key, key_set* key_sets); +void process_message(uint8_t* message_piece, uint8_t* processed_piece, key_set* key_sets, int32_t mode); #if 0 int64_t taosDesGenKey() { - unsigned int iseed = (unsigned int)time(NULL); + uint32_t iseed = (uint32_t)time(NULL); srand(iseed); - unsigned char key[8] = {0}; + uint8_t key[8] = {0}; generate_key(key); return *((int64_t*)key); } #endif -char* taosDesImp(unsigned char* key, char* src, unsigned int len, int process_mode) { - unsigned int number_of_blocks = len / 8; - unsigned char data_block[9] = {0}; - unsigned char processed_block[9] = {0}; - key_set key_sets[17]; memset(key_sets, 0, sizeof(key_sets)); - char* dest = calloc(len + 1, 1); +char* taosDesImp(uint8_t* key, char* src, uint32_t len, int32_t process_mode) { + uint32_t number_of_blocks = len / 8; + uint8_t data_block[9] = {0}; + uint8_t processed_block[9] = {0}; + key_set key_sets[17]; + memset(key_sets, 0, sizeof(key_sets)); + char* dest = calloc(len + 1, 1); generate_sub_keys(key, key_sets); - for (unsigned int block_count = 0; block_count < number_of_blocks; block_count++) { + for (uint32_t block_count = 0; block_count < number_of_blocks; block_count++) { memset(processed_block, 0, 8); memcpy(data_block, src + block_count * 8, 8); process_message(data_block, processed_block, key_sets, process_mode); @@ -59,15 +60,15 @@ char* taosDesImp(unsigned char* key, char* src, unsigned int len, int process_mo return dest; } -char* taosDesEncode(int64_t key, char* src, int len) { +char* taosDesEncode(int64_t key, char* src, int32_t len) { if (len % 8 != 0) return NULL; - unsigned char* keyStr = (unsigned char*)(&key); + uint8_t* keyStr = (uint8_t*)(&key); return taosDesImp(keyStr, src, len, ENCRYPTION_MODE); } -char* taosDesDecode(int64_t key, char* src, int len) { - unsigned char* keyStr = (unsigned char*)(&key); - char* temp = calloc(len + 8, 1); +char* taosDesDecode(int64_t key, char* src, int32_t len) { + uint8_t* keyStr = (uint8_t*)(&key); + char* temp = calloc(len + 8, 1); memcpy(temp, src, len); len += 8; @@ -77,67 +78,67 @@ char* taosDesDecode(int64_t key, char* src, int len) { return decode; } -int initial_key_permutaion[] = {57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, - 35, 27, 19, 11, 3, 60, 52, 44, 36, 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, - 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4}; +int32_t initial_key_permutaion[] = {57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, + 35, 27, 19, 11, 3, 60, 52, 44, 36, 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, + 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4}; -int initial_message_permutation[] = {58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, - 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, - 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, - 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7}; +int32_t initial_message_permutation[] = {58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, + 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, + 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, + 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7}; -int key_shift_sizes[] = {-1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1}; +int32_t key_shift_sizes[] = {-1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1}; -int sub_key_permutation[] = {14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4, - 26, 8, 16, 7, 27, 20, 13, 2, 41, 52, 31, 37, 47, 55, 30, 40, - 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32}; +int32_t sub_key_permutation[] = {14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4, + 26, 8, 16, 7, 27, 20, 13, 2, 41, 52, 31, 37, 47, 55, 30, 40, + 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32}; -int message_expansion[] = {32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9, 10, 11, - 12, 13, 12, 13, 14, 15, 16, 17, 16, 17, 18, 19, 20, 21, 20, 21, - 22, 23, 24, 25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1}; +int32_t message_expansion[] = {32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9, 10, 11, + 12, 13, 12, 13, 14, 15, 16, 17, 16, 17, 18, 19, 20, 21, 20, 21, + 22, 23, 24, 25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1}; -int S1[] = {14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, 0, 15, 7, 4, 14, 2, - 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, - 3, 10, 5, 0, 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13}; +int32_t S1[] = {14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, 0, 15, 7, 4, 14, 2, + 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, + 3, 10, 5, 0, 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13}; -int S2[] = {15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, 3, 13, 4, 7, 15, 2, - 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, - 9, 3, 2, 15, 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9}; +int32_t S2[] = {15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, 3, 13, 4, 7, 15, 2, + 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, + 9, 3, 2, 15, 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9}; -int S3[] = {10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, 13, 7, 0, 9, 3, 4, - 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, - 5, 10, 14, 7, 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12}; +int32_t S3[] = {10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, 13, 7, 0, 9, 3, 4, + 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, + 5, 10, 14, 7, 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12}; -int S4[] = {7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, 13, 8, 11, 5, 6, 15, - 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, - 5, 2, 8, 4, 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14}; +int32_t S4[] = {7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, 13, 8, 11, 5, 6, 15, + 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, + 5, 2, 8, 4, 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14}; -int S5[] = {2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, 14, 11, 2, 12, 4, 7, - 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, - 6, 3, 0, 14, 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3}; +int32_t S5[] = {2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, 14, 11, 2, 12, 4, 7, + 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, + 6, 3, 0, 14, 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3}; -int S6[] = {12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, 10, 15, 4, 2, 7, 12, - 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, - 1, 13, 11, 6, 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13}; +int32_t S6[] = {12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, 10, 15, 4, 2, 7, 12, + 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, + 1, 13, 11, 6, 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13}; -int S7[] = {4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, 13, 0, 11, 7, 4, 9, - 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, - 0, 5, 9, 2, 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12}; +int32_t S7[] = {4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, 13, 0, 11, 7, 4, 9, + 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, + 0, 5, 9, 2, 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12}; -int S8[] = {13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, 1, 15, 13, 8, 10, 3, - 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, - 15, 3, 5, 8, 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11}; +int32_t S8[] = {13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, 1, 15, 13, 8, 10, 3, + 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, + 15, 3, 5, 8, 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11}; -int right_sub_message_permutation[] = {16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, - 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25}; +int32_t right_sub_message_permutation[] = {16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, + 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25}; -int final_message_permutation[] = {40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, - 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29, - 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27, - 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25}; +int32_t final_message_permutation[] = {40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, + 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29, + 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27, + 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25}; void print_char_as_binary(char input) { - int i; + int32_t i; for (i = 0; i < 8; i++) { char shift_byte = 0x01 << (7 - i); if (shift_byte & input) { @@ -148,15 +149,15 @@ void print_char_as_binary(char input) { } } -void generate_key(unsigned char* key) { - int i; +void generate_key(uint8_t* key) { + int32_t i; for (i = 0; i < 8; i++) { key[i] = taosRand() % 255; } } void print_key_set(key_set _key_set) { - int i; + int32_t i; printf("K: \n"); for (i = 0; i < 8; i++) { printf("%02X : ", _key_set.k[i]); @@ -180,10 +181,10 @@ void print_key_set(key_set _key_set) { printf("\n"); } -void generate_sub_keys(unsigned char* main_key, key_set* key_sets) { - int i, j; - int shift_size; - unsigned char shift_byte, first_shift_bits, second_shift_bits, third_shift_bits, fourth_shift_bits; +void generate_sub_keys(uint8_t* main_key, key_set* key_sets) { + int32_t i, j; + int32_t shift_size; + uint8_t shift_byte, first_shift_bits, second_shift_bits, third_shift_bits, fourth_shift_bits; for (i = 0; i < 8; i++) { key_sets[0].k[i] = 0; @@ -277,12 +278,12 @@ void generate_sub_keys(unsigned char* main_key, key_set* key_sets) { } } -void process_message(unsigned char* message_piece, unsigned char* processed_piece, key_set* key_sets, int mode) { - int i, k; - int shift_size; - unsigned char shift_byte; +void process_message(uint8_t* message_piece, uint8_t* processed_piece, key_set* key_sets, int32_t mode) { + int32_t i, k; + int32_t shift_size; + uint8_t shift_byte; - unsigned char initial_permutation[8]; + uint8_t initial_permutation[8]; memset(initial_permutation, 0, 8); memset(processed_piece, 0, 8); @@ -295,15 +296,15 @@ void process_message(unsigned char* message_piece, unsigned char* processed_piec initial_permutation[i / 8] |= (shift_byte >> i % 8); } - unsigned char l[4], r[4]; + uint8_t l[4], r[4]; for (i = 0; i < 4; i++) { l[i] = initial_permutation[i]; r[i] = initial_permutation[i + 4]; } - unsigned char ln[4], rn[4], er[6], ser[4]; + uint8_t ln[4], rn[4], er[6], ser[4]; - int key_index; + int32_t key_index; for (k = 1; k <= 16; k++) { memcpy(ln, r, 4); @@ -328,7 +329,7 @@ void process_message(unsigned char* message_piece, unsigned char* processed_piec er[i] ^= key_sets[key_index].k[i]; } - unsigned char row, column; + uint8_t row, column; for (i = 0; i < 4; i++) { ser[i] = 0; @@ -345,7 +346,7 @@ void process_message(unsigned char* message_piece, unsigned char* processed_piec column = 0; column |= ((er[0] & 0x78) >> 3); - ser[0] |= ((unsigned char)S1[row * 16 + column] << 4); + ser[0] |= ((uint8_t)S1[row * 16 + column] << 4); row = 0; row |= (er[0] & 0x02); @@ -355,7 +356,7 @@ void process_message(unsigned char* message_piece, unsigned char* processed_piec column |= ((er[0] & 0x01) << 3); column |= ((er[1] & 0xE0) >> 5); - ser[0] |= (unsigned char)S2[row * 16 + column]; + ser[0] |= (uint8_t)S2[row * 16 + column]; // Byte 2 row = 0; @@ -366,7 +367,7 @@ void process_message(unsigned char* message_piece, unsigned char* processed_piec column |= ((er[1] & 0x07) << 1); column |= ((er[2] & 0x80) >> 7); - ser[1] |= ((unsigned char)S3[row * 16 + column] << 4); + ser[1] |= ((uint8_t)S3[row * 16 + column] << 4); row = 0; row |= ((er[2] & 0x20) >> 4); @@ -375,7 +376,7 @@ void process_message(unsigned char* message_piece, unsigned char* processed_piec column = 0; column |= ((er[2] & 0x1E) >> 1); - ser[1] |= (unsigned char)S4[row * 16 + column]; + ser[1] |= (uint8_t)S4[row * 16 + column]; // Byte 3 row = 0; @@ -385,7 +386,7 @@ void process_message(unsigned char* message_piece, unsigned char* processed_piec column = 0; column |= ((er[3] & 0x78) >> 3); - ser[2] |= ((unsigned char)S5[row * 16 + column] << 4); + ser[2] |= ((uint8_t)S5[row * 16 + column] << 4); row = 0; row |= (er[3] & 0x02); @@ -395,7 +396,7 @@ void process_message(unsigned char* message_piece, unsigned char* processed_piec column |= ((er[3] & 0x01) << 3); column |= ((er[4] & 0xE0) >> 5); - ser[2] |= (unsigned char)S6[row * 16 + column]; + ser[2] |= (uint8_t)S6[row * 16 + column]; // Byte 4 row = 0; @@ -406,7 +407,7 @@ void process_message(unsigned char* message_piece, unsigned char* processed_piec column |= ((er[4] & 0x07) << 1); column |= ((er[5] & 0x80) >> 7); - ser[3] |= ((unsigned char)S7[row * 16 + column] << 4); + ser[3] |= ((uint8_t)S7[row * 16 + column] << 4); row = 0; row |= ((er[5] & 0x20) >> 4); @@ -415,7 +416,7 @@ void process_message(unsigned char* message_piece, unsigned char* processed_piec column = 0; column |= ((er[5] & 0x1E) >> 1); - ser[3] |= (unsigned char)S8[row * 16 + column]; + ser[3] |= (uint8_t)S8[row * 16 + column]; for (i = 0; i < 4; i++) { rn[i] = 0; @@ -440,7 +441,7 @@ void process_message(unsigned char* message_piece, unsigned char* processed_piec } } - unsigned char pre_end_permutation[8]; + uint8_t pre_end_permutation[8]; for (i = 0; i < 4; i++) { pre_end_permutation[i] = r[i]; pre_end_permutation[4 + i] = l[i]; diff --git a/source/util/src/encode.c b/source/util/src/tencode.c similarity index 96% rename from source/util/src/encode.c rename to source/util/src/tencode.c index 758d3f442d..94b4cced46 100644 --- a/source/util/src/encode.c +++ b/source/util/src/tencode.c @@ -13,7 +13,8 @@ * along with this program. If not, see . */ -#include "encode.h" +#define _DEFAULT_SOURCE +#include "tencode.h" #if __STDC_VERSION__ >= 201112L static_assert(sizeof(float) == sizeof(uint32_t), "sizeof(float) must equal to sizeof(uint32_t)"); @@ -47,7 +48,7 @@ void tCoderClear(SCoder* pCoder) { } } -int tStartEncode(SCoder* pCoder) { +int32_t tStartEncode(SCoder* pCoder) { struct SCoderNode* pNode; ASSERT(pCoder->type == TD_ENCODER); @@ -96,7 +97,7 @@ void tEndEncode(SCoder* pCoder) { } } -int tStartDecode(SCoder* pCoder) { +int32_t tStartDecode(SCoder* pCoder) { int32_t len; struct SCoderNode* pNode; diff --git a/source/util/src/texception.c b/source/util/src/texception.c new file mode 100644 index 0000000000..33befb694a --- /dev/null +++ b/source/util/src/texception.c @@ -0,0 +1,149 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "texception.h" + +static threadlocal SExceptionNode* expList; + +void exceptionPushNode(SExceptionNode* node) { + node->prev = expList; + expList = node; +} + +int32_t exceptionPopNode() { + SExceptionNode* node = expList; + expList = node->prev; + return node->code; +} + +void exceptionThrow(int32_t code) { + expList->code = code; + longjmp(expList->jb, 1); +} + +static void cleanupWrapper_void_ptr_ptr(SCleanupAction* ca) { + void (*func)(void*, void*) = ca->func; + func(ca->arg1.Ptr, ca->arg2.Ptr); +} + +static void cleanupWrapper_void_ptr_bool(SCleanupAction* ca) { + void (*func)(void*, bool) = ca->func; + func(ca->arg1.Ptr, ca->arg2.Bool); +} + +static void cleanupWrapper_void_ptr(SCleanupAction* ca) { + void (*func)(void*) = ca->func; + func(ca->arg1.Ptr); +} + +static void cleanupWrapper_int_int(SCleanupAction* ca) { + int32_t (*func)(int32_t) = ca->func; + func(ca->arg1.Int); +} + +static void cleanupWrapper_void(SCleanupAction* ca) { + void (*func)() = ca->func; + func(); +} + +static void cleanupWrapper_int_ptr(SCleanupAction* ca) { + int32_t (*func)(void*) = ca->func; + func(ca->arg1.Ptr); +} + +typedef void (*wrapper)(SCleanupAction*); +static wrapper wrappers[] = { + cleanupWrapper_void_ptr_ptr, cleanupWrapper_void_ptr_bool, cleanupWrapper_void_ptr, + cleanupWrapper_int_int, cleanupWrapper_void, cleanupWrapper_int_ptr, +}; + +void cleanupPush_void_ptr_ptr(bool failOnly, void* func, void* arg1, void* arg2) { + assert(expList->numCleanupAction < expList->maxCleanupAction); + + SCleanupAction* ca = expList->cleanupActions + expList->numCleanupAction++; + ca->wrapper = 0; + ca->failOnly = failOnly; + ca->func = func; + ca->arg1.Ptr = arg1; + ca->arg2.Ptr = arg2; +} + +void cleanupPush_void_ptr_bool(bool failOnly, void* func, void* arg1, bool arg2) { + assert(expList->numCleanupAction < expList->maxCleanupAction); + + SCleanupAction* ca = expList->cleanupActions + expList->numCleanupAction++; + ca->wrapper = 1; + ca->failOnly = failOnly; + ca->func = func; + ca->arg1.Ptr = arg1; + ca->arg2.Bool = arg2; +} + +void cleanupPush_void_ptr(bool failOnly, void* func, void* arg) { + assert(expList->numCleanupAction < expList->maxCleanupAction); + + SCleanupAction* ca = expList->cleanupActions + expList->numCleanupAction++; + ca->wrapper = 2; + ca->failOnly = failOnly; + ca->func = func; + ca->arg1.Ptr = arg; +} + +void cleanupPush_int_int(bool failOnly, void* func, int32_t arg) { + assert(expList->numCleanupAction < expList->maxCleanupAction); + + SCleanupAction* ca = expList->cleanupActions + expList->numCleanupAction++; + ca->wrapper = 3; + ca->failOnly = failOnly; + ca->func = func; + ca->arg1.Int = arg; +} + +void cleanupPush_void(bool failOnly, void* func) { + assert(expList->numCleanupAction < expList->maxCleanupAction); + + SCleanupAction* ca = expList->cleanupActions + expList->numCleanupAction++; + ca->wrapper = 4; + ca->failOnly = failOnly; + ca->func = func; +} + +void cleanupPush_int_ptr(bool failOnly, void* func, void* arg) { + assert(expList->numCleanupAction < expList->maxCleanupAction); + + SCleanupAction* ca = expList->cleanupActions + expList->numCleanupAction++; + ca->wrapper = 5; + ca->failOnly = failOnly; + ca->func = func; + ca->arg1.Ptr = arg; +} + +int32_t cleanupGetActionCount() { return expList->numCleanupAction; } + +static void doExecuteCleanup(SExceptionNode* node, int32_t anchor, bool failed) { + while (node->numCleanupAction > anchor) { + --node->numCleanupAction; + SCleanupAction* ca = node->cleanupActions + node->numCleanupAction; + if (failed || !(ca->failOnly)) { + wrappers[ca->wrapper](ca); + } + } +} + +void cleanupExecuteTo(int32_t anchor, bool failed) { doExecuteCleanup(expList, anchor, failed); } + +void cleanupExecute(SExceptionNode* node, bool failed) { doExecuteCleanup(node, 0, failed); } +bool cleanupExceedLimit() { return expList->numCleanupAction >= expList->maxCleanupAction; } diff --git a/source/util/src/tfile.c b/source/util/src/tfile.c deleted file mode 100644 index ef9708df0c..0000000000 --- a/source/util/src/tfile.c +++ /dev/null @@ -1,170 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "taoserror.h" -#include "tref.h" -#include "tutil.h" -#include "tlog.h" - -static int32_t tsFileRsetId = -1; - -static int8_t tfInited = 0; - -// static void tfCloseFile(void *p) { taosCloseFile((TdFilePtr)(uintptr_t)p); } - -int32_t tfInit() { - // int8_t old = atomic_val_compare_exchange_8(&tfInited, 0, 1); - // if (old == 1) return 0; - // tsFileRsetId = taosOpenRef(2000, tfCloseFile); - // if (tsFileRsetId > 0) { - // return 0; - // } else { - // atomic_store_8(&tfInited, 0); - // return -1; - // } -} - -void tfCleanup() { - // atomic_store_8(&tfInited, 0); - // if (tsFileRsetId >= 0) taosCloseRef(tsFileRsetId); - // tsFileRsetId = -1; -} - -// static int64_t tfOpenImp(TdFilePtr pFile) { -// if (pFile == NULL) { -// terrno = TAOS_SYSTEM_ERROR(errno); -// return -1; -// } - -// void * p = (void *)(int64_t)pFile; -// int64_t rid = taosAddRef(tsFileRsetId, p); -// if (rid < 0) taosCloseFile(&pFile); - -// return rid; -// } - -// int64_t tfOpenRead(const char *pathname, int32_t flags) { -// int32_t pFile = taosOpenFile(pathname, TD_FILE_READ); -// return tfOpenImp(fd); -// } - -// int64_t tfOpenReadWrite(const char *pathname, int32_t flags) { -// int32_t pFile = taosOpenFile(pathname, TD_FILE_READ | TD_FILE_WRITE); -// return tfOpenImp(fd); -// } - -// int64_t tfOpenCreateWrite(const char *pathname, int32_t flags, mode_t mode) { -// int32_t pFile = taosOpenFile(pathname, TD_FILE_CTEATE | TD_FILE_WRITE); -// return tfOpenImp(fd); -// } - -// int64_t tfOpenCreateWriteAppend(const char *pathname, int32_t flags, mode_t mode) { -// int32_t pFile = taosOpenFile(pathname, TD_FILE_CTEATE | TD_FILE_WRITE | TD_FILE_APPEND); -// return tfOpenImp(fd); -// } - -// int64_t tfClose(int64_t tfd) { return taosRemoveRef(tsFileRsetId, tfd); } - -// int64_t tfWrite(int64_t tfd, void *buf, int64_t count) { -// void *p = taosAcquireRef(tsFileRsetId, tfd); -// if (p == NULL) return -1; - -// int32_t pFile = (TdFilePtr)(uintptr_t)p; - -// int64_t ret = taosWriteFile(pFile, buf, count); -// if (ret < 0) terrno = TAOS_SYSTEM_ERROR(errno); - -// taosReleaseRef(tsFileRsetId, tfd); -// return ret; -// } - -// int64_t tfRead(int64_t tfd, void *buf, int64_t count) { -// void *p = taosAcquireRef(tsFileRsetId, tfd); -// if (p == NULL) return -1; - -// int32_t pFile = (TdFilePtr)(uintptr_t)p; - -// int64_t ret = taosReadFile(pFile, buf, count); -// if (ret < 0) terrno = TAOS_SYSTEM_ERROR(errno); - -// taosReleaseRef(tsFileRsetId, tfd); -// return ret; -// } - -// int64_t tfPread(int64_t tfd, void *buf, int64_t count, int32_t offset) { -// void *p = taosAcquireRef(tsFileRsetId, tfd); -// if (p == NULL) return -1; - -// int32_t pFile = (TdFilePtr)(uintptr_t)p; - -// int64_t ret = pread(fd, buf, count, offset); -// if (ret < 0) terrno = TAOS_SYSTEM_ERROR(errno); - -// taosReleaseRef(tsFileRsetId, tfd); -// return ret; -// } - -// int32_t tfFsync(int64_t tfd) { -// void *p = taosAcquireRef(tsFileRsetId, tfd); -// if (p == NULL) return -1; - -// int32_t pFile = (TdFilePtr)(uintptr_t)p; -// int32_t code = taosFsyncFile(pFile); - -// taosReleaseRef(tsFileRsetId, tfd); -// return code; -// } - -// bool tfValid(int64_t tfd) { -// void *p = taosAcquireRef(tsFileRsetId, tfd); -// if (p == NULL) return false; - -// taosReleaseRef(tsFileRsetId, tfd); -// return true; -// } - -// int64_t tfLseek(int64_t tfd, int64_t offset, int32_t whence) { -// void *p = taosAcquireRef(tsFileRsetId, tfd); -// if (p == NULL) return -1; - -// int32_t pFile = (TdFilePtr)(uintptr_t)p; -// int64_t ret = taosLSeekFile(fd, offset, whence); - -// taosReleaseRef(tsFileRsetId, tfd); -// return ret; -// } - -// int32_t tfFtruncate(int64_t tfd, int64_t length) { -// void *p = taosAcquireRef(tsFileRsetId, tfd); -// if (p == NULL) return -1; - -// int32_t pFile = (TdFilePtr)(uintptr_t)p; -// int32_t code = taosFtruncateFile(fd, length); - -// taosReleaseRef(tsFileRsetId, tfd); -// return code; -// } - -// void *tfMmapReadOnly(int64_t tfd, int64_t length) { -// void *p = taosAcquireRef(tsFileRsetId, tfd); -// if (p == NULL) return NULL; -// int32_t pFile = (TdFilePtr)(uintptr_t)p; - -// void *ptr = mmap(NULL, length, PROT_READ, MAP_SHARED, fd, 0); -// taosReleaseRef(tsFileRsetId, tfd); -// return ptr; -// } diff --git a/source/util/src/tfunctional.c b/source/util/src/tfunctional.c index 8b20f8fc0a..c49fbdb504 100644 --- a/source/util/src/tfunctional.c +++ b/source/util/src/tfunctional.c @@ -13,37 +13,34 @@ * along with this program. If not, see . */ +#define _DEFAULT_SOURCE #include "tfunctional.h" -tGenericSavedFunc* genericSavedFuncInit(GenericVaFunc func, int numOfArgs) { +tGenericSavedFunc* genericSavedFuncInit(GenericVaFunc func, int32_t numOfArgs) { tGenericSavedFunc* pSavedFunc = malloc(sizeof(tGenericSavedFunc) + numOfArgs * (sizeof(void*))); - if(pSavedFunc == NULL) return NULL; + if (pSavedFunc == NULL) return NULL; pSavedFunc->func = func; return pSavedFunc; } -tI32SavedFunc* i32SavedFuncInit(I32VaFunc func, int numOfArgs) { - tI32SavedFunc* pSavedFunc = malloc(sizeof(tI32SavedFunc) + numOfArgs * sizeof(void *)); - if(pSavedFunc == NULL) return NULL; +tI32SavedFunc* i32SavedFuncInit(I32VaFunc func, int32_t numOfArgs) { + tI32SavedFunc* pSavedFunc = malloc(sizeof(tI32SavedFunc) + numOfArgs * sizeof(void*)); + if (pSavedFunc == NULL) return NULL; pSavedFunc->func = func; return pSavedFunc; } -tVoidSavedFunc* voidSavedFuncInit(VoidVaFunc func, int numOfArgs) { +tVoidSavedFunc* voidSavedFuncInit(VoidVaFunc func, int32_t numOfArgs) { tVoidSavedFunc* pSavedFunc = malloc(sizeof(tVoidSavedFunc) + numOfArgs * sizeof(void*)); - if(pSavedFunc == NULL) return NULL; + if (pSavedFunc == NULL) return NULL; pSavedFunc->func = func; return pSavedFunc; } -FORCE_INLINE void* genericInvoke(tGenericSavedFunc* const pSavedFunc) { - return pSavedFunc->func(pSavedFunc->args); -} +FORCE_INLINE void* genericInvoke(tGenericSavedFunc* const pSavedFunc) { return pSavedFunc->func(pSavedFunc->args); } -FORCE_INLINE int32_t i32Invoke(tI32SavedFunc* const pSavedFunc) { - return pSavedFunc->func(pSavedFunc->args); -} +FORCE_INLINE int32_t i32Invoke(tI32SavedFunc* const pSavedFunc) { return pSavedFunc->func(pSavedFunc->args); } FORCE_INLINE void voidInvoke(tVoidSavedFunc* const pSavedFunc) { - if(pSavedFunc) pSavedFunc->func(pSavedFunc->args); + if (pSavedFunc) pSavedFunc->func(pSavedFunc->args); } diff --git a/source/util/src/thash.c b/source/util/src/thash.c index 9f39b79968..219fc739ca 100644 --- a/source/util/src/thash.c +++ b/source/util/src/thash.c @@ -13,16 +13,15 @@ * along with this program. If not, see . */ -#include "os.h" +#define _DEFAULT_SOURCE #include "thash.h" -#include "tlog.h" -#include "taos.h" #include "tdef.h" +#include "tlog.h" // the add ref count operation may trigger the warning if the reference count is greater than the MAX_WARNING_REF_COUNT #define MAX_WARNING_REF_COUNT 10000 #define EXT_SIZE 1024 -#define HASH_NEED_RESIZE(_h) ((_h)->size >= (_h)->capacity * HASH_DEFAULT_LOAD_FACTOR) +#define HASH_NEED_RESIZE(_h) ((_h)->size >= (_h)->capacity * HASH_DEFAULT_LOAD_FACTOR) #define DO_FREE_HASH_NODE(_n) \ do { \ @@ -74,10 +73,12 @@ static FORCE_INLINE int32_t taosHashCapacity(int32_t length) { return i; } -static FORCE_INLINE SHashNode *doSearchInEntryList(SHashObj *pHashObj, SHashEntry *pe, const void *key, size_t keyLen, uint32_t hashVal) { +static FORCE_INLINE SHashNode *doSearchInEntryList(SHashObj *pHashObj, SHashEntry *pe, const void *key, size_t keyLen, + uint32_t hashVal) { SHashNode *pNode = pe->next; while (pNode) { - if ((pNode->keyLen == keyLen) && ((*(pHashObj->equalFp))(GET_HASH_NODE_KEY(pNode), key, keyLen) == 0) && pNode->removed == 0) { + if ((pNode->keyLen == keyLen) && ((*(pHashObj->equalFp))(GET_HASH_NODE_KEY(pNode), key, keyLen) == 0) && + pNode->removed == 0) { assert(pNode->hashVal == hashVal); break; } @@ -115,7 +116,8 @@ static SHashNode *doCreateHashNode(const void *key, size_t keyLen, const void *p * @param dsize size of actual data * @return hash node */ -static FORCE_INLINE SHashNode *doUpdateHashNode(SHashObj *pHashObj, SHashEntry* pe, SHashNode* prev, SHashNode *pNode, SHashNode *pNewNode) { +static FORCE_INLINE SHashNode *doUpdateHashNode(SHashObj *pHashObj, SHashEntry *pe, SHashNode *prev, SHashNode *pNode, + SHashNode *pNewNode) { assert(pNode->keyLen == pNewNode->keyLen); pNode->count--; @@ -129,11 +131,11 @@ static FORCE_INLINE SHashNode *doUpdateHashNode(SHashObj *pHashObj, SHashEntry* pNewNode->next = pNode->next; DO_FREE_HASH_NODE(pNode); } else { - pNewNode->next = pNode; + pNewNode->next = pNode; pe->num++; atomic_add_fetch_32(&pHashObj->size, 1); } - + return pNewNode; } @@ -175,7 +177,7 @@ SHashObj *taosHashInit(size_t capacity, _hash_fn_t fn, bool update, SHashLockTyp pHashObj->capacity = taosHashCapacity((int32_t)capacity); assert((pHashObj->capacity & (pHashObj->capacity - 1)) == 0); pHashObj->equalFp = memcmp; - pHashObj->hashFp = fn; + pHashObj->hashFp = fn; pHashObj->type = type; pHashObj->enableUpdate = update; @@ -201,8 +203,8 @@ SHashObj *taosHashInit(size_t capacity, _hash_fn_t fn, bool update, SHashLockTyp void taosHashSetEqualFp(SHashObj *pHashObj, _equal_fn_t fp) { if (pHashObj != NULL && fp != NULL) { pHashObj->equalFp = fp; - } -} + } +} int32_t taosHashGetSize(const SHashObj *pHashObj) { if (!pHashObj) { @@ -211,9 +213,7 @@ int32_t taosHashGetSize(const SHashObj *pHashObj) { return (int32_t)atomic_load_32(&pHashObj->size); } -static FORCE_INLINE bool taosHashTableEmpty(const SHashObj *pHashObj) { - return taosHashGetSize(pHashObj) == 0; -} +static FORCE_INLINE bool taosHashTableEmpty(const SHashObj *pHashObj) { return taosHashGetSize(pHashObj) == 0; } int32_t taosHashPutImpl(SHashObj *pHashObj, const void *key, size_t keyLen, void *data, size_t size, bool *newAdded) { uint32_t hashVal = (*pHashObj->hashFp)(key, (uint32_t)keyLen); @@ -224,12 +224,12 @@ int32_t taosHashPutImpl(SHashObj *pHashObj, const void *key, size_t keyLen, void // need the resize process, write lock applied if (HASH_NEED_RESIZE(pHashObj)) { - __wr_lock((void*) &pHashObj->lock, pHashObj->type); + __wr_lock((void *)&pHashObj->lock, pHashObj->type); taosHashTableResize(pHashObj); - __wr_unlock((void*) &pHashObj->lock, pHashObj->type); + __wr_unlock((void *)&pHashObj->lock, pHashObj->type); } - __rd_lock((void*) &pHashObj->lock, pHashObj->type); + __rd_lock((void *)&pHashObj->lock, pHashObj->type); int32_t slot = HASH_INDEX(hashVal, pHashObj->capacity); SHashEntry *pe = pHashObj->hashList[slot]; @@ -245,9 +245,10 @@ int32_t taosHashPutImpl(SHashObj *pHashObj, const void *key, size_t keyLen, void assert(pNode == NULL); } - SHashNode* prev = NULL; + SHashNode *prev = NULL; while (pNode) { - if ((pNode->keyLen == keyLen) && ((*(pHashObj->equalFp))(GET_HASH_NODE_KEY(pNode), key, keyLen) == 0) && pNode->removed == 0) { + if ((pNode->keyLen == keyLen) && ((*(pHashObj->equalFp))(GET_HASH_NODE_KEY(pNode), key, keyLen) == 0) && + pNode->removed == 0) { assert(pNode->hashVal == hashVal); break; } @@ -271,13 +272,13 @@ int32_t taosHashPutImpl(SHashObj *pHashObj, const void *key, size_t keyLen, void } // enable resize - __rd_unlock((void*) &pHashObj->lock, pHashObj->type); + __rd_unlock((void *)&pHashObj->lock, pHashObj->type); atomic_add_fetch_32(&pHashObj->size, 1); if (newAdded) { *newAdded = true; } - + return 0; } else { // not support the update operation, return error @@ -292,7 +293,7 @@ int32_t taosHashPutImpl(SHashObj *pHashObj, const void *key, size_t keyLen, void } // enable resize - __rd_unlock((void*) &pHashObj->lock, pHashObj->type); + __rd_unlock((void *)&pHashObj->lock, pHashObj->type); if (newAdded) { *newAdded = false; @@ -310,13 +311,13 @@ int32_t taosHashPutExt(SHashObj *pHashObj, const void *key, size_t keyLen, void return taosHashPutImpl(pHashObj, key, keyLen, data, size, newAdded); } - void *taosHashGet(SHashObj *pHashObj, const void *key, size_t keyLen) { return taosHashGetClone(pHashObj, key, keyLen, NULL); } -//TODO(yihaoDeng), merge with taosHashGetClone -void* taosHashGetCloneExt(SHashObj *pHashObj, const void *key, size_t keyLen, void (*fp)(void *), void** d, size_t *sz) { +// TODO(yihaoDeng), merge with taosHashGetClone +void *taosHashGetCloneExt(SHashObj *pHashObj, const void *key, size_t keyLen, void (*fp)(void *), void **d, + size_t *sz) { if (taosHashTableEmpty(pHashObj) || keyLen == 0 || key == NULL) { return NULL; } @@ -324,14 +325,14 @@ void* taosHashGetCloneExt(SHashObj *pHashObj, const void *key, size_t keyLen, vo uint32_t hashVal = (*pHashObj->hashFp)(key, (uint32_t)keyLen); // only add the read lock to disable the resize process - __rd_lock((void*) &pHashObj->lock, pHashObj->type); + __rd_lock((void *)&pHashObj->lock, pHashObj->type); int32_t slot = HASH_INDEX(hashVal, pHashObj->capacity); SHashEntry *pe = pHashObj->hashList[slot]; // no data, return directly if (atomic_load_32(&pe->num) == 0) { - __rd_unlock((void*) &pHashObj->lock, pHashObj->type); + __rd_unlock((void *)&pHashObj->lock, pHashObj->type); return NULL; } @@ -353,19 +354,19 @@ void* taosHashGetCloneExt(SHashObj *pHashObj, const void *key, size_t keyLen, vo if (fp != NULL) { fp(GET_HASH_NODE_DATA(pNode)); } - + if (*d == NULL) { - *sz = pNode->dataLen + EXT_SIZE; - *d = calloc(1, *sz); - } else if (*sz < pNode->dataLen){ *sz = pNode->dataLen + EXT_SIZE; - *d = realloc(*d, *sz); + *d = calloc(1, *sz); + } else if (*sz < pNode->dataLen) { + *sz = pNode->dataLen + EXT_SIZE; + *d = realloc(*d, *sz); } memcpy((char *)(*d), GET_HASH_NODE_DATA(pNode), pNode->dataLen); - // just make runtime happy + // just make runtime happy if ((*sz) - pNode->dataLen > 0) { memset((char *)(*d) + pNode->dataLen, 0, (*sz) - pNode->dataLen); - } + } data = GET_HASH_NODE_DATA(pNode); } @@ -374,11 +375,11 @@ void* taosHashGetCloneExt(SHashObj *pHashObj, const void *key, size_t keyLen, vo taosRUnLockLatch(&pe->latch); } - __rd_unlock((void*) &pHashObj->lock, pHashObj->type); + __rd_unlock((void *)&pHashObj->lock, pHashObj->type); return data; } -void* taosHashGetCloneImpl(SHashObj *pHashObj, const void *key, size_t keyLen, void* d, bool acquire) { +void *taosHashGetCloneImpl(SHashObj *pHashObj, const void *key, size_t keyLen, void *d, bool acquire) { if (taosHashTableEmpty(pHashObj) || keyLen == 0 || key == NULL) { return NULL; } @@ -386,14 +387,14 @@ void* taosHashGetCloneImpl(SHashObj *pHashObj, const void *key, size_t keyLen, v uint32_t hashVal = (*pHashObj->hashFp)(key, (uint32_t)keyLen); // only add the read lock to disable the resize process - __rd_lock((void*) &pHashObj->lock, pHashObj->type); + __rd_lock((void *)&pHashObj->lock, pHashObj->type); int32_t slot = HASH_INDEX(hashVal, pHashObj->capacity); SHashEntry *pe = pHashObj->hashList[slot]; // no data, return directly if (atomic_load_32(&pe->num) == 0) { - __rd_unlock((void*) &pHashObj->lock, pHashObj->type); + __rd_unlock((void *)&pHashObj->lock, pHashObj->type); return NULL; } @@ -431,20 +432,19 @@ void* taosHashGetCloneImpl(SHashObj *pHashObj, const void *key, size_t keyLen, v taosRUnLockLatch(&pe->latch); } - __rd_unlock((void*) &pHashObj->lock, pHashObj->type); + __rd_unlock((void *)&pHashObj->lock, pHashObj->type); return data; } -void* taosHashGetClone(SHashObj *pHashObj, const void *key, size_t keyLen, void* d) { +void *taosHashGetClone(SHashObj *pHashObj, const void *key, size_t keyLen, void *d) { return taosHashGetCloneImpl(pHashObj, key, keyLen, d, false); } -void* taosHashAcquire(SHashObj *pHashObj, const void *key, size_t keyLen) { +void *taosHashAcquire(SHashObj *pHashObj, const void *key, size_t keyLen) { return taosHashGetCloneImpl(pHashObj, key, keyLen, NULL, true); } - -int32_t taosHashRemove(SHashObj *pHashObj, const void *key, size_t keyLen/*, void *data, size_t dsize*/) { +int32_t taosHashRemove(SHashObj *pHashObj, const void *key, size_t keyLen /*, void *data, size_t dsize*/) { if (pHashObj == NULL || taosHashTableEmpty(pHashObj)) { return -1; } @@ -452,7 +452,7 @@ int32_t taosHashRemove(SHashObj *pHashObj, const void *key, size_t keyLen/*, voi uint32_t hashVal = (*pHashObj->hashFp)(key, (uint32_t)keyLen); // disable the resize process - __rd_lock((void*) &pHashObj->lock, pHashObj->type); + __rd_lock((void *)&pHashObj->lock, pHashObj->type); int32_t slot = HASH_INDEX(hashVal, pHashObj->capacity); SHashEntry *pe = pHashObj->hashList[slot]; @@ -466,23 +466,23 @@ int32_t taosHashRemove(SHashObj *pHashObj, const void *key, size_t keyLen/*, voi assert(pe->next == NULL); taosWUnLockLatch(&pe->latch); - __rd_unlock((void*) &pHashObj->lock, pHashObj->type); + __rd_unlock((void *)&pHashObj->lock, pHashObj->type); return -1; } - int code = -1; + int32_t code = -1; SHashNode *pNode = pe->next; SHashNode *prevNode = NULL; while (pNode) { - if ((pNode->keyLen == keyLen) && ((*(pHashObj->equalFp))(GET_HASH_NODE_KEY(pNode), key, keyLen) == 0) && pNode->removed == 0) + if ((pNode->keyLen == keyLen) && ((*(pHashObj->equalFp))(GET_HASH_NODE_KEY(pNode), key, keyLen) == 0) && + pNode->removed == 0) break; prevNode = pNode; pNode = pNode->next; } - if (pNode) { code = 0; // it is found @@ -495,7 +495,7 @@ int32_t taosHashRemove(SHashObj *pHashObj, const void *key, size_t keyLen/*, voi pe->next = pNode->next; } -// if (data) memcpy(data, GET_HASH_NODE_DATA(pNode), dsize); + // if (data) memcpy(data, GET_HASH_NODE_DATA(pNode), dsize); pe->num--; atomic_sub_fetch_32(&pHashObj->size, 1); @@ -507,7 +507,7 @@ int32_t taosHashRemove(SHashObj *pHashObj, const void *key, size_t keyLen/*, voi taosWUnLockLatch(&pe->latch); } - __rd_unlock((void*) &pHashObj->lock, pHashObj->type); + __rd_unlock((void *)&pHashObj->lock, pHashObj->type); return code; } @@ -518,7 +518,7 @@ int32_t taosHashCondTraverse(SHashObj *pHashObj, bool (*fp)(void *, void *), voi } // disable the resize process - __rd_lock((void*) &pHashObj->lock, pHashObj->type); + __rd_lock((void *)&pHashObj->lock, pHashObj->type); int32_t numOfEntries = (int32_t)pHashObj->capacity; for (int32_t i = 0; i < numOfEntries; ++i) { @@ -533,7 +533,7 @@ int32_t taosHashCondTraverse(SHashObj *pHashObj, bool (*fp)(void *, void *), voi // todo remove the first node SHashNode *pNode = NULL; - while((pNode = pEntry->next) != NULL) { + while ((pNode = pEntry->next) != NULL) { if (fp && (!fp(param, GET_HASH_NODE_DATA(pNode)))) { pEntry->num -= 1; atomic_sub_fetch_32(&pHashObj->size, 1); @@ -582,7 +582,7 @@ int32_t taosHashCondTraverse(SHashObj *pHashObj, bool (*fp)(void *, void *), voi } } - __rd_unlock((void*) &pHashObj->lock, pHashObj->type); + __rd_unlock((void *)&pHashObj->lock, pHashObj->type); return 0; } @@ -593,7 +593,7 @@ void taosHashClear(SHashObj *pHashObj) { SHashNode *pNode, *pNext; - __wr_lock((void*) &pHashObj->lock, pHashObj->type); + __wr_lock((void *)&pHashObj->lock, pHashObj->type); for (int32_t i = 0; i < pHashObj->capacity; ++i) { SHashEntry *pEntry = pHashObj->hashList[i]; @@ -617,7 +617,7 @@ void taosHashClear(SHashObj *pHashObj) { } atomic_store_32(&pHashObj->size, 0); - __wr_unlock((void*) &pHashObj->lock, pHashObj->type); + __wr_unlock((void *)&pHashObj->lock, pHashObj->type); } void taosHashCleanup(SHashObj *pHashObj) { @@ -676,7 +676,7 @@ void taosHashTableResize(SHashObj *pHashObj) { } int64_t st = taosGetTimestampUs(); - void *pNewEntryList = realloc(pHashObj->hashList, sizeof(void *) * newSize); + void *pNewEntryList = realloc(pHashObj->hashList, sizeof(void *) * newSize); if (pNewEntryList == NULL) { // todo handle error // uDebug("cache resize failed due to out of memory, capacity remain:%d", pHashObj->capacity); return; @@ -685,7 +685,7 @@ void taosHashTableResize(SHashObj *pHashObj) { pHashObj->hashList = pNewEntryList; size_t inc = newSize - pHashObj->capacity; - void * p = calloc(inc, sizeof(SHashEntry)); + void *p = calloc(inc, sizeof(SHashEntry)); for (int32_t i = 0; i < inc; ++i) { pHashObj->hashList[i + pHashObj->capacity] = (void *)((char *)p + i * sizeof(SHashEntry)); @@ -756,15 +756,13 @@ void taosHashTableResize(SHashObj *pHashObj) { } else { assert(pe->next != NULL); } - } - } int64_t et = taosGetTimestampUs(); uDebug("hash table resize completed, new capacity:%d, load factor:%f, elapsed time:%fms", (int32_t)pHashObj->capacity, - ((double)pHashObj->size) / pHashObj->capacity, (et - st) / 1000.0); + ((double)pHashObj->size) / pHashObj->capacity, (et - st) / 1000.0); } SHashNode *doCreateHashNode(const void *key, size_t keyLen, const void *pData, size_t dsize, uint32_t hashVal) { @@ -775,12 +773,12 @@ SHashNode *doCreateHashNode(const void *key, size_t keyLen, const void *pData, s return NULL; } - pNewNode->keyLen = (uint32_t)keyLen; + pNewNode->keyLen = (uint32_t)keyLen; pNewNode->hashVal = hashVal; - pNewNode->dataLen = (uint32_t) dsize; - pNewNode->count = 1; + pNewNode->dataLen = (uint32_t)dsize; + pNewNode->count = 1; pNewNode->removed = 0; - pNewNode->next = NULL; + pNewNode->next = NULL; memcpy(GET_HASH_NODE_DATA(pNewNode), pData, dsize); memcpy(GET_HASH_NODE_KEY(pNewNode), key, keyLen); @@ -802,15 +800,16 @@ size_t taosHashGetMemSize(const SHashObj *pHashObj) { return 0; } - return (pHashObj->capacity * (sizeof(SHashEntry) + POINTER_BYTES)) + sizeof(SHashNode) * taosHashGetSize(pHashObj) + sizeof(SHashObj); + return (pHashObj->capacity * (sizeof(SHashEntry) + POINTER_BYTES)) + sizeof(SHashNode) * taosHashGetSize(pHashObj) + + sizeof(SHashObj); } -FORCE_INLINE int32_t taosHashGetKey(void *data, void** key, size_t* keyLen) { +FORCE_INLINE int32_t taosHashGetKey(void *data, void **key, size_t *keyLen) { if (NULL == data || NULL == key) { return -1; } - - SHashNode * node = GET_HASH_PNODE(data); + + SHashNode *node = GET_HASH_PNODE(data); *key = GET_HASH_NODE_KEY(node); if (keyLen) { *keyLen = node->keyLen; @@ -820,19 +819,17 @@ FORCE_INLINE int32_t taosHashGetKey(void *data, void** key, size_t* keyLen) { } FORCE_INLINE int32_t taosHashGetDataLen(void *data) { - SHashNode * node = GET_HASH_PNODE(data); + SHashNode *node = GET_HASH_PNODE(data); return node->keyLen; } FORCE_INLINE uint32_t taosHashGetDataKeyLen(SHashObj *pHashObj, void *data) { - SHashNode * node = GET_HASH_PNODE(data); + SHashNode *node = GET_HASH_PNODE(data); return node->keyLen; } - // release the pNode, return next pNode, and lock the current entry -static void *taosHashReleaseNode(SHashObj *pHashObj, void *p, int *slot) { - +static void *taosHashReleaseNode(SHashObj *pHashObj, void *p, int32_t *slot) { SHashNode *pOld = (SHashNode *)GET_HASH_PNODE(p); SHashNode *prevNode = NULL; @@ -847,14 +844,13 @@ static void *taosHashReleaseNode(SHashObj *pHashObj, void *p, int *slot) { SHashNode *pNode = pe->next; while (pNode) { - if (pNode == pOld) - break; + if (pNode == pOld) break; prevNode = pNode; pNode = pNode->next; } - if (pNode) { + if (pNode) { pNode = pNode->next; while (pNode) { if (pNode->removed == 0) break; @@ -862,17 +858,17 @@ static void *taosHashReleaseNode(SHashObj *pHashObj, void *p, int *slot) { } pOld->count--; - if (pOld->count <=0) { + if (pOld->count <= 0) { if (prevNode) { prevNode->next = pOld->next; } else { pe->next = pOld->next; } - + pe->num--; atomic_sub_fetch_32(&pHashObj->size, 1); FREE_HASH_NODE(pHashObj, pOld); - } + } } else { uError("pNode:%p data:%p is not there!!!", pNode, p); } @@ -881,13 +877,13 @@ static void *taosHashReleaseNode(SHashObj *pHashObj, void *p, int *slot) { } void *taosHashIterate(SHashObj *pHashObj, void *p) { - if (pHashObj == NULL) return NULL; + if (pHashObj == NULL) return NULL; - int slot = 0; + int32_t slot = 0; char *data = NULL; // only add the read lock to disable the resize process - __rd_lock((void*) &pHashObj->lock, pHashObj->type); + __rd_lock((void *)&pHashObj->lock, pHashObj->type); SHashNode *pNode = NULL; if (p) { @@ -896,7 +892,7 @@ void *taosHashIterate(SHashObj *pHashObj, void *p) { SHashEntry *pe = pHashObj->hashList[slot]; if (pHashObj->type == HASH_ENTRY_LOCK) { taosWUnLockLatch(&pe->latch); - } + } slot = slot + 1; } @@ -921,7 +917,7 @@ void *taosHashIterate(SHashObj *pHashObj, void *p) { if (pHashObj->type == HASH_ENTRY_LOCK) { taosWUnLockLatch(&pe->latch); - } + } } } @@ -947,10 +943,10 @@ void *taosHashIterate(SHashObj *pHashObj, void *p) { if (pHashObj->type == HASH_ENTRY_LOCK) { taosWUnLockLatch(&pe->latch); - } + } } - __rd_unlock((void*) &pHashObj->lock, pHashObj->type); + __rd_unlock((void *)&pHashObj->lock, pHashObj->type); return data; } @@ -958,21 +954,17 @@ void taosHashCancelIterate(SHashObj *pHashObj, void *p) { if (pHashObj == NULL || p == NULL) return; // only add the read lock to disable the resize process - __rd_lock((void*) &pHashObj->lock, pHashObj->type); + __rd_lock((void *)&pHashObj->lock, pHashObj->type); - int slot; + int32_t slot; taosHashReleaseNode(pHashObj, p, &slot); SHashEntry *pe = pHashObj->hashList[slot]; if (pHashObj->type == HASH_ENTRY_LOCK) { taosWUnLockLatch(&pe->latch); - } + } - __rd_unlock((void*) &pHashObj->lock, pHashObj->type); + __rd_unlock((void *)&pHashObj->lock, pHashObj->type); } -void taosHashRelease(SHashObj *pHashObj, void *p) { - taosHashCancelIterate(pHashObj, p); -} - - +void taosHashRelease(SHashObj *pHashObj, void *p) { taosHashCancelIterate(pHashObj, p); } diff --git a/source/util/src/thashutil.c b/source/util/src/thashutil.c index 2d7f8a5b03..d5182cb892 100644 --- a/source/util/src/thashutil.c +++ b/source/util/src/thashutil.c @@ -13,10 +13,9 @@ * along with this program. If not, see . */ -#include "os.h" +#define _DEFAULT_SOURCE #include "thash.h" -#include "compare.h" -#include "taos.h" +#include "tcompare.h" #include "types.h" #define ROTL32(x, r) ((x) << (r) | (x) >> (32u - (r))) @@ -34,7 +33,7 @@ uint32_t MurmurHash3_32(const char *key, uint32_t len) { const uint8_t *data = (const uint8_t *)key; - const int nblocks = len >> 2u; + const int32_t nblocks = len >> 2u; uint32_t h1 = 0x12345678; @@ -43,7 +42,7 @@ uint32_t MurmurHash3_32(const char *key, uint32_t len) { const uint32_t *blocks = (const uint32_t *)(data + nblocks * 4); - for (int i = -nblocks; i; i++) { + for (int32_t i = -nblocks; i; i++) { uint32_t k1 = blocks[i]; k1 *= c1; @@ -92,7 +91,7 @@ uint32_t taosFloatHash(const char *key, uint32_t UNUSED_PARAM(len)) { return 0; } if (fabs(f) < FLT_MAX/BASE - DLT) { - int t = (int)(round(BASE * (f + DLT))); + int32_t t = (int32_t)(round(BASE * (f + DLT))); return (uint32_t)t; } else { return 0x7fc00000; @@ -108,7 +107,7 @@ uint32_t taosDoubleHash(const char *key, uint32_t UNUSED_PARAM(len)) { return 0; } if (fabs(f) < DBL_MAX/BASE - DLT) { - int t = (int)(round(BASE * (f + DLT))); + int32_t t = (int32_t)(round(BASE * (f + DLT))); return (uint32_t)t; } else { return 0x7fc00000; diff --git a/source/util/src/theap.c b/source/util/src/theap.c index aa822c7d5e..30af0483cc 100644 --- a/source/util/src/theap.c +++ b/source/util/src/theap.c @@ -13,15 +13,16 @@ * along with this program. If not, see . */ +#define _DEFAULT_SOURCE #include "theap.h" -size_t heapSize(Heap* heap) { - return heap->nelts; -} +size_t heapSize(Heap* heap) { return heap->nelts; } Heap* heapCreate(HeapCompareFn fn) { - Heap *heap = calloc(1, sizeof(Heap)); - if (heap == NULL) { return NULL; } + Heap* heap = calloc(1, sizeof(Heap)); + if (heap == NULL) { + return NULL; + } heap->min = NULL; heap->nelts = 0; @@ -29,18 +30,14 @@ Heap* heapCreate(HeapCompareFn fn) { return heap; } -void heapDestroy(Heap *heap) { - free(heap); -} +void heapDestroy(Heap* heap) { free(heap); } -HeapNode* heapMin(const Heap* heap) { - return heap->min; -} +HeapNode* heapMin(const Heap* heap) { return heap->min; } /* Swap parent with child. Child moves closer to the root, parent moves away. */ static void heapNodeSwap(Heap* heap, HeapNode* parent, HeapNode* child) { HeapNode* sibling; - HeapNode t; + HeapNode t; t = *parent; *parent = *child; @@ -54,13 +51,10 @@ static void heapNodeSwap(Heap* heap, HeapNode* parent, HeapNode* child) { child->right = parent; sibling = child->left; } - if (sibling != NULL) - sibling->parent = child; + if (sibling != NULL) sibling->parent = child; - if (parent->left != NULL) - parent->left->parent = parent; - if (parent->right != NULL) - parent->right->parent = parent; + if (parent->left != NULL) parent->left->parent = parent; + if (parent->right != NULL) parent->right->parent = parent; if (child->parent == NULL) heap->min = child; @@ -73,9 +67,9 @@ static void heapNodeSwap(Heap* heap, HeapNode* parent, HeapNode* child) { void heapInsert(Heap* heap, HeapNode* newnode) { HeapNode** parent; HeapNode** child; - unsigned int path; - unsigned int n; - unsigned int k; + uint32_t path; + uint32_t n; + uint32_t k; newnode->left = NULL; newnode->right = NULL; @@ -85,8 +79,7 @@ void heapInsert(Heap* heap, HeapNode* newnode) { * heap so we always insert at the left-most free node of the bottom row. */ path = 0; - for (k = 0, n = 1 + heap->nelts; n >= 2; k += 1, n /= 2) - path = (path << 1) | (n & 1); + for (k = 0, n = 1 + heap->nelts; n >= 2; k += 1, n /= 2) path = (path << 1) | (n & 1); /* Now traverse the heap using the path we calculated in the previous step. */ parent = child = &heap->min; @@ -113,22 +106,20 @@ void heapInsert(Heap* heap, HeapNode* newnode) { } void heapRemove(Heap* heap, HeapNode* node) { - HeapNode* smallest; + HeapNode* smallest; HeapNode** max; - HeapNode* child; - unsigned int path; - unsigned int k; - unsigned int n; + HeapNode* child; + uint32_t path; + uint32_t k; + uint32_t n; - if (heap->nelts == 0) - return; + if (heap->nelts == 0) return; /* Calculate the path from the min (the root) to the max, the left-most node * of the bottom row. */ path = 0; - for (k = 0, n = heap->nelts; n >= 2; k += 1, n /= 2) - path = (path << 1) | (n & 1); + for (k = 0, n = heap->nelts; n >= 2; k += 1, n /= 2) path = (path << 1) | (n & 1); /* Now traverse the heap using the path we calculated in the previous step. */ max = &heap->min; @@ -182,12 +173,9 @@ void heapRemove(Heap* heap, HeapNode* node) { */ for (;;) { smallest = child; - if (child->left != NULL && (heap->compFn)(child->left, smallest)) - smallest = child->left; - if (child->right != NULL && (heap->compFn)(child->right, smallest)) - smallest = child->right; - if (smallest == child) - break; + if (child->left != NULL && (heap->compFn)(child->left, smallest)) smallest = child->left; + if (child->right != NULL && (heap->compFn)(child->right, smallest)) smallest = child->right; + if (smallest == child) break; heapNodeSwap(heap, child, smallest); } @@ -195,12 +183,7 @@ void heapRemove(Heap* heap, HeapNode* node) { * this is required, because `max` node is not guaranteed to be the * actual maximum in tree */ - while (child->parent != NULL && (heap->compFn)(child, child->parent)) - heapNodeSwap(heap, child->parent, child); + while (child->parent != NULL && (heap->compFn)(child, child->parent)) heapNodeSwap(heap, child->parent, child); } -void heapDequeue(Heap* heap) { - heapRemove(heap, heap->min); -} - - +void heapDequeue(Heap* heap) { heapRemove(heap, heap->min); } diff --git a/source/util/src/tidpool.c b/source/util/src/tidpool.c index b4d76e6fb5..3ae537eae8 100644 --- a/source/util/src/tidpool.c +++ b/source/util/src/tidpool.c @@ -13,17 +13,11 @@ * along with this program. If not, see . */ +#define _DEFAULT_SOURCE +#include "tidpool.h" #include "tlog.h" -typedef struct { - int maxId; - int numOfFree; - int freeSlot; - bool * freeList; - pthread_mutex_t mutex; -} id_pool_t; - -void *taosInitIdPool(int maxId) { +void *taosInitIdPool(int32_t maxId) { id_pool_t *pIdPool = calloc(1, sizeof(id_pool_t)); if (pIdPool == NULL) return NULL; @@ -44,17 +38,14 @@ void *taosInitIdPool(int maxId) { return pIdPool; } -int taosAllocateId(void *handle) { - id_pool_t *pIdPool = handle; - if (handle == NULL) { - return -1; - } +int32_t taosAllocateId(id_pool_t *pIdPool) { + if (pIdPool == NULL) return -1; - int slot = -1; + int32_t slot = -1; pthread_mutex_lock(&pIdPool->mutex); if (pIdPool->numOfFree > 0) { - for (int i = 0; i < pIdPool->maxId; ++i) { + for (int32_t i = 0; i < pIdPool->maxId; ++i) { slot = (i + pIdPool->freeSlot) % pIdPool->maxId; if (!pIdPool->freeList[slot]) { pIdPool->freeList[slot] = true; @@ -69,13 +60,12 @@ int taosAllocateId(void *handle) { return slot + 1; } -void taosFreeId(void *handle, int id) { - id_pool_t *pIdPool = handle; - if (handle == NULL) return; +void taosFreeId(id_pool_t *pIdPool, int32_t id) { + if (pIdPool == NULL) return; pthread_mutex_lock(&pIdPool->mutex); - int slot = (id - 1) % pIdPool->maxId; + int32_t slot = (id - 1) % pIdPool->maxId; if (pIdPool->freeList[slot]) { pIdPool->freeList[slot] = false; pIdPool->numOfFree++; @@ -84,9 +74,7 @@ void taosFreeId(void *handle, int id) { pthread_mutex_unlock(&pIdPool->mutex); } -void taosIdPoolCleanUp(void *handle) { - id_pool_t *pIdPool = handle; - +void taosIdPoolCleanUp(id_pool_t *pIdPool) { if (pIdPool == NULL) return; uDebug("pool:%p is cleaned", pIdPool); @@ -100,22 +88,19 @@ void taosIdPoolCleanUp(void *handle) { free(pIdPool); } -int taosIdPoolNumOfUsed(void *handle) { - id_pool_t *pIdPool = handle; - +int32_t taosIdPoolNumOfUsed(id_pool_t *pIdPool) { pthread_mutex_lock(&pIdPool->mutex); - int ret = pIdPool->maxId - pIdPool->numOfFree; + int32_t ret = pIdPool->maxId - pIdPool->numOfFree; pthread_mutex_unlock(&pIdPool->mutex); return ret; } -bool taosIdPoolMarkStatus(void *handle, int id) { +bool taosIdPoolMarkStatus(id_pool_t *pIdPool, int32_t id) { bool ret = false; - id_pool_t *pIdPool = handle; pthread_mutex_lock(&pIdPool->mutex); - int slot = (id - 1) % pIdPool->maxId; + int32_t slot = (id - 1) % pIdPool->maxId; if (!pIdPool->freeList[slot]) { pIdPool->freeList[slot] = true; pIdPool->numOfFree--; @@ -128,8 +113,7 @@ bool taosIdPoolMarkStatus(void *handle, int id) { return ret; } -int taosUpdateIdPool(id_pool_t *handle, int maxId) { - id_pool_t *pIdPool = (id_pool_t*)handle; +int32_t taosUpdateIdPool(id_pool_t *pIdPool, int32_t maxId) { if (maxId <= pIdPool->maxId) { return 0; } @@ -154,11 +138,9 @@ int taosUpdateIdPool(id_pool_t *handle, int maxId) { return 0; } -int taosIdPoolMaxSize(void *handle) { - id_pool_t *pIdPool = (id_pool_t *)handle; - +int32_t taosIdPoolMaxSize(id_pool_t *pIdPool) { pthread_mutex_lock(&pIdPool->mutex); - int ret = pIdPool->maxId; + int32_t ret = pIdPool->maxId; pthread_mutex_unlock(&pIdPool->mutex); return ret; diff --git a/source/util/src/tjson.c b/source/util/src/tjson.c index 0c9d32ea13..4b68467450 100644 --- a/source/util/src/tjson.c +++ b/source/util/src/tjson.c @@ -13,22 +13,18 @@ * along with this program. If not, see . */ +#define _DEFAULT_SOURCE #include "tjson.h" - -#include "taoserror.h" #include "cJSON.h" +#include "taoserror.h" -SJson* tjsonCreateObject() { - return cJSON_CreateObject(); -} +SJson* tjsonCreateObject() { return cJSON_CreateObject(); } -void tjsonDelete(SJson* pJson) { - cJSON_Delete((cJSON*)pJson); -} +void tjsonDelete(SJson* pJson) { cJSON_Delete((cJSON*)pJson); } int32_t tjsonAddIntegerToObject(SJson* pJson, const char* pName, const uint64_t number) { char tmp[40] = {0}; - snprintf(tmp, tListLen(tmp), "%"PRId64, number); + snprintf(tmp, tListLen(tmp), "%" PRId64, number); return tjsonAddStringToObject(pJson, pName, tmp); } @@ -44,11 +40,9 @@ int32_t tjsonAddStringToObject(SJson* pJson, const char* pName, const char* pVal return (NULL == cJSON_AddStringToObject((cJSON*)pJson, pName, pVal) ? TSDB_CODE_FAILED : TSDB_CODE_SUCCESS); } -SJson* tjsonAddArrayToObject(SJson* pJson, const char* pName) { - return cJSON_AddArrayToObject((cJSON*)pJson, pName); -} +SJson* tjsonAddArrayToObject(SJson* pJson, const char* pName) { return cJSON_AddArrayToObject((cJSON*)pJson, pName); } -int32_t tjsonAddItemToObject(SJson *pJson, const char* pName, SJson* pItem) { +int32_t tjsonAddItemToObject(SJson* pJson, const char* pName, SJson* pItem) { return (cJSON_AddItemToObject((cJSON*)pJson, pName, pItem) ? TSDB_CODE_SUCCESS : TSDB_CODE_FAILED); } @@ -79,18 +73,11 @@ int32_t tjsonAddItem(SJson* pJson, FToJson func, const void* pObj) { return tjsonAddItemToArray(pJson, pJobj); } -char* tjsonToString(const SJson* pJson) { - return cJSON_Print((cJSON*)pJson); -} +char* tjsonToString(const SJson* pJson) { return cJSON_Print((cJSON*)pJson); } -char* tjsonToUnformattedString(const SJson* pJson) { - return cJSON_PrintUnformatted((cJSON*)pJson); -} +char* tjsonToUnformattedString(const SJson* pJson) { return cJSON_PrintUnformatted((cJSON*)pJson); } - -SJson* tjsonGetObjectItem(const SJson* pJson, const char* pName) { - return cJSON_GetObjectItem(pJson, pName); -} +SJson* tjsonGetObjectItem(const SJson* pJson, const char* pName) { return cJSON_GetObjectItem(pJson, pName); } int32_t tjsonGetStringValue(const SJson* pJson, const char* pName, char* pVal) { char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName)); @@ -153,7 +140,7 @@ int32_t tjsonGetUBigIntValue(const SJson* pJson, const char* pName, uint64_t* pV int32_t tjsonGetUTinyIntValue(const SJson* pJson, const char* pName, uint8_t* pVal) { uint64_t val = 0; - int32_t code = tjsonGetUBigIntValue(pJson, pName, &val); + int32_t code = tjsonGetUBigIntValue(pJson, pName, &val); *pVal = val; return code; } @@ -176,13 +163,9 @@ int32_t tjsonGetDoubleValue(const SJson* pJson, const char* pName, double* pVal) return TSDB_CODE_SUCCESS; } -int32_t tjsonGetArraySize(const SJson* pJson) { - return cJSON_GetArraySize(pJson); -} +int32_t tjsonGetArraySize(const SJson* pJson) { return cJSON_GetArraySize(pJson); } -SJson* tjsonGetArrayItem(const SJson* pJson, int32_t index) { - return cJSON_GetArrayItem(pJson, index); -} +SJson* tjsonGetArrayItem(const SJson* pJson, int32_t index) { return cJSON_GetArrayItem(pJson, index); } int32_t tjsonToObject(const SJson* pJson, const char* pName, FToObject func, void* pObj) { SJson* pJsonObj = tjsonGetObjectItem(pJson, pName); @@ -192,6 +175,4 @@ int32_t tjsonToObject(const SJson* pJson, const char* pName, FToObject func, voi return func(pJsonObj, pObj); } -SJson* tjsonParse(const char* pStr) { - return cJSON_Parse(pStr); -} +SJson* tjsonParse(const char* pStr) { return cJSON_Parse(pStr); } diff --git a/source/util/src/tlist.c b/source/util/src/tlist.c index f79bca1e4b..5fccba614b 100644 --- a/source/util/src/tlist.c +++ b/source/util/src/tlist.c @@ -13,15 +13,15 @@ * along with this program. If not, see . */ +#define _DEFAULT_SOURCE #include "tlist.h" -#include "os.h" -void tdListInit(SList *list, int eleSize) { +void tdListInit(SList *list, int32_t eleSize) { TD_DLIST_INIT(list); listEleSize(list) = eleSize; } -SList *tdListNew(int eleSize) { +SList *tdListNew(int32_t eleSize) { SList *list = (SList *)malloc(sizeof(SList)); if (list == NULL) return NULL; @@ -50,7 +50,7 @@ void tdListPrependNode(SList *list, SListNode *node) { TD_DLIST_PREPEND(list, no void tdListAppendNode(SList *list, SListNode *node) { TD_DLIST_APPEND(list, node); } -int tdListPrepend(SList *list, void *data) { +int32_t tdListPrepend(SList *list, void *data) { SListNode *node = (SListNode *)malloc(sizeof(SListNode) + list->eleSize); if (node == NULL) return -1; @@ -60,7 +60,7 @@ int tdListPrepend(SList *list, void *data) { return 0; } -int tdListAppend(SList *list, void *data) { +int32_t tdListAppend(SList *list, void *data) { SListNode *node = (SListNode *)calloc(1, sizeof(SListNode) + list->eleSize); if (node == NULL) return -1; diff --git a/source/util/src/tlockfree.c b/source/util/src/tlockfree.c index f54206f5cb..8edcca0c16 100644 --- a/source/util/src/tlockfree.c +++ b/source/util/src/tlockfree.c @@ -13,7 +13,7 @@ * along with this program. If not, see . */ -#include "os.h" +#define _DEFAULT_SOURCE #include "tlockfree.h" #define TD_RWLATCH_WRITE_FLAG 0x40000000 @@ -22,7 +22,7 @@ void taosInitRWLatch(SRWLatch *pLatch) { *pLatch = 0; } void taosWLockLatch(SRWLatch *pLatch) { SRWLatch oLatch, nLatch; - int nLoops = 0; + int32_t nLoops = 0; // Set write flag while (1) { @@ -57,7 +57,7 @@ void taosWUnLockLatch(SRWLatch *pLatch) { atomic_store_32(pLatch, 0); } void taosRLockLatch(SRWLatch *pLatch) { SRWLatch oLatch, nLatch; - int nLoops = 0; + int32_t nLoops = 0; while (1) { oLatch = atomic_load_32(pLatch); diff --git a/source/util/src/tlog.c b/source/util/src/tlog.c index 2ed8d6e347..d821e0440b 100644 --- a/source/util/src/tlog.c +++ b/source/util/src/tlog.c @@ -14,8 +14,8 @@ */ #define _DEFAULT_SOURCE -#include "tutil.h" #include "tlog.h" +#include "tutil.h" #define LOG_MAX_LINE_SIZE (1000) #define LOG_MAX_LINE_BUFFER_SIZE (LOG_MAX_LINE_SIZE + 10) @@ -113,7 +113,7 @@ static int32_t taosStartLog() { return 0; } -int32_t taosInitLog(const char *logName, int maxFiles) { +int32_t taosInitLog(const char *logName, int32_t maxFiles) { if (atomic_val_compare_exchange_8(&tsLogInited, 0, 1) != 0) return 0; osUpdate(); diff --git a/source/util/src/tlosertree.c b/source/util/src/tlosertree.c index 8b7f55809b..6349ab170c 100644 --- a/source/util/src/tlosertree.c +++ b/source/util/src/tlosertree.c @@ -13,12 +13,10 @@ * along with this program. If not, see . */ -#include "os.h" -#include "tlog.h" +#define _DEFAULT_SOURCE #include "tlosertree.h" #include "taoserror.h" - - +#include "tlog.h" // Set the initial value of the multiway merge tree. static void tMergeTreeInit(SMultiwayMergeTreeInfo* pTree) { @@ -33,10 +31,12 @@ static void tMergeTreeInit(SMultiwayMergeTreeInfo* pTree) { } } -int32_t tMergeTreeCreate(SMultiwayMergeTreeInfo** pTree, uint32_t numOfSources, void* param, __merge_compare_fn_t compareFn) { +int32_t tMergeTreeCreate(SMultiwayMergeTreeInfo** pTree, uint32_t numOfSources, void* param, + __merge_compare_fn_t compareFn) { int32_t totalEntries = numOfSources << 1u; - SMultiwayMergeTreeInfo* pTreeInfo = (SMultiwayMergeTreeInfo*)calloc(1, sizeof(SMultiwayMergeTreeInfo) + sizeof(STreeNode) * totalEntries); + SMultiwayMergeTreeInfo* pTreeInfo = + (SMultiwayMergeTreeInfo*)calloc(1, sizeof(SMultiwayMergeTreeInfo) + sizeof(STreeNode) * totalEntries); if (pTreeInfo == NULL) { uError("allocate memory for loser-tree failed. reason:%s", strerror(errno)); return TAOS_SYSTEM_ERROR(errno); @@ -88,7 +88,7 @@ void tMergeTreeAdjust(SMultiwayMergeTreeInfo* pTree, int32_t idx) { return; } - int32_t parentId = idx >> 1; + int32_t parentId = idx >> 1; STreeNode kLeaf = pTree->pNode[idx]; while (parentId > 0) { diff --git a/source/util/src/mallocator.c b/source/util/src/tmallocator.c similarity index 98% rename from source/util/src/mallocator.c rename to source/util/src/tmallocator.c index a56fbfa597..057b908a58 100644 --- a/source/util/src/mallocator.c +++ b/source/util/src/tmallocator.c @@ -13,7 +13,8 @@ * along with this program. If not, see . */ -#include "mallocator.h" +#define _DEFAULT_SOURCE +#include "tmallocator.h" /* ------------------------ HEAP ALLOCATOR ------------------------ */ #if 0 diff --git a/source/util/src/tmd5.c b/source/util/src/tmd5.c index 807f3c8122..2ea4415d7f 100644 --- a/source/util/src/tmd5.c +++ b/source/util/src/tmd5.c @@ -33,7 +33,7 @@ *********************************************************************** */ -#include "os.h" +#define _DEFAULT_SOURCE #include "tmd5.h" /* forward declaration */ @@ -98,13 +98,13 @@ void tMD5Init(T_MD5_CTX *mdContext) { account for the presence of each of the characters inBuf[0..inLen-1] in the message whose digest is being computed. */ -void tMD5Update(T_MD5_CTX *mdContext, uint8_t *inBuf, unsigned int inLen) { - uint32_t in[16]; - int mdi; - unsigned int i, ii; +void tMD5Update(T_MD5_CTX *mdContext, uint8_t *inBuf, uint32_t inLen) { + uint32_t in[16]; + uint32_t mdi; + uint32_t i, ii; /* compute number of bytes mod 64 */ - mdi = (int)((mdContext->i[0] >> 3) & 0x3F); + mdi = (uint32_t)((mdContext->i[0] >> 3) & 0x3F); /* update number of bits */ if ((mdContext->i[0] + ((uint32_t)inLen << 3)) < mdContext->i[0]) mdContext->i[1]++; @@ -130,17 +130,17 @@ void tMD5Update(T_MD5_CTX *mdContext, uint8_t *inBuf, unsigned int inLen) { ends with the desired message digest in mdContext->digest[0...15]. */ void tMD5Final(T_MD5_CTX *mdContext) { - uint32_t in[16]; - int mdi; - unsigned int i, ii; - unsigned int padLen; + uint32_t in[16]; + uint32_t mdi; + uint32_t i, ii; + uint32_t padLen; /* save number of bits */ in[14] = mdContext->i[0]; in[15] = mdContext->i[1]; /* compute number of bytes mod 64 */ - mdi = (int)((mdContext->i[0] >> 3) & 0x3F); + mdi = (uint32_t)((mdContext->i[0] >> 3) & 0x3F); /* pad out to 56 mod 64 */ padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi); diff --git a/source/util/src/tmempool.c b/source/util/src/tmempool.c index f980a05629..1fc9bfc7ab 100644 --- a/source/util/src/tmempool.c +++ b/source/util/src/tmempool.c @@ -13,22 +13,23 @@ * along with this program. If not, see . */ -#include "tlog.h" +#define _DEFAULT_SOURCE #include "tmempool.h" +#include "tlog.h" #include "tutil.h" typedef struct { - int numOfFree; /* number of free slots */ - int first; /* the first free slot */ - int numOfBlock; /* the number of blocks */ - int blockSize; /* block size in bytes */ - int * freeList; /* the index list */ - char * pool; /* the actual mem block */ + int32_t numOfFree; /* number of free slots */ + int32_t first; /* the first free slot */ + int32_t numOfBlock; /* the number of blocks */ + int32_t blockSize; /* block size in bytes */ + int32_t *freeList; /* the index list */ + char *pool; /* the actual mem block */ pthread_mutex_t mutex; } pool_t; -mpool_h taosMemPoolInit(int numOfBlock, int blockSize) { - int i; +mpool_h taosMemPoolInit(int32_t numOfBlock, int32_t blockSize) { + int32_t i; pool_t *pool_p; if (numOfBlock <= 1 || blockSize <= 1) { @@ -47,7 +48,7 @@ mpool_h taosMemPoolInit(int numOfBlock, int blockSize) { pool_p->blockSize = blockSize; pool_p->numOfBlock = numOfBlock; pool_p->pool = (char *)malloc((size_t)(blockSize * numOfBlock)); - pool_p->freeList = (int *)malloc(sizeof(int) * (size_t)numOfBlock); + pool_p->freeList = (int32_t *)malloc(sizeof(int32_t) * (size_t)numOfBlock); if (pool_p->pool == NULL || pool_p->freeList == NULL) { uError("failed to allocate memory\n"); @@ -69,7 +70,7 @@ mpool_h taosMemPoolInit(int numOfBlock, int blockSize) { } char *taosMemPoolMalloc(mpool_h handle) { - char * pos = NULL; + char *pos = NULL; pool_t *pool_p = (pool_t *)handle; pthread_mutex_lock(&(pool_p->mutex)); @@ -88,20 +89,20 @@ char *taosMemPoolMalloc(mpool_h handle) { } void taosMemPoolFree(mpool_h handle, char *pMem) { - int index; + int32_t index; pool_t *pool_p = (pool_t *)handle; if (pMem == NULL) return; - index = (int)(pMem - pool_p->pool) % pool_p->blockSize; + index = (int32_t)(pMem - pool_p->pool) % pool_p->blockSize; if (index != 0) { uError("invalid free address:%p\n", pMem); return; } - index = (int)((pMem - pool_p->pool) / pool_p->blockSize); + index = (int32_t)((pMem - pool_p->pool) / pool_p->blockSize); if (index < 0 || index >= pool_p->numOfBlock) { - uError("mempool: error, invalid address:%p\n", pMem); + uError("mempool: error, invalid address:%p", pMem); return; } diff --git a/source/util/src/tpagedbuf.c b/source/util/src/tpagedbuf.c index c8e5bba314..45c38165a1 100644 --- a/source/util/src/tpagedbuf.c +++ b/source/util/src/tpagedbuf.c @@ -1,48 +1,49 @@ -#include "tlog.h" +#define _DEFAULT_SOURCE #include "tpagedbuf.h" #include "taoserror.h" #include "tcompression.h" #include "thash.h" +#include "tlog.h" -#define GET_DATA_PAYLOAD(_p) ((char *)(_p)->pData + POINTER_BYTES) +#define GET_DATA_PAYLOAD(_p) ((char*)(_p)->pData + POINTER_BYTES) #define NO_IN_MEM_AVAILABLE_PAGES(_b) (listNEles((_b)->lruList) >= (_b)->inMemPages) typedef struct SPageDiskInfo { - int64_t offset; - int32_t length; + int64_t offset; + int32_t length; } SPageDiskInfo, SFreeListItem; struct SPageInfo { - SListNode* pn; // point to list node struct - void* pData; - int64_t offset; - int32_t pageId; - int32_t length:29; - bool used:1; // set current page is in used - bool dirty:1; // set current buffer page is dirty or not + SListNode* pn; // point to list node struct + void* pData; + int64_t offset; + int32_t pageId; + int32_t length : 29; + bool used : 1; // set current page is in used + bool dirty : 1; // set current buffer page is dirty or not }; struct SDiskbasedBuf { int32_t numOfPages; int64_t totalBufSize; - uint64_t fileSize; // disk file size + uint64_t fileSize; // disk file size TdFilePtr pFile; - int32_t allocateId; // allocated page id - char* path; // file path - int32_t pageSize; // current used page size - int32_t inMemPages; // numOfPages that are allocated in memory - SList* freePgList; // free page list - SHashObj* groupSet; // id hash table, todo remove it + int32_t allocateId; // allocated page id + char* path; // file path + int32_t pageSize; // current used page size + int32_t inMemPages; // numOfPages that are allocated in memory + SList* freePgList; // free page list + SHashObj* groupSet; // id hash table, todo remove it SHashObj* all; SList* lruList; - void* emptyDummyIdList; // dummy id list - void* assistBuf; // assistant buffer for compress/decompress data - SArray* pFree; // free area in file - bool comp; // compressed before flushed to disk - uint64_t nextPos; // next page flush position + void* emptyDummyIdList; // dummy id list + void* assistBuf; // assistant buffer for compress/decompress data + SArray* pFree; // free area in file + bool comp; // compressed before flushed to disk + uint64_t nextPos; // next page flush position - uint64_t qId; // for debug purpose - bool printStatis; // Print statistics info when closing this buffer. + uint64_t qId; // for debug purpose + bool printStatis; // Print statistics info when closing this buffer. SDiskbasedBufStatis statis; }; @@ -50,14 +51,14 @@ static int32_t createDiskFile(SDiskbasedBuf* pBuf) { // pBuf->file = fopen(pBuf->path, "wb+"); pBuf->pFile = taosOpenFile(pBuf->path, TD_FILE_CTEATE | TD_FILE_WRITE | TD_FILE_READ | TD_FILE_TRUNC); if (pBuf->pFile == NULL) { -// qError("failed to create tmp file: %s on disk. %s", pBuf->path, strerror(errno)); + // qError("failed to create tmp file: %s on disk. %s", pBuf->path, strerror(errno)); return TAOS_SYSTEM_ERROR(errno); } return TSDB_CODE_SUCCESS; } -static char* doCompressData(void* data, int32_t srcSize, int32_t *dst, SDiskbasedBuf* pBuf) { // do nothing +static char* doCompressData(void* data, int32_t srcSize, int32_t* dst, SDiskbasedBuf* pBuf) { // do nothing if (!pBuf->comp) { *dst = srcSize; return data; @@ -69,7 +70,7 @@ static char* doCompressData(void* data, int32_t srcSize, int32_t *dst, SDiskbase return data; } -static char* doDecompressData(void* data, int32_t srcSize, int32_t *dst, SDiskbasedBuf* pBuf) { // do nothing +static char* doDecompressData(void* data, int32_t srcSize, int32_t* dst, SDiskbasedBuf* pBuf) { // do nothing if (!pBuf->comp) { *dst = srcSize; return data; @@ -89,7 +90,7 @@ static uint64_t allocatePositionInFile(SDiskbasedBuf* pBuf, size_t size) { int32_t offset = -1; size_t num = taosArrayGetSize(pBuf->pFree); - for(int32_t i = 0; i < num; ++i) { + for (int32_t i = 0; i < num; ++i) { SFreeListItem* pi = taosArrayGet(pBuf->pFree, i); if (pi->length >= size) { offset = pi->offset; @@ -105,13 +106,9 @@ static uint64_t allocatePositionInFile(SDiskbasedBuf* pBuf, size_t size) { } } -static void setPageNotInBuf(SPageInfo* pPageInfo) { - pPageInfo->pData = NULL; -} +static void setPageNotInBuf(SPageInfo* pPageInfo) { pPageInfo->pData = NULL; } -static FORCE_INLINE size_t getAllocPageSize(int32_t pageSize) { - return pageSize + POINTER_BYTES + 2; -} +static FORCE_INLINE size_t getAllocPageSize(int32_t pageSize) { return pageSize + POINTER_BYTES + 2; } /** * +--------------------------+-------------------+--------------+ @@ -140,17 +137,17 @@ static char* doFlushPageToDisk(SDiskbasedBuf* pBuf, SPageInfo* pg) { pg->offset = allocatePositionInFile(pBuf, size); pBuf->nextPos += size; - int32_t ret = taosLSeekFile(pBuf->pFile, pg->offset, SEEK_SET); - if (ret != 0) { - terrno = TAOS_SYSTEM_ERROR(errno); - return NULL; - } + int32_t ret = taosLSeekFile(pBuf->pFile, pg->offset, SEEK_SET); + if (ret != 0) { + terrno = TAOS_SYSTEM_ERROR(errno); + return NULL; + } - ret = (int32_t) taosWriteFile(pBuf->pFile, t, size); - if (ret != size) { - terrno = TAOS_SYSTEM_ERROR(errno); - return NULL; - } + ret = (int32_t)taosWriteFile(pBuf->pFile, t, size); + if (ret != size) { + terrno = TAOS_SYSTEM_ERROR(errno); + return NULL; + } if (pBuf->fileSize < pg->offset + size) { pBuf->fileSize = pg->offset + size; @@ -170,18 +167,18 @@ static char* doFlushPageToDisk(SDiskbasedBuf* pBuf, SPageInfo* pg) { pBuf->nextPos += size; } - // 3. write to disk. - int32_t ret = taosLSeekFile(pBuf->pFile, pg->offset, SEEK_SET); - if (ret != 0) { - terrno = TAOS_SYSTEM_ERROR(errno); - return NULL; - } + // 3. write to disk. + int32_t ret = taosLSeekFile(pBuf->pFile, pg->offset, SEEK_SET); + if (ret != 0) { + terrno = TAOS_SYSTEM_ERROR(errno); + return NULL; + } - ret = (int32_t) taosWriteFile(pBuf->pFile, t, size); - if (ret != size) { - terrno = TAOS_SYSTEM_ERROR(errno); - return NULL; - } + ret = (int32_t)taosWriteFile(pBuf->pFile, t, size); + if (ret != size) { + terrno = TAOS_SYSTEM_ERROR(errno); + return NULL; + } if (pBuf->fileSize < pg->offset + size) { pBuf->fileSize = pg->offset + size; @@ -190,7 +187,7 @@ static char* doFlushPageToDisk(SDiskbasedBuf* pBuf, SPageInfo* pg) { pBuf->statis.flushBytes += size; pBuf->statis.flushPages += 1; } - } else {// NOTE: the size may be -1, the this recycle page has not been flushed to disk yet. + } else { // NOTE: the size may be -1, the this recycle page has not been flushed to disk yet. size = pg->length; if (size == -1) { printf("----\n"); @@ -208,7 +205,7 @@ static char* doFlushPageToDisk(SDiskbasedBuf* pBuf, SPageInfo* pg) { static char* flushPageToDisk(SDiskbasedBuf* pBuf, SPageInfo* pg) { int32_t ret = TSDB_CODE_SUCCESS; - assert(((int64_t) pBuf->numOfPages * pBuf->pageSize) == pBuf->totalBufSize && pBuf->numOfPages >= pBuf->inMemPages); + assert(((int64_t)pBuf->numOfPages * pBuf->pageSize) == pBuf->totalBufSize && pBuf->numOfPages >= pBuf->inMemPages); if (pBuf->pFile == NULL) { if ((ret = createDiskFile(pBuf)) != TSDB_CODE_SUCCESS) { @@ -232,7 +229,7 @@ static int32_t loadPageFromDisk(SDiskbasedBuf* pBuf, SPageInfo* pg) { return ret; } - void* pPage = (void*) GET_DATA_PAYLOAD(pg); + void* pPage = (void*)GET_DATA_PAYLOAD(pg); ret = (int32_t)taosReadFile(pBuf->pFile, pPage, pg->length); if (ret != pg->length) { ret = TAOS_SYSTEM_ERROR(errno); @@ -248,7 +245,7 @@ static int32_t loadPageFromDisk(SDiskbasedBuf* pBuf, SPageInfo* pg) { } static SIDList addNewGroup(SDiskbasedBuf* pBuf, int32_t groupId) { - assert(taosHashGet(pBuf->groupSet, (const char*) &groupId, sizeof(int32_t)) == NULL); + assert(taosHashGet(pBuf->groupSet, (const char*)&groupId, sizeof(int32_t)) == NULL); SArray* pa = taosArrayInit(1, POINTER_BYTES); int32_t ret = taosHashPut(pBuf->groupSet, (const char*)&groupId, sizeof(int32_t), &pa, POINTER_BYTES); @@ -264,7 +261,7 @@ static SPageInfo* registerPage(SDiskbasedBuf* pBuf, int32_t groupId, int32_t pag if (p == NULL) { // it is a new group id list = addNewGroup(pBuf, groupId); } else { - list = (SIDList) (*p); + list = (SIDList)(*p); } pBuf->numOfPages += 1; @@ -272,13 +269,13 @@ static SPageInfo* registerPage(SDiskbasedBuf* pBuf, int32_t groupId, int32_t pag SPageInfo* ppi = malloc(sizeof(SPageInfo)); ppi->pageId = pageId; - ppi->pData = NULL; + ppi->pData = NULL; ppi->offset = -1; ppi->length = -1; - ppi->used = true; - ppi->pn = NULL; + ppi->used = true; + ppi->pn = NULL; - return *(SPageInfo**) taosArrayPush(list, &ppi); + return *(SPageInfo**)taosArrayPush(list, &ppi); } static SListNode* getEldestUnrefedPage(SDiskbasedBuf* pBuf) { @@ -286,33 +283,33 @@ static SListNode* getEldestUnrefedPage(SDiskbasedBuf* pBuf) { tdListInitIter(pBuf->lruList, &iter, TD_LIST_BACKWARD); SListNode* pn = NULL; - while((pn = tdListNext(&iter)) != NULL) { - SPageInfo* pageInfo = *(SPageInfo**) pn->data; + while ((pn = tdListNext(&iter)) != NULL) { + SPageInfo* pageInfo = *(SPageInfo**)pn->data; assert(pageInfo->pageId >= 0 && pageInfo->pn == pn); if (!pageInfo->used) { -// printf("%d is chosen\n", pageInfo->pageId); + // printf("%d is chosen\n", pageInfo->pageId); break; } else { -// printf("page %d is used, dirty:%d\n", pageInfo->pageId, pageInfo->dirty); + // printf("page %d is used, dirty:%d\n", pageInfo->pageId, pageInfo->dirty); } } -// int32_t pos = listNEles(pBuf->lruList); -// SListIter iter1 = {0}; -// tdListInitIter(pBuf->lruList, &iter1, TD_LIST_BACKWARD); -// SListNode* pn1 = NULL; -// while((pn1 = tdListNext(&iter1)) != NULL) { -// SPageInfo* pageInfo = *(SPageInfo**) pn1->data; -// printf("page %d is used, dirty:%d, pos:%d\n", pageInfo->pageId, pageInfo->dirty, pos - 1); -// pos -= 1; -// } + // int32_t pos = listNEles(pBuf->lruList); + // SListIter iter1 = {0}; + // tdListInitIter(pBuf->lruList, &iter1, TD_LIST_BACKWARD); + // SListNode* pn1 = NULL; + // while((pn1 = tdListNext(&iter1)) != NULL) { + // SPageInfo* pageInfo = *(SPageInfo**) pn1->data; + // printf("page %d is used, dirty:%d, pos:%d\n", pageInfo->pageId, pageInfo->dirty, pos - 1); + // pos -= 1; + // } return pn; } static char* evacOneDataPage(SDiskbasedBuf* pBuf) { - char* bufPage = NULL; + char* bufPage = NULL; SListNode* pn = getEldestUnrefedPage(pBuf); // all pages are referenced by user, try to allocate new space @@ -323,12 +320,12 @@ static char* evacOneDataPage(SDiskbasedBuf* pBuf) { // increase by 50% of previous mem pages pBuf->inMemPages = (int32_t)(pBuf->inMemPages * 1.5f); -// qWarn("%p in memory buf page not sufficient, expand from %d to %d, page size:%d", pBuf, prev, -// pBuf->inMemPages, pBuf->pageSize); + // qWarn("%p in memory buf page not sufficient, expand from %d to %d, page size:%d", pBuf, prev, + // pBuf->inMemPages, pBuf->pageSize); } else { tdListPopNode(pBuf->lruList, pn); - SPageInfo* d = *(SPageInfo**) pn->data; + SPageInfo* d = *(SPageInfo**)pn->data; assert(d->pn == pn); d->pn = NULL; @@ -340,26 +337,27 @@ static char* evacOneDataPage(SDiskbasedBuf* pBuf) { return bufPage; } -static void lruListPushFront(SList *pList, SPageInfo* pi) { +static void lruListPushFront(SList* pList, SPageInfo* pi) { tdListPrepend(pList, &pi); SListNode* front = tdListGetHead(pList); pi->pn = front; } -static void lruListMoveToFront(SList *pList, SPageInfo* pi) { +static void lruListMoveToFront(SList* pList, SPageInfo* pi) { tdListPopNode(pList, pi->pn); tdListPrependNode(pList, pi->pn); } static SPageInfo* getPageInfoFromPayload(void* page) { int32_t offset = offsetof(SPageInfo, pData); - char* p = page - offset; + char* p = page - offset; - SPageInfo* ppi = ((SPageInfo**) p)[0]; + SPageInfo* ppi = ((SPageInfo**)p)[0]; return ppi; } -int32_t createDiskbasedBuf(SDiskbasedBuf** pBuf, int32_t pagesize, int32_t inMemBufSize, uint64_t qId, const char* dir) { +int32_t createDiskbasedBuf(SDiskbasedBuf** pBuf, int32_t pagesize, int32_t inMemBufSize, uint64_t qId, + const char* dir) { *pBuf = calloc(1, sizeof(SDiskbasedBuf)); SDiskbasedBuf* pPBuf = *pBuf; @@ -367,17 +365,17 @@ int32_t createDiskbasedBuf(SDiskbasedBuf** pBuf, int32_t pagesize, int32_t inMem return TSDB_CODE_OUT_OF_MEMORY; } - pPBuf->pageSize = pagesize; - pPBuf->numOfPages = 0; // all pages are in buffer in the first place + pPBuf->pageSize = pagesize; + pPBuf->numOfPages = 0; // all pages are in buffer in the first place pPBuf->totalBufSize = 0; - pPBuf->inMemPages = inMemBufSize/pagesize; // maximum allowed pages, it is a soft limit. - pPBuf->allocateId = -1; - pPBuf->comp = true; - pPBuf->pFile = NULL; - pPBuf->qId = qId; - pPBuf->fileSize = 0; - pPBuf->pFree = taosArrayInit(4, sizeof(SFreeListItem)); - pPBuf->freePgList = tdListNew(POINTER_BYTES); + pPBuf->inMemPages = inMemBufSize / pagesize; // maximum allowed pages, it is a soft limit. + pPBuf->allocateId = -1; + pPBuf->comp = true; + pPBuf->pFile = NULL; + pPBuf->qId = qId; + pPBuf->fileSize = 0; + pPBuf->pFree = taosArrayInit(4, sizeof(SFreeListItem)); + pPBuf->freePgList = tdListNew(POINTER_BYTES); // at least more than 2 pages must be in memory assert(inMemBufSize >= pagesize * 2); @@ -386,8 +384,8 @@ int32_t createDiskbasedBuf(SDiskbasedBuf** pBuf, int32_t pagesize, int32_t inMem // init id hash table _hash_fn_t fn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT); - pPBuf->groupSet = taosHashInit(10, fn, true, false); - pPBuf->assistBuf = malloc(pPBuf->pageSize + 2); // EXTRA BYTES + pPBuf->groupSet = taosHashInit(10, fn, true, false); + pPBuf->assistBuf = malloc(pPBuf->pageSize + 2); // EXTRA BYTES pPBuf->all = taosHashInit(10, fn, true, false); char path[PATH_MAX] = {0}; @@ -396,8 +394,9 @@ int32_t createDiskbasedBuf(SDiskbasedBuf** pBuf, int32_t pagesize, int32_t inMem pPBuf->emptyDummyIdList = taosArrayInit(1, sizeof(int32_t)); -// qDebug("QInfo:0x%"PRIx64" create resBuf for output, page size:%d, inmem buf pages:%d, file:%s", qId, pPBuf->pageSize, -// pPBuf->inMemPages, pPBuf->path); + // qDebug("QInfo:0x%"PRIx64" create resBuf for output, page size:%d, inmem buf pages:%d, file:%s", qId, + // pPBuf->pageSize, + // pPBuf->inMemPages, pPBuf->path); return TSDB_CODE_SUCCESS; } @@ -419,11 +418,11 @@ void* getNewBufPage(SDiskbasedBuf* pBuf, int32_t groupId, int32_t* pageId) { SPageInfo* pi = NULL; if (listNEles(pBuf->freePgList) != 0) { SListNode* pItem = tdListPopHead(pBuf->freePgList); - pi = *(SPageInfo**) pItem->data; + pi = *(SPageInfo**)pItem->data; pi->used = true; - *pageId = pi->pageId; + *pageId = pi->pageId; tfree(pItem); - } else {// create a new pageinfo + } else { // create a new pageinfo // register new id in this group *pageId = (++pBuf->allocateId); @@ -447,7 +446,7 @@ void* getNewBufPage(SDiskbasedBuf* pBuf, int32_t groupId, int32_t* pageId) { } ((void**)pi->pData)[0] = pi; - return (void *)(GET_DATA_PAYLOAD(pi)); + return (void*)(GET_DATA_PAYLOAD(pi)); } void* getBufPage(SDiskbasedBuf* pBuf, int32_t id) { @@ -457,21 +456,21 @@ void* getBufPage(SDiskbasedBuf* pBuf, int32_t id) { SPageInfo** pi = taosHashGet(pBuf->all, &id, sizeof(int32_t)); assert(pi != NULL && *pi != NULL); - if ((*pi)->pData != NULL) { // it is in memory + if ((*pi)->pData != NULL) { // it is in memory // no need to update the LRU list if only one page exists if (pBuf->numOfPages == 1) { (*pi)->used = true; - return (void *)(GET_DATA_PAYLOAD(*pi)); + return (void*)(GET_DATA_PAYLOAD(*pi)); } - SPageInfo** pInfo = (SPageInfo**) ((*pi)->pn->data); + SPageInfo** pInfo = (SPageInfo**)((*pi)->pn->data); assert(*pInfo == *pi); lruListMoveToFront(pBuf->lruList, (*pi)); (*pi)->used = true; - return (void *)(GET_DATA_PAYLOAD(*pi)); - } else { // not in memory + return (void*)(GET_DATA_PAYLOAD(*pi)); + } else { // not in memory assert((*pi)->pData == NULL && (*pi)->pn == NULL && (*pi)->length >= 0 && (*pi)->offset >= 0); char* availablePage = NULL; @@ -499,7 +498,7 @@ void* getBufPage(SDiskbasedBuf* pBuf, int32_t id) { return NULL; } - return (void *)(GET_DATA_PAYLOAD(*pi)); + return (void*)(GET_DATA_PAYLOAD(*pi)); } } @@ -527,7 +526,7 @@ SIDList getDataBufPagesIdList(SDiskbasedBuf* pBuf, int32_t groupId) { if (p == NULL) { // it is a new group id return pBuf->emptyDummyIdList; } else { - return (SArray*) (*p); + return (SArray*)(*p); } } @@ -539,30 +538,34 @@ void destroyDiskbasedBuf(SDiskbasedBuf* pBuf) { dBufPrintStatis(pBuf); if (pBuf->pFile != NULL) { - uDebug("Paged buffer closed, total:%.2f Kb (%d Pages), inmem size:%.2f Kb (%d Pages), file size:%.2f Kb, page size:%.2f Kb, %"PRIx64"\n", - pBuf->totalBufSize/1024.0, pBuf->numOfPages, listNEles(pBuf->lruList) * pBuf->pageSize / 1024.0, - listNEles(pBuf->lruList), pBuf->fileSize/1024.0, pBuf->pageSize/1024.0f, pBuf->qId); + uDebug( + "Paged buffer closed, total:%.2f Kb (%d Pages), inmem size:%.2f Kb (%d Pages), file size:%.2f Kb, page " + "size:%.2f Kb, %" PRIx64 "\n", + pBuf->totalBufSize / 1024.0, pBuf->numOfPages, listNEles(pBuf->lruList) * pBuf->pageSize / 1024.0, + listNEles(pBuf->lruList), pBuf->fileSize / 1024.0, pBuf->pageSize / 1024.0f, pBuf->qId); - taosCloseFile(&pBuf->pFile); + taosCloseFile(&pBuf->pFile); } else { - uDebug("Paged buffer closed, total:%.2f Kb, no file created, %"PRIx64, pBuf->totalBufSize/1024.0, pBuf->qId); + uDebug("Paged buffer closed, total:%.2f Kb, no file created, %" PRIx64, pBuf->totalBufSize / 1024.0, pBuf->qId); } // print the statistics information { - SDiskbasedBufStatis *ps = &pBuf->statis; - uDebug("Get/Release pages:%d/%d, flushToDisk:%.2f Kb (%d Pages), loadFromDisk:%.2f Kb (%d Pages), avgPageSize:%.2f Kb\n" - , ps->getPages, ps->releasePages, ps->flushBytes/1024.0f, ps->flushPages, ps->loadBytes/1024.0f, ps->loadPages - , ps->loadBytes/(1024.0 * ps->loadPages)); + SDiskbasedBufStatis* ps = &pBuf->statis; + uDebug( + "Get/Release pages:%d/%d, flushToDisk:%.2f Kb (%d Pages), loadFromDisk:%.2f Kb (%d Pages), avgPageSize:%.2f " + "Kb\n", + ps->getPages, ps->releasePages, ps->flushBytes / 1024.0f, ps->flushPages, ps->loadBytes / 1024.0f, + ps->loadPages, ps->loadBytes / (1024.0 * ps->loadPages)); } remove(pBuf->path); tfree(pBuf->path); SArray** p = taosHashIterate(pBuf->groupSet, NULL); - while(p) { + while (p) { size_t n = taosArrayGetSize(*p); - for(int32_t i = 0; i < n; ++i) { + for (int32_t i = 0; i < n; ++i) { SPageInfo* pi = taosArrayGetP(*p, i); tfree(pi->pData); tfree(pi); @@ -586,7 +589,7 @@ void destroyDiskbasedBuf(SDiskbasedBuf* pBuf) { } SPageInfo* getLastPageInfo(SIDList pList) { - size_t size = taosArrayGetSize(pList); + size_t size = taosArrayGetSize(pList); SPageInfo* pPgInfo = taosArrayGetP(pList, size - 1); return pPgInfo; } @@ -596,31 +599,23 @@ int32_t getPageId(const SPageInfo* pPgInfo) { return pPgInfo->pageId; } -int32_t getBufPageSize(const SDiskbasedBuf* pBuf) { - return pBuf->pageSize; -} +int32_t getBufPageSize(const SDiskbasedBuf* pBuf) { return pBuf->pageSize; } -int32_t getNumOfInMemBufPages(const SDiskbasedBuf* pBuf) { - return pBuf->inMemPages; -} +int32_t getNumOfInMemBufPages(const SDiskbasedBuf* pBuf) { return pBuf->inMemPages; } -bool isAllDataInMemBuf(const SDiskbasedBuf* pBuf) { - return pBuf->fileSize == 0; -} +bool isAllDataInMemBuf(const SDiskbasedBuf* pBuf) { return pBuf->fileSize == 0; } void setBufPageDirty(void* pPage, bool dirty) { SPageInfo* ppi = getPageInfoFromPayload(pPage); ppi->dirty = dirty; } -void setBufPageCompressOnDisk(SDiskbasedBuf* pBuf, bool comp) { - pBuf->comp = comp; -} +void setBufPageCompressOnDisk(SDiskbasedBuf* pBuf, bool comp) { pBuf->comp = comp; } -void dBufSetBufPageRecycled(SDiskbasedBuf *pBuf, void* pPage) { +void dBufSetBufPageRecycled(SDiskbasedBuf* pBuf, void* pPage) { SPageInfo* ppi = getPageInfoFromPayload(pPage); - ppi->used = false; + ppi->used = false; ppi->dirty = false; // add this pageinfo into the free page info list @@ -631,13 +626,9 @@ void dBufSetBufPageRecycled(SDiskbasedBuf *pBuf, void* pPage) { tdListAppend(pBuf->freePgList, &ppi); } -void dBufSetPrintInfo(SDiskbasedBuf* pBuf) { - pBuf->printStatis = true; -} +void dBufSetPrintInfo(SDiskbasedBuf* pBuf) { pBuf->printStatis = true; } -SDiskbasedBufStatis getDBufStatis(const SDiskbasedBuf* pBuf) { - return pBuf->statis; -} +SDiskbasedBufStatis getDBufStatis(const SDiskbasedBuf* pBuf) { return pBuf->statis; } void dBufPrintStatis(const SDiskbasedBuf* pBuf) { if (!pBuf->printStatis) { @@ -657,4 +648,3 @@ void dBufPrintStatis(const SDiskbasedBuf* pBuf) { ps->getPages, ps->releasePages, ps->flushBytes / 1024.0f, ps->flushPages, ps->loadBytes / 1024.0f, ps->loadPages, ps->loadBytes / (1024.0 * ps->loadPages)); } - diff --git a/source/util/src/tqueue.c b/source/util/src/tqueue.c index 0f6e2610c5..0003522033 100644 --- a/source/util/src/tqueue.c +++ b/source/util/src/tqueue.c @@ -30,19 +30,19 @@ typedef struct STaosQueue { int32_t itemSize; int32_t numOfItems; int32_t threadId; - STaosQnode * head; - STaosQnode * tail; - STaosQueue * next; // for queue set - STaosQset * qset; // for queue set - void * ahandle; // for queue set + STaosQnode *head; + STaosQnode *tail; + STaosQueue *next; // for queue set + STaosQset *qset; // for queue set + void *ahandle; // for queue set FItem itemFp; FItems itemsFp; pthread_mutex_t mutex; } STaosQueue; typedef struct STaosQset { - STaosQueue * head; - STaosQueue * current; + STaosQueue *head; + STaosQueue *current; pthread_mutex_t mutex; int32_t numOfQueues; int32_t numOfItems; @@ -82,7 +82,7 @@ void taosSetQueueFp(STaosQueue *queue, FItem itemFp, FItems itemsFp) { void taosCloseQueue(STaosQueue *queue) { if (queue == NULL) return; STaosQnode *pTemp; - STaosQset * qset; + STaosQset *qset; pthread_mutex_lock(&queue->mutex); STaosQnode *pNode = queue->head; diff --git a/source/util/src/tref.c b/source/util/src/tref.c index ca4388ec26..a9f6c21bf8 100644 --- a/source/util/src/tref.c +++ b/source/util/src/tref.c @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#include "os.h" - +#define _DEFAULT_SOURCE +#include "tref.h" #include "taoserror.h" #include "tlog.h" #include "tutil.h" @@ -25,49 +25,48 @@ #define TSDB_REF_STATE_DELETED 2 typedef struct SRefNode { - struct SRefNode *prev; // previous node - struct SRefNode *next; // next node - void *p; // pointer to resource protected, - int64_t rid; // reference ID - int32_t count; // number of references - int removed; // 1: removed + struct SRefNode *prev; // previous node + struct SRefNode *next; // next node + void *p; // pointer to resource protected, + int64_t rid; // reference ID + int32_t count; // number of references + int32_t removed; // 1: removed } SRefNode; - + typedef struct { - SRefNode **nodeList; // array of SRefNode linked list - int state; // 0: empty, 1: active; 2: deleted - int rsetId; // refSet ID, global unique - int64_t rid; // increase by one for each new reference - int max; // mod - int32_t count; // total number of SRefNodes in this set + SRefNode **nodeList; // array of SRefNode linked list + int32_t state; // 0: empty, 1: active; 2: deleted + int32_t rsetId; // refSet ID, global unique + int64_t rid; // increase by one for each new reference + int32_t max; // mod + int32_t count; // total number of SRefNodes in this set int64_t *lockedBy; - void (*fp)(void *); + void (*fp)(void *); } SRefSet; static SRefSet tsRefSetList[TSDB_REF_OBJECTS]; static pthread_once_t tsRefModuleInit = PTHREAD_ONCE_INIT; static pthread_mutex_t tsRefMutex; -static int tsRefSetNum = 0; -static int tsNextId = 0; +static int32_t tsRefSetNum = 0; +static int32_t tsNextId = 0; -static void taosInitRefModule(void); -static void taosLockList(int64_t *lockedBy); -static void taosUnlockList(int64_t *lockedBy); -static void taosIncRsetCount(SRefSet *pSet); -static void taosDecRsetCount(SRefSet *pSet); -static int taosDecRefCount(int rsetId, int64_t rid, int remove); +static void taosInitRefModule(void); +static void taosLockList(int64_t *lockedBy); +static void taosUnlockList(int64_t *lockedBy); +static void taosIncRsetCount(SRefSet *pSet); +static void taosDecRsetCount(SRefSet *pSet); +static int32_t taosDecRefCount(int32_t rsetId, int64_t rid, int32_t remove); -int taosOpenRef(int max, void (*fp)(void *)) -{ +int32_t taosOpenRef(int32_t max, void (*fp)(void *)) { SRefNode **nodeList; SRefSet *pSet; int64_t *lockedBy; - int i, rsetId; + int32_t i, rsetId; pthread_once(&tsRefModuleInit, taosInitRefModule); nodeList = calloc(sizeof(SRefNode *), (size_t)max); - if (nodeList == NULL) { + if (nodeList == NULL) { terrno = TSDB_CODE_REF_NO_MEMORY; return -1; } @@ -83,7 +82,7 @@ int taosOpenRef(int max, void (*fp)(void *)) for (i = 0; i < TSDB_REF_OBJECTS; ++i) { tsNextId = (tsNextId + 1) % TSDB_REF_OBJECTS; - if (tsNextId == 0) tsNextId = 1; // dont use 0 as rsetId + if (tsNextId == 0) tsNextId = 1; // dont use 0 as rsetId if (tsRefSetList[tsNextId].state == TSDB_REF_STATE_EMPTY) break; } @@ -103,8 +102,8 @@ int taosOpenRef(int max, void (*fp)(void *)) uTrace("rsetId:%d is opened, max:%d, fp:%p refSetNum:%d", rsetId, max, fp, tsRefSetNum); } else { rsetId = TSDB_CODE_REF_FULL; - free (nodeList); - free (lockedBy); + free(nodeList); + free(lockedBy); uTrace("run out of Ref ID, maximum:%d refSetNum:%d", TSDB_REF_OBJECTS, tsRefSetNum); } @@ -113,10 +112,9 @@ int taosOpenRef(int max, void (*fp)(void *)) return rsetId; } -int taosCloseRef(int rsetId) -{ - SRefSet *pSet; - int deleted = 0; +int32_t taosCloseRef(int32_t rsetId) { + SRefSet *pSet; + int32_t deleted = 0; if (rsetId < 0 || rsetId >= TSDB_REF_OBJECTS) { uTrace("rsetId:%d is invalid, out of range", rsetId); @@ -143,9 +141,8 @@ int taosCloseRef(int rsetId) return 0; } -int64_t taosAddRef(int rsetId, void *p) -{ - int hash; +int64_t taosAddRef(int32_t rsetId, void *p) { + int32_t hash; SRefNode *pNode; SRefSet *pSet; int64_t rid = 0; @@ -173,7 +170,7 @@ int64_t taosAddRef(int rsetId, void *p) rid = atomic_add_fetch_64(&pSet->rid, 1); hash = rid % pSet->max; - taosLockList(pSet->lockedBy+hash); + taosLockList(pSet->lockedBy + hash); pNode->p = p; pNode->rid = rid; @@ -186,26 +183,22 @@ int64_t taosAddRef(int rsetId, void *p) uTrace("rsetId:%d p:%p rid:%" PRId64 " is added, count:%d", rsetId, p, rid, pSet->count); - taosUnlockList(pSet->lockedBy+hash); + taosUnlockList(pSet->lockedBy + hash); return rid; } -int taosRemoveRef(int rsetId, int64_t rid) -{ - return taosDecRefCount(rsetId, rid, 1); -} +int32_t taosRemoveRef(int32_t rsetId, int64_t rid) { return taosDecRefCount(rsetId, rid, 1); } // if rid is 0, return the first p in hash list, otherwise, return the next after current rid -void *taosAcquireRef(int rsetId, int64_t rid) -{ - int hash; +void *taosAcquireRef(int32_t rsetId, int64_t rid) { + int32_t hash; SRefNode *pNode; SRefSet *pSet; void *p = NULL; if (rsetId < 0 || rsetId >= TSDB_REF_OBJECTS) { - //uTrace("rsetId:%d rid:%" PRId64 " failed to acquire, rsetId not valid", rsetId, rid); + // uTrace("rsetId:%d rid:%" PRId64 " failed to acquire, rsetId not valid", rsetId, rid); terrno = TSDB_CODE_REF_INVALID_ID; return NULL; } @@ -226,7 +219,7 @@ void *taosAcquireRef(int rsetId, int64_t rid) } hash = rid % pSet->max; - taosLockList(pSet->lockedBy+hash); + taosLockList(pSet->lockedBy + hash); pNode = pSet->nodeList[hash]; @@ -252,20 +245,17 @@ void *taosAcquireRef(int rsetId, int64_t rid) uTrace("rsetId:%d rid:%" PRId64 " is not there, failed to acquire", rsetId, rid); } - taosUnlockList(pSet->lockedBy+hash); + taosUnlockList(pSet->lockedBy + hash); taosDecRsetCount(pSet); return p; } -int taosReleaseRef(int rsetId, int64_t rid) -{ - return taosDecRefCount(rsetId, rid, 0); -} +int32_t taosReleaseRef(int32_t rsetId, int64_t rid) { return taosDecRefCount(rsetId, rid, 0); } // if rid is 0, return the first p in hash list, otherwise, return the next after current rid -void *taosIterateRef(int rsetId, int64_t rid) { +void *taosIterateRef(int32_t rsetId, int64_t rid) { SRefNode *pNode = NULL; SRefSet *pSet; @@ -293,10 +283,10 @@ void *taosIterateRef(int rsetId, int64_t rid) { do { newP = NULL; - int hash = 0; + int32_t hash = 0; if (rid > 0) { hash = rid % pSet->max; - taosLockList(pSet->lockedBy+hash); + taosLockList(pSet->lockedBy + hash); pNode = pSet->nodeList[hash]; while (pNode) { @@ -307,7 +297,7 @@ void *taosIterateRef(int rsetId, int64_t rid) { if (pNode == NULL) { uError("rsetId:%d rid:%" PRId64 " not there, quit", rsetId, rid); terrno = TSDB_CODE_REF_NOT_EXIST; - taosUnlockList(pSet->lockedBy+hash); + taosUnlockList(pSet->lockedBy + hash); taosDecRsetCount(pSet); return NULL; } @@ -320,14 +310,14 @@ void *taosIterateRef(int rsetId, int64_t rid) { pNode = pNode->next; } if (pNode == NULL) { - taosUnlockList(pSet->lockedBy+hash); + taosUnlockList(pSet->lockedBy + hash); hash++; } } if (pNode == NULL) { for (; hash < pSet->max; ++hash) { - taosLockList(pSet->lockedBy+hash); + taosLockList(pSet->lockedBy + hash); pNode = pSet->nodeList[hash]; if (pNode) { // check first place @@ -337,14 +327,14 @@ void *taosIterateRef(int rsetId, int64_t rid) { } if (pNode) break; } - taosUnlockList(pSet->lockedBy+hash); + taosUnlockList(pSet->lockedBy + hash); } } if (pNode) { pNode->count++; // acquire it newP = pNode->p; - taosUnlockList(pSet->lockedBy+hash); + taosUnlockList(pSet->lockedBy + hash); uTrace("rsetId:%d p:%p rid:%" PRId64 " is returned", rsetId, newP, rid); } else { uTrace("rsetId:%d the list is over", rsetId); @@ -359,22 +349,21 @@ void *taosIterateRef(int rsetId, int64_t rid) { return newP; } -int taosListRef() { +int32_t taosListRef() { SRefSet *pSet; SRefNode *pNode; - int num = 0; + int32_t num = 0; pthread_mutex_lock(&tsRefMutex); - for (int i = 0; i < TSDB_REF_OBJECTS; ++i) { + for (int32_t i = 0; i < TSDB_REF_OBJECTS; ++i) { pSet = tsRefSetList + i; - if (pSet->state == TSDB_REF_STATE_EMPTY) - continue; + if (pSet->state == TSDB_REF_STATE_EMPTY) continue; uInfo("rsetId:%d state:%d count::%d", i, pSet->state, pSet->count); - for (int j=0; j < pSet->max; ++j) { + for (int32_t j = 0; j < pSet->max; ++j) { pNode = pSet->nodeList[j]; while (pNode) { @@ -390,12 +379,12 @@ int taosListRef() { return num; } -static int taosDecRefCount(int rsetId, int64_t rid, int remove) { - int hash; +static int32_t taosDecRefCount(int32_t rsetId, int64_t rid, int32_t remove) { + int32_t hash; SRefSet *pSet; SRefNode *pNode; - int released = 0; - int code = 0; + int32_t released = 0; + int32_t code = 0; if (rsetId < 0 || rsetId >= TSDB_REF_OBJECTS) { uTrace("rsetId:%d rid:%" PRId64 " failed to remove, rsetId not valid", rsetId, rid); @@ -417,12 +406,11 @@ static int taosDecRefCount(int rsetId, int64_t rid, int remove) { } hash = rid % pSet->max; - taosLockList(pSet->lockedBy+hash); + taosLockList(pSet->lockedBy + hash); pNode = pSet->nodeList[hash]; while (pNode) { - if (pNode->rid == rid) - break; + if (pNode->rid == rid) break; pNode = pNode->next; } @@ -437,13 +425,13 @@ static int taosDecRefCount(int rsetId, int64_t rid, int remove) { } else { pSet->nodeList[hash] = pNode->next; } - + if (pNode->next) { pNode->next->prev = pNode->prev; } released = 1; } else { - uTrace("rsetId:%d p:%p rid:%" PRId64 " is released", rsetId, pNode->p, rid); + uTrace("rsetId:%d p:%p rid:%" PRId64 " is released", rsetId, pNode->p, rid); } } else { uTrace("rsetId:%d rid:%" PRId64 " is not there, failed to release/remove", rsetId, rid); @@ -451,10 +439,11 @@ static int taosDecRefCount(int rsetId, int64_t rid, int remove) { code = -1; } - taosUnlockList(pSet->lockedBy+hash); + taosUnlockList(pSet->lockedBy + hash); if (released) { - uTrace("rsetId:%d p:%p rid:%" PRId64 " is removed, count:%d, free mem: %p", rsetId, pNode->p, rid, pSet->count, pNode); + uTrace("rsetId:%d p:%p rid:%" PRId64 " is removed, count:%d, free mem: %p", rsetId, pNode->p, rid, pSet->count, + pNode); (*pSet->fp)(pNode->p); free(pNode); @@ -466,7 +455,7 @@ static int taosDecRefCount(int rsetId, int64_t rid, int remove) { static void taosLockList(int64_t *lockedBy) { int64_t tid = taosGetSelfPthreadId(); - int i = 0; + int32_t i = 0; while (atomic_val_compare_exchange_64(lockedBy, 0, tid) != 0) { if (++i % 100 == 0) { sched_yield(); @@ -481,9 +470,7 @@ static void taosUnlockList(int64_t *lockedBy) { } } -static void taosInitRefModule(void) { - pthread_mutex_init(&tsRefMutex, NULL); -} +static void taosInitRefModule(void) { pthread_mutex_init(&tsRefMutex, NULL); } static void taosIncRsetCount(SRefSet *pSet) { atomic_add_fetch_32(&pSet->count, 1); @@ -512,4 +499,3 @@ static void taosDecRsetCount(SRefSet *pSet) { pthread_mutex_unlock(&tsRefMutex); } - diff --git a/source/util/src/tsched.c b/source/util/src/tsched.c index 7db5abdd24..740e742bad 100644 --- a/source/util/src/tsched.c +++ b/source/util/src/tsched.c @@ -13,34 +13,35 @@ * along with this program. If not, see . */ -#include "tdef.h" -#include "tutil.h" -#include "tlog.h" +#define _DEFAULT_SOURCE #include "tsched.h" +#include "tdef.h" +#include "tlog.h" #include "ttimer.h" +#include "tutil.h" -#define DUMP_SCHEDULER_TIME_WINDOW 30000 //every 30sec, take a snap shot of task queue. +#define DUMP_SCHEDULER_TIME_WINDOW 30000 // every 30sec, take a snap shot of task queue. typedef struct { char label[TSDB_LABEL_LEN]; tsem_t emptySem; tsem_t fullSem; pthread_mutex_t queueMutex; - int fullSlot; - int emptySlot; - int queueSize; - int numOfThreads; - pthread_t * qthread; - SSchedMsg * queue; + int32_t fullSlot; + int32_t emptySlot; + int32_t queueSize; + int32_t numOfThreads; + pthread_t *qthread; + SSchedMsg *queue; bool stop; - void* pTmrCtrl; - void* pTimer; + void *pTmrCtrl; + void *pTimer; } SSchedQueue; static void *taosProcessSchedQueue(void *param); -static void taosDumpSchedulerStatus(void *qhandle, void *tmrId); +static void taosDumpSchedulerStatus(void *qhandle, void *tmrId); -void *taosInitScheduler(int queueSize, int numOfThreads, const char *label) { +void *taosInitScheduler(int32_t queueSize, int32_t numOfThreads, const char *label) { SSchedQueue *pSched = (SSchedQueue *)calloc(sizeof(SSchedQueue), 1); if (pSched == NULL) { uError("%s: no enough memory for pSched", label); @@ -62,7 +63,7 @@ void *taosInitScheduler(int queueSize, int numOfThreads, const char *label) { } pSched->queueSize = queueSize; - tstrncpy(pSched->label, label, sizeof(pSched->label)); // fix buffer overflow + tstrncpy(pSched->label, label, sizeof(pSched->label)); // fix buffer overflow pSched->fullSlot = 0; pSched->emptySlot = 0; @@ -73,7 +74,7 @@ void *taosInitScheduler(int queueSize, int numOfThreads, const char *label) { return NULL; } - if (tsem_init(&pSched->emptySem, 0, (unsigned int)pSched->queueSize) != 0) { + if (tsem_init(&pSched->emptySem, 0, (uint32_t)pSched->queueSize) != 0) { uError("init %s:empty semaphore failed(%s)", label, strerror(errno)); taosCleanUpScheduler(pSched); return NULL; @@ -86,11 +87,11 @@ void *taosInitScheduler(int queueSize, int numOfThreads, const char *label) { } pSched->stop = false; - for (int i = 0; i < numOfThreads; ++i) { + for (int32_t i = 0; i < numOfThreads; ++i) { pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); - int code = pthread_create(pSched->qthread + i, &attr, taosProcessSchedQueue, (void *)pSched); + int32_t code = pthread_create(pSched->qthread + i, &attr, taosProcessSchedQueue, (void *)pSched); pthread_attr_destroy(&attr); if (code != 0) { uError("%s: failed to create rpc thread(%s)", label, strerror(errno)); @@ -105,8 +106,8 @@ void *taosInitScheduler(int queueSize, int numOfThreads, const char *label) { return (void *)pSched; } -void *taosInitSchedulerWithInfo(int queueSize, int numOfThreads, const char *label, void *tmrCtrl) { - SSchedQueue* pSched = taosInitScheduler(queueSize, numOfThreads, label); +void *taosInitSchedulerWithInfo(int32_t queueSize, int32_t numOfThreads, const char *label, void *tmrCtrl) { + SSchedQueue *pSched = taosInitScheduler(queueSize, numOfThreads, label); if (tmrCtrl != NULL && pSched != NULL) { pSched->pTmrCtrl = tmrCtrl; @@ -119,7 +120,7 @@ void *taosInitSchedulerWithInfo(int queueSize, int numOfThreads, const char *lab void *taosProcessSchedQueue(void *scheduler) { SSchedMsg msg; SSchedQueue *pSched = (SSchedQueue *)scheduler; - int ret = 0; + int32_t ret = 0; char name[16] = {0}; snprintf(name, tListLen(name), "%s-taskQ", pSched->label); @@ -164,7 +165,7 @@ void *taosProcessSchedQueue(void *scheduler) { void taosScheduleTask(void *queueScheduler, SSchedMsg *pMsg) { SSchedQueue *pSched = (SSchedQueue *)queueScheduler; - int ret = 0; + int32_t ret = 0; if (pSched == NULL) { uError("sched is not ready, msg:%p is dropped", pMsg); @@ -200,12 +201,12 @@ void taosCleanUpScheduler(void *param) { if (pSched == NULL) return; pSched->stop = true; - for (int i = 0; i < pSched->numOfThreads; ++i) { - if (taosCheckPthreadValid(pSched->qthread[i])) { + for (int32_t i = 0; i < pSched->numOfThreads; ++i) { + if (taosCheckPthreadValid(pSched->qthread[i])) { tsem_post(&pSched->fullSem); } } - for (int i = 0; i < pSched->numOfThreads; ++i) { + for (int32_t i = 0; i < pSched->numOfThreads; ++i) { if (taosCheckPthreadValid(pSched->qthread[i])) { pthread_join(pSched->qthread[i], NULL); } @@ -214,14 +215,14 @@ void taosCleanUpScheduler(void *param) { tsem_destroy(&pSched->emptySem); tsem_destroy(&pSched->fullSem); pthread_mutex_destroy(&pSched->queueMutex); - + if (pSched->pTimer) { taosTmrStopA(&pSched->pTimer); } if (pSched->queue) free(pSched->queue); if (pSched->qthread) free(pSched->qthread); - free(pSched); // fix memory leak + free(pSched); // fix memory leak } // for debug purpose, dump the scheduler status every 1min. @@ -230,11 +231,11 @@ void taosDumpSchedulerStatus(void *qhandle, void *tmrId) { if (pSched == NULL || pSched->pTimer == NULL || pSched->pTimer != tmrId) { return; } - + int32_t size = ((pSched->emptySlot - pSched->fullSlot) + pSched->queueSize) % pSched->queueSize; if (size > 0) { uDebug("scheduler:%s, current tasks in queue:%d, task thread:%d", pSched->label, size, pSched->numOfThreads); } - + taosTmrReset(taosDumpSchedulerStatus, DUMP_SCHEDULER_TIME_WINDOW, pSched, pSched->pTmrCtrl, &pSched->pTimer); } diff --git a/source/util/src/tskiplist.c b/source/util/src/tskiplist.c index 1c325ca0a8..6b89ed2c43 100644 --- a/source/util/src/tskiplist.c +++ b/source/util/src/tskiplist.c @@ -14,13 +14,14 @@ * along with this program. If not, see . */ -#include "compare.h" +#define _DEFAULT_SOURCE #include "tskiplist.h" -#include "tutil.h" +#include "tcompare.h" #include "tlog.h" +#include "tutil.h" -static int initForwardBackwardPtr(SSkipList *pSkipList); -static SSkipListNode * getPriorNode(SSkipList *pSkipList, const char *val, int32_t order, SSkipListNode **pCur); +static int32_t initForwardBackwardPtr(SSkipList *pSkipList); +static SSkipListNode *getPriorNode(SSkipList *pSkipList, const char *val, int32_t order, SSkipListNode **pCur); static void tSkipListRemoveNodeImpl(SSkipList *pSkipList, SSkipListNode *pNode); static void tSkipListCorrectLevel(SSkipList *pSkipList); static SSkipListIterator *doCreateSkipListIterator(SSkipList *pSkipList, int32_t order); @@ -31,10 +32,9 @@ static SSkipListNode *tSkipListNewNode(uint8_t level); static SSkipListNode *tSkipListPutImpl(SSkipList *pSkipList, void *pData, SSkipListNode **direction, bool isForward, bool hasDup); - -static FORCE_INLINE int tSkipListWLock(SSkipList *pSkipList); -static FORCE_INLINE int tSkipListRLock(SSkipList *pSkipList); -static FORCE_INLINE int tSkipListUnlock(SSkipList *pSkipList); +static FORCE_INLINE int32_t tSkipListWLock(SSkipList *pSkipList); +static FORCE_INLINE int32_t tSkipListRLock(SSkipList *pSkipList); +static FORCE_INLINE int32_t tSkipListUnlock(SSkipList *pSkipList); static FORCE_INLINE int32_t getSkipListRandLevel(SSkipList *pSkipList); SSkipList *tSkipListCreate(uint8_t maxLevel, uint8_t keyType, uint16_t keyLen, __compar_fn_t comparFn, uint8_t flags, @@ -138,21 +138,21 @@ void tSkipListPutBatchByIter(SSkipList *pSkipList, void *iter, iter_next_fn_t it SSkipListNode *backward[MAX_SKIP_LIST_LEVEL] = {0}; SSkipListNode *forward[MAX_SKIP_LIST_LEVEL] = {0}; bool hasDup = false; - char * pKey = NULL; - char * pDataKey = NULL; - int compare = 0; + char *pKey = NULL; + char *pDataKey = NULL; + int32_t compare = 0; tSkipListWLock(pSkipList); - void* pData = iterate(iter); - if(pData == NULL) return; + void *pData = iterate(iter); + if (pData == NULL) return; // backward to put the first data hasDup = tSkipListGetPosToPut(pSkipList, backward, pData); tSkipListPutImpl(pSkipList, pData, backward, false, hasDup); - for (int level = 0; level < pSkipList->maxLevel; level++) { + for (int32_t level = 0; level < pSkipList->maxLevel; level++) { forward[level] = SL_NODE_GET_BACKWARD_POINTER(backward[level], level); } @@ -165,12 +165,12 @@ void tSkipListPutBatchByIter(SSkipList *pSkipList, void *iter, iter_next_fn_t it pKey = SL_GET_MAX_KEY(pSkipList); compare = pSkipList->comparFn(pDataKey, pKey); if (compare > 0) { - for (int i = 0; i < pSkipList->maxLevel; i++) { + for (int32_t i = 0; i < pSkipList->maxLevel; i++) { forward[i] = SL_NODE_GET_BACKWARD_POINTER(pSkipList->pTail, i); } } else { SSkipListNode *px = pSkipList->pHead; - for (int i = pSkipList->maxLevel - 1; i >= 0; --i) { + for (int32_t i = pSkipList->maxLevel - 1; i >= 0; --i) { if (i < pSkipList->level) { // set new px if (forward[i] != pSkipList->pHead) { @@ -357,7 +357,7 @@ void tSkipListPrint(SSkipList *pSkipList, int16_t nlevel) { SSkipListNode *p = SL_NODE_GET_FORWARD_POINTER(pSkipList->pHead, nlevel - 1); int32_t id = 1; - char * prev = NULL; + char *prev = NULL; while (p != pSkipList->pTail) { char *key = SL_GET_NODE_KEY(pSkipList, p); @@ -433,21 +433,21 @@ static SSkipListIterator *doCreateSkipListIterator(SSkipList *pSkipList, int32_t return iter; } -static FORCE_INLINE int tSkipListWLock(SSkipList *pSkipList) { +static FORCE_INLINE int32_t tSkipListWLock(SSkipList *pSkipList) { if (pSkipList->lock) { return pthread_rwlock_wrlock(pSkipList->lock); } return 0; } -static FORCE_INLINE int tSkipListRLock(SSkipList *pSkipList) { +static FORCE_INLINE int32_t tSkipListRLock(SSkipList *pSkipList) { if (pSkipList->lock) { return pthread_rwlock_rdlock(pSkipList->lock); } return 0; } -static FORCE_INLINE int tSkipListUnlock(SSkipList *pSkipList) { +static FORCE_INLINE int32_t tSkipListUnlock(SSkipList *pSkipList) { if (pSkipList->lock) { return pthread_rwlock_unlock(pSkipList->lock); } @@ -455,12 +455,12 @@ static FORCE_INLINE int tSkipListUnlock(SSkipList *pSkipList) { } static bool tSkipListGetPosToPut(SSkipList *pSkipList, SSkipListNode **backward, void *pData) { - int compare = 0; + int32_t compare = 0; bool hasDupKey = false; - char * pDataKey = pSkipList->keyFn(pData); + char *pDataKey = pSkipList->keyFn(pData); if (pSkipList->size == 0) { - for (int i = 0; i < pSkipList->maxLevel; i++) { + for (int32_t i = 0; i < pSkipList->maxLevel; i++) { backward[i] = pSkipList->pTail; } } else { @@ -470,7 +470,7 @@ static bool tSkipListGetPosToPut(SSkipList *pSkipList, SSkipListNode **backward, pKey = SL_GET_MAX_KEY(pSkipList); compare = pSkipList->comparFn(pDataKey, pKey); if (compare >= 0) { - for (int i = 0; i < pSkipList->maxLevel; i++) { + for (int32_t i = 0; i < pSkipList->maxLevel; i++) { backward[i] = pSkipList->pTail; } @@ -481,7 +481,7 @@ static bool tSkipListGetPosToPut(SSkipList *pSkipList, SSkipListNode **backward, pKey = SL_GET_MIN_KEY(pSkipList); compare = pSkipList->comparFn(pDataKey, pKey); if (compare < 0) { - for (int i = 0; i < pSkipList->maxLevel; i++) { + for (int32_t i = 0; i < pSkipList->maxLevel; i++) { backward[i] = SL_NODE_GET_FORWARD_POINTER(pSkipList->pHead, i); } @@ -489,7 +489,7 @@ static bool tSkipListGetPosToPut(SSkipList *pSkipList, SSkipListNode **backward, } SSkipListNode *px = pSkipList->pTail; - for (int i = pSkipList->maxLevel - 1; i >= 0; --i) { + for (int32_t i = pSkipList->maxLevel - 1; i >= 0; --i) { if (i < pSkipList->level) { SSkipListNode *p = SL_NODE_GET_BACKWARD_POINTER(px, i); while (p != pSkipList->pHead) { @@ -532,7 +532,8 @@ static void tSkipListRemoveNodeImpl(SSkipList *pSkipList, SSkipListNode *pNode) // Function must be called after calling tSkipListRemoveNodeImpl() function static void tSkipListCorrectLevel(SSkipList *pSkipList) { - while (pSkipList->level > 0 && SL_NODE_GET_FORWARD_POINTER(pSkipList->pHead, pSkipList->level - 1) == pSkipList->pTail) { + while (pSkipList->level > 0 && + SL_NODE_GET_FORWARD_POINTER(pSkipList->pHead, pSkipList->level - 1) == pSkipList->pTail) { pSkipList->level -= 1; } } @@ -636,7 +637,7 @@ static SSkipListNode *getPriorNode(SSkipList *pSkipList, const char *val, int32_ return pNode; } -static int initForwardBackwardPtr(SSkipList *pSkipList) { +static int32_t initForwardBackwardPtr(SSkipList *pSkipList) { uint32_t maxLevel = pSkipList->maxLevel; // head info @@ -685,12 +686,12 @@ static SSkipListNode *tSkipListPutImpl(SSkipList *pSkipList, void *pData, SSkipL pSkipList->insertHandleFn->args[1] = pNode->pData; pData = genericInvoke(pSkipList->insertHandleFn); } - if(pData) { + if (pData) { atomic_store_ptr(&(pNode->pData), pData); } } else { - //for compatiblity, duplicate key inserted when update=0 should be also calculated as affected rows! - if(pSkipList->insertHandleFn) { + // for compatiblity, duplicate key inserted when update=0 should be also calculated as affected rows! + if (pSkipList->insertHandleFn) { pSkipList->insertHandleFn->args[0] = NULL; pSkipList->insertHandleFn->args[1] = NULL; genericInvoke(pSkipList->insertHandleFn); diff --git a/source/util/src/tstep.c b/source/util/src/tstep.c deleted file mode 100644 index 30c8ec2b3a..0000000000 --- a/source/util/src/tstep.c +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "tlog.h" -#include "taoserror.h" -#include "tstep.h" - -typedef struct { - char * name; - InitFp initFp; - CleanupFp cleanupFp; -} SStep; - -typedef struct SSteps { - int32_t cursize; - int32_t maxsize; - SStep * steps; - ReportFp reportFp; -} SSteps; - -SSteps *taosStepInit(int32_t maxsize, ReportFp fp) { - SSteps *steps = calloc(1, sizeof(SSteps)); - if (steps == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; - } - - steps->maxsize = maxsize; - steps->cursize = 0; - steps->steps = calloc(maxsize, sizeof(SStep)); - steps->reportFp = fp; - - return steps; -} - -int32_t taosStepAdd(struct SSteps *steps, char *name, InitFp initFp, CleanupFp cleanupFp) { - if (steps == NULL) { - terrno = TSDB_CODE_INVALID_PTR; - return -1; - } - - if (steps->cursize >= steps->maxsize) { - uError("failed to add step since up to the maxsize"); - terrno = TSDB_CODE_OUT_OF_RANGE; - return -1; - } - - SStep step = {.name = name, .initFp = initFp, .cleanupFp = cleanupFp}; - steps->steps[steps->cursize++] = step; - return 0; -} - -static void taosStepCleanupImp(SSteps *steps, int32_t pos) { - for (int32_t s = pos; s >= 0; s--) { - SStep *step = steps->steps + s; - uDebug("step:%s will cleanup", step->name); - if (step->cleanupFp != NULL) { - (*step->cleanupFp)(); - } - } -} - -int32_t taosStepExec(SSteps *steps) { - if (steps == NULL) { - terrno = TSDB_CODE_INVALID_PTR; - return -1; - } - - for (int32_t s = 0; s < steps->cursize; s++) { - SStep *step = steps->steps + s; - if (step->initFp == NULL) continue; - - if (steps->reportFp != NULL) { - (*steps->reportFp)(step->name, "start initialize"); - } - - int32_t code = (*step->initFp)(); - if (code != 0) { - uDebug("step:%s will cleanup", step->name); - taosStepCleanupImp(steps, s); - return code; - } - - uInfo("step:%s is initialized", step->name); - - if (steps->reportFp != NULL) { - (*steps->reportFp)(step->name, "initialize completed"); - } - } - - return 0; -} - -void taosStepCleanup(SSteps *steps) { - if (steps == NULL) return; - taosStepCleanupImp(steps, steps->cursize - 1); -} \ No newline at end of file diff --git a/source/util/src/tstrbuild.c b/source/util/src/tstrbuild.c index 230bff42f5..f191f69986 100644 --- a/source/util/src/tstrbuild.c +++ b/source/util/src/tstrbuild.c @@ -12,7 +12,8 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#include "os.h" + +#define _DEFAULT_SOURCE #include "tstrbuild.h" void taosStringBuilderEnsureCapacity(SStringBuilder* sb, size_t size) { diff --git a/source/util/src/tthread.c b/source/util/src/tthread.c index 8c7a3ada05..f9e28d7b62 100644 --- a/source/util/src/tthread.c +++ b/source/util/src/tthread.c @@ -13,13 +13,9 @@ * along with this program. If not, see . */ +#define _DEFAULT_SOURCE #include "tthread.h" -#include "taoserror.h" -#include "tdef.h" -#include "tutil.h" -#include "tlog.h" -// create new thread pthread_t* taosCreateThread(void* (*__start_routine)(void*), void* param) { pthread_t* pthread = (pthread_t*)malloc(sizeof(pthread_t)); pthread_attr_t thattr; @@ -35,7 +31,6 @@ pthread_t* taosCreateThread(void* (*__start_routine)(void*), void* param) { return pthread; } -// destory thread bool taosDestoryThread(pthread_t* pthread) { if (pthread == NULL) return false; if (taosThreadRunning(pthread)) { @@ -47,10 +42,9 @@ bool taosDestoryThread(pthread_t* pthread) { return true; } -// thread running return true bool taosThreadRunning(pthread_t* pthread) { if (pthread == NULL) return false; - int ret = pthread_kill(*pthread, 0); + int32_t ret = pthread_kill(*pthread, 0); if (ret == ESRCH) return false; if (ret == EINVAL) return false; // alive diff --git a/source/util/src/ttimer.c b/source/util/src/ttimer.c index 2c04603269..abb42ef28d 100644 --- a/source/util/src/ttimer.c +++ b/source/util/src/ttimer.c @@ -13,12 +13,11 @@ * along with this program. If not, see . */ +#define _DEFAULT_SOURCE #include "ttimer.h" -#include "os.h" #include "taoserror.h" #include "tlog.h" #include "tsched.h" -#include "tutil.h" #define tmrFatal(...) \ { \ @@ -57,9 +56,9 @@ } \ } -#define TIMER_STATE_WAITING 0 -#define TIMER_STATE_EXPIRED 1 -#define TIMER_STATE_STOPPED 2 +#define TIMER_STATE_WAITING 0 +#define TIMER_STATE_EXPIRED 1 +#define TIMER_STATE_STOPPED 2 #define TIMER_STATE_CANCELED 3 typedef union _tmr_ctrl_t { @@ -118,9 +117,9 @@ static pthread_mutex_t tmrCtrlMutex; static tmr_ctrl_t* tmrCtrls; static tmr_ctrl_t* unusedTmrCtrl = NULL; static void* tmrQhandle; -static int numOfTmrCtrl = 0; +static int32_t numOfTmrCtrl = 0; -int taosTmrThreads = 1; +int32_t taosTmrThreads = 1; static uintptr_t nextTimerId = 0; static time_wheel_t wheels[] = { @@ -148,7 +147,7 @@ static void timerDecRef(tmr_obj_t* timer) { static void lockTimerList(timer_list_t* list) { int64_t tid = taosGetSelfPthreadId(); - int i = 0; + int32_t i = 0; while (atomic_val_compare_exchange_64(&(list->lockedBy), 0, tid) != 0) { if (++i % 1000 == 0) { sched_yield(); @@ -322,7 +321,7 @@ static void addToExpired(tmr_obj_t* head) { } } -static uintptr_t doStartTimer(tmr_obj_t* timer, TAOS_TMR_CALLBACK fp, int mseconds, void* param, tmr_ctrl_t* ctrl) { +static uintptr_t doStartTimer(tmr_obj_t* timer, TAOS_TMR_CALLBACK fp, int32_t mseconds, void* param, tmr_ctrl_t* ctrl) { uintptr_t id = getNextTimerId(); timer->id = id; timer->state = TIMER_STATE_WAITING; @@ -346,7 +345,7 @@ static uintptr_t doStartTimer(tmr_obj_t* timer, TAOS_TMR_CALLBACK fp, int msecon return id; } -tmr_h taosTmrStart(TAOS_TMR_CALLBACK fp, int mseconds, void* param, void* handle) { +tmr_h taosTmrStart(TAOS_TMR_CALLBACK fp, int32_t mseconds, void* param, void* handle) { tmr_ctrl_t* ctrl = (tmr_ctrl_t*)handle; if (ctrl == NULL || ctrl->label[0] == 0) { return NULL; @@ -361,10 +360,10 @@ tmr_h taosTmrStart(TAOS_TMR_CALLBACK fp, int mseconds, void* param, void* handle return (tmr_h)doStartTimer(timer, fp, mseconds, param, ctrl); } -static void taosTimerLoopFunc(int signo) { +static void taosTimerLoopFunc(int32_t signo) { int64_t now = taosGetMonotonicMs(); - for (int i = 0; i < tListLen(wheels); i++) { + for (int32_t i = 0; i < tListLen(wheels); i++) { // `expried` is a temporary expire list. // expired timers are first add to this list, then move // to expired queue as a batch to improve performance. @@ -471,7 +470,7 @@ bool taosTmrStopA(tmr_h* timerId) { return ret; } -bool taosTmrReset(TAOS_TMR_CALLBACK fp, int mseconds, void* param, void* handle, tmr_h* pTmrId) { +bool taosTmrReset(TAOS_TMR_CALLBACK fp, int32_t mseconds, void* param, void* handle, tmr_h* pTmrId) { tmr_ctrl_t* ctrl = (tmr_ctrl_t*)handle; if (ctrl == NULL || ctrl->label[0] == 0) { return false; @@ -500,7 +499,7 @@ bool taosTmrReset(TAOS_TMR_CALLBACK fp, int mseconds, void* param, void* handle, // wait until there's no other reference to this timer, // so that we can reuse this timer safely. - for (int i = 1; atomic_load_8(&timer->refCount) > 1; ++i) { + for (int32_t i = 1; atomic_load_8(&timer->refCount) > 1; ++i) { if (i % 1000 == 0) { sched_yield(); } @@ -532,7 +531,7 @@ static void taosTmrModuleInit(void) { pthread_mutex_init(&tmrCtrlMutex, NULL); int64_t now = taosGetMonotonicMs(); - for (int i = 0; i < tListLen(wheels); i++) { + for (int32_t i = 0; i < tListLen(wheels); i++) { time_wheel_t* wheel = wheels + i; if (pthread_mutex_init(&wheel->mutex, NULL) != 0) { tmrError("failed to create the mutex for wheel, reason:%s", strerror(errno)); @@ -561,7 +560,7 @@ static void taosTmrModuleInit(void) { tmrDebug("timer module is initialized, number of threads: %d", taosTmrThreads); } -void* taosTmrInit(int maxNumOfTmrs, int resolution, int longest, const char* label) { +void* taosTmrInit(int32_t maxNumOfTmrs, int32_t resolution, int32_t longest, const char* label) { const char* ret = taosMonotonicInit(); tmrDebug("ttimer monotonic clock source:%s", ret); @@ -607,7 +606,7 @@ void taosTmrCleanUp(void* handle) { taosCleanUpScheduler(tmrQhandle); - for (int i = 0; i < tListLen(wheels); i++) { + for (int32_t i = 0; i < tListLen(wheels); i++) { time_wheel_t* wheel = wheels + i; pthread_mutex_destroy(&wheel->mutex); free(wheel->slots); diff --git a/source/util/src/tutil.c b/source/util/src/tutil.c index 58a2e57f7c..7bd671a56c 100644 --- a/source/util/src/tutil.c +++ b/source/util/src/tutil.c @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#include "os.h" -#include "tdef.h" +#define _DEFAULT_SOURCE +#include "tutil.h" int32_t strdequote(char *z) { if (z == NULL) { @@ -47,38 +47,38 @@ int32_t strdequote(char *z) { return j + 1; // only one quote, do nothing } -int32_t strRmquote(char *z, int32_t len){ - // delete escape character: \\, \', \" - char delim = z[0]; - if (delim != '\'' && delim != '\"') { - return len; - } - - int32_t cnt = 0; - int32_t j = 0; - for (uint32_t k = 1; k < len - 1; ++k) { - if (z[k] == '\\' || (z[k] == delim && z[k + 1] == delim)) { - if (z[k] == '\\' && z[k + 1] == '_') { - //match '_' self - } else { - z[j] = z[k + 1]; - cnt++; - j++; - k++; - continue; - } +int32_t strRmquote(char *z, int32_t len) { + // delete escape character: \\, \', \" + char delim = z[0]; + if (delim != '\'' && delim != '\"') { + return len; + } + + int32_t cnt = 0; + int32_t j = 0; + for (uint32_t k = 1; k < len - 1; ++k) { + if (z[k] == '\\' || (z[k] == delim && z[k + 1] == delim)) { + if (z[k] == '\\' && z[k + 1] == '_') { + // match '_' self + } else { + z[j] = z[k + 1]; + cnt++; + j++; + k++; + continue; } - - z[j] = z[k]; - j++; } - - z[j] = 0; - - return len - 2 - cnt; + + z[j] = z[k]; + j++; + } + + z[j] = 0; + + return len - 2 - cnt; } -int32_t strndequote(char *dst, const char* z, int32_t len) { +int32_t strndequote(char *dst, const char *z, int32_t len) { assert(dst != NULL); if (z == NULL || len == 0) { return 0; @@ -90,7 +90,7 @@ int32_t strndequote(char *dst, const char* z, int32_t len) { while (z[i] != 0) { if (z[i] == quote) { if (z[i + 1] == quote) { - dst[j++] = (char) quote; + dst[j++] = (char)quote; i++; } else { dst[j++] = 0; @@ -139,7 +139,7 @@ size_t strtrim(char *z) { } else if (j != i) { z[i] = 0; } - + return i; } @@ -168,11 +168,11 @@ char **strsplit(char *z, const char *delim, int32_t *num) { char *strnchr(const char *haystack, char needle, int32_t len, bool skipquote) { for (int32_t i = 0; i < len; ++i) { - // skip the needle in quote, jump to the end of quoted string if (skipquote && (haystack[i] == '\'' || haystack[i] == '"')) { char quote = haystack[i++]; - while(i < len && haystack[i++] != quote); + while (i < len && haystack[i++] != quote) + ; if (i >= len) { return NULL; } @@ -186,9 +186,9 @@ char *strnchr(const char *haystack, char needle, int32_t len, bool skipquote) { return NULL; } -char* strtolower(char *dst, const char *src) { +char *strtolower(char *dst, const char *src) { int32_t esc = 0; - char quote = 0, *p = dst, c; + char quote = 0, *p = dst, c; assert(dst != NULL); @@ -213,9 +213,9 @@ char* strtolower(char *dst, const char *src) { return dst; } -char* strntolower(char *dst, const char *src, int32_t n) { +char *strntolower(char *dst, const char *src, int32_t n) { int32_t esc = 0; - char quote = 0, *p = dst, c; + char quote = 0, *p = dst, c; assert(dst != NULL); if (n == 0) { @@ -243,7 +243,7 @@ char* strntolower(char *dst, const char *src, int32_t n) { return dst; } -char* strntolower_s(char *dst, const char *src, int32_t n) { +char *strntolower_s(char *dst, const char *src, int32_t n) { char *p = dst, c; assert(dst != NULL); @@ -265,7 +265,7 @@ char* strntolower_s(char *dst, const char *src, int32_t n) { char *paGetToken(char *string, char **token, int32_t *tokenLen) { char quote = 0; - + while (*string != 0) { if (*string == ' ' || *string == '\t') { ++string; @@ -346,8 +346,8 @@ char *strbetween(char *string, char *begin, char *end) { char *result = NULL; char *_begin = strstr(string, begin); if (_begin != NULL) { - char *_end = strstr(_begin + strlen(begin), end); - int32_t size = (int32_t)(_end - _begin); + char *_end = strstr(_begin + strlen(begin), end); + int32_t size = (int32_t)(_end - _begin); if (_end != NULL && size > 0) { result = (char *)calloc(1, size); memcpy(result, _begin + strlen(begin), size - +strlen(begin)); @@ -401,11 +401,12 @@ int32_t taosHexStrToByteArray(char hexstr[], char bytes[]) { } char *taosIpStr(uint32_t ipInt) { - static char ipStrArray[3][30]; + static char ipStrArray[3][30]; static int32_t ipStrIndex = 0; char *ipStr = ipStrArray[(ipStrIndex++) % 3]; - //sprintf(ipStr, "0x%x:%u.%u.%u.%u", ipInt, ipInt & 0xFF, (ipInt >> 8) & 0xFF, (ipInt >> 16) & 0xFF, (uint8_t)(ipInt >> 24)); + // sprintf(ipStr, "0x%x:%u.%u.%u.%u", ipInt, ipInt & 0xFF, (ipInt >> 8) & 0xFF, (ipInt >> 16) & 0xFF, (uint8_t)(ipInt + // >> 24)); sprintf(ipStr, "%u.%u.%u.%u", ipInt & 0xFF, (ipInt >> 8) & 0xFF, (ipInt >> 16) & 0xFF, (uint8_t)(ipInt >> 24)); return ipStr; } diff --git a/source/util/src/tversion.c b/source/util/src/tversion.c index 8409637e80..c70fdc87a6 100644 --- a/source/util/src/tversion.c +++ b/source/util/src/tversion.c @@ -14,9 +14,8 @@ */ #define _DEFAULT_SOURCE -#include "os.h" +#include "tversion.h" #include "taoserror.h" -#include "tdef.h" int32_t taosVersionStrToInt(const char *vstr, int32_t *vint) { if (vstr == NULL) { diff --git a/source/util/src/tworker.c b/source/util/src/tworker.c index 2843f4e801..1657a85ee8 100644 --- a/source/util/src/tworker.c +++ b/source/util/src/tworker.c @@ -71,8 +71,8 @@ static void *tQWorkerThreadFp(SQWorker *worker) { SQWorkerPool *pool = worker->pool; FItem fp = NULL; - void * msg = NULL; - void * ahandle = NULL; + void *msg = NULL; + void *ahandle = NULL; int32_t code = 0; taosBlockSIGPIPE(); @@ -151,8 +151,8 @@ static void *tFWorkerThreadFp(SQWorker *worker) { SQWorkerPool *pool = worker->pool; FItem fp = NULL; - void * msg = NULL; - void * ahandle = NULL; + void *msg = NULL; + void *ahandle = NULL; int32_t code = 0; taosBlockSIGPIPE(); @@ -240,8 +240,8 @@ static void *tWWorkerThreadFp(SWWorker *worker) { SWWorkerPool *pool = worker->pool; FItems fp = NULL; - void * msg = NULL; - void * ahandle = NULL; + void *msg = NULL; + void *ahandle = NULL; int32_t numOfMsgs = 0; int32_t qtype = 0; diff --git a/source/util/test/cfgTest.cpp b/source/util/test/cfgTest.cpp index 800e261dcb..712fb2d09a 100644 --- a/source/util/test/cfgTest.cpp +++ b/source/util/test/cfgTest.cpp @@ -62,9 +62,9 @@ TEST_F(CfgTest, 02_Basic) { EXPECT_EQ(cfgGetSize(pConfig), 6); - int32_t size = 0; - SConfigItem *pItem = cfgIterate(pConfig, NULL); - while (pItem != NULL) { + int32_t size = taosArrayGetSize(pConfig->array); + for (int32_t i = 0; i < size; ++i) { + SConfigItem *pItem = (SConfigItem *)taosArrayGet(pConfig->array, i); switch (pItem->dtype) { case CFG_DTYPE_BOOL: printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->bval); @@ -88,14 +88,10 @@ TEST_F(CfgTest, 02_Basic) { printf("index:%d, cfg:%s invalid cfg dtype:%d\n", size, pItem->name, pItem->dtype); break; } - size++; - pItem = cfgIterate(pConfig, pItem); } - cfgCancelIterate(pConfig, pItem); - EXPECT_EQ(cfgGetSize(pConfig), 6); - pItem = cfgGetItem(pConfig, "test_bool"); + SConfigItem *pItem = cfgGetItem(pConfig, "test_bool"); EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT); EXPECT_EQ(pItem->dtype, CFG_DTYPE_BOOL); EXPECT_STREQ(pItem->name, "test_bool"); diff --git a/source/util/test/encodeTest.cpp b/source/util/test/encodeTest.cpp index 1b13c102a5..5505a6207f 100644 --- a/source/util/test/encodeTest.cpp +++ b/source/util/test/encodeTest.cpp @@ -2,7 +2,7 @@ #include "gtest/gtest.h" -#include "encode.h" +#include "tencode.h" #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wshift-count-overflow" @@ -12,41 +12,41 @@ #define BUF_SIZE 64 td_endian_t endian_arr[2] = {TD_LITTLE_ENDIAN, TD_BIG_ENDIAN}; -static int encode(SCoder *pCoder, int8_t val) { return tEncodeI8(pCoder, val); } -static int encode(SCoder *pCoder, uint8_t val) { return tEncodeU8(pCoder, val); } -static int encode(SCoder *pCoder, int16_t val) { return tEncodeI16(pCoder, val); } -static int encode(SCoder *pCoder, uint16_t val) { return tEncodeU16(pCoder, val); } -static int encode(SCoder *pCoder, int32_t val) { return tEncodeI32(pCoder, val); } -static int encode(SCoder *pCoder, uint32_t val) { return tEncodeU32(pCoder, val); } -static int encode(SCoder *pCoder, int64_t val) { return tEncodeI64(pCoder, val); } -static int encode(SCoder *pCoder, uint64_t val) { return tEncodeU64(pCoder, val); } +static int32_t encode(SCoder *pCoder, int8_t val) { return tEncodeI8(pCoder, val); } +static int32_t encode(SCoder *pCoder, uint8_t val) { return tEncodeU8(pCoder, val); } +static int32_t encode(SCoder *pCoder, int16_t val) { return tEncodeI16(pCoder, val); } +static int32_t encode(SCoder *pCoder, uint16_t val) { return tEncodeU16(pCoder, val); } +static int32_t encode(SCoder *pCoder, int32_t val) { return tEncodeI32(pCoder, val); } +static int32_t encode(SCoder *pCoder, uint32_t val) { return tEncodeU32(pCoder, val); } +static int32_t encode(SCoder *pCoder, int64_t val) { return tEncodeI64(pCoder, val); } +static int32_t encode(SCoder *pCoder, uint64_t val) { return tEncodeU64(pCoder, val); } -static int decode(SCoder *pCoder, int8_t *val) { return tDecodeI8(pCoder, val); } -static int decode(SCoder *pCoder, uint8_t *val) { return tDecodeU8(pCoder, val); } -static int decode(SCoder *pCoder, int16_t *val) { return tDecodeI16(pCoder, val); } -static int decode(SCoder *pCoder, uint16_t *val) { return tDecodeU16(pCoder, val); } -static int decode(SCoder *pCoder, int32_t *val) { return tDecodeI32(pCoder, val); } -static int decode(SCoder *pCoder, uint32_t *val) { return tDecodeU32(pCoder, val); } -static int decode(SCoder *pCoder, int64_t *val) { return tDecodeI64(pCoder, val); } -static int decode(SCoder *pCoder, uint64_t *val) { return tDecodeU64(pCoder, val); } +static int32_t decode(SCoder *pCoder, int8_t *val) { return tDecodeI8(pCoder, val); } +static int32_t decode(SCoder *pCoder, uint8_t *val) { return tDecodeU8(pCoder, val); } +static int32_t decode(SCoder *pCoder, int16_t *val) { return tDecodeI16(pCoder, val); } +static int32_t decode(SCoder *pCoder, uint16_t *val) { return tDecodeU16(pCoder, val); } +static int32_t decode(SCoder *pCoder, int32_t *val) { return tDecodeI32(pCoder, val); } +static int32_t decode(SCoder *pCoder, uint32_t *val) { return tDecodeU32(pCoder, val); } +static int32_t decode(SCoder *pCoder, int64_t *val) { return tDecodeI64(pCoder, val); } +static int32_t decode(SCoder *pCoder, uint64_t *val) { return tDecodeU64(pCoder, val); } -static int encodev(SCoder *pCoder, int8_t val) { return tEncodeI8(pCoder, val); } -static int encodev(SCoder *pCoder, uint8_t val) { return tEncodeU8(pCoder, val); } -static int encodev(SCoder *pCoder, int16_t val) { return tEncodeI16v(pCoder, val); } -static int encodev(SCoder *pCoder, uint16_t val) { return tEncodeU16v(pCoder, val); } -static int encodev(SCoder *pCoder, int32_t val) { return tEncodeI32v(pCoder, val); } -static int encodev(SCoder *pCoder, uint32_t val) { return tEncodeU32v(pCoder, val); } -static int encodev(SCoder *pCoder, int64_t val) { return tEncodeI64v(pCoder, val); } -static int encodev(SCoder *pCoder, uint64_t val) { return tEncodeU64v(pCoder, val); } +static int32_t encodev(SCoder *pCoder, int8_t val) { return tEncodeI8(pCoder, val); } +static int32_t encodev(SCoder *pCoder, uint8_t val) { return tEncodeU8(pCoder, val); } +static int32_t encodev(SCoder *pCoder, int16_t val) { return tEncodeI16v(pCoder, val); } +static int32_t encodev(SCoder *pCoder, uint16_t val) { return tEncodeU16v(pCoder, val); } +static int32_t encodev(SCoder *pCoder, int32_t val) { return tEncodeI32v(pCoder, val); } +static int32_t encodev(SCoder *pCoder, uint32_t val) { return tEncodeU32v(pCoder, val); } +static int32_t encodev(SCoder *pCoder, int64_t val) { return tEncodeI64v(pCoder, val); } +static int32_t encodev(SCoder *pCoder, uint64_t val) { return tEncodeU64v(pCoder, val); } -static int decodev(SCoder *pCoder, int8_t *val) { return tDecodeI8(pCoder, val); } -static int decodev(SCoder *pCoder, uint8_t *val) { return tDecodeU8(pCoder, val); } -static int decodev(SCoder *pCoder, int16_t *val) { return tDecodeI16v(pCoder, val); } -static int decodev(SCoder *pCoder, uint16_t *val) { return tDecodeU16v(pCoder, val); } -static int decodev(SCoder *pCoder, int32_t *val) { return tDecodeI32v(pCoder, val); } -static int decodev(SCoder *pCoder, uint32_t *val) { return tDecodeU32v(pCoder, val); } -static int decodev(SCoder *pCoder, int64_t *val) { return tDecodeI64v(pCoder, val); } -static int decodev(SCoder *pCoder, uint64_t *val) { return tDecodeU64v(pCoder, val); } +static int32_t decodev(SCoder *pCoder, int8_t *val) { return tDecodeI8(pCoder, val); } +static int32_t decodev(SCoder *pCoder, uint8_t *val) { return tDecodeU8(pCoder, val); } +static int32_t decodev(SCoder *pCoder, int16_t *val) { return tDecodeI16v(pCoder, val); } +static int32_t decodev(SCoder *pCoder, uint16_t *val) { return tDecodeU16v(pCoder, val); } +static int32_t decodev(SCoder *pCoder, int32_t *val) { return tDecodeI32v(pCoder, val); } +static int32_t decodev(SCoder *pCoder, uint32_t *val) { return tDecodeU32v(pCoder, val); } +static int32_t decodev(SCoder *pCoder, int64_t *val) { return tDecodeI64v(pCoder, val); } +static int32_t decodev(SCoder *pCoder, uint64_t *val) { return tDecodeU64v(pCoder, val); } template static void simple_encode_decode_func(bool var_len) { @@ -211,7 +211,7 @@ typedef struct { char * A_c; } SStructA_v1; -static int tSStructA_v1_encode(SCoder *pCoder, const SStructA_v1 *pSAV1) { +static int32_t tSStructA_v1_encode(SCoder *pCoder, const SStructA_v1 *pSAV1) { if (tStartEncode(pCoder) < 0) return -1; if (tEncodeI32(pCoder, pSAV1->A_a) < 0) return -1; @@ -222,7 +222,7 @@ static int tSStructA_v1_encode(SCoder *pCoder, const SStructA_v1 *pSAV1) { return 0; } -static int tSStructA_v1_decode(SCoder *pCoder, SStructA_v1 *pSAV1) { +static int32_t tSStructA_v1_decode(SCoder *pCoder, SStructA_v1 *pSAV1) { if (tStartDecode(pCoder) < 0) return -1; if (tDecodeI32(pCoder, &pSAV1->A_a) < 0) return -1; @@ -246,7 +246,7 @@ typedef struct { int16_t A_e; } SStructA_v2; -static int tSStructA_v2_encode(SCoder *pCoder, const SStructA_v2 *pSAV2) { +static int32_t tSStructA_v2_encode(SCoder *pCoder, const SStructA_v2 *pSAV2) { if (tStartEncode(pCoder) < 0) return -1; if (tEncodeI32(pCoder, pSAV2->A_a) < 0) return -1; @@ -261,7 +261,7 @@ static int tSStructA_v2_encode(SCoder *pCoder, const SStructA_v2 *pSAV2) { return 0; } -static int tSStructA_v2_decode(SCoder *pCoder, SStructA_v2 *pSAV2) { +static int32_t tSStructA_v2_decode(SCoder *pCoder, SStructA_v2 *pSAV2) { if (tStartDecode(pCoder) < 0) return -1; if (tDecodeI32(pCoder, &pSAV2->A_a) < 0) return -1; @@ -291,7 +291,7 @@ typedef struct { int8_t v_b; } SFinalReq_v1; -static int tSFinalReq_v1_encode(SCoder *pCoder, const SFinalReq_v1 *ps1) { +static int32_t tSFinalReq_v1_encode(SCoder *pCoder, const SFinalReq_v1 *ps1) { if (tStartEncode(pCoder) < 0) return -1; if (tSStructA_v1_encode(pCoder, ps1->pA) < 0) return -1; @@ -302,7 +302,7 @@ static int tSFinalReq_v1_encode(SCoder *pCoder, const SFinalReq_v1 *ps1) { return 0; } -static int tSFinalReq_v1_decode(SCoder *pCoder, SFinalReq_v1 *ps1) { +static int32_t tSFinalReq_v1_decode(SCoder *pCoder, SFinalReq_v1 *ps1) { if (tStartDecode(pCoder) < 0) return -1; ps1->pA = (SStructA_v1 *)TCODER_MALLOC(sizeof(*(ps1->pA)), pCoder); @@ -322,7 +322,7 @@ typedef struct { int16_t v_c; } SFinalReq_v2; -static int tSFinalReq_v2_encode(SCoder *pCoder, const SFinalReq_v2 *ps2) { +static int32_t tSFinalReq_v2_encode(SCoder *pCoder, const SFinalReq_v2 *ps2) { if (tStartEncode(pCoder) < 0) return -1; if (tSStructA_v2_encode(pCoder, ps2->pA) < 0) return -1; @@ -336,7 +336,7 @@ static int tSFinalReq_v2_encode(SCoder *pCoder, const SFinalReq_v2 *ps2) { return 0; } -static int tSFinalReq_v2_decode(SCoder *pCoder, SFinalReq_v2 *ps2) { +static int32_t tSFinalReq_v2_decode(SCoder *pCoder, SFinalReq_v2 *ps2) { if (tStartDecode(pCoder) < 0) return -1; ps2->pA = (SStructA_v2 *)TCODER_MALLOC(sizeof(*(ps2->pA)), pCoder); diff --git a/source/util/test/freelistTest.cpp b/source/util/test/freelistTest.cpp index 7a4e8be5b7..cf11d6b5bf 100644 --- a/source/util/test/freelistTest.cpp +++ b/source/util/test/freelistTest.cpp @@ -1,6 +1,6 @@ #include "gtest/gtest.h" -#include "freelist.h" +#include "tfreelist.h" TEST(TD_UTIL_FREELIST_TEST, simple_test) { SFreeList fl; diff --git a/tools/shell/src/backup/shellDarwin.c b/tools/shell/src/backup/shellDarwin.c index a1413be1ce..f2ab468574 100644 --- a/tools/shell/src/backup/shellDarwin.c +++ b/tools/shell/src/backup/shellDarwin.c @@ -19,7 +19,7 @@ #include "shell.h" #include "shellCommand.h" -#include "tkey.h" +#include "tbase64.h" #include "tscLog.h" diff --git a/tools/shell/src/shellLinux.c b/tools/shell/src/shellLinux.c index f304f35e3e..57d6df0051 100644 --- a/tools/shell/src/shellLinux.c +++ b/tools/shell/src/shellLinux.c @@ -18,7 +18,7 @@ #include "tglobal.h" #include "shell.h" #include "shellCommand.h" -#include "tkey.h" +#include "tbase64.h" #include "tlog.h" #include "version.h"