SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
Shell_Ranger.cc
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 
11 
12 
13 namespace SWC { namespace Utils { namespace shell {
14 
15 
17  : Interface("\033[32mSWC-DB(\033[36mrgr\033[32m)\033[33m> \033[00m",
18  "/tmp/.swc-cli-ranger-history",
19  CLI::RANGER),
20  clients(client::Clients::make(
21  *Env::Config::settings(),
22  Comm::IoContext::make("Rgr", 8),
23  nullptr, // std::make_shared<client::ManagerContext>()
24  nullptr // std::make_shared<client::RangerContext>()
25  )->init()
26  ) {
27 
28  add_option(
29  "report-resources",
30  {"report Ranger resources",
31  "report-resources endpoint/hostname[|port];"},
32  [ptr=this](std::string& cmd){return ptr->report_resources(cmd);},
33  "(?i)^(report-resources)"
34  );
35  add_option(
36  "report",
37  {"report loaded column or all and opt. ranges on a Ranger",
38  "report [cid=NUM/column='name'] [ranges] endpoint/hostname[|port];"},
39  [ptr=this](std::string& cmd){return ptr->report(cmd);},
40  "(?i)^(report)"
41  );
42 }
43 
45  clients->stop();
46 }
47 
48 bool Rgr::read_endpoint(std::string& host_or_ips,
49  Comm::EndPoints& endpoints) {
50  host_or_ips.erase(
51  std::remove_if(host_or_ips.begin(), host_or_ips.end(),
52  [](unsigned char x){return std::isspace(x);} ),
53  host_or_ips.cend()
54  );
55  std::string message;
56 
57  uint32_t port = 0;
58  size_t at = host_or_ips.find_first_of("|");
59  if(at != std::string::npos) {
60  std::string port_str = host_or_ips.substr(at+1);
61  host_or_ips = host_or_ips.substr(0, at);
62  try {
63  if((port = std::stoul(port_str)) > UINT16_MAX )
64  err = ERANGE;
65  } catch(...) {
67  err = e.code();
68  }
69  if(err) {
70  message.append("Bad value='"+port_str+ "' for port\n");
71  return error(message);
72  }
73  } else {
74  port = Env::Config::settings()->get_i16("swc.rgr.port");
75  }
76  if(!port) {
77  message.append("Bad value='"+std::to_string(port)+ "' for port\n");
78  return error(message);
79  }
80 
81  Config::Strings ips;
82  std::string host;
83  if(Comm::Resolver::is_ipv4_address(host_or_ips) ||
85  ips.push_back(host_or_ips);
86  else
87  host = host_or_ips;
88 
89  try {
90  endpoints = Comm::Resolver::get_endpoints(port, ips, host, {}, false);
91  if(endpoints.empty()) {
92  message.append("Empty endpoints\n");
93  err = EINVAL;
94  }
95  } catch(...) {
97  err = e.code();
98  message.append(e.what());
99  message.append("\n");
100  }
101  return err ? error(message) : true;
102 }
103 
104 bool Rgr::report_resources(std::string& cmd) {
105 
106  size_t at = cmd.find_first_of(" ");
107  std::string host_or_ips = cmd.substr(at+1);
108 
109  Comm::EndPoints endpoints;
110  bool r = read_endpoint(host_or_ips, endpoints);
111  if(err)
112  return r;
113 
116  clients,
117  endpoints,
118  [this, await=&res]
119  (const Comm::client::ConnQueue::ReqBase::Ptr&, const int& error,
121  if(!(err = error)) {
122  SWC_PRINT << "";
125  }
126  await->acknowledge();
127  }
128  );
129  res.wait();
130 
131  if(err) {
132  std::string message(Error::get_text(err));
133  message.append("\n");
134  return error(message);
135  }
136  return true;
137 }
138 
139 bool Rgr::report(std::string& cmd) {
140  std::string message;
141 
142  size_t at = cmd.find_first_of(" ");
143  cmd = cmd.substr(at+1);
145 
146  client::SQL::Reader reader(cmd, message);
147  while(reader.found_space());
148  if(reader.found_token("cid", 3)) {
149  reader.expect_eq();
150  if(!reader.err) {
151  bool was_set = false;
152  reader.read_uint64_t(cid, was_set);
153  }
154  if(reader.err)
155  return error(message);
156 
157  } else if(reader.found_token("column", 6)) {
158  reader.expect_eq();
159  if(reader.err)
160  return error(message);
161 
162  std::string col_name;
163  reader.read(col_name, " ");
164  if(col_name.empty())
165  return error("Missing Column Name");
166 
167  auto schema = reader.get_schema(clients, col_name);
168  if(reader.err)
169  return error(message);
170  cid = schema->cid;
171  }
172 
173  while(reader.found_space());
174  bool ranges = reader.found_token("ranges", 6);
175 
176  while(reader.found_space());
177  std::string host_or_ips(reader.ptr, reader.remain);
178  Comm::EndPoints endpoints;
179  bool r = read_endpoint(host_or_ips, endpoints);
180  if(err)
181  return r;
182 
183  auto func = cid == DB::Schema::NO_CID
188 
190 
191  switch(func) {
194  clients,
195  endpoints,
196  [this, await=&res]
197  (const Comm::client::ConnQueue::ReqBase::Ptr&, const int& error,
199  if(!(err = error)) {
200  SWC_PRINT << "";
203  }
204  await->acknowledge();
205  }
206  );
207  break;
208  }
211  clients,
212  endpoints,
213  [this, await=&res]
214  (const Comm::client::ConnQueue::ReqBase::Ptr&, const int& error,
216  if(!(err = error)) {
217  SWC_PRINT << "";
220  }
221  await->acknowledge();
222  }
223  );
224  break;
225  }
228  clients,
229  endpoints,
230  cid,
231  [this, await=&res]
232  (const Comm::client::ConnQueue::ReqBase::Ptr&, const int& error,
234  if(!(err = error)) {
235  SWC_PRINT << "";
238  }
239  await->acknowledge();
240  }
241  );
242  break;
243  }
246  clients,
247  endpoints,
248  cid,
249  [this, await=&res]
250  (const Comm::client::ConnQueue::ReqBase::Ptr&, const int& error,
252  if(!(err = error)) {
253  SWC_PRINT << "";
256  }
257  await->acknowledge();
258  }
259  );
260  break;
261  }
262  default: {
264  res.acknowledge();
265  break;
266  }
267  }
268 
269  res.wait();
270 
271  if(err) {
272  message.append(Error::get_text(err));
273  message.append("\n");
274  return error(message);
275  }
276  return true;
277 }
278 
279 
280 
281 }}} // namespace Utils::shell
SWC::Utils::shell::Interface::add_option
void add_option(const char *a_name, Core::Vector< std::string > &&a_desc, OptCall_t &&a_call, const char *a_re)
Definition: Shell.cc:249
SWC::Error::Exception::code
constexpr SWC_CAN_INLINE int code() const noexcept
Definition: Exception.h:51
SWC_LOG_OSTREAM
#define SWC_LOG_OSTREAM
Definition: Logger.h:44
SWC::client::SQL::Reader::expect_eq
void expect_eq()
Definition: Reader.cc:82
SWC::DB::SchemaPrimitives::NO_CID
static constexpr const cid_t NO_CID
Definition: Schema.h:25
StateSynchronization.h
SWC::Error::get_text
const char * get_text(const int err) noexcept
Definition: Error.cc:173
SWC::Env::Config::settings
static SWC::Config::Settings::Ptr & settings()
Definition: Settings.h:128
Report.h
SWC::client::SQL::Reader::read_uint64_t
void read_uint64_t(uint64_t &value, bool &was_set, const char *stop=nullptr)
Definition: Reader.cc:274
SWC::client::SQL::Reader::remain
uint32_t remain
Definition: Reader.h:124
SWC::Utils::shell::Interface::err
int err
Definition: Shell.h:54
SWC::Utils::shell::CLI
CLI
Definition: Shell.h:28
SWC::Utils::shell::Interface::error
bool error(const std::string &message)
Definition: Shell.cc:345
SWC_PRINT
#define SWC_PRINT
Definition: Logger.h:167
SWC_PRINT_CLOSE
#define SWC_PRINT_CLOSE
Definition: Logger.h:171
SWC::client::SQL::Reader
Definition: Reader.h:28
SWC::Comm::Protocol::Rgr::Req::ReportColumnRids::request
static void request(const SWC::client::Clients::Ptr &clients, const EndPoints &endpoints, cid_t cid, Cb_t &&cb, const uint32_t timeout=10000)
Definition: Report.h:128
SWC::Utils::shell::Interface
Definition: Shell.h:41
SWC::Comm::Protocol::Rgr::Params::Report::RspColumnRids::display
void display(std::ostream &out, const std::string &offset="") const
Definition: Report.cc:92
SWC::Comm::Protocol::Rgr::Params::Report::RspRes::display
void display(std::ostream &out, const std::string &offset="") const
Definition: Report.cc:33
SWC::Comm::Protocol::Rgr::Params::Report::CIDS
@ CIDS
Definition: Report.h:23
SWC::Utils::shell::Rgr::Rgr
Rgr()
Definition: Shell_Ranger.cc:16
SWC::client::SQL::Reader::found_space
bool found_space()
Definition: Reader.cc:37
SWC::Utils::shell::Rgr::read_endpoint
bool read_endpoint(std::string &host_or_ips, Comm::EndPoints &endpoints)
Definition: Shell_Ranger.cc:48
SWC::Core::Vector::empty
constexpr SWC_CAN_INLINE bool empty() const noexcept
Definition: Vector.h:168
SWC::client::SQL::Reader::get_schema
DB::Schema::Ptr get_schema(const Clients::Ptr &clients, const std::string &col)
Definition: Reader.cc:137
SWC::Comm::Protocol::Rgr::Req::ReportColumnsRanges::request
static SWC_CAN_INLINE void request(const SWC::client::Clients::Ptr &clients, const EndPoints &endpoints, Cb_t &&cb, const uint32_t timeout=10000)
Definition: Report.h:166
SWC::Core::StateSynchronization::wait
SWC_CAN_INLINE void wait()
Definition: StateSynchronization.h:25
SWC::client::SQL::Reader::read
void read(std::string &buf, const char *stop=nullptr, bool keep_escape=false)
Definition: Reader.cc:185
SWC_CURRENT_EXCEPTION
#define SWC_CURRENT_EXCEPTION(_msg_)
Definition: Exception.h:119
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::Utils::shell::Rgr::clients
client::Clients::Ptr clients
Definition: Shell_Ranger.h:20
SWC::client::SQL::Reader::found_token
bool found_token(const char *token, uint8_t token_len)
Definition: Reader.cc:58
SWC::Comm::Protocol::Rgr::Params::Report::RspColumnRids
Definition: Report.h:106
SWC::cid_t
uint64_t cid_t
Definition: Identifiers.h:16
SWC::Comm::Protocol::Rgr::Params::Report::COLUMNS_RANGES
@ COLUMNS_RANGES
Definition: Report.h:26
SWC::Comm::Protocol::Rgr::Params::Report::RspColumnsRanges
Definition: Report.h:131
SWC::Core::Vector< EndPoint >
SWC::Comm::Resolver::is_ipv4_address
bool is_ipv4_address(const std::string &str) noexcept
Definition: Resolver.cc:141
SWC::Comm::Protocol::Rgr::Req::ReportRes::request
static void request(const SWC::client::Clients::Ptr &clients, const EndPoints &endpoints, Cb_t &&cb, const uint32_t timeout=10000)
Definition: Report.h:57
SWC::client::SQL::Reader::err
int err
Definition: Reader.h:125
SWC::client::SQL::Reader::ptr
const char * ptr
Definition: Reader.h:123
SWC::Comm::Protocol::Rgr::Params::Report::RspColumnsRanges::display
void display(std::ostream &out, bool pretty=true, const std::string &offset="") const
Definition: Report.cc:228
SWC::Error::NOT_IMPLEMENTED
@ NOT_IMPLEMENTED
Definition: Error.h:74
SWC::Utils::shell::Rgr::~Rgr
virtual ~Rgr()
Definition: Shell_Ranger.cc:44
SWC::Core::StateSynchronization
Definition: StateSynchronization.h:16
Shell_Ranger.h
SWC::Core::StateSynchronization::acknowledge
SWC_CAN_INLINE void acknowledge() noexcept
Definition: StateSynchronization.h:31
SWC::Comm::client::ConnQueueReqBase::Ptr
std::shared_ptr< ConnQueueReqBase > Ptr
Definition: ClientConnQueue.h:25
SWC::Comm::Resolver::is_ipv6_address
bool is_ipv6_address(const std::string &str) noexcept
Definition: Resolver.cc:147
SWC::Comm::Resolver::get_endpoints
EndPoints get_endpoints(uint16_t defaul_port, const Config::Strings &addrs, const std::string &host, const Networks &nets, bool srv=false)
Definition: Resolver.cc:152
SWC::Core::Vector::push_back
SWC_CAN_INLINE void push_back(ArgsT &&... args)
Definition: Vector.h:331
Reader.h
SWC::Core::to_string
SWC_CAN_INLINE std::string to_string(const BitFieldInt< T, SZ > &v)
Definition: BitFieldInt.h:263
SWC::Error::Exception::what
virtual SWC_CAN_INLINE const char * what() const noexcept override
Definition: Exception.h:56
SWC::Comm::Protocol::Rgr::Params::Report::RspRes
Definition: Report.h:53
SWC::Comm::Protocol::Rgr::Params::Report::COLUMN_RANGES
@ COLUMN_RANGES
Definition: Report.h:25
SWC::Comm::Protocol::Rgr::Req::ReportCids::request
static SWC_CAN_INLINE void request(const SWC::client::Clients::Ptr &clients, const EndPoints &endpoints, Cb_t &&cb, const uint32_t timeout=10000)
Definition: Report.h:93
SWC::Utils::shell::Rgr::report_resources
bool report_resources(std::string &cmd)
Definition: Shell_Ranger.cc:104
SWC::Comm::Protocol::Rgr::Params::Report::COLUMN_RIDS
@ COLUMN_RIDS
Definition: Report.h:24
SWC::Comm::Protocol::Rgr::Params::Report::RspCids::display
void display(std::ostream &out, const std::string &offset="") const
Definition: Report.cc:62
SWC::Comm::Protocol::Rgr::Params::Report::RspCids
Definition: Report.h:81
SWC::Utils::shell::RANGER
@ RANGER
Definition: Shell.h:32
SWC::Error::Exception
Definition: Exception.h:21
SWC::Utils::shell::Rgr::report
bool report(std::string &cmd)
Definition: Shell_Ranger.cc:139