metaQuery.c 9.5 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 17
#include "vnodeInt.h"

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

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

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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    goto _err;
  }

  // decode the entry
  tCoderInit(&pReader->coder, TD_LITTLE_ENDIAN, pReader->pBuf, pReader->szBuf, TD_DECODER);

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

  return 0;

_err:
  return -1;
}

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

  // query uid.idx
  if (tdbDbGet(pMeta->pUidIdx, &uid, sizeof(uid), &pReader->pBuf, &pReader->szBuf) < 0) {
    return -1;
  }

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

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

  // query name.idx
  if (tdbDbGet(pMeta->pNameIdx, name, strlen(name) + 1, &pReader->pBuf, &pReader->szBuf) < 0) {
    return -1;
  }

  uid = *(tb_uid_t *)pReader->pBuf;
H
Hongze Cheng 已提交
74
  return metaGetTableEntryByUid(pReader, uid);
H
Hongze Cheng 已提交
75 76
}

H
Hongze Cheng 已提交
77
int metaReadNext(SMetaReader *pReader) {
H
Hongze Cheng 已提交
78 79
  SMeta *pMeta = pReader->pMeta;

H
Hongze Cheng 已提交
80
  // TODO
H
Hongze Cheng 已提交
81

H
Hongze Cheng 已提交
82 83 84 85
  return 0;
}

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

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

H
Hongze Cheng 已提交
94 95 96
  metaReaderInit(&pTbCur->mr, pMeta->pVnode, 0);

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

  return pTbCur;
}

void metaCloseTbCursor(SMTbCursor *pTbCur) {
  if (pTbCur) {
H
Hongze Cheng 已提交
103 104 105
    TDB_FREE(pTbCur->pKey);
    TDB_FREE(pTbCur->pVal);
    metaReaderClear(&pTbCur->mr);
H
Hongze Cheng 已提交
106 107 108 109 110 111 112
    if (pTbCur->pDbc) {
      tdbDbcClose(pTbCur->pDbc);
    }
    taosMemoryFree(pTbCur);
  }
}

H
Hongze Cheng 已提交
113
int metaTbCursorNext(SMTbCursor *pTbCur) {
H
Hongze Cheng 已提交
114 115 116 117 118
  int    ret;
  void  *pBuf;
  STbCfg tbCfg;

  for (;;) {
H
Hongze Cheng 已提交
119 120 121
    ret = tdbDbNext(pTbCur->pDbc, &pTbCur->pKey, &pTbCur->kLen, &pTbCur->pVal, &pTbCur->vLen);
    if (ret < 0) {
      return -1;
H
Hongze Cheng 已提交
122 123
    }

H
Hongze Cheng 已提交
124 125 126 127
    metaGetTableEntryByVersion(&pTbCur->mr, *(int64_t *)pTbCur->pVal, *(tb_uid_t *)pTbCur->pKey);
    if (pTbCur->mr.me.type == META_SUPER_TABLE) {
      continue;
    }
H
Hongze Cheng 已提交
128 129

    break;
H
Hongze Cheng 已提交
130 131
  }

H
Hongze Cheng 已提交
132
  return 0;
H
Hongze Cheng 已提交
133 134
}

H
Hongze Cheng 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
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;
  SCoder          coder = {0};

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

H
Hongze Cheng 已提交
157 158 159
  // decode
  pBuf = pVal;
  pSW = taosMemoryMalloc(sizeof(pSW));
H
Hongze Cheng 已提交
160

H
Hongze Cheng 已提交
161 162 163 164 165
  tCoderInit(&coder, TD_LITTLE_ENDIAN, pVal, vLen, TD_DECODER);
  tDecodeSSchemaWrapper(&coder, pSW);
  pSchema = taosMemoryMalloc(sizeof(SSchema) * pSW->nCols);
  memcpy(pSchema, pSW->pSchema, sizeof(SSchema) * pSW->nCols);
  tCoderClear(&coder);
H
Hongze Cheng 已提交
166

H
Hongze Cheng 已提交
167
  pSW->pSchema = pSchema;
H
Hongze Cheng 已提交
168

