mockCatalogService.cpp 24.5 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 152 153
  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;
  }

154 155 156 157 158 159 160 161 162
  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 已提交
163 164 165 166 167 168 169 170 171
  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 已提交
172 173 174
      STableIndexInfo info;

      taosArrayPush(*pIndexes, copyTableIndexInfo(&info, &index));
X
Xiaoyu Wang 已提交
175 176 177 178
    }
    return TSDB_CODE_SUCCESS;
  }

179
  int32_t catalogGetDnodeList(SArray** pDnodes) const {
180
    *pDnodes = taosArrayInit(dnode_.size(), sizeof(SEpSet));
181
    for (const auto& dnode : dnode_) {
182
      taosArrayPush(*pDnodes, &dnode.second);
183 184 185 186
    }
    return TSDB_CODE_SUCCESS;
  }

187 188 189 190 191
  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);
    }
192 193 194 195 196 197
    if (TSDB_CODE_SUCCESS == code) {
      code = getAllDbVgroup(pCatalogReq->pDbVgroup, &pMetaData->pDbVgroup);
    }
    if (TSDB_CODE_SUCCESS == code) {
      code = getAllDbCfg(pCatalogReq->pDbCfg, &pMetaData->pDbCfg);
    }
198 199 200 201 202 203
    if (TSDB_CODE_SUCCESS == code) {
      code = getAllDbInfo(pCatalogReq->pDbInfo, &pMetaData->pDbInfo);
    }
    if (TSDB_CODE_SUCCESS == code) {
      code = getAllUserAuth(pCatalogReq->pUser, &pMetaData->pUser);
    }
204 205 206
    if (TSDB_CODE_SUCCESS == code) {
      code = getAllUdf(pCatalogReq->pUdf, &pMetaData->pUdfList);
    }
X
Xiaoyu Wang 已提交
207 208 209
    if (TSDB_CODE_SUCCESS == code) {
      code = getAllTableIndex(pCatalogReq->pTableIndex, &pMetaData->pTableIndex);
    }
210
    if (TSDB_CODE_SUCCESS == code && pCatalogReq->dNodeRequired) {
211
      code = getAllDnodeList(&pMetaData->pDnodeList);
212
    }
D
dapan1121 已提交
213 214 215
    if (TSDB_CODE_SUCCESS == code) {
      code = getAllTableCfg(pCatalogReq->pTableCfg, &pMetaData->pTableCfg);
    }
216 217 218
    return code;
  }

X
Xiaoyu Wang 已提交
219 220
  TableBuilder& createTableBuilder(const std::string& db, const std::string& tbname, int8_t tableType,
                                   int32_t numOfColumns, int32_t numOfTags) {
221
    builder_ = TableBuilder::createTableBuilder(tableType, numOfColumns, numOfTags);
222
    meta_[db][tbname] = builder_->table();
X
Xiaoyu Wang 已提交
223
    meta_[db][tbname]->schema->uid = getNextId();
224 225 226
    return *(builder_.get());
  }

227 228
  void createSubTable(const std::string& db, const std::string& stbname, const std::string& tbname, int16_t vgid) {
    std::unique_ptr<STableMeta> table;
229 230
    if (TSDB_CODE_SUCCESS != copyTableSchemaMeta(db, stbname, &table)) {
      throw std::runtime_error("copyTableSchemaMeta failed");
231
    }
232
    meta_[db][tbname].reset(new MockTableMeta());
X
Xiaoyu Wang 已提交
233
    meta_[db][tbname]->schema = table.release();
X
Xiaoyu Wang 已提交
234
    meta_[db][tbname]->schema->uid = getNextId();
235
    meta_[db][tbname]->schema->tableType = TSDB_CHILD_TABLE;
H
Haojun Liao 已提交
236

X
Xiaoyu Wang 已提交
237
    SVgroupInfo vgroup = {vgid, 0, 0, {0}, 0};
X
Xiaoyu Wang 已提交
238
    genEpSet(&vgroup.epSet);
H
Haojun Liao 已提交
239 240

    meta_[db][tbname]->vgs.emplace_back(vgroup);
241
    // super table
H
Haojun Liao 已提交
242
    meta_[db][stbname]->vgs.emplace_back(vgroup);
243 244
  }

