tname.c 9.2 KB
Newer Older
S
common  
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * 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 <http://www.gnu.org/licenses/>.
 */
H
hjxilinx 已提交
15

S
common  
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
S
slguan 已提交
17
#include "tname.h"
18 19
#include "tcommon.h"
#include "tstrbuild.h"
H
hjxilinx 已提交
20

L
Liu Jicong 已提交
21
#define VALID_NAME_TYPE(x) ((x) == TSDB_DB_NAME_T || (x) == TSDB_TABLE_NAME_T)
H
hjxilinx 已提交
22

B
Bomin Zhang 已提交
23
#if 0
H
Haojun Liao 已提交
24 25 26 27
int64_t taosGetIntervalStartTimestamp(int64_t startTime, int64_t slidingTime, int64_t intervalTime, char timeUnit, int16_t precision) {
  if (slidingTime == 0) {
    return startTime;
  }
28 29 30 31 32 33 34 35
  int64_t start = startTime;
  if (timeUnit == 'n' || timeUnit == 'y') {
    start /= 1000;
    if (precision == TSDB_TIME_PRECISION_MICRO) {
      start /= 1000;
    }
    struct tm tm;
    time_t t = (time_t)start;
wafwerar's avatar
wafwerar 已提交
36
    taosLocalTime(&t, &tm);
37 38 39 40 41 42 43
    tm.tm_sec = 0;
    tm.tm_min = 0;
    tm.tm_hour = 0;
    tm.tm_mday = 1;

    if (timeUnit == 'y') {
      tm.tm_mon = 0;
S
Shengliang Guan 已提交
44
      tm.tm_year = (int)(tm.tm_year / slidingTime * slidingTime);
45 46
    } else {
      int mon = tm.tm_year * 12 + tm.tm_mon;
S
Shengliang Guan 已提交
47
      mon = (int)(mon / slidingTime * slidingTime);
48 49 50
      tm.tm_year = mon / 12;
      tm.tm_mon = mon % 12;
    }
H
Haojun Liao 已提交
51

52 53 54 55 56
    start = mktime(&tm) * 1000L;
    if (precision == TSDB_TIME_PRECISION_MICRO) {
      start *= 1000L;
    }
  } else {
H
Haojun Liao 已提交
57 58 59 60
    int64_t delta = startTime - intervalTime;
    int32_t factor = delta > 0? 1:-1;

    start = (delta / slidingTime + factor) * slidingTime;
61 62 63 64 65 66 67

    if (timeUnit == 'd' || timeUnit == 'w') {
      /*
      * here we revised the start time of day according to the local time zone,
      * but in case of DST, the start time of one day need to be dynamically decided.
      */
      // todo refactor to extract function that is available for Linux/Windows/Mac platform
L
Liu Jicong 已提交
68
#if defined(WINDOWS) && _MSC_VER >= 1900
69 70 71 72
      // see https://docs.microsoft.com/en-us/cpp/c-runtime-library/daylight-dstbias-timezone-and-tzname?view=vs-2019
      int64_t timezone = _timezone;
      int32_t daylight = _daylight;
      char**  tzname = _tzname;
L
Liu Jicong 已提交
73
#endif
74 75 76 77

      int64_t t = (precision == TSDB_TIME_PRECISION_MILLI) ? MILLISECOND_PER_SECOND : MILLISECOND_PER_SECOND * 1000L;
      start += timezone * t;
    }
H
Haojun Liao 已提交
78

79 80 81 82
    int64_t end = start + intervalTime - 1;
    if (end < startTime) {
      start += slidingTime;
    }
H
Haojun Liao 已提交
83 84 85 86
  }

  return start;
}
H
Haojun Liao 已提交
87

B
Bomin Zhang 已提交
88 89
#endif

wmmhello's avatar
wmmhello 已提交
90 91 92
SName* toName(int32_t acctId, const char* pDbName, const char* pTableName, SName* pName) {
  pName->type = TSDB_TABLE_NAME_T;
  pName->acctId = acctId;
G
Ganlin Zhao 已提交
93 94 95 96
  memset(pName->dbname, 0, TSDB_DB_NAME_LEN);
  strncpy(pName->dbname, pDbName, TSDB_DB_NAME_LEN - 1);
  memset(pName->tname, 0, TSDB_TABLE_NAME_LEN);
  strncpy(pName->tname, pTableName, TSDB_TABLE_NAME_LEN - 1);
wmmhello's avatar
wmmhello 已提交
97 98 99
  return pName;
}

100 101 102 103
int32_t tNameExtractFullName(const SName* name, char* dst) {
  assert(name != NULL && dst != NULL);

  // invalid full name format, abort
H
Haojun Liao 已提交
104
  if (!tNameIsValid(name)) {
105 106 107
    return -1;
  }

108
  int32_t len = snprintf(dst, TSDB_DB_FNAME_LEN, "%d.%s", name->acctId, name->dbname);
109 110 111

  size_t tnameLen = strlen(name->tname);
  if (tnameLen > 0) {
L
Liu Jicong 已提交
112
    /*assert(name->type == TSDB_TABLE_NAME_T);*/
113 114 115 116 117 118 119 120 121 122 123
    dst[len] = TS_PATH_DELIMITER[0];

    memcpy(dst + len + 1, name->tname, tnameLen);
    dst[len + tnameLen + 1] = 0;
  }

  return 0;
}

int32_t tNameLen(const SName* name) {
  assert(name != NULL);
124

L
Liu Jicong 已提交
125
  char    tmp[12] = {0};
126
  int32_t len = sprintf(tmp, "%d", name->acctId);
L
Liu Jicong 已提交
127 128
  int32_t len1 = (int32_t)strlen(name->dbname);
  int32_t len2 = (int32_t)strlen(name->tname);
129 130 131

  if (name->type == TSDB_DB_NAME_T) {
    assert(len2 == 0);
132
    return len + len1 + TSDB_NAME_DELIMITER_LEN;
133 134
  } else {
    assert(len2 > 0);
135
    return len + len1 + len2 + TSDB_NAME_DELIMITER_LEN * 2;
136 137 138
  }
}

H
Haojun Liao 已提交
139
bool tNameIsValid(const SName* name) {
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
  assert(name != NULL);

  if (!VALID_NAME_TYPE(name->type)) {
    return false;
  }

  if (name->type == TSDB_DB_NAME_T) {
    return strlen(name->dbname) > 0;
  } else {
    return strlen(name->dbname) > 0 && strlen(name->tname) > 0;
  }
}

SName* tNameDup(const SName* name) {
  assert(name != NULL);

wafwerar's avatar
wafwerar 已提交
156
  SName* p = taosMemoryMalloc(sizeof(SName));
157 158 159 160 161 162 163 164 165 166
  memcpy(p, name, sizeof(SName));
  return p;
}

int32_t tNameGetDbName(const SName* name, char* dst) {
  assert(name != NULL && dst != NULL);
  strncpy(dst, name->dbname, tListLen(name->dbname));
  return 0;
}

167
const char* tNameGetDbNameP(const SName* name) { return &name->dbname[0]; }
D
dapan1121 已提交
168

169 170
int32_t tNameGetFullDbName(const SName* name, char* dst) {
  assert(name != NULL && dst != NULL);
171
  snprintf(dst, TSDB_DB_FNAME_LEN, "%d.%s", name->acctId, name->dbname);
172 173 174 175 176
  return 0;
}

bool tNameIsEmpty(const SName* name) {
  assert(name != NULL);
H
Haojun Liao 已提交
177
  return name->type == 0 || name->acctId == 0;
178 179 180 181 182 183 184
}

const char* tNameGetTableName(const SName* name) {
  assert(name != NULL && name->type == TSDB_TABLE_NAME_T);
  return &name->tname[0];
}

L
Liu Jicong 已提交
185
void tNameAssign(SName* dst, const SName* src) { memcpy(dst, src, sizeof(SName)); }
186

H
Haojun Liao 已提交
187 188
int32_t tNameSetDbName(SName* dst, int32_t acct, const char* dbName, size_t nameLen) {
  assert(dst != NULL && dbName != NULL && nameLen > 0);
189 190

  // too long account id or too long db name
H
Haojun Liao 已提交
191
  if (nameLen >= tListLen(dst->dbname)) {
192 193 194
    return -1;
  }

H
Haojun Liao 已提交
195 196 197 198 199
  dst->type = TSDB_DB_NAME_T;
  dst->acctId = acct;
  tstrncpy(dst->dbname, dbName, nameLen + 1);
  return 0;
}
D
dapan1121 已提交
200

D
dapan1121 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213
int32_t tNameAddTbName(SName* dst, const char* tbName, size_t nameLen) {
  assert(dst != NULL && tbName != NULL && nameLen > 0);

  // too long account id or too long db name
  if (nameLen >= tListLen(dst->tname) || nameLen <= 0) {
    return -1;
  }

  dst->type = TSDB_TABLE_NAME_T;
  tstrncpy(dst->tname, tbName, nameLen + 1);
  return 0;
}

H
Haojun Liao 已提交
214
int32_t tNameSetAcctId(SName* dst, int32_t acctId) {
215
  assert(dst != NULL);
H
Haojun Liao 已提交
216
  dst->acctId = acctId;
217 218 219
  return 0;
}

D
dapan1121 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
bool tNameDBNameEqual(SName* left, SName* right) {
  if (NULL == left) {
    if (NULL == right) {
      return true;
    }

    return false;
  }

  if (NULL == right) {
    return false;
  }

  if (left->acctId != right->acctId) {
    return false;
  }

  return (0 == strcmp(left->dbname, right->dbname));
}

D
dapan1121 已提交
240 241 242 243 244 245 246 247 248
bool tNameTbNameEqual(SName* left, SName* right) {
  bool equal = tNameDBNameEqual(left, right);
  if (equal) {
    return (0 == strcmp(left->tname, right->tname));
  }

  return equal;
}

