mockCatalogService.cpp 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * 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/>.
 */

#include "mockCatalogService.h"

#include <iomanip>
#include <iostream>
#include <map>
H
Haojun Liao 已提交
21
#include "tdatablock.h"
22

23
#include "tname.h"
24 25 26 27 28 29 30
#include "ttypes.h"

std::unique_ptr<MockCatalogService> mockCatalogService;

class TableBuilder : public ITableBuilder {
public:
  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 44 45

    SVgroupInfo vgroup = {.vgId = vgid, .hashBegin = 0, .hashEnd = 0, };

L
Liu Jicong 已提交
46 47 48 49 50
    vgroup.epSet.eps[0] = (SEp){"dnode_1", 6030};
    vgroup.epSet.eps[1] = (SEp){"dnode_2", 6030};
    vgroup.epSet.eps[2] = (SEp){"dnode_3", 6030};
    vgroup.epSet.inUse = 0;
    vgroup.epSet.numOfEps = 3;
H
Haojun Liao 已提交
51 52

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

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

  virtual void done() {
62
    schema()->tableInfo.rowSize = rowsize_;
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  }

private:
  friend class MockCatalogServiceImpl;

  static std::unique_ptr<TableBuilder> createTableBuilder(int8_t tableType, int32_t numOfColumns, int32_t numOfTags) {
    STableMeta* meta = (STableMeta*)std::calloc(1, sizeof(STableMeta) + sizeof(SSchema) * (numOfColumns + numOfTags));
    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 已提交
79
  TableBuilder(STableMeta* schemaMeta) : colId_(1), rowsize_(0), meta_(new MockTableMeta()) {
X
Xiaoyu Wang 已提交
80
    meta_->schema = schemaMeta;
81 82
  }

X
Xiaoyu Wang 已提交
83
  STableMeta* schema() {
84 85 86 87
    return meta_->schema;
  }

  std::shared_ptr<MockTableMeta> table() {
88 89 90
    return meta_;
  }

91
  int32_t colId_;
92
  int32_t rowsize_;
93
  std::shared_ptr<MockTableMeta> meta_;
94 95 96 97 98 99
};

class MockCatalogServiceImpl {
public:
  static const int32_t numOfDataTypes = sizeof(tDataTypes) / sizeof(tDataTypes[0]);

100
  MockCatalogServiceImpl() : id_(1) {
101 102
  }

X
Xiaoyu Wang 已提交
103
  int32_t catalogGetHandle() const {
104
    return 0;
105 106
  }

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

X
Xiaoyu Wang 已提交
110 111
    char db[TSDB_DB_NAME_LEN] = {0};
    tNameGetDbName(pTableName, db);
H
Haojun Liao 已提交
112 113 114

    const char* tname = tNameGetTableName(pTableName);
    int32_t code = copyTableSchemaMeta(db, tname, &table);
115
    if (TSDB_CODE_SUCCESS != code) {
116
      std::cout << "db : " << db << ", table :" << tname << std::endl;
117 118
      return code;
    }
119
    *pTableMeta = table.release();
120
    return TSDB_CODE_SUCCESS;
121 122
  }

H
Haojun Liao 已提交
123
  int32_t catalogGetTableHashVgroup(const SName* pTableName, SVgroupInfo* vgInfo) const {
X
Xiaoyu Wang 已提交
124
    // todo
X
Xiaoyu Wang 已提交
125
    vgInfo->vgId = 1;
L
Liu Jicong 已提交
126
    addEpIntoEpSet(&vgInfo->epSet, "node1", 6030);
X
Xiaoyu Wang 已提交
127 128 129
    return 0;
  }

H
Haojun Liao 已提交
130 131 132
  int32_t catalogGetTableDistVgInfo(const SName* pTableName, SArray** pVgList) const {
    SVgroupInfo info = {0};
    info.vgId = 1;
X
Xiaoyu Wang 已提交
133
    addEpIntoEpSet(&info.epSet, "node1", 6030);
H
Haojun Liao 已提交
134 135 136 137 138 139 140 141 142

    info.hashBegin = 0;
    info.hashEnd = 1;
    *pVgList = taosArrayInit(4, sizeof(SVgroupInfo));

    taosArrayPush(*pVgList, &info);
    return 0;
  }

143 144
  TableBuilder& createTableBuilder(const std::string& db, const std::string& tbname, int8_t tableType, int32_t numOfColumns, int32_t numOfTags) {
    builder_ = TableBuilder::createTableBuilder(tableType, numOfColumns, numOfTags);
145 146
    meta_[db][tbname] = builder_->table();
    meta_[db][tbname]->schema->uid = id_++;
147 148 149
    return *(builder_.get());
  }

150 151
  void createSubTable(const std::string& db, const std::string& stbname, const std::string& tbname, int16_t vgid) {
    std::unique_ptr<STableMeta> table;
152 153
    if (TSDB_CODE_SUCCESS != copyTableSchemaMeta(db, stbname, &table)) {
      throw std::runtime_error("copyTableSchemaMeta failed");
154
    }
155
    meta_[db][tbname].reset(new MockTableMeta());
X
Xiaoyu Wang 已提交
156
    meta_[db][tbname]->schema = table.release();
157
    meta_[db][tbname]->schema->uid = id_++;
H
Haojun Liao 已提交
158 159

    SVgroupInfo vgroup = {.vgId = vgid, .hashBegin = 0, .hashEnd = 0,};
L
Liu Jicong 已提交
160 161 162 163
    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 已提交
164 165

    meta_[db][tbname]->vgs.emplace_back(vgroup);
166
    // super table
H
Haojun Liao 已提交
167
    meta_[db][stbname]->vgs.emplace_back(vgroup);
168 169
  }

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
  void showTables() const {
    // 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(' ')

    for (const auto& db : meta_) {
193
      std::cout << "Databse:" << db.first << std::endl;
194
      std::cout << SH("Table") << SH("Type") << SH("Precision") << IH("Vgid") << IH("RowSize") << std::endl;
195
      std::cout << SL(3, 1) << std::endl;
196
      for (const auto& table : db.second) {
197
        const auto& schema = table.second->schema;
198
        std::cout << SF(table.first) << SF(ttToString(schema->tableType)) << SF(pToString(schema->tableInfo.precision)) << IF(schema->vgId) << IF(schema->tableInfo.rowSize) << std::endl;
199 200 201 202 203 204
      }
      std::cout << std::endl;
    }

    for (const auto& db : meta_) {
      for (const auto& table : db.second) {
205
        const auto& schema = table.second->schema;
206 207 208
        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;
209 210
        int16_t numOfTags = schema->tableInfo.numOfTags;
        int16_t numOfFields = numOfTags + schema->tableInfo.numOfColumns;
211
        for (int16_t i = 0; i < numOfFields; ++i) {
212 213
          const SSchema* col = schema->schema + i;
          std::cout << SF(std::string(col->name)) << SH(ftToString(i, numOfTags)) << SH(dtToString(col->type)) << IF(col->bytes) << std::endl;
214 215
        }
        std::cout << std::endl;
216 217 218 219
      }
    }
  }

220 221 222 223 224 225 226 227 228 229 230 231
  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;
  }