245
  void showTables() const {
X
Xiaoyu Wang 已提交
246 247 248 249 250
// 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 已提交
251 252 253
#define CA(n, s)                                                                                        \
  std::setw(NOF((n) - int((s).length()))) << "" << (s) << std::setw(NOB((n) - int((s).length()))) << "" \
                                          << "|"
X
Xiaoyu Wang 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267
// 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(' ')
268 269

    for (const auto& db : meta_) {
270
      std::cout << "Databse:" << db.first << std::endl;
271
      std::cout << SH("Table") << SH("Type") << SH("Precision") << IH("Vgid") << IH("RowSize") << std::endl;
272
      std::cout << SL(3, 1) << std::endl;
273
      for (const auto& table : db.second) {
274
        const auto& schema = table.second->schema;
X
Xiaoyu Wang 已提交
275 276
        std::cout << SF(table.first) << SF(ttToString(schema->tableType)) << SF(pToString(schema->tableInfo.precision))
                  << IF(schema->vgId) << IF(schema->tableInfo.rowSize) << std::endl;
277 278 279 280 281 282
      }
      std::cout << std::endl;
    }

    for (const auto& db : meta_) {
      for (const auto& table : db.second) {
283
        const auto& schema = table.second->schema;
284 285 286
        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;
287 288
        int16_t numOfColumns = schema->tableInfo.numOfColumns;
        int16_t numOfFields = numOfColumns + schema->tableInfo.numOfTags;
289
        for (int16_t i = 0; i < numOfFields; ++i) {
290
          const SSchema* col = schema->schema + i;
X
Xiaoyu Wang 已提交
291 292
          std::cout << SF(std::string(col->name)) << SH(ftToString(i, numOfColumns)) << SH(dtToString(col->type))
                    << IF(col->bytes) << std::endl;
293 294
        }
        std::cout << std::endl;
295 296 297 298
      }
    }
  }

299 300 301 302 303 304 305 306
  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 已提交
307 308
    info->pCode = nullptr;
    info->pComment = nullptr;
309
    udf_.insert(std::make_pair(func, info));
310 311
  }

X
Xiaoyu Wang 已提交
312
  void createSmaIndex(const SMCreateSmaReq* pReq) {
313
    STableIndexInfo info = {0};
X
Xiaoyu Wang 已提交
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
    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);
    }
  }

331 332 333 334 335 336
  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));
  }

337 338 339 340 341 342 343 344
  void createDatabase(const std::string& db, bool rollup) {
    SDbCfgInfo cfg = {0};
    if (rollup) {
      cfg.pRetensions = taosArrayInit(TARRAY_MIN_SIZE, sizeof(SRetention));
    }
    dbCfg_.insert(std::make_pair(db, cfg));
  }

X
Xiaoyu Wang 已提交
345
 private:
346
  typedef std::map<std::string, std::shared_ptr<MockTableMeta>> TableMetaCache;
X
Xiaoyu Wang 已提交
347
  typedef std::map<std::string, TableMetaCache>                 DbMetaCache;
348
  typedef std::map<std::string, std::shared_ptr<SFuncInfo>>     UdfMetaCache;
X
Xiaoyu Wang 已提交
349
  typedef std::map<std::string, std::vector<STableIndexInfo>>   IndexMetaCache;
350
  typedef std::map<int32_t, SEpSet>                             DnodeCache;
351
  typedef std::map<std::string, SDbCfgInfo>                     DbCfgCache;
X
Xiaoyu Wang 已提交
352 353 354 355 356 357 358 359 360

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

X
Xiaoyu Wang 已提交
362 363 364 365 366 367
  STableIndexInfo* copyTableIndexInfo(STableIndexInfo* pDst, const STableIndexInfo* pSrc) const {
    memcpy(pDst, pSrc, sizeof(STableIndexInfo));
    pDst->expr = strdup(pSrc->expr);
    return pDst;
  }

368 369 370 371 372 373 374 375
  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);
  }

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

404
  std::string ftToString(int16_t colid, int16_t numOfColumns) const {
405
    return (0 == colid ? "column" : (colid < numOfColumns ? "column" : "tag"));
406 407
  }

X
Xiaoyu Wang 已提交
408
  STableMeta* getTableSchemaMeta(const std::string& db, const std::string& tbname) const {
409
    std::shared_ptr<MockTableMeta> table = getTableMeta(db, tbname);
X
Xiaoyu Wang 已提交
410
    return table ? table->schema : nullptr;
411 412
  }

X
Xiaoyu Wang 已提交
413 414
  int32_t copyTableSchemaMeta(const std::string& db, const std::string& tbname,
                              std::unique_ptr<STableMeta>* dst) const {
X
Xiaoyu Wang 已提交
415 416
    STableMeta* src = getTableSchemaMeta(db, tbname);
    if (nullptr == src) {
417 418 419
      return TSDB_CODE_TSC_INVALID_TABLE_NAME;
    }
    int32_t len = sizeof(STableMeta) + sizeof(SSchema) * (src->tableInfo.numOfTags + src->tableInfo.numOfColumns);
wafwerar's avatar
wafwerar 已提交
420
    dst->reset((STableMeta*)taosMemoryCalloc(1, len));
421 422 423
    if (!dst) {
      return TSDB_CODE_TSC_OUT_OF_MEMORY;
    }
X
Xiaoyu Wang 已提交
424
    memcpy(dst->get(), src, len);
425 426 427
    return TSDB_CODE_SUCCESS;
  }

