metaQuery.c 9.9 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
}
H
Hongze Cheng 已提交
23

H
Hongze Cheng 已提交
24
void metaReaderClear(SMetaReader *pReader) {
H
Hongze Cheng 已提交
25
  tDecoderClear(&pReader->coder);
H
Hongze Cheng 已提交
26
  tdbFree(pReader->pBuf);
H
Hongze Cheng 已提交
27 28
}

H
Hongze Cheng 已提交
29 30
int metaGetTableEntryByVersion(SMetaReader *pReader, int64_t version, tb_uid_t uid) {
  SMeta   *pMeta = pReader->pMeta;
H
Hongze Cheng 已提交
31 32
  STbDbKey tbDbKey = {.version = version, .uid = uid};

H
Hongze Cheng 已提交
33
  // query table.db
H
Hongze Cheng 已提交
34
  if (tdbDbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pReader->pBuf, &pReader->szBuf) < 0) {
H
Hongze Cheng 已提交
35
    terrno = TSDB_CODE_PAR_TABLE_NOT_EXIST;
H
Hongze Cheng 已提交
36 37 38 39
    goto _err;
  }

  // decode the entry
H
Hongze Cheng 已提交
40
  tDecoderInit(&pReader->coder, pReader->pBuf, pReader->szBuf);
H
Hongze Cheng 已提交
41 42 43 44 45 46 47 48 49 50 51

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

  return 0;

_err:
  return -1;
}

H
Hongze Cheng 已提交
52 53
int metaGetTableEntryByUid(SMetaReader *pReader, tb_uid_t uid) {
  SMeta  *pMeta = pReader->pMeta;
H
Hongze Cheng 已提交
54 55 56 57
  int64_t version;

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

  version = *(int64_t *)pReader->pBuf;
H
Hongze Cheng 已提交
63
  return metaGetTableEntryByVersion(pReader, version, uid);
H
Hongze Cheng 已提交
64 65
}

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

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

  uid = *(tb_uid_t *)pReader->pBuf;
H
Hongze Cheng 已提交
77
  return metaGetTableEntryByUid(pReader, uid);
H
Hongze Cheng 已提交
78 79
}

H
Hongze Cheng 已提交
80
int metaReadNext(SMetaReader *pReader) {
H
Hongze Cheng 已提交
81 82
  SMeta *pMeta = pReader->pMeta;

H
Hongze Cheng 已提交
83
  // TODO
H
Hongze Cheng 已提交
84

H
Hongze Cheng 已提交
85 86 87 88
  return 0;
}

#if 1  // ===================================================
H
Hongze Cheng 已提交
89 90 91 92 93 94 95 96
SMTbCursor *metaOpenTbCursor(SMeta *pMeta) {
  SMTbCursor *pTbCur = NULL;

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

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

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

H
Hongze Cheng 已提交
101 102
  tdbDbcMoveToFirst(pTbCur->pDbc);

H
Hongze Cheng 已提交
103 104 105 106 107
  return pTbCur;
}

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

H
Hongze Cheng 已提交
118
int metaTbCursorNext(SMTbCursor *pTbCur) {
H
Hongze Cheng 已提交
119 120 121 122 123
  int    ret;
  void  *pBuf;
  STbCfg tbCfg;

  for (;;) {
H
Hongze Cheng 已提交
124
    ret = tdbDbcNext(pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
H
Hongze Cheng 已提交
125 126
    if (ret < 0) {
      return -1;
H
Hongze Cheng 已提交
127 128
    }

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

    break;
H
Hongze Cheng 已提交
135 136
  }

H
Hongze Cheng 已提交
137
  return 0;
H
Hongze Cheng 已提交
138 139
}

H
Hongze Cheng 已提交
140 141 142 143 144 145 146 147 148 149
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 已提交
150
  SDecoder        coder = {0};
H
Hongze Cheng 已提交
151 152 153 154 155 156 157

  // fetch
  skmDbKey.uid = uid;
  skmDbKey.sver = sver;
  pKey = &skmDbKey;
  kLen = sizeof(skmDbKey);
  ret = tdbDbGet(pMeta->pSkmDb, pKey, kLen, &pVal, &vLen);
H
Hongze Cheng 已提交
158 159 160 161
  if (ret < 0) {
    return NULL;
  }

H
Hongze Cheng 已提交
162 163
  // decode
  pBuf = pVal;
L
Liu Jicong 已提交
164
  pSW = taosMemoryMalloc(sizeof(SSchemaWrapper));
H
Hongze Cheng 已提交
165

H
Hongze Cheng 已提交
166
  tDecoderInit(&coder, pVal, vLen);
H
Hongze Cheng 已提交
167 168 169
  tDecodeSSchemaWrapper(&coder, pSW);
  pSchema = taosMemoryMalloc(sizeof(SSchema) * pSW->nCols);
  memcpy(pSchema, pSW->pSchema, sizeof(SSchema) * pSW->nCols);
H
Hongze Cheng 已提交
170
  tDecoderClear(&coder);
H
Hongze Cheng 已提交
171

H
Hongze Cheng 已提交
172
  pSW->pSchema = pSchema;
H
Hongze Cheng 已提交
173

H
Hongze Cheng 已提交
174
  tdbFree(pVal);
H
Hongze Cheng 已提交
175 176

  return pSW;
H
Hongze Cheng 已提交
177 178
}

H
Hongze Cheng 已提交
179 180 181 182 183 184 185 186 187
struct SMCtbCursor {
  TDBC    *pCur;
  tb_uid_t suid;
  void    *pKey;
  void    *pVal;
  int      kLen;
  int      vLen;
};

