OmniSciDB  04ee39c94c
LockMgr.h
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 #ifndef LOCKMGR_H
18 #define LOCKMGR_H
19 
20 #include "Catalog/Catalog.h"
22 #include "Shared/types.h"
23 
24 #include <map>
25 #include <mutex>
26 #include <tuple>
27 #include "boost/variant.hpp"
28 
29 #include <rapidjson/document.h>
30 
31 namespace Lock_Namespace {
32 using namespace rapidjson;
33 using LockTypeContainer = boost::variant<mapd_shared_lock<mapd_shared_mutex>,
34  mapd_unique_lock<mapd_shared_mutex>>;
35 
37 
38 template <typename MutexType, typename KeyType>
39 class LockMgr {
40  public:
41  static std::shared_ptr<MutexType> getMutex(const LockType lockType, const KeyType& key);
42 
43  private:
44  static std::mutex aMutex_;
45  static std::map<std::tuple<LockType, KeyType>, std::shared_ptr<MutexType>> mutexMap_;
46 };
47 
48 template <typename MutexType, typename KeyType>
50 template <typename MutexType, typename KeyType>
51 std::map<std::tuple<LockType, KeyType>, std::shared_ptr<MutexType>>
53 
54 template <typename MutexType, typename KeyType>
55 std::shared_ptr<MutexType> LockMgr<MutexType, KeyType>::getMutex(const LockType lock_type,
56  const KeyType& key) {
57  auto lock_key = std::make_tuple(lock_type, key);
58 
59  std::unique_lock<std::mutex> lck(aMutex_);
60  auto mit = mutexMap_.find(lock_key);
61  if (mit != mutexMap_.end()) {
62  return mit->second;
63  }
64 
65  auto tMutex = std::make_shared<MutexType>();
66  mutexMap_[lock_key] = tMutex;
67  return tMutex;
68 }
69 
71  const std::string& tableName);
72 void getTableNames(std::map<std::string, bool>& tableNames, const Value& value);
73 void getTableNames(std::map<std::string, bool>& tableNames, const std::string query_ra);
74 std::string parse_to_ra(const Catalog_Namespace::Catalog& cat,
75  const std::string& query_str,
76  const Catalog_Namespace::SessionInfo& session_info);
77 
78 template <typename MutexType>
79 std::shared_ptr<MutexType> getTableMutex(const Catalog_Namespace::Catalog& cat,
80  const std::string& tableName,
81  const Lock_Namespace::LockType lockType) {
83  lockType, getTableChunkKey(cat, tableName));
84 }
85 
86 template <typename MutexType, template <typename> class LockType>
88  const std::string& tableName,
89  const Lock_Namespace::LockType lockType) {
90  auto lock = LockType<MutexType>(*getTableMutex<MutexType>(cat, tableName, lockType));
91  // "... we need to make sure that the table (and after alter column) the columns are
92  // still around after obtaining our locks ..."
93  auto chunkKey = getTableChunkKey(cat, tableName);
94  return lock;
95 }
96 
97 template <typename MutexType>
99  const std::map<std::string, bool>& tableNames,
100  std::vector<std::shared_ptr<LockTypeContainer>>& tableLocks,
101  const Lock_Namespace::LockType lockType) {
102  for (const auto& tableName : tableNames) {
103  if (tableName.second) {
104  tableLocks.emplace_back(std::make_shared<LockTypeContainer>(
105  getTableLock<MutexType, mapd_unique_lock>(cat, tableName.first, lockType)));
106  } else {
107  tableLocks.emplace_back(std::make_shared<LockTypeContainer>(
108  getTableLock<MutexType, mapd_shared_lock>(cat, tableName.first, lockType)));
109  }
110  }
111 }
112 
113 template <typename MutexType>
115  const std::string& query_ra,
116  std::vector<std::shared_ptr<LockTypeContainer>>& tableLocks,
117  const Lock_Namespace::LockType lockType) {
118  // parse ra to learn involved table names
119  std::map<std::string, bool> tableNames;
120  getTableNames(tableNames, query_ra);
121  getTableLocks<MutexType>(cat, tableNames, tableLocks, lockType);
122 }
123 
124 } // namespace Lock_Namespace
125 
126 #endif // LOCKMGR_H
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
std::shared_ptr< MutexType > getTableMutex(const Catalog_Namespace::Catalog &cat, const std::string &tableName, const Lock_Namespace::LockType lockType)
Definition: LockMgr.h:79
LockType< MutexType > getTableLock(const Catalog_Namespace::Catalog &cat, const std::string &tableName, const Lock_Namespace::LockType lockType)
Definition: LockMgr.h:87
ChunkKey getTableChunkKey(const Catalog_Namespace::Catalog &cat, const std::string &tableName)
Definition: LockMgr.cpp:66
static std::shared_ptr< MutexType > getMutex(const LockType lockType, const KeyType &key)
Definition: LockMgr.h:55
static std::mutex aMutex_
Definition: LockMgr.h:44
This file contains the class specification and related data structures for Catalog.
boost::variant< mapd_shared_lock< mapd_shared_mutex >, mapd_unique_lock< mapd_shared_mutex > > LockTypeContainer
Definition: LockMgr.h:34
static std::map< std::tuple< LockType, KeyType >, std::shared_ptr< MutexType > > mutexMap_
Definition: LockMgr.h:45
void getTableLocks(const Catalog_Namespace::Catalog &cat, const std::map< std::string, bool > &tableNames, std::vector< std::shared_ptr< LockTypeContainer >> &tableLocks, const Lock_Namespace::LockType lockType)
Definition: LockMgr.h:98
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
std::vector< int > ChunkKey
Definition: types.h:35