From 324d6db66fbe0bd9bd2af543b1772ebbc0db292b Mon Sep 17 00:00:00 2001 From: Yuqing Date: Sun, 10 Apr 2016 19:01:47 +0800 Subject: [PATCH] php extension support id generator --- HISTORY | 1 + php-fastcommon/fastcommon.c | 120 +++++++++++++++++++++++++- php-fastcommon/fastcommon.h | 4 + php-fastcommon/php-fastcommon.spec.in | 6 +- php-fastcommon/test.php | 9 ++ src/id_generator.h | 28 +++--- src/tests/test_id_generator.c | 5 +- 7 files changed, 155 insertions(+), 18 deletions(-) diff --git a/HISTORY b/HISTORY index 9c55e67..df7199c 100644 --- a/HISTORY +++ b/HISTORY @@ -4,6 +4,7 @@ Version 1.27 2016-04-10 * php-fastcommon.spec.in support PHP 7 * add file lock and unlock functions * add id generator for multi processes + * php extension support id generator Version 1.26 2016-03-16 * add logger parameter: compress_log_days_before diff --git a/php-fastcommon/fastcommon.c b/php-fastcommon/fastcommon.c index b95c8af..e6bd6df 100644 --- a/php-fastcommon/fastcommon.c +++ b/php-fastcommon/fastcommon.c @@ -7,16 +7,22 @@ #include #include #include +#include "common_define.h" #include "local_ip_func.h" #include "logger.h" #include "hash.h" #include "sockopt.h" #include "shared_func.h" +#include "id_generator.h" #include "fastcommon.h" #define MAJOR_VERSION 1 #define MINOR_VERSION 0 -#define PATCH_VERSION 3 +#define PATCH_VERSION 5 + +#define DEFAULT_SN_FILENAME "/tmp/fastcommon_id_generator.sn" + +static struct idg_context idg_context = {-1, 0}; #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 }; @@ -36,6 +42,9 @@ const zend_fcall_info empty_fcall_info = { 0, NULL, NULL, NULL, NULL, 0, NULL, N ZEND_FE(fastcommon_get_first_local_ip, NULL) ZEND_FE(fastcommon_get_next_local_ip, NULL) ZEND_FE(fastcommon_is_private_ip, NULL) + ZEND_FE(fastcommon_id_generator_init, NULL) + ZEND_FE(fastcommon_id_generator_next, NULL) + ZEND_FE(fastcommon_id_generator_destroy, NULL) {NULL, NULL, NULL} /* Must be the last line */ }; @@ -334,7 +343,7 @@ return true for private ip, otherwise false */ ZEND_FUNCTION(fastcommon_is_private_ip) { - int argc; + int argc; zend_size_t ip_len; char *ip; @@ -357,3 +366,110 @@ ZEND_FUNCTION(fastcommon_is_private_ip) RETURN_BOOL(is_private_ip(ip)); } +/* +bool fastcommon_id_generator_init([string filename = "/tmp/fastcommon_id_generator.sn", + int machine_id = 0, int mid_bits = 16]) +return true for success, false for fail +*/ +ZEND_FUNCTION(fastcommon_id_generator_init) +{ + int argc; + zend_size_t filename_len; + long machine_id; + long mid_bits; + char *filename; + + argc = ZEND_NUM_ARGS(); + if (argc > 3) { + logError("file: "__FILE__", line: %d, " + "fastcommon_id_generator_init parameters count: %d is invalid", + __LINE__, argc); + RETURN_BOOL(false); + } + + filename = DEFAULT_SN_FILENAME; + machine_id = 0; + mid_bits = 16; + if (zend_parse_parameters(argc TSRMLS_CC, "|sll", &filename, + &filename_len, &machine_id, &mid_bits) == FAILURE) + { + logError("file: "__FILE__", line: %d, " + "zend_parse_parameters fail!", __LINE__); + RETURN_BOOL(false); + } + + if (idg_context.fd >= 0) { + logError("file: "__FILE__", line: %d, " + "already inited!", __LINE__); + RETURN_BOOL(false); + } + + if (id_generator_init_ex(&idg_context, filename, + machine_id, mid_bits) != 0) + { + RETURN_BOOL(false); + } + + RETURN_BOOL(true); +} + +/* +long/string fastcommon_id_generator_next() +return id for success, false for fail +return long in 64 bits OS, return string in 32 bits Os +*/ +ZEND_FUNCTION(fastcommon_id_generator_next) +{ + int argc; + int64_t id; + + argc = ZEND_NUM_ARGS(); + if (argc != 0) { + logError("file: "__FILE__", line: %d, " + "fastcommon_id_generator_next parameters count: %d is invalid", + __LINE__, argc); + RETURN_BOOL(false); + } + + if (idg_context.fd < 0) { + if (id_generator_init(&idg_context, DEFAULT_SN_FILENAME) != 0) { + RETURN_BOOL(false); + } + } + + if (id_generator_next(&idg_context, &id) != 0) { + RETURN_BOOL(false); + } + +#if OS_BITS == 64 + RETURN_LONG(id); +#else + { + char buff[32]; + int len; + len = sprintf(buff, "%"PRId64, id); + ZEND_RETURN_STRINGL(buff, len, 1); + } +#endif +} + +/* +bool fastcommon_id_generator_destroy() +return true for success, false for fail +*/ +ZEND_FUNCTION(fastcommon_id_generator_destroy) +{ + int argc; + + argc = ZEND_NUM_ARGS(); + if (argc != 0) { + logError("file: "__FILE__", line: %d, " + "fastcommon_id_generator_destroy parameters count: %d is invalid", + __LINE__, argc); + RETURN_BOOL(false); + } + + id_generator_destroy(&idg_context); + RETURN_BOOL(true); +} + diff --git a/php-fastcommon/fastcommon.h b/php-fastcommon/fastcommon.h index 8458c3d..46cd248 100644 --- a/php-fastcommon/fastcommon.h +++ b/php-fastcommon/fastcommon.h @@ -26,6 +26,10 @@ ZEND_FUNCTION(fastcommon_get_first_local_ip); ZEND_FUNCTION(fastcommon_get_next_local_ip); ZEND_FUNCTION(fastcommon_is_private_ip); +ZEND_FUNCTION(fastcommon_id_generator_init); +ZEND_FUNCTION(fastcommon_id_generator_next); +ZEND_FUNCTION(fastcommon_id_generator_destroy); + #ifdef __cplusplus } #endif diff --git a/php-fastcommon/php-fastcommon.spec.in b/php-fastcommon/php-fastcommon.spec.in index 65f5a34..9ea2da8 100644 --- a/php-fastcommon/php-fastcommon.spec.in +++ b/php-fastcommon/php-fastcommon.spec.in @@ -1,7 +1,7 @@ %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) Name: php-fastcommon -Version: 1.0.4 +Version: 1.0.5 Release: 1%{?dist} Summary: The php extension for libfastcommon 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) -BuildRequires: libfastcommon-devel >= 1.0.22 -Requires: libfastcommon >= 1.0.22 +BuildRequires: libfastcommon-devel >= 1.0.27 +Requires: libfastcommon >= 1.0.27 %description This package provides the php extension for libfastcommon diff --git a/php-fastcommon/test.php b/php-fastcommon/test.php index 6e2dae3..35fbd62 100644 --- a/php-fastcommon/test.php +++ b/php-fastcommon/test.php @@ -15,3 +15,12 @@ while (($next_ip=fastcommon_get_next_local_ip($next_ip))) $is_private_ip = fastcommon_is_private_ip($next_ip); echo "local ip: $next_ip, private: $is_private_ip\n"; } + +fastcommon_id_generator_init("/tmp/sn.txt"); + +for ($i=0; $i<10; $i++) { + echo fastcommon_id_generator_next() . "\n"; +} + +fastcommon_id_generator_destroy(); + diff --git a/src/id_generator.h b/src/id_generator.h index 706aa74..79c9d57 100644 --- a/src/id_generator.h +++ b/src/id_generator.h @@ -35,17 +35,6 @@ struct idg_context { int64_t sn_mask; }; -/** -* init function - set mid_bits to 16 - set machine_id to 2 bytes of local ip address -* parameter: -* context: the id generator context -* filename: the filename to store id -* return error no, 0 for success, none zero for fail -*/ -int id_generator_init(struct idg_context *context, const char *filename); - /** * init function * parameter: @@ -58,6 +47,23 @@ int id_generator_init(struct idg_context *context, const char *filename); int id_generator_init_ex(struct idg_context *context, const char *filename, const int machine_id, const int mid_bits); +/** +* init function + set mid_bits to 16 + set machine_id to 2 bytes of local ip address +* parameter: +* context: the id generator context +* filename: the filename to store id +* return error no, 0 for success, none zero for fail +*/ +static inline int id_generator_init(struct idg_context *context, const char *filename) +{ + const int machine_id = 0; + const int mid_bits = 16; + return id_generator_init_ex(context, filename, machine_id, mid_bits); +} + + /** * init function * parameter: diff --git a/src/tests/test_id_generator.c b/src/tests/test_id_generator.c index 000d2cd..02b7e33 100644 --- a/src/tests/test_id_generator.c +++ b/src/tests/test_id_generator.c @@ -20,12 +20,13 @@ int main(int argc, char *argv[]) int result; int i; int64_t id; - const int machine_id = 0; + const int machine_id = 192; const int mid_bits = 8; log_init(); g_log_context.log_level = LOG_DEBUG; + //result = id_generator_init(&context, "/tmp/sn.txt"); result = id_generator_init_ex(&context, "/tmp/sn.txt", machine_id, mid_bits); if (result != 0) @@ -35,7 +36,7 @@ int main(int argc, char *argv[]) //id_generator_next(&context, &id); //printf("id: %"PRId64", %016llX\n", id, id); - for (i=0; i<10; i++) + for (i=0; i<100000; i++) { result = id_generator_next(&context, &id); if (result != 0)