metaQuery.c 38.6 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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
Hongze Cheng 已提交
16
#include "meta.h"
D
dapan1121 已提交
17 18
#include "osMemory.h"
#include "tencode.h"
H
Hongze Cheng 已提交
19

H
Hongze Cheng 已提交
20
void metaReaderInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
H
Hongze Cheng 已提交
21 22
  memset(pReader, 0, sizeof(*pReader));
  pReader->flags = flags;
H
Hongze Cheng 已提交
23
  pReader->pMeta = pMeta;
24
  if (pReader->pMeta && !(flags & META_READER_NOLOCK)) {
25 26
    metaRLock(pMeta);
  }
H
Hongze Cheng 已提交
27
}
H
Hongze Cheng 已提交
28

29 30 31 32 33 34 35
void metaReaderReleaseLock(SMetaReader *pReader) {
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
    metaULock(pReader->pMeta);
    pReader->flags |= META_READER_NOLOCK;
  }
}

H
Hongze Cheng 已提交
36
void metaReaderClear(SMetaReader *pReader) {
37
  if (pReader->pMeta && !(pReader->flags & META_READER_NOLOCK)) {
H
Hongze Cheng 已提交
38 39
    metaULock(pReader->pMeta);
  }
H
Hongze Cheng 已提交
40
  tDecoderClear(&pReader->coder);
H
Hongze Cheng 已提交
41
  tdbFree(pReader->pBuf);
H
Hongze Cheng 已提交
42 43
}

H
Hongze Cheng 已提交
44
int metaGetTableEntryByVersion(SMetaReader *pReader, int64_t version, tb_uid_t uid) {
H
Hongze Cheng 已提交
45
  SMeta   *pMeta = pReader->pMeta;
H
Hongze Cheng 已提交
46 47
  STbDbKey tbDbKey = {.version = version, .uid = uid};

H
Hongze Cheng 已提交
48
  // query table.db
H
Hongze Cheng 已提交
49
  if (tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf) < 0) {
H
Hongze Cheng 已提交
50
    terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
H
Hongze Cheng 已提交
51 52 53 54
    goto _err;
  }

  // decode the entry
H
Hongze Cheng 已提交
55
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
H
Hongze Cheng 已提交
56 57 58 59 60 61 62 63 64 65 66

  if (metaDecodeEntry(&pReader->coder, &pReader->me) < 0) {
    goto _err;
  }

  return 0;

_err:
  return -1;
}

dengyihao's avatar
dengyihao 已提交
67
// int metaGetTableEntryByUidTest(void* meta, SArray *uidList) {
wmmhello's avatar
wmmhello 已提交
68
//
dengyihao's avatar
dengyihao 已提交
69 70 71 72 73
//   SArray* readerList = taosArrayInit(taosArrayGetSize(uidList), sizeof(SMetaReader));
//   SArray* uidVersion = taosArrayInit(taosArrayGetSize(uidList), sizeof(STbDbKey));
//   SMeta  *pMeta = meta;
//   int64_t version;
//   SHashObj *uHash = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
wmmhello's avatar
wmmhello 已提交
74
//
dengyihao's avatar
dengyihao 已提交
75 76 77 78 79 80 81 82 83 84
//   int64_t stt1 = taosGetTimestampUs();
//   for(int i = 0; i < taosArrayGetSize(uidList); i++) {
//     void* ppVal = NULL;
//     int vlen = 0;
//     uint64_t *  uid = taosArrayGet(uidList, i);
//     // query uid.idx
//     if (tdbTbGet(pMeta->pUidIdx, uid, sizeof(*uid), &ppVal, &vlen) < 0) {
//       continue;
//     }
//     version = *(int64_t *)ppVal;
wmmhello's avatar
wmmhello 已提交
85
//
dengyihao's avatar
dengyihao 已提交
86 87 88 89 90 91
//     STbDbKey tbDbKey = {.version = version, .uid = *uid};
//     taosArrayPush(uidVersion, &tbDbKey);
//     taosHashPut(uHash, uid, sizeof(int64_t), ppVal, sizeof(int64_t));
//   }
//   int64_t stt2 = taosGetTimestampUs();
//   qDebug("metaGetTableEntryByUidTest1 rows:%d, cost:%ld us", taosArrayGetSize(uidList), stt2-stt1);
wmmhello's avatar
wmmhello 已提交
92
//
dengyihao's avatar
dengyihao 已提交
93 94 95 96 97
//   TBC        *pCur = NULL;
//   tdbTbcOpen(pMeta->pTbDb, &pCur, NULL);
//   tdbTbcMoveToFirst(pCur);
//   void *pKey = NULL;
//   int   kLen = 0;
wmmhello's avatar
wmmhello 已提交
98
//
dengyihao's avatar
dengyihao 已提交
99 100 101 102 103 104 105 106 107 108
//   while(1){
//     SMetaReader pReader = {0};
//     int32_t ret = tdbTbcNext(pCur, &pKey, &kLen, &pReader.pBuf, &pReader.szBuf);
//     if (ret < 0) break;
//     STbDbKey *tmp = (STbDbKey*)pKey;
//     int64_t *ver = (int64_t*)taosHashGet(uHash, &tmp->uid, sizeof(int64_t));
//     if(ver == NULL || *ver != tmp->version) continue;
//     taosArrayPush(readerList, &pReader);
//   }
//   tdbTbcClose(pCur);
wmmhello's avatar
wmmhello 已提交
109
//
dengyihao's avatar
dengyihao 已提交
110 111 112 113 114
//   taosArrayClear(readerList);
//   int64_t stt3 = taosGetTimestampUs();
//   qDebug("metaGetTableEntryByUidTest2 rows:%d, cost:%ld us", taosArrayGetSize(uidList), stt3-stt2);
//   for(int i = 0; i < taosArrayGetSize(uidVersion); i++) {
//     SMetaReader pReader = {0};
wmmhello's avatar
wmmhello 已提交
115
//
dengyihao's avatar
dengyihao 已提交
116 117 118 119 120 121 122 123 124
//     STbDbKey *tbDbKey = taosArrayGet(uidVersion, i);
//     // query table.db
//     if (tdbTbGet(pMeta->pTbDb, tbDbKey, sizeof(STbDbKey), &pReader.pBuf, &pReader.szBuf) < 0) {
//       continue;
//     }
//     taosArrayPush(readerList, &pReader);
//   }
//   int64_t stt4 = taosGetTimestampUs();
//   qDebug("metaGetTableEntryByUidTest3 rows:%d, cost:%ld us", taosArrayGetSize(uidList), stt4-stt3);
wmmhello's avatar
wmmhello 已提交
125
//
dengyihao's avatar
dengyihao 已提交
126 127 128 129 130
//   for(int i = 0; i < taosArrayGetSize(readerList); i++){
//     SMetaReader* pReader  = taosArrayGet(readerList, i);
//     metaReaderInit(pReader, meta, 0);
//     // decode the entry
//     tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
wmmhello's avatar
wmmhello 已提交
131
//
dengyihao's avatar
dengyihao 已提交
132 133 134 135 136 137 138 139
//     if (metaDecodeEntry(&pReader->coder, &pReader->me) < 0) {
//     }
//     metaReaderClear(pReader);
//   }
//   int64_t stt5 = taosGetTimestampUs();
//   qDebug("metaGetTableEntryByUidTest4 rows:%d, cost:%ld us", taosArrayGetSize(readerList), stt5-stt4);
//   return 0;
// }
wmmhello's avatar
wmmhello 已提交
140

H
Hongze Cheng 已提交
141
bool metaIsTableExist(SMeta *pMeta, tb_uid_t uid) {
142
  // query uid.idx
143 144
  metaRLock(pMeta);

145
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), NULL, NULL) < 0) {
146 147
    metaULock(pMeta);

148 149 150
    return false;
  }

151 152
  metaULock(pMeta);

153 154 155
  return true;
}

H
Hongze Cheng 已提交
156
int metaGetTableEntryByUid(SMetaReader *pReader, tb_uid_t uid) {
157
  SMeta  *pMeta = pReader->pMeta;
158
  int64_t version1;
H
Hongze Cheng 已提交
159 160

  // query uid.idx
H
Hongze Cheng 已提交
161
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pReader->pBuf, &pReader->szBuf) < 0) {
H
Hongze Cheng 已提交
162
    terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
H
Hongze Cheng 已提交
163 164 165
    return -1;
  }

166 167
  version1 = ((SUidIdxVal *)pReader->pBuf)[0].version;
  return metaGetTableEntryByVersion(pReader, version1, uid);
H
Hongze Cheng 已提交
168 169
}

H
Haojun Liao 已提交
170 171
int metaGetTableEntryByUidCache(SMetaReader *pReader, tb_uid_t uid) {
  SMeta *pMeta = pReader->pMeta;
172 173 174 175 176 177 178 179

  SMetaInfo info;
  if (metaGetInfo(pMeta, uid, &info, pReader) == TSDB_CODE_NOT_FOUND) {
    terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
    return -1;
  }

  return metaGetTableEntryByVersion(pReader, info.version, uid);
H
Hongze Cheng 已提交
180 181
}

