mockCatalogService.cpp 26.1 KB
Newer Older
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/>.
 */

X
Xiaoyu Wang 已提交
16 17
#include "mockCatalogService.h"

18 19 20
#include <iomanip>
#include <iostream>
#include <map>
X
Xiaoyu Wang 已提交
21
#include <set>
22

23
#include "tname.h"
24
#include "ttypes.h"
H
Haojun Liao 已提交
25
#include "tmisce.h"
26

27
std::unique_ptr<MockCatalogService> g_mockCatalogService;
28 29

class TableBuilder : public ITableBuilder {
X
Xiaoyu Wang 已提交
30
 public:
31
  virtual TableBuilder& addColumn(const std::string& name, int8_t type, int32_t bytes) {
X
Xiaoyu Wang 已提交
32
    assert(colId_ <= schema()->tableInfo.numOfTags + schema()->tableInfo.numOfColumns);
X
Xiaoyu Wang 已提交
33
    SSchema* col = schema()->schema + (colId_ - 1);
34
    col->type = type;
35
    col->colId = colId_++;
36 37
    col->bytes = bytes;
    strcpy(col->name, name.c_str());
38
    rowsize_ += bytes;
39 40 41 42
    return *this;
  }

  virtual TableBuilder& setVgid(int16_t vgid) {
43
    schema()->vgId = vgid;
H
Haojun Liao 已提交
44

X
Xiaoyu Wang 已提交
45
    SVgroupInfo vgroup = {vgid, 0, 0, {0}, 0};
wafwerar's avatar
wafwerar 已提交
46 47 48
    addEpIntoEpSet(&vgroup.epSet, "dnode_1", 6030);
    addEpIntoEpSet(&vgroup.epSet, "dnode_2", 6030);
    addEpIntoEpSet(&vgroup.epSet, "dnode_3", 6030);
L
Liu Jicong 已提交
49
    vgroup.epSet.inUse = 0;
H
Haojun Liao 已提交
50 51

    meta_->vgs.emplace_back(vgroup);
52 53 54 55
    return *this;
  }

  virtual TableBuilder& setPrecision(uint8_t precision) {
56
    schema()->tableInfo.precision = precision;
57 58 59
    return *this;
  }

X
Xiaoyu Wang 已提交
60
  virtual void done() { schema()->tableInfo.rowSize = rowsize_; }
61

X
Xiaoyu Wang 已提交
62
 private:
63 64 65
  friend class MockCatalogServiceImpl;

  static std::unique_ptr<TableBuilder> createTableBuilder(int8_t tableType, int32_t numOfColumns, int32_t numOfTags) {
X
Xiaoyu Wang 已提交
66 67
    STableMeta* meta =
        (STableMeta*)taosMemoryCalloc(1, sizeof(STableMeta) + sizeof(SSchema) * (numOfColumns + numOfTags));
68 69 70 71 72 73 74 75 76
    if (nullptr == meta) {
      throw std::bad_alloc();
    }
    meta->tableType = tableType;
    meta->tableInfo.numOfTags = numOfTags;
    meta->tableInfo.numOfColumns = numOfColumns;
    return std::unique_ptr<TableBuilder>(new TableBuilder(meta));
  }

X
Xiaoyu Wang 已提交
77
  TableBuilder(STableMeta* schemaMeta) : colId_(1), rowsize_(0), meta_(new MockTableMeta()) {
X
Xiaoyu Wang 已提交
78
    meta_->schema = schemaMeta;
79 80
  }

X
Xiaoyu Wang 已提交
81
  STableMeta* schema() { return meta_->schema; }
82

X
Xiaoyu Wang 已提交
83
  std::shared_ptr<MockTableMeta> table() { return meta_; }
84

85
  col_id_t                       colId_;
X
Xiaoyu Wang 已提交
86
  int32_t                        rowsize_;
87
  std::shared_ptr<MockTableMeta> meta_;
88 89 90
};

