mockCatalogService.cpp 21.7 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

X
Xiaoyu Wang 已提交
23
#include "tdatablock.h"
24
#include "tname.h"
25 26
#include "ttypes.h"

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) {}
95

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

H
Haojun Liao 已提交
98
  int32_t catalogGetTableMeta(const SName* pTableName, STableMeta** pTableMeta) const {
99
    std::unique_ptr<STableMeta> table;
H
Haojun Liao 已提交
100

X
Xiaoyu Wang 已提交
101 102
    char db[TSDB_DB_NAME_LEN] = {0};
    tNameGetDbName(pTableName, db);
H
Haojun Liao 已提交
103 104

    const char* tname = tNameGetTableName(pTableName);
X
Xiaoyu Wang 已提交
105
    int32_t     code = copyTableSchemaMeta(db, tname, &table);
106 107 108
    if (TSDB_CODE_SUCCESS != code) {
      return code;
    }
109
    *pTableMeta = table.release();
110
    return TSDB_CODE_SUCCESS;
111 112
  }

H
Haojun Liao 已提交
113
  int32_t catalogGetTableHashVgroup(const SName* pTableName, SVgroupInfo* vgInfo) const {
114 115
    vgInfo->vgId = 1;
    return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
116 117
  }

X
bugfix  
Xiaoyu Wang 已提交
118 119 120 121
  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 已提交
122 123
  }

X
Xiaoyu Wang 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  int32_t catalogGetDBVgInfo(const char* pDbFName, SArray** pVgList) const {
    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;
  }

143 144 145 146 147 148 149 150 151
  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 已提交
152 153 154 155 156 157 158 159 160
  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 已提交
161 162 163
      STableIndexInfo info;

      taosArrayPush(*pIndexes, copyTableIndexInfo(&info, &index));
X
Xiaoyu Wang 已提交
164 165 166 167
    }
    return TSDB_CODE_SUCCESS;
  }

168 169 170 171 172
  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);
    }
173 174 175 176 177 178
    if (TSDB_CODE_SUCCESS == code) {
      code = getAllDbVgroup(pCatalogReq->pDbVgroup, &pMetaData->pDbVgroup);
    }
    if (TSDB_CODE_SUCCESS == code) {
      code = getAllDbCfg(pCatalogReq->pDbCfg, &pMetaData->pDbCfg);
    }
179 180 181 182 183 184
    if (TSDB_CODE_SUCCESS == code) {
      code = getAllDbInfo(pCatalogReq->pDbInfo, &pMetaData->pDbInfo);
    }
    if (TSDB_CODE_SUCCESS == code) {
      code = getAllUserAuth(pCatalogReq->pUser, &pMetaData->pUser);
    }
185 186 187
    if (TSDB_CODE_SUCCESS == code) {
      code = getAllUdf(pCatalogReq->pUdf, &pMetaData->pUdfList);
    }
X
Xiaoyu Wang 已提交
188 189 190
    if (TSDB_CODE_SUCCESS == code) {
      code = getAllTableIndex(pCatalogReq->pTableIndex, &pMetaData->pTableIndex);
    }
191 192 193
    return code;
  }

X
Xiaoyu Wang 已提交
194 195
  TableBuilder& createTableBuilder(const std::string& db, const std::string& tbname, int8_t tableType,
                                   int32_t numOfColumns, int32_t numOfTags) {
196
    builder_ = TableBuilder::createTableBuilder(tableType, numOfColumns, numOfTags);
197
    meta_[db][tbname] = builder_->table();
X
Xiaoyu Wang 已提交
198
    meta_[db][tbname]->schema->uid = getNextId();
199 200 201
    return *(builder_.get());
  }

202 203
  void createSubTable(const std::string& db, const std::string& stbname, const std::string& tbname, int16_t vgid) {
    std::unique_ptr<STableMeta> table;
204 205
    if (TSDB_CODE_SUCCESS != copyTableSchemaMeta(db, stbname, &table)) {
      throw std::runtime_error("copyTableSchemaMeta failed");
206
    }
207
    meta_[db][tbname].reset(new MockTableMeta());
X
Xiaoyu Wang 已提交
208
    meta_[db][tbname]->schema = table.release();
X
Xiaoyu Wang 已提交
209
    meta_[db][tbname]->schema->uid = getNextId();
210
    meta_[db][tbname]->schema->tableType = TSDB_CHILD_TABLE;
H
Haojun Liao 已提交
211

X
Xiaoyu Wang 已提交
212
    SVgroupInfo vgroup = {vgid, 0, 0, {0}, 0};
X
Xiaoyu Wang 已提交
213
    genEpSet(&vgroup.epSet);
H
Haojun Liao 已提交
214 215

    meta_[db][tbname]->vgs.emplace_back(vgroup);
216
    // super table
H
Haojun Liao 已提交
217
    meta_[db][stbname]->vgs.emplace_back(vgroup);
218 219
  }

