ob_value_row_iterator.cpp 9.7 KB
Newer Older
O
oceanbase-admin 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/**
 * 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.
 */

L
leslieyuchen 已提交
13
#define USING_LOG_PREFIX STORAGE
O
oceanbase-admin 已提交
14
#include "ob_value_row_iterator.h"
L
leslieyuchen 已提交
15 16
#include "ob_partition_storage.h"
#include "ob_single_merge.h"
O
oceanbase-admin 已提交
17 18 19 20 21 22 23 24 25 26

namespace oceanbase {
using namespace oceanbase::common;
namespace storage {
ObValueRowIterator::ObValueRowIterator()
    : ObNewRowIterator(),
      is_inited_(false),
      unique_(false),
      allocator_(ObModIds::OB_VALUE_ROW_ITER),
      rows_(),
27 28
      cur_idx_(0),
      data_table_rowkey_cnt_(0)
O
oceanbase-admin 已提交
29 30 31 32 33
{}

ObValueRowIterator::~ObValueRowIterator()
{}

34
int ObValueRowIterator::init(bool unique, int64_t data_table_rowkey_cnt)
O
oceanbase-admin 已提交
35 36 37 38 39 40 41 42 43
{
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(is_inited_)) {
    ret = OB_INIT_TWICE;
    STORAGE_LOG(WARN, "ObValueRowIterator is already initialized", K(ret));
  } else {
    is_inited_ = true;
    unique_ = unique;
    cur_idx_ = 0;
44
    data_table_rowkey_cnt_ = data_table_rowkey_cnt;
O
oceanbase-admin 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58
  }
  return ret;
}

int ObValueRowIterator::add_row(common::ObNewRow& row)
{
  int ret = OB_SUCCESS;
  ObNewRow* cur_row = NULL;
  if (!row.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    STORAGE_LOG(WARN, "invalid row", K(ret), K(row));
  } else if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    STORAGE_LOG(WARN, "ObValueRowIterator is not initialized", K(ret));
59 60 61
  } else if (data_table_rowkey_cnt_ > row.count_) {
    ret = OB_INVALID_ARGUMENT;
    STORAGE_LOG(WARN, "invalid row", K(ret), K(row), K(data_table_rowkey_cnt_));
O
oceanbase-admin 已提交
62 63 64 65 66 67 68 69
  } else {
    bool exist = false;
    // check whether exists
    if (unique_ && rows_.count() > 0) {
      // we consider that in general, the probability that a row produces different conflicting rows
      // on multiple unique index is small, so there is usually only one row in the value row iterator
      // so using list traversal to deduplicate unique index is more efficiently
      // and also saves the CPU overhead that constructs the hash map
70
      ObStoreRowkey rowkey(row.cells_, data_table_rowkey_cnt_);
O
oceanbase-admin 已提交
71
      for (int64_t i = 0; OB_SUCC(ret) && !exist && i < rows_.count(); ++i) {
72 73 74 75 76 77 78 79 80
        if (data_table_rowkey_cnt_ > rows_.at(i).count_) {
          ret = OB_INVALID_ARGUMENT;
          STORAGE_LOG(WARN, "invalid row", K(ret), K(row), K(data_table_rowkey_cnt_));
        } else {
          ObStoreRowkey tmp_rowkey(rows_.at(i).cells_, data_table_rowkey_cnt_);
          STORAGE_LOG(DEBUG, "print rowkey info", K(rowkey), K(tmp_rowkey), K(data_table_rowkey_cnt_));
          if (OB_UNLIKELY(tmp_rowkey == rowkey)) {
            exist = true;
          }
O
oceanbase-admin 已提交
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
        }
      }
    }
    // store non-exist row
    if (OB_SUCC(ret)) {
      if (!exist) {
        if (NULL == (cur_row = rows_.alloc_place_holder())) {
          ret = OB_ALLOCATE_MEMORY_FAILED;
          STORAGE_LOG(ERROR, "add row error", K(ret));
        } else if (OB_SUCCESS != (ret = ob_write_row(allocator_, row, *cur_row))) {
          STORAGE_LOG(WARN, "copy row error", K(ret), K(row));
        }
      }
    }
  }
  return ret;
}

