| 2192 | } |
| 2193 | |
| 2194 | void TabletImpl::UpdateTableMetaForAddField(RpcController* controller, |
| 2195 | const ::fedb::api::UpdateTableMetaForAddFieldRequest* request, |
| 2196 | ::fedb::api::GeneralResponse* response, Closure* done) { |
| 2197 | brpc::ClosureGuard done_guard(done); |
| 2198 | uint32_t tid = request->tid(); |
| 2199 | std::map<uint32_t, std::shared_ptr<Table>> table_map; |
| 2200 | { |
| 2201 | std::lock_guard<SpinMutex> spin_lock(spin_mutex_); |
| 2202 | auto it = tables_.find(tid); |
| 2203 | if (it == tables_.end()) { |
| 2204 | response->set_code(::fedb::base::ReturnCode::kTableIsNotExist); |
| 2205 | response->set_msg("table doesn`t exist"); |
| 2206 | PDLOG(WARNING, "table tid %u doesn`t exist.", tid); |
| 2207 | return; |
| 2208 | } |
| 2209 | table_map = it->second; |
| 2210 | } |
| 2211 | for (auto pit = table_map.begin(); pit != table_map.end(); ++pit) { |
| 2212 | uint32_t pid = pit->first; |
| 2213 | std::shared_ptr<Table> table = pit->second; |
| 2214 | // judge if field exists |
| 2215 | ::fedb::api::TableMeta table_meta(*table->GetTableMeta()); |
| 2216 | if (request->has_column_desc()) { |
| 2217 | const auto& col = request->column_desc(); |
| 2218 | if (table->CheckFieldExist(col.name())) { |
| 2219 | continue; |
| 2220 | } |
| 2221 | fedb::common::ColumnDesc* column_desc = table_meta.add_added_column_desc(); |
| 2222 | column_desc->CopyFrom(col); |
| 2223 | } else { |
| 2224 | bool do_continue = false; |
| 2225 | const auto& cols = request->column_descs(); |
| 2226 | for (const auto& col : cols) { |
| 2227 | if (table->CheckFieldExist(col.name())) { |
| 2228 | do_continue = true; |
| 2229 | break; |
| 2230 | } |
| 2231 | } |
| 2232 | if (do_continue) { |
| 2233 | continue; |
| 2234 | } |
| 2235 | for (const auto& col : cols) { |
| 2236 | fedb::common::ColumnDesc* column_desc = table_meta.add_added_column_desc(); |
| 2237 | column_desc->CopyFrom(col); |
| 2238 | } |
| 2239 | } |
| 2240 | if (request->has_version_pair()) { |
| 2241 | fedb::common::VersionPair* pair = table_meta.add_schema_versions(); |
| 2242 | pair->CopyFrom(request->version_pair()); |
| 2243 | LOG(INFO) << "add version pair"; |
| 2244 | } |
| 2245 | table->SetTableMeta(table_meta); |
| 2246 | // update TableMeta.txt |
| 2247 | std::string db_root_path; |
| 2248 | bool ok = ChooseDBRootPath(tid, pid, db_root_path); |
| 2249 | if (!ok) { |
| 2250 | response->set_code(ReturnCode::kFailToGetDbRootPath); |
| 2251 | response->set_msg("fail to get db root path"); |
no test coverage detected