H
Hongze Cheng 已提交
182
int metaGetTableEntryByName(SMetaReader *pReader, const char *name) {
H
Hongze Cheng 已提交
183
  SMeta   *pMeta = pReader->pMeta;
H
Hongze Cheng 已提交
184 185 186
  tb_uid_t uid;

  // query name.idx
H
Hongze Cheng 已提交
187
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
H
Hongze Cheng 已提交
188
    terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
H
Hongze Cheng 已提交
189 190 191 192
    return -1;
  }

  uid = *(tb_uid_t *)pReader->pBuf;
H
Hongze Cheng 已提交
193
  return metaGetTableEntryByUid(pReader, uid);
H
Hongze Cheng 已提交
194 195
}

H
Hongze Cheng 已提交
196
tb_uid_t metaGetTableEntryUidByName(SMeta *pMeta, const char *name) {
H
Hongze Cheng 已提交
197
  void    *pData = NULL;
H
Hongze Cheng 已提交
198 199 200
  int      nData = 0;
  tb_uid_t uid = 0;

wmmhello's avatar
wmmhello 已提交
201 202
  metaRLock(pMeta);

H
Hongze Cheng 已提交
203 204 205 206 207
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pData, &nData) == 0) {
    uid = *(tb_uid_t *)pData;
    tdbFree(pData);
  }

wmmhello's avatar
wmmhello 已提交
208 209
  metaULock(pMeta);

210
  return uid;
H
Hongze Cheng 已提交
211 212
}

dengyihao's avatar
dengyihao 已提交
213
int metaGetTableNameByUid(void *meta, uint64_t uid, char *tbName) {
214
  int         code = 0;
D
dapan1121 已提交
215
  SMetaReader mr = {0};
dengyihao's avatar
dengyihao 已提交
216
  metaReaderInit(&mr, (SMeta *)meta, 0);
217 218 219 220 221
  code = metaGetTableEntryByUid(&mr, uid);
  if (code < 0) {
    metaReaderClear(&mr);
    return -1;
  }
D
dapan1121 已提交
222 223 224 225 226 227

  STR_TO_VARSTR(tbName, mr.me.name);
  metaReaderClear(&mr);

  return 0;
}
S
slzhou 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

int metaGetTableSzNameByUid(void *meta, uint64_t uid, char *tbName) {
  int         code = 0;
  SMetaReader mr = {0};
  metaReaderInit(&mr, (SMeta *)meta, 0);
  code = metaGetTableEntryByUid(&mr, uid);
  if (code < 0) {
    metaReaderClear(&mr);
    return -1;
  }
  strncpy(tbName, mr.me.name, TSDB_TABLE_NAME_LEN);
  metaReaderClear(&mr);

  return 0;
}

H
Haojun Liao 已提交
244
int metaGetTableUidByName(void *meta, char *tbName, uint64_t *uid) {
dengyihao's avatar
dengyihao 已提交
245 246 247
  int         code = 0;
  SMetaReader mr = {0};
  metaReaderInit(&mr, (SMeta *)meta, 0);
dengyihao's avatar
dengyihao 已提交
248 249 250 251

  SMetaReader *pReader = &mr;

  // query name.idx
dengyihao's avatar
dengyihao 已提交
252
  if (tdbTbGet(pReader->pMeta->pNameIdx, tbName, strlen(tbName) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
dengyihao's avatar
dengyihao 已提交
253
    terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
dengyihao's avatar
dengyihao 已提交
254 255 256 257
    metaReaderClear(&mr);
    return -1;
  }

dengyihao's avatar
dengyihao 已提交
258
  *uid = *(tb_uid_t *)pReader->pBuf;
dengyihao's avatar
dengyihao 已提交
259

dengyihao's avatar
dengyihao 已提交
260 261 262 263
  metaReaderClear(&mr);

  return 0;
}
D
dapan1121 已提交
264

dengyihao's avatar
dengyihao 已提交
265 266 267 268 269
int metaGetTableTypeByName(void *meta, char *tbName, ETableType *tbType) {
  int         code = 0;
  SMetaReader mr = {0};
  metaReaderInit(&mr, (SMeta *)meta, 0);

dengyihao's avatar
dengyihao 已提交
270 271 272
  code = metaGetTableEntryByName(&mr, tbName);
  if (code == 0) *tbType = mr.me.type;

dengyihao's avatar
dengyihao 已提交
273
  metaReaderClear(&mr);
dengyihao's avatar
dengyihao 已提交
274
  return code;
dengyihao's avatar
dengyihao 已提交
275 276
}

H
Hongze Cheng 已提交
277
int metaReadNext(SMetaReader *pReader) {
H
Hongze Cheng 已提交
278 279
  SMeta *pMeta = pReader->pMeta;

H
Hongze Cheng 已提交
280
  // TODO
H
Hongze Cheng 已提交
281

H
Hongze Cheng 已提交
282 283 284 285
  return 0;
}

#if 1  // ===================================================
H
Hongze Cheng 已提交
286 287 288 289 290 291 292 293
SMTbCursor *metaOpenTbCursor(SMeta *pMeta) {
  SMTbCursor *pTbCur = NULL;

  pTbCur = (SMTbCursor *)taosMemoryCalloc(1, sizeof(*pTbCur));
  if (pTbCur == NULL) {
    return NULL;
  }

H
Hongze Cheng 已提交
294
  metaReaderInit(&pTbCur->mr, pMeta, 0);
H
Hongze Cheng 已提交
295

H
Hongze Cheng 已提交
296
  tdbTbcOpen(pMeta->pUidIdx, &pTbCur->pDbc, NULL);
H
Hongze Cheng 已提交
297

H
Hongze Cheng 已提交
298
  tdbTbcMoveToFirst(pTbCur->pDbc);
H
Hongze Cheng 已提交
299

H
Hongze Cheng 已提交
300 301 302 303 304
  return pTbCur;
}

void metaCloseTbCursor(SMTbCursor *pTbCur) {
  if (pTbCur) {
H
Hongze Cheng 已提交
305 306
    tdbFree(pTbCur->pKey);
    tdbFree(pTbCur->pVal);
H
Hongze Cheng 已提交
307
    metaReaderClear(&pTbCur->mr);
H
Hongze Cheng 已提交
308
    if (pTbCur->pDbc) {
H
Hongze Cheng 已提交
309
      tdbTbcClose(pTbCur->pDbc);
H
Hongze Cheng 已提交
310 311 312 313 314
    }
    taosMemoryFree(pTbCur);
  }
}

X
Xiaoyu Wang 已提交
315
int32_t metaTbCursorNext(SMTbCursor *pTbCur, ETableType jumpTableType) {
H
Hongze Cheng 已提交
316
  int    ret;
H
Hongze Cheng 已提交
317
  void  *pBuf;
H
Hongze Cheng 已提交
318 319 320
  STbCfg tbCfg;

  for (;;) {
H
Hongze Cheng 已提交
321
    ret = tdbTbcNext(pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
H
Hongze Cheng 已提交
322 323
    if (ret < 0) {
      return -1;
H
Hongze Cheng 已提交
324 325
    }

M
Minglei Jin 已提交
326 327
    tDecoderClear(&pTbCur->mr.coder);

H
Hongze Cheng 已提交
328
    metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
wmmhello's avatar
wmmhello 已提交
329
    if (pTbCur->mr.me.type == jumpTableType) {
H
Hongze Cheng 已提交
330 331
      continue;
    }
H
Hongze Cheng 已提交
332 333

    break;
H
Hongze Cheng 已提交
334 335
  }

H
Hongze Cheng 已提交
336
  return 0;
H
Hongze Cheng 已提交
337 338
}

D
dapan1121 已提交
339
int32_t metaTbCursorPrev(SMTbCursor *pTbCur, ETableType jumpTableType) {
340 341 342 343 344 345 346 347 348 349 350 351 352
  int    ret;
  void  *pBuf;
  STbCfg tbCfg;

  for (;;) {
    ret = tdbTbcPrev(pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
    if (ret < 0) {
      return -1;
    }

    tDecoderClear(&pTbCur->mr.coder);

    metaGetTableEntryByVersion(&pTbCur->mr, ((SUidIdxVal *)pTbCur->pVal)[0].version, *(tb_uid_t *)pTbCur->pKey);
D
dapan1121 已提交
353
    if (pTbCur->mr.me.type == jumpTableType) {
354 355 356 357 358 359 360 361 362
      continue;
    }

    break;
  }

  return 0;
}

363
SSchemaWrapper *metaGetTableSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
H
Hongze Cheng 已提交
364
  void           *pData = NULL;
H
Hongze Cheng 已提交
365 366 367 368 369
  int             nData = 0;
  int64_t         version;
  SSchemaWrapper  schema = {0};
  SSchemaWrapper *pSchema = NULL;
  SDecoder        dc = {0};
370 371 372
  if (lock) {
    metaRLock(pMeta);
  }
H
Hongze Cheng 已提交
373 374 375 376
_query:
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
    goto _err;
  }
H
Hongze Cheng 已提交
377

H
Hongze Cheng 已提交
378
  version = ((SUidIdxVal *)pData)[0].version;
H
Hongze Cheng 已提交
379

H
Hongze Cheng 已提交
380 381 382 383 384 385
  tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData);
  SMetaEntry me = {0};
  tDecoderInit(&dc, pData, nData);
  metaDecodeEntry(&dc, &me);
  if (me.type == TSDB_SUPER_TABLE) {
    if (sver == -1 || sver == me.stbEntry.schemaRow.version) {
H
Hongze Cheng 已提交
386
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
H
Hongze Cheng 已提交
387 388
      tDecoderClear(&dc);
      goto _exit;
H
Hongze Cheng 已提交
389
    }
H
Hongze Cheng 已提交
390 391
  } else if (me.type == TSDB_CHILD_TABLE) {
    uid = me.ctbEntry.suid;
H
Hongze Cheng 已提交
392
    tDecoderClear(&dc);
H
Hongze Cheng 已提交
393
    goto _query;
H
Hongze Cheng 已提交
394
  } else {
H
Hongze Cheng 已提交
395 396 397 398
    if (sver == -1 || sver == me.ntbEntry.schemaRow.version) {
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
      tDecoderClear(&dc);
      goto _exit;
H
Hongze Cheng 已提交
399
    }
H
Hongze Cheng 已提交
400 401
  }
  tDecoderClear(&dc);
H
Hongze Cheng 已提交
402

H
Hongze Cheng 已提交
403 404 405
  // query from skm db
  if (tdbTbGet(pMeta->pSkmDb, &(SSkmDbKey){.uid = uid, .sver = sver}, sizeof(SSkmDbKey), &pData, &nData) < 0) {
    goto _err;
H
Hongze Cheng 已提交
406
  }
H
Hongze Cheng 已提交
407

H
Hongze Cheng 已提交
408
  tDecoderInit(&dc, pData, nData);
M
Minglei Jin 已提交
409
  tDecodeSSchemaWrapperEx(&dc, &schema);
H
Hongze Cheng 已提交
410 411 412 413
  pSchema = tCloneSSchemaWrapper(&schema);
  tDecoderClear(&dc);

_exit:
414 415 416
  if (lock) {
    metaULock(pMeta);
  }
H
Hongze Cheng 已提交
417 418
  tdbFree(pData);
  return pSchema;
H
Hongze Cheng 已提交
419

H
Hongze Cheng 已提交
420
_err:
421 422 423
  if (lock) {
    metaULock(pMeta);
  }
H
Hongze Cheng 已提交
424 425
  tdbFree(pData);
  return NULL;
H
Hongze Cheng 已提交
426 427
}

