mockCatalog.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/>.
 */
#include <iostream>
X
Xiaoyu Wang 已提交
16
#include "stub.h"
S
Shengliang Guan 已提交
17 18 19 20

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat"

21
#include <addr_any.h>
X
Xiaoyu Wang 已提交
22

S
Shengliang Guan 已提交
23 24
#pragma GCC diagnostic pop

wafwerar's avatar
wafwerar 已提交
25 26 27
#ifdef WINDOWS
#define TD_USE_WINSOCK
#endif
28
#include "mockCatalog.h"
29 30
namespace {

X
Xiaoyu Wang 已提交
31 32
void generateInformationSchema(MockCatalogService* mcs) {
  {
X
Xiaoyu Wang 已提交
33 34
    ITableBuilder& builder = mcs->createTableBuilder("information_schema", "dnodes", TSDB_SYSTEM_TABLE, 1)
                                 .addColumn("id", TSDB_DATA_TYPE_INT);
X
Xiaoyu Wang 已提交
35 36 37
    builder.done();
  }
  {
X
Xiaoyu Wang 已提交
38 39
    ITableBuilder& builder = mcs->createTableBuilder("information_schema", "mnodes", TSDB_SYSTEM_TABLE, 1)
                                 .addColumn("id", TSDB_DATA_TYPE_INT);
X
Xiaoyu Wang 已提交
40 41 42
    builder.done();
  }
  {
X
Xiaoyu Wang 已提交
43 44
    ITableBuilder& builder = mcs->createTableBuilder("information_schema", "modules", TSDB_SYSTEM_TABLE, 1)
                                 .addColumn("id", TSDB_DATA_TYPE_INT);
X
Xiaoyu Wang 已提交
45 46 47
    builder.done();
  }
  {
X
Xiaoyu Wang 已提交
48 49
    ITableBuilder& builder = mcs->createTableBuilder("information_schema", "qnodes", TSDB_SYSTEM_TABLE, 1)
                                 .addColumn("id", TSDB_DATA_TYPE_INT);
X
Xiaoyu Wang 已提交
50 51 52
    builder.done();
  }
  {
X
Xiaoyu Wang 已提交
53 54
    ITableBuilder& builder = mcs->createTableBuilder("information_schema", "user_databases", TSDB_SYSTEM_TABLE, 1)
                                 .addColumn("name", TSDB_DATA_TYPE_BINARY, TSDB_DB_NAME_LEN);
X
Xiaoyu Wang 已提交
55 56 57
    builder.done();
  }
  {
X
Xiaoyu Wang 已提交
58 59
    ITableBuilder& builder = mcs->createTableBuilder("information_schema", "user_functions", TSDB_SYSTEM_TABLE, 1)
                                 .addColumn("name", TSDB_DATA_TYPE_BINARY, TSDB_FUNC_NAME_LEN);
X
Xiaoyu Wang 已提交
60 61 62
    builder.done();
  }
  {
X
Xiaoyu Wang 已提交
63
    ITableBuilder& builder = mcs->createTableBuilder("information_schema", "user_indexes", TSDB_SYSTEM_TABLE, 2)
X
Xiaoyu Wang 已提交
64 65
                                 .addColumn("db_name", TSDB_DATA_TYPE_BINARY, TSDB_DB_NAME_LEN)
                                 .addColumn("table_name", TSDB_DATA_TYPE_BINARY, TSDB_TABLE_NAME_LEN);
X
Xiaoyu Wang 已提交
66 67 68
    builder.done();
  }
  {
X
Xiaoyu Wang 已提交
69
    ITableBuilder& builder = mcs->createTableBuilder("information_schema", "user_stables", TSDB_SYSTEM_TABLE, 2)
X
Xiaoyu Wang 已提交
70 71
                                 .addColumn("db_name", TSDB_DATA_TYPE_BINARY, TSDB_DB_NAME_LEN)
                                 .addColumn("stable_name", TSDB_DATA_TYPE_BINARY, TSDB_TABLE_NAME_LEN);
X
Xiaoyu Wang 已提交
72 73 74
    builder.done();
  }
  {
X
Xiaoyu Wang 已提交
75
    ITableBuilder& builder = mcs->createTableBuilder("information_schema", "user_tables", TSDB_SYSTEM_TABLE, 2)
X
Xiaoyu Wang 已提交
76 77
                                 .addColumn("db_name", TSDB_DATA_TYPE_BINARY, TSDB_DB_NAME_LEN)
                                 .addColumn("table_name", TSDB_DATA_TYPE_BINARY, TSDB_TABLE_NAME_LEN);
X
Xiaoyu Wang 已提交
78 79 80
    builder.done();
  }
  {
X
Xiaoyu Wang 已提交
81 82 83
    ITableBuilder& builder =
        mcs->createTableBuilder("information_schema", "user_table_distributed", TSDB_SYSTEM_TABLE, 1)
            .addColumn("db_name", TSDB_DATA_TYPE_BINARY, TSDB_DB_NAME_LEN);
X
Xiaoyu Wang 已提交
84 85 86
    builder.done();
  }
  {
X
Xiaoyu Wang 已提交
87 88
    ITableBuilder& builder = mcs->createTableBuilder("information_schema", "user_users", TSDB_SYSTEM_TABLE, 1)
                                 .addColumn("user_name", TSDB_DATA_TYPE_BINARY, TSDB_USER_LEN);
X
Xiaoyu Wang 已提交
89 90 91
    builder.done();
  }
  {
X
Xiaoyu Wang 已提交
92 93
    ITableBuilder& builder = mcs->createTableBuilder("information_schema", "vgroups", TSDB_SYSTEM_TABLE, 1)
                                 .addColumn("db_name", TSDB_DATA_TYPE_BINARY, TSDB_DB_NAME_LEN);
X
Xiaoyu Wang 已提交
94 95 96 97
    builder.done();
  }
}

98 99 100 101 102 103
void generatePerformanceSchema(MockCatalogService* mcs) {
  {
    ITableBuilder& builder = mcs->createTableBuilder("performance_schema", "trans", TSDB_SYSTEM_TABLE, 1)
                                 .addColumn("id", TSDB_DATA_TYPE_INT);
    builder.done();
  }
H
Haojun Liao 已提交
104 105
  {
    ITableBuilder& builder = mcs->createTableBuilder("performance_schema", "streams", TSDB_SYSTEM_TABLE, 1)
106
                                 .addColumn("stream_name", TSDB_DATA_TYPE_BINARY, TSDB_TABLE_NAME_LEN);
H
Haojun Liao 已提交
107 108
    builder.done();
  }
109 110
}

X
Xiaoyu Wang 已提交
111 112 113 114 115 116 117 118 119 120 121
/*
 * Table:t1
 *        Field        |        Type        |      DataType      |  Bytes   |
 * ==========================================================================
 *          ts         |       column       |     TIMESTAMP      |    8     |
 *          c1         |       column       |        INT         |    4     |
 *          c2         |       column       |      VARCHAR       |    20    |
 *          c3         |       column       |       BIGINT       |    8     |
 *          c4         |       column       |       DOUBLE       |    8     |
 *          c5         |       column       |       DOUBLE       |    8     |
 */
122
void generateTestT1(MockCatalogService* mcs) {
X
Xiaoyu Wang 已提交
123
  ITableBuilder& builder = mcs->createTableBuilder("test", "t1", TSDB_NORMAL_TABLE, 6)
X
Xiaoyu Wang 已提交
124 125 126 127 128 129 130 131
                               .setPrecision(TSDB_TIME_PRECISION_MILLI)
                               .setVgid(1)
                               .addColumn("ts", TSDB_DATA_TYPE_TIMESTAMP)
                               .addColumn("c1", TSDB_DATA_TYPE_INT)
                               .addColumn("c2", TSDB_DATA_TYPE_BINARY, 20)
                               .addColumn("c3", TSDB_DATA_TYPE_BIGINT)
                               .addColumn("c4", TSDB_DATA_TYPE_DOUBLE)
                               .addColumn("c5", TSDB_DATA_TYPE_DOUBLE);
132 133 134
  builder.done();
}

X
Xiaoyu Wang 已提交
135 136 137 138 139 140 141 142 143 144 145
/*
 * Super Table: st1
 *        Field        |        Type        |      DataType      |  Bytes   |
 * ==========================================================================
 *          ts         |       column       |     TIMESTAMP      |    8     |
 *          c1         |       column       |        INT         |    4     |
 *          c2         |       column       |      VARCHAR       |    20    |
 *         tag1        |        tag         |        INT         |    4     |
 *         tag2        |        tag         |      VARCHAR       |    20    |
 * Child Table: st1s1, st1s2
 */
146
void generateTestST1(MockCatalogService* mcs) {
147
  ITableBuilder& builder = mcs->createTableBuilder("test", "st1", TSDB_SUPER_TABLE, 3, 2)
X
Xiaoyu Wang 已提交
148 149 150 151 152 153
                               .setPrecision(TSDB_TIME_PRECISION_MILLI)
                               .addColumn("ts", TSDB_DATA_TYPE_TIMESTAMP)
                               .addColumn("c1", TSDB_DATA_TYPE_INT)
                               .addColumn("c2", TSDB_DATA_TYPE_BINARY, 20)
                               .addTag("tag1", TSDB_DATA_TYPE_INT)
                               .addTag("tag2", TSDB_DATA_TYPE_BINARY, 20);
154
  builder.done();
155 156
  mcs->createSubTable("test", "st1", "st1s1", 1);
  mcs->createSubTable("test", "st1", "st1s2", 2);
X
Xiaoyu Wang 已提交
157
  mcs->createSubTable("test", "st1", "st1s3", 1);
158 159
}

160 161 162 163 164 165
void generateFunctions(MockCatalogService* mcs) {
  mcs->createFunction("udf1", TSDB_FUNC_TYPE_SCALAR, TSDB_DATA_TYPE_INT, tDataTypes[TSDB_DATA_TYPE_INT].bytes, 0);
  mcs->createFunction("udf2", TSDB_FUNC_TYPE_AGGREGATE, TSDB_DATA_TYPE_DOUBLE, tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes,
                      8);
}

X
Xiaoyu Wang 已提交
166
}  // namespace
167