H
Hongze Cheng 已提交
188 189
SMCtbCursor *metaOpenCtbCursor(SMeta *pMeta, tb_uid_t uid) {
  SMCtbCursor *pCtbCur = NULL;
H
Hongze Cheng 已提交
190
  SCtbIdxKey   ctbIdxKey;
H
Hongze Cheng 已提交
191
  int          ret;
H
Hongze Cheng 已提交
192
  int          c;
H
Hongze Cheng 已提交
193

H
Hongze Cheng 已提交
194 195 196 197
  pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
  if (pCtbCur == NULL) {
    return NULL;
  }
H
Hongze Cheng 已提交
198

H
Hongze Cheng 已提交
199
  pCtbCur->suid = uid;
H
Hongze Cheng 已提交
200
  ret = tdbDbcOpen(pMeta->pCtbIdx, &pCtbCur->pCur, NULL);
H
Hongze Cheng 已提交
201 202 203 204
  if (ret < 0) {
    taosMemoryFree(pCtbCur);
    return NULL;
  }
H
Hongze Cheng 已提交
205

H
Hongze Cheng 已提交
206 207 208 209 210 211 212 213
  // 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 已提交
214 215 216 217
  return pCtbCur;
}

void metaCloseCtbCurosr(SMCtbCursor *pCtbCur) {
H
Hongze Cheng 已提交
218 219 220
  if (pCtbCur) {
    if (pCtbCur->pCur) {
      tdbDbcClose(pCtbCur->pCur);
H
Hongze Cheng 已提交
221

H
Hongze Cheng 已提交
222 223
      tdbFree(pCtbCur->pKey);
      tdbFree(pCtbCur->pVal);
H
Hongze Cheng 已提交
224
    }
H
Hongze Cheng 已提交
225

H
Hongze Cheng 已提交
226 227
    taosMemoryFree(pCtbCur);
  }
H
Hongze Cheng 已提交
228 229 230
}

tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
H
Hongze Cheng 已提交
231 232
  int         ret;
  SCtbIdxKey *pCtbIdxKey;
H
Hongze Cheng 已提交
233

H
Hongze Cheng 已提交
234
  ret = tdbDbcNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
H
Hongze Cheng 已提交
235 236 237
  if (ret < 0) {
    return 0;
  }
H
Hongze Cheng 已提交
238

H
Hongze Cheng 已提交
239
  pCtbIdxKey = pCtbCur->pKey;
H
Hongze Cheng 已提交
240 241 242
  if (pCtbIdxKey->suid > pCtbCur->suid) {
    return 0;
  }
H
Hongze Cheng 已提交
243

H
Hongze Cheng 已提交
244
  return pCtbIdxKey->uid;
H
Hongze Cheng 已提交
245 246 247 248
}

STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver) {
  tb_uid_t        quid;
H
Hongze Cheng 已提交
249 250 251 252
  SMetaReader     mr = {0};
  STSchema       *pTSchema = NULL;
  SSchemaWrapper *pSW = NULL;
  STSchemaBuilder sb = {0};
H
Hongze Cheng 已提交
253 254
  SSchema        *pSchema;

H
Hongze Cheng 已提交
255
  metaReaderInit(&mr, pMeta, 0);
H
Hongze Cheng 已提交
256 257 258 259
  metaGetTableEntryByUid(&mr, uid);

  if (mr.me.type == TSDB_CHILD_TABLE) {
    quid = mr.me.ctbEntry.suid;
H
Hongze Cheng 已提交
260 261 262 263
  } else {
    quid = uid;
  }

H
Hongze Cheng 已提交
264
  metaReaderClear(&mr);
H
Hongze Cheng 已提交
265

H
Hongze Cheng 已提交
266
  pSW = metaGetTableSchema(pMeta, quid, sver, 0);
H
Hongze Cheng 已提交
267 268 269 270 271 272 273 274
  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 已提交
275 276
  taosMemoryFree(pSW->pSchema);
  taosMemoryFree(pSW);
H
Hongze Cheng 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
  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 已提交
301
    if (tdbDbcNext(pCur->pCur, &pCur->pKey, &pCur->kLen, NULL, &pCur->vLen) == 0) {
H
Hongze Cheng 已提交
302 303 304 305 306 307 308 309 310 311 312 313 314
      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 已提交
315
        tdbFree(pSmaVal);
H
Hongze Cheng 已提交
316 317 318 319 320 321 322 323
        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 已提交
324
        tdbFree(pSmaVal);
H
Hongze Cheng 已提交
325 326 327 328 329
        metaCloseSmaCursor(pCur);
        tdDestroyTSmaWrapper(pSW);
        taosMemoryFreeClear(pSW);
        return NULL;
      }
H
Hongze Cheng 已提交
330
      tdbFree(pSmaVal);
H
Hongze Cheng 已提交
331 332 333 334 335 336 337 338 339 340 341 342 343 344 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
      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 已提交
372
    if (tdbDbcNext(pCur->pCur, &pCur->pKey, &pCur->kLen, NULL, &pCur->vLen) == 0) {
H
Hongze Cheng 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 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 439 440 441 442
      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 已提交
443
    tdbFree(pVal);
H
Hongze Cheng 已提交
444 445 446
    return NULL;
  }

H
Hongze Cheng 已提交
447
  tdbFree(pVal);
H
Hongze Cheng 已提交
448 449 450 451
  return pCfg;
#endif
#endif
  return NULL;
H
Hongze Cheng 已提交
452 453
}

L
Liu Jicong 已提交
454
#endif