• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #4917

07 Jan 2026 03:52PM UTC coverage: 65.42% (+0.02%) from 65.402%
#4917

push

travis-ci

web-flow
merge: from main to 3.0 branch #34204

31 of 34 new or added lines in 2 files covered. (91.18%)

819 existing lines in 129 files now uncovered.

202679 of 309814 relevant lines covered (65.42%)

116724351.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.23
/source/common/src/tname.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#define _DEFAULT_SOURCE
17
#include "tname.h"
18
#include "tcommon.h"
19

20
#define VALID_NAME_TYPE(x) ((x) == TSDB_DB_NAME_T || (x) == TSDB_TABLE_NAME_T)
21

22
void toName(int32_t acctId, const char* pDbName, const char* pTableName, SName* pName) {
1,052,471,063✔
23
  if (pName == NULL){
1,052,471,063✔
24
    return;
×
25
  }
26
  pName->type = TSDB_TABLE_NAME_T;
1,052,471,063✔
27
  pName->acctId = acctId;
1,052,480,026✔
28
  snprintf(pName->dbname, sizeof(pName->dbname), "%s", pDbName);
1,052,473,888✔
29
  snprintf(pName->tname, sizeof(pName->tname), "%s", pTableName);
1,052,476,308✔
30
}
31

32
int32_t tNameExtractFullName(const SName* name, char* dst) {
2,147,483,647✔
33
  // invalid full name format, abort
34
  if (!tNameIsValid(name)) {
2,147,483,647✔
35
    return TSDB_CODE_INVALID_PARA;
8✔
36
  }
37

38
  int32_t len = tsnprintf(dst, TSDB_DB_FNAME_LEN, "%d.%s", name->acctId, name->dbname);
2,147,483,647✔
39

40
  size_t tnameLen = strlen(name->tname);
2,147,483,647✔
41
  if (tnameLen > 0) {
2,147,483,647✔
42
    dst[len] = TS_PATH_DELIMITER[0];
2,147,483,647✔
43

44
    memcpy(dst + len + 1, name->tname, tnameLen);
2,147,483,647✔
45
    dst[len + tnameLen + 1] = 0;
2,147,483,647✔
46
  }
47

48
  return 0;
2,147,483,647✔
49
}
50

51
int32_t tNameLen(const SName* name) {
×
52
  char    tmp[12] = {0};
×
53
  int32_t len = sprintf(tmp, "%d", name->acctId);
×
54
  int32_t len1 = (int32_t)strlen(name->dbname);
×
55
  int32_t len2 = (int32_t)strlen(name->tname);
×
56

57
  if (name->type == TSDB_DB_NAME_T) {
×
58
    return len + len1 + TSDB_NAME_DELIMITER_LEN;
×
59
  } else {
60
    return len + len1 + len2 + TSDB_NAME_DELIMITER_LEN * 2;
×
61
  }
62
}
63

64
bool tNameIsValid(const SName* name) {
2,147,483,647✔
65
  if (!VALID_NAME_TYPE(name->type)) {
2,147,483,647✔
66
    return false;
×
67
  }
68

69
  if (name->type == TSDB_DB_NAME_T) {
2,147,483,647✔
70
    return strlen(name->dbname) > 0;
2,178,181✔
71
  } else {
72
    return strlen(name->dbname) > 0 && strlen(name->tname) > 0;
2,147,483,647✔
73
  }
74
}
75

76
SName* tNameDup(const SName* name) {
×
77
  SName* p = taosMemoryMalloc(sizeof(SName));
×
78
  if (p) TAOS_MEMCPY(p, name, sizeof(SName));
×
79
  return p;
×
80
}
81

82
int32_t tNameGetDbName(const SName* name, char* dst) {
12,789,471✔
83
  tstrncpy(dst, name->dbname, tListLen(name->dbname));
12,789,471✔
84
  return 0;
12,788,512✔
85
}
86

87
const char* tNameGetDbNameP(const SName* name) { return &name->dbname[0]; }
12,286✔
88

89
int32_t tNameGetFullDbName(const SName* name, char* dst) {
2,147,483,647✔
90
  if (name == NULL || dst == NULL) {
2,147,483,647✔
91
    return TSDB_CODE_INVALID_PARA;
×
92
  }
93
  (void)snprintf(dst, TSDB_DB_FNAME_LEN, "%d.%s", name->acctId, name->dbname);
2,147,483,647✔
94
  return 0;
2,147,483,647✔
95
}
96