232
private:
233
  typedef std::map<std::string, std::shared_ptr<MockTableMeta>> TableMetaCache;
234 235
  typedef std::map<std::string, TableMetaCache> DbMetaCache;

236 237 238 239 240 241 242 243
  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);
  }

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
  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";
    }
  }

270 271 272 273
  std::string dtToString(int8_t type) const {
    return tDataTypes[type].name;
  }

274 275 276 277
  std::string ftToString(int16_t colid, int16_t numOfTags) const {
    return (0 == colid ? "column" : (colid <= numOfTags ? "tag" : "column"));
  }

X
Xiaoyu Wang 已提交
278
  STableMeta* getTableSchemaMeta(const std::string& db, const std::string& tbname) const {
279
    std::shared_ptr<MockTableMeta> table = getTableMeta(db, tbname);
X
Xiaoyu Wang 已提交
280
    return table ? table->schema : nullptr;
281 282
  }

283
  int32_t copyTableSchemaMeta(const std::string& db, const std::string& tbname, std::unique_ptr<STableMeta>* dst) const {
X
Xiaoyu Wang 已提交
284 285
    STableMeta* src = getTableSchemaMeta(db, tbname);
    if (nullptr == src) {
286 287 288 289 290 291 292
      return TSDB_CODE_TSC_INVALID_TABLE_NAME;
    }
    int32_t len = sizeof(STableMeta) + sizeof(SSchema) * (src->tableInfo.numOfTags + src->tableInfo.numOfColumns);
    dst->reset((STableMeta*)std::calloc(1, len));
    if (!dst) {
      return TSDB_CODE_TSC_OUT_OF_MEMORY;
    }
X
Xiaoyu Wang 已提交
293
    memcpy(dst->get(), src, len);
294 295 296
    return TSDB_CODE_SUCCESS;
  }

297 298
  uint64_t id_;
  std::unique_ptr<TableBuilder> builder_;
299
  DbMetaCache meta_;
300 301 302 303 304 305 306 307 308 309 310 311
};

MockCatalogService::MockCatalogService() : impl_(new MockCatalogServiceImpl()) {
}

MockCatalogService::~MockCatalogService() {
}

ITableBuilder& MockCatalogService::createTableBuilder(const std::string& db, const std::string& tbname, int8_t tableType, int32_t numOfColumns, int32_t numOfTags) {
  return impl_->createTableBuilder(db, tbname, tableType, numOfColumns, numOfTags);
}

312 313 314 315
void MockCatalogService::createSubTable(const std::string& db, const std::string& stbname, const std::string& tbname, int16_t vgid) {
  impl_->createSubTable(db, stbname, tbname, vgid);
}

316 317
void MockCatalogService::showTables() const {
  impl_->showTables();
318 319 320 321 322 323
}

std::shared_ptr<MockTableMeta> MockCatalogService::getTableMeta(const std::string& db, const std::string& tbname) const {
  return impl_->getTableMeta(db, tbname);
}

H
Haojun Liao 已提交
324 325
int32_t MockCatalogService::catalogGetTableMeta(const SName* pTableName, STableMeta** pTableMeta) const {
  return impl_->catalogGetTableMeta(pTableName, pTableMeta);
326 327
}

H
Haojun Liao 已提交
328 329
int32_t MockCatalogService::catalogGetTableHashVgroup(const SName* pTableName, SVgroupInfo* vgInfo) const {
  return impl_->catalogGetTableHashVgroup(pTableName, vgInfo);
H
Haojun Liao 已提交
330 331 332 333
}

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