mockCatalog.cpp 10.4 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 76
    ITableBuilder& builder = mcs->createTableBuilder("information_schema", "user_streams", TSDB_SYSTEM_TABLE, 1)
                                 .addColumn("stream_name", TSDB_DATA_TYPE_BINARY, TSDB_TABLE_NAME_LEN);
X
Xiaoyu Wang 已提交
77 78 79
    builder.done();
  }
  {
X
Xiaoyu Wang 已提交
80
    ITableBuilder& builder = mcs->createTableBuilder("information_schema", "user_tables", TSDB_SYSTEM_TABLE, 2)
X
Xiaoyu Wang 已提交
81 82
                                 .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 已提交
83 84 85
    builder.done();
  }
  {
X
Xiaoyu Wang 已提交
86 87 88
    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 已提交
89 90 91
    builder.done();
  }
  {
X
Xiaoyu Wang 已提交
92 93
    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 已提交
94 95 96
    builder.done();
  }
  {
X
Xiaoyu Wang 已提交
97 98
    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 已提交
99 100 101 102
    builder.done();
  }
}

X
Xiaoyu Wang 已提交
103 104 105 106 107 108 109 110 111 112 113
/*
 * 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     |
 */
114
void generateTestT1(MockCatalogService* mcs) {
X
Xiaoyu Wang 已提交
115
  ITableBuilder& builder = mcs->createTableBuilder("test", "t1", TSDB_NORMAL_TABLE, 6)
X
Xiaoyu Wang 已提交
116 117 118 119 120 121 122 123
                               .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);
124 125 126
  builder.done();
}

X
Xiaoyu Wang 已提交
127 128 129 130 131 132 133 134 135 136 137
/*
 * 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
 */
138
void generateTestST1(MockCatalogService* mcs) {
139
  ITableBuilder& builder = mcs->createTableBuilder("test", "st1", TSDB_SUPER_TABLE, 3, 2)
X
Xiaoyu Wang 已提交
140 141 142 143 144 145
                               .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);
146
  builder.done();
147 148
  mcs->createSubTable("test", "st1", "st1s1", 1);
  mcs->createSubTable("test", "st1", "st1s2", 2);
149 150
}

X
Xiaoyu Wang 已提交
151
}  // namespace
152

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

X
Xiaoyu Wang 已提交
155 156
int32_t __catalogGetTableMeta(struct SCatalog* pCatalog, void* pRpc, const SEpSet* pMgmtEps, const SName* pTableName,
                              STableMeta** pTableMeta) {
H
Haojun Liao 已提交
157
  return mockCatalogService->catalogGetTableMeta(pTableName, pTableMeta);
X
Xiaoyu Wang 已提交
158 159
}

X
Xiaoyu Wang 已提交
160 161
int32_t __catalogGetTableHashVgroup(struct SCatalog* pCatalog, void* pRpc, const SEpSet* pMgmtEps,
                                    const SName* pTableName, SVgroupInfo* vgInfo) {
H
Haojun Liao 已提交
162
  return mockCatalogService->catalogGetTableHashVgroup(pTableName, vgInfo);
163 164
}

X
Xiaoyu Wang 已提交
165 166
int32_t __catalogGetTableDistVgInfo(SCatalog* pCtg, void* pRpc, const SEpSet* pMgmtEps, const SName* pTableName,
                                    SArray** pVgList) {
H
Haojun Liao 已提交
167
  return mockCatalogService->catalogGetTableDistVgInfo(pTableName, pVgList);
168 169
}

X
Xiaoyu Wang 已提交
170 171
int32_t __catalogGetDBVgVersion(SCatalog* pCtg, const char* dbFName, int32_t* version, int64_t* dbId,
                                int32_t* tableNum) {
172 173 174
  return 0;
}

X
Xiaoyu Wang 已提交
175 176
int32_t __catalogGetDBVgInfo(SCatalog* pCtg, void* pRpc, const SEpSet* pMgmtEps, const char* dbFName,
                             SArray** vgroupList) {
X
Xiaoyu Wang 已提交
177 178 179
  return 0;
}

180 181 182 183
int32_t __catalogGetDBCfg(SCatalog* pCtg, void* pRpc, const SEpSet* pMgmtEps, const char* dbFName, SDbCfgInfo* pDbCfg) {
  return 0;
}

184 185 186 187 188 189
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;
}

190 191
void initMetaDataEnv() {
  mockCatalogService.reset(new MockCatalogService());
X
Xiaoyu Wang 已提交
192 193 194 195

  static Stub stub;
  stub.set(catalogGetHandle, __catalogGetHandle);
  stub.set(catalogGetTableMeta, __catalogGetTableMeta);
196
  stub.set(catalogGetSTableMeta, __catalogGetTableMeta);
X
Xiaoyu Wang 已提交
197
  stub.set(catalogGetTableHashVgroup, __catalogGetTableHashVgroup);
198
  stub.set(catalogGetTableDistVgInfo, __catalogGetTableDistVgInfo);
X
Xiaoyu Wang 已提交
199
  stub.set(catalogGetDBVgVersion, __catalogGetDBVgVersion);
X
Xiaoyu Wang 已提交
200
  stub.set(catalogGetDBVgInfo, __catalogGetDBVgInfo);
201
  stub.set(catalogGetDBCfg, __catalogGetDBCfg);
202
  stub.set(catalogChkAuth, __catalogChkAuth);
X
Xiaoyu Wang 已提交
203 204 205 206 207 208 209 210 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 237 238 239 240 241 242
  // {
  //   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);
  //   }
  // }
243 244 245
}

void generateMetaData() {
X
Xiaoyu Wang 已提交
246
  generateInformationSchema(mockCatalogService.get());
247 248 249 250 251
  generateTestT1(mockCatalogService.get());
  generateTestST1(mockCatalogService.get());
  mockCatalogService->showTables();
}

X
Xiaoyu Wang 已提交
252
void destroyMetaDataEnv() { mockCatalogService.reset(); }