OmniSciDB  c07336695a
SessionInfo.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 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 SESSION_INFO_H
18 #define SESSION_INFO_H
19 
20 #include <atomic>
21 #include <cstdint>
22 #include <ctime>
23 #include <limits>
24 #include <list>
25 #include <map>
26 #include <memory>
27 #include <mutex>
28 #include <string>
29 #include <utility>
30 #include <vector>
31 
32 #include "../QueryEngine/CompilationOptions.h"
33 #include "../SqliteConnector/SqliteConnector.h"
34 #include "LeafHostInfo.h"
35 #include "SysCatalog.h"
36 
37 namespace Importer_NS {
38 class Loader;
39 class TypedImportBuffer;
40 } // namespace Importer_NS
41 
42 namespace Catalog_Namespace {
43 
44 class Catalog;
45 
46 // this class is defined to accommodate both Thrift and non-Thrift builds.
47 class MapDHandler {
48  public:
49  virtual void prepare_columnar_loader(
50  const std::string& session,
51  const std::string& table_name,
52  size_t num_cols,
53  std::unique_ptr<Importer_NS::Loader>* loader,
54  std::vector<std::unique_ptr<Importer_NS::TypedImportBuffer>>* import_buffers);
55  virtual ~MapDHandler() {}
56 };
57 
58 /*
59  * @type SessionInfo
60  * @brief a user session
61  */
62 class SessionInfo {
63  public:
64  SessionInfo(std::shared_ptr<MapDHandler> mapdHandler,
65  std::shared_ptr<Catalog> cat,
66  const UserMetadata& user,
67  const ExecutorDeviceType t,
68  const std::string& sid)
69  : mapdHandler_(mapdHandler)
70  , catalog_(cat)
71  , currentUser_(user)
72  , executor_device_type_(t)
73  , session_id_(sid)
74  , last_used_time_(time(0))
75  , start_time_(time(0))
76  , public_session_id_(public_session_id()) {}
77  SessionInfo(std::shared_ptr<Catalog> cat,
78  const UserMetadata& user,
79  const ExecutorDeviceType t,
80  const std::string& sid)
81  : SessionInfo(std::make_shared<MapDHandler>(), cat, user, t, sid) {}
83  : mapdHandler_(s.mapdHandler_)
84  , catalog_(s.catalog_)
85  , currentUser_(s.currentUser_)
86  , executor_device_type_(static_cast<ExecutorDeviceType>(s.executor_device_type_))
87  , session_id_(s.session_id_)
88  , public_session_id_(s.public_session_id_) {}
89  MapDHandler* get_mapdHandler() const { return mapdHandler_.get(); }
90  Catalog& getCatalog() const { return *catalog_; }
91  std::shared_ptr<Catalog> get_catalog_ptr() const { return catalog_; }
92  void set_catalog_ptr(std::shared_ptr<Catalog> c) { catalog_ = c; }
93  const UserMetadata& get_currentUser() const { return currentUser_; }
95  return executor_device_type_;
96  }
97  void set_executor_device_type(ExecutorDeviceType t) { executor_device_type_ = t; }
98  std::string get_session_id() const { return session_id_; }
99  time_t get_last_used_time() const { return last_used_time_; }
100  void update_last_used_time() { last_used_time_ = time(0); }
101  void reset_superuser() { currentUser_.isSuper = currentUser_.isReallySuper; }
102  void make_superuser() { currentUser_.isSuper = true; }
103  bool checkDBAccessPrivileges(const DBObjectType& permissionType,
104  const AccessPrivileges& privs,
105  const std::string& objectName = "") const;
106  time_t get_start_time() const { return start_time_; }
107  std::string const& get_public_session_id() const { return public_session_id_; }
108  operator std::string() const { return public_session_id_; }
109 
110  private:
111  std::shared_ptr<MapDHandler> mapdHandler_;
112  std::shared_ptr<Catalog> catalog_;
114  std::atomic<ExecutorDeviceType> executor_device_type_;
115  const std::string session_id_;
116  std::atomic<time_t> last_used_time_; // for tracking active session duration
117  std::atomic<time_t> start_time_; // for invalidating session after tolerance period
118  const std::string public_session_id_;
119  std::string public_session_id() const;
120 };
121 
122 std::ostream& operator<<(std::ostream& os, const SessionInfo& session_info);
123 
124 } // namespace Catalog_Namespace
125 
126 #endif /* SESSION_INFO_H */
time_t get_last_used_time() const
Definition: SessionInfo.h:99
std::shared_ptr< Catalog > catalog_
Definition: SessionInfo.h:112
const std::string session_id_
Definition: SessionInfo.h:115
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:81
DBObjectType
Definition: DBObject.h:42
ExecutorDeviceType
std::ostream & operator<<(std::ostream &o, CommandHistoryFileImpl< ENV_RESOLVER > const &cmd_file)
void c(const std::string &query_string, const ExecutorDeviceType device_type)
const std::string public_session_id_
Definition: SessionInfo.h:118
std::shared_ptr< Catalog > get_catalog_ptr() const
Definition: SessionInfo.h:91
std::atomic< time_t > last_used_time_
Definition: SessionInfo.h:116
std::shared_ptr< MapDHandler > mapdHandler_
Definition: SessionInfo.h:111
SessionInfo(const SessionInfo &s)
Definition: SessionInfo.h:82
const UserMetadata & get_currentUser() const
Definition: SessionInfo.h:93
std::string get_session_id() const
Definition: SessionInfo.h:98
std::atomic< time_t > start_time_
Definition: SessionInfo.h:117
SessionInfo(std::shared_ptr< MapDHandler > mapdHandler, std::shared_ptr< Catalog > cat, const UserMetadata &user, const ExecutorDeviceType t, const std::string &sid)
Definition: SessionInfo.h:64
std::atomic< ExecutorDeviceType > executor_device_type_
Definition: SessionInfo.h:114
This file contains the class specification and related data structures for SysCatalog.
SessionInfo(std::shared_ptr< Catalog > cat, const UserMetadata &user, const ExecutorDeviceType t, const std::string &sid)
Definition: SessionInfo.h:77
Catalog & getCatalog() const
Definition: SessionInfo.h:90
MapDHandler * get_mapdHandler() const
Definition: SessionInfo.h:89
void set_executor_device_type(ExecutorDeviceType t)
Definition: SessionInfo.h:97
void set_catalog_ptr(std::shared_ptr< Catalog > c)
Definition: SessionInfo.h:92
std::string const & get_public_session_id() const
Definition: SessionInfo.h:107
TSessionId session
const ExecutorDeviceType get_executor_device_type() const
Definition: SessionInfo.h:94