Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
#include <getopt.h>
#include <fstream>
#include <iostream>
#include <chrono>
#include <unistd.h>
#include <experimental/random>
 
#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<typename T>
std::basic_ostream<T>& operator<<(std::basic_ostream<T>& 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; f<structure.fields.size(); f++) {
        cout << " '" << structure.fields[f] << "' (" << fieldType2string(structure.fields_type[f]) << ")";
        json_fields[f] = structure.fields[f];
    }
    for(size_t f=0; f<structure.fields_type.size(); f++) {
        json_fields_types[f] = fieldType2string(structure.fields_type[f]);
    }
    cout << "\n";
    cout << "key fields:";
    for(size_t f=0; f<structure.keys.size(); f++) {
        cout << " '" << structure.keys[f] << "'";
        json_keys[f] = structure.keys[f];
    }
    cout << "\n";
    if(DGRID_OK != client->getTableSize(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<rows.rows.size(); i++) {
        json json_row;
 
        for(size_t f=0; f<structure.fields.size(); f++) {
            if(f < rows.rows[i].data.size())
                json_row[structure.fields[f]] = rows.rows[i].data[f];
            else
                json_row[structure.fields[f]] = "";
        }
        json_data[i] = json_row;
    }
    json_table["tabtype"] = tableType2string(structure.type);
    json_table["fields"]  = json_fields;
    json_table["types"]   = json_fields_types;
    json_table["keys"]    = json_keys;
    json_table["data"]    = json_data;
 
    raw_data = json_table.dump();
    // cout << COL_YEL << "JSON:" << COL_NORM << "\n" << raw_data << "\n";
    cout << "writing to file...\n";
 
    file.open(fname, std::ios::out | std::ios::trunc);
    if (!file) {
        cout << COL_RED << "Can't create file: " << strerror(errno) << COL_NORM << "\n";
        return;
    }
    file.write(raw_data.c_str(), raw_data.size());
    file.close();
    cout << COL_GRN << "DONE" << COL_NORM << "\n";
}
 
void restore_table(const TableID& id_table, const std::string& fname)
{
    DGrid::RecordID id_record;
    TableStructureData structure;
    json json_fields;
    json json_data;
    json json_table;
    std::string raw_data;
    std::ifstream file;
    std::vector<std::string> 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<char*> (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; i<json_data.size(); i++) {
        for(size_t f=0; f<structure.fields.size(); f++) {
            if(json_data[i].contains(structure.fields[f])) {
                values[f] = json_data[i].at(structure.fields[f]);
            } else {
                values[f] = "";
            }
        }
/*
        cout << "row [" << i << "]: ";
        for(auto f: fdata)
            cout << " '" << f << "' ";
        cout << "\n";
*/
        if(DGRID_OK != client->appendRow(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; i<rows.rows.size(); i++)
        dumpRow(rows.rows[i]);
 
    rows.clear();
}
 
void show_table(const std::string& session, const std::string& tspace, const std::string& tname, int64_t offset, int64_t limit)
{
    TableID id;
    if(DGRID_OK == client->getTableID(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<std::string> 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<std::string> 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<std::string> 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<std::string> 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<std::string> 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<std::string> 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 <lvl>    set debug level\n";
    cout << "-h, --host <ip>      dgridd addr (default: " << DEFAULT_DGRID_SERVER_ADDR << ")\n";
    cout << "-p, --port <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 <name> show stats for session\n";
    cout << "-T, --theme <name>   list K-V in selected theme (session must me defined)\n";
    cout << "-k, --key <name>     show selected key (session/theme must me defined)\n";
    cout << "-P, --tspace <name>  list tables in selected space (session must me defined)\n";
    cout << "-t, --table <name>   show table by name (session/tspace must me defined)\n";
    cout << "-i, --tableid <id>   show table by id\n";
    cout << "-d, --data           show table data (structure only displayed by default)\n";
    cout << "-o, --offset <o>     show rows starting from offset\n";
    cout << "-l, --limit <l>      limit rows count\n";
    cout << "-c, --clear          remove key/clear table\n";
    cout << "-u, --dump <file>    dump data to file\n";
    cout << "-r, --restore <file> restore data from file\n";
    cout << "== auth/user mgt:\n";
    cout << "-U, --user <user>:<passwd>  connect as user\n";
    cout << "-C, --cluster <cluster_key> auth with cluster_key\n";
    cout << "-M, --usermgt <action>:<username> 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;
};