id generator use last handle as default
parent
299ac49055
commit
34f4423ef5
|
|
@ -29,7 +29,7 @@ typedef struct {
|
||||||
|
|
||||||
static int le_consumer;
|
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)
|
#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 };
|
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;
|
PHPIDGContext *php_idg_context = (PHPIDGContext *)rsrc->ptr;
|
||||||
id_generator_destroy(&php_idg_context->idg_context);
|
id_generator_destroy(&php_idg_context->idg_context);
|
||||||
|
if (last_idg_context == php_idg_context)
|
||||||
|
{
|
||||||
|
last_idg_context = NULL;
|
||||||
|
}
|
||||||
efree(php_idg_context);
|
efree(php_idg_context);
|
||||||
rsrc->ptr = NULL;
|
rsrc->ptr = NULL;
|
||||||
}
|
}
|
||||||
|
|
@ -88,6 +92,10 @@ ZEND_RSRC_DTOR_FUNC(id_generator_dtor)
|
||||||
{
|
{
|
||||||
PHPIDGContext *php_idg_context = (PHPIDGContext *)res->ptr;
|
PHPIDGContext *php_idg_context = (PHPIDGContext *)res->ptr;
|
||||||
id_generator_destroy(&php_idg_context->idg_context);
|
id_generator_destroy(&php_idg_context->idg_context);
|
||||||
|
if (last_idg_context == php_idg_context)
|
||||||
|
{
|
||||||
|
last_idg_context = NULL;
|
||||||
|
}
|
||||||
efree(php_idg_context);
|
efree(php_idg_context);
|
||||||
res->ptr = NULL;
|
res->ptr = NULL;
|
||||||
}
|
}
|
||||||
|
|
@ -451,6 +459,7 @@ ZEND_FUNCTION(fastcommon_id_generator_init)
|
||||||
RETURN_BOOL(false);
|
RETURN_BOOL(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
last_idg_context = php_idg_context;
|
||||||
ZEND_REGISTER_RESOURCE(return_value, php_idg_context, le_consumer);
|
ZEND_REGISTER_RESOURCE(return_value, php_idg_context, le_consumer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -492,12 +501,13 @@ ZEND_FUNCTION(fastcommon_id_generator_next)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
context = &idg_context;
|
if (last_idg_context == NULL) {
|
||||||
if (context->fd < 0) {
|
logError("file: "__FILE__", line: %d, "
|
||||||
if (id_generator_init(context, DEFAULT_SN_FILENAME) != 0) {
|
"must call fastcommon_id_generator_init first", __LINE__);
|
||||||
RETURN_BOOL(false);
|
RETURN_BOOL(false);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
context = &last_idg_context->idg_context;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (id_generator_next_extra(context, extra, &id) != 0) {
|
if (id_generator_next_extra(context, extra, &id) != 0) {
|
||||||
|
|
@ -552,7 +562,12 @@ ZEND_FUNCTION(fastcommon_id_generator_get_extra)
|
||||||
}
|
}
|
||||||
else
|
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) {
|
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
|
return true for success, false for fail
|
||||||
*/
|
*/
|
||||||
ZEND_FUNCTION(fastcommon_id_generator_destroy)
|
ZEND_FUNCTION(fastcommon_id_generator_destroy)
|
||||||
|
|
@ -599,7 +614,13 @@ ZEND_FUNCTION(fastcommon_id_generator_destroy)
|
||||||
}
|
}
|
||||||
else
|
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);
|
id_generator_destroy(context);
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,11 @@ while (($next_ip=fastcommon_get_next_local_ip($next_ip)))
|
||||||
echo "local ip: $next_ip, private: $is_private_ip\n";
|
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);
|
$handle1 = fastcommon_id_generator_init("/tmp/sn1.txt", 0, 16, 0, 16);
|
||||||
$handle2 = fastcommon_id_generator_init("/tmp/sn2.txt", 0, 8, 8, 16);
|
$handle2 = fastcommon_id_generator_init("/tmp/sn2.txt", 0, 8, 8, 16);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue