mockCatalogService.cpp 12.0 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 {
X
bugfix  
Xiaoyu Wang 已提交
114 115 116
    char db[TSDB_DB_NAME_LEN] = {0};
    tNameGetDbName(pTableName, db);
    return copyTableVgroup(db, tNameGetTableName(pTableName), vgInfo);
X
Xiaoyu Wang 已提交
117 118
  }

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

X
Xiaoyu Wang 已提交
125 126
  TableBuilder& createTableBuilder(const std::string& db, const std::string& tbname, int8_t tableType,
                                   int32_t numOfColumns, int32_t numOfTags) {
127
    builder_ = TableBuilder::createTableBuilder(tableType, numOfColumns, numOfTags);
128 129
    meta_[db][tbname] = builder_->table();
    meta_[db][tbname]->schema->uid = id_++;
130 131 132
    return *(builder_.get());
  }

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

X
Xiaoyu Wang 已提交
143
    SVgroupInfo vgroup = {vgid, 0, 0, {0}, 0};
L
Liu Jicong 已提交
144 145 146 147
    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 已提交
148 149

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

154
  void showTables() const {
X
Xiaoyu Wang 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
// 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(' ')
177 178

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

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

208 209 210 211 212 213 214 215 216 217 218 219
  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 已提交
220
 private:
221
  typedef std::map<std::string, std::shared_ptr<MockTableMeta>> TableMetaCache;
X
Xiaoyu Wang 已提交
222
  typedef std::map<std::string, TableMetaCache>                 DbMetaCache;
223

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

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

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

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

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

X
bugfix  
Xiaoyu Wang 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
  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 已提交
305
  uint64_t                      id_;
306
  std::unique_ptr<TableBuilder> builder_;
X
Xiaoyu Wang 已提交
307
  DbMetaCache                   meta_;
308 309
};

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

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

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

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

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

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

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

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

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