metaQuery.c 13.2 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"
H
Hongze Cheng 已提交
17

H
Hongze Cheng 已提交
18
void metaReaderInit(SMetaReader *pReader, SMeta *pMeta, int32_t flags) {
H
Hongze Cheng 已提交
19 20
  memset(pReader, 0, sizeof(*pReader));
  pReader->flags = flags;
H
Hongze Cheng 已提交
21
  pReader->pMeta = pMeta;
H
Hongze Cheng 已提交
22
  metaRLock(pMeta);
H
Hongze Cheng 已提交
23
}
H
Hongze Cheng 已提交
24

H
Hongze Cheng 已提交
25
void metaReaderClear(SMetaReader *pReader) {
H
Hongze Cheng 已提交
26 27 28
  if (pReader->pMeta) {
    metaULock(pReader->pMeta);
  }
H
Hongze Cheng 已提交
29
  tDecoderClear(&pReader->coder);
H
Hongze Cheng 已提交
30
  tdbFree(pReader->pBuf);
H
Hongze Cheng 已提交
31 32
}

H
Hongze Cheng 已提交
33 34
int metaGetTableEntryByVersion(SMetaReader *pReader, int64_t version, tb_uid_t uid) {
  SMeta   *pMeta = pReader->pMeta;
H
Hongze Cheng 已提交
35 36
  STbDbKey tbDbKey = {.version = version, .uid = uid};

H
Hongze Cheng 已提交
37
  // query table.db
H
Hongze Cheng 已提交
38
  if (tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf) < 0) {
H
Hongze Cheng 已提交
39
    terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
H
Hongze Cheng 已提交
40 41 42 43
    goto _err;
  }

  // decode the entry
H
Hongze Cheng 已提交
44
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
H
Hongze Cheng 已提交
45 46 47 48 49 50 51 52 53 54 55

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

  return 0;

_err:
  return -1;
}

H
Hongze Cheng 已提交
56 57
int metaGetTableEntryByUid(SMetaReader *pReader, tb_uid_t uid) {
  SMeta  *pMeta = pReader->pMeta;
H
Hongze Cheng 已提交
58 59 60
  int64_t version;

  // query uid.idx
H
Hongze Cheng 已提交
61
  if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pReader->pBuf, &pReader->szBuf) < 0) {
H
Hongze Cheng 已提交
62
    terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
H
Hongze Cheng 已提交
63 64 65 66
    return -1;
  }

  version = *(int64_t *)pReader->pBuf;
H
Hongze Cheng 已提交
67
  return metaGetTableEntryByVersion(pReader, version, uid);
H
Hongze Cheng 已提交
68 69
}

H
Hongze Cheng 已提交
70 71
int metaGetTableEntryByName(SMetaReader *pReader, const char *name) {
  SMeta   *pMeta = pReader->pMeta;
H
Hongze Cheng 已提交
72 73 74
  tb_uid_t uid;

  // query name.idx
H
Hongze Cheng 已提交
75
  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
H
Hongze Cheng 已提交
76
    terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
H
Hongze Cheng 已提交
77 78 79 80
    return -1;
  }

  uid = *(tb_uid_t *)pReader->pBuf;
H
Hongze Cheng 已提交
81
  return metaGetTableEntryByUid(pReader, uid);
H
Hongze Cheng 已提交
82 83
}

H
Hongze Cheng 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96
tb_uid_t metaGetTableEntryUidByName(SMeta *pMeta, const char *name) {
  void    *pData = NULL;
  int      nData = 0;
  tb_uid_t uid = 0;

  if (tdbTbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pData, &nData) == 0) {
    uid = *(tb_uid_t *)pData;
    tdbFree(pData);
  }

  return 0;
}

H
Hongze Cheng 已提交
97
int metaReadNext(SMetaReader *pReader) {
H
Hongze Cheng 已提交
98 99
  SMeta *pMeta = pReader->pMeta;

H
Hongze Cheng 已提交
100
  // TODO
H
Hongze Cheng 已提交
101

H
Hongze Cheng 已提交
102 103 104 105
  return 0;
}

#if 1  // ===================================================
H
Hongze Cheng 已提交
106 107 108 109 110 111 112 113
SMTbCursor *metaOpenTbCursor(SMeta *pMeta) {
  SMTbCursor *pTbCur = NULL;

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

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

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

H
Hongze Cheng 已提交
118
  tdbTbcMoveToFirst(pTbCur->pDbc);
H
Hongze Cheng 已提交
119

H
Hongze Cheng 已提交
120 121 122 123 124
  return pTbCur;
}

void metaCloseTbCursor(SMTbCursor *pTbCur) {
  if (pTbCur) {
H
Hongze Cheng 已提交
125 126
    tdbFree(pTbCur->pKey);
    tdbFree(pTbCur->pVal);
H
Hongze Cheng 已提交
127
    metaReaderClear(&pTbCur->mr);
H
Hongze Cheng 已提交
128
    if (pTbCur->pDbc) {
H
Hongze Cheng 已提交
129
      tdbTbcClose(pTbCur->pDbc);
H
Hongze Cheng 已提交
130 131 132 133 134
    }
    taosMemoryFree(pTbCur);
  }
}

H
Hongze Cheng 已提交
135
int metaTbCursorNext(SMTbCursor *pTbCur) {
H
Hongze Cheng 已提交
136 137 138 139 140
  int    ret;
  void  *pBuf;
  STbCfg tbCfg;

  for (;;) {
H
Hongze Cheng 已提交
141
    ret = tdbTbcNext(pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
H
Hongze Cheng 已提交
142 143
    if (ret < 0) {
      return -1;
H
Hongze Cheng 已提交
144 145
    }

H
Hongze Cheng 已提交
146
    metaGetTableEntryByVersion(&pTbCur->mr, *(int64_t *)pTbCur->pVal, *(tb_uid_t *)pTbCur->pKey);
H
Hongze Cheng 已提交
147
    if (pTbCur->mr.me.type == TSDB_SUPER_TABLE) {
H
Hongze Cheng 已提交
148 149
      continue;
    }
H
Hongze Cheng 已提交
150 151

    break;
H
Hongze Cheng 已提交
152 153
  }

H
Hongze Cheng 已提交
154
  return 0;
H
Hongze Cheng 已提交
155 156
}

H
Hongze Cheng 已提交
157
SSchemaWrapper *metaGetTableSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, bool isinline) {
H
Hongze Cheng 已提交
158 159 160 161 162 163 164
  void           *pData = NULL;
  int             nData = 0;
  int64_t         version;
  SSchemaWrapper  schema = {0};
  SSchemaWrapper *pSchema = NULL;
  SDecoder        dc = {0};

H
fix  
Hongze Cheng 已提交
165
  metaRLock(pMeta);
H
Hongze Cheng 已提交
166 167 168 169 170 171
  if (sver < 0) {
    if (tdbTbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pData, &nData) < 0) {
      goto _err;
    }

    version = *(int64_t *)pData;
H
Hongze Cheng 已提交
172

H
Hongze Cheng 已提交
173
    tdbTbGet(pMeta->pTbDb, &(STbDbKey){.uid = uid, .version = version}, sizeof(STbDbKey), &pData, &nData);
H
Hongze Cheng 已提交
174

H
Hongze Cheng 已提交
175 176 177 178 179 180
    SMetaEntry me = {0};
    tDecoderInit(&dc, pData, nData);
    metaDecodeEntry(&dc, &me);
    if (me.type == TSDB_SUPER_TABLE) {
      pSchema = tCloneSSchemaWrapper(&me.stbEntry.schemaRow);
    } else if (me.type == TSDB_NORMAL_TABLE) {
H
Hongze Cheng 已提交
181
      pSchema = tCloneSSchemaWrapper(&me.ntbEntry.schemaRow);
H
Hongze Cheng 已提交
182 183 184 185 186 187 188 189
    } else {
      ASSERT(0);
    }
    tDecoderClear(&dc);
  } else {
    if (tdbTbGet(pMeta->pSkmDb, &(SSkmDbKey){.uid = uid, .sver = sver}, sizeof(SSkmDbKey), &pData, &nData) < 0) {
      goto _err;
    }
H
Hongze Cheng 已提交
190

H
Hongze Cheng 已提交
191 192 193 194 195
    tDecoderInit(&dc, pData, nData);
    tDecodeSSchemaWrapper(&dc, &schema);
    pSchema = tCloneSSchemaWrapper(&schema);
    tDecoderClear(&dc);
  }
