|
| 1 | +/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved. |
| 2 | +miniob is licensed under Mulan PSL v2. |
| 3 | +You can use this software according to the terms and conditions of the Mulan PSL v2. |
| 4 | +You may obtain a copy of Mulan PSL v2 at: |
| 5 | + http://license.coscl.org.cn/MulanPSL2 |
| 6 | +THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, |
| 7 | +EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, |
| 8 | +MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. |
| 9 | +See the Mulan PSL v2 for more details. */ |
| 10 | + |
| 11 | +// |
| 12 | +// Created by MiniOB Update Implementor. |
| 13 | +// |
| 14 | + |
| 15 | +#include "sql/operator/update_physical_operator.h" |
| 16 | +#include "common/log/log.h" |
| 17 | +#include "sql/expr/tuple.h" |
| 18 | +#include "storage/table/table.h" |
| 19 | +#include "storage/table/table_meta.h" |
| 20 | +#include "storage/trx/trx.h" |
| 21 | +#include <string.h> |
| 22 | + |
| 23 | +RC UpdatePhysicalOperator::set_value_to_record(char *record_data, const Value &value, const FieldMeta *field) |
| 24 | +{ |
| 25 | + size_t copy_len = field->len(); |
| 26 | + const size_t data_len = value.length(); |
| 27 | + if (field->type() == AttrType::CHARS) { |
| 28 | + if (copy_len > data_len) { |
| 29 | + copy_len = data_len + 1; // include '\0' |
| 30 | + } |
| 31 | + } |
| 32 | + memcpy(record_data + field->offset(), value.data(), copy_len); |
| 33 | + return RC::SUCCESS; |
| 34 | +} |
| 35 | + |
| 36 | +RC UpdatePhysicalOperator::open(Trx *trx) |
| 37 | +{ |
| 38 | + if (children_.empty()) { |
| 39 | + return RC::SUCCESS; |
| 40 | + } |
| 41 | + |
| 42 | + trx_ = trx; |
| 43 | + |
| 44 | + unique_ptr<PhysicalOperator> &child = children_[0]; |
| 45 | + |
| 46 | + RC rc = child->open(trx); |
| 47 | + if (rc != RC::SUCCESS) { |
| 48 | + LOG_WARN("failed to open child operator: %s", strrc(rc)); |
| 49 | + return rc; |
| 50 | + } |
| 51 | + |
| 52 | + while (OB_SUCC(rc = child->next())) { |
| 53 | + Tuple *tuple = child->current_tuple(); |
| 54 | + if (nullptr == tuple) { |
| 55 | + LOG_WARN("failed to get current tuple"); |
| 56 | + rc = RC::INTERNAL; |
| 57 | + break; |
| 58 | + } |
| 59 | + |
| 60 | + RowTuple *row_tuple = static_cast<RowTuple *>(tuple); |
| 61 | + Record &record = row_tuple->record(); |
| 62 | + records_.emplace_back(std::move(record)); |
| 63 | + } |
| 64 | + |
| 65 | + child->close(); |
| 66 | + |
| 67 | + if (rc != RC::SUCCESS && rc != RC::RECORD_EOF) { |
| 68 | + return rc; |
| 69 | + } |
| 70 | + |
| 71 | + const TableMeta &meta = table_->table_meta(); |
| 72 | + const int rec_size = meta.record_size(); |
| 73 | + |
| 74 | + // delete + insert to update rows |
| 75 | + for (Record &old_rec : records_) { |
| 76 | + Record new_rec; |
| 77 | + rc = new_rec.copy_data(old_rec.data(), rec_size); |
| 78 | + if (OB_FAIL(rc)) { |
| 79 | + LOG_WARN("failed to copy record. rc=%s", strrc(rc)); |
| 80 | + return rc; |
| 81 | + } |
| 82 | + |
| 83 | + Value write_val = value_; |
| 84 | + if (write_val.attr_type() != field_->type()) { |
| 85 | + Value casted; |
| 86 | + rc = Value::cast_to(write_val, field_->type(), casted); |
| 87 | + if (OB_FAIL(rc)) { |
| 88 | + LOG_WARN("failed to cast value. rc=%s", strrc(rc)); |
| 89 | + return rc; |
| 90 | + } |
| 91 | + write_val = std::move(casted); |
| 92 | + } |
| 93 | + |
| 94 | + rc = set_value_to_record(const_cast<char *>(new_rec.data()), write_val, field_); |
| 95 | + if (OB_FAIL(rc)) { |
| 96 | + LOG_WARN("failed to set value to record. rc=%s", strrc(rc)); |
| 97 | + return rc; |
| 98 | + } |
| 99 | + |
| 100 | + rc = trx_->delete_record(table_, old_rec); |
| 101 | + if (OB_FAIL(rc)) { |
| 102 | + LOG_WARN("failed to delete record. rc=%s", strrc(rc)); |
| 103 | + return rc; |
| 104 | + } |
| 105 | + |
| 106 | + rc = trx_->insert_record(table_, new_rec); |
| 107 | + if (OB_FAIL(rc)) { |
| 108 | + LOG_WARN("failed to insert record. rc=%s", strrc(rc)); |
| 109 | + return rc; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return RC::SUCCESS; |
| 114 | +} |
| 115 | + |
| 116 | +RC UpdatePhysicalOperator::next() |
| 117 | +{ |
| 118 | + return RC::RECORD_EOF; |
| 119 | +} |
| 120 | + |
| 121 | +RC UpdatePhysicalOperator::close() |
| 122 | +{ |
| 123 | + return RC::SUCCESS; |
| 124 | +} |
| 125 | + |
| 126 | + |
0 commit comments