tname.c 6.8 KB
Newer Older
H
hjxilinx 已提交
1 2 3
#include "os.h"
#include "tutil.h"

S
slguan 已提交
4
#include "tname.h"
H
Hongze Cheng 已提交
5
#include "tmsg.h"
H
hjxilinx 已提交
6

7
#define VALID_NAME_TYPE(x)  ((x) == TSDB_DB_NAME_T || (x) == TSDB_TABLE_NAME_T)
H
hjxilinx 已提交
8

H
Haojun Liao 已提交
9 10
bool tscValidateTableNameLength(size_t len) {
  return len < TSDB_TABLE_NAME_LEN;
H
Haojun Liao 已提交
11 12
}

13
// TODO refactor
14
SColumnFilterInfo* tFilterInfoDup(const SColumnFilterInfo* src, int32_t numOfFilters) {
W
wpan 已提交
15
  if (numOfFilters == 0 || src == NULL) {
H
Haojun Liao 已提交
16 17 18 19 20 21 22 23 24 25 26 27
    assert(src == NULL);
    return NULL;
  }

  SColumnFilterInfo* pFilter = calloc(1, numOfFilters * sizeof(SColumnFilterInfo));

  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;
      pFilter[j].pz = (int64_t) calloc(1, len);

H
Haojun Liao 已提交
28
      memcpy((char*)pFilter[j].pz, (char*)src[j].pz, (size_t) pFilter[j].len);
H
Haojun Liao 已提交
29 30 31 32 33 34 35 36
    }
  }

  assert(src->filterstr == 0 || src->filterstr == 1);
  assert(!(src->lowerRelOptr == TSDB_RELATION_INVALID && src->upperRelOptr == TSDB_RELATION_INVALID));

  return pFilter;
}
H
Haojun Liao 已提交
37

B
Bomin Zhang 已提交
38
#if 0
H
Haojun Liao 已提交
39 40 41 42
int64_t taosGetIntervalStartTimestamp(int64_t startTime, int64_t slidingTime, int64_t intervalTime, char timeUnit, int16_t precision) {
  if (slidingTime == 0) {
    return startTime;
  }
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
  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;
    localtime_r(&t, &tm);
    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 已提交
59
      tm.tm_year = (int)(tm.tm_year / slidingTime * slidingTime);
60 61
    } else {
      int mon = tm.tm_year * 12 + tm.tm_mon;
S
Shengliang Guan 已提交
62
      mon = (int)(mon / slidingTime * slidingTime);
63 64 65
      tm.tm_year = mon / 12;
      tm.tm_mon = mon % 12;
    }
H
Haojun Liao 已提交
66

67 68 69 70 71
    start = mktime(&tm) * 1000L;
    if (precision == TSDB_TIME_PRECISION_MICRO) {
      start *= 1000L;
    }
  } else {
H
Haojun Liao 已提交
72 73 74 75
    int64_t delta = startTime - intervalTime;
    int32_t factor = delta > 0? 1:-1;

    start = (delta / slidingTime + factor) * slidingTime;
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

    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
  #if defined(WINDOWS) && _MSC_VER >= 1900
      // 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;
  #endif

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

94 95 96 97
    int64_t end = start + intervalTime - 1;
    if (end < startTime) {
      start += slidingTime;
    }
H
Haojun Liao 已提交
98 99 100 101
  }

  return start;
}
H
Haojun Liao 已提交
102

B
Bomin Zhang 已提交
103 104
#endif

105 106 107 108
int32_t tNameExtractFullName(const SName* name, char* dst) {
  assert(name != NULL && dst != NULL);

  // invalid full name format, abort
H
Haojun Liao 已提交
109
  if (!tNameIsValid(name)) {
110 111 112
    return -1;
  }

113
  int32_t len = snprintf(dst, TSDB_DB_FNAME_LEN, "%d.%s", name->acctId, name->dbname);
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

  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);
129 130 131

  char tmp[12] = {0};
  int32_t len = sprintf(tmp, "%d", name->acctId);
