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

22
#include "tname.h"
23 24 25 26 27 28 29
#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) {
30 31
    assert(colId_ < schema()->tableInfo.numOfTags + schema()->tableInfo.numOfColumns);
    SSchema* col = schema()->schema + colId_;
32
    col->type = type;
33
    col->colId = colId_++;
34 35
    col->bytes = bytes;
    strcpy(col->name, name.c_str());
36
    rowsize_ += bytes;
37 38 39 40
    return *this;
  }

  virtual TableBuilder& setVgid(int16_t vgid) {
41
    schema()->vgId = vgid;
42
    meta_->vgs.emplace_back(SVgroupInfo{.vgId = vgid, .hashBegin = 0, .hashEnd = 0, .inUse = 0, .numOfEps = 3, .epAddr = {{"dnode_1", 6030}, {"dnode_2", 6030}, {"dnode_3", 6030}}});
43 44 45 46
    return *this;
  }

  virtual TableBuilder& setPrecision(uint8_t precision) {
47
    schema()->tableInfo.precision = precision;
48 49 50 51
    return *this;
  }

  virtual void done() {
52
    schema()->tableInfo.rowSize = rowsize_;
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  }

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

69 70
  TableBuilder(STableMeta* schemaMeta) : colId_(0), rowsize_(0), meta_(new MockTableMeta()) {
    meta_->schema.reset(schemaMeta);
71 72
  }

73 74 75 76 77
  std::shared_ptr<STableMeta> schema() {
    return meta_->schema;
  }

  std::shared_ptr<MockTableMeta> table() {
78 79 80
    return meta_;
  }

81
  int32_t colId_;
82
  int32_t rowsize_;
83
  std::shared_ptr<MockTableMeta> meta_;
84 85 86 87 88 89
};

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

90
  MockCatalogServiceImpl() : id_(1) {
91 92
  }

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

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

    char db[TSDB_DB_FNAME_LEN] = {0};
    tNameGetFullDbName(pTableName, db);

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

H
Haojun Liao 已提交
112
  int32_t catalogGetTableHashVgroup(const SName* pTableName, SVgroupInfo* vgInfo) const {
X
Xiaoyu Wang 已提交
113 114 115 116
    // todo
    return 0;
  }

117 118
  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);
119 120
    meta_[db][tbname] = builder_->table();
    meta_[db][tbname]->schema->uid = id_++;
121 122 123
    return *(builder_.get());
  }

124 125
  void createSubTable(const std::string& db, const std::string& stbname, const std::string& tbname, int16_t vgid) {
    std::unique_ptr<STableMeta> table;
126 127
    if (TSDB_CODE_SUCCESS != copyTableSchemaMeta(db, stbname, &table)) {
      throw std::runtime_error("copyTableSchemaMeta failed");
128
    }
129 130 131
    meta_[db][tbname].reset(new MockTableMeta());
    meta_[db][tbname]->schema.reset(table.release());
    meta_[db][tbname]->schema->uid = id_++;
132
    meta_[db][tbname]->vgs.emplace_back((SVgroupInfo){.vgId = vgid, .hashBegin = 0, .hashEnd = 0, .inUse = 0, .numOfEps = 3, .epAddr = {{"dnode_1", 6030}, {"dnode_2", 6030}, {"dnode_3", 6030}}});
133
    // super table
134
    meta_[db][stbname]->vgs.emplace_back((SVgroupInfo){.vgId = vgid, .hashBegin = 0, .hashEnd = 0, .inUse = 0, .numOfEps = 3, .epAddr = {{"dnode_1", 6030}, {"dnode_2", 6030}, {"dnode_3", 6030}}});
135 136
  }

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  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_) {
160
      std::cout << "Databse:" << db.first << std::endl;
161
      std::cout << SH("Table") << SH("Type") << SH("Precision") << IH("Vgid") << IH("RowSize") << std::endl;
162
      std::cout << SL(3, 1) << std::endl;
163
      for (const auto& table : db.second) {
164
        const auto& schema = table.second->schema;
165
        std::cout << SF(table.first) << SF(ttToString(schema->tableType)) << SF(pToString(schema->tableInfo.precision)) << IF(schema->vgId) << IF(schema->tableInfo.rowSize) << std::endl;
166 167 168 169 170 171
      }
      std::cout << std::endl;
    }

    for (const auto& db : meta_) {
      for (const auto& table : db.second) {
172
        const auto& schema = table.second->schema;
173 174 175
        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;
176 177
        int16_t numOfTags = schema->tableInfo.numOfTags;
        int16_t numOfFields = numOfTags + schema->tableInfo.numOfColumns;
178
        for (int16_t i = 0; i < numOfFields; ++i) {
179 180
          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;
181 182
        }
        std::cout << std::endl;
183 184 185 186
      }
    }
  }

187 188 189 190 191 192 193 194 195 196 197 198
  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;
  }

199
private:
200
  typedef std::map<std::string, std::shared_ptr<MockTableMeta>> TableMetaCache;
201 202
  typedef std::map<std::string, TableMetaCache> DbMetaCache;

203 204 205 206 207 208 209 210
  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);
  }

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
  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";
    }
  }

237 238 239 240
  std::string dtToString(int8_t type) const {
    return tDataTypes[type].name;
  }

241 242 243 244
  std::string ftToString(int16_t colid, int16_t numOfTags) const {
    return (0 == colid ? "column" : (colid <= numOfTags ? "tag" : "column"));
  }

245 246 247
  std::shared_ptr<STableMeta> getTableSchemaMeta(const std::string& db, const std::string& tbname) const {
    std::shared_ptr<MockTableMeta> table = getTableMeta(db, tbname);
    return table ? table->schema : std::shared_ptr<STableMeta>();
248 249
  }

250 251
  int32_t copyTableSchemaMeta(const std::string& db, const std::string& tbname, std::unique_ptr<STableMeta>* dst) const {
    std::shared_ptr<STableMeta> src = getTableSchemaMeta(db, tbname);
252 253 254 255 256 257 258 259 260 261 262 263
    if (!src) {
      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;
    }
    memcpy(dst->get(), src.get(), len);
    return TSDB_CODE_SUCCESS;
  }

264 265
  uint64_t id_;
  std::unique_ptr<TableBuilder> builder_;
266
  DbMetaCache meta_;
267 268 269 270 271 272 273 274 275 276 277 278
};

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

279 280 281 282
void MockCatalogService::createSubTable(const std::string& db, const std::string& stbname, const std::string& tbname, int16_t vgid) {
  impl_->createSubTable(db, stbname, tbname, vgid);
}

283 284
void MockCatalogService::showTables() const {
  impl_->showTables();
285 286 287 288 289 290
}

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

H
Haojun Liao 已提交
291 292
int32_t MockCatalogService::catalogGetTableMeta(const SName* pTableName, STableMeta** pTableMeta) const {
  return impl_->catalogGetTableMeta(pTableName, pTableMeta);
293 294
}

H
Haojun Liao 已提交
295 296
int32_t MockCatalogService::catalogGetTableHashVgroup(const SName* pTableName, SVgroupInfo* vgInfo) const {
  return impl_->catalogGetTableHashVgroup(pTableName, vgInfo);
X
Xiaoyu Wang 已提交
297
}