mockCatalog.cpp 13.3 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
/*
 * 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    |
X
Xiaoyu Wang 已提交
144
 *         tag3        |        tag         |     TIMESTAMP      |    8     |
X
Xiaoyu Wang 已提交
145 146
 * Child Table: st1s1, st1s2
 */
147
void generateTestST1(MockCatalogService* mcs) {
X
Xiaoyu Wang 已提交
148
  ITableBuilder& builder = mcs->createTableBuilder("test", "st1", TSDB_SUPER_TABLE, 3, 3)
X
Xiaoyu Wang 已提交
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)
X
Xiaoyu Wang 已提交
154 155
                               .addTag("tag2", TSDB_DATA_TYPE_BINARY, 20)
                               .addTag("tag3", TSDB_DATA_TYPE_TIMESTAMP);
156
  builder.done();
157 158
  mcs->createSubTable("test", "st1", "st1s1", 1);
  mcs->createSubTable("test", "st1", "st1s2", 2);
X
Xiaoyu Wang 已提交
159
  mcs->createSubTable("test", "st1", "st1s3", 1);
160 161
}

X
Xiaoyu Wang 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
/*
 * Super Table: st2
 *        Field        |        Type        |      DataType      |  Bytes   |
 * ==========================================================================
 *          ts         |       column       |     TIMESTAMP      |    8     |
 *          c1         |       column       |        INT         |    4     |
 *          c2         |       column       |      VARCHAR       |    20    |
 *         jtag        |        tag         |        json        |    --    |
 * Child Table: st2s1, st2s2
 */
void generateTestST2(MockCatalogService* mcs) {
  ITableBuilder& builder = mcs->createTableBuilder("test", "st2", TSDB_SUPER_TABLE, 3, 1)
                               .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("jtag", TSDB_DATA_TYPE_JSON);
  builder.done();
  mcs->createSubTable("test", "st2", "st2s1", 1);
  mcs->createSubTable("test", "st2", "st2s2", 2);
}

184 185 186 187 188 189
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 已提交
190
}  // namespace
191

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

X
Xiaoyu Wang 已提交
194
int32_t __catalogGetTableMeta(struct SCatalog* pCatalog, SRequestConnInfo* pConn, const SName* pTableName,
X
Xiaoyu Wang 已提交
195
                              STableMeta** pTableMeta) {
196
  return g_mockCatalogService->catalogGetTableMeta(pTableName, pTableMeta);
X
Xiaoyu Wang 已提交
197 198
}

X
Xiaoyu Wang 已提交
199 200
int32_t __catalogGetTableHashVgroup(struct SCatalog* pCatalog, SRequestConnInfo* pConn, const SName* pTableName,
                                    SVgroupInfo* vgInfo) {
201
  return g_mockCatalogService->catalogGetTableHashVgroup(pTableName, vgInfo);
202 203
}

X
Xiaoyu Wang 已提交
204
int32_t __catalogGetTableDistVgInfo(SCatalog* pCtg, SRequestConnInfo* pConn, const SName* pTableName,
X
Xiaoyu Wang 已提交
205
                                    SArray** pVgList) {
206
  return g_mockCatalogService->catalogGetTableDistVgInfo(pTableName, pVgList);
207 208
}

X
Xiaoyu Wang 已提交
209 210
int32_t __catalogGetDBVgVersion(SCatalog* pCtg, const char* dbFName, int32_t* version, int64_t* dbId,
                                int32_t* tableNum) {
211 212 213
  return 0;
}

X
Xiaoyu Wang 已提交
214
int32_t __catalogGetDBVgInfo(SCatalog* pCtg, SRequestConnInfo* pConn, const char* dbFName, SArray** pVgList) {
X
Xiaoyu Wang 已提交
215
  return g_mockCatalogService->catalogGetDBVgInfo(dbFName, pVgList);
X
Xiaoyu Wang 已提交
216 217
}

X
Xiaoyu Wang 已提交
218
int32_t __catalogGetDBCfg(SCatalog* pCtg, SRequestConnInfo* pConn, const char* dbFName, SDbCfgInfo* pDbCfg) {
219 220 221
  return 0;
}

X
Xiaoyu Wang 已提交
222 223
int32_t __catalogChkAuth(SCatalog* pCtg, SRequestConnInfo* pConn, const char* user, const char* dbFName, AUTH_TYPE type,
                         bool* pass) {
224 225 226 227
  *pass = true;
  return 0;
}

X
Xiaoyu Wang 已提交
228
int32_t __catalogGetUdfInfo(SCatalog* pCtg, SRequestConnInfo* pConn, const char* funcName, SFuncInfo* pInfo) {
229 230 231
  return g_mockCatalogService->catalogGetUdfInfo(funcName, pInfo);
}

X
Xiaoyu Wang 已提交
232 233
int32_t __catalogRefreshGetTableMeta(SCatalog* pCatalog, SRequestConnInfo* pConn, const SName* pTableName,
                                     STableMeta** pTableMeta, int32_t isSTable) {
X
Xiaoyu Wang 已提交
234 235 236 237 238
  return g_mockCatalogService->catalogGetTableMeta(pTableName, pTableMeta);
}

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

X
Xiaoyu Wang 已提交
239 240 241 242 243
int32_t __catalogGetTableIndex(SCatalog* pCtg, void* pTrans, const SEpSet* pMgmtEps, const SName* pName,
                               SArray** pRes) {
  return g_mockCatalogService->catalogGetTableIndex(pName, pRes);
}

244
void initMetaDataEnv() {
245
  g_mockCatalogService.reset(new MockCatalogService());
X
Xiaoyu Wang 已提交
246 247 248 249

  static Stub stub;
  stub.set(catalogGetHandle, __catalogGetHandle);
  stub.set(catalogGetTableMeta, __catalogGetTableMeta);
250
  stub.set(catalogGetSTableMeta, __catalogGetTableMeta);
X
Xiaoyu Wang 已提交
251
  stub.set(catalogGetTableHashVgroup, __catalogGetTableHashVgroup);
252
  stub.set(catalogGetTableDistVgInfo, __catalogGetTableDistVgInfo);
X
Xiaoyu Wang 已提交
253
  stub.set(catalogGetDBVgVersion, __catalogGetDBVgVersion);
X
Xiaoyu Wang 已提交
254
  stub.set(catalogGetDBVgInfo, __catalogGetDBVgInfo);
255
  stub.set(catalogGetDBCfg, __catalogGetDBCfg);
256
  stub.set(catalogChkAuth, __catalogChkAuth);
257
  stub.set(catalogGetUdfInfo, __catalogGetUdfInfo);
X
Xiaoyu Wang 已提交
258 259
  stub.set(catalogRefreshGetTableMeta, __catalogRefreshGetTableMeta);
  stub.set(catalogRemoveTableMeta, __catalogRemoveTableMeta);
X
Xiaoyu Wang 已提交
260
  stub.set(catalogGetTableIndex, __catalogGetTableIndex);
X
Xiaoyu Wang 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
  // {
  //   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);
  //   }
  // }
301 302 303
}

void generateMetaData() {
304 305 306 307
  generateInformationSchema(g_mockCatalogService.get());
  generatePerformanceSchema(g_mockCatalogService.get());
  generateTestT1(g_mockCatalogService.get());
  generateTestST1(g_mockCatalogService.get());
X
Xiaoyu Wang 已提交
308
  generateTestST2(g_mockCatalogService.get());
309
  generateFunctions(g_mockCatalogService.get());
310
  g_mockCatalogService->showTables();
311 312
}

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