249 250 251 252 253 254 255 256 257 258
int32_t tNameFromString(SName* dst, const char* str, uint32_t type) {
  assert(dst != NULL && str != NULL && strlen(str) > 0);

  char* p = NULL;
  if ((type & T_NAME_ACCT) == T_NAME_ACCT) {
    p = strstr(str, TS_PATH_DELIMITER);
    if (p == NULL) {
      return -1;
    }

wafwerar's avatar
wafwerar 已提交
259
    dst->acctId = taosStr2Int32(str, NULL, 10);
260 261 262 263
  }

  if ((type & T_NAME_DB) == T_NAME_DB) {
    dst->type = TSDB_DB_NAME_T;
L
Liu Jicong 已提交
264
    char* start = (char*)((p == NULL) ? str : (p + 1));
265 266

    int32_t len = 0;
267 268 269
    p = strstr(start, TS_PATH_DELIMITER);
    if (p == NULL) {
      len = (int32_t)strlen(start);
270
    } else {
271
      len = (int32_t)(p - start);
272 273 274
    }

    // too long account id or too long db name
D
dapan1121 已提交
275
    if ((len >= tListLen(dst->dbname)) || (len <= 0)) {
276 277 278
      return -1;
    }

L
Liu Jicong 已提交
279
    memcpy(dst->dbname, start, len);
280 281 282 283 284
    dst->dbname[len] = 0;
  }

  if ((type & T_NAME_TABLE) == T_NAME_TABLE) {
    dst->type = TSDB_TABLE_NAME_T;
L
Liu Jicong 已提交
285
    char* start = (char*)((p == NULL) ? str : (p + 1));
286 287

    // too long account id or too long db name
L
Liu Jicong 已提交
288
    int32_t len = (int32_t)strlen(start);
D
dapan1121 已提交
289
    if ((len >= tListLen(dst->tname)) || (len <= 0)) {
290 291 292
      return -1;
    }

L
Liu Jicong 已提交
293
    memcpy(dst->tname, start, len);
294 295 296 297 298
    dst->tname[len] = 0;
  }

  return 0;
}
H
Haojun Liao 已提交
299

300 301 302 303 304 305 306 307 308
static int compareKv(const void* p1, const void* p2) {
  SSmlKv* kv1 = *(SSmlKv**)p1;
  SSmlKv* kv2 = *(SSmlKv**)p2;
  int32_t kvLen1 = kv1->keyLen;
  int32_t kvLen2 = kv2->keyLen;
  int32_t res = strncasecmp(kv1->key, kv2->key, TMIN(kvLen1, kvLen2));
  if (res != 0) {
    return res;
  } else {
L
Liu Jicong 已提交
309
    return kvLen1 - kvLen2;
310 311
  }
}
H
Haojun Liao 已提交
312

313 314 315
/*
 * use stable name and tags to grearate child table name
 */
L
Liu Jicong 已提交
316
void buildChildTableName(RandTableName* rName) {
317 318
  SStringBuilder sb = {0};
  taosStringBuilderAppendStringLen(&sb, rName->sTableName, rName->sTableNameLen);
319 320
  taosArraySort(rName->tags, compareKv);
  for (int j = 0; j < taosArrayGetSize(rName->tags); ++j) {
wmmhello's avatar
wmmhello 已提交
321
    taosStringBuilderAppendChar(&sb, ',');
L
Liu Jicong 已提交
322
    SSmlKv* tagKv = taosArrayGetP(rName->tags, j);
323
    taosStringBuilderAppendStringLen(&sb, tagKv->key, tagKv->keyLen);
wmmhello's avatar
wmmhello 已提交
324
    taosStringBuilderAppendChar(&sb, '=');
L
Liu Jicong 已提交
325
    if (IS_VAR_DATA_TYPE(tagKv->type)) {
wmmhello's avatar
wmmhello 已提交
326
      taosStringBuilderAppendStringLen(&sb, tagKv->value, tagKv->length);
L
Liu Jicong 已提交
327
    } else {
wmmhello's avatar
wmmhello 已提交
328 329
      taosStringBuilderAppendStringLen(&sb, (char*)(&(tagKv->value)), tagKv->length);
    }
330
  }
L
Liu Jicong 已提交
331 332
  size_t    len = 0;
  char*     keyJoined = taosStringBuilderGetResult(&sb, &len);
333 334
  T_MD5_CTX context;
  tMD5Init(&context);
L
Liu Jicong 已提交
335
  tMD5Update(&context, (uint8_t*)keyJoined, (uint32_t)len);
336
  tMD5Final(&context);
wmmhello's avatar
wmmhello 已提交
337 338 339 340

  char temp[8] = {0};
  rName->childTableName[0] = 't';
  rName->childTableName[1] = '_';
341
  for (int i = 0; i < 16; i++) {
wmmhello's avatar
wmmhello 已提交
342 343 344
    sprintf(temp, "%02x", context.digest[i]);
    strcat(rName->childTableName, temp);
  }
345
  taosStringBuilderDestroy(&sb);
wmmhello's avatar
wmmhello 已提交
346
  rName->uid = *(uint64_t*)(context.digest);
347
}