int ObValueRowIterator::get_next_row(common::ObNewRow*& row)
{
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(!is_inited_)) {
    ret = OB_NOT_INIT;
    STORAGE_LOG(WARN, "ObValueRowIterator is not initialized", K(ret));
  } else if (cur_idx_ < rows_.count()) {
    row = &rows_.at(cur_idx_++);
  } else {
    ret = OB_ITER_END;
  }
  return ret;
}

int ObValueRowIterator::get_next_rows(ObNewRow*& rows, int64_t& row_count)
{
  int ret = OB_SUCCESS;
  if (cur_idx_ < rows_.count()) {
    rows = &(rows_.at(cur_idx_));
    row_count = rows_.count() - cur_idx_;
    cur_idx_ = rows_.count();
  } else {
    ret = OB_ITER_END;
  }
  return ret;
}

void ObValueRowIterator::reset()
{
  rows_.reset();
  allocator_.reset();
  is_inited_ = false;
  unique_ = false;
  cur_idx_ = 0;
}

L
leslieyuchen 已提交
135 136 137 138 139 140 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
ObSingleRowGetter::ObSingleRowGetter(ObIAllocator &allocator, ObPartitionStore &store)
    : store_(store),
      single_merge_(nullptr),
      store_ctx_(nullptr),
      output_projector_(allocator),
      relative_table_(nullptr),
      table_param_(nullptr),
      allocator_(allocator)
{}

ObSingleRowGetter::~ObSingleRowGetter()
{
  if (single_merge_ != nullptr) {
    single_merge_->~ObSingleMerge();
    allocator_.free(single_merge_);
    single_merge_ = nullptr;
  }
  if (table_param_ != nullptr) {
    table_param_->~ObTableParam();
    allocator_.free(table_param_);
    table_param_ = nullptr;
  }
}

int ObSingleRowGetter::init_dml_access_ctx(const ObStoreCtx &store_ctx, const ObDMLBaseParam &dml_param)
{
  int ret = OB_SUCCESS;
  common::ObVersionRange trans_version_range;
  // TODO (muwei) trans_version_range值后续由上层传入
  trans_version_range.snapshot_version_ = store_ctx.mem_ctx_->get_read_snapshot();
  trans_version_range.base_version_ = 0;
  trans_version_range.multi_version_start_ = 0;
  store_ctx_ = &store_ctx;

  if (OB_FAIL(access_ctx_.init(dml_param.query_flag_, store_ctx, allocator_, trans_version_range))) {
    LOG_WARN("failed to init table access ctx", K(ret));
  } else {
    access_ctx_.expr_ctx_ = const_cast<ObExprCtx *>(&dml_param.expr_ctx_);
  }
  return ret;
}

int ObSingleRowGetter::init_dml_access_param(
    ObRelativeTable &relative_table, const ObDMLBaseParam &dml_param, const ObIArray<uint64_t> &out_col_ids)
{
  int ret = OB_SUCCESS;
  relative_table_ = &relative_table;
  get_table_param_.tables_handle_ = &(relative_table.tables_handle_);
  if (!dml_param.virtual_columns_.empty() && !relative_table.is_index_table()) {
    //The index table does not contain virtual columns, no need to set virtual_columns
    access_param_.virtual_column_exprs_ = &(dml_param.virtual_columns_);
  }
  if (OB_UNLIKELY(!relative_table.use_schema_param())) {
    const share::schema::ObTableSchema *schema = relative_table.get_schema();
    if (OB_FAIL(create_table_param())) {
      LOG_WARN("create table param failed", K(ret));
    } else if (OB_FAIL(table_param_->convert(*schema, *schema, out_col_ids, false))) {
      LOG_WARN("build table param from schema fail", K(ret), KPC(schema));
    } else if (OB_FAIL(access_param_.init_dml_access_param(relative_table.get_table_id(),
                   relative_table.get_schema_version(),
                   relative_table.get_rowkey_column_num(),
                   *table_param_))) {
      LOG_WARN("init dml access param failed", K(ret));
    }
  } else {
    const share::schema::ObTableSchemaParam *schema_param = relative_table.get_schema_param();
    output_projector_.set_capacity(out_col_ids.count());
    for (int32_t i = 0; OB_SUCC(ret) && i < out_col_ids.count(); ++i) {
      int idx = OB_INVALID_INDEX;
      if (OB_FAIL(schema_param->get_col_map().get(out_col_ids.at(i), idx))) {
        LOG_WARN("get column index from column map failed", K(ret), K(out_col_ids.at(i)));
      } else if (OB_FAIL(output_projector_.push_back(idx))) {
        LOG_WARN("store output projector failed", K(ret));
      }
    }
    if (OB_SUCC(ret)) {
      if (OB_FAIL(access_param_.init_dml_access_param(relative_table.get_table_id(),
              relative_table.get_schema_version(),
              relative_table.get_rowkey_column_num(),
              *schema_param,
              &output_projector_))) {
        LOG_WARN("init dml access param failed", K(ret));
      }
    }
  }
  LOG_DEBUG("init dml access param", K(ret), K(out_col_ids), K(relative_table), K(dml_param), K_(access_param));
  return ret;
}