class MockCatalogServiceImpl {
X
Xiaoyu Wang 已提交
91
 public:
92 93
  static const int32_t numOfDataTypes = sizeof(tDataTypes) / sizeof(tDataTypes[0]);

X
Xiaoyu Wang 已提交
94
  MockCatalogServiceImpl() : id_(1), havaCache_(true) {}
95

96 97 98 99 100 101 102 103 104 105 106
  ~MockCatalogServiceImpl() {
    for (auto& cfg : dbCfg_) {
      taosArrayDestroy(cfg.second.pRetensions);
    }
    for (auto& indexes : index_) {
      for (auto& index : indexes.second) {
        taosMemoryFree(index.expr);
      }
    }
  }

X
Xiaoyu Wang 已提交
107
  int32_t catalogGetHandle() const { return 0; }
108

X
Xiaoyu Wang 已提交
109 110 111 112 113
  int32_t catalogGetTableMeta(const SName* pTableName, STableMeta** pTableMeta, bool onlyCache = false) const {
    if (onlyCache && !havaCache_) {
      return TSDB_CODE_SUCCESS;
    }

114
    std::unique_ptr<STableMeta> table;
H
Haojun Liao 已提交
115

X
Xiaoyu Wang 已提交
116 117
    char db[TSDB_DB_NAME_LEN] = {0};
    tNameGetDbName(pTableName, db);
H
Haojun Liao 已提交
118 119

    const char* tname = tNameGetTableName(pTableName);
X
Xiaoyu Wang 已提交
120
    int32_t     code = copyTableSchemaMeta(db, tname, &table);
121 122 123
    if (TSDB_CODE_SUCCESS != code) {
      return code;
    }
124
    *pTableMeta = table.release();
125
    return TSDB_CODE_SUCCESS;
126 127
  }

X
Xiaoyu Wang 已提交
128 129 130 131 132 133
  int32_t catalogGetTableHashVgroup(const SName* pTableName, SVgroupInfo* vgInfo, bool onlyCache = false) const {
    if (onlyCache && !havaCache_) {
      vgInfo->vgId = 0;
      return TSDB_CODE_SUCCESS;
    }

134 135
    vgInfo->vgId = 1;
    return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
136 137
  }

X
bugfix  
Xiaoyu Wang 已提交
138 139 140 141
  int32_t catalogGetTableDistVgInfo(const SName* pTableName, SArray** vgList) const {
    char db[TSDB_DB_NAME_LEN] = {0};
    tNameGetDbName(pTableName, db);
    return copyTableVgroup(db, tNameGetTableName(pTableName), vgList);
H
Haojun Liao 已提交
142 143
  }

144
  int32_t catalogGetDBVgList(const char* pDbFName, SArray** pVgList) const {
X
Xiaoyu Wang 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    std::string                 dbFName(pDbFName);
    DbMetaCache::const_iterator it = meta_.find(dbFName.substr(std::string(pDbFName).find_last_of('.') + 1));
    if (meta_.end() == it) {
      return TSDB_CODE_FAILED;
    }
    std::set<int32_t> vgSet;
    *pVgList = taosArrayInit(it->second.size(), sizeof(SVgroupInfo));
    for (const auto& vgs : it->second) {
      for (const auto& vg : vgs.second->vgs) {
        if (0 == vgSet.count(vg.vgId)) {
          taosArrayPush(*pVgList, &vg);
          vgSet.insert(vg.vgId);
        }
      }
    }
    return TSDB_CODE_SUCCESS;
  }

163 164 165 166 167 168 169 170 171 172 173
  int32_t catalogGetDBCfg(const char* pDbFName, SDbCfgInfo* pDbCfg) const {
    std::string                dbFName(pDbFName);
    DbCfgCache::const_iterator it = dbCfg_.find(dbFName.substr(std::string(pDbFName).find_last_of('.') + 1));
    if (dbCfg_.end() == it) {
      return TSDB_CODE_FAILED;
    }

    memcpy(pDbCfg, &(it->second), sizeof(SDbCfgInfo));
    return TSDB_CODE_SUCCESS;
  }

174 175 176 177 178 179 180 181 182
  int32_t catalogGetUdfInfo(const std::string& funcName, SFuncInfo* pInfo) const {
    auto it = udf_.find(funcName);
    if (udf_.end() == it) {
      return TSDB_CODE_FAILED;
    }
    memcpy(pInfo, it->second.get(), sizeof(SFuncInfo));
    return TSDB_CODE_SUCCESS;
  }

X
Xiaoyu Wang 已提交
183 184 185 186 187 188 189 190 191
  int32_t catalogGetTableIndex(const SName* pTableName, SArray** pIndexes) const {
    char tbFName[TSDB_TABLE_FNAME_LEN] = {0};
    tNameExtractFullName(pTableName, tbFName);
    auto it = index_.find(tbFName);
    if (index_.end() == it) {
      return TSDB_CODE_SUCCESS;
    }
    *pIndexes = taosArrayInit(it->second.size(), sizeof(STableIndexInfo));
    for (const auto& index : it->second) {
X
Xiaoyu Wang 已提交
192 193 194
      STableIndexInfo info;

      taosArrayPush(*pIndexes, copyTableIndexInfo(&info, &index));
X
Xiaoyu Wang 已提交
195 196 197 198
    }
    return TSDB_CODE_SUCCESS;
  }

199
  int32_t catalogGetDnodeList(SArray** pDnodes) const {
200
    *pDnodes = taosArrayInit(dnode_.size(), sizeof(SEpSet));
201
    for (const auto& dnode : dnode_) {
202
      taosArrayPush(*pDnodes, &dnode.second);
203 204 205 206
    }
    return TSDB_CODE_SUCCESS;
  }

207 208 209 210 211
  int32_t catalogGetAllMeta(const SCatalogReq* pCatalogReq, SMetaData* pMetaData) const {
    int32_t code = getAllTableMeta(pCatalogReq->pTableMeta, &pMetaData->pTableMeta);
    if (TSDB_CODE_SUCCESS == code) {
      code = getAllTableVgroup(pCatalogReq->pTableHash, &pMetaData->pTableHash);
    }
212 213 214 215 216 217
    if (TSDB_CODE_SUCCESS == code) {
      code = getAllDbVgroup(pCatalogReq->pDbVgroup, &pMetaData->pDbVgroup);
    }
    if (TSDB_CODE_SUCCESS == code) {
      code = getAllDbCfg(pCatalogReq->pDbCfg, &pMetaData->pDbCfg);
    }
218 219 220 221 222 223
    if (TSDB_CODE_SUCCESS == code) {
      code = getAllDbInfo(pCatalogReq->pDbInfo, &pMetaData->pDbInfo);
    }
    if (TSDB_CODE_SUCCESS == code) {
      code = getAllUserAuth(pCatalogReq->pUser, &pMetaData->pUser);
    }
224 225 226
    if (TSDB_CODE_SUCCESS == code) {
      code = getAllUdf(pCatalogReq->pUdf, &pMetaData->pUdfList);
    }
X
Xiaoyu Wang 已提交
227 228 229
    if (TSDB_CODE_SUCCESS == code) {
      code = getAllTableIndex(pCatalogReq->pTableIndex, &pMetaData->pTableIndex);
    }
230
    if (TSDB_CODE_SUCCESS == code && pCatalogReq->dNodeRequired) {
231
      code = getAllDnodeList(&pMetaData->pDnodeList);
232
    }
D
dapan1121 已提交
233 234 235
    if (TSDB_CODE_SUCCESS == code) {
      code = getAllTableCfg(pCatalogReq->pTableCfg, &pMetaData->pTableCfg);
    }
236 237 238
    return code;
  }

X
Xiaoyu Wang 已提交
239 240
  TableBuilder& createTableBuilder(const std::string& db, const std::string& tbname, int8_t tableType,
                                   int32_t numOfColumns, int32_t numOfTags) {
241
    builder_ = TableBuilder::createTableBuilder(tableType, numOfColumns, numOfTags);
242
    meta_[db][tbname] = builder_->table();
X
Xiaoyu Wang 已提交
243
    meta_[db][tbname]->schema->uid = getNextId();
244 245 246
    return *(builder_.get());
  }

247 248
  void createSubTable(const std::string& db, const std::string& stbname, const std::string& tbname, int16_t vgid) {
    std::unique_ptr<STableMeta> table;
249 250
    if (TSDB_CODE_SUCCESS != copyTableSchemaMeta(db, stbname, &table)) {
      throw std::runtime_error("copyTableSchemaMeta failed");
251
    }
252
    meta_[db][tbname].reset(new MockTableMeta());
X
Xiaoyu Wang 已提交
253
    meta_[db][tbname]->schema = table.release();
X
Xiaoyu Wang 已提交
254
    meta_[db][tbname]->schema->uid = getNextId();
255
    meta_[db][tbname]->schema->tableType = TSDB_CHILD_TABLE;
H
Haojun Liao 已提交
256

X
Xiaoyu Wang 已提交
257
    SVgroupInfo vgroup = {vgid, 0, 0, {0}, 0};
X
Xiaoyu Wang 已提交
258
    genEpSet(&vgroup.epSet);
H
Haojun Liao 已提交
259 260

    meta_[db][tbname]->vgs.emplace_back(vgroup);
261
    // super table
H
Haojun Liao 已提交
262
    meta_[db][stbname]->vgs.emplace_back(vgroup);
263 264
  }

265
  void showTables() const {
X
Xiaoyu Wang 已提交
266 267 268 269 270
// number of forward fills
#define NOF(n) ((n) / 2)
// number of backward fills
#define NOB(n) ((n) % 2 ? (n) / 2 + 1 : (n) / 2)
// center aligned
X
Xiaoyu Wang 已提交
271 272 273
#define CA(n, s)                                                                                        \
  std::setw(NOF((n) - int((s).length()))) << "" << (s) << std::setw(NOB((n) - int((s).length()))) << "" \
                                          << "|"
X
Xiaoyu Wang 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287
// string field length
#define SFL 20
// string field header
#define SH(h) CA(SFL, std::string(h))
// string field
#define SF(n) CA(SFL, n)
// integer field length
#define IFL 10
// integer field header
#define IH(i) CA(IFL, std::string(i))
// integer field
#define IF(i) CA(IFL, std::to_string(i))
// split line
#define SL(sn, in) std::setfill('=') << std::setw((sn) * (SFL + 1) + (in) * (IFL + 1)) << "" << std::setfill(' ')
288 289

    for (const auto& db : meta_) {
290
      std::cout << "Databse:" << db.first << std::endl;
291
      std::cout << SH("Table") << SH("Type") << SH("Precision") << IH("Vgid") << IH("RowSize") << std::endl;
292
      std::cout << SL(3, 1) << std::endl;
293
      for (const auto& table : db.second) {
294
        const auto& schema = table.second->schema;
X
Xiaoyu Wang 已提交
295 296
        std::cout << SF(table.first) << SF(ttToString(schema->tableType)) << SF(pToString(schema->tableInfo.precision))
                  << IF(schema->vgId) << IF(schema->tableInfo.rowSize) << std::endl;
297 298 299 300 301 302
      }
      std::cout << std::endl;
    }

    for (const auto& db : meta_) {
      for (const auto& table : db.second) {
303
        const auto& schema = table.second->schema;
304 305 306
        std::cout << "Table:" << table.first << std::endl;
        std::cout << SH("Field") << SH("Type") << SH("DataType") << IH("Bytes") << std::endl;
        std::cout << SL(3, 1) << std::endl;
307 308
        int16_t numOfColumns = schema->tableInfo.numOfColumns;
        int16_t numOfFields = numOfColumns + schema->tableInfo.numOfTags;
309
        for (int16_t i = 0; i < numOfFields; ++i) {
310
          const SSchema* col = schema->schema + i;
X
Xiaoyu Wang 已提交
311 312
          std::cout << SF(std::string(col->name)) << SH(ftToString(i, numOfColumns)) << SH(dtToString(col->type))
                    << IF(col->bytes) << std::endl;
313 314
        }
        std::cout << std::endl;
315 316 317 318
      }
    }
  }

319 320 321 322 323 324 325 326
  void createFunction(const std::string& func, int8_t funcType, int8_t outputType, int32_t outputLen, int32_t bufSize) {
    std::shared_ptr<SFuncInfo> info(new SFuncInfo);
    strcpy(info->name, func.c_str());
    info->funcType = funcType;
    info->scriptType = TSDB_FUNC_SCRIPT_BIN_LIB;
    info->outputType = outputType;
    info->outputLen = outputLen;
    info->bufSize = bufSize;
wafwerar's avatar
wafwerar 已提交
327 328
    info->pCode = nullptr;
    info->pComment = nullptr;
329
    udf_.insert(std::make_pair(func, info));
330 331
  }

X
Xiaoyu Wang 已提交
332
  void createSmaIndex(const SMCreateSmaReq* pReq) {
333
    STableIndexInfo info = {0};
X
Xiaoyu Wang 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
    info.intervalUnit = pReq->intervalUnit;
    info.slidingUnit = pReq->slidingUnit;
    info.interval = pReq->interval;
    info.offset = pReq->offset;
    info.sliding = pReq->sliding;
    info.dstTbUid = getNextId();
    info.dstVgId = pReq->dstVgId;
    genEpSet(&info.epSet);
    info.expr = strdup(pReq->expr);
    auto it = index_.find(pReq->stb);
    if (index_.end() == it) {
      index_.insert(std::make_pair(std::string(pReq->stb), std::vector<STableIndexInfo>{info}));
    } else {
      it->second.push_back(info);
    }
  }

351 352 353 354 355 356
  void createDnode(int32_t dnodeId, const std::string& host, int16_t port) {
    SEpSet epSet = {0};
    addEpIntoEpSet(&epSet, host.c_str(), port);
    dnode_.insert(std::make_pair(dnodeId, epSet));
  }

357
  void createDatabase(const std::string& db, bool rollup, int8_t cacheLast) {
358 359 360 361
    SDbCfgInfo cfg = {0};
    if (rollup) {
      cfg.pRetensions = taosArrayInit(TARRAY_MIN_SIZE, sizeof(SRetention));
    }
362
    cfg.cacheLast = cacheLast;
363 364 365
    dbCfg_.insert(std::make_pair(db, cfg));
  }

X
Xiaoyu Wang 已提交
366
 private:
367
  typedef std::map<std::string, std::shared_ptr<MockTableMeta>> TableMetaCache;
X
Xiaoyu Wang 已提交
368
  typedef std::map<std::string, TableMetaCache>                 DbMetaCache;
369
  typedef std::map<std::string, std::shared_ptr<SFuncInfo>>     UdfMetaCache;
X
Xiaoyu Wang 已提交
370
  typedef std::map<std::string, std::vector<STableIndexInfo>>   IndexMetaCache;
371
  typedef std::map<int32_t, SEpSet>                             DnodeCache;
372
  typedef std::map<std::string, SDbCfgInfo>                     DbCfgCache;
X
Xiaoyu Wang 已提交
373 374 375 376 377 378 379 380 381