220
  void showTables() const {
X
Xiaoyu Wang 已提交
221 222 223 224 225
// 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 已提交
226 227 228
#define CA(n, s)                                                                                        \
  std::setw(NOF((n) - int((s).length()))) << "" << (s) << std::setw(NOB((n) - int((s).length()))) << "" \
                                          << "|"
X
Xiaoyu Wang 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242
// 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(' ')
243 244

    for (const auto& db : meta_) {
245
      std::cout << "Databse:" << db.first << std::endl;
246
      std::cout << SH("Table") << SH("Type") << SH("Precision") << IH("Vgid") << IH("RowSize") << std::endl;
247
      std::cout << SL(3, 1) << std::endl;
248
      for (const auto& table : db.second) {
249
        const auto& schema = table.second->schema;
X
Xiaoyu Wang 已提交
250 251
        std::cout << SF(table.first) << SF(ttToString(schema->tableType)) << SF(pToString(schema->tableInfo.precision))
                  << IF(schema->vgId) << IF(schema->tableInfo.rowSize) << std::endl;
252 253 254 255 256 257
      }
      std::cout << std::endl;
    }

    for (const auto& db : meta_) {
      for (const auto& table : db.second) {
258
        const auto& schema = table.second->schema;
259 260 261
        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;
262 263
        int16_t numOfColumns = schema->tableInfo.numOfColumns;
        int16_t numOfFields = numOfColumns + schema->tableInfo.numOfTags;
264
        for (int16_t i = 0; i < numOfFields; ++i) {
265
          const SSchema* col = schema->schema + i;
X
Xiaoyu Wang 已提交
266 267
          std::cout << SF(std::string(col->name)) << SH(ftToString(i, numOfColumns)) << SH(dtToString(col->type))
                    << IF(col->bytes) << std::endl;
268 269
        }
        std::cout << std::endl;
270 271 272 273
      }
    }
  }

274 275 276 277 278 279 280 281
  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 已提交
282 283
    info->pCode = nullptr;
    info->pComment = nullptr;
284
    udf_.insert(std::make_pair(func, info));
285 286
  }

X
Xiaoyu Wang 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
  void createSmaIndex(const SMCreateSmaReq* pReq) {
    STableIndexInfo info;
    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);
    }
  }

X
Xiaoyu Wang 已提交
306
 private:
307
  typedef std::map<std::string, std::shared_ptr<MockTableMeta>> TableMetaCache;
X
Xiaoyu Wang 已提交
308
  typedef std::map<std::string, TableMetaCache>                 DbMetaCache;
309
  typedef std::map<std::string, std::shared_ptr<SFuncInfo>>     UdfMetaCache;
X
Xiaoyu Wang 已提交
310 311 312 313 314 315 316 317 318 319
  typedef std::map<std::string, std::vector<STableIndexInfo>>   IndexMetaCache;

  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;
  }
320

X
Xiaoyu Wang 已提交
321 322 323 324 325 326
  STableIndexInfo* copyTableIndexInfo(STableIndexInfo* pDst, const STableIndexInfo* pSrc) const {
    memcpy(pDst, pSrc, sizeof(STableIndexInfo));
    pDst->expr = strdup(pSrc->expr);
    return pDst;
  }

327 328 329 330 331 332 333 334
  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);
  }

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
  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 已提交
361
  std::string dtToString(int8_t type) const { return tDataTypes[type].name; }
362

363
  std::string ftToString(int16_t colid, int16_t numOfColumns) const {
364
    return (0 == colid ? "column" : (colid < numOfColumns ? "column" : "tag"));
365 366
  }

X
Xiaoyu Wang 已提交
367
  STableMeta* getTableSchemaMeta(const std::string& db, const std::string& tbname) const {
368
    std::shared_ptr<MockTableMeta> table = getTableMeta(db, tbname);
X
Xiaoyu Wang 已提交
369
    return table ? table->schema : nullptr;
370 371
  }

