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

#ifndef _OB_OCEANBASE_SCHEMA_SCHEMA_STRUCT_H
#define _OB_OCEANBASE_SCHEMA_SCHEMA_STRUCT_H

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

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

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

X
xy0 已提交
66 67
#define ASSIGN_CONST_STRING(dst, src, field, buffer, skip)                               \
  (const_cast<ObString &>((dst)->field)).assign(buffer + offset, (src)->field.length()); \
O
oceanbase-admin 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 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
  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)
// 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 已提交
140
int get_part_type_str(ObPartitionFuncType type, common::ObString &str, bool can_change = true);
O
oceanbase-admin 已提交
141 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

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

// 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 已提交
196 197
const char *ob_table_type_str(ObTableType type);
const char *ob_mysql_table_type_str(ObTableType type);
O
oceanbase-admin 已提交
198 199 200 201 202 203 204 205 206 207 208

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,
209 210 211 212 213 214
  /* 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 已提交
215 216
  INDEX_TYPE_NORMAL_GLOBAL_LOCAL_STORAGE = 7,
  INDEX_TYPE_UNIQUE_GLOBAL_LOCAL_STORAGE = 8,
217

O
oceanbase-admin 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
  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 已提交
235
const char *ob_view_check_option_str(const ViewCheckOption option);
O
oceanbase-admin 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
enum ObIndexStatus {
  // this is used in index virtual table:__index_process_info:means the table may be deleted when you get it
  INDEX_STATUS_NOT_FOUND = 0,
  INDEX_STATUS_UNAVAILABLE = 1,
  INDEX_STATUS_AVAILABLE = 2,
  INDEX_STATUS_UNIQUE_CHECKING = 3,    // not used anymore
  INDEX_STATUS_UNIQUE_INELIGIBLE = 4,  // not used anymore
  INDEX_STATUS_INDEX_ERROR = 5,
  INDEX_STATUS_RESTORE_INDEX_ERROR = 6,
  INDEX_STATUS_UNUSABLE = 7,
  INDEX_STATUS_MAX = 8,
};

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

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

struct ObRefreshSchemaStatus {
G
gm 已提交
265
public:
O
oceanbase-admin 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
  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 已提交
294
  bool operator==(const ObRefreshSchemaStatus &other) const
O
oceanbase-admin 已提交
295 296 297 298 299 300 301 302 303
  {
    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 已提交
304
public:
O
oceanbase-admin 已提交
305 306 307 308 309 310 311 312 313 314 315
  // 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 已提交
316
public:
O
oceanbase-admin 已提交
317 318 319 320 321 322
  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 已提交
323
  ObRefreshSchemaInfo(const ObRefreshSchemaInfo &other);
O
oceanbase-admin 已提交
324 325
  virtual ~ObRefreshSchemaInfo()
  {}
X
xy0 已提交
326
  int assign(const ObRefreshSchemaInfo &other);
O
oceanbase-admin 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
  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 已提交
363
private:
O
oceanbase-admin 已提交
364 365 366 367 368 369 370
  int64_t schema_version_;
  uint64_t tenant_id_;
  uint64_t sequence_id_;
  int64_t split_schema_version_;
};

class ObDropTenantInfo {
G
gm 已提交
371
public:
O
oceanbase-admin 已提交
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
  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 已提交
395
private:
O
oceanbase-admin 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
  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 已提交
440
const char *ob_index_status_str(ObIndexStatus status);
O
oceanbase-admin 已提交
441 442 443 444 445 446

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 已提交
447
  bool operator==(const ObTenantTableId &rv) const
O
oceanbase-admin 已提交
448 449 450
  {
    return (tenant_id_ == rv.tenant_id_) && (table_id_ == rv.table_id_);
  }
X
xy0 已提交
451
  ObTenantTableId &operator=(const ObTenantTableId &tenant_table_id);
O
oceanbase-admin 已提交
452 453 454 455
  int64_t hash() const
  {
    return table_id_;
  }
X
xy0 已提交
456
  bool operator<(const ObTenantTableId &rv) const
O
oceanbase-admin 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
  {
    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 已提交
486
  bool operator==(const ObTenantDatabaseId &rv) const
O
oceanbase-admin 已提交
487 488 489 490 491 492 493
  {
    return ((tenant_id_ == rv.tenant_id_) && (database_id_ == rv.database_id_));
  }
  int64_t hash() const
  {
    return database_id_;
  }
X
xy0 已提交
494
  bool operator<(const ObTenantDatabaseId &rv) const
O
oceanbase-admin 已提交
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
  {
    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 已提交
524
  bool operator==(const ObTenantTablegroupId &rv) const
O
oceanbase-admin 已提交
525 526 527 528 529 530 531
  {
    return (tenant_id_ == rv.tenant_id_) && (tablegroup_id_ == rv.tablegroup_id_);
  }
  int64_t hash() const
  {
    return tablegroup_id_;
  }
X
xy0 已提交
532
  bool operator<(const ObTenantTablegroupId &rv) const
O
oceanbase-admin 已提交
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
  {
    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 已提交
589
int need_change_schema_id(const ObSchemaType schema_type, const uint64_t schema_id, bool &need_change);
O
oceanbase-admin 已提交
590

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

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

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

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

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

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

G
gm 已提交
649
private:
O
oceanbase-admin 已提交
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
  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 已提交
708
inline const char *print_table_status(const TableStatus status_no)
O
oceanbase-admin 已提交
709
{
X
xy0 已提交
710
  const char *status_str = "UNKNOWN";
O
oceanbase-admin 已提交
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
  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 已提交
736
inline const char *print_part_status(const PartitionStatus status)
O
oceanbase-admin 已提交
737
{
X
xy0 已提交
738
  const char *str = "UNKNOWN";
O
oceanbase-admin 已提交
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
  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 已提交
828
  inline bool operator==(const ObSchemaObjVersion &other) const
O
oceanbase-admin 已提交
829 830 831 832
  {
    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 已提交
833
  inline bool operator!=(const ObSchemaObjVersion &other) const
O
oceanbase-admin 已提交
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
  {
    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 已提交
945 946 947
  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 已提交
948 949
  void reset();
  inline bool is_valid() const;
X
xy0 已提交
950
  int64_t to_string(char *buf, const int64_t buf_len) const;
O
oceanbase-admin 已提交
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972

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

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

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

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

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

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

template <typename DSTSCHEMA>
int ObSchema::set_charset_and_collation_options(
X
xy0 已提交
1133
    common::ObCharsetType src_charset_type, common::ObCollationType src_collation_type, DSTSCHEMA &dst)
O
oceanbase-admin 已提交
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
{
  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 已提交
1166
public:
X
xy0 已提交
1167
  explicit ObLocality(ObSchema *schema) : schema_(schema)
O
oceanbase-admin 已提交
1168
  {}
X
xy0 已提交
1169 1170 1171 1172
  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 已提交
1173
  int set_specific_replica_attr_array(
X
xy0 已提交
1174
      share::SchemaReplicaAttrArray &schema_replica_set, const common::ObIArray<ReplicaAttr> &src);
O
oceanbase-admin 已提交
1175 1176
  void reset_zone_replica_attr_array();
  int64_t get_convert_size() const;
X
xy0 已提交
1177
  inline const common::ObString &get_locality_str() const
O
oceanbase-admin 已提交
1178 1179 1180 1181 1182 1183
  {
    return locality_str_;
  }
  void reset();
  TO_STRING_KV(K_(locality_str), K_(zone_replica_attr_array));

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

class ObPrimaryZone {
  OB_UNIS_VERSION(1);

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

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

class ObSysVarSchema : public ObSchema {
  OB_UNIS_VERSION(1);

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

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

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

class ObTenantSchema : public ObSchema {
  OB_UNIS_VERSION(1);

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

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

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

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

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

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

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

class ObPartitionOption : public ObSchema {
  OB_UNIS_VERSION(1);

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

  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 已提交
2062
  int set_part_expr(const common::ObString &expr)
O
oceanbase-admin 已提交
2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095
  {
    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 已提交
2096
  inline const common::ObString &get_part_func_expr_str() const
O
oceanbase-admin 已提交
2097 2098 2099
  {
    return part_func_expr_;
  }
X
xy0 已提交
2100
  inline const char *get_part_func_expr() const
O
oceanbase-admin 已提交
2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131
  {
    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 已提交
2132
  const common::ObString &get_intervel_start_str() const
O
oceanbase-admin 已提交
2133 2134 2135
  {
    return interval_start_;
  }
X
xy0 已提交
2136
  const common::ObString &get_part_intervel_str() const
O
oceanbase-admin 已提交
2137 2138 2139 2140 2141 2142 2143 2144 2145 2146
  {
    return part_interval_;
  }
  inline int64_t get_auto_part_size() const
  {
    return auto_part_size_;
  }

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

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

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

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

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

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

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

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

G
gm 已提交
2236
public:
O
oceanbase-admin 已提交
2237 2238 2239
  friend class ObPartitionUtils;
  friend class sql::ObPartitionExecutorUtils;
  ObBasePartition();
X
xy0 已提交
2240
  explicit ObBasePartition(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286
  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 已提交
2287
  int set_part_name(common::ObString &part_name)
O
oceanbase-admin 已提交
2288 2289 2290
  {
    return deep_copy_str(part_name, name_);
  }
X
xy0 已提交
2291
  const common::ObString &get_part_name() const
O
oceanbase-admin 已提交
2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304
  {
    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 已提交
2305
  int assign(const ObBasePartition &src_part);
O
oceanbase-admin 已提交
2306 2307

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

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

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

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

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

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

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

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

struct ObSubPartition : public ObBasePartition {
  OB_UNIS_VERSION(1);

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

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

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

X
xy0 已提交
2614
  virtual const char *get_entity_name() const = 0;
O
oceanbase-admin 已提交
2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628
  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 已提交
2629
  virtual int get_primary_zone_inherit(ObSchemaGetterGuard &schema_guard, ObPrimaryZone &primary_zone) const = 0;
O
oceanbase-admin 已提交
2630
  virtual int get_zone_replica_attr_array_inherit(
X
xy0 已提交
2631
      ObSchemaGetterGuard &schema_guard, ZoneLocalityIArray &locality) const = 0;
O
oceanbase-admin 已提交
2632
  virtual int get_zone_list(
X
xy0 已提交
2633
      share::schema::ObSchemaGetterGuard &schema_guard, common::ObIArray<common::ObZone> &zone_list) const = 0;
O
oceanbase-admin 已提交
2634
  virtual int get_locality_str_inherit(
X
xy0 已提交
2635 2636 2637 2638 2639
      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 已提交
2640 2641
  virtual uint64_t get_tablegroup_id() const = 0;
  virtual void set_tablegroup_id(const uint64_t tg_id) = 0;
X
xy0 已提交
2642 2643 2644 2645
  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 已提交
2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699
  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 已提交
2700
  int get_first_individual_sub_part_num(int64_t &sub_part_num) const;
O
oceanbase-admin 已提交
2701 2702
  // SQL view, unable to see the delayed deleted partition
  int64_t get_all_part_num() const;
X
xy0 已提交
2703
  int get_all_partition_num(bool check_dropped_partition, int64_t &part_num) const;
O
oceanbase-admin 已提交
2704

X
xy0 已提交
2705 2706 2707 2708
  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 已提交
2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755

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

X
xy0 已提交
2777 2778 2779 2780 2781
  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 已提交
2782 2783

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

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

X
xy0 已提交
2807
  inline ObPartition **get_part_array() const
O
oceanbase-admin 已提交
2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819
  {
    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 已提交
2820
  inline ObSubPartition **get_def_subpart_array() const
O
oceanbase-admin 已提交
2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873
  {
    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 已提交
2874
  ObPartition **get_dropped_part_array() const
O
oceanbase-admin 已提交
2875 2876 2877 2878
  {
    return dropped_partition_array_;
  }
  // -------------------------------------
X
xy0 已提交
2879
  int get_part_id_by_name(const common::ObString partition_name, int64_t &part_id) const;
O
oceanbase-admin 已提交
2880 2881
  // 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 已提交
2882
      const int64_t part_id, const bool check_dropped_partition, const ObPartition *&partition) const;
O
oceanbase-admin 已提交
2883
  int get_subpart_info(
X
xy0 已提交
2884
      const int64_t part_id, ObSubPartition **&subpart_array, int64_t &subpart_num, int64_t &subpartition_num) const;
O
oceanbase-admin 已提交
2885
  //-------index partition-------
X
xy0 已提交
2886 2887
  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 已提交
2888

X
xy0 已提交
2889 2890
  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 已提交
2891

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

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

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

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

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

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

  DECLARE_VIRTUAL_TO_STRING;

G
gm 已提交
3272
private:
O
oceanbase-admin 已提交
3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294
  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 已提交
3295
public:
O
oceanbase-admin 已提交
3296 3297 3298
  /// Get part ids through range of partition_column

  // Get hash(FUNC_HASH/FUNC_KEY) part type ids
X
xy0 已提交
3299 3300
  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 已提交
3301 3302 3303 3304 3305
  // @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 已提交
3306 3307 3308 3309
  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 已提交
3310 3311 3312 3313 3314 3315
  // @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 已提交
3316 3317
  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 已提交
3318 3319

  // Get level-one range part_ids
X
xy0 已提交
3320 3321
  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 已提交
3322
  // The meaning of get_part_index can refer to get_hash_part_ids()
X
xy0 已提交
3323 3324
  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 已提交
3325 3326

  // Get level-one range part_ids
X
xy0 已提交
3327 3328
  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 已提交
3329 3330

  // The meaning of get_part_index can refer to get_hash_part_ids()
X
xy0 已提交
3331 3332
  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 已提交
3333 3334

  // Get level-two range part_ids
X
xy0 已提交
3335 3336
  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 已提交
3337
  // The meaning of get_subpart_index can refer to get_hash_subpart_ids()
X
xy0 已提交
3338 3339
  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 已提交
3340 3341 3342
      bool get_subpart_index = false);

  // Get level-two range part_ids
X
xy0 已提交
3343 3344
  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 已提交
3345 3346

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

  template <typename T>
X
xy0 已提交
3360 3361
  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 已提交
3362 3363

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

  // Convert rowkey to sql literal for show
X
xy0 已提交
3367 3368
  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 已提交
3369
  // Used to display the defined value of the LIST partition
X
xy0 已提交
3370 3371 3372 3373
  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 已提交
3374 3375

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

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

  // Get partition end with end_part rowkey, return end_pos
  template <typename T>
X
xy0 已提交
3387 3388
  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 已提交
3389 3390 3391 3392

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

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

G
gm 已提交
3397
private:
O
oceanbase-admin 已提交
3398 3399
  // Get level-one range part_ids
  // The meaning of get_part_index can refer to get_hash_part_ids()
X
xy0 已提交
3400 3401 3402
  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 已提交
3403 3404
  // Get level-two range part_ids
  // The meaning of get_subpart_index can refer to get_hash_subpart_ids()
X
xy0 已提交
3405 3406 3407
  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 已提交
3408 3409 3410 3411
};

template <typename T>
int ObPartitionUtils::get_range_part_idx(
X
xy0 已提交
3412
    const common::ObObj &obj, T *const *part_array, const int64_t part_num, int64_t &part_idx)
O
oceanbase-admin 已提交
3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435
{
  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 已提交
3436 3437
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 已提交
3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470
{
  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 已提交
3471
    const T *const *partition_array, const int64_t partition_num, const T &start_part, int64_t &start_pos)
O
oceanbase-admin 已提交
3472 3473 3474 3475 3476 3477
{
  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 已提交
3478
    const T *const *result =
O
oceanbase-admin 已提交
3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489
        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 已提交
3490 3491
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 已提交
3492 3493 3494 3495 3496 3497
{
  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 已提交
3498
    const T *const *result =
O
oceanbase-admin 已提交
3499 3500 3501 3502 3503 3504 3505 3506 3507
        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 已提交
3508
      lrow.cells_ = const_cast<common::ObObj *>((*result)->high_bound_val_.get_obj_ptr());
O
oceanbase-admin 已提交
3509 3510 3511 3512
      lrow.count_ = (*result)->high_bound_val_.get_obj_cnt();
      lrow.projector_ = (*result)->projector_;
      lrow.projector_size_ = (*result)->projector_size_;
      common::ObNewRow rrow;
X
xy0 已提交
3513
      rrow.cells_ = const_cast<common::ObObj *>(end_part.high_bound_val_.get_obj_ptr());
O
oceanbase-admin 已提交
3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564
      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 已提交
3565
public:
O
oceanbase-admin 已提交
3566
  ObViewSchema();
X
xy0 已提交
3567
  explicit ObViewSchema(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
3568
  virtual ~ObViewSchema();
X
xy0 已提交
3569 3570 3571 3572
  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 已提交
3573

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

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

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

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

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

  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 已提交
3803
  inline const common::ObString &get_table_name() const
O
oceanbase-admin 已提交
3804 3805 3806 3807
  {
    return table_name_;
  }

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

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

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

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

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

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

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

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

struct ObTenantPlanBaselineId {
  OB_UNIS_VERSION(1);

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

  DECLARE_TO_STRING;

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

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

  DECLARE_TO_STRING;

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

class ObPriv {
  OB_UNIS_VERSION_V(1);

G
gm 已提交
4243
public:
O
oceanbase-admin 已提交
4244 4245 4246 4247 4248 4249 4250
  ObPriv()
      : tenant_id_(common::OB_INVALID_ID),
        user_id_(common::OB_INVALID_ID),
        schema_version_(1),
        priv_set_(0),
        priv_array_()
  {}
X
xy0 已提交
4251
  ObPriv(common::ObIAllocator *allocator)
O
oceanbase-admin 已提交
4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263
      : 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 已提交
4264 4265
  ObPriv &operator=(const ObPriv &other);
  static bool cmp_tenant_user_id(const ObPriv *lhs, const ObTenantUserId &tenant_user_id)
O
oceanbase-admin 已提交
4266 4267 4268
  {
    return (lhs->get_tenant_user_id() < tenant_user_id);
  }
X
xy0 已提交
4269
  static bool equal_tenant_user_id(const ObPriv *lhs, const ObTenantUserId &tenant_user_id)
O
oceanbase-admin 已提交
4270 4271 4272
  {
    return (lhs->get_tenant_user_id() == tenant_user_id);
  }
X
xy0 已提交
4273
  static bool cmp_tenant_id(const ObPriv *lhs, const uint64_t tenant_id)
O
oceanbase-admin 已提交
4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309
  {
    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 已提交
4310
  int set_priv_array(const ObPackedPrivArray &other)
O
oceanbase-admin 已提交
4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334
  {
    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 已提交
4335
  inline const ObPackedPrivArray &get_priv_array() const
O
oceanbase-admin 已提交
4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351
  {
    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 已提交
4352
protected:
O
oceanbase-admin 已提交
4353 4354 4355 4356 4357 4358 4359 4360 4361 4362
  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 已提交
4363
public:
O
oceanbase-admin 已提交
4364 4365
  ObUserInfoHashWrapper() : tenant_id_(common::OB_INVALID_ID)
  {}
X
xy0 已提交
4366
  ObUserInfoHashWrapper(uint64_t tenant_id, const common::ObString &user_name)
O
oceanbase-admin 已提交
4367 4368 4369 4370 4371
      : tenant_id_(tenant_id), user_name_(user_name)
  {}
  ~ObUserInfoHashWrapper()
  {}
  inline uint64_t hash() const;
X
xy0 已提交
4372
  inline bool operator==(const ObUserInfoHashWrapper &rv) const;
O
oceanbase-admin 已提交
4373 4374 4375 4376 4377

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

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

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

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

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

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

G
gm 已提交
4697
private:
O
oceanbase-admin 已提交
4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713
  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
4714 4715
  uint64_t max_connections_;
  uint64_t max_user_connections_;
O
oceanbase-admin 已提交
4716 4717 4718 4719 4720 4721 4722 4723
};

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 已提交
4724
  bool operator==(const ObDBPrivSortKey &rhs) const
O
oceanbase-admin 已提交
4725 4726 4727
  {
    return (tenant_id_ == rhs.tenant_id_) && (user_id_ == rhs.user_id_) && (sort_ == rhs.sort_);
  }
X
xy0 已提交
4728
  bool operator!=(const ObDBPrivSortKey &rhs) const
O
oceanbase-admin 已提交
4729 4730 4731
  {
    return !(*this == rhs);
  }
X
xy0 已提交
4732
  bool operator<(const ObDBPrivSortKey &rhs) const
O
oceanbase-admin 已提交
4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753
  {
    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 已提交
4754
  ObOriginalDBKey(const uint64_t tenant_id, const uint64_t user_id, const common::ObString &db)
O
oceanbase-admin 已提交
4755 4756
      : tenant_id_(tenant_id), user_id_(user_id), db_(db)
  {}
X
xy0 已提交
4757
  bool operator==(const ObOriginalDBKey &rhs) const
O
oceanbase-admin 已提交
4758 4759 4760
  {
    return (tenant_id_ == rhs.tenant_id_) && (user_id_ == rhs.user_id_) && (db_ == rhs.db_);
  }
X
xy0 已提交
4761
  bool operator!=(const ObOriginalDBKey &rhs) const
O
oceanbase-admin 已提交
4762 4763 4764
  {
    return !(*this == rhs);
  }
X
xy0 已提交
4765
  bool operator<(const ObOriginalDBKey &rhs) const
O
oceanbase-admin 已提交
4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796
  {
    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 已提交
4797
  bool operator==(const ObSysPrivKey &rhs) const
O
oceanbase-admin 已提交
4798 4799 4800
  {
    return ((tenant_id_ == rhs.tenant_id_) && (grantee_id_ == rhs.grantee_id_));
  }
X
xy0 已提交
4801
  bool operator!=(const ObSysPrivKey &rhs) const
O
oceanbase-admin 已提交
4802 4803 4804
  {
    return !(*this == rhs);
  }
X
xy0 已提交
4805
  bool operator<(const ObSysPrivKey &rhs) const
O
oceanbase-admin 已提交
4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832
  {
    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 已提交
4833
public:
O
oceanbase-admin 已提交
4834 4835
  ObDBPriv() : ObPriv(), db_(), sort_(0)
  {}
X
xy0 已提交
4836
  explicit ObDBPriv(common::ObIAllocator *allocator) : ObSchema(allocator), ObPriv(allocator), db_(), sort_(0)
O
oceanbase-admin 已提交
4837 4838 4839
  {}
  virtual ~ObDBPriv()
  {}
X
xy0 已提交
4840
  ObDBPriv(const ObDBPriv &other) : ObSchema(), ObPriv()
O
oceanbase-admin 已提交
4841 4842 4843
  {
    *this = other;
  }
X
xy0 已提交
4844
  ObDBPriv &operator=(const ObDBPriv &other);
O
oceanbase-admin 已提交
4845

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

  // get methods
X
xy0 已提交
4882
  inline const char *get_database_name() const
O
oceanbase-admin 已提交
4883 4884 4885
  {
    return extract_str(db_);
  }
X
xy0 已提交
4886
  inline const common::ObString &get_database_name_str() const
O
oceanbase-admin 已提交
4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899
  {
    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 已提交
4900
private:
O
oceanbase-admin 已提交
4901 4902 4903 4904 4905 4906 4907 4908
  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 已提交
4909
  ObTablePrivDBKey(const uint64_t tenant_id, const uint64_t user_id, const common::ObString &db)
O
oceanbase-admin 已提交
4910 4911
      : tenant_id_(tenant_id), user_id_(user_id), db_(db)
  {}
X
xy0 已提交
4912
  bool operator==(const ObTablePrivDBKey &rhs) const
O
oceanbase-admin 已提交
4913 4914 4915
  {
    return (tenant_id_ == rhs.tenant_id_) && (user_id_ == rhs.user_id_) && (db_ == rhs.db_);
  }
X
xy0 已提交
4916
  bool operator!=(const ObTablePrivDBKey &rhs) const
O
oceanbase-admin 已提交
4917 4918 4919
  {
    return !(*this == rhs);
  }
X
xy0 已提交
4920
  bool operator<(const ObTablePrivDBKey &rhs) const
O
oceanbase-admin 已提交
4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939
  {
    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 已提交
4940
      const uint64_t tenant_id, const uint64_t user_id, const common::ObString &db, const common::ObString &table)
O
oceanbase-admin 已提交
4941 4942
      : tenant_id_(tenant_id), user_id_(user_id), db_(db), table_(table)
  {}
X
xy0 已提交
4943
  bool operator==(const ObTablePrivSortKey &rhs) const
O
oceanbase-admin 已提交
4944 4945 4946
  {
    return (tenant_id_ == rhs.tenant_id_) && (user_id_ == rhs.user_id_) && (db_ == rhs.db_) && (table_ == rhs.table_);
  }
X
xy0 已提交
4947
  bool operator!=(const ObTablePrivSortKey &rhs) const
O
oceanbase-admin 已提交
4948 4949 4950
  {
    return !(*this == rhs);
  }
X
xy0 已提交
4951
  bool operator<(const ObTablePrivSortKey &rhs) const
O
oceanbase-admin 已提交
4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003
  {
    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 已提交
5004
  bool operator==(const ObObjPrivSortKey &rhs) const
O
oceanbase-admin 已提交
5005 5006 5007 5008
  {
    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 已提交
5009
  bool operator!=(const ObObjPrivSortKey &rhs) const
O
oceanbase-admin 已提交
5010 5011 5012
  {
    return !(*this == rhs);
  }
X
xy0 已提交
5013
  bool operator<(const ObObjPrivSortKey &rhs) const
O
oceanbase-admin 已提交
5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063
  {
    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 已提交
5064
public:
O
oceanbase-admin 已提交
5065 5066 5067
  // constructor and destructor
  ObTablePriv() : ObSchema(), ObPriv()
  {}
X
xy0 已提交
5068
  explicit ObTablePriv(common::ObIAllocator *allocator) : ObSchema(allocator), ObPriv(allocator)
O
oceanbase-admin 已提交
5069
  {}
X
xy0 已提交
5070
  ObTablePriv(const ObTablePriv &other) : ObSchema(), ObPriv()
O
oceanbase-admin 已提交
5071 5072 5073 5074 5075 5076 5077
  {
    *this = other;
  }
  virtual ~ObTablePriv()
  {}

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

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

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

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

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

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

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

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

  // 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 已提交
5188
  static bool cmp_tenant_ur_obj_id(const ObObjPriv *lhs, const ObTenantUrObjId &rhs)
O
oceanbase-admin 已提交
5189 5190 5191
  {
    return (lhs->get_tenant_ur_obj_id() < rhs);
  }
X
xy0 已提交
5192
  static bool cmp(const ObObjPriv *lhs, const ObObjPriv *rhs)
O
oceanbase-admin 已提交
5193 5194 5195
  {
    return (NULL != lhs && NULL != rhs) ? lhs->get_sort_key() < rhs->get_sort_key() : false;
  }
X
xy0 已提交
5196
  static bool cmp_sort_key(const ObObjPriv *lhs, const ObObjPrivSortKey &sort_key)
O
oceanbase-admin 已提交
5197 5198 5199
  {
    return NULL != lhs ? lhs->get_sort_key() < sort_key : false;
  }
X
xy0 已提交
5200
  static bool equal(const ObObjPriv *lhs, const ObObjPriv *rhs)
O
oceanbase-admin 已提交
5201 5202 5203
  {
    return (NULL != lhs && NULL != rhs) ? lhs->get_sort_key() == rhs->get_sort_key() : false;
  }
X
xy0 已提交
5204
  static bool equal_sort_key(const ObObjPriv *lhs, const ObObjPrivSortKey &sort_key)
O
oceanbase-admin 已提交
5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263
  {
    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 已提交
5264
private:
O
oceanbase-admin 已提交
5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282
  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 已提交
5283
const char *ob_priv_level_str(const ObPrivLevel grant_level);
O
oceanbase-admin 已提交
5284 5285

struct ObNeedPriv {
X
xy0 已提交
5286
  ObNeedPriv(const common::ObString &db, const common::ObString &table, ObPrivLevel priv_level, ObPrivSet priv_set,
O
oceanbase-admin 已提交
5287 5288 5289 5290 5291
      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 已提交
5292
  int deep_copy(const ObNeedPriv &other, common::ObIAllocator &allocator);
O
oceanbase-admin 已提交
5293 5294 5295 5296 5297 5298 5299 5300 5301 5302
  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 已提交
5303
  explicit ObStmtNeedPrivs(common::ObIAllocator &alloc) : need_privs_(alloc)
O
oceanbase-admin 已提交
5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314
  {}
  ObStmtNeedPrivs() : need_privs_()
  {}
  ~ObStmtNeedPrivs()
  {
    reset();
  }
  void reset()
  {
    need_privs_.reset();
  }
X
xy0 已提交
5315
  int deep_copy(const ObStmtNeedPrivs &other, common::ObIAllocator &allocator);
O
oceanbase-admin 已提交
5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337
  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 已提交
5338
  ObOraNeedPriv(const common::ObString &db_name, uint64_t grantee_id, uint64_t obj_id, uint64_t col_id,
O
oceanbase-admin 已提交
5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350
      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 已提交
5351 5352
  int deep_copy(const ObOraNeedPriv &other, common::ObIAllocator &allocator);
  bool same_obj(const ObOraNeedPriv &other);
O
oceanbase-admin 已提交
5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369
  // 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 已提交
5370
  explicit ObStmtOraNeedPrivs(common::ObIAllocator &alloc) : need_privs_(alloc)
O
oceanbase-admin 已提交
5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381
  {}
  ObStmtOraNeedPrivs() : need_privs_()
  {}
  ~ObStmtOraNeedPrivs()
  {
    reset();
  }
  void reset()
  {
    need_privs_.reset();
  }
X
xy0 已提交
5382
  int deep_copy(const ObStmtOraNeedPrivs &other, common::ObIAllocator &allocator);
O
oceanbase-admin 已提交
5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399
  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 已提交
5400
      const common::ObString &db, const ObPrivSet user_priv_set, const ObPrivSet db_priv_set)
O
oceanbase-admin 已提交
5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461
      : 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 已提交
5462 5463
  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 已提交
5464 5465 5466 5467 5468 5469 5470 5471
      : tenant_name_(tenant_name),
        user_name_(user_name),
        client_ip_(client_ip),
        passwd_(passwd),
        db_(db),
        scramble_str_()
  {}

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

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

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

class ObHostnameStuct {
G
gm 已提交
5575
public:
O
oceanbase-admin 已提交
5576 5577 5578 5579 5580 5581
  ObHostnameStuct()
  {}
  ~ObHostnameStuct()
  {}
  static const uint32_t FAKE_PORT = 0;
  static const int MAX_IP_BITS = 128;
X
xy0 已提交
5582 5583 5584 5585 5586 5587
  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 已提交
5588 5589 5590 5591 5592 5593 5594 5595
};

enum ObHintFormat {
  HINT_NORMAL,
  HINT_LOCAL,
};

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

class ObMaxConcurrentParam {
  OB_UNIS_VERSION(1);

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

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

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

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

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

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

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

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

struct BaselineKey {
  OB_UNIS_VERSION(1);

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

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

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

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

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

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

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

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

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

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

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

G
gm 已提交
6218
private:
X
xy0 已提交
6219 6220 6221 6222
  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 已提交
6223

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

class ObDbLinkInfo : public ObDbLinkBaseInfo {
  OB_UNIS_VERSION(1);

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

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

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

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

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

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

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

G
gm 已提交
6763
private:
O
oceanbase-admin 已提交
6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777
  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 已提交
6778
inline bool ObOutlineNameHashWrapper::operator==(const ObOutlineNameHashWrapper &rv) const
O
oceanbase-admin 已提交
6779 6780 6781 6782 6783
{
  return (tenant_id_ == rv.tenant_id_) && (database_id_ == rv.database_id_) && (name_ == rv.name_);
}

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

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

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

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

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

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

  int init_tenant_space_table_id_map();
  int init_sys_table_name_map();
X
xy0 已提交
6904 6905 6906
  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 已提交
6907

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

  int64_t get_tenant_space_sys_table_num()
  {
    return tenant_space_sys_table_num_;
  }

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

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

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

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

G
gm 已提交
7201
public:
O
oceanbase-admin 已提交
7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225
  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 已提交
7226
  inline int set_foreign_key_name(const common::ObString &foreign_key_name)
O
oceanbase-admin 已提交
7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266
  {
    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 已提交
7267
  inline const char *get_update_action_str() const
O
oceanbase-admin 已提交
7268 7269 7270
  {
    return get_action_str(update_action_);
  }
X
xy0 已提交
7271
  inline const char *get_delete_action_str() const
O
oceanbase-admin 已提交
7272 7273 7274
  {
    return get_action_str(delete_action_);
  }
X
xy0 已提交
7275
  inline const char *get_action_str(ObReferenceAction action) const
O
oceanbase-admin 已提交
7276
  {
X
xy0 已提交
7277
    const char *ret = NULL;
O
oceanbase-admin 已提交
7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303
    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 已提交
7304
  int assign(const ObForeignKeyInfo &other);
O
oceanbase-admin 已提交
7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317
  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 已提交
7318
public:
O
oceanbase-admin 已提交
7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337
  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 已提交
7338
private:
X
xy0 已提交
7339
  static const char *reference_action_str_[ACTION_MAX + 1];
O
oceanbase-admin 已提交
7340 7341 7342 7343 7344
};

struct ObSimpleForeignKeyInfo {
  OB_UNIS_VERSION(1);

G
gm 已提交
7345
public:
O
oceanbase-admin 已提交
7346 7347 7348 7349 7350 7351 7352 7353 7354
  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 已提交
7355
      const common::ObString &foreign_key_name, const uint64_t foreign_key_id)
O
oceanbase-admin 已提交
7356 7357 7358 7359 7360 7361
      : 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 已提交
7362
  bool operator==(const ObSimpleForeignKeyInfo &other) const
O
oceanbase-admin 已提交
7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392
  {
    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 已提交
7393
public:
O
oceanbase-admin 已提交
7394 7395 7396 7397 7398 7399 7400 7401 7402
  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 已提交
7403
      const common::ObString &constraint_name, const uint64_t constraint_id)
O
oceanbase-admin 已提交
7404 7405 7406 7407 7408 7409
      : tenant_id_(tenant_id),
        database_id_(database_id),
        table_id_(table_id),
        constraint_name_(constraint_name),
        constraint_id_(constraint_id)
  {}
X
xy0 已提交
7410
  bool operator==(const ObSimpleConstraintInfo &other) const
O
oceanbase-admin 已提交
7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439
  {
    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 已提交
7440
    const PARTITION &l_part, const PARTITION &r_part, const ObPartitionFuncType part_type, bool &is_equal)
O
oceanbase-admin 已提交
7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486
{
  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 已提交
7487 7488
    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 已提交
7489 7490 7491 7492
    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 已提交
7493
        const common::ObNewRow &l_rowkey = l_list_values.at(i);
O
oceanbase-admin 已提交
7494 7495
        bool find_equal_item = false;
        for (int64_t j = 0; j < r_list_values.count() && !find_equal_item && is_equal; j++) {
X
xy0 已提交
7496
          const common::ObNewRow &r_rowkey = r_list_values.at(j);
O
oceanbase-admin 已提交
7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541
          // 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 已提交
7542
public:
O
oceanbase-admin 已提交
7543
  ObSequenceSchema();
X
xy0 已提交
7544
  explicit ObSequenceSchema(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
7545
  virtual ~ObSequenceSchema();
X
xy0 已提交
7546 7547 7548
  ObSequenceSchema &operator=(const ObSequenceSchema &src_schema);
  int assign(const ObSequenceSchema &src_schema);
  ObSequenceSchema(const ObSequenceSchema &src_schema);
O
oceanbase-admin 已提交
7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566
  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 已提交
7567
  inline int set_sequence_name(const char *name)
O
oceanbase-admin 已提交
7568 7569 7570
  {
    return deep_copy_str(name, name_);
  }
X
xy0 已提交
7571
  inline int set_sequence_name(const common::ObString &name)
O
oceanbase-admin 已提交
7572 7573 7574
  {
    return deep_copy_str(name, name_);
  }
X
xy0 已提交
7575
  inline int set_name(const common::ObString &name)
O
oceanbase-admin 已提交
7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599
  {
    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 已提交
7600
  int set_max_value(const common::number::ObNumber &num)
O
oceanbase-admin 已提交
7601 7602 7603
  {
    return option_.set_max_value(num);
  }
X
xy0 已提交
7604
  int set_min_value(const common::number::ObNumber &num)
O
oceanbase-admin 已提交
7605 7606 7607
  {
    return option_.set_min_value(num);
  }
X
xy0 已提交
7608
  int set_increment_by(const common::number::ObNumber &num)
O
oceanbase-admin 已提交
7609 7610 7611
  {
    return option_.set_increment_by(num);
  }
X
xy0 已提交
7612
  int set_start_with(const common::number::ObNumber &num)
O
oceanbase-admin 已提交
7613 7614 7615
  {
    return option_.set_start_with(num);
  }
X
xy0 已提交
7616
  int set_cache_size(const common::number::ObNumber &num)
O
oceanbase-admin 已提交
7617 7618 7619 7620
  {
    return option_.set_cache_size(num);
  }

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

  ObProfileSchema();
X
xy0 已提交
7795
  explicit ObProfileSchema(common::ObIAllocator *allocator);
O
oceanbase-admin 已提交
7796 7797 7798 7799 7800 7801 7802 7803 7804
  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 已提交
7805 7806
  ObProfileSchema &operator=(const ObProfileSchema &src_schema);
  ObProfileSchema(const ObProfileSchema &src_schema);
O
oceanbase-admin 已提交
7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819

  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 已提交
7820
  inline int set_profile_name(const char *name)
O
oceanbase-admin 已提交
7821 7822 7823
  {
    return deep_copy_str(name, profile_name_);
  }
X
xy0 已提交
7824
  inline int set_profile_name(const common::ObString &name)
O
oceanbase-admin 已提交
7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856
  {
    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 已提交
7857
  inline const char *get_profile_name() const
O
oceanbase-admin 已提交
7858 7859 7860
  {
    return extract_str(profile_name_);
  }
X
xy0 已提交
7861
  inline const common::ObString &get_profile_name_str() const
O
oceanbase-admin 已提交
7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890
  {
    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 已提交
7891
  static int get_default_value(const int64_t type, int64_t &value);
O
oceanbase-admin 已提交
7892

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

G
gm 已提交
7910
private:
O
oceanbase-admin 已提交
7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921
  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 已提交
7922
common::ObIAllocator *&schema_stack_allocator();
O
oceanbase-admin 已提交
7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936

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 已提交
7937
public:
O
oceanbase-admin 已提交
7938 7939 7940 7941 7942 7943 7944 7945 7946 7947
  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 已提交
7948
  void inline gen_mask(uint64_t type, uint64_t &id)
O
oceanbase-admin 已提交
7949 7950 7951 7952 7953 7954 7955 7956 7957 7958
  {
    id = (TYPE_MASK & type) | (VAL_MASK & id);
  }
};

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

#endif /* _OB_OCEANBASE_SCHEMA_SCHEMA_STRUCT_H */