97
bool tNameIsEmpty(const SName* name) { return name->type == 0 || name->acctId == 0; }
×
98

99
const char* tNameGetTableName(const SName* name) {
155,128,496✔
100
  if (!(name != NULL && name->type == TSDB_TABLE_NAME_T)) {
155,128,496✔
101
    terrno = TSDB_CODE_INVALID_PARA;
90✔
102
    return NULL;
×
103
  }
104
  return &name->tname[0];
155,128,939✔
105
}
106

107
int32_t tNameGetFullTableName(const SName* name, char* dst) {
1,114,035✔
108
  if (name == NULL || dst == NULL) {
1,114,035✔
109
    return TSDB_CODE_INVALID_PARA;
×
110
  }
111
  (void)snprintf(dst, TSDB_TABLE_FNAME_LEN, "%s.%s", name->dbname, name->tname);
1,114,035✔
112
  return 0;
1,114,035✔
113
}
114

115
void tNameAssign(SName* dst, const SName* src) { memcpy(dst, src, sizeof(SName)); }
10,743,702✔
116

117
int32_t tNameSetDbName(SName* dst, int32_t acct, const char* dbName, size_t nameLen) {
658,843,795✔
118
  // too long account id or too long db name
119
  if (nameLen <= 0 || nameLen >= tListLen(dst->dbname)) {
658,843,795✔
UNCOV
120
    return TSDB_CODE_INVALID_PARA;
×
121
  }
122

123
  dst->type = TSDB_DB_NAME_T;
658,849,056✔
124
  dst->acctId = acct;
658,849,046✔
125
  tstrncpy(dst->dbname, dbName, nameLen + 1);
658,847,382✔
126
  return 0;
658,844,898✔
127
}
128

129
int32_t tNameAddTbName(SName* dst, const char* tbName, size_t nameLen) {
1,597,653✔
130
  // too long account id or too long db name
131
  if (nameLen >= tListLen(dst->tname) || nameLen <= 0) {
1,597,653✔
132
    return TSDB_CODE_INVALID_PARA;
×
133
  }
134

135
  dst->type = TSDB_TABLE_NAME_T;
1,597,653✔
136
  tstrncpy(dst->tname, tbName, nameLen + 1);
1,597,653✔
137
  return 0;
1,597,653✔
138
}
139

140
int32_t tNameSetAcctId(SName* dst, int32_t acctId) {
×
141
  dst->acctId = acctId;
×
142
  return 0;
×
143
}
144

145
bool tNameDBNameEqual(SName* left, SName* right) {
22,945✔
146
  if (NULL == left) {
22,945✔
147
    if (NULL == right) {
×
148
      return true;
×
149
    }
150

151
    return false;
×
152
  }
153

154
  if (NULL == right) {
22,945✔
155
    return false;
×
156
  }
157

158
  if (left->acctId != right->acctId) {
22,945✔
159
    return false;
×
160
  }
161

162
  return (0 == strcmp(left->dbname, right->dbname));
22,945✔
163
}
164

165
bool tNameTbNameEqual(SName* left, SName* right) {
22,945✔
166
  bool equal = tNameDBNameEqual(left, right);
22,945✔
167
  if (equal) {
22,945✔
168
    return (0 == strcmp(left->tname, right->tname));
18,281✔
169
  }
170

171
  return equal;
4,664✔
172
}
173