X
bugfix  
Xiaoyu Wang 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
  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;
  }

449 450 451 452 453 454 455 456 457 458 459 460
  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;
  }

461 462 463
  int32_t getAllTableMeta(SArray* pTableMetaReq, SArray** pTableMetaData) const {
    if (NULL != pTableMetaReq) {
      int32_t ntables = taosArrayGetSize(pTableMetaReq);
X
Xiaoyu Wang 已提交
464
      *pTableMetaData = taosArrayInit(ntables, sizeof(SMetaRes));
465
      for (int32_t i = 0; i < ntables; ++i) {
X
Xiaoyu Wang 已提交
466 467 468
        SMetaRes res = {0};
        res.code = catalogGetTableMeta((const SName*)taosArrayGet(pTableMetaReq, i), (STableMeta**)&res.pRes);
        taosArrayPush(*pTableMetaData, &res);
469 470
      }
    }
X
Xiaoyu Wang 已提交
471
    return TSDB_CODE_SUCCESS;
472 473 474 475 476
  }

  int32_t getAllTableVgroup(SArray* pTableVgroupReq, SArray** pTableVgroupData) const {
    if (NULL != pTableVgroupReq) {
      int32_t ntables = taosArrayGetSize(pTableVgroupReq);
X
Xiaoyu Wang 已提交
477
      *pTableVgroupData = taosArrayInit(ntables, sizeof(SMetaRes));
478
      for (int32_t i = 0; i < ntables; ++i) {
X
Xiaoyu Wang 已提交
479 480 481 482
        SMetaRes res = {0};
        res.pRes = taosMemoryCalloc(1, sizeof(SVgroupInfo));
        res.code = catalogGetTableHashVgroup((const SName*)taosArrayGet(pTableVgroupReq, i), (SVgroupInfo*)res.pRes);
        taosArrayPush(*pTableVgroupData, &res);
483 484
      }
    }
X
Xiaoyu Wang 已提交
485
    return TSDB_CODE_SUCCESS;
486 487
  }

488 489 490 491
  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 已提交
492
      *pDbVgroupData = taosArrayInit(ndbs, sizeof(SMetaRes));
493
      for (int32_t i = 0; i < ndbs; ++i) {
X
Xiaoyu Wang 已提交
494 495
        SMetaRes res = {0};
        taosArrayPush(*pDbVgroupData, &res);
496 497 498 499 500 501 502 503 504
      }
    }
    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 已提交
505
      *pDbCfgData = taosArrayInit(ndbs, sizeof(SMetaRes));
506
      for (int32_t i = 0; i < ndbs; ++i) {
X
Xiaoyu Wang 已提交
507 508
        SMetaRes res = {0};
        res.pRes = taosMemoryCalloc(1, sizeof(SDbCfgInfo));
509
        res.code = catalogGetDBCfg((const char*)taosArrayGet(pDbCfgReq, i), (SDbCfgInfo*)res.pRes);
X
Xiaoyu Wang 已提交
510
        taosArrayPush(*pDbCfgData, &res);
511 512 513 514 515
      }
    }
    return code;
  }

516 517 518 519
  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 已提交
520
      *pDbInfoData = taosArrayInit(ndbs, sizeof(SMetaRes));
521
      for (int32_t i = 0; i < ndbs; ++i) {
X
Xiaoyu Wang 已提交
522 523 524
        SMetaRes res = {0};
        res.pRes = taosMemoryCalloc(1, sizeof(SDbInfo));
        taosArrayPush(*pDbInfoData, &res);
525 526 527 528 529 530 531 532 533
      }
    }
    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 已提交
534
      *pUserAuthData = taosArrayInit(num, sizeof(SMetaRes));
535
      for (int32_t i = 0; i < num; ++i) {
X
Xiaoyu Wang 已提交
536 537 538 539
        SMetaRes res = {0};
        res.pRes = taosMemoryCalloc(1, sizeof(bool));
        *(bool*)(res.pRes) = true;
        taosArrayPush(*pUserAuthData, &res);
540 541 542 543 544
      }
    }
    return code;
  }

