php extension support id generator
parent
9ddd1b8f03
commit
324d6db66f
1
HISTORY
1
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
|
||||
|
|
|
|||
|
|
@ -7,16 +7,22 @@
|
|||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
#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 */
|
||||
};
|
||||
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in New Issue