H
Hongze Cheng 已提交
428 429 430
int metaTtlSmaller(SMeta *pMeta, uint64_t ttl, SArray *uidList) {
  TBC *pCur;
  int  ret = tdbTbcOpen(pMeta->pTtlIdx, &pCur, NULL);
431 432 433 434 435 436 437 438 439 440 441 442 443 444
  if (ret < 0) {
    return ret;
  }

  STtlIdxKey ttlKey = {0};
  ttlKey.dtime = ttl;
  ttlKey.uid = INT64_MAX;
  int c = 0;
  tdbTbcMoveTo(pCur, &ttlKey, sizeof(ttlKey), &c);
  if (c < 0) {
    tdbTbcMoveToPrev(pCur);
  }

  void *pKey = NULL;
H
Hongze Cheng 已提交
445 446
  int   kLen = 0;
  while (1) {
447 448 449 450
    ret = tdbTbcPrev(pCur, &pKey, &kLen, NULL, NULL);
    if (ret < 0) {
      break;
    }
H
Hongze Cheng 已提交
451
    ttlKey = *(STtlIdxKey *)pKey;
452 453
    taosArrayPush(uidList, &ttlKey.uid);
  }
wmmhello's avatar
wmmhello 已提交
454
  tdbFree(pKey);
455 456 457 458
  tdbTbcClose(pCur);
  return 0;
}

H
Hongze Cheng 已提交
459
struct SMCtbCursor {
H
Hongze Cheng 已提交
460 461
  SMeta   *pMeta;
  TBC     *pCur;
H
Hongze Cheng 已提交
462
  tb_uid_t suid;
H
Hongze Cheng 已提交
463 464
  void    *pKey;
  void    *pVal;
H
Hongze Cheng 已提交
465 466 467 468
  int      kLen;
  int      vLen;
};

469
SMCtbCursor *metaOpenCtbCursor(SMeta *pMeta, tb_uid_t uid, int lock) {
H
Hongze Cheng 已提交
470
  SMCtbCursor *pCtbCur = NULL;
H
Hongze Cheng 已提交
471
  SCtbIdxKey   ctbIdxKey;
C
Cary Xu 已提交
472 473
  int          ret = 0;
  int          c = 0;
H
Hongze Cheng 已提交
474

H
Hongze Cheng 已提交
475 476 477 478
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
  if (pCtbCur == NULL) {
    return NULL;
  }
H
Hongze Cheng 已提交
479

H
fix  
Hongze Cheng 已提交
480
  pCtbCur->pMeta = pMeta;
H
Hongze Cheng 已提交
481
  pCtbCur->suid = uid;
482 483 484
  if (lock) {
    metaRLock(pMeta);
  }
H
fix  
Hongze Cheng 已提交
485

H
Hongze Cheng 已提交
486
  ret = tdbTbcOpen(pMeta->pCtbIdx, &pCtbCur->pCur, NULL);
H
Hongze Cheng 已提交
487
  if (ret < 0) {
H
fix  
Hongze Cheng 已提交
488
    metaULock(pMeta);
H
Hongze Cheng 已提交
489 490 491
    taosMemoryFree(pCtbCur);
    return NULL;
  }
H
Hongze Cheng 已提交
492

H
Hongze Cheng 已提交
493 494 495
  // move to the suid
  ctbIdxKey.suid = uid;
  ctbIdxKey.uid = INT64_MIN;
H
Hongze Cheng 已提交
496
  tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
H
Hongze Cheng 已提交
497
  if (c > 0) {
H
Hongze Cheng 已提交
498
    tdbTbcMoveToNext(pCtbCur->pCur);
H
Hongze Cheng 已提交
499 500
  }

H
Hongze Cheng 已提交
501 502 503
  return pCtbCur;
}

504
void metaCloseCtbCursor(SMCtbCursor *pCtbCur, int lock) {
H
Hongze Cheng 已提交
505
  if (pCtbCur) {
506
    if (pCtbCur->pMeta && lock) metaULock(pCtbCur->pMeta);
H
Hongze Cheng 已提交
507
    if (pCtbCur->pCur) {
H
Hongze Cheng 已提交
508
      tdbTbcClose(pCtbCur->pCur);
H
Hongze Cheng 已提交
509

H
Hongze Cheng 已提交
510 511
      tdbFree(pCtbCur->pKey);
      tdbFree(pCtbCur->pVal);
H
Hongze Cheng 已提交
512
    }
H
Hongze Cheng 已提交
513

H
Hongze Cheng 已提交
514 515
    taosMemoryFree(pCtbCur);
  }
H
Hongze Cheng 已提交
516 517 518
}

tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
H
Hongze Cheng 已提交
519 520
  int         ret;
  SCtbIdxKey *pCtbIdxKey;
H
Hongze Cheng 已提交
521

H
Hongze Cheng 已提交
522
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
H
Hongze Cheng 已提交
523 524 525
  if (ret < 0) {
    return 0;
  }
H
Hongze Cheng 已提交
526

H
Hongze Cheng 已提交
527
  pCtbIdxKey = pCtbCur->pKey;
H
Hongze Cheng 已提交
528 529 530
  if (pCtbIdxKey->suid > pCtbCur->suid) {
    return 0;
  }
H
Hongze Cheng 已提交
531

H
Hongze Cheng 已提交
532
  return pCtbIdxKey->uid;
H
Hongze Cheng 已提交
533 534
}

C
Cary Xu 已提交
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
struct SMStbCursor {
  SMeta   *pMeta;
  TBC     *pCur;
  tb_uid_t suid;
  void    *pKey;
  void    *pVal;
  int      kLen;
  int      vLen;
};

SMStbCursor *metaOpenStbCursor(SMeta *pMeta, tb_uid_t suid) {
  SMStbCursor *pStbCur = NULL;
  int          ret = 0;
  int          c = 0;

  pStbCur = (SMStbCursor *)taosMemoryCalloc(1, sizeof(*pStbCur));
  if (pStbCur == NULL) {
C
Cary Xu 已提交
552
    terrno = TSDB_CODE_OUT_OF_MEMORY;
C
Cary Xu 已提交
553 554 555 556 557 558 559 560 561
    return NULL;
  }

  pStbCur->pMeta = pMeta;
  pStbCur->suid = suid;
  metaRLock(pMeta);

  ret = tdbTbcOpen(pMeta->pSuidIdx, &pStbCur->pCur, NULL);
  if (ret < 0) {
C
Cary Xu 已提交
562
    terrno = TSDB_CODE_OUT_OF_MEMORY;
C
Cary Xu 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
    metaULock(pMeta);
    taosMemoryFree(pStbCur);
    return NULL;
  }

  // move to the suid
  tdbTbcMoveTo(pStbCur->pCur, &suid, sizeof(suid), &c);
  if (c > 0) {
    tdbTbcMoveToNext(pStbCur->pCur);
  }

  return pStbCur;
}

void metaCloseStbCursor(SMStbCursor *pStbCur) {
  if (pStbCur) {
    if (pStbCur->pMeta) metaULock(pStbCur->pMeta);
    if (pStbCur->pCur) {
      tdbTbcClose(pStbCur->pCur);

      tdbFree(pStbCur->pKey);
      tdbFree(pStbCur->pVal);
    }

    taosMemoryFree(pStbCur);
  }
}