X
Xiaoyu Wang 已提交
372 373
  int32_t copyTableSchemaMeta(const std::string& db, const std::string& tbname,
                              std::unique_ptr<STableMeta>* dst) const {
X
Xiaoyu Wang 已提交
374 375
    STableMeta* src = getTableSchemaMeta(db, tbname);
    if (nullptr == src) {
376 377 378
      return TSDB_CODE_TSC_INVALID_TABLE_NAME;
    }
    int32_t len = sizeof(STableMeta) + sizeof(SSchema) * (src->tableInfo.numOfTags + src->tableInfo.numOfColumns);
wafwerar's avatar
wafwerar 已提交
379
    dst->reset((STableMeta*)taosMemoryCalloc(1, len));
380 381 382
    if (!dst) {
      return TSDB_CODE_TSC_OUT_OF_MEMORY;
    }
X
Xiaoyu Wang 已提交
383
    memcpy(dst->get(), src, len);
384 385 386
    return TSDB_CODE_SUCCESS;
  }

X
bugfix  
Xiaoyu Wang 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
  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;
  }

408 409 410 411 412 413 414 415 416 417 418 419
  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;
  }

420 421 422
  int32_t getAllTableMeta(SArray* pTableMetaReq, SArray** pTableMetaData) const {
    if (NULL != pTableMetaReq) {
      int32_t ntables = taosArrayGetSize(pTableMetaReq);
X
Xiaoyu Wang 已提交
423
      *pTableMetaData = taosArrayInit(ntables, sizeof(SMetaRes));
424
      for (int32_t i = 0; i < ntables; ++i) {
X
Xiaoyu Wang 已提交
425 426 427
        SMetaRes res = {0};
        res.code = catalogGetTableMeta((const SName*)taosArrayGet(pTableMetaReq, i), (STableMeta**)&res.pRes);
        taosArrayPush(*pTableMetaData, &res);
428 429
      }
    }
X
Xiaoyu Wang 已提交
430
    return TSDB_CODE_SUCCESS;
431 432 433 434 435
  }

  int32_t getAllTableVgroup(SArray* pTableVgroupReq, SArray** pTableVgroupData) const {
    if (NULL != pTableVgroupReq) {
      int32_t ntables = taosArrayGetSize(pTableVgroupReq);
X
Xiaoyu Wang 已提交
436
      *pTableVgroupData = taosArrayInit(ntables, sizeof(SMetaRes));
437
      for (int32_t i = 0; i < ntables; ++i) {
X
Xiaoyu Wang 已提交
438 439 440 441
        SMetaRes res = {0};
        res.pRes = taosMemoryCalloc(1, sizeof(SVgroupInfo));
        res.code = catalogGetTableHashVgroup((const SName*)taosArrayGet(pTableVgroupReq, i), (SVgroupInfo*)res.pRes);
        taosArrayPush(*pTableVgroupData, &res);
442 443
      }
    }
X
Xiaoyu Wang 已提交
444
    return TSDB_CODE_SUCCESS;
445 446
  }

447 448 449 450
  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 已提交
451
      *pDbVgroupData = taosArrayInit(ndbs, sizeof(SMetaRes));
452
      for (int32_t i = 0; i < ndbs; ++i) {
X
Xiaoyu Wang 已提交
453 454
        SMetaRes res = {0};
        taosArrayPush(*pDbVgroupData, &res);
455 456 457 458 459 460 461 462 463
      }
    }
    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 已提交
464
      *pDbCfgData = taosArrayInit(ndbs, sizeof(SMetaRes));
465
      for (int32_t i = 0; i < ndbs; ++i) {
X
Xiaoyu Wang 已提交
466 467 468
        SMetaRes res = {0};
        res.pRes = taosMemoryCalloc(1, sizeof(SDbCfgInfo));
        taosArrayPush(*pDbCfgData, &res);
469 470 471 472 473
      }
    }
    return code;
  }

474 475 476 477
  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 已提交
478
      *pDbInfoData = taosArrayInit(ndbs, sizeof(SMetaRes));
479
      for (int32_t i = 0; i < ndbs; ++i) {
X
Xiaoyu Wang 已提交
480 481 482
        SMetaRes res = {0};
        res.pRes = taosMemoryCalloc(1, sizeof(SDbInfo));
        taosArrayPush(*pDbInfoData, &res);
483 484 485 486 487 488 489 490 491
      }
    }
    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 已提交
492
      *pUserAuthData = taosArrayInit(num, sizeof(SMetaRes));
493
      for (int32_t i = 0; i < num; ++i) {
X
Xiaoyu Wang 已提交
494 495 496 497
        SMetaRes res = {0};
        res.pRes = taosMemoryCalloc(1, sizeof(bool));
        *(bool*)(res.pRes) = true;
        taosArrayPush(*pUserAuthData, &res);
498 499 500 501 502
      }
    }
    return code;
  }