132 133 134 135 136
  int32_t len1 = (int32_t) strlen(name->dbname);
  int32_t len2 = (int32_t) strlen(name->tname);

  if (name->type == TSDB_DB_NAME_T) {
    assert(len2 == 0);
137
    return len + len1 + TSDB_NAME_DELIMITER_LEN;
138 139
  } else {
    assert(len2 > 0);
140
    return len + len1 + len2 + TSDB_NAME_DELIMITER_LEN * 2;
141 142 143
  }
}

H
Haojun Liao 已提交
144
bool tNameIsValid(const SName* name) {
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  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);

161
  SName* p = malloc(sizeof(SName));
162 163 164 165 166 167 168 169 170 171 172 173
  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);
174
  snprintf(dst, TSDB_DB_FNAME_LEN, "%d.%s", name->acctId, name->dbname);
175 176 177 178 179
  return 0;
}

bool tNameIsEmpty(const SName* name) {
  assert(name != NULL);
H
Haojun Liao 已提交
180
  return name->type == 0 || name->acctId == 0;
181 182 183 184 185 186 187 188 189 190 191
}

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

void tNameAssign(SName* dst, const SName* src) {
  memcpy(dst, src, sizeof(SName));
}

H
Haojun Liao 已提交
192 193
int32_t tNameSetDbName(SName* dst, int32_t acct, const char* dbName, size_t nameLen) {
  assert(dst != NULL && dbName != NULL && nameLen > 0);
194 195

  // too long account id or too long db name
H
Haojun Liao 已提交
196
  if (nameLen >= tListLen(dst->dbname)) {
197 198 199
    return -1;
  }

H
Haojun Liao 已提交
200 201 202 203 204
  dst->type = TSDB_DB_NAME_T;
  dst->acctId = acct;
  tstrncpy(dst->dbname, dbName, nameLen + 1);
  return 0;
}
D
dapan1121 已提交
205

H
Haojun Liao 已提交
206 207 208
int32_t tNameSetAcctId(SName* dst, int32_t acctId) {
  assert(dst != NULL && acct != NULL);
  dst->acctId = acctId;
209 210 211 212 213 214 215 216 217 218 219 220 221
  return 0;
}

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;
    }

H
Haojun Liao 已提交
222
    dst->acctId = strtoll(str, NULL, 10);
223 224 225 226 227 228 229 230 231
  }

  if ((type & T_NAME_DB) == T_NAME_DB) {
    dst->type = TSDB_DB_NAME_T;
    char* start = (char*)((p == NULL)? str:(p+1));

    int32_t len = 0;
    p = strstr(start, TS_PATH_DELIMITER);
    if (p == NULL) {
H
Haojun Liao 已提交
232
      len = (int32_t) strlen(start);
233
    } else {
H
Haojun Liao 已提交
234
      len = (int32_t) (p - start);
235 236 237
    }

    // too long account id or too long db name
D
dapan1121 已提交
238
    if ((len >= tListLen(dst->dbname)) || (len <= 0)) {
239 240 241 242 243 244 245 246 247 248 249 250
      return -1;
    }

    memcpy (dst->dbname, start, len);
    dst->dbname[len] = 0;
  }

  if ((type & T_NAME_TABLE) == T_NAME_TABLE) {
    dst->type = TSDB_TABLE_NAME_T;
    char* start = (char*) ((p == NULL)? str: (p+1));

    // too long account id or too long db name
251
    int32_t len = (int32_t) strlen(start);
D
dapan1121 已提交
252
    if ((len >= tListLen(dst->tname)) || (len <= 0)) {
253 254 255 256 257 258 259 260 261
      return -1;
    }

    memcpy (dst->tname, start, len);
    dst->tname[len] = 0;
  }

  return 0;
}
H
Haojun Liao 已提交
262 263 264 265 266 267 268 269 270 271

SSchema createSchema(uint8_t type, int32_t bytes, int32_t colId, const char* name) {
  SSchema s = {0};
  s.type  = type;
  s.bytes = bytes;
  s.colId = colId;

  tstrncpy(s.name, name, tListLen(s.name));
  return s;
}