tb_uid_t metaStbCursorNext(SMStbCursor *pStbCur) {
  int ret;

  ret = tdbTbcNext(pStbCur->pCur, &pStbCur->pKey, &pStbCur->kLen, &pStbCur->pVal, &pStbCur->vLen);
  if (ret < 0) {
    return 0;
  }
H
Hongze Cheng 已提交
598
  return *(tb_uid_t *)pStbCur->pKey;
C
Cary Xu 已提交
599 600
}

601
STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) {
H
Hongze Cheng 已提交
602
  STSchema       *pTSchema = NULL;
H
Hongze Cheng 已提交
603
  SSchemaWrapper *pSW = NULL;
H
Hongze Cheng 已提交
604

605
  pSW = metaGetTableSchema(pMeta, uid, sver, lock);
C
Cary Xu 已提交
606
  if (!pSW) return NULL;
H
fix  
Hongze Cheng 已提交
607

H
Hongze Cheng 已提交
608
  pTSchema = tBuildTSchema(pSW->pSchema, pSW->nCols, pSW->version);
H
Hongze Cheng 已提交
609

H
Hongze Cheng 已提交
610 611
  taosMemoryFree(pSW->pSchema);
  taosMemoryFree(pSW);
H
Hongze Cheng 已提交
612 613 614
  return pTSchema;
}

H
Hongze Cheng 已提交
615
int32_t metaGetTbTSchemaEx(SMeta *pMeta, tb_uid_t suid, tb_uid_t uid, int32_t sver, STSchema **ppTSchema) {
H
Hongze Cheng 已提交
616 617
  int32_t code = 0;

H
Hongze Cheng 已提交
618 619
  void     *pData = NULL;
  int       nData = 0;
H
Hongze Cheng 已提交
620 621 622
  SSkmDbKey skmDbKey;
  if (sver <= 0) {
    SMetaInfo info;
623
    if (metaGetInfo(pMeta, suid ? suid : uid, &info, NULL) == 0) {
H
Hongze Cheng 已提交
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
      sver = info.skmVer;
    } else {
      TBC *pSkmDbC = NULL;
      int  c;

      skmDbKey.uid = suid ? suid : uid;
      skmDbKey.sver = INT32_MAX;

      tdbTbcOpen(pMeta->pSkmDb, &pSkmDbC, NULL);
      metaRLock(pMeta);

      if (tdbTbcMoveTo(pSkmDbC, &skmDbKey, sizeof(skmDbKey), &c) < 0) {
        metaULock(pMeta);
        tdbTbcClose(pSkmDbC);
        code = TSDB_CODE_NOT_FOUND;
        goto _exit;
      }

642
      if (c == 0) {
643 644
        metaULock(pMeta);
        tdbTbcClose(pSkmDbC);
645
        code = TSDB_CODE_FAILED;
646
        metaError("meta/query: incorrect c: %" PRId32 ".", c);
647 648
        goto _exit;
      }
H
Hongze Cheng 已提交
649 650 651 652 653 654 655 656 657

      if (c < 0) {
        tdbTbcMoveToPrev(pSkmDbC);
      }

      const void *pKey = NULL;
      int32_t     nKey = 0;
      tdbTbcGet(pSkmDbC, &pKey, &nKey, NULL, NULL);

H
Hongze Cheng 已提交
658
      if (((SSkmDbKey *)pKey)->uid != skmDbKey.uid) {
H
Hongze Cheng 已提交
659 660 661 662 663 664 665 666 667 668 669 670
        metaULock(pMeta);
        tdbTbcClose(pSkmDbC);
        code = TSDB_CODE_NOT_FOUND;
        goto _exit;
      }

      sver = ((SSkmDbKey *)pKey)->sver;

      metaULock(pMeta);
      tdbTbcClose(pSkmDbC);
    }
  }
H
Hongze Cheng 已提交
671

672
  if (ASSERTS(sver > 0, "failed to get table schema version: %d", sver)) {
H
Hongze Cheng 已提交
673
    code = TSDB_CODE_NOT_FOUND;
674 675
    goto _exit;
  }
H
Hongze Cheng 已提交
676 677 678

  skmDbKey.uid = suid ? suid : uid;
  skmDbKey.sver = sver;
H
Hongze Cheng 已提交
679
  metaRLock(pMeta);
H
Hongze Cheng 已提交
680
  if (tdbTbGet(pMeta->pSkmDb, &skmDbKey, sizeof(SSkmDbKey), &pData, &nData) < 0) {
H
Hongze Cheng 已提交
681
    metaULock(pMeta);
H
Hongze Cheng 已提交
682 683
    code = TSDB_CODE_NOT_FOUND;
    goto _exit;
H
Hongze Cheng 已提交
684 685 686 687 688 689 690 691 692
  }
  metaULock(pMeta);

  // decode
  SDecoder        dc = {0};
  SSchemaWrapper  schema;
  SSchemaWrapper *pSchemaWrapper = &schema;

  tDecoderInit(&dc, pData, nData);
M
Minglei Jin 已提交
693
  (void)tDecodeSSchemaWrapper(&dc, pSchemaWrapper);
H
Hongze Cheng 已提交
694
  tDecoderClear(&dc);
695
  tdbFree(pData);
H
Hongze Cheng 已提交
696 697

  // convert
H
Hongze Cheng 已提交
698
  STSchema *pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
H
Haojun Liao 已提交
699 700 701 702
  if (pTSchema == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
  }

H
Hongze Cheng 已提交
703 704 705
  *ppTSchema = pTSchema;
  taosMemoryFree(pSchemaWrapper->pSchema);

H
Hongze Cheng 已提交
706
_exit:
H
Hongze Cheng 已提交
707 708 709
  return code;
}

710 711
// N.B. Called by statusReq per second
int64_t metaGetTbNum(SMeta *pMeta) {
712 713 714 715 716
  // num of child tables (excluding normal tables , stables and others)

  /* int64_t num = 0; */
  /* vnodeGetAllCtbNum(pMeta->pVnode, &num); */

717
  return pMeta->pVnode->config.vndStats.numOfCTables + pMeta->pVnode->config.vndStats.numOfNTables;
718 719 720
}

// N.B. Called by statusReq per second
M
Minglei Jin 已提交
721
int64_t metaGetTimeSeriesNum(SMeta *pMeta) {
722
  // sum of (number of columns of stable -  1) * number of ctables (excluding timestamp column)
723 724
  if (pMeta->pVnode->config.vndStats.numOfTimeSeries <= 0 ||
      ++pMeta->pVnode->config.vndStats.itvTimeSeries % (60 * 5) == 0) {
725 726 727 728
    int64_t num = 0;
    vnodeGetTimeSeriesNum(pMeta->pVnode, &num);
    pMeta->pVnode->config.vndStats.numOfTimeSeries = num;

729
    pMeta->pVnode->config.vndStats.itvTimeSeries = (TD_VID(pMeta->pVnode) % 100) * 2;
730
  }
731

732
  return pMeta->pVnode->config.vndStats.numOfTimeSeries + pMeta->pVnode->config.vndStats.numOfNTimeSeries;
C
Cary Xu 已提交
733
}
H
Hongze Cheng 已提交
734

735
int64_t metaGetNtbNum(SMeta *pMeta) { return pMeta->pVnode->config.vndStats.numOfNTables; }
736

C
Cary Xu 已提交
737
typedef struct {
H
Hongze Cheng 已提交
738 739
  SMeta   *pMeta;
  TBC     *pCur;
C
Cary Xu 已提交
740
  tb_uid_t uid;
H
Hongze Cheng 已提交
741 742
  void    *pKey;
  void    *pVal;
C
Cary Xu 已提交
743 744 745 746 747 748 749 750 751 752 753 754 755
  int      kLen;
  int      vLen;
} SMSmaCursor;

SMSmaCursor *metaOpenSmaCursor(SMeta *pMeta, tb_uid_t uid) {
  SMSmaCursor *pSmaCur = NULL;
  SSmaIdxKey   smaIdxKey;
  int          ret;
  int          c;

  pSmaCur = (SMSmaCursor *)taosMemoryCalloc(1, sizeof(*pSmaCur));
  if (pSmaCur == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
756 757 758
    return NULL;
  }

C
Cary Xu 已提交
759 760 761 762
  pSmaCur->pMeta = pMeta;
  pSmaCur->uid = uid;
  metaRLock(pMeta);

H
Hongze Cheng 已提交
763
  ret = tdbTbcOpen(pMeta->pSmaIdx, &pSmaCur->pCur, NULL);
C
Cary Xu 已提交
764 765 766
  if (ret < 0) {
    metaULock(pMeta);
    taosMemoryFree(pSmaCur);
H
Hongze Cheng 已提交
767 768 769
    return NULL;
  }

C
Cary Xu 已提交
770 771 772
  // move to the suid
  smaIdxKey.uid = uid;
  smaIdxKey.smaUid = INT64_MIN;
H
Hongze Cheng 已提交
773
  tdbTbcMoveTo(pSmaCur->pCur, &smaIdxKey, sizeof(smaIdxKey), &c);
C
Cary Xu 已提交
774
  if (c > 0) {
H
Hongze Cheng 已提交
775
    tdbTbcMoveToNext(pSmaCur->pCur);
C
Cary Xu 已提交
776
  }
H
Hongze Cheng 已提交
777

C
Cary Xu 已提交
778 779
  return pSmaCur;
}
H
Hongze Cheng 已提交
780