X
Xiaoyu Wang 已提交
168
int32_t __catalogGetHandle(const char* clusterId, struct SCatalog** catalogHandle) { return 0; }
169

X
Xiaoyu Wang 已提交
170 171
int32_t __catalogGetTableMeta(struct SCatalog* pCatalog, void* pRpc, const SEpSet* pMgmtEps, const SName* pTableName,
                              STableMeta** pTableMeta) {
172
  return g_mockCatalogService->catalogGetTableMeta(pTableName, pTableMeta);
X
Xiaoyu Wang 已提交
173 174
}

X
Xiaoyu Wang 已提交
175 176
int32_t __catalogGetTableHashVgroup(struct SCatalog* pCatalog, void* pRpc, const SEpSet* pMgmtEps,
                                    const SName* pTableName, SVgroupInfo* vgInfo) {
177
  return g_mockCatalogService->catalogGetTableHashVgroup(pTableName, vgInfo);
178 179
}

X
Xiaoyu Wang 已提交
180 181
int32_t __catalogGetTableDistVgInfo(SCatalog* pCtg, void* pRpc, const SEpSet* pMgmtEps, const SName* pTableName,
                                    SArray** pVgList) {
182
  return g_mockCatalogService->catalogGetTableDistVgInfo(pTableName, pVgList);
183 184
}

