add function id_generator_next_extra_ptr

pull/37/head
YuQing 2018-11-09 22:07:21 +08:00
parent 32c1445d41
commit f6609c7612
5 changed files with 80 additions and 21 deletions

View File

@ -1,5 +1,5 @@
Version 1.40 2018-10-29
Version 1.40 2018-11-09
* add function conn_pool_parse_server_info and conn_pool_load_server_info
* support directive: #@add_annotation, for example:
#@add_annotation CONFIG_GET /usr/lib/libshmcache.so /etc/libshmcache.conf
@ -10,6 +10,8 @@ Version 1.40 2018-10-29
* add function format_http_date
* add function hash_find1 and hash_find2
* add function resolve_path and fast_buffer_append_file
* add function id_generator_next_extra_ptr, with NULL pointer to set
extra data to sn % (1 << extra_bits)
Version 1.39 2018-07-31
* add #@function REPLACE_VARS

View File

@ -23,11 +23,14 @@
#define MINOR_VERSION 0
#define PATCH_VERSION 8
#define IDG_FLAGS_EXTRA_DATA_BY_MOD 1
#define PHP_IDG_RESOURCE_NAME "fastcommon_idg"
#define DEFAULT_SN_FILENAME "/tmp/fastcommon_id_generator.sn"
typedef struct {
struct idg_context idg_context;
int flags;
} PHPIDGContext;
static int le_consumer;
@ -144,7 +147,7 @@ PHP_MINIT_FUNCTION(fastcommon)
{
static char buff[16];
log_init();
log_try_init();
le_consumer = zend_register_list_destructors_ex(id_generator_dtor, NULL,
PHP_IDG_RESOURCE_NAME, module_number);
@ -158,6 +161,9 @@ PHP_MINIT_FUNCTION(fastcommon)
FASTCOMMON_REGISTER_CHAR_STR_CONSTANT("FASTCOMMON_LOG_TIME_PRECISION_NONE",
LOG_TIME_PRECISION_NONE, buff + 6);
REGISTER_LONG_CONSTANT("FASTCOMMON_IDG_FLAGS_EXTRA_DATA_BY_MOD",
IDG_FLAGS_EXTRA_DATA_BY_MOD, CONST_CS | CONST_PERSISTENT);
return SUCCESS;
}
@ -479,7 +485,8 @@ ZEND_FUNCTION(fastcommon_is_private_ip)
/*
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 mode = 0644])
int machine_id = 0, int mid_bits = 16, int extra_bits = 0, int sn_bits = 16,
int mode = 0644, int flags = 0])
return resource handle for success, false for fail
*/
ZEND_FUNCTION(fastcommon_id_generator_init)
@ -491,11 +498,12 @@ ZEND_FUNCTION(fastcommon_id_generator_init)
long extra_bits;
long sn_bits;
long mode;
long flags;
char *filename;
PHPIDGContext *php_idg_context;
argc = ZEND_NUM_ARGS();
if (argc > 6) {
if (argc > 7) {
logError("file: "__FILE__", line: %d, "
"fastcommon_id_generator_init parameters count: %d is invalid",
__LINE__, argc);
@ -509,9 +517,10 @@ ZEND_FUNCTION(fastcommon_id_generator_init)
extra_bits = 0;
sn_bits = 16;
mode = ID_GENERATOR_DEFAULT_FILE_MODE;
if (zend_parse_parameters(argc TSRMLS_CC, "|slllll", &filename,
flags = 0;
if (zend_parse_parameters(argc TSRMLS_CC, "|sllllll", &filename,
&filename_len, &machine_id, &mid_bits, &extra_bits,
&sn_bits, &mode) == FAILURE)
&sn_bits, &mode, &flags) == FAILURE)
{
logError("file: "__FILE__", line: %d, "
"zend_parse_parameters fail!", __LINE__);
@ -532,6 +541,7 @@ ZEND_FUNCTION(fastcommon_id_generator_init)
RETURN_BOOL(false);
}
php_idg_context->flags = flags;
last_idg_context = php_idg_context;
ZEND_REGISTER_RESOURCE(return_value, php_idg_context, le_consumer);
}
@ -545,10 +555,11 @@ ZEND_FUNCTION(fastcommon_id_generator_next)
{
int argc;
long extra;
int extra_val;
int *extra_ptr;
int64_t id;
zval *zhandle;
PHPIDGContext *php_idg_context;
struct idg_context *context;
argc = ZEND_NUM_ARGS();
if (argc > 2) {
@ -570,7 +581,6 @@ ZEND_FUNCTION(fastcommon_id_generator_next)
{
ZEND_FETCH_RESOURCE(php_idg_context, PHPIDGContext *, &zhandle, -1,
PHP_IDG_RESOURCE_NAME, le_consumer);
context = &php_idg_context->idg_context;
}
else
{
@ -580,12 +590,22 @@ ZEND_FUNCTION(fastcommon_id_generator_next)
RETURN_BOOL(false);
}
context = &last_idg_context->idg_context;
php_idg_context = last_idg_context;
}
if (id_generator_next_extra(context, extra, &id) != 0) {
RETURN_BOOL(false);
}
logInfo("flags: %d", php_idg_context->flags);
if ((php_idg_context->flags & IDG_FLAGS_EXTRA_DATA_BY_MOD)) {
extra_ptr = NULL;
} else {
extra_val = extra;
extra_ptr = &extra_val;
}
if (id_generator_next_extra_ptr(&php_idg_context->idg_context,
extra_ptr, &id) != 0)
{
RETURN_BOOL(false);
}
#if OS_BITS == 64
RETURN_LONG(id);

View File

@ -182,12 +182,13 @@ void id_generator_destroy(struct idg_context *context)
}
}
int id_generator_next_extra(struct idg_context *context, const int extra,
int64_t *id)
int id_generator_next_extra_ptr(struct idg_context *context,
const int *extra, int64_t *id)
{
int result;
int len;
int bytes;
int new_extra;
int64_t sn;
char buff[32];
char *endptr;
@ -250,9 +251,18 @@ int id_generator_next_extra(struct idg_context *context, const int extra,
file_unlock(context->fd);
if (extra == NULL)
{
new_extra = sn % (1 << context->extra_bits);
}
else
{
new_extra = *extra;
}
*id = (((int64_t)time(NULL)) << context->mes_bits_sum) |
context->masked_mid | ((extra << context->sn_bits) & context->extra_mask) |
context->masked_mid |
((new_extra << context->sn_bits) & context->extra_mask) |
(sn & context->sn_mask);
return result;
}

View File

@ -122,15 +122,42 @@ static inline int id_generator_init(struct idg_context *context, const char *fil
void id_generator_destroy(struct idg_context *context);
/**
* generate next id ex
* generate next id with extra pointer
* parameter:
* context: the id generator context
* extra: the extra data pointer, NULL for set extra data to sn % (1 << extra_bits)
* id: store the id
* return error no, 0 for success, none zero for fail
*/
int id_generator_next_extra_ptr(struct idg_context *context,
const int *extra, int64_t *id);
/**
* generate next id with extra data
* parameter:
* context: the id generator context
* extra: the extra data
* id: store the id
* return error no, 0 for success, none zero for fail
*/
int id_generator_next_extra(struct idg_context *context, const int extra,
int64_t *id);
static inline int id_generator_next_extra(struct idg_context *context,
const int extra, int64_t *id)
{
return id_generator_next_extra_ptr(context, &extra, id);
}
/**
* generate next id, set extra data to sn % (1 << extra_bits)
* parameter:
* context: the id generator context
* id: store the id
* return error no, 0 for success, none zero for fail
*/
static inline int id_generator_next_extra_by_mod(struct idg_context *context,
int64_t *id)
{
return id_generator_next_extra_ptr(context, NULL, id);
}
/**
* generate next id
@ -141,7 +168,8 @@ int id_generator_next_extra(struct idg_context *context, const int extra,
*/
static inline int id_generator_next(struct idg_context *context, int64_t *id)
{
return id_generator_next_extra(context, 0, id);
const int extra = 0;
return id_generator_next_extra_ptr(context, &extra, id);
}
/**

View File

@ -49,4 +49,3 @@ int main(int argc, char *argv[])
id_generator_destroy(&context);
return 0;
}