1+ #include " runtime_config.h"
2+
3+ #include < stdexcept>
4+ #include < sstream>
5+
6+ RuntimeConfig::RuntimeConfig (
7+ std::shared_ptr<spark::connect::SparkConnectService::Stub> stub,
8+ const std::string &session_id,
9+ const std::string &user_id)
10+ : stub_(std::move(stub)), session_id_(session_id), user_id_(user_id)
11+ {
12+ }
13+
14+ spark::connect::ConfigResponse RuntimeConfig::sendConfig (
15+ const spark::connect::ConfigRequest::Operation &op) const
16+ {
17+ spark::connect::ConfigRequest request;
18+ request.set_session_id (session_id_);
19+ request.mutable_user_context ()->set_user_id (user_id_);
20+ *request.mutable_operation () = op;
21+
22+ grpc::ClientContext context;
23+ spark::connect::ConfigResponse response;
24+ grpc::Status status = stub_->Config (&context, request, &response);
25+
26+ if (!status.ok ())
27+ throw std::runtime_error (" Config RPC failed: " + status.error_message ());
28+
29+ return response;
30+ }
31+
32+ void RuntimeConfig::set (const std::string &key, const std::string &value)
33+ {
34+ spark::connect::ConfigRequest::Operation op;
35+ auto *kv = op.mutable_set ()->add_pairs ();
36+ kv->set_key (key);
37+ kv->set_value (value);
38+ sendConfig (op);
39+ }
40+
41+ void RuntimeConfig::set (const std::string &key, bool value)
42+ {
43+ set (key, value ? std::string (" true" ) : std::string (" false" ));
44+ }
45+
46+ void RuntimeConfig::set (const std::string &key, int64_t value)
47+ {
48+ set (key, std::to_string (value));
49+ }
50+
51+ void RuntimeConfig::set (const std::string &key, const char *value)
52+ {
53+ set (key, std::string (value));
54+ }
55+
56+ std::string RuntimeConfig::get (const std::string &key) const
57+ {
58+ spark::connect::ConfigRequest::Operation op;
59+ op.mutable_get ()->add_keys (key);
60+ auto response = sendConfig (op);
61+
62+ if (response.pairs_size () == 0 || !response.pairs (0 ).has_value ())
63+ throw std::runtime_error (" Config key not found: " + key);
64+
65+ return response.pairs (0 ).value ();
66+ }
67+
68+ std::string RuntimeConfig::get (const std::string &key,
69+ const std::string &default_value) const
70+ {
71+ spark::connect::ConfigRequest::Operation op;
72+ auto *gd = op.mutable_get_with_default ()->add_pairs ();
73+ gd->set_key (key);
74+ gd->set_value (default_value);
75+ auto response = sendConfig (op);
76+
77+ if (response.pairs_size () == 0 )
78+ return default_value;
79+
80+ return response.pairs (0 ).has_value () ? response.pairs (0 ).value () : default_value;
81+ }
82+
83+ std::optional<std::string> RuntimeConfig::getOption (const std::string &key) const
84+ {
85+ spark::connect::ConfigRequest::Operation op;
86+ op.mutable_get_option ()->add_keys (key);
87+ auto response = sendConfig (op);
88+
89+ if (response.pairs_size () == 0 || !response.pairs (0 ).has_value ())
90+ return std::nullopt ;
91+
92+ return response.pairs (0 ).value ();
93+ }
94+
95+ std::map<std::string, std::string> RuntimeConfig::getAll () const
96+ {
97+ spark::connect::ConfigRequest::Operation op;
98+
99+ // -------------------------------------------------
100+ // No arguments are needed, this returns all pairs
101+ // -------------------------------------------------
102+ op.mutable_get_all ();
103+ auto response = sendConfig (op);
104+
105+ std::map<std::string, std::string> result;
106+ for (const auto &pair : response.pairs ())
107+ {
108+ if (pair.has_value ())
109+ result[pair.key ()] = pair.value ();
110+ else
111+ result[pair.key ()] = " " ;
112+ }
113+ return result;
114+ }
115+
116+ void RuntimeConfig::unset (const std::string &key)
117+ {
118+ spark::connect::ConfigRequest::Operation op;
119+ op.mutable_unset ()->add_keys (key);
120+ sendConfig (op);
121+ }
122+
123+ bool RuntimeConfig::isModifiable (const std::string &key) const
124+ {
125+ spark::connect::ConfigRequest::Operation op;
126+ op.mutable_is_modifiable ()->add_keys (key);
127+ auto response = sendConfig (op);
128+
129+ if (response.pairs_size () == 0 || !response.pairs (0 ).has_value ())
130+ return false ;
131+
132+ return response.pairs (0 ).value () == " true" ;
133+ }
0 commit comments