mockCatalogService.cpp 11.9 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 21
#include <iomanip>
#include <iostream>
#include <map>

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

std::unique_ptr<MockCatalogService> mockCatalogService;

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

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

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

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

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

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

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

  static std::unique_ptr<TableBuilder> createTableBuilder(int8_t tableType, int32_t numOfColumns, int32_t numOfTags) {
X
Xiaoyu Wang 已提交
65 66
    STableMeta* meta =
        (STableMeta*)taosMemoryCalloc(1, sizeof(STableMeta) + sizeof(SSchema) * (numOfColumns + numOfTags));
67 68 69 70 71 72 73 74 75
    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 已提交
76
  TableBuilder(STableMeta* schemaMeta) : colId_(1), rowsize_(0), meta_(new MockTableMeta()) {
X
Xiaoyu Wang 已提交
77
    meta_->schema = schemaMeta;
78 79
  }

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

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

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

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

X
Xiaoyu Wang 已提交
93
  MockCatalogServiceImpl() : id_(1) {}
94

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

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

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

    const char* tname = tNameGetTableName(pTableName);
X
Xiaoyu Wang 已提交
104
    int32_t     code = copyTableSchemaMeta(db, tname, &table);
105
    if (TSDB_CODE_SUCCESS != code) {
106
      std::cout << "db : " << db << ", table :" << tname << std::endl;
107 108
      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
  TableBuilder& createTableBuilder(const std::string& db, const std::string& tbname, int8_t tableType,
                                   int32_t numOfColumns, int32_t numOfTags) {
126
    builder_ = TableBuilder::createTableBuilder(tableType, numOfColumns, numOfTags);
127 128
    meta_[db][tbname] = builder_->table();
    meta_[db][tbname]->schema->uid = id_++;
129 130 131
    return *(builder_.get());
  }

132 133
  void createSubTable(const std::string& db, const std::string& stbname, const std::string& tbname, int16_t vgid) {
    std::unique_ptr<STableMeta> table;
134 135
    if (TSDB_CODE_SUCCESS != copyTableSchemaMeta(db, stbname, &table)) {
      throw std::runtime_error("copyTableSchemaMeta failed");
136
    }
137
    meta_[db][tbname].reset(new MockTableMeta());
X
Xiaoyu Wang 已提交
138
    meta_[db][tbname]->schema = table.release();
139
    meta_[db][tbname]->schema->uid = id_++;
140
    meta_[db][tbname]->schema->tableType = TSDB_CHILD_TABLE;
H
Haojun Liao 已提交
141

X
Xiaoyu Wang 已提交
142
    SVgroupInfo vgroup = {vgid, 0, 0, {0}, 0};
L
Liu Jicong 已提交
143 144 145 146
    addEpIntoEpSet(&vgroup.epSet, "dnode_1", 6030);
    addEpIntoEpSet(&vgroup.epSet, "dnode_2", 6030);
    addEpIntoEpSet(&vgroup.epSet, "dnode_3", 6030);
    vgroup.epSet.inUse = 0;
H
Haojun Liao 已提交
147 148

    meta_[db][tbname]->vgs.emplace_back(vgroup);
149
    // super table
H
Haojun Liao 已提交
150
    meta_[db][stbname]->vgs.emplace_back(vgroup);
151 152
  }

153
  void showTables() const {
X
Xiaoyu Wang 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
// 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
#define CA(n, s)                                                                              \
  std::setw(NOF((n) - (s).length())) << "" << (s) << std::setw(NOB((n) - (s).length())) << "" \
                                     << "|"
// 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(' ')
176 177

    for (const auto& db : meta_) {
178
      std::cout << "Databse:" << db.first << std::endl;
179
      std::cout << SH("Table") << SH("Type") << SH("Precision") << IH("Vgid") << IH("RowSize") << std::endl;
180
      std::cout << SL(3, 1) << std::endl;
181
      for (const auto& table : db.second) {
182
        const auto& schema = table.second->schema;
X
Xiaoyu Wang 已提交
183 184
        std::cout << SF(table.first) << SF(ttToString(schema->tableType)) << SF(pToString(schema->tableInfo.precision))
                  << IF(schema->vgId) << IF(schema->tableInfo.rowSize) << std::endl;
185 186 187 188 189 190
      }
      std::cout << std::endl;
    }

    for (const auto& db : meta_) {
      for (const auto& table : db.second) {
191
        const auto& schema = table.second->schema;
192 193 194
        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;
195 196
        int16_t numOfColumns = schema->tableInfo.numOfColumns;
        int16_t numOfFields = numOfColumns + schema->tableInfo.numOfTags;
197
        for (int16_t i = 0; i < numOfFields; ++i) {
198
          const SSchema* col = schema->schema + i;
X
Xiaoyu Wang 已提交
199 200
          std::cout << SF(std::string(col->name)) << SH(ftToString(i, numOfColumns)) << SH(dtToString(col->type))
                    << IF(col->bytes) << std::endl;
201 202
        }
        std::cout << std::endl;
203 204 205 206
      }
    }
  }

207 208 209 210 211 212 213 214 215 216 217 218
  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;
  }

X
Xiaoyu Wang 已提交
219
 private:
220
  typedef std::map<std::string, std::shared_ptr<MockTableMeta>> TableMetaCache;
X
Xiaoyu Wang 已提交
221
  typedef std::map<std::string, TableMetaCache>                 DbMetaCache;
222

223 224 225 226 227 228 229 230
  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);
  }

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
  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 已提交