  uint64_t getNextId() { return id_++; }

  void genEpSet(SEpSet* pEpSet) {
    addEpIntoEpSet(pEpSet, "dnode_1", 6030);
    addEpIntoEpSet(pEpSet, "dnode_2", 6030);
    addEpIntoEpSet(pEpSet, "dnode_3", 6030);
    pEpSet->inUse = 0;
  }
382

X
Xiaoyu Wang 已提交
383 384 385 386 387 388
  STableIndexInfo* copyTableIndexInfo(STableIndexInfo* pDst, const STableIndexInfo* pSrc) const {
    memcpy(pDst, pSrc, sizeof(STableIndexInfo));
    pDst->expr = strdup(pSrc->expr);
    return pDst;
  }

389 390 391 392 393 394 395 396
  std::string toDbname(const std::string& dbFullName) const {
    std::string::size_type n = dbFullName.find(".");
    if (n == std::string::npos) {
      return dbFullName;
    }
    return dbFullName.substr(n + 1);
  }

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
  std::string ttToString(int8_t tableType) const {
    switch (tableType) {
      case TSDB_SUPER_TABLE:
        return "super table";
      case TSDB_CHILD_TABLE:
        return "child table";
      case TSDB_NORMAL_TABLE:
        return "normal table";
      default:
        return "unknown";
    }
  }

