-
Notifications
You must be signed in to change notification settings - Fork 20
/
ktremotedb.h
2443 lines (2431 loc) · 86.6 KB
/
ktremotedb.h
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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************************************************************
* Remote database
* Copyright (C) 2009-2012 FAL Labs
* This file is part of Kyoto Tycoon.
* This program is free software: you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation, either version
* 3 of the License, or any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*************************************************************************************************/
#ifndef _KTREMOTEDB_H // duplication check
#define _KTREMOTEDB_H
#include <ktcommon.h>
#include <ktutil.h>
#include <ktsocket.h>
#include <ktthserv.h>
#include <kthttp.h>
#include <ktrpc.h>
#include <ktulog.h>
#include <ktshlib.h>
#include <kttimeddb.h>
#include <ktdbext.h>
namespace kyototycoon { // common namespace
/**
* Remote database.
* @note This class is a concrete class to access remote database servers. This class can be
* inherited but overwriting methods is forbidden. Before every database operation, it is
* necessary to call the RemoteDB::open method in order to connect to a database server. To
* avoid resource starvation it is important to close every database file by the RemoteDB::close
* method when the connection is no longer in use. Although all methods of this class are
* thread-safe, its instance does not have mutual exclusion mechanism. So, multiple threads
* must not share the same instance and they must use their own respective instances.
*/
class RemoteDB {
public:
class Cursor;
class Error;
struct BulkRecord;
/** The maximum size of each record data. */
static const size_t DATAMAXSIZ = 1ULL << 28;
private:
struct OrderedKey;
/** An alias of list of cursors. */
typedef std::list<Cursor*> CursorList;
/** The size for a record buffer. */
static const int32_t RECBUFSIZ = 2048;
public:
/**
* Cursor to indicate a record.
*/
class Cursor {
friend class RemoteDB;
public:
/**
* Constructor.
* @param db the container database object.
*/
explicit Cursor(RemoteDB* db) : db_(db), id_(0) {
_assert_(db);
uint64_t uid = (((uint64_t)(intptr_t)db_ >> 8) << 16) ^ ((uint64_t)(intptr_t)this >> 8);
uid ^= ((uint64_t)(kc::time() * 65536)) << 24;
id_ = ((uid << 16) & (kc::INT64MAX >> 4)) + (++db->curcnt_);
db_->curs_.push_back(this);
}
/**
* Destructor.
*/
virtual ~Cursor() {
_assert_(true);
if (!db_) return;
std::map<std::string, std::string> inmap;
set_cur_param(inmap);
std::map<std::string, std::string> outmap;
db_->rpc_.call("cur_delete", &inmap, &outmap);
db_->curs_.remove(this);
}
/**
* Jump the cursor to the first record for forward scan.
* @return true on success, or false on failure.
*/
bool jump() {
_assert_(true);
std::map<std::string, std::string> inmap;
db_->set_sig_param(inmap);
db_->set_db_param(inmap);
set_cur_param(inmap);
std::map<std::string, std::string> outmap;
RPCClient::ReturnValue rv = db_->rpc_.call("cur_jump", &inmap, &outmap);
if (rv != RPCClient::RVSUCCESS) {
db_->set_rpc_error(rv, outmap);
return false;
}
return true;
}
/**
* Jump the cursor to a record for forward scan.
* @param kbuf the pointer to the key region.
* @param ksiz the size of the key region.
* @return true on success, or false on failure.
*/
bool jump(const char* kbuf, size_t ksiz) {
_assert_(kbuf && ksiz <= kc::MEMMAXSIZ);
std::map<std::string, std::string> inmap;
db_->set_sig_param(inmap);
db_->set_db_param(inmap);
set_cur_param(inmap);
inmap["key"] = std::string(kbuf, ksiz);
std::map<std::string, std::string> outmap;
RPCClient::ReturnValue rv = db_->rpc_.call("cur_jump", &inmap, &outmap);
if (rv != RPCClient::RVSUCCESS) {
db_->set_rpc_error(rv, outmap);
return false;
}
return true;
}
/**
* Jump the cursor to a record for forward scan.
* @note Equal to the original Cursor::jump method except that the parameter is std::string.
*/
bool jump(const std::string& key) {
_assert_(true);
return jump(key.c_str(), key.size());
}
/**
* Jump the cursor to the last record for backward scan.
* @return true on success, or false on failure.
* @note This method is dedicated to tree databases. Some database types, especially hash
* databases, may provide a dummy implementation.
*/
bool jump_back() {
_assert_(true);
std::map<std::string, std::string> inmap;
db_->set_sig_param(inmap);
db_->set_db_param(inmap);
set_cur_param(inmap);
std::map<std::string, std::string> outmap;
RPCClient::ReturnValue rv = db_->rpc_.call("cur_jump_back", &inmap, &outmap);
if (rv != RPCClient::RVSUCCESS) {
db_->set_rpc_error(rv, outmap);
return false;
}
return true;
}
/**
* Jump the cursor to a record for backward scan.
* @param kbuf the pointer to the key region.
* @param ksiz the size of the key region.
* @return true on success, or false on failure.
* @note This method is dedicated to tree databases. Some database types, especially hash
* databases, will provide a dummy implementation.
*/
bool jump_back(const char* kbuf, size_t ksiz) {
_assert_(kbuf && ksiz <= kc::MEMMAXSIZ);
std::map<std::string, std::string> inmap;
db_->set_sig_param(inmap);
db_->set_db_param(inmap);
set_cur_param(inmap);
inmap["key"] = std::string(kbuf, ksiz);
std::map<std::string, std::string> outmap;
RPCClient::ReturnValue rv = db_->rpc_.call("cur_jump_back", &inmap, &outmap);
if (rv != RPCClient::RVSUCCESS) {
db_->set_rpc_error(rv, outmap);
return false;
}
return true;
}
/**
* Jump the cursor to a record for backward scan.
* @note Equal to the original Cursor::jump_back method except that the parameter is
* std::string.
*/
bool jump_back(const std::string& key) {
_assert_(true);
return jump_back(key.c_str(), key.size());
}
/**
* Step the cursor to the next record.
* @return true on success, or false on failure.
*/
bool step() {
_assert_(true);
std::map<std::string, std::string> inmap;
db_->set_sig_param(inmap);
db_->set_db_param(inmap);
set_cur_param(inmap);
std::map<std::string, std::string> outmap;
RPCClient::ReturnValue rv = db_->rpc_.call("cur_step", &inmap, &outmap);
if (rv != RPCClient::RVSUCCESS) {
db_->set_rpc_error(rv, outmap);
return false;
}
return true;
}
/**
* Step the cursor to the previous record.
* @return true on success, or false on failure.
* @note This method is dedicated to tree databases. Some database types, especially hash
* databases, may provide a dummy implementation.
*/
bool step_back() {
_assert_(true);
std::map<std::string, std::string> inmap;
db_->set_sig_param(inmap);
db_->set_db_param(inmap);
set_cur_param(inmap);
std::map<std::string, std::string> outmap;
RPCClient::ReturnValue rv = db_->rpc_.call("cur_step_back", &inmap, &outmap);
if (rv != RPCClient::RVSUCCESS) {
db_->set_rpc_error(rv, outmap);
return false;
}
return true;
}
/**
* Set the value of the current record.
* @param vbuf the pointer to the value region.
* @param vsiz the size of the value region.
* @param xt the expiration time from now in seconds. If it is negative, the absolute value
* is treated as the epoch time.
* @param step true to move the cursor to the next record, or false for no move.
* @return true on success, or false on failure.
*/
bool set_value(const char* vbuf, size_t vsiz, int64_t xt = kc::INT64MAX, bool step = false) {
_assert_(vbuf && vsiz <= kc::MEMMAXSIZ);
std::map<std::string, std::string> inmap;
db_->set_sig_param(inmap);
db_->set_db_param(inmap);
set_cur_param(inmap);
inmap["value"] = std::string(vbuf, vsiz);
if (xt < TimedDB::XTMAX) kc::strprintf(&inmap["xt"], "%lld", (long long)xt);
if (step) inmap["step"] = "";
std::map<std::string, std::string> outmap;
RPCClient::ReturnValue rv = db_->rpc_.call("cur_set_value", &inmap, &outmap);
if (rv != RPCClient::RVSUCCESS) {
db_->set_rpc_error(rv, outmap);
return false;
}
return true;
}
/**
* Set the value of the current record.
* @note Equal to the original Cursor::set_value method except that the parameter is
* std::string.
*/
bool set_value_str(const std::string& value, int64_t xt = kc::INT64MAX, bool step = false) {
_assert_(true);
return set_value(value.c_str(), value.size(), xt, step);
}
/**
* Remove the current record.
* @return true on success, or false on failure.
* @note If no record corresponds to the key, false is returned. The cursor is moved to the
* next record implicitly.
*/
bool remove() {
_assert_(true);
std::map<std::string, std::string> inmap;
db_->set_sig_param(inmap);
db_->set_db_param(inmap);
set_cur_param(inmap);
std::map<std::string, std::string> outmap;
RPCClient::ReturnValue rv = db_->rpc_.call("cur_remove", &inmap, &outmap);
if (rv != RPCClient::RVSUCCESS) {
db_->set_rpc_error(rv, outmap);
return false;
}
return true;
}
/**
* Get the key of the current record.
* @param sp the pointer to the variable into which the size of the region of the return
* value is assigned.
* @param step true to move the cursor to the next record, or false for no move.
* @return the pointer to the key region of the current record, or NULL on failure.
* @note If the cursor is invalidated, NULL is returned. Because an additional zero
* code is appended at the end of the region of the return value, the return value can be
* treated as a C-style string. Because the region of the return value is allocated with the
* the new[] operator, it should be released with the delete[] operator when it is no longer
* in use.
*/
char* get_key(size_t* sp, bool step = false) {
_assert_(sp);
std::map<std::string, std::string> inmap;
db_->set_sig_param(inmap);
db_->set_db_param(inmap);
set_cur_param(inmap);
if (step) inmap["step"] = "";
std::map<std::string, std::string> outmap;
RPCClient::ReturnValue rv = db_->rpc_.call("cur_get_key", &inmap, &outmap);
if (rv != RPCClient::RVSUCCESS) {
db_->set_rpc_error(rv, outmap);
return NULL;
}
size_t ksiz;
const char* kbuf = strmapget(outmap, "key", &ksiz);
if (!kbuf) {
db_->set_error(RPCClient::RVELOGIC, "no information");
return NULL;
}
char* rbuf = new char[ksiz+1];
std::memcpy(rbuf, kbuf, ksiz);
rbuf[ksiz] = '\0';
*sp = ksiz;
return rbuf;
}
/**
* Get the key of the current record.
* @note Equal to the original Cursor::get_key method except that a parameter is a string to
* contain the result and the return value is bool for success.
*/
bool get_key(std::string* key, bool step = false) {
_assert_(key);
size_t ksiz;
char* kbuf = get_key(&ksiz, step);
if (!kbuf) return false;
key->clear();
key->append(kbuf, ksiz);
delete[] kbuf;
return true;
}
/**
* Get the value of the current record.
* @param sp the pointer to the variable into which the size of the region of the return
* value is assigned.
* @param step true to move the cursor to the next record, or false for no move.
* @return the pointer to the value region of the current record, or NULL on failure.
* @note If the cursor is invalidated, NULL is returned. Because an additional zero
* code is appended at the end of the region of the return value, the return value can be
* treated as a C-style string. Because the region of the return value is allocated with the
* the new[] operator, it should be released with the delete[] operator when it is no longer
* in use.
*/
char* get_value(size_t* sp, bool step = false) {
_assert_(sp);
std::map<std::string, std::string> inmap;
db_->set_sig_param(inmap);
db_->set_db_param(inmap);
set_cur_param(inmap);
if (step) inmap["step"] = "";
std::map<std::string, std::string> outmap;
RPCClient::ReturnValue rv = db_->rpc_.call("cur_get_value", &inmap, &outmap);
if (rv != RPCClient::RVSUCCESS) {
db_->set_rpc_error(rv, outmap);
return NULL;
}
size_t vsiz;
const char* vbuf = strmapget(outmap, "value", &vsiz);
if (!vbuf) {
db_->set_error(RPCClient::RVELOGIC, "no information");
return NULL;
}
char* rbuf = new char[vsiz+1];
std::memcpy(rbuf, vbuf, vsiz);
rbuf[vsiz] = '\0';
*sp = vsiz;
return rbuf;
}
/**
* Get the value of the current record.
* @note Equal to the original Cursor::get_value method except that a parameter is a string
* to contain the result and the return value is bool for success.
*/
bool get_value(std::string* value, bool step = false) {
_assert_(value);
size_t vsiz;
char* vbuf = get_value(&vsiz, step);
if (!vbuf) return false;
value->clear();
value->append(vbuf, vsiz);
delete[] vbuf;
return true;
}
/**
* Get a pair of the key and the value of the current record.
* @param ksp the pointer to the variable into which the size of the region of the return
* value is assigned.
* @param vbp the pointer to the variable into which the pointer to the value region is
* assigned.
* @param vsp the pointer to the variable into which the size of the value region is
* assigned.
* @param xtp the pointer to the variable into which the absolute expiration time is
* assigned. If it is NULL, it is ignored.
* @param step true to move the cursor to the next record, or false for no move.
* @return the pointer to the pair of the key region, or NULL on failure.
* @note If the cursor is invalidated, NULL is returned. Because an additional zero code is
* appended at the end of each region of the key and the value, each region can be treated
* as a C-style string. The return value should be deleted explicitly by the caller with
* the detele[] operator.
*/
char* get(size_t* ksp, const char** vbp, size_t* vsp, int64_t* xtp = NULL,
bool step = false) {
_assert_(ksp && vbp && vsp);
std::map<std::string, std::string> inmap;
db_->set_sig_param(inmap);
db_->set_db_param(inmap);
set_cur_param(inmap);
if (step) inmap["step"] = "";
std::map<std::string, std::string> outmap;
RPCClient::ReturnValue rv = db_->rpc_.call("cur_get", &inmap, &outmap);
if (rv != RPCClient::RVSUCCESS) {
db_->set_rpc_error(rv, outmap);
*ksp = 0;
*vbp = NULL;
*vsp = 0;
return NULL;
}
size_t ksiz = 0;
const char* kbuf = strmapget(outmap, "key", &ksiz);
size_t vsiz = 0;
const char* vbuf = strmapget(outmap, "value", &vsiz);
if (!kbuf || !vbuf) {
db_->set_error(RPCClient::RVELOGIC, "no information");
*ksp = 0;
*vbp = NULL;
*vsp = 0;
return NULL;
}
const char* rp = strmapget(outmap, "xt");
int64_t xt = rp ? kc::atoi(rp) : kc::INT64MAX;
char* rbuf = new char[ksiz+vsiz+2];
std::memcpy(rbuf, kbuf, ksiz);
rbuf[ksiz] = '\0';
std::memcpy(rbuf + ksiz + 1 , vbuf, vsiz);
rbuf[ksiz+1+vsiz] = '\0';
*ksp = ksiz;
*vbp = rbuf + ksiz + 1;
*vsp = vsiz;
if (xtp) *xtp = xt;
return rbuf;
}
/**
* Get a pair of the key and the value of the current record.
* @note Equal to the original Cursor::get method except that parameters are strings
* to contain the result and the return value is bool for success.
*/
bool get(std::string* key, std::string* value, int64_t* xtp = NULL, bool step = false) {
_assert_(key && value);
size_t ksiz, vsiz;
const char* vbuf;
char* kbuf = get(&ksiz, &vbuf, &vsiz, xtp, step);
if (!kbuf) return false;
key->clear();
key->append(kbuf, ksiz);
value->clear();
value->append(vbuf, vsiz);
delete[] kbuf;
return true;
}
/**
* Get a pair of the key and the value of the current record and remove it atomically.
* @param ksp the pointer to the variable into which the size of the region of the return
* value is assigned.
* @param vbp the pointer to the variable into which the pointer to the value region is
* assigned.
* @param vsp the pointer to the variable into which the size of the value region is
* assigned.
* @param xtp the pointer to the variable into which the absolute expiration time is
* assigned. If it is NULL, it is ignored.
* @return the pointer to the pair of the key region, or NULL on failure.
* @note If the cursor is invalidated, NULL is returned. Because an additional zero code is
* appended at the end of each region of the key and the value, each region can be treated
* as a C-style string. The return value should be deleted explicitly by the caller with
* the detele[] operator. The cursor is moved to the next record implicitly.
*/
char* seize(size_t* ksp, const char** vbp, size_t* vsp, int64_t* xtp = NULL) {
_assert_(ksp && vbp && vsp);
std::map<std::string, std::string> inmap;
db_->set_sig_param(inmap);
db_->set_db_param(inmap);
set_cur_param(inmap);
std::map<std::string, std::string> outmap;
RPCClient::ReturnValue rv = db_->rpc_.call("cur_seize", &inmap, &outmap);
if (rv != RPCClient::RVSUCCESS) {
db_->set_rpc_error(rv, outmap);
return NULL;
}
size_t ksiz = 0;
const char* kbuf = strmapget(outmap, "key", &ksiz);
size_t vsiz = 0;
const char* vbuf = strmapget(outmap, "value", &vsiz);
if (!kbuf || !vbuf) {
db_->set_error(RPCClient::RVELOGIC, "no information");
return NULL;
}
const char* rp = strmapget(outmap, "xt");
int64_t xt = rp ? kc::atoi(rp) : kc::INT64MAX;
char* rbuf = new char[ksiz+vsiz+2];
std::memcpy(rbuf, kbuf, ksiz);
rbuf[ksiz] = '\0';
std::memcpy(rbuf + ksiz + 1 , vbuf, vsiz);
rbuf[ksiz+1+vsiz] = '\0';
*ksp = ksiz;
*vbp = rbuf + ksiz + 1;
*vsp = vsiz;
if (xtp) *xtp = xt;
return rbuf;
}
/**
* Get a pair of the key and the value of the current record and remove it atomically.
* @note Equal to the original Cursor::seize method except that parameters are strings
* to contain the result and the return value is bool for success.
*/
bool seize(std::string* key, std::string* value, int64_t* xtp = NULL) {
_assert_(key && value);
size_t ksiz, vsiz;
const char* vbuf;
char* kbuf = seize(&ksiz, &vbuf, &vsiz, xtp);
if (!kbuf) return false;
key->clear();
key->append(kbuf, ksiz);
value->clear();
value->append(vbuf, vsiz);
delete[] kbuf;
return true;
}
/**
* Get the database object.
* @return the database object.
*/
RemoteDB* db() {
_assert_(true);
return db_;
}
/**
* Get the last happened error.
* @return the last happened error.
*/
Error error() {
_assert_(true);
return db()->error();
}
private:
/**
* Set the parameter of the target cursor.
* @param inmap the string map to contain the input parameters.
*/
void set_cur_param(std::map<std::string, std::string>& inmap) {
_assert_(true);
kc::strprintf(&inmap["CUR"], "%lld", (long long)id_);
}
/** Dummy constructor to forbid the use. */
Cursor(const Cursor&);
/** Dummy Operator to forbid the use. */
Cursor& operator =(const Cursor&);
/** The inner database. */
RemoteDB* db_;
/** The ID number. */
int64_t id_;
};
/**
* Error data.
*/
class Error {
public:
/**
* Error codes.
*/
enum Code {
SUCCESS = RPCClient::RVSUCCESS, ///< success
NOIMPL = RPCClient::RVENOIMPL, ///< not implemented
INVALID = RPCClient::RVEINVALID, ///< invalid operation
LOGIC = RPCClient::RVELOGIC, ///< logical inconsistency
TIMEOUT = RPCClient::RVETIMEOUT, ///< timeout
INTERNAL = RPCClient::RVEINTERNAL, ///< internal error
NETWORK = RPCClient::RVENETWORK, ///< network error
EMISC = RPCClient::RVEMISC ///< miscellaneous error
};
/**
* Default constructor.
*/
explicit Error() : code_(SUCCESS), message_("no error") {
_assert_(true);
}
/**
* Copy constructor.
* @param src the source object.
*/
Error(const Error& src) : code_(src.code_), message_(src.message_) {
_assert_(true);
}
/**
* Constructor.
* @param code an error code.
* @param message a supplement message.
*/
explicit Error(Code code, const std::string& message) : code_(code), message_(message) {
_assert_(true);
}
/**
* Destructor.
*/
~Error() {
_assert_(true);
}
/**
* Set the error information.
* @param code an error code.
* @param message a supplement message.
*/
void set(Code code, const std::string& message) {
_assert_(true);
code_ = code;
message_ = message;
}
/**
* Get the error code.
* @return the error code.
*/
Code code() const {
_assert_(true);
return code_;
}
/**
* Get the readable string of the code.
* @return the readable string of the code.
*/
const char* name() const {
_assert_(true);
return codename(code_);
}
/**
* Get the supplement message.
* @return the supplement message.
*/
const char* message() const {
_assert_(true);
return message_.c_str();
}
/**
* Get the readable string of an error code.
* @param code the error code.
* @return the readable string of the error code.
*/
static const char* codename(Code code) {
_assert_(true);
switch (code) {
case SUCCESS: return "success";
case NOIMPL: return "not implemented";
case INVALID: return "invalid operation";
case LOGIC: return "logical inconsistency";
case INTERNAL: return "internal error";
case NETWORK: return "network error";
default: break;
}
return "miscellaneous error";
}
/**
* Assignment operator from the self type.
* @param right the right operand.
* @return the reference to itself.
*/
Error& operator =(const Error& right) {
_assert_(true);
if (&right == this) return *this;
code_ = right.code_;
message_ = right.message_;
return *this;
}
/**
* Cast operator to integer.
* @return the error code.
*/
operator int32_t() const {
return code_;
}
private:
/** The error code. */
Code code_;
/** The supplement message. */
std::string message_;
};
/**
* Record for bulk operation.
*/
struct BulkRecord {
uint16_t dbidx; ///< index of the target database
std::string key; ///< key
std::string value; ///< value
int64_t xt; ///< expiration time
};
/**
* Magic data in binary protocol.
*/
enum BinaryMagic {
BMNOP = 0xb0, ///< no operation
BMREPLICATION = 0xb1, ///< replication
BMPLAYSCRIPT = 0xb4, ///< call a scripting procedure
BMSETBULK = 0xb8, ///< set in bulk
BMREMOVEBULK = 0xb9, ///< remove in bulk
BMGETBULK = 0xba, ///< get in bulk
BMERROR = 0xbf ///< error
};
/**
* Options in binary protocol.
*/
enum BinaryOption {
BONOREPLY = 1 << 0 ///< no reply
};
/**
* Default constructor.
*/
explicit RemoteDB() :
rpc_(), ecode_(RPCClient::RVSUCCESS), emsg_("no error"),
dbexpr_(""), curs_(), curcnt_(0),
sigwait_(false), sigwaitname_(""), sigwaittime_(0), sigsend_(false), sigsendbrd_(false) {
_assert_(true);
}
/**
* Destructor.
*/
virtual ~RemoteDB() {
_assert_(true);
if (!curs_.empty()) {
CursorList::const_iterator cit = curs_.begin();
CursorList::const_iterator citend = curs_.end();
while (cit != citend) {
Cursor* cur = *cit;
cur->db_ = NULL;
++cit;
}
}
}
/**
* Get the last happened error code.
* @return the last happened error code.
*/
Error error() const {
_assert_(true);
return Error((Error::Code)ecode_, emsg_);
}
/**
* Open the connection.
* @param host the name or the address of the server. If it is an empty string, the local host
* is specified.
* @param port the port numger of the server.
* @param timeout the timeout of each operation in seconds. If it is not more than 0, no
* timeout is specified.
* @param bool secure if this is to be a secure socket
* @param char* ca path of the ca
* @param char* pk path of the private key
* @param char* cert path of the certificate
* @return true on success, or false on failure.
*/
bool open(const std::string& host = "", int32_t port = DEFPORT, double timeout = -1,
bool secure = false, const char* ca = NULL, const char* pk = NULL,
const char* cert = NULL) {
_assert_(true);
if (!rpc_.open(host, port, timeout, secure, ca, pk, cert)) {
set_error(RPCClient::RVENETWORK, "connection failed");
return false;
}
return true;
}
/**
* Close the connection.
* @param grace true for graceful shutdown, or false for immediate disconnection.
* @return true on success, or false on failure.
*/
bool close(bool grace = true) {
_assert_(true);
return rpc_.close();
}
/**
* Get the report of the server information.
* @param strmap a string map to contain the result.
* @return true on success, or false on failure.
*/
bool report(std::map<std::string, std::string>* strmap) {
_assert_(strmap);
strmap->clear();
std::map<std::string, std::string> inmap;
set_sig_param(inmap);
std::map<std::string, std::string> outmap;
RPCClient::ReturnValue rv = rpc_.call("report", &inmap, &outmap);
if (rv != RPCClient::RVSUCCESS) {
set_rpc_error(rv, outmap);
return false;
}
strmap->insert(outmap.begin(), outmap.end());
return true;
}
/**
* Call a procedure of the scripting extension.
* @param name the name of the procedure to call.
* @param params a string map containing the input parameters.
* @param result a string map to contain the output data.
* @return true on success, or false on failure.
*/
bool play_script(const std::string& name, const std::map<std::string, std::string>& params,
std::map<std::string, std::string>* result) {
_assert_(result);
result->clear();
std::map<std::string, std::string> inmap;
set_sig_param(inmap);
inmap["name"] = name;
std::map<std::string, std::string>::const_iterator it = params.begin();
std::map<std::string, std::string>::const_iterator itend = params.end();
while (it != itend) {
std::string key = "_";
key.append(it->first);
inmap[key] = it->second;
++it;
}
std::map<std::string, std::string> outmap;
RPCClient::ReturnValue rv = rpc_.call("play_script", &inmap, &outmap);
if (rv != RPCClient::RVSUCCESS) {
set_rpc_error(rv, outmap);
return false;
}
it = outmap.begin();
itend = outmap.end();
while (it != itend) {
const char* kbuf = it->first.data();
size_t ksiz = it->first.size();
if (ksiz > 0 && *kbuf == '_') {
std::string key(kbuf + 1, ksiz - 1);
(*result)[key] = it->second;
}
++it;
}
return true;
}
/**
* Set the replication configuration.
* @param host the name or the address of the master server. If it is an empty string,
* replication is disabled.
* @param port the port numger of the server.
* @param ts the maximum time stamp of already read logs. If it is kyotocabinet::UINT64MAX,
* the current setting is not modified. If it is kyotocabinet::UINT64MAX - 1, the current
* time is specified.
* @param iv the interval of each replication operation in milliseconds. If it is negative,
* the current interval is not modified.
* @return true on success, or false on failure.
*/
bool tune_replication(const std::string& host = "", int32_t port = DEFPORT,
uint64_t ts = kc::UINT64MAX, double iv = -1) {
std::map<std::string, std::string> inmap;
set_sig_param(inmap);
if (!host.empty()) inmap["host"] = host;
if (port != DEFPORT) kc::strprintf(&inmap["port"], "%d", port);
if (ts == kc::UINT64MAX - 1) {
inmap["ts"] = "now";
} else if (ts != kc::UINT64MAX) {
kc::strprintf(&inmap["ts"], "%llu", (unsigned long long)ts);
}
if (iv >= 0) kc::strprintf(&inmap["iv"], "%.6f", iv);
std::map<std::string, std::string> outmap;
RPCClient::ReturnValue rv = rpc_.call("tune_replication", &inmap, &outmap);
if (rv != RPCClient::RVSUCCESS) {
set_rpc_error(rv, outmap);
return false;
}
return true;
}
/**
* Get status of each update log files.
* @param fstvec a vector to store status structures of each update log files.
* @return true on success, or false on failure.
*/
bool ulog_list(std::vector<UpdateLogger::FileStatus>* fstvec) {
_assert_(fstvec);
fstvec->clear();
std::map<std::string, std::string> inmap;
set_sig_param(inmap);
std::map<std::string, std::string> outmap;
RPCClient::ReturnValue rv = rpc_.call("ulog_list", &inmap, &outmap);
if (rv != RPCClient::RVSUCCESS) {
set_rpc_error(rv, outmap);
return false;
}
std::map<std::string, std::string>::iterator it = outmap.begin();
std::map<std::string, std::string>::iterator itend = outmap.end();
while (it != itend) {
size_t idx = it->second.find(':');
if (idx != std::string::npos) {
const std::string& tsstr = it->second.substr(idx + 1);
int64_t fsiz = kc::atoi(it->second.c_str());
int64_t fts = kc::atoi(tsstr.c_str());
if (!it->first.empty() && fsiz >= 0 && fts >= 0) {
UpdateLogger::FileStatus fs = { it->first, (uint64_t)fsiz, (uint64_t)fts };
fstvec->push_back(fs);
}
}
++it;
}
return true;
}
/**
* Remove old update log files.
* @param ts the maximum time stamp of disposable logs.
* @return true on success, or false on failure.
*/
bool ulog_remove(uint64_t ts = kc::UINT64MAX) {
_assert_(true);
std::map<std::string, std::string> inmap;
set_sig_param(inmap);
kc::strprintf(&inmap["ts"], "%llu", (unsigned long long)ts);
std::map<std::string, std::string> outmap;
RPCClient::ReturnValue rv = rpc_.call("ulog_remove", &inmap, &outmap);
if (rv != RPCClient::RVSUCCESS) {
set_rpc_error(rv, outmap);
return false;
}
return true;
}
/**
* Get the miscellaneous status information.
* @param strmap a string map to contain the result.
* @return true on success, or false on failure.
*/
bool status(std::map<std::string, std::string>* strmap) {
_assert_(strmap);
strmap->clear();
std::map<std::string, std::string> inmap;
set_sig_param(inmap);
set_db_param(inmap);
std::map<std::string, std::string> outmap;
RPCClient::ReturnValue rv = rpc_.call("status", &inmap, &outmap);
if (rv != RPCClient::RVSUCCESS) {
set_rpc_error(rv, outmap);
return false;
}
strmap->insert(outmap.begin(), outmap.end());
return true;
}
/**
* Remove all records.
* @return true on success, or false on failure.
*/
bool clear() {
_assert_(true);
std::map<std::string, std::string> inmap;
set_sig_param(inmap);
set_db_param(inmap);
std::map<std::string, std::string> outmap;
RPCClient::ReturnValue rv = rpc_.call("clear", &inmap, &outmap);
if (rv != RPCClient::RVSUCCESS) {
set_rpc_error(rv, outmap);
return false;
}
return true;
}
/**
* Synchronize updated contents with the file and the device.
* @param hard true for physical synchronization with the device, or false for logical
* synchronization with the file system.
* @param command the command name to process the database file. If it is an empty string, no
* postprocessing is performed.
* @return true on success, or false on failure.
*/
bool synchronize(bool hard, const std::string& command = "") {
_assert_(true);
std::map<std::string, std::string> inmap;
set_sig_param(inmap);
set_db_param(inmap);
if (hard) inmap["hard"] = "";
if (!command.empty()) inmap["command"] = command;
std::map<std::string, std::string> outmap;
RPCClient::ReturnValue rv = rpc_.call("synchronize", &inmap, &outmap);
if (rv != RPCClient::RVSUCCESS) {
set_rpc_error(rv, outmap);
return false;
}
return true;
}
/**
* Get the number of records.
* @return the number of records, or -1 on failure.
*/
int64_t count() {
_assert_(true);
std::map<std::string, std::string> inmap;
set_sig_param(inmap);
set_db_param(inmap);
std::map<std::string, std::string> outmap;
RPCClient::ReturnValue rv = rpc_.call("status", &inmap, &outmap);
if (rv != RPCClient::RVSUCCESS) {
set_rpc_error(rv, outmap);
return -1;
}
const char* rp = strmapget(outmap, "count");
if (!rp) {
set_error(RPCClient::RVELOGIC, "no information");
return -1;
}
return kc::atoi(rp);
}
/**
* Get the size of the database file.
* @return the size of the database file in bytes, or -1 on failure.
*/