ob_schema_struct.h 240.0 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
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,
};

250 251
bool can_rereplicate_index_status(const ObIndexStatus &idst);

O
oceanbase-admin 已提交
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
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 已提交
268
public:
O
oceanbase-admin 已提交
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
  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 已提交
297
  bool operator==(const ObRefreshSchemaStatus &other) const
O
oceanbase-admin 已提交
298 299 300 301 302 303 304 305 306
  {
    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 已提交
307
public:
O
oceanbase-admin 已提交
308 309 310 311 312 313 314 315 316 317 318
  // 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 已提交
319
public:
O
oceanbase-admin 已提交
320 321 322 323 324 325
  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 已提交
326
  ObRefreshSchemaInfo(const ObRefreshSchemaInfo &other);
O
oceanbase-admin 已提交
327 328
  virtual ~ObRefreshSchemaInfo()
  {}
X
xy0 已提交
329
  int assign(const ObRefreshSchemaInfo &other);
O
oceanbase-admin 已提交
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 364 365
  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 已提交
366
private:
O
oceanbase-admin 已提交
367 368 369 370 371 372 373
  int64_t schema_version_;
  uint64_t tenant_id_;
  uint64_t sequence_id_;
  int64_t split_schema_version_;
};

class ObDropTenantInfo {
G
gm 已提交
374
public:
O
oceanbase-admin 已提交
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
  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 已提交
398
private:
O
oceanbase-admin 已提交
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 441 442
  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 已提交
443
const char *ob_index_status_str(ObIndexStatus status);
O
oceanbase-admin 已提交
444 445 446 447 448 449

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 已提交
450
  bool operator==(const ObTenantTableId &rv) const
O
oceanbase-admin 已提交
451 452 453
  {
    return (tenant_id_ == rv.tenant_id_) && (table_id_ == rv.table_id_);
  }
X
xy0 已提交
454
  ObTenantTableId &operator=(const ObTenantTableId &tenant_table_id);
O
oceanbase-admin 已提交
455 456 457 458
  int64_t hash() const
  {
    return table_id_;
  }
X
xy0 已提交
459
  bool operator<(const ObTenantTableId &rv) const
O
oceanbase-admin 已提交
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 487 488
  {
    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 已提交
489
  bool operator==(const ObTenantDatabaseId &rv) const
O
oceanbase-admin 已提交
490 491 492 493 494 495 496
  {
    return ((tenant_id_ == rv.tenant_id_) && (database_id_ == rv.database_id_));
  }
  int64_t hash() const
  {
    return database_id_;
  }
X
xy0 已提交
497
  bool operator<(const ObTenantDatabaseId &rv) const
O
oceanbase-admin 已提交
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 525 526
  {
    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 已提交
527
  bool operator==(const ObTenantTablegroupId &rv) const
O
oceanbase-admin 已提交
528 529 530 531 532 533 534
  {
    return (tenant_id_ == rv.tenant_id_) && (tablegroup_id_ == rv.tablegroup_id_);
  }
  int64_t hash() const
  {
    return tablegroup_id_;
  }
X
xy0 已提交
535
  bool operator<(const ObTenantTablegroupId &rv) const
O
oceanbase-admin 已提交
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 590 591
  {
    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 已提交
592
int need_change_schema_id(const ObSchemaType schema_type, const uint64_t schema_id, bool &need_change);
O
oceanbase-admin 已提交
593

X
xy0 已提交
594
const char *schema_type_str(const ObSchemaType schema_type);
O
oceanbase-admin 已提交
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 622 623

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 已提交
624
public:
O
oceanbase-admin 已提交
625 626
  PartIdPartitionArrayCmp() : ret_(common::OB_SUCCESS)
  {}
X
xy0 已提交
627
  bool operator()(const share::schema::ObPartition *left, const share::schema::ObPartition *right);
O
oceanbase-admin 已提交
628

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

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

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

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

G
gm 已提交
652
private:
O
oceanbase-admin 已提交
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 709 710
  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 已提交
711
inline const char *print_table_status(const TableStatus status_no)
O
oceanbase-admin 已提交
712
{
X
xy0 已提交
713
  const char *status_str = "UNKNOWN";
O
oceanbase-admin 已提交
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
  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 已提交
739
inline const char *print_part_status(const PartitionStatus status)
O
oceanbase-admin 已提交
740
{
X
xy0 已提交
741
  const char *str = "UNKNOWN";
O
oceanbase-admin 已提交
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 829 830
  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 已提交
831
  inline bool operator==(const ObSchemaObjVersion &other) const
O
oceanbase-admin 已提交
832 833 834 835
  {
    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 已提交
836
  inline bool operator!=(const ObSchemaObjVersion &other) const
O
oceanbase-admin 已提交
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 946 947
  {
    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 已提交
948 949 950
  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 已提交
951 952
  void reset();
  inline bool is_valid() const;
X
xy0 已提交
953
  int64_t to_string(char *buf, const int64_t buf_len) const;
O
oceanbase-admin 已提交
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975

  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 已提交
976
  ObZoneScore(common::ObString &zone, int64_t score) : zone_(zone), score_(score)
O
oceanbase-admin 已提交
977 978 979 980 981
  {}
  ObZoneScore() : zone_(), score_(INT64_MAX)
  {}
  virtual ~ObZoneScore()
  {}
X
xy0 已提交
982
  bool operator<(const ObZoneScore &that)
O
oceanbase-admin 已提交
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
  {
    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 已提交
1002
  ObZoneRegion(const ObZoneRegion &that) : zone_(that.zone_), region_(that.region_)
O
oceanbase-admin 已提交
1003
  {}
X
xy0 已提交
1004
  ObZoneRegion(const common::ObZone &zone, const common::ObRegion &region) : zone_(zone), region_(region)
O
oceanbase-admin 已提交
1005 1006 1007 1008 1009 1010 1011 1012
  {}
  virtual ~ObZoneRegion()
  {}
  void reset()
  {
    zone_.reset();
    region_.reset();
  }
X
xy0 已提交
1013
  int assign(const ObZoneRegion &that)
O
oceanbase-admin 已提交
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
  {
    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 已提交
1034
public:
O
oceanbase-admin 已提交
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
  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 已提交
1051
  int compare(const common::ObString &str1, const common::ObString &str2);
O
oceanbase-admin 已提交
1052

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

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

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

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

G
gm 已提交
1106
protected:
O
oceanbase-admin 已提交
1107
  static const int64_t STRING_ARRAY_EXTEND_CNT = 7;
X
xy0 已提交
1108 1109 1110 1111 1112
  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 已提交
1113
  int deep_copy_string_array(
X
xy0 已提交
1114 1115
      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 已提交
1116
  int serialize_string_array(
X
xy0 已提交
1117
      char *buf, const int64_t buf_len, int64_t &pos, const common::ObArrayHelper<common::ObString> &str_array) const;
O
oceanbase-admin 已提交
1118
  int deserialize_string_array(
X
xy0 已提交
1119 1120 1121 1122 1123
      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 已提交
1124 1125 1126 1127
  // 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 已提交
1128
  ObSchema *buffer_;
O
oceanbase-admin 已提交
1129 1130
  int error_ret_;
  bool is_inner_allocator_;
X
xy0 已提交
1131
  common::ObIAllocator *allocator_;
O
oceanbase-admin 已提交
1132 1133 1134 1135
};

template <typename DSTSCHEMA>
int ObSchema::set_charset_and_collation_options(
X
xy0 已提交
1136
    common::ObCharsetType src_charset_type, common::ObCollationType src_collation_type, DSTSCHEMA &dst)
O
oceanbase-admin 已提交
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 1167 1168
{
  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 已提交
1169
public:
X
xy0 已提交
1170
  explicit ObLocality(ObSchema *schema) : schema_(schema)
O
oceanbase-admin 已提交
1171
  {}
X
xy0 已提交
1172 1173 1174 1175
  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 已提交
1176
  int set_specific_replica_attr_array(
X
xy0 已提交
1177
      share::SchemaReplicaAttrArray &schema_replica_set, const common::ObIArray<ReplicaAttr> &src);
O
oceanbase-admin 已提交
1178 1179
  void reset_zone_replica_attr_array();
  int64_t get_convert_size() const;
X
xy0 已提交
1180
  inline const common::ObString &get_locality_str() const
O
oceanbase-admin 已提交
1181 1182 1183 1184 1185 1186
  {
    return locality_str_;
  }
  void reset();
  TO_STRING_KV(K_(locality_str), K_(zone_replica_attr_array));

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

class ObPrimaryZone {
  OB_UNIS_VERSION(1);

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

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

class ObSysVarSchema : public ObSchema {
  OB_UNIS_VERSION(1);

G
gm 已提交
1223
public:
O
oceanbase-admin 已提交
1224 1225 1226 1227 1228 1229
  ObSysVarSchema() : ObSchema()
  {
    reset();
  }
  ~ObSysVarSchema()
  {}
X
xy0 已提交
1230 1231 1232 1233
  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 已提交
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
  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 已提交
1248
  const common::ObString &get_name() const
O
oceanbase-admin 已提交
1249 1250 1251
  {
    return name_;
  }
X
xy0 已提交
1252
  int set_name(const common::ObString &name)
O
oceanbase-admin 已提交
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
  {
    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 已提交
1264
  const common::ObString &get_value() const
O
oceanbase-admin 已提交
1265 1266 1267 1268
  {
    return value_;
  }
  int get_value(
X
xy0 已提交
1269
      common::ObIAllocator *allocator, const common::ObDataTypeCastParams &dtc_params, common::ObObj &value) const;
O
oceanbase-admin 已提交
1270
  inline int get_value(
X
xy0 已提交
1271
      common::ObIAllocator *allocator, const common::ObTimeZoneInfo *tz_info, common::ObObj &value) const
O
oceanbase-admin 已提交
1272 1273 1274 1275
  {
    const common::ObDataTypeCastParams dtc_params(tz_info);
    return get_value(allocator, dtc_params, value);
  }
X
xy0 已提交
1276
  int set_value(const common::ObString &value)
O
oceanbase-admin 已提交
1277 1278 1279
  {
    return deep_copy_str(value, value_);
  }
X
xy0 已提交
1280
  const common::ObString &get_min_val() const
O
oceanbase-admin 已提交
1281 1282 1283
  {
    return min_val_;
  }
X
xy0 已提交
1284
  int set_min_val(const common::ObString &min_val)
O
oceanbase-admin 已提交
1285 1286 1287
  {
    return deep_copy_str(min_val, min_val_);
  }
X
xy0 已提交
1288
  const common::ObString &get_max_val() const
O
oceanbase-admin 已提交
1289 1290 1291
  {
    return max_val_;
  }
X
xy0 已提交
1292
  int set_max_val(const common::ObString &max_val)
O
oceanbase-admin 已提交
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
  {
    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 已提交
1312
  int set_info(const common::ObString &info)
O
oceanbase-admin 已提交
1313 1314 1315
  {
    return deep_copy_str(info, info_);
  }
X
xy0 已提交
1316
  const common::ObString &get_info() const
O
oceanbase-admin 已提交
1317 1318 1319
  {
    return info_;
  }
X
xy0 已提交
1320
  int set_zone(const common::ObZone &zone)
O
oceanbase-admin 已提交
1321 1322 1323
  {
    return zone_.assign(zone);
  }
X
xy0 已提交
1324
  int set_zone(const common::ObString &zone)
O
oceanbase-admin 已提交
1325 1326 1327
  {
    return zone_.assign(zone);
  }
X
xy0 已提交
1328
  const common::ObZone &get_zone() const
O
oceanbase-admin 已提交
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 1357 1358
  {
    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 已提交
1359
private:
O
oceanbase-admin 已提交
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
  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 已提交
1375
public:
O
oceanbase-admin 已提交
1376 1377
  // base methods
  ObSysVariableSchema();
X
xy0 已提交
1378
  explicit ObSysVariableSchema(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
1379
  virtual ~ObSysVariableSchema();
X
xy0 已提交
1380 1381
  ObSysVariableSchema(const ObSysVariableSchema &src_schema);
  ObSysVariableSchema &operator=(const ObSysVariableSchema &src_schema);
O
oceanbase-admin 已提交
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
  // 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 已提交
1408
  int add_sysvar_schema(const share::schema::ObSysVarSchema &sysvar_schema);
O
oceanbase-admin 已提交
1409 1410 1411 1412 1413 1414
  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 已提交
1415 1416 1417 1418
  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 已提交
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
  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 已提交
1431
  int get_oracle_mode(bool &is_oracle_mode) const;
O
oceanbase-admin 已提交
1432
  TO_STRING_KV(K_(tenant_id), K_(schema_version), "sysvars",
X
xy0 已提交
1433
      common::ObArrayWrap<ObSysVarSchema *>(sysvar_array_, ObSysVarFactory::ALL_SYS_VARS_COUNT), K_(read_only),
O
oceanbase-admin 已提交
1434 1435
      K_(name_case_mode));

G
gm 已提交
1436
private:
O
oceanbase-admin 已提交
1437 1438
  uint64_t tenant_id_;
  int64_t schema_version_;
X
xy0 已提交
1439
  ObSysVarSchema *sysvar_array_[ObSysVarFactory::ALL_SYS_VARS_COUNT];
O
oceanbase-admin 已提交
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
  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 已提交
1452
const char *ob_tenant_status_str(const ObTenantStatus);
O
oceanbase-admin 已提交
1453

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

class ObTenantSchema : public ObSchema {
  OB_UNIS_VERSION(1);

G
gm 已提交
1459
public:
O
oceanbase-admin 已提交
1460 1461
  // base methods
  ObTenantSchema();
X
xy0 已提交
1462
  explicit ObTenantSchema(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
1463
  virtual ~ObTenantSchema();
X
xy0 已提交
1464 1465 1466
  ObTenantSchema(const ObTenantSchema &src_schema);
  ObTenantSchema &operator=(const ObTenantSchema &src_schema);
  int assign(const ObTenantSchema &src_schema);
O
oceanbase-admin 已提交
1467
  // for sorted vector
X
xy0 已提交
1468
  static bool cmp(const ObTenantSchema *lhs, const ObTenantSchema *rhs)
O
oceanbase-admin 已提交
1469 1470 1471
  {
    return (NULL != lhs && NULL != rhs) ? lhs->get_tenant_id() < rhs->get_tenant_id() : false;
  }
X
xy0 已提交
1472
  static bool equal(const ObTenantSchema *lhs, const ObTenantSchema *rhs)
O
oceanbase-admin 已提交
1473 1474 1475
  {
    return (NULL != lhs && NULL != rhs) ? lhs->get_tenant_id() == rhs->get_tenant_id() : false;
  }
X
xy0 已提交
1476
  static bool cmp_tenant_id(const ObTenantSchema *lhs, const uint64_t tenant_id)
O
oceanbase-admin 已提交
1477 1478 1479
  {
    return NULL != lhs ? lhs->get_tenant_id() < tenant_id : false;
  }
X
xy0 已提交
1480
  static bool equal_tenant_id(const ObTenantSchema *lhs, const uint64_t tenant_id)
O
oceanbase-admin 已提交
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
  {
    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 已提交
1493
  inline int set_tenant_name(const char *tenant_name)
O
oceanbase-admin 已提交
1494 1495 1496
  {
    return deep_copy_str(tenant_name, tenant_name_);
  }
X
xy0 已提交
1497
  inline int set_comment(const char *comment)
O
oceanbase-admin 已提交
1498 1499 1500
  {
    return deep_copy_str(comment, comment_);
  }
X
xy0 已提交
1501
  inline int set_locality(const char *locality)
O
oceanbase-admin 已提交
1502 1503 1504
  {
    return deep_copy_str(locality, locality_str_);
  }
X
xy0 已提交
1505
  inline int set_previous_locality(const char *previous_locality)
O
oceanbase-admin 已提交
1506 1507 1508
  {
    return deep_copy_str(previous_locality, previous_locality_str_);
  }
X
xy0 已提交
1509
  inline int set_tenant_name(const common::ObString &tenant_name)
O
oceanbase-admin 已提交
1510 1511 1512
  {
    return deep_copy_str(tenant_name, tenant_name_);
  }
X
xy0 已提交
1513 1514 1515 1516 1517
  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 已提交
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
  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 已提交
1530
  inline int set_comment(const common::ObString &comment)
O
oceanbase-admin 已提交
1531 1532 1533
  {
    return deep_copy_str(comment, comment_);
  }
X
xy0 已提交
1534
  inline int set_locality(const common::ObString &locality)
O
oceanbase-admin 已提交
1535 1536 1537
  {
    return deep_copy_str(locality, locality_str_);
  }
X
xy0 已提交
1538
  inline int set_previous_locality(const common::ObString &previous_locality)
O
oceanbase-admin 已提交
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553
  {
    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 已提交
1554 1555
  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 已提交
1556
  int set_specific_replica_attr_array(
X
xy0 已提交
1557
      share::SchemaReplicaAttrArray &schema_replica_set, const common::ObIArray<ReplicaAttr> &src);
O
oceanbase-admin 已提交
1558 1559 1560 1561 1562 1563 1564
  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 已提交
1565
  int set_default_tablegroup_name(const common::ObString &tablegroup_name)
O
oceanbase-admin 已提交
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
  {
    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 已提交
1583
  inline const char *get_tenant_name() const
O
oceanbase-admin 已提交
1584 1585 1586
  {
    return extract_str(tenant_name_);
  }
X
xy0 已提交
1587
  inline const char *get_comment() const
O
oceanbase-admin 已提交
1588 1589 1590
  {
    return extract_str(comment_);
  }
X
xy0 已提交
1591
  inline const char *get_locality() const
O
oceanbase-admin 已提交
1592 1593 1594
  {
    return extract_str(locality_str_);
  }
X
xy0 已提交
1595
  inline const char *get_previous_locality() const
O
oceanbase-admin 已提交
1596 1597 1598
  {
    return extract_str(previous_locality_str_);
  }
X
xy0 已提交
1599
  inline const common::ObString &get_tenant_name_str() const
O
oceanbase-admin 已提交
1600 1601 1602 1603 1604 1605 1606
  {
    return tenant_name_;
  }
  inline int64_t get_logonly_replica_num() const
  {
    return logonly_replica_num_;
  }
X
xy0 已提交
1607
  inline const common::ObString &get_primary_zone() const
O
oceanbase-admin 已提交
1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
  {
    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 已提交
1623
  inline const common::ObString &get_comment_str() const
O
oceanbase-admin 已提交
1624 1625 1626
  {
    return comment_;
  }
X
xy0 已提交
1627
  inline const common::ObString &get_locality_str() const
O
oceanbase-admin 已提交
1628 1629 1630
  {
    return locality_str_;
  }
X
xy0 已提交
1631
  inline const common::ObString &get_previous_locality_str() const
O
oceanbase-admin 已提交
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
  {
    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 已提交
1644 1645 1646 1647 1648
      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 已提交
1649 1650 1651 1652
  {
    return primary_zone_array_;
  }
  int get_primary_zone_inherit(
X
xy0 已提交
1653 1654 1655
      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 已提交
1656
  int64_t get_full_replica_num() const;
X
xy0 已提交
1657 1658
  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 已提交
1659
  int get_zone_list(
X
xy0 已提交
1660
      share::schema::ObSchemaGetterGuard &schema_guard, common::ObIArray<common::ObZone> &zone_list) const;
O
oceanbase-admin 已提交
1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
  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 已提交
1673
  inline const common::ObString &get_default_tablegroup_name() const
O
oceanbase-admin 已提交
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 1731 1732
  {
    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 已提交
1733
      common::ObArrayWrap<ObSysVarSchema *>(sysvar_array_, ObSysVarFactory::ALL_SYS_VARS_COUNT),
O
oceanbase-admin 已提交
1734 1735 1736
      K_(default_tablegroup_id), K_(default_tablegroup_name), K_(compatibility_mode), K_(drop_tenant_time), K_(status),
      K_(in_recyclebin));

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

G
gm 已提交
1745
private:
O
oceanbase-admin 已提交
1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767
  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 已提交
1768
  ObSysVarSchema *sysvar_array_[ObSysVarFactory::ALL_SYS_VARS_COUNT];  // deprecated
O
oceanbase-admin 已提交
1769 1770 1771 1772 1773 1774 1775 1776
  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 已提交
1777
inline int ObTenantSchema::set_zone_list(const common::ObIArray<common::ObString> &zone_list)
O
oceanbase-admin 已提交
1778 1779 1780 1781
{
  return deep_copy_string_array(zone_list, zone_list_);
}

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

X
xy0 已提交
1787
inline int ObTenantSchema::add_zone(const common::ObString &zone)
O
oceanbase-admin 已提交
1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804
{
  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 已提交
1805
public:
O
oceanbase-admin 已提交
1806 1807
  // base methods
  ObDatabaseSchema();
X
xy0 已提交
1808
  explicit ObDatabaseSchema(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
1809
  virtual ~ObDatabaseSchema();
X
xy0 已提交
1810 1811 1812
  ObDatabaseSchema(const ObDatabaseSchema &src_schema);
  ObDatabaseSchema &operator=(const ObDatabaseSchema &src_schema);
  int assign(const ObDatabaseSchema &src_schema);
O
oceanbase-admin 已提交
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825
  // 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 已提交
1826
  int set_database_name(const char *database_name)
O
oceanbase-admin 已提交
1827 1828 1829
  {
    return deep_copy_str(database_name, database_name_);
  }
X
xy0 已提交
1830
  int set_database_name(const common::ObString &database_name)
O
oceanbase-admin 已提交
1831 1832 1833
  {
    return deep_copy_str(database_name, database_name_);
  }
X
xy0 已提交
1834 1835 1836 1837 1838 1839
  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 已提交
1840 1841 1842
  {
    return deep_copy_str(comment, comment_);
  }
X
xy0 已提交
1843
  int set_comment(const common::ObString &comment)
O
oceanbase-admin 已提交
1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866
  {
    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 已提交
1867
  int set_default_tablegroup_name(const common::ObString &tablegroup_name)
O
oceanbase-admin 已提交
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893
  {
    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 已提交
1894
  inline const char *get_database_name() const
O
oceanbase-admin 已提交
1895 1896 1897
  {
    return extract_str(database_name_);
  }
X
xy0 已提交
1898
  inline const common::ObString &get_database_name_str() const
O
oceanbase-admin 已提交
1899 1900 1901
  {
    return database_name_;
  }
X
xy0 已提交
1902
  inline const common::ObString &get_primary_zone() const
O
oceanbase-admin 已提交
1903 1904 1905
  {
    return primary_zone_;
  }
X
xy0 已提交
1906
  inline const char *get_comment() const
O
oceanbase-admin 已提交
1907 1908 1909
  {
    return extract_str(comment_);
  }
X
xy0 已提交
1910
  inline const common::ObString &get_comment_str() const
O
oceanbase-admin 已提交
1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933
  {
    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 已提交
1934
  inline const common::ObString &get_default_tablegroup_name() const
O
oceanbase-admin 已提交
1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
  {
    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 已提交
1946
  inline const common::ObString &get_locality_str() const
O
oceanbase-admin 已提交
1947 1948 1949 1950 1951
  {
    static const common::ObString dummy;
    return dummy;
  }
  int get_raw_first_primary_zone(
X
xy0 已提交
1952 1953 1954 1955 1956 1957
      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 已提交
1958 1959 1960 1961
  {
    return primary_zone_array_;
  }
  int get_primary_zone_inherit(
X
xy0 已提交
1962 1963
      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 已提交
1964
  int get_zone_replica_attr_array_inherit(
X
xy0 已提交
1965
      share::schema::ObSchemaGetterGuard &schema_guard, ZoneLocalityIArray &locality) const;
O
oceanbase-admin 已提交
1966 1967
  // In the current implementation, the zone_list of the Database is directly read from the corresponding tenant.
  int get_zone_list(
X
xy0 已提交
1968
      share::schema::ObSchemaGetterGuard &schema_guard, common::ObIArray<common::ObZone> &zone_list) const;
O
oceanbase-admin 已提交
1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989
  // 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 已提交
1990
private:
O
oceanbase-admin 已提交
1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011
  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 已提交
2012
inline int ObDatabaseSchema::set_zone_list(const common::ObIArray<common::ObString> &zone_list)
O
oceanbase-admin 已提交
2013 2014 2015 2016
{
  return deep_copy_string_array(zone_list, zone_list_);
}

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

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

class ObPartitionOption : public ObSchema {
  OB_UNIS_VERSION(1);

G
gm 已提交
2030
public:
O
oceanbase-admin 已提交
2031
  ObPartitionOption();
X
xy0 已提交
2032
  explicit ObPartitionOption(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
2033
  virtual ~ObPartitionOption();
X
xy0 已提交
2034 2035 2036 2037
  ObPartitionOption(const ObPartitionOption &expr);
  ObPartitionOption &operator=(const ObPartitionOption &expr);
  bool operator==(const ObPartitionOption &expr) const;
  bool operator!=(const ObPartitionOption &expr) const;
O
oceanbase-admin 已提交
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 2063 2064

  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 已提交
2065
  int set_part_expr(const common::ObString &expr)
O
oceanbase-admin 已提交
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 2097 2098
  {
    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 已提交
2099
  inline const common::ObString &get_part_func_expr_str() const
O
oceanbase-admin 已提交
2100 2101 2102
  {
    return part_func_expr_;
  }
X
xy0 已提交
2103
  inline const char *get_part_func_expr() const
O
oceanbase-admin 已提交
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 2133 2134
  {
    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 已提交
2135
  const common::ObString &get_intervel_start_str() const
O
oceanbase-admin 已提交
2136 2137 2138
  {
    return interval_start_;
  }
X
xy0 已提交
2139
  const common::ObString &get_part_intervel_str() const
O
oceanbase-admin 已提交
2140 2141 2142 2143 2144 2145 2146 2147 2148 2149
  {
    return part_interval_;
  }
  inline int64_t get_auto_part_size() const
  {
    return auto_part_size_;
  }

  // other methods
  virtual void reset();
X
xy0 已提交
2150
  int64_t assign(const ObPartitionOption &src_part);
O
oceanbase-admin 已提交
2151 2152 2153 2154 2155
  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 已提交
2156
private:
O
oceanbase-admin 已提交
2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172
  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 已提交
2173
public:
O
oceanbase-admin 已提交
2174 2175
  ObSchemaAllocator() : allocator_(NULL)
  {}
X
xy0 已提交
2176
  ObSchemaAllocator(common::ObIAllocator &allocator) : allocator_(&allocator)
O
oceanbase-admin 已提交
2177 2178
  {}

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

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

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

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

// 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 已提交
2208
public:
O
oceanbase-admin 已提交
2209 2210 2211
  InnerPartListVectorCmp() : ret_(common::OB_SUCCESS)
  {}

G
gm 已提交
2212
public:
X
xy0 已提交
2213
  bool operator()(const common::ObNewRow &left, const common::ObNewRow &right)
O
oceanbase-admin 已提交
2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230
  {
    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 已提交
2231
private:
O
oceanbase-admin 已提交
2232 2233 2234 2235 2236 2237 2238
  int ret_;
};

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

G
gm 已提交
2239
public:
O
oceanbase-admin 已提交
2240 2241 2242
  friend class ObPartitionUtils;
  friend class sql::ObPartitionExecutorUtils;
  ObBasePartition();
X
xy0 已提交
2243
  explicit ObBasePartition(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
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 2288 2289
  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 已提交
2290
  int set_part_name(common::ObString &part_name)
O
oceanbase-admin 已提交
2291 2292 2293
  {
    return deep_copy_str(part_name, name_);
  }
X
xy0 已提交
2294
  const common::ObString &get_part_name() const
O
oceanbase-admin 已提交
2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307
  {
    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 已提交
2308
  int assign(const ObBasePartition &src_part);
O
oceanbase-admin 已提交
2309 2310

  // This interface is not strictly semantically less than, please note
X
xy0 已提交
2311 2312 2313 2314 2315
  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 已提交
2316 2317 2318
  {
    return list_row_values_.push_back(row);
  }
X
xy0 已提交
2319 2320 2321 2322
  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 已提交
2323 2324 2325 2326 2327
  {
    return high_bound_val_;
  }
  virtual int64_t get_deep_copy_size() const;

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

X
xy0 已提交
2333
  bool same_base_partition(const ObBasePartition &other) const
O
oceanbase-admin 已提交
2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348
  {
    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 已提交
2349
  const common::ObIArray<int64_t> &get_source_part_ids() const
O
oceanbase-admin 已提交
2350 2351 2352
  {
    return source_part_ids_;
  }
X
xy0 已提交
2353
  common::ObIArray<int64_t> &get_source_part_ids()
O
oceanbase-admin 已提交
2354 2355 2356
  {
    return source_part_ids_;
  }
X
xy0 已提交
2357
  int set_source_part_id(common::ObString &partition_str);
O
oceanbase-admin 已提交
2358 2359
  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 已提交
2360
  int get_source_part_ids_str(char *str, const int64_t buf_len) const;
O
oceanbase-admin 已提交
2361 2362 2363 2364 2365 2366 2367 2368 2369
  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 已提交
2370
protected:
O
oceanbase-admin 已提交
2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386
  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 已提交
2387
  int32_t *projector_;
O
oceanbase-admin 已提交
2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402
  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 已提交
2403
public:
O
oceanbase-admin 已提交
2404
  ObPartition();
X
xy0 已提交
2405 2406
  explicit ObPartition(common::ObIAllocator *allocator);
  int assign(const ObPartition &src_part);
O
oceanbase-admin 已提交
2407 2408 2409

  void reset();
  virtual int64_t get_convert_size() const;
X
xy0 已提交
2410
  virtual int clone(common::ObIAllocator &allocator, ObPartition *&dst) const;
O
oceanbase-admin 已提交
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 2458 2459

  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 已提交
2460
  ObSubPartition **get_subpart_array() const
O
oceanbase-admin 已提交
2461 2462 2463 2464 2465 2466 2467
  {
    return subpartition_array_;
  }
  int64_t get_subpartition_num() const
  {
    return subpartition_num_;
  }
X
xy0 已提交
2468
  ObSubPartition **get_sorted_part_id_subpartition_array() const
O
oceanbase-admin 已提交
2469 2470 2471
  {
    return sorted_part_id_subpartition_array_;
  }
X
xy0 已提交
2472
  bool same_partition(const ObPartition &other) const
O
oceanbase-admin 已提交
2473 2474 2475 2476 2477
  {
    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 已提交
2478
  int add_partition(const ObSubPartition &subpartition);
O
oceanbase-admin 已提交
2479
  int generate_mapping_pg_subpartition_array();
X
xy0 已提交
2480
  ObSubPartition **get_dropped_subpart_array() const
O
oceanbase-admin 已提交
2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494
  {
    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 已提交
2495
      common::ObArrayWrap<ObSubPartition *>(subpartition_array_, subpartition_num_), K_(subpartition_array_capacity),
O
oceanbase-admin 已提交
2496
      "dropped_subpartition_array",
X
xy0 已提交
2497
      common::ObArrayWrap<ObSubPartition *>(dropped_subpartition_array_, dropped_subpartition_num_),
O
oceanbase-admin 已提交
2498 2499
      K_(dropped_subpartition_array_capacity));

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

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

G
gm 已提交
2507
private:
O
oceanbase-admin 已提交
2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524
  // 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 已提交
2525
  ObSubPartition **subpartition_array_;
O
oceanbase-admin 已提交
2526
  // Generated when the schema is refreshed, only used by the table, used to quickly index pg_key, not serialized
X
xy0 已提交
2527
  ObSubPartition **sorted_part_id_subpartition_array_;
O
oceanbase-admin 已提交
2528 2529 2530
  /* delay delete subpartition */
  int64_t dropped_subpartition_num_;
  int64_t dropped_subpartition_array_capacity_;
X
xy0 已提交
2531
  ObSubPartition **dropped_subpartition_array_;
O
oceanbase-admin 已提交
2532 2533 2534 2535 2536
};

struct ObSubPartition : public ObBasePartition {
  OB_UNIS_VERSION(1);

G
gm 已提交
2537
public:
O
oceanbase-admin 已提交
2538 2539 2540
  // 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 已提交
2541
public:
O
oceanbase-admin 已提交
2542
  ObSubPartition();
X
xy0 已提交
2543 2544
  explicit ObSubPartition(common::ObIAllocator *allocator);
  int assign(const ObSubPartition &src_part);
O
oceanbase-admin 已提交
2545
  virtual void reset();
X
xy0 已提交
2546
  virtual int clone(common::ObIAllocator &allocator, ObSubPartition *&dst) const;
O
oceanbase-admin 已提交
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 2584 2585

  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 已提交
2586 2587
  static bool less_than(const ObSubPartition *lhs, const ObSubPartition *rhs);
  bool same_sub_partition(const ObSubPartition &other) const
O
oceanbase-admin 已提交
2588 2589 2590 2591
  {
    return same_base_partition(other) && subpart_id_ == other.subpart_id_;
  }
  virtual int64_t get_convert_size() const;
X
xy0 已提交
2592 2593
  bool key_match(const ObSubPartition &other) const;
  static bool hash_like_func_less_than(const ObSubPartition *lhs, const ObSubPartition *rhs);
O
oceanbase-admin 已提交
2594 2595 2596
  INHERIT_TO_STRING_KV("BasePartition", ObBasePartition, K_(subpart_id), K_(subpart_idx), K_(mapping_pg_sub_part_id),
      K_(drop_schema_version));

G
gm 已提交
2597
private:
O
oceanbase-admin 已提交
2598 2599 2600 2601 2602 2603 2604 2605 2606 2607
  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 已提交
2608
public:
O
oceanbase-admin 已提交
2609 2610
  // base methods
  ObPartitionSchema();
X
xy0 已提交
2611
  explicit ObPartitionSchema(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
2612
  virtual ~ObPartitionSchema();
X
xy0 已提交
2613 2614
  ObPartitionSchema(const ObPartitionSchema &src_schema);
  ObPartitionSchema &operator=(const ObPartitionSchema &src_schema);
O
oceanbase-admin 已提交
2615 2616
  // partition related

X
xy0 已提交
2617
  virtual const char *get_entity_name() const = 0;
O
oceanbase-admin 已提交
2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631
  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 已提交
2632
  virtual int get_primary_zone_inherit(ObSchemaGetterGuard &schema_guard, ObPrimaryZone &primary_zone) const = 0;
O
oceanbase-admin 已提交
2633
  virtual int get_zone_replica_attr_array_inherit(
X
xy0 已提交
2634
      ObSchemaGetterGuard &schema_guard, ZoneLocalityIArray &locality) const = 0;
O
oceanbase-admin 已提交
2635
  virtual int get_zone_list(
X
xy0 已提交
2636
      share::schema::ObSchemaGetterGuard &schema_guard, common::ObIArray<common::ObZone> &zone_list) const = 0;
O
oceanbase-admin 已提交
2637
  virtual int get_locality_str_inherit(
X
xy0 已提交
2638 2639 2640 2641 2642
      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 已提交
2643 2644
  virtual uint64_t get_tablegroup_id() const = 0;
  virtual void set_tablegroup_id(const uint64_t tg_id) = 0;
X
xy0 已提交
2645 2646 2647 2648
  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 已提交
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 2701 2702
  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 已提交
2703
  int get_first_individual_sub_part_num(int64_t &sub_part_num) const;
O
oceanbase-admin 已提交
2704 2705
  // SQL view, unable to see the delayed deleted partition
  int64_t get_all_part_num() const;
X
xy0 已提交
2706
  int get_all_partition_num(bool check_dropped_partition, int64_t &part_num) const;
O
oceanbase-admin 已提交
2707

X
xy0 已提交
2708 2709 2710 2711
  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 已提交
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 2757 2758

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

X
xy0 已提交
2780 2781 2782 2783 2784
  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 已提交
2785 2786

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

  /** 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 已提交
2800 2801
  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 已提交
2802
  // Get the partition name corresponding to partition_id (non-partition/partition/subpartition)
X
xy0 已提交
2803
  int get_partition_name(const int64_t partition_id, char *buf, const int64_t buf_size, int64_t *pos /* = NULL*/) const;
O
oceanbase-admin 已提交
2804
  // get partition name
X
xy0 已提交
2805
  int get_part_name(const int64_t part_id, char *buf, const int64_t buf_size, int64_t *pos /* = NULL*/) const;
O
oceanbase-admin 已提交
2806
  // is_def_subpart refers to the generation of __all_def_sub_part or the part_name in the __all_sub_part table
X
xy0 已提交
2807 2808
  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 已提交
2809

X
xy0 已提交
2810
  inline ObPartition **get_part_array() const
O
oceanbase-admin 已提交
2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822
  {
    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 已提交
2823
  inline ObSubPartition **get_def_subpart_array() const
O
oceanbase-admin 已提交
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 2875 2876
  {
    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 已提交
2877
  ObPartition **get_dropped_part_array() const
O
oceanbase-admin 已提交
2878 2879 2880 2881
  {
    return dropped_partition_array_;
  }
  // -------------------------------------
X
xy0 已提交
2882
  int get_part_id_by_name(const common::ObString partition_name, int64_t &part_id) const;
O
oceanbase-admin 已提交
2883 2884
  // 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 已提交
2885
      const int64_t part_id, const bool check_dropped_partition, const ObPartition *&partition) const;
O
oceanbase-admin 已提交
2886
  int get_subpart_info(
X
xy0 已提交
2887
      const int64_t part_id, ObSubPartition **&subpart_array, int64_t &subpart_num, int64_t &subpartition_num) const;
O
oceanbase-admin 已提交
2888
  //-------index partition-------
X
xy0 已提交
2889 2890
  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 已提交
2891

X
xy0 已提交
2892 2893
  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 已提交
2894

X
xy0 已提交
2895 2896
  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 已提交
2897 2898 2899 2900
  //----------------------
  // 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 已提交
2901
      const int64_t part_id, const bool check_dropped_partition, int64_t &partition_index) const;
O
oceanbase-admin 已提交
2902 2903
  // Binary search range partition, only applicable to the partition that has not been split
  int get_partition_index_binary_search(
X
xy0 已提交
2904
      const int64_t part_id, const bool check_dropped_partition, int64_t &partition_index) const;
O
oceanbase-admin 已提交
2905 2906
  // Iterate all the partitions to get the offset
  int get_partition_index_loop(
X
xy0 已提交
2907
      const int64_t part_id, const bool check_dropped_partition, int64_t &partition_index) const;
O
oceanbase-admin 已提交
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 2940 2941

  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 已提交
2942
      const common::ObPartitionKey &dst_part_key, common::ObPartitionKey &source_part_key) const;
O
oceanbase-admin 已提交
2943
  DECLARE_VIRTUAL_TO_STRING;
X
xy0 已提交
2944 2945
  int try_assign_part_array(const share::schema::ObPartitionSchema &that);
  int try_assign_def_subpart_array(const share::schema::ObPartitionSchema &that);
O
oceanbase-admin 已提交
2946

G
gm 已提交
2947
protected:
X
xy0 已提交
2948 2949
  int inner_add_partition(const ObPartition &part);
  int inner_add_partition(const ObSubPartition &part);
O
oceanbase-admin 已提交
2950
  template <class T>
X
xy0 已提交
2951 2952 2953 2954
  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 已提交
2955

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

G
gm 已提交
2959
protected:
O
oceanbase-admin 已提交
2960 2961 2962 2963 2964 2965 2966
  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 已提交
2967
  ObPartition **partition_array_;
O
oceanbase-admin 已提交
2968 2969 2970 2971 2972
  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 已提交
2973
  ObSubPartition **def_subpartition_array_;
O
oceanbase-admin 已提交
2974 2975 2976 2977 2978 2979 2980 2981 2982 2983
  // 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 已提交
2984
  ObPartition **dropped_partition_array_;
O
oceanbase-admin 已提交
2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995
  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 已提交
2996
public:
O
oceanbase-admin 已提交
2997 2998
  // base methods
  ObTablegroupSchema();
X
xy0 已提交
2999
  explicit ObTablegroupSchema(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
3000
  virtual ~ObTablegroupSchema();
X
xy0 已提交
3001 3002 3003
  ObTablegroupSchema(const ObTablegroupSchema &src_schema);
  ObTablegroupSchema &operator=(const ObTablegroupSchema &src_schema);
  int assign(const ObTablegroupSchema &src_schema);
O
oceanbase-admin 已提交
3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020
  // 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 已提交
3021
  inline int set_tablegroup_name(const char *name)
O
oceanbase-admin 已提交
3022 3023 3024
  {
    return deep_copy_str(name, tablegroup_name_);
  }
X
xy0 已提交
3025
  inline int set_comment(const char *comment)
O
oceanbase-admin 已提交
3026 3027 3028
  {
    return deep_copy_str(comment, comment_);
  }
X
xy0 已提交
3029
  inline int set_tablegroup_name(const common::ObString &name)
O
oceanbase-admin 已提交
3030 3031 3032
  {
    return deep_copy_str(name, tablegroup_name_);
  }
X
xy0 已提交
3033
  inline int set_table_name(const common::ObString &name)
O
oceanbase-admin 已提交
3034 3035 3036
  {
    return deep_copy_str(name, tablegroup_name_);
  }
X
xy0 已提交
3037
  inline int set_comment(const common::ObString &comment)
O
oceanbase-admin 已提交
3038 3039 3040
  {
    return deep_copy_str(comment, comment_);
  }
X
xy0 已提交
3041
  inline int set_locality(const common::ObString &locality)
O
oceanbase-admin 已提交
3042 3043 3044
  {
    return deep_copy_str(locality, locality_info_.locality_str_);
  }
X
xy0 已提交
3045
  inline int set_primary_zone(const common::ObString &primary_zone)
O
oceanbase-admin 已提交
3046 3047 3048
  {
    return deep_copy_str(primary_zone, primary_zone_info_.primary_zone_str_);
  }
X
xy0 已提交
3049
  inline int set_previous_locality(const common::ObString &previous_locality)
O
oceanbase-admin 已提交
3050 3051 3052
  {
    return deep_copy_str(previous_locality, previous_locality_);
  }
X
xy0 已提交
3053
  int set_primary_zone_array(const common::ObIArray<ObZoneScore> &primary_zone_array);
O
oceanbase-admin 已提交
3054

X
xy0 已提交
3055 3056 3057
  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 已提交
3058 3059 3060
  {
    return deep_copy_str(split_partition, split_partition_name_);
  }
X
xy0 已提交
3061
  inline int set_split_rowkey(const common::ObRowkey &rowkey)
O
oceanbase-admin 已提交
3062 3063 3064
  {
    return rowkey.deep_copy(split_high_bound_val_, *get_allocator());
  }
X
xy0 已提交
3065
  inline int set_split_list_value(common::ObRowkey &list_values)
O
oceanbase-admin 已提交
3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085
  {
    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 已提交
3086
  inline const char *get_tablegroup_name_str() const
O
oceanbase-admin 已提交
3087 3088 3089
  {
    return extract_str(tablegroup_name_);
  }
X
xy0 已提交
3090
  inline const char *get_comment() const
O
oceanbase-admin 已提交
3091 3092 3093
  {
    return extract_str(comment_);
  }
X
xy0 已提交
3094
  inline const common::ObString &get_tablegroup_name() const
O
oceanbase-admin 已提交
3095 3096 3097
  {
    return tablegroup_name_;
  }
X
xy0 已提交
3098
  inline const common::ObString &get_table_name() const
O
oceanbase-admin 已提交
3099 3100 3101
  {
    return tablegroup_name_;
  }
X
xy0 已提交
3102
  virtual const char *get_entity_name() const override
O
oceanbase-admin 已提交
3103 3104 3105
  {
    return extract_str(tablegroup_name_);
  }
X
xy0 已提交
3106
  inline const common::ObString &get_comment_str() const
O
oceanbase-admin 已提交
3107 3108 3109
  {
    return comment_;
  }
X
xy0 已提交
3110
  inline const ObLocality &get_locality_info() const
O
oceanbase-admin 已提交
3111 3112 3113
  {
    return locality_info_;
  }
X
xy0 已提交
3114
  virtual common::ObString &get_locality_str() override
O
oceanbase-admin 已提交
3115 3116 3117
  {
    return locality_info_.locality_str_;
  }
X
xy0 已提交
3118
  virtual const common::ObString &get_locality_str() const override
O
oceanbase-admin 已提交
3119 3120 3121
  {
    return locality_info_.locality_str_;
  }
X
xy0 已提交
3122
  inline const char *get_locality() const
O
oceanbase-admin 已提交
3123 3124 3125
  {
    return extract_str(locality_info_.locality_str_);
  }
X
xy0 已提交
3126
  inline const char *get_previous_locality() const
O
oceanbase-admin 已提交
3127 3128 3129
  {
    return extract_str(previous_locality_);
  }
X
xy0 已提交
3130
  virtual const common::ObString &get_previous_locality_str() const override
O
oceanbase-admin 已提交
3131 3132 3133
  {
    return previous_locality_;
  }
X
xy0 已提交
3134
  inline const ObPrimaryZone &get_primary_zone_info() const
O
oceanbase-admin 已提交
3135 3136 3137
  {
    return primary_zone_info_;
  }
X
xy0 已提交
3138
  inline const common::ObString &get_primary_zone() const
O
oceanbase-admin 已提交
3139 3140 3141
  {
    return primary_zone_info_.primary_zone_str_;
  }
X
xy0 已提交
3142
  int set_zone_list(const common::ObIArray<common::ObZone> &zone_list)
O
oceanbase-admin 已提交
3143 3144 3145 3146
  {
    UNUSED(zone_list);
    return common::OB_SUCCESS;
  }
X
xy0 已提交
3147
  inline const common::ObString &get_split_partition_name() const
O
oceanbase-admin 已提交
3148 3149 3150
  {
    return split_partition_name_;
  }
X
xy0 已提交
3151
  inline const common::ObRowkey &get_split_rowkey() const
O
oceanbase-admin 已提交
3152 3153 3154
  {
    return split_high_bound_val_;
  }
X
xy0 已提交
3155
  inline const common::ObRowkey &get_split_list_row_values() const
O
oceanbase-admin 已提交
3156 3157 3158 3159 3160 3161 3162
  {
    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 已提交
3163 3164
      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 已提交
3165
  virtual int get_zone_replica_attr_array_inherit(
X
xy0 已提交
3166
      ObSchemaGetterGuard &schema_guard, ZoneLocalityIArray &locality) const override;
O
oceanbase-admin 已提交
3167
  virtual int get_locality_str_inherit(
X
xy0 已提交
3168 3169 3170 3171 3172 3173 3174 3175
      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 已提交
3176 3177 3178 3179
  {
    return primary_zone_info_.primary_zone_array_;
  }
  virtual int get_primary_zone_inherit(
X
xy0 已提交
3180
      share::schema::ObSchemaGetterGuard &schema_guard, share::schema::ObPrimaryZone &primary_zone) const override;
O
oceanbase-admin 已提交
3181 3182
  int fill_additional_options();
  int check_in_locality_modification(
X
xy0 已提交
3183 3184 3185 3186 3187 3188
      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 已提交
3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209
  // 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 已提交
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 3244 3245
  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 已提交
3246 3247
  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 已提交
3248
  // other methods
G
gm 已提交
3249 3250
  virtual void reset() override;
  int64_t get_convert_size() const override;
G
gm 已提交
3251 3252 3253 3254
  virtual bool has_self_partition() const override
  {
    return get_binding();
  }
G
gm 已提交
3255
  virtual bool is_valid() const override;
G
gm 已提交
3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271
  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;
  }
3272 3273 3274 3275
  inline bool can_rereplicate_global_index_table() const
  {
    return true;
  }
O
oceanbase-admin 已提交
3276 3277 3278

  DECLARE_VIRTUAL_TO_STRING;

G
gm 已提交
3279
private:
O
oceanbase-admin 已提交
3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301
  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 已提交
3302
public:
O
oceanbase-admin 已提交
3303 3304 3305
  /// Get part ids through range of partition_column

  // Get hash(FUNC_HASH/FUNC_KEY) part type ids
X
xy0 已提交
3306 3307
  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 已提交
3308 3309 3310 3311 3312
  // @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 已提交
3313 3314 3315 3316
  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 已提交
3317 3318 3319 3320 3321 3322
  // @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 已提交
3323 3324
  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 已提交
3325 3326

  // Get level-one range part_ids
X
xy0 已提交
3327 3328
  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 已提交
3329
  // The meaning of get_part_index can refer to get_hash_part_ids()
X
xy0 已提交
3330 3331
  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 已提交
3332 3333

  // Get level-one range part_ids
X
xy0 已提交
3334 3335
  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 已提交
3336 3337

  // The meaning of get_part_index can refer to get_hash_part_ids()
X
xy0 已提交
3338 3339
  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 已提交
3340 3341

  // Get level-two range part_ids
X
xy0 已提交
3342 3343
  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 已提交
3344
  // The meaning of get_subpart_index can refer to get_hash_subpart_ids()
X
xy0 已提交
3345 3346
  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 已提交
3347 3348 3349
      bool get_subpart_index = false);

  // Get level-two range part_ids
X
xy0 已提交
3350 3351
  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 已提交
3352 3353

  // The meaning of get_subpart_index can refer to get_hash_subpart_ids()
X
xy0 已提交
3354 3355
  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 已提交
3356 3357 3358 3359 3360
      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 已提交
3361
  static int calc_hash_part_idx(const uint64_t val, const int64_t part_num, int64_t &partition_idx);
O
oceanbase-admin 已提交
3362 3363
  template <typename T>
  static int get_range_part_idx(
X
xy0 已提交
3364
      const common::ObObj &obj, T *const *part_array, const int64_t part_num, int64_t &part_idx);
O
oceanbase-admin 已提交
3365 3366

  template <typename T>
X
xy0 已提交
3367 3368
  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 已提交
3369 3370

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

  // Convert rowkey to sql literal for show
X
xy0 已提交
3374 3375
  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 已提交
3376
  // Used to display the defined value of the LIST partition
X
xy0 已提交
3377 3378 3379 3380
  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 已提交
3381 3382

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

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

  // Get partition end with end_part rowkey, return end_pos
  template <typename T>
X
xy0 已提交
3394 3395
  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 已提交
3396 3397 3398 3399

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

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

G
gm 已提交
3404
private:
O
oceanbase-admin 已提交
3405 3406
  // Get level-one range part_ids
  // The meaning of get_part_index can refer to get_hash_part_ids()
X
xy0 已提交
3407 3408 3409
  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 已提交
3410 3411
  // Get level-two range part_ids
  // The meaning of get_subpart_index can refer to get_hash_subpart_ids()
X
xy0 已提交
3412 3413 3414
  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 已提交
3415 3416 3417 3418
};

template <typename T>
int ObPartitionUtils::get_range_part_idx(
X
xy0 已提交
3419
    const common::ObObj &obj, T *const *part_array, const int64_t part_num, int64_t &part_idx)
O
oceanbase-admin 已提交
3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442
{
  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 已提交
3443 3444
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 已提交
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 3472 3473 3474 3475 3476 3477
{
  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 已提交
3478
    const T *const *partition_array, const int64_t partition_num, const T &start_part, int64_t &start_pos)
O
oceanbase-admin 已提交
3479 3480 3481 3482 3483 3484
{
  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 已提交
3485
    const T *const *result =
O
oceanbase-admin 已提交
3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496
        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 已提交
3497 3498
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 已提交
3499 3500 3501 3502 3503 3504
{
  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 已提交
3505
    const T *const *result =
O
oceanbase-admin 已提交
3506 3507 3508 3509 3510 3511 3512 3513 3514
        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 已提交
3515
      lrow.cells_ = const_cast<common::ObObj *>((*result)->high_bound_val_.get_obj_ptr());
O
oceanbase-admin 已提交
3516 3517 3518 3519
      lrow.count_ = (*result)->high_bound_val_.get_obj_cnt();
      lrow.projector_ = (*result)->projector_;
      lrow.projector_size_ = (*result)->projector_size_;
      common::ObNewRow rrow;
X
xy0 已提交
3520
      rrow.cells_ = const_cast<common::ObObj *>(end_part.high_bound_val_.get_obj_ptr());
O
oceanbase-admin 已提交
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 3566 3567 3568 3569 3570 3571
      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 已提交
3572
public:
O
oceanbase-admin 已提交
3573
  ObViewSchema();
X
xy0 已提交
3574
  explicit ObViewSchema(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
3575
  virtual ~ObViewSchema();
X
xy0 已提交
3576 3577 3578 3579
  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 已提交
3580

X
xy0 已提交
3581
  inline int set_view_definition(const char *view_definition)
O
oceanbase-admin 已提交
3582 3583 3584
  {
    return deep_copy_str(view_definition, view_definition_);
  }
X
xy0 已提交
3585
  inline int set_view_definition(const common::ObString &view_definition)
O
oceanbase-admin 已提交
3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611
  {
    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 已提交
3612
  inline const common::ObString &get_view_definition_str() const
O
oceanbase-admin 已提交
3613 3614 3615
  {
    return view_definition_;
  }
X
xy0 已提交
3616
  inline const char *get_view_definition() const
O
oceanbase-admin 已提交
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 3643 3644 3645 3646 3647 3648
  {
    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 已提交
3649
private:
O
oceanbase-admin 已提交
3650 3651 3652 3653 3654 3655 3656 3657 3658
  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 已提交
3659
public:
O
oceanbase-admin 已提交
3660 3661
  ObColumnSchemaHashWrapper()
  {}
X
xy0 已提交
3662
  explicit ObColumnSchemaHashWrapper(const common::ObString &str) : column_name_(str)
O
oceanbase-admin 已提交
3663 3664 3665
  {}
  ~ObColumnSchemaHashWrapper()
  {}
X
xy0 已提交
3666
  void set_name(const common::ObString &str)
O
oceanbase-admin 已提交
3667 3668 3669
  {
    column_name_ = str;
  }
X
xy0 已提交
3670
  inline bool operator==(const ObColumnSchemaHashWrapper &other) const
O
oceanbase-admin 已提交
3671 3672 3673 3674 3675 3676 3677 3678
  {
    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 已提交
3679
public:
O
oceanbase-admin 已提交
3680 3681
  ObColumnSchemaWrapper() : column_name_(), prefix_len_(0)
  {}
X
xy0 已提交
3682
  explicit ObColumnSchemaWrapper(const common::ObString &str, int32_t prefix_len)
O
oceanbase-admin 已提交
3683 3684 3685 3686
      : column_name_(str), prefix_len_(prefix_len)
  {}
  ~ObColumnSchemaWrapper()
  {}
X
xy0 已提交
3687
  void set_name(const common::ObString &str)
O
oceanbase-admin 已提交
3688 3689 3690
  {
    column_name_ = str;
  }
X
xy0 已提交
3691
  inline bool name_equal(const ObColumnSchemaWrapper &other) const
O
oceanbase-admin 已提交
3692 3693 3694 3695
  {
    ObCompareNameWithTenantID name_cmp;
    return (0 == name_cmp.compare(column_name_, other.column_name_));
  }
X
xy0 已提交
3696
  inline bool all_equal(const ObColumnSchemaWrapper &other) const
O
oceanbase-admin 已提交
3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719
  {
    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 已提交
3720
public:
X
xf0 已提交
3721 3722
  ObIndexSchemaHashWrapper()
      : tenant_id_(common::OB_INVALID_ID), database_id_(common::OB_INVALID_ID), data_table_id_(common::OB_INVALID_ID)
O
oceanbase-admin 已提交
3723
  {}
X
xf0 已提交
3724
  ObIndexSchemaHashWrapper(
X
xy0 已提交
3725
      uint64_t tenant_id, const uint64_t database_id, const uint64_t data_table_id, const common::ObString &index_name)
X
xf0 已提交
3726
      : tenant_id_(tenant_id), database_id_(database_id), data_table_id_(data_table_id), index_name_(index_name)
O
oceanbase-admin 已提交
3727 3728 3729 3730
  {}
  ~ObIndexSchemaHashWrapper()
  {}
  inline uint64_t hash() const;
X
xy0 已提交
3731
  inline bool operator==(const ObIndexSchemaHashWrapper &rv) const;
O
oceanbase-admin 已提交
3732 3733 3734 3735 3736 3737 3738 3739 3740

  inline uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
  inline uint64_t get_database_id() const
  {
    return database_id_;
  }
X
xf0 已提交
3741 3742 3743 3744
  inline uint64_t get_data_table_id() const
  {
    return data_table_id_;
  }
X
xy0 已提交
3745
  inline const common::ObString &get_index_name() const
O
oceanbase-admin 已提交
3746 3747 3748 3749
  {
    return index_name_;
  }

G
gm 已提交
3750
private:
O
oceanbase-admin 已提交
3751 3752
  uint64_t tenant_id_;
  uint64_t database_id_;
X
xf0 已提交
3753
  uint64_t data_table_id_;  // only for mysql mode
O
oceanbase-admin 已提交
3754 3755 3756 3757 3758 3759 3760 3761
  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 已提交
3762
  hash_ret = common::murmurhash(&data_table_id_, sizeof(uint64_t), hash_ret);
O
oceanbase-admin 已提交
3763 3764 3765 3766 3767
  // case insensitive
  hash_ret = common::ObCharset::hash(common::CS_TYPE_UTF8MB4_GENERAL_CI, index_name_, hash_ret);
  return hash_ret;
}

X
xy0 已提交
3768
inline bool ObIndexSchemaHashWrapper::operator==(const ObIndexSchemaHashWrapper &rv) const
O
oceanbase-admin 已提交
3769 3770 3771 3772
{
  // mysql case insensitive
  // oracle case sensitive
  ObCompareNameWithTenantID name_cmp(tenant_id_);
X
xf0 已提交
3773
  return (tenant_id_ == rv.tenant_id_) && (database_id_ == rv.database_id_) && (data_table_id_ == rv.data_table_id_) &&
O
oceanbase-admin 已提交
3774 3775 3776 3777
         (0 == name_cmp.compare(index_name_, rv.index_name_));
}

class ObTableSchemaHashWrapper {
G
gm 已提交
3778
public:
O
oceanbase-admin 已提交
3779 3780 3781 3782 3783 3784 3785
  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 已提交
3786
      const common::ObNameCaseMode mode, const common::ObString &table_name)
O
oceanbase-admin 已提交
3787 3788 3789 3790 3791 3792 3793 3794 3795
      : 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 已提交
3796
  bool operator==(const ObTableSchemaHashWrapper &rv) const;
O
oceanbase-admin 已提交
3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809

  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 已提交
3810
  inline const common::ObString &get_table_name() const
O
oceanbase-admin 已提交
3811 3812 3813 3814
  {
    return table_name_;
  }

G
gm 已提交
3815
private:
O
oceanbase-admin 已提交
3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833
  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 已提交
3834
inline bool ObTableSchemaHashWrapper::operator==(const ObTableSchemaHashWrapper &rv) const
O
oceanbase-admin 已提交
3835 3836 3837 3838 3839 3840 3841 3842 3843
{
  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 已提交
3844
public:
O
oceanbase-admin 已提交
3845 3846 3847
  ObDatabaseSchemaHashWrapper() : tenant_id_(common::OB_INVALID_ID), name_case_mode_(common::OB_NAME_CASE_INVALID)
  {}
  ObDatabaseSchemaHashWrapper(
X
xy0 已提交
3848
      const uint64_t tenant_id, const common::ObNameCaseMode mode, const common::ObString &database_name)
O
oceanbase-admin 已提交
3849 3850 3851 3852 3853
      : tenant_id_(tenant_id), name_case_mode_(mode), database_name_(database_name)
  {}
  ~ObDatabaseSchemaHashWrapper()
  {}
  inline uint64_t hash() const;
X
xy0 已提交
3854
  inline bool operator==(const ObDatabaseSchemaHashWrapper &rv) const;
O
oceanbase-admin 已提交
3855 3856 3857 3858 3859 3860 3861 3862 3863

  inline uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
  inline common::ObNameCaseMode get_name_case_mode() const
  {
    return name_case_mode_;
  }
X
xy0 已提交
3864
  inline const common::ObString &get_database_name() const
O
oceanbase-admin 已提交
3865 3866 3867 3868
  {
    return database_name_;
  }

G
gm 已提交
3869
private:
O
oceanbase-admin 已提交
3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883
  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 已提交
3884
inline bool ObDatabaseSchemaHashWrapper::operator==(const ObDatabaseSchemaHashWrapper &rv) const
O
oceanbase-admin 已提交
3885 3886 3887 3888 3889 3890 3891
{
  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 已提交
3892
public:
O
oceanbase-admin 已提交
3893 3894
  ObTablegroupSchemaHashWrapper() : tenant_id_(common::OB_INVALID_ID)
  {}
X
xy0 已提交
3895
  ObTablegroupSchemaHashWrapper(uint64_t tenant_id, const common::ObString &tablegroup_name)
O
oceanbase-admin 已提交
3896 3897 3898 3899 3900
      : tenant_id_(tenant_id), tablegroup_name_(tablegroup_name)
  {}
  ~ObTablegroupSchemaHashWrapper()
  {}
  inline uint64_t hash() const;
X
xy0 已提交
3901
  inline bool operator==(const ObTablegroupSchemaHashWrapper &rv) const;
O
oceanbase-admin 已提交
3902 3903 3904 3905 3906

  inline uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
X
xy0 已提交
3907
  inline const common::ObString &get_tablegroup_name() const
O
oceanbase-admin 已提交
3908 3909 3910 3911
  {
    return tablegroup_name_;
  }

G
gm 已提交
3912
private:
O
oceanbase-admin 已提交
3913 3914 3915 3916 3917
  uint64_t tenant_id_;
  common::ObString tablegroup_name_;
};

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

G
gm 已提交
3945
private:
O
oceanbase-admin 已提交
3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960
  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 已提交
3961
inline bool ObForeignKeyInfoHashWrapper::operator==(const ObForeignKeyInfoHashWrapper &rv) const
O
oceanbase-admin 已提交
3962 3963 3964 3965 3966 3967 3968 3969 3970
{
  // 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 已提交
3971
public:
O
oceanbase-admin 已提交
3972 3973 3974 3975 3976 3977
  ObConstraintInfoHashWrapper()
  {
    tenant_id_ = common::OB_INVALID_ID;
    database_id_ = common::OB_INVALID_ID;
    constraint_name_.assign_ptr("", 0);
  }
X
xy0 已提交
3978
  ObConstraintInfoHashWrapper(uint64_t tenant_id, const uint64_t database_id, const common::ObString &constraint_name)
O
oceanbase-admin 已提交
3979 3980 3981 3982 3983
      : tenant_id_(tenant_id), database_id_(database_id), constraint_name_(constraint_name)
  {}
  ~ObConstraintInfoHashWrapper()
  {}
  inline uint64_t hash() const;
X
xy0 已提交
3984
  inline bool operator==(const ObConstraintInfoHashWrapper &rv) const;
O
oceanbase-admin 已提交
3985 3986 3987 3988 3989 3990 3991 3992
  inline uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
  inline uint64_t get_database_id() const
  {
    return database_id_;
  }
X
xy0 已提交
3993
  inline const common::ObString &get_constraint_name() const
O
oceanbase-admin 已提交
3994 3995 3996 3997
  {
    return constraint_name_;
  }

G
gm 已提交
3998
private:
O
oceanbase-admin 已提交
3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013
  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 已提交
4014
inline bool ObConstraintInfoHashWrapper::operator==(const ObConstraintInfoHashWrapper &rv) const
O
oceanbase-admin 已提交
4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030
{
  // 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 已提交
4031
inline bool ObTablegroupSchemaHashWrapper::operator==(const ObTablegroupSchemaHashWrapper &rv) const
O
oceanbase-admin 已提交
4032 4033 4034 4035 4036 4037 4038
{
  return (tenant_id_ == rv.tenant_id_) && (tablegroup_name_ == rv.tablegroup_name_);
}

struct ObTenantPlanBaselineId {
  OB_UNIS_VERSION(1);

G
gm 已提交
4039
public:
O
oceanbase-admin 已提交
4040 4041 4042 4043 4044
  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 已提交
4045
  bool operator==(const ObTenantPlanBaselineId &rhs) const
O
oceanbase-admin 已提交
4046 4047 4048
  {
    return (tenant_id_ == rhs.tenant_id_) && (plan_baseline_id_ == rhs.plan_baseline_id_);
  }
X
xy0 已提交
4049
  bool operator!=(const ObTenantPlanBaselineId &rhs) const
O
oceanbase-admin 已提交
4050 4051 4052
  {
    return !(*this == rhs);
  }
X
xy0 已提交
4053
  bool operator<(const ObTenantPlanBaselineId &rhs) const
O
oceanbase-admin 已提交
4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079
  {
    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 已提交
4080
public:
O
oceanbase-admin 已提交
4081 4082 4083 4084 4085
  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 已提交
4086
  bool operator==(const ObTenantOutlineId &rhs) const
O
oceanbase-admin 已提交
4087 4088 4089
  {
    return (tenant_id_ == rhs.tenant_id_) && (outline_id_ == rhs.outline_id_);
  }
X
xy0 已提交
4090
  bool operator!=(const ObTenantOutlineId &rhs) const
O
oceanbase-admin 已提交
4091 4092 4093
  {
    return !(*this == rhs);
  }
X
xy0 已提交
4094
  bool operator<(const ObTenantOutlineId &rhs) const
O
oceanbase-admin 已提交
4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121
  {
    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 已提交
4122
public:
O
oceanbase-admin 已提交
4123 4124 4125 4126
  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 已提交
4127
  bool operator==(const ObTenantUserId &rhs) const
O
oceanbase-admin 已提交
4128 4129 4130
  {
    return (tenant_id_ == rhs.tenant_id_) && (user_id_ == rhs.user_id_);
  }
X
xy0 已提交
4131
  bool operator!=(const ObTenantUserId &rhs) const
O
oceanbase-admin 已提交
4132 4133 4134
  {
    return !(*this == rhs);
  }
X
xy0 已提交
4135
  bool operator<(const ObTenantUserId &rhs) const
O
oceanbase-admin 已提交
4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162
  {
    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 已提交
4163
public:
O
oceanbase-admin 已提交
4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174
  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 已提交
4175
  bool operator==(const ObTenantUrObjId &rhs) const
O
oceanbase-admin 已提交
4176 4177 4178 4179
  {
    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 已提交
4180
  bool operator!=(const ObTenantUrObjId &rhs) const
O
oceanbase-admin 已提交
4181 4182 4183
  {
    return !(*this == rhs);
  }
X
xy0 已提交
4184
  bool operator<(const ObTenantUrObjId &rhs) const
O
oceanbase-admin 已提交
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 4220 4221 4222 4223 4224 4225
  {
    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 已提交
4226
public:
O
oceanbase-admin 已提交
4227 4228 4229 4230 4231
  explicit ObPrintPrivSet(ObPrivSet priv_set) : priv_set_(priv_set)
  {}

  DECLARE_TO_STRING;

G
gm 已提交
4232
private:
O
oceanbase-admin 已提交
4233 4234 4235 4236
  ObPrivSet priv_set_;
};

class ObPrintPackedPrivArray {
G
gm 已提交
4237
public:
X
xy0 已提交
4238
  explicit ObPrintPackedPrivArray(const ObPackedPrivArray &packed_priv_array) : packed_priv_array_(packed_priv_array)
O
oceanbase-admin 已提交
4239 4240 4241 4242
  {}

  DECLARE_TO_STRING;

G
gm 已提交
4243
private:
X
xy0 已提交
4244
  const ObPackedPrivArray &packed_priv_array_;
O
oceanbase-admin 已提交
4245 4246 4247 4248 4249
};

class ObPriv {
  OB_UNIS_VERSION_V(1);

G
gm 已提交
4250
public:
O
oceanbase-admin 已提交
4251 4252 4253 4254 4255 4256 4257
  ObPriv()
      : tenant_id_(common::OB_INVALID_ID),
        user_id_(common::OB_INVALID_ID),
        schema_version_(1),
        priv_set_(0),
        priv_array_()
  {}
X
xy0 已提交
4258
  ObPriv(common::ObIAllocator *allocator)
O
oceanbase-admin 已提交
4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270
      : 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 已提交
4271 4272
  ObPriv &operator=(const ObPriv &other);
  static bool cmp_tenant_user_id(const ObPriv *lhs, const ObTenantUserId &tenant_user_id)
O
oceanbase-admin 已提交
4273 4274 4275
  {
    return (lhs->get_tenant_user_id() < tenant_user_id);
  }
X
xy0 已提交
4276
  static bool equal_tenant_user_id(const ObPriv *lhs, const ObTenantUserId &tenant_user_id)
O
oceanbase-admin 已提交
4277 4278 4279
  {
    return (lhs->get_tenant_user_id() == tenant_user_id);
  }
X
xy0 已提交
4280
  static bool cmp_tenant_id(const ObPriv *lhs, const uint64_t tenant_id)
O
oceanbase-admin 已提交
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 4311 4312 4313 4314 4315 4316
  {
    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 已提交
4317
  int set_priv_array(const ObPackedPrivArray &other)
O
oceanbase-admin 已提交
4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341
  {
    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 已提交
4342
  inline const ObPackedPrivArray &get_priv_array() const
O
oceanbase-admin 已提交
4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358
  {
    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 已提交
4359
protected:
O
oceanbase-admin 已提交
4360 4361 4362 4363 4364 4365 4366 4367 4368 4369
  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 已提交
4370
public:
O
oceanbase-admin 已提交
4371 4372
  ObUserInfoHashWrapper() : tenant_id_(common::OB_INVALID_ID)
  {}
X
xy0 已提交
4373
  ObUserInfoHashWrapper(uint64_t tenant_id, const common::ObString &user_name)
O
oceanbase-admin 已提交
4374 4375 4376 4377 4378
      : tenant_id_(tenant_id), user_name_(user_name)
  {}
  ~ObUserInfoHashWrapper()
  {}
  inline uint64_t hash() const;
X
xy0 已提交
4379
  inline bool operator==(const ObUserInfoHashWrapper &rv) const;
O
oceanbase-admin 已提交
4380 4381 4382 4383 4384

  inline uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
X
xy0 已提交
4385
  inline const common::ObString &get_user_name() const
O
oceanbase-admin 已提交
4386 4387 4388 4389
  {
    return user_name_;
  }

G
gm 已提交
4390
private:
O
oceanbase-admin 已提交
4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402
  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 已提交
4403
inline bool ObUserInfoHashWrapper::operator==(const ObUserInfoHashWrapper &other) const
O
oceanbase-admin 已提交
4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417
{
  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 已提交
4418
ObSSLType get_ssl_type_from_string(const common::ObString &ssl_type_str);
O
oceanbase-admin 已提交
4419 4420 4421 4422 4423 4424 4425 4426

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

X
xy0 已提交
4427
const char *get_ssl_spec_type_str(const ObSSLSpecifiedType ssl_spec_type);
O
oceanbase-admin 已提交
4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440

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 已提交
4441
public:
O
oceanbase-admin 已提交
4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458
  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),
4459 4460 4461
        role_id_option_array_(),
        max_connections_(0),
        max_user_connections_(0)
O
oceanbase-admin 已提交
4462
  {}
X
xy0 已提交
4463
  explicit ObUserInfo(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
4464
  virtual ~ObUserInfo();
X
xy0 已提交
4465 4466 4467
  ObUserInfo(const ObUserInfo &other);
  ObUserInfo &operator=(const ObUserInfo &other);
  static bool cmp(const ObUserInfo *lhs, const ObUserInfo *rhs)
O
oceanbase-admin 已提交
4468 4469 4470
  {
    return (NULL != lhs && NULL != rhs) ? lhs->get_tenant_user_id() < rhs->get_tenant_user_id() : false;
  }
X
xy0 已提交
4471
  static bool equal(const ObUserInfo *lhs, const ObUserInfo *rhs)
O
oceanbase-admin 已提交
4472 4473 4474 4475 4476
  {
    return (NULL != lhs && NULL != rhs) ? lhs->get_tenant_user_id() == rhs->get_tenant_user_id() : false;
  }

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

G
gm 已提交
4704
private:
O
oceanbase-admin 已提交
4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720
  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
4721 4722
  uint64_t max_connections_;
  uint64_t max_user_connections_;
O
oceanbase-admin 已提交
4723 4724 4725 4726 4727 4728 4729 4730
};

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 已提交
4731
  bool operator==(const ObDBPrivSortKey &rhs) const
O
oceanbase-admin 已提交
4732 4733 4734
  {
    return (tenant_id_ == rhs.tenant_id_) && (user_id_ == rhs.user_id_) && (sort_ == rhs.sort_);
  }
X
xy0 已提交
4735
  bool operator!=(const ObDBPrivSortKey &rhs) const
O
oceanbase-admin 已提交
4736 4737 4738
  {
    return !(*this == rhs);
  }
X
xy0 已提交
4739
  bool operator<(const ObDBPrivSortKey &rhs) const
O
oceanbase-admin 已提交
4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760
  {
    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 已提交
4761
  ObOriginalDBKey(const uint64_t tenant_id, const uint64_t user_id, const common::ObString &db)
O
oceanbase-admin 已提交
4762 4763
      : tenant_id_(tenant_id), user_id_(user_id), db_(db)
  {}
X
xy0 已提交
4764
  bool operator==(const ObOriginalDBKey &rhs) const
O
oceanbase-admin 已提交
4765 4766 4767
  {
    return (tenant_id_ == rhs.tenant_id_) && (user_id_ == rhs.user_id_) && (db_ == rhs.db_);
  }
X
xy0 已提交
4768
  bool operator!=(const ObOriginalDBKey &rhs) const
O
oceanbase-admin 已提交
4769 4770 4771
  {
    return !(*this == rhs);
  }
X
xy0 已提交
4772
  bool operator<(const ObOriginalDBKey &rhs) const
O
oceanbase-admin 已提交
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 4798 4799 4800 4801 4802 4803
  {
    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 已提交
4804
  bool operator==(const ObSysPrivKey &rhs) const
O
oceanbase-admin 已提交
4805 4806 4807
  {
    return ((tenant_id_ == rhs.tenant_id_) && (grantee_id_ == rhs.grantee_id_));
  }
X
xy0 已提交
4808
  bool operator!=(const ObSysPrivKey &rhs) const
O
oceanbase-admin 已提交
4809 4810 4811
  {
    return !(*this == rhs);
  }
X
xy0 已提交
4812
  bool operator<(const ObSysPrivKey &rhs) const
O
oceanbase-admin 已提交
4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839
  {
    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 已提交
4840
public:
O
oceanbase-admin 已提交
4841 4842
  ObDBPriv() : ObPriv(), db_(), sort_(0)
  {}
X
xy0 已提交
4843
  explicit ObDBPriv(common::ObIAllocator *allocator) : ObSchema(allocator), ObPriv(allocator), db_(), sort_(0)
O
oceanbase-admin 已提交
4844 4845 4846
  {}
  virtual ~ObDBPriv()
  {}
X
xy0 已提交
4847
  ObDBPriv(const ObDBPriv &other) : ObSchema(), ObPriv()
O
oceanbase-admin 已提交
4848 4849 4850
  {
    *this = other;
  }
X
xy0 已提交
4851
  ObDBPriv &operator=(const ObDBPriv &other);
O
oceanbase-admin 已提交
4852

X
xy0 已提交
4853
  static bool cmp(const ObDBPriv *lhs, const ObDBPriv *rhs)
O
oceanbase-admin 已提交
4854 4855 4856
  {
    return (NULL != lhs && NULL != rhs) ? (lhs->get_sort_key() < rhs->get_sort_key()) : false;
  }
X
xy0 已提交
4857
  static bool cmp_sort_key(const ObDBPriv *lhs, const ObDBPrivSortKey &sort_key)
O
oceanbase-admin 已提交
4858 4859 4860 4861 4862 4863 4864
  {
    return NULL != lhs ? lhs->get_sort_key() < sort_key : false;
  }
  ObDBPrivSortKey get_sort_key() const
  {
    return ObDBPrivSortKey(tenant_id_, user_id_, sort_);
  }
X
xy0 已提交
4865
  static bool equal(const ObDBPriv *lhs, const ObDBPriv *rhs)
O
oceanbase-admin 已提交
4866 4867 4868 4869 4870 4871 4872 4873 4874
  {
    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 已提交
4875
  inline int set_database_name(const char *db)
O
oceanbase-admin 已提交
4876 4877 4878
  {
    return deep_copy_str(db, db_);
  }
X
xy0 已提交
4879
  inline int set_database_name(const common::ObString &db)
O
oceanbase-admin 已提交
4880 4881 4882 4883 4884 4885 4886 4887 4888
  {
    return deep_copy_str(db, db_);
  }
  inline void set_sort(const uint64_t sort)
  {
    sort_ = sort;
  }

  // get methods
X
xy0 已提交
4889
  inline const char *get_database_name() const
O
oceanbase-admin 已提交
4890 4891 4892
  {
    return extract_str(db_);
  }
X
xy0 已提交
4893
  inline const common::ObString &get_database_name_str() const
O
oceanbase-admin 已提交
4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906
  {
    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 已提交
4907
private:
O
oceanbase-admin 已提交
4908 4909 4910 4911 4912 4913 4914 4915
  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 已提交
4916
  ObTablePrivDBKey(const uint64_t tenant_id, const uint64_t user_id, const common::ObString &db)
O
oceanbase-admin 已提交
4917 4918
      : tenant_id_(tenant_id), user_id_(user_id), db_(db)
  {}
X
xy0 已提交
4919
  bool operator==(const ObTablePrivDBKey &rhs) const
O
oceanbase-admin 已提交
4920 4921 4922
  {
    return (tenant_id_ == rhs.tenant_id_) && (user_id_ == rhs.user_id_) && (db_ == rhs.db_);
  }
X
xy0 已提交
4923
  bool operator!=(const ObTablePrivDBKey &rhs) const
O
oceanbase-admin 已提交
4924 4925 4926
  {
    return !(*this == rhs);
  }
X
xy0 已提交
4927
  bool operator<(const ObTablePrivDBKey &rhs) const
O
oceanbase-admin 已提交
4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946
  {
    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 已提交
4947
      const uint64_t tenant_id, const uint64_t user_id, const common::ObString &db, const common::ObString &table)
O
oceanbase-admin 已提交
4948 4949
      : tenant_id_(tenant_id), user_id_(user_id), db_(db), table_(table)
  {}
X
xy0 已提交
4950
  bool operator==(const ObTablePrivSortKey &rhs) const
O
oceanbase-admin 已提交
4951 4952 4953
  {
    return (tenant_id_ == rhs.tenant_id_) && (user_id_ == rhs.user_id_) && (db_ == rhs.db_) && (table_ == rhs.table_);
  }
X
xy0 已提交
4954
  bool operator!=(const ObTablePrivSortKey &rhs) const
O
oceanbase-admin 已提交
4955 4956 4957
  {
    return !(*this == rhs);
  }
X
xy0 已提交
4958
  bool operator<(const ObTablePrivSortKey &rhs) const
O
oceanbase-admin 已提交
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 5005 5006 5007 5008 5009 5010
  {
    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 已提交
5011
  bool operator==(const ObObjPrivSortKey &rhs) const
O
oceanbase-admin 已提交
5012 5013 5014 5015
  {
    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 已提交
5016
  bool operator!=(const ObObjPrivSortKey &rhs) const
O
oceanbase-admin 已提交
5017 5018 5019
  {
    return !(*this == rhs);
  }
X
xy0 已提交
5020
  bool operator<(const ObObjPrivSortKey &rhs) const
O
oceanbase-admin 已提交
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 5065 5066 5067 5068 5069 5070
  {
    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 已提交
5071
public:
O
oceanbase-admin 已提交
5072 5073 5074
  // constructor and destructor
  ObTablePriv() : ObSchema(), ObPriv()
  {}
X
xy0 已提交
5075
  explicit ObTablePriv(common::ObIAllocator *allocator) : ObSchema(allocator), ObPriv(allocator)
O
oceanbase-admin 已提交
5076
  {}
X
xy0 已提交
5077
  ObTablePriv(const ObTablePriv &other) : ObSchema(), ObPriv()
O
oceanbase-admin 已提交
5078 5079 5080 5081 5082 5083 5084
  {
    *this = other;
  }
  virtual ~ObTablePriv()
  {}

  // operator=
X
xy0 已提交
5085
  ObTablePriv &operator=(const ObTablePriv &other);
O
oceanbase-admin 已提交
5086 5087 5088 5089 5090 5091

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

  ObTablePrivDBKey get_db_key() const
  {
    return ObTablePrivDBKey(tenant_id_, user_id_, db_);
  }
X
xy0 已提交
5113
  static bool cmp_db_key(const ObTablePriv *lhs, const ObTablePrivDBKey &db_key)
O
oceanbase-admin 已提交
5114 5115 5116 5117 5118
  {
    return lhs->get_db_key() < db_key;
  }

  // set methods
X
xy0 已提交
5119
  inline int set_database_name(const char *db)
O
oceanbase-admin 已提交
5120 5121 5122
  {
    return deep_copy_str(db, db_);
  }
X
xy0 已提交
5123
  inline int set_database_name(const common::ObString &db)
O
oceanbase-admin 已提交
5124 5125 5126
  {
    return deep_copy_str(db, db_);
  }
X
xy0 已提交
5127
  inline int set_table_name(const char *table)
O
oceanbase-admin 已提交
5128 5129 5130
  {
    return deep_copy_str(table, table_);
  }
X
xy0 已提交
5131
  inline int set_table_name(const common::ObString &table)
O
oceanbase-admin 已提交
5132 5133 5134 5135 5136
  {
    return deep_copy_str(table, table_);
  }

  // get methods
X
xy0 已提交
5137
  inline const char *get_database_name() const
O
oceanbase-admin 已提交
5138 5139 5140
  {
    return extract_str(db_);
  }
X
xy0 已提交
5141
  inline const common::ObString &get_database_name_str() const
O
oceanbase-admin 已提交
5142 5143 5144
  {
    return db_;
  }
X
xy0 已提交
5145
  inline const char *get_table_name() const
O
oceanbase-admin 已提交
5146 5147 5148
  {
    return extract_str(table_);
  }
X
xy0 已提交
5149
  inline const common::ObString &get_table_name_str() const
O
oceanbase-admin 已提交
5150 5151 5152 5153 5154 5155 5156 5157 5158 5159
  {
    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 已提交
5160
private:
O
oceanbase-admin 已提交
5161 5162 5163 5164 5165 5166 5167
  common::ObString db_;
  common::ObString table_;
};

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

G
gm 已提交
5168
public:
O
oceanbase-admin 已提交
5169 5170 5171
  // constructor and destructor
  ObObjPriv() : ObSchema(), ObPriv()
  {}
X
xy0 已提交
5172
  explicit ObObjPriv(common::ObIAllocator *allocator) : ObSchema(allocator), ObPriv()
O
oceanbase-admin 已提交
5173
  {}
X
xy0 已提交
5174
  ObObjPriv(const ObObjPriv &other) : ObSchema(), ObPriv()
O
oceanbase-admin 已提交
5175 5176 5177 5178 5179 5180 5181
  {
    *this = other;
  }
  virtual ~ObObjPriv()
  {}

  // operator=
X
xy0 已提交
5182
  ObObjPriv &operator=(const ObObjPriv &other);
O
oceanbase-admin 已提交
5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194

  // 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 已提交
5195
  static bool cmp_tenant_ur_obj_id(const ObObjPriv *lhs, const ObTenantUrObjId &rhs)
O
oceanbase-admin 已提交
5196 5197 5198
  {
    return (lhs->get_tenant_ur_obj_id() < rhs);
  }
X
xy0 已提交
5199
  static bool cmp(const ObObjPriv *lhs, const ObObjPriv *rhs)
O
oceanbase-admin 已提交
5200 5201 5202
  {
    return (NULL != lhs && NULL != rhs) ? lhs->get_sort_key() < rhs->get_sort_key() : false;
  }
X
xy0 已提交
5203
  static bool cmp_sort_key(const ObObjPriv *lhs, const ObObjPrivSortKey &sort_key)
O
oceanbase-admin 已提交
5204 5205 5206
  {
    return NULL != lhs ? lhs->get_sort_key() < sort_key : false;
  }
X
xy0 已提交
5207
  static bool equal(const ObObjPriv *lhs, const ObObjPriv *rhs)
O
oceanbase-admin 已提交
5208 5209 5210
  {
    return (NULL != lhs && NULL != rhs) ? lhs->get_sort_key() == rhs->get_sort_key() : false;
  }
X
xy0 已提交
5211
  static bool equal_sort_key(const ObObjPriv *lhs, const ObObjPrivSortKey &sort_key)
O
oceanbase-admin 已提交
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 5265 5266 5267 5268 5269 5270
  {
    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 已提交
5271
private:
O
oceanbase-admin 已提交
5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289
  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 已提交
5290
const char *ob_priv_level_str(const ObPrivLevel grant_level);
O
oceanbase-admin 已提交
5291 5292

struct ObNeedPriv {
X
xy0 已提交
5293
  ObNeedPriv(const common::ObString &db, const common::ObString &table, ObPrivLevel priv_level, ObPrivSet priv_set,
O
oceanbase-admin 已提交
5294 5295 5296 5297 5298
      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 已提交
5299
  int deep_copy(const ObNeedPriv &other, common::ObIAllocator &allocator);
O
oceanbase-admin 已提交
5300 5301 5302 5303 5304 5305 5306 5307 5308 5309
  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 已提交
5310
  explicit ObStmtNeedPrivs(common::ObIAllocator &alloc) : need_privs_(alloc)
O
oceanbase-admin 已提交
5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321
  {}
  ObStmtNeedPrivs() : need_privs_()
  {}
  ~ObStmtNeedPrivs()
  {
    reset();
  }
  void reset()
  {
    need_privs_.reset();
  }
X
xy0 已提交
5322
  int deep_copy(const ObStmtNeedPrivs &other, common::ObIAllocator &allocator);
O
oceanbase-admin 已提交
5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344
  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 已提交
5345
  ObOraNeedPriv(const common::ObString &db_name, uint64_t grantee_id, uint64_t obj_id, uint64_t col_id,
O
oceanbase-admin 已提交
5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357
      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 已提交
5358 5359
  int deep_copy(const ObOraNeedPriv &other, common::ObIAllocator &allocator);
  bool same_obj(const ObOraNeedPriv &other);
O
oceanbase-admin 已提交
5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376
  // 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 已提交
5377
  explicit ObStmtOraNeedPrivs(common::ObIAllocator &alloc) : need_privs_(alloc)
O
oceanbase-admin 已提交
5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388
  {}
  ObStmtOraNeedPrivs() : need_privs_()
  {}
  ~ObStmtOraNeedPrivs()
  {
    reset();
  }
  void reset()
  {
    need_privs_.reset();
  }
X
xy0 已提交
5389
  int deep_copy(const ObStmtOraNeedPrivs &other, common::ObIAllocator &allocator);
O
oceanbase-admin 已提交
5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406
  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 已提交
5407
      const common::ObString &db, const ObPrivSet user_priv_set, const ObPrivSet db_priv_set)
O
oceanbase-admin 已提交
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 5463 5464 5465 5466 5467 5468
      : 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 已提交
5469 5470
  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 已提交
5471 5472 5473 5474 5475 5476 5477 5478
      : tenant_name_(tenant_name),
        user_name_(user_name),
        client_ip_(client_ip),
        passwd_(passwd),
        db_(db),
        scramble_str_()
  {}

X
xy0 已提交
5479 5480 5481
  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 已提交
5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502
      : 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 已提交
5503
public:
O
oceanbase-admin 已提交
5504 5505
  ObSysPriv() : ObPriv(), grantee_id_(common::OB_INVALID_ID)
  {}
X
xy0 已提交
5506
  explicit ObSysPriv(common::ObIAllocator *allocator) : ObSchema(allocator), ObPriv(allocator)
O
oceanbase-admin 已提交
5507 5508 5509
  {}
  virtual ~ObSysPriv()
  {}
X
xy0 已提交
5510
  ObSysPriv(const ObSysPriv &other) : ObSchema(), ObPriv(), grantee_id_(common::OB_INVALID_ID)
O
oceanbase-admin 已提交
5511 5512 5513
  {
    *this = other;
  }
X
xy0 已提交
5514
  ObSysPriv &operator=(const ObSysPriv &other);
O
oceanbase-admin 已提交
5515

X
xy0 已提交
5516
  static bool cmp_tenant_grantee_id(const ObSysPriv *lhs, const ObTenantUserId &tenant_grantee_id)
O
oceanbase-admin 已提交
5517 5518 5519
  {
    return (lhs->get_tenant_grantee_id() < tenant_grantee_id);
  }
X
xy0 已提交
5520
  static bool equal_tenant_grantee_id(const ObSysPriv *lhs, const ObTenantUserId &tenant_grantee_id)
O
oceanbase-admin 已提交
5521 5522 5523
  {
    return (lhs->get_tenant_grantee_id() == tenant_grantee_id);
  }
X
xy0 已提交
5524
  static bool cmp_tenant_id(const ObPriv *lhs, const uint64_t tenant_id)
O
oceanbase-admin 已提交
5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548
  {
    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 已提交
5549
  static bool cmp(const ObSysPriv *lhs, const ObSysPriv *rhs)
O
oceanbase-admin 已提交
5550 5551 5552
  {
    return (NULL != lhs && NULL != rhs) ? (lhs->get_key() < rhs->get_key()) : false;
  }
X
xy0 已提交
5553
  static bool cmp_key(const ObSysPriv *lhs, const ObSysPrivKey &key)
O
oceanbase-admin 已提交
5554 5555 5556 5557 5558 5559 5560
  {
    return NULL != lhs ? lhs->get_key() < key : false;
  }
  ObSysPrivKey get_key() const
  {
    return ObSysPrivKey(tenant_id_, grantee_id_);
  }
X
xy0 已提交
5561
  static bool equal(const ObSysPriv *lhs, const ObSysPriv *rhs)
O
oceanbase-admin 已提交
5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574
  {
    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 已提交
5575
private:
O
oceanbase-admin 已提交
5576 5577 5578
  uint64_t grantee_id_;
};

X
xy0 已提交
5579
int get_int_value(const common::ObString &str, int64_t &value);
O
oceanbase-admin 已提交
5580 5581

class ObHostnameStuct {
G
gm 已提交
5582
public:
O
oceanbase-admin 已提交
5583 5584 5585 5586 5587 5588
  ObHostnameStuct()
  {}
  ~ObHostnameStuct()
  {}
  static const uint32_t FAKE_PORT = 0;
  static const int MAX_IP_BITS = 128;
X
xy0 已提交
5589 5590 5591 5592 5593 5594
  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 已提交
5595 5596 5597 5598 5599 5600 5601 5602
};

enum ObHintFormat {
  HINT_NORMAL,
  HINT_LOCAL,
};

class ObFixedParam {
G
gm 已提交
5603
public:
O
oceanbase-admin 已提交
5604 5605 5606 5607
  ObFixedParam() : offset_(common::OB_INVALID_INDEX), value_()
  {}
  virtual ~ObFixedParam()
  {}
X
xy0 已提交
5608 5609
  bool has_equal_value(const common::ObObj &other_value) const;
  bool is_equal(const ObFixedParam &other_param) const;
O
oceanbase-admin 已提交
5610 5611 5612 5613 5614 5615 5616 5617
  VIRTUAL_TO_STRING_KV(K(offset_), K(value_));
  int64_t offset_;
  common::ObObj value_;
};

class ObMaxConcurrentParam {
  OB_UNIS_VERSION(1);

G
gm 已提交
5618
public:
O
oceanbase-admin 已提交
5619 5620
  static const int64_t UNLIMITED = -1;
  typedef common::ObArray<ObFixedParam, common::ObWrapperAllocator> FixParamStore;
X
xy0 已提交
5621
  explicit ObMaxConcurrentParam(common::ObIAllocator *allocator, const common::ObMemAttr &attr = common::ObMemAttr());
O
oceanbase-admin 已提交
5622 5623 5624
  virtual ~ObMaxConcurrentParam();
  int destroy();
  int64_t get_convert_size() const;
X
xy0 已提交
5625
  int assign(const ObMaxConcurrentParam &max_concurrent_param);
O
oceanbase-admin 已提交
5626 5627 5628 5629
  int64_t get_concurrent_num() const
  {
    return concurrent_num_;
  }
X
xy0 已提交
5630
  int match_fixed_param(const ParamStore &const_param_store, bool &is_match) const;
O
oceanbase-admin 已提交
5631 5632 5633 5634 5635 5636 5637 5638
  bool is_concurrent_limit_param() const
  {
    return concurrent_num_ != UNLIMITED;
  }
  bool is_outline_content_param() const
  {
    return !outline_content_.empty();
  }
X
xy0 已提交
5639 5640 5641
  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 已提交
5642 5643 5644 5645 5646
  {
    mem_attr_ = attr;
  }
  VIRTUAL_TO_STRING_KV(K_(concurrent_num), K_(outline_content), K_(fixed_param_store));

G
gm 已提交
5647
private:
X
xy0 已提交
5648 5649
  int deep_copy_outline_content(const common::ObString &src);
  int deep_copy_param_value(const common::ObObj &src, common::ObObj &dest);
O
oceanbase-admin 已提交
5650

G
gm 已提交
5651
public:
X
xy0 已提交
5652
  common::ObIAllocator *allocator_;
O
oceanbase-admin 已提交
5653 5654 5655 5656 5657
  int64_t concurrent_num_;
  common::ObString outline_content_;
  common::ObMemAttr mem_attr_;
  FixParamStore fixed_param_store_;

G
gm 已提交
5658
private:
O
oceanbase-admin 已提交
5659 5660 5661 5662 5663
  DISALLOW_COPY_AND_ASSIGN(ObMaxConcurrentParam);
};

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

G
gm 已提交
5667
public:
O
oceanbase-admin 已提交
5668
  ObOutlineParamsWrapper();
X
xy0 已提交
5669
  explicit ObOutlineParamsWrapper(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
5670 5671
  ~ObOutlineParamsWrapper();
  int destroy();
X
xy0 已提交
5672 5673 5674
  int assign(const ObOutlineParamsWrapper &src);
  int set_allocator(common::ObIAllocator *allocator, const common::ObMemAttr &attr = common::ObMemAttr());
  OutlineParamsArray &get_outline_params()
O
oceanbase-admin 已提交
5675 5676 5677
  {
    return outline_params_;
  }
X
xy0 已提交
5678
  const OutlineParamsArray &get_outline_params() const
O
oceanbase-admin 已提交
5679 5680 5681
  {
    return outline_params_;
  }
X
xy0 已提交
5682
  ObMaxConcurrentParam *get_outline_param(int64_t index) const;
O
oceanbase-admin 已提交
5683 5684 5685 5686 5687
  int64_t get_convert_size() const;
  bool is_empty() const
  {
    return 0 == outline_params_.count();
  }
X
xy0 已提交
5688 5689 5690
  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 已提交
5691 5692 5693 5694 5695 5696 5697 5698 5699
  int64_t get_param_count() const
  {
    return outline_params_.count();
  }
  void reset_allocator()
  {
    allocator_ = NULL;
    mem_attr_ = common::ObMemAttr();
  }
X
xy0 已提交
5700
  void set_mem_attr(const common::ObMemAttr &attr)
O
oceanbase-admin 已提交
5701 5702 5703 5704 5705
  {
    mem_attr_ = attr;
  };
  TO_STRING_KV(K_(outline_params));

G
gm 已提交
5706
private:
X
xy0 已提交
5707
  common::ObIAllocator *allocator_;
O
oceanbase-admin 已提交
5708 5709 5710
  OutlineParamsArray outline_params_;
  common::ObMemAttr mem_attr_;

G
gm 已提交
5711
private:
O
oceanbase-admin 已提交
5712 5713 5714 5715 5716 5717
  DISALLOW_COPY_AND_ASSIGN(ObOutlineParamsWrapper);
};

struct BaselineKey {
  OB_UNIS_VERSION(1);

G
gm 已提交
5718
public:
O
oceanbase-admin 已提交
5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733
  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 已提交
5734
  inline bool operator==(const BaselineKey &other_key) const
O
oceanbase-admin 已提交
5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756
  {
    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 已提交
5757
public:
O
oceanbase-admin 已提交
5758
  ObPlanBaselineInfo();
X
xy0 已提交
5759
  explicit ObPlanBaselineInfo(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
5760 5761
  virtual ~ObPlanBaselineInfo();

X
xy0 已提交
5762 5763
  ObPlanBaselineInfo(const ObPlanBaselineInfo &src_schema);
  ObPlanBaselineInfo &operator=(const ObPlanBaselineInfo &src_schema);
O
oceanbase-admin 已提交
5764 5765
  void reset();
  int64_t get_convert_size() const;
X
xy0 已提交
5766
  static bool cmp(const ObPlanBaselineInfo *lhs, const ObPlanBaselineInfo *rhs)
O
oceanbase-admin 已提交
5767 5768 5769
  {
    return (NULL != lhs && NULL != rhs) ? lhs->get_plan_baseline_id() < rhs->get_plan_baseline_id() : false;
  }
X
xy0 已提交
5770
  static bool equal(const ObPlanBaselineInfo *lhs, const ObPlanBaselineInfo *rhs)
O
oceanbase-admin 已提交
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 5853 5854 5855 5856 5857 5858
  {
    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 已提交
5859
  int set_sql_id(const char *sql_id)
O
oceanbase-admin 已提交
5860 5861 5862
  {
    return deep_copy_str(sql_id, sql_id_);
  }
X
xy0 已提交
5863
  int set_sql_id(const common::ObString &sql_id)
O
oceanbase-admin 已提交
5864 5865 5866 5867
  {
    return deep_copy_str(sql_id, sql_id_);
  }

X
xy0 已提交
5868
  inline const char *get_sql_id() const
O
oceanbase-admin 已提交
5869 5870 5871
  {
    return extract_str(sql_id_);
  }
X
xy0 已提交
5872
  inline const common::ObString &get_sql_id_str() const
O
oceanbase-admin 已提交
5873 5874 5875 5876
  {
    return sql_id_;
  }

X
xy0 已提交
5877
  int set_hints_info(const char *hints_info)
O
oceanbase-admin 已提交
5878 5879 5880
  {
    return deep_copy_str(hints_info, hints_info_);
  }
X
xy0 已提交
5881
  int set_hints_info(const common::ObString &hints_info)
O
oceanbase-admin 已提交
5882 5883 5884 5885
  {
    return deep_copy_str(hints_info, hints_info_);
  }

X
xy0 已提交
5886
  inline const char *get_hints_info() const
O
oceanbase-admin 已提交
5887 5888 5889
  {
    return extract_str(hints_info_);
  }
X
xy0 已提交
5890
  inline const common::ObString &get_hints_info_str() const
O
oceanbase-admin 已提交
5891 5892 5893 5894
  {
    return hints_info_;
  }

X
xy0 已提交
5895
  int set_outline_data(const char *outline_data)
O
oceanbase-admin 已提交
5896 5897 5898
  {
    return deep_copy_str(outline_data, outline_data_);
  }
X
xy0 已提交
5899
  int set_outline_data(const common::ObString &outline_data)
O
oceanbase-admin 已提交
5900 5901 5902 5903
  {
    return deep_copy_str(outline_data, outline_data_);
  }

X
xy0 已提交
5904
  inline const char *get_outline_data() const
O
oceanbase-admin 已提交
5905 5906 5907
  {
    return extract_str(outline_data_);
  }
X
xy0 已提交
5908
  inline const common::ObString &get_outline_data_str() const
O
oceanbase-admin 已提交
5909 5910 5911 5912
  {
    return outline_data_;
  }

X
xy0 已提交
5913
  int set_sql_text(const char *constructed_sql)
O
oceanbase-admin 已提交
5914 5915 5916
  {
    return deep_copy_str(constructed_sql, key_.constructed_sql_);
  }
X
xy0 已提交
5917
  int set_sql_text(const common::ObString &constructed_sql)
O
oceanbase-admin 已提交
5918 5919 5920 5921
  {
    return deep_copy_str(constructed_sql, key_.constructed_sql_);
  }

X
xy0 已提交
5922
  inline const char *get_sql_text() const
O
oceanbase-admin 已提交
5923 5924 5925
  {
    return extract_str(key_.constructed_sql_);
  }
X
xy0 已提交
5926
  inline const common::ObString &get_sql_text_str() const
O
oceanbase-admin 已提交
5927 5928 5929 5930
  {
    return key_.constructed_sql_;
  }

X
xy0 已提交
5931
  int set_params_info(const char *params_info)
O
oceanbase-admin 已提交
5932 5933 5934
  {
    return deep_copy_str(params_info, key_.params_info_str_);
  }
X
xy0 已提交
5935
  int set_params_info(const common::ObString &params_info)
O
oceanbase-admin 已提交
5936 5937 5938 5939
  {
    return deep_copy_str(params_info, key_.params_info_str_);
  }

X
xy0 已提交
5940
  inline const char *get_params_info() const
O
oceanbase-admin 已提交
5941 5942 5943
  {
    return extract_str(key_.params_info_str_);
  }
X
xy0 已提交
5944
  inline const common::ObString &get_params_info_str() const
O
oceanbase-admin 已提交
5945 5946 5947 5948 5949 5950 5951
  {
    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 已提交
5952
public:
O
oceanbase-admin 已提交
5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969
  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 已提交
5970
public:
O
oceanbase-admin 已提交
5971
  ObOutlineInfo();
X
xy0 已提交
5972
  explicit ObOutlineInfo(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
5973
  virtual ~ObOutlineInfo();
X
xy0 已提交
5974 5975
  ObOutlineInfo(const ObOutlineInfo &src_schema);
  ObOutlineInfo &operator=(const ObOutlineInfo &src_schema);
O
oceanbase-admin 已提交
5976 5977 5978 5979
  void reset();
  bool is_valid() const;
  bool is_valid_for_replace() const;
  int64_t get_convert_size() const;
X
xy0 已提交
5980
  static bool cmp(const ObOutlineInfo *lhs, const ObOutlineInfo *rhs)
O
oceanbase-admin 已提交
5981 5982 5983
  {
    return (NULL != lhs && NULL != rhs) ? lhs->get_outline_id() < rhs->get_outline_id() : false;
  }
X
xy0 已提交
5984
  static bool equal(const ObOutlineInfo *lhs, const ObOutlineInfo *rhs)
O
oceanbase-admin 已提交
5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004
  {
    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 已提交
6005
  int set_name(const char *name)
O
oceanbase-admin 已提交
6006 6007 6008
  {
    return deep_copy_str(name, name_);
  }
X
xy0 已提交
6009
  int set_name(const common::ObString &name)
O
oceanbase-admin 已提交
6010 6011 6012
  {
    return deep_copy_str(name, name_);
  }
X
xy0 已提交
6013
  int set_signature(const char *sig)
O
oceanbase-admin 已提交
6014 6015 6016
  {
    return deep_copy_str(sig, signature_);
  }
X
xy0 已提交
6017
  int set_signature(const common::ObString &sig)
O
oceanbase-admin 已提交
6018 6019 6020
  {
    return deep_copy_str(sig, signature_);
  }
X
xy0 已提交
6021
  int set_sql_id(const char *sql_id)
O
oceanbase-admin 已提交
6022 6023 6024
  {
    return deep_copy_str(sql_id, sql_id_);
  }
X
xy0 已提交
6025
  int set_sql_id(const common::ObString &sql_id)
O
oceanbase-admin 已提交
6026 6027 6028
  {
    return deep_copy_str(sql_id, sql_id_);
  }
X
xy0 已提交
6029 6030
  int set_outline_params(const common::ObString &outline_params_str);
  int set_outline_content(const char *content)
O
oceanbase-admin 已提交
6031 6032 6033
  {
    return deep_copy_str(content, outline_content_);
  }
X
xy0 已提交
6034
  int set_outline_content(const common::ObString &content)
O
oceanbase-admin 已提交
6035 6036 6037
  {
    return deep_copy_str(content, outline_content_);
  }
X
xy0 已提交
6038
  int set_sql_text(const char *sql)
O
oceanbase-admin 已提交
6039 6040 6041
  {
    return deep_copy_str(sql, sql_text_);
  }
X
xy0 已提交
6042
  int set_sql_text(const common::ObString &sql)
O
oceanbase-admin 已提交
6043 6044 6045
  {
    return deep_copy_str(sql, sql_text_);
  }
X
xy0 已提交
6046
  int set_outline_target(const char *target)
O
oceanbase-admin 已提交
6047 6048 6049
  {
    return deep_copy_str(target, outline_target_);
  }
X
xy0 已提交
6050
  int set_outline_target(const common::ObString &target)
O
oceanbase-admin 已提交
6051 6052 6053
  {
    return deep_copy_str(target, outline_target_);
  }
X
xy0 已提交
6054
  int set_owner(const char *owner)
O
oceanbase-admin 已提交
6055 6056 6057
  {
    return deep_copy_str(owner, owner_);
  }
X
xy0 已提交
6058
  int set_owner(const common::ObString &owner)
O
oceanbase-admin 已提交
6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069
  {
    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 已提交
6070
  int set_version(const char *version)
O
oceanbase-admin 已提交
6071 6072 6073
  {
    return deep_copy_str(version, version_);
  }
X
xy0 已提交
6074
  int set_version(const common::ObString &version)
O
oceanbase-admin 已提交
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 6121 6122 6123 6124 6125 6126
  {
    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 已提交
6127
  inline const char *get_name() const
O
oceanbase-admin 已提交
6128 6129 6130
  {
    return extract_str(name_);
  }
X
xy0 已提交
6131
  inline const common::ObString &get_name_str() const
O
oceanbase-admin 已提交
6132 6133 6134
  {
    return name_;
  }
X
xy0 已提交
6135
  inline const char *get_signature() const
O
oceanbase-admin 已提交
6136 6137 6138
  {
    return extract_str(signature_);
  }
X
xy0 已提交
6139
  inline const char *get_sql_id() const
O
oceanbase-admin 已提交
6140 6141 6142
  {
    return extract_str(sql_id_);
  }
X
xy0 已提交
6143
  inline const common::ObString &get_sql_id_str() const
O
oceanbase-admin 已提交
6144 6145 6146
  {
    return sql_id_;
  }
X
xy0 已提交
6147
  inline const common::ObString &get_signature_str() const
O
oceanbase-admin 已提交
6148 6149 6150
  {
    return signature_;
  }
X
xy0 已提交
6151
  inline const char *get_outline_content() const
O
oceanbase-admin 已提交
6152 6153 6154
  {
    return extract_str(outline_content_);
  }
X
xy0 已提交
6155
  inline const common::ObString &get_outline_content_str() const
O
oceanbase-admin 已提交
6156 6157 6158
  {
    return outline_content_;
  }
X
xy0 已提交
6159
  inline const char *get_sql_text() const
O
oceanbase-admin 已提交
6160 6161 6162
  {
    return extract_str(sql_text_);
  }
X
xy0 已提交
6163
  inline const common::ObString &get_sql_text_str() const
O
oceanbase-admin 已提交
6164 6165 6166
  {
    return sql_text_;
  }
X
xy0 已提交
6167
  inline common::ObString &get_sql_text_str()
O
oceanbase-admin 已提交
6168 6169 6170
  {
    return sql_text_;
  }
X
xy0 已提交
6171
  inline const char *get_outline_target() const
O
oceanbase-admin 已提交
6172 6173 6174
  {
    return extract_str(outline_target_);
  }
X
xy0 已提交
6175
  inline const common::ObString &get_outline_target_str() const
O
oceanbase-admin 已提交
6176 6177 6178
  {
    return outline_target_;
  }
X
xy0 已提交
6179
  inline common::ObString &get_outline_target_str()
O
oceanbase-admin 已提交
6180 6181 6182
  {
    return outline_target_;
  }
X
xy0 已提交
6183
  inline const char *get_owner() const
O
oceanbase-admin 已提交
6184 6185 6186
  {
    return extract_str(owner_);
  }
X
xy0 已提交
6187
  inline const common::ObString &get_owner_str() const
O
oceanbase-admin 已提交
6188 6189 6190
  {
    return owner_;
  }
X
xy0 已提交
6191
  inline const char *get_version() const
O
oceanbase-admin 已提交
6192 6193 6194
  {
    return extract_str(version_);
  }
X
xy0 已提交
6195
  inline const common::ObString &get_version_str() const
O
oceanbase-admin 已提交
6196 6197 6198
  {
    return version_;
  }
X
xy0 已提交
6199
  int get_visible_signature(common::ObString &visiable_signature) const;
O
oceanbase-admin 已提交
6200
  int get_outline_sql(
X
xy0 已提交
6201 6202 6203
      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 已提交
6204 6205 6206
  {
    return outline_params_wrapper_;
  }
X
xy0 已提交
6207
  ObOutlineParamsWrapper &get_outline_params_wrapper()
O
oceanbase-admin 已提交
6208 6209 6210 6211 6212 6213 6214
  {
    return outline_params_wrapper_;
  }
  bool has_outline_params() const
  {
    return outline_params_wrapper_.get_outline_params().count() > 0;
  }
X
xy0 已提交
6215
  int has_concurrent_limit_param(bool &has) const;
O
oceanbase-admin 已提交
6216
  int gen_valid_allocator();
X
xy0 已提交
6217 6218 6219
  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 已提交
6220 6221 6222
  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 已提交
6223
  static bool is_sql_id_valid(const common::ObString &sql_id);
O
oceanbase-admin 已提交
6224

G
gm 已提交
6225
private:
X
xy0 已提交
6226 6227 6228 6229
  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 已提交
6230

G
gm 已提交
6231
protected:
O
oceanbase-admin 已提交
6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252
  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 已提交
6253
public:
O
oceanbase-admin 已提交
6254 6255 6256 6257
  ObDbLinkBaseInfo() : ObSchema()
  {
    reset();
  }
X
xy0 已提交
6258
  explicit ObDbLinkBaseInfo(common::ObIAllocator *allocator) : ObSchema(allocator)
O
oceanbase-admin 已提交
6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280
  {
    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 已提交
6281
  inline int set_dblink_name(const common::ObString &name)
O
oceanbase-admin 已提交
6282 6283 6284
  {
    return deep_copy_str(name, dblink_name_);
  }
X
xy0 已提交
6285
  inline int set_cluster_name(const common::ObString &name)
O
oceanbase-admin 已提交
6286 6287 6288
  {
    return deep_copy_str(name, cluster_name_);
  }
X
xy0 已提交
6289
  inline int set_tenant_name(const common::ObString &name)
O
oceanbase-admin 已提交
6290 6291 6292
  {
    return deep_copy_str(name, tenant_name_);
  }
X
xy0 已提交
6293
  inline int set_user_name(const common::ObString &name)
O
oceanbase-admin 已提交
6294 6295 6296
  {
    return deep_copy_str(name, user_name_);
  }
X
xy0 已提交
6297
  inline int set_password(const common::ObString &password)
O
oceanbase-admin 已提交
6298 6299 6300
  {
    return deep_copy_str(password, password_);
  }
X
xy0 已提交
6301
  inline void set_host_addr(const common::ObAddr &addr)
O
oceanbase-admin 已提交
6302 6303 6304
  {
    host_addr_ = addr;
  }
X
xy0 已提交
6305
  inline int set_host_ip(const common::ObString &host_ip)
O
oceanbase-admin 已提交
6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330
  {
    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 已提交
6331
  inline const common::ObString &get_dblink_name() const
O
oceanbase-admin 已提交
6332 6333 6334
  {
    return dblink_name_;
  }
X
xy0 已提交
6335
  inline const common::ObString &get_cluster_name() const
O
oceanbase-admin 已提交
6336 6337 6338
  {
    return cluster_name_;
  }
X
xy0 已提交
6339
  inline const common::ObString &get_tenant_name() const
O
oceanbase-admin 已提交
6340 6341 6342
  {
    return tenant_name_;
  }
X
xy0 已提交
6343
  inline const common::ObString &get_user_name() const
O
oceanbase-admin 已提交
6344 6345 6346
  {
    return user_name_;
  }
X
xy0 已提交
6347
  inline const common::ObString &get_password() const
O
oceanbase-admin 已提交
6348 6349 6350
  {
    return password_;
  }
X
xy0 已提交
6351
  inline const common::ObAddr &get_host_addr() const
O
oceanbase-admin 已提交
6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368
  {
    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 已提交
6369
protected:
O
oceanbase-admin 已提交
6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382
  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 已提交
6383
public:
O
oceanbase-admin 已提交
6384 6385 6386 6387
  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 已提交
6388
  ObTenantDbLinkId(const ObTenantDbLinkId &other) : tenant_id_(other.tenant_id_), dblink_id_(other.dblink_id_)
O
oceanbase-admin 已提交
6389
  {}
X
xy0 已提交
6390
  ObTenantDbLinkId &operator=(const ObTenantDbLinkId &other)
O
oceanbase-admin 已提交
6391 6392 6393 6394 6395
  {
    tenant_id_ = other.tenant_id_;
    dblink_id_ = other.dblink_id_;
    return *this;
  }
X
xy0 已提交
6396
  bool operator==(const ObTenantDbLinkId &rhs) const
O
oceanbase-admin 已提交
6397 6398 6399
  {
    return (tenant_id_ == rhs.tenant_id_) && (dblink_id_ == rhs.dblink_id_);
  }
X
xy0 已提交
6400
  bool operator!=(const ObTenantDbLinkId &rhs) const
O
oceanbase-admin 已提交
6401 6402 6403
  {
    return !(*this == rhs);
  }
X
xy0 已提交
6404
  bool operator<(const ObTenantDbLinkId &rhs) const
O
oceanbase-admin 已提交
6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428
  {
    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 已提交
6429
private:
O
oceanbase-admin 已提交
6430 6431 6432 6433 6434 6435 6436
  uint64_t tenant_id_;
  uint64_t dblink_id_;
};

class ObDbLinkInfo : public ObDbLinkBaseInfo {
  OB_UNIS_VERSION(1);

G
gm 已提交
6437
public:
O
oceanbase-admin 已提交
6438 6439
  ObDbLinkInfo() : ObDbLinkBaseInfo()
  {}
X
xy0 已提交
6440
  explicit ObDbLinkInfo(common::ObIAllocator *allocator) : ObDbLinkBaseInfo(allocator)
O
oceanbase-admin 已提交
6441 6442 6443 6444 6445 6446
  {}
  virtual ~ObDbLinkInfo()
  {}
};

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

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

G
gm 已提交
6570
private:
O
oceanbase-admin 已提交
6571
  // void *alloc(int64_t size);
G
gm 已提交
6572
private:
O
oceanbase-admin 已提交
6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586
  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 已提交
6587
public:
O
oceanbase-admin 已提交
6588 6589
  ObTenantUDFId() : tenant_id_(common::OB_INVALID_ID), udf_name_()
  {}
X
xy0 已提交
6590
  ObTenantUDFId(const uint64_t tenant_id, const common::ObString &name) : tenant_id_(tenant_id), udf_name_(name)
O
oceanbase-admin 已提交
6591
  {}
X
xy0 已提交
6592
  bool operator==(const ObTenantUDFId &rhs) const
O
oceanbase-admin 已提交
6593 6594 6595
  {
    return (tenant_id_ == rhs.tenant_id_) && (udf_name_ == rhs.udf_name_);
  }
X
xy0 已提交
6596
  bool operator!=(const ObTenantUDFId &rhs) const
O
oceanbase-admin 已提交
6597 6598 6599
  {
    return !(*this == rhs);
  }
X
xy0 已提交
6600
  bool operator<(const ObTenantUDFId &rhs) const
O
oceanbase-admin 已提交
6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626
  {
    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 已提交
6627
public:
O
oceanbase-admin 已提交
6628 6629 6630 6631 6632
  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 已提交
6633
  bool operator==(const ObTenantSynonymId &rhs) const
O
oceanbase-admin 已提交
6634 6635 6636
  {
    return (tenant_id_ == rhs.tenant_id_) && (synonym_id_ == rhs.synonym_id_);
  }
X
xy0 已提交
6637
  bool operator!=(const ObTenantSynonymId &rhs) const
O
oceanbase-admin 已提交
6638 6639 6640
  {
    return !(*this == rhs);
  }
X
xy0 已提交
6641
  bool operator<(const ObTenantSynonymId &rhs) const
O
oceanbase-admin 已提交
6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667
  {
    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 已提交
6668
public:
O
oceanbase-admin 已提交
6669 6670 6671 6672 6673
  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 已提交
6674
  bool operator==(const ObTenantSequenceId &rhs) const
O
oceanbase-admin 已提交
6675 6676 6677
  {
    return (tenant_id_ == rhs.tenant_id_) && (sequence_id_ == rhs.sequence_id_);
  }
X
xy0 已提交
6678
  bool operator!=(const ObTenantSequenceId &rhs) const
O
oceanbase-admin 已提交
6679 6680 6681
  {
    return !(*this == rhs);
  }
X
xy0 已提交
6682
  bool operator<(const ObTenantSequenceId &rhs) const
O
oceanbase-admin 已提交
6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708
  {
    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 已提交
6709
public:
O
oceanbase-admin 已提交
6710 6711 6712 6713 6714 6715 6716 6717 6718
  ObAlterOutlineInfo() : ObOutlineInfo(), alter_option_bitset_()
  {}
  virtual ~ObAlterOutlineInfo()
  {}
  void reset()
  {
    ObOutlineInfo::reset();
    alter_option_bitset_.reset();
  }
X
xy0 已提交
6719
  const common::ObBitSet<> &get_alter_option_bitset() const
O
oceanbase-admin 已提交
6720 6721 6722
  {
    return alter_option_bitset_;
  }
X
xy0 已提交
6723
  common::ObBitSet<> &get_alter_option_bitset()
O
oceanbase-admin 已提交
6724 6725 6726 6727 6728
  {
    return alter_option_bitset_;
  }
  INHERIT_TO_STRING_KV("ObOutlineInfo", ObOutlineInfo, K_(alter_option_bitset));

G
gm 已提交
6729
private:
O
oceanbase-admin 已提交
6730 6731 6732 6733
  common::ObBitSet<> alter_option_bitset_;
};

class ObOutlineNameHashWrapper {
G
gm 已提交
6734
public:
O
oceanbase-admin 已提交
6735 6736
  ObOutlineNameHashWrapper() : tenant_id_(common::OB_INVALID_ID), database_id_(common::OB_INVALID_ID), name_()
  {}
X
xy0 已提交
6737
  ObOutlineNameHashWrapper(const uint64_t tenant_id, const uint64_t database_id, const common::ObString &name_)
O
oceanbase-admin 已提交
6738 6739 6740 6741 6742
      : tenant_id_(tenant_id), database_id_(database_id), name_(name_)
  {}
  ~ObOutlineNameHashWrapper()
  {}
  inline uint64_t hash() const;
X
xy0 已提交
6743
  inline bool operator==(const ObOutlineNameHashWrapper &rv) const;
O
oceanbase-admin 已提交
6744 6745 6746 6747 6748 6749 6750 6751
  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 已提交
6752
  inline void set_name(const common::ObString &name)
O
oceanbase-admin 已提交
6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764
  {
    name_ = name;
  }

  inline uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
  inline uint64_t get_database_id() const
  {
    return database_id_;
  }
X
xy0 已提交
6765
  inline const common::ObString &get_name() const
O
oceanbase-admin 已提交
6766 6767 6768 6769
  {
    return name_;
  }

G
gm 已提交
6770
private:
O
oceanbase-admin 已提交
6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784
  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 已提交
6785
inline bool ObOutlineNameHashWrapper::operator==(const ObOutlineNameHashWrapper &rv) const
O
oceanbase-admin 已提交
6786 6787 6788 6789 6790
{
  return (tenant_id_ == rv.tenant_id_) && (database_id_ == rv.database_id_) && (name_ == rv.name_);
}

class ObOutlineSignatureHashWrapper {
G
gm 已提交
6791
public:
O
oceanbase-admin 已提交
6792 6793
  ObOutlineSignatureHashWrapper() : tenant_id_(common::OB_INVALID_ID), database_id_(common::OB_INVALID_ID), signature_()
  {}
X
xy0 已提交
6794
  ObOutlineSignatureHashWrapper(const uint64_t tenant_id, const uint64_t database_id, const common::ObString &signature)
O
oceanbase-admin 已提交
6795 6796 6797 6798 6799
      : tenant_id_(tenant_id), database_id_(database_id), signature_(signature)
  {}
  ~ObOutlineSignatureHashWrapper()
  {}
  inline uint64_t hash() const;
X
xy0 已提交
6800
  inline bool operator==(const ObOutlineSignatureHashWrapper &rv) const;
O
oceanbase-admin 已提交
6801 6802 6803 6804 6805 6806 6807 6808
  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 已提交
6809
  inline void set_signature(const common::ObString &signature)
O
oceanbase-admin 已提交
6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821
  {
    signature_ = signature;
  }

  inline uint64_t get_tenant_id() const
  {
    return tenant_id_;
  }
  inline uint64_t get_database_id() const
  {
    return database_id_;
  }
X
xy0 已提交
6822
  inline const common::ObString &get_signature() const
O
oceanbase-admin 已提交
6823 6824 6825 6826
  {
    return signature_;
  }

G
gm 已提交
6827
private:
O
oceanbase-admin 已提交
6828 6829 6830 6831 6832 6833
  uint64_t tenant_id_;
  uint64_t database_id_;
  common::ObString signature_;
};

class ObOutlineSqlIdHashWrapper {
G
gm 已提交
6834
public:
O
oceanbase-admin 已提交
6835 6836
  ObOutlineSqlIdHashWrapper() : tenant_id_(common::OB_INVALID_ID), database_id_(common::OB_INVALID_ID), sql_id_()
  {}
X
xy0 已提交
6837
  ObOutlineSqlIdHashWrapper(const uint64_t tenant_id, const uint64_t database_id, const common::ObString &sql_id)
O
oceanbase-admin 已提交
6838 6839 6840 6841 6842
      : tenant_id_(tenant_id), database_id_(database_id), sql_id_(sql_id)
  {}
  ~ObOutlineSqlIdHashWrapper()
  {}
  inline uint64_t hash() const;
X
xy0 已提交
6843
  inline bool operator==(const ObOutlineSqlIdHashWrapper &rv) const;
O
oceanbase-admin 已提交
6844 6845 6846 6847 6848 6849 6850 6851
  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 已提交
6852
  inline void set_sql_id(const common::ObString &sql_id)
O
oceanbase-admin 已提交
6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864
  {
    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 已提交
6865
  inline const common::ObString &get_sql_id() const
O
oceanbase-admin 已提交
6866 6867 6868 6869
  {
    return sql_id_;
  }

G
gm 已提交
6870
private:
O
oceanbase-admin 已提交
6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884
  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 已提交
6885
inline bool ObOutlineSqlIdHashWrapper::operator==(const ObOutlineSqlIdHashWrapper &rv) const
O
oceanbase-admin 已提交
6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898
{
  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 已提交
6899
inline bool ObOutlineSignatureHashWrapper::operator==(const ObOutlineSignatureHashWrapper &rv) const
O
oceanbase-admin 已提交
6900 6901 6902 6903 6904
{
  return (tenant_id_ == rv.tenant_id_) && (database_id_ == rv.database_id_) && (signature_ == rv.signature_);
}

class ObSysTableChecker {
G
gm 已提交
6905
private:
O
oceanbase-admin 已提交
6906 6907 6908 6909 6910
  ObSysTableChecker();
  ~ObSysTableChecker();

  int init_tenant_space_table_id_map();
  int init_sys_table_name_map();
X
xy0 已提交
6911 6912 6913
  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 已提交
6914

G
gm 已提交
6915
public:
X
xy0 已提交
6916
  static ObSysTableChecker &instance();
O
oceanbase-admin 已提交
6917 6918 6919 6920 6921 6922 6923 6924
  int init();
  int destroy();

  int64_t get_tenant_space_sys_table_num()
  {
    return tenant_space_sys_table_num_;
  }

X
xy0 已提交
6925
  static int is_tenant_space_table_id(const uint64_t table_id, bool &is_tenant_space_table);
O
oceanbase-admin 已提交
6926
  static int is_sys_table_name(
X
xy0 已提交
6927
      const uint64_t database_id, const common::ObString &table_name, bool &is_tenant_space_table);
O
oceanbase-admin 已提交
6928 6929 6930
  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 已提交
6931
  static int is_tenant_table_need_weak_read(const uint64_t table_id, bool &need_weak_read);
O
oceanbase-admin 已提交
6932 6933 6934
  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 已提交
6935
public:
O
oceanbase-admin 已提交
6936
  class TableNameWrapper {
G
gm 已提交
6937
  public:
O
oceanbase-admin 已提交
6938 6939 6940
    TableNameWrapper()
        : database_id_(common::OB_INVALID_ID), name_case_mode_(common::OB_NAME_CASE_INVALID), table_name_()
    {}
X
xy0 已提交
6941
    TableNameWrapper(const uint64_t database_id, const common::ObNameCaseMode mode, const common::ObString &table_name)
O
oceanbase-admin 已提交
6942 6943 6944 6945 6946
        : database_id_(database_id), name_case_mode_(mode), table_name_(table_name)
    {}
    ~TableNameWrapper()
    {}
    inline uint64_t hash() const;
X
xy0 已提交
6947
    bool operator==(const TableNameWrapper &rv) const;
O
oceanbase-admin 已提交
6948 6949
    TO_STRING_KV(K_(database_id), K_(name_case_mode), K_(table_name));

G
gm 已提交
6950
  private:
O
oceanbase-admin 已提交
6951 6952 6953 6954 6955 6956
    uint64_t database_id_;  // pure_database_id
    common::ObNameCaseMode name_case_mode_;
    common::ObString table_name_;
  };
  typedef common::ObSEArray<TableNameWrapper, 2> TableNameWrapperArray;

G
gm 已提交
6957
private:
O
oceanbase-admin 已提交
6958 6959
  static const int64_t TABLE_BUCKET_NUM = 300;

G
gm 已提交
6960
private:
O
oceanbase-admin 已提交
6961
  common::hash::ObHashSet<uint64_t, common::hash::NoPthreadDefendMode> tenant_space_table_id_map_;
X
xy0 已提交
6962
  common::hash::ObHashMap<uint64_t, TableNameWrapperArray *, common::hash::NoPthreadDefendMode> sys_table_name_map_;
O
oceanbase-admin 已提交
6963 6964 6965 6966 6967 6968 6969 6970 6971 6972
  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 已提交
6973
public:
O
oceanbase-admin 已提交
6974 6975 6976 6977 6978 6979 6980 6981 6982 6983
  enum RecycleObjType {
    INVALID = 0,
    TABLE,
    INDEX,
    VIEW,
    DATABASE,
    RESERVED_OBJ_TYPE,
    TRIGGER,
    TENANT,
  };
X
xy0 已提交
6984
  ObRecycleObject(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996
  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 已提交
6997 6998
  ObRecycleObject(const ObRecycleObject &recycle_obj);
  ObRecycleObject &operator=(const ObRecycleObject &recycle_obj);
O
oceanbase-admin 已提交
6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019
  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 已提交
7020
  const common::ObString &get_object_name() const
O
oceanbase-admin 已提交
7021 7022 7023
  {
    return object_name_;
  }
X
xy0 已提交
7024
  const common::ObString &get_original_name() const
O
oceanbase-admin 已提交
7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047
  {
    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 已提交
7048
  int set_object_name(const common::ObString &object_name)
O
oceanbase-admin 已提交
7049 7050 7051
  {
    return deep_copy_str(object_name, object_name_);
  }
X
xy0 已提交
7052
  int set_original_name(const common::ObString &original_name)
O
oceanbase-admin 已提交
7053 7054 7055 7056 7057 7058 7059
  {
    return deep_copy_str(original_name, original_name_);
  }
  void set_type(const RecycleObjType type)
  {
    type_ = type;
  }
X
xy0 已提交
7060 7061
  int set_type_by_table_schema(const ObSimpleTableSchemaV2 &table_schema);
  static RecycleObjType get_type_by_table_schema(const ObSimpleTableSchemaV2 &table_schema);
O
oceanbase-admin 已提交
7062
  // for backup
X
xy0 已提交
7063
  int set_tablegroup_name(const common::ObString &tablegroup_name)
O
oceanbase-admin 已提交
7064 7065 7066
  {
    return deep_copy_str(tablegroup_name, tablegroup_name_);
  }
X
xy0 已提交
7067
  const common::ObString &get_tablegroup_name() const
O
oceanbase-admin 已提交
7068 7069 7070
  {
    return tablegroup_name_;
  }
X
xy0 已提交
7071
  int set_database_name(const common::ObString &database_name)
O
oceanbase-admin 已提交
7072 7073 7074
  {
    return deep_copy_str(database_name, database_name_);
  }
X
xy0 已提交
7075
  const common::ObString &get_database_name() const
O
oceanbase-admin 已提交
7076 7077 7078 7079 7080 7081
  {
    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 已提交
7082
private:
O
oceanbase-admin 已提交
7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107
  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 已提交
7108
public:
O
oceanbase-admin 已提交
7109 7110 7111 7112 7113 7114
  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 已提交
7115
  bool operator==(const ObBasedSchemaObjectInfo &other) const
O
oceanbase-admin 已提交
7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133
  {
    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 已提交
7134
public:
O
oceanbase-admin 已提交
7135 7136 7137 7138 7139 7140
  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 已提交
7141
  bool operator==(const ObAuxTableMetaInfo &other) const
O
oceanbase-admin 已提交
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 7177 7178 7179 7180 7181 7182
  {
    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 已提交
7183
public:
O
oceanbase-admin 已提交
7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203
  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 已提交
7204
  ObForeignKeyInfo(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
7205 7206 7207
  virtual ~ObForeignKeyInfo()
  {}

G
gm 已提交
7208
public:
O
oceanbase-admin 已提交
7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232
  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 已提交
7233
  inline int set_foreign_key_name(const common::ObString &foreign_key_name)
O
oceanbase-admin 已提交
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 7268 7269 7270 7271 7272 7273
  {
    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 已提交
7274
  inline const char *get_update_action_str() const
O
oceanbase-admin 已提交
7275 7276 7277
  {
    return get_action_str(update_action_);
  }
X
xy0 已提交
7278
  inline const char *get_delete_action_str() const
O
oceanbase-admin 已提交
7279 7280 7281
  {
    return get_action_str(delete_action_);
  }
X
xy0 已提交
7282
  inline const char *get_action_str(ObReferenceAction action) const
O
oceanbase-admin 已提交
7283
  {
X
xy0 已提交
7284
    const char *ret = NULL;
O
oceanbase-admin 已提交
7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310
    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 已提交
7311
  int assign(const ObForeignKeyInfo &other);
O
oceanbase-admin 已提交
7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324
  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 已提交
7325
public:
O
oceanbase-admin 已提交
7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344
  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 已提交
7345
private:
X
xy0 已提交
7346
  static const char *reference_action_str_[ACTION_MAX + 1];
O
oceanbase-admin 已提交
7347 7348 7349 7350 7351
};

struct ObSimpleForeignKeyInfo {
  OB_UNIS_VERSION(1);

G
gm 已提交
7352
public:
O
oceanbase-admin 已提交
7353 7354 7355 7356 7357 7358 7359 7360 7361
  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 已提交
7362
      const common::ObString &foreign_key_name, const uint64_t foreign_key_id)
O
oceanbase-admin 已提交
7363 7364 7365 7366 7367 7368
      : 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 已提交
7369
  bool operator==(const ObSimpleForeignKeyInfo &other) const
O
oceanbase-admin 已提交
7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399
  {
    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 已提交
7400
public:
O
oceanbase-admin 已提交
7401 7402 7403 7404 7405 7406 7407 7408 7409
  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 已提交
7410
      const common::ObString &constraint_name, const uint64_t constraint_id)
O
oceanbase-admin 已提交
7411 7412 7413 7414 7415 7416
      : tenant_id_(tenant_id),
        database_id_(database_id),
        table_id_(table_id),
        constraint_name_(constraint_name),
        constraint_id_(constraint_id)
  {}
X
xy0 已提交
7417
  bool operator==(const ObSimpleConstraintInfo &other) const
O
oceanbase-admin 已提交
7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446
  {
    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 已提交
7447
    const PARTITION &l_part, const PARTITION &r_part, const ObPartitionFuncType part_type, bool &is_equal)
O
oceanbase-admin 已提交
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 7488 7489 7490 7491 7492 7493
{
  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 已提交
7494 7495
    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 已提交
7496 7497 7498 7499
    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 已提交
7500
        const common::ObNewRow &l_rowkey = l_list_values.at(i);
O
oceanbase-admin 已提交
7501 7502
        bool find_equal_item = false;
        for (int64_t j = 0; j < r_list_values.count() && !find_equal_item && is_equal; j++) {
X
xy0 已提交
7503
          const common::ObNewRow &r_rowkey = r_list_values.at(j);
O
oceanbase-admin 已提交
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 7543 7544 7545 7546 7547 7548
          // 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 已提交
7549
public:
O
oceanbase-admin 已提交
7550
  ObSequenceSchema();
X
xy0 已提交
7551
  explicit ObSequenceSchema(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
7552
  virtual ~ObSequenceSchema();
X
xy0 已提交
7553 7554 7555
  ObSequenceSchema &operator=(const ObSequenceSchema &src_schema);
  int assign(const ObSequenceSchema &src_schema);
  ObSequenceSchema(const ObSequenceSchema &src_schema);
O
oceanbase-admin 已提交
7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573
  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 已提交
7574
  inline int set_sequence_name(const char *name)
O
oceanbase-admin 已提交
7575 7576 7577
  {
    return deep_copy_str(name, name_);
  }
X
xy0 已提交
7578
  inline int set_sequence_name(const common::ObString &name)
O
oceanbase-admin 已提交
7579 7580 7581
  {
    return deep_copy_str(name, name_);
  }
X
xy0 已提交
7582
  inline int set_name(const common::ObString &name)
O
oceanbase-admin 已提交
7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606
  {
    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 已提交
7607
  int set_max_value(const common::number::ObNumber &num)
O
oceanbase-admin 已提交
7608 7609 7610
  {
    return option_.set_max_value(num);
  }
X
xy0 已提交
7611
  int set_min_value(const common::number::ObNumber &num)
O
oceanbase-admin 已提交
7612 7613 7614
  {
    return option_.set_min_value(num);
  }
X
xy0 已提交
7615
  int set_increment_by(const common::number::ObNumber &num)
O
oceanbase-admin 已提交
7616 7617 7618
  {
    return option_.set_increment_by(num);
  }
X
xy0 已提交
7619
  int set_start_with(const common::number::ObNumber &num)
O
oceanbase-admin 已提交
7620 7621 7622
  {
    return option_.set_start_with(num);
  }
X
xy0 已提交
7623
  int set_cache_size(const common::number::ObNumber &num)
O
oceanbase-admin 已提交
7624 7625 7626 7627
  {
    return option_.set_cache_size(num);
  }

X
xy0 已提交
7628
  inline const common::number::ObNumber &get_min_value() const
O
oceanbase-admin 已提交
7629 7630 7631
  {
    return option_.get_min_value();
  }
X
xy0 已提交
7632
  inline const common::number::ObNumber &get_max_value() const
O
oceanbase-admin 已提交
7633 7634 7635
  {
    return option_.get_max_value();
  }
X
xy0 已提交
7636
  inline const common::number::ObNumber &get_increment_by() const
O
oceanbase-admin 已提交
7637 7638 7639
  {
    return option_.get_increment_by();
  }
X
xy0 已提交
7640
  inline const common::number::ObNumber &get_start_with() const
O
oceanbase-admin 已提交
7641 7642 7643
  {
    return option_.get_start_with();
  }
X
xy0 已提交
7644
  inline const common::number::ObNumber &get_cache_size() const
O
oceanbase-admin 已提交
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 7672 7673 7674 7675 7676 7677
  {
    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 已提交
7678
  inline const common::ObString &get_sequence_name() const
O
oceanbase-admin 已提交
7679 7680 7681
  {
    return name_;
  }
X
xy0 已提交
7682
  inline const char *get_sequence_name_str() const
O
oceanbase-admin 已提交
7683 7684 7685
  {
    return extract_str(name_);
  }
X
xy0 已提交
7686
  inline share::ObSequenceOption &get_sequence_option()
O
oceanbase-admin 已提交
7687 7688 7689
  {
    return option_;
  }
X
xy0 已提交
7690
  inline const share::ObSequenceOption &get_sequence_option() const
O
oceanbase-admin 已提交
7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702
  {
    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 已提交
7703
private:
O
oceanbase-admin 已提交
7704 7705 7706
  // 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 已提交
7707
private:
O
oceanbase-admin 已提交
7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721
  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 已提交
7722
public:
O
oceanbase-admin 已提交
7723 7724 7725 7726 7727
  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 已提交
7728
  bool operator==(const ObTenantCommonSchemaId &rhs) const
O
oceanbase-admin 已提交
7729 7730 7731
  {
    return (tenant_id_ == rhs.tenant_id_) && (schema_id_ == rhs.schema_id_);
  }
X
xy0 已提交
7732
  bool operator!=(const ObTenantCommonSchemaId &rhs) const
O
oceanbase-admin 已提交
7733 7734 7735
  {
    return !(*this == rhs);
  }
X
xy0 已提交
7736
  bool operator<(const ObTenantCommonSchemaId &rhs) const
O
oceanbase-admin 已提交
7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762
  {
    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 已提交
7763
public:
O
oceanbase-admin 已提交
7764 7765 7766 7767 7768 7769 7770 7771 7772
  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 已提交
7773
public:
O
oceanbase-admin 已提交
7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798
  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 已提交
7799
  static const char *PARAM_VALUE_NAMES[MAX_PARAMS];
O
oceanbase-admin 已提交
7800 7801

  ObProfileSchema();
X
xy0 已提交
7802
  explicit ObProfileSchema(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
7803 7804 7805 7806 7807 7808 7809 7810 7811
  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 已提交
7812 7813
  ObProfileSchema &operator=(const ObProfileSchema &src_schema);
  ObProfileSchema(const ObProfileSchema &src_schema);
O
oceanbase-admin 已提交
7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826

  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 已提交
7827
  inline int set_profile_name(const char *name)
O
oceanbase-admin 已提交
7828 7829 7830
  {
    return deep_copy_str(name, profile_name_);
  }
X
xy0 已提交
7831
  inline int set_profile_name(const common::ObString &name)
O
oceanbase-admin 已提交
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 7858 7859 7860 7861 7862 7863
  {
    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 已提交
7864
  inline const char *get_profile_name() const
O
oceanbase-admin 已提交
7865 7866 7867
  {
    return extract_str(profile_name_);
  }
X
xy0 已提交
7868
  inline const common::ObString &get_profile_name_str() const
O
oceanbase-admin 已提交
7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897
  {
    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 已提交
7898
  static int get_default_value(const int64_t type, int64_t &value);
O
oceanbase-admin 已提交
7899

X
xy0 已提交
7900
  inline const char *get_password_verify_function() const
O
oceanbase-admin 已提交
7901 7902 7903
  {
    return extract_str(password_verify_function_);
  }
X
xy0 已提交
7904
  inline const common::ObString &get_password_verify_function_str() const
O
oceanbase-admin 已提交
7905 7906 7907
  {
    return password_verify_function_;
  }
X
xy0 已提交
7908
  inline int set_password_verify_function(const char *name)
O
oceanbase-admin 已提交
7909 7910 7911
  {
    return deep_copy_str(name, password_verify_function_);
  }
X
xy0 已提交
7912
  inline int set_password_verify_function(const common::ObString &name)
O
oceanbase-admin 已提交
7913 7914 7915 7916
  {
    return deep_copy_str(name, password_verify_function_);
  }

G
gm 已提交
7917
private:
O
oceanbase-admin 已提交
7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928
  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 已提交
7929
common::ObIAllocator *&schema_stack_allocator();
O
oceanbase-admin 已提交
7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943

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 已提交
7944
public:
O
oceanbase-admin 已提交
7945 7946 7947 7948 7949 7950 7951 7952 7953 7954
  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 已提交
7955
  void inline gen_mask(uint64_t type, uint64_t &id)
O
oceanbase-admin 已提交
7956 7957 7958 7959 7960 7961 7962 7963 7964 7965
  {
    id = (TYPE_MASK & type) | (VAL_MASK & id);
  }
};

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

#endif /* _OB_OCEANBASE_SCHEMA_SCHEMA_STRUCT_H */