-
Notifications
You must be signed in to change notification settings - Fork 44
/
leveldb.c
1664 lines (1313 loc) · 45.4 KB
/
leveldb.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-2016 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: Reeze Xia <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "ext/standard/info.h"
#include "Zend/zend_exceptions.h"
#include "Zend/zend_interfaces.h"
#include "php_leveldb.h"
#include <leveldb/c.h>
#define LEVELDB_CHECK_OPEN_BASEDIR(file) \
if (php_check_open_basedir((file))){ \
RETURN_FALSE; \
}
#define PHP_LEVELDB_ERROR_DB_CLOSED 1
#define PHP_LEVELDB_ERROR_ITERATOR_CLOSED 2
#define LEVELDB_CHECK_DB_NOT_CLOSED(db_object) \
if ((db_object)->db == NULL) { \
zend_throw_exception(php_leveldb_ce_LevelDBException, "Can not operate on closed db", PHP_LEVELDB_ERROR_DB_CLOSED); \
return; \
}
/* PHP-LevelDB MAGIC identifier don't change this */
#define PHP_LEVELDB_CUSTOM_COMPARATOR_NAME "php_leveldb.custom_comparator"
#define php_leveldb_obj_new(class_type, ce) \
class_type *intern; \
\
intern = (class_type *)ecalloc(1, sizeof(class_type) + zend_object_properties_size(ce)); \
\
zend_object_std_init(&intern->std, ce); \
object_properties_init(&intern->std, ce); \
\
intern->std.handlers = & class_type##_handlers; \
\
return &intern->std;
#define LEVELDB_CHECK_ERROR(err) \
if ((err) != NULL) { \
zend_throw_exception(php_leveldb_ce_LevelDBException, err, 0); \
leveldb_free(err); \
return; \
}
/* {{{ leveldb_functions[]
*/
const zend_function_entry leveldb_functions[] = {
PHP_FE_END /* Must be the last line in leveldb_functions[] */
};
/* }}} */
/* {{{ leveldb_module_entry
*/
zend_module_entry leveldb_module_entry = {
STANDARD_MODULE_HEADER,
"leveldb",
leveldb_functions,
PHP_MINIT(leveldb),
PHP_MSHUTDOWN(leveldb),
NULL,
NULL,
PHP_MINFO(leveldb),
PHP_LEVELDB_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_LEVELDB
ZEND_GET_MODULE(leveldb)
#endif
/* Handlers */
static zend_object_handlers leveldb_object_handlers;
static zend_object_handlers leveldb_snapshot_object_handlers;
static zend_object_handlers leveldb_write_batch_object_handlers;
static zend_object_handlers leveldb_iterator_object_handlers;
/* Class entries */
zend_class_entry *php_leveldb_ce_LevelDBException;
zend_class_entry *php_leveldb_class_entry;
zend_class_entry *php_leveldb_write_batch_class_entry;
zend_class_entry *php_leveldb_iterator_class_entry;
zend_class_entry *php_leveldb_snapshot_class_entry;
/* Objects */
typedef struct {
leveldb_t *db;
/* default read options */
unsigned char verify_check_sum;
unsigned char fill_cache;
/* default write options */
unsigned char sync;
/* custom comparator */
leveldb_comparator_t *comparator;
zend_string *callable_name;
zend_object std;
} leveldb_object;
typedef struct {
leveldb_writebatch_t *batch;
zend_object std;
} leveldb_write_batch_object;
typedef struct {
leveldb_iterator_t *iterator;
leveldb_object *db;
zval zdb;
zend_object std;
} leveldb_iterator_object;
/* define an overloaded iterator structure */
typedef struct {
zend_object_iterator intern;
leveldb_iterator_object *iterator_obj;
zval *current;
} leveldb_iterator_iterator;
typedef struct {
leveldb_object *db;
zval zdb;
leveldb_snapshot_t *snapshot;
zend_object std;
} leveldb_snapshot_object;
#define LEVELDB_INTERNAL_OBJ_FROM_ZOBJ(class_name, obj) ((class_name *)((char *)(obj) - XtOffsetOf(class_name, std)))
#define LEVELDB_INTERNAL_OBJ_FROM_ZV(obj_name, zv) LEVELDB_INTERNAL_OBJ_FROM_ZOBJ(obj_name, Z_OBJ_P(zv))
#define LEVELDB_OBJ_FROM_ZV(zv) LEVELDB_INTERNAL_OBJ_FROM_ZV(leveldb_object, zv)
#define LEVELDB_SNAPSHOT_OBJ_FROM_ZV(zv) LEVELDB_INTERNAL_OBJ_FROM_ZV(leveldb_snapshot_object, zv)
#define LEVELDB_WRITE_BATCH_OBJ_FROM_ZV(zv) LEVELDB_INTERNAL_OBJ_FROM_ZV(leveldb_write_batch_object, zv)
#define LEVELDB_ITERATOR_OBJ_FROM_ZV(zv) LEVELDB_INTERNAL_OBJ_FROM_ZV(leveldb_iterator_object, zv)
#define LEVELDB_OBJ_FROM_ZOBJ(obj) LEVELDB_INTERNAL_OBJ_FROM_ZOBJ(leveldb_object, obj)
#define LEVELDB_SNAPSHOT_OBJ_FROM_ZOBJ(obj) LEVELDB_INTERNAL_OBJ_FROM_ZOBJ(leveldb_snapshot_object, obj)
#define LEVELDB_WRITE_BATCH_OBJ_FROM_ZOBJ(obj) LEVELDB_INTERNAL_OBJ_FROM_ZOBJ(leveldb_write_batch_object, obj)
#define LEVELDB_ITERATOR_OBJ_FROM_ZOBJ(obj) LEVELDB_INTERNAL_OBJ_FROM_ZOBJ(leveldb_iterator_object, obj)
void php_leveldb_object_free(zend_object *std)
{
leveldb_object *obj = LEVELDB_OBJ_FROM_ZOBJ(std);
if (obj->db) {
leveldb_close(obj->db);
}
if (obj->comparator) {
leveldb_comparator_destroy(obj->comparator);
zend_string_release(obj->callable_name);
}
zend_object_std_dtor(std);
}
static zend_object* php_leveldb_object_new(zend_class_entry *class_type)
{
php_leveldb_obj_new(leveldb_object, class_type);
}
/* Write batch object operate */
void php_leveldb_write_batch_object_free(zend_object *std)
{
leveldb_write_batch_object *obj = LEVELDB_WRITE_BATCH_OBJ_FROM_ZOBJ(std);
if (obj->batch) {
leveldb_writebatch_destroy(obj->batch);
}
zend_object_std_dtor(std);
}
static zend_object* php_leveldb_write_batch_object_new(zend_class_entry *class_type)
{
php_leveldb_obj_new(leveldb_write_batch_object, class_type);
}
/* Iterator object operate */
void php_leveldb_iterator_object_free(zend_object *std)
{
leveldb_iterator_object *obj = LEVELDB_ITERATOR_OBJ_FROM_ZOBJ(std);
if (obj->db && obj->db->db && obj->iterator) {
leveldb_iter_destroy(obj->iterator);
/* decr. obj counter */
zval_ptr_dtor(&obj->zdb);
}
zend_object_std_dtor(std);
}
static zend_object* php_leveldb_iterator_object_new(zend_class_entry *class_type)
{
php_leveldb_obj_new(leveldb_iterator_object, class_type);
}
/* Snapshot object operate */
void php_leveldb_snapshot_object_free(zend_object *std)
{
leveldb_t *db;
leveldb_snapshot_object *obj = LEVELDB_SNAPSHOT_OBJ_FROM_ZOBJ(std);
if (obj->snapshot && obj->db) {
if((db = obj->db->db) != NULL) {
leveldb_release_snapshot(db, obj->snapshot);
}
/* decr. obj counter */
zval_ptr_dtor(&obj->zdb);
}
zend_object_std_dtor(std);
}
static zend_object* php_leveldb_snapshot_object_new(zend_class_entry *class_type)
{
php_leveldb_obj_new(leveldb_snapshot_object, class_type);
}
/* arg info */
ZEND_BEGIN_ARG_INFO_EX(arginfo_leveldb_void, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_leveldb_construct, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
ZEND_ARG_ARRAY_INFO(0, options, 0)
ZEND_ARG_ARRAY_INFO(0, read_options, 0)
ZEND_ARG_ARRAY_INFO(0, write_options, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_leveldb_get, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
ZEND_ARG_ARRAY_INFO(0, read_options, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_leveldb_set, 0, 0, 2)
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, value, IS_STRING, 0)
ZEND_ARG_ARRAY_INFO(0, write_options, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_leveldb_delete, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
ZEND_ARG_ARRAY_INFO(0, write_options, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_leveldb_write, 0, 0, 1)
ZEND_ARG_OBJ_INFO(0, batch, LevelDBWriteBatch, 0)
ZEND_ARG_ARRAY_INFO(0, write_options, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_leveldb_getProperty, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_leveldb_getApproximateSizes, 0, 0, 2)
ZEND_ARG_ARRAY_INFO(0, start, 0)
ZEND_ARG_ARRAY_INFO(0, limit, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_leveldb_compactRange, 0, 0, 2)
ZEND_ARG_TYPE_INFO(0, start, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, limit, IS_STRING, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_leveldb_get_iterator, 0, 0, 0)
ZEND_ARG_ARRAY_INFO(0, options, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_leveldb_destroy, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
ZEND_ARG_ARRAY_INFO(0, options, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_leveldb_repair, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
ZEND_ARG_ARRAY_INFO(0, options, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_leveldb_iterator_construct, 0, 0, 1)
ZEND_ARG_OBJ_INFO(0, db, LevelDB, 0)
ZEND_ARG_ARRAY_INFO(0, read_options, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_leveldb_iterator_seek, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_leveldb_snapshot_construct, 0, 0, 1)
ZEND_ARG_OBJ_INFO(0, db, LevelDB, 0)
ZEND_END_ARG_INFO()
/* Methods */
/* {{{ php_leveldb_class_methods */
static zend_function_entry php_leveldb_class_methods[] = {
PHP_ME(LevelDB, __construct, arginfo_leveldb_construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(LevelDB, get, arginfo_leveldb_get, ZEND_ACC_PUBLIC)
PHP_ME(LevelDB, set, arginfo_leveldb_set, ZEND_ACC_PUBLIC)
/* make put an alias of set, since leveldb use put */
PHP_MALIAS(LevelDB, put, set, arginfo_leveldb_set, ZEND_ACC_PUBLIC)
PHP_ME(LevelDB, delete, arginfo_leveldb_delete, ZEND_ACC_PUBLIC)
PHP_ME(LevelDB, write, arginfo_leveldb_write, ZEND_ACC_PUBLIC)
PHP_ME(LevelDB, getProperty, arginfo_leveldb_getProperty, ZEND_ACC_PUBLIC)
PHP_ME(LevelDB, getApproximateSizes, arginfo_leveldb_getApproximateSizes, ZEND_ACC_PUBLIC)
PHP_ME(LevelDB, compactRange, arginfo_leveldb_compactRange, ZEND_ACC_PUBLIC)
PHP_ME(LevelDB, close, arginfo_leveldb_void, ZEND_ACC_PUBLIC | ZEND_ACC_DEPRECATED)
PHP_ME(LevelDB, getIterator, arginfo_leveldb_get_iterator, ZEND_ACC_PUBLIC)
PHP_ME(LevelDB, getSnapshot, arginfo_leveldb_void, ZEND_ACC_PUBLIC)
PHP_ME(LevelDB, destroy, arginfo_leveldb_destroy, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(LevelDB, repair, arginfo_leveldb_repair, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_FE_END
};
/* }}} */
/* {{{ php_leveldb_write_batch_class_methods */
static zend_function_entry php_leveldb_write_batch_class_methods[] = {
PHP_ME(LevelDBWriteBatch, __construct, arginfo_leveldb_void, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(LevelDBWriteBatch, set, arginfo_leveldb_set, ZEND_ACC_PUBLIC)
/* make put an alias of set, since leveldb use put */
PHP_MALIAS(LevelDBWriteBatch, put, set, arginfo_leveldb_set, ZEND_ACC_PUBLIC)
PHP_ME(LevelDBWriteBatch, delete, arginfo_leveldb_delete, ZEND_ACC_PUBLIC)
PHP_ME(LevelDBWriteBatch, clear, arginfo_leveldb_void, ZEND_ACC_PUBLIC)
PHP_FE_END
};
/* }}} */
/* {{{ php_leveldb_iterator_class_methods */
static zend_function_entry php_leveldb_iterator_class_methods[] = {
PHP_ME(LevelDBIterator, __construct, arginfo_leveldb_iterator_construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(LevelDBIterator, valid, arginfo_leveldb_void, ZEND_ACC_PUBLIC)
PHP_ME(LevelDBIterator, rewind, arginfo_leveldb_void, ZEND_ACC_PUBLIC)
PHP_ME(LevelDBIterator, last, arginfo_leveldb_void, ZEND_ACC_PUBLIC)
PHP_ME(LevelDBIterator, seek, arginfo_leveldb_iterator_seek, ZEND_ACC_PUBLIC)
PHP_ME(LevelDBIterator, next, arginfo_leveldb_void, ZEND_ACC_PUBLIC)
PHP_ME(LevelDBIterator, prev, arginfo_leveldb_void, ZEND_ACC_PUBLIC)
PHP_ME(LevelDBIterator, key, arginfo_leveldb_void, ZEND_ACC_PUBLIC)
PHP_ME(LevelDBIterator, current, arginfo_leveldb_void, ZEND_ACC_PUBLIC)
PHP_ME(LevelDBIterator, getError, arginfo_leveldb_void, ZEND_ACC_PUBLIC)
PHP_ME(LevelDBIterator, destroy, arginfo_leveldb_void, ZEND_ACC_PUBLIC)
PHP_FE_END
};
/* }}} */
/* {{{ php_leveldb_iterator_class_methods */
static zend_function_entry php_leveldb_snapshot_class_methods[] = {
PHP_ME(LevelDBSnapshot, __construct, arginfo_leveldb_snapshot_construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(LevelDBSnapshot, release, arginfo_leveldb_void, ZEND_ACC_PUBLIC)
PHP_FE_END
};
/* }}} */
static void leveldb_custom_comparator_destructor(void *stat)
{
zval *callable = (zval *)stat;
zval_ptr_dtor(callable);
efree(callable);
}
static int leveldb_custom_comparator_compare(void *stat, const char *a, size_t alen, const char *b, size_t blen)
{
zval *callable = (zval *)stat;
zval params[2], result;
int ret;
ZVAL_STRINGL(¶ms[0], (char *)a, alen);
ZVAL_STRINGL(¶ms[1], (char *)b, blen);
ZVAL_NULL(&result);
assert(!ZVAL_IS_NULL(callable));
if (call_user_function(EG(function_table), NULL, callable, &result, 2, params) == SUCCESS && !Z_ISUNDEF(result)) {
convert_to_long(&result);
ret = Z_LVAL(result);
}else{
ret = 0;
}
zval_ptr_dtor(¶ms[0]);
zval_ptr_dtor(¶ms[1]);
zval_ptr_dtor(&result);
return ret;
}
static const char* leveldb_custom_comparator_name(void *stat)
{
return PHP_LEVELDB_CUSTOM_COMPARATOR_NAME;
}
static inline leveldb_options_t* php_leveldb_get_open_options(zval *options_zv, leveldb_comparator_t **out_comparator, zend_string **callable_name)
{
zval *value;
HashTable *ht;
leveldb_options_t *options = leveldb_options_create();
/* default true */
leveldb_options_set_create_if_missing(options, 1);
if (options_zv == NULL) {
return options;
}
ht = Z_ARRVAL_P(options_zv);
if ((value = zend_hash_str_find(ht, ZEND_STRL("create_if_missing"))) != NULL) {
leveldb_options_set_create_if_missing(options, zend_is_true(value));
}
if ((value = zend_hash_str_find(ht, ZEND_STRL("error_if_exists"))) != NULL) {
leveldb_options_set_error_if_exists(options, zend_is_true(value));
}
if ((value = zend_hash_str_find(ht, ZEND_STRL("paranoid_checks"))) != NULL) {
leveldb_options_set_paranoid_checks(options, zend_is_true(value));
}
if ((value = zend_hash_str_find(ht, ZEND_STRL("write_buffer_size"))) != NULL) {
convert_to_long(value);
leveldb_options_set_write_buffer_size(options, Z_LVAL_P(value));
}
if ((value = zend_hash_str_find(ht, ZEND_STRL("max_open_files"))) != NULL) {
convert_to_long(value);
leveldb_options_set_max_open_files(options, Z_LVAL_P(value));
}
if ((value = zend_hash_str_find(ht, ZEND_STRL("block_size"))) != NULL) {
convert_to_long(value);
leveldb_options_set_block_size(options, Z_LVAL_P(value));
}
if ((value = zend_hash_str_find(ht, ZEND_STRL("block_cache_size"))) != NULL) {
convert_to_long(value);
leveldb_options_set_cache(options, leveldb_cache_create_lru(Z_LVAL_P(value)));
}
if ((value = zend_hash_str_find(ht, ZEND_STRL("block_restart_interval"))) != NULL) {
convert_to_long(value);
leveldb_options_set_block_restart_interval(options, Z_LVAL_P(value));
}
if ((value = zend_hash_str_find(ht, ZEND_STRL("compression"))) != NULL) {
convert_to_long(value);
switch (Z_LVAL_P(value))
{
case leveldb_no_compression:
case leveldb_snappy_compression:
#ifdef MOJANG_LEVELDB
case leveldb_zlib_compression:
case leveldb_zlib_raw_compression:
#endif
leveldb_options_set_compression(options, Z_LVAL_P(value));
break;
default:
php_error_docref(NULL, E_WARNING, "Unsupported compression type");
break;
}
}
if ((value = zend_hash_str_find(ht, ZEND_STRL("comparator"))) != NULL
&& !ZVAL_IS_NULL(value)) {
leveldb_comparator_t *comparator;
if (!zend_is_callable(value, 0, callable_name)) {
zend_throw_exception_ex(php_leveldb_ce_LevelDBException, 0,
"Invalid open option: comparator, %s() is not callable", ZSTR_VAL(*callable_name));
zend_string_release(*callable_name);
*callable_name = NULL;
leveldb_options_destroy(options);
return NULL;
}
zval *z2 = (zval *)emalloc(sizeof(zval));
ZVAL_COPY(z2, value);
comparator = leveldb_comparator_create((void *)(z2),
leveldb_custom_comparator_destructor, leveldb_custom_comparator_compare,
leveldb_custom_comparator_name);
if (comparator) {
*out_comparator = comparator;
}
leveldb_options_set_comparator(options, comparator);
}
return options;
}
static inline leveldb_readoptions_t *php_leveldb_get_readoptions(leveldb_object *intern, zval *options_zv)
{
zval *value;
HashTable *ht;
leveldb_readoptions_t *readoptions = leveldb_readoptions_create();
if (options_zv == NULL) {
return readoptions;
}
ht = Z_ARRVAL_P(options_zv);
if ((value = zend_hash_str_find(ht, ZEND_STRL("verify_check_sum"))) != NULL) {
leveldb_readoptions_set_verify_checksums(readoptions, zend_is_true(value));
} else {
leveldb_readoptions_set_verify_checksums(readoptions, intern->verify_check_sum);
}
if ((value = zend_hash_str_find(ht, ZEND_STRL("fill_cache"))) != NULL) {
leveldb_readoptions_set_fill_cache(readoptions, zend_is_true(value));
} else {
leveldb_readoptions_set_fill_cache(readoptions, intern->fill_cache);
}
if ((value = zend_hash_str_find(ht, ZEND_STRL("snapshot"))) != NULL
&& !ZVAL_IS_NULL(value)) {
if (Z_TYPE_P(value) == IS_OBJECT && Z_OBJCE_P(value) == php_leveldb_snapshot_class_entry) {
leveldb_snapshot_object *obj = LEVELDB_SNAPSHOT_OBJ_FROM_ZV(value);
if (obj->snapshot == NULL) {
zend_throw_exception_ex(php_leveldb_ce_LevelDBException, 0,
"Invalid snapshot parameter, it has been released");
leveldb_readoptions_destroy(readoptions);
return NULL;
}
leveldb_readoptions_set_snapshot(readoptions, obj->snapshot);
} else {
zend_throw_exception_ex(php_leveldb_ce_LevelDBException, 0,
"Invalid snapshot parameter, it must be an instance of LevelDBSnapshot");
leveldb_readoptions_destroy(readoptions);
return NULL;
}
}
return readoptions;
}
static inline void php_leveldb_set_readoptions(leveldb_object *intern, zval *options_zv)
{
zval *value;
HashTable *ht;
if (options_zv == NULL) {
return;
}
ht = Z_ARRVAL_P(options_zv);
if ((value = zend_hash_str_find(ht, ZEND_STRL("verify_check_sum"))) != NULL) {
intern->verify_check_sum = zend_is_true(value);
}
if ((value = zend_hash_str_find(ht, ZEND_STRL("fill_cache"))) != NULL) {
intern->fill_cache = zend_is_true(value);
}
}
static inline leveldb_writeoptions_t *php_leveldb_get_writeoptions(leveldb_object *intern, zval *options_zv)
{
zval *value;
HashTable *ht;
leveldb_writeoptions_t *writeoptions = leveldb_writeoptions_create();
if (options_zv == NULL) {
return writeoptions;
}
ht = Z_ARRVAL_P(options_zv);
if ((value = zend_hash_str_find(ht, ZEND_STRL("sync"))) != NULL) {
leveldb_writeoptions_set_sync(writeoptions, zend_is_true(value));
} else {
leveldb_writeoptions_set_sync(writeoptions, intern->sync);
}
return writeoptions;
}
static inline void php_leveldb_set_writeoptions(leveldb_object *intern, zval *options_zv)
{
zval *value;
if (options_zv == NULL) {
return;
}
if ((value = zend_hash_str_find(Z_ARRVAL_P(options_zv), ZEND_STRL("sync"))) != NULL) {
intern->sync = zend_is_true(value);
}
}
/* {{{ proto LevelDB LevelDB::__construct(string $name [, array $options [, array $readoptions [, array $writeoptions]]])
Instantiates a LevelDB object and opens the give database */
PHP_METHOD(LevelDB, __construct)
{
char *name;
size_t name_len;
zval *options_zv = NULL, *readoptions_zv = NULL, *writeoptions_zv = NULL;
char *err = NULL;
leveldb_t *db = NULL;
leveldb_options_t *openoptions;
leveldb_object *intern;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|a!a!a!",
&name, &name_len, &options_zv, &readoptions_zv, &writeoptions_zv) == FAILURE) {
return;
}
LEVELDB_CHECK_OPEN_BASEDIR(name);
intern = LEVELDB_OBJ_FROM_ZV(getThis());
if (intern->db) {
leveldb_close(intern->db);
}
openoptions = php_leveldb_get_open_options(options_zv, &intern->comparator, &intern->callable_name);
if (!openoptions) {
return;
}
php_leveldb_set_readoptions(intern, readoptions_zv);
php_leveldb_set_writeoptions(intern, writeoptions_zv);
db = leveldb_open(openoptions, (const char *)name, &err);
leveldb_options_destroy(openoptions);
LEVELDB_CHECK_ERROR(err);
intern->db = db;
}
/* }}} */
/* {{{ proto mixed LevelDB::get(string $key [, array $read_options])
Returns the value for the given key or false */
PHP_METHOD(LevelDB, get)
{
char *key, *value;
size_t key_len;
size_t value_len;
zval *readoptions_zv = NULL;
char *err = NULL;
leveldb_readoptions_t *readoptions;
leveldb_object *intern;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|z",
&key, &key_len, &readoptions_zv) == FAILURE) {
return;
}
intern = LEVELDB_OBJ_FROM_ZV(getThis());
LEVELDB_CHECK_DB_NOT_CLOSED(intern);
readoptions = php_leveldb_get_readoptions(intern, readoptions_zv);
value = leveldb_get(intern->db, readoptions, key, key_len, &value_len, &err);
leveldb_readoptions_destroy(readoptions);
LEVELDB_CHECK_ERROR(err);
if (value == NULL) {
RETURN_FALSE;
}
RETVAL_STRINGL(value, value_len);
leveldb_free(value);
}
/* }}} */
/* {{{ proto bool LevelDB::set(string $key, string $value [, array $write_options])
An alias method for LevelDB::put() */
/* proto bool LevelDB::put(string $key, string $value [, array $write_options])
Puts the value for the given key */
PHP_METHOD(LevelDB, set)
{
char *key, *value;
size_t key_len, value_len;
zval *writeoptions_zv = NULL;
char *err = NULL;
leveldb_writeoptions_t *writeoptions;
leveldb_object *intern;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss|z",
&key, &key_len, &value, &value_len, &writeoptions_zv) == FAILURE) {
return;
}
intern = LEVELDB_OBJ_FROM_ZV(getThis());
LEVELDB_CHECK_DB_NOT_CLOSED(intern);
writeoptions = php_leveldb_get_writeoptions(intern, writeoptions_zv);
leveldb_put(intern->db, writeoptions, key, key_len, value, value_len, &err);
leveldb_writeoptions_destroy(writeoptions);
LEVELDB_CHECK_ERROR(err);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool LevelDB::delete(string $key [, array $write_options])
Deletes the given key */
PHP_METHOD(LevelDB, delete)
{
char *key;
size_t key_len;
zval *writeoptions_zv = NULL;
char *err = NULL;
leveldb_writeoptions_t *writeoptions;
leveldb_object *intern;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|z",
&key, &key_len, &writeoptions_zv) == FAILURE) {
return;
}
intern = LEVELDB_OBJ_FROM_ZV(getThis());
LEVELDB_CHECK_DB_NOT_CLOSED(intern);
writeoptions = php_leveldb_get_writeoptions(intern, writeoptions_zv);
leveldb_delete(intern->db, writeoptions, key, key_len, &err);
leveldb_writeoptions_destroy(writeoptions);
LEVELDB_CHECK_ERROR(err);
if (err != NULL) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool LevelDB::write(LevelDBWriteBatch $batch [, array $write_options])
Executes all of the operations added in the write batch */
PHP_METHOD(LevelDB, write)
{
zval *writeoptions_zv = NULL;
zval *write_batch;
char *err = NULL;
leveldb_object *intern;
leveldb_writeoptions_t *writeoptions;
leveldb_write_batch_object *write_batch_object;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z",
&write_batch, php_leveldb_write_batch_class_entry, &writeoptions_zv) == FAILURE) {
return;
}
intern = LEVELDB_OBJ_FROM_ZV(getThis());
LEVELDB_CHECK_DB_NOT_CLOSED(intern);
writeoptions = php_leveldb_get_writeoptions(intern, writeoptions_zv);
write_batch_object = LEVELDB_WRITE_BATCH_OBJ_FROM_ZV(write_batch);
leveldb_write(intern->db, writeoptions, write_batch_object->batch, &err);
leveldb_writeoptions_destroy(writeoptions);
LEVELDB_CHECK_ERROR(err);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto mixed LevelDB::getProperty(string $name)
Returns the property of the db */
PHP_METHOD(LevelDB, getProperty)
{
char *name, *property;
size_t name_len;
leveldb_object *intern;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name, &name_len) == FAILURE) {
return;
}
intern = LEVELDB_OBJ_FROM_ZV(getThis());
LEVELDB_CHECK_DB_NOT_CLOSED(intern);
property = leveldb_property_value(intern->db, (const char *)name);
if (property == NULL) {
RETURN_FALSE;
}
RETVAL_STRING(property);
leveldb_free(property);
}
/* }}} */
/* {{{ proto void LevelDB::compactRange(string $start, string $limit)
Compact the range between the specified */
PHP_METHOD(LevelDB, compactRange)
{
char *start, *limit;
size_t start_len, limit_len;
leveldb_object *intern;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &start, &start_len, &limit, &limit_len) == FAILURE) {
return;
}
intern = LEVELDB_OBJ_FROM_ZV(getThis());
LEVELDB_CHECK_DB_NOT_CLOSED(intern);
leveldb_compact_range(intern->db, (const char *)start, (size_t)start_len, (const char *)limit, (size_t)limit_len);
}
/* }}} */
/* {{{ proto mixed LevelDB::getApproximateSizes(array $start, array $limit)
Get the approximate number of bytes of file system space used by one or more key ranges
or return false on failure */
PHP_METHOD(LevelDB, getApproximateSizes)
{
leveldb_object *intern;
zval *start, *limit, *start_val, *limit_val;
char **start_key, **limit_key;
size_t *start_len, *limit_len;
unsigned int num_ranges, i = 0;
HashPosition pos_start, pos_limit;
uint64_t *sizes;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "aa", &start, &limit) == FAILURE) {
return;
}
intern = LEVELDB_OBJ_FROM_ZV(getThis());
LEVELDB_CHECK_DB_NOT_CLOSED(intern);
num_ranges = zend_hash_num_elements(Z_ARRVAL_P(start));
if (num_ranges != zend_hash_num_elements(Z_ARRVAL_P(limit))) {
php_error_docref(NULL, E_WARNING, "The num of start keys and limit keys didn't match");
RETURN_FALSE;
}
array_init(return_value);
start_key = emalloc(num_ranges * sizeof(char *));
limit_key = emalloc(num_ranges * sizeof(char *));
start_len = emalloc(num_ranges * sizeof(size_t));
limit_len = emalloc(num_ranges * sizeof(size_t));
sizes = emalloc(num_ranges * sizeof(uint64_t));
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(start), &pos_start);
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(limit), &pos_limit);
while ((start_val = zend_hash_get_current_data_ex(Z_ARRVAL_P(start), &pos_start)) != NULL &&
(limit_val = zend_hash_get_current_data_ex(Z_ARRVAL_P(limit), &pos_limit)) != NULL) {
start_key[i] = Z_STRVAL_P(start_val);
start_len[i] = Z_STRLEN_P(start_val);
limit_key[i] = Z_STRVAL_P(limit_val);
limit_len[i] = Z_STRLEN_P(limit_val);
zend_hash_move_forward_ex(Z_ARRVAL_P(start), &pos_start);
zend_hash_move_forward_ex(Z_ARRVAL_P(limit), &pos_limit);
++i;
}
leveldb_approximate_sizes(intern->db, num_ranges,
(const char* const *)start_key, (const size_t *)start_len,
(const char* const *)limit_key, (const size_t *)limit_len, sizes);
for (i = 0; i < num_ranges; ++i) {
add_next_index_long(return_value, sizes[i]);
}
efree(start_key);
efree(start_len);
efree(limit_key);
efree(limit_len);
efree(sizes);
}
/* }}} */
/* {{{ proto bool LevelDB::close()
Close the opened db */
PHP_METHOD(LevelDB, close)
{
if (zend_parse_parameters_none() == FAILURE) {
return;
}
/* NOOP and deprecated function */
RETURN_TRUE;
}
/* }}} */
/* {{{ proto LevelDBIterator LevelDB::getIterator([array $read_options])
Gets a new iterator for the db */
PHP_METHOD(LevelDB, getIterator)
{
zval *readoptions_zv = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|a", &readoptions_zv) == FAILURE) {
return;
}
object_init_ex(return_value, php_leveldb_iterator_class_entry);
#if PHP_VERSION_ID < 80000
zend_call_method(return_value, php_leveldb_iterator_class_entry,
#else
zend_call_method(Z_OBJ_P(return_value), php_leveldb_iterator_class_entry,
#endif
&php_leveldb_iterator_class_entry->constructor, "__construct", sizeof("__construct") - 1,
NULL, (readoptions_zv == NULL ? 1 : 2), getThis(), readoptions_zv);
}
/* }}} */
/* {{{ proto LevelDBSnapshot LevelDB::getSnapshot()
Gets a new snapshot for the db */
PHP_METHOD(LevelDB, getSnapshot)
{
if (zend_parse_parameters_none() == FAILURE) {
return;
}
object_init_ex(return_value, php_leveldb_snapshot_class_entry);
#if PHP_VERSION_ID < 80000
zend_call_method_with_1_params(return_value, php_leveldb_snapshot_class_entry,
&php_leveldb_snapshot_class_entry->constructor, "__construct", NULL, getThis());
#else
zend_call_method_with_1_params(Z_OBJ_P(return_value), php_leveldb_snapshot_class_entry,
&php_leveldb_snapshot_class_entry->constructor, "__construct", NULL, getThis());
#endif
}
/* }}} */
/* {{{ proto bool LevelDB::destroy(string $name [, array $options])
Destroy the contents of the specified database */
PHP_METHOD(LevelDB, destroy)
{
char *name;
size_t name_len;
zval *options_zv = NULL;
char *err = NULL;
zend_string *callable_name = NULL;
leveldb_options_t *options;
leveldb_comparator_t *comparator = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|z",
&name, &name_len, &options_zv) == FAILURE) {
return;
}
LEVELDB_CHECK_OPEN_BASEDIR(name);
options = php_leveldb_get_open_options(options_zv, &comparator, &callable_name);
if (!options) {
return;
}
leveldb_destroy_db(options, name, &err);
if (comparator) {
leveldb_comparator_destroy(comparator);
zend_string_release(callable_name);
}