174
int32_t tNameFromString(SName* dst, const char* str, uint32_t type) {
2,147,483,647✔
175
  if (strlen(str) == 0) {
2,147,483,647✔
176
    return TSDB_CODE_INVALID_PARA;
×
177
  }
178

179
  char* p = NULL;
2,147,483,647✔
180
  if ((type & T_NAME_ACCT) == T_NAME_ACCT) {
2,147,483,647✔
181
    p = strstr(str, TS_PATH_DELIMITER);
1,977,848,782✔
182
    if (p == NULL) {
1,977,848,782✔
183
      return TSDB_CODE_INVALID_PARA;
×
184
    }
185

186
    dst->acctId = taosStr2Int32(str, NULL, 10);
1,977,848,782✔
187
  }
188

189
  if ((type & T_NAME_DB) == T_NAME_DB) {
2,147,483,647✔
190
    dst->type = TSDB_DB_NAME_T;
1,977,823,599✔
191
    char* start = (char*)((p == NULL) ? str : (p + 1));
1,977,820,414✔
192

193
    int32_t len = 0;
1,977,830,547✔
194
    p = strstr(start, TS_PATH_DELIMITER);
1,977,830,547✔
195
    if (p == NULL) {
1,977,830,547✔
196
      len = (int32_t)strlen(start);
486,800,183✔
197
    } else {
198
      len = (int32_t)(p - start);
1,491,030,364✔
199
    }
200

201
    // too long account id or too long db name
202
    if ((len >= tListLen(dst->dbname)) || (len <= 0)) {
1,977,830,547✔
203
      return TSDB_CODE_INVALID_PARA;
33✔
204
    }
205

206
    memcpy(dst->dbname, start, len);
1,977,848,654✔
207
    dst->dbname[len] = 0;
1,977,847,845✔
208
  }
209

210
  if ((type & T_NAME_TABLE) == T_NAME_TABLE) {
2,147,483,647✔
211
    dst->type = TSDB_TABLE_NAME_T;
2,104,108,188✔
212
    char* start = (char*)((p == NULL) ? str : (p + 1));
2,104,105,032✔
213

214
    // too long account id or too long db name
215
    int32_t len = (int32_t)strlen(start);
2,104,096,342✔
216
    if ((len >= tListLen(dst->tname)) || (len <= 0)) {
2,104,096,342✔
UNCOV
217
      return TSDB_CODE_INVALID_PARA;
×
218
    }
219

220
    memcpy(dst->tname, start, len);
2,104,110,138✔
221
    dst->tname[len] = 0;
2,104,112,343✔
222
  }
223

224
  return 0;
2,147,483,647✔
225
}
226

227
static int compareKv(const void* p1, const void* p2) {
13,125,522✔
228
  SSmlKv* kv1 = (SSmlKv*)p1;
13,125,522✔
229
  SSmlKv* kv2 = (SSmlKv*)p2;
13,125,522✔
230
  int32_t kvLen1 = kv1->keyLen;
13,125,522✔
231
  int32_t kvLen2 = kv2->keyLen;
13,125,522✔
232
  int32_t res = taosStrncasecmp(kv1->key, kv2->key, TMIN(kvLen1, kvLen2));
13,125,578✔
233
  if (res != 0) {
13,125,522✔
234
    return res;
12,770,516✔
235
  } else {
236
    return kvLen1 - kvLen2;
355,006✔
237
  }
238
}
239

240
/*
241
 * use stable name and tags to generate child table name
242
 */
243
int32_t buildChildTableName(RandTableName* rName) {
747,817✔
244
  taosArraySort(rName->tags, compareKv);
747,817✔
245

246
  T_MD5_CTX context;
735,962✔
247
  tMD5Init(&context);
747,830✔
248

249
  // add stable name
250
  tMD5Update(&context, (uint8_t*)rName->stbFullName, rName->stbFullNameLen);
747,804✔
251

252
  // add tags
253
  for (int j = 0; j < taosArrayGetSize(rName->tags); ++j) {
5,920,396✔
254
    SSmlKv* tagKv = taosArrayGet(rName->tags, j);
5,172,592✔
255
    if (tagKv == NULL) {
5,172,523✔
256
      return TSDB_CODE_SML_INVALID_DATA;
×
257
    }
258

259
    tMD5Update(&context, (uint8_t*)",", 1);
5,172,523✔
260
    tMD5Update(&context, (uint8_t*)tagKv->key, tagKv->keyLen);
5,173,211✔
261
    tMD5Update(&context, (uint8_t*)"=", 1);
5,173,224✔
262

263
    if (IS_VAR_DATA_TYPE(tagKv->type)) {
5,173,155✔
264
      tMD5Update(&context, (uint8_t*)tagKv->value, tagKv->length);
3,876,341✔
265
    } else {
266
      tMD5Update(&context, (uint8_t*)(&(tagKv->value)), tagKv->length);
1,296,758✔
267
    }
268
  }
269

270
  tMD5Final(&context);
747,791✔
271

272
  rName->ctbShortName[0] = 't';
747,779✔
273
  rName->ctbShortName[1] = '_';
747,779✔
274
  (void)taosByteArrayToHexStr((char*)context.digest, 16, rName->ctbShortName + 2);
747,723✔
275
  rName->ctbShortName[34] = 0;
747,748✔
276

277
  return TSDB_CODE_SUCCESS;
747,748✔
278
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc