php extension persistent for idg
parent
742206ea41
commit
b7d21b57b2
|
|
@ -29,13 +29,23 @@
|
|||
#define DEFAULT_SN_FILENAME "/tmp/fastcommon_id_generator.sn"
|
||||
|
||||
typedef struct {
|
||||
struct idg_context idg_context;
|
||||
struct idg_context *idg_context;
|
||||
int flags;
|
||||
} 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 PHPIDGContext *last_idg_context = NULL;
|
||||
static HashArray idg_htable;
|
||||
|
||||
typedef struct {
|
||||
int alloc;
|
||||
|
|
@ -111,32 +121,25 @@ zend_module_entry fastcommon_module_entry = {
|
|||
|
||||
ZEND_RSRC_DTOR_FUNC(id_generator_dtor)
|
||||
{
|
||||
PHPIDGContext *php_idg_context = NULL;
|
||||
#if PHP_MAJOR_VERSION < 7
|
||||
if (rsrc->ptr != NULL)
|
||||
{
|
||||
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);
|
||||
if (rsrc->ptr != NULL) {
|
||||
php_idg_context = (PHPIDGContext *)rsrc->ptr;
|
||||
rsrc->ptr = NULL;
|
||||
}
|
||||
#else
|
||||
if (res->ptr != NULL)
|
||||
{
|
||||
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);
|
||||
if (res->ptr != NULL) {
|
||||
php_idg_context = (PHPIDGContext *)res->ptr;
|
||||
res->ptr = NULL;
|
||||
}
|
||||
#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) \
|
||||
|
|
@ -150,6 +153,9 @@ PHP_MINIT_FUNCTION(fastcommon)
|
|||
log_try_init();
|
||||
le_consumer = zend_register_list_destructors_ex(id_generator_dtor, NULL,
|
||||
PHP_IDG_RESOURCE_NAME, module_number);
|
||||
if (hash_init(&idg_htable, simple_hash, 64, 0.75) != 0) {
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
memset(buff, 0, sizeof(buff));
|
||||
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));
|
||||
}
|
||||
|
||||
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",
|
||||
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;
|
||||
PHPIDGContext *php_idg_context;
|
||||
|
||||
argc = ZEND_NUM_ARGS();
|
||||
if (argc > 7) {
|
||||
logError("file: "__FILE__", line: %d, "
|
||||
"fastcommon_id_generator_init parameters count: %d is invalid",
|
||||
__LINE__, argc);
|
||||
RETURN_BOOL(false);
|
||||
}
|
||||
argc = ZEND_NUM_ARGS();
|
||||
if (argc > 7) {
|
||||
logError("file: "__FILE__", line: %d, "
|
||||
"fastcommon_id_generator_init parameters count: %d is invalid",
|
||||
__LINE__, argc);
|
||||
RETURN_BOOL(false);
|
||||
}
|
||||
|
||||
filename = DEFAULT_SN_FILENAME;
|
||||
filename = DEFAULT_SN_FILENAME;
|
||||
filename_len = 0;
|
||||
machine_id = 0;
|
||||
mid_bits = 16;
|
||||
machine_id = 0;
|
||||
mid_bits = 16;
|
||||
extra_bits = 0;
|
||||
sn_bits = 16;
|
||||
mode = ID_GENERATOR_DEFAULT_FILE_MODE;
|
||||
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,
|
||||
&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, "
|
||||
"emalloc %d bytes fail!", __LINE__, (int)sizeof(PHPIDGContext));
|
||||
RETURN_BOOL(false);
|
||||
logError("file: "__FILE__", line: %d, "
|
||||
"zend_parse_parameters fail!", __LINE__);
|
||||
RETURN_BOOL(false);
|
||||
}
|
||||
|
||||
if (id_generator_init_extra_ex(&php_idg_context->idg_context, filename,
|
||||
machine_id, mid_bits, extra_bits, sn_bits, mode) != 0)
|
||||
{
|
||||
RETURN_BOOL(false);
|
||||
}
|
||||
php_idg_context = (PHPIDGContext *)emalloc(sizeof(PHPIDGContext));
|
||||
if (php_idg_context == NULL) {
|
||||
logError("file: "__FILE__", line: %d, "
|
||||
"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;
|
||||
last_idg_context = php_idg_context;
|
||||
|
|
@ -577,13 +620,10 @@ ZEND_FUNCTION(fastcommon_id_generator_next)
|
|||
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,
|
||||
PHP_IDG_RESOURCE_NAME, le_consumer);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
if (last_idg_context == NULL) {
|
||||
logError("file: "__FILE__", line: %d, "
|
||||
"must call fastcommon_id_generator_init first", __LINE__);
|
||||
|
|
@ -600,7 +640,7 @@ ZEND_FUNCTION(fastcommon_id_generator_next)
|
|||
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)
|
||||
{
|
||||
RETURN_BOOL(false);
|
||||
|
|
@ -628,7 +668,6 @@ ZEND_FUNCTION(fastcommon_id_generator_get_extra)
|
|||
long id;
|
||||
zval *zhandle;
|
||||
PHPIDGContext *php_idg_context;
|
||||
struct idg_context *context;
|
||||
|
||||
argc = ZEND_NUM_ARGS();
|
||||
if (argc > 2) {
|
||||
|
|
@ -646,29 +685,25 @@ ZEND_FUNCTION(fastcommon_id_generator_get_extra)
|
|||
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,
|
||||
PHP_IDG_RESOURCE_NAME, le_consumer);
|
||||
context = &php_idg_context->idg_context;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
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;
|
||||
php_idg_context = last_idg_context;
|
||||
}
|
||||
|
||||
if (context->fd < 0) {
|
||||
if (php_idg_context->idg_context->fd < 0) {
|
||||
logError("file: "__FILE__", line: %d, "
|
||||
"must call fastcommon_id_generator_init first", __LINE__);
|
||||
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;
|
||||
zval *zhandle;
|
||||
PHPIDGContext *php_idg_context;
|
||||
struct idg_context *context;
|
||||
|
||||
argc = ZEND_NUM_ARGS();
|
||||
if (argc > 1) {
|
||||
|
|
@ -698,24 +732,18 @@ ZEND_FUNCTION(fastcommon_id_generator_destroy)
|
|||
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,
|
||||
PHP_IDG_RESOURCE_NAME, le_consumer);
|
||||
context = &php_idg_context->idg_context;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
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);
|
||||
RETURN_BOOL(true);
|
||||
}
|
||||
|
||||
|
|
@ -729,7 +757,6 @@ ZEND_FUNCTION(fastcommon_id_generator_get_timestamp)
|
|||
long id;
|
||||
zval *zhandle;
|
||||
PHPIDGContext *php_idg_context;
|
||||
struct idg_context *context;
|
||||
|
||||
argc = ZEND_NUM_ARGS();
|
||||
if (argc > 2) {
|
||||
|
|
@ -747,29 +774,25 @@ ZEND_FUNCTION(fastcommon_id_generator_get_timestamp)
|
|||
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,
|
||||
PHP_IDG_RESOURCE_NAME, le_consumer);
|
||||
context = &php_idg_context->idg_context;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
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;
|
||||
php_idg_context = last_idg_context;
|
||||
}
|
||||
|
||||
if (context->fd < 0) {
|
||||
if (php_idg_context->idg_context->fd < 0) {
|
||||
logError("file: "__FILE__", line: %d, "
|
||||
"must call fastcommon_id_generator_init first", __LINE__);
|
||||
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",
|
||||
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;
|
||||
$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";
|
||||
|
||||
$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_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 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);
|
||||
|
|
|
|||
Loading…
Reference in New Issue