mockCatalog.cpp 8.7 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();
  }
}

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

void generateTestST1(MockCatalogService* mcs) {
117
  ITableBuilder& builder = mcs->createTableBuilder("test", "st1", TSDB_SUPER_TABLE, 3, 2)
X
Xiaoyu Wang 已提交
118 119 120 121 122 123
                               .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);
124
  builder.done();
125 126
  mcs->createSubTable("test", "st1", "st1s1", 1);
  mcs->createSubTable("test", "st1", "st1s2", 2);
127 128
}

X
Xiaoyu Wang 已提交
129
}  // namespace
130

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

X
Xiaoyu Wang 已提交
133 134
int32_t __catalogGetTableMeta(struct SCatalog* pCatalog, void* pRpc, const SEpSet* pMgmtEps, const SName* pTableName,
                              STableMeta** pTableMeta) {
H
Haojun Liao 已提交
135
  return mockCatalogService->catalogGetTableMeta(pTableName, pTableMeta);
X
Xiaoyu Wang 已提交
136 137
}

X
Xiaoyu Wang 已提交
138 139
int32_t __catalogGetTableHashVgroup(struct SCatalog* pCatalog, void* pRpc, const SEpSet* pMgmtEps,
                                    const SName* pTableName, SVgroupInfo* vgInfo) {
H
Haojun Liao 已提交
140
  return mockCatalogService->catalogGetTableHashVgroup(pTableName, vgInfo);
141 142
}

X
Xiaoyu Wang 已提交
143 144
int32_t __catalogGetTableDistVgInfo(SCatalog* pCtg, void* pRpc, const SEpSet* pMgmtEps, const SName* pTableName,
                                    SArray** pVgList) {
H
Haojun Liao 已提交
145
  return mockCatalogService->catalogGetTableDistVgInfo(pTableName, pVgList);
146 147
}

X
Xiaoyu Wang 已提交
148 149
int32_t __catalogGetDBVgVersion(SCatalog* pCtg, const char* dbFName, int32_t* version, int64_t* dbId,
                                int32_t* tableNum) {
150 151 152
  return 0;
}

X
Xiaoyu Wang 已提交
153 154
int32_t __catalogGetDBVgInfo(SCatalog* pCtg, void* pRpc, const SEpSet* pMgmtEps, const char* dbFName,
                             SArray** vgroupList) {
X
Xiaoyu Wang 已提交
155 156 157
  return 0;
}

158 159
void initMetaDataEnv() {
  mockCatalogService.reset(new MockCatalogService());
X
Xiaoyu Wang 已提交
160 161 162 163

  static Stub stub;
  stub.set(catalogGetHandle, __catalogGetHandle);
  stub.set(catalogGetTableMeta, __catalogGetTableMeta);
X
Xiaoyu Wang 已提交
164
  stub.set(catalogGetTableHashVgroup, __catalogGetTableHashVgroup);
165
  stub.set(catalogGetTableDistVgInfo, __catalogGetTableDistVgInfo);
X
Xiaoyu Wang 已提交
166
  stub.set(catalogGetDBVgVersion, __catalogGetDBVgVersion);
X
Xiaoyu Wang 已提交
167
  stub.set(catalogGetDBVgInfo, __catalogGetDBVgInfo);
X
Xiaoyu Wang 已提交
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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  // {
  //   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);
  //   }
  // }
208 209 210
}

void generateMetaData() {
X
Xiaoyu Wang 已提交
211
  generateInformationSchema(mockCatalogService.get());
212 213 214 215 216
  generateTestT1(mockCatalogService.get());
  generateTestST1(mockCatalogService.get());
  mockCatalogService->showTables();
}

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