OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TableLockMgr.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 OmniSci, 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 #pragma once
18 
20 #include <Shared/types.h>
21 
22 #include <map>
23 #include <memory>
24 #include <string>
25 
26 #include "LockMgr.h"
27 
28 #include <Catalog/Catalog.h>
29 
30 namespace Lock_Helpers {
31 
32 template <typename LOCK_TYPE, typename LOCK_MGR_TYPE>
34  const std::string& table_name) {
35  const auto chunk_key = Lock_Namespace::getTableChunkKey(cat, table_name);
36 
37  auto& table_lock_mgr = LOCK_MGR_TYPE::instance();
38  return LOCK_TYPE(table_lock_mgr.getTableMutex(chunk_key));
39 }
40 
41 } // namespace Lock_Helpers
42 
43 namespace Lock_Namespace {
44 
46 
47 using WriteLock = mapd_unique_lock<MutexType>;
48 using ReadLock = mapd_shared_lock<MutexType>;
49 
50 struct TableLock {
53 };
54 
55 template <class T>
57  public:
58  MutexType& getTableMutex(const ChunkKey table_key) {
59  std::lock_guard<std::mutex> access_map_lock(map_mutex_);
60  return table_mutex_map_[table_key];
61  }
62 
63  static void getTableLocks(const Catalog_Namespace::Catalog& cat,
64  const std::map<std::string, bool>& table_names,
65  std::vector<TableLock>& table_locks) {
66  for (const auto& table_name_itr : table_names) {
67  TableLock table_lock;
68  if (table_name_itr.second) {
69  table_lock.write_lock = T::getWriteLockForTable(cat, table_name_itr.first);
70  } else {
71  table_lock.read_lock = T::getReadLockForTable(cat, table_name_itr.first);
72  }
73  table_locks.emplace_back(std::move(table_lock));
74  }
75  }
76 
77  static void getTableLocks(const Catalog_Namespace::Catalog& cat,
78  const std::string& query_ra,
79  std::vector<TableLock>& table_locks) {
80  // parse ra to learn involved table names
81  std::map<std::string, bool> table_names;
82  getTableNames(table_names, query_ra);
83  return T::getTableLocks(cat, table_names, table_locks);
84  }
85 
87  const std::string& table_name) {
88  return Lock_Helpers::getLockForTableImpl<WriteLock, T>(cat, table_name);
89  }
90  static WriteLock getWriteLockForTable(const ChunkKey table_key) {
91  auto& table_lock_mgr = T::instance();
92  return WriteLock(table_lock_mgr.getTableMutex(table_key));
93  }
94 
96  const std::string& table_name) {
97  return Lock_Helpers::getLockForTableImpl<ReadLock, T>(cat, table_name);
98  }
99  static ReadLock getReadLockForTable(const ChunkKey table_key) {
100  auto& table_lock_mgr = T::instance();
101  return ReadLock(table_lock_mgr.getTableMutex(table_key));
102  }
103 
104  protected:
106 
107  std::mutex map_mutex_;
108  std::map<ChunkKey, MutexType> table_mutex_map_;
109 };
110 
111 class TableLockMgr : public TableLockMgrImpl<TableLockMgr> {
112  public:
113  static TableLockMgr& instance() {
114  static TableLockMgr table_lock_mgr;
115  return table_lock_mgr;
116  }
117 
118  private:
120 };
121 
122 } // namespace Lock_Namespace
static ReadLock getReadLockForTable(const Catalog_Namespace::Catalog &cat, const std::string &table_name)
Definition: TableLockMgr.h:95
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
std::map< ChunkKey, MutexType > table_mutex_map_
Definition: TableLockMgr.h:108
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:81
static TableLockMgr & instance()
Definition: TableLockMgr.h:113
ChunkKey getTableChunkKey(const Catalog_Namespace::Catalog &cat, const std::string &tableName)
Definition: LockMgr.cpp:67
static ReadLock getReadLockForTable(const ChunkKey table_key)
Definition: TableLockMgr.h:99
This file contains the class specification and related data structures for Catalog.
MutexType & getTableMutex(const ChunkKey table_key)
Definition: TableLockMgr.h:58
std::shared_timed_mutex mapd_shared_mutex
static void getTableLocks(const Catalog_Namespace::Catalog &cat, const std::string &query_ra, std::vector< TableLock > &table_locks)
Definition: TableLockMgr.h:77
static WriteLock getWriteLockForTable(const ChunkKey table_key)
Definition: TableLockMgr.h:90
mapd_shared_lock< MutexType > ReadLock
Definition: TableLockMgr.h:48
mapd_shared_mutex MutexType
Definition: TableLockMgr.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:97
LOCK_TYPE getLockForTableImpl(const Catalog_Namespace::Catalog &cat, const std::string &table_name)
Definition: TableLockMgr.h:33
mapd_unique_lock< MutexType > WriteLock
Definition: TableLockMgr.h:47
static WriteLock getWriteLockForTable(const Catalog_Namespace::Catalog &cat, const std::string &table_name)
Definition: TableLockMgr.h:86
static void getTableLocks(const Catalog_Namespace::Catalog &cat, const std::map< std::string, bool > &table_names, std::vector< TableLock > &table_locks)
Definition: TableLockMgr.h:63