forked from tony2001/tarantool-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtarantool.c
2043 lines (1721 loc) · 52.2 KB
/
tarantool.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 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2008 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 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_01.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. |
+----------------------------------------------------------------------+
| Author: Alexandre Kalendarev [email protected] |
| Copyright (c) 2011 |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdint.h>
#include <stdbool.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <inttypes.h>
#include <php.h>
#include <php_ini.h>
#include <php_network.h>
#include <ext/standard/info.h>
#include <zend_exceptions.h>
#include "php_tarantool.h"
static zend_class_entry *t_io_exception_ce;
static zend_class_entry *tarantool_ce;
/*============================================================================*
* Tarantool extension structures defintion
*============================================================================*/
/* I/O buffer */
struct io_buf {
/* buffer size */
size_t size;
/* buffer capacity */
size_t capacity;
/* read position in the I/O buffer */
size_t read_pos;
/* buffer value */
uint8_t *value;
};
/* tarantool object */
typedef struct tarantool_object {
zend_object zo;
/* host name */
char *host;
/* tarantool primary port */
int port;
/* tarantool admin port */
int admin_port;
/* tarantool primary connection */
php_stream *stream;
/* tarantool admin connecion */
php_stream *admin_stream;
/* I/O buffer */
struct io_buf *io_buf;
/* additional buffer for splice args */
struct io_buf *splice_field;
} tarantool_object;
/* iproto header */
struct iproto_header {
/* command code */
uint32_t type;
/* command length */
uint32_t length;
/* request id */
uint32_t request_id;
} __attribute__((packed));
/* tarantool select command request */
struct tnt_select_request {
/* space number */
int32_t space_no;
/* index number */
int32_t index_no;
/* select offset from begining */
int32_t offset;
/* maximail number tuples in responce */
int32_t limit;
} __attribute__((packed));
/* tarantool insert command request */
struct tnt_insert_request {
/* space number */
int32_t space_no;
/* flags */
int32_t flags;
} __attribute__((packed));
/* tarantool update fields command request */
struct tnt_update_fields_request {
/* space number */
int32_t space_no;
/* flags */
int32_t flags;
} __attribute__((packed));
/* tarantool delete command request */
struct tnt_delete_request {
/* space number */
int32_t space_no;
/* flags */
int32_t flags;
} __attribute__((packed));
/* tarantool call command request */
struct tnt_call_request {
/* flags */
int32_t flags;
} __attribute__((packed));
/* tarantool command response */
struct tnt_response {
/* return code */
int32_t return_code;
union {
/* count */
int32_t count;
/* error message */
char return_msg[0];
};
} __attribute__((packed));
/*============================================================================*
* Global variables definition
*============================================================================*/
/*----------------------------------------------------------------------------*
* Tarantool module variables
*----------------------------------------------------------------------------*/
/* module functions list */
zend_function_entry tarantool_module_functions[] = {
{NULL, NULL, NULL}
};
/* tarantool module struct */
zend_module_entry tarantool_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"tarantool",
tarantool_module_functions,
PHP_MINIT(tarantool),
PHP_MSHUTDOWN(tarantool),
NULL,
NULL,
PHP_MINFO(tarantool),
#if ZEND_MODULE_API_NO >= 20010901
"1.0",
#endif
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_TARANTOOL
ZEND_GET_MODULE(tarantool)
#endif
/*----------------------------------------------------------------------------*
* Tarantool class variables
*----------------------------------------------------------------------------*/
/* tarantool class methods */
const zend_function_entry tarantool_class_methods[] = {
PHP_ME(tarantool_class, __construct, NULL, ZEND_ACC_PUBLIC)
PHP_ME(tarantool_class, select, NULL, ZEND_ACC_PUBLIC)
PHP_ME(tarantool_class, insert, NULL, ZEND_ACC_PUBLIC)
PHP_ME(tarantool_class, update_fields, NULL, ZEND_ACC_PUBLIC)
PHP_ME(tarantool_class, delete, NULL, ZEND_ACC_PUBLIC)
PHP_ME(tarantool_class, call, NULL, ZEND_ACC_PUBLIC)
PHP_ME(tarantool_class, admin, NULL, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
/* tarantool class */
/*============================================================================*
* local functions declaration
*============================================================================*/
/*----------------------------------------------------------------------------*
* I/O buffer interface
*----------------------------------------------------------------------------*/
/* create I/O buffer instance */
static struct io_buf *
io_buf_create();
/* destroy I/O buffer */
static void
io_buf_destroy(struct io_buf *buf);
/* reserv I/O buffer space */
inline static bool
io_buf_reserve(struct io_buf *buf, size_t n);
/* resize I/O buffer */
inline static bool
io_buf_resize(struct io_buf *buf, size_t n);
/* calculate next capacity for I/O buffer */
inline static size_t
io_buf_next_capacity(size_t n);
/* clean I/O buffer */
static void
io_buf_clean(struct io_buf *buf);
/* read struct from buffer */
static bool
io_buf_read_struct(struct io_buf *buf, void **ptr, size_t n);
/* read 32-bit integer from buffer */
static bool
io_buf_read_int32(struct io_buf *buf, int32_t *val);
/* read 64-bit integer from buffer */
static bool
io_buf_read_int64(struct io_buf *buf, int64_t *val);
/* read var integer from buffer */
static bool
io_buf_read_varint(struct io_buf *buf, int32_t *val);
/* read string from buffer */
static bool
io_buf_read_str(struct io_buf *buf, char **str, size_t len);
/* read fied from buffer */
static bool
io_buf_read_field(struct io_buf *buf, zval *tuple);
/* read tuple from buffer */
static bool
io_buf_read_tuple(struct io_buf *buf, zval **tuple);
/*
* Write to I/O buffer functions
*/
/* write struct to I/O buffer */
static void *
io_buf_write_struct(struct io_buf *buf, size_t n);
/* write byte to I/O buffer */
static bool
io_buf_write_byte(struct io_buf *buf, int8_t value);
/* write 32-bit integer to I/O buffer */
static bool
io_buf_write_int32(struct io_buf *buf, int32_t value);
/* write 64-bit integer to I/O buffer */
static bool
io_buf_write_int64(struct io_buf *buf, int64_t value);
/* write varint to I/O buffer */
static bool
io_buf_write_varint(struct io_buf *buf, int32_t value);
/* write string to I/O buffer */
static bool
io_buf_write_str(struct io_buf *buf, uint8_t *str, size_t len);
/* write 32-bit integer as tuple's field to I/O buffer */
static bool
io_buf_write_field_int32(struct io_buf *buf, uint32_t value);
/* write 64-bit integer as tuple's field to I/O buffer */
static bool
io_buf_write_field_int64(struct io_buf *buf, uint64_t value);
/* write string tuple's field to I/O buffer */
static bool
io_buf_write_field_str(struct io_buf *buf, uint8_t *val, size_t len);
/* write tuple to I/O buffer */
static bool
io_buf_write_tuple_int(struct io_buf *buf, zval *tuple);
/* write tuple (string) to I/O buffer */
static bool
io_buf_write_tuple_str(struct io_buf *buf, zval *tuple);
/* write tuple (array) to I/O buffer */
static bool
io_buf_write_tuple_array(struct io_buf *buf, zval *tuple);
/* write tuple to I/O buffer */
static bool
io_buf_write_tuple(struct io_buf *buf, zval *tuple);
/* write array of tuples to I/O buffer */
static bool
io_buf_write_tuples_list_array(struct io_buf *buf, zval *tuples_list);
/* write tuples list to I/O buffer */
static bool
io_buf_write_tuples_list(struct io_buf *buf, zval *tuples_list);
/*
* I/O buffer send/recv
*/
/* send administation command request */
static bool
io_buf_send_yaml(php_stream *stream, struct io_buf *buf);
/* receive administration command response */
static bool
io_buf_recv_yaml(php_stream *stream, struct io_buf *buf);
/* send request by iproto */
static bool
io_buf_send_iproto(php_stream *stream, int32_t type, int32_t request_id, struct io_buf *buf);
/* receive response by iproto */
static bool
io_buf_recv_iproto(php_stream *stream, struct io_buf *buf);
/*----------------------------------------------------------------------------*
* support local functions
*----------------------------------------------------------------------------*/
/* tarantool class instance allocator */
static zend_object_value
alloc_tarantool_object(zend_class_entry *entry TSRMLS_DC);
/* free tarantool class instance */
static void
free_tarantool_object(tarantool_object *tnt TSRMLS_DC);
/* establic connection */
static php_stream *
establish_connection(char *host, int port);
/* find long by key in the hash table */
static bool hash_find_long_ex(HashTable *hash, char *key, int key_len, long *value);
#define hash_find_long(hash, key, value) hash_find_long_ex((hash), (key), strlen(key), (value))
/* find string by key in the hash table */
static bool hash_find_str_ex(HashTable *hash, char *key, int key_len, char **value, int *value_length);
#define hash_find_str(hash, key, value, value_length) hash_find_str_ex((hash), (key), strlen(key), (value), (value_length))
/* find scalar by key in the hash table */
static bool hash_find_scalar_ex(HashTable *hash, char *key, int key_len, zval ***value);
#define hash_find_scalar(hash, key, value) hash_find_scalar_ex((hash), (key), strlen(key), (value))
/*============================================================================*
* Interface definition
*============================================================================*/
/*----------------------------------------------------------------------------*
* Tarantool main module interface
*----------------------------------------------------------------------------*/
/* initialize module function */
PHP_MINIT_FUNCTION(tarantool)
{
zend_class_entry ce;
/* register constants */
/* register tarantool flags */
REGISTER_LONG_CONSTANT("TARANTOOL_FLAGS_RETURN_TUPLE",
TARANTOOL_FLAGS_RETURN_TUPLE,
CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("TARANTOOL_FLAGS_ADD",
TARANTOOL_FLAGS_ADD,
CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("TARANTOOL_FLAGS_REPLACE",
TARANTOOL_FLAGS_REPLACE,
CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("TARANTOOL_FLAGS_NOT_STORE",
TARANTOOL_FLAGS_NOT_STORE,
CONST_CS | CONST_PERSISTENT);
/* register tarantool update fields operations */
REGISTER_LONG_CONSTANT("TARANTOOL_OP_ASSIGN",
TARANTOOL_OP_ASSIGN,
CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("TARANTOOL_OP_ADD",
TARANTOOL_OP_ADD,
CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("TARANTOOL_OP_AND",
TARANTOOL_OP_AND,
CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("TARANTOOL_OP_XOR",
TARANTOOL_OP_XOR,
CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("TARANTOOL_OP_OR",
TARANTOOL_OP_OR,
CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("TARANTOOL_OP_SPLICE",
TARANTOOL_OP_SPLICE,
CONST_CS | CONST_PERSISTENT);
/* register classes */
/* register tarantool class */
INIT_CLASS_ENTRY(ce, "Tarantool", tarantool_class_methods);
ce.create_object = alloc_tarantool_object;
tarantool_ce = zend_register_internal_class(&ce TSRMLS_CC);
INIT_CLASS_ENTRY(ce, "Tarantool_IO_Exception", NULL);
t_io_exception_ce = zend_register_internal_class_ex(&ce, zend_exception_get_default(TSRMLS_C), NULL TSRMLS_CC);
return SUCCESS;
}
/* shutdown module function */
PHP_MSHUTDOWN_FUNCTION(tarantool)
{
return SUCCESS;
}
/* show information about this module */
PHP_MINFO_FUNCTION(tarantool)
{
php_info_print_table_start();
php_info_print_table_header(2, "Tarantool support", "enabled");
php_info_print_table_row(2, "Extension version", TARANTOOL_EXTENSION_VERSION);
php_info_print_table_end();
}
/*----------------------------------------------------------------------------*
* Tarantool class interface
*----------------------------------------------------------------------------*/
PHP_METHOD(tarantool_class, __construct)
{
/*
* parse method's parameters
*/
zval *id;
char *host = NULL;
int host_len = 0;
long port = 0;
long admin_port = 0;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osl|l", &id, tarantool_ce, &host, &host_len, &port, &admin_port) == FAILURE) {
return;
}
/*
* validate parameters
*/
/* check host name */
if (host == NULL || host_len == 0) {
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC,
"invalid tarantool's hostname");
return;
}
/* validate port value */
if (port <= 0 || port >= 65536) {
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC,
"invalid primary port value: %li", port);
return;
}
/* check admin port */
if (admin_port) {
/* validate port value */
if (admin_port < 0 || admin_port >= 65536) {
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC,
"invalid admin port value: %li", admin_port);
return;
}
}
/* initialize object structure */
tarantool_object *object = (tarantool_object *) zend_object_store_get_object(id TSRMLS_CC);
if (object->host) {
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "trying to initialize already initialized Tarantool object");
return;
}
object->host = estrdup(host);
object->port = port;
object->admin_port = admin_port;
object->stream = NULL;
object->admin_stream = NULL;
object->io_buf = io_buf_create();
if (!object->io_buf) {
return;
}
object->splice_field = io_buf_create();
if (!object->splice_field) {
return;
}
object->stream = establish_connection(object->host, object->port);
if (!object->stream) {
return;
}
}
PHP_METHOD(tarantool_class, select)
{
/*
* parse methods parameters
*/
zval *id;
long space_no = 0;
long index_no = 0;
zval *keys_list = NULL;
long limit = -1;
long offset = 0;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ollz|ll", &id, tarantool_ce, &space_no, &index_no, &keys_list, &limit,&offset) == FAILURE) {
return;
}
tarantool_object *tnt = (tarantool_object *) zend_object_store_get_object(
id TSRMLS_CC);
/*
* send request
*/
/* clean-up buffer */
io_buf_clean(tnt->io_buf);
/* fill select command */
/* fill command header */
struct tnt_select_request *request = (struct tnt_select_request *) io_buf_write_struct(tnt->io_buf, sizeof(struct tnt_select_request));
if (request == NULL)
return;
request->space_no = space_no;
request->index_no = index_no;
request->offset = offset;
request->limit = limit;
/* fill keys */
if (!io_buf_write_tuples_list(tnt->io_buf, keys_list))
return;
/* send iproto request */
if (!io_buf_send_iproto(tnt->stream, TARANTOOL_COMMAND_SELECT, 0, tnt->io_buf))
return;
/*
* receive response
*/
/* clean-up buffer */
io_buf_clean(tnt->io_buf);
/* receive */
if (!io_buf_recv_iproto(tnt->stream, tnt->io_buf))
return;
/* read response */
struct tnt_response *response;
if (!io_buf_read_struct(tnt->io_buf, (void **) &response, sizeof(struct tnt_response))) {
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "select failed: invalid response was received");
return;
}
/* check return code */
if (response->return_code) {
/* error happen, throw exceprion */
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC,
"select failed: %"PRIi32"(0x%08"PRIx32"): %s",
response->return_code,
response->return_code,
response->return_msg);
return;
}
if (array_init(return_value) != SUCCESS) {
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC,
"select failed: create array failed");
return;
}
/* put count to result array */
add_assoc_long(return_value, "count", response->count);
/* put tuple list to result array */
zval *tuples_list;
MAKE_STD_ZVAL(tuples_list);
if (array_init(tuples_list) == FAILURE) {
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC,
"select failed: create array failed");
return;
}
/* read tuples for responce */
int i;
for (i = 0; i < response->count; ++i) {
zval *tuple;
if (!io_buf_read_tuple(tnt->io_buf, &tuple)) {
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC,
"select failed: invalid response was received");
return;
}
add_next_index_zval(tuples_list, tuple);
}
add_assoc_zval(return_value, "tuples_list", tuples_list);
}
PHP_METHOD(tarantool_class, insert)
{
/*
* parse methods parameters
*/
zval *id;
long space_no = 0;
long flags = 0;
zval *tuple = NULL;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ola|l", &id, tarantool_ce, &space_no, &tuple, &flags) == FAILURE) {
return;
}
tarantool_object *tnt = (tarantool_object *) zend_object_store_get_object(
id TSRMLS_CC);
/*
* send request
*/
/* clean-up buffer */
io_buf_clean(tnt->io_buf);
/* fill insert command */
struct tnt_insert_request *request = (struct tnt_insert_request *) io_buf_write_struct(
tnt->io_buf, sizeof(struct tnt_insert_request));
if (request == NULL)
return;
/* space number */
request->space_no = space_no;
/* flags */
request->flags = flags;
/* tuple */
if (!io_buf_write_tuple(tnt->io_buf, tuple))
return;
/* send iproto request */
if (!io_buf_send_iproto(tnt->stream, TARANTOOL_COMMAND_INSERT, 0, tnt->io_buf))
return;
/*
* receive response
*/
/* clean-up buffer */
io_buf_clean(tnt->io_buf);
/* receive */
if (!io_buf_recv_iproto(tnt->stream, tnt->io_buf))
return;
/* read response */
struct tnt_response *response;
if (!io_buf_read_struct(tnt->io_buf,
(void **) &response,
sizeof(struct tnt_response))) {
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC,
"insert failed: invalid response was received");
return;
}
/* check return code */
if (response->return_code) {
/* error happen, throw exceprion */
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC,
"insert failed: %"PRIi32"(0x%08"PRIx32"): %s",
response->return_code,
response->return_code,
response->return_msg);
return;
}
/*
* fill return value
*/
if (array_init(return_value) != SUCCESS) {
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC,
"insert failed: create array failed");
return;
}
/* put count to result array */
add_assoc_long(return_value, "count", response->count);
/* check "return tuple" flag */
if (flags & TARANTOOL_FLAGS_RETURN_TUPLE) {
/* ok, the responce should contain inserted tuple */
if (!io_buf_read_tuple(tnt->io_buf, &tuple)) {
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC,
"insert failed: invalid response was received");
return;
}
/* put returned tuple to result array */
add_assoc_zval(return_value, "tuple", tuple);
}
}
PHP_METHOD(tarantool_class, update_fields)
{
/*
* parse methods parameters
*/
zval *id;
long space_no = 0;
long flags = 0;
zval *tuple = NULL;
zval *op_list = NULL;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Olza|l", &id, tarantool_ce, &space_no, &tuple, &op_list, &flags) == FAILURE) {
return;
}
tarantool_object *tnt = (tarantool_object *) zend_object_store_get_object(
id TSRMLS_CC);
/*
* send request
*/
/* clean-up buffer */
io_buf_clean(tnt->io_buf);
/* fill insert command */
struct tnt_update_fields_request *request = (struct tnt_update_fields_request *) io_buf_write_struct(
tnt->io_buf, sizeof(struct tnt_update_fields_request));
if (request == NULL)
return;
/* space number */
request->space_no = space_no;
/* flags */
request->flags = flags;
/* tuple */
if (!io_buf_write_tuple(tnt->io_buf, tuple))
return;
HashTable *op_list_array = Z_ARRVAL_P(op_list);
int op_count = zend_hash_num_elements(op_list_array);
/* write number of update fields operaion */
if (!io_buf_write_int32(tnt->io_buf, op_count))
return;
HashPosition itr;
zval **op;
for (zend_hash_internal_pointer_reset_ex(op_list_array, &itr);
zend_hash_get_current_data_ex(op_list_array, (void **) &op, &itr) == SUCCESS;
zend_hash_move_forward_ex(op_list_array, &itr)) {
/* check operation type */
if (Z_TYPE_PP(op) != IS_ARRAY) {
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC,
"invalid operations list");
return;
}
HashTable *op_array = Z_ARRVAL_PP(op);
long field_no;
long opcode;
if (!hash_find_long(op_array, "field", &field_no)) {
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC,
"can't find 'field' in the update field operation");
return;
}
if (!hash_find_long(op_array, "op", &opcode)) {
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC,
"can't find 'op' in the update field operation");
return;
}
/* write field number */
if (!io_buf_write_int32(tnt->io_buf, field_no))
return;
/* write operation code */
if (!io_buf_write_byte(tnt->io_buf, opcode))
return;
zval **assing_arg = NULL;
long arith_arg;
long splice_offset;
long splice_length;
char *splice_list;
int splice_list_len;
switch (opcode) {
case TARANTOOL_OP_ASSIGN:
if (!hash_find_scalar(op_array, "arg", &assing_arg)) {
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC,
"can't find 'arg' in the update field operation");
return;
}
if (Z_TYPE_PP(assing_arg) == IS_LONG) {
/* write as interger */
if (!io_buf_write_field_str(tnt->io_buf, (uint8_t *) &Z_LVAL_PP(assing_arg), sizeof(int32_t)))
return;
} else {
/* write as string */
if (!io_buf_write_field_str(tnt->io_buf, (uint8_t *) Z_STRVAL_PP(assing_arg), Z_STRLEN_PP(assing_arg)))
return;
}
break;
case TARANTOOL_OP_ADD:
case TARANTOOL_OP_AND:
case TARANTOOL_OP_XOR:
case TARANTOOL_OP_OR:
if (!hash_find_long(op_array, "arg", &arith_arg)) {
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC,
"can't find 'arg' in the update field operation");
return;
}
/* write arith arg */
if (!io_buf_write_field_str(tnt->io_buf, (uint8_t *) &arith_arg, sizeof(int32_t)))
return;
break;
case TARANTOOL_OP_SPLICE:
/*
* read splice args
*/
/* read offset */
if (!hash_find_long(op_array, "offset", &splice_offset)) {
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC,
"can't find 'offset' in the update field operation");
return;
}
/* read length */
if (!hash_find_long(op_array, "length", &splice_length)) {
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC,
"can't find 'length' in the update field operation");
return;
}
/* read list */
if (!hash_find_str(op_array, "list", &splice_list, &splice_list_len)) {
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC,
"can't find 'list' in the update field operation");
return;
}
/*
* write splice args
*/
io_buf_clean(tnt->splice_field);
/* write offset to separate buffer */
if (!io_buf_write_field_str(tnt->splice_field, (uint8_t *) &splice_offset, sizeof(int32_t)))
return;
/* write length to separate buffer */
if (!io_buf_write_field_str(tnt->splice_field, (uint8_t *) &splice_length, sizeof(int32_t)))
return;
/* write list to separate buffer */
if (!io_buf_write_field_str(tnt->splice_field, (uint8_t *) splice_list, splice_list_len))
return;
/* write splice args as alone field */
if (!io_buf_write_field_str(tnt->io_buf, tnt->splice_field->value, tnt->splice_field->size))
return;
break;
default:
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC,
"invalid operaion code %i", opcode);
return;
}
}
/* send iproto request */
if (!io_buf_send_iproto(tnt->stream, TARANTOOL_COMMAND_UPDATE, 0, tnt->io_buf))
return;
/*
* receive response
*/
/* clean-up buffer */
io_buf_clean(tnt->io_buf);
/* receive */
if (!io_buf_recv_iproto(tnt->stream, tnt->io_buf))
return;
/* read response */
struct tnt_response *response;
if (!io_buf_read_struct(tnt->io_buf,
(void **) &response,
sizeof(struct tnt_response))) {
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC,
"update fields failed: invalid response was received");
return;
}
/* check return code */
if (response->return_code) {
/* error happen, throw exceprion */
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC,
"update fields failed: %"PRIi32"(0x%08"PRIx32"): %s",
response->return_code,
response->return_code,
response->return_msg);
return;
}
/*
* fill return value
*/
if (array_init(return_value) != SUCCESS) {
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC,
"update fields failed: create array failed");
return;
}
/* put count to result array */
add_assoc_long(return_value, "count", response->count);
/* check "return tuple" flag */
if ((response->count > 0) && (flags & TARANTOOL_FLAGS_RETURN_TUPLE)) {
/* ok, the responce should contain inserted tuple */
if (!io_buf_read_tuple(tnt->io_buf, &tuple)) {
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC,
"update fields failed: invalid response was received");
return;
}
/* put returned tuple to result array */
add_assoc_zval(return_value, "tuple", tuple);
}
}
PHP_METHOD(tarantool_class, delete)
{
/*
* parse methods parameters
*/
zval *id;
long space_no = 0;
long flags = 0;
zval *tuple = NULL;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Olz|l", &id, tarantool_ce, &space_no, &tuple, &flags) == FAILURE) {
return;
}
tarantool_object *tnt = (tarantool_object *) zend_object_store_get_object(
id TSRMLS_CC);
/*
* send request
*/
/* clean-up buffer */
io_buf_clean(tnt->io_buf);
/* fill delete command */
struct tnt_delete_request *request = (struct tnt_delete_request *) io_buf_write_struct(
tnt->io_buf, sizeof(struct tnt_delete_request));
if (request == NULL)
return;
/* space number */
request->space_no = space_no;
/* flags */
request->flags = flags;
/* tuple */
if (!io_buf_write_tuple(tnt->io_buf, tuple))
return;
/* send iproto request */
if (!io_buf_send_iproto(tnt->stream, TARANTOOL_COMMAND_DELETE, 0, tnt->io_buf))
return;