tname.c 9.0 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

L
Liu Jicong 已提交
23
bool tscValidateTableNameLength(size_t len) { return len < TSDB_TABLE_NAME_LEN; }
H
Haojun Liao 已提交
24

S
monitor  
Shengliang Guan 已提交
25
#if 0
26
// TODO refactor
27
SColumnFilterInfo* tFilterInfoDup(const SColumnFilterInfo* src, int32_t numOfFilters) {
W
wpan 已提交
28
  if (numOfFilters == 0 || src == NULL) {
H
Haojun Liao 已提交
29 30 31 32
    assert(src == NULL);
    return NULL;
  }

wafwerar's avatar
wafwerar 已提交
33
  SColumnFilterInfo* pFilter = taosMemoryCalloc(1, numOfFilters * sizeof(SColumnFilterInfo));
H
Haojun Liao 已提交
34 35 36 37 38

  memcpy(pFilter, src, sizeof(SColumnFilterInfo) * numOfFilters);
  for (int32_t j = 0; j < numOfFilters; ++j) {
    if (pFilter[j].filterstr) {
      size_t len = (size_t) pFilter[j].len + 1 * TSDB_NCHAR_SIZE;
wafwerar's avatar
wafwerar 已提交
39
      pFilter[j].pz = (int64_t) taosMemoryCalloc(1, len);
H
Haojun Liao 已提交
40

H
Haojun Liao 已提交
41
      memcpy((char*)pFilter[j].pz, (char*)src[j].pz, (size_t) pFilter[j].len);
H
Haojun Liao 已提交
42 43 44 45
    }
  }

  assert(src->filterstr == 0 || src->filterstr == 1);
D
dapan1121 已提交
46
  assert(!(src->lowerRelOptr == 0 && src->upperRelOptr == 0));
H
Haojun Liao 已提交
47 48 49

  return pFilter;
}
S
monitor  
Shengliang Guan 已提交
50
#endif
B
Bomin Zhang 已提交
51
#if 0
H
Haojun Liao 已提交
52 53 54 55
int64_t taosGetIntervalStartTimestamp(int64_t startTime, int64_t slidingTime, int64_t intervalTime, char timeUnit, int16_t precision) {
  if (slidingTime == 0) {
    return startTime;
  }
56 57 58 59 60 61 62 63
  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 已提交
64
    taosLocalTime(&t, &tm);
65 66 67 68 69 70 71
    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 已提交
72
      tm.tm_year = (int)(tm.tm_year / slidingTime * slidingTime);
73 74
    } else {
      int mon = tm.tm_year * 12 + tm.tm_mon;
S
Shengliang Guan 已提交
75
      mon = (int)(mon / slidingTime * slidingTime);
76 77 78
      tm.tm_year = mon / 12;
      tm.tm_mon = mon % 12;
    }
H
Haojun Liao 已提交
79

80 81 82 83 84
    start = mktime(&tm) * 1000L;
    if (precision == TSDB_TIME_PRECISION_MICRO) {
      start *= 1000L;
    }
  } else {
H
Haojun Liao 已提交
85 86 87 88
    int64_t delta = startTime - intervalTime;
    int32_t factor = delta > 0? 1:-1;

    start = (delta / slidingTime + factor) * slidingTime;
89 90 91 92 93 94 95

    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 已提交
96
#if defined(WINDOWS) && _MSC_VER >= 1900
97 98 99 100
      // 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 已提交
101
#endif
102 103 104 105

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

107 108 109 110
    int64_t end = start + intervalTime - 1;
    if (end < startTime) {
      start += slidingTime;
    }
H
Haojun Liao 已提交
111 112 113 114
  }

  return start;
}
H
Haojun Liao 已提交
115

B
Bomin Zhang 已提交
116 117
#endif

118 119 120 121
int32_t tNameExtractFullName(const SName* name, char* dst) {
  assert(name != NULL && dst != NULL);

  // invalid full name format, abort
H
Haojun Liao 已提交
122
  if (!tNameIsValid(name)) {
123 124 125
    return -1;
  }

126
  int32_t len = snprintf(dst, TSDB_DB_FNAME_LEN, "%d.%s", name->acctId, name->dbname);
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

  size_t tnameLen = strlen(name->tname);
  if (tnameLen > 0) {
    assert(name->type == TSDB_TABLE_NAME_T);
    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);
142

L
Liu Jicong 已提交
143
  char    tmp[12] = {0};
144
  int32_t len = sprintf(tmp, "%d", name->acctId);
L
Liu Jicong 已提交
145 146
  int32_t len1 = (int32_t)strlen(name->dbname);
  int32_t len2 = (int32_t)strlen(name->tname);
147 148 149

  if (name->type == TSDB_DB_NAME_T) {
    assert(len2 == 0);
150
    return len + len1 + TSDB_NAME_DELIMITER_LEN;
151 152
  } else {
    assert(len2 > 0);
153
    return len + len1 + len2 + TSDB_NAME_DELIMITER_LEN * 2;
154 155 156
  }
}

