From 34f4423ef50b309908188f23fe630428ca4771a8 Mon Sep 17 00:00:00 2001 From: yuqing Date: Thu, 19 May 2016 20:03:26 +0800 Subject: [PATCH] id generator use last handle as default --- php-fastcommon/fastcommon.c | 39 ++++++++++++++++++++++++++++--------- php-fastcommon/test.php | 6 +++++- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/php-fastcommon/fastcommon.c b/php-fastcommon/fastcommon.c index 0a44c37..9f40be2 100644 --- a/php-fastcommon/fastcommon.c +++ b/php-fastcommon/fastcommon.c @@ -29,7 +29,7 @@ typedef struct { static int le_consumer; -static struct idg_context idg_context = {-1, 0}; +static PHPIDGContext *last_idg_context = NULL; #if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 3) const zend_fcall_info empty_fcall_info = { 0, NULL, NULL, NULL, NULL, 0, NULL, NULL, 0 }; @@ -80,6 +80,10 @@ ZEND_RSRC_DTOR_FUNC(id_generator_dtor) { 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; } @@ -88,6 +92,10 @@ ZEND_RSRC_DTOR_FUNC(id_generator_dtor) { 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; } @@ -451,6 +459,7 @@ ZEND_FUNCTION(fastcommon_id_generator_init) RETURN_BOOL(false); } + last_idg_context = php_idg_context; ZEND_REGISTER_RESOURCE(return_value, php_idg_context, le_consumer); } @@ -492,12 +501,13 @@ ZEND_FUNCTION(fastcommon_id_generator_next) } else { - context = &idg_context; - if (context->fd < 0) { - if (id_generator_init(context, DEFAULT_SN_FILENAME) != 0) { - RETURN_BOOL(false); - } + if (last_idg_context == NULL) { + logError("file: "__FILE__", line: %d, " + "must call fastcommon_id_generator_init first", __LINE__); + RETURN_BOOL(false); } + + context = &last_idg_context->idg_context; } if (id_generator_next_extra(context, extra, &id) != 0) { @@ -552,7 +562,12 @@ ZEND_FUNCTION(fastcommon_id_generator_get_extra) } else { - context = &idg_context; + if (last_idg_context == NULL) { + logError("file: "__FILE__", line: %d, " + "must call fastcommon_id_generator_init first", __LINE__); + RETURN_BOOL(false); + } + context = &last_idg_context->idg_context; } if (context->fd < 0) { @@ -565,7 +580,7 @@ ZEND_FUNCTION(fastcommon_id_generator_get_extra) } /* -bool fastcommon_id_generator_destroy([$handle = NULL]) +bool fastcommon_id_generator_destroy([resource handle = NULL]) return true for success, false for fail */ ZEND_FUNCTION(fastcommon_id_generator_destroy) @@ -599,7 +614,13 @@ ZEND_FUNCTION(fastcommon_id_generator_destroy) } else { - context = &idg_context; + if (last_idg_context == NULL) { + logError("file: "__FILE__", line: %d, " + "must call fastcommon_id_generator_init first", __LINE__); + RETURN_BOOL(false); + } + context = &last_idg_context->idg_context; + last_idg_context = NULL; } id_generator_destroy(context); diff --git a/php-fastcommon/test.php b/php-fastcommon/test.php index 46f508a..782580f 100644 --- a/php-fastcommon/test.php +++ b/php-fastcommon/test.php @@ -16,7 +16,11 @@ while (($next_ip=fastcommon_get_next_local_ip($next_ip))) echo "local ip: $next_ip, private: $is_private_ip\n"; } -//fastcommon_id_generator_init(); +$handle = fastcommon_id_generator_init(); + +$id = fastcommon_id_generator_next(1); +printf("id1: %d %X, extra: %d\n", $id, $id, fastcommon_id_generator_get_extra($id)); +unset($handle); $handle1 = fastcommon_id_generator_init("/tmp/sn1.txt", 0, 16, 0, 16); $handle2 = fastcommon_id_generator_init("/tmp/sn2.txt", 0, 8, 8, 16);