X
Xiaoyu Wang 已提交
185 186
int32_t __catalogGetDBVgVersion(SCatalog* pCtg, const char* dbFName, int32_t* version, int64_t* dbId,
                                int32_t* tableNum) {
187 188 189
  return 0;
}

X
Xiaoyu Wang 已提交
190
int32_t __catalogGetDBVgInfo(SCatalog* pCtg, void* pRpc, const SEpSet* pMgmtEps, const char* dbFName,
X
Xiaoyu Wang 已提交
191 192
                             SArray** pVgList) {
  return g_mockCatalogService->catalogGetDBVgInfo(dbFName, pVgList);
X
Xiaoyu Wang 已提交
193 194
}

195 196 197 198
int32_t __catalogGetDBCfg(SCatalog* pCtg, void* pRpc, const SEpSet* pMgmtEps, const char* dbFName, SDbCfgInfo* pDbCfg) {
  return 0;
}

199 200 201 202 203 204
int32_t __catalogChkAuth(SCatalog* pCtg, void* pRpc, const SEpSet* pMgmtEps, const char* user, const char* dbFName,
                         AUTH_TYPE type, bool* pass) {
  *pass = true;
  return 0;
}

205 206 207 208 209
int32_t __catalogGetUdfInfo(SCatalog* pCtg, void* pTrans, const SEpSet* pMgmtEps, const char* funcName,
                            SFuncInfo* pInfo) {
  return g_mockCatalogService->catalogGetUdfInfo(funcName, pInfo);
}

