/Users/deen/code/yugabyte-db/src/yb/yql/cql/ql/ql_session.h
Line | Count | Source |
1 | | //-------------------------------------------------------------------------------------------------- |
2 | | // Copyright (c) YugaByte, Inc. |
3 | | // |
4 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
5 | | // in compliance with the License. You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
10 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
11 | | // or implied. See the License for the specific language governing permissions and limitations |
12 | | // under the License. |
13 | | // |
14 | | // |
15 | | // This class represents a QL session of a client connection (e.g. CQL client connection). |
16 | | //-------------------------------------------------------------------------------------------------- |
17 | | #ifndef YB_YQL_CQL_QL_QL_SESSION_H_ |
18 | | #define YB_YQL_CQL_QL_QL_SESSION_H_ |
19 | | |
20 | | #include <memory> |
21 | | #include <string> |
22 | | |
23 | | #include <boost/thread/locks.hpp> |
24 | | #include <gflags/gflags.h> |
25 | | |
26 | | #include "yb/util/shared_lock.h" |
27 | | |
28 | | DECLARE_bool(use_cassandra_authentication); |
29 | | |
30 | | namespace yb { |
31 | | namespace ql { |
32 | | |
33 | | static const char* const kUndefinedKeyspace = ""; // Must be empty string. |
34 | | static const char* const kUndefinedRoleName = ""; // Must be empty string. |
35 | | |
36 | | class QLSession { |
37 | | public: |
38 | | // Public types. |
39 | | typedef std::shared_ptr<QLSession> SharedPtr; |
40 | | typedef std::shared_ptr<const QLSession> SharedPtrConst; |
41 | | |
42 | | // Constructors. |
43 | 9.40k | QLSession() : user_authenticated_(!FLAGS_use_cassandra_authentication) {} |
44 | | |
45 | 8.96k | virtual ~QLSession() {} |
46 | | |
47 | | // Access functions for current keyspace. It can be accessed by multiple calls in parallel so |
48 | | // they need to be thread-safe for shared reads / exclusive writes. |
49 | 70.7k | std::string current_keyspace() const { |
50 | 70.7k | SharedLock<std::shared_timed_mutex> l(current_keyspace_mutex_); |
51 | 70.7k | return current_keyspace_; |
52 | 70.7k | } |
53 | | |
54 | 5.17k | void set_current_keyspace(const std::string& keyspace) { |
55 | 5.17k | boost::lock_guard<std::shared_timed_mutex> l(current_keyspace_mutex_); |
56 | 5.17k | current_keyspace_ = keyspace; |
57 | 5.17k | } |
58 | | |
59 | | // Access functions for current role_name. It can be accessed by multiple calls in parallel so |
60 | | // they need to be thread-safe for shared reads / exclusive writes. |
61 | 32.0k | std::string current_role_name() const { |
62 | 32.0k | SharedLock<std::shared_timed_mutex> l(current_role_name_mutex_); |
63 | 32.0k | return current_role_name_; |
64 | 32.0k | } |
65 | | |
66 | 6.16k | void set_current_role_name(const std::string& role_name) { |
67 | 6.16k | boost::lock_guard<std::shared_timed_mutex> l(current_role_name_mutex_); |
68 | 6.16k | current_role_name_ = role_name; |
69 | 6.16k | } |
70 | | |
71 | | // Access functions for 'user is authenticated' flag. It can be accessed by multiple calls in |
72 | | // parallel so they need to be thread-safe for shared reads / exclusive writes. |
73 | 67.3k | bool is_user_authenticated() const { |
74 | 67.3k | SharedLock<std::shared_timed_mutex> l(user_authenticated_mutex_); |
75 | 67.3k | return user_authenticated_; |
76 | 67.3k | } |
77 | | |
78 | 6.20k | void set_user_authenticated(bool authenticated = true) { |
79 | 6.20k | boost::lock_guard<std::shared_timed_mutex> l(user_authenticated_mutex_); |
80 | 6.20k | user_authenticated_ = authenticated; |
81 | 6.20k | } |
82 | | |
83 | | private: |
84 | | // Mutexes to protect access to the class fields. |
85 | | mutable std::shared_timed_mutex current_keyspace_mutex_; |
86 | | mutable std::shared_timed_mutex current_role_name_mutex_; |
87 | | mutable std::shared_timed_mutex user_authenticated_mutex_; |
88 | | // Current keyspace. |
89 | | std::string current_keyspace_ = kUndefinedKeyspace; |
90 | | // TODO (Bristy) : After Login has been done, test this. |
91 | | std::string current_role_name_ = kUndefinedRoleName; |
92 | | |
93 | | bool user_authenticated_; |
94 | | }; |
95 | | |
96 | | } // namespace ql |
97 | | } // namespace yb |
98 | | |
99 | | #endif // YB_YQL_CQL_QL_QL_SESSION_H_ |