ob_schema_struct.h 239.8 KB
Newer Older
O
oceanbase-admin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
/**
 * Copyright (c) 2021 OceanBase
 * OceanBase CE is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */

#ifndef _OB_OCEANBASE_SCHEMA_SCHEMA_STRUCT_H
#define _OB_OCEANBASE_SCHEMA_SCHEMA_STRUCT_H

#include <stdint.h>
#include "lib/hash/ob_hashset.h"
#include "lib/hash/ob_placement_hashset.h"
#include "lib/net/ob_addr.h"
#include "common/ob_range.h"
#include "common/row/ob_row_util.h"
#include "share/ob_replica_info.h"
#include "share/ob_duplicate_scope_define.h"
#include "share/sequence/ob_sequence_option.h"
#include "share/system_variable/ob_system_variable_factory.h"
#include "share/schema/ob_priv_type.h"
#include "share/ob_priv_common.h"

#ifdef __cplusplus
extern "C" {
#endif
struct _ParseNode;
typedef struct _ParseNode ParseNode;
#ifdef __cplusplus
}
#endif

namespace oceanbase {
namespace common {
class ObIAllocator;
class ObSqlString;
class ObString;
class ObDataTypeCastParams;
class ObPartitionKey;
}  // namespace common
namespace sql {
class ObSQLSessionInfo;
class ObPartitionExecutorUtils;
}  // namespace sql
namespace rootserver {
class ObRandomZoneSelector;
class ObReplicaAddr;
}  // namespace rootserver
namespace share {
namespace schema {
typedef common::Ob2DArray<common::ObObjParam, common::OB_MALLOC_BIG_BLOCK_SIZE, common::ObWrapperAllocator, false>
    ParamStore;
class ObSchemaGetterGuard;
class ObSimpleTableSchemaV2;
class ObTableSchema;
class ObColumnSchemaV2;
#define ASSIGN_STRING(dst, src, field, buffer, skip)           \
  (dst)->field.assign(buffer + offset, (src)->field.length()); \
  offset += (src)->field.length() + (skip);

X
xy0 已提交
66 67
#define ASSIGN_CONST_STRING(dst, src, field, buffer, skip)                               \
  (const_cast<ObString &>((dst)->field)).assign(buffer + offset, (src)->field.length()); \
O
oceanbase-admin 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  offset += (src)->field.length() + (skip);

// match the default now func
#define IS_DEFAULT_NOW_STR(data_type, def_str) \
  ((ObDateTimeType == data_type || ObTimestampType == data_type) && (def_str == N_UPPERCASE_CUR_TIMESTAMP))

#define IS_DEFAULT_NOW_OBJ(def_obj) \
  (ObExtendType == def_obj.get_type() && ObActionFlag::OP_DEFAULT_NOW_FLAG == def_obj.get_ext())

#define OB_ORACLE_CONS_OR_IDX_CUTTED_NAME_LEN 60

// the lower 32-bit flag need be store in __all_column
static const uint64_t OB_MIN_ID = 0;  // used for lower_bound
#define NON_CASCADE_FLAG INT64_C(0)
#define VIRTUAL_GENERATED_COLUMN_FLAG (INT64_C(1) << 0)
#define STORED_GENERATED_COLUMN_FLAG (INT64_C(1) << 1)
#define CTE_GENERATED_COLUMN_FLAG (INT64_C(1) << 2)
#define DEFAULT_EXPR_V2_COLUMN_FLAG (INT64_C(1) << 3)
#define PRIMARY_VP_COLUMN_FLAG (INT64_C(1) << 4)
#define RESERVED_COLUMN_FLAG (INT64_C(1) << 5)
#define INVISIBLE_COLUMN_FLAG (INT64_C(1) << 6)
// The logic of the new table without a primary key changes the column (partition key) to the primary key
#define HEAP_ALTER_ROWKEY_FLAG (INT64_C(1) << 8)
R
rq0 已提交
91
#define PAD_WHEN_CALC_GENERATED_COLUMN_FLAG (INT64_C(1) << 19)
O
oceanbase-admin 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
// the high 32-bit flag isn't stored in __all_column
#define GENERATED_DEPS_CASCADE_FLAG (INT64_C(1) << 32)
#define GENERATED_CTXCAT_CASCADE_FLAG (INT64_C(1) << 33)
#define TABLE_PART_KEY_COLUMN_FLAG (INT64_C(1) << 34)

#define STORED_COLUMN_FLAGS_MASK 0xFFFFFFFF
//-------enum defenition
enum ObTableLoadType {
  TABLE_LOAD_TYPE_IN_DISK = 0,
  TABLE_LOAD_TYPE_IN_RAM = 1,
  TABLE_LOAD_TYPE_MAX = 2,
};
// the defination type of table
enum ObTableDefType {
  TABLE_DEF_TYPE_INTERNAL = 0,
  TABLE_DEF_TYPE_USER = 1,
  TABLE_DEF_TYPE_MAX = 2,
};
// level of patition
enum ObPartitionLevel {
  PARTITION_LEVEL_ZERO = 0,  // means non-partitioned table
  PARTITION_LEVEL_ONE = 1,
  PARTITION_LEVEL_TWO = 2,
  PARTITION_LEVEL_MAX,
};
// type of hash name to generate
enum ObHashNameType { FIRST_PART = 0, TEMPLATE_SUB_PART = 1, INDIVIDUAL_SUB_PART = 2 };

OB_INLINE int64_t generate_phy_part_id(int64_t part_idx, int64_t sub_part_idx, ObPartitionLevel part_level)
{
  return (PARTITION_LEVEL_TWO == part_level) ? common::generate_phy_part_id(part_idx, sub_part_idx) : part_idx;
}

enum ObPartitionFuncType {
  // TODO add other type
  PARTITION_FUNC_TYPE_HASH = 0,
  PARTITION_FUNC_TYPE_KEY,
  PARTITION_FUNC_TYPE_KEY_IMPLICIT,
  PARTITION_FUNC_TYPE_RANGE,
  PARTITION_FUNC_TYPE_RANGE_COLUMNS,
  PARTITION_FUNC_TYPE_LIST,
  PARTITION_FUNC_TYPE_KEY_V2,
  PARTITION_FUNC_TYPE_LIST_COLUMNS,
  PARTITION_FUNC_TYPE_HASH_V2,
  PARTITION_FUNC_TYPE_KEY_V3,
  PARTITION_FUNC_TYPE_KEY_IMPLICIT_V2,
  PARTITION_FUNC_TYPE_MAX,
};

X
xy0 已提交
141
int get_part_type_str(ObPartitionFuncType type, common::ObString &str, bool can_change = true);
O
oceanbase-admin 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

inline bool is_hash_part(const ObPartitionFuncType part_type)
{
  return PARTITION_FUNC_TYPE_HASH == part_type || PARTITION_FUNC_TYPE_HASH_V2 == part_type;
}

inline bool is_hash_like_part(const ObPartitionFuncType part_type)
{
  return PARTITION_FUNC_TYPE_HASH == part_type || PARTITION_FUNC_TYPE_HASH_V2 == part_type ||
         PARTITION_FUNC_TYPE_KEY == part_type || PARTITION_FUNC_TYPE_KEY_IMPLICIT == part_type ||
         PARTITION_FUNC_TYPE_KEY_IMPLICIT_V2 == part_type || PARTITION_FUNC_TYPE_KEY_V2 == part_type ||
         PARTITION_FUNC_TYPE_KEY_V3 == part_type;
}

inline bool is_key_part(const ObPartitionFuncType part_type)
{
  return PARTITION_FUNC_TYPE_KEY == part_type || PARTITION_FUNC_TYPE_KEY_V2 == part_type ||
         PARTITION_FUNC_TYPE_KEY_IMPLICIT == part_type || PARTITION_FUNC_TYPE_KEY_IMPLICIT_V2 == part_type ||
         PARTITION_FUNC_TYPE_KEY_V3 == part_type;
}

inline bool is_range_part(const ObPartitionFuncType part_type)
{
  return PARTITION_FUNC_TYPE_RANGE == part_type || PARTITION_FUNC_TYPE_RANGE_COLUMNS == part_type;
}

inline bool is_list_part(const ObPartitionFuncType part_type)
{
  return PARTITION_FUNC_TYPE_LIST == part_type || PARTITION_FUNC_TYPE_LIST_COLUMNS == part_type;
}

X
xy0 已提交
173
int is_sys_table_name(uint64_t database_id, const common::ObString &table_name, bool &is_sys_table);
O
oceanbase-admin 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

// adding new table type, take care ObRootUtils::is_balance_target_schema() interface
// This structure indicates whether the tableSchema of this type is the object of load balancing,
// and the judgment is based on whether the table schema has physical partitions of entities,
// and only non-system tables are load-balanced.
enum ObTableType {
  SYSTEM_TABLE = 0,
  SYSTEM_VIEW,
  VIRTUAL_TABLE,
  USER_TABLE,
  USER_VIEW,
  USER_INDEX,          // urgly, compatible with uniform process in ddl_service
                       // will add index for sys table???
  TMP_TABLE,           // Temporary table in mysql compatibility mode
  MATERIALIZED_VIEW,   // Must be put behind, otherwise compatibility will fail
  TMP_TABLE_ORA_SESS,  // Temporary table in oracle compatibility mode, session level
  TMP_TABLE_ORA_TRX,   // Temporary tables in oracle compatibility mode, transaction level
  TMP_TABLE_ALL,       // All types of temporary tables, only used for alter system statements
  AUX_VERTIAL_PARTITION_TABLE,
  MAX_TABLE_TYPE
};

// ObTableType=>const char* ; used for show tables
X
xy0 已提交
197 198
const char *ob_table_type_str(ObTableType type);
const char *ob_mysql_table_type_str(ObTableType type);
O
oceanbase-admin 已提交
199 200 201 202 203 204 205 206 207 208 209

ObTableType get_inner_table_type_by_id(const uint64_t tid);

enum ObIndexType {
  INDEX_TYPE_IS_NOT = 0,  // is not index table
  INDEX_TYPE_NORMAL_LOCAL = 1,
  INDEX_TYPE_UNIQUE_LOCAL = 2,
  INDEX_TYPE_NORMAL_GLOBAL = 3,
  INDEX_TYPE_UNIQUE_GLOBAL = 4,
  INDEX_TYPE_PRIMARY = 5,
  INDEX_TYPE_DOMAIN_CTXCAT = 6,
210 211 212 213 214 215
  /* create table t1(c1 int primary key, c2 int);
   * create index i1 on t1(c2)
   * i1 is a global index.
   * But we regard i1 as a local index for better access performance.
   * Since it is non-partitioned, it's safe to do so.
   */
O
oceanbase-admin 已提交
216 217
  INDEX_TYPE_NORMAL_GLOBAL_LOCAL_STORAGE = 7,
  INDEX_TYPE_UNIQUE_GLOBAL_LOCAL_STORAGE = 8,
218

O
oceanbase-admin 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
  INDEX_TYPE_MAX = 9,
};

// using type for index
enum ObIndexUsingType {
  USING_BTREE = 0,
  USING_HASH,
  USING_TYPE_MAX,
};

enum ViewCheckOption {
  VIEW_CHECK_OPTION_NONE = 0,
  VIEW_CHECK_OPTION_LOCAL = 1,
  VIEW_CHECK_OPTION_CASCADED = 2,
  VIEW_CHECK_OPTION_MAX = 3,
};

X
xy0 已提交
236
const char *ob_view_check_option_str(const ViewCheckOption option);
O
oceanbase-admin 已提交
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
enum ObIndexStatus {
  // this is used in index virtual table:__index_process_info:means the table may be deleted when you get it
  INDEX_STATUS_NOT_FOUND = 0,
  INDEX_STATUS_UNAVAILABLE = 1,
  INDEX_STATUS_AVAILABLE = 2,
  INDEX_STATUS_UNIQUE_CHECKING = 3,    // not used anymore
  INDEX_STATUS_UNIQUE_INELIGIBLE = 4,  // not used anymore
  INDEX_STATUS_INDEX_ERROR = 5,
  INDEX_STATUS_RESTORE_INDEX_ERROR = 6,
  INDEX_STATUS_UNUSABLE = 7,
  INDEX_STATUS_MAX = 8,
};

enum ObPartitionStatus {
  PARTITION_STATUS_INVALID = -1,
  PARTITION_STATUS_ACTIVE = 0,
  PARTITION_STATUS_LOGICAL_SPLITTING = 1,
  PARTITION_STATUS_MERGE = 2,
  PARTITION_STATUS_PHYSICAL_SPLITTING = 3,
  PARTITION_STATUS_MAX,
};

enum ObAlterColumnMode {
  ALTER_COLUMN_MODE_FORBIDDEN = 0,
  ALTER_COLUMN_MODE_ALLOWED = 1,
  ALTER_COLUMN_MODE_FORBIDDEN_WHEN_MAJOR = 2,
};

struct ObRefreshSchemaStatus {
G
gm 已提交
266
public:
O
oceanbase-admin 已提交
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
  ObRefreshSchemaStatus()
      : tenant_id_(common::OB_INVALID_TENANT_ID),
        snapshot_timestamp_(common::OB_INVALID_TIMESTAMP),
        readable_schema_version_(common::OB_INVALID_VERSION),
        created_schema_version_(common::OB_INVALID_VERSION)
  {}

  ObRefreshSchemaStatus(const uint64_t tenant_id, const int64_t snapshot_timestamp,
      const int64_t readable_schema_version, const int64_t created_schema_version)
      : tenant_id_(tenant_id),
        snapshot_timestamp_(snapshot_timestamp),
        readable_schema_version_(readable_schema_version),
        created_schema_version_(created_schema_version)
  {}

  void reset()
  {
    tenant_id_ = common::OB_INVALID_TENANT_ID;
    snapshot_timestamp_ = common::OB_INVALID_TIMESTAMP;
    readable_schema_version_ = common::OB_INVALID_VERSION;
    created_schema_version_ = common::OB_INVALID_VERSION;
  }

  bool is_valid() const
  {
    return common::OB_INVALID_TENANT_ID != tenant_id_;
  }

X
xy0 已提交
295
  bool operator==(const ObRefreshSchemaStatus &other) const
O
oceanbase-admin 已提交
296 297 298 299 300 301 302 303 304
  {
    return ((this == &other) ||
            (this->tenant_id_ == other.tenant_id_ && this->snapshot_timestamp_ == other.snapshot_timestamp_ &&
                this->readable_schema_version_ == other.readable_schema_version_ &&
                this->created_schema_version_ == other.created_schema_version_));
  }

  TO_STRING_KV(K_(tenant_id), K_(snapshot_timestamp), K_(readable_schema_version), K_(created_schema_version));

G
gm 已提交
305
public:
O
oceanbase-admin 已提交
306 307 308 309 310 311 312 313 314 315 316
  // tenant_id_ is OB_INVALID_TENANT_ID which means non-split mode, effectively means split mode
  uint64_t tenant_id_;
  // snapshot_timestamp_ > 0 Indicates that a weakly consistent read is required, and is used in standalone cluster mode
  int64_t snapshot_timestamp_;
  int64_t readable_schema_version_;
  int64_t created_schema_version_;
};

struct ObRefreshSchemaInfo {
  OB_UNIS_VERSION(1);

G
gm 已提交
317
public:
O
oceanbase-admin 已提交
318 319 320 321 322 323
  ObRefreshSchemaInfo()
      : schema_version_(common::OB_INVALID_VERSION),
        tenant_id_(common::OB_INVALID_TENANT_ID),
        sequence_id_(common::OB_INVALID_ID),
        split_schema_version_(common::OB_INVALID_VERSION)
  {}
X
xy0 已提交
324
  ObRefreshSchemaInfo(const ObRefreshSchemaInfo &other);
O
oceanbase-admin 已提交
325 326
  virtual ~ObRefreshSchemaInfo()
  {}
X
xy0 已提交
327
  int assign(const ObRefreshSchemaInfo &other);
O
oceanbase-admin 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
  void reset();
  bool is_valid() const;
  void set_tenant_id(const uint64_t tenant_id)
  {
    tenant_id_ = tenant_id;
  }
  void set_schema_version(const int64_t schema_version)
  {
    schema_version_ = schema_version;
  }
  void set_sequence_id(const uint64_t sequence_id)
  {
    sequence_id_ = sequence_id;
  }
  void set_split_schema_version(const int64_t split_schema_version)
  {
    split_schema_version_ = split_schema_version;
  }
  uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
  int64_t get_schema_version() const
  {
    return schema_version_;
  }
  uint64_t get_sequence_id() const
  {
    return sequence_id_;
  }
  int64_t get_split_schema_version() const
  {
    return split_schema_version_;
  }
  TO_STRING_KV(K_(schema_version), K_(tenant_id), K_(sequence_id), K_(split_schema_version));

G
gm 已提交
364
private:
O
oceanbase-admin 已提交
365 366 367 368 369 370 371
  int64_t schema_version_;
  uint64_t tenant_id_;
  uint64_t sequence_id_;
  int64_t split_schema_version_;
};

class ObDropTenantInfo {
G
gm 已提交
372
public:
O
oceanbase-admin 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
  ObDropTenantInfo() : tenant_id_(common::OB_INVALID_TENANT_ID), schema_version_(common::OB_INVALID_VERSION)
  {}
  virtual ~ObDropTenantInfo(){};
  void reset();
  bool is_valid() const;
  uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
  int64_t get_schema_version() const
  {
    return schema_version_;
  }
  void set_tenant_id(const uint64_t tenant_id)
  {
    tenant_id_ = tenant_id;
  }
  void set_schema_version(const int64_t schema_version)
  {
    schema_version_ = schema_version;
  }
  TO_STRING_KV(K_(tenant_id), K_(schema_version));

G
gm 已提交
396
private:
O
oceanbase-admin 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
  uint64_t tenant_id_;
  int64_t schema_version_;
};

struct ObIndexTableStat {
  ObIndexTableStat()
      : index_id_(common::OB_INVALID_ID), index_status_(share::schema::INDEX_STATUS_UNAVAILABLE), is_drop_schema_(false)
  {}
  ObIndexTableStat(const uint64_t index_id, const share::schema::ObIndexStatus index_status, const bool is_drop_schema)
      : index_id_(index_id), index_status_(index_status), is_drop_schema_(is_drop_schema)
  {}
  TO_STRING_KV(K_(index_id), K_(index_status), K_(is_drop_schema));
  uint64_t index_id_;
  share::schema::ObIndexStatus index_status_;
  bool is_drop_schema_;
};

inline bool is_final_invalid_index_status(const ObIndexStatus index_status, const bool is_drop_schema)
{
  bool ret_bool = !(index_status >= INDEX_STATUS_UNAVAILABLE && index_status <= INDEX_STATUS_UNIQUE_CHECKING);

  if (!ret_bool) {
    ret_bool = is_drop_schema;
  }
  return ret_bool;
}

inline bool is_final_index_status(const ObIndexStatus index_status, const bool is_dropped_schema)
{
  return INDEX_STATUS_AVAILABLE == index_status || INDEX_STATUS_INDEX_ERROR == index_status ||
         INDEX_STATUS_RESTORE_INDEX_ERROR == index_status || INDEX_STATUS_UNUSABLE == index_status || is_dropped_schema;
}

inline bool is_error_index_status(const ObIndexStatus index_status, const bool is_dropped_schema)
{
  return INDEX_STATUS_INDEX_ERROR == index_status || INDEX_STATUS_RESTORE_INDEX_ERROR == index_status ||
         INDEX_STATUS_UNUSABLE == index_status || is_dropped_schema;
}

inline bool is_available_index_status(const ObIndexStatus index_status)
{
  return INDEX_STATUS_AVAILABLE == index_status;
}

X
xy0 已提交
441
const char *ob_index_status_str(ObIndexStatus status);
O
oceanbase-admin 已提交
442 443 444 445 446 447

struct ObTenantTableId {
  ObTenantTableId() : tenant_id_(common::OB_INVALID_ID), table_id_(common::OB_INVALID_ID)
  {}
  ObTenantTableId(const uint64_t tenant_id, const uint64_t table_id) : tenant_id_(tenant_id), table_id_(table_id)
  {}
X
xy0 已提交
448
  bool operator==(const ObTenantTableId &rv) const
O
oceanbase-admin 已提交
449 450 451
  {
    return (tenant_id_ == rv.tenant_id_) && (table_id_ == rv.table_id_);
  }
X
xy0 已提交
452
  ObTenantTableId &operator=(const ObTenantTableId &tenant_table_id);
O
oceanbase-admin 已提交
453 454 455 456
  int64_t hash() const
  {
    return table_id_;
  }
X
xy0 已提交
457
  bool operator<(const ObTenantTableId &rv) const
O
oceanbase-admin 已提交
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
  {
    bool res = tenant_id_ < rv.tenant_id_;
    if (tenant_id_ == rv.tenant_id_) {
      res = table_id_ < rv.table_id_;
    }
    return res;
  }
  void reset()
  {
    tenant_id_ = common::OB_INVALID_ID;
    table_id_ = common::OB_INVALID_ID;
  }
  bool is_valid() const
  {
    return (common::OB_INVALID_ID != tenant_id_) && (common::OB_INVALID_ID != table_id_);
  }

  TO_STRING_KV(K_(tenant_id), K_(table_id));

  uint64_t tenant_id_;
  uint64_t table_id_;
};

struct ObTenantDatabaseId {
  ObTenantDatabaseId() : tenant_id_(common::OB_INVALID_ID), database_id_(common::OB_INVALID_ID)
  {}
  ObTenantDatabaseId(const uint64_t tenant_id, const uint64_t database_id)
      : tenant_id_(tenant_id), database_id_(database_id)
  {}
X
xy0 已提交
487
  bool operator==(const ObTenantDatabaseId &rv) const
O
oceanbase-admin 已提交
488 489 490 491 492 493 494
  {
    return ((tenant_id_ == rv.tenant_id_) && (database_id_ == rv.database_id_));
  }
  int64_t hash() const
  {
    return database_id_;
  }
X
xy0 已提交
495
  bool operator<(const ObTenantDatabaseId &rv) const
O
oceanbase-admin 已提交
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
  {
    bool res = tenant_id_ < rv.tenant_id_;
    if (tenant_id_ == rv.tenant_id_) {
      res = database_id_ < rv.database_id_;
    }
    return res;
  }
  void reset()
  {
    tenant_id_ = common::OB_INVALID_ID;
    database_id_ = common::OB_INVALID_ID;
  }
  bool is_valid() const
  {
    return (common::OB_INVALID_ID != tenant_id_) && (common::OB_INVALID_ID != database_id_);
  }

  TO_STRING_KV(K_(tenant_id), K_(database_id));

  uint64_t tenant_id_;
  uint64_t database_id_;
};

struct ObTenantTablegroupId {
  ObTenantTablegroupId() : tenant_id_(common::OB_INVALID_ID), tablegroup_id_(common::OB_INVALID_ID)
  {}
  ObTenantTablegroupId(const uint64_t tenant_id, const uint64_t tablegroup_id)
      : tenant_id_(tenant_id), tablegroup_id_(tablegroup_id)
  {}
X
xy0 已提交
525
  bool operator==(const ObTenantTablegroupId &rv) const
O
oceanbase-admin 已提交
526 527 528 529 530 531 532
  {
    return (tenant_id_ == rv.tenant_id_) && (tablegroup_id_ == rv.tablegroup_id_);
  }
  int64_t hash() const
  {
    return tablegroup_id_;
  }
X
xy0 已提交
533
  bool operator<(const ObTenantTablegroupId &rv) const
O
oceanbase-admin 已提交
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
  {
    bool res = tenant_id_ < rv.tenant_id_;
    if (tenant_id_ == rv.tenant_id_) {
      res = tablegroup_id_ < rv.tablegroup_id_;
    }
    return res;
  }
  bool is_valid() const
  {
    return (common::OB_INVALID_ID != tenant_id_) && (common::OB_INVALID_ID != tablegroup_id_);
  }
  void reset()
  {
    tenant_id_ = common::OB_INVALID_ID;
    tablegroup_id_ = common::OB_INVALID_ID;
  }

  TO_STRING_KV(K_(tenant_id), K_(tablegroup_id));

  uint64_t tenant_id_;
  uint64_t tablegroup_id_;
};

typedef enum {
  TENANT_SCHEMA = 0,
  OUTLINE_SCHEMA = 1,
  USER_SCHEMA = 2,
  DATABASE_SCHEMA = 3,
  TABLEGROUP_SCHEMA = 4,
  TABLE_SCHEMA = 5,
  DATABASE_PRIV = 6,
  TABLE_PRIV = 7,
  ROUTINE_SCHEMA = 8,
  SYNONYM_SCHEMA = 9,
  PLAN_BASELINE_SCHEMA = 10,
  PACKAGE_SCHEMA = 12,
  UDF_SCHEMA = 13,
  SEQUENCE_SCHEMA = 14,
  SYS_VARIABLE_SCHEMA = 15,
  UDT_SCHEMA = 16,
  // At present, only liboblog constructs simple table schema in real time in lazy mode, ob does not use it temporarily
  TABLE_SIMPLE_SCHEMA = 17,
  TABLESPACE_SCHEMA = 18,
  TRIGGER_SCHEMA = 19,
  KEYSTORE_SCHEMA = 20,
  PROFILE_SCHEMA = 25,
  AUDIT_SCHEMA = 26,  // UNUSED
  SYS_PRIV = 27,
  OBJ_PRIV = 28,
  DBLINK_SCHEMA = 29,
  LINK_TABLE_SCHEMA = 30,
  ///<<< add schema type before this line
  OB_MAX_SCHEMA
} ObSchemaType;

// The user tenant system table schema is taken from the system tenant, and the schema_id needs to be changed
X
xy0 已提交
590
int need_change_schema_id(const ObSchemaType schema_type, const uint64_t schema_id, bool &need_change);
O
oceanbase-admin 已提交
591

X
xy0 已提交
592
const char *schema_type_str(const ObSchemaType schema_type);
O
oceanbase-admin 已提交
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621

bool is_normal_schema(const ObSchemaType schema_type);

struct ObSchemaStatisticsInfo {
  ObSchemaStatisticsInfo(ObSchemaType schema_type, int64_t size = 0, int64_t count = 0)
      : schema_type_(schema_type), size_(size), count_(count)
  {}
  ObSchemaStatisticsInfo() : schema_type_(OB_MAX_SCHEMA), size_(0), count_(0)
  {}
  ~ObSchemaStatisticsInfo()
  {}
  bool is_valid() const
  {
    return OB_MAX_SCHEMA != schema_type_;
  }
  void reset()
  {
    schema_type_ = OB_MAX_SCHEMA;
    size_ = 0;
    count_ = 0;
  }
  TO_STRING_KV(K_(schema_type), K_(size), K_(count));
  ObSchemaType schema_type_;
  int64_t size_;
  int64_t count_;
};

class ObPartition;
class PartIdPartitionArrayCmp {
G
gm 已提交
622
public:
O
oceanbase-admin 已提交
623 624
  PartIdPartitionArrayCmp() : ret_(common::OB_SUCCESS)
  {}
X
xy0 已提交
625
  bool operator()(const share::schema::ObPartition *left, const share::schema::ObPartition *right);
O
oceanbase-admin 已提交
626

G
gm 已提交
627
public:
O
oceanbase-admin 已提交
628 629 630 631 632
  int get_ret() const
  {
    return ret_;
  }

G
gm 已提交
633
private:
O
oceanbase-admin 已提交
634 635 636 637 638
  int ret_;
};

class ObSubPartition;
class SubPartIdPartitionArrayCmp {
G
gm 已提交
639
public:
O
oceanbase-admin 已提交
640 641
  SubPartIdPartitionArrayCmp() : ret_(common::OB_SUCCESS)
  {}
X
xy0 已提交
642
  bool operator()(const share::schema::ObSubPartition *left, const share::schema::ObSubPartition *right);
O
oceanbase-admin 已提交
643

G
gm 已提交
644
public:
O
oceanbase-admin 已提交
645 646 647 648 649
  int get_ret() const
  {
    return ret_;
  }

G
gm 已提交
650
private:
O
oceanbase-admin 已提交
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
  int ret_;
};

struct ObSimpleTableSchema {
  uint64_t tenant_id_;
  uint64_t database_id_;
  uint64_t tablegroup_id_;
  uint64_t table_id_;
  uint64_t data_table_id_;
  common::ObString table_name_;
  int64_t schema_version_;
  ObTableType table_type_;
  ObSimpleTableSchema()
      : tenant_id_(common::OB_INVALID_ID),
        database_id_(common::OB_INVALID_ID),
        tablegroup_id_(common::OB_INVALID_ID),
        table_id_(common::OB_INVALID_ID),
        data_table_id_(common::OB_INVALID_ID),
        schema_version_(common::OB_INVALID_VERSION),
        table_type_(MAX_TABLE_TYPE)
  {}
  void reset()
  {
    tenant_id_ = common::OB_INVALID_ID;
    database_id_ = common::OB_INVALID_ID;
    tablegroup_id_ = common::OB_INVALID_ID;
    table_id_ = common::OB_INVALID_ID;
    data_table_id_ = common::OB_INVALID_ID;
    table_name_.reset();
    schema_version_ = common::OB_INVALID_VERSION;
    table_type_ = MAX_TABLE_TYPE;
  }
  TO_STRING_KV(K_(tenant_id), K_(database_id), K_(tablegroup_id), K_(table_id), K_(data_table_id), K_(table_name),
      K_(schema_version), K_(table_type));
  bool is_valid() const
  {
    return (common::OB_INVALID_ID != tenant_id_ && common::OB_INVALID_ID != database_id_ &&
            common::OB_INVALID_ID != tablegroup_id_ && common::OB_INVALID_ID != table_id_ &&
            common::OB_INVALID_ID != data_table_id_ && !table_name_.empty() && schema_version_ >= 0 &&
            table_type_ != MAX_TABLE_TYPE);
  }
};

enum TableStatus {
  TABLE_STATUS_INVALID = -1,
  // The current schema version does not exist, it is a future table
  TABLE_NOT_CREATE,
  TABLE_EXIST,    // table exist
  TABLE_DELETED,  // table is deleted
};

enum TenantStatus {
  TENANT_STATUS_INVALID = -1,
  TENANT_NOT_CREATE,
  TENANT_EXIST,
  TENANT_DELETED,
};

X
xy0 已提交
709
inline const char *print_table_status(const TableStatus status_no)
O
oceanbase-admin 已提交
710
{
X
xy0 已提交
711
  const char *status_str = "UNKNOWN";
O
oceanbase-admin 已提交
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
  switch (status_no) {
    case TABLE_NOT_CREATE:
      status_str = "TABLE_NOT_CREATE";
      break;
    case TABLE_EXIST:
      status_str = "TABLE_EXIST";
      break;
    case TABLE_DELETED:
      status_str = "TABLE_DELETED";
      break;
    default:
      status_str = "UNKNOWN";
      break;
  }
  return status_str;
}

enum PartitionStatus {
  PART_STATUS_INVALID = -1,
  // The current schema version has not been created, it is a future partition
  PART_NOT_CREATE,
  PART_EXIST,    // schema exist
  PART_DELETED,  // partition is deleted
};

X
xy0 已提交
737
inline const char *print_part_status(const PartitionStatus status)
O
oceanbase-admin 已提交
738
{
X
xy0 已提交
739
  const char *str = "UNKNOWN";
O
oceanbase-admin 已提交
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
  switch (status) {
    case PART_STATUS_INVALID:
      str = "PART_STATUS_INVALID";
      break;
    case PART_NOT_CREATE:
      str = "PART_NOT_CREATE";
      break;
    case PART_EXIST:
      str = "PART_EXIST";
      break;
    case PART_DELETED:
      str = "PART_DELETED";
      break;
    default:
      str = "UNKNOWN";
      break;
  }
  return str;
}

enum ObDependencyTableType {
  DEPENDENCY_INVALID = 0,
  DEPENDENCY_TABLE = 1,
  DEPENDENCY_VIEW = 2,
  DEPENDENCY_SYNONYM = 3,
  DEPENDENCY_PROCEDURE = 4,
  DEPENDENCY_OUTLINE = 5,
  DEPENDENCY_FUNCTION = 6,
  DEPENDENCY_PACKAGE = 7,
  DEPENDENCY_PACKAGE_BODY = 8,
  DEPENDENCY_SEQUENCE = 9,
  DEPENDENCY_TYPE = 10,
  DEPENDENCY_TABLESPACE = 15,
  DEPENDENCY_UDT = 16,
  DEPENDENCY_UDT_BODY = 17
};

enum class ObObjectType {
  INVALID = 0,
  TABLE,
  SEQUENCE,
  PACKAGE,
  TYPE,
  PACKAGE_BODY,
  TYPE_BODY,
  TRIGGER,
  VIEW,
  FUNCTION,
  DIRECTORY,
  INDEX,
  PROCEDURE,
  SYNONYM,
  MAX_TYPE,
};
struct ObSchemaObjVersion {
  ObSchemaObjVersion()
      : object_id_(common::OB_INVALID_ID),
        version_(0),
        // The default is table, which is compatible with the current logic
        object_type_(DEPENDENCY_TABLE),
        is_db_explicit_(false),
        is_existed_(true)
  {}

  ObSchemaObjVersion(int64_t object_id, int64_t version, ObDependencyTableType object_type)
      : object_id_(object_id), version_(version), object_type_(object_type), is_db_explicit_(false), is_existed_(true)
  {}

  inline void reset()
  {
    object_id_ = common::OB_INVALID_ID;
    version_ = 0;
    // The default is table, which is compatible with the current logic
    object_type_ = DEPENDENCY_TABLE;
    is_db_explicit_ = false;
    is_existed_ = true;
  }
  inline int64_t get_object_id() const
  {
    return object_id_;
  }
  inline int64_t get_version() const
  {
    return version_;
  }
  inline ObDependencyTableType get_type() const
  {
    return object_type_;
  }
X
xy0 已提交
829
  inline bool operator==(const ObSchemaObjVersion &other) const
O
oceanbase-admin 已提交
830 831 832 833
  {
    return object_id_ == other.object_id_ && version_ == other.version_ && object_type_ == other.object_type_ &&
           is_db_explicit_ == other.is_db_explicit_ && is_existed_ == other.is_existed_;
  }
X
xy0 已提交
834
  inline bool operator!=(const ObSchemaObjVersion &other) const
O
oceanbase-admin 已提交
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
  {
    return !operator==(other);
  }
  ObSchemaType get_schema_type() const
  {
    ObSchemaType ret_type = OB_MAX_SCHEMA;
    switch (object_type_) {
      case DEPENDENCY_TABLE:
      case DEPENDENCY_VIEW:
        ret_type = TABLE_SCHEMA;
        break;
      case DEPENDENCY_SYNONYM:
        ret_type = SYNONYM_SCHEMA;
        break;
      case DEPENDENCY_PROCEDURE:
      case DEPENDENCY_FUNCTION:
        ret_type = ROUTINE_SCHEMA;
        break;
      case DEPENDENCY_PACKAGE:
      case DEPENDENCY_PACKAGE_BODY:
        ret_type = PACKAGE_SCHEMA;
        break;
      case DEPENDENCY_OUTLINE:
        ret_type = OUTLINE_SCHEMA;
        break;
      case DEPENDENCY_SEQUENCE:
        ret_type = SEQUENCE_SCHEMA;
        break;
      case DEPENDENCY_TABLESPACE:
        ret_type = TABLESPACE_SCHEMA;
        break;
      case DEPENDENCY_TYPE:
        ret_type = UDT_SCHEMA;
        break;
      default:
        break;
    }
    return ret_type;
  }
  ObObjectType get_schema_object_type() const
  {
    ObObjectType ret_type = ObObjectType::MAX_TYPE;
    switch (object_type_) {
      case DEPENDENCY_TABLE:
        ret_type = ObObjectType::TABLE;
        break;
      case DEPENDENCY_VIEW:
        ret_type = ObObjectType::VIEW;
        break;
      case DEPENDENCY_PROCEDURE:
        ret_type = ObObjectType::PROCEDURE;
        break;
      case DEPENDENCY_FUNCTION:
        ret_type = ObObjectType::FUNCTION;
        break;
      case DEPENDENCY_PACKAGE:
        ret_type = ObObjectType::PACKAGE;
        break;
      case DEPENDENCY_PACKAGE_BODY:
        ret_type = ObObjectType::PACKAGE_BODY;
        break;
      case DEPENDENCY_SEQUENCE:
        ret_type = ObObjectType::SEQUENCE;
        break;
      case DEPENDENCY_TYPE:
        ret_type = ObObjectType::TYPE;
        break;
      default:
        break;
    }
    return ret_type;
  }
  inline bool is_valid() const
  {
    return common::OB_INVALID_ID != object_id_;
  }
  inline bool is_base_table() const
  {
    return DEPENDENCY_TABLE == object_type_;
  }
  inline bool is_synonym() const
  {
    return DEPENDENCY_SYNONYM == object_type_;
  }
  inline bool is_procedure() const
  {
    return DEPENDENCY_PROCEDURE == object_type_;
  }
  inline bool is_db_explicit() const
  {
    return is_db_explicit_;
  }
  inline bool is_existed() const
  {
    return is_existed_;
  }

  int64_t object_id_;
  int64_t version_;
  ObDependencyTableType object_type_;
  bool is_db_explicit_;
  bool is_existed_;

  TO_STRING_KV(N_TID, object_id_, N_SCHEMA_VERSION, version_, K_(object_type), K_(is_db_explicit), K_(is_existed));
  OB_UNIS_VERSION(1);
};

struct ObSysParam {
  ObSysParam();
  ~ObSysParam();

X
xy0 已提交
946 947 948
  int init(const uint64_t tenant_id, const common::ObZone &zone, const common::ObString &name, const int64_t data_type,
      const common::ObString &value, const common::ObString &min_val, const common::ObString &max_val,
      const common::ObString &info, const int64_t flags);
O
oceanbase-admin 已提交
949 950
  void reset();
  inline bool is_valid() const;
X
xy0 已提交
951
  int64_t to_string(char *buf, const int64_t buf_len) const;
O
oceanbase-admin 已提交
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973

  uint64_t tenant_id_;
  common::ObZone zone_;
  char name_[common::OB_MAX_SYS_PARAM_NAME_LENGTH];
  int64_t data_type_;
  char value_[common::OB_MAX_SYS_PARAM_VALUE_LENGTH];
  char min_val_[common::OB_MAX_SYS_PARAM_VALUE_LENGTH];
  char max_val_[common::OB_MAX_SYS_PARAM_VALUE_LENGTH];
  char info_[common::OB_MAX_SYS_PARAM_INFO_LENGTH];
  int64_t flags_;
};

bool ObSysParam::is_valid() const
{
  return common::OB_INVALID_ID != tenant_id_;
}
typedef common::ObFixedBitSet<common::OB_MAX_USER_DEFINED_COLUMNS_COUNT> ColumnReferenceSet;

// this is used for schema, and zone in this is a ObString,
// Caution: zone_ here doesn't have buffer, users should manange
//          the buffer memory all by themselves.
struct ObZoneScore {
X
xy0 已提交
974
  ObZoneScore(common::ObString &zone, int64_t score) : zone_(zone), score_(score)
O
oceanbase-admin 已提交
975 976 977 978 979
  {}
  ObZoneScore() : zone_(), score_(INT64_MAX)
  {}
  virtual ~ObZoneScore()
  {}
X
xy0 已提交
980
  bool operator<(const ObZoneScore &that)
O
oceanbase-admin 已提交
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
  {
    return score_ < that.score_;
  }
  void reset()
  {
    zone_.reset();
    score_ = INT64_MAX;
  }
  TO_STRING_KV(K(zone_), K(score_));

  common::ObString zone_;
  int64_t score_;
};
// ObZoneRegion is used to construct the primary zone array of TableSchema/DataBaseSchema/TenantSchema.
// It is only an intermediate variable during the construction process. ObZoneRegion will not be saved
// in the schema eventually. This structure saves the zone and the region in which the zone is located.
struct ObZoneRegion {
  ObZoneRegion() : zone_(), region_()
  {}
X
xy0 已提交
1000
  ObZoneRegion(const ObZoneRegion &that) : zone_(that.zone_), region_(that.region_)
O
oceanbase-admin 已提交
1001
  {}
X
xy0 已提交
1002
  ObZoneRegion(const common::ObZone &zone, const common::ObRegion &region) : zone_(zone), region_(region)
O
oceanbase-admin 已提交
1003 1004 1005 1006 1007 1008 1009 1010
  {}
  virtual ~ObZoneRegion()
  {}
  void reset()
  {
    zone_.reset();
    region_.reset();
  }
X
xy0 已提交
1011
  int assign(const ObZoneRegion &that)
O
oceanbase-admin 已提交
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
  {
    int ret = common::OB_SUCCESS;
    if (OB_FAIL(zone_.assign(that.zone_.ptr()))) {
      SHARE_LOG(WARN, "fail to assign zone", K(ret));
    } else if (OB_FAIL(region_.assign(that.region_.ptr()))) {
      SHARE_LOG(WARN, "fail to assign region", K(ret));
    } else {
    }
    return ret;
  }
  TO_STRING_KV(K(zone_), K(region_));

  common::ObZone zone_;
  common::ObRegion region_;
};

typedef common::ObIArray<share::ObZoneReplicaAttrSet> ZoneLocalityIArray;
typedef common::ObArrayHelper<share::SchemaZoneReplicaAttrSet> ZoneLocalityArray;

class ObCompareNameWithTenantID {
G
gm 已提交
1032
public:
O
oceanbase-admin 已提交
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
  ObCompareNameWithTenantID()
      : tenant_id_(common::OB_INVALID_ID),
        name_case_mode_(common::OB_NAME_CASE_INVALID),
        database_id_(common::OB_INVALID_ID)
  {}
  ObCompareNameWithTenantID(uint64_t tenant_id)
      : tenant_id_(tenant_id), name_case_mode_(common::OB_NAME_CASE_INVALID), database_id_(common::OB_INVALID_ID)
  {}
  ObCompareNameWithTenantID(uint64_t tenant_id, common::ObNameCaseMode mode)
      : tenant_id_(tenant_id), name_case_mode_(mode), database_id_(common::OB_INVALID_ID)
  {}
  ObCompareNameWithTenantID(uint64_t tenant_id, common::ObNameCaseMode mode, uint64_t database_id)
      : tenant_id_(tenant_id), name_case_mode_(mode), database_id_(database_id)
  {}
  ~ObCompareNameWithTenantID()
  {}
X
xy0 已提交
1049
  int compare(const common::ObString &str1, const common::ObString &str2);
O
oceanbase-admin 已提交
1050

G
gm 已提交
1051
private:
O
oceanbase-admin 已提交
1052 1053 1054 1055 1056 1057
  uint64_t tenant_id_;
  common::ObNameCaseMode name_case_mode_;
  uint64_t database_id_;
};

class ObSchema {
G
gm 已提交
1058
public:
O
oceanbase-admin 已提交
1059 1060 1061 1062
  friend class ObLocality;
  friend class ObPrimaryZone;
  ObSchema();
  // explicit ObSchema(common::ObDataBuffer &buffer);
X
xy0 已提交
1063
  explicit ObSchema(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
1064 1065 1066 1067 1068 1069 1070
  virtual ~ObSchema();
  virtual void reset();
  virtual bool is_valid() const
  {
    return common::OB_SUCCESS == error_ret_;
  }
  virtual int zone_array2str(
X
xy0 已提交
1071
      const common::ObIArray<common::ObZone> &zone_list, char *str, const int64_t buf_size) const;
O
oceanbase-admin 已提交
1072
  virtual int string_array2str(
X
xy0 已提交
1073 1074
      const common::ObIArray<common::ObString> &string_array, char *buf, const int64_t buf_size) const;
  virtual int str2string_array(const char *str, common::ObIArray<common::ObString> &string_array) const;
O
oceanbase-admin 已提交
1075 1076 1077 1078 1079 1080 1081

  virtual int64_t get_convert_size() const
  {
    return 0;
  };
  template <typename DSTSCHEMA>
  static int set_charset_and_collation_options(
X
xy0 已提交
1082
      common::ObCharsetType src_charset_type, common::ObCollationType src_collation_type, DSTSCHEMA &dst);
O
oceanbase-admin 已提交
1083 1084
  static common::ObCollationType get_cs_type_with_cmp_mode(const common::ObNameCaseMode mode);

X
xy0 已提交
1085
  ObSchema *get_buffer() const
O
oceanbase-admin 已提交
1086 1087 1088
  {
    return buffer_;
  }
X
xy0 已提交
1089
  void set_buffer(ObSchema *buffer)
O
oceanbase-admin 已提交
1090 1091 1092
  {
    buffer_ = buffer;
  }
X
xy0 已提交
1093
  common::ObIAllocator *get_allocator();
O
oceanbase-admin 已提交
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
  void reset_allocator();
  int get_assign_ret()
  {
    return error_ret_;
  }
  inline int get_err_ret() const
  {
    return error_ret_;
  }

G
gm 已提交
1104
protected:
O
oceanbase-admin 已提交
1105
  static const int64_t STRING_ARRAY_EXTEND_CNT = 7;
X
xy0 已提交
1106 1107 1108 1109 1110
  void *alloc(const int64_t size);
  void free(void *ptr);
  int deep_copy_str(const char *src, common::ObString &dest);
  int deep_copy_str(const common::ObString &src, common::ObString &dest);
  int deep_copy_obj(const common::ObObj &src, common::ObObj &dest);
O
oceanbase-admin 已提交
1111
  int deep_copy_string_array(
X
xy0 已提交
1112 1113
      const common::ObIArray<common::ObString> &src, common::ObArrayHelper<common::ObString> &dst);
  int add_string_to_array(const common::ObString &str, common::ObArrayHelper<common::ObString> &str_array);
O
oceanbase-admin 已提交
1114
  int serialize_string_array(
X
xy0 已提交
1115
      char *buf, const int64_t buf_len, int64_t &pos, const common::ObArrayHelper<common::ObString> &str_array) const;
O
oceanbase-admin 已提交
1116
  int deserialize_string_array(
X
xy0 已提交
1117 1118 1119 1120 1121
      const char *buf, const int64_t data_len, int64_t &pos, common::ObArrayHelper<common::ObString> &str_array);
  int64_t get_string_array_serialize_size(const common::ObArrayHelper<common::ObString> &str_array) const;
  void reset_string(common::ObString &str);
  void reset_string_array(common::ObArrayHelper<common::ObString> &str_array);
  const char *extract_str(const common::ObString &str) const;
O
oceanbase-admin 已提交
1122 1123 1124 1125
  // buffer is the memory used to store schema item, if not same with this pointer,
  // it means that this schema item has already been rewrote to buffer when rewriting
  // other schema manager, and when we want to rewrite this schema item in current schema
  // manager, we just set pointer of schema item in schema manager to buffer
X
xy0 已提交
1126
  ObSchema *buffer_;
O
oceanbase-admin 已提交
1127 1128
  int error_ret_;
  bool is_inner_allocator_;
X
xy0 已提交
1129
  common::ObIAllocator *allocator_;
O
oceanbase-admin 已提交
1130 1131 1132 1133
};

template <typename DSTSCHEMA>
int ObSchema::set_charset_and_collation_options(
X
xy0 已提交
1134
    common::ObCharsetType src_charset_type, common::ObCollationType src_collation_type, DSTSCHEMA &dst)
O
oceanbase-admin 已提交
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
{
  int ret = common::OB_SUCCESS;
  if (dst.get_charset_type() == common::CHARSET_INVALID && dst.get_collation_type() == common::CS_TYPE_INVALID) {
    // use upper layer schema's charset and collation type
    if (src_charset_type == common::CHARSET_INVALID || src_collation_type == common::CS_TYPE_INVALID) {
      ret = common::OB_ERR_UNEXPECTED;
      SHARE_SCHEMA_LOG(WARN, "charset type or collation type is invalid ", K(ret));
    } else {
      dst.set_charset_type(src_charset_type);
      dst.set_collation_type(src_collation_type);
    }
  } else {
    common::ObCharsetType charset_type = dst.get_charset_type();
    common::ObCollationType collation_type = dst.get_collation_type();
    if (OB_FAIL(common::ObCharset::check_and_fill_info(charset_type, collation_type))) {
      SHARE_SCHEMA_LOG(WARN, "fail to check charset collation", K(charset_type), K(collation_type), K(ret));
    } else {
      dst.set_charset_type(charset_type);
      dst.set_collation_type(collation_type);
    }
  }
  if (common::OB_SUCCESS == ret &&
      !common::ObCharset::is_valid_collation(dst.get_charset_type(), dst.get_collation_type())) {
    ret = common::OB_ERR_UNEXPECTED;
    SHARE_SCHEMA_LOG(WARN, "invalid collation!", K(dst.get_charset_type()), K(dst.get_collation_type()), K(ret));
  }
  return ret;
}

class ObLocality {
  OB_UNIS_VERSION(1);

G
gm 已提交
1167
public:
X
xy0 已提交
1168
  explicit ObLocality(ObSchema *schema) : schema_(schema)
O
oceanbase-admin 已提交
1169
  {}
X
xy0 已提交
1170 1171 1172 1173
  int assign(const ObLocality &other);
  int set_locality_str(const common::ObString &locality);
  int set_zone_replica_attr_array(const common::ObIArray<share::ObZoneReplicaAttrSet> &src);
  int set_zone_replica_attr_array(const common::ObIArray<share::SchemaZoneReplicaAttrSet> &src);
O
oceanbase-admin 已提交
1174
  int set_specific_replica_attr_array(
X
xy0 已提交
1175
      share::SchemaReplicaAttrArray &schema_replica_set, const common::ObIArray<ReplicaAttr> &src);
O
oceanbase-admin 已提交
1176 1177
  void reset_zone_replica_attr_array();
  int64_t get_convert_size() const;
X
xy0 已提交
1178
  inline const common::ObString &get_locality_str() const
O
oceanbase-admin 已提交
1179 1180 1181 1182 1183 1184
  {
    return locality_str_;
  }
  void reset();
  TO_STRING_KV(K_(locality_str), K_(zone_replica_attr_array));

G
gm 已提交
1185
public:
O
oceanbase-admin 已提交
1186 1187
  common::ObString locality_str_;
  ZoneLocalityArray zone_replica_attr_array_;
X
xy0 已提交
1188
  ObSchema *schema_;
O
oceanbase-admin 已提交
1189 1190 1191 1192 1193
};

class ObPrimaryZone {
  OB_UNIS_VERSION(1);

G
gm 已提交
1194
public:
X
xy0 已提交
1195
  explicit ObPrimaryZone(ObSchema *schema) : schema_(schema)
O
oceanbase-admin 已提交
1196
  {}
X
xy0 已提交
1197 1198
  int assign(const ObPrimaryZone &other);
  inline const common::ObString &get_primary_zone() const
O
oceanbase-admin 已提交
1199 1200 1201
  {
    return primary_zone_str_;
  }
X
xy0 已提交
1202
  inline const common::ObIArray<ObZoneScore> &get_primary_zone_array() const
O
oceanbase-admin 已提交
1203 1204 1205
  {
    return primary_zone_array_;
  }
X
xy0 已提交
1206 1207
  int set_primary_zone_array(const common::ObIArray<ObZoneScore> &primary_zone_array);
  int set_primary_zone(const common::ObString &zone);
O
oceanbase-admin 已提交
1208 1209 1210 1211
  int64_t get_convert_size() const;
  void reset();
  TO_STRING_KV(K_(primary_zone_str), K_(primary_zone_array));

G
gm 已提交
1212
public:
O
oceanbase-admin 已提交
1213 1214
  common::ObString primary_zone_str_;
  common::ObSEArray<ObZoneScore, common::MAX_ZONE_NUM, common::ObNullAllocator> primary_zone_array_;
X
xy0 已提交
1215
  ObSchema *schema_;
O
oceanbase-admin 已提交
1216 1217 1218 1219 1220
};

class ObSysVarSchema : public ObSchema {
  OB_UNIS_VERSION(1);

G
gm 已提交
1221
public:
O
oceanbase-admin 已提交
1222 1223 1224 1225 1226 1227
  ObSysVarSchema() : ObSchema()
  {
    reset();
  }
  ~ObSysVarSchema()
  {}
X
xy0 已提交
1228 1229 1230 1231
  explicit ObSysVarSchema(common::ObIAllocator *allocator);
  explicit ObSysVarSchema(const ObSysVarSchema &src_schema);
  ObSysVarSchema &operator=(const ObSysVarSchema &src_schema);
  int assign(const ObSysVarSchema &src_schema);
O
oceanbase-admin 已提交
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
  virtual bool is_valid() const
  {
    return ObSchema::is_valid() && tenant_id_ != common::OB_INVALID_ID && !name_.empty();
  }
  void reset();
  int64_t get_convert_size() const;
  uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
  void set_tenant_id(uint64_t tenant_id)
  {
    tenant_id_ = tenant_id;
  }
X
xy0 已提交
1246
  const common::ObString &get_name() const
O
oceanbase-admin 已提交
1247 1248 1249
  {
    return name_;
  }
X
xy0 已提交
1250
  int set_name(const common::ObString &name)
O
oceanbase-admin 已提交
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
  {
    return deep_copy_str(name, name_);
  }
  common::ObObjType get_data_type() const
  {
    return data_type_;
  }
  void set_data_type(common::ObObjType data_type)
  {
    data_type_ = data_type;
  }
X
xy0 已提交
1262
  const common::ObString &get_value() const
O
oceanbase-admin 已提交
1263 1264 1265 1266
  {
    return value_;
  }
  int get_value(
X
xy0 已提交
1267
      common::ObIAllocator *allocator, const common::ObDataTypeCastParams &dtc_params, common::ObObj &value) const;
O
oceanbase-admin 已提交
1268
  inline int get_value(
X
xy0 已提交
1269
      common::ObIAllocator *allocator, const common::ObTimeZoneInfo *tz_info, common::ObObj &value) const
O
oceanbase-admin 已提交
1270 1271 1272 1273
  {
    const common::ObDataTypeCastParams dtc_params(tz_info);
    return get_value(allocator, dtc_params, value);
  }
X
xy0 已提交
1274
  int set_value(const common::ObString &value)
O
oceanbase-admin 已提交
1275 1276 1277
  {
    return deep_copy_str(value, value_);
  }
X
xy0 已提交
1278
  const common::ObString &get_min_val() const
O
oceanbase-admin 已提交
1279 1280 1281
  {
    return min_val_;
  }
X
xy0 已提交
1282
  int set_min_val(const common::ObString &min_val)
O
oceanbase-admin 已提交
1283 1284 1285
  {
    return deep_copy_str(min_val, min_val_);
  }
X
xy0 已提交
1286
  const common::ObString &get_max_val() const
O
oceanbase-admin 已提交
1287 1288 1289
  {
    return max_val_;
  }
X
xy0 已提交
1290
  int set_max_val(const common::ObString &max_val)
O
oceanbase-admin 已提交
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
  {
    return deep_copy_str(max_val, max_val_);
  }
  int64_t get_schema_version() const
  {
    return schema_version_;
  }
  void set_schema_version(int64_t schema_version)
  {
    schema_version_ = schema_version;
  }
  int64_t get_flags() const
  {
    return flags_;
  }
  void set_flags(int64_t flags)
  {
    flags_ = flags;
  }
X
xy0 已提交
1310
  int set_info(const common::ObString &info)
O
oceanbase-admin 已提交
1311 1312 1313
  {
    return deep_copy_str(info, info_);
  }
X
xy0 已提交
1314
  const common::ObString &get_info() const
O
oceanbase-admin 已提交
1315 1316 1317
  {
    return info_;
  }
X
xy0 已提交
1318
  int set_zone(const common::ObZone &zone)
O
oceanbase-admin 已提交
1319 1320 1321
  {
    return zone_.assign(zone);
  }
X
xy0 已提交
1322
  int set_zone(const common::ObString &zone)
O
oceanbase-admin 已提交
1323 1324 1325
  {
    return zone_.assign(zone);
  }
X
xy0 已提交
1326
  const common::ObZone &get_zone() const
O
oceanbase-admin 已提交
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
  {
    return zone_;
  }
  bool is_invisible() const
  {
    return 0 != (flags_ & ObSysVarFlag::INVISIBLE);
  }
  bool is_global() const
  {
    return 0 != (flags_ & ObSysVarFlag::GLOBAL_SCOPE);
  }
  bool is_query_sensitive() const
  {
    return 0 != (flags_ & ObSysVarFlag::QUERY_SENSITIVE);
  }
  bool is_oracle_only() const
  {
    return 0 != (flags_ & ObSysVarFlag::ORACLE_ONLY);
  }
  bool is_mysql_only() const
  {
    return 0 != (flags_ & ObSysVarFlag::MYSQL_ONLY);
  }
  bool is_read_only() const
  {
    return 0 != (flags_ & ObSysVarFlag::READONLY);
  }
  TO_STRING_KV(K_(tenant_id), K_(name), K_(data_type), K_(value), K_(min_val), K_(max_val), K_(info), K_(zone),
      K_(schema_version), K_(flags));

G
gm 已提交
1357
private:
O
oceanbase-admin 已提交
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
  uint64_t tenant_id_;
  common::ObString name_;
  common::ObObjType data_type_;
  common::ObString value_;
  common::ObString min_val_;
  common::ObString max_val_;
  common::ObString info_;
  common::ObZone zone_;
  int64_t schema_version_;
  int64_t flags_;
};

class ObSysVariableSchema : public ObSchema {
  OB_UNIS_VERSION(1);

G
gm 已提交
1373
public:
O
oceanbase-admin 已提交
1374 1375
  // base methods
  ObSysVariableSchema();
X
xy0 已提交
1376
  explicit ObSysVariableSchema(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
1377
  virtual ~ObSysVariableSchema();
X
xy0 已提交
1378 1379
  ObSysVariableSchema(const ObSysVariableSchema &src_schema);
  ObSysVariableSchema &operator=(const ObSysVariableSchema &src_schema);
O
oceanbase-admin 已提交
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
  // set methods
  inline void set_tenant_id(const uint64_t tenant_id)
  {
    tenant_id_ = tenant_id;
  }
  inline void set_schema_version(const int64_t schema_version)
  {
    schema_version_ = schema_version;
  }
  // get methods
  inline uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
  inline int64_t get_schema_version() const
  {
    return schema_version_;
  }
  // other methods
  virtual bool is_valid() const;
  virtual void reset();
  void reset_sysvars()
  {
    memset(sysvar_array_, 0, sizeof(sysvar_array_));
  }
  int64_t get_convert_size() const;
X
xy0 已提交
1406
  int add_sysvar_schema(const share::schema::ObSysVarSchema &sysvar_schema);
O
oceanbase-admin 已提交
1407 1408 1409 1410 1411 1412
  int load_default_system_variable(bool is_sys_tenant);
  int64_t get_sysvar_count() const
  {
    return ObSysVarFactory::ALL_SYS_VARS_COUNT;
  }
  int64_t get_real_sysvar_count() const;
X
xy0 已提交
1413 1414 1415 1416
  int get_sysvar_schema(const common::ObString &sysvar_name, const ObSysVarSchema *&sysvar_schema) const;
  int get_sysvar_schema(ObSysVarClassType var_type, const ObSysVarSchema *&sysvar_schema) const;
  const ObSysVarSchema *get_sysvar_schema(int64_t idx) const;
  ObSysVarSchema *get_sysvar_schema(int64_t idx);
O
oceanbase-admin 已提交
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
  bool is_read_only() const
  {
    return read_only_;
  }
  common::ObNameCaseMode get_name_case_mode() const
  {
    return name_case_mode_;
  }
  void set_name_case_mode(const common::ObNameCaseMode mode)
  {
    name_case_mode_ = mode;
  }
X
xy0 已提交
1429
  int get_oracle_mode(bool &is_oracle_mode) const;
O
oceanbase-admin 已提交
1430
  TO_STRING_KV(K_(tenant_id), K_(schema_version), "sysvars",
X
xy0 已提交
1431
      common::ObArrayWrap<ObSysVarSchema *>(sysvar_array_, ObSysVarFactory::ALL_SYS_VARS_COUNT), K_(read_only),
O
oceanbase-admin 已提交
1432 1433
      K_(name_case_mode));

G
gm 已提交
1434
private:
O
oceanbase-admin 已提交
1435 1436
  uint64_t tenant_id_;
  int64_t schema_version_;
X
xy0 已提交
1437
  ObSysVarSchema *sysvar_array_[ObSysVarFactory::ALL_SYS_VARS_COUNT];
O
oceanbase-admin 已提交
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
  bool read_only_;
  common::ObNameCaseMode name_case_mode_;
};

enum ObTenantStatus {
  TENANT_STATUS_NORMAL = 0,
  TENANT_STATUS_CREATING = 1,
  TENANT_STATUS_DROPPING = 2,
  TENANT_STATUS_RESTORE = 3,
  TENANT_STATUS_MAX
};

X
xy0 已提交
1450
const char *ob_tenant_status_str(const ObTenantStatus);
O
oceanbase-admin 已提交
1451

X
xy0 已提交
1452
int get_tenant_status(const common::ObString &str, ObTenantStatus &status);
O
oceanbase-admin 已提交
1453 1454 1455 1456

class ObTenantSchema : public ObSchema {
  OB_UNIS_VERSION(1);

G
gm 已提交
1457
public:
O
oceanbase-admin 已提交
1458 1459
  // base methods
  ObTenantSchema();
X
xy0 已提交
1460
  explicit ObTenantSchema(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
1461
  virtual ~ObTenantSchema();
X
xy0 已提交
1462 1463 1464
  ObTenantSchema(const ObTenantSchema &src_schema);
  ObTenantSchema &operator=(const ObTenantSchema &src_schema);
  int assign(const ObTenantSchema &src_schema);
O
oceanbase-admin 已提交
1465
  // for sorted vector
X
xy0 已提交
1466
  static bool cmp(const ObTenantSchema *lhs, const ObTenantSchema *rhs)
O
oceanbase-admin 已提交
1467 1468 1469
  {
    return (NULL != lhs && NULL != rhs) ? lhs->get_tenant_id() < rhs->get_tenant_id() : false;
  }
X
xy0 已提交
1470
  static bool equal(const ObTenantSchema *lhs, const ObTenantSchema *rhs)
O
oceanbase-admin 已提交
1471 1472 1473
  {
    return (NULL != lhs && NULL != rhs) ? lhs->get_tenant_id() == rhs->get_tenant_id() : false;
  }
X
xy0 已提交
1474
  static bool cmp_tenant_id(const ObTenantSchema *lhs, const uint64_t tenant_id)
O
oceanbase-admin 已提交
1475 1476 1477
  {
    return NULL != lhs ? lhs->get_tenant_id() < tenant_id : false;
  }
X
xy0 已提交
1478
  static bool equal_tenant_id(const ObTenantSchema *lhs, const uint64_t tenant_id)
O
oceanbase-admin 已提交
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
  {
    return NULL != lhs ? lhs->get_tenant_id() == tenant_id : false;
  }
  // set methods
  inline void set_tenant_id(const uint64_t tenant_id)
  {
    tenant_id_ = tenant_id;
  }
  inline void set_schema_version(const int64_t schema_version)
  {
    schema_version_ = schema_version;
  }
X
xy0 已提交
1491
  inline int set_tenant_name(const char *tenant_name)
O
oceanbase-admin 已提交
1492 1493 1494
  {
    return deep_copy_str(tenant_name, tenant_name_);
  }
X
xy0 已提交
1495
  inline int set_comment(const char *comment)
O
oceanbase-admin 已提交
1496 1497 1498
  {
    return deep_copy_str(comment, comment_);
  }
X
xy0 已提交
1499
  inline int set_locality(const char *locality)
O
oceanbase-admin 已提交
1500 1501 1502
  {
    return deep_copy_str(locality, locality_str_);
  }
X
xy0 已提交
1503
  inline int set_previous_locality(const char *previous_locality)
O
oceanbase-admin 已提交
1504 1505 1506
  {
    return deep_copy_str(previous_locality, previous_locality_str_);
  }
X
xy0 已提交
1507
  inline int set_tenant_name(const common::ObString &tenant_name)
O
oceanbase-admin 已提交
1508 1509 1510
  {
    return deep_copy_str(tenant_name, tenant_name_);
  }
X
xy0 已提交
1511 1512 1513 1514 1515
  inline int set_zone_list(const common::ObIArray<common::ObString> &zone_list);
  int set_zone_list(const common::ObIArray<common::ObZone> &zone_list);
  inline int set_primary_zone(const common::ObString &zone);
  int set_primary_zone_array(const common::ObIArray<ObZoneScore> &primary_zone_array);
  inline int add_zone(const common::ObString &zone);
O
oceanbase-admin 已提交
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
  inline void set_locked(const bool locked)
  {
    locked_ = locked;
  }
  inline void set_read_only(const bool read_only)
  {
    read_only_ = read_only;
  }
  inline void set_rewrite_merge_version(const int64_t version)
  {
    rewrite_merge_version_ = version;
  }
X
xy0 已提交
1528
  inline int set_comment(const common::ObString &comment)
O
oceanbase-admin 已提交
1529 1530 1531
  {
    return deep_copy_str(comment, comment_);
  }
X
xy0 已提交
1532
  inline int set_locality(const common::ObString &locality)
O
oceanbase-admin 已提交
1533 1534 1535
  {
    return deep_copy_str(locality, locality_str_);
  }
X
xy0 已提交
1536
  inline int set_previous_locality(const common::ObString &previous_locality)
O
oceanbase-admin 已提交
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
  {
    return deep_copy_str(previous_locality, previous_locality_str_);
  }
  inline void set_charset_type(const common::ObCharsetType type)
  {
    charset_type_ = type;
  }
  inline void set_collation_type(const common::ObCollationType type)
  {
    collation_type_ = type;
  }
  inline void set_name_case_mode(const common::ObNameCaseMode mode)
  {
    name_case_mode_ = mode;
  }
X
xy0 已提交
1552 1553
  int set_zone_replica_attr_array(const common::ObIArray<share::ObZoneReplicaAttrSet> &src);
  int set_zone_replica_attr_array(const common::ObIArray<share::SchemaZoneReplicaAttrSet> &src);
O
oceanbase-admin 已提交
1554
  int set_specific_replica_attr_array(
X
xy0 已提交
1555
      share::SchemaReplicaAttrArray &schema_replica_set, const common::ObIArray<ReplicaAttr> &src);
O
oceanbase-admin 已提交
1556 1557 1558 1559 1560 1561 1562
  void reset_zone_replica_attr_array();
  inline void set_storage_format_version(const int64_t storage_format_version);
  inline void set_storage_format_work_version(const int64_t storage_format_work_version);
  void set_default_tablegroup_id(const uint64_t tablegroup_id)
  {
    default_tablegroup_id_ = tablegroup_id;
  }
X
xy0 已提交
1563
  int set_default_tablegroup_name(const common::ObString &tablegroup_name)
O
oceanbase-admin 已提交
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580
  {
    return deep_copy_str(tablegroup_name, default_tablegroup_name_);
  }
  inline void set_compatibility_mode(const common::ObCompatibilityMode compatibility_mode)
  {
    compatibility_mode_ = compatibility_mode;
  }

  // get methods
  inline uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
  inline int64_t get_schema_version() const
  {
    return schema_version_;
  }
X
xy0 已提交
1581
  inline const char *get_tenant_name() const
O
oceanbase-admin 已提交
1582 1583 1584
  {
    return extract_str(tenant_name_);
  }
X
xy0 已提交
1585
  inline const char *get_comment() const
O
oceanbase-admin 已提交
1586 1587 1588
  {
    return extract_str(comment_);
  }
X
xy0 已提交
1589
  inline const char *get_locality() const
O
oceanbase-admin 已提交
1590 1591 1592
  {
    return extract_str(locality_str_);
  }
X
xy0 已提交
1593
  inline const char *get_previous_locality() const
O
oceanbase-admin 已提交
1594 1595 1596
  {
    return extract_str(previous_locality_str_);
  }
X
xy0 已提交
1597
  inline const common::ObString &get_tenant_name_str() const
O
oceanbase-admin 已提交
1598 1599 1600 1601 1602 1603 1604
  {
    return tenant_name_;
  }
  inline int64_t get_logonly_replica_num() const
  {
    return logonly_replica_num_;
  }
X
xy0 已提交
1605
  inline const common::ObString &get_primary_zone() const
O
oceanbase-admin 已提交
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
  {
    return primary_zone_;
  }
  inline bool get_locked() const
  {
    return locked_;
  }
  inline bool is_read_only() const
  {
    return read_only_;
  }
  inline int64_t get_rewrite_merge_version() const
  {
    return rewrite_merge_version_;
  }
X
xy0 已提交
1621
  inline const common::ObString &get_comment_str() const
O
oceanbase-admin 已提交
1622 1623 1624
  {
    return comment_;
  }
X
xy0 已提交
1625
  inline const common::ObString &get_locality_str() const
O
oceanbase-admin 已提交
1626 1627 1628
  {
    return locality_str_;
  }
X
xy0 已提交
1629
  inline const common::ObString &get_previous_locality_str() const
O
oceanbase-admin 已提交
1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
  {
    return previous_locality_str_;
  }
  inline common::ObCharsetType get_charset_type() const
  {
    return charset_type_;
  }
  inline common::ObCollationType get_collation_type() const
  {
    return collation_type_;
  }
  int get_raw_first_primary_zone(
X
xy0 已提交
1642 1643 1644 1645 1646
      const rootserver::ObRandomZoneSelector &random_selector, common::ObZone &first_primary_zone) const;
  int get_first_primary_zone(const rootserver::ObRandomZoneSelector &random_selector,
      const common::ObIArray<rootserver::ObReplicaAddr> &replica_addrs, common::ObZone &first_primary_zone) const;
  int get_primary_zone_score(const common::ObZone &zone, int64_t &zone_score) const;
  inline const common::ObIArray<ObZoneScore> &get_primary_zone_array() const
O
oceanbase-admin 已提交
1647 1648 1649 1650
  {
    return primary_zone_array_;
  }
  int get_primary_zone_inherit(
X
xy0 已提交
1651 1652 1653
      share::schema::ObSchemaGetterGuard &schema_guard, share::schema::ObPrimaryZone &primary_zone) const;
  int get_zone_replica_attr_array(ZoneLocalityIArray &locality) const;
  int get_zone_replica_attr_array_inherit(ObSchemaGetterGuard &schema_guard, ZoneLocalityIArray &locality) const;
O
oceanbase-admin 已提交
1654
  int64_t get_full_replica_num() const;
X
xy0 已提交
1655 1656
  int get_paxos_replica_num(share::schema::ObSchemaGetterGuard &schema_guard, int64_t &num) const;
  int get_zone_list(common::ObIArray<common::ObZone> &zone_list) const;
O
oceanbase-admin 已提交
1657
  int get_zone_list(
X
xy0 已提交
1658
      share::schema::ObSchemaGetterGuard &schema_guard, common::ObIArray<common::ObZone> &zone_list) const;
O
oceanbase-admin 已提交
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
  inline int64_t get_storage_format_version() const
  {
    return storage_format_version_;
  }
  inline int64_t get_storage_format_work_version() const
  {
    return storage_format_work_version_;
  }
  inline uint64_t get_default_tablegroup_id() const
  {
    return default_tablegroup_id_;
  }
X
xy0 已提交
1671
  inline const common::ObString &get_default_tablegroup_name() const
O
oceanbase-admin 已提交
1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
  {
    return default_tablegroup_name_;
  }
  inline common::ObCompatibilityMode get_compatibility_mode() const
  {
    return compatibility_mode_;
  }
  inline void set_drop_tenant_time(const int64_t drop_tenant_time)
  {
    drop_tenant_time_ = drop_tenant_time;
  }
  inline int64_t get_drop_tenant_time() const
  {
    return drop_tenant_time_;
  }
  inline bool is_dropping() const
  {
    return TENANT_STATUS_DROPPING == status_;
  }
  inline bool is_in_recyclebin() const
  {
    return in_recyclebin_;
  }
  inline void set_in_recyclebin(const bool in_recyclebin)
  {
    in_recyclebin_ = in_recyclebin;
  }
  inline bool is_creating() const
  {
    return TENANT_STATUS_CREATING == status_;
  }
  inline bool is_restore() const
  {
    return TENANT_STATUS_RESTORE == status_;
  }
  inline bool is_normal() const
  {
    return TENANT_STATUS_NORMAL == status_;
  }
  inline void set_status(const ObTenantStatus status)
  {
    status_ = status;
  }
  inline ObTenantStatus get_status() const
  {
    return status_;
  }
  // other methods
  virtual bool is_valid() const;
  virtual void reset();
  void reset_sysvars()
  {
    memset(sysvar_array_, 0, sizeof(sysvar_array_));
  }
  int64_t get_convert_size() const;
  TO_STRING_KV(K_(tenant_id), K_(schema_version), K_(tenant_name), K_(replica_num), K_(zone_list), K_(primary_zone),
      K_(charset_type), K_(locked), K_(comment), K_(name_case_mode), K_(read_only), K_(rewrite_merge_version),
      K_(locality_str), K_(logonly_replica_num), K_(zone_replica_attr_array), K_(primary_zone_array),
      K_(storage_format_version), K_(storage_format_work_version), K_(previous_locality_str), "sysvars",
X
xy0 已提交
1731
      common::ObArrayWrap<ObSysVarSchema *>(sysvar_array_, ObSysVarFactory::ALL_SYS_VARS_COUNT),
O
oceanbase-admin 已提交
1732 1733 1734
      K_(default_tablegroup_id), K_(default_tablegroup_name), K_(compatibility_mode), K_(drop_tenant_time), K_(status),
      K_(in_recyclebin));

G
gm 已提交
1735
private:
X
xy0 已提交
1736
  int add_sysvar_schema(const share::schema::ObSysVarSchema &sysvar_schema);
O
oceanbase-admin 已提交
1737 1738 1739 1740
  int64_t get_sysvar_count() const
  {
    return ObSysVarFactory::ALL_SYS_VARS_COUNT;
  }
X
xy0 已提交
1741
  const ObSysVarSchema *get_sysvar_schema(int64_t idx) const;
O
oceanbase-admin 已提交
1742

G
gm 已提交
1743
private:
O
oceanbase-admin 已提交
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
  uint64_t tenant_id_;
  int64_t schema_version_;
  common::ObString tenant_name_;
  int64_t replica_num_;
  common::ObArrayHelper<common::ObString> zone_list_;
  common::ObString primary_zone_;
  bool locked_;
  bool read_only_;  // After the schema is split, the value of the system variable shall prevail
  int64_t rewrite_merge_version_;
  common::ObCharsetType charset_type_;
  common::ObCollationType collation_type_;
  common::ObNameCaseMode name_case_mode_;  // deprecated
  common::ObString comment_;
  common::ObString locality_str_;
  int64_t logonly_replica_num_;
  ZoneLocalityArray zone_replica_attr_array_;
  // primary_zone_ is primary zone list,
  // The following is the parsed array of a single zone, which has been sorted according to priority
  common::ObSEArray<ObZoneScore, common::MAX_ZONE_NUM, common::ObNullAllocator> primary_zone_array_;
  common::ObString previous_locality_str_;
  int64_t storage_format_version_;
  int64_t storage_format_work_version_;
X
xy0 已提交
1766
  ObSysVarSchema *sysvar_array_[ObSysVarFactory::ALL_SYS_VARS_COUNT];  // deprecated
O
oceanbase-admin 已提交
1767 1768 1769 1770 1771 1772 1773 1774
  uint64_t default_tablegroup_id_;
  common::ObString default_tablegroup_name_;
  common::ObCompatibilityMode compatibility_mode_;
  int64_t drop_tenant_time_;
  ObTenantStatus status_;
  bool in_recyclebin_;
};

X
xy0 已提交
1775
inline int ObTenantSchema::set_zone_list(const common::ObIArray<common::ObString> &zone_list)
O
oceanbase-admin 已提交
1776 1777 1778 1779
{
  return deep_copy_string_array(zone_list, zone_list_);
}

X
xy0 已提交
1780
inline int ObTenantSchema::set_primary_zone(const common::ObString &zone)
O
oceanbase-admin 已提交
1781 1782 1783 1784
{
  return deep_copy_str(zone, primary_zone_);
}

X
xy0 已提交
1785
inline int ObTenantSchema::add_zone(const common::ObString &zone)
O
oceanbase-admin 已提交
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
{
  return add_string_to_array(zone, zone_list_);
}

inline void ObTenantSchema::set_storage_format_version(const int64_t storage_format_version)
{
  storage_format_version_ = storage_format_version;
}

inline void ObTenantSchema::set_storage_format_work_version(const int64_t storage_format_work_version)
{
  storage_format_work_version_ = storage_format_work_version;
}

class ObDatabaseSchema : public ObSchema {
  OB_UNIS_VERSION(1);

G
gm 已提交
1803
public:
O
oceanbase-admin 已提交
1804 1805
  // base methods
  ObDatabaseSchema();
X
xy0 已提交
1806
  explicit ObDatabaseSchema(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
1807
  virtual ~ObDatabaseSchema();
X
xy0 已提交
1808 1809 1810
  ObDatabaseSchema(const ObDatabaseSchema &src_schema);
  ObDatabaseSchema &operator=(const ObDatabaseSchema &src_schema);
  int assign(const ObDatabaseSchema &src_schema);
O
oceanbase-admin 已提交
1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823
  // set methods
  inline void set_tenant_id(const uint64_t tenant_id)
  {
    tenant_id_ = tenant_id;
  }
  inline void set_database_id(const uint64_t database_id)
  {
    database_id_ = database_id;
  }
  inline void set_schema_version(const int64_t schema_version)
  {
    schema_version_ = schema_version;
  }
X
xy0 已提交
1824
  int set_database_name(const char *database_name)
O
oceanbase-admin 已提交
1825 1826 1827
  {
    return deep_copy_str(database_name, database_name_);
  }
X
xy0 已提交
1828
  int set_database_name(const common::ObString &database_name)
O
oceanbase-admin 已提交
1829 1830 1831
  {
    return deep_copy_str(database_name, database_name_);
  }
X
xy0 已提交
1832 1833 1834 1835 1836 1837
  inline int set_zone_list(const common::ObIArray<common::ObString> &zone_list);
  int set_zone_list(const common::ObIArray<common::ObZone> &zone_list);
  inline int set_primary_zone(const common::ObString &zone);
  int set_primary_zone_array(const common::ObIArray<ObZoneScore> &primary_zone_array);
  inline int add_zone(const common::ObString &zone);
  int set_comment(const char *comment)
O
oceanbase-admin 已提交
1838 1839 1840
  {
    return deep_copy_str(comment, comment_);
  }
X
xy0 已提交
1841
  int set_comment(const common::ObString &comment)
O
oceanbase-admin 已提交
1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864
  {
    return deep_copy_str(comment, comment_);
  }
  inline void set_charset_type(const common::ObCharsetType type)
  {
    charset_type_ = type;
  }
  inline void set_collation_type(const common::ObCollationType type)
  {
    collation_type_ = type;
  }
  inline void set_name_case_mode(const common::ObNameCaseMode mode)
  {
    name_case_mode_ = mode;
  }
  inline void set_read_only(const bool read_only)
  {
    read_only_ = read_only;
  }
  void set_default_tablegroup_id(const uint64_t tablegroup_id)
  {
    default_tablegroup_id_ = tablegroup_id;
  }
X
xy0 已提交
1865
  int set_default_tablegroup_name(const common::ObString &tablegroup_name)
O
oceanbase-admin 已提交
1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891
  {
    return deep_copy_str(tablegroup_name, default_tablegroup_name_);
  }
  inline void set_in_recyclebin(const bool in_recyclebin)
  {
    in_recyclebin_ = in_recyclebin;
  }
  inline bool is_hidden() const
  {
    return common::OB_RECYCLEBIN_SCHEMA_ID == common::extract_pure_id(database_id_) ||
           common::OB_PUBLIC_SCHEMA_ID == common::extract_pure_id(database_id_);
  }

  // get methods
  inline uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
  inline uint64_t get_database_id() const
  {
    return database_id_;
  }
  inline int64_t get_schema_version() const
  {
    return schema_version_;
  }
X
xy0 已提交
1892
  inline const char *get_database_name() const
O
oceanbase-admin 已提交
1893 1894 1895
  {
    return extract_str(database_name_);
  }
X
xy0 已提交
1896
  inline const common::ObString &get_database_name_str() const
O
oceanbase-admin 已提交
1897 1898 1899
  {
    return database_name_;
  }
X
xy0 已提交
1900
  inline const common::ObString &get_primary_zone() const
O
oceanbase-admin 已提交
1901 1902 1903
  {
    return primary_zone_;
  }
X
xy0 已提交
1904
  inline const char *get_comment() const
O
oceanbase-admin 已提交
1905 1906 1907
  {
    return extract_str(comment_);
  }
X
xy0 已提交
1908
  inline const common::ObString &get_comment_str() const
O
oceanbase-admin 已提交
1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931
  {
    return comment_;
  }
  inline common::ObCharsetType get_charset_type() const
  {
    return charset_type_;
  }
  inline common::ObCollationType get_collation_type() const
  {
    return collation_type_;
  }
  inline common::ObNameCaseMode get_name_case_mode() const
  {
    return name_case_mode_;
  }
  inline bool is_read_only() const
  {
    return read_only_;
  }
  inline uint64_t get_default_tablegroup_id() const
  {
    return default_tablegroup_id_;
  }
X
xy0 已提交
1932
  inline const common::ObString &get_default_tablegroup_name() const
O
oceanbase-admin 已提交
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
  {
    return default_tablegroup_name_;
  }
  inline bool is_in_recyclebin() const
  {
    return in_recyclebin_;
  }
  inline bool is_or_in_recyclebin() const
  {
    return in_recyclebin_ || database_id_ == common::combine_id(tenant_id_, common::OB_RECYCLEBIN_SCHEMA_ID);
  }
X
xy0 已提交
1944
  inline const common::ObString &get_locality_str() const
O
oceanbase-admin 已提交
1945 1946 1947 1948 1949
  {
    static const common::ObString dummy;
    return dummy;
  }
  int get_raw_first_primary_zone(
X
xy0 已提交
1950 1951 1952 1953 1954 1955
      const rootserver::ObRandomZoneSelector &random_selector, common::ObZone &first_primary_zone) const;
  int get_first_primary_zone_inherit(share::schema::ObSchemaGetterGuard &schema_guard,
      const rootserver::ObRandomZoneSelector &random_selector,
      const common::ObIArray<rootserver::ObReplicaAddr> &replica_addrs, common::ObZone &first_primary_zone) const;
  int get_primary_zone_score(const common::ObZone &zone, int64_t &zone_score) const;
  inline const common::ObIArray<ObZoneScore> &get_primary_zone_array() const
O
oceanbase-admin 已提交
1956 1957 1958 1959
  {
    return primary_zone_array_;
  }
  int get_primary_zone_inherit(
X
xy0 已提交
1960 1961
      share::schema::ObSchemaGetterGuard &schema_guard, share::schema::ObPrimaryZone &primary_zone) const;
  int get_paxos_replica_num(share::schema::ObSchemaGetterGuard &schema_guard, int64_t &num) const;
O
oceanbase-admin 已提交
1962
  int get_zone_replica_attr_array_inherit(
X
xy0 已提交
1963
      share::schema::ObSchemaGetterGuard &schema_guard, ZoneLocalityIArray &locality) const;
O
oceanbase-admin 已提交
1964 1965
  // In the current implementation, the zone_list of the Database is directly read from the corresponding tenant.
  int get_zone_list(
X
xy0 已提交
1966
      share::schema::ObSchemaGetterGuard &schema_guard, common::ObIArray<common::ObZone> &zone_list) const;
O
oceanbase-admin 已提交
1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987
  // other methods
  int64_t get_convert_size() const;
  virtual bool is_valid() const;
  virtual void reset();
  void set_drop_schema_version(const int64_t schema_version)
  {
    drop_schema_version_ = schema_version;
  }
  int64_t get_drop_schema_version() const
  {
    return drop_schema_version_;
  }
  bool is_dropped_schema() const
  {
    return drop_schema_version_ > 0;
  }
  TO_STRING_KV(K_(tenant_id), K_(database_id), K_(schema_version), K_(database_name), K_(replica_num), K_(zone_list),
      K_(primary_zone), K_(charset_type), K_(collation_type), K_(name_case_mode), K_(comment), K_(read_only),
      K_(default_tablegroup_id), K_(default_tablegroup_name), K_(in_recyclebin), K_(primary_zone_array),
      K_(drop_schema_version));

G
gm 已提交
1988
private:
O
oceanbase-admin 已提交
1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
  uint64_t tenant_id_;
  uint64_t database_id_;
  int64_t schema_version_;
  common::ObString database_name_;
  int64_t replica_num_;
  common::ObArrayHelper<common::ObString> zone_list_;
  common::ObString primary_zone_;
  common::ObCharsetType charset_type_;      // default:utf8mb4
  common::ObCollationType collation_type_;  // default:utf8mb4_general_ci
  common::ObNameCaseMode name_case_mode_;   // default:OB_NAME_CASE_INVALID
  common::ObString comment_;
  bool read_only_;
  uint64_t default_tablegroup_id_;
  common::ObString default_tablegroup_name_;
  bool in_recyclebin_;
  // primary_zone_ is primary zone list,
  // The following is the parsed array of a single zone, which has been sorted according to priority
  common::ObSEArray<ObZoneScore, common::MAX_ZONE_NUM, common::ObNullAllocator> primary_zone_array_;
  int64_t drop_schema_version_;
};

X
xy0 已提交
2010
inline int ObDatabaseSchema::set_zone_list(const common::ObIArray<common::ObString> &zone_list)
O
oceanbase-admin 已提交
2011 2012 2013 2014
{
  return deep_copy_string_array(zone_list, zone_list_);
}

X
xy0 已提交
2015
inline int ObDatabaseSchema::set_primary_zone(const common::ObString &zone)
O
oceanbase-admin 已提交
2016 2017 2018 2019
{
  return deep_copy_str(zone, primary_zone_);
}

X
xy0 已提交
2020
inline int ObDatabaseSchema::add_zone(const common::ObString &zone)
O
oceanbase-admin 已提交
2021 2022 2023 2024 2025 2026 2027
{
  return add_string_to_array(zone, zone_list_);
}

class ObPartitionOption : public ObSchema {
  OB_UNIS_VERSION(1);

G
gm 已提交
2028
public:
O
oceanbase-admin 已提交
2029
  ObPartitionOption();
X
xy0 已提交
2030
  explicit ObPartitionOption(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
2031
  virtual ~ObPartitionOption();
X
xy0 已提交
2032 2033 2034 2035
  ObPartitionOption(const ObPartitionOption &expr);
  ObPartitionOption &operator=(const ObPartitionOption &expr);
  bool operator==(const ObPartitionOption &expr) const;
  bool operator!=(const ObPartitionOption &expr) const;
O
oceanbase-admin 已提交
2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062

  inline bool is_range_part() const
  {
    return share::schema::is_range_part(part_func_type_);
  }
  inline bool is_hash_part() const
  {
    return share::schema::is_hash_part(part_func_type_);
  }
  inline bool is_hash_like_part() const
  {
    return share::schema::is_hash_like_part(part_func_type_);
  }
  inline bool is_key_part() const
  {
    return share::schema::is_key_part(part_func_type_);
  }
  inline bool is_list_part() const
  {
    return share::schema::is_list_part(part_func_type_);
  }
  inline bool is_auto_range_part() const
  {
    return auto_part_ && is_range_part();
  }

  // set methods
X
xy0 已提交
2063
  int set_part_expr(const common::ObString &expr)
O
oceanbase-admin 已提交
2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
  {
    return deep_copy_str(expr, part_func_expr_);
  }
  inline void set_part_num(const int64_t part_num)
  {
    part_num_ = part_num;
  }
  inline void set_part_func_type(const ObPartitionFuncType func_type)
  {
    part_func_type_ = func_type;
  }
  inline void set_sub_part_func_type(const ObPartitionFuncType func_type)
  {
    part_func_type_ = func_type;
  }
  inline void set_max_used_part_id(const int64_t max_used_part_id)
  {
    max_used_part_id_ = max_used_part_id;
  }
  inline void set_partition_cnt_within_partition_table(const int64_t partition_cnt_within_partition_table)
  {
    partition_cnt_within_partition_table_ = partition_cnt_within_partition_table;
  }
  inline void set_auto_part(const bool auto_part)
  {
    auto_part_ = auto_part;
  }
  inline void set_auto_part_size(const int64_t auto_part_size)
  {
    auto_part_size_ = auto_part_size;
  }

  // get methods
X
xy0 已提交
2097
  inline const common::ObString &get_part_func_expr_str() const
O
oceanbase-admin 已提交
2098 2099 2100
  {
    return part_func_expr_;
  }
X
xy0 已提交
2101
  inline const char *get_part_func_expr() const
O
oceanbase-admin 已提交
2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132
  {
    return extract_str(part_func_expr_);
  }
  inline int64_t get_part_num() const
  {
    return part_num_;
  }
  inline int64_t get_part_space() const
  {
    return part_space_;
  }
  inline ObPartitionFuncType get_part_func_type() const
  {
    return part_func_type_;
  }
  inline ObPartitionFuncType get_sub_part_func_type() const
  {
    return part_func_type_;
  }
  inline int64_t get_max_used_part_id() const
  {
    return max_used_part_id_;
  }
  inline int64_t get_partition_cnt_within_partition_table() const
  {
    return partition_cnt_within_partition_table_;
  }
  bool is_columns() const
  {
    return is_columns_;
  }
X
xy0 已提交
2133
  const common::ObString &get_intervel_start_str() const
O
oceanbase-admin 已提交
2134 2135 2136
  {
    return interval_start_;
  }
X
xy0 已提交
2137
  const common::ObString &get_part_intervel_str() const
O
oceanbase-admin 已提交
2138 2139 2140 2141 2142 2143 2144 2145 2146 2147
  {
    return part_interval_;
  }
  inline int64_t get_auto_part_size() const
  {
    return auto_part_size_;
  }

  // other methods
  virtual void reset();
X
xy0 已提交
2148
  int64_t assign(const ObPartitionOption &src_part);
O
oceanbase-admin 已提交
2149 2150 2151 2152 2153
  int64_t get_convert_size() const;
  virtual bool is_valid() const;
  TO_STRING_KV(K_(part_func_type), K_(part_func_expr), K_(part_num), K_(partition_cnt_within_partition_table),
      K_(max_used_part_id), K_(auto_part), K_(auto_part_size));

G
gm 已提交
2154
private:
O
oceanbase-admin 已提交
2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170
  ObPartitionFuncType part_func_type_;
  common::ObString part_func_expr_;
  int64_t part_num_;
  int32_t part_space_;               // Partition id space, see document for details
  bool is_columns_;                  // Whether it is the range columns/list columns partition
  common::ObString interval_start_;  // interval start value

  common::ObString part_interval_;  // interval partition step
  int64_t max_used_part_id_;
  int64_t partition_cnt_within_partition_table_;
  bool auto_part_;          // Whether it is automatic partition
  int64_t auto_part_size_;  // Automatic partition size, 0 is auto
};

// For any questions about the role of this structure, please contact @jiage
class ObSchemaAllocator : public common::ObIAllocator {
G
gm 已提交
2171
public:
O
oceanbase-admin 已提交
2172 2173
  ObSchemaAllocator() : allocator_(NULL)
  {}
X
xy0 已提交
2174
  ObSchemaAllocator(common::ObIAllocator &allocator) : allocator_(&allocator)
O
oceanbase-admin 已提交
2175 2176
  {}

N
nroskill 已提交
2177
  virtual void* alloc(const int64_t sz) override
O
oceanbase-admin 已提交
2178 2179 2180 2181
  {
    return alloc(sz, common::default_memattr);
  }

N
nroskill 已提交
2182
  virtual void* alloc(const int64_t sz, const common::ObMemAttr& attr) override
O
oceanbase-admin 已提交
2183
  {
X
xy0 已提交
2184
    void *ret = NULL;
O
oceanbase-admin 已提交
2185 2186 2187 2188 2189 2190
    if (allocator_) {
      ret = allocator_->alloc(sz, attr);
    }
    return ret;
  }

N
nroskill 已提交
2191 2192 2193 2194
  virtual void free(void *p) override
  {
    allocator_->free(p);
  }
O
oceanbase-admin 已提交
2195 2196
  virtual ~ObSchemaAllocator(){};

G
gm 已提交
2197
private:
X
xy0 已提交
2198
  common::ObIAllocator *allocator_;
O
oceanbase-admin 已提交
2199 2200 2201 2202 2203 2204 2205
};

// May need to add a schema_version field, this mark when the table was created
// If it is at the table level, need not to add it, just use the table level.
// This level is added. Indicates that the partition level is considered.
// Then it is to modify the subpartition of a single primary partition without affecting other things
struct InnerPartListVectorCmp {
G
gm 已提交
2206
public:
O
oceanbase-admin 已提交
2207 2208 2209
  InnerPartListVectorCmp() : ret_(common::OB_SUCCESS)
  {}

G
gm 已提交
2210
public:
X
xy0 已提交
2211
  bool operator()(const common::ObNewRow &left, const common::ObNewRow &right)
O
oceanbase-admin 已提交
2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228
  {
    bool bool_ret = false;
    int cmp = 0;
    if (common::OB_SUCCESS != ret_) {
      // failed before
    } else if (common::OB_SUCCESS != (ret_ = common::ObRowUtil::compare_row(left, right, cmp))) {
      SHARE_SCHEMA_LOG(ERROR, "l or r is invalid", K(ret_));
    } else {
      bool_ret = (cmp < 0);
    }
    return bool_ret;
  }
  int get_ret() const
  {
    return ret_;
  }

G
gm 已提交
2229
private:
O
oceanbase-admin 已提交
2230 2231 2232 2233 2234 2235 2236
  int ret_;
};

class ObPartitionUtils;
struct ObBasePartition : public ObSchema {
  OB_UNIS_VERSION(1);

G
gm 已提交
2237
public:
O
oceanbase-admin 已提交
2238 2239 2240
  friend class ObPartitionUtils;
  friend class sql::ObPartitionExecutorUtils;
  ObBasePartition();
X
xy0 已提交
2241
  explicit ObBasePartition(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287
  virtual void reset();
  void set_tenant_id(const uint64_t tenant_id)
  {
    tenant_id_ = tenant_id;
  }
  uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }

  void set_table_id(const uint64_t table_id)
  {
    table_id_ = table_id;
  }
  uint64_t get_table_id() const
  {
    return table_id_;
  }

  void set_part_id(const int64_t part_id)
  {
    part_id_ = part_id;
  }
  int64_t get_part_id() const
  {
    return part_id_;
  }

  void set_part_idx(const int64_t part_idx)
  {
    part_idx_ = part_idx;
  }
  int64_t get_part_idx() const
  {
    return part_idx_;
  }

  void set_schema_version(int64_t schema_version)
  {
    schema_version_ = schema_version;
  }
  int64_t get_schema_version() const
  {
    return schema_version_;
  }

X
xy0 已提交
2288
  int set_part_name(common::ObString &part_name)
O
oceanbase-admin 已提交
2289 2290 2291
  {
    return deep_copy_str(part_name, name_);
  }
X
xy0 已提交
2292
  const common::ObString &get_part_name() const
O
oceanbase-admin 已提交
2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305
  {
    return name_;
  }

  void set_tablespace_id(const int64_t tablespace_id)
  {
    tablespace_id_ = tablespace_id;
  }
  int64_t get_tablespace_id() const
  {
    return tablespace_id_;
  }

X
xy0 已提交
2306
  int assign(const ObBasePartition &src_part);
O
oceanbase-admin 已提交
2307 2308

  // This interface is not strictly semantically less than, please note
X
xy0 已提交
2309 2310 2311 2312 2313
  static bool less_than(const ObBasePartition *lhs, const ObBasePartition *rhs);
  static bool list_part_func_layout(const ObBasePartition *lhs, const ObBasePartition *rhs);
  static bool range_like_func_less_than(const ObBasePartition *lhs, const ObBasePartition *rhs);
  static bool hash_like_func_less_than(const ObBasePartition *lhs, const ObBasePartition *rhs);
  int add_list_row(const common::ObNewRow &row)
O
oceanbase-admin 已提交
2314 2315 2316
  {
    return list_row_values_.push_back(row);
  }
X
xy0 已提交
2317 2318 2319 2320
  int set_high_bound_val(const common::ObRowkey &high_bound_val);
  int set_high_bound_val_with_hex_str(const common::ObString &high_bound_val_hex);
  int set_list_vector_values_with_hex_str(const common::ObString &list_vector_vals_hex);
  const common::ObRowkey &get_high_bound_val() const
O
oceanbase-admin 已提交
2321 2322 2323 2324 2325
  {
    return high_bound_val_;
  }
  virtual int64_t get_deep_copy_size() const;

X
xy0 已提交
2326
  const common::ObIArray<common::ObNewRow> &get_list_row_values() const
O
oceanbase-admin 已提交
2327 2328 2329 2330
  {
    return list_row_values_;
  }

X
xy0 已提交
2331
  bool same_base_partition(const ObBasePartition &other) const
O
oceanbase-admin 已提交
2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346
  {
    return part_id_ == other.part_id_ && high_bound_val_ == other.high_bound_val_ && status_ == other.status_;
  }
  bool allow_ddl_operator() const
  {
    bool ret = true;
    if (1 == source_part_ids_.count() && -1 != source_part_ids_.at(0)) {
      ret = false;
    }
    return ret;
  }
  ObPartitionStatus get_status() const
  {
    return status_;
  }
X
xy0 已提交
2347
  const common::ObIArray<int64_t> &get_source_part_ids() const
O
oceanbase-admin 已提交
2348 2349 2350
  {
    return source_part_ids_;
  }
X
xy0 已提交
2351
  common::ObIArray<int64_t> &get_source_part_ids()
O
oceanbase-admin 已提交
2352 2353 2354
  {
    return source_part_ids_;
  }
X
xy0 已提交
2355
  int set_source_part_id(common::ObString &partition_str);
O
oceanbase-admin 已提交
2356 2357
  VIRTUAL_TO_STRING_KV(K_(tenant_id), K_(table_id), K_(part_id), K_(name), K_(high_bound_val), K_(list_row_values),
      K_(part_idx), K_(is_empty_partition_name));
X
xy0 已提交
2358
  int get_source_part_ids_str(char *str, const int64_t buf_len) const;
O
oceanbase-admin 已提交
2359 2360 2361 2362 2363 2364 2365 2366 2367
  void set_is_empty_partition_name(bool is_empty)
  {
    is_empty_partition_name_ = is_empty;
  }
  bool is_empty_partition_name() const
  {
    return is_empty_partition_name_;
  }

G
gm 已提交
2368
protected:
O
oceanbase-admin 已提交
2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384
  uint64_t tenant_id_;
  uint64_t table_id_;
  int64_t part_id_;
  int64_t schema_version_;
  common::ObString name_;
  common::ObRowkey high_bound_val_;
  ObSchemaAllocator schema_allocator_;
  common::ObSEArray<common::ObNewRow, 8> list_row_values_;
  enum ObPartitionStatus status_;
  int64_t spare1_;  // TODO Consider whether it is really useful
  int64_t spare2_;
  int64_t spare3_;
  /**
   * @warning: The projector can only be used by the less than interface for partition comparison calculations,
   *  and it is not allowed to be used in other places
   */
X
xy0 已提交
2385
  int32_t *projector_;
O
oceanbase-admin 已提交
2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400
  int64_t projector_size_;
  common::ObSEArray<int64_t, 2> source_part_ids_;
  int64_t part_idx_;  // Valid for hash partition
  // The partition management operation of tablegroup, because after adding pg,
  // the operation of tablegroup needs to be processed first
  // Under Oracle tenants, partition_name is allowed to be empty. There may be a partition name conflict
  // when filling the tablegroup first and directly copying it to the table. Therefore, add a variable
  // when copying to mark this as an empty partition name and do not copy the partition name.
  bool is_empty_partition_name_;
  int64_t tablespace_id_;
};

struct ObPartition : public ObBasePartition {
  OB_UNIS_VERSION(1);

G
gm 已提交
2401
public:
O
oceanbase-admin 已提交
2402
  ObPartition();
X
xy0 已提交
2403 2404
  explicit ObPartition(common::ObIAllocator *allocator);
  int assign(const ObPartition &src_part);
O
oceanbase-admin 已提交
2405 2406 2407

  void reset();
  virtual int64_t get_convert_size() const;
X
xy0 已提交
2408
  virtual int clone(common::ObIAllocator &allocator, ObPartition *&dst) const;
O
oceanbase-admin 已提交
2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457

  void set_sub_part_num(const int64_t sub_part_num)
  {
    sub_part_num_ = sub_part_num;
  }
  int64_t get_sub_part_num() const
  {
    return sub_part_num_;
  }
  int32_t get_sub_part_space() const
  {
    return sub_part_space_;
  }
  void set_mapping_pg_part_id(const int64_t mapping_pg_part_id)
  {
    mapping_pg_part_id_ = mapping_pg_part_id;
  }
  int64_t get_mapping_pg_part_id() const
  {
    return mapping_pg_part_id_;
  }
  common::ObObj get_sub_interval_start() const
  {
    return sub_interval_start_;
  }
  common::ObObj get_sub_part_interval() const
  {
    return sub_part_interval_;
  }
  void set_drop_schema_version(const int64_t schema_version)
  {
    drop_schema_version_ = schema_version;
  }
  int64_t get_drop_schema_version() const
  {
    return drop_schema_version_;
  }
  bool is_dropped_partition() const
  {
    return drop_schema_version_ > 0;
  }
  void set_max_used_sub_part_id(const int64_t max_used_sub_part_id)
  {
    max_used_sub_part_id_ = max_used_sub_part_id;
  }
  int64_t get_max_used_sub_part_id() const
  {
    return max_used_sub_part_id_;
  }
X
xy0 已提交
2458
  ObSubPartition **get_subpart_array() const
O
oceanbase-admin 已提交
2459 2460 2461 2462 2463 2464 2465
  {
    return subpartition_array_;
  }
  int64_t get_subpartition_num() const
  {
    return subpartition_num_;
  }
X
xy0 已提交
2466
  ObSubPartition **get_sorted_part_id_subpartition_array() const
O
oceanbase-admin 已提交
2467 2468 2469
  {
    return sorted_part_id_subpartition_array_;
  }
X
xy0 已提交
2470
  bool same_partition(const ObPartition &other) const
O
oceanbase-admin 已提交
2471 2472 2473 2474 2475
  {
    return same_base_partition(other) && sub_part_num_ == other.sub_part_num_ &&
           sub_part_space_ == other.sub_part_space_ && sub_interval_start_ == other.sub_interval_start_ &&
           sub_part_interval_ == other.sub_part_interval_;
  }
X
xy0 已提交
2476
  int add_partition(const ObSubPartition &subpartition);
O
oceanbase-admin 已提交
2477
  int generate_mapping_pg_subpartition_array();
X
xy0 已提交
2478
  ObSubPartition **get_dropped_subpart_array() const
O
oceanbase-admin 已提交
2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492
  {
    return dropped_subpartition_array_;
  }
  int64_t get_dropped_subpartition_num() const
  {
    return dropped_subpartition_num_;
  }
  void reset_dropped_subpartition()
  {
    dropped_subpartition_num_ = 0;
    dropped_subpartition_array_ = NULL;
  }
  INHERIT_TO_STRING_KV("BasePartition", ObBasePartition, K_(mapping_pg_part_id), K_(source_part_ids),
      K_(drop_schema_version), K_(max_used_sub_part_id), "subpartition_array",
X
xy0 已提交
2493
      common::ObArrayWrap<ObSubPartition *>(subpartition_array_, subpartition_num_), K_(subpartition_array_capacity),
O
oceanbase-admin 已提交
2494
      "dropped_subpartition_array",
X
xy0 已提交
2495
      common::ObArrayWrap<ObSubPartition *>(dropped_subpartition_array_, dropped_subpartition_num_),
O
oceanbase-admin 已提交
2496 2497
      K_(dropped_subpartition_array_capacity));

G
gm 已提交
2498
private:
O
oceanbase-admin 已提交
2499
  int inner_add_partition(
X
xy0 已提交
2500
      const ObSubPartition &part, ObSubPartition **&part_array, int64_t &part_array_capacity, int64_t &part_num);
O
oceanbase-admin 已提交
2501

G
gm 已提交
2502
protected:
O
oceanbase-admin 已提交
2503 2504
  static const int64_t DEFAULT_ARRAY_CAPACITY = 128;

G
gm 已提交
2505
private:
O
oceanbase-admin 已提交
2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522
  // The 14x branch gz_commmon cluster once supported ob_admin to increase the function of subpartition
  // However, this function does not modify sub_part_num, so it is not assumed that the sub_part_num column value
  // of __all_part_history of the templated subpartition table is correct, which is corrected when the schema is
  // refreshed
  int64_t sub_part_num_;
  int32_t sub_part_space_;
  common::ObObj sub_interval_start_;
  common::ObObj sub_part_interval_;
  int64_t mapping_pg_part_id_;
  int64_t drop_schema_version_;
  // The following new members are only valid for non-templated subpartitions
  // Ensure that sub_part_id is not reused when adding or deleting subpartitions
  int64_t max_used_sub_part_id_;
  // nontemplate equal sub_part_num_
  int64_t subpartition_num_;
  // The array size of subpartition_array is not necessarily equal to subpartition_num_
  int64_t subpartition_array_capacity_;
X
xy0 已提交
2523
  ObSubPartition **subpartition_array_;
O
oceanbase-admin 已提交
2524
  // Generated when the schema is refreshed, only used by the table, used to quickly index pg_key, not serialized
X
xy0 已提交
2525
  ObSubPartition **sorted_part_id_subpartition_array_;
O
oceanbase-admin 已提交
2526 2527 2528
  /* delay delete subpartition */
  int64_t dropped_subpartition_num_;
  int64_t dropped_subpartition_array_capacity_;
X
xy0 已提交
2529
  ObSubPartition **dropped_subpartition_array_;
O
oceanbase-admin 已提交
2530 2531 2532 2533 2534
};

struct ObSubPartition : public ObBasePartition {
  OB_UNIS_VERSION(1);

G
gm 已提交
2535
public:
O
oceanbase-admin 已提交
2536 2537 2538
  // For template, because it does not belong to any part, we set part_id to -1
  static const int64_t TEMPLATE_PART_ID = -1;

G
gm 已提交
2539
public:
O
oceanbase-admin 已提交
2540
  ObSubPartition();
X
xy0 已提交
2541 2542
  explicit ObSubPartition(common::ObIAllocator *allocator);
  int assign(const ObSubPartition &src_part);
O
oceanbase-admin 已提交
2543
  virtual void reset();
X
xy0 已提交
2544
  virtual int clone(common::ObIAllocator &allocator, ObSubPartition *&dst) const;
O
oceanbase-admin 已提交
2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583

  void set_sub_part_id(const int64_t subpart_id)
  {
    subpart_id_ = subpart_id;
  }
  int64_t get_sub_part_id() const
  {
    return subpart_id_;
  }
  void set_sub_part_idx(const int64_t subpart_idx)
  {
    subpart_idx_ = subpart_idx;
  }
  int64_t get_sub_part_idx() const
  {
    return subpart_idx_;
  }
  void set_mapping_pg_sub_part_id(const int64_t mapping_pg_sub_part_id)
  {
    mapping_pg_sub_part_id_ = mapping_pg_sub_part_id;
  }
  int64_t get_mapping_pg_sub_part_id() const
  {
    return mapping_pg_sub_part_id_;
  }
  void set_drop_schema_version(const int64_t schema_version)
  {
    drop_schema_version_ = schema_version;
  }
  int64_t get_drop_schema_version() const
  {
    return drop_schema_version_;
  }
  bool is_dropped_partition() const
  {
    return drop_schema_version_ > 0;
  }

  // first compare part_idx,then compare high_bound_val
X
xy0 已提交
2584 2585
  static bool less_than(const ObSubPartition *lhs, const ObSubPartition *rhs);
  bool same_sub_partition(const ObSubPartition &other) const
O
oceanbase-admin 已提交
2586 2587 2588 2589
  {
    return same_base_partition(other) && subpart_id_ == other.subpart_id_;
  }
  virtual int64_t get_convert_size() const;
X
xy0 已提交
2590 2591
  bool key_match(const ObSubPartition &other) const;
  static bool hash_like_func_less_than(const ObSubPartition *lhs, const ObSubPartition *rhs);
O
oceanbase-admin 已提交
2592 2593 2594
  INHERIT_TO_STRING_KV("BasePartition", ObBasePartition, K_(subpart_id), K_(subpart_idx), K_(mapping_pg_sub_part_id),
      K_(drop_schema_version));

G
gm 已提交
2595
private:
O
oceanbase-admin 已提交
2596 2597 2598 2599 2600 2601 2602 2603 2604 2605
  int64_t subpart_id_;
  int64_t subpart_idx_;             // Valid for hash partition;
  int64_t mapping_pg_sub_part_id_;  // nontemplate table, is partition_id of pg.
                                    // template table, is sub_part_id of pg of template
  int64_t drop_schema_version_;
};

class ObPartitionSchema : public ObSchema {
  OB_UNIS_VERSION(1);

G
gm 已提交
2606
public:
O
oceanbase-admin 已提交
2607 2608
  // base methods
  ObPartitionSchema();
X
xy0 已提交
2609
  explicit ObPartitionSchema(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
2610
  virtual ~ObPartitionSchema();
X
xy0 已提交
2611 2612
  ObPartitionSchema(const ObPartitionSchema &src_schema);
  ObPartitionSchema &operator=(const ObPartitionSchema &src_schema);
O
oceanbase-admin 已提交
2613 2614
  // partition related

X
xy0 已提交
2615
  virtual const char *get_entity_name() const = 0;
O
oceanbase-admin 已提交
2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629
  virtual uint64_t get_tenant_id() const = 0;
  virtual void set_tenant_id(const uint64_t tenant_id) = 0;
  virtual uint64_t get_table_id() const = 0;
  virtual void set_table_id(const uint64_t table_id) = 0;
  virtual int64_t get_schema_version() const = 0;
  virtual void set_schema_version(const int64_t schema_version) = 0;
  virtual bool is_user_partition_table() const = 0;
  virtual bool is_user_subpartition_table() const = 0;
  virtual ObPartitionLevel get_part_level() const
  {
    return part_level_;
  }
  virtual int64_t get_partition_cnt() const = 0;
  virtual bool has_self_partition() const = 0;
X
xy0 已提交
2630
  virtual int get_primary_zone_inherit(ObSchemaGetterGuard &schema_guard, ObPrimaryZone &primary_zone) const = 0;
O
oceanbase-admin 已提交
2631
  virtual int get_zone_replica_attr_array_inherit(
X
xy0 已提交
2632
      ObSchemaGetterGuard &schema_guard, ZoneLocalityIArray &locality) const = 0;
O
oceanbase-admin 已提交
2633
  virtual int get_zone_list(
X
xy0 已提交
2634
      share::schema::ObSchemaGetterGuard &schema_guard, common::ObIArray<common::ObZone> &zone_list) const = 0;
O
oceanbase-admin 已提交
2635
  virtual int get_locality_str_inherit(
X
xy0 已提交
2636 2637 2638 2639 2640
      share::schema::ObSchemaGetterGuard &guard, const common::ObString *&locality_str) const = 0;
  virtual int get_first_primary_zone_inherit(share::schema::ObSchemaGetterGuard &schema_guard,
      const rootserver::ObRandomZoneSelector &random_selector,
      const common::ObIArray<rootserver::ObReplicaAddr> &replica_addrs, common::ObZone &first_primary_zone) const = 0;
  virtual int check_is_duplicated(share::schema::ObSchemaGetterGuard &guard, bool &is_duplicated) const = 0;
O
oceanbase-admin 已提交
2641 2642
  virtual uint64_t get_tablegroup_id() const = 0;
  virtual void set_tablegroup_id(const uint64_t tg_id) = 0;
X
xy0 已提交
2643 2644 2645 2646
  virtual const common::ObString &get_locality_str() const = 0;
  virtual common::ObString &get_locality_str() = 0;
  virtual const common::ObString &get_previous_locality_str() const = 0;
  virtual int get_paxos_replica_num(share::schema::ObSchemaGetterGuard &schema_guard, int64_t &num) const = 0;
O
oceanbase-admin 已提交
2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700
  virtual share::ObDuplicateScope get_duplicate_scope() const = 0;
  virtual void set_duplicate_scope(const share::ObDuplicateScope duplicate_scope) = 0;
  virtual void set_duplicate_scope(const int64_t duplicate_scope) = 0;
  inline virtual int64_t get_part_func_expr_num() const
  {
    return 0;
  }
  inline virtual void set_part_func_expr_num(const int64_t part_func_expr_num)
  {
    UNUSED(part_func_expr_num);
  }
  inline virtual int64_t get_sub_part_func_expr_num() const
  {
    return 0;
  }
  inline virtual void set_sub_part_func_expr_num(const int64_t sub_part_func_expr_num)
  {
    UNUSED(sub_part_func_expr_num);
  }
  // Define the copy distribution method of the partition table, including:
  // 0. Disperse in any way
  // 1. Prioritize the first-partition division
  // 2. Prioritize the second-partition division
  int64_t distribute_by() const;

  inline void set_part_num(const int64_t part_num)
  {
    part_num_ = part_num;
    part_option_.set_part_num(part_num);
  }
  // Only the templated secondary partition is valid, pay attention!!
  inline void set_def_sub_part_num(const int64_t def_subpart_num)
  {
    def_subpart_num_ = def_subpart_num;
    sub_part_option_.set_part_num(def_subpart_num);
  }
  inline void set_part_level(const ObPartitionLevel part_level)
  {
    part_level_ = part_level;
  }
  virtual bool is_sub_part_template() const
  {
    return is_sub_part_template_;
  }
  void set_is_sub_part_template(bool is_sub_part_template)
  {
    is_sub_part_template_ = is_sub_part_template;
  }
  inline int64_t get_first_part_num() const
  {
    return part_option_.get_part_num();
  }
  // Only the templated secondary partition is valid, pay attention!!
  int64_t get_def_sub_part_num() const;
X
xy0 已提交
2701
  int get_first_individual_sub_part_num(int64_t &sub_part_num) const;
O
oceanbase-admin 已提交
2702 2703
  // SQL view, unable to see the delayed deleted partition
  int64_t get_all_part_num() const;
X
xy0 已提交
2704
  int get_all_partition_num(bool check_dropped_partition, int64_t &part_num) const;
O
oceanbase-admin 已提交
2705

X
xy0 已提交
2706 2707 2708 2709
  int check_part_name(const ObPartition &partition);
  int check_part_id(const ObPartition &partition);
  int add_partition(const ObPartition &partition);
  int add_def_subpartition(const ObSubPartition &subpartition);
O
oceanbase-admin 已提交
2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756

  inline bool is_hash_part() const
  {
    return part_option_.is_hash_part();
  }
  inline bool is_hash_subpart() const
  {
    return sub_part_option_.is_hash_part();
  }
  inline bool is_key_part() const
  {
    return part_option_.is_key_part();
  }
  inline bool is_key_subpart() const
  {
    return sub_part_option_.is_key_part();
  }
  inline bool is_range_part() const
  {
    return part_option_.is_range_part();
  }
  inline bool is_range_subpart() const
  {
    return sub_part_option_.is_range_part();
  }

  inline bool is_hash_like_part() const
  {
    return part_option_.is_hash_like_part();
  }
  inline bool is_hash_like_subpart() const
  {
    return sub_part_option_.is_hash_like_part();
  }
  inline bool is_list_part() const
  {
    return part_option_.is_list_part();
  }
  inline bool is_list_subpart() const
  {
    return sub_part_option_.is_list_part();
  }
  inline bool is_auto_partitioned_table() const
  {
    return part_option_.is_auto_range_part();
  }

X
xy0 已提交
2757
  inline const ObPartitionOption &get_part_option() const
O
oceanbase-admin 已提交
2758 2759 2760
  {
    return part_option_;
  }
X
xy0 已提交
2761
  inline ObPartitionOption &get_part_option()
O
oceanbase-admin 已提交
2762 2763 2764
  {
    return part_option_;
  }
X
xy0 已提交
2765
  inline const ObPartitionOption &get_sub_part_option() const
O
oceanbase-admin 已提交
2766 2767 2768
  {
    return sub_part_option_;
  }
X
xy0 已提交
2769
  inline ObPartitionOption &get_sub_part_option()
O
oceanbase-admin 已提交
2770 2771 2772 2773 2774 2775 2776 2777
  {
    return sub_part_option_;
  }
  inline int64_t get_auto_part_size() const
  {
    return part_option_.get_auto_part_size();
  }

X
xy0 已提交
2778 2779 2780 2781 2782
  int serialize_partitions(char *buf, const int64_t data_len, int64_t &pos) const;
  int serialize_dropped_partitions(char *buf, const int64_t data_len, int64_t &pos) const;
  int serialize_def_subpartitions(char *buf, const int64_t data_len, int64_t &pos) const;
  int deserialize_partitions(const char *buf, const int64_t data_len, int64_t &pos);
  int deserialize_def_subpartitions(const char *buf, const int64_t data_len, int64_t &pos);
O
oceanbase-admin 已提交
2783 2784

  int get_partition_by_part_id(
X
xy0 已提交
2785
      const int64_t part_id, const bool check_dropped_partition, const ObPartition *&partition) const;
O
oceanbase-admin 已提交
2786
  // both part_id and subpart_id is not OB_TWO_PART_MASK
X
xy0 已提交
2787
  int get_subpartition(const int64_t part_id, const int64_t subpart_id, const ObSubPartition *&subpartition) const;
O
oceanbase-admin 已提交
2788 2789 2790 2791 2792 2793 2794 2795 2796 2797

  /** generate part name of hash partition, for resolver only
   * @name_type: Type of generated hash partition name:
   *                                FIRST_PART generate partition name
   *                                TEMPLATE_SUB_PART generate subpartition name
   *                                INDIVIDUAL_SUB_PART generate subpartition name of nontemplate
   * @need_upper_case: Does the generated partition name need to be capitalized?
   * @partition: nantemplate table, the partition name needs to be spelled with the partition name,
   *    and partition indicates the partition where the current subpartition is located
   */
X
xy0 已提交
2798 2799
  int gen_hash_part_name(const int64_t part_id, const ObHashNameType name_type, const bool need_upper_case, char *buf,
      const int64_t buf_size, int64_t *pos, const ObPartition *partition = NULL) const;
O
oceanbase-admin 已提交
2800
  // Get the partition name corresponding to partition_id (non-partition/partition/subpartition)
X
xy0 已提交
2801
  int get_partition_name(const int64_t partition_id, char *buf, const int64_t buf_size, int64_t *pos /* = NULL*/) const;
O
oceanbase-admin 已提交
2802
  // get partition name
X
xy0 已提交
2803
  int get_part_name(const int64_t part_id, char *buf, const int64_t buf_size, int64_t *pos /* = NULL*/) const;
O
oceanbase-admin 已提交
2804
  // is_def_subpart refers to the generation of __all_def_sub_part or the part_name in the __all_sub_part table
X
xy0 已提交
2805 2806
  int get_subpart_name(const int64_t part_id, const int64_t subpart_id, const bool is_def_subpart, char *buf,
      const int64_t buf_size, int64_t *pos /* = NULL*/) const;
O
oceanbase-admin 已提交
2807

X
xy0 已提交
2808
  inline ObPartition **get_part_array() const
O
oceanbase-admin 已提交
2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820
  {
    return partition_array_;
  }
  inline int64_t get_partition_capacity() const
  {
    return partition_array_capacity_;
  }
  inline int64_t get_partition_num() const
  {
    return partition_num_;
  }
  // The following interfaces can only be used for templated subpartition tables
X
xy0 已提交
2821
  inline ObSubPartition **get_def_subpart_array() const
O
oceanbase-admin 已提交
2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874
  {
    return def_subpartition_array_;
  }
  inline int64_t get_def_subpartition_capacity() const
  {
    return def_subpartition_array_capacity_;
  }
  inline int64_t get_def_subpartition_num() const
  {
    return def_subpartition_num_;
  }
  /* ----------------------------------*/
  inline void set_partition_schema_version(const int64_t schema_version)
  {
    partition_schema_version_ = schema_version;
  }
  inline int64_t get_partition_schema_version() const
  {
    return partition_schema_version_;
  }
  // -----dropped partition related -----
  void set_drop_schema_version(const int64_t schema_version)
  {
    drop_schema_version_ = schema_version;
  }
  int64_t get_drop_schema_version() const
  {
    return drop_schema_version_;
  }
  bool is_dropped_schema() const
  {
    return drop_schema_version_ > 0;
  }
  int64_t get_dropped_partition_num() const
  {
    return dropped_partition_num_;
  }
  int clear_dropped_partition();
  void reset_dropped_partition()
  {
    dropped_partition_num_ = 0;
    dropped_partition_array_ = NULL;
  }
  // The partition information needs to be reorganized when truncate the subpartition
  void reset_partition_for_trun_subpart()
  {
    partition_array_capacity_ = 0;
    part_num_ = 0;
    partition_num_ = 0;
    partition_array_ = NULL;
  }
  // This interface is only for iterators, and the corresponding iterator should be used to access the deleted
  // partition, or the schema_guard/schema_service corresponding interface should be used
X
xy0 已提交
2875
  ObPartition **get_dropped_part_array() const
O
oceanbase-admin 已提交
2876 2877 2878 2879
  {
    return dropped_partition_array_;
  }
  // -------------------------------------
X
xy0 已提交
2880
  int get_part_id_by_name(const common::ObString partition_name, int64_t &part_id) const;
O
oceanbase-admin 已提交
2881 2882
  // FIXME:() Refresh the schema, do not assume that the partition_array is in order, and can be optimized later
  int find_partition_by_part_id(
X
xy0 已提交
2883
      const int64_t part_id, const bool check_dropped_partition, const ObPartition *&partition) const;
O
oceanbase-admin 已提交
2884
  int get_subpart_info(
X
xy0 已提交
2885
      const int64_t part_id, ObSubPartition **&subpart_array, int64_t &subpart_num, int64_t &subpartition_num) const;
O
oceanbase-admin 已提交
2886
  //-------index partition-------
X
xy0 已提交
2887 2888
  int get_part_id_by_idx(const int64_t part_idx, int64_t &part_id) const;
  int get_hash_subpart_id_by_idx(const int64_t part_id, const int64_t subpart_idx, int64_t &subpart_id) const;
O
oceanbase-admin 已提交
2889

X
xy0 已提交
2890 2891
  int get_hash_part_idx_by_id(const int64_t part_id, int64_t &part_idx) const;
  int get_hash_subpart_idx_by_id(const int64_t part_id, const int64_t subpart_id, int64_t &subpart_idx) const;
O
oceanbase-admin 已提交
2892

X
xy0 已提交
2893 2894
  int convert_partition_idx_to_id(const int64_t partition_idx, int64_t &partition_id) const;
  int convert_partition_id_to_idx(const int64_t partition_id, int64_t &partition_idx) const;
O
oceanbase-admin 已提交
2895 2896 2897 2898
  //----------------------
  // Get the offset of the partition in partition_array, just take the offset of the partition,
  // not including the subpartition
  int get_partition_index_by_id(
X
xy0 已提交
2899
      const int64_t part_id, const bool check_dropped_partition, int64_t &partition_index) const;
O
oceanbase-admin 已提交
2900 2901
  // Binary search range partition, only applicable to the partition that has not been split
  int get_partition_index_binary_search(
X
xy0 已提交
2902
      const int64_t part_id, const bool check_dropped_partition, int64_t &partition_index) const;
O
oceanbase-admin 已提交
2903 2904
  // Iterate all the partitions to get the offset
  int get_partition_index_loop(
X
xy0 已提交
2905
      const int64_t part_id, const bool check_dropped_partition, int64_t &partition_index) const;
O
oceanbase-admin 已提交
2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939

  inline void set_partition_status(const ObPartitionStatus partition_status)
  {
    partition_status_ = partition_status;
  }
  inline ObPartitionStatus get_partition_status() const
  {
    return partition_status_;
  }
  bool is_in_splitting() const
  {
    return partition_status_ == PARTITION_STATUS_LOGICAL_SPLITTING ||
           partition_status_ == PARTITION_STATUS_PHYSICAL_SPLITTING;
  }
  bool is_in_logical_split() const
  {
    return partition_status_ == PARTITION_STATUS_LOGICAL_SPLITTING;
  }
  bool is_in_physical_split() const
  {
    return partition_status_ == PARTITION_STATUS_PHYSICAL_SPLITTING;
  }
  // other methods
  virtual void reset();
  virtual bool is_valid() const;
  virtual void set_min_partition_id(uint64_t partition_id)
  {
    UNUSED(partition_id);
  }
  virtual uint64_t get_min_partition_id() const
  {
    return 0;
  }
  virtual int get_split_source_partition_key(
X
xy0 已提交
2940
      const common::ObPartitionKey &dst_part_key, common::ObPartitionKey &source_part_key) const;
O
oceanbase-admin 已提交
2941
  DECLARE_VIRTUAL_TO_STRING;
X
xy0 已提交
2942 2943
  int try_assign_part_array(const share::schema::ObPartitionSchema &that);
  int try_assign_def_subpart_array(const share::schema::ObPartitionSchema &that);
O
oceanbase-admin 已提交
2944

G
gm 已提交
2945
protected:
X
xy0 已提交
2946 2947
  int inner_add_partition(const ObPartition &part);
  int inner_add_partition(const ObSubPartition &part);
O
oceanbase-admin 已提交
2948
  template <class T>
X
xy0 已提交
2949 2950 2951 2952
  int inner_add_partition(const T &part, T **&part_array, int64_t &part_array_capacity, int64_t &part_num);
  int get_subpart_name(const int64_t part_id, const int64_t subpart_id, char *buf, const int64_t buf_size,
      int64_t *pos /* = NULL*/) const;
  int get_def_subpart_name(const int64_t subpart_id, char *buf, const int64_t buf_size, int64_t *pos /* = NULL*/) const;
O
oceanbase-admin 已提交
2953

G
gm 已提交
2954
protected:
O
oceanbase-admin 已提交
2955 2956
  static const int64_t DEFAULT_ARRAY_CAPACITY = 128;

G
gm 已提交
2957
protected:
O
oceanbase-admin 已提交
2958 2959 2960 2961 2962 2963 2964
  int64_t part_num_;
  // Redundant value, derived from sub_part_option_, valid when is_sub_part_template = true
  int64_t def_subpart_num_;
  ObPartitionLevel part_level_;
  ObPartitionOption part_option_;
  // The part_num_ is only valid when is_sub_part_template = true
  ObPartitionOption sub_part_option_;
X
xy0 已提交
2965
  ObPartition **partition_array_;
O
oceanbase-admin 已提交
2966 2967 2968 2969 2970
  int64_t partition_array_capacity_;  // The array size of partition_array is not necessarily equal to partition_num;
  // Equal to part_num; historical reasons cause the two values to have the same meaning;
  // need to be modified together with part_num_
  int64_t partition_num_;
  /* template table, when is_sub_part_template = true, valid */
X
xy0 已提交
2971
  ObSubPartition **def_subpartition_array_;
O
oceanbase-admin 已提交
2972 2973 2974 2975 2976 2977 2978 2979 2980 2981
  // The array size of subpartition_array, not necessarily equal to subpartition_num
  int64_t def_subpartition_array_capacity_;
  int64_t def_subpartition_num_;  // equal subpart_num
  /* template subpartition define end*/
  // Record the split schema, initialized to 0, not cleared after splitting, the bottom layer needs to be used
  int64_t partition_schema_version_;
  ObPartitionStatus partition_status_;
  /* delay delete table/tablegroup/partition start*/
  int64_t drop_schema_version_;
  // delay delete partition, Not visible to SQL
X
xy0 已提交
2982
  ObPartition **dropped_partition_array_;
O
oceanbase-admin 已提交
2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993
  int64_t dropped_partition_array_capacity_;
  int64_t dropped_partition_num_;
  /* delay delete table/tablegroup/partition end*/
  // It is meaningful when the subpartition table is used to mark whether the subpartition is a templated definition,
  // and it is compatible with
  bool is_sub_part_template_;
};

class ObTablegroupSchema : public ObPartitionSchema {
  OB_UNIS_VERSION(1);

G
gm 已提交
2994
public:
O
oceanbase-admin 已提交
2995 2996
  // base methods
  ObTablegroupSchema();
X
xy0 已提交
2997
  explicit ObTablegroupSchema(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
2998
  virtual ~ObTablegroupSchema();
X
xy0 已提交
2999 3000 3001
  ObTablegroupSchema(const ObTablegroupSchema &src_schema);
  ObTablegroupSchema &operator=(const ObTablegroupSchema &src_schema);
  int assign(const ObTablegroupSchema &src_schema);
O
oceanbase-admin 已提交
3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018
  // set methods
  inline void set_binding(const bool binding)
  {
    binding_ = binding;
  }
  inline void set_tenant_id(const uint64_t tenant_id) override
  {
    tenant_id_ = tenant_id;
  }
  inline void set_schema_version(const int64_t schema_version) override
  {
    schema_version_ = schema_version;
  }
  virtual void set_tablegroup_id(const uint64_t tablegroup_id) override
  {
    tablegroup_id_ = tablegroup_id;
  }
X
xy0 已提交
3019
  inline int set_tablegroup_name(const char *name)
O
oceanbase-admin 已提交
3020 3021 3022
  {
    return deep_copy_str(name, tablegroup_name_);
  }
X
xy0 已提交
3023
  inline int set_comment(const char *comment)
O
oceanbase-admin 已提交
3024 3025 3026
  {
    return deep_copy_str(comment, comment_);
  }
X
xy0 已提交
3027
  inline int set_tablegroup_name(const common::ObString &name)
O
oceanbase-admin 已提交
3028 3029 3030
  {
    return deep_copy_str(name, tablegroup_name_);
  }
X
xy0 已提交
3031
  inline int set_table_name(const common::ObString &name)
O
oceanbase-admin 已提交
3032 3033 3034
  {
    return deep_copy_str(name, tablegroup_name_);
  }
X
xy0 已提交
3035
  inline int set_comment(const common::ObString &comment)
O
oceanbase-admin 已提交
3036 3037 3038
  {
    return deep_copy_str(comment, comment_);
  }
X
xy0 已提交
3039
  inline int set_locality(const common::ObString &locality)
O
oceanbase-admin 已提交
3040 3041 3042
  {
    return deep_copy_str(locality, locality_info_.locality_str_);
  }
X
xy0 已提交
3043
  inline int set_primary_zone(const common::ObString &primary_zone)
O
oceanbase-admin 已提交
3044 3045 3046
  {
    return deep_copy_str(primary_zone, primary_zone_info_.primary_zone_str_);
  }
X
xy0 已提交
3047
  inline int set_previous_locality(const common::ObString &previous_locality)
O
oceanbase-admin 已提交
3048 3049 3050
  {
    return deep_copy_str(previous_locality, previous_locality_);
  }
X
xy0 已提交
3051
  int set_primary_zone_array(const common::ObIArray<ObZoneScore> &primary_zone_array);
O
oceanbase-admin 已提交
3052

X
xy0 已提交
3053 3054 3055
  int set_zone_replica_attr_array(const common::ObIArray<share::ObZoneReplicaAttrSet> &src);
  int set_zone_replica_attr_array(const common::ObIArray<share::SchemaZoneReplicaAttrSet> &src);
  inline int set_split_partition(const common::ObString &split_partition)
O
oceanbase-admin 已提交
3056 3057 3058
  {
    return deep_copy_str(split_partition, split_partition_name_);
  }
X
xy0 已提交
3059
  inline int set_split_rowkey(const common::ObRowkey &rowkey)
O
oceanbase-admin 已提交
3060 3061 3062
  {
    return rowkey.deep_copy(split_high_bound_val_, *get_allocator());
  }
X
xy0 已提交
3063
  inline int set_split_list_value(common::ObRowkey &list_values)
O
oceanbase-admin 已提交
3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083
  {
    return list_values.deep_copy(split_list_row_values_, *get_allocator());
  }
  // get methods
  inline bool get_binding() const
  {
    return binding_;
  }
  inline uint64_t get_tenant_id() const override
  {
    return tenant_id_;
  }
  virtual inline int64_t get_schema_version() const override
  {
    return schema_version_;
  }
  virtual uint64_t get_tablegroup_id() const override
  {
    return tablegroup_id_;
  }
X
xy0 已提交
3084
  inline const char *get_tablegroup_name_str() const
O
oceanbase-admin 已提交
3085 3086 3087
  {
    return extract_str(tablegroup_name_);
  }
X
xy0 已提交
3088
  inline const char *get_comment() const
O
oceanbase-admin 已提交
3089 3090 3091
  {
    return extract_str(comment_);
  }
X
xy0 已提交
3092
  inline const common::ObString &get_tablegroup_name() const
O
oceanbase-admin 已提交
3093 3094 3095
  {
    return tablegroup_name_;
  }
X
xy0 已提交
3096
  inline const common::ObString &get_table_name() const
O
oceanbase-admin 已提交
3097 3098 3099
  {
    return tablegroup_name_;
  }
X
xy0 已提交
3100
  virtual const char *get_entity_name() const override
O
oceanbase-admin 已提交
3101 3102 3103
  {
    return extract_str(tablegroup_name_);
  }
X
xy0 已提交
3104
  inline const common::ObString &get_comment_str() const
O
oceanbase-admin 已提交
3105 3106 3107
  {
    return comment_;
  }
X
xy0 已提交
3108
  inline const ObLocality &get_locality_info() const
O
oceanbase-admin 已提交
3109 3110 3111
  {
    return locality_info_;
  }
X
xy0 已提交
3112
  virtual common::ObString &get_locality_str() override
O
oceanbase-admin 已提交
3113 3114 3115
  {
    return locality_info_.locality_str_;
  }
X
xy0 已提交
3116
  virtual const common::ObString &get_locality_str() const override
O
oceanbase-admin 已提交
3117 3118 3119
  {
    return locality_info_.locality_str_;
  }
X
xy0 已提交
3120
  inline const char *get_locality() const
O
oceanbase-admin 已提交
3121 3122 3123
  {
    return extract_str(locality_info_.locality_str_);
  }
X
xy0 已提交
3124
  inline const char *get_previous_locality() const
O
oceanbase-admin 已提交
3125 3126 3127
  {
    return extract_str(previous_locality_);
  }
X
xy0 已提交
3128
  virtual const common::ObString &get_previous_locality_str() const override
O
oceanbase-admin 已提交
3129 3130 3131
  {
    return previous_locality_;
  }
X
xy0 已提交
3132
  inline const ObPrimaryZone &get_primary_zone_info() const
O
oceanbase-admin 已提交
3133 3134 3135
  {
    return primary_zone_info_;
  }
X
xy0 已提交
3136
  inline const common::ObString &get_primary_zone() const
O
oceanbase-admin 已提交
3137 3138 3139
  {
    return primary_zone_info_.primary_zone_str_;
  }
X
xy0 已提交
3140
  int set_zone_list(const common::ObIArray<common::ObZone> &zone_list)
O
oceanbase-admin 已提交
3141 3142 3143 3144
  {
    UNUSED(zone_list);
    return common::OB_SUCCESS;
  }
X
xy0 已提交
3145
  inline const common::ObString &get_split_partition_name() const
O
oceanbase-admin 已提交
3146 3147 3148
  {
    return split_partition_name_;
  }
X
xy0 已提交
3149
  inline const common::ObRowkey &get_split_rowkey() const
O
oceanbase-admin 已提交
3150 3151 3152
  {
    return split_high_bound_val_;
  }
X
xy0 已提交
3153
  inline const common::ObRowkey &get_split_list_row_values() const
O
oceanbase-admin 已提交
3154 3155 3156 3157 3158 3159 3160
  {
    return split_list_row_values_;
  }
  void reset_locality_options();
  // In the current implementation, if the locality of the zone_list of the tablegroup is empty,
  // it will be read from the tenant, otherwise it will be parsed from the locality
  virtual int get_zone_list(
X
xy0 已提交
3161 3162
      share::schema::ObSchemaGetterGuard &schema_guard, common::ObIArray<common::ObZone> &zone_list) const override;
  virtual int get_zone_replica_attr_array(ZoneLocalityIArray &locality) const;
O
oceanbase-admin 已提交
3163
  virtual int get_zone_replica_attr_array_inherit(
X
xy0 已提交
3164
      ObSchemaGetterGuard &schema_guard, ZoneLocalityIArray &locality) const override;
O
oceanbase-admin 已提交
3165
  virtual int get_locality_str_inherit(
X
xy0 已提交
3166 3167 3168 3169 3170 3171 3172 3173
      ObSchemaGetterGuard &schema_guard, const common::ObString *&locality_str) const override;
  virtual int get_first_primary_zone_inherit(share::schema::ObSchemaGetterGuard &schema_guard,
      const rootserver::ObRandomZoneSelector &random_selector,
      const common::ObIArray<rootserver::ObReplicaAddr> &replica_addrs,
      common::ObZone &first_primary_zone) const override;
  virtual int check_is_duplicated(share::schema::ObSchemaGetterGuard &guard, bool &is_duplicated) const override;
  int get_primary_zone_score(const common::ObZone &zone, int64_t &zone_score) const;
  inline const common::ObIArray<ObZoneScore> &get_primary_zone_array() const
O
oceanbase-admin 已提交
3174 3175 3176 3177
  {
    return primary_zone_info_.primary_zone_array_;
  }
  virtual int get_primary_zone_inherit(
X
xy0 已提交
3178
      share::schema::ObSchemaGetterGuard &schema_guard, share::schema::ObPrimaryZone &primary_zone) const override;
O
oceanbase-admin 已提交
3179 3180
  int fill_additional_options();
  int check_in_locality_modification(
X
xy0 已提交
3181 3182 3183 3184 3185 3186
      share::schema::ObSchemaGetterGuard &schema_guard, bool &in_locality_modification) const;
  int check_is_readonly_at_all(share::schema::ObSchemaGetterGuard &guard, const common::ObZone &zone,
      const common::ObRegion &region, bool &readonly_at_all) const;
  int get_full_replica_num(share::schema::ObSchemaGetterGuard &schema_guard, int64_t &num) const;
  virtual int get_paxos_replica_num(share::schema::ObSchemaGetterGuard &schema_guard, int64_t &num) const override;
  int get_all_replica_num(share::schema::ObSchemaGetterGuard &schema_guard, int64_t &num) const;
O
oceanbase-admin 已提交
3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207
  // partition related
  virtual share::ObDuplicateScope get_duplicate_scope() const override
  {
    return share::ObDuplicateScope::DUPLICATE_SCOPE_NONE;
  }
  virtual void set_duplicate_scope(const share::ObDuplicateScope duplicate_scope) override
  {
    UNUSED(duplicate_scope);
  }
  virtual void set_duplicate_scope(const int64_t duplicate_scope) override
  {
    UNUSED(duplicate_scope);
  }
  inline virtual bool is_user_partition_table() const override
  {
    return PARTITION_LEVEL_ONE == get_part_level() || PARTITION_LEVEL_TWO == get_part_level();
  }
  inline virtual bool is_user_subpartition_table() const override
  {
    return PARTITION_LEVEL_TWO == get_part_level();
  }
G
gm 已提交
3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243
  inline virtual uint64_t get_table_id() const override
  {
    return tablegroup_id_;
  }  // for partition schema used
  inline virtual uint64_t get_database_id() const
  {
    return 0;
  }
  inline virtual void set_database_id(const uint64_t database_id)
  {
    UNUSED(database_id);
  }
  inline virtual void set_table_id(const uint64_t tablegroup_id) override
  {
    tablegroup_id_ = tablegroup_id;
  }
  inline int64_t get_part_func_expr_num() const override
  {
    return part_func_expr_num_;
  }
  inline void set_part_func_expr_num(const int64_t part_func_expr_num) override
  {
    part_func_expr_num_ = part_func_expr_num;
  }
  inline int64_t get_sub_part_func_expr_num() const override
  {
    return sub_part_func_expr_num_;
  }
  inline void set_sub_part_func_expr_num(const int64_t sub_part_func_expr_num) override
  {
    sub_part_func_expr_num_ = sub_part_func_expr_num;
  }
  virtual int64_t get_partition_cnt() const override
  {
    return 0;
  }
X
xy0 已提交
3244 3245
  virtual int calc_part_func_expr_num(int64_t &part_func_expr_num) const;
  virtual int calc_subpart_func_expr_num(int64_t &subpart_func_expr_num) const;
G
gm 已提交
3246
  // other methods
G
gm 已提交
3247 3248
  virtual void reset() override;
  int64_t get_convert_size() const override;
G
gm 已提交
3249 3250 3251 3252
  virtual bool has_self_partition() const override
  {
    return get_binding();
  }
G
gm 已提交
3253
  virtual bool is_valid() const override;
G
gm 已提交
3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269
  bool is_mock_global_index_invalid() const
  {
    return is_mock_global_index_invalid_;
  }
  bool is_global_index_table() const
  {
    return false;
  }
  void set_mock_global_index_invalid(const bool is_invalid)
  {
    is_mock_global_index_invalid_ = is_invalid;
  }
  bool can_read_index() const
  {
    return true;
  }
O
oceanbase-admin 已提交
3270 3271 3272

  DECLARE_VIRTUAL_TO_STRING;

G
gm 已提交
3273
private:
O
oceanbase-admin 已提交
3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295
  uint64_t tenant_id_;
  uint64_t tablegroup_id_;
  int64_t schema_version_;
  common::ObString tablegroup_name_;
  common::ObString comment_;
  // 2.0 add
  ObLocality locality_info_;
  ObPrimaryZone primary_zone_info_;
  int64_t part_func_expr_num_;
  int64_t sub_part_func_expr_num_;
  common::ObString previous_locality_;
  common::ObString split_partition_name_;
  common::ObRowkey split_high_bound_val_;
  common::ObRowkey split_list_row_values_;
  // Indicates whether the tablegroup has physical physical partitions
  // true: With physical physical partition
  // false: Without physical physical partition
  bool binding_;
  bool is_mock_global_index_invalid_;  // Standalone cluster use; no serialization required
};

class ObPartitionUtils {
G
gm 已提交
3296
public:
O
oceanbase-admin 已提交
3297 3298 3299
  /// Get part ids through range of partition_column

  // Get hash(FUNC_HASH/FUNC_KEY) part type ids
X
xy0 已提交
3300 3301
  static int get_hash_part_ids(const ObTableSchema &table_schema, const common::ObNewRange &range,
      const int64_t part_num, common::ObIArray<int64_t> &part_ids);
O
oceanbase-admin 已提交
3302 3303 3304 3305 3306
  // @param: get_part_index Indicates whether this function is called to get whether it is part_index or part_id
  // - true  Indicates that part_index is to be obtained, and part_index indicates the position of partition
  //  in the partition array.
  // - false Indicates that the part to be obtained is part_id, and part_id indicates the logical id of the primary
  // partition
X
xy0 已提交
3307 3308 3309 3310
  static int get_hash_part_ids(const ObTableSchema &table_schema, const common::ObNewRow &row, int64_t part_num,
      common::ObIArray<int64_t> &part_ids, bool get_part_index = false);
  static int get_hash_subpart_ids(const ObTableSchema &table_schema, const int64_t part_id,
      const common::ObNewRange &range, const int64_t subpart_num, common::ObIArray<int64_t> &subpart_ids);
O
oceanbase-admin 已提交
3311 3312 3313 3314 3315 3316
  // @param: get_subpart_index  Indicates whether this function is called to get whether it is subpart_index or
  // subpart_id
  // - true  Indicates that subpart_index is to be obtained, and subpart_index indicates the position of subpartition
  //  in the subpartition array.
  // - false Indicates that the subpart to be obtained is subpart_id, and subpart_id indicates the logical id of
  //  the primary subpartition
X
xy0 已提交
3317 3318
  static int get_hash_subpart_ids(const ObTableSchema &table_schema, const int64_t part_id, const common::ObNewRow &row,
      int64_t subpart_num, common::ObIArray<int64_t> &subpart_ids, bool get_subpart_index = false);
O
oceanbase-admin 已提交
3319 3320

  // Get level-one range part_ids
X
xy0 已提交
3321 3322
  static int get_range_part_ids(const common::ObNewRange &range, const bool reverse,
      ObPartition *const *partition_array, const int64_t partition_num, common::ObIArray<int64_t> &part_ids);
O
oceanbase-admin 已提交
3323
  // The meaning of get_part_index can refer to get_hash_part_ids()
X
xy0 已提交
3324 3325
  static int get_range_part_ids(const common::ObNewRow &row, ObPartition *const *partition_array,
      const int64_t partition_num, common::ObIArray<int64_t> &part_ids, bool get_part_index = false);
O
oceanbase-admin 已提交
3326 3327

  // Get level-one range part_ids
X
xy0 已提交
3328 3329
  static int get_list_part_ids(const common::ObNewRange &range, const bool reverse, ObPartition *const *partition_array,
      const int64_t partition_num, common::ObIArray<int64_t> &part_ids);
O
oceanbase-admin 已提交
3330 3331

  // The meaning of get_part_index can refer to get_hash_part_ids()
X
xy0 已提交
3332 3333
  static int get_list_part_ids(const common::ObNewRow &row, ObPartition *const *partition_array,
      const int64_t partition_num, common::ObIArray<int64_t> &part_ids, bool get_part_index = false);
O
oceanbase-admin 已提交
3334 3335

  // Get level-two range part_ids
X
xy0 已提交
3336 3337
  static int get_range_part_ids(const int64_t part_id, const common::ObNewRange &range, const bool reverse,
      ObSubPartition *const *partition_array, const int64_t partition_num, common::ObIArray<int64_t> &part_ids);
O
oceanbase-admin 已提交
3338
  // The meaning of get_subpart_index can refer to get_hash_subpart_ids()
X
xy0 已提交
3339 3340
  static int get_range_part_ids(const int64_t part_id, const common::ObNewRow &row,
      ObSubPartition *const *partition_array, const int64_t partition_num, common::ObIArray<int64_t> &part_ids,
O
oceanbase-admin 已提交
3341 3342 3343
      bool get_subpart_index = false);

  // Get level-two range part_ids
X
xy0 已提交
3344 3345
  static int get_list_part_ids(const int64_t part_id, const common::ObNewRange &range, const bool reverse,
      ObSubPartition *const *partition_array, const int64_t partition_num, common::ObIArray<int64_t> &part_ids);
O
oceanbase-admin 已提交
3346 3347

  // The meaning of get_subpart_index can refer to get_hash_subpart_ids()
X
xy0 已提交
3348 3349
  static int get_list_part_ids(const int64_t part_id, const common::ObNewRow &row,
      ObSubPartition *const *partition_array, const int64_t partition_num, common::ObIArray<int64_t> &part_ids,
O
oceanbase-admin 已提交
3350 3351 3352 3353 3354
      bool get_subpart_index = false);

  // According to the given hash value val and partition number part_num,
  // distinguish between oracle and mysql modes to calculate which partition this fold falls on
  // This interface is called at get_hash_part_idxs, get_hash_subpart_ids, etc.
X
xy0 已提交
3355
  static int calc_hash_part_idx(const uint64_t val, const int64_t part_num, int64_t &partition_idx);
O
oceanbase-admin 已提交
3356 3357
  template <typename T>
  static int get_range_part_idx(
X
xy0 已提交
3358
      const common::ObObj &obj, T *const *part_array, const int64_t part_num, int64_t &part_idx);
O
oceanbase-admin 已提交
3359 3360

  template <typename T>
X
xy0 已提交
3361 3362
  static int get_range_part_bounds(T *const *part_array, const int64_t part_num, const int64_t part_idx,
      common::ObObj &lower_bound, common::ObObj &upper_bound);
O
oceanbase-admin 已提交
3363 3364

  // Get all part idxs of part num
X
xy0 已提交
3365
  static int get_all_part(const ObTableSchema &table_schema, int64_t part_num, common::ObIArray<int64_t> &part_ids);
O
oceanbase-admin 已提交
3366 3367

  // Convert rowkey to sql literal for show
X
xy0 已提交
3368 3369
  static int convert_rowkey_to_sql_literal(const common::ObRowkey &rowkey, char *buf, const int64_t buf_len,
      int64_t &pos, bool print_collation, const common::ObTimeZoneInfo *tz_info);
O
oceanbase-admin 已提交
3370
  // Used to display the defined value of the LIST partition
X
xy0 已提交
3371 3372 3373 3374
  static int convert_rows_to_sql_literal(const common::ObIArray<common::ObNewRow> &rows, char *buf,
      const int64_t buf_len, int64_t &pos, bool print_collation, const common::ObTimeZoneInfo *tz_info);
  static int print_oracle_datetime_literal(const common::ObObj &tmp_obj, char *buf, const int64_t buf_len, int64_t &pos,
      const common::ObTimeZoneInfo *tz_info);
O
oceanbase-admin 已提交
3375 3376

  // Convert rowkey's serialize buff to hex.For record all rowkey info.
X
xy0 已提交
3377
  static int convert_rowkey_to_hex(const common::ObRowkey &rowkey, char *buf, const int64_t buf_len, int64_t &pos);
O
oceanbase-admin 已提交
3378
  static int convert_rows_to_hex(
X
xy0 已提交
3379
      const common::ObIArray<common::ObNewRow> &rows, char *buf, const int64_t buf_len, int64_t &pos);
O
oceanbase-admin 已提交
3380 3381 3382 3383

  // Get partition start with start_part rowkey, return start_pos
  template <typename T>
  static int get_start(
X
xy0 已提交
3384
      const T *const *partition_array, const int64_t partition_num, const T &start_part, int64_t &start_pos);
O
oceanbase-admin 已提交
3385 3386 3387

  // Get partition end with end_part rowkey, return end_pos
  template <typename T>
X
xy0 已提交
3388 3389
  static int get_end(const T *const *partition_array, const int64_t partition_num,
      const common::ObBorderFlag &border_flag, const T &end_part, int64_t &end_pos);
O
oceanbase-admin 已提交
3390 3391 3392 3393

  // check if partition value equal
  template <typename PARTITION>
  static int check_partition_value(
X
xy0 已提交
3394
      const PARTITION &l_part, const PARTITION &r_part, const ObPartitionFuncType part_type, bool &is_equal);
O
oceanbase-admin 已提交
3395

X
xy0 已提交
3396
  static bool is_types_equal_for_partition_check(const common::ObObjType &typ1, const common::ObObjType &type2);
O
oceanbase-admin 已提交
3397

G
gm 已提交
3398
private:
O
oceanbase-admin 已提交
3399 3400
  // Get level-one range part_ids
  // The meaning of get_part_index can refer to get_hash_part_ids()
X
xy0 已提交
3401 3402 3403
  static int get_range_part_ids(ObPartition &start, ObPartition &end, const common::ObBorderFlag &border_flag,
      const bool reverse, ObPartition *const *partition_array, const int64_t partition_num,
      common::ObIArray<int64_t> &part_ids, bool get_part_index = false);
O
oceanbase-admin 已提交
3404 3405
  // Get level-two range part_ids
  // The meaning of get_subpart_index can refer to get_hash_subpart_ids()
X
xy0 已提交
3406 3407 3408
  static int get_range_part_ids(ObSubPartition &start_bound, ObSubPartition &end_bound,
      const common::ObBorderFlag &border_flag, const bool reverse, ObSubPartition *const *partition_array,
      const int64_t partition_num, common::ObIArray<int64_t> &part_ids, bool get_subpart_index = false);
O
oceanbase-admin 已提交
3409 3410 3411 3412
};

template <typename T>
int ObPartitionUtils::get_range_part_idx(
X
xy0 已提交
3413
    const common::ObObj &obj, T *const *part_array, const int64_t part_num, int64_t &part_idx)
O
oceanbase-admin 已提交
3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436
{
  int ret = common::OB_SUCCESS;
  if (obj.is_null()) {
    part_idx = 0;
  } else {
    common::ObObj buf_obj = obj;
    common::ObRowkey row_key(&buf_obj, 1);
    T end_key;
    end_key.high_bound_val_ = row_key;
    common::ObBorderFlag border_flag;
    border_flag.set_inclusive_start();
    border_flag.set_inclusive_end();
    if (part_num < 1) {
      ret = common::OB_ERR_UNEXPECTED;
      SHARE_SCHEMA_LOG(WARN, "Unexpected partition num", K(part_num), K(ret));
    } else if (OB_FAIL(get_end(part_array, part_num, border_flag, end_key, part_idx))) {
      SHARE_SCHEMA_LOG(WARN, "Failed to get end partition idx", K(end_key), K(ret));
    }
  }
  return ret;
}

template <typename T>
X
xy0 已提交
3437 3438
int ObPartitionUtils::get_range_part_bounds(T *const *part_array, const int64_t part_num, const int64_t part_idx,
    common::ObObj &lower_bound, common::ObObj &upper_bound)
O
oceanbase-admin 已提交
3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471
{
  int ret = common::OB_SUCCESS;
  if (OB_UNLIKELY(!(0 <= part_idx && part_idx < part_num))) {
    ret = common::OB_ERR_UNEXPECTED;
    SHARE_SCHEMA_LOG(WARN, "invalid part_num or part_idx", K(part_num), K(part_idx), K(ret));
  } else if (OB_ISNULL(part_array[part_idx])) {
    ret = common::OB_ERR_UNEXPECTED;
    SHARE_SCHEMA_LOG(WARN, "part_array[part_idx] is null", K(part_idx), K(ret));
  } else if (part_array[part_idx]->get_high_bound_val().get_obj_cnt() > 1) {
    ret = common::OB_NOT_IMPLEMENT;
    SHARE_SCHEMA_LOG(WARN, "range columns count should be 1 now", K(ret));
  } else {
    if (part_idx > 0) {
      if (OB_ISNULL(part_array[part_idx - 1])) {
        ret = common::OB_ERR_UNEXPECTED;
        SHARE_SCHEMA_LOG(WARN, "part_array[part_idx - 1] is null", K(part_idx - 1), K(ret));
      } else {
        lower_bound = part_array[part_idx - 1]->get_high_bound_val().get_obj_ptr()[0];
      }
    } else {
      lower_bound.set_min_value();
    }
    if (part_idx < part_num - 1) {
      upper_bound = part_array[part_idx]->get_high_bound_val().get_obj_ptr()[0];
    } else {
      upper_bound.set_max_value();
    }
  }
  return ret;
}

template <typename T>
int ObPartitionUtils::get_start(
X
xy0 已提交
3472
    const T *const *partition_array, const int64_t partition_num, const T &start_part, int64_t &start_pos)
O
oceanbase-admin 已提交
3473 3474 3475 3476 3477 3478
{
  int ret = common::OB_SUCCESS;
  if (OB_ISNULL(partition_array)) {
    ret = common::OB_ERR_UNEXPECTED;
    SHARE_SCHEMA_LOG(WARN, "Partition array should not be NULL", K(ret));
  } else {
X
xy0 已提交
3479
    const T *const *result =
O
oceanbase-admin 已提交
3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490
        std::upper_bound(partition_array, partition_array + partition_num, &start_part, T::less_than);
    start_pos = result - partition_array;
    if (start_pos < 0) {
      ret = common::OB_ERR_UNEXPECTED;
      SHARE_SCHEMA_LOG(WARN, "Start pos error", K(partition_num), K(ret));
    }
  }
  return ret;
}

template <typename T>
X
xy0 已提交
3491 3492
int ObPartitionUtils::get_end(const T *const *partition_array, const int64_t partition_num,
    const common::ObBorderFlag &border_flag, const T &end_part, int64_t &end_pos)
O
oceanbase-admin 已提交
3493 3494 3495 3496 3497 3498
{
  int ret = common::OB_SUCCESS;
  if (OB_ISNULL(partition_array)) {
    ret = common::OB_ERR_UNEXPECTED;
    SHARE_SCHEMA_LOG(WARN, "Input partition array should not be NULL", K(ret));
  } else {
X
xy0 已提交
3499
    const T *const *result =
O
oceanbase-admin 已提交
3500 3501 3502 3503 3504 3505 3506 3507 3508
        std::lower_bound(partition_array, partition_array + partition_num, &end_part, T::less_than);
    int64_t pos = result - partition_array;
    if (pos >= partition_num) {
      end_pos = partition_num - 1;
    } else if (OB_ISNULL(result) || OB_ISNULL(*result)) {
      ret = common::OB_ERR_UNEXPECTED;
      SHARE_SCHEMA_LOG(WARN, "result should not be NULL", K(ret), K(result));
    } else {
      common::ObNewRow lrow;
X
xy0 已提交
3509
      lrow.cells_ = const_cast<common::ObObj *>((*result)->high_bound_val_.get_obj_ptr());
O
oceanbase-admin 已提交
3510 3511 3512 3513
      lrow.count_ = (*result)->high_bound_val_.get_obj_cnt();
      lrow.projector_ = (*result)->projector_;
      lrow.projector_size_ = (*result)->projector_size_;
      common::ObNewRow rrow;
X
xy0 已提交
3514
      rrow.cells_ = const_cast<common::ObObj *>(end_part.high_bound_val_.get_obj_ptr());
O
oceanbase-admin 已提交
3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565
      rrow.count_ = end_part.high_bound_val_.get_obj_cnt();
      rrow.projector_ = end_part.projector_;
      rrow.projector_size_ = end_part.projector_size_;
      int cmp = 0;
      if (common::OB_SUCCESS != common::ObRowUtil::compare_row(lrow, rrow, cmp)) {
        SHARE_SCHEMA_LOG(ERROR, "lhs or rhs is invalid");
      }
      if (0 == cmp) {
        if (pos == partition_num - 1) {
          end_pos = partition_num - 1;
        } else {
          if (border_flag.inclusive_end()) {
            end_pos = pos + 1;
          } else {
            end_pos = pos;
          }
        }
      } else {
        end_pos = pos;
      }
    }
  }
  return ret;
}

// TODO Put the partition description information together
// struct ObPartitionDesc
//{
//  OB_UNIS_VERSION(1);
//  int get_part(const common::ObNewRange &range,
//               bool reverse,
//               common::ObIArray<int64_t> &part_ids);
//
//  int get_sub_part(uint64_t table_id,
//                   int64_t part_id,
//                   const common::ObNewRange &range,
//                   bool reverse,
//                   common::ObIArray<int64_t> &part_ids);
//  //first partition
//  ObPartition **partition_array_;
//  int64_t partition_array_capacity_;
//  int64_t partition_num_;
//  //second partition
//  ObSubPartition **subpartition_array_;
//  int64_t subpartition_array_capacity_;
//  int64_t subpartition_num_;
//};

class ObViewSchema : public ObSchema {
  OB_UNIS_VERSION(1);

G
gm 已提交
3566
public:
O
oceanbase-admin 已提交
3567
  ObViewSchema();
X
xy0 已提交
3568
  explicit ObViewSchema(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
3569
  virtual ~ObViewSchema();
X
xy0 已提交
3570 3571 3572 3573
  ObViewSchema(const ObViewSchema &src_schema);
  ObViewSchema &operator=(const ObViewSchema &src_schema);
  bool operator==(const ObViewSchema &other) const;
  bool operator!=(const ObViewSchema &other) const;
O
oceanbase-admin 已提交
3574

X
xy0 已提交
3575
  inline int set_view_definition(const char *view_definition)
O
oceanbase-admin 已提交
3576 3577 3578
  {
    return deep_copy_str(view_definition, view_definition_);
  }
X
xy0 已提交
3579
  inline int set_view_definition(const common::ObString &view_definition)
O
oceanbase-admin 已提交
3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605
  {
    return deep_copy_str(view_definition, view_definition_);
  }
  inline void set_view_check_option(const ViewCheckOption option)
  {
    view_check_option_ = option;
  }
  inline void set_view_is_updatable(const bool is_updatable)
  {
    view_is_updatable_ = is_updatable;
  }
  inline void set_materialized(const bool materialized)
  {
    materialized_ = materialized;
  }
  inline void set_character_set_client(const common::ObCharsetType character_set_client)
  {
    character_set_client_ = character_set_client;
  }
  inline void set_collation_connection(const common::ObCollationType collation_connection)
  {
    collation_connection_ = collation_connection;
  }

  // view_definition_ is defined using utf8, for sql resolve please use
  // ObSQLUtils::generate_view_definition_for_resolve
X
xy0 已提交
3606
  inline const common::ObString &get_view_definition_str() const
O
oceanbase-admin 已提交
3607 3608 3609
  {
    return view_definition_;
  }
X
xy0 已提交
3610
  inline const char *get_view_definition() const
O
oceanbase-admin 已提交
3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642
  {
    return extract_str(view_definition_);
  }
  inline ViewCheckOption get_view_check_option() const
  {
    return view_check_option_;
  }
  inline bool get_view_is_updatable() const
  {
    return view_is_updatable_;
  }
  inline bool get_materialized() const
  {
    return materialized_;
  }
  inline common::ObCharsetType get_character_set_client() const
  {
    return character_set_client_;
  }
  inline common::ObCollationType get_collation_connection() const
  {
    return collation_connection_;
  }

  int64_t get_convert_size() const;
  virtual bool is_valid() const;
  virtual void reset();

  TO_STRING_KV(N_VIEW_DEFINITION, view_definition_, N_CHECK_OPTION, ob_view_check_option_str(view_check_option_),
      N_IS_UPDATABLE, STR_BOOL(view_is_updatable_), N_IS_MATERIALIZED, STR_BOOL(materialized_),
      K_(character_set_client), K_(collation_connection));

G
gm 已提交
3643
private:
O
oceanbase-admin 已提交
3644 3645 3646 3647 3648 3649 3650 3651 3652
  common::ObString view_definition_;
  ViewCheckOption view_check_option_;
  bool view_is_updatable_;
  bool materialized_;
  common::ObCharsetType character_set_client_;
  common::ObCollationType collation_connection_;
};

class ObColumnSchemaHashWrapper {
G
gm 已提交
3653
public:
O
oceanbase-admin 已提交
3654 3655
  ObColumnSchemaHashWrapper()
  {}
X
xy0 已提交
3656
  explicit ObColumnSchemaHashWrapper(const common::ObString &str) : column_name_(str)
O
oceanbase-admin 已提交
3657 3658 3659
  {}
  ~ObColumnSchemaHashWrapper()
  {}
X
xy0 已提交
3660
  void set_name(const common::ObString &str)
O
oceanbase-admin 已提交
3661 3662 3663
  {
    column_name_ = str;
  }
X
xy0 已提交
3664
  inline bool operator==(const ObColumnSchemaHashWrapper &other) const
O
oceanbase-admin 已提交
3665 3666 3667 3668 3669 3670 3671 3672
  {
    ObCompareNameWithTenantID name_cmp;
    return (0 == name_cmp.compare(column_name_, other.column_name_));
  }
  inline uint64_t hash() const;
  common::ObString column_name_;
};
class ObColumnSchemaWrapper {
G
gm 已提交
3673
public:
O
oceanbase-admin 已提交
3674 3675
  ObColumnSchemaWrapper() : column_name_(), prefix_len_(0)
  {}
X
xy0 已提交
3676
  explicit ObColumnSchemaWrapper(const common::ObString &str, int32_t prefix_len)
O
oceanbase-admin 已提交
3677 3678 3679 3680
      : column_name_(str), prefix_len_(prefix_len)
  {}
  ~ObColumnSchemaWrapper()
  {}
X
xy0 已提交
3681
  void set_name(const common::ObString &str)
O
oceanbase-admin 已提交
3682 3683 3684
  {
    column_name_ = str;
  }
X
xy0 已提交
3685
  inline bool name_equal(const ObColumnSchemaWrapper &other) const
O
oceanbase-admin 已提交
3686 3687 3688 3689
  {
    ObCompareNameWithTenantID name_cmp;
    return (0 == name_cmp.compare(column_name_, other.column_name_));
  }
X
xy0 已提交
3690
  inline bool all_equal(const ObColumnSchemaWrapper &other) const
O
oceanbase-admin 已提交
3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713
  {
    ObCompareNameWithTenantID name_cmp;
    return (0 == name_cmp.compare(column_name_, other.column_name_)) && prefix_len_ == other.prefix_len_;
  }
  TO_STRING_KV(K_(column_name), K_(prefix_len));

  common::ObString column_name_;
  int32_t prefix_len_;
};
typedef ObColumnSchemaHashWrapper ObColumnNameHashWrapper;
typedef ObColumnSchemaHashWrapper ObIndexNameHashWrapper;
typedef ObColumnSchemaHashWrapper ObPartitionNameHashWrapper;
typedef ObColumnSchemaHashWrapper ObForeignKeyNameHashWrapper;
typedef ObColumnSchemaWrapper ObColumnNameWrapper;

inline uint64_t ObColumnSchemaHashWrapper::hash() const
{
  uint64_t hash_ret = 0;
  // case insensitive
  hash_ret = common::ObCharset::hash(common::CS_TYPE_UTF8MB4_GENERAL_CI, column_name_, hash_ret);
  return hash_ret;
}
class ObIndexSchemaHashWrapper {
G
gm 已提交
3714
public:
X
xf0 已提交
3715 3716
  ObIndexSchemaHashWrapper()
      : tenant_id_(common::OB_INVALID_ID), database_id_(common::OB_INVALID_ID), data_table_id_(common::OB_INVALID_ID)
O
oceanbase-admin 已提交
3717
  {}
X
xf0 已提交
3718
  ObIndexSchemaHashWrapper(
X
xy0 已提交
3719
      uint64_t tenant_id, const uint64_t database_id, const uint64_t data_table_id, const common::ObString &index_name)
X
xf0 已提交
3720
      : tenant_id_(tenant_id), database_id_(database_id), data_table_id_(data_table_id), index_name_(index_name)
O
oceanbase-admin 已提交
3721 3722 3723 3724
  {}
  ~ObIndexSchemaHashWrapper()
  {}
  inline uint64_t hash() const;
X
xy0 已提交
3725
  inline bool operator==(const ObIndexSchemaHashWrapper &rv) const;
O
oceanbase-admin 已提交
3726 3727 3728 3729 3730 3731 3732 3733 3734

  inline uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
  inline uint64_t get_database_id() const
  {
    return database_id_;
  }
X
xf0 已提交
3735 3736 3737 3738
  inline uint64_t get_data_table_id() const
  {
    return data_table_id_;
  }
X
xy0 已提交
3739
  inline const common::ObString &get_index_name() const
O
oceanbase-admin 已提交
3740 3741 3742 3743
  {
    return index_name_;
  }

G
gm 已提交
3744
private:
O
oceanbase-admin 已提交
3745 3746
  uint64_t tenant_id_;
  uint64_t database_id_;
X
xf0 已提交
3747
  uint64_t data_table_id_;  // only for mysql mode
O
oceanbase-admin 已提交
3748 3749 3750 3751 3752 3753 3754 3755
  common::ObString index_name_;
};

inline uint64_t ObIndexSchemaHashWrapper::hash() const
{
  uint64_t hash_ret = 0;
  hash_ret = common::murmurhash(&tenant_id_, sizeof(uint64_t), 0);
  hash_ret = common::murmurhash(&database_id_, sizeof(uint64_t), hash_ret);
X
xf0 已提交
3756
  hash_ret = common::murmurhash(&data_table_id_, sizeof(uint64_t), hash_ret);
O
oceanbase-admin 已提交
3757 3758 3759 3760 3761
  // case insensitive
  hash_ret = common::ObCharset::hash(common::CS_TYPE_UTF8MB4_GENERAL_CI, index_name_, hash_ret);
  return hash_ret;
}

X
xy0 已提交
3762
inline bool ObIndexSchemaHashWrapper::operator==(const ObIndexSchemaHashWrapper &rv) const
O
oceanbase-admin 已提交
3763 3764 3765 3766
{
  // mysql case insensitive
  // oracle case sensitive
  ObCompareNameWithTenantID name_cmp(tenant_id_);
X
xf0 已提交
3767
  return (tenant_id_ == rv.tenant_id_) && (database_id_ == rv.database_id_) && (data_table_id_ == rv.data_table_id_) &&
O
oceanbase-admin 已提交
3768 3769 3770 3771
         (0 == name_cmp.compare(index_name_, rv.index_name_));
}

class ObTableSchemaHashWrapper {
G
gm 已提交
3772
public:
O
oceanbase-admin 已提交
3773 3774 3775 3776 3777 3778 3779
  ObTableSchemaHashWrapper()
      : tenant_id_(common::OB_INVALID_ID),
        database_id_(common::OB_INVALID_ID),
        session_id_(common::OB_INVALID_ID),
        name_case_mode_(common::OB_NAME_CASE_INVALID)
  {}
  ObTableSchemaHashWrapper(const uint64_t tenant_id, const uint64_t database_id, const uint64_t session_id,
X
xy0 已提交
3780
      const common::ObNameCaseMode mode, const common::ObString &table_name)
O
oceanbase-admin 已提交
3781 3782 3783 3784 3785 3786 3787 3788 3789
      : tenant_id_(tenant_id),
        database_id_(database_id),
        session_id_(session_id),
        name_case_mode_(mode),
        table_name_(table_name)
  {}
  ~ObTableSchemaHashWrapper()
  {}
  inline uint64_t hash() const;
X
xy0 已提交
3790
  bool operator==(const ObTableSchemaHashWrapper &rv) const;
O
oceanbase-admin 已提交
3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803

  inline uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
  inline uint64_t get_database_id() const
  {
    return database_id_;
  }
  inline uint64_t get_session_id() const
  {
    return session_id_;
  }
X
xy0 已提交
3804
  inline const common::ObString &get_table_name() const
O
oceanbase-admin 已提交
3805 3806 3807 3808
  {
    return table_name_;
  }

G
gm 已提交
3809
private:
O
oceanbase-admin 已提交
3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827
  uint64_t tenant_id_;
  uint64_t database_id_;
  uint64_t session_id_;
  common::ObNameCaseMode name_case_mode_;
  common::ObString table_name_;
};

inline uint64_t ObTableSchemaHashWrapper::hash() const
{
  uint64_t hash_ret = 0;
  hash_ret = common::murmurhash(&tenant_id_, sizeof(uint64_t), 0);
  hash_ret = common::murmurhash(&database_id_, sizeof(uint64_t), hash_ret);
  common::ObCollationType cs_type = ObSchema::get_cs_type_with_cmp_mode(name_case_mode_);
  hash_ret = common::ObCharset::hash(cs_type, table_name_, hash_ret);
  return hash_ret;
}

// See ObSchemaMgr::get_table_schema comment for session visibility judgment
X
xy0 已提交
3828
inline bool ObTableSchemaHashWrapper::operator==(const ObTableSchemaHashWrapper &rv) const
O
oceanbase-admin 已提交
3829 3830 3831 3832 3833 3834 3835 3836 3837
{
  ObCompareNameWithTenantID name_cmp(tenant_id_, name_case_mode_, database_id_);
  return (tenant_id_ == rv.tenant_id_) && (database_id_ == rv.database_id_) &&
         (name_case_mode_ == rv.name_case_mode_) &&
         (session_id_ == rv.session_id_ || common::OB_INVALID_ID == rv.session_id_) &&
         (0 == name_cmp.compare(table_name_, rv.table_name_));
}

class ObDatabaseSchemaHashWrapper {
G
gm 已提交
3838
public:
O
oceanbase-admin 已提交
3839 3840 3841
  ObDatabaseSchemaHashWrapper() : tenant_id_(common::OB_INVALID_ID), name_case_mode_(common::OB_NAME_CASE_INVALID)
  {}
  ObDatabaseSchemaHashWrapper(
X
xy0 已提交
3842
      const uint64_t tenant_id, const common::ObNameCaseMode mode, const common::ObString &database_name)
O
oceanbase-admin 已提交
3843 3844 3845 3846 3847
      : tenant_id_(tenant_id), name_case_mode_(mode), database_name_(database_name)
  {}
  ~ObDatabaseSchemaHashWrapper()
  {}
  inline uint64_t hash() const;
X
xy0 已提交
3848
  inline bool operator==(const ObDatabaseSchemaHashWrapper &rv) const;
O
oceanbase-admin 已提交
3849 3850 3851 3852 3853 3854 3855 3856 3857

  inline uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
  inline common::ObNameCaseMode get_name_case_mode() const
  {
    return name_case_mode_;
  }
X
xy0 已提交
3858
  inline const common::ObString &get_database_name() const
O
oceanbase-admin 已提交
3859 3860 3861 3862
  {
    return database_name_;
  }

G
gm 已提交
3863
private:
O
oceanbase-admin 已提交
3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877
  uint64_t tenant_id_;
  common::ObNameCaseMode name_case_mode_;
  common::ObString database_name_;
};

inline uint64_t ObDatabaseSchemaHashWrapper::hash() const
{
  uint64_t hash_ret = 0;
  hash_ret = common::murmurhash(&tenant_id_, sizeof(uint64_t), 0);
  common::ObCollationType cs_type = ObSchema::get_cs_type_with_cmp_mode(name_case_mode_);
  hash_ret = common::ObCharset::hash(cs_type, database_name_, hash_ret);
  return hash_ret;
}

X
xy0 已提交
3878
inline bool ObDatabaseSchemaHashWrapper::operator==(const ObDatabaseSchemaHashWrapper &rv) const
O
oceanbase-admin 已提交
3879 3880 3881 3882 3883 3884 3885
{
  ObCompareNameWithTenantID name_cmp(tenant_id_, name_case_mode_);
  return (tenant_id_ == rv.tenant_id_) && (name_case_mode_ == rv.name_case_mode_) &&
         (0 == name_cmp.compare(database_name_, rv.database_name_));
}

class ObTablegroupSchemaHashWrapper {
G
gm 已提交
3886
public:
O
oceanbase-admin 已提交
3887 3888
  ObTablegroupSchemaHashWrapper() : tenant_id_(common::OB_INVALID_ID)
  {}
X
xy0 已提交
3889
  ObTablegroupSchemaHashWrapper(uint64_t tenant_id, const common::ObString &tablegroup_name)
O
oceanbase-admin 已提交
3890 3891 3892 3893 3894
      : tenant_id_(tenant_id), tablegroup_name_(tablegroup_name)
  {}
  ~ObTablegroupSchemaHashWrapper()
  {}
  inline uint64_t hash() const;
X
xy0 已提交
3895
  inline bool operator==(const ObTablegroupSchemaHashWrapper &rv) const;
O
oceanbase-admin 已提交
3896 3897 3898 3899 3900

  inline uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
X
xy0 已提交
3901
  inline const common::ObString &get_tablegroup_name() const
O
oceanbase-admin 已提交
3902 3903 3904 3905
  {
    return tablegroup_name_;
  }

G
gm 已提交
3906
private:
O
oceanbase-admin 已提交
3907 3908 3909 3910 3911
  uint64_t tenant_id_;
  common::ObString tablegroup_name_;
};

class ObForeignKeyInfoHashWrapper {
G
gm 已提交
3912
public:
O
oceanbase-admin 已提交
3913 3914 3915 3916 3917 3918
  ObForeignKeyInfoHashWrapper()
  {
    tenant_id_ = common::OB_INVALID_ID;
    database_id_ = common::OB_INVALID_ID;
    foreign_key_name_.assign_ptr("", 0);
  }
X
xy0 已提交
3919
  ObForeignKeyInfoHashWrapper(uint64_t tenant_id, const uint64_t database_id, const common::ObString &foreign_key_name)
O
oceanbase-admin 已提交
3920 3921 3922 3923 3924
      : tenant_id_(tenant_id), database_id_(database_id), foreign_key_name_(foreign_key_name)
  {}
  ~ObForeignKeyInfoHashWrapper()
  {}
  inline uint64_t hash() const;
X
xy0 已提交
3925
  inline bool operator==(const ObForeignKeyInfoHashWrapper &rv) const;
O
oceanbase-admin 已提交
3926 3927 3928 3929 3930 3931 3932 3933
  inline uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
  inline uint64_t get_database_id() const
  {
    return database_id_;
  }
X
xy0 已提交
3934
  inline const common::ObString &get_foreign_key_name() const
O
oceanbase-admin 已提交
3935 3936 3937 3938
  {
    return foreign_key_name_;
  }

G
gm 已提交
3939
private:
O
oceanbase-admin 已提交
3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954
  uint64_t tenant_id_;
  uint64_t database_id_;
  common::ObString foreign_key_name_;
};

inline uint64_t ObForeignKeyInfoHashWrapper::hash() const
{
  uint64_t hash_ret = 0;
  hash_ret = common::murmurhash(&tenant_id_, sizeof(uint64_t), 0);
  hash_ret = common::murmurhash(&database_id_, sizeof(uint64_t), hash_ret);
  // case insensitive
  hash_ret = common::ObCharset::hash(common::CS_TYPE_UTF8MB4_GENERAL_CI, foreign_key_name_, hash_ret);
  return hash_ret;
}

X
xy0 已提交
3955
inline bool ObForeignKeyInfoHashWrapper::operator==(const ObForeignKeyInfoHashWrapper &rv) const
O
oceanbase-admin 已提交
3956 3957 3958 3959 3960 3961 3962 3963 3964
{
  // mysql case insensitive
  // oracle case sensitive
  ObCompareNameWithTenantID name_cmp(tenant_id_);
  return (tenant_id_ == rv.tenant_id_) && (database_id_ == rv.database_id_) &&
         (0 == name_cmp.compare(foreign_key_name_, rv.foreign_key_name_));
}

class ObConstraintInfoHashWrapper {
G
gm 已提交
3965
public:
O
oceanbase-admin 已提交
3966 3967 3968 3969 3970 3971
  ObConstraintInfoHashWrapper()
  {
    tenant_id_ = common::OB_INVALID_ID;
    database_id_ = common::OB_INVALID_ID;
    constraint_name_.assign_ptr("", 0);
  }
X
xy0 已提交
3972
  ObConstraintInfoHashWrapper(uint64_t tenant_id, const uint64_t database_id, const common::ObString &constraint_name)
O
oceanbase-admin 已提交
3973 3974 3975 3976 3977
      : tenant_id_(tenant_id), database_id_(database_id), constraint_name_(constraint_name)
  {}
  ~ObConstraintInfoHashWrapper()
  {}
  inline uint64_t hash() const;
X
xy0 已提交
3978
  inline bool operator==(const ObConstraintInfoHashWrapper &rv) const;
O
oceanbase-admin 已提交
3979 3980 3981 3982 3983 3984 3985 3986
  inline uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
  inline uint64_t get_database_id() const
  {
    return database_id_;
  }
X
xy0 已提交
3987
  inline const common::ObString &get_constraint_name() const
O
oceanbase-admin 已提交
3988 3989 3990 3991
  {
    return constraint_name_;
  }

G
gm 已提交
3992
private:
O
oceanbase-admin 已提交
3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007
  uint64_t tenant_id_;
  uint64_t database_id_;
  common::ObString constraint_name_;
};

inline uint64_t ObConstraintInfoHashWrapper::hash() const
{
  uint64_t hash_ret = 0;
  hash_ret = common::murmurhash(&tenant_id_, sizeof(uint64_t), 0);
  hash_ret = common::murmurhash(&database_id_, sizeof(uint64_t), hash_ret);
  // case insensitive
  hash_ret = common::ObCharset::hash(common::CS_TYPE_UTF8MB4_GENERAL_CI, constraint_name_, hash_ret);
  return hash_ret;
}

X
xy0 已提交
4008
inline bool ObConstraintInfoHashWrapper::operator==(const ObConstraintInfoHashWrapper &rv) const
O
oceanbase-admin 已提交
4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024
{
  // mysql case insensitive
  // oracle case sensitive
  ObCompareNameWithTenantID name_cmp(tenant_id_);
  return (tenant_id_ == rv.tenant_id_) && (database_id_ == rv.database_id_) &&
         (0 == name_cmp.compare(constraint_name_, rv.constraint_name_));
}

inline uint64_t ObTablegroupSchemaHashWrapper::hash() const
{
  uint64_t hash_ret = 0;
  hash_ret = common::murmurhash(&tenant_id_, sizeof(uint64_t), 0);
  hash_ret = common::murmurhash(tablegroup_name_.ptr(), tablegroup_name_.length(), hash_ret);
  return hash_ret;
}

X
xy0 已提交
4025
inline bool ObTablegroupSchemaHashWrapper::operator==(const ObTablegroupSchemaHashWrapper &rv) const
O
oceanbase-admin 已提交
4026 4027 4028 4029 4030 4031 4032
{
  return (tenant_id_ == rv.tenant_id_) && (tablegroup_name_ == rv.tablegroup_name_);
}

struct ObTenantPlanBaselineId {
  OB_UNIS_VERSION(1);

G
gm 已提交
4033
public:
O
oceanbase-admin 已提交
4034 4035 4036 4037 4038
  ObTenantPlanBaselineId() : tenant_id_(common::OB_INVALID_ID), plan_baseline_id_(common::OB_INVALID_ID)
  {}
  ObTenantPlanBaselineId(const uint64_t tenant_id, const uint64_t plan_baseline_id)
      : tenant_id_(tenant_id), plan_baseline_id_(plan_baseline_id)
  {}
X
xy0 已提交
4039
  bool operator==(const ObTenantPlanBaselineId &rhs) const
O
oceanbase-admin 已提交
4040 4041 4042
  {
    return (tenant_id_ == rhs.tenant_id_) && (plan_baseline_id_ == rhs.plan_baseline_id_);
  }
X
xy0 已提交
4043
  bool operator!=(const ObTenantPlanBaselineId &rhs) const
O
oceanbase-admin 已提交
4044 4045 4046
  {
    return !(*this == rhs);
  }
X
xy0 已提交
4047
  bool operator<(const ObTenantPlanBaselineId &rhs) const
O
oceanbase-admin 已提交
4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073
  {
    bool bret = tenant_id_ < rhs.tenant_id_;
    if (tenant_id_ == rhs.tenant_id_) {
      bret = plan_baseline_id_ < rhs.plan_baseline_id_;
    }
    return bret;
  }
  inline uint64_t hash() const
  {
    uint64_t hash_ret = 0;
    hash_ret = common::murmurhash(&tenant_id_, sizeof(tenant_id_), 0);
    hash_ret = common::murmurhash(&plan_baseline_id_, sizeof(plan_baseline_id_), hash_ret);
    return hash_ret;
  }
  bool is_valid() const
  {
    return (tenant_id_ != common::OB_INVALID_ID) && (plan_baseline_id_ != common::OB_INVALID_ID);
  }
  TO_STRING_KV(K_(tenant_id), K_(plan_baseline_id));
  uint64_t tenant_id_;
  uint64_t plan_baseline_id_;
};

struct ObTenantOutlineId {
  OB_UNIS_VERSION(1);

G
gm 已提交
4074
public:
O
oceanbase-admin 已提交
4075 4076 4077 4078 4079
  ObTenantOutlineId() : tenant_id_(common::OB_INVALID_ID), outline_id_(common::OB_INVALID_ID)
  {}
  ObTenantOutlineId(const uint64_t tenant_id, const uint64_t outline_id)
      : tenant_id_(tenant_id), outline_id_(outline_id)
  {}
X
xy0 已提交
4080
  bool operator==(const ObTenantOutlineId &rhs) const
O
oceanbase-admin 已提交
4081 4082 4083
  {
    return (tenant_id_ == rhs.tenant_id_) && (outline_id_ == rhs.outline_id_);
  }
X
xy0 已提交
4084
  bool operator!=(const ObTenantOutlineId &rhs) const
O
oceanbase-admin 已提交
4085 4086 4087
  {
    return !(*this == rhs);
  }
X
xy0 已提交
4088
  bool operator<(const ObTenantOutlineId &rhs) const
O
oceanbase-admin 已提交
4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115
  {
    bool bret = tenant_id_ < rhs.tenant_id_;
    if (tenant_id_ == rhs.tenant_id_) {
      bret = outline_id_ < rhs.outline_id_;
    }
    return bret;
  }
  inline uint64_t hash() const
  {
    uint64_t hash_ret = 0;
    hash_ret = common::murmurhash(&tenant_id_, sizeof(tenant_id_), 0);
    hash_ret = common::murmurhash(&outline_id_, sizeof(outline_id_), hash_ret);
    return hash_ret;
  }
  bool is_valid() const
  {
    return (tenant_id_ != common::OB_INVALID_ID) && (outline_id_ != common::OB_INVALID_ID);
  }
  TO_STRING_KV(K_(tenant_id), K_(outline_id));
  uint64_t tenant_id_;
  uint64_t outline_id_;
};

// For managing privilege
struct ObTenantUserId {
  OB_UNIS_VERSION(1);

G
gm 已提交
4116
public:
O
oceanbase-admin 已提交
4117 4118 4119 4120
  ObTenantUserId() : tenant_id_(common::OB_INVALID_ID), user_id_(common::OB_INVALID_ID)
  {}
  ObTenantUserId(const uint64_t tenant_id, const uint64_t user_id) : tenant_id_(tenant_id), user_id_(user_id)
  {}
X
xy0 已提交
4121
  bool operator==(const ObTenantUserId &rhs) const
O
oceanbase-admin 已提交
4122 4123 4124
  {
    return (tenant_id_ == rhs.tenant_id_) && (user_id_ == rhs.user_id_);
  }
X
xy0 已提交
4125
  bool operator!=(const ObTenantUserId &rhs) const
O
oceanbase-admin 已提交
4126 4127 4128
  {
    return !(*this == rhs);
  }
X
xy0 已提交
4129
  bool operator<(const ObTenantUserId &rhs) const
O
oceanbase-admin 已提交
4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156
  {
    bool bret = tenant_id_ < rhs.tenant_id_;
    if (tenant_id_ == rhs.tenant_id_) {
      bret = user_id_ < rhs.user_id_;
    }
    return bret;
  }
  inline uint64_t hash() const
  {
    uint64_t hash_ret = 0;
    hash_ret = common::murmurhash(&tenant_id_, sizeof(tenant_id_), 0);
    hash_ret = common::murmurhash(&user_id_, sizeof(user_id_), hash_ret);
    return hash_ret;
  }
  bool is_valid() const
  {
    return (tenant_id_ != common::OB_INVALID_ID) && (user_id_ != common::OB_INVALID_ID);
  }
  TO_STRING_KV(K_(tenant_id), K_(user_id));
  uint64_t tenant_id_;
  uint64_t user_id_;
};

// For managing privilege
struct ObTenantUrObjId {
  OB_UNIS_VERSION(1);

G
gm 已提交
4157
public:
O
oceanbase-admin 已提交
4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168
  ObTenantUrObjId()
      : tenant_id_(common::OB_INVALID_ID),
        grantee_id_(common::OB_INVALID_ID),
        obj_id_(common::OB_INVALID_ID),
        obj_type_(common::OB_INVALID_ID),
        col_id_(common::OB_INVALID_ID)
  {}
  ObTenantUrObjId(const uint64_t tenant_id, const uint64_t grantee_id, const uint64_t obj_id, const uint64_t obj_type,
      const uint64_t col_id)
      : tenant_id_(tenant_id), grantee_id_(grantee_id), obj_id_(obj_id), obj_type_(obj_type), col_id_(col_id)
  {}
X
xy0 已提交
4169
  bool operator==(const ObTenantUrObjId &rhs) const
O
oceanbase-admin 已提交
4170 4171 4172 4173
  {
    return (tenant_id_ == rhs.tenant_id_) && (grantee_id_ == rhs.grantee_id_) && (obj_id_ == rhs.obj_id_) &&
           (obj_type_ == rhs.obj_type_) && (col_id_ == rhs.col_id_);
  }
X
xy0 已提交
4174
  bool operator!=(const ObTenantUrObjId &rhs) const
O
oceanbase-admin 已提交
4175 4176 4177
  {
    return !(*this == rhs);
  }
X
xy0 已提交
4178
  bool operator<(const ObTenantUrObjId &rhs) const
O
oceanbase-admin 已提交
4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219
  {
    bool bret = tenant_id_ < rhs.tenant_id_;
    if (tenant_id_ == rhs.tenant_id_) {
      bret = grantee_id_ < rhs.grantee_id_;
      if (false == bret && grantee_id_ == rhs.grantee_id_) {
        bret = obj_id_ < rhs.obj_id_;
        if (false == bret && obj_id_ == rhs.obj_id_) {
          bret = obj_type_ < rhs.obj_type_;
          if (false == bret && obj_type_ == rhs.obj_type_) {
            bret = col_id_ < rhs.col_id_;
          }
        }
      }
    }
    return bret;
  }
  inline uint64_t hash() const
  {
    uint64_t hash_ret = 0;
    hash_ret = common::murmurhash(&tenant_id_, sizeof(tenant_id_), 0);
    hash_ret = common::murmurhash(&grantee_id_, sizeof(grantee_id_), hash_ret);
    hash_ret = common::murmurhash(&obj_id_, sizeof(obj_id_), hash_ret);
    hash_ret = common::murmurhash(&obj_type_, sizeof(obj_type_), hash_ret);
    hash_ret = common::murmurhash(&col_id_, sizeof(col_id_), hash_ret);
    return hash_ret;
  }
  bool is_valid() const
  {
    return (tenant_id_ != common::OB_INVALID_ID) && (grantee_id_ != common::OB_INVALID_ID) &&
           (obj_id_ != common::OB_INVALID_ID) && (obj_type_ != common::OB_INVALID_ID) &&
           (col_id_ != common::OB_INVALID_ID);
  }
  TO_STRING_KV(K_(tenant_id), K_(grantee_id), K_(obj_id), K_(obj_type), K_(col_id));
  uint64_t tenant_id_;
  uint64_t grantee_id_;
  uint64_t obj_id_;
  uint64_t obj_type_;
  uint64_t col_id_;
};

class ObPrintPrivSet {
G
gm 已提交
4220
public:
O
oceanbase-admin 已提交
4221 4222 4223 4224 4225
  explicit ObPrintPrivSet(ObPrivSet priv_set) : priv_set_(priv_set)
  {}

  DECLARE_TO_STRING;

G
gm 已提交
4226
private:
O
oceanbase-admin 已提交
4227 4228 4229 4230
  ObPrivSet priv_set_;
};

class ObPrintPackedPrivArray {
G
gm 已提交
4231
public:
X
xy0 已提交
4232
  explicit ObPrintPackedPrivArray(const ObPackedPrivArray &packed_priv_array) : packed_priv_array_(packed_priv_array)
O
oceanbase-admin 已提交
4233 4234 4235 4236
  {}

  DECLARE_TO_STRING;

G
gm 已提交
4237
private:
X
xy0 已提交
4238
  const ObPackedPrivArray &packed_priv_array_;
O
oceanbase-admin 已提交
4239 4240 4241 4242 4243
};

class ObPriv {
  OB_UNIS_VERSION_V(1);

G
gm 已提交
4244
public:
O
oceanbase-admin 已提交
4245 4246 4247 4248 4249 4250 4251
  ObPriv()
      : tenant_id_(common::OB_INVALID_ID),
        user_id_(common::OB_INVALID_ID),
        schema_version_(1),
        priv_set_(0),
        priv_array_()
  {}
X
xy0 已提交
4252
  ObPriv(common::ObIAllocator *allocator)
O
oceanbase-admin 已提交
4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264
      : tenant_id_(common::OB_INVALID_ID),
        user_id_(common::OB_INVALID_ID),
        schema_version_(1),
        priv_set_(0),
        priv_array_(common::OB_MALLOC_NORMAL_BLOCK_SIZE, common::ModulePageAllocator(*allocator))
  {}
  ObPriv(const uint64_t tenant_id, const uint64_t user_id, const int64_t schema_version, const ObPrivSet priv_set)
      : tenant_id_(tenant_id), user_id_(user_id), schema_version_(schema_version), priv_set_(priv_set), priv_array_()
  {}

  virtual ~ObPriv()
  {}
X
xy0 已提交
4265 4266
  ObPriv &operator=(const ObPriv &other);
  static bool cmp_tenant_user_id(const ObPriv *lhs, const ObTenantUserId &tenant_user_id)
O
oceanbase-admin 已提交
4267 4268 4269
  {
    return (lhs->get_tenant_user_id() < tenant_user_id);
  }
X
xy0 已提交
4270
  static bool equal_tenant_user_id(const ObPriv *lhs, const ObTenantUserId &tenant_user_id)
O
oceanbase-admin 已提交
4271 4272 4273
  {
    return (lhs->get_tenant_user_id() == tenant_user_id);
  }
X
xy0 已提交
4274
  static bool cmp_tenant_id(const ObPriv *lhs, const uint64_t tenant_id)
O
oceanbase-admin 已提交
4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310
  {
    return (lhs->get_tenant_id() < tenant_id);
  }
  ObTenantUserId get_tenant_user_id() const
  {
    return ObTenantUserId(tenant_id_, user_id_);
  }

  inline void set_tenant_id(const uint64_t tenant_id)
  {
    tenant_id_ = tenant_id;
  }
  inline void set_user_id(const uint64_t user_id)
  {
    user_id_ = user_id;
  }
  inline void set_schema_version(const uint64_t schema_version)
  {
    schema_version_ = schema_version;
  }
  inline void set_priv(const ObPrivType priv)
  {
    priv_set_ |= priv;
  }
  inline void set_priv_set(const ObPrivSet priv_set)
  {
    priv_set_ = priv_set;
  }
  inline void reset_priv_set()
  {
    priv_set_ = 0;
  }
  inline void set_obj_privs(const ObPackedObjPriv obj_privs)
  {
    priv_set_ = obj_privs;
  }
X
xy0 已提交
4311
  int set_priv_array(const ObPackedPrivArray &other)
O
oceanbase-admin 已提交
4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335
  {
    return priv_array_.assign(other);
  }

  inline uint64_t get_tenant_id() const
  {
    return tenant_id_;
  };
  inline uint64_t get_user_id() const
  {
    return user_id_;
  }
  inline int64_t get_schema_version() const
  {
    return schema_version_;
  }
  inline ObPrivSet get_priv_set() const
  {
    return priv_set_;
  }
  inline ObPrivType get_priv(const ObPrivType priv) const
  {
    return priv_set_ & priv;
  }
X
xy0 已提交
4336
  inline const ObPackedPrivArray &get_priv_array() const
O
oceanbase-admin 已提交
4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352
  {
    return priv_array_;
  }
  inline ObPackedObjPriv get_obj_privs() const
  {
    return priv_set_;
  }
  virtual void reset();
  int64_t get_convert_size() const;
  virtual bool is_valid() const
  {
    return common::OB_INVALID_ID != tenant_id_ && common::OB_INVALID_ID != user_id_ && schema_version_ > 0;
  }

  TO_STRING_KV(K_(tenant_id), K_(user_id), K_(schema_version), "privileges", ObPrintPrivSet(priv_set_));

G
gm 已提交
4353
protected:
O
oceanbase-admin 已提交
4354 4355 4356 4357 4358 4359 4360 4361 4362 4363
  uint64_t tenant_id_;
  uint64_t user_id_;
  int64_t schema_version_;
  ObPrivSet priv_set_;
  // ObPrivSet ora_sys_priv_set_;
  ObPackedPrivArray priv_array_;
};

// Not used now
class ObUserInfoHashWrapper {
G
gm 已提交
4364
public:
O
oceanbase-admin 已提交
4365 4366
  ObUserInfoHashWrapper() : tenant_id_(common::OB_INVALID_ID)
  {}
X
xy0 已提交
4367
  ObUserInfoHashWrapper(uint64_t tenant_id, const common::ObString &user_name)
O
oceanbase-admin 已提交
4368 4369 4370 4371 4372
      : tenant_id_(tenant_id), user_name_(user_name)
  {}
  ~ObUserInfoHashWrapper()
  {}
  inline uint64_t hash() const;
X
xy0 已提交
4373
  inline bool operator==(const ObUserInfoHashWrapper &rv) const;
O
oceanbase-admin 已提交
4374 4375 4376 4377 4378

  inline uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
X
xy0 已提交
4379
  inline const common::ObString &get_user_name() const
O
oceanbase-admin 已提交
4380 4381 4382 4383
  {
    return user_name_;
  }

G
gm 已提交
4384
private:
O
oceanbase-admin 已提交
4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396
  uint64_t tenant_id_;
  common::ObString user_name_;
};

inline uint64_t ObUserInfoHashWrapper::hash() const
{
  uint64_t hash_ret = 0;
  hash_ret = common::murmurhash(&tenant_id_, sizeof(uint64_t), 0);
  hash_ret = common::murmurhash(user_name_.ptr(), user_name_.length(), hash_ret);
  return hash_ret;
}

X
xy0 已提交
4397
inline bool ObUserInfoHashWrapper::operator==(const ObUserInfoHashWrapper &other) const
O
oceanbase-admin 已提交
4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411
{
  return (tenant_id_ == other.tenant_id_) && (user_name_ == other.user_name_);
}

enum class ObSSLType : int {
  SSL_TYPE_NOT_SPECIFIED = 0,
  SSL_TYPE_NONE,
  SSL_TYPE_ANY,
  SSL_TYPE_X509,
  SSL_TYPE_SPECIFIED,
  SSL_TYPE_MAX
};

common::ObString get_ssl_type_string(const ObSSLType ssl_type);
X
xy0 已提交
4412
ObSSLType get_ssl_type_from_string(const common::ObString &ssl_type_str);
O
oceanbase-admin 已提交
4413 4414 4415 4416 4417 4418 4419 4420

enum class ObSSLSpecifiedType : int {
  SSL_SPEC_TYPE_CIPHER = 0,
  SSL_SPEC_TYPE_ISSUER,
  SSL_SPEC_TYPE_SUBJECT,
  SSL_SPEC_TYPE_MAX
};

X
xy0 已提交
4421
const char *get_ssl_spec_type_str(const ObSSLSpecifiedType ssl_spec_type);
O
oceanbase-admin 已提交
4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434

enum ObUserType {
  OB_USER = 0,
  OB_ROLE,
  OB_TYPE_MAX,
};

#define ADMIN_OPTION_SHIFT 0
#define DISABLE_FLAG_SHIFT 1

class ObUserInfo : public ObSchema, public ObPriv {
  OB_UNIS_VERSION(1);

G
gm 已提交
4435
public:
O
oceanbase-admin 已提交
4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452
  ObUserInfo()
      : ObSchema(),
        ObPriv(),
        user_name_(),
        host_name_(),
        passwd_(),
        info_(),
        locked_(false),
        ssl_type_(ObSSLType::SSL_TYPE_NOT_SPECIFIED),
        ssl_cipher_(),
        x509_issuer_(),
        x509_subject_(),
        type_(OB_USER),
        grantee_id_array_(),
        role_id_array_(),
        profile_id_(common::OB_INVALID_ID),
        password_last_changed_timestamp_(common::OB_INVALID_TIMESTAMP),
4453 4454 4455
        role_id_option_array_(),
        max_connections_(0),
        max_user_connections_(0)
O
oceanbase-admin 已提交
4456
  {}
X
xy0 已提交
4457
  explicit ObUserInfo(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
4458
  virtual ~ObUserInfo();
X
xy0 已提交
4459 4460 4461
  ObUserInfo(const ObUserInfo &other);
  ObUserInfo &operator=(const ObUserInfo &other);
  static bool cmp(const ObUserInfo *lhs, const ObUserInfo *rhs)
O
oceanbase-admin 已提交
4462 4463 4464
  {
    return (NULL != lhs && NULL != rhs) ? lhs->get_tenant_user_id() < rhs->get_tenant_user_id() : false;
  }
X
xy0 已提交
4465
  static bool equal(const ObUserInfo *lhs, const ObUserInfo *rhs)
O
oceanbase-admin 已提交
4466 4467 4468 4469 4470
  {
    return (NULL != lhs && NULL != rhs) ? lhs->get_tenant_user_id() == rhs->get_tenant_user_id() : false;
  }

  // set methods
X
xy0 已提交
4471
  inline int set_user_name(const char *user_name)
O
oceanbase-admin 已提交
4472 4473 4474
  {
    return deep_copy_str(user_name, user_name_);
  }
X
xy0 已提交
4475
  inline int set_user_name(const common::ObString &user_name)
O
oceanbase-admin 已提交
4476 4477 4478
  {
    return deep_copy_str(user_name, user_name_);
  }
X
xy0 已提交
4479
  inline int set_host(const char *host_name)
O
oceanbase-admin 已提交
4480 4481 4482
  {
    return deep_copy_str(host_name, host_name_);
  }
X
xy0 已提交
4483
  inline int set_host(const common::ObString &host_name)
O
oceanbase-admin 已提交
4484 4485 4486
  {
    return deep_copy_str(host_name, host_name_);
  }
X
xy0 已提交
4487
  inline int set_passwd(const char *passwd)
O
oceanbase-admin 已提交
4488 4489 4490
  {
    return deep_copy_str(passwd, passwd_);
  }
X
xy0 已提交
4491
  inline int set_passwd(const common::ObString &passwd)
O
oceanbase-admin 已提交
4492 4493 4494
  {
    return deep_copy_str(passwd, passwd_);
  }
X
xy0 已提交
4495
  inline int set_info(const char *info)
O
oceanbase-admin 已提交
4496 4497 4498
  {
    return deep_copy_str(info, info_);
  }
X
xy0 已提交
4499
  inline int set_info(const common::ObString &info)
O
oceanbase-admin 已提交
4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510
  {
    return deep_copy_str(info, info_);
  }
  inline void set_is_locked(const bool locked)
  {
    locked_ = locked;
  }
  inline void set_ssl_type(const ObSSLType ssl_type)
  {
    ssl_type_ = ssl_type;
  }
X
xy0 已提交
4511
  inline int set_ssl_cipher(const char *ssl_cipher)
O
oceanbase-admin 已提交
4512 4513 4514
  {
    return deep_copy_str(ssl_cipher, ssl_cipher_);
  }
X
xy0 已提交
4515
  inline int set_ssl_cipher(const common::ObString &ssl_cipher)
O
oceanbase-admin 已提交
4516 4517 4518
  {
    return deep_copy_str(ssl_cipher, ssl_cipher_);
  }
X
xy0 已提交
4519
  inline int set_x509_issuer(const char *x509_issuer)
O
oceanbase-admin 已提交
4520 4521 4522
  {
    return deep_copy_str(x509_issuer, x509_issuer_);
  }
X
xy0 已提交
4523
  inline int set_x509_issuer(const common::ObString &x509_issuer)
O
oceanbase-admin 已提交
4524 4525 4526
  {
    return deep_copy_str(x509_issuer, x509_issuer_);
  }
X
xy0 已提交
4527
  inline int set_x509_subject(const char *x509_subject)
O
oceanbase-admin 已提交
4528 4529 4530
  {
    return deep_copy_str(x509_subject, x509_subject_);
  }
X
xy0 已提交
4531
  inline int set_x509_subject(const common::ObString &x509_subject)
O
oceanbase-admin 已提交
4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546
  {
    return deep_copy_str(x509_subject, x509_subject_);
  }
  inline void set_type(const uint64_t type)
  {
    type_ = type;
  }
  inline void set_profile_id(const uint64_t profile_id)
  {
    profile_id_ = profile_id;
  }
  inline void set_password_last_changed(int64_t ts)
  {
    password_last_changed_timestamp_ = ts;
  }
4547 4548 4549 4550 4551 4552 4553 4554
  inline void set_max_connections(uint64_t max_connections)
  {
    max_connections_ = max_connections;
  }
  inline void set_max_user_connections(uint64_t max_user_connections)
  {
    max_user_connections_ = max_user_connections;
  }
O
oceanbase-admin 已提交
4555
  // get methods
X
xy0 已提交
4556
  inline const char *get_user_name() const
O
oceanbase-admin 已提交
4557 4558 4559
  {
    return extract_str(user_name_);
  }
X
xy0 已提交
4560
  inline const common::ObString &get_user_name_str() const
O
oceanbase-admin 已提交
4561 4562 4563
  {
    return user_name_;
  }
X
xy0 已提交
4564
  inline const char *get_host_name() const
O
oceanbase-admin 已提交
4565 4566 4567
  {
    return extract_str(host_name_);
  }
X
xy0 已提交
4568
  inline const common::ObString &get_host_name_str() const
O
oceanbase-admin 已提交
4569 4570 4571
  {
    return host_name_;
  }
X
xy0 已提交
4572
  inline const char *get_passwd() const
O
oceanbase-admin 已提交
4573 4574 4575
  {
    return extract_str(passwd_);
  }
X
xy0 已提交
4576
  inline const common::ObString &get_passwd_str() const
O
oceanbase-admin 已提交
4577 4578 4579
  {
    return passwd_;
  }
X
xy0 已提交
4580
  inline const char *get_info() const
O
oceanbase-admin 已提交
4581 4582 4583
  {
    return extract_str(info_);
  }
X
xy0 已提交
4584
  inline const common::ObString &get_info_str() const
O
oceanbase-admin 已提交
4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599
  {
    return info_;
  }
  inline bool get_is_locked() const
  {
    return locked_;
  }
  inline ObSSLType get_ssl_type() const
  {
    return ssl_type_;
  }
  inline const common::ObString get_ssl_type_str() const
  {
    return get_ssl_type_string(ssl_type_);
  }
X
xy0 已提交
4600
  inline const char *get_ssl_cipher() const
O
oceanbase-admin 已提交
4601 4602 4603
  {
    return extract_str(ssl_cipher_);
  }
X
xy0 已提交
4604
  inline const common::ObString &get_ssl_cipher_str() const
O
oceanbase-admin 已提交
4605 4606 4607
  {
    return ssl_cipher_;
  }
X
xy0 已提交
4608
  inline const char *get_x509_issuer() const
O
oceanbase-admin 已提交
4609 4610 4611
  {
    return extract_str(x509_issuer_);
  }
X
xy0 已提交
4612
  inline const common::ObString &get_x509_issuer_str() const
O
oceanbase-admin 已提交
4613 4614 4615
  {
    return x509_issuer_;
  }
X
xy0 已提交
4616
  inline const char *get_x509_subject() const
O
oceanbase-admin 已提交
4617 4618 4619
  {
    return extract_str(x509_subject_);
  }
X
xy0 已提交
4620
  inline const common::ObString &get_x509_subject_str() const
O
oceanbase-admin 已提交
4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631
  {
    return x509_subject_;
  }
  inline uint64_t get_profile_id() const
  {
    return profile_id_;
  }
  inline int64_t get_password_last_changed() const
  {
    return password_last_changed_timestamp_;
  }
4632 4633 4634 4635 4636 4637 4638 4639
  inline uint64_t get_max_connections() const
  {
    return max_connections_;
  }
  inline uint64_t get_max_user_connections() const
  {
    return max_user_connections_;
  }
O
oceanbase-admin 已提交
4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652
  // role
  inline bool is_role() const
  {
    return OB_ROLE == type_;
  }
  inline int get_grantee_count() const
  {
    return grantee_id_array_.count();
  }
  inline int get_role_count() const
  {
    return role_id_array_.count();
  }
X
xy0 已提交
4653
  const common::ObSEArray<uint64_t, 8> &get_grantee_id_array() const
O
oceanbase-admin 已提交
4654 4655 4656
  {
    return grantee_id_array_;
  }
X
xy0 已提交
4657
  const common::ObSEArray<uint64_t, 8> &get_role_id_array() const
O
oceanbase-admin 已提交
4658 4659 4660
  {
    return role_id_array_;
  }
X
xy0 已提交
4661
  const common::ObSEArray<uint64_t, 8> &get_role_id_option_array() const
O
oceanbase-admin 已提交
4662 4663 4664 4665 4666 4667 4668 4669
  {
    return role_id_option_array_;
  }
  int add_grantee_id(const uint64_t id)
  {
    return grantee_id_array_.push_back(id);
  }
  int add_role_id(const uint64_t id, const uint64_t admin_option = NO_OPTION, const uint64_t disable_flag = 0);
X
xy0 已提交
4670
  void set_admin_option(uint64_t &option, const uint64_t admin_option)
O
oceanbase-admin 已提交
4671 4672 4673
  {
    option |= (admin_option << ADMIN_OPTION_SHIFT);
  }
X
xy0 已提交
4674
  void set_disable_flag(uint64_t &option, const uint64_t disable_flag)
O
oceanbase-admin 已提交
4675 4676 4677
  {
    option |= (disable_flag << DISABLE_FLAG_SHIFT);
  }
X
xy0 已提交
4678
  int get_nth_role_option(uint64_t nth, uint64_t &option) const;
O
oceanbase-admin 已提交
4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695
  uint64_t get_admin_option(uint64_t option) const
  {
    return 1 & (option >> ADMIN_OPTION_SHIFT);
  }
  uint64_t get_disable_option(uint64_t option) const
  {
    return 1 & (option >> DISABLE_FLAG_SHIFT);
  }

  // other methods
  virtual bool is_valid() const;
  virtual void reset();
  int64_t get_convert_size() const;
  TO_STRING_KV(K_(tenant_id), K_(user_id), K_(user_name), K_(host_name), "privileges", ObPrintPrivSet(priv_set_),
      K_(info), K_(locked), K_(ssl_type), K_(ssl_cipher), K_(x509_issuer), K_(x509_subject), K_(type),
      K_(grantee_id_array), K_(role_id_array), K_(profile_id));
  bool role_exists(const uint64_t role_id, const uint64_t option) const;
X
xy0 已提交
4696
  int get_seq_by_role_id(uint64_t role_id, uint64_t &seq) const;
O
oceanbase-admin 已提交
4697

G
gm 已提交
4698
private:
O
oceanbase-admin 已提交
4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714
  common::ObString user_name_;
  common::ObString host_name_;
  common::ObString passwd_;
  common::ObString info_;
  bool locked_;
  ObSSLType ssl_type_;
  common::ObString ssl_cipher_;
  common::ObString x509_issuer_;
  common::ObString x509_subject_;

  int type_;
  common::ObSEArray<uint64_t, 8> grantee_id_array_;  // Record role granted to user
  common::ObSEArray<uint64_t, 8> role_id_array_;     // Record which roles the user/role has
  uint64_t profile_id_;
  int64_t password_last_changed_timestamp_;
  common::ObSEArray<uint64_t, 8> role_id_option_array_;  // Record which roles the user/role has
4715 4716
  uint64_t max_connections_;
  uint64_t max_user_connections_;
O
oceanbase-admin 已提交
4717 4718 4719 4720 4721 4722 4723 4724
};

struct ObDBPrivSortKey {
  ObDBPrivSortKey() : tenant_id_(common::OB_INVALID_ID), user_id_(common::OB_INVALID_ID), sort_(0)
  {}
  ObDBPrivSortKey(const uint64_t tenant_id, const uint64_t user_id, const uint64_t sort_value)
      : tenant_id_(tenant_id), user_id_(user_id), sort_(sort_value)
  {}
X
xy0 已提交
4725
  bool operator==(const ObDBPrivSortKey &rhs) const
O
oceanbase-admin 已提交
4726 4727 4728
  {
    return (tenant_id_ == rhs.tenant_id_) && (user_id_ == rhs.user_id_) && (sort_ == rhs.sort_);
  }
X
xy0 已提交
4729
  bool operator!=(const ObDBPrivSortKey &rhs) const
O
oceanbase-admin 已提交
4730 4731 4732
  {
    return !(*this == rhs);
  }
X
xy0 已提交
4733
  bool operator<(const ObDBPrivSortKey &rhs) const
O
oceanbase-admin 已提交
4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754
  {
    bool bret = tenant_id_ < rhs.tenant_id_;
    if (false == bret && tenant_id_ == rhs.tenant_id_) {
      bret = user_id_ < rhs.user_id_;
      if (false == bret && user_id_ == rhs.user_id_) {
        bret = sort_ > rhs.sort_;  // sort values of 'sort_' from big to small
      }
    }
    return bret;
  }

  TO_STRING_KV(K_(tenant_id), K_(user_id), K(sort_));

  uint64_t tenant_id_;
  uint64_t user_id_;
  uint64_t sort_;
};

struct ObOriginalDBKey {
  ObOriginalDBKey() : tenant_id_(common::OB_INVALID_ID), user_id_(common::OB_INVALID_ID)
  {}
X
xy0 已提交
4755
  ObOriginalDBKey(const uint64_t tenant_id, const uint64_t user_id, const common::ObString &db)
O
oceanbase-admin 已提交
4756 4757
      : tenant_id_(tenant_id), user_id_(user_id), db_(db)
  {}
X
xy0 已提交
4758
  bool operator==(const ObOriginalDBKey &rhs) const
O
oceanbase-admin 已提交
4759 4760 4761
  {
    return (tenant_id_ == rhs.tenant_id_) && (user_id_ == rhs.user_id_) && (db_ == rhs.db_);
  }
X
xy0 已提交
4762
  bool operator!=(const ObOriginalDBKey &rhs) const
O
oceanbase-admin 已提交
4763 4764 4765
  {
    return !(*this == rhs);
  }
X
xy0 已提交
4766
  bool operator<(const ObOriginalDBKey &rhs) const
O
oceanbase-admin 已提交
4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797
  {
    bool bret = tenant_id_ < rhs.tenant_id_;
    if (false == bret && tenant_id_ == rhs.tenant_id_) {
      bret = user_id_ < rhs.user_id_;
    }
    return bret;
  }
  // Not used yet.
  inline uint64_t hash() const
  {
    uint64_t hash_ret = 0;
    hash_ret = common::murmurhash(&tenant_id_, sizeof(tenant_id_), 0);
    hash_ret = common::murmurhash(&user_id_, sizeof(user_id_), hash_ret);
    hash_ret = common::murmurhash(db_.ptr(), db_.length(), hash_ret);
    return hash_ret;
  }
  bool is_valid() const
  {
    return (tenant_id_ != common::OB_INVALID_ID) && (user_id_ != common::OB_INVALID_ID);
  }
  TO_STRING_KV(K_(tenant_id), K_(user_id), K_(db));
  uint64_t tenant_id_;
  uint64_t user_id_;
  common::ObString db_;
};

struct ObSysPrivKey {
  ObSysPrivKey() : tenant_id_(common::OB_INVALID_ID), grantee_id_(common::OB_INVALID_ID)
  {}
  ObSysPrivKey(const uint64_t tenant_id, const uint64_t user_id) : tenant_id_(tenant_id), grantee_id_(user_id)
  {}
X
xy0 已提交
4798
  bool operator==(const ObSysPrivKey &rhs) const
O
oceanbase-admin 已提交
4799 4800 4801
  {
    return ((tenant_id_ == rhs.tenant_id_) && (grantee_id_ == rhs.grantee_id_));
  }
X
xy0 已提交
4802
  bool operator!=(const ObSysPrivKey &rhs) const
O
oceanbase-admin 已提交
4803 4804 4805
  {
    return !(*this == rhs);
  }
X
xy0 已提交
4806
  bool operator<(const ObSysPrivKey &rhs) const
O
oceanbase-admin 已提交
4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833
  {
    bool bret = tenant_id_ < rhs.tenant_id_;
    if (false == bret && tenant_id_ == rhs.tenant_id_) {
      bret = grantee_id_ < rhs.grantee_id_;
    }
    return bret;
  }
  // Not used yet.
  inline uint64_t hash() const
  {
    uint64_t hash_ret = 0;
    hash_ret = common::murmurhash(&tenant_id_, sizeof(tenant_id_), 0);
    hash_ret = common::murmurhash(&grantee_id_, sizeof(grantee_id_), hash_ret);
    return hash_ret;
  }
  bool is_valid() const
  {
    return (tenant_id_ != common::OB_INVALID_ID) && (grantee_id_ != common::OB_INVALID_ID);
  }
  TO_STRING_KV(K_(tenant_id), K_(grantee_id));
  uint64_t tenant_id_;
  uint64_t grantee_id_;
};

class ObDBPriv : public ObSchema, public ObPriv {
  OB_UNIS_VERSION(1);

G
gm 已提交
4834
public:
O
oceanbase-admin 已提交
4835 4836
  ObDBPriv() : ObPriv(), db_(), sort_(0)
  {}
X
xy0 已提交
4837
  explicit ObDBPriv(common::ObIAllocator *allocator) : ObSchema(allocator), ObPriv(allocator), db_(), sort_(0)
O
oceanbase-admin 已提交
4838 4839 4840
  {}
  virtual ~ObDBPriv()
  {}
X
xy0 已提交
4841
  ObDBPriv(const ObDBPriv &other) : ObSchema(), ObPriv()
O
oceanbase-admin 已提交
4842 4843 4844
  {
    *this = other;
  }
X
xy0 已提交
4845
  ObDBPriv &operator=(const ObDBPriv &other);
O
oceanbase-admin 已提交
4846

X
xy0 已提交
4847
  static bool cmp(const ObDBPriv *lhs, const ObDBPriv *rhs)
O
oceanbase-admin 已提交
4848 4849 4850
  {
    return (NULL != lhs && NULL != rhs) ? (lhs->get_sort_key() < rhs->get_sort_key()) : false;
  }
X
xy0 已提交
4851
  static bool cmp_sort_key(const ObDBPriv *lhs, const ObDBPrivSortKey &sort_key)
O
oceanbase-admin 已提交
4852 4853 4854 4855 4856 4857 4858
  {
    return NULL != lhs ? lhs->get_sort_key() < sort_key : false;
  }
  ObDBPrivSortKey get_sort_key() const
  {
    return ObDBPrivSortKey(tenant_id_, user_id_, sort_);
  }
X
xy0 已提交
4859
  static bool equal(const ObDBPriv *lhs, const ObDBPriv *rhs)
O
oceanbase-admin 已提交
4860 4861 4862 4863 4864 4865 4866 4867 4868
  {
    return (NULL != lhs && NULL != rhs) ? lhs->get_sort_key() == rhs->get_sort_key() : false;
  }  // point check
  ObOriginalDBKey get_original_key() const
  {
    return ObOriginalDBKey(tenant_id_, user_id_, db_);
  }

  // set methods
X
xy0 已提交
4869
  inline int set_database_name(const char *db)
O
oceanbase-admin 已提交
4870 4871 4872
  {
    return deep_copy_str(db, db_);
  }
X
xy0 已提交
4873
  inline int set_database_name(const common::ObString &db)
O
oceanbase-admin 已提交
4874 4875 4876 4877 4878 4879 4880 4881 4882
  {
    return deep_copy_str(db, db_);
  }
  inline void set_sort(const uint64_t sort)
  {
    sort_ = sort;
  }

  // get methods
X
xy0 已提交
4883
  inline const char *get_database_name() const
O
oceanbase-admin 已提交
4884 4885 4886
  {
    return extract_str(db_);
  }
X
xy0 已提交
4887
  inline const common::ObString &get_database_name_str() const
O
oceanbase-admin 已提交
4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900
  {
    return db_;
  }
  inline uint64_t get_sort() const
  {
    return sort_;
  }
  // other methods
  virtual bool is_valid() const;
  virtual void reset();
  int64_t get_convert_size() const;
  TO_STRING_KV(K_(tenant_id), K_(user_id), K_(db), "privileges", ObPrintPrivSet(priv_set_));

G
gm 已提交
4901
private:
O
oceanbase-admin 已提交
4902 4903 4904 4905 4906 4907 4908 4909
  common::ObString db_;
  uint64_t sort_;
};

// In order to find in table_privs_ whether a table is authorized under a certain db
struct ObTablePrivDBKey {
  ObTablePrivDBKey() : tenant_id_(common::OB_INVALID_ID), user_id_(common::OB_INVALID_ID)
  {}
X
xy0 已提交
4910
  ObTablePrivDBKey(const uint64_t tenant_id, const uint64_t user_id, const common::ObString &db)
O
oceanbase-admin 已提交
4911 4912
      : tenant_id_(tenant_id), user_id_(user_id), db_(db)
  {}
X
xy0 已提交
4913
  bool operator==(const ObTablePrivDBKey &rhs) const
O
oceanbase-admin 已提交
4914 4915 4916
  {
    return (tenant_id_ == rhs.tenant_id_) && (user_id_ == rhs.user_id_) && (db_ == rhs.db_);
  }
X
xy0 已提交
4917
  bool operator!=(const ObTablePrivDBKey &rhs) const
O
oceanbase-admin 已提交
4918 4919 4920
  {
    return !(*this == rhs);
  }
X
xy0 已提交
4921
  bool operator<(const ObTablePrivDBKey &rhs) const
O
oceanbase-admin 已提交
4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940
  {
    bool bret = tenant_id_ < rhs.tenant_id_;
    if (false == bret && tenant_id_ == rhs.tenant_id_) {
      bret = user_id_ < rhs.user_id_;
      if (false == bret && user_id_ == rhs.user_id_) {
        bret = db_ < rhs.db_;
      }
    }
    return bret;
  }
  uint64_t tenant_id_;
  uint64_t user_id_;
  common::ObString db_;
};

struct ObTablePrivSortKey {
  ObTablePrivSortKey() : tenant_id_(common::OB_INVALID_ID), user_id_(common::OB_INVALID_ID)
  {}
  ObTablePrivSortKey(
X
xy0 已提交
4941
      const uint64_t tenant_id, const uint64_t user_id, const common::ObString &db, const common::ObString &table)
O
oceanbase-admin 已提交
4942 4943
      : tenant_id_(tenant_id), user_id_(user_id), db_(db), table_(table)
  {}
X
xy0 已提交
4944
  bool operator==(const ObTablePrivSortKey &rhs) const
O
oceanbase-admin 已提交
4945 4946 4947
  {
    return (tenant_id_ == rhs.tenant_id_) && (user_id_ == rhs.user_id_) && (db_ == rhs.db_) && (table_ == rhs.table_);
  }
X
xy0 已提交
4948
  bool operator!=(const ObTablePrivSortKey &rhs) const
O
oceanbase-admin 已提交
4949 4950 4951
  {
    return !(*this == rhs);
  }
X
xy0 已提交
4952
  bool operator<(const ObTablePrivSortKey &rhs) const
O
oceanbase-admin 已提交
4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004
  {
    bool bret = tenant_id_ < rhs.tenant_id_;
    if (false == bret && tenant_id_ == rhs.tenant_id_) {
      bret = user_id_ < rhs.user_id_;
      if (false == bret && user_id_ == rhs.user_id_) {
        bret = db_ < rhs.db_;
        if (false == bret && db_ == rhs.db_) {
          bret = table_ < rhs.table_;
        }
      }
    }
    return bret;
  }
  // Not used yet.
  inline uint64_t hash() const
  {
    uint64_t hash_ret = 0;
    hash_ret = common::murmurhash(&tenant_id_, sizeof(tenant_id_), 0);
    hash_ret = common::murmurhash(&user_id_, sizeof(user_id_), hash_ret);
    hash_ret = common::murmurhash(db_.ptr(), db_.length(), hash_ret);
    hash_ret = common::murmurhash(table_.ptr(), table_.length(), hash_ret);
    return hash_ret;
  }
  bool is_valid() const
  {
    return (tenant_id_ != common::OB_INVALID_ID) && (user_id_ != common::OB_INVALID_ID);
  }
  TO_STRING_KV(K_(tenant_id), K_(user_id), K_(db), K_(table));
  uint64_t tenant_id_;
  uint64_t user_id_;
  common::ObString db_;
  common::ObString table_;
};

struct ObObjPrivSortKey {
  ObObjPrivSortKey()
      : tenant_id_(common::OB_INVALID_ID),
        obj_id_(common::OB_INVALID_ID),
        obj_type_(common::OB_INVALID_ID),
        col_id_(common::OB_INVALID_ID),
        grantor_id_(common::OB_INVALID_ID),
        grantee_id_(common::OB_INVALID_ID)
  {}
  ObObjPrivSortKey(const uint64_t tenant_id, const uint64_t obj_id, const uint64_t obj_type, const uint64_t col_id,
      const uint64_t grantor_id, const uint64_t grantee_id)
      : tenant_id_(tenant_id),
        obj_id_(obj_id),
        obj_type_(obj_type),
        col_id_(col_id),
        grantor_id_(grantor_id),
        grantee_id_(grantee_id)
  {}
X
xy0 已提交
5005
  bool operator==(const ObObjPrivSortKey &rhs) const
O
oceanbase-admin 已提交
5006 5007 5008 5009
  {
    return (tenant_id_ == rhs.tenant_id_) && (obj_id_ == rhs.obj_id_) && (obj_type_ == rhs.obj_type_) &&
           (col_id_ == rhs.col_id_) && (grantor_id_ == rhs.grantor_id_) && (grantee_id_ == rhs.grantee_id_);
  }
X
xy0 已提交
5010
  bool operator!=(const ObObjPrivSortKey &rhs) const
O
oceanbase-admin 已提交
5011 5012 5013
  {
    return !(*this == rhs);
  }
X
xy0 已提交
5014
  bool operator<(const ObObjPrivSortKey &rhs) const
O
oceanbase-admin 已提交
5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064
  {
    bool bret = tenant_id_ < rhs.tenant_id_;
    if (false == bret && tenant_id_ == rhs.tenant_id_) {
      bret = grantee_id_ < rhs.grantee_id_;
      if (false == bret && grantee_id_ == rhs.grantee_id_) {
        bret = obj_id_ < rhs.obj_id_;
        if (false == bret && obj_id_ == rhs.obj_id_) {
          bret = obj_type_ < rhs.obj_type_;
          if (false == bret && obj_type_ == rhs.obj_type_) {
            bret = col_id_ < rhs.col_id_;
            if (false == bret && col_id_ == rhs.col_id_) {
              bret = grantor_id_ < rhs.grantor_id_;
            }
          }
        }
      }
    }
    return bret;
  }
  inline uint64_t hash() const
  {
    uint64_t hash_ret = 0;
    hash_ret = common::murmurhash(&tenant_id_, sizeof(tenant_id_), 0);
    hash_ret = common::murmurhash(&grantee_id_, sizeof(grantee_id_), hash_ret);
    hash_ret = common::murmurhash(&obj_id_, sizeof(obj_id_), hash_ret);
    hash_ret = common::murmurhash(&obj_type_, sizeof(obj_type_), hash_ret);
    hash_ret = common::murmurhash(&col_id_, sizeof(col_id_), hash_ret);
    hash_ret = common::murmurhash(&grantor_id_, sizeof(grantor_id_), hash_ret);
    return hash_ret;
  }
  bool is_valid() const
  {
    return (tenant_id_ != common::OB_INVALID_ID) && (obj_id_ != common::OB_INVALID_ID) &&
           (obj_type_ != common::OB_INVALID_ID) && (grantor_id_ != common::OB_INVALID_ID) &&
           (grantee_id_ != common::OB_INVALID_ID);
  }
  TO_STRING_KV(K_(tenant_id), K_(obj_id), K_(obj_type), K_(col_id), K_(grantor_id), K_(grantee_id));
  uint64_t tenant_id_;
  uint64_t obj_id_;
  uint64_t obj_type_;
  uint64_t col_id_;
  uint64_t grantor_id_;
  uint64_t grantee_id_;
};

typedef common::ObSEArray<share::schema::ObObjPrivSortKey, 4> ObObjPrivSortKeyArray;

class ObTablePriv : public ObSchema, public ObPriv {
  OB_UNIS_VERSION(1);

G
gm 已提交
5065
public:
O
oceanbase-admin 已提交
5066 5067 5068
  // constructor and destructor
  ObTablePriv() : ObSchema(), ObPriv()
  {}
X
xy0 已提交
5069
  explicit ObTablePriv(common::ObIAllocator *allocator) : ObSchema(allocator), ObPriv(allocator)
O
oceanbase-admin 已提交
5070
  {}
X
xy0 已提交
5071
  ObTablePriv(const ObTablePriv &other) : ObSchema(), ObPriv()
O
oceanbase-admin 已提交
5072 5073 5074 5075 5076 5077 5078
  {
    *this = other;
  }
  virtual ~ObTablePriv()
  {}

  // operator=
X
xy0 已提交
5079
  ObTablePriv &operator=(const ObTablePriv &other);
O
oceanbase-admin 已提交
5080 5081 5082 5083 5084 5085

  // for sort
  ObTablePrivSortKey get_sort_key() const
  {
    return ObTablePrivSortKey(tenant_id_, user_id_, db_, table_);
  }
X
xy0 已提交
5086
  static bool cmp(const ObTablePriv *lhs, const ObTablePriv *rhs)
O
oceanbase-admin 已提交
5087 5088 5089
  {
    return (NULL != lhs && NULL != rhs) ? lhs->get_sort_key() < rhs->get_sort_key() : false;
  }
X
xy0 已提交
5090
  static bool cmp_sort_key(const ObTablePriv *lhs, const ObTablePrivSortKey &sort_key)
O
oceanbase-admin 已提交
5091 5092 5093
  {
    return NULL != lhs ? lhs->get_sort_key() < sort_key : false;
  }
X
xy0 已提交
5094
  static bool equal(const ObTablePriv *lhs, const ObTablePriv *rhs)
O
oceanbase-admin 已提交
5095 5096 5097
  {
    return (NULL != lhs && NULL != rhs) ? lhs->get_sort_key() == rhs->get_sort_key() : false;
  }
X
xy0 已提交
5098
  static bool equal_sort_key(const ObTablePriv *lhs, const ObTablePrivSortKey &sort_key)
O
oceanbase-admin 已提交
5099 5100 5101 5102 5103 5104 5105 5106
  {
    return NULL != lhs ? lhs->get_sort_key() == sort_key : false;
  }

  ObTablePrivDBKey get_db_key() const
  {
    return ObTablePrivDBKey(tenant_id_, user_id_, db_);
  }
X
xy0 已提交
5107
  static bool cmp_db_key(const ObTablePriv *lhs, const ObTablePrivDBKey &db_key)
O
oceanbase-admin 已提交
5108 5109 5110 5111 5112
  {
    return lhs->get_db_key() < db_key;
  }

  // set methods
X
xy0 已提交
5113
  inline int set_database_name(const char *db)
O
oceanbase-admin 已提交
5114 5115 5116
  {
    return deep_copy_str(db, db_);
  }
X
xy0 已提交
5117
  inline int set_database_name(const common::ObString &db)
O
oceanbase-admin 已提交
5118 5119 5120
  {
    return deep_copy_str(db, db_);
  }
X
xy0 已提交
5121
  inline int set_table_name(const char *table)
O
oceanbase-admin 已提交
5122 5123 5124
  {
    return deep_copy_str(table, table_);
  }
X
xy0 已提交
5125
  inline int set_table_name(const common::ObString &table)
O
oceanbase-admin 已提交
5126 5127 5128 5129 5130
  {
    return deep_copy_str(table, table_);
  }

  // get methods
X
xy0 已提交
5131
  inline const char *get_database_name() const
O
oceanbase-admin 已提交
5132 5133 5134
  {
    return extract_str(db_);
  }
X
xy0 已提交
5135
  inline const common::ObString &get_database_name_str() const
O
oceanbase-admin 已提交
5136 5137 5138
  {
    return db_;
  }
X
xy0 已提交
5139
  inline const char *get_table_name() const
O
oceanbase-admin 已提交
5140 5141 5142
  {
    return extract_str(table_);
  }
X
xy0 已提交
5143
  inline const common::ObString &get_table_name_str() const
O
oceanbase-admin 已提交
5144 5145 5146 5147 5148 5149 5150 5151 5152 5153
  {
    return table_;
  }

  TO_STRING_KV(K_(tenant_id), K_(user_id), K_(db), K_(table), "privileges", ObPrintPrivSet(priv_set_));
  // other methods
  virtual bool is_valid() const;
  virtual void reset();
  int64_t get_convert_size() const;

G
gm 已提交
5154
private:
O
oceanbase-admin 已提交
5155 5156 5157 5158 5159 5160 5161
  common::ObString db_;
  common::ObString table_;
};

class ObObjPriv : public ObSchema, public ObPriv {
  OB_UNIS_VERSION(1);

G
gm 已提交
5162
public:
O
oceanbase-admin 已提交
5163 5164 5165
  // constructor and destructor
  ObObjPriv() : ObSchema(), ObPriv()
  {}
X
xy0 已提交
5166
  explicit ObObjPriv(common::ObIAllocator *allocator) : ObSchema(allocator), ObPriv()
O
oceanbase-admin 已提交
5167
  {}
X
xy0 已提交
5168
  ObObjPriv(const ObObjPriv &other) : ObSchema(), ObPriv()
O
oceanbase-admin 已提交
5169 5170 5171 5172 5173 5174 5175
  {
    *this = other;
  }
  virtual ~ObObjPriv()
  {}

  // operator=
X
xy0 已提交
5176
  ObObjPriv &operator=(const ObObjPriv &other);
O
oceanbase-admin 已提交
5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188

  // for sort
  ObObjPrivSortKey get_sort_key() const
  {
    return ObObjPrivSortKey(tenant_id_, obj_id_, obj_type_, col_id_, grantor_id_, grantee_id_);
  }

  ObTenantUrObjId get_tenant_ur_obj_id() const
  {
    return ObTenantUrObjId(tenant_id_, grantee_id_, obj_id_, obj_type_, col_id_);
  }

X
xy0 已提交
5189
  static bool cmp_tenant_ur_obj_id(const ObObjPriv *lhs, const ObTenantUrObjId &rhs)
O
oceanbase-admin 已提交
5190 5191 5192
  {
    return (lhs->get_tenant_ur_obj_id() < rhs);
  }
X
xy0 已提交
5193
  static bool cmp(const ObObjPriv *lhs, const ObObjPriv *rhs)
O
oceanbase-admin 已提交
5194 5195 5196
  {
    return (NULL != lhs && NULL != rhs) ? lhs->get_sort_key() < rhs->get_sort_key() : false;
  }
X
xy0 已提交
5197
  static bool cmp_sort_key(const ObObjPriv *lhs, const ObObjPrivSortKey &sort_key)
O
oceanbase-admin 已提交
5198 5199 5200
  {
    return NULL != lhs ? lhs->get_sort_key() < sort_key : false;
  }
X
xy0 已提交
5201
  static bool equal(const ObObjPriv *lhs, const ObObjPriv *rhs)
O
oceanbase-admin 已提交
5202 5203 5204
  {
    return (NULL != lhs && NULL != rhs) ? lhs->get_sort_key() == rhs->get_sort_key() : false;
  }
X
xy0 已提交
5205
  static bool equal_sort_key(const ObObjPriv *lhs, const ObObjPrivSortKey &sort_key)
O
oceanbase-admin 已提交
5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264
  {
    return NULL != lhs ? lhs->get_sort_key() == sort_key : false;
  }

  // ObTablePrivDBKey get_db_key() const
  // { return ObTablePrivDBKey(tenant_id_, user_id_, db_); }
  // static bool cmp_db_key(const ObTablePriv *lhs, const ObTablePrivDBKey &db_key)
  // { return lhs->get_db_key() < db_key; }

  // set methods
  inline void set_obj_id(const uint64_t obj_id)
  {
    obj_id_ = obj_id;
  }
  inline void set_col_id(const uint64_t col_id)
  {
    col_id_ = col_id;
  }
  inline void set_grantor_id(const uint64_t grantor_id)
  {
    grantor_id_ = grantor_id;
  }
  inline void set_grantee_id(const uint64_t grantee_id)
  {
    grantee_id_ = grantee_id;
  }
  inline void set_objtype(const uint64_t objtype)
  {
    obj_type_ = objtype;
  }
  // get methods
  inline uint64_t get_objtype() const
  {
    return obj_type_;
  }
  inline uint64_t get_obj_id() const
  {
    return obj_id_;
  }
  inline uint64_t get_col_id() const
  {
    return col_id_;
  }
  inline uint64_t get_grantee_id() const
  {
    return grantee_id_;
  }
  inline uint64_t get_grantor_id() const
  {
    return grantor_id_;
  }

  TO_STRING_KV(K_(tenant_id), K_(user_id), K_(obj_id), K_(obj_type), K_(col_id), "privileges",
      ObPrintPrivSet(priv_set_), K_(grantor_id), K_(grantee_id));
  // other methods
  virtual bool is_valid() const;
  virtual void reset();
  int64_t get_convert_size() const;

G
gm 已提交
5265
private:
O
oceanbase-admin 已提交
5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283
  uint64_t obj_id_;
  uint64_t obj_type_;
  uint64_t col_id_;
  uint64_t grantor_id_;
  uint64_t grantee_id_;
};

enum ObPrivLevel {
  OB_PRIV_INVALID_LEVEL,
  OB_PRIV_USER_LEVEL,
  OB_PRIV_DB_LEVEL,
  OB_PRIV_TABLE_LEVEL,
  OB_PRIV_DB_ACCESS_LEVEL,
  OB_PRIV_SYS_ORACLE_LEVEL, /* oracle-mode system privilege */
  OB_PRIV_OBJ_ORACLE_LEVEL, /* oracle-mode object privilege */
  OB_PRIV_MAX_LEVEL,
};

X
xy0 已提交
5284
const char *ob_priv_level_str(const ObPrivLevel grant_level);
O
oceanbase-admin 已提交
5285 5286

struct ObNeedPriv {
X
xy0 已提交
5287
  ObNeedPriv(const common::ObString &db, const common::ObString &table, ObPrivLevel priv_level, ObPrivSet priv_set,
O
oceanbase-admin 已提交
5288 5289 5290 5291 5292
      const bool is_sys_table)
      : db_(db), table_(table), priv_level_(priv_level), priv_set_(priv_set), is_sys_table_(is_sys_table)
  {}
  ObNeedPriv() : db_(), table_(), priv_level_(OB_PRIV_INVALID_LEVEL), priv_set_(0), is_sys_table_(false)
  {}
X
xy0 已提交
5293
  int deep_copy(const ObNeedPriv &other, common::ObIAllocator &allocator);
O
oceanbase-admin 已提交
5294 5295 5296 5297 5298 5299 5300 5301 5302 5303
  common::ObString db_;
  common::ObString table_;
  ObPrivLevel priv_level_;
  ObPrivSet priv_set_;
  bool is_sys_table_;  // May be used to represent the table of schema metadata
  TO_STRING_KV(K_(db), K_(table), K_(priv_set), K_(priv_level), K_(is_sys_table));
};

struct ObStmtNeedPrivs {
  typedef common::ObFixedArray<ObNeedPriv, common::ObIAllocator> NeedPrivs;
X
xy0 已提交
5304
  explicit ObStmtNeedPrivs(common::ObIAllocator &alloc) : need_privs_(alloc)
O
oceanbase-admin 已提交
5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315
  {}
  ObStmtNeedPrivs() : need_privs_()
  {}
  ~ObStmtNeedPrivs()
  {
    reset();
  }
  void reset()
  {
    need_privs_.reset();
  }
X
xy0 已提交
5316
  int deep_copy(const ObStmtNeedPrivs &other, common::ObIAllocator &allocator);
O
oceanbase-admin 已提交
5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338
  NeedPrivs need_privs_;
  TO_STRING_KV(K_(need_privs));
};

#define CHECK_FLAG_NORMAL 0X0
#define CHECK_FLAG_DIRECT 0X1
#define CHECK_FLAG_WITH_GRANT_OPTION 0x2
/* flag Can proceed or */

// Define the permissions required in oracle mode.
// Record a set of system permissions, or an object permission
struct ObOraNeedPriv {
  ObOraNeedPriv()
      : db_name_(),
        grantee_id_(common::OB_INVALID_ID),
        obj_id_(common::OB_INVALID_ID),
        col_id_(common::OB_INVALID_ID),
        obj_type_(common::OB_INVALID_ID),
        check_flag_(false),
        obj_privs_(0),
        owner_id_(common::OB_INVALID_ID)
  {}
X
xy0 已提交
5339
  ObOraNeedPriv(const common::ObString &db_name, uint64_t grantee_id, uint64_t obj_id, uint64_t col_id,
O
oceanbase-admin 已提交
5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351
      uint64_t obj_type, uint32_t check_flag, ObPackedObjPriv obj_privs, uint64_t owner_id)
      : db_name_(db_name),
        grantee_id_(grantee_id),
        obj_id_(obj_id),
        col_id_(col_id),
        obj_type_(obj_type),
        check_flag_(check_flag),
        obj_privs_(obj_privs),
        owner_id_(owner_id)
  {}
  ~ObOraNeedPriv()
  {}
X
xy0 已提交
5352 5353
  int deep_copy(const ObOraNeedPriv &other, common::ObIAllocator &allocator);
  bool same_obj(const ObOraNeedPriv &other);
O
oceanbase-admin 已提交
5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370
  // ObOraNeedPriv& operator=(const ObOraNeedPriv &other);
  common::ObString db_name_;
  uint64_t grantee_id_;
  uint64_t obj_id_;
  uint64_t col_id_;
  uint64_t obj_type_;
  uint64_t check_flag_;
  ObPackedObjPriv obj_privs_;
  uint64_t owner_id_;
  TO_STRING_KV(
      K_(db_name), K_(grantee_id), K_(obj_id), K_(col_id), K_(obj_type), K_(check_flag), K_(obj_privs), K_(owner_id));
};

// Define the permissions required for a statement in oracle mode. Use an array to record,
// the system permissions and multiple object permissions are recorded in the array
struct ObStmtOraNeedPrivs {
  typedef common::ObFixedArray<ObOraNeedPriv, common::ObIAllocator> OraNeedPrivs;
X
xy0 已提交
5371
  explicit ObStmtOraNeedPrivs(common::ObIAllocator &alloc) : need_privs_(alloc)
O
oceanbase-admin 已提交
5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382
  {}
  ObStmtOraNeedPrivs() : need_privs_()
  {}
  ~ObStmtOraNeedPrivs()
  {
    reset();
  }
  void reset()
  {
    need_privs_.reset();
  }
X
xy0 已提交
5383
  int deep_copy(const ObStmtOraNeedPrivs &other, common::ObIAllocator &allocator);
O
oceanbase-admin 已提交
5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400
  OraNeedPrivs need_privs_;
  TO_STRING_KV(K_(need_privs));
};

struct ObSessionPrivInfo {
  ObSessionPrivInfo()
      : tenant_id_(common::OB_INVALID_ID),
        user_id_(common::OB_INVALID_ID),
        user_name_(),
        host_name_(),
        db_(),
        user_priv_set_(0),
        db_priv_set_(0),
        effective_tenant_id_(common::OB_INVALID_ID),
        enable_role_id_array_()
  {}
  ObSessionPrivInfo(const uint64_t tenant_id, const uint64_t effective_tenant_id, const uint64_t user_id,
X
xy0 已提交
5401
      const common::ObString &db, const ObPrivSet user_priv_set, const ObPrivSet db_priv_set)
O
oceanbase-admin 已提交
5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462
      : tenant_id_(tenant_id),
        user_id_(user_id),
        user_name_(),
        host_name_(),
        db_(db),
        user_priv_set_(user_priv_set),
        db_priv_set_(db_priv_set),
        effective_tenant_id_(effective_tenant_id),
        enable_role_id_array_()
  {}

  virtual ~ObSessionPrivInfo()
  {}

  void reset()
  {
    tenant_id_ = common::OB_INVALID_ID;
    effective_tenant_id_ = common::OB_INVALID_ID;
    user_id_ = common::OB_INVALID_ID;
    user_name_.reset();
    host_name_.reset();
    db_.reset();
    user_priv_set_ = 0;
    db_priv_set_ = 0;
    enable_role_id_array_.reset();
  }
  bool is_valid() const
  {
    return (tenant_id_ != common::OB_INVALID_ID) && (user_id_ != common::OB_INVALID_ID);
  }
  bool is_tenant_changed() const
  {
    return common::OB_INVALID_ID != effective_tenant_id_ && tenant_id_ != effective_tenant_id_;
  }
  void set_effective_tenant_id(uint64_t effective_tenant_id)
  {
    effective_tenant_id_ = effective_tenant_id;
  }
  uint64_t get_effective_tenant_id()
  {
    return effective_tenant_id_;
  }
  virtual TO_STRING_KV(K_(tenant_id), K_(effective_tenant_id), K_(user_id), K_(user_name), K_(host_name), K_(db),
      K_(user_priv_set), K_(db_priv_set));

  uint64_t tenant_id_;  // for privilege.Current login tenant. if normal tenant access
                        // sys tenant's object should use other method for priv checking.
  uint64_t user_id_;
  common::ObString user_name_;
  common::ObString host_name_;
  common::ObString db_;  // db name in current session
  ObPrivSet user_priv_set_;
  ObPrivSet db_priv_set_;  // user's db_priv_set of db
  // Only used for privilege check to determine whether there are currently tenants, otherwise the value is illegal
  uint64_t effective_tenant_id_;
  common::ObSEArray<uint64_t, 8> enable_role_id_array_;
};

struct ObUserLoginInfo {
  ObUserLoginInfo()
  {}
X
xy0 已提交
5463 5464
  ObUserLoginInfo(const common::ObString &tenant_name, const common::ObString &user_name,
      const common::ObString &client_ip, const common::ObString &passwd, const common::ObString &db)
O
oceanbase-admin 已提交
5465 5466 5467 5468 5469 5470 5471 5472
      : tenant_name_(tenant_name),
        user_name_(user_name),
        client_ip_(client_ip),
        passwd_(passwd),
        db_(db),
        scramble_str_()
  {}

X
xy0 已提交
5473 5474 5475
  ObUserLoginInfo(const common::ObString &tenant_name, const common::ObString &user_name,
      const common::ObString &client_ip, const common::ObString &passwd, const common::ObString &db,
      const common::ObString &scramble_str)
O
oceanbase-admin 已提交
5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496
      : tenant_name_(tenant_name),
        user_name_(user_name),
        client_ip_(client_ip),
        passwd_(passwd),
        db_(db),
        scramble_str_(scramble_str)
  {}

  TO_STRING_KV(K_(tenant_name), K_(user_name), K_(client_ip), K_(db), K_(scramble_str));
  common::ObString tenant_name_;
  common::ObString user_name_;
  common::ObString client_ip_;  // client ip for current user
  common::ObString passwd_;
  common::ObString db_;
  common::ObString scramble_str_;
};

// oracle compatible: define u/r system permissions
class ObSysPriv : public ObSchema, public ObPriv {
  OB_UNIS_VERSION(1);

G
gm 已提交
5497
public:
O
oceanbase-admin 已提交
5498 5499
  ObSysPriv() : ObPriv(), grantee_id_(common::OB_INVALID_ID)
  {}
X
xy0 已提交
5500
  explicit ObSysPriv(common::ObIAllocator *allocator) : ObSchema(allocator), ObPriv(allocator)
O
oceanbase-admin 已提交
5501 5502 5503
  {}
  virtual ~ObSysPriv()
  {}
X
xy0 已提交
5504
  ObSysPriv(const ObSysPriv &other) : ObSchema(), ObPriv(), grantee_id_(common::OB_INVALID_ID)
O
oceanbase-admin 已提交
5505 5506 5507
  {
    *this = other;
  }
X
xy0 已提交
5508
  ObSysPriv &operator=(const ObSysPriv &other);
O
oceanbase-admin 已提交
5509

X
xy0 已提交
5510
  static bool cmp_tenant_grantee_id(const ObSysPriv *lhs, const ObTenantUserId &tenant_grantee_id)
O
oceanbase-admin 已提交
5511 5512 5513
  {
    return (lhs->get_tenant_grantee_id() < tenant_grantee_id);
  }
X
xy0 已提交
5514
  static bool equal_tenant_grantee_id(const ObSysPriv *lhs, const ObTenantUserId &tenant_grantee_id)
O
oceanbase-admin 已提交
5515 5516 5517
  {
    return (lhs->get_tenant_grantee_id() == tenant_grantee_id);
  }
X
xy0 已提交
5518
  static bool cmp_tenant_id(const ObPriv *lhs, const uint64_t tenant_id)
O
oceanbase-admin 已提交
5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542
  {
    return (lhs->get_tenant_id() < tenant_id);
  }
  ObTenantUserId get_tenant_grantee_id() const
  {
    return ObTenantUserId(tenant_id_, grantee_id_);
  }
  void set_grantee_id(uint64_t grantee_id)
  {
    grantee_id_ = grantee_id;
  }
  uint64_t get_grantee_id() const
  {
    return grantee_id_;
  }
  void set_revoke()
  {
    user_id_ = 234;
  }
  bool is_revoke()
  {
    return user_id_ == 234;
  }

X
xy0 已提交
5543
  static bool cmp(const ObSysPriv *lhs, const ObSysPriv *rhs)
O
oceanbase-admin 已提交
5544 5545 5546
  {
    return (NULL != lhs && NULL != rhs) ? (lhs->get_key() < rhs->get_key()) : false;
  }
X
xy0 已提交
5547
  static bool cmp_key(const ObSysPriv *lhs, const ObSysPrivKey &key)
O
oceanbase-admin 已提交
5548 5549 5550 5551 5552 5553 5554
  {
    return NULL != lhs ? lhs->get_key() < key : false;
  }
  ObSysPrivKey get_key() const
  {
    return ObSysPrivKey(tenant_id_, grantee_id_);
  }
X
xy0 已提交
5555
  static bool equal(const ObSysPriv *lhs, const ObSysPriv *rhs)
O
oceanbase-admin 已提交
5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568
  {
    return (NULL != lhs && NULL != rhs) ? lhs->get_key() == rhs->get_key() : false;
  }  // point check
  // ObSysPrivKey get_original_key() const
  //{ return ObSysPrivKey(tenant_id_, user_id_); }

  // other methods
  virtual bool is_valid() const;
  virtual void reset();
  int64_t get_convert_size() const;
  TO_STRING_KV(K_(tenant_id), K_(user_id), K_(grantee_id), "privileges", ObPrintPrivSet(priv_set_), "packedprivarray",
      ObPrintPackedPrivArray(priv_array_));

G
gm 已提交
5569
private:
O
oceanbase-admin 已提交
5570 5571 5572
  uint64_t grantee_id_;
};

X
xy0 已提交
5573
int get_int_value(const common::ObString &str, int64_t &value);
O
oceanbase-admin 已提交
5574 5575

class ObHostnameStuct {
G
gm 已提交
5576
public:
O
oceanbase-admin 已提交
5577 5578 5579 5580 5581 5582
  ObHostnameStuct()
  {}
  ~ObHostnameStuct()
  {}
  static const uint32_t FAKE_PORT = 0;
  static const int MAX_IP_BITS = 128;
X
xy0 已提交
5583 5584 5585 5586 5587 5588
  static int get_int_value(const common::ObString &str, int64_t &value);
  static bool calc_ip(const common::ObString &host_ip, common::ObAddr &addr);
  static bool calc_ip_mask(const common::ObString &host_ip_mask, common::ObAddr &mask);
  static bool is_ip_match(const common::ObString &client_ip, common::ObString host_name);
  static bool is_wild_match(const common::ObString &client_ip, const common::ObString &host_name);
  static bool is_in_white_list(const common::ObString &client_ip, common::ObString &ip_white_list);
O
oceanbase-admin 已提交
5589 5590 5591 5592 5593 5594 5595 5596
};

enum ObHintFormat {
  HINT_NORMAL,
  HINT_LOCAL,
};

class ObFixedParam {
G
gm 已提交
5597
public:
O
oceanbase-admin 已提交
5598 5599 5600 5601
  ObFixedParam() : offset_(common::OB_INVALID_INDEX), value_()
  {}
  virtual ~ObFixedParam()
  {}
X
xy0 已提交
5602 5603
  bool has_equal_value(const common::ObObj &other_value) const;
  bool is_equal(const ObFixedParam &other_param) const;
O
oceanbase-admin 已提交
5604 5605 5606 5607 5608 5609 5610 5611
  VIRTUAL_TO_STRING_KV(K(offset_), K(value_));
  int64_t offset_;
  common::ObObj value_;
};

class ObMaxConcurrentParam {
  OB_UNIS_VERSION(1);

G
gm 已提交
5612
public:
O
oceanbase-admin 已提交
5613 5614
  static const int64_t UNLIMITED = -1;
  typedef common::ObArray<ObFixedParam, common::ObWrapperAllocator> FixParamStore;
X
xy0 已提交
5615
  explicit ObMaxConcurrentParam(common::ObIAllocator *allocator, const common::ObMemAttr &attr = common::ObMemAttr());
O
oceanbase-admin 已提交
5616 5617 5618
  virtual ~ObMaxConcurrentParam();
  int destroy();
  int64_t get_convert_size() const;
X
xy0 已提交
5619
  int assign(const ObMaxConcurrentParam &max_concurrent_param);
O
oceanbase-admin 已提交
5620 5621 5622 5623
  int64_t get_concurrent_num() const
  {
    return concurrent_num_;
  }
X
xy0 已提交
5624
  int match_fixed_param(const ParamStore &const_param_store, bool &is_match) const;
O
oceanbase-admin 已提交
5625 5626 5627 5628 5629 5630 5631 5632
  bool is_concurrent_limit_param() const
  {
    return concurrent_num_ != UNLIMITED;
  }
  bool is_outline_content_param() const
  {
    return !outline_content_.empty();
  }
X
xy0 已提交
5633 5634 5635
  int get_fixed_param_with_offset(int64_t offset, ObFixedParam &fixed_param, bool &is_found) const;
  int same_param_as(const ObMaxConcurrentParam &other, bool &is_same) const;
  void set_mem_attr(const common::ObMemAttr &attr)
O
oceanbase-admin 已提交
5636 5637 5638 5639 5640
  {
    mem_attr_ = attr;
  }
  VIRTUAL_TO_STRING_KV(K_(concurrent_num), K_(outline_content), K_(fixed_param_store));

G
gm 已提交
5641
private:
X
xy0 已提交
5642 5643
  int deep_copy_outline_content(const common::ObString &src);
  int deep_copy_param_value(const common::ObObj &src, common::ObObj &dest);
O
oceanbase-admin 已提交
5644

G
gm 已提交
5645
public:
X
xy0 已提交
5646
  common::ObIAllocator *allocator_;
O
oceanbase-admin 已提交
5647 5648 5649 5650 5651
  int64_t concurrent_num_;
  common::ObString outline_content_;
  common::ObMemAttr mem_attr_;
  FixParamStore fixed_param_store_;

G
gm 已提交
5652
private:
O
oceanbase-admin 已提交
5653 5654 5655 5656 5657
  DISALLOW_COPY_AND_ASSIGN(ObMaxConcurrentParam);
};

// used for outline manager
class ObOutlineParamsWrapper {
5658
  typedef common::ObArray<ObMaxConcurrentParam *, common::ObWrapperAllocatorWithAttr> OutlineParamsArray;
O
oceanbase-admin 已提交
5659 5660
  OB_UNIS_VERSION(1);

G
gm 已提交
5661
public:
O
oceanbase-admin 已提交
5662
  ObOutlineParamsWrapper();
X
xy0 已提交
5663
  explicit ObOutlineParamsWrapper(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
5664 5665
  ~ObOutlineParamsWrapper();
  int destroy();
X
xy0 已提交
5666 5667 5668
  int assign(const ObOutlineParamsWrapper &src);
  int set_allocator(common::ObIAllocator *allocator, const common::ObMemAttr &attr = common::ObMemAttr());
  OutlineParamsArray &get_outline_params()
O
oceanbase-admin 已提交
5669 5670 5671
  {
    return outline_params_;
  }
X
xy0 已提交
5672
  const OutlineParamsArray &get_outline_params() const
O
oceanbase-admin 已提交
5673 5674 5675
  {
    return outline_params_;
  }
X
xy0 已提交
5676
  ObMaxConcurrentParam *get_outline_param(int64_t index) const;
O
oceanbase-admin 已提交
5677 5678 5679 5680 5681
  int64_t get_convert_size() const;
  bool is_empty() const
  {
    return 0 == outline_params_.count();
  }
X
xy0 已提交
5682 5683 5684
  int add_param(const ObMaxConcurrentParam &param);
  int has_param(const ObMaxConcurrentParam &param, bool &has_param) const;
  int has_concurrent_limit_param(bool &has) const;
O
oceanbase-admin 已提交
5685 5686 5687 5688 5689 5690 5691 5692 5693
  int64_t get_param_count() const
  {
    return outline_params_.count();
  }
  void reset_allocator()
  {
    allocator_ = NULL;
    mem_attr_ = common::ObMemAttr();
  }
X
xy0 已提交
5694
  void set_mem_attr(const common::ObMemAttr &attr)
O
oceanbase-admin 已提交
5695 5696 5697 5698 5699
  {
    mem_attr_ = attr;
  };
  TO_STRING_KV(K_(outline_params));

G
gm 已提交
5700
private:
X
xy0 已提交
5701
  common::ObIAllocator *allocator_;
O
oceanbase-admin 已提交
5702 5703 5704
  OutlineParamsArray outline_params_;
  common::ObMemAttr mem_attr_;

G
gm 已提交
5705
private:
O
oceanbase-admin 已提交
5706 5707 5708 5709 5710 5711
  DISALLOW_COPY_AND_ASSIGN(ObOutlineParamsWrapper);
};

struct BaselineKey {
  OB_UNIS_VERSION(1);

G
gm 已提交
5712
public:
O
oceanbase-admin 已提交
5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727
  BaselineKey() : tenant_id_(common::OB_INVALID_ID), db_id_(common::OB_INVALID_ID)
  {}

  TO_STRING_KV(K_(tenant_id), K_(db_id), K_(constructed_sql), K_(params_info_str));

  inline uint64_t hash(uint64_t seed = 0) const
  {
    uint64_t hash_val = seed;
    hash_val = common::murmurhash(&tenant_id_, sizeof(tenant_id_), hash_val);
    hash_val = common::murmurhash(&db_id_, sizeof(db_id_), hash_val);
    hash_val = constructed_sql_.hash(hash_val);
    hash_val = params_info_str_.hash(hash_val);
    return hash_val;
  }

X
xy0 已提交
5728
  inline bool operator==(const BaselineKey &other_key) const
O
oceanbase-admin 已提交
5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750
  {
    return tenant_id_ == other_key.tenant_id_ && db_id_ == other_key.db_id_ &&
           constructed_sql_ == other_key.constructed_sql_ && params_info_str_ == other_key.params_info_str_;
  }

  void reset()
  {
    tenant_id_ = common::OB_INVALID_ID;
    db_id_ = common::OB_INVALID_ID;
    constructed_sql_.reset();
    params_info_str_.reset();
  }

  uint64_t tenant_id_;                // tenant id  -- ob sql layer obtain
  uint64_t db_id_;                    // database id -- ob sql layer obtain
  common::ObString constructed_sql_;  // Parameterized text string
  common::ObString params_info_str_;  // ObPlanSet obtain
};

class ObPlanBaselineInfo : public ObSchema {
  OB_UNIS_VERSION(1);

G
gm 已提交
5751
public:
O
oceanbase-admin 已提交
5752
  ObPlanBaselineInfo();
X
xy0 已提交
5753
  explicit ObPlanBaselineInfo(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
5754 5755
  virtual ~ObPlanBaselineInfo();

X
xy0 已提交
5756 5757
  ObPlanBaselineInfo(const ObPlanBaselineInfo &src_schema);
  ObPlanBaselineInfo &operator=(const ObPlanBaselineInfo &src_schema);
O
oceanbase-admin 已提交
5758 5759
  void reset();
  int64_t get_convert_size() const;
X
xy0 已提交
5760
  static bool cmp(const ObPlanBaselineInfo *lhs, const ObPlanBaselineInfo *rhs)
O
oceanbase-admin 已提交
5761 5762 5763
  {
    return (NULL != lhs && NULL != rhs) ? lhs->get_plan_baseline_id() < rhs->get_plan_baseline_id() : false;
  }
X
xy0 已提交
5764
  static bool equal(const ObPlanBaselineInfo *lhs, const ObPlanBaselineInfo *rhs)
O
oceanbase-admin 已提交
5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852
  {
    return (NULL != lhs && NULL != rhs) ? lhs->get_plan_baseline_id() == rhs->get_plan_baseline_id() : false;
  }

  inline uint64_t get_tenant_id() const
  {
    return key_.tenant_id_;
  }
  inline uint64_t get_database_id() const
  {
    return key_.db_id_;
  }
  inline uint64_t get_plan_baseline_id() const
  {
    return plan_baseline_id_;
  }
  inline int64_t get_schema_version() const
  {
    return schema_version_;
  }
  inline uint64_t get_plan_hash_value() const
  {
    return plan_hash_value_;
  }
  inline bool get_fixed() const
  {
    return fixed_;
  }
  inline bool get_enabled() const
  {
    return enabled_;
  }
  inline bool get_executions() const
  {
    return executions_;
  }
  inline bool get_cpu_time() const
  {
    return cpu_time_;
  }
  inline bool get_hints_all_worked() const
  {
    return hints_all_worked_;
  }

  inline void set_tenant_id(const uint64_t id)
  {
    key_.tenant_id_ = id;
  }
  inline void set_database_id(const uint64_t id)
  {
    key_.db_id_ = id;
  }
  inline void set_plan_baseline_id(uint64_t id)
  {
    plan_baseline_id_ = id;
  }
  inline void set_schema_version(int64_t version)
  {
    schema_version_ = version;
  }
  inline void set_plan_hash_value(uint64_t v)
  {
    plan_hash_value_ = v;
  }
  inline void set_fixed(bool v)
  {
    fixed_ = v;
  }
  inline void set_enabled(bool v)
  {
    enabled_ = v;
  }
  inline void set_executions(bool v)
  {
    executions_ = v;
  }
  inline void set_cpu_time(bool v)
  {
    cpu_time_ = v;
  }
  inline void set_hints_all_worked(bool hints_all_worked)
  {
    hints_all_worked_ = hints_all_worked;
  }

  // int set_plan_hash_value(const common::number::ObNumber &num);

X
xy0 已提交
5853
  int set_sql_id(const char *sql_id)
O
oceanbase-admin 已提交
5854 5855 5856
  {
    return deep_copy_str(sql_id, sql_id_);
  }
X
xy0 已提交
5857
  int set_sql_id(const common::ObString &sql_id)
O
oceanbase-admin 已提交
5858 5859 5860 5861
  {
    return deep_copy_str(sql_id, sql_id_);
  }

X
xy0 已提交
5862
  inline const char *get_sql_id() const
O
oceanbase-admin 已提交
5863 5864 5865
  {
    return extract_str(sql_id_);
  }
X
xy0 已提交
5866
  inline const common::ObString &get_sql_id_str() const
O
oceanbase-admin 已提交
5867 5868 5869 5870
  {
    return sql_id_;
  }

X
xy0 已提交
5871
  int set_hints_info(const char *hints_info)
O
oceanbase-admin 已提交
5872 5873 5874
  {
    return deep_copy_str(hints_info, hints_info_);
  }
X
xy0 已提交
5875
  int set_hints_info(const common::ObString &hints_info)
O
oceanbase-admin 已提交
5876 5877 5878 5879
  {
    return deep_copy_str(hints_info, hints_info_);
  }

X
xy0 已提交
5880
  inline const char *get_hints_info() const
O
oceanbase-admin 已提交
5881 5882 5883
  {
    return extract_str(hints_info_);
  }
X
xy0 已提交
5884
  inline const common::ObString &get_hints_info_str() const
O
oceanbase-admin 已提交
5885 5886 5887 5888
  {
    return hints_info_;
  }

X
xy0 已提交
5889
  int set_outline_data(const char *outline_data)
O
oceanbase-admin 已提交
5890 5891 5892
  {
    return deep_copy_str(outline_data, outline_data_);
  }
X
xy0 已提交
5893
  int set_outline_data(const common::ObString &outline_data)
O
oceanbase-admin 已提交
5894 5895 5896 5897
  {
    return deep_copy_str(outline_data, outline_data_);
  }

X
xy0 已提交
5898
  inline const char *get_outline_data() const
O
oceanbase-admin 已提交
5899 5900 5901
  {
    return extract_str(outline_data_);
  }
X
xy0 已提交
5902
  inline const common::ObString &get_outline_data_str() const
O
oceanbase-admin 已提交
5903 5904 5905 5906
  {
    return outline_data_;
  }

X
xy0 已提交
5907
  int set_sql_text(const char *constructed_sql)
O
oceanbase-admin 已提交
5908 5909 5910
  {
    return deep_copy_str(constructed_sql, key_.constructed_sql_);
  }
X
xy0 已提交
5911
  int set_sql_text(const common::ObString &constructed_sql)
O
oceanbase-admin 已提交
5912 5913 5914 5915
  {
    return deep_copy_str(constructed_sql, key_.constructed_sql_);
  }

X
xy0 已提交
5916
  inline const char *get_sql_text() const
O
oceanbase-admin 已提交
5917 5918 5919
  {
    return extract_str(key_.constructed_sql_);
  }
X
xy0 已提交
5920
  inline const common::ObString &get_sql_text_str() const
O
oceanbase-admin 已提交
5921 5922 5923 5924
  {
    return key_.constructed_sql_;
  }

X
xy0 已提交
5925
  int set_params_info(const char *params_info)
O
oceanbase-admin 已提交
5926 5927 5928
  {
    return deep_copy_str(params_info, key_.params_info_str_);
  }
X
xy0 已提交
5929
  int set_params_info(const common::ObString &params_info)
O
oceanbase-admin 已提交
5930 5931 5932 5933
  {
    return deep_copy_str(params_info, key_.params_info_str_);
  }

X
xy0 已提交
5934
  inline const char *get_params_info() const
O
oceanbase-admin 已提交
5935 5936 5937
  {
    return extract_str(key_.params_info_str_);
  }
X
xy0 已提交
5938
  inline const common::ObString &get_params_info_str() const
O
oceanbase-admin 已提交
5939 5940 5941 5942 5943 5944 5945
  {
    return key_.params_info_str_;
  }

  VIRTUAL_TO_STRING_KV(K_(key), K_(plan_baseline_id), K_(schema_version), K_(outline_data), K_(plan_hash_value),
      K_(fixed), K_(enabled), K_(executions), K_(cpu_time), K_(hints_info), K_(hints_all_worked));

G
gm 已提交
5946
public:
O
oceanbase-admin 已提交
5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963
  BaselineKey key_;
  uint64_t plan_baseline_id_;
  int64_t schema_version_;         // the last modify timestamp of this version
  common::ObString outline_data_;  // Hint collection of fixed plan
  common::ObString sql_id_;        // sql id //The last byte stores'\0'
  uint64_t plan_hash_value_;
  bool fixed_;
  bool enabled_;
  int64_t executions_;  // The total number of executions in the evolution process
  int64_t cpu_time_;    // The total CPU time consumed during the evolution process
  common::ObString hints_info_;
  bool hints_all_worked_;
};

class ObOutlineInfo : public ObSchema {
  OB_UNIS_VERSION(1);

G
gm 已提交
5964
public:
O
oceanbase-admin 已提交
5965
  ObOutlineInfo();
X
xy0 已提交
5966
  explicit ObOutlineInfo(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
5967
  virtual ~ObOutlineInfo();
X
xy0 已提交
5968 5969
  ObOutlineInfo(const ObOutlineInfo &src_schema);
  ObOutlineInfo &operator=(const ObOutlineInfo &src_schema);
O
oceanbase-admin 已提交
5970 5971 5972 5973
  void reset();
  bool is_valid() const;
  bool is_valid_for_replace() const;
  int64_t get_convert_size() const;
X
xy0 已提交
5974
  static bool cmp(const ObOutlineInfo *lhs, const ObOutlineInfo *rhs)
O
oceanbase-admin 已提交
5975 5976 5977
  {
    return (NULL != lhs && NULL != rhs) ? lhs->get_outline_id() < rhs->get_outline_id() : false;
  }
X
xy0 已提交
5978
  static bool equal(const ObOutlineInfo *lhs, const ObOutlineInfo *rhs)
O
oceanbase-admin 已提交
5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998
  {
    return (NULL != lhs && NULL != rhs) ? lhs->get_outline_id() == rhs->get_outline_id() : false;
  }

  inline void set_tenant_id(const uint64_t id)
  {
    tenant_id_ = id;
  }
  inline void set_database_id(const uint64_t id)
  {
    database_id_ = id;
  }
  inline void set_outline_id(uint64_t id)
  {
    outline_id_ = id;
  }
  inline void set_schema_version(int64_t version)
  {
    schema_version_ = version;
  }
X
xy0 已提交
5999
  int set_name(const char *name)
O
oceanbase-admin 已提交
6000 6001 6002
  {
    return deep_copy_str(name, name_);
  }
X
xy0 已提交
6003
  int set_name(const common::ObString &name)
O
oceanbase-admin 已提交
6004 6005 6006
  {
    return deep_copy_str(name, name_);
  }
X
xy0 已提交
6007
  int set_signature(const char *sig)
O
oceanbase-admin 已提交
6008 6009 6010
  {
    return deep_copy_str(sig, signature_);
  }
X
xy0 已提交
6011
  int set_signature(const common::ObString &sig)
O
oceanbase-admin 已提交
6012 6013 6014
  {
    return deep_copy_str(sig, signature_);
  }
X
xy0 已提交
6015
  int set_sql_id(const char *sql_id)
O
oceanbase-admin 已提交
6016 6017 6018
  {
    return deep_copy_str(sql_id, sql_id_);
  }
X
xy0 已提交
6019
  int set_sql_id(const common::ObString &sql_id)
O
oceanbase-admin 已提交
6020 6021 6022
  {
    return deep_copy_str(sql_id, sql_id_);
  }
X
xy0 已提交
6023 6024
  int set_outline_params(const common::ObString &outline_params_str);
  int set_outline_content(const char *content)
O
oceanbase-admin 已提交
6025 6026 6027
  {
    return deep_copy_str(content, outline_content_);
  }
X
xy0 已提交
6028
  int set_outline_content(const common::ObString &content)
O
oceanbase-admin 已提交
6029 6030 6031
  {
    return deep_copy_str(content, outline_content_);
  }
X
xy0 已提交
6032
  int set_sql_text(const char *sql)
O
oceanbase-admin 已提交
6033 6034 6035
  {
    return deep_copy_str(sql, sql_text_);
  }
X
xy0 已提交
6036
  int set_sql_text(const common::ObString &sql)
O
oceanbase-admin 已提交
6037 6038 6039
  {
    return deep_copy_str(sql, sql_text_);
  }
X
xy0 已提交
6040
  int set_outline_target(const char *target)
O
oceanbase-admin 已提交
6041 6042 6043
  {
    return deep_copy_str(target, outline_target_);
  }
X
xy0 已提交
6044
  int set_outline_target(const common::ObString &target)
O
oceanbase-admin 已提交
6045 6046 6047
  {
    return deep_copy_str(target, outline_target_);
  }
X
xy0 已提交
6048
  int set_owner(const char *owner)
O
oceanbase-admin 已提交
6049 6050 6051
  {
    return deep_copy_str(owner, owner_);
  }
X
xy0 已提交
6052
  int set_owner(const common::ObString &owner)
O
oceanbase-admin 已提交
6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063
  {
    return deep_copy_str(owner, owner_);
  }
  void set_owner_id(const uint64_t owner_id)
  {
    owner_id_ = owner_id;
  }
  void set_used(const bool used)
  {
    used_ = used;
  }
X
xy0 已提交
6064
  int set_version(const char *version)
O
oceanbase-admin 已提交
6065 6066 6067
  {
    return deep_copy_str(version, version_);
  }
X
xy0 已提交
6068
  int set_version(const common::ObString &version)
O
oceanbase-admin 已提交
6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120
  {
    return deep_copy_str(version, version_);
  }
  void set_compatible(const bool compatible)
  {
    compatible_ = compatible;
  }
  void set_enabled(const bool enabled)
  {
    enabled_ = enabled;
  }
  void set_format(const ObHintFormat hint_format)
  {
    format_ = hint_format;
  }

  inline uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
  inline uint64_t get_owner_id() const
  {
    return owner_id_;
  }
  inline uint64_t get_database_id() const
  {
    return database_id_;
  }
  inline uint64_t get_outline_id() const
  {
    return outline_id_;
  }
  inline int64_t get_schema_version() const
  {
    return schema_version_;
  }
  inline bool is_used() const
  {
    return used_;
  }
  inline bool is_enabled() const
  {
    return enabled_;
  }
  inline bool is_compatible() const
  {
    return compatible_;
  }
  inline ObHintFormat get_hint_format() const
  {
    return format_;
  }
X
xy0 已提交
6121
  inline const char *get_name() const
O
oceanbase-admin 已提交
6122 6123 6124
  {
    return extract_str(name_);
  }
X
xy0 已提交
6125
  inline const common::ObString &get_name_str() const
O
oceanbase-admin 已提交
6126 6127 6128
  {
    return name_;
  }
X
xy0 已提交
6129
  inline const char *get_signature() const
O
oceanbase-admin 已提交
6130 6131 6132
  {
    return extract_str(signature_);
  }
X
xy0 已提交
6133
  inline const char *get_sql_id() const
O
oceanbase-admin 已提交
6134 6135 6136
  {
    return extract_str(sql_id_);
  }
X
xy0 已提交
6137
  inline const common::ObString &get_sql_id_str() const
O
oceanbase-admin 已提交
6138 6139 6140
  {
    return sql_id_;
  }
X
xy0 已提交
6141
  inline const common::ObString &get_signature_str() const
O
oceanbase-admin 已提交
6142 6143 6144
  {
    return signature_;
  }
X
xy0 已提交
6145
  inline const char *get_outline_content() const
O
oceanbase-admin 已提交
6146 6147 6148
  {
    return extract_str(outline_content_);
  }
X
xy0 已提交
6149
  inline const common::ObString &get_outline_content_str() const
O
oceanbase-admin 已提交
6150 6151 6152
  {
    return outline_content_;
  }
X
xy0 已提交
6153
  inline const char *get_sql_text() const
O
oceanbase-admin 已提交
6154 6155 6156
  {
    return extract_str(sql_text_);
  }
X
xy0 已提交
6157
  inline const common::ObString &get_sql_text_str() const
O
oceanbase-admin 已提交
6158 6159 6160
  {
    return sql_text_;
  }
X
xy0 已提交
6161
  inline common::ObString &get_sql_text_str()
O
oceanbase-admin 已提交
6162 6163 6164
  {
    return sql_text_;
  }
X
xy0 已提交
6165
  inline const char *get_outline_target() const
O
oceanbase-admin 已提交
6166 6167 6168
  {
    return extract_str(outline_target_);
  }
X
xy0 已提交
6169
  inline const common::ObString &get_outline_target_str() const
O
oceanbase-admin 已提交
6170 6171 6172
  {
    return outline_target_;
  }
X
xy0 已提交
6173
  inline common::ObString &get_outline_target_str()
O
oceanbase-admin 已提交
6174 6175 6176
  {
    return outline_target_;
  }
X
xy0 已提交
6177
  inline const char *get_owner() const
O
oceanbase-admin 已提交
6178 6179 6180
  {
    return extract_str(owner_);
  }
X
xy0 已提交
6181
  inline const common::ObString &get_owner_str() const
O
oceanbase-admin 已提交
6182 6183 6184
  {
    return owner_;
  }
X
xy0 已提交
6185
  inline const char *get_version() const
O
oceanbase-admin 已提交
6186 6187 6188
  {
    return extract_str(version_);
  }
X
xy0 已提交
6189
  inline const common::ObString &get_version_str() const
O
oceanbase-admin 已提交
6190 6191 6192
  {
    return version_;
  }
X
xy0 已提交
6193
  int get_visible_signature(common::ObString &visiable_signature) const;
O
oceanbase-admin 已提交
6194
  int get_outline_sql(
X
xy0 已提交
6195 6196 6197
      common::ObIAllocator &allocator, const sql::ObSQLSessionInfo &session, common::ObString &outline_sql) const;
  int get_hex_str_from_outline_params(common::ObString &hex_str, common::ObIAllocator &allocator) const;
  const ObOutlineParamsWrapper &get_outline_params_wrapper() const
O
oceanbase-admin 已提交
6198 6199 6200
  {
    return outline_params_wrapper_;
  }
X
xy0 已提交
6201
  ObOutlineParamsWrapper &get_outline_params_wrapper()
O
oceanbase-admin 已提交
6202 6203 6204 6205 6206 6207 6208
  {
    return outline_params_wrapper_;
  }
  bool has_outline_params() const
  {
    return outline_params_wrapper_.get_outline_params().count() > 0;
  }
X
xy0 已提交
6209
  int has_concurrent_limit_param(bool &has) const;
O
oceanbase-admin 已提交
6210
  int gen_valid_allocator();
X
xy0 已提交
6211 6212 6213
  int add_param(const ObMaxConcurrentParam &src_param);
  static int gen_limit_sql(const common::ObString &visible_signature, const ObMaxConcurrentParam *param,
      const sql::ObSQLSessionInfo &session, common::ObIAllocator &allocator, common::ObString &limit_sql);
O
oceanbase-admin 已提交
6214 6215 6216
  VIRTUAL_TO_STRING_KV(K_(tenant_id), K_(database_id), K_(outline_id), K_(schema_version), K_(name), K_(signature),
      K_(sql_id), K_(outline_content), K_(sql_text), K_(owner_id), K_(owner), K_(used), K_(compatible), K_(enabled),
      K_(format), K_(outline_params_wrapper), K_(outline_target));
X
xy0 已提交
6217
  static bool is_sql_id_valid(const common::ObString &sql_id);
O
oceanbase-admin 已提交
6218

G
gm 已提交
6219
private:
X
xy0 已提交
6220 6221 6222 6223
  static int replace_question_mark(const common::ObString &not_param_sql, const ObMaxConcurrentParam &concurrent_param,
      int64_t start_pos, int64_t cur_pos, int64_t &question_mark_offset, common::ObSqlString &string_helper);
  static int replace_not_param(const common::ObString &not_param_sql, const ParseNode &node, int64_t start_pos,
      int64_t cur_pos, common::ObSqlString &string_helper);
O
oceanbase-admin 已提交
6224

G
gm 已提交
6225
protected:
O
oceanbase-admin 已提交
6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246
  uint64_t tenant_id_;
  uint64_t database_id_;
  uint64_t outline_id_;
  int64_t schema_version_;      // the last modify timestamp of this version
  common::ObString name_;       // outline name
  common::ObString signature_;  // SQL after constant parameterization
  common::ObString sql_id_;     // Used to directly determine sql_id
  common::ObString outline_content_;
  common::ObString sql_text_;
  common::ObString outline_target_;
  common::ObString owner_;
  bool used_;
  common::ObString version_;
  bool compatible_;
  bool enabled_;
  ObHintFormat format_;
  uint64_t owner_id_;
  ObOutlineParamsWrapper outline_params_wrapper_;
};

class ObDbLinkBaseInfo : public ObSchema {
G
gm 已提交
6247
public:
O
oceanbase-admin 已提交
6248 6249 6250 6251
  ObDbLinkBaseInfo() : ObSchema()
  {
    reset();
  }
X
xy0 已提交
6252
  explicit ObDbLinkBaseInfo(common::ObIAllocator *allocator) : ObSchema(allocator)
O
oceanbase-admin 已提交
6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274
  {
    reset();
  }
  virtual ~ObDbLinkBaseInfo()
  {}

  inline void set_tenant_id(const uint64_t id)
  {
    tenant_id_ = id;
  }
  inline void set_owner_id(const uint64_t id)
  {
    owner_id_ = id;
  }
  inline void set_dblink_id(const uint64_t id)
  {
    dblink_id_ = id;
  }
  inline void set_schema_version(const int64_t version)
  {
    schema_version_ = version;
  }
X
xy0 已提交
6275
  inline int set_dblink_name(const common::ObString &name)
O
oceanbase-admin 已提交
6276 6277 6278
  {
    return deep_copy_str(name, dblink_name_);
  }
X
xy0 已提交
6279
  inline int set_cluster_name(const common::ObString &name)
O
oceanbase-admin 已提交
6280 6281 6282
  {
    return deep_copy_str(name, cluster_name_);
  }
X
xy0 已提交
6283
  inline int set_tenant_name(const common::ObString &name)
O
oceanbase-admin 已提交
6284 6285 6286
  {
    return deep_copy_str(name, tenant_name_);
  }
X
xy0 已提交
6287
  inline int set_user_name(const common::ObString &name)
O
oceanbase-admin 已提交
6288 6289 6290
  {
    return deep_copy_str(name, user_name_);
  }
X
xy0 已提交
6291
  inline int set_password(const common::ObString &password)
O
oceanbase-admin 已提交
6292 6293 6294
  {
    return deep_copy_str(password, password_);
  }
X
xy0 已提交
6295
  inline void set_host_addr(const common::ObAddr &addr)
O
oceanbase-admin 已提交
6296 6297 6298
  {
    host_addr_ = addr;
  }
X
xy0 已提交
6299
  inline int set_host_ip(const common::ObString &host_ip)
O
oceanbase-admin 已提交
6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324
  {
    host_addr_.set_ip_addr(host_ip, 0);
    return common::OB_SUCCESS;
  }
  inline void set_host_port(const int32_t host_port)
  {
    host_addr_.set_port(host_port);
  }

  inline uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
  inline uint64_t get_owner_id() const
  {
    return owner_id_;
  }
  inline uint64_t get_dblink_id() const
  {
    return dblink_id_;
  }
  inline int64_t get_schema_version() const
  {
    return schema_version_;
  }
X
xy0 已提交
6325
  inline const common::ObString &get_dblink_name() const
O
oceanbase-admin 已提交
6326 6327 6328
  {
    return dblink_name_;
  }
X
xy0 已提交
6329
  inline const common::ObString &get_cluster_name() const
O
oceanbase-admin 已提交
6330 6331 6332
  {
    return cluster_name_;
  }
X
xy0 已提交
6333
  inline const common::ObString &get_tenant_name() const
O
oceanbase-admin 已提交
6334 6335 6336
  {
    return tenant_name_;
  }
X
xy0 已提交
6337
  inline const common::ObString &get_user_name() const
O
oceanbase-admin 已提交
6338 6339 6340
  {
    return user_name_;
  }
X
xy0 已提交
6341
  inline const common::ObString &get_password() const
O
oceanbase-admin 已提交
6342 6343 6344
  {
    return password_;
  }
X
xy0 已提交
6345
  inline const common::ObAddr &get_host_addr() const
O
oceanbase-admin 已提交
6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362
  {
    return host_addr_;
  }
  inline uint32_t get_host_ip() const
  {
    return host_addr_.get_ipv4();
  }
  inline int32_t get_host_port() const
  {
    return host_addr_.get_port();
  }

  void reset();
  virtual bool is_valid() const;
  VIRTUAL_TO_STRING_KV(K_(tenant_id), K_(owner_id), K_(dblink_id), K_(dblink_name), K_(cluster_name), K_(tenant_name),
      K_(user_name), K_(host_addr));

G
gm 已提交
6363
protected:
O
oceanbase-admin 已提交
6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376
  uint64_t tenant_id_;
  uint64_t owner_id_;
  uint64_t dblink_id_;
  int64_t schema_version_;
  common::ObString dblink_name_;
  common::ObString cluster_name_;
  common::ObString tenant_name_;
  common::ObString user_name_;
  common::ObString password_;  // only encrypt when write to sys table.
  common::ObAddr host_addr_;
};

struct ObTenantDbLinkId {
G
gm 已提交
6377
public:
O
oceanbase-admin 已提交
6378 6379 6380 6381
  ObTenantDbLinkId() : tenant_id_(common::OB_INVALID_ID), dblink_id_(common::OB_INVALID_ID)
  {}
  ObTenantDbLinkId(uint64_t tenant_id, uint64_t dblink_id) : tenant_id_(tenant_id), dblink_id_(dblink_id)
  {}
X
xy0 已提交
6382
  ObTenantDbLinkId(const ObTenantDbLinkId &other) : tenant_id_(other.tenant_id_), dblink_id_(other.dblink_id_)
O
oceanbase-admin 已提交
6383
  {}
X
xy0 已提交
6384
  ObTenantDbLinkId &operator=(const ObTenantDbLinkId &other)
O
oceanbase-admin 已提交
6385 6386 6387 6388 6389
  {
    tenant_id_ = other.tenant_id_;
    dblink_id_ = other.dblink_id_;
    return *this;
  }
X
xy0 已提交
6390
  bool operator==(const ObTenantDbLinkId &rhs) const
O
oceanbase-admin 已提交
6391 6392 6393
  {
    return (tenant_id_ == rhs.tenant_id_) && (dblink_id_ == rhs.dblink_id_);
  }
X
xy0 已提交
6394
  bool operator!=(const ObTenantDbLinkId &rhs) const
O
oceanbase-admin 已提交
6395 6396 6397
  {
    return !(*this == rhs);
  }
X
xy0 已提交
6398
  bool operator<(const ObTenantDbLinkId &rhs) const
O
oceanbase-admin 已提交
6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422
  {
    return (tenant_id_ < rhs.tenant_id_) || (tenant_id_ == rhs.tenant_id_ && dblink_id_ < rhs.dblink_id_);
  }
  inline uint64_t hash() const
  {
    uint64_t hash_ret = 0;
    hash_ret = common::murmurhash(&tenant_id_, sizeof(tenant_id_), hash_ret);
    hash_ret = common::murmurhash(&dblink_id_, sizeof(dblink_id_), hash_ret);
    return hash_ret;
  }
  inline bool is_valid() const
  {
    return (tenant_id_ != common::OB_INVALID_ID) && (dblink_id_ != common::OB_INVALID_ID);
  }
  inline uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
  inline uint64_t get_dblink_id() const
  {
    return dblink_id_;
  }
  TO_STRING_KV(K_(tenant_id), K_(dblink_id));

G
gm 已提交
6423
private:
O
oceanbase-admin 已提交
6424 6425 6426 6427 6428 6429 6430
  uint64_t tenant_id_;
  uint64_t dblink_id_;
};

class ObDbLinkInfo : public ObDbLinkBaseInfo {
  OB_UNIS_VERSION(1);

G
gm 已提交
6431
public:
O
oceanbase-admin 已提交
6432 6433
  ObDbLinkInfo() : ObDbLinkBaseInfo()
  {}
X
xy0 已提交
6434
  explicit ObDbLinkInfo(common::ObIAllocator *allocator) : ObDbLinkBaseInfo(allocator)
O
oceanbase-admin 已提交
6435 6436 6437 6438 6439 6440
  {}
  virtual ~ObDbLinkInfo()
  {}
};

class ObDbLinkSchema : public ObDbLinkBaseInfo {
G
gm 已提交
6441
public:
O
oceanbase-admin 已提交
6442 6443
  ObDbLinkSchema() : ObDbLinkBaseInfo()
  {}
X
xy0 已提交
6444
  explicit ObDbLinkSchema(common::ObIAllocator *allocator) : ObDbLinkBaseInfo(allocator)
O
oceanbase-admin 已提交
6445
  {}
X
xy0 已提交
6446
  ObDbLinkSchema(const ObDbLinkSchema &src_schema);
O
oceanbase-admin 已提交
6447 6448
  virtual ~ObDbLinkSchema()
  {}
X
xy0 已提交
6449 6450
  ObDbLinkSchema &operator=(const ObDbLinkSchema &other);
  bool operator==(const ObDbLinkSchema &other) const;
O
oceanbase-admin 已提交
6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465

  inline ObTenantDbLinkId get_tenant_dblink_id() const
  {
    return ObTenantDbLinkId(tenant_id_, dblink_id_);
  }
  inline int64_t get_convert_size() const
  {
    return sizeof(ObDbLinkSchema) + dblink_name_.length() + 1 + cluster_name_.length() + 1 + tenant_name_.length() + 1 +
           user_name_.length() + 1 + password_.length() + 1;
  }
};

class ObSynonymInfo : public ObSchema {
  OB_UNIS_VERSION(1);

G
gm 已提交
6466
public:
O
oceanbase-admin 已提交
6467
  ObSynonymInfo();
X
xy0 已提交
6468
  explicit ObSynonymInfo(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
6469 6470
  VIRTUAL_TO_STRING_KV(K_(tenant_id), K_(database_id), K_(synonym_id), K_(schema_version));
  virtual ~ObSynonymInfo();
X
xy0 已提交
6471 6472
  ObSynonymInfo &operator=(const ObSynonymInfo &src_schema);
  ObSynonymInfo(const ObSynonymInfo &src_schema);
O
oceanbase-admin 已提交
6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493
  inline void set_tenant_id(const uint64_t id)
  {
    tenant_id_ = id;
  }
  inline void set_database_id(const uint64_t id)
  {
    database_id_ = id;
  }
  inline void set_object_database_id(const uint64_t id)
  {
    object_db_id_ = id;
  }
  inline void set_synonym_id(uint64_t id)
  {
    synonym_id_ = id;
  }
  inline void set_schema_version(int64_t version)
  {
    schema_version_ = version;
  }
  int64_t get_convert_size() const;
X
xy0 已提交
6494
  int set_synonym_name(const char *name)
O
oceanbase-admin 已提交
6495 6496 6497
  {
    return deep_copy_str(name, name_);
  }
X
xy0 已提交
6498
  int set_synonym_name(const common::ObString &name)
O
oceanbase-admin 已提交
6499 6500 6501
  {
    return deep_copy_str(name, name_);
  }
X
xy0 已提交
6502
  int set_object_name(const char *name)
O
oceanbase-admin 已提交
6503 6504 6505
  {
    return deep_copy_str(name, object_name_);
  }
X
xy0 已提交
6506
  int set_object_name(const common::ObString &name)
O
oceanbase-admin 已提交
6507 6508 6509
  {
    return deep_copy_str(name, object_name_);
  }
X
xy0 已提交
6510
  int set_version(const char *version)
O
oceanbase-admin 已提交
6511 6512 6513
  {
    return deep_copy_str(version, version_);
  }
X
xy0 已提交
6514
  int set_version(const common::ObString &version)
O
oceanbase-admin 已提交
6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533
  {
    return deep_copy_str(version, version_);
  }
  inline uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
  inline uint64_t get_database_id() const
  {
    return database_id_;
  }
  inline uint64_t get_synonym_id() const
  {
    return synonym_id_;
  }
  inline int64_t get_schema_version() const
  {
    return schema_version_;
  }
X
xy0 已提交
6534
  inline const char *get_synonym_name() const
O
oceanbase-admin 已提交
6535 6536 6537
  {
    return extract_str(name_);
  }
X
xy0 已提交
6538
  inline const common::ObString &get_synonym_name_str() const
O
oceanbase-admin 已提交
6539 6540 6541
  {
    return name_;
  }
X
xy0 已提交
6542
  inline const char *get_object_name() const
O
oceanbase-admin 已提交
6543 6544 6545
  {
    return extract_str(object_name_);
  }
X
xy0 已提交
6546
  inline const common::ObString &get_object_name_str() const
O
oceanbase-admin 已提交
6547 6548 6549
  {
    return object_name_;
  }
X
xy0 已提交
6550
  inline const char *get_version() const
O
oceanbase-admin 已提交
6551 6552 6553
  {
    return extract_str(version_);
  }
X
xy0 已提交
6554
  inline const common::ObString &get_version_str() const
O
oceanbase-admin 已提交
6555 6556 6557 6558 6559 6560 6561 6562 6563
  {
    return version_;
  }
  inline uint64_t get_object_database_id() const
  {
    return object_db_id_;
  }
  void reset();

G
gm 已提交
6564
private:
O
oceanbase-admin 已提交
6565
  // void *alloc(int64_t size);
G
gm 已提交
6566
private:
O
oceanbase-admin 已提交
6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580
  uint64_t tenant_id_;
  uint64_t database_id_;
  uint64_t synonym_id_;
  int64_t schema_version_;  // the last modify timestamp of this version
  common::ObString name_;   // synonym name
  common::ObString version_;
  common::ObString object_name_;
  uint64_t object_db_id_;
  // common::ObArenaAllocator allocator_;
};

struct ObTenantUDFId {
  OB_UNIS_VERSION(1);

G
gm 已提交
6581
public:
O
oceanbase-admin 已提交
6582 6583
  ObTenantUDFId() : tenant_id_(common::OB_INVALID_ID), udf_name_()
  {}
X
xy0 已提交
6584
  ObTenantUDFId(const uint64_t tenant_id, const common::ObString &name) : tenant_id_(tenant_id), udf_name_(name)
O
oceanbase-admin 已提交
6585
  {}
X
xy0 已提交
6586
  bool operator==(const ObTenantUDFId &rhs) const
O
oceanbase-admin 已提交
6587 6588 6589
  {
    return (tenant_id_ == rhs.tenant_id_) && (udf_name_ == rhs.udf_name_);
  }
X
xy0 已提交
6590
  bool operator!=(const ObTenantUDFId &rhs) const
O
oceanbase-admin 已提交
6591 6592 6593
  {
    return !(*this == rhs);
  }
X
xy0 已提交
6594
  bool operator<(const ObTenantUDFId &rhs) const
O
oceanbase-admin 已提交
6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620
  {
    bool bret = tenant_id_ < rhs.tenant_id_;
    if (tenant_id_ == rhs.tenant_id_) {
      bret = udf_name_ < rhs.udf_name_;
    }
    return bret;
  }
  inline uint64_t hash() const
  {
    uint64_t hash_ret = 0;
    hash_ret = common::murmurhash(&tenant_id_, sizeof(tenant_id_), 0);
    hash_ret = common::murmurhash(udf_name_.ptr(), udf_name_.length(), hash_ret);
    return hash_ret;
  }
  bool is_valid() const
  {
    return (tenant_id_ != common::OB_INVALID_ID) && (udf_name_.length() != 0);
  }
  TO_STRING_KV(K_(tenant_id), K_(udf_name));
  uint64_t tenant_id_;
  common::ObString udf_name_;
};

struct ObTenantSynonymId {
  OB_UNIS_VERSION(1);

G
gm 已提交
6621
public:
O
oceanbase-admin 已提交
6622 6623 6624 6625 6626
  ObTenantSynonymId() : tenant_id_(common::OB_INVALID_ID), synonym_id_(common::OB_INVALID_ID)
  {}
  ObTenantSynonymId(const uint64_t tenant_id, const uint64_t synonym_id)
      : tenant_id_(tenant_id), synonym_id_(synonym_id)
  {}
X
xy0 已提交
6627
  bool operator==(const ObTenantSynonymId &rhs) const
O
oceanbase-admin 已提交
6628 6629 6630
  {
    return (tenant_id_ == rhs.tenant_id_) && (synonym_id_ == rhs.synonym_id_);
  }
X
xy0 已提交
6631
  bool operator!=(const ObTenantSynonymId &rhs) const
O
oceanbase-admin 已提交
6632 6633 6634
  {
    return !(*this == rhs);
  }
X
xy0 已提交
6635
  bool operator<(const ObTenantSynonymId &rhs) const
O
oceanbase-admin 已提交
6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661
  {
    bool bret = tenant_id_ < rhs.tenant_id_;
    if (tenant_id_ == rhs.tenant_id_) {
      bret = synonym_id_ < rhs.synonym_id_;
    }
    return bret;
  }
  inline uint64_t hash() const
  {
    uint64_t hash_ret = 0;
    hash_ret = common::murmurhash(&tenant_id_, sizeof(tenant_id_), 0);
    hash_ret = common::murmurhash(&synonym_id_, sizeof(synonym_id_), hash_ret);
    return hash_ret;
  }
  bool is_valid() const
  {
    return (tenant_id_ != common::OB_INVALID_ID) && (synonym_id_ != common::OB_INVALID_ID);
  }
  TO_STRING_KV(K_(tenant_id), K_(synonym_id));
  uint64_t tenant_id_;
  uint64_t synonym_id_;
};

struct ObTenantSequenceId {
  OB_UNIS_VERSION(1);

G
gm 已提交
6662
public:
O
oceanbase-admin 已提交
6663 6664 6665 6666 6667
  ObTenantSequenceId() : tenant_id_(common::OB_INVALID_ID), sequence_id_(common::OB_INVALID_ID)
  {}
  ObTenantSequenceId(const uint64_t tenant_id, const uint64_t sequence_id)
      : tenant_id_(tenant_id), sequence_id_(sequence_id)
  {}
X
xy0 已提交
6668
  bool operator==(const ObTenantSequenceId &rhs) const
O
oceanbase-admin 已提交
6669 6670 6671
  {
    return (tenant_id_ == rhs.tenant_id_) && (sequence_id_ == rhs.sequence_id_);
  }
X
xy0 已提交
6672
  bool operator!=(const ObTenantSequenceId &rhs) const
O
oceanbase-admin 已提交
6673 6674 6675
  {
    return !(*this == rhs);
  }
X
xy0 已提交
6676
  bool operator<(const ObTenantSequenceId &rhs) const
O
oceanbase-admin 已提交
6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702
  {
    bool bret = tenant_id_ < rhs.tenant_id_;
    if (tenant_id_ == rhs.tenant_id_) {
      bret = sequence_id_ < rhs.sequence_id_;
    }
    return bret;
  }
  inline uint64_t hash() const
  {
    uint64_t hash_ret = 0;
    hash_ret = common::murmurhash(&tenant_id_, sizeof(tenant_id_), 0);
    hash_ret = common::murmurhash(&sequence_id_, sizeof(sequence_id_), hash_ret);
    return hash_ret;
  }
  bool is_valid() const
  {
    return (tenant_id_ != common::OB_INVALID_ID) && (sequence_id_ != common::OB_INVALID_ID);
  }
  TO_STRING_KV(K_(tenant_id), K_(sequence_id));
  uint64_t tenant_id_;
  uint64_t sequence_id_;
};

class ObAlterOutlineInfo : public ObOutlineInfo {
  OB_UNIS_VERSION(1);

G
gm 已提交
6703
public:
O
oceanbase-admin 已提交
6704 6705 6706 6707 6708 6709 6710 6711 6712
  ObAlterOutlineInfo() : ObOutlineInfo(), alter_option_bitset_()
  {}
  virtual ~ObAlterOutlineInfo()
  {}
  void reset()
  {
    ObOutlineInfo::reset();
    alter_option_bitset_.reset();
  }
X
xy0 已提交
6713
  const common::ObBitSet<> &get_alter_option_bitset() const
O
oceanbase-admin 已提交
6714 6715 6716
  {
    return alter_option_bitset_;
  }
X
xy0 已提交
6717
  common::ObBitSet<> &get_alter_option_bitset()
O
oceanbase-admin 已提交
6718 6719 6720 6721 6722
  {
    return alter_option_bitset_;
  }
  INHERIT_TO_STRING_KV("ObOutlineInfo", ObOutlineInfo, K_(alter_option_bitset));

G
gm 已提交
6723
private:
O
oceanbase-admin 已提交
6724 6725 6726 6727
  common::ObBitSet<> alter_option_bitset_;
};

class ObOutlineNameHashWrapper {
G
gm 已提交
6728
public:
O
oceanbase-admin 已提交
6729 6730
  ObOutlineNameHashWrapper() : tenant_id_(common::OB_INVALID_ID), database_id_(common::OB_INVALID_ID), name_()
  {}
X
xy0 已提交
6731
  ObOutlineNameHashWrapper(const uint64_t tenant_id, const uint64_t database_id, const common::ObString &name_)
O
oceanbase-admin 已提交
6732 6733 6734 6735 6736
      : tenant_id_(tenant_id), database_id_(database_id), name_(name_)
  {}
  ~ObOutlineNameHashWrapper()
  {}
  inline uint64_t hash() const;
X
xy0 已提交
6737
  inline bool operator==(const ObOutlineNameHashWrapper &rv) const;
O
oceanbase-admin 已提交
6738 6739 6740 6741 6742 6743 6744 6745
  inline void set_tenant_id(uint64_t tenant_id)
  {
    tenant_id_ = tenant_id;
  }
  inline void set_database_id(uint64_t database_id)
  {
    database_id_ = database_id;
  }
X
xy0 已提交
6746
  inline void set_name(const common::ObString &name)
O
oceanbase-admin 已提交
6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758
  {
    name_ = name;
  }

  inline uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
  inline uint64_t get_database_id() const
  {
    return database_id_;
  }
X
xy0 已提交
6759
  inline const common::ObString &get_name() const
O
oceanbase-admin 已提交
6760 6761 6762 6763
  {
    return name_;
  }

G
gm 已提交
6764
private:
O
oceanbase-admin 已提交
6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778
  uint64_t tenant_id_;
  uint64_t database_id_;
  common::ObString name_;
};

inline uint64_t ObOutlineNameHashWrapper::hash() const
{
  uint64_t hash_ret = 0;
  hash_ret = common::murmurhash(&tenant_id_, sizeof(uint64_t), 0);
  hash_ret = common::murmurhash(&database_id_, sizeof(uint64_t), hash_ret);
  hash_ret = common::murmurhash(name_.ptr(), name_.length(), hash_ret);
  return hash_ret;
}

X
xy0 已提交
6779
inline bool ObOutlineNameHashWrapper::operator==(const ObOutlineNameHashWrapper &rv) const
O
oceanbase-admin 已提交
6780 6781 6782 6783 6784
{
  return (tenant_id_ == rv.tenant_id_) && (database_id_ == rv.database_id_) && (name_ == rv.name_);
}

class ObOutlineSignatureHashWrapper {
G
gm 已提交
6785
public:
O
oceanbase-admin 已提交
6786 6787
  ObOutlineSignatureHashWrapper() : tenant_id_(common::OB_INVALID_ID), database_id_(common::OB_INVALID_ID), signature_()
  {}
X
xy0 已提交
6788
  ObOutlineSignatureHashWrapper(const uint64_t tenant_id, const uint64_t database_id, const common::ObString &signature)
O
oceanbase-admin 已提交
6789 6790 6791 6792 6793
      : tenant_id_(tenant_id), database_id_(database_id), signature_(signature)
  {}
  ~ObOutlineSignatureHashWrapper()
  {}
  inline uint64_t hash() const;
X
xy0 已提交
6794
  inline bool operator==(const ObOutlineSignatureHashWrapper &rv) const;
O
oceanbase-admin 已提交
6795 6796 6797 6798 6799 6800 6801 6802
  inline void set_tenant_id(uint64_t tenant_id)
  {
    tenant_id_ = tenant_id;
  }
  inline void set_database_id(uint64_t database_id)
  {
    database_id_ = database_id;
  }
X
xy0 已提交
6803
  inline void set_signature(const common::ObString &signature)
O
oceanbase-admin 已提交
6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815
  {
    signature_ = signature;
  }

  inline uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
  inline uint64_t get_database_id() const
  {
    return database_id_;
  }
X
xy0 已提交
6816
  inline const common::ObString &get_signature() const
O
oceanbase-admin 已提交
6817 6818 6819 6820
  {
    return signature_;
  }

G
gm 已提交
6821
private:
O
oceanbase-admin 已提交
6822 6823 6824 6825 6826 6827
  uint64_t tenant_id_;
  uint64_t database_id_;
  common::ObString signature_;
};

class ObOutlineSqlIdHashWrapper {
G
gm 已提交
6828
public:
O
oceanbase-admin 已提交
6829 6830
  ObOutlineSqlIdHashWrapper() : tenant_id_(common::OB_INVALID_ID), database_id_(common::OB_INVALID_ID), sql_id_()
  {}
X
xy0 已提交
6831
  ObOutlineSqlIdHashWrapper(const uint64_t tenant_id, const uint64_t database_id, const common::ObString &sql_id)
O
oceanbase-admin 已提交
6832 6833 6834 6835 6836
      : tenant_id_(tenant_id), database_id_(database_id), sql_id_(sql_id)
  {}
  ~ObOutlineSqlIdHashWrapper()
  {}
  inline uint64_t hash() const;
X
xy0 已提交
6837
  inline bool operator==(const ObOutlineSqlIdHashWrapper &rv) const;
O
oceanbase-admin 已提交
6838 6839 6840 6841 6842 6843 6844 6845
  inline void set_tenant_id(uint64_t tenant_id)
  {
    tenant_id_ = tenant_id;
  }
  inline void set_database_id(uint64_t database_id)
  {
    database_id_ = database_id;
  }
X
xy0 已提交
6846
  inline void set_sql_id(const common::ObString &sql_id)
O
oceanbase-admin 已提交
6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858
  {
    sql_id_ = sql_id;
  }

  inline uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
  inline uint64_t get_database_id() const
  {
    return database_id_;
  }
X
xy0 已提交
6859
  inline const common::ObString &get_sql_id() const
O
oceanbase-admin 已提交
6860 6861 6862 6863
  {
    return sql_id_;
  }

G
gm 已提交
6864
private:
O
oceanbase-admin 已提交
6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878
  uint64_t tenant_id_;
  uint64_t database_id_;
  common::ObString sql_id_;
};

inline uint64_t ObOutlineSqlIdHashWrapper::hash() const
{
  uint64_t hash_ret = 0;
  hash_ret = common::murmurhash(&tenant_id_, sizeof(uint64_t), 0);
  hash_ret = common::murmurhash(&database_id_, sizeof(uint64_t), hash_ret);
  hash_ret = common::murmurhash(sql_id_.ptr(), sql_id_.length(), hash_ret);
  return hash_ret;
}

X
xy0 已提交
6879
inline bool ObOutlineSqlIdHashWrapper::operator==(const ObOutlineSqlIdHashWrapper &rv) const
O
oceanbase-admin 已提交
6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892
{
  return (tenant_id_ == rv.tenant_id_) && (database_id_ == rv.database_id_) && (sql_id_ == rv.sql_id_);
}

inline uint64_t ObOutlineSignatureHashWrapper::hash() const
{
  uint64_t hash_ret = 0;
  hash_ret = common::murmurhash(&tenant_id_, sizeof(uint64_t), 0);
  hash_ret = common::murmurhash(&database_id_, sizeof(uint64_t), hash_ret);
  hash_ret = common::murmurhash(signature_.ptr(), signature_.length(), hash_ret);
  return hash_ret;
}

X
xy0 已提交
6893
inline bool ObOutlineSignatureHashWrapper::operator==(const ObOutlineSignatureHashWrapper &rv) const
O
oceanbase-admin 已提交
6894 6895 6896 6897 6898
{
  return (tenant_id_ == rv.tenant_id_) && (database_id_ == rv.database_id_) && (signature_ == rv.signature_);
}

class ObSysTableChecker {
G
gm 已提交
6899
private:
O
oceanbase-admin 已提交
6900 6901 6902 6903 6904
  ObSysTableChecker();
  ~ObSysTableChecker();

  int init_tenant_space_table_id_map();
  int init_sys_table_name_map();
X
xy0 已提交
6905 6906 6907
  int check_tenant_space_table_id(const uint64_t table_id, bool &is_tenant_space_table);
  int check_sys_table_name(const uint64_t database_id, const common::ObString &table_name, bool &is_tenant_space_table);
  int ob_write_string(const common::ObString &src, common::ObString &dst);
O
oceanbase-admin 已提交
6908

G
gm 已提交
6909
public:
X
xy0 已提交
6910
  static ObSysTableChecker &instance();
O
oceanbase-admin 已提交
6911 6912 6913 6914 6915 6916 6917 6918
  int init();
  int destroy();

  int64_t get_tenant_space_sys_table_num()
  {
    return tenant_space_sys_table_num_;
  }

X
xy0 已提交
6919
  static int is_tenant_space_table_id(const uint64_t table_id, bool &is_tenant_space_table);
O
oceanbase-admin 已提交
6920
  static int is_sys_table_name(
X
xy0 已提交
6921
      const uint64_t database_id, const common::ObString &table_name, bool &is_tenant_space_table);
O
oceanbase-admin 已提交
6922 6923 6924
  static bool is_tenant_table_in_version_2200(const uint64_t table_id);
  static bool is_cluster_private_tenant_table(const uint64_t table_id);
  static bool is_backup_private_tenant_table(const uint64_t table_id);
X
xy0 已提交
6925
  static int is_tenant_table_need_weak_read(const uint64_t table_id, bool &need_weak_read);
O
oceanbase-admin 已提交
6926 6927 6928
  static bool is_rs_restart_related_table_id(const uint64_t table_id);
  static bool is_rs_restart_related_partition(const uint64_t table_id, const int64_t partition_id);

G
gm 已提交
6929
public:
O
oceanbase-admin 已提交
6930
  class TableNameWrapper {
G
gm 已提交
6931
  public:
O
oceanbase-admin 已提交
6932 6933 6934
    TableNameWrapper()
        : database_id_(common::OB_INVALID_ID), name_case_mode_(common::OB_NAME_CASE_INVALID), table_name_()
    {}
X
xy0 已提交
6935
    TableNameWrapper(const uint64_t database_id, const common::ObNameCaseMode mode, const common::ObString &table_name)
O
oceanbase-admin 已提交
6936 6937 6938 6939 6940
        : database_id_(database_id), name_case_mode_(mode), table_name_(table_name)
    {}
    ~TableNameWrapper()
    {}
    inline uint64_t hash() const;
X
xy0 已提交
6941
    bool operator==(const TableNameWrapper &rv) const;
O
oceanbase-admin 已提交
6942 6943
    TO_STRING_KV(K_(database_id), K_(name_case_mode), K_(table_name));

G
gm 已提交
6944
  private:
O
oceanbase-admin 已提交
6945 6946 6947 6948 6949 6950
    uint64_t database_id_;  // pure_database_id
    common::ObNameCaseMode name_case_mode_;
    common::ObString table_name_;
  };
  typedef common::ObSEArray<TableNameWrapper, 2> TableNameWrapperArray;

G
gm 已提交
6951
private:
O
oceanbase-admin 已提交
6952 6953
  static const int64_t TABLE_BUCKET_NUM = 300;

G
gm 已提交
6954
private:
O
oceanbase-admin 已提交
6955
  common::hash::ObHashSet<uint64_t, common::hash::NoPthreadDefendMode> tenant_space_table_id_map_;
X
xy0 已提交
6956
  common::hash::ObHashMap<uint64_t, TableNameWrapperArray *, common::hash::NoPthreadDefendMode> sys_table_name_map_;
O
oceanbase-admin 已提交
6957 6958 6959 6960 6961 6962 6963 6964 6965 6966
  int64_t tenant_space_sys_table_num_;  // Number of tenant-level system tables (including system table indexes)
  common::ObArenaAllocator allocator_;
  bool is_inited_;
  DISALLOW_COPY_AND_ASSIGN(ObSysTableChecker);
};

class ObTableSchema;
class ObRecycleObject : public ObSchema {
  OB_UNIS_VERSION(1);

G
gm 已提交
6967
public:
O
oceanbase-admin 已提交
6968 6969 6970 6971 6972 6973 6974 6975 6976 6977
  enum RecycleObjType {
    INVALID = 0,
    TABLE,
    INDEX,
    VIEW,
    DATABASE,
    RESERVED_OBJ_TYPE,
    TRIGGER,
    TENANT,
  };
X
xy0 已提交
6978
  ObRecycleObject(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990
  ObRecycleObject()
      : ObSchema(),
        tenant_id_(common::OB_INVALID_ID),
        database_id_(common::OB_INVALID_ID),
        table_id_(common::OB_INVALID_ID),
        tablegroup_id_(common::OB_INVALID_ID),
        object_name_(),
        original_name_(),
        type_(INVALID),
        tablegroup_name_(),
        database_name_()
  {}
X
xy0 已提交
6991 6992
  ObRecycleObject(const ObRecycleObject &recycle_obj);
  ObRecycleObject &operator=(const ObRecycleObject &recycle_obj);
O
oceanbase-admin 已提交
6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013
  virtual ~ObRecycleObject()
  {}
  inline bool is_valid() const;
  virtual void reset();

  uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
  uint64_t get_database_id() const
  {
    return database_id_;
  }
  uint64_t get_table_id() const
  {
    return table_id_;
  }
  uint64_t get_tablegroup_id() const
  {
    return tablegroup_id_;
  }
X
xy0 已提交
7014
  const common::ObString &get_object_name() const
O
oceanbase-admin 已提交
7015 7016 7017
  {
    return object_name_;
  }
X
xy0 已提交
7018
  const common::ObString &get_original_name() const
O
oceanbase-admin 已提交
7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041
  {
    return original_name_;
  }
  RecycleObjType get_type() const
  {
    return type_;
  }
  void set_tenant_id(const uint64_t tenant_id)
  {
    tenant_id_ = tenant_id;
  }
  void set_database_id(const uint64_t db_id)
  {
    database_id_ = db_id;
  }
  void set_table_id(const uint64_t table_id)
  {
    table_id_ = table_id;
  }
  void set_tablegroup_id(const uint64_t tablegroup_id)
  {
    tablegroup_id_ = tablegroup_id;
  }
X
xy0 已提交
7042
  int set_object_name(const common::ObString &object_name)
O
oceanbase-admin 已提交
7043 7044 7045
  {
    return deep_copy_str(object_name, object_name_);
  }
X
xy0 已提交
7046
  int set_original_name(const common::ObString &original_name)
O
oceanbase-admin 已提交
7047 7048 7049 7050 7051 7052 7053
  {
    return deep_copy_str(original_name, original_name_);
  }
  void set_type(const RecycleObjType type)
  {
    type_ = type;
  }
X
xy0 已提交
7054 7055
  int set_type_by_table_schema(const ObSimpleTableSchemaV2 &table_schema);
  static RecycleObjType get_type_by_table_schema(const ObSimpleTableSchemaV2 &table_schema);
O
oceanbase-admin 已提交
7056
  // for backup
X
xy0 已提交
7057
  int set_tablegroup_name(const common::ObString &tablegroup_name)
O
oceanbase-admin 已提交
7058 7059 7060
  {
    return deep_copy_str(tablegroup_name, tablegroup_name_);
  }
X
xy0 已提交
7061
  const common::ObString &get_tablegroup_name() const
O
oceanbase-admin 已提交
7062 7063 7064
  {
    return tablegroup_name_;
  }
X
xy0 已提交
7065
  int set_database_name(const common::ObString &database_name)
O
oceanbase-admin 已提交
7066 7067 7068
  {
    return deep_copy_str(database_name, database_name_);
  }
X
xy0 已提交
7069
  const common::ObString &get_database_name() const
O
oceanbase-admin 已提交
7070 7071 7072 7073 7074 7075
  {
    return database_name_;
  }
  TO_STRING_KV(K_(tenant_id), K_(database_id), K_(table_id), K_(tablegroup_id), K_(object_name), K_(original_name),
      K_(type), K_(tablegroup_name), K_(database_name));

G
gm 已提交
7076
private:
O
oceanbase-admin 已提交
7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101
  uint64_t tenant_id_;
  uint64_t database_id_;
  uint64_t table_id_;
  uint64_t tablegroup_id_;
  common::ObString object_name_;
  common::ObString original_name_;
  RecycleObjType type_;
  // for backup
  common::ObString tablegroup_name_;
  common::ObString database_name_;
};

inline bool ObRecycleObject::is_valid() const
{
  return INVALID != type_ && !object_name_.empty() && !original_name_.empty();
}

typedef common::hash::ObPlacementHashSet<uint64_t, common::OB_MAX_TABLE_NUM_PER_STMT> DropTableIdHashSet;
// Used to count vertical partition columns
typedef common::hash::ObPlacementHashSet<common::ObString, common::OB_MAX_USER_DEFINED_COLUMNS_COUNT>
    VPColumnNameHashSet;

struct ObBasedSchemaObjectInfo {
  OB_UNIS_VERSION(1);

G
gm 已提交
7102
public:
O
oceanbase-admin 已提交
7103 7104 7105 7106 7107 7108
  ObBasedSchemaObjectInfo()
      : schema_id_(common::OB_INVALID_ID), schema_type_(OB_MAX_SCHEMA), schema_version_(common::OB_INVALID_VERSION)
  {}
  ObBasedSchemaObjectInfo(const uint64_t schema_id, const ObSchemaType schema_type, const int64_t schema_version)
      : schema_id_(schema_id), schema_type_(schema_type), schema_version_(schema_version)
  {}
X
xy0 已提交
7109
  bool operator==(const ObBasedSchemaObjectInfo &other) const
O
oceanbase-admin 已提交
7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127
  {
    return (schema_id_ == other.schema_id_ && schema_type_ == other.schema_type_ &&
            schema_version_ == other.schema_version_);
  }
  int64_t get_convert_size() const
  {
    int64_t convert_size = sizeof(*this);
    return convert_size;
  }
  TO_STRING_KV(K_(schema_id), K_(schema_type), K_(schema_version));
  uint64_t schema_id_;
  ObSchemaType schema_type_;
  int64_t schema_version_;
};

struct ObAuxTableMetaInfo {
  OB_UNIS_VERSION(1);

G
gm 已提交
7128
public:
O
oceanbase-admin 已提交
7129 7130 7131 7132 7133 7134
  ObAuxTableMetaInfo()
      : table_id_(common::OB_INVALID_ID), table_type_(MAX_TABLE_TYPE), drop_schema_version_(common::OB_INVALID_VERSION)
  {}
  ObAuxTableMetaInfo(const uint64_t table_id, const ObTableType table_type, const int64_t drop_schema_version)
      : table_id_(table_id), table_type_(table_type), drop_schema_version_(drop_schema_version)
  {}
X
xy0 已提交
7135
  bool operator==(const ObAuxTableMetaInfo &other) const
O
oceanbase-admin 已提交
7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176
  {
    return (table_id_ == other.table_id_ && table_type_ == other.table_type_ &&
            drop_schema_version_ == other.drop_schema_version_);
  }
  int64_t get_convert_size() const
  {
    int64_t convert_size = sizeof(*this);
    return convert_size;
  }
  bool is_dropped_schema() const
  {
    return drop_schema_version_ > 0;
  }
  TO_STRING_KV(K_(table_id), K_(table_type), K_(drop_schema_version));
  uint64_t table_id_;
  ObTableType table_type_;
  int64_t drop_schema_version_;
};

enum ObConstraintType {
  CONSTRAINT_TYPE_INVALID = 0,
  CONSTRAINT_TYPE_PRIMARY_KEY = 1,
  CONSTRAINT_TYPE_UNIQUE_KEY = 2,
  CONSTRAINT_TYPE_CHECK = 3,
  CONSTRAINT_TYPE_MAX,
};

enum ObReferenceAction {
  ACTION_INVALID = 0,
  ACTION_RESTRICT,
  ACTION_CASCADE,
  ACTION_SET_NULL,
  ACTION_NO_ACTION,
  ACTION_SET_DEFAULT,  // no use now
  ACTION_CHECK_EXIST,  // not a valid option, just used for construct dml stmt.
  ACTION_MAX
};

class ObForeignKeyInfo {
  OB_UNIS_VERSION(1);

G
gm 已提交
7177
public:
O
oceanbase-admin 已提交
7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197
  ObForeignKeyInfo()
      : table_id_(common::OB_INVALID_ID),
        foreign_key_id_(common::OB_INVALID_ID),
        child_table_id_(common::OB_INVALID_ID),
        parent_table_id_(common::OB_INVALID_ID),
        child_column_ids_(),
        parent_column_ids_(),
        update_action_(ACTION_INVALID),
        delete_action_(ACTION_INVALID),
        foreign_key_name_(),
        enable_flag_(true),
        is_modify_enable_flag_(false),
        validate_flag_(true),
        is_modify_validate_flag_(false),
        rely_flag_(false),
        is_modify_rely_flag_(false),
        is_modify_fk_state_(false),
        ref_cst_type_(CONSTRAINT_TYPE_INVALID),
        ref_cst_id_(common::OB_INVALID_ID)
  {}
X
xy0 已提交
7198
  ObForeignKeyInfo(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
7199 7200 7201
  virtual ~ObForeignKeyInfo()
  {}

G
gm 已提交
7202
public:
O
oceanbase-admin 已提交
7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226
  inline void set_table_id(uint64_t table_id)
  {
    table_id_ = table_id;
  }
  inline void set_foreign_key_id(uint64_t foreign_key_id)
  {
    foreign_key_id_ = foreign_key_id;
  }
  inline void set_child_table_id(uint64_t child_table_id)
  {
    child_table_id_ = child_table_id;
  }
  inline void set_parent_table_id(uint64_t parent_table_id)
  {
    parent_table_id_ = parent_table_id;
  }
  inline void set_update_action(uint64_t update_action)
  {
    update_action_ = static_cast<ObReferenceAction>(update_action);
  }
  inline void set_delete_action(uint64_t delete_action)
  {
    delete_action_ = static_cast<ObReferenceAction>(delete_action);
  }
X
xy0 已提交
7227
  inline int set_foreign_key_name(const common::ObString &foreign_key_name)
O
oceanbase-admin 已提交
7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267
  {
    foreign_key_name_ = foreign_key_name;
    return common::OB_SUCCESS;
  }
  inline void set_enable_flag(const bool enable_flag)
  {
    enable_flag_ = enable_flag;
  }
  inline void set_is_modify_enable_flag(const bool is_modify_enable_flag)
  {
    is_modify_enable_flag_ = is_modify_enable_flag;
  }
  inline void set_validate_flag(const bool validate_flag)
  {
    validate_flag_ = validate_flag;
  }
  inline void set_is_modify_validate_flag(const bool is_modify_validate_flag)
  {
    is_modify_validate_flag_ = is_modify_validate_flag;
  }
  inline void set_rely_flag(const bool rely_flag)
  {
    rely_flag_ = rely_flag;
  }
  inline void set_is_modify_rely_flag(const bool is_modify_rely_flag)
  {
    is_modify_rely_flag_ = is_modify_rely_flag;
  }
  inline void set_is_modify_fk_state(const bool is_modify_fk_state)
  {
    is_modify_fk_state_ = is_modify_fk_state;
  }
  inline void set_ref_cst_type(ObConstraintType ref_cst_type)
  {
    ref_cst_type_ = ref_cst_type;
  }
  inline void set_ref_cst_id(uint64_t ref_cst_id)
  {
    ref_cst_id_ = ref_cst_id;
  }
X
xy0 已提交
7268
  inline const char *get_update_action_str() const
O
oceanbase-admin 已提交
7269 7270 7271
  {
    return get_action_str(update_action_);
  }
X
xy0 已提交
7272
  inline const char *get_delete_action_str() const
O
oceanbase-admin 已提交
7273 7274 7275
  {
    return get_action_str(delete_action_);
  }
X
xy0 已提交
7276
  inline const char *get_action_str(ObReferenceAction action) const
O
oceanbase-admin 已提交
7277
  {
X
xy0 已提交
7278
    const char *ret = NULL;
O
oceanbase-admin 已提交
7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304
    if (ACTION_RESTRICT <= action && action <= ACTION_SET_DEFAULT) {
      ret = reference_action_str_[action];
    }
    return ret;
  }
  inline void reset()
  {
    table_id_ = common::OB_INVALID_ID;
    foreign_key_id_ = common::OB_INVALID_ID;
    child_table_id_ = common::OB_INVALID_ID;
    parent_table_id_ = common::OB_INVALID_ID;
    child_column_ids_.reset();
    parent_column_ids_.reset();
    update_action_ = ACTION_INVALID;
    delete_action_ = ACTION_INVALID;
    foreign_key_name_.reset();
    enable_flag_ = true;
    is_modify_enable_flag_ = false;
    validate_flag_ = true;
    is_modify_validate_flag_ = false;
    rely_flag_ = false;
    is_modify_rely_flag_ = false;
    is_modify_fk_state_ = false;
    ref_cst_type_ = CONSTRAINT_TYPE_INVALID;
    ref_cst_id_ = common::OB_INVALID_ID;
  }
X
xy0 已提交
7305
  int assign(const ObForeignKeyInfo &other);
O
oceanbase-admin 已提交
7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318
  inline int64_t get_convert_size() const
  {
    int64_t convert_size = sizeof(*this);
    convert_size += child_column_ids_.get_data_size();
    convert_size += parent_column_ids_.get_data_size();
    convert_size += foreign_key_name_.length() + 1;
    return convert_size;
  }
  TO_STRING_KV(K_(table_id), K_(foreign_key_id), K_(child_table_id), K_(parent_table_id), K_(child_column_ids),
      K_(parent_column_ids), K_(update_action), K_(delete_action), K_(foreign_key_name), K_(enable_flag),
      K_(is_modify_enable_flag), K_(validate_flag), K_(is_modify_validate_flag), K_(rely_flag), K_(is_modify_rely_flag),
      K_(is_modify_fk_state), K_(ref_cst_type), K_(ref_cst_id));

G
gm 已提交
7319
public:
O
oceanbase-admin 已提交
7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338
  uint64_t table_id_;  // table_id is not in __all_foreign_key.
  uint64_t foreign_key_id_;
  uint64_t child_table_id_;
  uint64_t parent_table_id_;
  common::ObSEArray<uint64_t, 8> child_column_ids_;
  common::ObSEArray<uint64_t, 8> parent_column_ids_;
  ObReferenceAction update_action_;
  ObReferenceAction delete_action_;
  common::ObString foreign_key_name_;
  bool enable_flag_;
  bool is_modify_enable_flag_;
  bool validate_flag_;
  bool is_modify_validate_flag_;
  bool rely_flag_;
  bool is_modify_rely_flag_;
  bool is_modify_fk_state_;
  ObConstraintType ref_cst_type_;
  uint64_t ref_cst_id_;

G
gm 已提交
7339
private:
X
xy0 已提交
7340
  static const char *reference_action_str_[ACTION_MAX + 1];
O
oceanbase-admin 已提交
7341 7342 7343 7344 7345
};

struct ObSimpleForeignKeyInfo {
  OB_UNIS_VERSION(1);

G
gm 已提交
7346
public:
O
oceanbase-admin 已提交
7347 7348 7349 7350 7351 7352 7353 7354 7355
  ObSimpleForeignKeyInfo()
  {
    tenant_id_ = common::OB_INVALID_ID;
    database_id_ = common::OB_INVALID_ID;
    table_id_ = common::OB_INVALID_ID;
    foreign_key_name_.assign_ptr("", 0);
    foreign_key_id_ = common::OB_INVALID_ID;
  }
  ObSimpleForeignKeyInfo(const uint64_t tenant_id, const uint64_t database_id, const uint64_t table_id,
X
xy0 已提交
7356
      const common::ObString &foreign_key_name, const uint64_t foreign_key_id)
O
oceanbase-admin 已提交
7357 7358 7359 7360 7361 7362
      : tenant_id_(tenant_id),
        database_id_(database_id),
        table_id_(table_id),
        foreign_key_name_(foreign_key_name),
        foreign_key_id_(foreign_key_id)
  {}
X
xy0 已提交
7363
  bool operator==(const ObSimpleForeignKeyInfo &other) const
O
oceanbase-admin 已提交
7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393
  {
    return (tenant_id_ == other.tenant_id_ && database_id_ == other.database_id_ && table_id_ == other.table_id_ &&
            foreign_key_name_ == other.foreign_key_name_ && foreign_key_id_ == other.foreign_key_id_);
  }
  int64_t get_convert_size() const
  {
    int64_t convert_size = sizeof(*this);
    convert_size += foreign_key_name_.length() + 1;
    return convert_size;
  }
  void reset()
  {
    tenant_id_ = common::OB_INVALID_ID;
    database_id_ = common::OB_INVALID_ID;
    table_id_ = common::OB_INVALID_ID;
    foreign_key_name_.assign_ptr("", 0);
    foreign_key_id_ = common::OB_INVALID_ID;
  }
  TO_STRING_KV(K_(tenant_id), K_(database_id), K_(table_id), K_(foreign_key_name), K_(foreign_key_id));

  uint64_t tenant_id_;
  uint64_t database_id_;
  uint64_t table_id_;
  common::ObString foreign_key_name_;
  uint64_t foreign_key_id_;
};

struct ObSimpleConstraintInfo {
  OB_UNIS_VERSION(1);

G
gm 已提交
7394
public:
O
oceanbase-admin 已提交
7395 7396 7397 7398 7399 7400 7401 7402 7403
  ObSimpleConstraintInfo()
  {
    tenant_id_ = common::OB_INVALID_ID;
    database_id_ = common::OB_INVALID_ID;
    table_id_ = common::OB_INVALID_ID;
    constraint_name_.assign_ptr("", 0);
    constraint_id_ = common::OB_INVALID_ID;
  }
  ObSimpleConstraintInfo(const uint64_t tenant_id, const uint64_t database_id, const uint64_t table_id,
X
xy0 已提交
7404
      const common::ObString &constraint_name, const uint64_t constraint_id)
O
oceanbase-admin 已提交
7405 7406 7407 7408 7409 7410
      : tenant_id_(tenant_id),
        database_id_(database_id),
        table_id_(table_id),
        constraint_name_(constraint_name),
        constraint_id_(constraint_id)
  {}
X
xy0 已提交
7411
  bool operator==(const ObSimpleConstraintInfo &other) const
O
oceanbase-admin 已提交
7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440
  {
    return (tenant_id_ == other.tenant_id_ && database_id_ == other.database_id_ && table_id_ == other.table_id_ &&
            constraint_name_ == other.constraint_name_ && constraint_id_ == other.constraint_id_);
  }
  int64_t get_convert_size() const
  {
    int64_t convert_size = sizeof(*this);
    convert_size += constraint_name_.length() + 1;
    return convert_size;
  }
  void reset()
  {
    tenant_id_ = common::OB_INVALID_ID;
    database_id_ = common::OB_INVALID_ID;
    table_id_ = common::OB_INVALID_ID;
    constraint_name_.assign_ptr("", 0);
    constraint_id_ = common::OB_INVALID_ID;
  }
  TO_STRING_KV(K_(tenant_id), K_(database_id), K_(table_id), K_(constraint_name), K_(constraint_id));

  uint64_t tenant_id_;
  uint64_t database_id_;
  uint64_t table_id_;
  common::ObString constraint_name_;
  uint64_t constraint_id_;
};

template <typename PARTITION>
int ObPartitionUtils::check_partition_value(
X
xy0 已提交
7441
    const PARTITION &l_part, const PARTITION &r_part, const ObPartitionFuncType part_type, bool &is_equal)
O
oceanbase-admin 已提交
7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487
{
  int ret = common::OB_SUCCESS;
  is_equal = true;
  if (is_hash_like_part(part_type)) {
    is_equal = true;
  } else if (is_range_part(part_type)) {
    if (l_part.get_high_bound_val().get_obj_cnt() != r_part.get_high_bound_val().get_obj_cnt()) {
      is_equal = false;
      SHARE_SCHEMA_LOG(DEBUG,
          "fail to check partition value, value count not equal",
          "left",
          l_part.get_high_bound_val(),
          "right",
          r_part.get_high_bound_val());
    } else {
      for (int64_t i = 0; i < l_part.get_high_bound_val().get_obj_cnt() && is_equal; i++) {
        const common::ObObjMeta meta1 = l_part.get_high_bound_val().get_obj_ptr()[i].get_meta();
        const common::ObObjMeta meta2 = r_part.get_high_bound_val().get_obj_ptr()[i].get_meta();
        if (meta1.get_collation_level() == meta2.get_collation_level() &&
            meta1.get_collation_type() == meta2.get_collation_type()) {
          is_equal = is_types_equal_for_partition_check(meta1.get_type(), meta2.get_type());
        } else {
          is_equal = false;
        }
        if (false == is_equal) {
          SHARE_SCHEMA_LOG(DEBUG,
              "fail to check partition values, value meta not equal",
              "left",
              l_part.get_high_bound_val().get_obj_ptr()[i],
              "right",
              r_part.get_high_bound_val().get_obj_ptr()[i],
              K(lib::is_oracle_mode()));
        } else if (0 !=
                   l_part.get_high_bound_val().get_obj_ptr()[i].compare(r_part.get_high_bound_val().get_obj_ptr()[i],
                       r_part.get_high_bound_val().get_obj_ptr()[i].get_collation_type())) {
          is_equal = false;
          SHARE_SCHEMA_LOG(DEBUG,
              "fail to check partition values, value not equal",
              "left",
              l_part.get_high_bound_val().get_obj_ptr()[i],
              "right",
              r_part.get_high_bound_val().get_obj_ptr()[i]);
        }
      }
    }
  } else if (is_list_part(part_type)) {
X
xy0 已提交
7488 7489
    const common::ObIArray<common::ObNewRow> &l_list_values = l_part.get_list_row_values();
    const common::ObIArray<common::ObNewRow> &r_list_values = r_part.get_list_row_values();
O
oceanbase-admin 已提交
7490 7491 7492 7493
    if (l_list_values.count() != r_list_values.count()) {
      is_equal = false;
    } else {
      for (int64_t i = 0; i < l_list_values.count() && is_equal; i++) {
X
xy0 已提交
7494
        const common::ObNewRow &l_rowkey = l_list_values.at(i);
O
oceanbase-admin 已提交
7495 7496
        bool find_equal_item = false;
        for (int64_t j = 0; j < r_list_values.count() && !find_equal_item && is_equal; j++) {
X
xy0 已提交
7497
          const common::ObNewRow &r_rowkey = r_list_values.at(j);
O
oceanbase-admin 已提交
7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542
          // First check that the count and meta information are consistent;
          if (l_rowkey.get_count() != r_rowkey.get_count()) {
            is_equal = false;
            SHARE_SCHEMA_LOG(
                DEBUG, "fail to check partition value, value count not equal", "left", l_rowkey, "right", r_rowkey);
          } else {
            for (int64_t z = 0; z < l_rowkey.get_count() && is_equal; z++) {
              const common::ObObjMeta meta1 = l_rowkey.get_cell(z).get_meta();
              const common::ObObjMeta meta2 = r_rowkey.get_cell(z).get_meta();
              if (meta1.get_collation_level() == meta2.get_collation_level() &&
                  meta1.get_collation_type() == meta2.get_collation_type()) {
                is_equal = is_types_equal_for_partition_check(meta1.get_type(), meta2.get_type());
              } else {
                is_equal = false;
              }
              if (false == is_equal) {
                SHARE_SCHEMA_LOG(DEBUG,
                    "fail to check partition values, value meta not equal",
                    "left",
                    l_rowkey.get_cell(z),
                    "right",
                    r_rowkey.get_cell(z));
              }
            }
          }
          // Check whether the value of rowkey is the same;
          if (OB_SUCC(ret) && is_equal && l_rowkey == r_rowkey) {
            find_equal_item = true;
          }
        }  // end for (int64_t j = 0
        if (!find_equal_item) {
          is_equal = false;
        }
      }  // end for (int64_t i = 0;
    }
  } else {
    ret = common::OB_INVALID_ARGUMENT;
    SHARE_SCHEMA_LOG(WARN, "invalid part_type", K(ret), K(part_type));
  }
  return ret;
}

class ObSequenceSchema : public ObSchema {
  OB_UNIS_VERSION(1);

G
gm 已提交
7543
public:
O
oceanbase-admin 已提交
7544
  ObSequenceSchema();
X
xy0 已提交
7545
  explicit ObSequenceSchema(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
7546
  virtual ~ObSequenceSchema();
X
xy0 已提交
7547 7548 7549
  ObSequenceSchema &operator=(const ObSequenceSchema &src_schema);
  int assign(const ObSequenceSchema &src_schema);
  ObSequenceSchema(const ObSequenceSchema &src_schema);
O
oceanbase-admin 已提交
7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567
  int64_t get_convert_size() const;

  inline void set_tenant_id(const uint64_t id)
  {
    tenant_id_ = id;
  }
  inline void set_database_id(const uint64_t id)
  {
    database_id_ = id;
  }
  inline void set_sequence_id(uint64_t id)
  {
    sequence_id_ = id;
  }
  inline void set_schema_version(int64_t version)
  {
    schema_version_ = version;
  }
X
xy0 已提交
7568
  inline int set_sequence_name(const char *name)
O
oceanbase-admin 已提交
7569 7570 7571
  {
    return deep_copy_str(name, name_);
  }
X
xy0 已提交
7572
  inline int set_sequence_name(const common::ObString &name)
O
oceanbase-admin 已提交
7573 7574 7575
  {
    return deep_copy_str(name, name_);
  }
X
xy0 已提交
7576
  inline int set_name(const common::ObString &name)
O
oceanbase-admin 已提交
7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600
  {
    return set_sequence_name(name);
  }
  // inline void set_max_value(int64_t val) { option_.set_max_value(val); }
  // inline void set_min_value(int64_t val) { option_.set_min_value(val); }
  // inline void set_increment_by(int64_t val) { option_.set_increment_by(val); }
  // inline void set_start_with(int64_t val) { option_.set_start_with(val); }
  // inline void set_cache_size(int64_t val) { option_.set_cache_size(val); }
  inline void set_cycle_flag(bool cycle)
  {
    option_.set_cycle_flag(cycle);
  }
  inline void set_order_flag(bool order)
  {
    option_.set_order_flag(order);
  }

  // Temporary compatibility code, in order to support max_value etc. as Number type
  // int set_max_value(const common::ObString &str);
  // int set_min_value(const common::ObString &str);
  // int set_increment_by(const common::ObString &str);
  // int set_start_with(const common::ObString &str);
  // int set_cache_size(const common::ObString &str);

X
xy0 已提交
7601
  int set_max_value(const common::number::ObNumber &num)
O
oceanbase-admin 已提交
7602 7603 7604
  {
    return option_.set_max_value(num);
  }
X
xy0 已提交
7605
  int set_min_value(const common::number::ObNumber &num)
O
oceanbase-admin 已提交
7606 7607 7608
  {
    return option_.set_min_value(num);
  }
X
xy0 已提交
7609
  int set_increment_by(const common::number::ObNumber &num)
O
oceanbase-admin 已提交
7610 7611 7612
  {
    return option_.set_increment_by(num);
  }
X
xy0 已提交
7613
  int set_start_with(const common::number::ObNumber &num)
O
oceanbase-admin 已提交
7614 7615 7616
  {
    return option_.set_start_with(num);
  }
X
xy0 已提交
7617
  int set_cache_size(const common::number::ObNumber &num)
O
oceanbase-admin 已提交
7618 7619 7620 7621
  {
    return option_.set_cache_size(num);
  }

X
xy0 已提交
7622
  inline const common::number::ObNumber &get_min_value() const
O
oceanbase-admin 已提交
7623 7624 7625
  {
    return option_.get_min_value();
  }
X
xy0 已提交
7626
  inline const common::number::ObNumber &get_max_value() const
O
oceanbase-admin 已提交
7627 7628 7629
  {
    return option_.get_max_value();
  }
X
xy0 已提交
7630
  inline const common::number::ObNumber &get_increment_by() const
O
oceanbase-admin 已提交
7631 7632 7633
  {
    return option_.get_increment_by();
  }
X
xy0 已提交
7634
  inline const common::number::ObNumber &get_start_with() const
O
oceanbase-admin 已提交
7635 7636 7637
  {
    return option_.get_start_with();
  }
X
xy0 已提交
7638
  inline const common::number::ObNumber &get_cache_size() const
O
oceanbase-admin 已提交
7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671
  {
    return option_.get_cache_size();
  }

  inline uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
  inline uint64_t get_database_id() const
  {
    return database_id_;
  }
  inline uint64_t get_sequence_id() const
  {
    return sequence_id_;
  }
  // inline int64_t get_min_value() const { return option_.get_min_value(); }
  // inline int64_t get_max_value() const { return option_.get_max_value(); }
  // inline int64_t get_increment_by() const { return option_.get_increment_by(); }
  // inline int64_t get_start_with() const { return option_.get_start_with(); }
  // inline int64_t get_cache_size() const { return option_.get_cache_size(); }
  inline bool get_cycle_flag() const
  {
    return option_.get_cycle_flag();
  }
  inline bool get_order_flag() const
  {
    return option_.get_order_flag();
  }
  inline int64_t get_schema_version() const
  {
    return schema_version_;
  }
X
xy0 已提交
7672
  inline const common::ObString &get_sequence_name() const
O
oceanbase-admin 已提交
7673 7674 7675
  {
    return name_;
  }
X
xy0 已提交
7676
  inline const char *get_sequence_name_str() const
O
oceanbase-admin 已提交
7677 7678 7679
  {
    return extract_str(name_);
  }
X
xy0 已提交
7680
  inline share::ObSequenceOption &get_sequence_option()
O
oceanbase-admin 已提交
7681 7682 7683
  {
    return option_;
  }
X
xy0 已提交
7684
  inline const share::ObSequenceOption &get_sequence_option() const
O
oceanbase-admin 已提交
7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696
  {
    return option_;
  }
  inline ObTenantSequenceId get_tenant_sequence_id() const
  {
    return ObTenantSequenceId(tenant_id_, sequence_id_);
  }

  void reset();

  VIRTUAL_TO_STRING_KV(K_(name), K_(tenant_id), K_(database_id), K_(sequence_id), K_(schema_version), K_(option));

G
gm 已提交
7697
private:
O
oceanbase-admin 已提交
7698 7699 7700
  // void *alloc(int64_t size);
  // int get_value(const common::ObString &str, int64_t &val);
  // int get_value(const common::number::ObNumber &num, int64_t &val);
G
gm 已提交
7701
private:
O
oceanbase-admin 已提交
7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715
  uint64_t tenant_id_;
  uint64_t database_id_;
  uint64_t sequence_id_;
  int64_t schema_version_;  // the last modify timestamp of this version
  common::ObString name_;   // sequence name
  share::ObSequenceOption option_;
  // common::ObArenaAllocator allocator_;
};

typedef ObSequenceSchema ObSequenceInfo;

class ObTenantCommonSchemaId {
  OB_UNIS_VERSION(1);

G
gm 已提交
7716
public:
O
oceanbase-admin 已提交
7717 7718 7719 7720 7721
  ObTenantCommonSchemaId() : tenant_id_(common::OB_INVALID_TENANT_ID), schema_id_(common::OB_INVALID_ID)
  {}
  ObTenantCommonSchemaId(const uint64_t tenant_id, const uint64_t schema_id)
      : tenant_id_(tenant_id), schema_id_(schema_id)
  {}
X
xy0 已提交
7722
  bool operator==(const ObTenantCommonSchemaId &rhs) const
O
oceanbase-admin 已提交
7723 7724 7725
  {
    return (tenant_id_ == rhs.tenant_id_) && (schema_id_ == rhs.schema_id_);
  }
X
xy0 已提交
7726
  bool operator!=(const ObTenantCommonSchemaId &rhs) const
O
oceanbase-admin 已提交
7727 7728 7729
  {
    return !(*this == rhs);
  }
X
xy0 已提交
7730
  bool operator<(const ObTenantCommonSchemaId &rhs) const
O
oceanbase-admin 已提交
7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756
  {
    bool bret = tenant_id_ < rhs.tenant_id_;
    if (tenant_id_ == rhs.tenant_id_) {
      bret = schema_id_ < rhs.schema_id_;
    }
    return bret;
  }
  inline uint64_t hash() const
  {
    uint64_t hash_ret = 0;
    hash_ret = common::murmurhash(&tenant_id_, sizeof(tenant_id_), 0);
    hash_ret = common::murmurhash(&schema_id_, sizeof(schema_id_), hash_ret);
    return hash_ret;
  }
  bool is_valid() const
  {
    return (tenant_id_ != common::OB_INVALID_TENANT_ID) && (schema_id_ != common::OB_INVALID_ID);
  }
  TO_STRING_KV(K_(tenant_id), K_(schema_id));
  uint64_t tenant_id_;
  uint64_t schema_id_;
};

class ObTenantProfileId : public ObTenantCommonSchemaId {
  OB_UNIS_VERSION(1);

G
gm 已提交
7757
public:
O
oceanbase-admin 已提交
7758 7759 7760 7761 7762 7763 7764 7765 7766
  ObTenantProfileId() : ObTenantCommonSchemaId()
  {}
  ObTenantProfileId(const uint64_t tenant_id, const uint64_t profile_id) : ObTenantCommonSchemaId(tenant_id, profile_id)
  {}
};

class ObProfileSchema : public ObSchema {  // simple schema
  OB_UNIS_VERSION(1);

G
gm 已提交
7767
public:
O
oceanbase-admin 已提交
7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792
  static const int64_t INVALID_VALUE = -1;

  enum PARAM_TYPES {
    FAILED_LOGIN_ATTEMPTS = 0,
    PASSWORD_LOCK_TIME,
    PASSWORD_VERIFY_FUNCTION,
    PASSWORD_LIFE_TIME,
    PASSWORD_GRACE_TIME,
    /*
    PASSWORD_REUSE_TIME,
    PASSWORD_REUSE_MAX,
    SESSIONS_PER_USER,
    CPU_PER_SESSION,
    CPU_PER_CALL,
    CONNECT_TIME,
    IDLE_TIME,
    LOGICAL_READS_PER_SESSION,
    LOGICAL_READS_PER_CALL,
    PRIVATE_SGA,
    */
    MAX_PARAMS
  };

  static const int64_t DEFAULT_PARAM_VALUES[MAX_PARAMS];
  static const int64_t INVALID_PARAM_VALUES[MAX_PARAMS];
X
xy0 已提交
7793
  static const char *PARAM_VALUE_NAMES[MAX_PARAMS];
O
oceanbase-admin 已提交
7794 7795

  ObProfileSchema();
X
xy0 已提交
7796
  explicit ObProfileSchema(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
7797 7798 7799 7800 7801 7802 7803 7804 7805
  virtual ~ObProfileSchema();

  TO_STRING_KV(K_(tenant_id), K_(profile_id), K_(schema_version), K_(profile_name), K_(failed_login_attempts),
      K_(password_lock_time), K_(password_life_time), K_(password_grace_time));

  bool is_valid() const;
  void reset();
  int64_t get_convert_size() const;

X
xy0 已提交
7806 7807
  ObProfileSchema &operator=(const ObProfileSchema &src_schema);
  ObProfileSchema(const ObProfileSchema &src_schema);
O
oceanbase-admin 已提交
7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820

  inline void set_tenant_id(const uint64_t id)
  {
    tenant_id_ = id;
  }
  inline void set_profile_id(uint64_t id)
  {
    profile_id_ = id;
  }
  inline void set_schema_version(int64_t version)
  {
    schema_version_ = version;
  }
X
xy0 已提交
7821
  inline int set_profile_name(const char *name)
O
oceanbase-admin 已提交
7822 7823 7824
  {
    return deep_copy_str(name, profile_name_);
  }
X
xy0 已提交
7825
  inline int set_profile_name(const common::ObString &name)
O
oceanbase-admin 已提交
7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857
  {
    return deep_copy_str(name, profile_name_);
  }
  inline void set_failed_login_attempts(const int64_t value)
  {
    failed_login_attempts_ = value;
  }
  inline void set_password_lock_time(const int64_t value)
  {
    password_lock_time_ = value;
  }
  inline void set_password_life_time(const int64_t value)
  {
    password_life_time_ = value;
  }
  inline void set_password_grace_time(const int64_t value)
  {
    password_grace_time_ = value;
  }

  inline uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
  inline uint64_t get_profile_id() const
  {
    return profile_id_;
  }
  inline int64_t get_schema_version() const
  {
    return schema_version_;
  }
X
xy0 已提交
7858
  inline const char *get_profile_name() const
O
oceanbase-admin 已提交
7859 7860 7861
  {
    return extract_str(profile_name_);
  }
X
xy0 已提交
7862
  inline const common::ObString &get_profile_name_str() const
O
oceanbase-admin 已提交
7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891
  {
    return profile_name_;
  }
  inline int64_t get_failed_login_attempts() const
  {
    return failed_login_attempts_;
  }
  inline int64_t get_password_lock_time() const
  {
    return password_lock_time_;
  }
  inline int64_t get_password_life_time() const
  {
    return password_life_time_;
  }
  inline int64_t get_password_grace_time() const
  {
    return password_grace_time_;
  }

  inline ObTenantProfileId get_tenant_profile_id() const
  {
    return ObTenantProfileId(tenant_id_, profile_id_);
  }

  int set_value(const int64_t type, const int64_t value);
  int set_default_value(const int64_t type);
  int set_default_values();
  int set_invalid_values();
X
xy0 已提交
7892
  static int get_default_value(const int64_t type, int64_t &value);
O
oceanbase-admin 已提交
7893

X
xy0 已提交
7894
  inline const char *get_password_verify_function() const
O
oceanbase-admin 已提交
7895 7896 7897
  {
    return extract_str(password_verify_function_);
  }
X
xy0 已提交
7898
  inline const common::ObString &get_password_verify_function_str() const
O
oceanbase-admin 已提交
7899 7900 7901
  {
    return password_verify_function_;
  }
X
xy0 已提交
7902
  inline int set_password_verify_function(const char *name)
O
oceanbase-admin 已提交
7903 7904 7905
  {
    return deep_copy_str(name, password_verify_function_);
  }
X
xy0 已提交
7906
  inline int set_password_verify_function(const common::ObString &name)
O
oceanbase-admin 已提交
7907 7908 7909 7910
  {
    return deep_copy_str(name, password_verify_function_);
  }

G
gm 已提交
7911
private:
O
oceanbase-admin 已提交
7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922
  uint64_t tenant_id_;
  uint64_t profile_id_;
  int64_t schema_version_;
  common::ObString profile_name_;
  int64_t failed_login_attempts_;
  int64_t password_lock_time_;
  common::ObString password_verify_function_;
  int64_t password_life_time_;
  int64_t password_grace_time_;
};

X
xy0 已提交
7923
common::ObIAllocator *&schema_stack_allocator();
O
oceanbase-admin 已提交
7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937

struct ObObjectStruct {
  ObObjectStruct() : type_(ObObjectType::INVALID), id_(common::OB_INVALID_ID){};
  ObObjectStruct(const ObObjectType type, const uint64_t id) : type_(type), id_(id){};
  ~ObObjectStruct(){};

  TO_STRING_KV(K_(type), K_(id));

  ObObjectType type_;
  uint64_t id_;
};

//
class IObErrorInfo {
G
gm 已提交
7938
public:
O
oceanbase-admin 已提交
7939 7940 7941 7942 7943 7944 7945 7946 7947 7948
  IObErrorInfo()
  {}
  virtual ~IObErrorInfo() = 0;
  virtual uint64_t get_object_id() const = 0;
  virtual uint64_t get_tenant_id() const = 0;
  virtual uint64_t get_database_id() const = 0;
  virtual int64_t get_schema_version() const = 0;
  virtual ObObjectType get_object_type() const = 0;
  static constexpr uint64_t TYPE_MASK = (0x7) << (sizeof(uint64_t) - 3);
  static constexpr uint64_t VAL_MASK = (-1) >> 3;
X
xy0 已提交
7949
  void inline gen_mask(uint64_t type, uint64_t &id)
O
oceanbase-admin 已提交
7950 7951 7952 7953 7954 7955 7956 7957 7958 7959
  {
    id = (TYPE_MASK & type) | (VAL_MASK & id);
  }
};

}  // namespace schema
}  // namespace share
}  // namespace oceanbase

#endif /* _OB_OCEANBASE_SCHEMA_SCHEMA_STRUCT_H */