OceanBase里面的rowkey是什么概念,是由哪些要素构成的?

Rowkey是OceanBase诞生之初就引入的概念,最终被确立是在OceanBase 0.3。

为了便于理解,不妨把OceanBase想象成一个Key-Value系统,Rowkey就是Key,Value就是返回的行数据。

如果你对mysql数据库熟悉,那么不妨把Rowkey理解成primary key,它就是那几个主键列的组合,列的顺序与primary key中定义的顺序一致。


OceanBase中的静态数据是按照rowkey顺序存储在磁盘中的,这样做的好处是:

1. 支持每一行的快速定位(想象一下BTree的查找过程,如果数据无序,根本无从查起,也不叫BTree了)

2. 支持连续行的扫描。一个rowkey对应一行,一个rowkey前缀则对应一片连续行。


在OceanBase 0.3之前的Rowkey是一个概念,在代码中并没有一个实体对象。例如,在0.2版本中,内存中的行数据是存储在一个大的二维数组中(实际是用一维数组存储,不过逻辑上是二维,对象叫ObCellArray),纵向维度就是一行行的数据,横向维度就是一列列的数据。每一行的开头几列都是Rowkey列,后面几列就是用户看到的数据列。

0.3之后引入了ObRowkey对象,用于表示一行的rowkey,它的构成要素包括:

1. 它记录了主键列的列数

2. 它以数组的形式记录了主键列的值,值在数组中的顺序与primary key的定义顺序一致。

这里是缩略版的ObRowkey定义:

    class ObRowkey
    {
      public:
        ObRowkey() : obj_ptr_(NULL), obj_cnt_(0) {}
        ObRowkey(ObObj* ptr, const int64_t cnt) : obj_ptr_(ptr), obj_cnt_(cnt) {}
        ~ObRowkey() {}
        inline int64_t get_obj_cnt() const { return obj_cnt_; }
        inline const ObObj* get_obj_ptr() const { return obj_ptr_; }
        // for convenience compactible with ObString
        inline int64_t length()  const { return obj_cnt_; }
        inline const ObObj* ptr() const { return obj_ptr_; }
        int64_t get_binary_key_length() const ;
        inline bool is_empty_row() const { return NULL == obj_ptr_ && 0 == obj_cnt_; }
        // is min rowkey or max rowkey
        inline bool is_min_row(void) const { return (*this == ObRowkey::MIN_ROWKEY); }
        inline bool is_max_row(void) const { return (*this == ObRowkey::MAX_ROWKEY); }
        inline void set_min_row(void) { *this = ObRowkey::MIN_ROWKEY; }
        inline void set_max_row(void) { *this = ObRowkey::MAX_ROWKEY; }
      private:
        ObObj* obj_ptr_;
        int64_t obj_cnt_;
      public:
        static ObObj MIN_OBJECT;
        static ObObj MAX_OBJECT;
        static ObRowkey MIN_ROWKEY;
        static ObRowkey MAX_ROWKEY;
    };


但它不负责记录:

1. 每个主键列在表中是第几列

2. 每个主键列的数据类型是什么

如果要知道这些信息,需要借助ObRowkeyInfo结构:

    class ObRowkeyInfo
    {

    public:
      ObRowkeyInfo();
      ~ObRowkeyInfo();

      inline int64_t get_size() const
      {
        return size_;
      }

      /**
       * get sum of every column's length.
       */
      int64_t get_binary_rowkey_length() const;

      /**
       * Get rowkey column by index
       * @param[in]  index   column index in RowkeyInfo
       * @param[out] column
       *
       * @return int  return OB_SUCCESS if get the column, otherwist return OB_ERROR
       */
      int get_column(const int64_t index, ObRowkeyColumn& column) const;
      const ObRowkeyColumn *get_column(const int64_t index) const;

      /**
       * Get rowkey column id by index
       * @param[in]  index   column index in RowkeyInfo
       * @param[out] column_id in ObRowkeyInfo
       *
       * @return int  return OB_SUCCESS if get the column, otherwist return OB_ERROR
       */
      int get_column_id(const int64_t index, uint64_t & column_id) const;

      /**
       * Add column to rowkey info
       * @param column column to add
       * @return itn  return OB_SUCCESS if add success, otherwise return OB_ERROR
       */
      int add_column(const ObRowkeyColumn& column);

      int get_index(const uint64_t column_id, int64_t &index, ObRowkeyColumn& column) const;
      int get_index(const uint64_t column_id, int64_t &index) const;
      bool is_rowkey_column(const uint64_t column_id) const;
      int set_column(int64_t idx, const ObRowkeyColumn& column);

      int64_t to_string(char* buf, const int64_t buf_len) const;
      NEED_SERIALIZE_AND_DESERIALIZE;
    private:
      ObRowkeyColumn columns_[OB_MAX_ROWKEY_COLUMN_NUMBER];
      int64_t size_;
    };



如果您对OceanBase感兴趣,请关注OceanBase官网:http://alibaba.github.io/oceanbase/




原文地址:https://www.cnblogs.com/javawebsoa/p/3235298.html