X
Xiaoyu Wang 已提交
210 211 212 213 214 215 216
int32_t __catalogRefreshGetTableMeta(SCatalog* pCatalog, void* pTransporter, const SEpSet* pMgmtEps,
                                     const SName* pTableName, STableMeta** pTableMeta, int32_t isSTable) {
  return g_mockCatalogService->catalogGetTableMeta(pTableName, pTableMeta);
}

int32_t __catalogRemoveTableMeta(SCatalog* pCtg, SName* pTableName) { return 0; }

217
void initMetaDataEnv() {
218
  g_mockCatalogService.reset(new MockCatalogService());
X
Xiaoyu Wang 已提交
219 220 221 222

  static Stub stub;
  stub.set(catalogGetHandle, __catalogGetHandle);
  stub.set(catalogGetTableMeta, __catalogGetTableMeta);
223
  stub.set(catalogGetSTableMeta, __catalogGetTableMeta);
X
Xiaoyu Wang 已提交
224
  stub.set(catalogGetTableHashVgroup, __catalogGetTableHashVgroup);
225
  stub.set(catalogGetTableDistVgInfo, __catalogGetTableDistVgInfo);
X
Xiaoyu Wang 已提交
226
  stub.set(catalogGetDBVgVersion, __catalogGetDBVgVersion);
X
Xiaoyu Wang 已提交
227
  stub.set(catalogGetDBVgInfo, __catalogGetDBVgInfo);
228
  stub.set(catalogGetDBCfg, __catalogGetDBCfg);
229
  stub.set(catalogChkAuth, __catalogChkAuth);
230
  stub.set(catalogGetUdfInfo, __catalogGetUdfInfo);
X
Xiaoyu Wang 已提交
231 232
  stub.set(catalogRefreshGetTableMeta, __catalogRefreshGetTableMeta);
  stub.set(catalogRemoveTableMeta, __catalogRemoveTableMeta);
X
Xiaoyu Wang 已提交
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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
  // {
  //   AddrAny any("libcatalog.so");
  //   std::map<std::string,void*> result;
  //   any.get_global_func_addr_dynsym("^catalogGetHandle$", result);
  //   for (const auto& f : result) {
  //     stub.set(f.second, __catalogGetHandle);
  //   }
  // }
  // {
  //   AddrAny any("libcatalog.so");
  //   std::map<std::string,void*> result;
  //   any.get_global_func_addr_dynsym("^catalogGetTableMeta$", result);
  //   for (const auto& f : result) {
  //     stub.set(f.second, __catalogGetTableMeta);
  //   }
  // }
  // {
  //   AddrAny any("libcatalog.so");
  //   std::map<std::string,void*> result;
  //   any.get_global_func_addr_dynsym("^catalogGetTableHashVgroup$", result);
  //   for (const auto& f : result) {
  //     stub.set(f.second, __catalogGetTableHashVgroup);
  //   }
  // }
  // {
  //   AddrAny any("libcatalog.so");
  //   std::map<std::string,void*> result;
  //   any.get_global_func_addr_dynsym("^catalogGetTableDistVgInfo$", result);
  //   for (const auto& f : result) {
  //     stub.set(f.second, __catalogGetTableDistVgInfo);
  //   }
  // }
  // {
  //   AddrAny any("libcatalog.so");
  //   std::map<std::string,void*> result;
  //   any.get_global_func_addr_dynsym("^catalogGetDBVgVersion$", result);
  //   for (const auto& f : result) {
  //     stub.set(f.second, __catalogGetDBVgVersion);
  //   }
  // }
273 274 275
}

void generateMetaData() {
276 277 278 279
  generateInformationSchema(g_mockCatalogService.get());
  generatePerformanceSchema(g_mockCatalogService.get());
  generateTestT1(g_mockCatalogService.get());
  generateTestST1(g_mockCatalogService.get());
280
  generateFunctions(g_mockCatalogService.get());
281
  g_mockCatalogService->showTables();
282 283
}

284
void destroyMetaDataEnv() { g_mockCatalogService.reset(); }