H
Hongze Cheng 已提交
169 170 171
  TDB_FREE(pVal);

  return pSW;
H
Hongze Cheng 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
}

SMCtbCursor *metaOpenCtbCursor(SMeta *pMeta, tb_uid_t uid) {
  SMCtbCursor *pCtbCur = NULL;
  // SMetaDB     *pDB = pMeta->pDB;
  // int          ret;

  // pCtbCur = (SMCtbCursor *)taosMemoryCalloc(1, sizeof(*pCtbCur));
  // if (pCtbCur == NULL) {
  //   return NULL;
  // }

  // pCtbCur->suid = uid;
  // ret = tdbDbcOpen(pDB->pCtbIdx, &pCtbCur->pCur);
  // if (ret < 0) {
  //   taosMemoryFree(pCtbCur);
  //   return NULL;
  // }

  return pCtbCur;
}

void metaCloseCtbCurosr(SMCtbCursor *pCtbCur) {
  // if (pCtbCur) {
  //   if (pCtbCur->pCur) {
  //     tdbDbcClose(pCtbCur->pCur);

  //     TDB_FREE(pCtbCur->pKey);
  //     TDB_FREE(pCtbCur->pVal);
  //   }

  //   taosMemoryFree(pCtbCur);
  // }
}

tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) {
  // int         ret;
  // SCtbIdxKey *pCtbIdxKey;

  // ret = tdbDbNext(pCtbCur->pCur, &pCtbCur->pKey, &pCtbCur->kLen, &pCtbCur->pVal, &pCtbCur->vLen);
  // if (ret < 0) {
  //   return 0;
  // }

  // pCtbIdxKey = pCtbCur->pKey;

  // return pCtbIdxKey->uid;
  return 0;
}

STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver) {
  tb_uid_t        quid;
H
Hongze Cheng 已提交
224 225 226 227
  SMetaReader     mr = {0};
  STSchema       *pTSchema = NULL;
  SSchemaWrapper *pSW = NULL;
  STSchemaBuilder sb = {0};
H
Hongze Cheng 已提交
228 229
  SSchema        *pSchema;

H
Hongze Cheng 已提交
230 231 232 233 234
  metaReaderInit(&mr, pMeta->pVnode, 0);
  metaGetTableEntryByUid(&mr, uid);

  if (mr.me.type == TSDB_CHILD_TABLE) {
    quid = mr.me.ctbEntry.suid;
H
Hongze Cheng 已提交
235 236 237 238
  } else {
    quid = uid;
  }

H
Hongze Cheng 已提交
239
  metaReaderClear(&mr);
H
Hongze Cheng 已提交
240

H
Hongze Cheng 已提交
241
  pSW = metaGetTableSchema(pMeta, quid, sver, 0);
H
Hongze Cheng 已提交
242 243 244 245 246 247 248 249
  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 已提交
250 251
  taosMemoryFree(pSW->pSchema);
  taosMemoryFree(pSW);
H
Hongze Cheng 已提交
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 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 372 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
  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?
    if (tdbDbNext(pCur->pCur, &pCur->pKey, &pCur->kLen, NULL, &pCur->vLen) == 0) {
      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) {
        TDB_FREE(pSmaVal);
        metaCloseSmaCursor(pCur);
        tdDestroyTSmaWrapper(pSW);
        taosMemoryFreeClear(pSW);
        return NULL;
      }
      pSW->tSma = tptr;
      pBuf = pSmaVal;
      if (tDecodeTSma(pBuf, pSW->tSma + pSW->number - 1) == NULL) {
        TDB_FREE(pSmaVal);
        metaCloseSmaCursor(pCur);
        tdDestroyTSmaWrapper(pSW);
        taosMemoryFreeClear(pSW);
        return NULL;
      }
      TDB_FREE(pSmaVal);
      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?
    if (tdbDbNext(pCur->pCur, &pCur->pKey, &pCur->kLen, NULL, &pCur->vLen) == 0) {
      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);
    TDB_FREE(pVal);
    return NULL;
  }

  TDB_FREE(pVal);
  return pCfg;
#endif
#endif
  return NULL;
H
Hongze Cheng 已提交
427 428 429
}

#endif