-
Notifications
You must be signed in to change notification settings - Fork 3
/
SeasSnowflake.cpp
342 lines (293 loc) · 9.77 KB
/
SeasSnowflake.cpp
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
/*
+----------------------------------------------------------------------+
| SeasSnowflake |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2021 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: SeasX Group <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
extern "C" {
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "Zend/zend_exceptions.h"
#include "php7_wrapper.h"
}
#include "php_SeasSnowflake.h"
#include <iostream>
#include <map>
#include <sstream>
#include "./src/Timer.h"
#include "./src/IdWorker.h"
using namespace SeasSnowFlake::Src;
// using namespace clickhouse;
using namespace std;
zend_class_entry *SeasSnowflake_ce;
#ifdef COMPILE_DL_SEASSNOWFLAKE
extern "C" {
ZEND_GET_MODULE(SeasSnowflake)
}
#endif
PHP_FUNCTION(SeasSnowflake_version)
{
SC_RETURN_STRINGL(PHP_SEASSNOWFLAKE_VERSION, strlen(PHP_SEASSNOWFLAKE_VERSION));
}
static PHP_METHOD(SEASSNOWFLAKE_RES_NAME, __construct);
static PHP_METHOD(SEASSNOWFLAKE_RES_NAME, degenerate);
static PHP_METHOD(SEASSNOWFLAKE_RES_NAME, generate);
ZEND_BEGIN_ARG_INFO_EX(SeasSnowflake_construct, 0, 0, 1)
ZEND_ARG_INFO(0, parames)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(SeasSnowflake_generate, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(SeasSnowflake_degenerate, 0, 0, 1)
ZEND_ARG_INFO(0, id)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(seasSnowflake_get_version_arginfo, 0, 0, 0)
ZEND_END_ARG_INFO()
/* */
const zend_function_entry SeasSnowflake_functions[] =
{
PHP_FE(SeasSnowflake_version, seasSnowflake_get_version_arginfo)
PHP_FE_END
};
/* }}} */
const zend_function_entry SeasSnowflake_methods[] =
{
PHP_ME(SEASSNOWFLAKE_RES_NAME, __construct, SeasSnowflake_construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(SEASSNOWFLAKE_RES_NAME, generate, SeasSnowflake_generate, ZEND_ACC_PUBLIC )
PHP_ME(SEASSNOWFLAKE_RES_NAME, degenerate, SeasSnowflake_degenerate, ZEND_ACC_PUBLIC)
PHP_FE_END
};
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(SeasSnowflake)
{
zend_class_entry SeasSnowflake;
INIT_CLASS_ENTRY(SeasSnowflake, SEASSNOWFLAKE_RES_NAME, SeasSnowflake_methods);
#if PHP_VERSION_ID >= 70000
SeasSnowflake_ce = zend_register_internal_class_ex(&SeasSnowflake, NULL);
zend_declare_property_null(SeasSnowflake_ce, "worker_id", sizeof("worker_id")-1, ZEND_ACC_PUBLIC);
zend_declare_property_null(SeasSnowflake_ce, "datacenter_id", sizeof("datacenter_id")-1, ZEND_ACC_PUBLIC);
#else
SeasSnowflake_ce = zend_register_internal_class_ex(&SeasSnowflake, NULL, NULL TSRMLS_CC);
#endif
SeasSnowflake_ce->ce_flags |= ZEND_ACC_FINAL;
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(SeasSnowflake)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(SeasSnowflake)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(SeasSnowflake)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(SeasSnowflake)
{
php_info_print_table_start();
php_info_print_table_header(2, "SeasSnowflake support", "enabled");
php_info_print_table_row(2, "Version", PHP_SEASSNOWFLAKE_VERSION);
php_info_print_table_row(2, "Author", "SeasX Group[email: [email protected]]");
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* }}} */
/* {{{ SeasSnowflake_module_entry
*/
zend_module_entry SeasSnowflake_module_entry =
{
STANDARD_MODULE_HEADER,
SEASSNOWFLAKE_RES_NAME,
SeasSnowflake_functions,
PHP_MINIT(SeasSnowflake),
PHP_MSHUTDOWN(SeasSnowflake),
PHP_RINIT(SeasSnowflake),
PHP_RSHUTDOWN(SeasSnowflake),
PHP_MINFO(SeasSnowflake),
PHP_SEASSNOWFLAKE_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
PHP_METHOD(SEASSNOWFLAKE_RES_NAME, __construct)
{
zval *parames;
#ifndef FAST_ZPP
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", ¶mes) == FAILURE)
{
return;
}
#else
#undef IS_UNDEF
#define IS_UNDEF Z_EXPECTED_LONG
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ARRAY(parames)
ZEND_PARSE_PARAMETERS_END();
#undef IS_UNDEF
#define IS_UNDEF 0
#endif
HashTable *_ht = Z_ARRVAL_P(parames);
zval *value;
zval *this_obj;
this_obj = getThis();
try
{
#if PHP_VERSION_ID < 80000
if (php_array_get_value(_ht, "worker_id", value))
{
convert_to_long(value);
zend_update_property_long(SeasSnowflake_ce, this_obj, "worker_id", sizeof("worker_id") - 1, Z_LVAL_P(value) TSRMLS_CC);
}
if (php_array_get_value(_ht, "datacenter_id", value))
{
convert_to_long(value);
zend_update_property_long(SeasSnowflake_ce, this_obj, "datacenter_id", sizeof("datacenter_id") - 1, Z_LVAL_P(value) TSRMLS_CC);
}
#else
if (php_array_get_value(_ht, "worker_id", value))
{
convert_to_long(value);
zend_object *workerObject;
workerObject=Z_OBJ_P(this_obj);
zend_update_property_long(SeasSnowflake_ce, workerObject, "worker_id", sizeof("worker_id") - 1, Z_LVAL_P(value) );
}
if (php_array_get_value(_ht, "datacenter_id", value))
{
convert_to_long(value);
zend_object *datecenterObject;
datecenterObject=Z_OBJ_P(this_obj);
zend_update_property_long(SeasSnowflake_ce, datecenterObject, "datacenter_id", sizeof("datacenter_id") - 1, Z_LVAL_P(value) );
}
#endif
}
catch (const std::exception& e)
{
#if PHP_VERSION_ID < 80000
sc_zend_throw_exception(NULL, e.what(), 0 TSRMLS_CC);
#else
sc_zend_throw_exception(NULL, e.what(), 0 );
#endif
}
RETURN_TRUE;
}
/* }}} */
/*
* create a id
*/
PHP_METHOD(SEASSNOWFLAKE_RES_NAME, generate)
{
auto &idWorker = Singleton<IdWorker>::instance();
zval *this_obj;
this_obj = getThis();
zval *worker_id = sc_zend_read_property(SeasSnowflake_ce, this_obj, "worker_id", sizeof("worker_id") - 1, 0);
zval *datacenter_id = sc_zend_read_property(SeasSnowflake_ce, this_obj, "datacenter_id", sizeof("datacenter_id") - 1, 0);
if (zend_parse_parameters_none() == FAILURE)
{
return;
}
idWorker.setDatacenterId(Z_LVAL_P(worker_id));
idWorker.setWorkerId(Z_LVAL_P(datacenter_id));
try{
uint64_t id= idWorker.generate();
RETVAL_LONG(id);
}catch (const std::exception& e){
#if PHP_VERSION_ID < 80000
sc_zend_throw_exception(NULL, e.what(), 0 TSRMLS_CC);
#else
sc_zend_throw_exception(NULL, e.what(), 0 );
#endif
}
return;
}
/*
*
* degenerate(string id)
*/
PHP_METHOD(SEASSNOWFLAKE_RES_NAME, degenerate)
{
char *id = NULL;
#if PHP_VERSION_ID < 70000
int l_id = 0;
#else
size_t l_id = 0;
#endif
// zval* params = NULL;
auto &idWorker = Singleton<IdWorker>::instance();
zval *this_obj;
this_obj = getThis();
zval *worker_id = sc_zend_read_property(SeasSnowflake_ce, this_obj, "worker_id", sizeof("worker_id") - 1, 0);
zval *datacenter_id = sc_zend_read_property(SeasSnowflake_ce, this_obj, "datacenter_id", sizeof("datacenter_id") - 1, 0);
idWorker.setDatacenterId(Z_LVAL_P(worker_id));
idWorker.setWorkerId(Z_LVAL_P(datacenter_id));
#ifndef FAST_ZPP
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &id, &l_id) == FAILURE)
{
return;
}
#else
#undef IS_UNDEF
#define IS_UNDEF Z_EXPECTED_LONG
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(id, l_id)
Z_PARAM_OPTIONAL
// Z_PARAM_ARRAY(params)
ZEND_PARSE_PARAMETERS_END();
#undef IS_UNDEF
#define IS_UNDEF 0
#endif
try
{
uint64_t id_i = idWorker.stringToUINT64(id);
uint64_t interval=idWorker.degenerateInterval(id_i);
uint workerId=idWorker.degenerateWorkerId(id_i);
uint sequence= idWorker.degenerateSequence(id_i);
uint dataCenterId=idWorker.degenerateDataCenterId(id_i);
uint64_t beginTimestamp=idWorker.degenerateBeginTimestamp(id_i);
uint64_t timestamp= idWorker.degenerateTimestamp(id_i);
zval arr, *parr = &arr;
array_init(&arr);
add_assoc_long_ex(&arr, "worker_id", strlen("worker_id"), workerId);
add_assoc_long_ex(&arr, "datacenter_id", strlen("datacenter_id"), dataCenterId);
add_assoc_long_ex(&arr, "sequence", strlen("sequence"), sequence);
add_assoc_long_ex(&arr, "timestamp", strlen("timestamp"), timestamp);
add_assoc_long_ex(&arr, "begin_timestamp", strlen("begin_timestamp"), beginTimestamp);
add_assoc_long_ex(&arr, "interval", strlen("interval"), interval);
RETURN_ZVAL(parr, 0, 1);
} catch (const std::exception& e){
#if PHP_VERSION_ID < 80000
sc_zend_throw_exception(NULL, e.what(), 0 TSRMLS_CC);
#else
sc_zend_throw_exception(NULL, e.what(), 0 );
#endif
}
return ;
}
/* }}} */