H
Hongze Cheng 已提交
196

H
Hongze Cheng 已提交
197 198 199
  metaULock(pMeta);
  tdbFree(pData);
  return pSchema;
H
Hongze Cheng 已提交
200

H
Hongze Cheng 已提交
201 202 203 204
_err:
  metaULock(pMeta);
  tdbFree(pData);
  return NULL;
H
Hongze Cheng 已提交
205 206
}

H
Hongze Cheng 已提交
207
struct SMCtbCursor {
H
fix  
Hongze Cheng 已提交
208
  SMeta   *pMeta;
H
Hongze Cheng 已提交
209
  TBC     *pCur;
H
Hongze Cheng 已提交
210 211 212 213 214 215 216
  tb_uid_t suid;
  void    *pKey;
  void    *pVal;
  int      kLen;
  int      vLen;
};

H
Hongze Cheng 已提交
217 218
SMCtbCursor *metaOpenCtbCursor(SMeta *pMeta, tb_uid_t uid) {
  SMCtbCursor *pCtbCur = NULL;
H
Hongze Cheng 已提交
219
  SCtbIdxKey   ctbIdxKey;
H
Hongze Cheng 已提交
220
  int          ret;
H
Hongze Cheng 已提交
221
  int          c;
H
Hongze Cheng 已提交
222

H
Hongze Cheng 已提交
223 224 225 226
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
  if (pCtbCur == NULL) {
    return NULL;
  }
H
Hongze Cheng 已提交
227

H
fix  
Hongze Cheng 已提交
228
  pCtbCur->pMeta = pMeta;
H
Hongze Cheng 已提交
229
  pCtbCur->suid = uid;
H
fix  
Hongze Cheng 已提交
230 231
  metaRLock(pMeta);

H
Hongze Cheng 已提交
232
  ret = tdbTbcOpen(pMeta->pCtbIdx, &pCtbCur->pCur, NULL);
H
Hongze Cheng 已提交
233
  if (ret < 0) {
H
fix  
Hongze Cheng 已提交
234
    metaULock(pMeta);
H
Hongze Cheng 已提交
235 236 237
    taosMemoryFree(pCtbCur);
    return NULL;
  }
H
Hongze Cheng 已提交
238

H
Hongze Cheng 已提交
239 240 241
  // move to the suid
  ctbIdxKey.suid = uid;
  ctbIdxKey.uid = INT64_MIN;
H
Hongze Cheng 已提交
242
  tdbTbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
H
Hongze Cheng 已提交
243
  if (c > 0) {
H
Hongze Cheng 已提交
244
    tdbTbcMoveToNext(pCtbCur->pCur);
H
Hongze Cheng 已提交
245 246
  }

H
Hongze Cheng 已提交
247 248 249
  return pCtbCur;
}

C
Cary Xu 已提交
250
void metaCloseCtbCursor(SMCtbCursor *pCtbCur) {
H
Hongze Cheng 已提交
251
  if (pCtbCur) {
H
fix  
Hongze Cheng 已提交
252
    if (pCtbCur->pMeta) metaULock(pCtbCur->pMeta);
H
Hongze Cheng 已提交
253
    if (pCtbCur->pCur) {
H
Hongze Cheng 已提交
254
      tdbTbcClose(pCtbCur->pCur);
H
Hongze Cheng 已提交
255

H
Hongze Cheng 已提交
256 257
      tdbFree(pCtbCur->pKey);
      tdbFree(pCtbCur->pVal);
H
Hongze Cheng 已提交
258
    }
H
Hongze Cheng 已提交
259

H
Hongze Cheng 已提交
260 261
    taosMemoryFree(pCtbCur);
  }
H
Hongze Cheng 已提交
262 263 264
}

tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
H
Hongze Cheng 已提交
265 266
  int         ret;
  SCtbIdxKey *pCtbIdxKey;
H
Hongze Cheng 已提交
267

H
Hongze Cheng 已提交
268
  ret = tdbTbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
H
Hongze Cheng 已提交
269 270 271
  if (ret < 0) {
    return 0;
  }
H
Hongze Cheng 已提交
272

H
Hongze Cheng 已提交
273
  pCtbIdxKey = pCtbCur->pKey;
H
Hongze Cheng 已提交
274 275 276
  if (pCtbIdxKey->suid > pCtbCur->suid) {
    return 0;
  }
H
Hongze Cheng 已提交
277

H
Hongze Cheng 已提交
278
  return pCtbIdxKey->uid;
H
Hongze Cheng 已提交
279 280 281 282
}

STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver) {
  tb_uid_t        quid;
H
Hongze Cheng 已提交
283 284 285 286
  SMetaReader     mr = {0};
  STSchema       *pTSchema = NULL;
  SSchemaWrapper *pSW = NULL;
  STSchemaBuilder sb = {0};
H
Hongze Cheng 已提交
287 288
  SSchema        *pSchema;

H
Hongze Cheng 已提交
289
  metaReaderInit(&mr, pMeta, 0);
H
Hongze Cheng 已提交
290 291 292 293
  metaGetTableEntryByUid(&mr, uid);

  if (mr.me.type == TSDB_CHILD_TABLE) {
    quid = mr.me.ctbEntry.suid;
H
Hongze Cheng 已提交
294 295 296 297
  } else {
    quid = uid;
  }

H
Hongze Cheng 已提交
298
  metaReaderClear(&mr);
H
Hongze Cheng 已提交
299

H
Hongze Cheng 已提交
300
  pSW = metaGetTableSchema(pMeta, quid, sver, 0);
C
Cary Xu 已提交
301
  if (!pSW) return NULL;
H
fix  
Hongze Cheng 已提交
302

303
  tdInitTSchemaBuilder(&sb, pSW->version);
H
Hongze Cheng 已提交
304 305 306 307 308
  for (int i = 0; i < pSW->nCols; i++) {
    pSchema = pSW->pSchema + i;
    tdAddColToSchema(&sb, pSchema->type, pSchema->flags, pSchema->colId, pSchema->bytes);
  }
  pTSchema = tdGetSchemaFromBuilder(&sb);
C
Cary Xu 已提交
309

H
Hongze Cheng 已提交
310 311
  tdDestroyTSchemaBuilder(&sb);

H
Hongze Cheng 已提交
312 313
  taosMemoryFree(pSW->pSchema);
  taosMemoryFree(pSW);
H
Hongze Cheng 已提交
314 315 316
  return pTSchema;
}

C
Cary Xu 已提交
317 318 319 320 321
int metaGetTbNum(SMeta *pMeta) {
  // TODO
  // ASSERT(0);
  return 0;
}
H
Hongze Cheng 已提交
322

C
Cary Xu 已提交
323 324
typedef struct {
  SMeta   *pMeta;
H
Hongze Cheng 已提交
325
  TBC     *pCur;
C
Cary Xu 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
  tb_uid_t uid;
  void    *pKey;
  void    *pVal;
  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 已提交
342 343 344
    return NULL;
  }

C
Cary Xu 已提交
345 346 347 348
  pSmaCur->pMeta = pMeta;
  pSmaCur->uid = uid;
  metaRLock(pMeta);

H
Hongze Cheng 已提交
349
  ret = tdbTbcOpen(pMeta->pSmaIdx, &pSmaCur->pCur, NULL);