  std::string pToString(uint8_t precision) const {
    switch (precision) {
      case TSDB_TIME_PRECISION_MILLI:
        return "millisecond";
      case TSDB_TIME_PRECISION_MICRO:
        return "microsecond";
      case TSDB_TIME_PRECISION_NANO:
        return "nanosecond";
      default:
        return "unknown";
    }
  }

X
Xiaoyu Wang 已提交
423
  std::string dtToString(int8_t type) const { return tDataTypes[type].name; }
424

425
  std::string ftToString(int16_t colid, int16_t numOfColumns) const {
426
    return (0 == colid ? "column" : (colid < numOfColumns ? "column" : "tag"));
427 428
  }

X
Xiaoyu Wang 已提交
429
  STableMeta* getTableSchemaMeta(const std::string& db, const std::string& tbname) const {
430
    std::shared_ptr<MockTableMeta> table = getTableMeta(db, tbname);
X
Xiaoyu Wang 已提交
431
    return table ? table->schema : nullptr;
432 433
  }

X
Xiaoyu Wang 已提交
434 435
  int32_t copyTableSchemaMeta(const std::string& db, const std::string& tbname,
                              std::unique_ptr<STableMeta>* dst) const {
X
Xiaoyu Wang 已提交
436 437
    STableMeta* src = getTableSchemaMeta(db, tbname);
    if (nullptr == src) {
438 439 440
      return TSDB_CODE_TSC_INVALID_TABLE_NAME;
    }
    int32_t len = sizeof(STableMeta) + sizeof(SSchema) * (src->tableInfo.numOfTags + src->tableInfo.numOfColumns);
wafwerar's avatar
wafwerar 已提交
441
    dst->reset((STableMeta*)taosMemoryCalloc(1, len));
442 443 444
    if (!dst) {
      return TSDB_CODE_TSC_OUT_OF_MEMORY;
    }
X
Xiaoyu Wang 已提交
445
    memcpy(dst->get(), src, len);
446 447 448
    return TSDB_CODE_SUCCESS;
  }

X
bugfix  
Xiaoyu Wang 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
  int32_t copyTableVgroup(const std::string& db, const std::string& tbname, SVgroupInfo* vg) const {
    std::shared_ptr<MockTableMeta> table = getTableMeta(db, tbname);
    if (table->vgs.empty()) {
      return TSDB_CODE_SUCCESS;
    }
    memcpy(vg, &(table->vgs[0]), sizeof(SVgroupInfo));
    return TSDB_CODE_SUCCESS;
  }