503 504 505
  int32_t getAllUdf(SArray* pUdfReq, SArray** pUdfData) const {
    if (NULL != pUdfReq) {
      int32_t num = taosArrayGetSize(pUdfReq);
X
Xiaoyu Wang 已提交
506
      *pUdfData = taosArrayInit(num, sizeof(SMetaRes));
507
      for (int32_t i = 0; i < num; ++i) {
X
Xiaoyu Wang 已提交
508 509 510 511
        SMetaRes res = {0};
        res.pRes = taosMemoryCalloc(1, sizeof(SFuncInfo));
        res.code = catalogGetUdfInfo((char*)taosArrayGet(pUdfReq, i), (SFuncInfo*)res.pRes);
        taosArrayPush(*pUdfData, &res);
512 513
      }
    }
X
Xiaoyu Wang 已提交
514
    return TSDB_CODE_SUCCESS;
515 516
  }

X
Xiaoyu Wang 已提交
517 518 519 520 521 522 523 524 525 526 527 528 529
  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;
  }

X
Xiaoyu Wang 已提交
530
  uint64_t                      id_;
531
  std::unique_ptr<TableBuilder> builder_;
X
Xiaoyu Wang 已提交
532
  DbMetaCache                   meta_;
533
  UdfMetaCache                  udf_;
X
Xiaoyu Wang 已提交
534
  IndexMetaCache                index_;
535 536
};

X
Xiaoyu Wang 已提交
537
MockCatalogService::MockCatalogService() : impl_(new MockCatalogServiceImpl()) {}
538

X
Xiaoyu Wang 已提交
539
MockCatalogService::~MockCatalogService() {}
540

X
Xiaoyu Wang 已提交
541 542
ITableBuilder& MockCatalogService::createTableBuilder(const std::string& db, const std::string& tbname,
                                                      int8_t tableType, int32_t numOfColumns, int32_t numOfTags) {
543 544 545
  return impl_->createTableBuilder(db, tbname, tableType, numOfColumns, numOfTags);
}

X
Xiaoyu Wang 已提交
546 547
void MockCatalogService::createSubTable(const std::string& db, const std::string& stbname, const std::string& tbname,
                                        int16_t vgid) {
548 549 550
  impl_->createSubTable(db, stbname, tbname, vgid);
}

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

553 554 555
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);
556 557
}

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

H
Haojun Liao 已提交
560 561
int32_t MockCatalogService::catalogGetTableMeta(const SName* pTableName, STableMeta** pTableMeta) const {
  return impl_->catalogGetTableMeta(pTableName, pTableMeta);
562 563
}

H
Haojun Liao 已提交
564 565
int32_t MockCatalogService::catalogGetTableHashVgroup(const SName* pTableName, SVgroupInfo* vgInfo) const {
  return impl_->catalogGetTableHashVgroup(pTableName, vgInfo);
H
Haojun Liao 已提交
566 567 568 569
}

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

X
Xiaoyu Wang 已提交
572 573 574 575
int32_t MockCatalogService::catalogGetDBVgInfo(const char* pDbFName, SArray** pVgList) const {
  return impl_->catalogGetDBVgInfo(pDbFName, pVgList);
}

576 577 578 579
int32_t MockCatalogService::catalogGetUdfInfo(const std::string& funcName, SFuncInfo* pInfo) const {
  return impl_->catalogGetUdfInfo(funcName, pInfo);
}

X
Xiaoyu Wang 已提交
580 581 582 583
int32_t MockCatalogService::catalogGetTableIndex(const SName* pTableName, SArray** pIndexes) const {
  return impl_->catalogGetTableIndex(pTableName, pIndexes);
}

584 585 586
int32_t MockCatalogService::catalogGetAllMeta(const SCatalogReq* pCatalogReq, SMetaData* pMetaData) const {
  return impl_->catalogGetAllMeta(pCatalogReq, pMetaData);
}
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618

void MockCatalogService::destoryCatalogReq(SCatalogReq* pReq) {
  taosArrayDestroy(pReq->pDbVgroup);
  taosArrayDestroy(pReq->pDbCfg);
  taosArrayDestroy(pReq->pDbInfo);
  taosArrayDestroy(pReq->pTableMeta);
  taosArrayDestroy(pReq->pTableHash);
  taosArrayDestroy(pReq->pUdf);
  taosArrayDestroy(pReq->pIndex);
  taosArrayDestroy(pReq->pUser);
  taosArrayDestroy(pReq->pTableIndex);
  delete pReq;
}

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

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);
  delete pData;
}