OmniSciDB  04ee39c94c
LockMgr.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "LockMgr.h"
20 #include "gen-cpp/CalciteServer.h"
21 
22 namespace Lock_Namespace {
23 
24 using namespace rapidjson;
25 
26 void getTableNames(std::map<std::string, bool>& tableNames, const std::string query_ra) {
27  rapidjson::Document query_ast;
28  query_ast.Parse(query_ra.c_str());
29  CHECK(!query_ast.HasParseError());
30  CHECK(query_ast.IsObject());
31  getTableNames(tableNames, query_ast);
32 }
33 
34 void getTableNames(std::map<std::string, bool>& tableNames, const Value& value) {
35  if (value.IsArray()) {
36  for (SizeType i = 0; i < value.Size(); ++i) {
37  getTableNames(tableNames, value[i]);
38  }
39  return;
40  } else if (value.IsObject()) {
41  for (auto mit = value.MemberBegin(); mit != value.MemberEnd(); ++mit) {
42  getTableNames(tableNames, mit->value);
43  }
44  } else {
45  return;
46  }
47 
48  if (value.FindMember("rels") == value.MemberEnd()) {
49  return;
50  }
51  const auto& rels = value["rels"];
52  CHECK(rels.IsArray());
53  for (SizeType i = 0; i < rels.Size(); ++i) {
54  const auto& rel = rels[i];
55  const auto& relop = json_str(rel["relOp"]);
56  if (rel.FindMember("table") != rel.MemberEnd()) {
57  if ("EnumerableTableScan" == relop || "LogicalTableModify" == relop) {
58  const auto t = rel["table"].GetArray();
59  CHECK(t[1].IsString());
60  tableNames[t[1].GetString()] |= "LogicalTableModify" == relop;
61  }
62  }
63  }
64 }
65 
67  const std::string& tableName) {
68  if (const auto tdp = cat.getMetadataForTable(tableName, false)) {
69  ChunkKey chunk_key{cat.getCurrentDB().dbId, tdp->tableId};
70  return chunk_key;
71  } else {
72  throw std::runtime_error("Table " + tableName + " does not exist.");
73  }
74 }
75 
76 std::string parse_to_ra(const Catalog_Namespace::Catalog& cat,
77  const std::string& query_str,
78  const Catalog_Namespace::SessionInfo& session_info) {
79  return cat.getCalciteMgr()
80  ->process(session_info, query_str, {}, true, false, false)
81  .plan_result;
82 }
83 } // namespace Lock_Namespace
void getTableNames(std::map< std::string, bool > &tableNames, const std::string query_ra)
Definition: LockMgr.cpp:26
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:81
const TableDescriptor * getMetadataForTable(const std::string &tableName, const bool populateFragmenter=true) const
Returns a pointer to a const TableDescriptor struct matching the provided tableName.
std::shared_ptr< Calcite > getCalciteMgr() const
Definition: Catalog.h:178
const std::string json_str(const rapidjson::Value &obj) noexcept
Definition: JsonAccessors.h:44
ChunkKey getTableChunkKey(const Catalog_Namespace::Catalog &cat, const std::string &tableName)
Definition: LockMgr.cpp:66
const DBMetadata & getCurrentDB() const
Definition: Catalog.h:176
std::string parse_to_ra(const Catalog_Namespace::Catalog &cat, const std::string &query_str, const Catalog_Namespace::SessionInfo &session_info)
Definition: LockMgr.cpp:76
#define CHECK(condition)
Definition: Logger.h:187
std::vector< int > ChunkKey
Definition: types.h:35