  int32_t copyTableVgroup(const std::string& db, const std::string& tbname, SArray** vgList) const {
    std::shared_ptr<MockTableMeta> table = getTableMeta(db, tbname);
    if (table->vgs.empty()) {
      return TSDB_CODE_SUCCESS;
    }
    *vgList = taosArrayInit(table->vgs.size(), sizeof(SVgroupInfo));
    for (const SVgroupInfo& vg : table->vgs) {
      taosArrayPush(*vgList, &vg);
    }
    return TSDB_CODE_SUCCESS;
  }

470 471 472 473 474 475 476 477 478 479 480 481
  std::shared_ptr<MockTableMeta> getTableMeta(const std::string& db, const std::string& tbname) const {
    DbMetaCache::const_iterator it = meta_.find(db);
    if (meta_.end() == it) {
      return std::shared_ptr<MockTableMeta>();
    }
    TableMetaCache::const_iterator tit = it->second.find(tbname);
    if (it->second.end() == tit) {
      return std::shared_ptr<MockTableMeta>();
    }
    return tit->second;
  }

482 483
  int32_t getAllTableMeta(SArray* pTableMetaReq, SArray** pTableMetaData) const {
    if (NULL != pTableMetaReq) {
484 485 486 487 488 489 490 491 492 493
      int32_t ndbs = taosArrayGetSize(pTableMetaReq);
      *pTableMetaData = taosArrayInit(ndbs, sizeof(SMetaRes));
      for (int32_t i = 0; i < ndbs; ++i) {
        STablesReq* pReq = (STablesReq*)taosArrayGet(pTableMetaReq, i);
        int32_t     ntables = taosArrayGetSize(pReq->pTables);
        for (int32_t j = 0; j < ntables; ++j) {
          SMetaRes res = {0};
          res.code = catalogGetTableMeta((const SName*)taosArrayGet(pReq->pTables, j), (STableMeta**)&res.pRes);
          taosArrayPush(*pTableMetaData, &res);
        }
494 495
      }
    }
X
Xiaoyu Wang 已提交
496
    return TSDB_CODE_SUCCESS;
497 498 499 500
  }