H
Haojun Liao 已提交
157
bool tNameIsValid(const SName* name) {
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
  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 已提交
174
  SName* p = taosMemoryMalloc(sizeof(SName));
175 176 177 178 179 180 181 182 183 184 185 186
  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;
}

int32_t tNameGetFullDbName(const SName* name, char* dst) {
  assert(name != NULL && dst != NULL);
187
  snprintf(dst, TSDB_DB_FNAME_LEN, "%d.%s", name->acctId, name->dbname);
188 189 190 191 192
  return 0;
}

bool tNameIsEmpty(const SName* name) {
  assert(name != NULL);
H
Haojun Liao 已提交
193
  return name->type == 0 || name->acctId == 0;
194 195 196 197 198 199 200
}

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

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

H
Haojun Liao 已提交
203 204
int32_t tNameSetDbName(SName* dst, int32_t acct, const char* dbName, size_t nameLen) {
  assert(dst != NULL && dbName != NULL && nameLen > 0);
205 206

  // too long account id or too long db name
H
Haojun Liao 已提交
207
  if (nameLen >= tListLen(dst->dbname)) {
208 209 210
    return -1;
  }

H
Haojun Liao 已提交
211 212 213 214 215
  dst->type = TSDB_DB_NAME_T;
  dst->acctId = acct;
  tstrncpy(dst->dbname, dbName, nameLen + 1);
  return 0;
}
D
dapan1121 已提交
216

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

D
dapan1121 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
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));
}

243 244 245 246 247 248 249 250 251 252
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 已提交
253
    dst->acctId = taosStr2Int32(str, NULL, 10);
254 255 256 257
  }

  if ((type & T_NAME_DB) == T_NAME_DB) {
    dst->type = TSDB_DB_NAME_T;
L
Liu Jicong 已提交
258
    char* start = (char*)((p == NULL) ? str : (p + 1));
259 260 261 262

    int32_t len = 0;
    p = strstr(start, TS_PATH_DELIMITER);
    if (p == NULL) {
L
Liu Jicong 已提交
263
      len = (int32_t)strlen(start);
264
    } else {
L
Liu Jicong 已提交
265
      len = (int32_t)(p - start);
266 267 268
    }

    // too long account id or too long db name
D
dapan1121 已提交
269
    if ((len >= tListLen(dst->dbname)) || (len <= 0)) {
270 271 272
      return -1;
    }

L
Liu Jicong 已提交
273
    memcpy(dst->dbname, start, len);
274 275 276 277 278
    dst->dbname[len] = 0;
  }

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

    // too long account id or too long db name
L
Liu Jicong 已提交
282
    int32_t len = (int32_t)strlen(start);
D
dapan1121 已提交
283
    if ((len >= tListLen(dst->tname)) || (len <= 0)) {
284 285 286
      return -1;
    }

L
Liu Jicong 已提交
287
    memcpy(dst->tname, start, len);
288 289 290 291 292
    dst->tname[len] = 0;
  }

  return 0;
}
H
Haojun Liao 已提交
293

294 295 296 297 298 299 300 301 302
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 已提交
303
    return kvLen1 - kvLen2;
304 305
  }
}
H
Haojun Liao 已提交
306

307 308 309
/*
 * use stable name and tags to grearate child table name
 */
L
Liu Jicong 已提交
310
void buildChildTableName(RandTableName* rName) {
311 312 313 314 315 316 317
  int32_t size = taosArrayGetSize(rName->tags);
  ASSERT(size > 0);
  taosArraySort(rName->tags, compareKv);

  SStringBuilder sb = {0};
  taosStringBuilderAppendStringLen(&sb, rName->sTableName, rName->sTableNameLen);
  for (int j = 0; j < size; ++j) {
L
Liu Jicong 已提交
318
    SSmlKv* tagKv = taosArrayGetP(rName->tags, j);
319
    taosStringBuilderAppendStringLen(&sb, tagKv->key, tagKv->keyLen);
wmmhello's avatar
wmmhello 已提交
320 321 322 323 324
    if(IS_VAR_DATA_TYPE(tagKv->type)){
      taosStringBuilderAppendStringLen(&sb, tagKv->value, tagKv->length);
    }else{
      taosStringBuilderAppendStringLen(&sb, (char*)(&(tagKv->value)), tagKv->length);
    }
325
  }
L
Liu Jicong 已提交
326 327
  size_t    len = 0;
  char*     keyJoined = taosStringBuilderGetResult(&sb, &len);
328 329
  T_MD5_CTX context;
  tMD5Init(&context);
L
Liu Jicong 已提交
330
  tMD5Update(&context, (uint8_t*)keyJoined, (uint32_t)len);
331 332 333
  tMD5Final(&context);
  uint64_t digest1 = *(uint64_t*)(context.digest);
  uint64_t digest2 = *(uint64_t*)(context.digest + 8);
L
Liu Jicong 已提交
334
  snprintf(rName->childTableName, TSDB_TABLE_NAME_LEN, "t_%016" PRIx64 "%016" PRIx64, digest1, digest2);
335 336 337
  taosStringBuilderDestroy(&sb);
  rName->uid = digest1;
}