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

taosdata / TDengine / #3903

24 Apr 2025 11:36AM UTC coverage: 55.307% (+0.09%) from 55.213%
#3903

push

travis-ci

happyguoxy
Sync branches at 2025-04-24 19:35

175024 of 316459 relevant lines covered (55.31%)

1151858.11 hits per line

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

73.97
/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) {
141,196✔
23
  if (pName == NULL){
141,196✔
24
    return;
×
25
  }
26
  pName->type = TSDB_TABLE_NAME_T;
141,196✔
27
  pName->acctId = acctId;
141,196✔
28
  snprintf(pName->dbname, sizeof(pName->dbname), "%s", pDbName);
141,196✔
29
  snprintf(pName->tname, sizeof(pName->tname), "%s", pTableName);
141,196✔
30
}
31

32
int32_t tNameExtractFullName(const SName* name, char* dst) {
501,625✔
33
  // invalid full name format, abort
34
  if (!tNameIsValid(name)) {
501,625✔
35
    return TSDB_CODE_INVALID_PARA;
10✔
36
  }
37

38
  int32_t len = tsnprintf(dst, TSDB_DB_FNAME_LEN, "%d.%s", name->acctId, name->dbname);
501,671✔
39

40
  size_t tnameLen = strlen(name->tname);
501,816✔
41
  if (tnameLen > 0) {
501,816✔
42
    dst[len] = TS_PATH_DELIMITER[0];
501,137✔
43

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

48
  return 0;
501,816✔
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) {
501,578✔
65
  if (!VALID_NAME_TYPE(name->type)) {
501,578✔
66
    return false;
1✔
67
  }
68

69
  if (name->type == TSDB_DB_NAME_T) {
501,577✔
70
    return strlen(name->dbname) > 0;
798✔
71
  } else {
72
    return strlen(name->dbname) > 0 && strlen(name->tname) > 0;
500,779✔
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) {
3,855✔
83
  tstrncpy(dst, name->dbname, tListLen(name->dbname));
3,855✔
84
  return 0;
3,855✔
85
}
86

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

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

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

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

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

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

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

123
  dst->type = TSDB_DB_NAME_T;
145,616✔
124
  dst->acctId = acct;
145,616✔
125
  tstrncpy(dst->dbname, dbName, nameLen + 1);
145,616✔
126
  return 0;
145,616✔
127
}
128

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

135
  dst->type = TSDB_TABLE_NAME_T;
261✔
136
  tstrncpy(dst->tname, tbName, nameLen + 1);
261✔
137
  return 0;
261✔
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) {
2✔
146
  if (NULL == left) {
2✔
147
    if (NULL == right) {
×
148
      return true;
×
149
    }
150

151
    return false;
×
152
  }
153

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

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

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

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

171
  return equal;
×
172
}
173

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

179
  char* p = NULL;
405,128✔
180
  if ((type & T_NAME_ACCT) == T_NAME_ACCT) {
405,128✔
181
    p = strstr(str, TS_PATH_DELIMITER);
266,303✔
182
    if (p == NULL) {
266,303✔
183
      return TSDB_CODE_INVALID_PARA;
×
184
    }
185

186
    dst->acctId = taosStr2Int32(str, NULL, 10);
266,303✔
187
  }
188

189
  if ((type & T_NAME_DB) == T_NAME_DB) {
405,287✔
190
    dst->type = TSDB_DB_NAME_T;
266,489✔
191
    char* start = (char*)((p == NULL) ? str : (p + 1));
266,489✔
192

193
    int32_t len = 0;
266,489✔
194
    p = strstr(start, TS_PATH_DELIMITER);
266,489✔
195
    if (p == NULL) {
266,489✔
196
      len = (int32_t)strlen(start);
88,808✔
197
    } else {
198
      len = (int32_t)(p - start);
177,681✔
199
    }
200

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

206
    memcpy(dst->dbname, start, len);
266,505✔
207
    dst->dbname[len] = 0;
266,505✔
208
  }
209

210
  if ((type & T_NAME_TABLE) == T_NAME_TABLE) {
405,303✔
211
    dst->type = TSDB_TABLE_NAME_T;
316,652✔
212
    char* start = (char*)((p == NULL) ? str : (p + 1));
316,652✔
213

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

220
    memcpy(dst->tname, start, len);
316,632✔
221
    dst->tname[len] = 0;
316,632✔
222
  }
223

224
  return 0;
405,283✔
225
}
226

227
static int compareKv(const void* p1, const void* p2) {
4,378✔
228
  SSmlKv* kv1 = (SSmlKv*)p1;
4,378✔
229
  SSmlKv* kv2 = (SSmlKv*)p2;
4,378✔
230
  int32_t kvLen1 = kv1->keyLen;
4,378✔
231
  int32_t kvLen2 = kv2->keyLen;
4,378✔
232
  int32_t res = taosStrncasecmp(kv1->key, kv2->key, TMIN(kvLen1, kvLen2));
4,378✔
233
  if (res != 0) {
4,378✔
234
    return res;
4,354✔
235
  } else {
236
    return kvLen1 - kvLen2;
24✔
237
  }
238
}
239

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

246
  T_MD5_CTX context;
247
  tMD5Init(&context);
371✔
248

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

252
  // add tags
253
  for (int j = 0; j < taosArrayGetSize(rName->tags); ++j) {
3,289✔
254
    SSmlKv* tagKv = taosArrayGet(rName->tags, j);
2,918✔
255
    if (tagKv == NULL) {
2,918✔
256
      return TSDB_CODE_SML_INVALID_DATA;
×
257
    }
258

259
    tMD5Update(&context, (uint8_t*)",", 1);
2,918✔
260
    tMD5Update(&context, (uint8_t*)tagKv->key, tagKv->keyLen);
2,919✔
261
    tMD5Update(&context, (uint8_t*)"=", 1);
2,919✔
262

263
    if (IS_VAR_DATA_TYPE(tagKv->type)) {
2,919✔
264
      tMD5Update(&context, (uint8_t*)tagKv->value, tagKv->length);
1,214✔
265
    } else {
266
      tMD5Update(&context, (uint8_t*)(&(tagKv->value)), tagKv->length);
1,705✔
267
    }
268
  }
269

270
  tMD5Final(&context);
371✔
271

272
  rName->ctbShortName[0] = 't';
371✔
273
  rName->ctbShortName[1] = '_';
371✔
274
  taosByteArrayToHexStr(context.digest, 16, rName->ctbShortName + 2);
371✔
275
  rName->ctbShortName[34] = 0;
371✔
276

277
  return TSDB_CODE_SUCCESS;
371✔
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