  int32_t getAllTableVgroup(SArray* pTableVgroupReq, SArray** pTableVgroupData) const {
    if (NULL != pTableVgroupReq) {
501 502 503 504 505 506 507 508 509 510 511
      int32_t ndbs = taosArrayGetSize(pTableVgroupReq);
      *pTableVgroupData = taosArrayInit(ndbs, sizeof(SMetaRes));
      for (int32_t i = 0; i < ndbs; ++i) {
        STablesReq* pReq = (STablesReq*)taosArrayGet(pTableVgroupReq, i);
        int32_t     ntables = taosArrayGetSize(pReq->pTables);
        for (int32_t j = 0; j < ntables; ++j) {
          SMetaRes res = {0};
          res.pRes = taosMemoryCalloc(1, sizeof(SVgroupInfo));
          res.code = catalogGetTableHashVgroup((const SName*)taosArrayGet(pReq->pTables, j), (SVgroupInfo*)res.pRes);
          taosArrayPush(*pTableVgroupData, &res);
        }
512 513
      }
    }
X
Xiaoyu Wang 已提交
514
    return TSDB_CODE_SUCCESS;
515 516
  }

517 518 519 520
  int32_t getAllDbVgroup(SArray* pDbVgroupReq, SArray** pDbVgroupData) const {
    int32_t code = TSDB_CODE_SUCCESS;
    if (NULL != pDbVgroupReq) {
      int32_t ndbs = taosArrayGetSize(pDbVgroupReq);
X
Xiaoyu Wang 已提交
521
      *pDbVgroupData = taosArrayInit(ndbs, sizeof(SMetaRes));
522
      for (int32_t i = 0; i < ndbs; ++i) {
X
Xiaoyu Wang 已提交
523 524
        SMetaRes res = {0};
        taosArrayPush(*pDbVgroupData, &res);
525 526 527 528 529 530 531 532 533
      }
    }
    return code;
  }

  int32_t getAllDbCfg(SArray* pDbCfgReq, SArray** pDbCfgData) const {
    int32_t code = TSDB_CODE_SUCCESS;
    if (NULL != pDbCfgReq) {
      int32_t ndbs = taosArrayGetSize(pDbCfgReq);
X
Xiaoyu Wang 已提交
534
      *pDbCfgData = taosArrayInit(ndbs, sizeof(SMetaRes));
535
      for (int32_t i = 0; i < ndbs; ++i) {
X
Xiaoyu Wang 已提交
536 537
        SMetaRes res = {0};
        res.pRes = taosMemoryCalloc(1, sizeof(SDbCfgInfo));
538
        res.code = catalogGetDBCfg((const char*)taosArrayGet(pDbCfgReq, i), (SDbCfgInfo*)res.pRes);
X
Xiaoyu Wang 已提交
539
        taosArrayPush(*pDbCfgData, &res);
540 541 542 543 544
      }
    }
    return code;
  }

545 546 547 548
  int32_t getAllDbInfo(SArray* pDbInfoReq, SArray** pDbInfoData) const {
    int32_t code = TSDB_CODE_SUCCESS;
    if (NULL != pDbInfoReq) {
      int32_t ndbs = taosArrayGetSize(pDbInfoReq);
X
Xiaoyu Wang 已提交
549
      *pDbInfoData = taosArrayInit(ndbs, sizeof(SMetaRes));
550
      for (int32_t i = 0; i < ndbs; ++i) {
X
Xiaoyu Wang 已提交
551 552 553
        SMetaRes res = {0};
        res.pRes = taosMemoryCalloc(1, sizeof(SDbInfo));
        taosArrayPush(*pDbInfoData, &res);
554 555 556 557 558 559 560 561 562
      }
    }
    return code;
  }

