enh: improve performance of buildChildTableName (#30159)

This commit is contained in:
Bomin Zhang 2025-03-17 10:39:47 +08:00 committed by GitHub
parent ab92886820
commit 3606a1af3a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 31 deletions

View File

@ -16,7 +16,6 @@
#define _DEFAULT_SOURCE
#include "tname.h"
#include "tcommon.h"
#include "tstrbuild.h"
#define VALID_NAME_TYPE(x) ((x) == TSDB_DB_NAME_T || (x) == TSDB_TABLE_NAME_T)
@ -234,43 +233,38 @@ static int compareKv(const void* p1, const void* p2) {
* use stable name and tags to grearate child table name
*/
int32_t buildChildTableName(RandTableName* rName) {
SStringBuilder sb = {0};
taosStringBuilderAppendStringLen(&sb, rName->stbFullName, rName->stbFullNameLen);
if (sb.buf == NULL) {
return TSDB_CODE_OUT_OF_MEMORY;
}
taosArraySort(rName->tags, compareKv);
T_MD5_CTX context;
tMD5Init(&context);
// add stable name
tMD5Update(&context, (uint8_t*)rName->stbFullName, rName->stbFullNameLen);
// add tags
for (int j = 0; j < taosArrayGetSize(rName->tags); ++j) {
taosStringBuilderAppendChar(&sb, ',');
SSmlKv* tagKv = taosArrayGet(rName->tags, j);
if (tagKv == NULL) {
return TSDB_CODE_SML_INVALID_DATA;
}
taosStringBuilderAppendStringLen(&sb, tagKv->key, tagKv->keyLen);
taosStringBuilderAppendChar(&sb, '=');
tMD5Update(&context, (uint8_t*)",", 1);
tMD5Update(&context, (uint8_t*)tagKv->key, tagKv->keyLen);
tMD5Update(&context, (uint8_t*)"=", 1);
if (IS_VAR_DATA_TYPE(tagKv->type)) {
taosStringBuilderAppendStringLen(&sb, tagKv->value, tagKv->length);
tMD5Update(&context, (uint8_t*)tagKv->value, tagKv->length);
} else {
taosStringBuilderAppendStringLen(&sb, (char*)(&(tagKv->value)), tagKv->length);
tMD5Update(&context, (uint8_t*)(&(tagKv->value)), tagKv->length);
}
}
size_t len = 0;
char* keyJoined = taosStringBuilderGetResult(&sb, &len);
T_MD5_CTX context;
tMD5Init(&context);
tMD5Update(&context, (uint8_t*)keyJoined, (uint32_t)len);
tMD5Final(&context);
char temp[8] = {0};
rName->ctbShortName[0] = 't';
rName->ctbShortName[1] = '_';
for (int i = 0; i < 16; i++) {
(void)sprintf(temp, "%02x", context.digest[i]);
(void)strcat(rName->ctbShortName, temp);
}
taosStringBuilderDestroy(&sb);
taosByteArrayToHexStr(context.digest, 16, rName->ctbShortName + 2);
rName->ctbShortName[34] = 0;
return TSDB_CODE_SUCCESS;
}

View File

@ -329,9 +329,9 @@ char *strbetween(char *string, char *begin, char *end) {
return result;
}
int32_t tintToHex(uint64_t val, char hex[]) {
const char hexstr[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
static const char hexstr[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
int32_t tintToHex(uint64_t val, char hex[]) {
int32_t j = 0, k = 0;
if (val == 0) {
hex[j++] = hexstr[0];
@ -355,13 +355,12 @@ int32_t titoa(uint64_t val, size_t radix, char str[]) {
return 0;
}
const char *s = "0123456789abcdef";
char buf[65] = {0};
int32_t i = 0;
uint64_t v = val;
do {
buf[i++] = s[v % radix];
buf[i++] = hexstr[v % radix];
v /= radix;
} while (v > 0);
@ -373,13 +372,12 @@ int32_t titoa(uint64_t val, size_t radix, char str[]) {
return i;
}
int32_t taosByteArrayToHexStr(char bytes[], int32_t len, char hexstr[]) {
int32_t taosByteArrayToHexStr(char bytes[], int32_t len, char str[]) {
int32_t i;
char hexval[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
for (i = 0; i < len; i++) {
hexstr[i * 2] = hexval[((bytes[i] >> 4u) & 0xF)];
hexstr[(i * 2) + 1] = hexval[(bytes[i]) & 0x0F];
str[i * 2] = hexstr[((bytes[i] >> 4u) & 0xF)];
str[(i * 2) + 1] = hexstr[(bytes[i]) & 0x0F];
}
return 0;