C
Cary Xu 已提交
350 351 352
  if (ret < 0) {
    metaULock(pMeta);
    taosMemoryFree(pSmaCur);
H
Hongze Cheng 已提交
353 354 355
    return NULL;
  }

C
Cary Xu 已提交
356 357 358
  // move to the suid
  smaIdxKey.uid = uid;
  smaIdxKey.smaUid = INT64_MIN;
H
Hongze Cheng 已提交
359
  tdbTbcMoveTo(pSmaCur->pCur, &smaIdxKey, sizeof(smaIdxKey), &c);
C
Cary Xu 已提交
360
  if (c > 0) {
H
Hongze Cheng 已提交
361
    tdbTbcMoveToNext(pSmaCur->pCur);
C
Cary Xu 已提交
362
  }
H
Hongze Cheng 已提交
363

C
Cary Xu 已提交
364 365
  return pSmaCur;
}
H
Hongze Cheng 已提交
366

C
Cary Xu 已提交
367 368 369 370
void metaCloseSmaCursor(SMSmaCursor *pSmaCur) {
  if (pSmaCur) {
    if (pSmaCur->pMeta) metaULock(pSmaCur->pMeta);
    if (pSmaCur->pCur) {
H
Hongze Cheng 已提交
371
      tdbTbcClose(pSmaCur->pCur);
H
Hongze Cheng 已提交
372

C
Cary Xu 已提交
373 374 375
      tdbFree(pSmaCur->pKey);
      tdbFree(pSmaCur->pVal);
    }
H
Hongze Cheng 已提交
376

C
Cary Xu 已提交
377 378 379 380 381 382 383 384
    taosMemoryFree(pSmaCur);
  }
}

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

H
Hongze Cheng 已提交
385
  ret = tdbTbcNext(pSmaCur->pCur, &pSmaCur->pKey, &pSmaCur->kLen, &pSmaCur->pVal, &pSmaCur->vLen);
C
Cary Xu 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
  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;
  SArray       *pSmaIds = NULL;

  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;
  STSma  *pTSma = NULL;
  for (int i = 0; i < pSW->number; ++i) {
    smaId = *(tb_uid_t *)taosArrayGet(pSmaIds, i);
    if (metaGetTableEntryByUid(&mr, smaId) < 0) {
      metaWarn("vgId:%d no entry for tbId: %" PRIi64 ", smaId: %" PRIi64, TD_VID(pMeta->pVnode), uid, smaId);
      continue;
    }
    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 已提交
439
        memcpy((void *)pTSma->expr, mr.me.smaEntry.tsma->expr, pTSma->exprLen);
H
Hongze Cheng 已提交
440
      }
C
Cary Xu 已提交
441 442 443 444 445
      if (pTSma->tagsFilterLen > 0) {
        if (!(pTSma->tagsFilter = taosMemoryCalloc(1, pTSma->tagsFilterLen))) {
          terrno = TSDB_CODE_OUT_OF_MEMORY;
          goto _err;
        }
H
Hongze Cheng 已提交
446
      }
H
Hongze Cheng 已提交
447
      memcpy((void *)pTSma->tagsFilter, mr.me.smaEntry.tsma->tagsFilter, pTSma->tagsFilterLen);
C
Cary Xu 已提交
448 449 450 451 452
    } else {
      pTSma->exprLen = 0;
      pTSma->expr = NULL;
      pTSma->tagsFilterLen = 0;
      pTSma->tagsFilter = NULL;
H
Hongze Cheng 已提交
453
    }
H
Hongze Cheng 已提交
454

C
Cary Xu 已提交
455
    ++smaIdx;
H
Hongze Cheng 已提交
456 457
  }

C
Cary Xu 已提交
458 459
  if (smaIdx <= 0) goto _err;
  pSW->number = smaIdx;
H
Hongze Cheng 已提交
460

C
Cary Xu 已提交
461 462
  metaReaderClear(&mr);
  taosArrayDestroy(pSmaIds);
H
Hongze Cheng 已提交
463
  return pSW;