  int32_t getAllUserAuth(SArray* pUserAuthReq, SArray** pUserAuthData) const {
    int32_t code = TSDB_CODE_SUCCESS;
    if (NULL != pUserAuthReq) {
      int32_t num = taosArrayGetSize(pUserAuthReq);
X
Xiaoyu Wang 已提交
563
      *pUserAuthData = taosArrayInit(num, sizeof(SMetaRes));
564
      for (int32_t i = 0; i < num; ++i) {
X
Xiaoyu Wang 已提交
565 566 567 568
        SMetaRes res = {0};
        res.pRes = taosMemoryCalloc(1, sizeof(bool));
        *(bool*)(res.pRes) = true;
        taosArrayPush(*pUserAuthData, &res);
569 570 571 572 573
      }
    }
    return code;
  }

574 575 576
  int32_t getAllUdf(SArray* pUdfReq, SArray** pUdfData) const {
    if (NULL != pUdfReq) {
      int32_t num = taosArrayGetSize(pUdfReq);
X
Xiaoyu Wang 已提交
577
      *pUdfData = taosArrayInit(num, sizeof(SMetaRes));
578
      for (int32_t i = 0; i < num; ++i) {
X
Xiaoyu Wang 已提交
579 580 581 582
        SMetaRes res = {0};
        res.pRes = taosMemoryCalloc(1, sizeof(SFuncInfo));
        res.code = catalogGetUdfInfo((char*)taosArrayGet(pUdfReq, i), (SFuncInfo*)res.pRes);
        taosArrayPush(*pUdfData, &res);
583 584
      }
    }
X
Xiaoyu Wang 已提交
585
    return TSDB_CODE_SUCCESS;
586 587
  }

X
Xiaoyu Wang 已提交
588 589 590 591 592 593 594 595 596 597 598 599 600
  int32_t getAllTableIndex(SArray* pTableIndex, SArray** pTableIndexData) const {
    if (NULL != pTableIndex) {
      int32_t num = taosArrayGetSize(pTableIndex);
      *pTableIndexData = taosArrayInit(num, sizeof(SMetaRes));
      for (int32_t i = 0; i < num; ++i) {
        SMetaRes res = {0};
        res.code = catalogGetTableIndex((const SName*)taosArrayGet(pTableIndex, i), (SArray**)(&res.pRes));
        taosArrayPush(*pTableIndexData, &res);
      }
    }
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
601 602 603 604 605 606 607 608 609 610 611 612 613 614
  int32_t getAllTableCfg(SArray* pTableCfgReq, SArray** pTableCfgData) const {
    if (NULL != pTableCfgReq) {
      int32_t ntables = taosArrayGetSize(pTableCfgReq);
      *pTableCfgData = taosArrayInit(ntables, sizeof(SMetaRes));
      for (int32_t i = 0; i < ntables; ++i) {
        SMetaRes res = {0};
        res.pRes = taosMemoryCalloc(1, sizeof(STableCfg));
        res.code = TSDB_CODE_SUCCESS;
        taosArrayPush(*pTableCfgData, &res);
      }
    }
    return TSDB_CODE_SUCCESS;
  }

615 616
  int32_t getAllDnodeList(SArray** pDnodes) const {
    SMetaRes res = {0};
617
    catalogGetDnodeList((SArray**)&res.pRes);
618 619
    *pDnodes = taosArrayInit(1, sizeof(SMetaRes));
    taosArrayPush(*pDnodes, &res);
620
    return TSDB_CODE_SUCCESS;
621 622
  }

X
Xiaoyu Wang 已提交
623
  uint64_t                      id_;
624
  std::unique_ptr<TableBuilder> builder_;
X
Xiaoyu Wang 已提交
625
  DbMetaCache                   meta_;
626
  UdfMetaCache                  udf_;
X
Xiaoyu Wang 已提交
627
  IndexMetaCache                index_;
628
  DnodeCache                    dnode_;
629
  DbCfgCache                    dbCfg_;
X
Xiaoyu Wang 已提交
630
  bool                          havaCache_;
631 632
};

X
Xiaoyu Wang 已提交
633
MockCatalogService::MockCatalogService() : impl_(new MockCatalogServiceImpl()) {}
634

X
Xiaoyu Wang 已提交
635
MockCatalogService::~MockCatalogService() {}
636

X
Xiaoyu Wang 已提交
637 638
ITableBuilder& MockCatalogService::createTableBuilder(const std::string& db, const std::string& tbname,
                                                      int8_t tableType, int32_t numOfColumns, int32_t numOfTags) {
639 640 641
  return impl_->createTableBuilder(db, tbname, tableType, numOfColumns, numOfTags);
}

X
Xiaoyu Wang 已提交
642 643
void MockCatalogService::createSubTable(const std::string& db, const std::string& stbname, const std::string& tbname,
                                        int16_t vgid) {
644 645 646
  impl_->createSubTable(db, stbname, tbname, vgid);
}

X
Xiaoyu Wang 已提交
647
void MockCatalogService::showTables() const { impl_->showTables(); }
648

649 650 651
void MockCatalogService::createFunction(const std::string& func, int8_t funcType, int8_t outputType, int32_t outputLen,
                                        int32_t bufSize) {
  impl_->createFunction(func, funcType, outputType, outputLen, bufSize);
652 653
}

X
Xiaoyu Wang 已提交
654 655
void MockCatalogService::createSmaIndex(const SMCreateSmaReq* pReq) { impl_->createSmaIndex(pReq); }

656 657 658 659
void MockCatalogService::createDnode(int32_t dnodeId, const std::string& host, int16_t port) {
  impl_->createDnode(dnodeId, host, port);
}

660 661 662
void MockCatalogService::createDatabase(const std::string& db, bool rollup, int8_t cacheLast) {
  impl_->createDatabase(db, rollup, cacheLast);
}
663

X
Xiaoyu Wang 已提交
664 665 666
int32_t MockCatalogService::catalogGetTableMeta(const SName* pTableName, STableMeta** pTableMeta,
                                                bool onlyCache) const {
  return impl_->catalogGetTableMeta(pTableName, pTableMeta, onlyCache);
667 668
}

X
Xiaoyu Wang 已提交
669 670 671
int32_t MockCatalogService::catalogGetTableHashVgroup(const SName* pTableName, SVgroupInfo* vgInfo,
                                                      bool onlyCache) const {
  return impl_->catalogGetTableHashVgroup(pTableName, vgInfo, onlyCache);
H
Haojun Liao 已提交
672 673 674 675
}

int32_t MockCatalogService::catalogGetTableDistVgInfo(const SName* pTableName, SArray** pVgList) const {
  return impl_->catalogGetTableDistVgInfo(pTableName, pVgList);
X
Xiaoyu Wang 已提交
676
}
677

678 679
int32_t MockCatalogService::catalogGetDBVgList(const char* pDbFName, SArray** pVgList) const {
  return impl_->catalogGetDBVgList(pDbFName, pVgList);
X
Xiaoyu Wang 已提交
680 681
}

682 683 684 685
int32_t MockCatalogService::catalogGetDBCfg(const char* pDbFName, SDbCfgInfo* pDbCfg) const {
  return impl_->catalogGetDBCfg(pDbFName, pDbCfg);
}

686 687 688 689
int32_t MockCatalogService::catalogGetUdfInfo(const std::string& funcName, SFuncInfo* pInfo) const {
  return impl_->catalogGetUdfInfo(funcName, pInfo);
}

X
Xiaoyu Wang 已提交
690 691 692 693
int32_t MockCatalogService::catalogGetTableIndex(const SName* pTableName, SArray** pIndexes) const {
  return impl_->catalogGetTableIndex(pTableName, pIndexes);
}

694 695
int32_t MockCatalogService::catalogGetDnodeList(SArray** pDnodes) const { return impl_->catalogGetDnodeList(pDnodes); }

696 697 698
int32_t MockCatalogService::catalogGetAllMeta(const SCatalogReq* pCatalogReq, SMetaData* pMetaData) const {
  return impl_->catalogGetAllMeta(pCatalogReq, pMetaData);
}
699

700 701 702 703 704
void MockCatalogService::destoryTablesReq(void* p) {
  STablesReq* pRes = (STablesReq*)p;
  taosArrayDestroy(pRes->pTables);
}

705 706 707 708
void MockCatalogService::destoryCatalogReq(SCatalogReq* pReq) {
  taosArrayDestroy(pReq->pDbVgroup);
  taosArrayDestroy(pReq->pDbCfg);
  taosArrayDestroy(pReq->pDbInfo);
709 710
  taosArrayDestroyEx(pReq->pTableMeta, destoryTablesReq);
  taosArrayDestroyEx(pReq->pTableHash, destoryTablesReq);
711 712 713 714
  taosArrayDestroy(pReq->pUdf);
  taosArrayDestroy(pReq->pIndex);
  taosArrayDestroy(pReq->pUser);
  taosArrayDestroy(pReq->pTableIndex);
715
  taosArrayDestroy(pReq->pTableCfg);
716 717 718 719 720 721 722 723
  delete pReq;
}

void MockCatalogService::destoryMetaRes(void* p) {
  SMetaRes* pRes = (SMetaRes*)p;
  taosMemoryFree(pRes->pRes);
}

724 725 726 727 728
void MockCatalogService::destoryMetaArrayRes(void* p) {
  SMetaRes* pRes = (SMetaRes*)p;
  taosArrayDestroy((SArray*)pRes->pRes);
}

729 730 731 732 733 734 735 736 737 738 739
void MockCatalogService::destoryMetaData(SMetaData* pData) {
  taosArrayDestroyEx(pData->pDbVgroup, destoryMetaRes);
  taosArrayDestroyEx(pData->pDbCfg, destoryMetaRes);
  taosArrayDestroyEx(pData->pDbInfo, destoryMetaRes);
  taosArrayDestroyEx(pData->pTableMeta, destoryMetaRes);
  taosArrayDestroyEx(pData->pTableHash, destoryMetaRes);
  taosArrayDestroyEx(pData->pTableIndex, destoryMetaRes);
  taosArrayDestroyEx(pData->pUdfList, destoryMetaRes);
  taosArrayDestroyEx(pData->pIndex, destoryMetaRes);
  taosArrayDestroyEx(pData->pUser, destoryMetaRes);
  taosArrayDestroyEx(pData->pQnodeList, destoryMetaRes);
740 741 742
  taosArrayDestroyEx(pData->pTableCfg, destoryMetaRes);
  taosArrayDestroyEx(pData->pDnodeList, destoryMetaArrayRes);
  taosMemoryFree(pData->pSvrVer);
743 744
  delete pData;
}