C
Cary Xu 已提交
781 782 783 784
void metaCloseSmaCursor(SMSmaCursor *pSmaCur) {
  if (pSmaCur) {
    if (pSmaCur->pMeta) metaULock(pSmaCur->pMeta);
    if (pSmaCur->pCur) {
H
Hongze Cheng 已提交
785
      tdbTbcClose(pSmaCur->pCur);
H
Hongze Cheng 已提交
786

C
Cary Xu 已提交
787 788 789
      tdbFree(pSmaCur->pKey);
      tdbFree(pSmaCur->pVal);
    }
H
Hongze Cheng 已提交
790

C
Cary Xu 已提交
791 792 793 794 795 796 797 798
    taosMemoryFree(pSmaCur);
  }
}

tb_uid_t metaSmaCursorNext(SMSmaCursor *pSmaCur) {
  int         ret;
  SSmaIdxKey *pSmaIdxKey;

H
Hongze Cheng 已提交
799
  ret = tdbTbcNext(pSmaCur->pCur, &pSmaCur->pKey, &pSmaCur->kLen, &pSmaCur->pVal, &pSmaCur->vLen);
C
Cary Xu 已提交
800 801 802 803 804 805 806 807 808 809 810 811 812 813
  if (ret < 0) {
    return 0;
  }

  pSmaIdxKey = pSmaCur->pKey;
  if (pSmaIdxKey->uid > pSmaCur->uid) {
    return 0;
  }

  return pSmaIdxKey->uid;
}

STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid, bool deepCopy) {
  STSmaWrapper *pSW = NULL;
H
Hongze Cheng 已提交
814
  SArray       *pSmaIds = NULL;
C
Cary Xu 已提交
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837

  if (!(pSmaIds = metaGetSmaIdsByTable(pMeta, uid))) {
    return NULL;
  }

  pSW = taosMemoryCalloc(1, sizeof(*pSW));
  if (!pSW) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

  pSW->number = taosArrayGetSize(pSmaIds);
  pSW->tSma = taosMemoryCalloc(pSW->number, sizeof(STSma));

  if (!pSW->tSma) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

  SMetaReader mr = {0};
  metaReaderInit(&mr, pMeta, 0);
  int64_t smaId;
  int     smaIdx = 0;
H
Hongze Cheng 已提交
838
  STSma  *pTSma = NULL;
C
Cary Xu 已提交
839 840 841
  for (int i = 0; i < pSW->number; ++i) {
    smaId = *(tb_uid_t *)taosArrayGet(pSmaIds, i);
    if (metaGetTableEntryByUid(&mr, smaId) < 0) {
842
      tDecoderClear(&mr.coder);
S
Shengliang Guan 已提交
843
      metaWarn("vgId:%d, no entry for tbId:%" PRIi64 ", smaId:%" PRIi64, TD_VID(pMeta->pVnode), uid, smaId);
C
Cary Xu 已提交
844 845
      continue;
    }
846
    tDecoderClear(&mr.coder);
C
Cary Xu 已提交
847 848 849 850 851 852 853 854
    pTSma = pSW->tSma + smaIdx;
    memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));
    if (deepCopy) {
      if (pTSma->exprLen > 0) {
        if (!(pTSma->expr = taosMemoryCalloc(1, pTSma->exprLen))) {
          terrno = TSDB_CODE_OUT_OF_MEMORY;
          goto _err;
        }
H
Hongze Cheng 已提交
855
        memcpy((void *)pTSma->expr, mr.me.smaEntry.tsma->expr, pTSma->exprLen);
H
Hongze Cheng 已提交
856
      }
C
Cary Xu 已提交
857 858 859 860 861
      if (pTSma->tagsFilterLen > 0) {
        if (!(pTSma->tagsFilter = taosMemoryCalloc(1, pTSma->tagsFilterLen))) {
          terrno = TSDB_CODE_OUT_OF_MEMORY;
          goto _err;
        }
H
Hongze Cheng 已提交
862
      }
H
Hongze Cheng 已提交
863
      memcpy((void *)pTSma->tagsFilter, mr.me.smaEntry.tsma->tagsFilter, pTSma->tagsFilterLen);
C
Cary Xu 已提交
864 865 866 867 868
    } else {
      pTSma->exprLen = 0;
      pTSma->expr = NULL;
      pTSma->tagsFilterLen = 0;
      pTSma->tagsFilter = NULL;
H
Hongze Cheng 已提交
869
    }
H
Hongze Cheng 已提交
870

C
Cary Xu 已提交
871
    ++smaIdx;
H
Hongze Cheng 已提交
872 873
  }

C
Cary Xu 已提交
874 875
  if (smaIdx <= 0) goto _err;
  pSW->number = smaIdx;
H
Hongze Cheng 已提交
876

C
Cary Xu 已提交
877 878
  metaReaderClear(&mr);
  taosArrayDestroy(pSmaIds);
H
Hongze Cheng 已提交
879
  return pSW;
C
Cary Xu 已提交
880 881 882
_err:
  metaReaderClear(&mr);
  taosArrayDestroy(pSmaIds);
C
Cary Xu 已提交
883
  tFreeTSmaWrapper(pSW, deepCopy);
H
Hongze Cheng 已提交
884 885 886
  return NULL;
}

C
Cary Xu 已提交
887
STSma *metaGetSmaInfoByIndex(SMeta *pMeta, int64_t indexUid) {
H
Hongze Cheng 已提交
888
  STSma      *pTSma = NULL;
C
Cary Xu 已提交
889 890 891
  SMetaReader mr = {0};
  metaReaderInit(&mr, pMeta, 0);
  if (metaGetTableEntryByUid(&mr, indexUid) < 0) {
S
Shengliang Guan 已提交
892
    metaWarn("vgId:%d, failed to get table entry for smaId:%" PRIi64, TD_VID(pMeta->pVnode), indexUid);
C
Cary Xu 已提交
893 894 895 896 897 898 899 900 901 902 903 904 905 906
    metaReaderClear(&mr);
    return NULL;
  }
  pTSma = (STSma *)taosMemoryMalloc(sizeof(STSma));
  if (!pTSma) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    metaReaderClear(&mr);
    return NULL;
  }

  memcpy(pTSma, mr.me.smaEntry.tsma, sizeof(STSma));

  metaReaderClear(&mr);
  return pTSma;
H
Hongze Cheng 已提交
907 908
}

C
Cary Xu 已提交
909
SArray *metaGetSmaIdsByTable(SMeta *pMeta, tb_uid_t uid) {
H
Hongze Cheng 已提交
910
  SArray     *pUids = NULL;
C
Cary Xu 已提交
911
  SSmaIdxKey *pSmaIdxKey = NULL;
H
Hongze Cheng 已提交
912

C
Cary Xu 已提交
913 914
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, uid);
  if (!pCur) {
H
Hongze Cheng 已提交
915 916 917
    return NULL;
  }

C
Cary Xu 已提交
918 919 920 921 922
  while (1) {
    tb_uid_t id = metaSmaCursorNext(pCur);
    if (id == 0) {
      break;
    }
H
Hongze Cheng 已提交
923

C
Cary Xu 已提交
924 925
    if (!pUids) {
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
H
Hongze Cheng 已提交
926
      if (!pUids) {
C
Cary Xu 已提交
927 928 929
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        metaCloseSmaCursor(pCur);
        return NULL;
H
Hongze Cheng 已提交
930
      }
C
Cary Xu 已提交
931
    }
H
Hongze Cheng 已提交
932

C
Cary Xu 已提交
933
    pSmaIdxKey = (SSmaIdxKey *)pCur->pKey;
H
Hongze Cheng 已提交
934

C
Cary Xu 已提交
935
    if (!taosArrayPush(pUids, &pSmaIdxKey->smaUid)) {
C
Cary Xu 已提交
936 937 938 939
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      metaCloseSmaCursor(pCur);
      taosArrayDestroy(pUids);
      return NULL;
H
Hongze Cheng 已提交
940 941 942 943 944 945 946
    }
  }

  metaCloseSmaCursor(pCur);
  return pUids;
}

C
Cary Xu 已提交
947
SArray *metaGetSmaTbUids(SMeta *pMeta) {
H
Hongze Cheng 已提交
948
  SArray     *pUids = NULL;
C
Cary Xu 已提交
949 950 951 952 953
  SSmaIdxKey *pSmaIdxKey = NULL;
  tb_uid_t    lastUid = 0;

  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, 0);
  if (!pCur) {
H
Hongze Cheng 已提交
954 955 956
    return NULL;
  }

C
Cary Xu 已提交
957 958 959 960 961
  while (1) {
    tb_uid_t uid = metaSmaCursorNext(pCur);
    if (uid == 0) {
      break;
    }
H
Hongze Cheng 已提交
962

C
Cary Xu 已提交
963 964 965
    if (lastUid == uid) {
      continue;
    }
H
Hongze Cheng 已提交
966

C
Cary Xu 已提交
967 968 969 970 971 972 973 974 975 976 977
    lastUid = uid;

    if (!pUids) {
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
      if (!pUids) {
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        metaCloseSmaCursor(pCur);
        return NULL;
      }
    }

C
Cary Xu 已提交
978
    if (!taosArrayPush(pUids, &uid)) {
C
Cary Xu 已提交
979 980 981 982 983
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      metaCloseSmaCursor(pCur);
      taosArrayDestroy(pUids);
      return NULL;
    }
H
Hongze Cheng 已提交
984 985
  }

C
Cary Xu 已提交
986 987
  metaCloseSmaCursor(pCur);
  return pUids;
H
Hongze Cheng 已提交
988 989
}

L
Liu Jicong 已提交
990
#endif
H
Hongze Cheng 已提交
991

992
const void *metaGetTableTagVal(void *pTag, int16_t type, STagVal *val) {
dengyihao's avatar
dengyihao 已提交
993
  STag *tag = (STag *)pTag;
dengyihao's avatar
dengyihao 已提交
994
  if (type == TSDB_DATA_TYPE_JSON) {
wmmhello's avatar
wmmhello 已提交
995 996
    return tag;
  }
wmmhello's avatar
wmmhello 已提交
997 998
  bool find = tTagGet(tag, val);

dengyihao's avatar
dengyihao 已提交
999
  if (!find) {
wmmhello's avatar
wmmhello 已提交
1000 1001
    return NULL;
  }
wmmhello's avatar
wmmhello 已提交
1002

wmmhello's avatar
wmmhello 已提交
1003
#ifdef TAG_FILTER_DEBUG
wmmhello's avatar
wmmhello 已提交
1004
  if (IS_VAR_DATA_TYPE(val->type)) {
1005
    char *buf = taosMemoryCalloc(val->nData + 1, 1);
wmmhello's avatar
wmmhello 已提交
1006 1007 1008 1009 1010 1011 1012 1013 1014
    memcpy(buf, val->pData, val->nData);
    metaDebug("metaTag table val varchar index:%d cid:%d type:%d value:%s", 1, val->cid, val->type, buf);
    taosMemoryFree(buf);
  } else {
    double dval = 0;
    GET_TYPED_DATA(dval, double, val->type, &val->i64);
    metaDebug("metaTag table val number index:%d cid:%d type:%d value:%f", 1, val->cid, val->type, dval);
  }

1015 1016
  SArray *pTagVals = NULL;
  tTagToValArray((STag *)pTag, &pTagVals);
wmmhello's avatar
wmmhello 已提交
1017
  for (int i = 0; i < taosArrayGetSize(pTagVals); i++) {
1018
    STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i);
wmmhello's avatar
wmmhello 已提交
1019 1020

    if (IS_VAR_DATA_TYPE(pTagVal->type)) {
1021
      char *buf = taosMemoryCalloc(pTagVal->nData + 1, 1);
wmmhello's avatar
wmmhello 已提交
1022 1023 1024 1025 1026 1027 1028 1029 1030
      memcpy(buf, pTagVal->pData, pTagVal->nData);
      metaDebug("metaTag table varchar index:%d cid:%d type:%d value:%s", i, pTagVal->cid, pTagVal->type, buf);
      taosMemoryFree(buf);
    } else {
      double dval = 0;
      GET_TYPED_DATA(dval, double, pTagVal->type, &pTagVal->i64);
      metaDebug("metaTag table number index:%d cid:%d type:%d value:%f", i, pTagVal->cid, pTagVal->type, dval);
    }
  }
wmmhello's avatar
wmmhello 已提交
1031
#endif
wmmhello's avatar
wmmhello 已提交
1032

wmmhello's avatar
wmmhello 已提交
1033
  return val;
dengyihao's avatar
dengyihao 已提交
1034
}
wmmhello's avatar
wmmhello 已提交
1035 1036

typedef struct {
H
Hongze Cheng 已提交
1037 1038
  SMeta   *pMeta;
  TBC     *pCur;
wmmhello's avatar
wmmhello 已提交
1039 1040 1041
  tb_uid_t suid;
  int16_t  cid;
  int16_t  type;
H
Hongze Cheng 已提交
1042 1043
  void    *pKey;
  void    *pVal;
wmmhello's avatar
wmmhello 已提交
1044 1045 1046 1047
  int32_t  kLen;
  int32_t  vLen;
} SIdxCursor;

dengyihao's avatar
dengyihao 已提交
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
int32_t metaFilterCreateTime(SMeta *pMeta, SMetaFltParam *param, SArray *pUids) {
  int32_t ret = 0;

  SIdxCursor *pCursor = NULL;
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
  pCursor->pMeta = pMeta;
  pCursor->suid = param->suid;
  pCursor->cid = param->cid;
  pCursor->type = param->type;

  metaRLock(pMeta);
  ret = tdbTbcOpen(pMeta->pCtimeIdx, &pCursor->pCur, NULL);
  if (ret != 0) {
    goto END;
  }
  int64_t uidLimit = param->reverse ? INT64_MAX : 0;

  SCtimeIdxKey  ctimeKey = {.ctime = *(int64_t *)(param->val), .uid = uidLimit};
  SCtimeIdxKey *pCtimeKey = &ctimeKey;

  int cmp = 0;
  if (tdbTbcMoveTo(pCursor->pCur, &ctimeKey, sizeof(ctimeKey), &cmp) < 0) {
    goto END;
  }
dengyihao's avatar
dengyihao 已提交
1072

dengyihao's avatar
dengyihao 已提交
1073
  int32_t valid = 0;
dengyihao's avatar
dengyihao 已提交
1074 1075
  int32_t count = 0;

dengyihao's avatar
dengyihao 已提交
1076
  static const int8_t TRY_ERROR_LIMIT = 1;
dengyihao's avatar
dengyihao 已提交
1077
  do {
dengyihao's avatar
dengyihao 已提交
1078 1079 1080 1081 1082 1083
    void   *entryKey = NULL;
    int32_t nEntryKey = -1;
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, NULL, NULL);
    if (valid < 0) break;

    SCtimeIdxKey *p = entryKey;
dengyihao's avatar
dengyihao 已提交
1084
    if (count > TRY_ERROR_LIMIT) break;
dengyihao's avatar
dengyihao 已提交
1085

dengyihao's avatar
dengyihao 已提交
1086
    int32_t cmp = (*param->filterFunc)((void *)&p->ctime, (void *)&pCtimeKey->ctime, param->type);
dengyihao's avatar
dengyihao 已提交
1087 1088 1089
    if (cmp == 0)
      taosArrayPush(pUids, &p->uid);
    else {
dengyihao's avatar
dengyihao 已提交
1090 1091 1092 1093
      if (param->equal == true) {
        if (count > TRY_ERROR_LIMIT) break;
        count++;
      }
dengyihao's avatar
dengyihao 已提交
1094
    }
dengyihao's avatar
dengyihao 已提交
1095 1096
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
    if (valid < 0) break;
dengyihao's avatar
dengyihao 已提交
1097
  } while (1);
dengyihao's avatar
dengyihao 已提交
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119

END:
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
  taosMemoryFree(pCursor);
  return ret;
}

int32_t metaFilterTableName(SMeta *pMeta, SMetaFltParam *param, SArray *pUids) {
  int32_t ret = 0;
  char   *buf = NULL;

  STagIdxKey *pKey = NULL;
  int32_t     nKey = 0;

  SIdxCursor *pCursor = NULL;
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
  pCursor->pMeta = pMeta;
  pCursor->suid = param->suid;
  pCursor->cid = param->cid;
  pCursor->type = param->type;

dengyihao's avatar
dengyihao 已提交
1120 1121
  char *pName = param->val;

dengyihao's avatar
dengyihao 已提交
1122 1123
  metaRLock(pMeta);
  ret = tdbTbcOpen(pMeta->pNameIdx, &pCursor->pCur, NULL);
dengyihao's avatar
dengyihao 已提交
1124 1125 1126 1127 1128 1129 1130 1131 1132
  if (ret != 0) {
    goto END;
  }

  int cmp = 0;
  if (tdbTbcMoveTo(pCursor->pCur, pName, strlen(pName) + 1, &cmp) < 0) {
    goto END;
  }
  int32_t valid = 0;
dengyihao's avatar
dengyihao 已提交
1133 1134
  int32_t count = 0;

dengyihao's avatar
dengyihao 已提交
1135
  int32_t TRY_ERROR_LIMIT = 1;
dengyihao's avatar
dengyihao 已提交
1136
  do {
dengyihao's avatar
dengyihao 已提交
1137 1138 1139 1140 1141
    void   *pEntryKey = NULL, *pEntryVal = NULL;
    int32_t nEntryKey = -1, nEntryVal = 0;
    valid = tdbTbcGet(pCursor->pCur, (const void **)pEntryKey, &nEntryKey, (const void **)&pEntryVal, &nEntryVal);
    if (valid < 0) break;

dengyihao's avatar
dengyihao 已提交
1142 1143
    if (count > TRY_ERROR_LIMIT) break;

1144
    char *pTableKey = (char *)pEntryKey;
H
Haojun Liao 已提交
1145
    cmp = (*param->filterFunc)(pTableKey, pName, pCursor->type);
dengyihao's avatar
dengyihao 已提交
1146 1147 1148 1149
    if (cmp == 0) {
      tb_uid_t tuid = *(tb_uid_t *)pEntryVal;
      taosArrayPush(pUids, &tuid);
    } else {
dengyihao's avatar
dengyihao 已提交
1150 1151 1152 1153
      if (param->equal == true) {
        if (count > TRY_ERROR_LIMIT) break;
        count++;
      }
dengyihao's avatar
dengyihao 已提交
1154 1155 1156 1157 1158
    }
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
    if (valid < 0) {
      break;
    }
dengyihao's avatar
dengyihao 已提交
1159
  } while (1);
dengyihao's avatar
dengyihao 已提交
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199

END:
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
  taosMemoryFree(buf);
  taosMemoryFree(pKey);

  taosMemoryFree(pCursor);

  return ret;
}
int32_t metaFilterTtl(SMeta *pMeta, SMetaFltParam *param, SArray *pUids) {
  int32_t ret = 0;
  char   *buf = NULL;

  STtlIdxKey *pKey = NULL;
  int32_t     nKey = 0;

  SIdxCursor *pCursor = NULL;
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
  pCursor->pMeta = pMeta;
  pCursor->suid = param->suid;
  pCursor->cid = param->cid;
  pCursor->type = param->type;

  metaRLock(pMeta);
  ret = tdbTbcOpen(pMeta->pTtlIdx, &pCursor->pCur, NULL);

END:
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
  taosMemoryFree(buf);
  taosMemoryFree(pKey);

  taosMemoryFree(pCursor);

  return ret;
  // impl later
  return 0;
}
dengyihao's avatar
dengyihao 已提交
1200
int32_t metaFilterTableIds(SMeta *pMeta, SMetaFltParam *param, SArray *pUids) {
D
dapan1121 已提交
1201 1202 1203 1204 1205 1206 1207 1208
  SMetaEntry oStbEntry = {0};
  int32_t    ret = -1;
  char      *buf = NULL;
  void      *pData = NULL;
  int        nData = 0;

  SDecoder    dc = {0};
  STbDbKey    tbDbKey = {0};
dengyihao's avatar
dengyihao 已提交
1209 1210 1211 1212
  STagIdxKey *pKey = NULL;
  int32_t     nKey = 0;

  SIdxCursor *pCursor = NULL;
wmmhello's avatar
wmmhello 已提交
1213 1214 1215 1216 1217 1218 1219
  pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor));
  pCursor->pMeta = pMeta;
  pCursor->suid = param->suid;
  pCursor->cid = param->cid;
  pCursor->type = param->type;

  metaRLock(pMeta);
D
dapan1121 已提交
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245

  if (tdbTbGet(pMeta->pUidIdx, &param->suid, sizeof(tb_uid_t), &pData, &nData) != 0) {
    goto END;
  }
  tbDbKey.uid = param->suid;
  tbDbKey.version = ((SUidIdxVal *)pData)[0].version;
  tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData);

  tDecoderInit(&dc, pData, nData);
  ret = metaDecodeEntry(&dc, &oStbEntry);

  if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) {
    ret = -1;
    goto END;
  }
  ret = -1;
  for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) {
    SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i;
    if (schema->colId == param->cid && param->type == schema->type && (IS_IDX_ON(schema) || i == 0)) {
      ret = 0;
    }
  }
  if (ret != 0) {
    goto END;
  }

dengyihao's avatar
dengyihao 已提交
1246
  ret = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL);
D
dapan1121 已提交
1247
  if (ret != 0) {
wmmhello's avatar
wmmhello 已提交
1248 1249 1250
    goto END;
  }

dengyihao's avatar
dengyihao 已提交
1251
  int32_t maxSize = 0;
wmmhello's avatar
wmmhello 已提交
1252
  int32_t nTagData = 0;
H
Hongze Cheng 已提交
1253
  void   *tagData = NULL;
dengyihao's avatar
dengyihao 已提交
1254

dengyihao's avatar
dengyihao 已提交
1255
  if (param->val == NULL) {
S
Shengliang Guan 已提交
1256
    metaError("vgId:%d, failed to filter NULL data", TD_VID(pMeta->pVnode));
dengyihao's avatar
dengyihao 已提交
1257
    ret = -1;
dengyihao's avatar
dengyihao 已提交
1258
    goto END;
dengyihao's avatar
dengyihao 已提交
1259
  } else {
dengyihao's avatar
dengyihao 已提交
1260 1261 1262 1263 1264 1265 1266 1267
    if (IS_VAR_DATA_TYPE(param->type)) {
      tagData = varDataVal(param->val);
      nTagData = varDataLen(param->val);

      if (param->type == TSDB_DATA_TYPE_NCHAR) {
        maxSize = 4 * nTagData + 1;
        buf = taosMemoryCalloc(1, maxSize);
        if (false == taosMbsToUcs4(tagData, nTagData, (TdUcs4 *)buf, maxSize, &maxSize)) {
D
dapan1121 已提交
1268
          ret = -1;
dengyihao's avatar
dengyihao 已提交
1269 1270
          goto END;
        }
dengyihao's avatar
dengyihao 已提交
1271

dengyihao's avatar
dengyihao 已提交
1272 1273 1274 1275 1276 1277
        tagData = buf;
        nTagData = maxSize;
      }
    } else {
      tagData = param->val;
      nTagData = tDataTypes[param->type].bytes;
dengyihao's avatar
dengyihao 已提交
1278
    }
wmmhello's avatar
wmmhello 已提交
1279
  }
dengyihao's avatar
dengyihao 已提交
1280
  ret = metaCreateTagIdxKey(pCursor->suid, pCursor->cid, tagData, nTagData, pCursor->type,
wmmhello's avatar
wmmhello 已提交
1281
                            param->reverse ? INT64_MAX : INT64_MIN, &pKey, &nKey);
dengyihao's avatar
dengyihao 已提交
1282

wmmhello's avatar
wmmhello 已提交
1283 1284 1285
  if (ret != 0) {
    goto END;
  }
D
dapan1121 已提交
1286

wmmhello's avatar
wmmhello 已提交
1287
  int cmp = 0;
D
dapan1121 已提交
1288 1289
  ret = tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp);
  if (ret != 0) {
wmmhello's avatar
wmmhello 已提交
1290 1291
    goto END;
  }
dengyihao's avatar
dengyihao 已提交
1292

dengyihao's avatar
dengyihao 已提交
1293
  int     count = 0;
dengyihao's avatar
dengyihao 已提交
1294
  int32_t valid = 0;
dengyihao's avatar
dengyihao 已提交
1295
  bool    found = false;
dengyihao's avatar
dengyihao 已提交
1296

dengyihao's avatar
dengyihao 已提交
1297
  static const int8_t TRY_ERROR_LIMIT = 1;
dengyihao's avatar
dengyihao 已提交
1298 1299 1300

  /// src:   [[suid, cid1, type1]....[suid, cid2, type2]....[suid, cid3, type3]...]
  /// target:                        [suid, cid2, type2]
1301
  int diffCidCount = 0;
dengyihao's avatar
dengyihao 已提交
1302
  do {
dengyihao's avatar
dengyihao 已提交
1303 1304 1305
    void   *entryKey = NULL, *entryVal = NULL;
    int32_t nEntryKey, nEntryVal;

wmmhello's avatar
wmmhello 已提交
1306 1307 1308 1309
    valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal);
    if (valid < 0) {
      break;
    }
dengyihao's avatar
dengyihao 已提交
1310
    if (count > TRY_ERROR_LIMIT) {
dengyihao's avatar
dengyihao 已提交
1311 1312 1313
      break;
    }

wmmhello's avatar
wmmhello 已提交
1314
    STagIdxKey *p = entryKey;
dengyihao's avatar
dengyihao 已提交
1315
    if (p == NULL) break;
dengyihao's avatar
dengyihao 已提交
1316

dengyihao's avatar
dengyihao 已提交
1317
    if (p->type != pCursor->type || p->suid != pCursor->suid || p->cid != pCursor->cid) {
1318 1319 1320
      if (found == true) break;  //
      if (diffCidCount > TRY_ERROR_LIMIT) break;
      diffCidCount++;
dengyihao's avatar
dengyihao 已提交
1321 1322 1323
      count++;
      valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
      if (valid < 0) {
dengyihao's avatar
dengyihao 已提交
1324
        break;
dengyihao's avatar
dengyihao 已提交
1325 1326
      } else {
        continue;
dengyihao's avatar
dengyihao 已提交
1327 1328
      }
    }
dengyihao's avatar
dengyihao 已提交
1329

dengyihao's avatar
dengyihao 已提交
1330 1331 1332 1333 1334 1335
    int32_t cmp = (*param->filterFunc)(p->data, pKey->data, pKey->type);
    if (cmp == 0) {
      // match
      tb_uid_t tuid = 0;
      if (IS_VAR_DATA_TYPE(pKey->type)) {
        tuid = *(tb_uid_t *)(p->data + varDataTLen(p->data));
wmmhello's avatar
wmmhello 已提交
1336
      } else {
dengyihao's avatar
dengyihao 已提交
1337
        tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes);
wmmhello's avatar
wmmhello 已提交
1338
      }
dengyihao's avatar
dengyihao 已提交
1339
      taosArrayPush(pUids, &tuid);
dengyihao's avatar
dengyihao 已提交
1340
      found = true;
dengyihao's avatar
dengyihao 已提交
1341
    } else {
dengyihao's avatar
dengyihao 已提交
1342 1343 1344 1345
      if (param->equal == true) {
        if (count > TRY_ERROR_LIMIT) break;
        count++;
      }
wmmhello's avatar
wmmhello 已提交
1346 1347 1348 1349 1350
    }
    valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur);
    if (valid < 0) {
      break;
    }
dengyihao's avatar
dengyihao 已提交
1351
  } while (1);
dengyihao's avatar
dengyihao 已提交
1352

wmmhello's avatar
wmmhello 已提交
1353 1354 1355
END:
  if (pCursor->pMeta) metaULock(pCursor->pMeta);
  if (pCursor->pCur) tdbTbcClose(pCursor->pCur);
D
dapan1121 已提交
1356 1357 1358 1359
  if (oStbEntry.pBuf) taosMemoryFree(oStbEntry.pBuf);
  tDecoderClear(&dc);
  tdbFree(pData);

dengyihao's avatar
dengyihao 已提交
1360
  taosMemoryFree(buf);
dengyihao's avatar
dengyihao 已提交
1361
  taosMemoryFree(pKey);
wmmhello's avatar
wmmhello 已提交
1362 1363 1364 1365

  taosMemoryFree(pCursor);

  return ret;
dengyihao's avatar
dengyihao 已提交
1366
}
H
Hongze Cheng 已提交
1367

dengyihao's avatar
dengyihao 已提交
1368
static int32_t metaGetTableTagByUid(SMeta *pMeta, int64_t suid, int64_t uid, void **tag, int32_t *len, bool lock) {
dengyihao's avatar
dengyihao 已提交
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
  int ret = 0;
  if (lock) {
    metaRLock(pMeta);
  }

  SCtbIdxKey ctbIdxKey = {.suid = suid, .uid = uid};
  ret = tdbTbGet(pMeta->pCtbIdx, &ctbIdxKey, sizeof(SCtbIdxKey), tag, len);
  if (lock) {
    metaULock(pMeta);
  }

  return ret;
}
H
Haojun Liao 已提交
1382

1383
int32_t metaGetTableTagsByUids(SMeta *pMeta, int64_t suid, SArray *uidList) {
dengyihao's avatar
dengyihao 已提交
1384 1385 1386
  const int32_t LIMIT = 128;

  int32_t isLock = false;
dengyihao's avatar
dengyihao 已提交
1387 1388
  int32_t sz = uidList ? taosArrayGetSize(uidList) : 0;
  for (int i = 0; i < sz; i++) {
1389
    STUidTagInfo *p = taosArrayGet(uidList, i);
dengyihao's avatar
dengyihao 已提交
1390 1391 1392 1393 1394 1395 1396 1397

    if (i % LIMIT == 0) {
      if (isLock) metaULock(pMeta);

      metaRLock(pMeta);
      isLock = true;
    }

X
Xiaoyu Wang 已提交
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
    //    if (taosHashGet(tags, &p->uid, sizeof(tb_uid_t)) == NULL) {
    void   *val = NULL;
    int32_t len = 0;
    if (metaGetTableTagByUid(pMeta, suid, p->uid, &val, &len, false) == 0) {
      p->pTagVal = taosMemoryMalloc(len);
      memcpy(p->pTagVal, val, len);
      tdbFree(val);
    } else {
      metaError("vgId:%d, failed to table tags, suid: %" PRId64 ", uid: %" PRId64 "", TD_VID(pMeta->pVnode), suid,
                p->uid);
dengyihao's avatar
dengyihao 已提交
1408 1409
    }
  }
X
Xiaoyu Wang 已提交
1410
  //  }
dengyihao's avatar
dengyihao 已提交
1411
  if (isLock) metaULock(pMeta);
dengyihao's avatar
dengyihao 已提交
1412 1413
  return 0;
}
dengyihao's avatar
dengyihao 已提交
1414

1415
int32_t metaGetTableTags(SMeta *pMeta, uint64_t suid, SArray *pUidTagInfo) {
1416
  SMCtbCursor *pCur = metaOpenCtbCursor(pMeta, suid, 1);
1417

1418 1419 1420 1421
  // If len > 0 means there already have uids, and we only want the
  // tags of the specified tables, of which uid in the uid list. Otherwise, all table tags are retrieved and kept
  // in the hash map, that may require a lot of memory
  SHashObj *pSepecifiedUidMap = NULL;
1422 1423
  size_t    numOfElems = taosArrayGetSize(pUidTagInfo);
  if (numOfElems > 0) {
X
Xiaoyu Wang 已提交
1424 1425
    pSepecifiedUidMap =
        taosHashInit(numOfElems / 0.7, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
1426
    for (int i = 0; i < numOfElems; i++) {
H
Haojun Liao 已提交
1427 1428
      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, i);
      taosHashPut(pSepecifiedUidMap, &pTagInfo->uid, sizeof(uint64_t), &i, sizeof(int32_t));
1429 1430 1431
    }
  }

H
Haojun Liao 已提交
1432 1433 1434 1435 1436 1437
  if (numOfElems == 0) {  // all data needs to be added into the pUidTagInfo list
    while (1) {
      tb_uid_t uid = metaCtbCursorNext(pCur);
      if (uid == 0) {
        break;
      }
1438

1439 1440 1441 1442
      STUidTagInfo info = {.uid = uid, .pTagVal = pCur->pVal};
      info.pTagVal = taosMemoryMalloc(pCur->vLen);
      memcpy(info.pTagVal, pCur->pVal, pCur->vLen);
      taosArrayPush(pUidTagInfo, &info);
1443
    }
H
Haojun Liao 已提交
1444 1445 1446 1447 1448 1449
  } else {  // only the specified tables need to be added
    while (1) {
      tb_uid_t uid = metaCtbCursorNext(pCur);
      if (uid == 0) {
        break;
      }
1450

H
Haojun Liao 已提交
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
      int32_t *index = taosHashGet(pSepecifiedUidMap, &uid, sizeof(uint64_t));
      if (index == NULL) {
        continue;
      }

      STUidTagInfo *pTagInfo = taosArrayGet(pUidTagInfo, *index);
      if (pTagInfo->pTagVal == NULL) {
        pTagInfo->pTagVal = taosMemoryMalloc(pCur->vLen);
        memcpy(pTagInfo->pTagVal, pCur->pVal, pCur->vLen);
      }
    }
1462 1463
  }

1464
  taosHashCleanup(pSepecifiedUidMap);
1465
  metaCloseCtbCursor(pCur, 1);
1466 1467
  return TSDB_CODE_SUCCESS;
}
wmmhello's avatar
wmmhello 已提交
1468

H
Hongze Cheng 已提交
1469 1470
int32_t metaCacheGet(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo);

1471
int32_t metaGetInfo(SMeta *pMeta, int64_t uid, SMetaInfo *pInfo, SMetaReader *pReader) {
H
Hongze Cheng 已提交
1472 1473 1474
  int32_t code = 0;
  void   *pData = NULL;
  int     nData = 0;
1475
  int     lock = 0;
H
Hongze Cheng 已提交
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499

  metaRLock(pMeta);

  // search cache
  if (metaCacheGet(pMeta, uid, pInfo) == 0) {
    metaULock(pMeta);
    goto _exit;
  }

  // search TDB
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
    // not found
    metaULock(pMeta);
    code = TSDB_CODE_NOT_FOUND;
    goto _exit;
  }

  metaULock(pMeta);

  pInfo->uid = uid;
  pInfo->suid = ((SUidIdxVal *)pData)->suid;
  pInfo->version = ((SUidIdxVal *)pData)->version;
  pInfo->skmVer = ((SUidIdxVal *)pData)->skmVer;

1500 1501 1502 1503 1504 1505 1506
  if (pReader != NULL) {
    lock = !(pReader->flags & META_READER_NOLOCK);
    if (lock) {
      metaULock(pReader->pMeta);
      // metaReaderReleaseLock(pReader);
    }
  }
H
Hongze Cheng 已提交
1507 1508 1509 1510 1511
  // upsert the cache
  metaWLock(pMeta);
  metaCacheUpsert(pMeta, pInfo);
  metaULock(pMeta);

1512 1513 1514 1515
  if (lock) {
    metaRLock(pReader->pMeta);
  }

H
Hongze Cheng 已提交
1516 1517 1518 1519
_exit:
  tdbFree(pData);
  return code;
}
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548

int32_t metaGetStbStats(SMeta *pMeta, int64_t uid, SMetaStbStats *pInfo) {
  int32_t code = 0;

  metaRLock(pMeta);

  // fast path: search cache
  if (metaStatsCacheGet(pMeta, uid, pInfo) == TSDB_CODE_SUCCESS) {
    metaULock(pMeta);
    goto _exit;
  }

  // slow path: search TDB
  int64_t ctbNum = 0;
  vnodeGetCtbNum(pMeta->pVnode, uid, &ctbNum);

  metaULock(pMeta);

  pInfo->uid = uid;
  pInfo->ctbNum = ctbNum;

  // upsert the cache
  metaWLock(pMeta);
  metaStatsCacheUpsert(pMeta, pInfo);
  metaULock(pMeta);

_exit:
  return code;
}
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558

void metaUpdateStbStats(SMeta *pMeta, int64_t uid, int64_t delta) {
  SMetaStbStats stats = {0};

  if (metaStatsCacheGet(pMeta, uid, &stats) == TSDB_CODE_SUCCESS) {
    stats.ctbNum += delta;

    metaStatsCacheUpsert(pMeta, &stats);
  }
}