metaQuery.c 10.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 (tdbDbGet(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 61
  int64_t version;

  // query uid.idx
  if (tdbDbGet(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 75
  tb_uid_t uid;

  // query name.idx
  if (tdbDbGet(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
int metaReadNext(SMetaReader *pReader) {
H
Hongze Cheng 已提交
85 86
  SMeta *pMeta = pReader->pMeta;

H
Hongze Cheng 已提交
87
  // TODO
H
Hongze Cheng 已提交
88

H
Hongze Cheng 已提交
89 90 91 92
  return 0;
}

#if 1  // ===================================================
H
Hongze Cheng 已提交
93 94 95 96 97 98 99 100
SMTbCursor *metaOpenTbCursor(SMeta *pMeta) {
  SMTbCursor *pTbCur = NULL;

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

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

H
Hongze Cheng 已提交
103
  tdbDbcOpen(pMeta->pUidIdx, &pTbCur->pDbc, NULL);
H
Hongze Cheng 已提交
104

H
Hongze Cheng 已提交
105 106
  tdbDbcMoveToFirst(pTbCur->pDbc);

H
Hongze Cheng 已提交
107 108 109 110 111
  return pTbCur;
}

void metaCloseTbCursor(SMTbCursor *pTbCur) {
  if (pTbCur) {
H
Hongze Cheng 已提交
112 113
    tdbFree(pTbCur->pKey);
    tdbFree(pTbCur->pVal);
H
Hongze Cheng 已提交
114
    metaReaderClear(&pTbCur->mr);
H
Hongze Cheng 已提交
115 116 117 118 119 120 121
    if (pTbCur->pDbc) {
      tdbDbcClose(pTbCur->pDbc);
    }
    taosMemoryFree(pTbCur);
  }
}

H
Hongze Cheng 已提交
122
int metaTbCursorNext(SMTbCursor *pTbCur) {
H
Hongze Cheng 已提交
123 124 125 126 127
  int    ret;
  void  *pBuf;
  STbCfg tbCfg;

  for (;;) {
H
Hongze Cheng 已提交
128
    ret = tdbDbcNext(pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
H
Hongze Cheng 已提交
129 130
    if (ret < 0) {
      return -1;
H
Hongze Cheng 已提交
131 132
    }

H
Hongze Cheng 已提交
133
    metaGetTableEntryByVersion(&pTbCur->mr, *(int64_t *)pTbCur->pVal, *(tb_uid_t *)pTbCur->pKey);
H
Hongze Cheng 已提交
134
    if (pTbCur->mr.me.type == TSDB_SUPER_TABLE) {
H
Hongze Cheng 已提交
135 136
      continue;
    }
H
Hongze Cheng 已提交
137 138

    break;
H
Hongze Cheng 已提交
139 140
  }

H
Hongze Cheng 已提交
141
  return 0;
H
Hongze Cheng 已提交
142 143
}

H
Hongze Cheng 已提交
144 145 146 147 148 149 150 151 152 153
SSchemaWrapper *metaGetTableSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, bool isinline) {
  void           *pKey = NULL;
  void           *pVal = NULL;
  int             kLen = 0;
  int             vLen = 0;
  int             ret;
  SSkmDbKey       skmDbKey;
  SSchemaWrapper *pSW = NULL;
  SSchema        *pSchema = NULL;
  void           *pBuf;
H
Hongze Cheng 已提交
154
  SDecoder        coder = {0};
H
Hongze Cheng 已提交
155 156 157 158 159 160

  // fetch
  skmDbKey.uid = uid;
  skmDbKey.sver = sver;
  pKey = &skmDbKey;
  kLen = sizeof(skmDbKey);
H
fix  
Hongze Cheng 已提交
161
  metaRLock(pMeta);
H
Hongze Cheng 已提交
162
  ret = tdbDbGet(pMeta->pSkmDb, pKey, kLen, &pVal, &vLen);
H
fix  
Hongze Cheng 已提交
163
  metaULock(pMeta);
H
Hongze Cheng 已提交
164 165 166 167
  if (ret < 0) {
    return NULL;
  }

H
Hongze Cheng 已提交
168 169
  // decode
  pBuf = pVal;
L
Liu Jicong 已提交
170
  pSW = taosMemoryMalloc(sizeof(SSchemaWrapper));
H
Hongze Cheng 已提交
171

H
Hongze Cheng 已提交
172
  tDecoderInit(&coder, pVal, vLen);
H
Hongze Cheng 已提交
173 174 175
  tDecodeSSchemaWrapper(&coder, pSW);
  pSchema = taosMemoryMalloc(sizeof(SSchema) * pSW->nCols);
  memcpy(pSchema, pSW->pSchema, sizeof(SSchema) * pSW->nCols);
H
Hongze Cheng 已提交
176
  tDecoderClear(&coder);
H
Hongze Cheng 已提交
177

H
Hongze Cheng 已提交
178
  pSW->pSchema = pSchema;
H
Hongze Cheng 已提交
179

H
Hongze Cheng 已提交
180
  tdbFree(pVal);
H
Hongze Cheng 已提交
181 182

  return pSW;
H
Hongze Cheng 已提交
183 184
}

H
Hongze Cheng 已提交
185
struct SMCtbCursor {
H
fix  
Hongze Cheng 已提交
186
  SMeta   *pMeta;
H
Hongze Cheng 已提交
187 188 189 190 191 192 193 194
  TDBC    *pCur;
  tb_uid_t suid;
  void    *pKey;
  void    *pVal;
  int      kLen;
  int      vLen;
};

H
Hongze Cheng 已提交
195 196
SMCtbCursor *metaOpenCtbCursor(SMeta *pMeta, tb_uid_t uid) {
  SMCtbCursor *pCtbCur = NULL;
H
Hongze Cheng 已提交
197
  SCtbIdxKey   ctbIdxKey;
H
Hongze Cheng 已提交
198
  int          ret;
H
Hongze Cheng 已提交
199
  int          c;
H
Hongze Cheng 已提交
200

H
Hongze Cheng 已提交
201 202 203 204
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
  if (pCtbCur == NULL) {
    return NULL;
  }
H
Hongze Cheng 已提交
205

H
fix  
Hongze Cheng 已提交
206
  pCtbCur->pMeta = pMeta;
H
Hongze Cheng 已提交
207
  pCtbCur->suid = uid;
H
fix  
Hongze Cheng 已提交
208 209
  metaRLock(pMeta);

H
Hongze Cheng 已提交
210
  ret = tdbDbcOpen(pMeta->pCtbIdx, &pCtbCur->pCur, NULL);
H
Hongze Cheng 已提交
211
  if (ret < 0) {
H
fix  
Hongze Cheng 已提交
212
    metaULock(pMeta);
H
Hongze Cheng 已提交
213 214 215
    taosMemoryFree(pCtbCur);
    return NULL;
  }
H
Hongze Cheng 已提交
216

H
Hongze Cheng 已提交
217 218 219 220 221 222 223 224
  // move to the suid
  ctbIdxKey.suid = uid;
  ctbIdxKey.uid = INT64_MIN;
  tdbDbcMoveTo(pCtbCur->pCur, &ctbIdxKey, sizeof(ctbIdxKey), &c);
  if (c > 0) {
    tdbDbcMoveToNext(pCtbCur->pCur);
  }

H
Hongze Cheng 已提交
225 226 227 228
  return pCtbCur;
}

void metaCloseCtbCurosr(SMCtbCursor *pCtbCur) {
H
Hongze Cheng 已提交
229
  if (pCtbCur) {
H
fix  
Hongze Cheng 已提交
230
    if (pCtbCur->pMeta) metaULock(pCtbCur->pMeta);
H
Hongze Cheng 已提交
231 232
    if (pCtbCur->pCur) {
      tdbDbcClose(pCtbCur->pCur);
H
Hongze Cheng 已提交
233

H
Hongze Cheng 已提交
234 235
      tdbFree(pCtbCur->pKey);
      tdbFree(pCtbCur->pVal);
H
Hongze Cheng 已提交
236
    }
H
Hongze Cheng 已提交
237

H
Hongze Cheng 已提交
238 239
    taosMemoryFree(pCtbCur);
  }
H
Hongze Cheng 已提交
240 241 242
}

tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
H
Hongze Cheng 已提交
243 244
  int         ret;
  SCtbIdxKey *pCtbIdxKey;
H
Hongze Cheng 已提交
245

H
Hongze Cheng 已提交
246
  ret = tdbDbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
H
Hongze Cheng 已提交
247 248 249
  if (ret < 0) {
    return 0;
  }
H
Hongze Cheng 已提交
250

H
Hongze Cheng 已提交
251
  pCtbIdxKey = pCtbCur->pKey;
H
Hongze Cheng 已提交
252 253 254
  if (pCtbIdxKey->suid > pCtbCur->suid) {
    return 0;
  }
H
Hongze Cheng 已提交
255

H
Hongze Cheng 已提交
256
  return pCtbIdxKey->uid;
H
Hongze Cheng 已提交
257 258 259 260
}

STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver) {
  tb_uid_t        quid;
H
Hongze Cheng 已提交
261 262 263 264
  SMetaReader     mr = {0};
  STSchema       *pTSchema = NULL;
  SSchemaWrapper *pSW = NULL;
  STSchemaBuilder sb = {0};
H
Hongze Cheng 已提交
265 266
  SSchema        *pSchema;

H
Hongze Cheng 已提交
267
  metaReaderInit(&mr, pMeta, 0);
H
Hongze Cheng 已提交
268 269 270 271
  metaGetTableEntryByUid(&mr, uid);

  if (mr.me.type == TSDB_CHILD_TABLE) {
    quid = mr.me.ctbEntry.suid;
H
Hongze Cheng 已提交
272 273 274 275
  } else {
    quid = uid;
  }

H
Hongze Cheng 已提交
276
  metaReaderClear(&mr);
H
Hongze Cheng 已提交
277

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

H
Hongze Cheng 已提交
281 282 283 284 285 286 287 288
  tdInitTSchemaBuilder(&sb, 0);
  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);
  tdDestroyTSchemaBuilder(&sb);

H
Hongze Cheng 已提交
289 290
  taosMemoryFree(pSW->pSchema);
  taosMemoryFree(pSW);
H
Hongze Cheng 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
  return pTSchema;
}

STSmaWrapper *metaGetSmaInfoByTable(SMeta *pMeta, tb_uid_t uid) {
#if 0
#ifdef META_TDB_SMA_TEST
  STSmaWrapper *pSW = NULL;

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

  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, uid);
  if (pCur == NULL) {
    taosMemoryFree(pSW);
    return NULL;
  }

  void       *pBuf = NULL;
  SSmaIdxKey *pSmaIdxKey = NULL;

  while (true) {
    // TODO: lock during iterate?
H
Hongze Cheng 已提交
315
    if (tdbDbcNext(pCur->pCur, &pCur->pKey, &pCur->kLen, NULL, &pCur->vLen) == 0) {
H
Hongze Cheng 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328
      pSmaIdxKey = pCur->pKey;
      ASSERT(pSmaIdxKey != NULL);

      void *pSmaVal = metaGetSmaInfoByIndex(pMeta, pSmaIdxKey->smaUid, false);

      if (pSmaVal == NULL) {
        tsdbWarn("no tsma exists for indexUid: %" PRIi64, pSmaIdxKey->smaUid);
        continue;
      }

      ++pSW->number;
      STSma *tptr = (STSma *)taosMemoryRealloc(pSW->tSma, pSW->number * sizeof(STSma));
      if (tptr == NULL) {
H
Hongze Cheng 已提交
329
        tdbFree(pSmaVal);
H
Hongze Cheng 已提交
330 331 332 333 334 335 336 337
        metaCloseSmaCursor(pCur);
        tdDestroyTSmaWrapper(pSW);
        taosMemoryFreeClear(pSW);
        return NULL;
      }
      pSW->tSma = tptr;
      pBuf = pSmaVal;
      if (tDecodeTSma(pBuf, pSW->tSma + pSW->number - 1) == NULL) {
H
Hongze Cheng 已提交
338
        tdbFree(pSmaVal);
H
Hongze Cheng 已提交
339 340 341 342 343
        metaCloseSmaCursor(pCur);
        tdDestroyTSmaWrapper(pSW);
        taosMemoryFreeClear(pSW);
        return NULL;
      }
H
Hongze Cheng 已提交
344
      tdbFree(pSmaVal);
H
Hongze Cheng 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
      continue;
    }
    break;
  }

  metaCloseSmaCursor(pCur);

  return pSW;

#endif
#endif
  return NULL;
}

int metaGetTbNum(SMeta *pMeta) {
  // TODO
  // ASSERT(0);
  return 0;
}

SArray *metaGetSmaTbUids(SMeta *pMeta, bool isDup) {
#if 0
  // TODO
  // ASSERT(0); // comment this line to pass CI
  // return NULL:
#ifdef META_TDB_SMA_TEST
  SArray  *pUids = NULL;
  SMetaDB *pDB = pMeta->pDB;
  void    *pKey;

  // TODO: lock?
  SMSmaCursor *pCur = metaOpenSmaCursor(pMeta, 0);
  if (pCur == NULL) {
    return NULL;
  }
  // TODO: lock?

  SSmaIdxKey *pSmaIdxKey = NULL;
  tb_uid_t    uid = 0;
  while (true) {
    // TODO: lock during iterate?
H
Hongze Cheng 已提交
386
    if (tdbDbcNext(pCur->pCur, &pCur->pKey, &pCur->kLen, NULL, &pCur->vLen) == 0) {
H
Hongze Cheng 已提交
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 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
      ASSERT(pSmaIdxKey != NULL);
      pSmaIdxKey = pCur->pKey;

      if (pSmaIdxKey->uid == 0 || pSmaIdxKey->uid == uid) {
        continue;
      }
      uid = pSmaIdxKey->uid;

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

      taosArrayPush(pUids, &uid);

      continue;
    }
    break;
  }

  metaCloseSmaCursor(pCur);

  return pUids;
#endif
#endif
  return NULL;
}

void *metaGetSmaInfoByIndex(SMeta *pMeta, int64_t indexUid, bool isDecode) {
#if 0
  // TODO
  // ASSERT(0);
  // return NULL;
#ifdef META_TDB_SMA_TEST
  SMetaDB *pDB = pMeta->pDB;
  void    *pKey = NULL;
  void    *pVal = NULL;
  int      kLen = 0;
  int      vLen = 0;
  int      ret = -1;

  // Set key
  pKey = (void *)&indexUid;
  kLen = sizeof(indexUid);

  // Query
  ret = tdbDbGet(pDB->pSmaDB, pKey, kLen, &pVal, &vLen);
  if (ret != 0 || !pVal) {
    return NULL;
  }

  if (!isDecode) {
    // return raw value
    return pVal;
  }

  // Decode
  STSma *pCfg = (STSma *)taosMemoryCalloc(1, sizeof(STSma));
  if (pCfg == NULL) {
    taosMemoryFree(pVal);
    return NULL;
  }

  void *pBuf = pVal;
  if (tDecodeTSma(pBuf, pCfg) == NULL) {
    tdDestroyTSma(pCfg);
    taosMemoryFree(pCfg);
H
Hongze Cheng 已提交
457
    tdbFree(pVal);
H
Hongze Cheng 已提交
458 459 460
    return NULL;
  }

H
Hongze Cheng 已提交
461
  tdbFree(pVal);
H
Hongze Cheng 已提交
462 463 464 465
  return pCfg;
#endif
#endif
  return NULL;
H
Hongze Cheng 已提交
466 467
}

L
Liu Jicong 已提交
468
#endif