php extension persistent for idg
parent
742206ea41
commit
b7d21b57b2
|
|
@ -29,13 +29,23 @@
|
||||||
#define DEFAULT_SN_FILENAME "/tmp/fastcommon_id_generator.sn"
|
#define DEFAULT_SN_FILENAME "/tmp/fastcommon_id_generator.sn"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
struct idg_context idg_context;
|
struct idg_context *idg_context;
|
||||||
int flags;
|
int flags;
|
||||||
} PHPIDGContext;
|
} PHPIDGContext;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char filename[MAX_PATH_SIZE];
|
||||||
|
int machine_id;
|
||||||
|
int mid_bits;
|
||||||
|
int extra_bits;
|
||||||
|
int sn_bits;
|
||||||
|
int flags;
|
||||||
|
} PHPIDGKeyInfo;
|
||||||
|
|
||||||
static int le_consumer;
|
static int le_consumer;
|
||||||
|
|
||||||
static PHPIDGContext *last_idg_context = NULL;
|
static PHPIDGContext *last_idg_context = NULL;
|
||||||
|
static HashArray idg_htable;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int alloc;
|
int alloc;
|
||||||
|
|
@ -111,32 +121,25 @@ zend_module_entry fastcommon_module_entry = {
|
||||||
|
|
||||||
ZEND_RSRC_DTOR_FUNC(id_generator_dtor)
|
ZEND_RSRC_DTOR_FUNC(id_generator_dtor)
|
||||||
{
|
{
|
||||||
|
PHPIDGContext *php_idg_context = NULL;
|
||||||
#if PHP_MAJOR_VERSION < 7
|
#if PHP_MAJOR_VERSION < 7
|
||||||
if (rsrc->ptr != NULL)
|
if (rsrc->ptr != NULL) {
|
||||||
{
|
php_idg_context = (PHPIDGContext *)rsrc->ptr;
|
||||||
PHPIDGContext *php_idg_context = (PHPIDGContext *)rsrc->ptr;
|
|
||||||
id_generator_destroy(&php_idg_context->idg_context);
|
|
||||||
if (last_idg_context == php_idg_context)
|
|
||||||
{
|
|
||||||
last_idg_context = NULL;
|
|
||||||
}
|
|
||||||
efree(php_idg_context);
|
|
||||||
rsrc->ptr = NULL;
|
rsrc->ptr = NULL;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
if (res->ptr != NULL)
|
if (res->ptr != NULL) {
|
||||||
{
|
php_idg_context = (PHPIDGContext *)res->ptr;
|
||||||
PHPIDGContext *php_idg_context = (PHPIDGContext *)res->ptr;
|
|
||||||
id_generator_destroy(&php_idg_context->idg_context);
|
|
||||||
if (last_idg_context == php_idg_context)
|
|
||||||
{
|
|
||||||
last_idg_context = NULL;
|
|
||||||
}
|
|
||||||
efree(php_idg_context);
|
|
||||||
res->ptr = NULL;
|
res->ptr = NULL;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
if (php_idg_context != NULL) {
|
||||||
|
if (last_idg_context == php_idg_context) {
|
||||||
|
last_idg_context = NULL;
|
||||||
|
}
|
||||||
|
efree(php_idg_context);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#define FASTCOMMON_REGISTER_CHAR_STR_CONSTANT(key, c, buff) \
|
#define FASTCOMMON_REGISTER_CHAR_STR_CONSTANT(key, c, buff) \
|
||||||
|
|
@ -150,6 +153,9 @@ PHP_MINIT_FUNCTION(fastcommon)
|
||||||
log_try_init();
|
log_try_init();
|
||||||
le_consumer = zend_register_list_destructors_ex(id_generator_dtor, NULL,
|
le_consumer = zend_register_list_destructors_ex(id_generator_dtor, NULL,
|
||||||
PHP_IDG_RESOURCE_NAME, module_number);
|
PHP_IDG_RESOURCE_NAME, module_number);
|
||||||
|
if (hash_init(&idg_htable, simple_hash, 64, 0.75) != 0) {
|
||||||
|
return FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
memset(buff, 0, sizeof(buff));
|
memset(buff, 0, sizeof(buff));
|
||||||
FASTCOMMON_REGISTER_CHAR_STR_CONSTANT("FASTCOMMON_LOG_TIME_PRECISION_SECOND",
|
FASTCOMMON_REGISTER_CHAR_STR_CONSTANT("FASTCOMMON_LOG_TIME_PRECISION_SECOND",
|
||||||
|
|
@ -483,6 +489,43 @@ ZEND_FUNCTION(fastcommon_is_private_ip)
|
||||||
RETURN_BOOL(is_private_ip(ip));
|
RETURN_BOOL(is_private_ip(ip));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static struct idg_context *get_idg_context(const char *filename,
|
||||||
|
const int machine_id, const int mid_bits, const int extra_bits,
|
||||||
|
const int sn_bits, const int mode)
|
||||||
|
{
|
||||||
|
PHPIDGKeyInfo key_info;
|
||||||
|
struct idg_context *idg_context;
|
||||||
|
|
||||||
|
memset(&key_info, 0, sizeof(key_info));
|
||||||
|
snprintf(key_info.filename, sizeof(key_info.filename), "%s", filename);
|
||||||
|
key_info.machine_id = machine_id;
|
||||||
|
key_info.mid_bits = mid_bits;
|
||||||
|
key_info.extra_bits = extra_bits;
|
||||||
|
key_info.sn_bits = sn_bits;
|
||||||
|
|
||||||
|
idg_context = (struct idg_context *)hash_find(&idg_htable,
|
||||||
|
&key_info, sizeof(key_info));
|
||||||
|
if (idg_context == NULL) {
|
||||||
|
idg_context = (struct idg_context *)malloc(sizeof(struct idg_context));
|
||||||
|
if (idg_context == NULL) {
|
||||||
|
logError("file: "__FILE__", line: %d, "
|
||||||
|
"malloc %d bytes fail!", __LINE__,
|
||||||
|
(int)sizeof(struct idg_context));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (id_generator_init_extra_ex(idg_context, filename,
|
||||||
|
machine_id, mid_bits, extra_bits, sn_bits, mode) != 0)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
hash_insert_ex(&idg_htable, &key_info, sizeof(key_info),
|
||||||
|
idg_context, 0, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
return idg_context;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
resource fastcommon_id_generator_init([string filename = "/tmp/fastcommon_id_generator.sn",
|
resource fastcommon_id_generator_init([string filename = "/tmp/fastcommon_id_generator.sn",
|
||||||
int machine_id = 0, int mid_bits = 16, int extra_bits = 0, int sn_bits = 16,
|
int machine_id = 0, int mid_bits = 16, int extra_bits = 0, int sn_bits = 16,
|
||||||
|
|
@ -502,44 +545,44 @@ ZEND_FUNCTION(fastcommon_id_generator_init)
|
||||||
char *filename;
|
char *filename;
|
||||||
PHPIDGContext *php_idg_context;
|
PHPIDGContext *php_idg_context;
|
||||||
|
|
||||||
argc = ZEND_NUM_ARGS();
|
argc = ZEND_NUM_ARGS();
|
||||||
if (argc > 7) {
|
if (argc > 7) {
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
"fastcommon_id_generator_init parameters count: %d is invalid",
|
"fastcommon_id_generator_init parameters count: %d is invalid",
|
||||||
__LINE__, argc);
|
__LINE__, argc);
|
||||||
RETURN_BOOL(false);
|
RETURN_BOOL(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
filename = DEFAULT_SN_FILENAME;
|
filename = DEFAULT_SN_FILENAME;
|
||||||
filename_len = 0;
|
filename_len = 0;
|
||||||
machine_id = 0;
|
machine_id = 0;
|
||||||
mid_bits = 16;
|
mid_bits = 16;
|
||||||
extra_bits = 0;
|
extra_bits = 0;
|
||||||
sn_bits = 16;
|
sn_bits = 16;
|
||||||
mode = ID_GENERATOR_DEFAULT_FILE_MODE;
|
mode = ID_GENERATOR_DEFAULT_FILE_MODE;
|
||||||
flags = 0;
|
flags = 0;
|
||||||
if (zend_parse_parameters(argc TSRMLS_CC, "|sllllll", &filename,
|
if (zend_parse_parameters(argc TSRMLS_CC, "|sllllll", &filename,
|
||||||
&filename_len, &machine_id, &mid_bits, &extra_bits,
|
&filename_len, &machine_id, &mid_bits, &extra_bits,
|
||||||
&sn_bits, &mode, &flags) == FAILURE)
|
&sn_bits, &mode, &flags) == FAILURE)
|
||||||
{
|
|
||||||
logError("file: "__FILE__", line: %d, "
|
|
||||||
"zend_parse_parameters fail!", __LINE__);
|
|
||||||
RETURN_BOOL(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
php_idg_context = (PHPIDGContext *)emalloc(sizeof(PHPIDGContext));
|
|
||||||
if (php_idg_context == NULL)
|
|
||||||
{
|
{
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
"emalloc %d bytes fail!", __LINE__, (int)sizeof(PHPIDGContext));
|
"zend_parse_parameters fail!", __LINE__);
|
||||||
RETURN_BOOL(false);
|
RETURN_BOOL(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (id_generator_init_extra_ex(&php_idg_context->idg_context, filename,
|
php_idg_context = (PHPIDGContext *)emalloc(sizeof(PHPIDGContext));
|
||||||
machine_id, mid_bits, extra_bits, sn_bits, mode) != 0)
|
if (php_idg_context == NULL) {
|
||||||
{
|
logError("file: "__FILE__", line: %d, "
|
||||||
RETURN_BOOL(false);
|
"emalloc %d bytes fail!", __LINE__,
|
||||||
}
|
(int)sizeof(PHPIDGContext));
|
||||||
|
RETURN_BOOL(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((php_idg_context->idg_context=get_idg_context(filename, machine_id,
|
||||||
|
mid_bits, extra_bits, sn_bits, mode)) == NULL)
|
||||||
|
{
|
||||||
|
RETURN_BOOL(false);
|
||||||
|
}
|
||||||
|
|
||||||
php_idg_context->flags = flags;
|
php_idg_context->flags = flags;
|
||||||
last_idg_context = php_idg_context;
|
last_idg_context = php_idg_context;
|
||||||
|
|
@ -577,13 +620,10 @@ ZEND_FUNCTION(fastcommon_id_generator_next)
|
||||||
RETURN_BOOL(false);
|
RETURN_BOOL(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (zhandle != NULL && !ZVAL_IS_NULL(zhandle))
|
if (zhandle != NULL && !ZVAL_IS_NULL(zhandle)) {
|
||||||
{
|
|
||||||
ZEND_FETCH_RESOURCE(php_idg_context, PHPIDGContext *, &zhandle, -1,
|
ZEND_FETCH_RESOURCE(php_idg_context, PHPIDGContext *, &zhandle, -1,
|
||||||
PHP_IDG_RESOURCE_NAME, le_consumer);
|
PHP_IDG_RESOURCE_NAME, le_consumer);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
if (last_idg_context == NULL) {
|
if (last_idg_context == NULL) {
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
"must call fastcommon_id_generator_init first", __LINE__);
|
"must call fastcommon_id_generator_init first", __LINE__);
|
||||||
|
|
@ -600,7 +640,7 @@ ZEND_FUNCTION(fastcommon_id_generator_next)
|
||||||
extra_ptr = &extra_val;
|
extra_ptr = &extra_val;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (id_generator_next_extra_ptr(&php_idg_context->idg_context,
|
if (id_generator_next_extra_ptr(php_idg_context->idg_context,
|
||||||
extra_ptr, &id) != 0)
|
extra_ptr, &id) != 0)
|
||||||
{
|
{
|
||||||
RETURN_BOOL(false);
|
RETURN_BOOL(false);
|
||||||
|
|
@ -628,7 +668,6 @@ ZEND_FUNCTION(fastcommon_id_generator_get_extra)
|
||||||
long id;
|
long id;
|
||||||
zval *zhandle;
|
zval *zhandle;
|
||||||
PHPIDGContext *php_idg_context;
|
PHPIDGContext *php_idg_context;
|
||||||
struct idg_context *context;
|
|
||||||
|
|
||||||
argc = ZEND_NUM_ARGS();
|
argc = ZEND_NUM_ARGS();
|
||||||
if (argc > 2) {
|
if (argc > 2) {
|
||||||
|
|
@ -646,29 +685,25 @@ ZEND_FUNCTION(fastcommon_id_generator_get_extra)
|
||||||
RETURN_BOOL(false);
|
RETURN_BOOL(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (zhandle != NULL && !ZVAL_IS_NULL(zhandle))
|
if (zhandle != NULL && !ZVAL_IS_NULL(zhandle)) {
|
||||||
{
|
|
||||||
ZEND_FETCH_RESOURCE(php_idg_context, PHPIDGContext *, &zhandle, -1,
|
ZEND_FETCH_RESOURCE(php_idg_context, PHPIDGContext *, &zhandle, -1,
|
||||||
PHP_IDG_RESOURCE_NAME, le_consumer);
|
PHP_IDG_RESOURCE_NAME, le_consumer);
|
||||||
context = &php_idg_context->idg_context;
|
} else {
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (last_idg_context == NULL) {
|
if (last_idg_context == NULL) {
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
"must call fastcommon_id_generator_init first", __LINE__);
|
"must call fastcommon_id_generator_init first", __LINE__);
|
||||||
RETURN_BOOL(false);
|
RETURN_BOOL(false);
|
||||||
}
|
}
|
||||||
context = &last_idg_context->idg_context;
|
php_idg_context = last_idg_context;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (context->fd < 0) {
|
if (php_idg_context->idg_context->fd < 0) {
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
"must call fastcommon_id_generator_init first", __LINE__);
|
"must call fastcommon_id_generator_init first", __LINE__);
|
||||||
RETURN_BOOL(false);
|
RETURN_BOOL(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
RETURN_LONG(id_generator_get_extra(context, id));
|
RETURN_LONG(id_generator_get_extra(php_idg_context->idg_context, id));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -680,7 +715,6 @@ ZEND_FUNCTION(fastcommon_id_generator_destroy)
|
||||||
int argc;
|
int argc;
|
||||||
zval *zhandle;
|
zval *zhandle;
|
||||||
PHPIDGContext *php_idg_context;
|
PHPIDGContext *php_idg_context;
|
||||||
struct idg_context *context;
|
|
||||||
|
|
||||||
argc = ZEND_NUM_ARGS();
|
argc = ZEND_NUM_ARGS();
|
||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
|
|
@ -698,24 +732,18 @@ ZEND_FUNCTION(fastcommon_id_generator_destroy)
|
||||||
RETURN_BOOL(false);
|
RETURN_BOOL(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (zhandle != NULL && !ZVAL_IS_NULL(zhandle))
|
if (zhandle != NULL && !ZVAL_IS_NULL(zhandle)) {
|
||||||
{
|
|
||||||
ZEND_FETCH_RESOURCE(php_idg_context, PHPIDGContext *, &zhandle, -1,
|
ZEND_FETCH_RESOURCE(php_idg_context, PHPIDGContext *, &zhandle, -1,
|
||||||
PHP_IDG_RESOURCE_NAME, le_consumer);
|
PHP_IDG_RESOURCE_NAME, le_consumer);
|
||||||
context = &php_idg_context->idg_context;
|
} else {
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (last_idg_context == NULL) {
|
if (last_idg_context == NULL) {
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
"must call fastcommon_id_generator_init first", __LINE__);
|
"must call fastcommon_id_generator_init first", __LINE__);
|
||||||
RETURN_BOOL(false);
|
RETURN_BOOL(false);
|
||||||
}
|
}
|
||||||
context = &last_idg_context->idg_context;
|
|
||||||
last_idg_context = NULL;
|
last_idg_context = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
id_generator_destroy(context);
|
|
||||||
RETURN_BOOL(true);
|
RETURN_BOOL(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -729,7 +757,6 @@ ZEND_FUNCTION(fastcommon_id_generator_get_timestamp)
|
||||||
long id;
|
long id;
|
||||||
zval *zhandle;
|
zval *zhandle;
|
||||||
PHPIDGContext *php_idg_context;
|
PHPIDGContext *php_idg_context;
|
||||||
struct idg_context *context;
|
|
||||||
|
|
||||||
argc = ZEND_NUM_ARGS();
|
argc = ZEND_NUM_ARGS();
|
||||||
if (argc > 2) {
|
if (argc > 2) {
|
||||||
|
|
@ -747,29 +774,25 @@ ZEND_FUNCTION(fastcommon_id_generator_get_timestamp)
|
||||||
RETURN_BOOL(false);
|
RETURN_BOOL(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (zhandle != NULL && !ZVAL_IS_NULL(zhandle))
|
if (zhandle != NULL && !ZVAL_IS_NULL(zhandle)) {
|
||||||
{
|
|
||||||
ZEND_FETCH_RESOURCE(php_idg_context, PHPIDGContext *, &zhandle, -1,
|
ZEND_FETCH_RESOURCE(php_idg_context, PHPIDGContext *, &zhandle, -1,
|
||||||
PHP_IDG_RESOURCE_NAME, le_consumer);
|
PHP_IDG_RESOURCE_NAME, le_consumer);
|
||||||
context = &php_idg_context->idg_context;
|
} else {
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (last_idg_context == NULL) {
|
if (last_idg_context == NULL) {
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
"must call fastcommon_id_generator_init first", __LINE__);
|
"must call fastcommon_id_generator_init first", __LINE__);
|
||||||
RETURN_BOOL(false);
|
RETURN_BOOL(false);
|
||||||
}
|
}
|
||||||
context = &last_idg_context->idg_context;
|
php_idg_context = last_idg_context;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (context->fd < 0) {
|
if (php_idg_context->idg_context->fd < 0) {
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
"must call fastcommon_id_generator_init first", __LINE__);
|
"must call fastcommon_id_generator_init first", __LINE__);
|
||||||
RETURN_BOOL(false);
|
RETURN_BOOL(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
RETURN_LONG(id_generator_get_timestamp(context, id));
|
RETURN_LONG(id_generator_get_timestamp(php_idg_context->idg_context, id));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
||||||
|
|
@ -26,11 +26,11 @@ unset($handle);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
resource fastcommon_id_generator_init([string filename = "/tmp/fastcommon_id_generator.sn",
|
resource fastcommon_id_generator_init([string filename = "/tmp/fastcommon_id_generator.sn",
|
||||||
int machine_id = 0, int mid_bits = 16, int extra_bits = 0, int sn_bits = 16])
|
int machine_id = 0, int mid_bits = 16, int extra_bits = 0, int sn_bits = 16, int flags = 0])
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$id = 6301319781687017475;
|
$id = 6301319781687017475;
|
||||||
$handle1 = fastcommon_id_generator_init("/tmp/sn1.txt", 0, 8, 10, 14, 0775);
|
$handle1 = fastcommon_id_generator_init("/tmp/sn1.txt", 0, 8, 10, 14, 0666);
|
||||||
echo 'extra no: ' . fastcommon_id_generator_get_extra($id, $handle1) . "\n";
|
echo 'extra no: ' . fastcommon_id_generator_get_extra($id, $handle1) . "\n";
|
||||||
|
|
||||||
$handle2 = fastcommon_id_generator_init("/tmp/sn2.txt", 0, 8, 8, 16);
|
$handle2 = fastcommon_id_generator_init("/tmp/sn2.txt", 0, 8, 8, 16);
|
||||||
|
|
@ -58,10 +58,6 @@ for ($i=0; $i<10; $i++) {
|
||||||
fastcommon_id_generator_get_timestamp($id, $handle));
|
fastcommon_id_generator_get_timestamp($id, $handle));
|
||||||
}
|
}
|
||||||
|
|
||||||
fastcommon_id_generator_destroy($handle);
|
|
||||||
fastcommon_id_generator_destroy($handle1);
|
|
||||||
fastcommon_id_generator_destroy($handle2);
|
|
||||||
|
|
||||||
fastcommon_error_log("this is a test\n", 3, "/tmp/test.log");
|
fastcommon_error_log("this is a test\n", 3, "/tmp/test.log");
|
||||||
fastcommon_error_log("this is a test11\n", 3, "/tmp/test1.log", FASTCOMMON_LOG_TIME_PRECISION_MSECOND);
|
fastcommon_error_log("this is a test11\n", 3, "/tmp/test1.log", FASTCOMMON_LOG_TIME_PRECISION_MSECOND);
|
||||||
fastcommon_error_log("this is a test12\n", 3, "/tmp/test1.log", FASTCOMMON_LOG_TIME_PRECISION_MSECOND);
|
fastcommon_error_log("this is a test12\n", 3, "/tmp/test1.log", FASTCOMMON_LOG_TIME_PRECISION_MSECOND);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue