forked from mkoppanen/php-tokyo_tyrant
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tokyo_tyrant.c
1890 lines (1558 loc) · 53.6 KB
/
tokyo_tyrant.c
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
/*
+----------------------------------------------------------------------+
| PHP Version 5 / Tokyo tyrant |
+----------------------------------------------------------------------+
| Copyright (c) 2009 Mikko Koppanen |
+----------------------------------------------------------------------+
| This source file is subject to version 3.0 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_0.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Mikko Koppanen <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "php_tokyo_tyrant.h"
#include "php_tokyo_tyrant_private.h"
#include "php_tokyo_tyrant_funcs.h"
#include "php_tokyo_tyrant_connection.h"
#include "SAPI.h"
#include "php_variables.h"
#include "ext/standard/info.h"
#include "Zend/zend_exceptions.h"
#include "Zend/zend_interfaces.h"
#if PHP_MAJOR_VERSION >=5 && PHP_MINOR_VERSION >= 3
# include "ext/date/php_date.h"
#endif
zend_class_entry *php_tokyo_tyrant_sc_entry;
zend_class_entry *php_tokyo_tyrant_table_sc_entry;
zend_class_entry *php_tokyo_tyrant_query_sc_entry;
zend_class_entry *php_tokyo_tyrant_exception_sc_entry;
static zend_object_handlers tokyo_tyrant_object_handlers;
static zend_object_handlers tokyo_tyrant_table_object_handlers;
static zend_object_handlers tokyo_tyrant_query_object_handlers;
ZEND_DECLARE_MODULE_GLOBALS(tokyo_tyrant);
/* {{{ TokyoTyrant TokyoTyrant::__construct([string host, int port, array params]);
The constructor
@throws TokyoTyrantException if host is set and database connection fails
*/
PHP_METHOD(tokyotyrant, __construct)
{
php_tokyo_tyrant_object *intern;
char *host = NULL;
int host_len, port = PHP_TOKYO_TYRANT_DEFAULT_PORT;
zval *params = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sla!", &host, &host_len, &port, ¶ms) == FAILURE) {
return;
}
intern = PHP_TOKYO_OBJECT;
if (host && !php_tt_connect(intern, host, port, params TSRMLS_CC)) {
PHP_TOKYO_TYRANT_EXCEPTION(intern, "Unable to connect to database: %s");
}
return;
}
/* }}} */
/* {{{ TokyoTyrant TokyoTyrant::connect(string host, int port[, array params]);
Connect to a database
@throws TokyoTyrantException if disconnecting previous connection fails
@throws TokyoTyrantException if connection fails
*/
PHP_METHOD(tokyotyrant, connect)
{
php_tokyo_tyrant_object *intern;
char *host = NULL;
int host_len, port = PHP_TOKYO_TYRANT_DEFAULT_PORT;
zval *params = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|la!", &host, &host_len, &port, ¶ms) == FAILURE) {
return;
}
intern = PHP_TOKYO_OBJECT;
if (!php_tt_connect(intern, host, port, params TSRMLS_CC)) {
PHP_TOKYO_TYRANT_EXCEPTION(intern, "Unable to connect to database: %s");
}
PHP_TOKYO_CHAIN_METHOD;
}
/* }}} */
/* {{{ TokyoTyrant TokyoTyrant::connectUri(string uri);
Connect to a database using uri tcp://hostname:port?persistent=1&timeout=2.2&reconnect=true
@throws TokyoTyrantException if disconnecting previous connection fails
@throws TokyoTyrantException if connection fails
*/
PHP_METHOD(tokyotyrant, connecturi)
{
php_tokyo_tyrant_object *intern;
char *uri;
int uri_len;
php_url *url;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &uri, &uri_len) == FAILURE) {
return;
}
if (!(url = php_url_parse(uri))) {
PHP_TOKYO_TYRANT_EXCEPTION_MSG("Failed to parse the uri");
}
intern = PHP_TOKYO_OBJECT;
if (!php_tt_connect2(intern, url TSRMLS_CC)) {
php_url_free(url);
PHP_TOKYO_TYRANT_EXCEPTION(intern, "Unable to connect to database: %s");
}
php_url_free(url);
PHP_TOKYO_CHAIN_METHOD;
}
/* }}} */
/* {{{ static int _php_tt_real_write(TCRDB *rdb, long type, char *key, int key_len, char *value TSRMLS_DC) */
static int _php_tt_real_write(TCRDB *rdb, long type, char *key, int key_len, char *value TSRMLS_DC)
{
int code = 0;
int new_len;
char *kbuf = php_tt_prefix(key, key_len, &new_len TSRMLS_CC);
switch (type) {
case PHP_TOKYO_TYRANT_OP_PUT:
code = tcrdbput2(rdb, kbuf, value);
break;
case PHP_TOKYO_TYRANT_OP_PUTKEEP:
code = tcrdbputkeep2(rdb, kbuf, value);
break;
case PHP_TOKYO_TYRANT_OP_PUTCAT:
code = tcrdbputcat2(rdb, kbuf, value);
break;
case PHP_TOKYO_TYRANT_OP_PUTNR:
code = tcrdbputnr2(rdb, kbuf, value);
break;
case PHP_TOKYO_TYRANT_OP_OUT:
code = tcrdbout2(rdb, kbuf);
break;
case PHP_TOKYO_TYRANT_OP_TBLOUT:
code = tcrdbtblout(rdb, kbuf, new_len);
break;
}
efree(kbuf);
/* TODO: maybe add strict flag but ignore non-existent keys for now */
if (!code) {
if (tcrdbecode(rdb) == TTENOREC) {
code = 1;
}
}
return code;
}
/* }}} */
/* {{{ static int _php_tt_op_many(zval **ppzval, int num_args, va_list args, zend_hash_key *hash_key) */
static int _php_tt_op_many(zval **ppzval, int num_args, va_list args, zend_hash_key *hash_key)
{
zval tmpcopy;
php_tokyo_tyrant_object *intern;
int type, *code, key_len;
char *key, key_buf[256];
if (num_args != 3) {
return 0;
}
TSRMLS_FETCH();
intern = va_arg(args, php_tokyo_tyrant_object *);
type = va_arg(args, int);
code = va_arg(args, int *);
if (hash_key->nKeyLength == 0) {
key_buf[0] = '\0';
key_len = sprintf(key_buf, "%ld", hash_key->h);
key = key_buf;
} else {
key = hash_key->arKey;
key_len = hash_key->nKeyLength;
}
tmpcopy = **ppzval;
zval_copy_ctor(&tmpcopy);
INIT_PZVAL(&tmpcopy);
convert_to_string(&tmpcopy);
if (type == PHP_TOKYO_TYRANT_OP_OUT || type == PHP_TOKYO_TYRANT_OP_TBLOUT) {
*code = _php_tt_real_write(intern->conn->rdb, type, Z_STRVAL(tmpcopy), Z_STRLEN(tmpcopy), NULL TSRMLS_CC);
} else {
*code = _php_tt_real_write(intern->conn->rdb, type, key, key_len, Z_STRVAL(tmpcopy) TSRMLS_CC);
}
zval_dtor(&tmpcopy);
if (!(*code)) {
return ZEND_HASH_APPLY_STOP;
}
return ZEND_HASH_APPLY_KEEP;
}
/* }}} */
/* {{{ static void _php_tt_write_wrapper(INTERNAL_FUNCTION_PARAMETERS, long type) */
static void _php_tt_write_wrapper(INTERNAL_FUNCTION_PARAMETERS, long type)
{
php_tokyo_tyrant_object *intern;
zval *key, *value = NULL;
int code;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|z!", &key, &value) == FAILURE) {
return;
}
PHP_TOKYO_CONNECTED_OBJECT(intern);
if (Z_TYPE_P(key) == IS_ARRAY) {
zend_hash_apply_with_arguments(Z_ARRVAL_P(key), (apply_func_args_t) _php_tt_op_many, 3, intern, type, &code);
if (!code) {
if (type == PHP_TOKYO_TYRANT_OP_OUT || type == PHP_TOKYO_TYRANT_OP_TBLOUT) {
PHP_TOKYO_TYRANT_EXCEPTION(intern, "Unable to remove the records: %s");
} else {
PHP_TOKYO_TYRANT_EXCEPTION(intern, "Unable to put the records: %s");
}
}
} else {
convert_to_string(key);
if (type == PHP_TOKYO_TYRANT_OP_OUT || type == PHP_TOKYO_TYRANT_OP_TBLOUT) {
if (!_php_tt_real_write(intern->conn->rdb, type, Z_STRVAL_P(key), Z_STRLEN_P(key), NULL TSRMLS_CC)) {
zend_throw_exception_ex(php_tokyo_tyrant_exception_sc_entry, tcrdbecode(intern->conn->rdb) TSRMLS_CC, "Unable to remove the record '%s': %s",
Z_STRVAL_P(key), tcrdberrmsg(tcrdbecode(intern->conn->rdb)));
return;
}
} else {
if (!value) {
PHP_TOKYO_TYRANT_EXCEPTION_MSG("Unable to store the record: no value provided");
}
convert_to_string(value);
if (!_php_tt_real_write(intern->conn->rdb, type, Z_STRVAL_P(key), Z_STRLEN_P(key), Z_STRVAL_P(value) TSRMLS_CC)) {
zend_throw_exception_ex(php_tokyo_tyrant_exception_sc_entry, tcrdbecode(intern->conn->rdb) TSRMLS_CC, "Unable to store the record '%s': %s",
Z_STRVAL_P(key), tcrdberrmsg(tcrdbecode(intern->conn->rdb)));
return;
}
}
}
PHP_TOKYO_CHAIN_METHOD;
}
/* }}} */
/* {{{ TokyoTyrant TokyoTyrant::put(mixed key[, string value]);
Stores a string record
@throws TokyoTyrantException if put fails
@throws TokyoTyrantException if key is not an array and not value is provided
*/
PHP_METHOD(tokyotyrant, put)
{
_php_tt_write_wrapper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_TOKYO_TYRANT_OP_PUT);
}
/* }}} */
/* {{{ TokyoTyrant TokyoTyrant::putkeep(mixed key[, string value]);
Stores a string record if not exists
@throws TokyoTyrantException if put fails
@throws TokyoTyrantException if key is not an array and not value is provided
*/
PHP_METHOD(tokyotyrant, putkeep)
{
_php_tt_write_wrapper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_TOKYO_TYRANT_OP_PUTKEEP);
}
/* }}} */
/* {{{ TokyoTyrant TokyoTyrant::putcat(mixed key[, string value]);
Concatenate a string value
@throws TokyoTyrantException if put fails
@throws TokyoTyrantException if key is not an array and not value is provided
*/
PHP_METHOD(tokyotyrant, putcat)
{
_php_tt_write_wrapper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_TOKYO_TYRANT_OP_PUTCAT);
}
/* }}} */
/* {{{ TokyoTyrant TokyoTyrant::putnr(mixed key[, string value]);
Put a record and don't wait for server to respond
@throws TokyoTyrantException if put fails
@throws TokyoTyrantException if key is not an array and not value is provided
*/
PHP_METHOD(tokyotyrant, putnr)
{
_php_tt_write_wrapper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_TOKYO_TYRANT_OP_PUTNR);
}
/* }}} */
/* {{{ TokyoTyrant TokyoTyrant::putshl(string key, string value, int width);
Concatenate a string value and shift to left
@throws TokyoTyrantException if put fails
@throws TokyoTyrantException if key is not an array and not value is provided
*/
PHP_METHOD(tokyotyrant, putshl)
{
php_tokyo_tyrant_object *intern;
char *kbuf, *key, *value;
int code, key_len, value_len, new_len;
long width;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssl", &key, &key_len, &value, &value_len, &width) == FAILURE) {
return;
}
PHP_TOKYO_CONNECTED_OBJECT(intern);
/* Create a prefix */
kbuf = php_tt_prefix(key, key_len, &new_len TSRMLS_CC);
code = tcrdbputshl2(intern->conn->rdb, kbuf, value, width);
efree(kbuf);
if (!code) {
PHP_TOKYO_TYRANT_EXCEPTION(intern, "Unable to putshl the record: %s");
}
PHP_TOKYO_CHAIN_METHOD;
}
/* }}} */
/* {{{ TokyoTyrant TokyoTyrant::out(mixed key);
Removes a record
@throws TokyoTyrantException if remove fails
*/
PHP_METHOD(tokyotyrant, out)
{
_php_tt_write_wrapper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_TOKYO_TYRANT_OP_OUT);
}
/* }}} */
/* {{{ mixed TokyoTyrant::get(mixed key);
Gets string record(s)
@throws TokyoTyrantException if get fails
*/
PHP_METHOD(tokyotyrant, get)
{
php_tokyo_tyrant_object *intern;
zval *key;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &key) == FAILURE) {
return;
}
PHP_TOKYO_CONNECTED_OBJECT(intern);
if (Z_TYPE_P(key) == IS_ARRAY) {
TCMAP *map = php_tt_zval_to_tcmap(key, 1 TSRMLS_CC);
tcrdbget3(intern->conn->rdb, map);
if (!map) {
PHP_TOKYO_TYRANT_EXCEPTION(intern, "Unable to get the records: %s");
}
php_tt_tcmap_to_zval(map, return_value TSRMLS_CC);
tcmapdel(map);
} else {
zval tmpcopy;
char *value, *kbuf;
int new_len;
tmpcopy = *key;
zval_copy_ctor(&tmpcopy);
INIT_PZVAL(&tmpcopy);
convert_to_string(&tmpcopy);
kbuf = php_tt_prefix(Z_STRVAL(tmpcopy), Z_STRLEN(tmpcopy), &new_len TSRMLS_CC);
value = tcrdbget2(intern->conn->rdb, kbuf);
zval_dtor(&tmpcopy);
efree(kbuf);
if (!value) {
PHP_TOKYO_TYRANT_EXCEPTION(intern, "Unable to get the record: %s");
}
RETVAL_STRING(value, 1);
free(value);
}
return;
}
/* }}} */
/* {{{ int TokyoTyrant::add(string key, mixed value[, CONST type]);
Add to a key
@throws TokyoTyrantException if sync fails
*/
PHP_METHOD(tokyotyrant, add)
{
php_tokyo_tyrant_object *intern;
char *key, *kbuf;
int key_len = 0, new_len, retint;
long type = 0;
zval *value;
double retdouble;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|l", &key, &key_len, &value, &type) == FAILURE) {
return;
}
PHP_TOKYO_CONNECTED_OBJECT(intern);
kbuf = php_tt_prefix(key, key_len, &new_len TSRMLS_CC);
if (type == 0) {
if (Z_TYPE_P(value) == IS_DOUBLE) {
type = PHP_TOKYO_TYRANT_RECTYPE_DOUBLE;
} else {
type = PHP_TOKYO_TYRANT_RECTYPE_INT;
}
}
switch (type) {
case PHP_TOKYO_TYRANT_RECTYPE_INT:
convert_to_long(value);
retint = tcrdbaddint(intern->conn->rdb, kbuf, new_len, Z_LVAL_P(value));
if (retint == INT_MIN) {
RETURN_NULL();
}
RETVAL_LONG(retint);
break;
case PHP_TOKYO_TYRANT_RECTYPE_DOUBLE:
convert_to_double(value);
retdouble = tcrdbadddouble(intern->conn->rdb, kbuf, new_len, Z_DVAL_P(value));
if (isnan(retdouble)) {
RETURN_NULL();
}
RETVAL_DOUBLE(retdouble);
break;
default:
efree(kbuf);
PHP_TOKYO_TYRANT_EXCEPTION_MSG("Unknown record type");
break;
}
efree(kbuf);
return;
}
/* }}} */
/* {{{ TokyoTyrant TokyoTyrant::sync();
Flushes the changes to disk
@throws TokyoTyrantException if sync fails
*/
PHP_METHOD(tokyotyrant, sync)
{
php_tokyo_tyrant_object *intern;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) {
return;
}
PHP_TOKYO_CONNECTED_OBJECT(intern);
if (!tcrdbsync(intern->conn->rdb)) {
PHP_TOKYO_TYRANT_EXCEPTION(intern, "Unable to synchronise the database: %s");
}
PHP_TOKYO_CHAIN_METHOD;
}
/* }}} */
/* {{{ TokyoTyrant TokyoTyrant::vanish();
Empties the remote database
*/
PHP_METHOD(tokyotyrant, vanish)
{
php_tokyo_tyrant_object *intern;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) {
return;
}
PHP_TOKYO_CONNECTED_OBJECT(intern);
if (!tcrdbvanish(intern->conn->rdb)) {
PHP_TOKYO_TYRANT_EXCEPTION(intern, "Unable to empty the database: %s");
}
PHP_TOKYO_CHAIN_METHOD;
}
/* }}} */
/* {{{ string TokyoTyrant::stat();
Gets the status string of the remote database
*/
PHP_METHOD(tokyotyrant, stat)
{
php_tokyo_tyrant_object *intern;
char *status = NULL, *ptr;
char k[1024], v[1024];
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) {
return;
}
PHP_TOKYO_CONNECTED_OBJECT(intern);
status = tcrdbstat(intern->conn->rdb);
if (!status) {
PHP_TOKYO_TYRANT_EXCEPTION(intern, "Unable to get the status string: %s");
}
array_init(return_value);
/* add elements */
ptr = strtok(status, "\n");
while (ptr) {
if (strlen(ptr) >= 1024) {
continue;
}
memset(k, '\0', 1024);
memset(v, '\0', 1024);
if (sscanf(ptr, "%s\t%s", k, v) != 2) {
continue;
}
add_assoc_string(return_value, k, v, 1);
ptr = strtok(NULL, "\n");
}
free(status);
return;
}
/* }}} */
/* {{{ int TokyoTyrant::size(string key);
Get the size of a row
*/
PHP_METHOD(tokyotyrant, size)
{
php_tokyo_tyrant_object *intern;
char *key, *kbuf;
int key_len, new_len;
long rec_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &key, &key_len) == FAILURE) {
return;
}
PHP_TOKYO_CONNECTED_OBJECT(intern);
kbuf = php_tt_prefix(key, key_len, &new_len TSRMLS_CC);
rec_len = tcrdbvsiz2(intern->conn->rdb, kbuf);
efree(kbuf);
if (rec_len == -1) {
PHP_TOKYO_TYRANT_EXCEPTION(intern, "Unable to get the record size: %s");
}
RETURN_LONG(rec_len);
}
/* }}} */
/* {{{ int TokyoTyrant::num();
Number of records in the database
*/
PHP_METHOD(tokyotyrant, num)
{
php_tokyo_tyrant_object *intern;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) {
return;
}
PHP_TOKYO_CONNECTED_OBJECT(intern);
RETURN_LONG(tcrdbrnum(intern->conn->rdb));
}
/* }}} */
/* {{{ array TokyoTyrant::fwmkeys(string prefix, int max_records) */
PHP_METHOD(tokyotyrant, fwmkeys)
{
php_tokyo_tyrant_object *intern;
char *prefix, *kbuf;
int prefix_len, new_len;
long max_recs;
TCLIST *res = NULL;
const char *rbuf;
int i, rsiz;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &prefix, &prefix_len, &max_recs) == FAILURE) {
return;
}
PHP_TOKYO_CONNECTED_OBJECT(intern);
res = tcrdbfwmkeys2(intern->conn->rdb, prefix, max_recs);
array_init(return_value);
for (i = 0; i < tclistnum(res); i++) {
rbuf = tclistval(res, i, &rsiz);
kbuf = php_tt_prefix((char *)rbuf, rsiz, &new_len TSRMLS_CC);
add_next_index_stringl(return_value, kbuf, new_len, 0);
}
tclistdel(res);
return;
}
/* }}} */
/* {{{ string TokyoTyrant::ext(string name, CONST opts, string key, string value);
Get the size of a row
*/
PHP_METHOD(tokyotyrant, ext)
{
php_tokyo_tyrant_object *intern;
char *name, *key, *value, *response;
int name_len, key_len, value_len;
long opts;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "slss", &name, &name_len, &opts, &key, &key_len, &value, &value_len) == FAILURE) {
return;
}
PHP_TOKYO_CONNECTED_OBJECT(intern);
response = tcrdbext2(intern->conn->rdb, name, opts, key, value);
if (!response) {
PHP_TOKYO_TYRANT_EXCEPTION(intern, "Unable to execute the remote script: %s");
}
RETVAL_STRING(response, 1);
free(response);
return;
}
/* }}} */
/* {{{ int TokyoTyrant::copy(string path);
Copy the database
*/
PHP_METHOD(tokyotyrant, copy)
{
php_tokyo_tyrant_object *intern;
char *path;
int path_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &path, &path_len) == FAILURE) {
return;
}
PHP_TOKYO_CONNECTED_OBJECT(intern);
if (!tcrdbcopy(intern->conn->rdb, path)) {
PHP_TOKYO_TYRANT_EXCEPTION(intern, "Unable to copy the database: %s");
}
PHP_TOKYO_CHAIN_METHOD;
}
/* }}} */
#if PHP_MAJOR_VERSION >=5 && PHP_MINOR_VERSION >= 3
static uint_fast64_t _php_tt_get_ts(zval *date_param TSRMLS_DC)
{
zval *fname, retval, *params[1];
MAKE_STD_ZVAL(fname);
ZVAL_STRING(fname, "date_timestamp_get", 1);
params[0] = date_param;
call_user_function(EG(function_table), NULL, fname, &retval, 1, params TSRMLS_CC);
zval_dtor(fname);
FREE_ZVAL(fname);
if (Z_TYPE(retval) == IS_BOOL && !Z_BVAL(retval)) {
return 0;
}
/* Microseconds */
return (Z_LVAL(retval) * 1000);
}
/* {{{ TokyoTyrant TokyoTyrant::restore(string log_dir, mixed timestamp[, bool check_consistency = true]);
restore the database
*/
PHP_METHOD(tokyotyrant, restore)
{
zval *date_param;
php_tokyo_tyrant_object *intern;
char *path;
int path_len, opts;
uint_fast64_t ts;
zend_bool check_consistency = 1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|b", &path, &path_len, &date_param, &check_consistency) == FAILURE) {
return;
}
#if !defined(__LP64__) && !defined(__ILP64__)
PHP_TOKYO_TYRANT_EXCEPTION_MSG("TokyoTyrant::restore is not supported on a 32bit platform");
#endif
PHP_TOKYO_CONNECTED_OBJECT(intern);
if (Z_TYPE_P(date_param) == IS_OBJECT) {
zend_class_entry *date_ce_date = php_date_get_date_ce();
if (!instanceof_function_ex(Z_OBJCE_P(date_param), date_ce_date, 0 TSRMLS_CC)) {
PHP_TOKYO_TYRANT_EXCEPTION_MSG("The timestamp parameter must be instanceof DateTime");
}
ts = _php_tt_get_ts(date_param TSRMLS_CC);
if (ts == 0) {
PHP_TOKYO_TYRANT_EXCEPTION_MSG("Failed to get timestamp from the DateTime object");
}
} else {
convert_to_long(date_param);
ts = Z_LVAL_P(date_param);
}
if (check_consistency) {
opts |= RDBROCHKCON;
}
if (!tcrdbrestore(intern->conn->rdb, path, ts, opts)) {
PHP_TOKYO_TYRANT_EXCEPTION(intern, "Unable to restore the database: %s");
}
PHP_TOKYO_CHAIN_METHOD;
}
/* }}} */
#else
/* {{{ TokyoTyrant TokyoTyrant::restore(string log_dir, int timestamp[, bool check_consistency = true]);
restore the database
*/
PHP_METHOD(tokyotyrant, restore)
{
php_tokyo_tyrant_object *intern;
char *path;
int path_len, opts;
uint_fast64_t ts;
zend_bool check_consistency = 1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|b", &path, &path_len, &ts, &check_consistency) == FAILURE) {
return;
}
#if !defined(__LP64__) && !defined(__ILP64__)
PHP_TOKYO_TYRANT_EXCEPTION_MSG("TokyoTyrant::restore is not supported on a 32bit platform");
#endif
PHP_TOKYO_CONNECTED_OBJECT(intern);
if (check_consistency) {
opts |= RDBROCHKCON;
}
if (!tcrdbrestore(intern->conn->rdb, path, ts, opts)) {
PHP_TOKYO_TYRANT_EXCEPTION(intern, "Unable to restore the database: %s");
}
PHP_TOKYO_CHAIN_METHOD;
}
/* }}} */
#endif
#if PHP_MAJOR_VERSION >=5 && PHP_MINOR_VERSION >= 3
/* {{{ TokyoTyrant TokyoTyrant::setMaster(string host, int port, mixed timestamp[, zend_bool check_consistency]);
Set the master
*/
PHP_METHOD(tokyotyrant, setmaster)
{
zval *date_param;
php_tokyo_tyrant_object *intern;
char *host;
int host_len, code, opts;
long port;
uint_fast64_t ts;
zend_bool check_consistency = 1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "slz|b", &host, &host_len, &port, &date_param, &check_consistency) == FAILURE) {
return;
}
PHP_TOKYO_CONNECTED_OBJECT(intern);
if (Z_TYPE_P(date_param) == IS_OBJECT) {
zend_class_entry *date_ce_date = php_date_get_date_ce();
if (!instanceof_function_ex(Z_OBJCE_P(date_param), date_ce_date, 0 TSRMLS_CC)) {
PHP_TOKYO_TYRANT_EXCEPTION_MSG("The timestamp parameter must be instanceof DateTime");
}
ts = _php_tt_get_ts(date_param TSRMLS_CC);
if (ts == 0) {
PHP_TOKYO_TYRANT_EXCEPTION_MSG("Failed to get timestamp from the DateTime object");
}
} else {
convert_to_long(date_param);
ts = Z_LVAL_P(date_param);
}
if (check_consistency) {
opts |= RDBROCHKCON;
}
if (host_len == 0) {
code = tcrdbsetmst(intern->conn->rdb, NULL, 0, ts, opts);
} else {
code = tcrdbsetmst(intern->conn->rdb, host, port, ts, opts);
}
if (!code) {
PHP_TOKYO_TYRANT_EXCEPTION(intern, "Unable to set the master: %s");
}
PHP_TOKYO_CHAIN_METHOD;
}
/* }}} */
#else
/* {{{ TokyoTyrant TokyoTyrant::setMaster(string host, int port, int timestamp[, zend_bool check_consistency]);
Set the master
*/
PHP_METHOD(tokyotyrant, setmaster)
{
php_tokyo_tyrant_object *intern;
char *host;
int host_len, code;
long ts, opts = 0, port;
zend_bool check_consistency = 1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sll|b", &host, &host_len, &port, &ts, &check_consistency) == FAILURE) {
return;
}
PHP_TOKYO_CONNECTED_OBJECT(intern);
if (check_consistency) {
opts |= RDBROCHKCON;
}
if (host_len == 0) {
code = tcrdbsetmst(intern->conn->rdb, NULL, 0, ts, opts);
} else {
code = tcrdbsetmst(intern->conn->rdb, host, port, ts, opts);
}
if (!code) {
PHP_TOKYO_TYRANT_EXCEPTION(intern, "Unable to set the master: %s");
}
PHP_TOKYO_CHAIN_METHOD;
}
/* }}} */
#endif
/** -------- Begin table api -------- **/
/* {{{ TokyoTyrantQuery TokyoTyrantTable::getquery();
Get query object
@throws TokyoTyrantException if not connected to a database
*/
PHP_METHOD(tokyotyranttable, getquery)
{
php_tokyo_tyrant_object *intern;
php_tokyo_tyrant_query_object *intern_query;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) {
return;
}
PHP_TOKYO_CONNECTED_OBJECT(intern);
object_init_ex(return_value, php_tokyo_tyrant_query_sc_entry);
intern_query = (php_tokyo_tyrant_query_object *)zend_object_store_get_object(return_value TSRMLS_CC);
php_tt_query_object_init(intern_query, getThis() TSRMLS_CC);
return;
}
/* }}} */
/* {{{ TokyoTyrantQuery TokyoTyrantTable::genUid();
Generate unique PK
@throws TokyoTyrantException if not connected to a database
@throws TokyoTyrantException if uid generation fails
*/
PHP_METHOD(tokyotyranttable, genuid)
{
php_tokyo_tyrant_object *intern;
long pk = -1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) {
return;
}
PHP_TOKYO_CONNECTED_OBJECT(intern);
pk = (long)tcrdbtblgenuid(intern->conn->rdb);
if (pk == -1) {
PHP_TOKYO_TYRANT_EXCEPTION_MSG("Unable to generate primary key. Not connected to a table database?");
}
RETURN_LONG(pk);
}
/* }}} */
static void _php_tt_table_write_wrapper(INTERNAL_FUNCTION_PARAMETERS, long type)
{
php_tokyo_tyrant_object *intern;
zval *keys;
TCMAP *map;
char *key = NULL, *kbuf = NULL;
int key_len = 0, new_len;
int code = 0;
long pk = -1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!a", &key, &key_len, &keys) == FAILURE) {
return;
}
PHP_TOKYO_CONNECTED_OBJECT(intern);
if (key) {
kbuf = php_tt_prefix(key, key_len, &new_len TSRMLS_CC);
} else {
char buf[256];
int len;
pk = (long)tcrdbtblgenuid(intern->conn->rdb);
if (pk == -1) {
PHP_TOKYO_TYRANT_EXCEPTION_MSG("Unable to generate primary key. Not connected to a table database?");
}
memset(buf, 0, 256);
len = sprintf(buf, "%ld", pk);
kbuf = php_tt_prefix(buf, len, &new_len TSRMLS_CC);
}
map = php_tt_zval_to_tcmap(keys, 0 TSRMLS_CC);
if (!map) {
PHP_TOKYO_TYRANT_EXCEPTION_MSG("Unable to get values from the argument");
}
switch (type) {
case PHP_TOKYO_TYRANT_OP_TBLPUT:
code = tcrdbtblput(intern->conn->rdb, kbuf, new_len, map);
break;
case PHP_TOKYO_TYRANT_OP_TBLPUTKEEP:
code = tcrdbtblputkeep(intern->conn->rdb, kbuf, new_len, map);
break;
case PHP_TOKYO_TYRANT_OP_TBLPUTCAT:
code = tcrdbtblputcat(intern->conn->rdb, kbuf, new_len, map);
break;
}
tcmapdel(map);
if (!code) {
efree(kbuf);
PHP_TOKYO_TYRANT_EXCEPTION(intern, "Unable to store columns: %s");
}
if (pk > 0) {
RETVAL_LONG(pk);
efree(kbuf);
} else {
RETVAL_STRINGL(kbuf, new_len, 0);
}
return;
}
/* {{{ int TokyoTyrantTable::put(string pk, array row);
put a row. if pk = null new key is generated
@throws TokyoTyrantException if not connected to a database
@throws TokyoTyrantException if get fails
*/
PHP_METHOD(tokyotyranttable, put)
{
_php_tt_table_write_wrapper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_TOKYO_TYRANT_OP_TBLPUT);
}
/* }}} */
/* {{{ int TokyoTyrantTable::putkeep(string pk, array row);
put a row
@throws TokyoTyrantException if not connected to a database
@throws TokyoTyrantException if get fails
*/
PHP_METHOD(tokyotyranttable, putkeep)
{
_php_tt_table_write_wrapper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_TOKYO_TYRANT_OP_TBLPUTKEEP);
}
/* }}} */
/* {{{ int TokyoTyrantTable::putcat(string pk, array row);
put a row
@throws TokyoTyrantException if not connected to a database
@throws TokyoTyrantException if get fails
*/
PHP_METHOD(tokyotyranttable, putcat)
{
_php_tt_table_write_wrapper(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_TOKYO_TYRANT_OP_TBLPUTCAT);
}
/* }}} */
/* {{{ int TokyoTyrantTable::add(void);
not supported
*/
PHP_METHOD(tokyotyranttable, add)
{
PHP_TOKYO_TYRANT_EXCEPTION_MSG("add operation is not supported with table databases");