#include #include #include #include #include #include #include "dgrid.h" #include "dgrid_config.h" #include "dgrid_client.h" #include "dgrid_logger.h" #include "json.hpp" using json = nlohmann::json; #define MIGRATION_TABLE_SIZE_DEFAULT 10 #define DEFAULT_DGRID_SERVER_ADDR "127.0.0.1" #define GLOBAL_PSEUDO_SESSION "global" using namespace std; using namespace std::chrono; using namespace DGrid; #define COL_RED "\E[31;60m" #define COL_GRN "\E[32;60m" #define COL_YEL "\E[33;60m" #define COL_CYAN "\E[36;60m" #define COL_WHITE "\E[39;60m" #define COL_NORM "\E[0m" DGridResult rc; DGridDataClient* client = nullptr; bool show_stats = false; bool show_all_childs = false; bool show_table_data = false; void dumpRow(const DGrid::TableResponseRowData& row) { // cout << "\trc: " << rc << ", rc str: "<< DGridResultString(rc) << ", error_string: '" << client->last_error() << "'\n"; cout << "#" << row.id_record << " [" << row.index << "]: "; for(auto s: row.data) { cout << " '" << s << "'"; }; cout << "\n"; } template std::basic_ostream& operator<<(std::basic_ostream& s, const TableID& o) { s << raw_to_hexstring(o); return s; } void remove_kv(const std::string& session, const std::string& theme, const std::string& key) { if(session == GLOBAL_PSEUDO_SESSION) rc = client->del_global_value(theme, key); else rc = client->del_session_value(theme, key); if(DGRID_OK == rc) cout << COL_GRN << "Key removed: " << session << "/" << theme << "/" << key << COL_NORM << "\n"; else cout << COL_RED << DGridResultString(rc) << ": '" << client->last_error() << "'" << COL_NORM << "\n"; } void clear_table(const TableID& id) { rc = client->clearTable(id); if(DGRID_OK == rc) cout << COL_GRN << "Table cleared: " << id << COL_NORM << "\n"; else cout << COL_RED << DGridResultString(rc) << ": '" << client->last_error() << "'" << COL_NORM << "\n"; } void clear_table(const std::string& session, const std::string& tspace, const std::string& tname) { TableID id; if(DGRID_OK == client->getTableID(tspace, tname, id)) clear_table(id); } void show_kv(const std::string& session, const std::string& theme, const std::string& key) { std::string value; if(session == GLOBAL_PSEUDO_SESSION) rc = client->get_global_value(theme, key, value); else rc = client->get_session_value(theme, key, value); if (DGRID_OK == rc) cout << COL_GRN << "session: '" << session << "'" << ", theme: '" << theme << "'" << ", key: '" << key << COL_NORM << "'\n" << "value: '" << COL_YEL << value << COL_NORM "'\n"; else cout << COL_RED << "error: '" << client->last_error() << "'" << COL_NORM << "\n"; } void dump_table(const TableID& id_table, const std::string& fname) { uint64_t count; TableStructureData structure; TableResponseRowData row; TableResponseData rows; json json_fields; json json_fields_types; json json_keys; json json_data; json json_table; std::string raw_data; std::ofstream file; cout << COL_GRN << "Dumping table " << id_table << " to file '" << fname << "'" << COL_NORM << "...\n"; if(DGRID_OK != client->getTableStructure(id_table, structure)) { cout << COL_RED << "error: '" << client->last_error() << "'" << COL_NORM << "\n"; return; } cout << "table type: " << tableType2string(structure.type) << "\n"; cout << "fields:"; for(size_t f=0; fgetTableSize(id_table, count)) { cout << COL_RED << "error: '" << client->last_error() << "'" << COL_NORM << "\n"; return; } cout << "rows count: " << count << "\n"; cout << "reading table...\n"; if(DGRID_OK != client->selectAllRows(id_table, rows)) { cout << COL_RED << "error: '" << client->last_error() << "'" << COL_NORM << "\n"; return; } cout << "rows received: " << rows.rows.size() << "\n"; cout << "serializing data...\n"; for(size_t i=0; i values; cout << COL_GRN << "Restoring table " << id_table << " from file '" << fname << "'" << COL_NORM << "...\n"; if(DGRID_OK != client->getTableStructure(id_table, structure)) { cout << COL_RED << "error: '" << client->last_error() << "'" << COL_NORM << "\n"; return; } cout << "table type: " << tableType2string(structure.type) << "\n"; cout << "fields:"; for(auto f: structure.fields) cout << " '" << f << "' "; cout << "\n"; cout << "key fields:"; for(auto f: structure.keys) cout << " '" << f << "' "; cout << "\n"; cout << "reading file...\n"; file.open(fname, std::ios::in); if (!file) { cout << COL_RED << "Can't open file: " << strerror(errno) << COL_NORM << "\n"; return; } else { size_t size = file.rdbuf()->in_avail(); raw_data.resize(size); file.read(const_cast (raw_data.c_str()), size); } file.close(); // cout << COL_YEL << "JSON:" << COL_NORM << "\n" << raw_data << "\n"; cout << "parsing data...\n"; json_table = json::parse(raw_data); if(!json_table.contains("fields")) { cout << COL_RED << "No 'fields' in input JSON" << COL_NORM << "\n"; return; } if(!json_table.contains("data")) { cout << COL_RED << "No 'data' in input JSON" << COL_NORM << "\n"; return; } json_fields = json_table.at("fields"); json_data = json_table.at("data"); cout << "clearing table...\n"; if(DGRID_OK != client->clearTable(id_table)) { cout << COL_RED << "error: '" << client->last_error() << "'" << COL_NORM << "\n"; return; } cout << "writing data to table (" << json_data.size() << " rows)...\n"; values.resize(structure.fields.size()); for(size_t i=0; iappendRow(id_table, values, id_record)) { cout << COL_RED << "error: '" << client->last_error() << "'" << COL_NORM << "\n"; } } cout << COL_GRN << "DONE" << COL_NORM << "\n"; } void dump_table(const std::string& session, const std::string& tspace, const std::string& tname, const std::string& fname) { TableID id; if(DGRID_OK == client->getTableID(tspace, tname, id)) dump_table(id, fname); else cout << COL_RED << "error: '" << client->last_error() << "'" << COL_NORM << "\n"; } void restore_table(const std::string& session, const std::string& tspace, const std::string& tname, const std::string& fname) { TableID id; if(DGRID_OK == client->getTableID(tspace, tname, id)) restore_table(id, fname); else cout << COL_RED << "error: '" << client->last_error() << "'" << COL_NORM << "\n"; } void show_table(const TableID& id, int64_t offset, int64_t limit) { uint64_t count; TableResponseRowData row; TableResponseData rows; TableStructureData structure; rc = client->getTableSize(id, count); if(DGRID_OK != rc) { cout << "\trc: " << rc << ", rc str: "<< DGridResultString(rc) << ", error_string: '" << client->last_error() << "'\n"; return; } cout << COL_GRN << "Table " << id << COL_NORM << "\n" << "rows count: " << count << "\n"; if (limit<=0) limit = count; rc = client->getTableStructure(id, structure); if(DGRID_OK != rc) { cout << "\trc: " << rc << ", rc str: "<< DGridResultString(rc) << ", error_string: '" << client->last_error() << "'\n"; return; } cout << "table type: " << tableType2string(structure.type) << "\n"; cout << "fields:"; if(structure.fields.size() != structure.fields_len.size()) { cout << COL_RED << "Structure error: fields.size != fields_len.size" << COL_NORM << "\n"; return; } if(structure.fields.size() != structure.fields_type.size()) { cout << COL_RED << "Structure error: fields.size != fields_type.size" << COL_NORM << "\n"; return; } for(size_t i=0; i< structure.fields.size(); i++) cout << " '" << structure.fields[i] << "' (" << fieldType2string(structure.fields_type[i]) << ", "<< structure.fields_len[i] << ") "; cout << "\n"; cout << "key fields:"; for(auto f: structure.keys) cout << " '" << f << "' "; cout << "\n"; if (!show_table_data || !count) return; if (TABLE_HASHED == structure.type) { if (count > (uint64_t)limit) { cout << "Too large hashed table\n"; return; } rc = client->selectAllRows(id, rows); } else { offset++; rc = client->selectRows(id, offset, offset+limit - 1, rows); } if(DGRID_OK != rc) { cout << "\trc: " << rc << ", rc str: "<< DGridResultString(rc) << ", error_string: '" << client->last_error() << "'\n"; return; } cout << COL_YEL << "Table data: " << COL_NORM << "\n"; for(size_t i=0; igetTableID(tspace, tname, id)) show_table(id, offset, limit); else cout << COL_RED << "error: '" << client->last_error() << "'" << COL_NORM << "\n"; } void show_stats_kv(const std::string& session = "", const std::string& theme = "") { cout << COL_GRN << "K-V store" << COL_NORM << "\n"; std::vector sessions; if(session.empty()) { if(DGRID_OK != client->stats_KVSessionsList(sessions)) return; cout << "sessions: " << sessions.size() << "\n"; sessions.push_back(""); } else { sessions.push_back(session); } for(std::string s: sessions) { if(s == GLOBAL_PSEUDO_SESSION) s = ""; cout << " > session '" << (s.empty() ? GLOBAL_PSEUDO_SESSION : s) << "'"; std::vector themes; if (theme.empty()) { if(DGRID_OK != client->stats_KVThemesList(s, themes)) { cout << "\n"; continue; } } else { themes.push_back(theme); } cout << ", themes: " << themes.size() << "\n"; if(show_all_childs || !session.empty()) { for(std::string t: themes) { std::vector keys; cout << " > theme '" << t << "'"; if(DGRID_OK != client->stats_KVKeysList(s, t, keys)) { cout << "\n"; continue; } cout << ", keys: " << keys.size() << "\n"; if(show_all_childs || !theme.empty()) { for(std::string k: keys) cout << " '" << k << "'\n"; } } } } } void show_stats_tables(const std::string& session = "", const std::string& space = "") { cout << COL_GRN << "Tables store" << COL_NORM << "\n"; std::vector sessions; if(session.empty()) { if(DGRID_OK != client->stats_TableSessionsList(sessions)) return; cout << "sessions: " << sessions.size() << "\n"; } else { sessions.push_back(session); } for(std::string s: sessions) { if(s == GLOBAL_PSEUDO_SESSION) s = ""; cout << " > session '" << (s.empty() ? GLOBAL_PSEUDO_SESSION : s) << "'"; client->set_session(s); std::vector spaces; if(space.empty() && !s.empty()) { if(DGRID_OK != client->stats_TableSpacesList(s == GLOBAL_PSEUDO_SESSION ? "" : s, spaces)) continue; } else { spaces.push_back(space); } cout << ", spaces: " << spaces.size() << "\n"; if(show_all_childs || !session.empty()) { for(std::string ts: spaces) { cout << " > space '" << (ts.empty() ? GLOBAL_PSEUDO_SESSION : ts) << "'"; std::vector tables; if(DGRID_OK != client->stats_TablesList(s, ts, tables)) { cout << "\n"; continue; } cout << ", tables: " << tables.size() << "\n"; if(show_all_childs || !space.empty()) { uint64_t count(0); for(std::string t: tables) { cout << " '" << t << "'"; rc = client->getTableSize(ts, t, count); cout << " ("<< count << " rows)\n"; } } } } } } int user_management(const std::string& mgt_string) { std::string action; std::string username; size_t pos = mgt_string.find_first_of(":"); action = mgt_string.substr(0, pos); cout << "User MGT command: '" << action << "'"; if(pos != std::string::npos) { username = mgt_string.substr(pos+1); cout << " (user = '" << username << "')"; } cout << "\n"; if(username.empty()) { cout << "User name: "; cin >> username; } if(action == "create" || action == "update") { std::string passwd; cout << "password for user '" << username << "': "; cin >> passwd; if(DGRID_OK != client->user_create(username, passwd)) { cout << "user update failed: " << client->last_error() << "\n"; return 2; } cout << "User updated: '" << username << "'\n"; return 0; } else if (action == "delete") { if(DGRID_OK != client->user_delete(username)) { cout << "delete failed: " << client->last_error() << "\n"; return 2; } cout << "User deleted: '" << username << "'\n"; return 0; } else { cout << "Invalid user mgt action '" << action << "'\n"; return 1; } } void show_help() { cout << "== usage:\n"; cout << "-H, --help show this help\n"; cout << "-D, --debug set debug level\n"; cout << "-h, --host dgridd addr (default: " << DEFAULT_DGRID_SERVER_ADDR << ")\n"; cout << "-p, --port dgridd port (default: " << DEFAULT_DGRID_SERVER_PORT << ")\n"; cout << "-S, --stats show stats\n"; cout << "-a, --all show add childs\n"; cout << "-s, --session show stats for session\n"; cout << "-T, --theme list K-V in selected theme (session must me defined)\n"; cout << "-k, --key show selected key (session/theme must me defined)\n"; cout << "-P, --tspace list tables in selected space (session must me defined)\n"; cout << "-t, --table show table by name (session/tspace must me defined)\n"; cout << "-i, --tableid show table by id\n"; cout << "-d, --data show table data (structure only displayed by default)\n"; cout << "-o, --offset show rows starting from offset\n"; cout << "-l, --limit limit rows count\n"; cout << "-c, --clear remove key/clear table\n"; cout << "-u, --dump dump data to file\n"; cout << "-r, --restore restore data from file\n"; cout << "== auth/user mgt:\n"; cout << "-U, --user : connect as user\n"; cout << "-C, --cluster auth with cluster_key\n"; cout << "-M, --usermgt : user management: action = create/update/delete\n"; } static struct option long_options[] = { {"help", 0, NULL, 'H'}, {"debug", 1, NULL, 'D'}, {"host", 0, NULL, 'h'}, {"port", 0, NULL, 'p'}, {"stats", 0, NULL, 'S'}, {"all", 0, NULL, 'a'}, {"session", 1, NULL, 's'}, {"theme", 1, NULL, 'T'}, {"key", 1, NULL, 'k'}, {"tspace", 1, NULL, 'P'}, {"table", 1, NULL, 't'}, {"tableid", 1, NULL, 'i'}, {"data", 0, NULL, 'd'}, {"offset", 1, NULL, 'o'}, {"limit", 1, NULL, 'l'}, {"clear", 0, NULL, 'c'}, {"dump", 1, NULL, 'u'}, {"restore", 1, NULL, 'r'}, {"user", 1, NULL, 'U'}, {"cluster", 1, NULL, 'C'}, {"usermgt", 1, NULL, 'M'}, {0,0,0,0} }; int main(int ac, char **av) { int debug_level = LOG_INFO; std::string server_addr = DEFAULT_DGRID_SERVER_ADDR; uint16_t server_port = DEFAULT_DGRID_SERVER_PORT; std::string auth_data; std::string cluster_key; std::string user_mgt; std::string session; std::string theme, key; std::string tspace, tname; std::string dump_fname, restore_fname; TableID table_id; int64_t show_offset(0); int64_t show_limit(-1); bool clear_data = false; std::string value; while (1) { char c = getopt_long(ac, av, "HD:h:p:Sas:T:k:P:t:i:l:o:dcu:r:U:C:M:", long_options, nullptr); if (c == -1) break; switch(c) { case 'H': show_help(); exit(1); case 'D': debug_level = atoi(optarg); break; case 'h': server_addr = std::string(optarg); break; case 'p': server_port = atoi(optarg); break; case 'S': show_stats = true; break; case 'a': show_all_childs = true; break; case 's': session = std::string(optarg); break; case 'T': theme = std::string(optarg); break; case 'k': key = std::string(optarg); break; case 'P': tspace = std::string(optarg); break; case 't': tname = std::string(optarg); break; case 'i': table_id = hexstring_to_raw(optarg); break; case 'd': show_table_data = true; break; case 'o': show_offset = atoll(optarg); break; case 'l': show_limit = atoll(optarg); break; case 'c': clear_data = true; break; case 'u': dump_fname = std::string(optarg); break; case 'r': restore_fname = std::string(optarg); break; case 'U': auth_data = std::string(optarg); break; case 'C': cluster_key = std::string(optarg); break; case 'M': user_mgt = std::string(optarg); break; } } if(session.empty() && (!theme.empty() || !tspace.empty())) { cout << COL_RED << "Session must be defined!" << COL_NORM "\n"; exit(1); } if(theme.empty() && !key.empty()) { cout << COL_RED << "Theme must be defined!" << COL_NORM "\n"; exit(1); } else if (session != GLOBAL_PSEUDO_SESSION && tspace.empty() && !tname.empty()) { cout << COL_RED << "Table space must be defined!" << COL_NORM "\n"; exit(1); } if ((clear_data && !dump_fname.empty()) || (clear_data && !restore_fname.empty()) || (!dump_fname.empty() && !restore_fname.empty())) { cout << COL_RED << "clear/dump/restore conflict, one operation can be selected" << COL_NORM << "\n"; exit(1); } if(!table_id && !show_stats && session.empty() && user_mgt.empty()) { show_help(); exit(1); } DGrid::setLogLevel(debug_level); Config* config = Config::instance(); config->load(DEFAULT_CONFIG_FILE); client = new DGrid::DGridDataClient(); if(!auth_data.empty()) { size_t pos = auth_data.find_first_of(":"); std::string username = auth_data.substr(0, pos); std::string passwd; if(pos != std::string::npos) { passwd = auth_data.substr(pos+1); } if(passwd.empty()) { cout << "password: "; cin >> passwd; } client->setAuthData(username,passwd); } else { if(cluster_key.empty()) cluster_key = config->get_cluster_key(); client->setAuthData(cluster_key); } if(!client->openConnection(server_addr, server_port)) { if(client->authState() == AUTH_STATE_FAILED) { cout << "Auth failed" << "\n"; return 2; } return 3; } if(!user_mgt.empty()) { return user_management(user_mgt); } if(!table_id.empty()) { if(clear_data) clear_table(table_id); else if (!dump_fname.empty()) dump_table(table_id, dump_fname); else if (!restore_fname.empty()) restore_table(table_id, restore_fname); else show_table(table_id, show_offset, show_limit); } else if(show_stats || !session.empty()) { if(!session.empty() && session != GLOBAL_PSEUDO_SESSION) client->set_session(session); if(!key.empty()) { if(clear_data) remove_kv(session, theme, key); else show_kv(session, theme, key); } else if (!tname.empty()) { if(clear_data) clear_table(session, tspace, tname); else if (!dump_fname.empty()) dump_table(session, tspace, tname, dump_fname); else if (!restore_fname.empty()) restore_table(session, tspace, tname, restore_fname); else show_table(session, tspace, tname, show_offset, show_limit); } else { if(tspace.empty()) show_stats_kv(session, theme); if(theme.empty()) show_stats_tables(session, tspace); } } // client->clear_session(); client->closeConnection(); delete client; return 0; };