545 546 547
  int32_t getAllUdf(SArray* pUdfReq, SArray** pUdfData) const {
    if (NULL != pUdfReq) {
      int32_t num = taosArrayGetSize(pUdfReq);
X
Xiaoyu Wang 已提交
548
      *pUdfData = taosArrayInit(num, sizeof(SMetaRes));
549
      for (int32_t i = 0; i < num; ++i) {
X
Xiaoyu Wang 已提交
550 551 552 553
        SMetaRes res = {0};
        res.pRes = taosMemoryCalloc(1, sizeof(SFuncInfo));
        res.code = catalogGetUdfInfo((char*)taosArrayGet(pUdfReq, i), (SFuncInfo*)res.pRes);
        taosArrayPush(*pUdfData, &res);
554 555
      }
    }
X
Xiaoyu Wang 已提交
556
    return TSDB_CODE_SUCCESS;
557 558
  }

X
Xiaoyu Wang 已提交
559 560 561 562 563 564 565 566 567 568 569 570 571
  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 已提交
572 573 574 575 576 577 578 579 580 581 582 583 584 585
  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;
  }

586 587
  int32_t getAllDnodeList(SArray** pDnodes) const {
    SMetaRes res = {0};
588
    catalogGetDnodeList((SArray**)&res.pRes);
589 590
    *pDnodes = taosArrayInit(1, sizeof(SMetaRes));
    taosArrayPush(*pDnodes, &res);
591
    return TSDB_CODE_SUCCESS;
592 593
  }

X
Xiaoyu Wang 已提交
594
  uint64_t                      id_;
595
  std::unique_ptr<TableBuilder> builder_;
X
Xiaoyu Wang 已提交
596
  DbMetaCache                   meta_;
597
  UdfMetaCache                  udf_;
X
Xiaoyu Wang 已提交
598
  IndexMetaCache                index_;
599
  DnodeCache                    dnode_;
600
  DbCfgCache                    dbCfg_;
601 602
};

X
Xiaoyu Wang 已提交
603
MockCatalogService::MockCatalogService() : impl_(new MockCatalogServiceImpl()) {}
604

X
Xiaoyu Wang 已提交
605
MockCatalogService::~MockCatalogService() {}
606

X
Xiaoyu Wang 已提交
607 608
ITableBuilder& MockCatalogService::createTableBuilder(const std::string& db, const std::string& tbname,
                                                      int8_t tableType, int32_t numOfColumns, int32_t numOfTags) {
609 610 611
  return impl_->createTableBuilder(db, tbname, tableType, numOfColumns, numOfTags);
}

X
Xiaoyu Wang 已提交
612 613
void MockCatalogService::createSubTable(const std::string& db, const std::string& stbname, const std::string& tbname,
                                        int16_t vgid) {
614 615 616
  impl_->createSubTable(db, stbname, tbname, vgid);
}

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

619 620 621
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);
622 623
}

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

626 627 628 629
void MockCatalogService::createDnode(int32_t dnodeId, const std::string& host, int16_t port) {
  impl_->createDnode(dnodeId, host, port);
}

630 631
void MockCatalogService::createDatabase(const std::string& db, bool rollup) { impl_->createDatabase(db, rollup); }

H
Haojun Liao 已提交
632 633
int32_t MockCatalogService::catalogGetTableMeta(const SName* pTableName, STableMeta** pTableMeta) const {
  return impl_->catalogGetTableMeta(pTableName, pTableMeta);
634 635
}

H
Haojun Liao 已提交
636 637
int32_t MockCatalogService::catalogGetTableHashVgroup(const SName* pTableName, SVgroupInfo* vgInfo) const {
  return impl_->catalogGetTableHashVgroup(pTableName, vgInfo);
H
Haojun Liao 已提交
638 639 640 641
}

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

X
Xiaoyu Wang 已提交
644 645 646 647
int32_t MockCatalogService::catalogGetDBVgInfo(const char* pDbFName, SArray** pVgList) const {
  return impl_->catalogGetDBVgInfo(pDbFName, pVgList);
}

648 649 650 651
int32_t MockCatalogService::catalogGetDBCfg(const char* pDbFName, SDbCfgInfo* pDbCfg) const {
  return impl_->catalogGetDBCfg(pDbFName, pDbCfg);
}

652 653 654 655
int32_t MockCatalogService::catalogGetUdfInfo(const std::string& funcName, SFuncInfo* pInfo) const {
  return impl_->catalogGetUdfInfo(funcName, pInfo);
}

X
Xiaoyu Wang 已提交
656 657 658 659
int32_t MockCatalogService::catalogGetTableIndex(const SName* pTableName, SArray** pIndexes) const {
  return impl_->catalogGetTableIndex(pTableName, pIndexes);
}

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

662 663 664
int32_t MockCatalogService::catalogGetAllMeta(const SCatalogReq* pCatalogReq, SMetaData* pMetaData) const {
  return impl_->catalogGetAllMeta(pCatalogReq, pMetaData);
}
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696

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