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

taosdata / TDengine / #3768

28 Mar 2025 10:15AM UTC coverage: 33.726% (-0.3%) from 33.993%
#3768

push

travis-ci

happyguoxy
test:alter lcov result

144891 of 592084 branches covered (24.47%)

Branch coverage included in aggregate %.

218795 of 486283 relevant lines covered (44.99%)

765715.29 hits per line

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

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

32
int32_t tNameExtractFullName(const SName* name, char* dst) {
236,036✔
33
  // invalid full name format, abort
34
  if (!tNameIsValid(name)) {
236,036✔
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);
236,039✔
39

40
  size_t tnameLen = strlen(name->tname);
236,048✔
41
  if (tnameLen > 0) {
236,048✔
42
    dst[len] = TS_PATH_DELIMITER[0];
235,401✔
43

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

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

69
  if (name->type == TSDB_DB_NAME_T) {
236,034✔
70
    return strlen(name->dbname) > 0;
729✔
71
  } else {
72
    return strlen(name->dbname) > 0 && strlen(name->tname) > 0;
235,305!
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,842✔
83
  tstrncpy(dst, name->dbname, tListLen(name->dbname));
3,842✔
84
  return 0;
3,842✔
85
}
86

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

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

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

99
const char* tNameGetTableName(const SName* name) {
6,626✔
100
  if (!(name != NULL && name->type == TSDB_TABLE_NAME_T)) {
6,626!
101
    terrno = TSDB_CODE_INVALID_PARA;
102
    return NULL;
×
103
  }
104
  return &name->tname[0];
6,627✔
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)); }
322✔
116

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

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

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

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

151
    return false;
×
152
  }
153

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

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

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

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

171
  return equal;
×
172
}
173

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

179
  char* p = NULL;
195,674✔
180
  if ((type & T_NAME_ACCT) == T_NAME_ACCT) {
195,674✔
181
    p = strstr(str, TS_PATH_DELIMITER);
80,067✔
182
    if (p == NULL) {
80,067!
183
      return TSDB_CODE_INVALID_PARA;
×
184
    }
185

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

189
  if ((type & T_NAME_DB) == T_NAME_DB) {
195,664✔
190
    dst->type = TSDB_DB_NAME_T;
80,059✔
191
    char* start = (char*)((p == NULL) ? str : (p + 1));
80,059!
192

193
    int32_t len = 0;
80,059✔
194
    p = strstr(start, TS_PATH_DELIMITER);
80,059✔
195
    if (p == NULL) {
80,059✔
196
      len = (int32_t)strlen(start);
23,742✔
197
    } else {
198
      len = (int32_t)(p - start);
56,317✔
199
    }
200

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

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

210
  if ((type & T_NAME_TABLE) == T_NAME_TABLE) {
195,666✔
211
    dst->type = TSDB_TABLE_NAME_T;
171,962✔
212
    char* start = (char*)((p == NULL) ? str : (p + 1));
171,962✔
213

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

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

224
  return 0;
195,667✔
225
}
226

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

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

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

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

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

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

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

270
  tMD5Final(&context);
374✔
271

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

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