C
Cary Xu 已提交
464 465 466 467
_err:
  metaReaderClear(&mr);
  taosArrayDestroy(pSmaIds);
  tdFreeTSmaWrapper(pSW, deepCopy);
H
Hongze Cheng 已提交
468 469 470
  return NULL;
}

C
Cary Xu 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
STSma *metaGetSmaInfoByIndex(SMeta *pMeta, int64_t indexUid) {
  STSma      *pTSma = NULL;
  SMetaReader mr = {0};
  metaReaderInit(&mr, pMeta, 0);
  if (metaGetTableEntryByUid(&mr, indexUid) < 0) {
    metaWarn("vgId:%d failed to get table entry for smaId: %" PRIi64, TD_VID(pMeta->pVnode), indexUid);
    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 已提交
491 492
}

C
Cary Xu 已提交
493 494 495
SArray *metaGetSmaIdsByTable(SMeta *pMeta, tb_uid_t uid) {
  SArray     *pUids = NULL;
  SSmaIdxKey *pSmaIdxKey = NULL;
H
Hongze Cheng 已提交
496

C
Cary Xu 已提交
497 498
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, uid);
  if (!pCur) {
H
Hongze Cheng 已提交
499 500 501
    return NULL;
  }

C
Cary Xu 已提交
502 503 504 505 506
  while (1) {
    tb_uid_t id = metaSmaCursorNext(pCur);
    if (id == 0) {
      break;
    }
H
Hongze Cheng 已提交
507

C
Cary Xu 已提交
508 509
    if (!pUids) {
      pUids = taosArrayInit(16, sizeof(tb_uid_t));
H
Hongze Cheng 已提交
510
      if (!pUids) {
C
Cary Xu 已提交
511 512 513
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        metaCloseSmaCursor(pCur);
        return NULL;
H
Hongze Cheng 已提交
514
      }
C
Cary Xu 已提交
515
    }
H
Hongze Cheng 已提交
516

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

C
Cary Xu 已提交
519 520 521 522 523
    if (taosArrayPush(pUids, &pSmaIdxKey->smaUid) < 0) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      metaCloseSmaCursor(pCur);
      taosArrayDestroy(pUids);
      return NULL;
H
Hongze Cheng 已提交
524 525 526 527 528 529 530
    }
  }

  metaCloseSmaCursor(pCur);
  return pUids;
}

C
Cary Xu 已提交
531 532 533 534 535 536 537
SArray *metaGetSmaTbUids(SMeta *pMeta) {
  SArray     *pUids = NULL;
  SSmaIdxKey *pSmaIdxKey = NULL;
  tb_uid_t    lastUid = 0;

  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, 0);
  if (!pCur) {
H
Hongze Cheng 已提交
538 539 540
    return NULL;
  }

C
Cary Xu 已提交
541 542 543 544 545
  while (1) {
    tb_uid_t uid = metaSmaCursorNext(pCur);
    if (uid == 0) {
      break;
    }
H
Hongze Cheng 已提交
546

C
Cary Xu 已提交
547 548 549
    if (lastUid == uid) {
      continue;
    }
H
Hongze Cheng 已提交
550

C
Cary Xu 已提交
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
    lastUid = uid;

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

    if (taosArrayPush(pUids, &uid) < 0) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      metaCloseSmaCursor(pCur);
      taosArrayDestroy(pUids);
      return NULL;
    }
H
Hongze Cheng 已提交
568 569
  }

C
Cary Xu 已提交
570 571
  metaCloseSmaCursor(pCur);
  return pUids;
H
Hongze Cheng 已提交
572 573
}

L
Liu Jicong 已提交
574
#endif
H
Hongze Cheng 已提交
575 576 577 578 579

const void *metaGetTableTagVal(SMetaEntry *pEntry, int16_t cid) {
  ASSERT(pEntry->type == TSDB_CHILD_TABLE);
  return tdGetKVRowValOfCol((const SKVRow)pEntry->ctbEntry.pTags, cid);
}