257
  std::string dtToString(int8_t type) const { return tDataTypes[type].name; }
258

259
  std::string ftToString(int16_t colid, int16_t numOfColumns) const {
260
    return (0 == colid ? "column" : (colid < numOfColumns ? "column" : "tag"));
261 262
  }

X
Xiaoyu Wang 已提交
263
  STableMeta* getTableSchemaMeta(const std::string& db, const std::string& tbname) const {
264
    std::shared_ptr<MockTableMeta> table = getTableMeta(db, tbname);
X
Xiaoyu Wang 已提交
265
    return table ? table->schema : nullptr;
266 267
  }

X
Xiaoyu Wang 已提交
268 269
  int32_t copyTableSchemaMeta(const std::string& db, const std::string& tbname,
                              std::unique_ptr<STableMeta>* dst) const {
X
Xiaoyu Wang 已提交
270 271
    STableMeta* src = getTableSchemaMeta(db, tbname);
    if (nullptr == src) {
272 273 274
      return TSDB_CODE_TSC_INVALID_TABLE_NAME;
    }
    int32_t len = sizeof(STableMeta) + sizeof(SSchema) * (src->tableInfo.numOfTags + src->tableInfo.numOfColumns);
wafwerar's avatar
wafwerar 已提交
275
    dst->reset((STableMeta*)taosMemoryCalloc(1, len));
276 277 278
    if (!dst) {
      return TSDB_CODE_TSC_OUT_OF_MEMORY;
    }
X
Xiaoyu Wang 已提交
279
    memcpy(dst->get(), src, len);
280 281 282
    return TSDB_CODE_SUCCESS;
  }

X
bugfix  
Xiaoyu Wang 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
  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;
  }

X
Xiaoyu Wang 已提交
304
  uint64_t                      id_;
305
  std::unique_ptr<TableBuilder> builder_;
X
Xiaoyu Wang 已提交
306
  DbMetaCache                   meta_;
307 308
};

X
Xiaoyu Wang 已提交
309
MockCatalogService::MockCatalogService() : impl_(new MockCatalogServiceImpl()) {}
310

X
Xiaoyu Wang 已提交
311
MockCatalogService::~MockCatalogService() {}
312

X
Xiaoyu Wang 已提交
313 314
ITableBuilder& MockCatalogService::createTableBuilder(const std::string& db, const std::string& tbname,
                                                      int8_t tableType, int32_t numOfColumns, int32_t numOfTags) {
315 316 317
  return impl_->createTableBuilder(db, tbname, tableType, numOfColumns, numOfTags);
}

X
Xiaoyu Wang 已提交
318 319
void MockCatalogService::createSubTable(const std::string& db, const std::string& stbname, const std::string& tbname,
                                        int16_t vgid) {
320 321 322
  impl_->createSubTable(db, stbname, tbname, vgid);
}

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

X
Xiaoyu Wang 已提交
325 326
std::shared_ptr<MockTableMeta> MockCatalogService::getTableMeta(const std::string& db,
                                                                const std::string& tbname) const {
327 328 329
  return impl_->getTableMeta(db, tbname);
}

H
Haojun Liao 已提交
330 331
int32_t MockCatalogService::catalogGetTableMeta(const SName* pTableName, STableMeta** pTableMeta) const {
  return impl_->catalogGetTableMeta(pTableName, pTableMeta);
332 333
}

H
Haojun Liao 已提交
334 335
int32_t MockCatalogService::catalogGetTableHashVgroup(const SName* pTableName, SVgroupInfo* vgInfo) const {
  return impl_->catalogGetTableHashVgroup(pTableName, vgInfo);
H
Haojun Liao 已提交
336 337 338 339
}

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