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