int ObSingleRowGetter::create_table_param()
{
  int ret = OB_SUCCESS;
  void *buf = nullptr;
  if (table_param_ != nullptr) {
    ret = OB_INIT_TWICE;
    LOG_WARN("init table param twice", K(ret));
  } else if (OB_ISNULL(buf = allocator_.alloc(sizeof(share::schema::ObTableParam)))) {
    ret = OB_ALLOCATE_MEMORY_FAILED;
    LOG_WARN("allocate table param failed", K(ret), K(sizeof(share::schema::ObTableParam)));
  } else {
    table_param_ = new (buf) share::schema::ObTableParam(allocator_);
  }
  return ret;
}

int ObSingleRowGetter::open(const ObStoreRowkey &rowkey, bool use_fuse_row_cache)
{
  int ret = OB_SUCCESS;
  void *buf = nullptr;

  new (&ext_rowkey_) ObExtStoreRowkey(rowkey);
  if (OB_ISNULL(buf = allocator_.alloc(sizeof(ObSingleMerge)))) {
    ret = OB_ALLOCATE_MEMORY_FAILED;
    LOG_WARN("Fail to allocate memory for multi get merge ", K(ret));
  } else {
    {
      ObStorageWriterGuard guard(store_, *store_ctx_, false);
      if (OB_FAIL(guard.refresh_and_protect_table(*relative_table_))) {
        STORAGE_LOG(WARN, "fail to protect table", K(ret));
      }
    }
    single_merge_ = new (buf) ObSingleMerge();
  }
  if (OB_SUCC(ret)) {
    if (OB_FAIL(single_merge_->init(access_param_, access_ctx_, get_table_param_))) {
      STORAGE_LOG(WARN, "Fail to init ObSingleMerge, ", K(ret));
    } else if (OB_FAIL(single_merge_->open(ext_rowkey_))) {
      STORAGE_LOG(WARN, "Fail to open iter, ", K(ret));
    }
    if (use_fuse_row_cache) {
      access_ctx_.use_fuse_row_cache_ = true;
      access_ctx_.fuse_row_cache_hit_rate_ = 100L;
    }
  }
  return ret;
}

int ObSingleRowGetter::get_next_row(ObNewRow *&row)
{
  int ret = OB_SUCCESS;
  row = nullptr;
  while (OB_SUCC(ret)) {
    ObStoreRow *store_row = NULL;
    if (OB_FAIL(single_merge_->get_next_row(store_row))) {
      if (OB_ITER_END != ret) {
        STORAGE_LOG(WARN, "failed to get next row", K(ret));
      }
    } else if (ObActionFlag::OP_ROW_EXIST == store_row->flag_) {
      row = &store_row->row_val_;
      break;
    }
  }
  return ret;
}
O
oceanbase-admin 已提交
289 290
}  // end namespace storage
}  // end namespace oceanbase