add function id_generator_get_timestamp
parent
962773d029
commit
1c2b72f9a6
3
HISTORY
3
HISTORY
|
|
@ -1,7 +1,8 @@
|
||||||
|
|
||||||
Version 1.28 2016-06-15
|
Version 1.29 2016-06-17
|
||||||
* ini_file_reader support #@if
|
* ini_file_reader support #@if
|
||||||
* ini_file_reader support #@for
|
* ini_file_reader support #@for
|
||||||
|
* add function id_generator_get_timestamp
|
||||||
|
|
||||||
Version 1.28 2016-06-08
|
Version 1.28 2016-06-08
|
||||||
* id generator support extra bits
|
* id generator support extra bits
|
||||||
|
|
|
||||||
|
|
@ -52,6 +52,7 @@ const zend_fcall_info empty_fcall_info = { 0, NULL, NULL, NULL, NULL, 0, NULL, N
|
||||||
ZEND_FE(fastcommon_id_generator_init, NULL)
|
ZEND_FE(fastcommon_id_generator_init, NULL)
|
||||||
ZEND_FE(fastcommon_id_generator_next, NULL)
|
ZEND_FE(fastcommon_id_generator_next, NULL)
|
||||||
ZEND_FE(fastcommon_id_generator_get_extra, NULL)
|
ZEND_FE(fastcommon_id_generator_get_extra, NULL)
|
||||||
|
ZEND_FE(fastcommon_id_generator_get_timestamp, NULL)
|
||||||
ZEND_FE(fastcommon_id_generator_destroy, NULL)
|
ZEND_FE(fastcommon_id_generator_destroy, NULL)
|
||||||
{NULL, NULL, NULL} /* Must be the last line */
|
{NULL, NULL, NULL} /* Must be the last line */
|
||||||
};
|
};
|
||||||
|
|
@ -627,3 +628,56 @@ ZEND_FUNCTION(fastcommon_id_generator_destroy)
|
||||||
RETURN_BOOL(true);
|
RETURN_BOOL(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
long fastcommon_id_generator_get_timestamp(long id [, $handle = NULL])
|
||||||
|
return the timestamp
|
||||||
|
*/
|
||||||
|
ZEND_FUNCTION(fastcommon_id_generator_get_timestamp)
|
||||||
|
{
|
||||||
|
int argc;
|
||||||
|
long id;
|
||||||
|
zval *zhandle;
|
||||||
|
PHPIDGContext *php_idg_context;
|
||||||
|
struct idg_context *context;
|
||||||
|
|
||||||
|
argc = ZEND_NUM_ARGS();
|
||||||
|
if (argc > 2) {
|
||||||
|
logError("file: "__FILE__", line: %d, "
|
||||||
|
"fastcommon_id_generator_get_timestamp parameters count: %d is invalid",
|
||||||
|
__LINE__, argc);
|
||||||
|
RETURN_BOOL(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
zhandle = NULL;
|
||||||
|
if (zend_parse_parameters(argc TSRMLS_CC, "l|z", &id, &zhandle) == FAILURE)
|
||||||
|
{
|
||||||
|
logError("file: "__FILE__", line: %d, "
|
||||||
|
"zend_parse_parameters fail!", __LINE__);
|
||||||
|
RETURN_BOOL(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
{
|
||||||
|
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) {
|
||||||
|
logError("file: "__FILE__", line: %d, "
|
||||||
|
"must call fastcommon_id_generator_init first", __LINE__);
|
||||||
|
RETURN_BOOL(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
RETURN_LONG(id_generator_get_timestamp(context, id));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ ZEND_FUNCTION(fastcommon_is_private_ip);
|
||||||
ZEND_FUNCTION(fastcommon_id_generator_init);
|
ZEND_FUNCTION(fastcommon_id_generator_init);
|
||||||
ZEND_FUNCTION(fastcommon_id_generator_next);
|
ZEND_FUNCTION(fastcommon_id_generator_next);
|
||||||
ZEND_FUNCTION(fastcommon_id_generator_get_extra);
|
ZEND_FUNCTION(fastcommon_id_generator_get_extra);
|
||||||
|
ZEND_FUNCTION(fastcommon_id_generator_get_timestamp);
|
||||||
ZEND_FUNCTION(fastcommon_id_generator_destroy);
|
ZEND_FUNCTION(fastcommon_id_generator_destroy);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
%define php_inidir %(php --ini | head -n 1 | awk -F ':' '{print $2;}' | sed 's/ //g')
|
%define php_inidir %(php --ini | head -n 1 | awk -F ':' '{print $2;}' | sed 's/ //g')
|
||||||
%define php_extdir %(php-config --extension-dir 2>/dev/null)
|
%define php_extdir %(php-config --extension-dir 2>/dev/null)
|
||||||
Name: php-fastcommon
|
Name: php-fastcommon
|
||||||
Version: 1.0.7
|
Version: 1.0.8
|
||||||
Release: 1%{?dist}
|
Release: 1%{?dist}
|
||||||
Summary: The php extension for libfastcommon
|
Summary: The php extension for libfastcommon
|
||||||
License: GPL
|
License: GPL
|
||||||
|
|
@ -11,8 +11,8 @@ Source: http://perso.orange.fr/sebastien.godard/%{name}-%{version}.tar.gz
|
||||||
|
|
||||||
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
|
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
|
||||||
|
|
||||||
BuildRequires: libfastcommon-devel >= 1.0.28
|
BuildRequires: libfastcommon-devel >= 1.0.29
|
||||||
Requires: libfastcommon >= 1.0.28
|
Requires: libfastcommon >= 1.0.29
|
||||||
|
|
||||||
%description
|
%description
|
||||||
This package provides the php extension for libfastcommon
|
This package provides the php extension for libfastcommon
|
||||||
|
|
|
||||||
|
|
@ -19,25 +19,35 @@ while (($next_ip=fastcommon_get_next_local_ip($next_ip)))
|
||||||
$handle = fastcommon_id_generator_init();
|
$handle = fastcommon_id_generator_init();
|
||||||
|
|
||||||
$id = fastcommon_id_generator_next(1);
|
$id = fastcommon_id_generator_next(1);
|
||||||
printf("id: %d %X, extra: %d\n", $id, $id, fastcommon_id_generator_get_extra($id));
|
printf("id: %d %X, extra: %d, timestamp: %d\n", $id, $id,
|
||||||
|
fastcommon_id_generator_get_extra($id),
|
||||||
|
fastcommon_id_generator_get_timestamp($id));
|
||||||
unset($handle);
|
unset($handle);
|
||||||
|
|
||||||
$handle1 = fastcommon_id_generator_init("/tmp/sn1.txt", 0, 8, 10, 14);
|
$handle1 = fastcommon_id_generator_init("/tmp/sn1.txt", 0, 8, 10, 14);
|
||||||
$handle2 = fastcommon_id_generator_init("/tmp/sn2.txt", 0, 8, 8, 16);
|
$handle2 = fastcommon_id_generator_init("/tmp/sn2.txt", 0, 8, 8, 16);
|
||||||
|
|
||||||
$id = fastcommon_id_generator_next(1, $handle1);
|
$id = fastcommon_id_generator_next(1, $handle1);
|
||||||
printf("id1: %d %X, extra: %d\n", $id, $id, fastcommon_id_generator_get_extra($id, $handle1));
|
printf("id1: %d %X, extra: %d, timestamp: %d\n", $id, $id,
|
||||||
|
fastcommon_id_generator_get_extra($id, $handle1),
|
||||||
|
fastcommon_id_generator_get_timestamp($id, $handle1));
|
||||||
|
|
||||||
$id = fastcommon_id_generator_next(2, $handle2);
|
$id = fastcommon_id_generator_next(2, $handle2);
|
||||||
printf("id2: %d %X, extra: %d\n", $id, $id, fastcommon_id_generator_get_extra($id, $handle2));
|
printf("id2: %d %X, extra: %d, timestamp: %d\n", $id, $id,
|
||||||
|
fastcommon_id_generator_get_extra($id, $handle2),
|
||||||
|
fastcommon_id_generator_get_timestamp($id, $handle2));
|
||||||
|
|
||||||
$handle = fastcommon_id_generator_init("/tmp/sn.txt", 0, 8, 10, 14);
|
$handle = fastcommon_id_generator_init("/tmp/sn.txt", 0, 8, 10, 14);
|
||||||
$id = fastcommon_id_generator_next(512, $handle);
|
$id = fastcommon_id_generator_next(512, $handle);
|
||||||
printf("%d %X, extra: %d\n", $id, $id, fastcommon_id_generator_get_extra($id, $handle));
|
printf("%d %X, extra: %d, timestamp: %d\n", $id, $id,
|
||||||
|
fastcommon_id_generator_get_extra($id, $handle),
|
||||||
|
fastcommon_id_generator_get_timestamp($id, $handle));
|
||||||
|
|
||||||
for ($i=0; $i<10; $i++) {
|
for ($i=0; $i<10; $i++) {
|
||||||
$id = fastcommon_id_generator_next($i, $handle);
|
$id = fastcommon_id_generator_next($i, $handle);
|
||||||
printf("%d %X, extra: %d\n", $id, $id, fastcommon_id_generator_get_extra($id, $handle));
|
printf("%d %X, extra: %d, timestamp: %d\n", $id, $id,
|
||||||
|
fastcommon_id_generator_get_extra($id, $handle),
|
||||||
|
fastcommon_id_generator_get_timestamp($id, $handle));
|
||||||
}
|
}
|
||||||
|
|
||||||
fastcommon_id_generator_destroy($handle);
|
fastcommon_id_generator_destroy($handle);
|
||||||
|
|
|
||||||
|
|
@ -132,11 +132,25 @@ static inline int id_generator_next(struct idg_context *context, int64_t *id)
|
||||||
* id: the id
|
* id: the id
|
||||||
* return the extra data
|
* return the extra data
|
||||||
*/
|
*/
|
||||||
static inline int id_generator_get_extra(struct idg_context *context, const int64_t id)
|
static inline int id_generator_get_extra(struct idg_context *context,
|
||||||
|
const int64_t id)
|
||||||
{
|
{
|
||||||
return (int)((id & context->extra_mask) >> context->sn_bits);
|
return (int)((id & context->extra_mask) >> context->sn_bits);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get timestamp from id
|
||||||
|
* parameter:
|
||||||
|
* context: the id generator context
|
||||||
|
* id: the id
|
||||||
|
* return the timestamp
|
||||||
|
*/
|
||||||
|
static inline long id_generator_get_timestamp(struct idg_context *context,
|
||||||
|
const int64_t id)
|
||||||
|
{
|
||||||
|
return (long)(id >> context->mes_bits_sum);
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue